From 8a77bda99ec873e5c87980c65983cd6cbf834005 Mon Sep 17 00:00:00 2001 From: celogeek <65178+celogeek@users.noreply.github.com> Date: Sun, 5 Mar 2023 17:59:50 +0100 Subject: [PATCH] simplify content opf, revamp needspace --- internal/epub/core.go | 33 ++-------- internal/epub/image_processing.go | 15 +++-- internal/epub/templates/content.opf.tmpl | 78 ++++++++++++------------ 3 files changed, 53 insertions(+), 73 deletions(-) diff --git a/internal/epub/core.go b/internal/epub/core.go index ae6e1dc..ebbdd8e 100644 --- a/internal/epub/core.go +++ b/internal/epub/core.go @@ -60,27 +60,10 @@ func NewEpub(options *EpubOptions) *ePub { panic(err) } - var spreadRight = options.Manga - tmpl := template.New("parser") tmpl.Funcs(template.FuncMap{ "mod": func(i, j int) bool { return i%j == 0 }, "zoom": func(s int, z float32) int { return int(float32(s) * z) }, - "spread": func() (spread string) { - if spreadRight { - spread = "right" - } else { - spread = "left" - } - spreadRight = !spreadRight - return - }, - "spread_blank": func(part int) bool { - if !options.NoBlankPage && part == 1 && spreadRight == options.Manga { - return true - } - return false - }, }) return &ePub{ @@ -118,14 +101,6 @@ func (e *ePub) getParts() ([]*epubPart, error) { if e.HasCover { images = images[1:] } - if e.LimitMb == 0 { - parts = append(parts, &epubPart{ - Cover: cover, - Images: images, - }) - return parts, nil - } - maxSize := uint64(e.LimitMb * 1024 * 1024) xhtmlSize := uint64(1024) @@ -135,20 +110,24 @@ func (e *ePub) getParts() ([]*epubPart, error) { currentSize := baseSize currentImages := make([]*Image, 0) part := 1 + imgIsOnRightSide := false for _, img := range images { imgSize := img.Data.CompressedSize() + xhtmlSize - if len(currentImages) > 0 && currentSize+imgSize > maxSize { + if maxSize > 0 && len(currentImages) > 0 && currentSize+imgSize > maxSize { parts = append(parts, &epubPart{ Cover: cover, Images: currentImages, }) part += 1 + imgIsOnRightSide = false currentSize = baseSize currentImages = make([]*Image, 0) } currentSize += imgSize + img.NeedSpace = img.Part == 1 && imgIsOnRightSide currentImages = append(currentImages, img) + imgIsOnRightSide = !imgIsOnRightSide } if len(currentImages) > 0 { parts = append(parts, &epubPart{ @@ -235,7 +214,7 @@ func (e *ePub) Write() error { return err } - if !e.NoBlankPage && img.Part == 1 { + if img.NeedSpace { if err := wz.WriteFile( fmt.Sprintf("OEBPS/Text/%d_sp.xhtml", img.Id), e.render(blankTmpl, map[string]any{ diff --git a/internal/epub/image_processing.go b/internal/epub/image_processing.go index a0b0616..0134f93 100644 --- a/internal/epub/image_processing.go +++ b/internal/epub/image_processing.go @@ -22,12 +22,13 @@ import ( ) type Image struct { - Id int - Part int - Data *ImageData - Width int - Height int - IsCover bool + Id int + Part int + Data *ImageData + Width int + Height int + IsCover bool + NeedSpace bool } type imageTask struct { @@ -154,6 +155,7 @@ func LoadImages(path string, options *ImageOptions) ([]*Image, error) { dst.Bounds().Dx(), dst.Bounds().Dy(), img.Id == 0, + false, } // Auto split double page @@ -176,6 +178,7 @@ func LoadImages(path string, options *ImageOptions) ([]*Image, error) { dst.Bounds().Dx(), dst.Bounds().Dy(), false, + false, // NeedSpace reajust during parts computation } } } diff --git a/internal/epub/templates/content.opf.tmpl b/internal/epub/templates/content.opf.tmpl index b41fc1f..ba7bb93 100644 --- a/internal/epub/templates/content.opf.tmpl +++ b/internal/epub/templates/content.opf.tmpl @@ -1,53 +1,51 @@ {{ $info := .Info }} - -{{ $info.Title }} -en-US -urn:uuid:{{ $info.UID }} -GO Comic Converter -GO Comic Converter -{{ $info.UpdatedAt }} - - - - - - - - - - - - - - - - - + + {{ $info.Title }} + en-US + urn:uuid:{{ $info.UID }} + GO Comic Converter + GO Comic Converter + {{ $info.UpdatedAt }} + + + + + portrait + {{ if eq $info.AddPanelView true }} - + {{ end }} - + + + + + +{{ if eq $info.AddPanelView true }} + +{{ end }} + {{ range .Images }} - {{ if eq .IsCover false }} - -{{ end }} -{{ if eq $info.NoBlankPage false }} -{{ if eq .Part 1 }} - + {{ end }} {{ end }} -{{ end }} - - - + {{ range .Images }} -{{ if spread_blank .Part }} - + +{{ if eq .NeedSpace true }} + {{ end }} - {{ end }} - + + + +{{ range .Images }} +{{ if eq .NeedSpace true }} + +{{ end }} + +{{ end }} +