From bf172e0d78fa08060176fd0014499692230200ba Mon Sep 17 00:00:00 2001 From: celogeek <65178+celogeek@users.noreply.github.com> Date: Sun, 1 Oct 2023 17:36:33 +0200 Subject: [PATCH] update style for max compatibility --- internal/epub/epub.go | 34 +++++-------- internal/epub/image/epub_image.go | 48 ++++++++----------- internal/epub/options/epub_options.go | 4 ++ .../templates/epub_templates_style.css.tmpl | 34 ++++++++----- .../templates/epub_templates_text.xhtml.tmpl | 4 +- 5 files changed, 60 insertions(+), 64 deletions(-) diff --git a/internal/epub/epub.go b/internal/epub/epub.go index df4da11..5a70328 100644 --- a/internal/epub/epub.go +++ b/internal/epub/epub.go @@ -75,10 +75,9 @@ func (e *ePub) writeImage(wz *epubzip.EPUBZip, img *epubimage.Image, zipImg *zip err := wz.WriteContent( img.EPUBPagePath(), []byte(e.render(epubtemplates.Text, map[string]any{ - "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, ""), + "Title": fmt.Sprintf("Image %d Part %d", img.Id, img.Part), + "ViewPort": e.Image.View.Port(), + "Image": img.ImgStyle("", e.Image.View.Width, e.Image.View.Height), })), ) if err == nil { @@ -94,7 +93,7 @@ func (e *ePub) writeBlank(wz *epubzip.EPUBZip, img *epubimage.Image) error { img.EPUBSpacePath(), []byte(e.render(epubtemplates.Blank, map[string]any{ "Title": fmt.Sprintf("Blank Page %d", img.Id), - "ViewPort": fmt.Sprintf("width=%d,height=%d", e.Image.View.Width, e.Image.View.Height), + "ViewPort": e.Image.View.Port(), })), ) } @@ -111,10 +110,9 @@ func (e *ePub) writeCoverImage(wz *epubzip.EPUBZip, img *epubimage.Image, part, if err := wz.WriteContent( "OEBPS/Text/cover.xhtml", []byte(e.render(epubtemplates.Text, map[string]any{ - "Title": title, - "ViewPort": fmt.Sprintf("width=%d,height=%d", e.Image.View.Width, e.Image.View.Height), - "ImagePath": fmt.Sprintf("Images/cover.%s", e.Image.Format), - "ImageStyle": img.ImgStyle(e.Image.View.Width, e.Image.View.Height, ""), + "Title": title, + "ViewPort": e.Image.View.Port(), + "Image": img.ImgStyle("cover", e.Image.View.Width, e.Image.View.Height), })), ); err != nil { return err @@ -144,21 +142,12 @@ func (e *ePub) writeCoverImage(wz *epubzip.EPUBZip, img *epubimage.Image, part, // write title image func (e *ePub) writeTitleImage(wz *epubzip.EPUBZip, img *epubimage.Image, title string) error { - titleAlign := "" - if !e.Image.View.PortraitOnly { - if e.Image.Manga { - titleAlign = "right:0" - } else { - titleAlign = "left:0" - } - } - if !e.Image.View.PortraitOnly { if err := wz.WriteContent( "OEBPS/Text/space_title.xhtml", []byte(e.render(epubtemplates.Blank, map[string]any{ "Title": "Blank Page Title", - "ViewPort": fmt.Sprintf("width=%d,height=%d", e.Image.View.Width, e.Image.View.Height), + "ViewPort": e.Image.View.Port(), })), ); err != nil { return err @@ -168,10 +157,9 @@ func (e *ePub) writeTitleImage(wz *epubzip.EPUBZip, img *epubimage.Image, title if err := wz.WriteContent( "OEBPS/Text/title.xhtml", []byte(e.render(epubtemplates.Text, map[string]any{ - "Title": title, - "ViewPort": fmt.Sprintf("width=%d,height=%d", e.Image.View.Width, e.Image.View.Height), - "ImagePath": fmt.Sprintf("Images/title.%s", e.Image.Format), - "ImageStyle": img.ImgStyle(e.Image.View.Width, e.Image.View.Height, titleAlign), + "Title": title, + "ViewPort": e.Image.View.Port(), + "Image": img.ImgStyle("title", e.Image.View.Width, e.Image.View.Height), })), ); err != nil { return err diff --git a/internal/epub/image/epub_image.go b/internal/epub/image/epub_image.go index 4b84595..b5a5fef 100644 --- a/internal/epub/image/epub_image.go +++ b/internal/epub/image/epub_image.go @@ -6,7 +6,6 @@ package epubimage import ( "fmt" "image" - "strings" ) type Image struct { @@ -66,38 +65,24 @@ func (i *Image) ImgPath() string { return fmt.Sprintf("Images/%s.%s", i.ImgKey(), i.Format) } +// special path +func (i *Image) ImgSpecialPath(kind string) string { + if kind == "" { + return i.ImgPath() + } + return fmt.Sprintf("Images/%s.%s", kind, i.Format) +} + // image path into the EPUB func (i *Image) EPUBImgPath() string { return fmt.Sprintf("OEBPS/%s", i.ImgPath()) } -// style to apply to the image. -// -// center by default. -// align to left or right if it's part of the splitted double page. -func (i *Image) ImgStyle(viewWidth, viewHeight int, align string) string { - relWidth, relHeight := i.RelSize(viewWidth, viewHeight) - marginW, marginH := float64(viewWidth-relWidth)/2, float64(viewHeight-relHeight)/2 +func (i *Image) Top(viewWidth, viewHeight int) string { + _, relHeight := i.RelSize(viewWidth, viewHeight) + marginH := float64(viewHeight-relHeight) / 2 - style := []string{} - - style = append(style, fmt.Sprintf("width:%dpx", relWidth)) - style = append(style, fmt.Sprintf("height:%dpx", relHeight)) - style = append(style, fmt.Sprintf("top:%.2f%%", marginH*100/float64(viewHeight))) - if align == "" { - switch i.Position { - case "rendition:page-spread-left": - style = append(style, "right:0") - case "rendition:page-spread-right": - style = append(style, "left:0") - default: - style = append(style, fmt.Sprintf("left:%.2f%%", marginW*100/float64(viewWidth))) - } - } else { - style = append(style, align) - } - - return strings.Join(style, "; ") + return fmt.Sprintf("%.2f", marginH*100/float64(viewHeight)) } func (i *Image) RelSize(viewWidth, viewHeight int) (relWidth, relHeight int) { @@ -121,3 +106,12 @@ func (i *Image) RelSize(viewWidth, viewHeight int) (relWidth, relHeight int) { return } + +func (i *Image) ImgStyle(kind string, viewWidth, viewHeight int) map[string]any { + return map[string]any{ + "Path": i.ImgSpecialPath(kind), + "Width": i.Width, + "Height": i.Height, + "Top": i.Top(viewWidth, viewHeight), + } +} diff --git a/internal/epub/options/epub_options.go b/internal/epub/options/epub_options.go index 2de0277..58e87be 100644 --- a/internal/epub/options/epub_options.go +++ b/internal/epub/options/epub_options.go @@ -21,6 +21,10 @@ type View struct { Color Color } +func (v *View) Port() string { + return fmt.Sprintf("width=%d, height=%d", v.Width, v.Height) +} + type Image struct { Crop *Crop Quality int diff --git a/internal/epub/templates/epub_templates_style.css.tmpl b/internal/epub/templates/epub_templates_style.css.tmpl index cfa4f08..abe97c2 100644 --- a/internal/epub/templates/epub_templates_style.css.tmpl +++ b/internal/epub/templates/epub_templates_style.css.tmpl @@ -1,18 +1,26 @@ +@page { + margin: 0; + padding: 0; +} + body { - color: #{{ .View.Color.Foreground }}; - background: #{{ .View.Color.Background }}; - top: 0; - left: 0; - margin: 0; - padding: 0; - width: {{ .View.Width }}px; - height: {{ .View.Height }}px; - text-align: center; + color: #{{ .View.Color.Foreground }}; + background: #{{ .View.Color.Background }}; + width: {{ .View.Width }}px; + height: {{ .View.Height }}px; + display: block; + margin: 0; + padding: 0; +} + +div { + margin: 0; + padding: 0; + position: relative; + text-align: center; } img { - position: absolute; - margin:0; - padding:0; - z-index:0; + margin: 0; + padding: 0; } \ No newline at end of file diff --git a/internal/epub/templates/epub_templates_text.xhtml.tmpl b/internal/epub/templates/epub_templates_text.xhtml.tmpl index 21cea8f..d918065 100644 --- a/internal/epub/templates/epub_templates_text.xhtml.tmpl +++ b/internal/epub/templates/epub_templates_text.xhtml.tmpl @@ -8,6 +8,8 @@ - {{ .Title }} +
+ {{ .Title }} +
\ No newline at end of file