From 5960c569386e57b487ddc1b797d23db422643927 Mon Sep 17 00:00:00 2001 From: celogeek <65178+celogeek@users.noreply.github.com> Date: Sat, 22 Apr 2023 16:34:18 +0200 Subject: [PATCH] create title page --- internal/epub/epub.go | 16 ++++++++++++---- internal/epub/epub_content.go | 7 ++++++- internal/epub/epub_image_data.go | 4 ++++ internal/epub/epub_image_processing.go | 19 ++++++++++--------- internal/epub/epub_templates.go | 3 --- internal/epub/epub_toc.go | 4 ++-- .../templates/epub_templates_title.xhtml.tmpl | 16 ---------------- 7 files changed, 34 insertions(+), 35 deletions(-) delete mode 100644 internal/epub/templates/epub_templates_title.xhtml.tmpl diff --git a/internal/epub/epub.go b/internal/epub/epub.go index a1ba9fa..00cb6eb 100644 --- a/internal/epub/epub.go +++ b/internal/epub/epub.go @@ -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 diff --git a/internal/epub/epub_content.go b/internal/epub/epub_content.go index 6fce0d5..3c95291 100644 --- a/internal/epub/epub_content.go +++ b/internal/epub/epub_content.go @@ -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", diff --git a/internal/epub/epub_image_data.go b/internal/epub/epub_image_data.go index ebd8538..bda346c 100644 --- a/internal/epub/epub_image_data.go +++ b/internal/epub/epub_image_data.go @@ -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) diff --git a/internal/epub/epub_image_processing.go b/internal/epub/epub_image_processing.go index 580d04b..d368735 100644 --- a/internal/epub/epub_image_processing.go +++ b/internal/epub/epub_image_processing.go @@ -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(), diff --git a/internal/epub/epub_templates.go b/internal/epub/epub_templates.go index 5c33487..e15e798 100644 --- a/internal/epub/epub_templates.go +++ b/internal/epub/epub_templates.go @@ -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 diff --git a/internal/epub/epub_toc.go b/internal/epub/epub_toc.go index 0824ca3..ef79cfd 100644 --- a/internal/epub/epub_toc.go +++ b/internal/epub/epub_toc.go @@ -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) diff --git a/internal/epub/templates/epub_templates_title.xhtml.tmpl b/internal/epub/templates/epub_templates_title.xhtml.tmpl deleted file mode 100644 index 037886c..0000000 --- a/internal/epub/templates/epub_templates_title.xhtml.tmpl +++ /dev/null @@ -1,16 +0,0 @@ - - - -
- -