improve spread, and blank for double page rendering

When autosplitdoublepage is activated,
We will display a blank page after the full image,
if necessary, for good rendering on a reader that support,
double page rendering (like Apple Book).
This commit is contained in:
Celogeek 2023-01-16 00:20:38 +01:00
parent f1fd7a9b77
commit 21b099ab33
Signed by: celogeek
GPG Key ID: E6B7BDCFC446233A
4 changed files with 64 additions and 18 deletions

View File

@ -56,10 +56,27 @@ 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 part == 1 && spreadRight == options.Manga {
return true
}
return false
},
}) })
return &ePub{ return &ePub{
@ -186,13 +203,28 @@ func (e *ePub) Write() error {
wz.WriteImage(part.Cover.Data) wz.WriteImage(part.Cover.Data)
for _, img := range part.Images { for _, img := range part.Images {
text := fmt.Sprintf("OEBPS/Text/%d_p%d.xhtml", img.Id, img.Part) if err := wz.WriteFile(
if err := wz.WriteFile(text, e.render(textTmpl, map[string]any{ fmt.Sprintf("OEBPS/Text/%d_p%d.xhtml", img.Id, img.Part),
"Image": img, e.render(textTmpl, map[string]any{
"Manga": e.Manga, "Image": img,
})); err != nil { "Manga": e.Manga,
}),
); err != nil {
return err return err
} }
if img.Part == 1 {
if err := wz.WriteFile(
fmt.Sprintf("OEBPS/Text/%d_sp.xhtml", img.Id),
e.render(blankTmpl, map[string]any{
"Info": e,
"Image": img,
}),
); err != nil {
return err
}
}
if err := wz.WriteImage(img.Data); err != nil { if err := wz.WriteImage(img.Data); err != nil {
return err return err
} }

View File

@ -22,3 +22,6 @@ var partTmpl string
//go:embed "templates/text.xhtml.tmpl" //go:embed "templates/text.xhtml.tmpl"
var textTmpl string var textTmpl string
//go:embed "templates/blank.xhtml.tmpl"
var blankTmpl string

View File

@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops">
<head>
<title>Page {{ .Image.Id }} Space</title>
<link href="style.css" type="text/css" rel="stylesheet"/>
<meta name="viewport" content="width={{ .Info.ViewWidth }}, height={{ .Info.ViewHeight }}"/>
</head>
<body style="">
<div style="text-align:center;top:0.0%;">
<h1>{{ if .Info.Manga }}&#8592;{{ else }}&#8594;{{ end }}</h1>
</div>
</body>
</html>

View File

@ -28,21 +28,18 @@
{{ range .Images }} {{ range .Images }}
<item id="page_{{ .Id }}_p{{ .Part}}" href="Text/{{ .Id }}_p{{ .Part}}.xhtml" media-type="application/xhtml+xml"/> <item id="page_{{ .Id }}_p{{ .Part}}" href="Text/{{ .Id }}_p{{ .Part}}.xhtml" media-type="application/xhtml+xml"/>
<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"/>
{{ if eq .Part 1 }}
<item id="page_{{ .Id }}_sp" href="Text/{{ .Id }}_sp.xhtml" media-type="application/xhtml+xml"/>
{{ end }}
{{ end }} {{ end }}
</manifest> </manifest>
{{ if .Info.Manga }} <spine page-progression-direction="{{ if .Info.Manga }}rtl{{ else }}ltr{{ end }}" toc="ncx">
<spine page-progression-direction="rtl" toc="ncx"> <itemref idref="page_part" linear="yes" properties="page-spread-{{ spread }}"/>
<itemref idref="page_part" linear="yes" properties="page-spread-right"/> {{ range .Images }}
{{ range $idx, $ := .Images }} {{ if spread_blank .Part }}
<itemref idref="page_{{ $.Id }}_p{{ $.Part }}" linear="yes" properties="page-spread-{{ if mod $idx 2 }}left{{ else }}right{{ end }}"/> <itemref idref="page_{{ .Id }}_sp" linear="yes" properties="page-spread-{{ spread }}"/>
{{ end }}
<itemref idref="page_{{ .Id }}_p{{ .Part }}" linear="yes" properties="page-spread-{{ spread }}"/>
{{ end }} {{ end }}
</spine> </spine>
{{ else }}
<spine page-progression-direction="ltr" toc="ncx">
<itemref idref="page_part" linear="yes" properties="page-spread-left"/>
{{ range $idx, $ := .Images }}
<itemref idref="page_{{ $.Id }}_p{{ $.Part }}" linear="yes" properties="page-spread-{{ if mod $idx 2 }}right{{ else }}end{{ end }}"/>
{{ end }}
</spine>
{{ end }}
</package> </package>