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)
}
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{

View File

@ -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
}
}
}

View File

@ -1,53 +1,51 @@
<?xml version="1.0" encoding="UTF-8"?>
<package version="3.0" unique-identifier="BookID" xmlns="http://www.idpf.org/2007/opf">
{{ $info := .Info }}
<metadata xmlns:opf="http://www.idpf.org/2007/opf" xmlns:dc="http://purl.org/dc/elements/1.1/">
<dc:title>{{ $info.Title }}</dc:title>
<dc:language>en-US</dc:language>
<dc:identifier id="BookID">urn:uuid:{{ $info.UID }}</dc:identifier>
<dc:contributor id="contributor">GO Comic Converter</dc:contributor>
<dc:creator>GO Comic Converter</dc:creator>
<meta property="dcterms:modified">{{ $info.UpdatedAt }}</meta>
<meta name="cover" content="cover"/>
<meta name="fixed-layout" content="true"/>
<meta name="original-resolution" content="{{ $info.ViewWidth }}x{{ $info.ViewHeight }}"/>
<meta name="book-type" content="comic"/>
<meta name="primary-writing-mode" content="horizontal-{{ if $info.Manga }}rl{{ else }}lr{{ end }}"/>
<meta name="zero-gutter" content="true"/>
<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="region-mag" content="true"/>
</metadata>
<manifest>
<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="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"/>
<metadata xmlns:opf="http://www.idpf.org/2007/opf" xmlns:dc="http://purl.org/dc/elements/1.1/">
<dc:title>{{ $info.Title }}</dc:title>
<dc:language>en-US</dc:language>
<dc:identifier id="BookID">urn:uuid:{{ $info.UID }}</dc:identifier>
<dc:contributor id="contributor">GO Comic Converter</dc:contributor>
<dc:creator>GO Comic Converter</dc:creator>
<meta property="dcterms:modified">{{ $info.UpdatedAt }}</meta>
<meta name="fixed-layout" content="true"/>
<meta name="original-resolution" content="{{ $info.ViewWidth }}x{{ $info.ViewHeight }}"/>
<meta name="book-type" content="comic"/>
<meta name="primary-writing-mode" content="horizontal-{{ if $info.Manga }}rl{{ else }}lr{{ end }}"/>
<meta property="rendition:orientation">portrait</meta>
<meta name="orientation-lock" content="portrait"/>
{{ if eq $info.AddPanelView true }}
<item id="panelview_css" href="Text/panelview.css" media-type="text/css"/>
<meta name="region-mag" content="true"/>
{{ end }}
<item id="page_part" href="Text/part.xhtml" media-type="application/xhtml+xml"/>
</metadata>
<manifest>
<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="style_css" href="Text/style.css" media-type="text/css"/>
{{ if eq $info.AddPanelView true }}
<item id="panelview_css" href="Text/panelview.css" media-type="text/css"/>
{{ end }}
<item id="cover" href="Images/{{ .Cover.Id }}_p{{ .Cover.Part }}.jpg" media-type="image/jpeg" properties="cover-image"/>
{{ range .Images }}
<item id="page_{{ .Id }}_p{{ .Part}}" href="Text/{{ .Id }}_p{{ .Part}}.xhtml" media-type="application/xhtml+xml"/>
{{ if eq .IsCover false }}
<item id="img_{{ .Id }}_p{{ .Part}}" href="Images/{{ .Id }}_p{{ .Part}}.jpg" media-type="image/jpeg"/>
{{ end }}
{{ if eq $info.NoBlankPage false }}
{{ if eq .Part 1 }}
<item id="page_{{ .Id }}_sp" href="Text/{{ .Id }}_sp.xhtml" media-type="application/xhtml+xml"/>
<item id="img_{{ .Id }}_p{{ .Part}}" href="Images/{{ .Id }}_p{{ .Part}}.jpg" media-type="image/jpeg"/>
{{ end }}
{{ end }}
{{ end }}
</manifest>
<spine page-progression-direction="{{ if $info.Manga }}rtl{{ else }}ltr{{ end }}" toc="ncx">
<itemref idref="page_part" linear="yes" properties="page-spread-{{ spread }}"/>
<item id="page_part" href="Text/part.xhtml" media-type="application/xhtml+xml"/>
{{ range .Images }}
{{ if spread_blank .Part }}
<itemref idref="page_{{ .Id }}_sp" linear="yes" properties="page-spread-{{ spread }}"/>
<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"/>
{{ end }}
<itemref idref="page_{{ .Id }}_p{{ .Part }}" linear="yes" properties="page-spread-{{ spread }}"/>
{{ end }}
</spine>
</manifest>
<spine toc="ncx" page-progression-direction="{{ if $info.Manga }}rtl{{ else }}ltr{{ end }}">
<itemref idref="page_part" linear="yes"/>
{{ range .Images }}
{{ if eq .NeedSpace true }}
<itemref idref="page_{{ .Id }}_sp" linear="yes"/>
{{ end }}
<itemref idref="page_{{ .Id }}_p{{ .Part }}" linear="yes"/>
{{ end }}
</spine>
</package>