mirror of
https://github.com/celogeek/go-comic-converter.git
synced 2025-05-27 17:39:55 +02:00
Compare commits
No commits in common. "4e4245fd211f2f3c2b817c65248c3cdb147e12f3" and "1ad8f9ddd0073be8e7177c848f83be420041f21a" have entirely different histories.
4e4245fd21
...
1ad8f9ddd0
@ -10,13 +10,27 @@ import (
|
|||||||
"text/template"
|
"text/template"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
epubimage "github.com/celogeek/go-comic-converter/v2/internal/epub/image"
|
"github.com/celogeek/go-comic-converter/v2/internal/epub/templates"
|
||||||
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 Options struct {
|
type ImageOptions 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
|
||||||
@ -27,12 +41,12 @@ type Options struct {
|
|||||||
DryVerbose bool
|
DryVerbose bool
|
||||||
SortPathMode int
|
SortPathMode int
|
||||||
Quiet bool
|
Quiet bool
|
||||||
Workers int
|
|
||||||
Image *epubimage.Options
|
*ImageOptions
|
||||||
}
|
}
|
||||||
|
|
||||||
type ePub struct {
|
type ePub struct {
|
||||||
*Options
|
*EpubOptions
|
||||||
UID string
|
UID string
|
||||||
Publisher string
|
Publisher string
|
||||||
UpdatedAt string
|
UpdatedAt string
|
||||||
@ -41,11 +55,11 @@ type ePub struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type epubPart struct {
|
type epubPart struct {
|
||||||
Cover *epubimage.Image
|
Cover *Image
|
||||||
Images []*epubimage.Image
|
Images []*Image
|
||||||
}
|
}
|
||||||
|
|
||||||
func New(options *Options) *ePub {
|
func NewEpub(options *EpubOptions) *ePub {
|
||||||
uid, err := uuid.NewV4()
|
uid, err := uuid.NewV4()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
@ -58,7 +72,7 @@ func New(options *Options) *ePub {
|
|||||||
})
|
})
|
||||||
|
|
||||||
return &ePub{
|
return &ePub{
|
||||||
Options: options,
|
EpubOptions: 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"),
|
||||||
@ -81,14 +95,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.EpubZip, img *epubimage.Image) error {
|
func (e *ePub) writeImage(wz *epubZip, img *Image) error {
|
||||||
err := wz.WriteFile(
|
err := wz.WriteFile(
|
||||||
fmt.Sprintf("OEBPS/%s", img.TextPath()),
|
fmt.Sprintf("OEBPS/%s", img.TextPath()),
|
||||||
e.render(epubtemplates.Text, map[string]any{
|
e.render(templates.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.Image.ViewWidth, e.Image.ViewHeight),
|
"ViewPort": fmt.Sprintf("width=%d,height=%d", e.ViewWidth, e.ViewHeight),
|
||||||
"ImagePath": img.ImgPath(),
|
"ImagePath": img.ImgPath(),
|
||||||
"ImageStyle": img.ImgStyle(e.Image.ViewWidth, e.Image.ViewHeight, e.Image.Manga),
|
"ImageStyle": img.ImgStyle(e.ViewWidth, e.ViewHeight, e.Manga),
|
||||||
}),
|
}),
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -99,12 +113,12 @@ func (e *ePub) writeImage(wz *epubzip.EpubZip, img *epubimage.Image) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *ePub) writeBlank(wz *epubzip.EpubZip, img *epubimage.Image) error {
|
func (e *ePub) writeBlank(wz *epubZip, img *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(epubtemplates.Blank, map[string]any{
|
e.render(templates.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.Image.ViewWidth, e.Image.ViewHeight),
|
"ViewPort": fmt.Sprintf("width=%d,height=%d", e.ViewWidth, e.ViewHeight),
|
||||||
}),
|
}),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
@ -128,7 +142,7 @@ func (e *ePub) getParts() ([]*epubPart, error) {
|
|||||||
|
|
||||||
parts := make([]*epubPart, 0)
|
parts := make([]*epubPart, 0)
|
||||||
cover := images[0]
|
cover := images[0]
|
||||||
if e.Image.HasCover {
|
if e.HasCover {
|
||||||
images = images[1:]
|
images = images[1:]
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -145,12 +159,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.Image.HasCover {
|
if e.HasCover {
|
||||||
baseSize += cover.Data.CompressedSize()
|
baseSize += cover.Data.CompressedSize()
|
||||||
}
|
}
|
||||||
|
|
||||||
currentSize := baseSize
|
currentSize := baseSize
|
||||||
currentImages := make([]*epubimage.Image, 0)
|
currentImages := make([]*Image, 0)
|
||||||
part := 1
|
part := 1
|
||||||
|
|
||||||
for _, img := range images {
|
for _, img := range images {
|
||||||
@ -162,10 +176,10 @@ func (e *ePub) getParts() ([]*epubPart, error) {
|
|||||||
})
|
})
|
||||||
part += 1
|
part += 1
|
||||||
currentSize = baseSize
|
currentSize = baseSize
|
||||||
if !e.Image.HasCover {
|
if !e.HasCover {
|
||||||
currentSize += cover.Data.CompressedSize()
|
currentSize += cover.Data.CompressedSize()
|
||||||
}
|
}
|
||||||
currentImages = make([]*epubimage.Image, 0)
|
currentImages = make([]*Image, 0)
|
||||||
}
|
}
|
||||||
currentSize += imgSize
|
currentSize += imgSize
|
||||||
currentImages = append(currentImages, img)
|
currentImages = append(currentImages, img)
|
||||||
@ -195,8 +209,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.Image.HasCover {
|
if e.HasCover {
|
||||||
fmt.Fprintf(os.Stderr, "Cover:\n%s\n", e.getTree([]*epubimage.Image{p.Cover}, false))
|
fmt.Fprintf(os.Stderr, "Cover:\n%s\n", e.getTree([]*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))
|
||||||
}
|
}
|
||||||
@ -205,7 +219,7 @@ func (e *ePub) Write() error {
|
|||||||
|
|
||||||
totalParts := len(epubParts)
|
totalParts := len(epubParts)
|
||||||
|
|
||||||
bar := e.NewBar(totalParts, "Writing Part", 2, 2)
|
bar := NewBar(e.Quiet, 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 := ""
|
||||||
@ -216,7 +230,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 := epubzip.New(path)
|
wz, err := newEpubZip(path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -228,19 +242,19 @@ func (e *ePub) Write() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
content := []zipContent{
|
content := []zipContent{
|
||||||
{"META-INF/container.xml", epubtemplates.Container},
|
{"META-INF/container.xml", templates.Container},
|
||||||
{"META-INF/com.apple.ibooks.display-options.xml", epubtemplates.AppleBooks},
|
{"META-INF/com.apple.ibooks.display-options.xml", templates.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(epubtemplates.Style, map[string]any{
|
{"OEBPS/Text/style.css", e.render(templates.Style, map[string]any{
|
||||||
"PageWidth": e.Image.ViewWidth,
|
"PageWidth": e.ViewWidth,
|
||||||
"PageHeight": e.Image.ViewHeight,
|
"PageHeight": e.ViewHeight,
|
||||||
})},
|
})},
|
||||||
{"OEBPS/Text/title.xhtml", e.render(epubtemplates.Text, map[string]any{
|
{"OEBPS/Text/title.xhtml", e.render(templates.Text, map[string]any{
|
||||||
"Title": title,
|
"Title": title,
|
||||||
"ViewPort": fmt.Sprintf("width=%d,height=%d", e.Image.ViewWidth, e.Image.ViewHeight),
|
"ViewPort": fmt.Sprintf("width=%d,height=%d", e.ViewWidth, e.ViewHeight),
|
||||||
"ImagePath": "Images/title.jpg",
|
"ImagePath": "Images/title.jpg",
|
||||||
"ImageStyle": part.Cover.ImgStyle(e.Image.ViewWidth, e.Image.ViewHeight, e.Image.Manga),
|
"ImageStyle": part.Cover.ImgStyle(e.ViewWidth, e.ViewHeight, e.Manga),
|
||||||
})},
|
})},
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -252,13 +266,13 @@ func (e *ePub) Write() error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if err := wz.WriteImage(e.createTitleImageData(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
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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.Image.HasCover || i > 0 {
|
if e.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
|
||||||
}
|
}
|
||||||
|
@ -4,19 +4,12 @@ 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 {
|
||||||
@ -38,7 +31,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.Image.ViewWidth, e.Image.ViewHeight)}, ""},
|
{"opf:meta", TagAttrs{"name": "original-resolution", "content": fmt.Sprintf("%dx%d", e.ViewWidth, e.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"},
|
||||||
@ -48,7 +41,7 @@ func (e *ePub) getMeta(title string, part *epubPart, currentPart, totalPart int)
|
|||||||
{"dc:date", TagAttrs{}, e.UpdatedAt},
|
{"dc:date", TagAttrs{}, e.UpdatedAt},
|
||||||
}
|
}
|
||||||
|
|
||||||
if e.Image.Manga {
|
if e.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"}, ""})
|
||||||
@ -70,13 +63,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 *epubimage.Image) Tag {
|
iTag := func(img *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 *epubimage.Image) Tag {
|
hTag := func(img *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 *epubimage.Image) Tag {
|
sTag := func(img *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{
|
||||||
@ -86,7 +79,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.Image.HasCover || currentPart > 1 {
|
if e.HasCover || currentPart > 1 {
|
||||||
items = append(items, iTag(part.Cover), hTag(part.Cover))
|
items = append(items, iTag(part.Cover), hTag(part.Cover))
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -102,12 +95,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.Image.Manga
|
isOnTheRight := !e.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.Image.Manga
|
isOnTheRight = !e.Manga
|
||||||
return "rendition:page-spread-center"
|
return "rendition:page-spread-center"
|
||||||
}
|
}
|
||||||
if isOnTheRight {
|
if isOnTheRight {
|
||||||
@ -123,10 +116,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.Image.NoBlankPage)},
|
TagAttrs{"idref": img.Key("page"), "properties": getSpread(img.DoublePage && e.NoBlankPage)},
|
||||||
"",
|
"",
|
||||||
})
|
})
|
||||||
if img.DoublePage && isOnTheRight && !e.Image.NoBlankPage {
|
if img.DoublePage && isOnTheRight && !e.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)},
|
||||||
@ -134,7 +127,7 @@ func (e *ePub) getSpine(title string, part *epubPart, currentPart, totalPart int
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if e.Image.Manga == isOnTheRight {
|
if e.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)},
|
||||||
@ -186,7 +179,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.Image.Manga {
|
if e.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")
|
||||||
@ -200,3 +193,9 @@ 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
|
||||||
|
}
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
package epubimagedata
|
package epub
|
||||||
|
|
||||||
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 New(id int, part int, img image.Image, quality int) *ImageData {
|
func newImageData(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 NewRaw(name, img, quality)
|
return newData(name, img, quality)
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewRaw(name string, img image.Image, quality int) *ImageData {
|
func newData(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)
|
@ -1,16 +1,16 @@
|
|||||||
package epubimage
|
package epub
|
||||||
|
|
||||||
import (
|
import (
|
||||||
epubfilters "github.com/celogeek/go-comic-converter/v2/internal/epub/filters"
|
"github.com/celogeek/go-comic-converter/v2/internal/epub/filters"
|
||||||
"github.com/disintegration/gift"
|
"github.com/disintegration/gift"
|
||||||
)
|
)
|
||||||
|
|
||||||
func NewGift(options *Options) *gift.GIFT {
|
func NewGift(options *ImageOptions) *gift.GIFT {
|
||||||
g := gift.New()
|
g := gift.New()
|
||||||
g.SetParallelization(false)
|
g.SetParallelization(false)
|
||||||
|
|
||||||
if options.AutoRotate {
|
if options.AutoRotate {
|
||||||
g.Add(epubfilters.AutoRotate(options.ViewWidth, options.ViewHeight))
|
g.Add(filters.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 *Options) *gift.GIFT {
|
|||||||
g.Add(gift.Brightness(float32(options.Brightness)))
|
g.Add(gift.Brightness(float32(options.Brightness)))
|
||||||
}
|
}
|
||||||
g.Add(
|
g.Add(
|
||||||
epubfilters.Resize(options.ViewWidth, options.ViewHeight, gift.LanczosResampling),
|
filters.Resize(options.ViewWidth, options.ViewHeight, gift.LanczosResampling),
|
||||||
epubfilters.Pixel(),
|
filters.Pixel(),
|
||||||
)
|
)
|
||||||
return g
|
return g
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewGiftSplitDoublePage(options *Options) []*gift.GIFT {
|
func NewGiftSplitDoublePage(options *ImageOptions) []*gift.GIFT {
|
||||||
gifts := make([]*gift.GIFT, 2)
|
gifts := make([]*gift.GIFT, 2)
|
||||||
|
|
||||||
gifts[0] = gift.New(
|
gifts[0] = gift.New(
|
||||||
epubfilters.CropSplitDoublePage(options.Manga),
|
filters.CropSplitDoublePage(options.Manga),
|
||||||
)
|
)
|
||||||
|
|
||||||
gifts[1] = gift.New(
|
gifts[1] = gift.New(
|
||||||
epubfilters.CropSplitDoublePage(!options.Manga),
|
filters.CropSplitDoublePage(!options.Manga),
|
||||||
)
|
)
|
||||||
|
|
||||||
for _, g := range gifts {
|
for _, g := range gifts {
|
||||||
@ -45,7 +45,7 @@ func NewGiftSplitDoublePage(options *Options) []*gift.GIFT {
|
|||||||
}
|
}
|
||||||
|
|
||||||
g.Add(
|
g.Add(
|
||||||
epubfilters.Resize(options.ViewWidth, options.ViewHeight, gift.LanczosResampling),
|
filters.Resize(options.ViewWidth, options.ViewHeight, gift.LanczosResampling),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
@ -17,9 +17,7 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
epubimage "github.com/celogeek/go-comic-converter/v2/internal/epub/image"
|
"github.com/celogeek/go-comic-converter/v2/internal/epub/sortpath"
|
||||||
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"
|
||||||
@ -32,6 +30,69 @@ 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
|
||||||
@ -90,8 +151,8 @@ BOTTOM:
|
|||||||
return imgArea
|
return imgArea
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *ePub) LoadImages() ([]*epubimage.Image, error) {
|
func (e *ePub) LoadImages() ([]*Image, error) {
|
||||||
images := make([]*epubimage.Image, 0)
|
images := make([]*Image, 0)
|
||||||
|
|
||||||
fi, err := os.Stat(e.Input)
|
fi, err := os.Stat(e.Input)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -124,7 +185,7 @@ func (e *ePub) LoadImages() ([]*epubimage.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, &epubimage.Image{
|
images = append(images, &Image{
|
||||||
Id: img.Id,
|
Id: img.Id,
|
||||||
Path: img.Path,
|
Path: img.Path,
|
||||||
Name: img.Name,
|
Name: img.Name,
|
||||||
@ -134,13 +195,13 @@ func (e *ePub) LoadImages() ([]*epubimage.Image, error) {
|
|||||||
return images, nil
|
return images, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
imageOutput := make(chan *epubimage.Image)
|
imageOutput := make(chan *Image)
|
||||||
|
|
||||||
// processing
|
// processing
|
||||||
bar := e.NewBar(imageCount, "Processing", 1, 2)
|
bar := NewBar(e.Quiet, imageCount, "Processing", 1, 2)
|
||||||
wg := &sync.WaitGroup{}
|
wg := &sync.WaitGroup{}
|
||||||
|
|
||||||
for i := 0; i < e.Workers; i++ {
|
for i := 0; i < e.ImageOptions.Workers; i++ {
|
||||||
wg.Add(1)
|
wg.Add(1)
|
||||||
go func() {
|
go func() {
|
||||||
defer wg.Done()
|
defer wg.Done()
|
||||||
@ -155,14 +216,14 @@ func (e *ePub) LoadImages() ([]*epubimage.Image, error) {
|
|||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
if e.Image.Crop {
|
if e.ImageOptions.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 := epubimage.NewGift(e.Options.Image)
|
g := NewGift(e.ImageOptions)
|
||||||
|
|
||||||
// Convert image
|
// Convert image
|
||||||
dst := image.NewGray(g.Bounds(src.Bounds()))
|
dst := image.NewGray(g.Bounds(src.Bounds()))
|
||||||
@ -173,17 +234,17 @@ func (e *ePub) LoadImages() ([]*epubimage.Image, error) {
|
|||||||
raw = dst
|
raw = dst
|
||||||
}
|
}
|
||||||
|
|
||||||
imageOutput <- &epubimage.Image{
|
imageOutput <- &Image{
|
||||||
Id: img.Id,
|
Id: img.Id,
|
||||||
Part: 0,
|
Part: 0,
|
||||||
Raw: raw,
|
Raw: raw,
|
||||||
Data: epubimagedata.New(img.Id, 0, dst, e.Image.Quality),
|
Data: newImageData(img.Id, 0, dst, e.ImageOptions.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.Image.ViewHeight &&
|
src.Bounds().Dx() > e.ImageOptions.ViewHeight &&
|
||||||
src.Bounds().Dy() > e.Image.ViewWidth,
|
src.Bounds().Dy() > e.ImageOptions.ViewWidth,
|
||||||
Path: img.Path,
|
Path: img.Path,
|
||||||
Name: img.Name,
|
Name: img.Name,
|
||||||
}
|
}
|
||||||
@ -191,21 +252,21 @@ func (e *ePub) LoadImages() ([]*epubimage.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.Image.HasCover || img.Id > 0) &&
|
if (!e.ImageOptions.HasCover || img.Id > 0) &&
|
||||||
e.Image.AutoSplitDoublePage &&
|
e.ImageOptions.AutoSplitDoublePage &&
|
||||||
src.Bounds().Dx() > src.Bounds().Dy() &&
|
src.Bounds().Dx() > src.Bounds().Dy() &&
|
||||||
src.Bounds().Dx() > e.Image.ViewHeight &&
|
src.Bounds().Dx() > e.ImageOptions.ViewHeight &&
|
||||||
src.Bounds().Dy() > e.Image.ViewWidth {
|
src.Bounds().Dy() > e.ImageOptions.ViewWidth {
|
||||||
gifts := epubimage.NewGiftSplitDoublePage(e.Options.Image)
|
gifts := NewGiftSplitDoublePage(e.ImageOptions)
|
||||||
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 <- &epubimage.Image{
|
imageOutput <- &Image{
|
||||||
Id: img.Id,
|
Id: img.Id,
|
||||||
Part: part,
|
Part: part,
|
||||||
Data: epubimagedata.New(img.Id, part, dst, e.Image.Quality),
|
Data: newImageData(img.Id, part, dst, e.ImageOptions.Quality),
|
||||||
Width: dst.Bounds().Dx(),
|
Width: dst.Bounds().Dx(),
|
||||||
Height: dst.Bounds().Dy(),
|
Height: dst.Bounds().Dy(),
|
||||||
IsCover: false,
|
IsCover: false,
|
||||||
@ -224,11 +285,11 @@ func (e *ePub) LoadImages() ([]*epubimage.Image, error) {
|
|||||||
close(imageOutput)
|
close(imageOutput)
|
||||||
}()
|
}()
|
||||||
|
|
||||||
for img := range imageOutput {
|
for image := range imageOutput {
|
||||||
if !(e.Image.NoBlankPage && img.Width == 1 && img.Height == 1) {
|
if !(e.ImageOptions.NoBlankPage && image.Width == 1 && image.Height == 1) {
|
||||||
images = append(images, img)
|
images = append(images, image)
|
||||||
}
|
}
|
||||||
if img.Part == 0 {
|
if image.Part == 0 {
|
||||||
bar.Add(1)
|
bar.Add(1)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -458,7 +519,7 @@ func loadPdf(input string) (int, chan *imageTask, error) {
|
|||||||
return nbPages, output, nil
|
return nbPages, output, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *ePub) createTitleImageData(title string, img *epubimage.Image, currentPart, totalPart int) *epubimagedata.ImageData {
|
func (e *ePub) createTitleImageDate(title string, img *Image, currentPart, totalPart int) *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()))
|
||||||
@ -515,5 +576,5 @@ func (e *ePub) createTitleImageData(title string, img *epubimage.Image, currentP
|
|||||||
}
|
}
|
||||||
c.DrawString(title, freetype.Pt(textLeft, img.Height/2+textHeight/4))
|
c.DrawString(title, freetype.Pt(textLeft, img.Height/2+textHeight/4))
|
||||||
|
|
||||||
return epubimagedata.NewRaw("OEBPS/Images/title.jpg", dst, e.Image.Quality)
|
return newData("OEBPS/Images/title.jpg", dst, e.Quality)
|
||||||
}
|
}
|
||||||
|
@ -7,8 +7,8 @@ import (
|
|||||||
"github.com/schollz/progressbar/v3"
|
"github.com/schollz/progressbar/v3"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (e *ePub) NewBar(max int, description string, currentJob, totalJob int) *progressbar.ProgressBar {
|
func NewBar(quiet bool, max int, description string, currentJob, totalJob int) *progressbar.ProgressBar {
|
||||||
if e.Quiet {
|
if 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)))
|
||||||
|
@ -5,10 +5,9 @@ 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 []*epubimage.Image) string {
|
func (e *ePub) getToc(title string, images []*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")
|
||||||
|
@ -2,13 +2,58 @@ 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"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func (e *ePub) getTree(images []*epubimage.Image, skip_files bool) string {
|
type Tree struct {
|
||||||
t := epubtree.New()
|
Nodes map[string]*Node
|
||||||
|
}
|
||||||
|
|
||||||
|
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)
|
||||||
@ -21,5 +66,5 @@ func (e *ePub) getTree(images []*epubimage.Image, skip_files bool) string {
|
|||||||
c = c.Children[0]
|
c = c.Children[0]
|
||||||
}
|
}
|
||||||
|
|
||||||
return c.ToString("")
|
return c.toString("")
|
||||||
}
|
}
|
||||||
|
@ -1,36 +1,34 @@
|
|||||||
package epubzip
|
package epub
|
||||||
|
|
||||||
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 New(path string) (*EpubZip, error) {
|
func newEpubZip(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",
|
||||||
@ -52,7 +50,7 @@ func (e *EpubZip) WriteMagic() error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *EpubZip) WriteImage(image *epubimagedata.ImageData) error {
|
func (e *epubZip) WriteImage(image *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
|
||||||
@ -61,7 +59,7 @@ func (e *EpubZip) WriteImage(image *epubimagedata.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:
|
@ -1,4 +1,4 @@
|
|||||||
package epubfilters
|
package filters
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"image"
|
"image"
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
package epubfilters
|
package filters
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"image"
|
"image"
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
package epubfilters
|
package filters
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"image"
|
"image"
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
package epubfilters
|
package filters
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"image"
|
"image"
|
||||||
|
@ -1,71 +0,0 @@
|
|||||||
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)
|
|
||||||
}
|
|
@ -1,15 +0,0 @@
|
|||||||
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
|
|
||||||
}
|
|
@ -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
|
||||||
}
|
}
|
@ -1,4 +1,4 @@
|
|||||||
package epubtemplates
|
package templates
|
||||||
|
|
||||||
import _ "embed"
|
import _ "embed"
|
||||||
|
|
||||||
|
@ -1,53 +0,0 @@
|
|||||||
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()
|
|
||||||
}
|
|
7
main.go
7
main.go
@ -7,7 +7,6 @@ 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"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -94,7 +93,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.New(&epub.Options{
|
if err := epub.NewEpub(&epub.EpubOptions{
|
||||||
Input: cmd.Options.Input,
|
Input: cmd.Options.Input,
|
||||||
Output: cmd.Options.Output,
|
Output: cmd.Options.Output,
|
||||||
LimitMb: cmd.Options.LimitMb,
|
LimitMb: cmd.Options.LimitMb,
|
||||||
@ -102,7 +101,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,
|
||||||
Image: &epubimage.Options{
|
ImageOptions: &epub.ImageOptions{
|
||||||
ViewWidth: perfectWidth,
|
ViewWidth: perfectWidth,
|
||||||
ViewHeight: perfectHeight,
|
ViewHeight: perfectHeight,
|
||||||
Quality: cmd.Options.Quality,
|
Quality: cmd.Options.Quality,
|
||||||
@ -114,8 +113,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,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user