diff --git a/internal/epub/epub.go b/internal/epub/epub.go index d3e288c..3c78fd6 100644 --- a/internal/epub/epub.go +++ b/internal/epub/epub.go @@ -10,28 +10,13 @@ import ( "text/template" "time" - "github.com/celogeek/go-comic-converter/v2/internal/epub/epubzip" - "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" ) -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 { +type Options struct { Input string Output string Title string @@ -42,12 +27,12 @@ type EpubOptions struct { DryVerbose bool SortPathMode int Quiet bool - - *ImageOptions + Workers int + Image *epubimage.Options } type ePub struct { - *EpubOptions + *Options UID string Publisher string UpdatedAt string @@ -56,11 +41,11 @@ type ePub struct { } type epubPart struct { - Cover *Image - Images []*Image + Cover *epubimage.Image + Images []*epubimage.Image } -func NewEpub(options *EpubOptions) *ePub { +func New(options *Options) *ePub { uid, err := uuid.NewV4() if err != nil { panic(err) @@ -73,7 +58,7 @@ func NewEpub(options *EpubOptions) *ePub { }) return &ePub{ - EpubOptions: options, + Options: options, UID: uid.String(), Publisher: "GO Comic Converter", UpdatedAt: time.Now().UTC().Format("2006-01-02T15:04:05Z"), @@ -96,14 +81,14 @@ func (e *ePub) render(templateString string, data any) string { return stripBlank.ReplaceAllString(result.String(), "\n") } -func (e *ePub) writeImage(wz *epubzip.EpubZip, img *Image) error { +func (e *ePub) writeImage(wz *epubzip.EpubZip, img *epubimage.Image) error { err := wz.WriteFile( 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), - "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(), - "ImageStyle": img.ImgStyle(e.ViewWidth, e.ViewHeight, e.Manga), + "ImageStyle": img.ImgStyle(e.Image.ViewWidth, e.Image.ViewHeight, e.Image.Manga), }), ) @@ -114,12 +99,12 @@ func (e *ePub) writeImage(wz *epubzip.EpubZip, img *Image) error { return err } -func (e *ePub) writeBlank(wz *epubzip.EpubZip, img *Image) error { +func (e *ePub) writeBlank(wz *epubzip.EpubZip, img *epubimage.Image) error { return wz.WriteFile( 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), - "ViewPort": fmt.Sprintf("width=%d,height=%d", e.ViewWidth, e.ViewHeight), + "ViewPort": fmt.Sprintf("width=%d,height=%d", e.Image.ViewWidth, e.Image.ViewHeight), }), ) } @@ -143,7 +128,7 @@ func (e *ePub) getParts() ([]*epubPart, error) { parts := make([]*epubPart, 0) cover := images[0] - if e.HasCover { + if e.Image.HasCover { images = images[1:] } @@ -160,12 +145,12 @@ func (e *ePub) getParts() ([]*epubPart, error) { xhtmlSize := uint64(1024) // descriptor files + title baseSize := uint64(16*1024) + cover.Data.CompressedSize() - if e.HasCover { + if e.Image.HasCover { baseSize += cover.Data.CompressedSize() } currentSize := baseSize - currentImages := make([]*Image, 0) + currentImages := make([]*epubimage.Image, 0) part := 1 for _, img := range images { @@ -177,10 +162,10 @@ func (e *ePub) getParts() ([]*epubPart, error) { }) part += 1 currentSize = baseSize - if !e.HasCover { + if !e.Image.HasCover { currentSize += cover.Data.CompressedSize() } - currentImages = make([]*Image, 0) + currentImages = make([]*epubimage.Image, 0) } currentSize += imgSize currentImages = append(currentImages, img) @@ -210,8 +195,8 @@ func (e *ePub) Write() error { p := epubParts[0] fmt.Fprintf(os.Stderr, "TOC:\n - %s\n%s\n", e.Title, e.getTree(p.Images, true)) if e.DryVerbose { - if e.HasCover { - fmt.Fprintf(os.Stderr, "Cover:\n%s\n", e.getTree([]*Image{p.Cover}, false)) + if e.Image.HasCover { + 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)) } @@ -220,7 +205,7 @@ func (e *ePub) Write() error { 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 { ext := filepath.Ext(e.Output) suffix := "" @@ -243,19 +228,19 @@ func (e *ePub) Write() error { } content := []zipContent{ - {"META-INF/container.xml", templates.Container}, - {"META-INF/com.apple.ibooks.display-options.xml", templates.AppleBooks}, + {"META-INF/container.xml", epubtemplates.Container}, + {"META-INF/com.apple.ibooks.display-options.xml", epubtemplates.AppleBooks}, {"OEBPS/content.opf", e.getContent(title, part, i+1, totalParts).String()}, {"OEBPS/toc.xhtml", e.getToc(title, part.Images)}, - {"OEBPS/Text/style.css", e.render(templates.Style, map[string]any{ - "PageWidth": e.ViewWidth, - "PageHeight": e.ViewHeight, + {"OEBPS/Text/style.css", e.render(epubtemplates.Style, map[string]any{ + "PageWidth": e.Image.ViewWidth, + "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, - "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", - "ImageStyle": part.Cover.ImgStyle(e.ViewWidth, e.ViewHeight, e.Manga), + "ImageStyle": part.Cover.ImgStyle(e.Image.ViewWidth, e.Image.ViewHeight, e.Image.Manga), })}, } @@ -267,13 +252,13 @@ func (e *ePub) Write() error { 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 } // Cover exist or part > 1 // 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 { return err } diff --git a/internal/epub/epub_content.go b/internal/epub/epub_content.go index ab7e754..9cbc44c 100644 --- a/internal/epub/epub_content.go +++ b/internal/epub/epub_content.go @@ -4,12 +4,19 @@ import ( "fmt" "github.com/beevik/etree" + epubimage "github.com/celogeek/go-comic-converter/v2/internal/epub/image" ) type Content struct { doc *etree.Document } +func (c *Content) String() string { + c.doc.Indent(2) + r, _ := c.doc.WriteToString() + return r +} + type TagAttrs map[string]string 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{"name": "book-type", "content": "comic"}, ""}, {"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:identifier", TagAttrs{"id": "ean"}, fmt.Sprintf("urn:uuid:%s", e.UID)}, {"dc:language", TagAttrs{}, "en"}, @@ -41,7 +48,7 @@ func (e *ePub) getMeta(title string, part *epubPart, currentPart, totalPart int) {"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"}, ""}) } else { 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 { - 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"}, ""} } - 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"}, ""} } - 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"}, ""} } 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"}, ""}, } - if e.HasCover || currentPart > 1 { + if e.Image.HasCover || currentPart > 1 { 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 { - isOnTheRight := !e.Manga + isOnTheRight := !e.Image.Manga getSpread := func(doublePageNoBlank bool) string { isOnTheRight = !isOnTheRight if doublePageNoBlank { // Center the double page then start back to comic mode (mange/normal) - isOnTheRight = !e.Manga + isOnTheRight = !e.Image.Manga return "rendition:page-spread-center" } if isOnTheRight { @@ -116,10 +123,10 @@ func (e *ePub) getSpine(title string, part *epubPart, currentPart, totalPart int for _, img := range part.Images { spine = append(spine, Tag{ "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{ "itemref", 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{ "itemref", 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) spine := pkg.CreateElement("spine") - if e.Manga { + if e.Image.Manga { spine.CreateAttr("page-progression-direction", "rtl") } else { spine.CreateAttr("page-progression-direction", "ltr") @@ -193,9 +200,3 @@ func (e *ePub) getContent(title string, part *epubPart, currentPart, totalPart i doc, } } - -func (c *Content) String() string { - c.doc.Indent(2) - r, _ := c.doc.WriteToString() - return r -} diff --git a/internal/epub/epub_image_processing.go b/internal/epub/epub_image_processing.go index 01eb8c8..207f4d8 100644 --- a/internal/epub/epub_image_processing.go +++ b/internal/epub/epub_image_processing.go @@ -17,8 +17,9 @@ import ( "strings" "sync" - "github.com/celogeek/go-comic-converter/v2/internal/epub/imagedata" - "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/golang/freetype" "github.com/golang/freetype/truetype" @@ -31,69 +32,6 @@ import ( _ "golang.org/x/image/webp" ) -type Image struct { - Id int - Part int - Raw image.Image - Data *imagedata.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 { Id int Reader io.ReadCloser @@ -152,8 +90,8 @@ BOTTOM: return imgArea } -func (e *ePub) LoadImages() ([]*Image, error) { - images := make([]*Image, 0) +func (e *ePub) LoadImages() ([]*epubimage.Image, error) { + images := make([]*epubimage.Image, 0) fi, err := os.Stat(e.Input) if err != nil { @@ -186,7 +124,7 @@ func (e *ePub) LoadImages() ([]*Image, error) { if e.Dry { for img := range imageInput { img.Reader.Close() - images = append(images, &Image{ + images = append(images, &epubimage.Image{ Id: img.Id, Path: img.Path, Name: img.Name, @@ -196,13 +134,13 @@ func (e *ePub) LoadImages() ([]*Image, error) { return images, nil } - imageOutput := make(chan *Image) + imageOutput := make(chan *epubimage.Image) // processing - bar := NewBar(e.Quiet, imageCount, "Processing", 1, 2) + bar := e.NewBar(imageCount, "Processing", 1, 2) wg := &sync.WaitGroup{} - for i := 0; i < e.ImageOptions.Workers; i++ { + for i := 0; i < e.Workers; i++ { wg.Add(1) go func() { defer wg.Done() @@ -217,14 +155,14 @@ func (e *ePub) LoadImages() ([]*Image, error) { os.Exit(1) } - if e.ImageOptions.Crop { + if e.Image.Crop { g := gift.New(gift.Crop(findMarging(src))) newSrc := image.NewNRGBA(g.Bounds(src.Bounds())) g.Draw(newSrc, src) src = newSrc } - g := NewGift(e.ImageOptions) + g := epubimage.NewGift(e.Options.Image) // Convert image dst := image.NewGray(g.Bounds(src.Bounds())) @@ -235,17 +173,17 @@ func (e *ePub) LoadImages() ([]*Image, error) { raw = dst } - imageOutput <- &Image{ + imageOutput <- &epubimage.Image{ Id: img.Id, Part: 0, Raw: raw, - Data: imagedata.New(img.Id, 0, dst, e.ImageOptions.Quality), + Data: epubimagedata.New(img.Id, 0, dst, e.Image.Quality), Width: dst.Bounds().Dx(), Height: dst.Bounds().Dy(), IsCover: img.Id == 0, DoublePage: src.Bounds().Dx() > src.Bounds().Dy() && - src.Bounds().Dx() > e.ImageOptions.ViewHeight && - src.Bounds().Dy() > e.ImageOptions.ViewWidth, + src.Bounds().Dx() > e.Image.ViewHeight && + src.Bounds().Dy() > e.Image.ViewWidth, Path: img.Path, Name: img.Name, } @@ -253,21 +191,21 @@ func (e *ePub) LoadImages() ([]*Image, error) { // Auto split double page // Except for cover // Only if the src image have width > height and is bigger than the view - if (!e.ImageOptions.HasCover || img.Id > 0) && - e.ImageOptions.AutoSplitDoublePage && + if (!e.Image.HasCover || img.Id > 0) && + e.Image.AutoSplitDoublePage && src.Bounds().Dx() > src.Bounds().Dy() && - src.Bounds().Dx() > e.ImageOptions.ViewHeight && - src.Bounds().Dy() > e.ImageOptions.ViewWidth { - gifts := NewGiftSplitDoublePage(e.ImageOptions) + src.Bounds().Dx() > e.Image.ViewHeight && + src.Bounds().Dy() > e.Image.ViewWidth { + gifts := epubimage.NewGiftSplitDoublePage(e.Options.Image) for i, g := range gifts { part := i + 1 dst := image.NewGray(g.Bounds(src.Bounds())) g.Draw(dst, src) - imageOutput <- &Image{ + imageOutput <- &epubimage.Image{ Id: img.Id, Part: part, - Data: imagedata.New(img.Id, part, dst, e.ImageOptions.Quality), + Data: epubimagedata.New(img.Id, part, dst, e.Image.Quality), Width: dst.Bounds().Dx(), Height: dst.Bounds().Dy(), IsCover: false, @@ -286,11 +224,11 @@ func (e *ePub) LoadImages() ([]*Image, error) { close(imageOutput) }() - for image := range imageOutput { - if !(e.ImageOptions.NoBlankPage && image.Width == 1 && image.Height == 1) { - images = append(images, image) + for img := range imageOutput { + if !(e.Image.NoBlankPage && img.Width == 1 && img.Height == 1) { + images = append(images, img) } - if image.Part == 0 { + if img.Part == 0 { bar.Add(1) } } @@ -520,7 +458,7 @@ func loadPdf(input string) (int, chan *imageTask, error) { return nbPages, output, nil } -func (e *ePub) createTitleImageDate(title string, img *Image, currentPart, totalPart int) *imagedata.ImageData { +func (e *ePub) createTitleImageData(title string, img *epubimage.Image, currentPart, totalPart int) *epubimagedata.ImageData { // Create a blur version of the cover g := gift.New(gift.GaussianBlur(8)) dst := image.NewGray(g.Bounds(img.Raw.Bounds())) @@ -577,5 +515,5 @@ func (e *ePub) createTitleImageDate(title string, img *Image, currentPart, total } c.DrawString(title, freetype.Pt(textLeft, img.Height/2+textHeight/4)) - return imagedata.NewRaw("OEBPS/Images/title.jpg", dst, e.Quality) + return epubimagedata.NewRaw("OEBPS/Images/title.jpg", dst, e.Image.Quality) } diff --git a/internal/epub/epub_progress.go b/internal/epub/epub_progress.go index d9d1790..0ba69ac 100644 --- a/internal/epub/epub_progress.go +++ b/internal/epub/epub_progress.go @@ -7,8 +7,8 @@ import ( "github.com/schollz/progressbar/v3" ) -func NewBar(quiet bool, max int, description string, currentJob, totalJob int) *progressbar.ProgressBar { - if quiet { +func (e *ePub) NewBar(max int, description string, currentJob, totalJob int) *progressbar.ProgressBar { + if e.Quiet { return progressbar.DefaultSilent(int64(max)) } fmtJob := fmt.Sprintf("%%0%dd", len(fmt.Sprint(totalJob))) diff --git a/internal/epub/epub_toc.go b/internal/epub/epub_toc.go index ef79cfd..bdf8607 100644 --- a/internal/epub/epub_toc.go +++ b/internal/epub/epub_toc.go @@ -5,9 +5,10 @@ import ( "strings" "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.CreateProcInst("xml", `version="1.0" encoding="UTF-8"`) doc.CreateDirective("DOCTYPE html") diff --git a/internal/epub/epub_tree.go b/internal/epub/epub_tree.go index dc329ed..015ef78 100644 --- a/internal/epub/epub_tree.go +++ b/internal/epub/epub_tree.go @@ -3,11 +3,12 @@ package epub import ( "path/filepath" - "github.com/celogeek/go-comic-converter/v2/internal/epub/tree" + 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 []*Image, skip_files bool) string { - t := tree.New() +func (e *ePub) getTree(images []*epubimage.Image, skip_files bool) string { + t := epubtree.New() for _, img := range images { if skip_files { t.Add(img.Path) diff --git a/internal/epub/filters/epub_filters_autorotate.go b/internal/epub/filters/epub_filters_autorotate.go index 36c47c2..886c4f8 100644 --- a/internal/epub/filters/epub_filters_autorotate.go +++ b/internal/epub/filters/epub_filters_autorotate.go @@ -1,4 +1,4 @@ -package filters +package epubfilters import ( "image" diff --git a/internal/epub/filters/epub_filters_crop.go b/internal/epub/filters/epub_filters_crop.go index a45cb21..87905f0 100644 --- a/internal/epub/filters/epub_filters_crop.go +++ b/internal/epub/filters/epub_filters_crop.go @@ -1,4 +1,4 @@ -package filters +package epubfilters import ( "image" diff --git a/internal/epub/filters/epub_filters_pixel.go b/internal/epub/filters/epub_filters_pixel.go index c9bc071..61b0a9a 100644 --- a/internal/epub/filters/epub_filters_pixel.go +++ b/internal/epub/filters/epub_filters_pixel.go @@ -1,4 +1,4 @@ -package filters +package epubfilters import ( "image" diff --git a/internal/epub/filters/epub_filters_resize.go b/internal/epub/filters/epub_filters_resize.go index c08a0e5..ddde7d4 100644 --- a/internal/epub/filters/epub_filters_resize.go +++ b/internal/epub/filters/epub_filters_resize.go @@ -1,4 +1,4 @@ -package filters +package epubfilters import ( "image" diff --git a/internal/epub/image/epub_image.go b/internal/epub/image/epub_image.go new file mode 100644 index 0000000..e47b019 --- /dev/null +++ b/internal/epub/image/epub_image.go @@ -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) +} diff --git a/internal/epub/epub_image_filters.go b/internal/epub/image/epub_image_filters.go similarity index 52% rename from internal/epub/epub_image_filters.go rename to internal/epub/image/epub_image_filters.go index e6022ff..0e73de0 100644 --- a/internal/epub/epub_image_filters.go +++ b/internal/epub/image/epub_image_filters.go @@ -1,16 +1,16 @@ -package epub +package epubimage 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" ) -func NewGift(options *ImageOptions) *gift.GIFT { +func NewGift(options *Options) *gift.GIFT { g := gift.New() g.SetParallelization(false) if options.AutoRotate { - g.Add(filters.AutoRotate(options.ViewWidth, options.ViewHeight)) + g.Add(epubfilters.AutoRotate(options.ViewWidth, options.ViewHeight)) } if options.Contrast != 0 { 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( - filters.Resize(options.ViewWidth, options.ViewHeight, gift.LanczosResampling), - filters.Pixel(), + epubfilters.Resize(options.ViewWidth, options.ViewHeight, gift.LanczosResampling), + epubfilters.Pixel(), ) return g } -func NewGiftSplitDoublePage(options *ImageOptions) []*gift.GIFT { +func NewGiftSplitDoublePage(options *Options) []*gift.GIFT { gifts := make([]*gift.GIFT, 2) gifts[0] = gift.New( - filters.CropSplitDoublePage(options.Manga), + epubfilters.CropSplitDoublePage(options.Manga), ) gifts[1] = gift.New( - filters.CropSplitDoublePage(!options.Manga), + epubfilters.CropSplitDoublePage(!options.Manga), ) for _, g := range gifts { @@ -45,7 +45,7 @@ func NewGiftSplitDoublePage(options *ImageOptions) []*gift.GIFT { } g.Add( - filters.Resize(options.ViewWidth, options.ViewHeight, gift.LanczosResampling), + epubfilters.Resize(options.ViewWidth, options.ViewHeight, gift.LanczosResampling), ) } diff --git a/internal/epub/image/epub_image_options.go b/internal/epub/image/epub_image_options.go new file mode 100644 index 0000000..26ed66d --- /dev/null +++ b/internal/epub/image/epub_image_options.go @@ -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 +} diff --git a/internal/epub/imagedata/epub_image_data.go b/internal/epub/imagedata/epub_image_data.go index a8d6c26..ee4c85d 100644 --- a/internal/epub/imagedata/epub_image_data.go +++ b/internal/epub/imagedata/epub_image_data.go @@ -1,4 +1,4 @@ -package imagedata +package epubimagedata import ( "archive/zip" diff --git a/internal/epub/templates/epub_templates.go b/internal/epub/templates/epub_templates.go index df65026..6976e6c 100644 --- a/internal/epub/templates/epub_templates.go +++ b/internal/epub/templates/epub_templates.go @@ -1,4 +1,4 @@ -package templates +package epubtemplates import _ "embed" diff --git a/internal/epub/tree/epub_tree.go b/internal/epub/tree/epub_tree.go index 3b69b54..7ccbcab 100644 --- a/internal/epub/tree/epub_tree.go +++ b/internal/epub/tree/epub_tree.go @@ -1,4 +1,4 @@ -package tree +package epubtree import ( "path/filepath" diff --git a/internal/epub/epubzip/epub_zip.go b/internal/epub/zip/epub_zip.go similarity index 90% rename from internal/epub/epubzip/epub_zip.go rename to internal/epub/zip/epub_zip.go index 37e9a05..7944d74 100644 --- a/internal/epub/epubzip/epub_zip.go +++ b/internal/epub/zip/epub_zip.go @@ -6,7 +6,7 @@ import ( "os" "time" - "github.com/celogeek/go-comic-converter/v2/internal/epub/imagedata" + epubimagedata "github.com/celogeek/go-comic-converter/v2/internal/epub/imagedata" ) type EpubZip struct { @@ -52,7 +52,7 @@ func (e *EpubZip) WriteMagic() error { return err } -func (e *EpubZip) WriteImage(image *imagedata.ImageData) error { +func (e *EpubZip) WriteImage(image *epubimagedata.ImageData) error { m, err := e.wz.CreateRaw(image.Header) if err != nil { return err diff --git a/internal/epub/sortpath/epub_sortpath.go b/internal/sortpath/sortpath.go similarity index 100% rename from internal/epub/sortpath/epub_sortpath.go rename to internal/sortpath/sortpath.go diff --git a/main.go b/main.go index 7916dd1..f2e87ba 100644 --- a/main.go +++ b/main.go @@ -7,6 +7,7 @@ import ( "github.com/celogeek/go-comic-converter/v2/internal/converter" "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" ) @@ -93,7 +94,7 @@ $ go install github.com/celogeek/go-comic-converter/v%d@%s profile := cmd.Options.GetProfile() perfectWidth, perfectHeight := profile.PerfectDim() - if err := epub.NewEpub(&epub.EpubOptions{ + if err := epub.New(&epub.Options{ Input: cmd.Options.Input, Output: cmd.Options.Output, LimitMb: cmd.Options.LimitMb, @@ -101,7 +102,7 @@ $ go install github.com/celogeek/go-comic-converter/v%d@%s Author: cmd.Options.Author, StripFirstDirectoryFromToc: cmd.Options.StripFirstDirectoryFromToc, SortPathMode: cmd.Options.SortPathMode, - ImageOptions: &epub.ImageOptions{ + Image: &epubimage.Options{ ViewWidth: perfectWidth, ViewHeight: perfectHeight, Quality: cmd.Options.Quality, @@ -113,8 +114,8 @@ $ go install github.com/celogeek/go-comic-converter/v%d@%s NoBlankPage: cmd.Options.NoBlankPage, Manga: cmd.Options.Manga, HasCover: cmd.Options.HasCover, - Workers: cmd.Options.Workers, }, + Workers: cmd.Options.Workers, Dry: cmd.Options.Dry, DryVerbose: cmd.Options.DryVerbose, Quiet: cmd.Options.Quiet,