mirror of
https://github.com/celogeek/go-comic-converter.git
synced 2025-05-24 15:52:38 +02:00
create title page
This commit is contained in:
parent
82aa66c363
commit
9c93b4f1d4
1
go.mod
1
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
|
||||
|
2
go.sum
2
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=
|
||||
|
@ -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
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user