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..d5d7681 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,40 +65,19 @@ 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 - - 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, "; ") -} - func (i *Image) RelSize(viewWidth, viewHeight int) (relWidth, relHeight int) { w, h := viewWidth, viewHeight srcw, srch := i.Width, i.Height @@ -121,3 +99,25 @@ func (i *Image) RelSize(viewWidth, viewHeight int) (relWidth, relHeight int) { return } + +func (i *Image) ImgStyle(kind string, viewWidth, viewHeight int) map[string]any { + align := "" + switch i.Position { + case "rendition:page-spread-right": + align = "left:0" + case "rendition:page-spread-left": + align = "right:0" + } + + relWidth, relHeight := i.RelSize(viewWidth, viewHeight) + marginH := float64(viewHeight-relHeight) / 2 + top := fmt.Sprintf("%.2f", marginH*100/float64(viewHeight)) + + return map[string]any{ + "Path": i.ImgSpecialPath(kind), + "Width": relWidth, + "Height": relHeight, + "Top": top, + "Align": align, + } +} 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..6dd924b 100644 --- a/internal/epub/templates/epub_templates_style.css.tmpl +++ b/internal/epub/templates/epub_templates_style.css.tmpl @@ -1,18 +1,31 @@ +@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; + margin: 0; + padding: 0; +} + +div { + margin: 0; + padding: 0; + text-align: center; + position: absolute; +} + +span { + margin: 0; + padding: 0; + text-indent: 0; } 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..1211c4e 100644 --- a/internal/epub/templates/epub_templates_text.xhtml.tmpl +++ b/internal/epub/templates/epub_templates_text.xhtml.tmpl @@ -7,7 +7,11 @@ - - {{ .Title }} + +
+ + {{ .Title }} + +
\ No newline at end of file