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(
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

View File

@ -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,
}
}

View File

@ -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

View File

@ -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;
}

View File

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