Compare commits

...

4 Commits

Author SHA1 Message Date
4e4245fd21
reorg epub package 2023-04-24 10:47:04 +02:00
56dcfc27f1
remove compare export 2023-04-23 14:10:47 +02:00
a150128e73
move tree 2023-04-23 14:10:35 +02:00
2444dfee3b
move epub image data and epub zip 2023-04-23 14:07:31 +02:00
19 changed files with 268 additions and 244 deletions

View File

@ -10,27 +10,13 @@ import (
"text/template" "text/template"
"time" "time"
"github.com/celogeek/go-comic-converter/v2/internal/epub/templates" epubimage "github.com/celogeek/go-comic-converter/v2/internal/epub/image"
epubtemplates "github.com/celogeek/go-comic-converter/v2/internal/epub/templates"
epubzip "github.com/celogeek/go-comic-converter/v2/internal/epub/zip"
"github.com/gofrs/uuid" "github.com/gofrs/uuid"
) )
type ImageOptions struct { type Options struct {
Crop bool
ViewWidth int
ViewHeight int
Quality int
Algo string
Brightness int
Contrast int
AutoRotate bool
AutoSplitDoublePage bool
NoBlankPage bool
Manga bool
HasCover bool
Workers int
}
type EpubOptions struct {
Input string Input string
Output string Output string
Title string Title string
@ -41,12 +27,12 @@ type EpubOptions struct {
DryVerbose bool DryVerbose bool
SortPathMode int SortPathMode int
Quiet bool Quiet bool
Workers int
*ImageOptions Image *epubimage.Options
} }
type ePub struct { type ePub struct {
*EpubOptions *Options
UID string UID string
Publisher string Publisher string
UpdatedAt string UpdatedAt string
@ -55,11 +41,11 @@ type ePub struct {
} }
type epubPart struct { type epubPart struct {
Cover *Image Cover *epubimage.Image
Images []*Image Images []*epubimage.Image
} }
func NewEpub(options *EpubOptions) *ePub { func New(options *Options) *ePub {
uid, err := uuid.NewV4() uid, err := uuid.NewV4()
if err != nil { if err != nil {
panic(err) panic(err)
@ -72,7 +58,7 @@ func NewEpub(options *EpubOptions) *ePub {
}) })
return &ePub{ return &ePub{
EpubOptions: options, Options: options,
UID: uid.String(), UID: uid.String(),
Publisher: "GO Comic Converter", Publisher: "GO Comic Converter",
UpdatedAt: time.Now().UTC().Format("2006-01-02T15:04:05Z"), UpdatedAt: time.Now().UTC().Format("2006-01-02T15:04:05Z"),
@ -95,14 +81,14 @@ func (e *ePub) render(templateString string, data any) string {
return stripBlank.ReplaceAllString(result.String(), "\n") return stripBlank.ReplaceAllString(result.String(), "\n")
} }
func (e *ePub) writeImage(wz *epubZip, img *Image) error { func (e *ePub) writeImage(wz *epubzip.EpubZip, img *epubimage.Image) error {
err := wz.WriteFile( err := wz.WriteFile(
fmt.Sprintf("OEBPS/%s", img.TextPath()), fmt.Sprintf("OEBPS/%s", img.TextPath()),
e.render(templates.Text, map[string]any{ e.render(epubtemplates.Text, map[string]any{
"Title": fmt.Sprintf("Image %d Part %d", img.Id, img.Part), "Title": fmt.Sprintf("Image %d Part %d", img.Id, img.Part),
"ViewPort": fmt.Sprintf("width=%d,height=%d", e.ViewWidth, e.ViewHeight), "ViewPort": fmt.Sprintf("width=%d,height=%d", e.Image.ViewWidth, e.Image.ViewHeight),
"ImagePath": img.ImgPath(), "ImagePath": img.ImgPath(),
"ImageStyle": img.ImgStyle(e.ViewWidth, e.ViewHeight, e.Manga), "ImageStyle": img.ImgStyle(e.Image.ViewWidth, e.Image.ViewHeight, e.Image.Manga),
}), }),
) )
@ -113,12 +99,12 @@ func (e *ePub) writeImage(wz *epubZip, img *Image) error {
return err return err
} }
func (e *ePub) writeBlank(wz *epubZip, img *Image) error { func (e *ePub) writeBlank(wz *epubzip.EpubZip, img *epubimage.Image) error {
return wz.WriteFile( return wz.WriteFile(
fmt.Sprintf("OEBPS/Text/%d_sp.xhtml", img.Id), fmt.Sprintf("OEBPS/Text/%d_sp.xhtml", img.Id),
e.render(templates.Blank, map[string]any{ e.render(epubtemplates.Blank, map[string]any{
"Title": fmt.Sprintf("Blank Page %d", img.Id), "Title": fmt.Sprintf("Blank Page %d", img.Id),
"ViewPort": fmt.Sprintf("width=%d,height=%d", e.ViewWidth, e.ViewHeight), "ViewPort": fmt.Sprintf("width=%d,height=%d", e.Image.ViewWidth, e.Image.ViewHeight),
}), }),
) )
} }
@ -142,7 +128,7 @@ func (e *ePub) getParts() ([]*epubPart, error) {
parts := make([]*epubPart, 0) parts := make([]*epubPart, 0)
cover := images[0] cover := images[0]
if e.HasCover { if e.Image.HasCover {
images = images[1:] images = images[1:]
} }
@ -159,12 +145,12 @@ func (e *ePub) getParts() ([]*epubPart, error) {
xhtmlSize := uint64(1024) xhtmlSize := uint64(1024)
// descriptor files + title // descriptor files + title
baseSize := uint64(16*1024) + cover.Data.CompressedSize() baseSize := uint64(16*1024) + cover.Data.CompressedSize()
if e.HasCover { if e.Image.HasCover {
baseSize += cover.Data.CompressedSize() baseSize += cover.Data.CompressedSize()
} }
currentSize := baseSize currentSize := baseSize
currentImages := make([]*Image, 0) currentImages := make([]*epubimage.Image, 0)
part := 1 part := 1
for _, img := range images { for _, img := range images {
@ -176,10 +162,10 @@ func (e *ePub) getParts() ([]*epubPart, error) {
}) })
part += 1 part += 1
currentSize = baseSize currentSize = baseSize
if !e.HasCover { if !e.Image.HasCover {
currentSize += cover.Data.CompressedSize() currentSize += cover.Data.CompressedSize()
} }
currentImages = make([]*Image, 0) currentImages = make([]*epubimage.Image, 0)
} }
currentSize += imgSize currentSize += imgSize
currentImages = append(currentImages, img) currentImages = append(currentImages, img)
@ -209,8 +195,8 @@ func (e *ePub) Write() error {
p := epubParts[0] p := epubParts[0]
fmt.Fprintf(os.Stderr, "TOC:\n - %s\n%s\n", e.Title, e.getTree(p.Images, true)) fmt.Fprintf(os.Stderr, "TOC:\n - %s\n%s\n", e.Title, e.getTree(p.Images, true))
if e.DryVerbose { if e.DryVerbose {
if e.HasCover { if e.Image.HasCover {
fmt.Fprintf(os.Stderr, "Cover:\n%s\n", e.getTree([]*Image{p.Cover}, false)) fmt.Fprintf(os.Stderr, "Cover:\n%s\n", e.getTree([]*epubimage.Image{p.Cover}, false))
} }
fmt.Fprintf(os.Stderr, "Files:\n%s\n", e.getTree(p.Images, false)) fmt.Fprintf(os.Stderr, "Files:\n%s\n", e.getTree(p.Images, false))
} }
@ -219,7 +205,7 @@ func (e *ePub) Write() error {
totalParts := len(epubParts) totalParts := len(epubParts)
bar := NewBar(e.Quiet, totalParts, "Writing Part", 2, 2) bar := e.NewBar(totalParts, "Writing Part", 2, 2)
for i, part := range epubParts { for i, part := range epubParts {
ext := filepath.Ext(e.Output) ext := filepath.Ext(e.Output)
suffix := "" suffix := ""
@ -230,7 +216,7 @@ func (e *ePub) Write() error {
} }
path := fmt.Sprintf("%s%s%s", e.Output[0:len(e.Output)-len(ext)], suffix, ext) path := fmt.Sprintf("%s%s%s", e.Output[0:len(e.Output)-len(ext)], suffix, ext)
wz, err := newEpubZip(path) wz, err := epubzip.New(path)
if err != nil { if err != nil {
return err return err
} }
@ -242,19 +228,19 @@ func (e *ePub) Write() error {
} }
content := []zipContent{ content := []zipContent{
{"META-INF/container.xml", templates.Container}, {"META-INF/container.xml", epubtemplates.Container},
{"META-INF/com.apple.ibooks.display-options.xml", templates.AppleBooks}, {"META-INF/com.apple.ibooks.display-options.xml", epubtemplates.AppleBooks},
{"OEBPS/content.opf", e.getContent(title, part, i+1, totalParts).String()}, {"OEBPS/content.opf", e.getContent(title, part, i+1, totalParts).String()},
{"OEBPS/toc.xhtml", e.getToc(title, part.Images)}, {"OEBPS/toc.xhtml", e.getToc(title, part.Images)},
{"OEBPS/Text/style.css", e.render(templates.Style, map[string]any{ {"OEBPS/Text/style.css", e.render(epubtemplates.Style, map[string]any{
"PageWidth": e.ViewWidth, "PageWidth": e.Image.ViewWidth,
"PageHeight": e.ViewHeight, "PageHeight": e.Image.ViewHeight,
})}, })},
{"OEBPS/Text/title.xhtml", e.render(templates.Text, map[string]any{ {"OEBPS/Text/title.xhtml", e.render(epubtemplates.Text, map[string]any{
"Title": title, "Title": title,
"ViewPort": fmt.Sprintf("width=%d,height=%d", e.ViewWidth, e.ViewHeight), "ViewPort": fmt.Sprintf("width=%d,height=%d", e.Image.ViewWidth, e.Image.ViewHeight),
"ImagePath": "Images/title.jpg", "ImagePath": "Images/title.jpg",
"ImageStyle": part.Cover.ImgStyle(e.ViewWidth, e.ViewHeight, e.Manga), "ImageStyle": part.Cover.ImgStyle(e.Image.ViewWidth, e.Image.ViewHeight, e.Image.Manga),
})}, })},
} }
@ -266,13 +252,13 @@ func (e *ePub) Write() error {
return err return err
} }
} }
if err := wz.WriteImage(e.createTitleImageDate(title, part.Cover, i+1, totalParts)); err != nil { if err := wz.WriteImage(e.createTitleImageData(title, part.Cover, i+1, totalParts)); err != nil {
return err return err
} }
// Cover exist or part > 1 // Cover exist or part > 1
// If no cover, part 2 and more will include the image as a cover // If no cover, part 2 and more will include the image as a cover
if e.HasCover || i > 0 { if e.Image.HasCover || i > 0 {
if err := e.writeImage(wz, part.Cover); err != nil { if err := e.writeImage(wz, part.Cover); err != nil {
return err return err
} }

View File

@ -4,12 +4,19 @@ import (
"fmt" "fmt"
"github.com/beevik/etree" "github.com/beevik/etree"
epubimage "github.com/celogeek/go-comic-converter/v2/internal/epub/image"
) )
type Content struct { type Content struct {
doc *etree.Document doc *etree.Document
} }
func (c *Content) String() string {
c.doc.Indent(2)
r, _ := c.doc.WriteToString()
return r
}
type TagAttrs map[string]string type TagAttrs map[string]string
type Tag struct { type Tag struct {
@ -31,7 +38,7 @@ func (e *ePub) getMeta(title string, part *epubPart, currentPart, totalPart int)
{"meta", TagAttrs{"property": "schema:accessibilityHazard"}, "noSoundHazard"}, {"meta", TagAttrs{"property": "schema:accessibilityHazard"}, "noSoundHazard"},
{"meta", TagAttrs{"name": "book-type", "content": "comic"}, ""}, {"meta", TagAttrs{"name": "book-type", "content": "comic"}, ""},
{"opf:meta", TagAttrs{"name": "fixed-layout", "content": "true"}, ""}, {"opf:meta", TagAttrs{"name": "fixed-layout", "content": "true"}, ""},
{"opf:meta", TagAttrs{"name": "original-resolution", "content": fmt.Sprintf("%dx%d", e.ViewWidth, e.ViewHeight)}, ""}, {"opf:meta", TagAttrs{"name": "original-resolution", "content": fmt.Sprintf("%dx%d", e.Image.ViewWidth, e.Image.ViewHeight)}, ""},
{"dc:title", TagAttrs{}, title}, {"dc:title", TagAttrs{}, title},
{"dc:identifier", TagAttrs{"id": "ean"}, fmt.Sprintf("urn:uuid:%s", e.UID)}, {"dc:identifier", TagAttrs{"id": "ean"}, fmt.Sprintf("urn:uuid:%s", e.UID)},
{"dc:language", TagAttrs{}, "en"}, {"dc:language", TagAttrs{}, "en"},
@ -41,7 +48,7 @@ func (e *ePub) getMeta(title string, part *epubPart, currentPart, totalPart int)
{"dc:date", TagAttrs{}, e.UpdatedAt}, {"dc:date", TagAttrs{}, e.UpdatedAt},
} }
if e.Manga { if e.Image.Manga {
metas = append(metas, Tag{"meta", TagAttrs{"name": "primary-writing-mode", "content": "horizontal-rl"}, ""}) metas = append(metas, Tag{"meta", TagAttrs{"name": "primary-writing-mode", "content": "horizontal-rl"}, ""})
} else { } else {
metas = append(metas, Tag{"meta", TagAttrs{"name": "primary-writing-mode", "content": "horizontal-lr"}, ""}) metas = append(metas, Tag{"meta", TagAttrs{"name": "primary-writing-mode", "content": "horizontal-lr"}, ""})
@ -63,13 +70,13 @@ func (e *ePub) getMeta(title string, part *epubPart, currentPart, totalPart int)
} }
func (e *ePub) getManifest(title string, part *epubPart, currentPart, totalPart int) []Tag { func (e *ePub) getManifest(title string, part *epubPart, currentPart, totalPart int) []Tag {
iTag := func(img *Image) Tag { iTag := func(img *epubimage.Image) Tag {
return Tag{"item", TagAttrs{"id": img.Key("img"), "href": img.ImgPath(), "media-type": "image/jpeg"}, ""} return Tag{"item", TagAttrs{"id": img.Key("img"), "href": img.ImgPath(), "media-type": "image/jpeg"}, ""}
} }
hTag := func(img *Image) Tag { hTag := func(img *epubimage.Image) Tag {
return Tag{"item", TagAttrs{"id": img.Key("page"), "href": img.TextPath(), "media-type": "application/xhtml+xml"}, ""} return Tag{"item", TagAttrs{"id": img.Key("page"), "href": img.TextPath(), "media-type": "application/xhtml+xml"}, ""}
} }
sTag := func(img *Image) Tag { sTag := func(img *epubimage.Image) Tag {
return Tag{"item", TagAttrs{"id": img.SpaceKey("page"), "href": img.SpacePath(), "media-type": "application/xhtml+xml"}, ""} return Tag{"item", TagAttrs{"id": img.SpaceKey("page"), "href": img.SpacePath(), "media-type": "application/xhtml+xml"}, ""}
} }
items := []Tag{ items := []Tag{
@ -79,7 +86,7 @@ func (e *ePub) getManifest(title string, part *epubPart, currentPart, totalPart
{"item", TagAttrs{"id": "img_title", "href": "Images/title.jpg", "media-type": "image/jpeg"}, ""}, {"item", TagAttrs{"id": "img_title", "href": "Images/title.jpg", "media-type": "image/jpeg"}, ""},
} }
if e.HasCover || currentPart > 1 { if e.Image.HasCover || currentPart > 1 {
items = append(items, iTag(part.Cover), hTag(part.Cover)) items = append(items, iTag(part.Cover), hTag(part.Cover))
} }
@ -95,12 +102,12 @@ func (e *ePub) getManifest(title string, part *epubPart, currentPart, totalPart
} }
func (e *ePub) getSpine(title string, part *epubPart, currentPart, totalPart int) []Tag { func (e *ePub) getSpine(title string, part *epubPart, currentPart, totalPart int) []Tag {
isOnTheRight := !e.Manga isOnTheRight := !e.Image.Manga
getSpread := func(doublePageNoBlank bool) string { getSpread := func(doublePageNoBlank bool) string {
isOnTheRight = !isOnTheRight isOnTheRight = !isOnTheRight
if doublePageNoBlank { if doublePageNoBlank {
// Center the double page then start back to comic mode (mange/normal) // Center the double page then start back to comic mode (mange/normal)
isOnTheRight = !e.Manga isOnTheRight = !e.Image.Manga
return "rendition:page-spread-center" return "rendition:page-spread-center"
} }
if isOnTheRight { if isOnTheRight {
@ -116,10 +123,10 @@ func (e *ePub) getSpine(title string, part *epubPart, currentPart, totalPart int
for _, img := range part.Images { for _, img := range part.Images {
spine = append(spine, Tag{ spine = append(spine, Tag{
"itemref", "itemref",
TagAttrs{"idref": img.Key("page"), "properties": getSpread(img.DoublePage && e.NoBlankPage)}, TagAttrs{"idref": img.Key("page"), "properties": getSpread(img.DoublePage && e.Image.NoBlankPage)},
"", "",
}) })
if img.DoublePage && isOnTheRight && !e.NoBlankPage { if img.DoublePage && isOnTheRight && !e.Image.NoBlankPage {
spine = append(spine, Tag{ spine = append(spine, Tag{
"itemref", "itemref",
TagAttrs{"idref": img.SpaceKey("page"), "properties": getSpread(false)}, TagAttrs{"idref": img.SpaceKey("page"), "properties": getSpread(false)},
@ -127,7 +134,7 @@ func (e *ePub) getSpine(title string, part *epubPart, currentPart, totalPart int
}) })
} }
} }
if e.Manga == isOnTheRight { if e.Image.Manga == isOnTheRight {
spine = append(spine, Tag{ spine = append(spine, Tag{
"itemref", "itemref",
TagAttrs{"idref": part.Images[len(part.Images)-1].SpaceKey("page"), "properties": getSpread(false)}, TagAttrs{"idref": part.Images[len(part.Images)-1].SpaceKey("page"), "properties": getSpread(false)},
@ -179,7 +186,7 @@ func (e *ePub) getContent(title string, part *epubPart, currentPart, totalPart i
addToElement(manifest, e.getManifest) addToElement(manifest, e.getManifest)
spine := pkg.CreateElement("spine") spine := pkg.CreateElement("spine")
if e.Manga { if e.Image.Manga {
spine.CreateAttr("page-progression-direction", "rtl") spine.CreateAttr("page-progression-direction", "rtl")
} else { } else {
spine.CreateAttr("page-progression-direction", "ltr") spine.CreateAttr("page-progression-direction", "ltr")
@ -193,9 +200,3 @@ func (e *ePub) getContent(title string, part *epubPart, currentPart, totalPart i
doc, doc,
} }
} }
func (c *Content) String() string {
c.doc.Indent(2)
r, _ := c.doc.WriteToString()
return r
}

View File

@ -17,7 +17,9 @@ import (
"strings" "strings"
"sync" "sync"
"github.com/celogeek/go-comic-converter/v2/internal/epub/sortpath" epubimage "github.com/celogeek/go-comic-converter/v2/internal/epub/image"
epubimagedata "github.com/celogeek/go-comic-converter/v2/internal/epub/imagedata"
"github.com/celogeek/go-comic-converter/v2/internal/sortpath"
"github.com/disintegration/gift" "github.com/disintegration/gift"
"github.com/golang/freetype" "github.com/golang/freetype"
"github.com/golang/freetype/truetype" "github.com/golang/freetype/truetype"
@ -30,69 +32,6 @@ import (
_ "golang.org/x/image/webp" _ "golang.org/x/image/webp"
) )
type Image struct {
Id int
Part int
Raw image.Image
Data *ImageData
Width int
Height int
IsCover bool
DoublePage bool
Path string
Name string
}
func (i *Image) Key(prefix string) string {
return fmt.Sprintf("%s_%d_p%d", prefix, i.Id, i.Part)
}
func (i *Image) SpaceKey(prefix string) string {
return fmt.Sprintf("%s_%d_sp", prefix, i.Id)
}
func (i *Image) TextPath() string {
return fmt.Sprintf("Text/%d_p%d.xhtml", i.Id, i.Part)
}
func (i *Image) ImgPath() string {
return fmt.Sprintf("Images/%d_p%d.jpg", i.Id, i.Part)
}
func (i *Image) ImgStyle(viewWidth, viewHeight int, manga bool) string {
marginW, marginH := float64(viewWidth-i.Width)/2, float64(viewHeight-i.Height)/2
left, top := marginW*100/float64(viewWidth), marginH*100/float64(viewHeight)
var align string
switch i.Part {
case 0:
align = fmt.Sprintf("left:%.2f%%", left)
case 1:
if manga {
align = "left:0"
} else {
align = "right:0"
}
case 2:
if manga {
align = "right:0"
} else {
align = "left:0"
}
}
return fmt.Sprintf(
"width:%dpx; height:%dpx; top:%.2f%%; %s;",
i.Width,
i.Height,
top,
align,
)
}
func (i *Image) SpacePath() string {
return fmt.Sprintf("Text/%d_sp.xhtml", i.Id)
}
type imageTask struct { type imageTask struct {
Id int Id int
Reader io.ReadCloser Reader io.ReadCloser
@ -151,8 +90,8 @@ BOTTOM:
return imgArea return imgArea
} }
func (e *ePub) LoadImages() ([]*Image, error) { func (e *ePub) LoadImages() ([]*epubimage.Image, error) {
images := make([]*Image, 0) images := make([]*epubimage.Image, 0)
fi, err := os.Stat(e.Input) fi, err := os.Stat(e.Input)
if err != nil { if err != nil {
@ -185,7 +124,7 @@ func (e *ePub) LoadImages() ([]*Image, error) {
if e.Dry { if e.Dry {
for img := range imageInput { for img := range imageInput {
img.Reader.Close() img.Reader.Close()
images = append(images, &Image{ images = append(images, &epubimage.Image{
Id: img.Id, Id: img.Id,
Path: img.Path, Path: img.Path,
Name: img.Name, Name: img.Name,
@ -195,13 +134,13 @@ func (e *ePub) LoadImages() ([]*Image, error) {
return images, nil return images, nil
} }
imageOutput := make(chan *Image) imageOutput := make(chan *epubimage.Image)
// processing // processing
bar := NewBar(e.Quiet, imageCount, "Processing", 1, 2) bar := e.NewBar(imageCount, "Processing", 1, 2)
wg := &sync.WaitGroup{} wg := &sync.WaitGroup{}
for i := 0; i < e.ImageOptions.Workers; i++ { for i := 0; i < e.Workers; i++ {
wg.Add(1) wg.Add(1)
go func() { go func() {
defer wg.Done() defer wg.Done()
@ -216,14 +155,14 @@ func (e *ePub) LoadImages() ([]*Image, error) {
os.Exit(1) os.Exit(1)
} }
if e.ImageOptions.Crop { if e.Image.Crop {
g := gift.New(gift.Crop(findMarging(src))) g := gift.New(gift.Crop(findMarging(src)))
newSrc := image.NewNRGBA(g.Bounds(src.Bounds())) newSrc := image.NewNRGBA(g.Bounds(src.Bounds()))
g.Draw(newSrc, src) g.Draw(newSrc, src)
src = newSrc src = newSrc
} }
g := NewGift(e.ImageOptions) g := epubimage.NewGift(e.Options.Image)
// Convert image // Convert image
dst := image.NewGray(g.Bounds(src.Bounds())) dst := image.NewGray(g.Bounds(src.Bounds()))
@ -234,17 +173,17 @@ func (e *ePub) LoadImages() ([]*Image, error) {
raw = dst raw = dst
} }
imageOutput <- &Image{ imageOutput <- &epubimage.Image{
Id: img.Id, Id: img.Id,
Part: 0, Part: 0,
Raw: raw, Raw: raw,
Data: newImageData(img.Id, 0, dst, e.ImageOptions.Quality), Data: epubimagedata.New(img.Id, 0, dst, e.Image.Quality),
Width: dst.Bounds().Dx(), Width: dst.Bounds().Dx(),
Height: dst.Bounds().Dy(), Height: dst.Bounds().Dy(),
IsCover: img.Id == 0, IsCover: img.Id == 0,
DoublePage: src.Bounds().Dx() > src.Bounds().Dy() && DoublePage: src.Bounds().Dx() > src.Bounds().Dy() &&
src.Bounds().Dx() > e.ImageOptions.ViewHeight && src.Bounds().Dx() > e.Image.ViewHeight &&
src.Bounds().Dy() > e.ImageOptions.ViewWidth, src.Bounds().Dy() > e.Image.ViewWidth,
Path: img.Path, Path: img.Path,
Name: img.Name, Name: img.Name,
} }
@ -252,21 +191,21 @@ func (e *ePub) LoadImages() ([]*Image, error) {
// Auto split double page // Auto split double page
// Except for cover // Except for cover
// Only if the src image have width > height and is bigger than the view // Only if the src image have width > height and is bigger than the view
if (!e.ImageOptions.HasCover || img.Id > 0) && if (!e.Image.HasCover || img.Id > 0) &&
e.ImageOptions.AutoSplitDoublePage && e.Image.AutoSplitDoublePage &&
src.Bounds().Dx() > src.Bounds().Dy() && src.Bounds().Dx() > src.Bounds().Dy() &&
src.Bounds().Dx() > e.ImageOptions.ViewHeight && src.Bounds().Dx() > e.Image.ViewHeight &&
src.Bounds().Dy() > e.ImageOptions.ViewWidth { src.Bounds().Dy() > e.Image.ViewWidth {
gifts := NewGiftSplitDoublePage(e.ImageOptions) gifts := epubimage.NewGiftSplitDoublePage(e.Options.Image)
for i, g := range gifts { for i, g := range gifts {
part := i + 1 part := i + 1
dst := image.NewGray(g.Bounds(src.Bounds())) dst := image.NewGray(g.Bounds(src.Bounds()))
g.Draw(dst, src) g.Draw(dst, src)
imageOutput <- &Image{ imageOutput <- &epubimage.Image{
Id: img.Id, Id: img.Id,
Part: part, Part: part,
Data: newImageData(img.Id, part, dst, e.ImageOptions.Quality), Data: epubimagedata.New(img.Id, part, dst, e.Image.Quality),
Width: dst.Bounds().Dx(), Width: dst.Bounds().Dx(),
Height: dst.Bounds().Dy(), Height: dst.Bounds().Dy(),
IsCover: false, IsCover: false,
@ -285,11 +224,11 @@ func (e *ePub) LoadImages() ([]*Image, error) {
close(imageOutput) close(imageOutput)
}() }()
for image := range imageOutput { for img := range imageOutput {
if !(e.ImageOptions.NoBlankPage && image.Width == 1 && image.Height == 1) { if !(e.Image.NoBlankPage && img.Width == 1 && img.Height == 1) {
images = append(images, image) images = append(images, img)
} }
if image.Part == 0 { if img.Part == 0 {
bar.Add(1) bar.Add(1)
} }
} }
@ -519,7 +458,7 @@ 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 { func (e *ePub) createTitleImageData(title string, img *epubimage.Image, currentPart, totalPart int) *epubimagedata.ImageData {
// Create a blur version of the cover // Create a blur version of the cover
g := gift.New(gift.GaussianBlur(8)) g := gift.New(gift.GaussianBlur(8))
dst := image.NewGray(g.Bounds(img.Raw.Bounds())) dst := image.NewGray(g.Bounds(img.Raw.Bounds()))
@ -576,5 +515,5 @@ func (e *ePub) createTitleImageDate(title string, img *Image, currentPart, total
} }
c.DrawString(title, freetype.Pt(textLeft, img.Height/2+textHeight/4)) c.DrawString(title, freetype.Pt(textLeft, img.Height/2+textHeight/4))
return newData("OEBPS/Images/title.jpg", dst, e.Quality) return epubimagedata.NewRaw("OEBPS/Images/title.jpg", dst, e.Image.Quality)
} }

View File

@ -7,8 +7,8 @@ import (
"github.com/schollz/progressbar/v3" "github.com/schollz/progressbar/v3"
) )
func NewBar(quiet bool, max int, description string, currentJob, totalJob int) *progressbar.ProgressBar { func (e *ePub) NewBar(max int, description string, currentJob, totalJob int) *progressbar.ProgressBar {
if quiet { if e.Quiet {
return progressbar.DefaultSilent(int64(max)) return progressbar.DefaultSilent(int64(max))
} }
fmtJob := fmt.Sprintf("%%0%dd", len(fmt.Sprint(totalJob))) fmtJob := fmt.Sprintf("%%0%dd", len(fmt.Sprint(totalJob)))

View File

@ -5,9 +5,10 @@ import (
"strings" "strings"
"github.com/beevik/etree" "github.com/beevik/etree"
epubimage "github.com/celogeek/go-comic-converter/v2/internal/epub/image"
) )
func (e *ePub) getToc(title string, images []*Image) string { func (e *ePub) getToc(title string, images []*epubimage.Image) string {
doc := etree.NewDocument() doc := etree.NewDocument()
doc.CreateProcInst("xml", `version="1.0" encoding="UTF-8"`) doc.CreateProcInst("xml", `version="1.0" encoding="UTF-8"`)
doc.CreateDirective("DOCTYPE html") doc.CreateDirective("DOCTYPE html")

View File

@ -2,58 +2,13 @@ package epub
import ( import (
"path/filepath" "path/filepath"
"strings"
epubimage "github.com/celogeek/go-comic-converter/v2/internal/epub/image"
epubtree "github.com/celogeek/go-comic-converter/v2/internal/epub/tree"
) )
type Tree struct { func (e *ePub) getTree(images []*epubimage.Image, skip_files bool) string {
Nodes map[string]*Node t := epubtree.New()
}
type Node struct {
Value string
Children []*Node
}
func NewTree() *Tree {
return &Tree{map[string]*Node{
".": {".", []*Node{}},
}}
}
func (n *Tree) Root() *Node {
return n.Nodes["."]
}
func (n *Tree) Add(filename string) {
cn := n.Root()
cp := ""
for _, p := range strings.Split(filepath.Clean(filename), string(filepath.Separator)) {
cp = filepath.Join(cp, p)
if _, ok := n.Nodes[cp]; !ok {
n.Nodes[cp] = &Node{Value: p, Children: []*Node{}}
cn.Children = append(cn.Children, n.Nodes[cp])
}
cn = n.Nodes[cp]
}
}
func (n *Node) toString(indent string) string {
r := strings.Builder{}
if indent != "" {
r.WriteString(indent)
r.WriteString("- ")
r.WriteString(n.Value)
r.WriteString("\n")
}
indent += " "
for _, c := range n.Children {
r.WriteString(c.toString(indent))
}
return r.String()
}
func (e *ePub) getTree(images []*Image, skip_files bool) string {
t := NewTree()
for _, img := range images { for _, img := range images {
if skip_files { if skip_files {
t.Add(img.Path) t.Add(img.Path)
@ -66,5 +21,5 @@ func (e *ePub) getTree(images []*Image, skip_files bool) string {
c = c.Children[0] c = c.Children[0]
} }
return c.toString("") return c.ToString("")
} }

View File

@ -1,4 +1,4 @@
package filters package epubfilters
import ( import (
"image" "image"

View File

@ -1,4 +1,4 @@
package filters package epubfilters
import ( import (
"image" "image"

View File

@ -1,4 +1,4 @@
package filters package epubfilters
import ( import (
"image" "image"

View File

@ -1,4 +1,4 @@
package filters package epubfilters
import ( import (
"image" "image"

View File

@ -0,0 +1,71 @@
package epubimage
import (
"fmt"
"image"
epubimagedata "github.com/celogeek/go-comic-converter/v2/internal/epub/imagedata"
)
type Image struct {
Id int
Part int
Raw image.Image
Data *epubimagedata.ImageData
Width int
Height int
IsCover bool
DoublePage bool
Path string
Name string
}
func (i *Image) Key(prefix string) string {
return fmt.Sprintf("%s_%d_p%d", prefix, i.Id, i.Part)
}
func (i *Image) SpaceKey(prefix string) string {
return fmt.Sprintf("%s_%d_sp", prefix, i.Id)
}
func (i *Image) TextPath() string {
return fmt.Sprintf("Text/%d_p%d.xhtml", i.Id, i.Part)
}
func (i *Image) ImgPath() string {
return fmt.Sprintf("Images/%d_p%d.jpg", i.Id, i.Part)
}
func (i *Image) ImgStyle(viewWidth, viewHeight int, manga bool) string {
marginW, marginH := float64(viewWidth-i.Width)/2, float64(viewHeight-i.Height)/2
left, top := marginW*100/float64(viewWidth), marginH*100/float64(viewHeight)
var align string
switch i.Part {
case 0:
align = fmt.Sprintf("left:%.2f%%", left)
case 1:
if manga {
align = "left:0"
} else {
align = "right:0"
}
case 2:
if manga {
align = "right:0"
} else {
align = "left:0"
}
}
return fmt.Sprintf(
"width:%dpx; height:%dpx; top:%.2f%%; %s;",
i.Width,
i.Height,
top,
align,
)
}
func (i *Image) SpacePath() string {
return fmt.Sprintf("Text/%d_sp.xhtml", i.Id)
}

View File

@ -1,16 +1,16 @@
package epub package epubimage
import ( import (
"github.com/celogeek/go-comic-converter/v2/internal/epub/filters" epubfilters "github.com/celogeek/go-comic-converter/v2/internal/epub/filters"
"github.com/disintegration/gift" "github.com/disintegration/gift"
) )
func NewGift(options *ImageOptions) *gift.GIFT { func NewGift(options *Options) *gift.GIFT {
g := gift.New() g := gift.New()
g.SetParallelization(false) g.SetParallelization(false)
if options.AutoRotate { if options.AutoRotate {
g.Add(filters.AutoRotate(options.ViewWidth, options.ViewHeight)) g.Add(epubfilters.AutoRotate(options.ViewWidth, options.ViewHeight))
} }
if options.Contrast != 0 { if options.Contrast != 0 {
g.Add(gift.Contrast(float32(options.Contrast))) g.Add(gift.Contrast(float32(options.Contrast)))
@ -19,21 +19,21 @@ func NewGift(options *ImageOptions) *gift.GIFT {
g.Add(gift.Brightness(float32(options.Brightness))) g.Add(gift.Brightness(float32(options.Brightness)))
} }
g.Add( g.Add(
filters.Resize(options.ViewWidth, options.ViewHeight, gift.LanczosResampling), epubfilters.Resize(options.ViewWidth, options.ViewHeight, gift.LanczosResampling),
filters.Pixel(), epubfilters.Pixel(),
) )
return g return g
} }
func NewGiftSplitDoublePage(options *ImageOptions) []*gift.GIFT { func NewGiftSplitDoublePage(options *Options) []*gift.GIFT {
gifts := make([]*gift.GIFT, 2) gifts := make([]*gift.GIFT, 2)
gifts[0] = gift.New( gifts[0] = gift.New(
filters.CropSplitDoublePage(options.Manga), epubfilters.CropSplitDoublePage(options.Manga),
) )
gifts[1] = gift.New( gifts[1] = gift.New(
filters.CropSplitDoublePage(!options.Manga), epubfilters.CropSplitDoublePage(!options.Manga),
) )
for _, g := range gifts { for _, g := range gifts {
@ -45,7 +45,7 @@ func NewGiftSplitDoublePage(options *ImageOptions) []*gift.GIFT {
} }
g.Add( g.Add(
filters.Resize(options.ViewWidth, options.ViewHeight, gift.LanczosResampling), epubfilters.Resize(options.ViewWidth, options.ViewHeight, gift.LanczosResampling),
) )
} }

View File

@ -0,0 +1,15 @@
package epubimage
type Options struct {
Crop bool
ViewWidth int
ViewHeight int
Quality int
Brightness int
Contrast int
AutoRotate bool
AutoSplitDoublePage bool
NoBlankPage bool
Manga bool
HasCover bool
}

View File

@ -1,4 +1,4 @@
package epub package epubimagedata
import ( import (
"archive/zip" "archive/zip"
@ -20,12 +20,12 @@ func (img *ImageData) CompressedSize() uint64 {
return img.Header.CompressedSize64 + 30 + uint64(len(img.Header.Name)) return img.Header.CompressedSize64 + 30 + uint64(len(img.Header.Name))
} }
func newImageData(id int, part int, img image.Image, quality int) *ImageData { func New(id int, part int, img image.Image, quality int) *ImageData {
name := fmt.Sprintf("OEBPS/Images/%d_p%d.jpg", id, part) name := fmt.Sprintf("OEBPS/Images/%d_p%d.jpg", id, part)
return newData(name, img, quality) return NewRaw(name, img, quality)
} }
func newData(name string, img image.Image, quality int) *ImageData { func NewRaw(name string, img image.Image, quality int) *ImageData {
data := bytes.NewBuffer([]byte{}) data := bytes.NewBuffer([]byte{})
if err := jpeg.Encode(data, img, &jpeg.Options{Quality: quality}); err != nil { if err := jpeg.Encode(data, img, &jpeg.Options{Quality: quality}); err != nil {
panic(err) panic(err)

View File

@ -1,4 +1,4 @@
package templates package epubtemplates
import _ "embed" import _ "embed"

View File

@ -0,0 +1,53 @@
package epubtree
import (
"path/filepath"
"strings"
)
type Tree struct {
Nodes map[string]*Node
}
type Node struct {
Value string
Children []*Node
}
func New() *Tree {
return &Tree{map[string]*Node{
".": {".", []*Node{}},
}}
}
func (n *Tree) Root() *Node {
return n.Nodes["."]
}
func (n *Tree) Add(filename string) {
cn := n.Root()
cp := ""
for _, p := range strings.Split(filepath.Clean(filename), string(filepath.Separator)) {
cp = filepath.Join(cp, p)
if _, ok := n.Nodes[cp]; !ok {
n.Nodes[cp] = &Node{Value: p, Children: []*Node{}}
cn.Children = append(cn.Children, n.Nodes[cp])
}
cn = n.Nodes[cp]
}
}
func (n *Node) ToString(indent string) string {
r := strings.Builder{}
if indent != "" {
r.WriteString(indent)
r.WriteString("- ")
r.WriteString(n.Value)
r.WriteString("\n")
}
indent += " "
for _, c := range n.Children {
r.WriteString(c.ToString(indent))
}
return r.String()
}

View File

@ -1,34 +1,36 @@
package epub package epubzip
import ( import (
"archive/zip" "archive/zip"
"fmt" "fmt"
"os" "os"
"time" "time"
epubimagedata "github.com/celogeek/go-comic-converter/v2/internal/epub/imagedata"
) )
type epubZip struct { type EpubZip struct {
w *os.File w *os.File
wz *zip.Writer wz *zip.Writer
} }
func newEpubZip(path string) (*epubZip, error) { func New(path string) (*EpubZip, error) {
w, err := os.Create(path) w, err := os.Create(path)
if err != nil { if err != nil {
return nil, err return nil, err
} }
wz := zip.NewWriter(w) wz := zip.NewWriter(w)
return &epubZip{w, wz}, nil return &EpubZip{w, wz}, nil
} }
func (e *epubZip) Close() error { func (e *EpubZip) Close() error {
if err := e.wz.Close(); err != nil { if err := e.wz.Close(); err != nil {
return err return err
} }
return e.w.Close() return e.w.Close()
} }
func (e *epubZip) WriteMagic() error { func (e *EpubZip) WriteMagic() error {
t := time.Now() t := time.Now()
fh := &zip.FileHeader{ fh := &zip.FileHeader{
Name: "mimetype", Name: "mimetype",
@ -50,7 +52,7 @@ func (e *epubZip) WriteMagic() error {
return err return err
} }
func (e *epubZip) WriteImage(image *ImageData) error { func (e *EpubZip) WriteImage(image *epubimagedata.ImageData) error {
m, err := e.wz.CreateRaw(image.Header) m, err := e.wz.CreateRaw(image.Header)
if err != nil { if err != nil {
return err return err
@ -59,7 +61,7 @@ func (e *epubZip) WriteImage(image *ImageData) error {
return err return err
} }
func (e *epubZip) WriteFile(file string, data any) error { func (e *EpubZip) WriteFile(file string, data any) error {
var content []byte var content []byte
switch b := data.(type) { switch b := data.(type) {
case string: case string:

View File

@ -16,7 +16,7 @@ type part struct {
number float64 number float64
} }
func (a part) Compare(b part) float64 { func (a part) compare(b part) float64 {
if a.number == 0 || b.number == 0 { if a.number == 0 || b.number == 0 {
return float64(strings.Compare(a.fullname, b.fullname)) return float64(strings.Compare(a.fullname, b.fullname))
} }
@ -70,7 +70,7 @@ func comparePart(a, b []part) float64 {
m = len(b) m = len(b)
} }
for i := 0; i < m; i++ { for i := 0; i < m; i++ {
c := a[i].Compare(b[i]) c := a[i].compare(b[i])
if c != 0 { if c != 0 {
return c return c
} }

View File

@ -7,6 +7,7 @@ import (
"github.com/celogeek/go-comic-converter/v2/internal/converter" "github.com/celogeek/go-comic-converter/v2/internal/converter"
"github.com/celogeek/go-comic-converter/v2/internal/epub" "github.com/celogeek/go-comic-converter/v2/internal/epub"
epubimage "github.com/celogeek/go-comic-converter/v2/internal/epub/image"
"github.com/tcnksm/go-latest" "github.com/tcnksm/go-latest"
) )
@ -93,7 +94,7 @@ $ go install github.com/celogeek/go-comic-converter/v%d@%s
profile := cmd.Options.GetProfile() profile := cmd.Options.GetProfile()
perfectWidth, perfectHeight := profile.PerfectDim() perfectWidth, perfectHeight := profile.PerfectDim()
if err := epub.NewEpub(&epub.EpubOptions{ if err := epub.New(&epub.Options{
Input: cmd.Options.Input, Input: cmd.Options.Input,
Output: cmd.Options.Output, Output: cmd.Options.Output,
LimitMb: cmd.Options.LimitMb, LimitMb: cmd.Options.LimitMb,
@ -101,7 +102,7 @@ $ go install github.com/celogeek/go-comic-converter/v%d@%s
Author: cmd.Options.Author, Author: cmd.Options.Author,
StripFirstDirectoryFromToc: cmd.Options.StripFirstDirectoryFromToc, StripFirstDirectoryFromToc: cmd.Options.StripFirstDirectoryFromToc,
SortPathMode: cmd.Options.SortPathMode, SortPathMode: cmd.Options.SortPathMode,
ImageOptions: &epub.ImageOptions{ Image: &epubimage.Options{
ViewWidth: perfectWidth, ViewWidth: perfectWidth,
ViewHeight: perfectHeight, ViewHeight: perfectHeight,
Quality: cmd.Options.Quality, Quality: cmd.Options.Quality,
@ -113,8 +114,8 @@ $ go install github.com/celogeek/go-comic-converter/v%d@%s
NoBlankPage: cmd.Options.NoBlankPage, NoBlankPage: cmd.Options.NoBlankPage,
Manga: cmd.Options.Manga, Manga: cmd.Options.Manga,
HasCover: cmd.Options.HasCover, HasCover: cmd.Options.HasCover,
Workers: cmd.Options.Workers,
}, },
Workers: cmd.Options.Workers,
Dry: cmd.Options.Dry, Dry: cmd.Options.Dry,
DryVerbose: cmd.Options.DryVerbose, DryVerbose: cmd.Options.DryVerbose,
Quiet: cmd.Options.Quiet, Quiet: cmd.Options.Quiet,