From 6f74f959fdab0583fbaa9ad4d91f9273204aad17 Mon Sep 17 00:00:00 2001 From: celogeek <65178+celogeek@users.noreply.github.com> Date: Sat, 4 Mar 2023 23:14:43 +0100 Subject: [PATCH] support comic without cover --- README.md | 2 ++ internal/epub/core.go | 14 +++++++++++--- internal/epub/image_data.go | 4 ---- internal/epub/image_processing.go | 15 +++++++++------ internal/epub/templates/content.opf.tmpl | 4 +++- main.go | 5 +++++ 6 files changed, 30 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index 672647e..b3726b9 100644 --- a/README.md +++ b/README.md @@ -96,6 +96,8 @@ Usage of go-comic-converter: Manga mode (right to left) -noblankpage Remove blank pages + -nocover + Indicate if your comic doesn't have a cover. The first page will be used as a cover and include after the title. -nocrop Disable cropping -output string diff --git a/internal/epub/core.go b/internal/epub/core.go index 5cf21fe..36517c2 100644 --- a/internal/epub/core.go +++ b/internal/epub/core.go @@ -25,6 +25,7 @@ type ImageOptions struct { AutoSplitDoublePage bool NoBlankPage bool Manga bool + HasCover bool Workers int } @@ -113,7 +114,9 @@ func (e *ePub) getParts() ([]*epubPart, error) { parts := make([]*epubPart, 0) cover := images[0] - images = images[1:] + if e.HasCover { + images = images[1:] + } if e.LimitMb == 0 { parts = append(parts, &epubPart{ Cover: cover, @@ -185,7 +188,7 @@ func (e *ePub) Write() error { content := []zipContent{ {"META-INF/container.xml", containerTmpl}, - {"OEBPS/content.opf", e.render(contentTmpl, map[string]any{"Info": e, "Images": part.Images})}, + {"OEBPS/content.opf", e.render(contentTmpl, map[string]any{"Info": e, "Cover": part.Cover, "Images": part.Images})}, {"OEBPS/toc.ncx", e.render(tocTmpl, map[string]any{"Info": e})}, {"OEBPS/nav.xhtml", e.render(navTmpl, map[string]any{"Info": e})}, {"OEBPS/Text/style.css", styleTmpl}, @@ -204,7 +207,12 @@ func (e *ePub) Write() error { return err } } - wz.WriteImage(part.Cover.Data) + + // Cover exist or part > 1 + // If no cover, part 2 and more will include the image as a cover + if e.HasCover || i > 0 { + wz.WriteImage(part.Cover.Data) + } for _, img := range part.Images { if err := wz.WriteFile( diff --git a/internal/epub/image_data.go b/internal/epub/image_data.go index be8226b..ebd8538 100644 --- a/internal/epub/image_data.go +++ b/internal/epub/image_data.go @@ -22,10 +22,6 @@ func (img *ImageData) CompressedSize() uint64 { func newImageData(id int, part int, img image.Image, quality int) *ImageData { name := fmt.Sprintf("OEBPS/Images/%d_p%d.jpg", id, part) - if id == 0 { - name = "OEBPS/Images/cover.jpg" - } - data := bytes.NewBuffer([]byte{}) if err := jpeg.Encode(data, img, &jpeg.Options{Quality: quality}); err != nil { panic(err) diff --git a/internal/epub/image_processing.go b/internal/epub/image_processing.go index 72b1788..a0b0616 100644 --- a/internal/epub/image_processing.go +++ b/internal/epub/image_processing.go @@ -22,11 +22,12 @@ import ( ) type Image struct { - Id int - Part int - Data *ImageData - Width int - Height int + Id int + Part int + Data *ImageData + Width int + Height int + IsCover bool } type imageTask struct { @@ -152,12 +153,13 @@ func LoadImages(path string, options *ImageOptions) ([]*Image, error) { newImageData(img.Id, 0, dst, options.Quality), dst.Bounds().Dx(), dst.Bounds().Dy(), + img.Id == 0, } // Auto split double page // Except for cover // Only if the src image have width > height and is bigger than the view - if img.Id > 0 && + if (!options.HasCover || img.Id > 0) && options.AutoSplitDoublePage && src.Bounds().Dx() > src.Bounds().Dy() && src.Bounds().Dx() > options.ViewHeight && @@ -173,6 +175,7 @@ func LoadImages(path string, options *ImageOptions) ([]*Image, error) { newImageData(img.Id, part, dst, options.Quality), dst.Bounds().Dx(), dst.Bounds().Dy(), + false, } } } diff --git a/internal/epub/templates/content.opf.tmpl b/internal/epub/templates/content.opf.tmpl index 6a59158..d79a63e 100644 --- a/internal/epub/templates/content.opf.tmpl +++ b/internal/epub/templates/content.opf.tmpl @@ -23,12 +23,14 @@ - + {{ range .Images }} +{{ if eq .IsCover false }} +{{ end }} {{ if eq $info.NoBlankPage false }} {{ if eq .Part 1 }} diff --git a/main.go b/main.go index 5e2d22a..c640a93 100644 --- a/main.go +++ b/main.go @@ -71,6 +71,7 @@ type Option struct { AutoSplitDoublePage bool NoBlankPage bool Manga bool + NoCover bool Workers int LimitMb int } @@ -106,6 +107,7 @@ Options: AutoSplitDoublePage: %v NoBlankPage : %v Manga : %v + HasCover : %v LimitMb : %s Workers : %d `, @@ -122,6 +124,7 @@ Options: o.AutoSplitDoublePage, o.NoBlankPage, o.Manga, + !o.NoCover, limitmb, o.Workers, ) @@ -154,6 +157,7 @@ func main() { flag.BoolVar(&opt.AutoSplitDoublePage, "autosplitdoublepage", false, "Auto Split double page when width > height") flag.BoolVar(&opt.NoBlankPage, "noblankpage", false, "Remove blank pages") flag.BoolVar(&opt.Manga, "manga", false, "Manga mode (right to left)") + flag.BoolVar(&opt.NoCover, "nocover", false, "Indicate if your comic doesn't have a cover. The first page will be used as a cover and include after the title.") flag.IntVar(&opt.LimitMb, "limitmb", 0, "Limit size of the ePub: Default nolimit (0), Minimum 20") flag.IntVar(&opt.Workers, "workers", runtime.NumCPU(), "Number of workers") flag.Usage = func() { @@ -261,6 +265,7 @@ func main() { AutoSplitDoublePage: opt.AutoSplitDoublePage, NoBlankPage: opt.NoBlankPage, Manga: opt.Manga, + HasCover: !opt.NoCover, Workers: opt.Workers, }, }).Write(); err != nil {