simplify content opf, revamp needspace

This commit is contained in:
Celogeek 2023-03-05 17:59:50 +01:00
parent 393bba0266
commit 8a77bda99e
Signed by: celogeek
GPG Key ID: E6B7BDCFC446233A
3 changed files with 53 additions and 73 deletions

View File

@ -60,27 +60,10 @@ func NewEpub(options *EpubOptions) *ePub {
panic(err) panic(err)
} }
var spreadRight = options.Manga
tmpl := template.New("parser") tmpl := template.New("parser")
tmpl.Funcs(template.FuncMap{ tmpl.Funcs(template.FuncMap{
"mod": func(i, j int) bool { return i%j == 0 }, "mod": func(i, j int) bool { return i%j == 0 },
"zoom": func(s int, z float32) int { return int(float32(s) * z) }, "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{ return &ePub{
@ -118,14 +101,6 @@ func (e *ePub) getParts() ([]*epubPart, error) {
if e.HasCover { if e.HasCover {
images = images[1:] images = images[1:]
} }
if e.LimitMb == 0 {
parts = append(parts, &epubPart{
Cover: cover,
Images: images,
})
return parts, nil
}
maxSize := uint64(e.LimitMb * 1024 * 1024) maxSize := uint64(e.LimitMb * 1024 * 1024)
xhtmlSize := uint64(1024) xhtmlSize := uint64(1024)
@ -135,20 +110,24 @@ func (e *ePub) getParts() ([]*epubPart, error) {
currentSize := baseSize currentSize := baseSize
currentImages := make([]*Image, 0) currentImages := make([]*Image, 0)
part := 1 part := 1
imgIsOnRightSide := false
for _, img := range images { for _, img := range images {
imgSize := img.Data.CompressedSize() + xhtmlSize 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{ parts = append(parts, &epubPart{
Cover: cover, Cover: cover,
Images: currentImages, Images: currentImages,
}) })
part += 1 part += 1
imgIsOnRightSide = false
currentSize = baseSize currentSize = baseSize
currentImages = make([]*Image, 0) currentImages = make([]*Image, 0)
} }
currentSize += imgSize currentSize += imgSize
img.NeedSpace = img.Part == 1 && imgIsOnRightSide
currentImages = append(currentImages, img) currentImages = append(currentImages, img)
imgIsOnRightSide = !imgIsOnRightSide
} }
if len(currentImages) > 0 { if len(currentImages) > 0 {
parts = append(parts, &epubPart{ parts = append(parts, &epubPart{
@ -235,7 +214,7 @@ func (e *ePub) Write() error {
return err return err
} }
if !e.NoBlankPage && img.Part == 1 { if img.NeedSpace {
if err := wz.WriteFile( if err := wz.WriteFile(
fmt.Sprintf("OEBPS/Text/%d_sp.xhtml", img.Id), fmt.Sprintf("OEBPS/Text/%d_sp.xhtml", img.Id),
e.render(blankTmpl, map[string]any{ e.render(blankTmpl, map[string]any{

View File

@ -28,6 +28,7 @@ type Image struct {
Width int Width int
Height int Height int
IsCover bool IsCover bool
NeedSpace bool
} }
type imageTask struct { type imageTask struct {
@ -154,6 +155,7 @@ func LoadImages(path string, options *ImageOptions) ([]*Image, error) {
dst.Bounds().Dx(), dst.Bounds().Dx(),
dst.Bounds().Dy(), dst.Bounds().Dy(),
img.Id == 0, img.Id == 0,
false,
} }
// Auto split double page // Auto split double page
@ -176,6 +178,7 @@ func LoadImages(path string, options *ImageOptions) ([]*Image, error) {
dst.Bounds().Dx(), dst.Bounds().Dx(),
dst.Bounds().Dy(), dst.Bounds().Dy(),
false, false,
false, // NeedSpace reajust during parts computation
} }
} }
} }

View File

@ -8,46 +8,44 @@
<dc:contributor id="contributor">GO Comic Converter</dc:contributor> <dc:contributor id="contributor">GO Comic Converter</dc:contributor>
<dc:creator>GO Comic Converter</dc:creator> <dc:creator>GO Comic Converter</dc:creator>
<meta property="dcterms:modified">{{ $info.UpdatedAt }}</meta> <meta property="dcterms:modified">{{ $info.UpdatedAt }}</meta>
<meta name="cover" content="cover"/>
<meta name="fixed-layout" content="true"/> <meta name="fixed-layout" content="true"/>
<meta name="original-resolution" content="{{ $info.ViewWidth }}x{{ $info.ViewHeight }}"/> <meta name="original-resolution" content="{{ $info.ViewWidth }}x{{ $info.ViewHeight }}"/>
<meta name="book-type" content="comic"/> <meta name="book-type" content="comic"/>
<meta name="primary-writing-mode" content="horizontal-{{ if $info.Manga }}rl{{ else }}lr{{ end }}"/> <meta name="primary-writing-mode" content="horizontal-{{ if $info.Manga }}rl{{ else }}lr{{ end }}"/>
<meta name="zero-gutter" content="true"/> <meta property="rendition:orientation">portrait</meta>
<meta name="zero-margin" content="true"/>
<meta name="ke-border-color" content="#FFFFFF"/>
<meta name="ke-border-width" content="0"/>
<meta name="orientation-lock" content="portrait"/> <meta name="orientation-lock" content="portrait"/>
{{ if eq $info.AddPanelView true }}
<meta name="region-mag" content="true"/> <meta name="region-mag" content="true"/>
{{ end }}
</metadata> </metadata>
<manifest> <manifest>
<item id="ncx" href="toc.ncx" media-type="application/x-dtbncx+xml"/> <item id="ncx" href="toc.ncx" media-type="application/x-dtbncx+xml"/>
<item id="nav" href="nav.xhtml" properties="nav" media-type="application/xhtml+xml"/> <item id="nav" href="nav.xhtml" properties="nav" media-type="application/xhtml+xml"/>
<item id="cover" href="Images/{{ .Cover.Id }}_p{{ .Cover.Part }}.jpg" media-type="image/jpeg" properties="cover-image"/>
<item id="style_css" href="Text/style.css" media-type="text/css"/> <item id="style_css" href="Text/style.css" media-type="text/css"/>
{{ if eq $info.AddPanelView true }} {{ if eq $info.AddPanelView true }}
<item id="panelview_css" href="Text/panelview.css" media-type="text/css"/> <item id="panelview_css" href="Text/panelview.css" media-type="text/css"/>
{{ end }} {{ end }}
<item id="page_part" href="Text/part.xhtml" media-type="application/xhtml+xml"/> <item id="cover" href="Images/{{ .Cover.Id }}_p{{ .Cover.Part }}.jpg" media-type="image/jpeg" properties="cover-image"/>
{{ range .Images }} {{ range .Images }}
<item id="page_{{ .Id }}_p{{ .Part}}" href="Text/{{ .Id }}_p{{ .Part}}.xhtml" media-type="application/xhtml+xml"/>
{{ if eq .IsCover false }} {{ if eq .IsCover false }}
<item id="img_{{ .Id }}_p{{ .Part}}" href="Images/{{ .Id }}_p{{ .Part}}.jpg" media-type="image/jpeg"/> <item id="img_{{ .Id }}_p{{ .Part}}" href="Images/{{ .Id }}_p{{ .Part}}.jpg" media-type="image/jpeg"/>
{{ end }} {{ end }}
{{ if eq $info.NoBlankPage false }} {{ end }}
{{ if eq .Part 1 }} <item id="page_part" href="Text/part.xhtml" media-type="application/xhtml+xml"/>
{{ range .Images }}
<item id="page_{{ .Id }}_p{{ .Part}}" href="Text/{{ .Id }}_p{{ .Part}}.xhtml" media-type="application/xhtml+xml"/>
{{ if eq .NeedSpace true }}
<item id="page_{{ .Id }}_sp" href="Text/{{ .Id }}_sp.xhtml" media-type="application/xhtml+xml"/> <item id="page_{{ .Id }}_sp" href="Text/{{ .Id }}_sp.xhtml" media-type="application/xhtml+xml"/>
{{ end }} {{ end }}
{{ end }} {{ end }}
{{ end }}
</manifest> </manifest>
<spine page-progression-direction="{{ if $info.Manga }}rtl{{ else }}ltr{{ end }}" toc="ncx"> <spine toc="ncx" page-progression-direction="{{ if $info.Manga }}rtl{{ else }}ltr{{ end }}">
<itemref idref="page_part" linear="yes" properties="page-spread-{{ spread }}"/> <itemref idref="page_part" linear="yes"/>
{{ range .Images }} {{ range .Images }}
{{ if spread_blank .Part }} {{ if eq .NeedSpace true }}
<itemref idref="page_{{ .Id }}_sp" linear="yes" properties="page-spread-{{ spread }}"/> <itemref idref="page_{{ .Id }}_sp" linear="yes"/>
{{ end }} {{ end }}
<itemref idref="page_{{ .Id }}_p{{ .Part }}" linear="yes" properties="page-spread-{{ spread }}"/> <itemref idref="page_{{ .Id }}_p{{ .Part }}" linear="yes"/>
{{ end }} {{ end }}
</spine> </spine>
</package> </package>