update style for max compatibility

This commit is contained in:
Celogeek 2023-10-01 17:36:33 +02:00
parent 6c92597d8c
commit 8e7a916b10
Signed by: celogeek
SSH Key Fingerprint: SHA256:DEDfxIK2nUWXbslbRkww3zsauDjhWHlTXar+ak4lDJ4
5 changed files with 77 additions and 68 deletions

View File

@ -75,10 +75,9 @@ func (e *ePub) writeImage(wz *epubzip.EPUBZip, img *epubimage.Image, zipImg *zip
err := wz.WriteContent( err := wz.WriteContent(
img.EPUBPagePath(), img.EPUBPagePath(),
[]byte(e.render(epubtemplates.Text, map[string]any{ []byte(e.render(epubtemplates.Text, map[string]any{
"Title": fmt.Sprintf("Image %d Part %d", img.Id, img.Part), "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), "ViewPort": e.Image.View.Port(),
"ImagePath": img.ImgPath(), "Image": img.ImgStyle("", e.Image.View.Width, e.Image.View.Height),
"ImageStyle": img.ImgStyle(e.Image.View.Width, e.Image.View.Height, ""),
})), })),
) )
if err == nil { if err == nil {
@ -94,7 +93,7 @@ func (e *ePub) writeBlank(wz *epubzip.EPUBZip, img *epubimage.Image) error {
img.EPUBSpacePath(), img.EPUBSpacePath(),
[]byte(e.render(epubtemplates.Blank, map[string]any{ []byte(e.render(epubtemplates.Blank, map[string]any{
"Title": fmt.Sprintf("Blank Page %d", img.Id), "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( if err := wz.WriteContent(
"OEBPS/Text/cover.xhtml", "OEBPS/Text/cover.xhtml",
[]byte(e.render(epubtemplates.Text, map[string]any{ []byte(e.render(epubtemplates.Text, map[string]any{
"Title": title, "Title": title,
"ViewPort": fmt.Sprintf("width=%d,height=%d", e.Image.View.Width, e.Image.View.Height), "ViewPort": e.Image.View.Port(),
"ImagePath": fmt.Sprintf("Images/cover.%s", e.Image.Format), "Image": img.ImgStyle("cover", e.Image.View.Width, e.Image.View.Height),
"ImageStyle": img.ImgStyle(e.Image.View.Width, e.Image.View.Height, ""),
})), })),
); err != nil { ); err != nil {
return err return err
@ -144,21 +142,12 @@ func (e *ePub) writeCoverImage(wz *epubzip.EPUBZip, img *epubimage.Image, part,
// write title image // write title image
func (e *ePub) writeTitleImage(wz *epubzip.EPUBZip, img *epubimage.Image, title string) error { 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 !e.Image.View.PortraitOnly {
if err := wz.WriteContent( if err := wz.WriteContent(
"OEBPS/Text/space_title.xhtml", "OEBPS/Text/space_title.xhtml",
[]byte(e.render(epubtemplates.Blank, map[string]any{ []byte(e.render(epubtemplates.Blank, map[string]any{
"Title": "Blank Page Title", "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 { ); err != nil {
return err return err
@ -168,10 +157,9 @@ func (e *ePub) writeTitleImage(wz *epubzip.EPUBZip, img *epubimage.Image, title
if err := wz.WriteContent( if err := wz.WriteContent(
"OEBPS/Text/title.xhtml", "OEBPS/Text/title.xhtml",
[]byte(e.render(epubtemplates.Text, map[string]any{ []byte(e.render(epubtemplates.Text, map[string]any{
"Title": title, "Title": title,
"ViewPort": fmt.Sprintf("width=%d,height=%d", e.Image.View.Width, e.Image.View.Height), "ViewPort": e.Image.View.Port(),
"ImagePath": fmt.Sprintf("Images/title.%s", e.Image.Format), "Image": img.ImgStyle("title", e.Image.View.Width, e.Image.View.Height),
"ImageStyle": img.ImgStyle(e.Image.View.Width, e.Image.View.Height, titleAlign),
})), })),
); err != nil { ); err != nil {
return err return err

View File

@ -6,7 +6,6 @@ package epubimage
import ( import (
"fmt" "fmt"
"image" "image"
"strings"
) )
type Image struct { type Image struct {
@ -66,40 +65,19 @@ func (i *Image) ImgPath() string {
return fmt.Sprintf("Images/%s.%s", i.ImgKey(), i.Format) 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 // image path into the EPUB
func (i *Image) EPUBImgPath() string { func (i *Image) EPUBImgPath() string {
return fmt.Sprintf("OEBPS/%s", i.ImgPath()) 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) { func (i *Image) RelSize(viewWidth, viewHeight int) (relWidth, relHeight int) {
w, h := viewWidth, viewHeight w, h := viewWidth, viewHeight
srcw, srch := i.Width, i.Height srcw, srch := i.Width, i.Height
@ -121,3 +99,25 @@ func (i *Image) RelSize(viewWidth, viewHeight int) (relWidth, relHeight int) {
return 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,
}
}

View File

@ -21,6 +21,10 @@ type View struct {
Color Color Color Color
} }
func (v *View) Port() string {
return fmt.Sprintf("width=%d, height=%d", v.Width, v.Height)
}
type Image struct { type Image struct {
Crop *Crop Crop *Crop
Quality int Quality int

View File

@ -1,18 +1,31 @@
@page {
margin: 0;
padding: 0;
}
body { body {
color: #{{ .View.Color.Foreground }}; color: #{{ .View.Color.Foreground }};
background: #{{ .View.Color.Background }}; background: #{{ .View.Color.Background }};
top: 0; width: {{ .View.Width }}px;
left: 0; height: {{ .View.Height }}px;
margin: 0; margin: 0;
padding: 0; padding: 0;
width: {{ .View.Width }}px; }
height: {{ .View.Height }}px;
text-align: center; div {
margin: 0;
padding: 0;
text-align: center;
position: absolute;
}
span {
margin: 0;
padding: 0;
text-indent: 0;
} }
img { img {
position: absolute; margin: 0;
margin:0; padding: 0;
padding:0;
z-index:0;
} }

View File

@ -7,7 +7,11 @@
<link href="style.css" type="text/css" rel="stylesheet"/> <link href="style.css" type="text/css" rel="stylesheet"/>
<meta name="viewport" content="{{ .ViewPort }}"/> <meta name="viewport" content="{{ .ViewPort }}"/>
</head> </head>
<body> <body style="">
<img src="../{{ .ImagePath }}" alt="{{ .Title }}" style="{{ .ImageStyle }}"/> <div style="top:{{ .Image.Top }}%; {{ .Image.Align }}">
<span>
<img src="../{{ .Image.Path }}" alt="{{ .Title }}" width="{{ .Image.Width }}" height="{{ .Image.Height }}"/>
</span>
</div>
</body> </body>
</html> </html>