From bcad0389fa11589e490cb45f13095d7f5e59217d Mon Sep 17 00:00:00 2001 From: celogeek <65178+celogeek@users.noreply.github.com> Date: Sun, 30 Apr 2023 16:25:02 +0200 Subject: [PATCH] improve alignment on spread images for landscape --- internal/epub/epub.go | 8 ++++-- internal/epub/image/epub_image.go | 26 +++++++------------ .../epub/templates/epub_templates_content.go | 4 ++- 3 files changed, 19 insertions(+), 19 deletions(-) diff --git a/internal/epub/epub.go b/internal/epub/epub.go index e3623f8..abf1b45 100644 --- a/internal/epub/epub.go +++ b/internal/epub/epub.go @@ -77,7 +77,7 @@ func (e *ePub) writeImage(wz *epubzip.EPUBZip, img *epubimage.Image, zipImg *zip "Title": fmt.Sprintf("Image %d Part %d", img.Id, img.Part), "ViewPort": fmt.Sprintf("width=%d,height=%d", e.Image.View.Width, e.Image.View.Height), "ImagePath": img.ImgPath(), - "ImageStyle": img.ImgStyle(e.Image.View.Width, e.Image.View.Height, e.Image.Manga), + "ImageStyle": img.ImgStyle(e.Image.View.Width, e.Image.View.Height, ""), })), ) if err == nil { @@ -251,6 +251,10 @@ func (e *ePub) Write() error { if totalParts > 1 { title = fmt.Sprintf("%s [%d/%d]", title, i+1, totalParts) } + titleAlign := "left:0" + if e.Image.Manga { + titleAlign = "right:0" + } content := []zipContent{ {"META-INF/container.xml", epubtemplates.Container}, @@ -280,7 +284,7 @@ func (e *ePub) Write() error { "Title": title, "ViewPort": fmt.Sprintf("width=%d,height=%d", e.Image.View.Width, e.Image.View.Height), "ImagePath": "Images/title.jpg", - "ImageStyle": part.Cover.ImgStyle(e.Image.View.Width, e.Image.View.Height, e.Image.Manga), + "ImageStyle": part.Cover.ImgStyle(e.Image.View.Width, e.Image.View.Height, titleAlign), })}, } diff --git a/internal/epub/image/epub_image.go b/internal/epub/image/epub_image.go index 0bcf180..e88a414 100644 --- a/internal/epub/image/epub_image.go +++ b/internal/epub/image/epub_image.go @@ -19,6 +19,7 @@ type Image struct { DoublePage bool Path string Name string + Position string } // key name of the blank plage after the image @@ -70,24 +71,17 @@ func (i *Image) EPUBImgPath() string { // // center by default. // align to left or right if it's part of the splitted double page. -func (i *Image) ImgStyle(viewWidth, viewHeight int, manga bool) string { +func (i *Image) ImgStyle(viewWidth, viewHeight int, align string) string { marginW, marginH := float64(viewWidth-i.Width)/2, float64(viewHeight-i.Height)/2 - left, top := marginW*100/float64(viewWidth), marginH*100/float64(viewHeight) - var align string - switch i.Part { - case 0: - align = fmt.Sprintf("left:%.2f%%", left) - case 1: - if manga { - align = "left:0" - } else { + + if align == "" { + switch i.Position { + case "rendition:page-spread-left": align = "right:0" - } - case 2: - if manga { - align = "right:0" - } else { + case "rendition:page-spread-right": align = "left:0" + default: + align = fmt.Sprintf("left:%.2f%%", marginW*100/float64(viewWidth)) } } @@ -95,7 +89,7 @@ func (i *Image) ImgStyle(viewWidth, viewHeight int, manga bool) string { "width:%dpx; height:%dpx; top:%.2f%%; %s;", i.Width, i.Height, - top, + marginH*100/float64(viewHeight), align, ) } diff --git a/internal/epub/templates/epub_templates_content.go b/internal/epub/templates/epub_templates_content.go index e3e9492..7271032 100644 --- a/internal/epub/templates/epub_templates_content.go +++ b/internal/epub/templates/epub_templates_content.go @@ -195,9 +195,11 @@ func getSpine(o *ContentOptions) []tag { "", }) } + // register position for style adjustment + img.Position = getSpread(img.DoublePage) spine = append(spine, tag{ "itemref", - tagAttrs{"idref": img.PageKey(), "properties": getSpread(img.DoublePage)}, + tagAttrs{"idref": img.PageKey(), "properties": img.Position}, "", }) }