From 9c93b4f1d4650ec2716aeedd1801c64a98eb43ce Mon Sep 17 00:00:00 2001 From: celogeek <65178+celogeek@users.noreply.github.com> Date: Sat, 22 Apr 2023 19:07:53 +0200 Subject: [PATCH] create title page --- go.mod | 1 + go.sum | 2 ++ internal/epub/epub.go | 69 +++++++++++++++++++++++++++++++++++++++++-- 3 files changed, 69 insertions(+), 3 deletions(-) diff --git a/go.mod b/go.mod index 5ccb349..7e6174f 100644 --- a/go.mod +++ b/go.mod @@ -6,6 +6,7 @@ require ( github.com/beevik/etree v1.1.0 github.com/disintegration/gift v1.2.1 github.com/gofrs/uuid v4.4.0+incompatible + github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 github.com/nwaples/rardecode v1.1.3 github.com/raff/pdfreader v0.0.0-20220308062436-033e8ac577f0 github.com/schollz/progressbar/v3 v3.13.1 diff --git a/go.sum b/go.sum index 189f181..54216f9 100644 --- a/go.sum +++ b/go.sum @@ -7,6 +7,8 @@ github.com/disintegration/gift v1.2.1 h1:Y005a1X4Z7Uc+0gLpSAsKhWi4qLtsdEcMIbbdvd github.com/disintegration/gift v1.2.1/go.mod h1:Jh2i7f7Q2BM7Ezno3PhfezbR1xpUg9dUg3/RlKGr4HI= github.com/gofrs/uuid v4.4.0+incompatible h1:3qXRTX8/NbyulANqlc0lchS1gqAVxRgsuW1YrTJupqA= github.com/gofrs/uuid v4.4.0+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRxnplIgP/c0N/04lM= +github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 h1:DACJavvAHhabrF08vX0COfcOBJRhZ8lUbR+ZWIs0Y5g= +github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0/go.mod h1:E/TSTwGwJL78qG/PmXZO1EjYhfJinVAhrmmHX6Z8B9k= github.com/google/go-cmp v0.5.2 h1:X2ev0eStA3AbceY54o37/0PQ/UWqKEiiO2dKL5OPaFM= github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-github v17.0.0+incompatible h1:N0LgJ1j65A7kfXrZnUDaYCs/Sf4rEjNlfyDHW9dolSY= diff --git a/internal/epub/epub.go b/internal/epub/epub.go index 3c24aa0..10247a0 100644 --- a/internal/epub/epub.go +++ b/internal/epub/epub.go @@ -2,6 +2,8 @@ package epub import ( "fmt" + "image" + "image/draw" "os" "path/filepath" "regexp" @@ -10,7 +12,12 @@ 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 { @@ -122,8 +129,64 @@ func (e *ePub) writeBlank(wz *epubZip, img *Image) error { ) } -func (e ePub) getTitleImageData(img *Image, currentPart, totalPart int) *ImageData { - return newData("OEBPS/Images/title.jpg", img.Raw, e.Quality) +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) { @@ -269,7 +332,7 @@ func (e *ePub) Write() error { return err } } - if err := wz.WriteImage(e.getTitleImageData(part.Cover, i+1, totalParts)); err != nil { + if err := wz.WriteImage(e.getTitleImageData(title, part.Cover, i+1, totalParts)); err != nil { return err }