diff --git a/internal/epub/epub.go b/internal/epub/epub.go index b0f4c5b..8063577 100644 --- a/internal/epub/epub.go +++ b/internal/epub/epub.go @@ -97,9 +97,10 @@ func (e *ePub) writeImage(wz *epubZip, img *Image) error { err := wz.WriteFile( fmt.Sprintf("OEBPS/%s", img.TextPath()), e.render(textTmpl, map[string]any{ - "Title": fmt.Sprintf("Image %d Part %d", img.Id, img.Part), - "ViewPort": fmt.Sprintf("width=%d,height=%d", e.ViewWidth, e.ViewHeight), - "ImagePath": img.ImgPath(), + "Title": fmt.Sprintf("Image %d Part %d", img.Id, img.Part), + "ViewPort": fmt.Sprintf("width=%d,height=%d", e.ViewWidth, e.ViewHeight), + "ImagePath": img.ImgPath(), + "ImageStyle": img.ImgStyle(e.ViewWidth, e.ViewHeight, e.Manga), }), ) diff --git a/internal/epub/epub_image_filters.go b/internal/epub/epub_image_filters.go index 2714565..e6022ff 100644 --- a/internal/epub/epub_image_filters.go +++ b/internal/epub/epub_image_filters.go @@ -20,7 +20,6 @@ func NewGift(options *ImageOptions) *gift.GIFT { } g.Add( filters.Resize(options.ViewWidth, options.ViewHeight, gift.LanczosResampling), - filters.Position(options.ViewWidth, options.ViewHeight, filters.PositionCenter), filters.Pixel(), ) return g @@ -37,7 +36,7 @@ func NewGiftSplitDoublePage(options *ImageOptions) []*gift.GIFT { filters.CropSplitDoublePage(!options.Manga), ) - for i, g := range gifts { + for _, g := range gifts { if options.Contrast != 0 { g.Add(gift.Contrast(float32(options.Contrast))) } @@ -45,14 +44,8 @@ func NewGiftSplitDoublePage(options *ImageOptions) []*gift.GIFT { g.Add(gift.Brightness(float32(options.Brightness))) } - position := filters.PositionLeft - if (i == 1) == options.Manga { - position = filters.PositionRight - } - g.Add( filters.Resize(options.ViewWidth, options.ViewHeight, gift.LanczosResampling), - filters.Position(options.ViewWidth, options.ViewHeight, position), ) } diff --git a/internal/epub/epub_image_processing.go b/internal/epub/epub_image_processing.go index 10d6147..0dc4360 100644 --- a/internal/epub/epub_image_processing.go +++ b/internal/epub/epub_image_processing.go @@ -53,6 +53,36 @@ func (i *Image) ImgPath() string { return fmt.Sprintf("Images/%d_p%d.jpg", i.Id, i.Part) } +func (i *Image) ImgStyle(viewWidth, viewHeight int, manga bool) 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 { + align = "right:0" + } + case 2: + if manga { + align = "right:0" + } else { + align = "left:0" + } + } + + return fmt.Sprintf( + "width:%dpx; height:%dpx; top:%.2f%%; %s;", + i.Width, + i.Height, + top, + align, + ) +} + func (i *Image) SpacePath() string { return fmt.Sprintf("Text/%d_sp.xhtml", i.Id) } diff --git a/internal/epub/filters/epub_filters_position.go b/internal/epub/filters/epub_filters_position.go deleted file mode 100644 index d36e212..0000000 --- a/internal/epub/filters/epub_filters_position.go +++ /dev/null @@ -1,67 +0,0 @@ -package filters - -import ( - "image" - "image/draw" - - "github.com/disintegration/gift" -) - -const ( - PositionCenter = iota - PositionLeft - PositionRight -) - -func Position(viewWidth, viewHeight int, align int) gift.Filter { - return &positionFilter{ - viewWidth, viewHeight, align, - } -} - -type positionFilter struct { - viewWidth, viewHeight, align int -} - -func (p *positionFilter) Bounds(srcBounds image.Rectangle) image.Rectangle { - w, h := p.viewWidth, p.viewHeight - srcw, srch := srcBounds.Dx(), srcBounds.Dy() - - if w <= 0 || h <= 0 || srcw <= 0 || srch <= 0 { - return image.Rect(0, 0, 0, 0) - } - - return image.Rect(0, 0, w, h) -} - -func (p *positionFilter) Draw(dst draw.Image, src image.Image, options *gift.Options) { - if dst.Bounds().Dx() == 0 || dst.Bounds().Dy() == 0 { - return - } - - draw.Draw(dst, dst.Bounds(), image.White, dst.Bounds().Min, draw.Over) - - srcBounds := src.Bounds() - left, top := 0, (dst.Bounds().Dy()-srcBounds.Dy())/2 - - if p.align == PositionCenter { - left = (dst.Bounds().Dx() - srcBounds.Dx()) / 2 - } - - if p.align == PositionRight { - left = dst.Bounds().Dx() - srcBounds.Dx() - } - - draw.Draw( - dst, - image.Rect( - left, - top, - dst.Bounds().Dx(), - dst.Bounds().Dy(), - ), - src, - srcBounds.Min, - draw.Over, - ) -} diff --git a/internal/epub/templates/epub_templates_style.css.tmpl b/internal/epub/templates/epub_templates_style.css.tmpl index a8d41d9..7059093 100644 --- a/internal/epub/templates/epub_templates_style.css.tmpl +++ b/internal/epub/templates/epub_templates_style.css.tmpl @@ -15,8 +15,4 @@ img { margin:0; padding:0; z-index:0; - top:0; - left:0; - width: {{ .PageWidth }}px; - height: {{ .PageHeight }}px; } \ 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 6a509d8..a88b9e1 100644 --- a/internal/epub/templates/epub_templates_text.xhtml.tmpl +++ b/internal/epub/templates/epub_templates_text.xhtml.tmpl @@ -9,7 +9,7 @@