mirror of
https://github.com/celogeek/go-comic-converter.git
synced 2025-05-25 08:12:36 +02:00
move create title
This commit is contained in:
parent
aae62f62c3
commit
42c58c733b
@ -2,8 +2,6 @@ package epub
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"image"
|
|
||||||
"image/draw"
|
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"regexp"
|
"regexp"
|
||||||
@ -12,12 +10,7 @@ import (
|
|||||||
"text/template"
|
"text/template"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/disintegration/gift"
|
|
||||||
"github.com/gofrs/uuid"
|
"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 {
|
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) {
|
func (e *ePub) getParts() ([]*epubPart, error) {
|
||||||
images, err := e.LoadImages()
|
images, err := e.LoadImages()
|
||||||
|
|
||||||
@ -332,7 +265,7 @@ func (e *ePub) Write() error {
|
|||||||
return err
|
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
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -6,6 +6,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"image"
|
"image"
|
||||||
"image/color"
|
"image/color"
|
||||||
|
"image/draw"
|
||||||
_ "image/jpeg"
|
_ "image/jpeg"
|
||||||
_ "image/png"
|
_ "image/png"
|
||||||
"io"
|
"io"
|
||||||
@ -18,9 +19,13 @@ import (
|
|||||||
|
|
||||||
"github.com/celogeek/go-comic-converter/v2/internal/epub/sortpath"
|
"github.com/celogeek/go-comic-converter/v2/internal/epub/sortpath"
|
||||||
"github.com/disintegration/gift"
|
"github.com/disintegration/gift"
|
||||||
|
"github.com/golang/freetype"
|
||||||
|
"github.com/golang/freetype/truetype"
|
||||||
"github.com/nwaples/rardecode"
|
"github.com/nwaples/rardecode"
|
||||||
pdfimage "github.com/raff/pdfreader/image"
|
pdfimage "github.com/raff/pdfreader/image"
|
||||||
"github.com/raff/pdfreader/pdfread"
|
"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/tiff"
|
||||||
_ "golang.org/x/image/webp"
|
_ "golang.org/x/image/webp"
|
||||||
)
|
)
|
||||||
@ -513,3 +518,63 @@ func loadPdf(input string) (int, chan *imageTask, error) {
|
|||||||
|
|
||||||
return nbPages, output, nil
|
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)
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user