diff --git a/internal/epub/epub.go b/internal/epub/epub.go index 10247a0..7c8c0a7 100644 --- a/internal/epub/epub.go +++ b/internal/epub/epub.go @@ -2,8 +2,6 @@ package epub import ( "fmt" - "image" - "image/draw" "os" "path/filepath" "regexp" @@ -12,12 +10,7 @@ import ( "text/template" "time" - "github.com/disintegration/gift" "github.com/gofrs/uuid" - "github.com/golang/freetype" - "github.com/golang/freetype/truetype" - "golang.org/x/image/font" - "golang.org/x/image/font/gofont/gomonobold" ) type ImageOptions struct { @@ -129,66 +122,6 @@ func (e *ePub) writeBlank(wz *epubZip, img *Image) error { ) } -func (e ePub) getTitleImageData(title string, img *Image, currentPart, totalPart int) *ImageData { - // Create a blur version of the cover - g := gift.New(gift.GaussianBlur(8)) - dst := image.NewGray(g.Bounds(img.Raw.Bounds())) - g.Draw(dst, img.Raw) - - // Calculate size of title - f, _ := truetype.Parse(gomonobold.TTF) - borderSize := 4 - var fontSize, textWidth, textHeight int - for fontSize = 64; fontSize >= 12; fontSize -= 1 { - face := truetype.NewFace(f, &truetype.Options{Size: float64(fontSize), DPI: 72}) - textWidth = font.MeasureString(face, title).Ceil() - textHeight = face.Metrics().Ascent.Ceil() + face.Metrics().Descent.Ceil() - if textWidth+2*borderSize < img.Width && 3*textHeight+2*borderSize < img.Height { - break - } - } - - // Draw rectangle in the middle of the image - textPosStart := img.Height/2 - textHeight/2 - textPosEnd := img.Height/2 + textHeight/2 - marginSize := fontSize - borderArea := image.Rect(0, textPosStart-borderSize-marginSize, img.Width, textPosEnd+borderSize+marginSize) - textArea := image.Rect(borderSize, textPosStart-marginSize, img.Width-borderSize, textPosEnd+marginSize) - - draw.Draw( - dst, - borderArea, - image.Black, - image.Point{}, - draw.Over, - ) - - draw.Draw( - dst, - textArea, - image.White, - image.Point{}, - draw.Over, - ) - - // Draw text - c := freetype.NewContext() - c.SetDPI(72) - c.SetFontSize(float64(fontSize)) - c.SetFont(f) - c.SetClip(textArea) - c.SetDst(dst) - c.SetSrc(image.Black) - - textLeft := img.Width/2 - textWidth/2 - if textLeft < borderSize { - textLeft = borderSize - } - c.DrawString(title, freetype.Pt(textLeft, img.Height/2+textHeight/4)) - - return newData("OEBPS/Images/title.jpg", dst, e.Quality) -} - func (e *ePub) getParts() ([]*epubPart, error) { images, err := e.LoadImages() @@ -332,7 +265,7 @@ func (e *ePub) Write() error { return err } } - if err := wz.WriteImage(e.getTitleImageData(title, part.Cover, i+1, totalParts)); err != nil { + if err := wz.WriteImage(e.createTitleImageDate(title, part.Cover, i+1, totalParts)); err != nil { return err } diff --git a/internal/epub/epub_image_processing.go b/internal/epub/epub_image_processing.go index d368735..f77b017 100644 --- a/internal/epub/epub_image_processing.go +++ b/internal/epub/epub_image_processing.go @@ -6,6 +6,7 @@ import ( "fmt" "image" "image/color" + "image/draw" _ "image/jpeg" _ "image/png" "io" @@ -18,9 +19,13 @@ import ( "github.com/celogeek/go-comic-converter/v2/internal/epub/sortpath" "github.com/disintegration/gift" + "github.com/golang/freetype" + "github.com/golang/freetype/truetype" "github.com/nwaples/rardecode" pdfimage "github.com/raff/pdfreader/image" "github.com/raff/pdfreader/pdfread" + "golang.org/x/image/font" + "golang.org/x/image/font/gofont/gomonobold" "golang.org/x/image/tiff" _ "golang.org/x/image/webp" ) @@ -513,3 +518,63 @@ func loadPdf(input string) (int, chan *imageTask, error) { return nbPages, output, nil } + +func (e *ePub) createTitleImageDate(title string, img *Image, currentPart, totalPart int) *ImageData { + // Create a blur version of the cover + g := gift.New(gift.GaussianBlur(8)) + dst := image.NewGray(g.Bounds(img.Raw.Bounds())) + g.Draw(dst, img.Raw) + + // Calculate size of title + f, _ := truetype.Parse(gomonobold.TTF) + borderSize := 4 + var fontSize, textWidth, textHeight int + for fontSize = 64; fontSize >= 12; fontSize -= 1 { + face := truetype.NewFace(f, &truetype.Options{Size: float64(fontSize), DPI: 72}) + textWidth = font.MeasureString(face, title).Ceil() + textHeight = face.Metrics().Ascent.Ceil() + face.Metrics().Descent.Ceil() + if textWidth+2*borderSize < img.Width && 3*textHeight+2*borderSize < img.Height { + break + } + } + + // Draw rectangle in the middle of the image + textPosStart := img.Height/2 - textHeight/2 + textPosEnd := img.Height/2 + textHeight/2 + marginSize := fontSize + borderArea := image.Rect(0, textPosStart-borderSize-marginSize, img.Width, textPosEnd+borderSize+marginSize) + textArea := image.Rect(borderSize, textPosStart-marginSize, img.Width-borderSize, textPosEnd+marginSize) + + draw.Draw( + dst, + borderArea, + image.Black, + image.Point{}, + draw.Over, + ) + + draw.Draw( + dst, + textArea, + image.White, + image.Point{}, + draw.Over, + ) + + // Draw text + c := freetype.NewContext() + c.SetDPI(72) + c.SetFontSize(float64(fontSize)) + c.SetFont(f) + c.SetClip(textArea) + c.SetDst(dst) + c.SetSrc(image.Black) + + textLeft := img.Width/2 - textWidth/2 + if textLeft < borderSize { + textLeft = borderSize + } + c.DrawString(title, freetype.Pt(textLeft, img.Height/2+textHeight/4)) + + return newData("OEBPS/Images/title.jpg", dst, e.Quality) +}