create title page

This commit is contained in:
Celogeek 2023-04-22 16:34:18 +02:00
parent e79cd346c4
commit 5960c56938
Signed by: celogeek
SSH Key Fingerprint: SHA256:njNJLzoLQdbV9PC6ehcruRb0QnEgxABoCYZ+0+aUIYc
7 changed files with 34 additions and 35 deletions

View File

@ -122,6 +122,10 @@ func (e *ePub) writeBlank(wz *epubZip, img *Image) error {
)
}
func (e ePub) getTitleImageData(img *Image, currentPart, totalPart int) *ImageData {
return newData("OEBPS/Images/title.jpg", img.Raw, e.Quality)
}
func (e *ePub) getParts() ([]*epubPart, error) {
images, err := e.LoadImages()
@ -243,10 +247,11 @@ func (e *ePub) Write() error {
"PageWidth": e.ViewWidth,
"PageHeight": e.ViewHeight,
})},
{"OEBPS/Text/title.xhtml", e.render(titleTmpl, map[string]any{
"Info": e,
"Part": i + 1,
"Total": totalParts,
{"OEBPS/Text/title.xhtml", e.render(textTmpl, map[string]any{
"Title": title,
"ViewPort": fmt.Sprintf("width=%d,height=%d", e.ViewWidth, e.ViewHeight),
"ImagePath": "Images/title.jpg",
"ImageStyle": part.Cover.ImgStyle(e.ViewWidth, e.ViewHeight, e.Manga),
})},
}
@ -258,6 +263,9 @@ func (e *ePub) Write() error {
return err
}
}
if err := wz.WriteImage(e.getTitleImageData(part.Cover, i+1, totalParts)); err != nil {
return err
}
// Cover exist or part > 1
// If no cover, part 2 and more will include the image as a cover

View File

@ -75,6 +75,8 @@ func (e *ePub) getManifest(title string, part *epubPart, currentPart, totalPart
items := []Tag{
{"item", TagAttrs{"id": "toc", "href": "toc.xhtml", "properties": "nav", "media-type": "application/xhtml+xml"}, ""},
{"item", TagAttrs{"id": "css", "href": "Text/style.css", "media-type": "text/css"}, ""},
{"item", TagAttrs{"id": "page_title", "href": "Text/title.xhtml", "media-type": "application/xhtml+xml"}, ""},
{"item", TagAttrs{"id": "img_title", "href": "Images/title.jpg", "media-type": "image/jpeg"}, ""},
}
if e.HasCover || currentPart > 1 {
@ -93,7 +95,6 @@ func (e *ePub) getManifest(title string, part *epubPart, currentPart, totalPart
}
func (e *ePub) getSpine(title string, part *epubPart, currentPart, totalPart int) []Tag {
spine := []Tag{}
isOnTheRight := !e.Manga
getSpread := func(doublePageNoBlank bool) string {
isOnTheRight = !isOnTheRight
@ -108,6 +109,10 @@ func (e *ePub) getSpine(title string, part *epubPart, currentPart, totalPart int
return "rendition:page-spread-left"
}
}
spine := []Tag{
{"itemref", TagAttrs{"idref": "page_title", "properties": getSpread(false)}, ""},
}
for _, img := range part.Images {
spine = append(spine, Tag{
"itemref",

View File

@ -22,6 +22,10 @@ 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)
return newData(name, img, quality)
}
func newData(name string, img image.Image, quality int) *ImageData {
data := bytes.NewBuffer([]byte{})
if err := jpeg.Encode(data, img, &jpeg.Options{Quality: quality}); err != nil {
panic(err)

View File

@ -28,6 +28,7 @@ import (
type Image struct {
Id int
Part int
Raw image.Image
Data *ImageData
Width int
Height int
@ -180,15 +181,9 @@ func (e *ePub) LoadImages() ([]*Image, error) {
for img := range imageInput {
img.Reader.Close()
images = append(images, &Image{
Id: img.Id,
Part: 0,
Data: nil,
Width: 0,
Height: 0,
IsCover: false,
DoublePage: false,
Path: img.Path,
Name: img.Name,
Id: img.Id,
Path: img.Path,
Name: img.Name,
})
}
@ -229,9 +224,15 @@ func (e *ePub) LoadImages() ([]*Image, error) {
dst := image.NewGray(g.Bounds(src.Bounds()))
g.Draw(dst, src)
var raw image.Image
if img.Id == 0 {
raw = dst
}
imageOutput <- &Image{
Id: img.Id,
Part: 0,
Raw: raw,
Data: newImageData(img.Id, 0, dst, e.ImageOptions.Quality),
Width: dst.Bounds().Dx(),
Height: dst.Bounds().Dy(),

View File

@ -11,9 +11,6 @@ var appleBooksTmpl string
//go:embed "templates/epub_templates_style.css.tmpl"
var styleTmpl string
//go:embed "templates/epub_templates_title.xhtml.tmpl"
var titleTmpl string
//go:embed "templates/epub_templates_text.xhtml.tmpl"
var textTmpl string

View File

@ -53,8 +53,8 @@ func (e *ePub) getToc(title string, images []*Image) string {
beginning := etree.NewElement("li")
beginningLink := beginning.CreateElement("a")
beginningLink.CreateAttr("href", images[0].TextPath())
beginningLink.CreateText("Start of the book")
beginningLink.CreateAttr("href", "Text/title.xhtml")
beginningLink.CreateText(title)
ol.InsertChildAt(0, beginning)
nav.AddChild(ol)

View File

@ -1,16 +0,0 @@
<?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>
<meta charset="utf-8" />
<title>Part {{ .Part }}</title>
<link href="style.css" type="text/css" rel="stylesheet"/>
<meta name="viewport" content="width={{ .Info.ViewWidth }}, height={{ .Info.ViewHeight }}"/>
</head>
<body>
<h2>{{ .Info.Title }}</h2>
{{ if gt .Total 1 }}
<h3>Part {{ .Part }} / {{ .Total }}</h3>
{{ end }}
</body>
</html>