Compare commits

..

1 Commits

Author SHA1 Message Date
ef27485273
WIP 2023-04-17 18:30:26 +02:00
28 changed files with 102 additions and 98 deletions

View File

@ -4,7 +4,6 @@ import (
"fmt"
"os"
"path/filepath"
"strings"
"github.com/celogeek/go-comic-converter/v2/internal/converter/profiles"
"gopkg.in/yaml.v3"
@ -112,7 +111,7 @@ func (o *Options) LoadDefault() error {
}
func (o *Options) ShowDefault() string {
var profileDesc, viewDesc string
var profileDesc string
profile := o.GetProfile()
if profile != nil {
profileDesc = fmt.Sprintf(
@ -122,13 +121,6 @@ func (o *Options) ShowDefault() string {
profile.Width,
profile.Height,
)
perfectWidth, perfectHeight := profile.PerfectDim()
viewDesc = fmt.Sprintf(
"%dx%d",
perfectWidth,
perfectHeight,
)
}
limitmb := "nolimit"
if o.LimitMb > 0 {
@ -147,8 +139,6 @@ func (o *Options) ShowDefault() string {
return fmt.Sprintf(`
Profile : %s
ViewRatio : 1:%s
View : %s
Quality : %d
Crop : %v
Brightness : %d
@ -162,8 +152,6 @@ func (o *Options) ShowDefault() string {
StripFirstDirectoryFromToc: %v
SortPathMode : %s`,
profileDesc,
strings.TrimRight(fmt.Sprintf("%f", profiles.PerfectRatio), "0"),
viewDesc,
o.Quality,
o.Crop,
o.Brightness,

View File

@ -12,19 +12,6 @@ type Profile struct {
Height int
}
const PerfectRatio = 1.5
func (p Profile) PerfectDim() (int, int) {
width, height := float64(p.Width), float64(p.Height)
perfectWidth, perfectHeight := height/PerfectRatio, width*PerfectRatio
if perfectWidth > width {
perfectWidth = width
} else {
perfectHeight = height
}
return int(perfectWidth), int(perfectHeight)
}
type Profiles []Profile
func New() Profiles {

View File

@ -24,6 +24,7 @@ func (e *ePub) getMeta(title string, part *epubPart, currentPart, totalPart int)
{"meta", TagAttrs{"property": "rendition:layout"}, "pre-paginated"},
{"meta", TagAttrs{"property": "rendition:spread"}, "auto"},
{"meta", TagAttrs{"property": "rendition:orientation"}, "auto"},
{"meta", TagAttrs{"property": "ibooks:specified-fonts"}, "true"},
{"meta", TagAttrs{"property": "schema:accessMode"}, "visual"},
{"meta", TagAttrs{"property": "schema:accessModeSufficient"}, "visual"},
{"meta", TagAttrs{"property": "schema:accessibilityHazard"}, "noFlashingHazard"},
@ -150,7 +151,7 @@ func (e *ePub) getContent(title string, part *epubPart, currentPart, totalPart i
pkg.CreateAttr("xmlns", "http://www.idpf.org/2007/opf")
pkg.CreateAttr("unique-identifier", "ean")
pkg.CreateAttr("version", "3.0")
pkg.CreateAttr("prefix", "rendition: http://www.idpf.org/vocab/rendition/#")
pkg.CreateAttr("prefix", "rendition: http://www.idpf.org/vocab/rendition/# ibooks: http://vocabulary.itunes.apple.com/rdf/ibooks/vocabulary-extensions-1.0/")
addToElement := func(elm *etree.Element, meth func(title string, part *epubPart, currentPart, totalPart int) []Tag) {
for _, p := range meth(title, part, currentPart, totalPart) {

View File

@ -97,10 +97,14 @@ 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(),
"ImageStyle": img.ImgStyle(e.ViewWidth, e.ViewHeight, e.Manga),
"Title": fmt.Sprintf("Image %d Part %d", img.Id, img.Part),
"ViewPort": fmt.Sprintf("width=%d, height=%d", e.ViewWidth, e.ViewHeight),
"ImageStyle": fmt.Sprintf(
"width:%dpx; height:%dpx;",
img.Width,
img.Height,
),
"ImagePath": img.ImgPath(),
}),
)
@ -116,7 +120,7 @@ func (e *ePub) writeBlank(wz *epubZip, img *Image) error {
fmt.Sprintf("OEBPS/Text/%d_sp.xhtml", img.Id),
e.render(blankTmpl, map[string]any{
"Title": fmt.Sprintf("Blank Page %d", img.Id),
"ViewPort": fmt.Sprintf("width=%d,height=%d", e.ViewWidth, e.ViewHeight),
"ViewPort": fmt.Sprintf("width=%d, height=%d", e.ViewWidth, e.ViewHeight),
}),
)
}

View File

@ -1,21 +0,0 @@
package epub
import _ "embed"
//go:embed "templates/epub_templates_container.xml.tmpl"
var containerTmpl string
//go:embed "templates/epub_templates_applebooks.xml.tmpl"
var appleBooksTmpl string
//go:embed "templates/epub_templates_style.css.tmpl"
var styleTmpl string
//go:embed "templates/epub_templates_title.xhtml.tmpl"
var titleTmpl string
//go:embed "templates/epub_templates_text.xhtml.tmpl"
var textTmpl string
//go:embed "templates/epub_templates_blank.xhtml.tmpl"
var blankTmpl string

View File

@ -0,0 +1,56 @@
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 {
return image.Rect(0, 0, p.viewWidth, p.viewHeight)
}
func (p *positionFilter) Draw(dst draw.Image, src image.Image, options *gift.Options) {
draw.Draw(dst, dst.Bounds(), image.White, dst.Bounds().Min, draw.Over)
srcBounds := src.Bounds()
left, top := 0, (p.viewHeight-srcBounds.Dy())/2
if p.align == PositionCenter {
left = (p.viewWidth - srcBounds.Dx()) / 2
}
if p.align == PositionRight {
left = p.viewWidth - srcBounds.Dx()
}
draw.Draw(
dst,
image.Rect(
left,
top,
p.viewWidth,
p.viewHeight,
),
src,
srcBounds.Min,
draw.Over,
)
}

View File

@ -20,6 +20,7 @@ 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
@ -36,7 +37,7 @@ func NewGiftSplitDoublePage(options *ImageOptions) []*gift.GIFT {
filters.CropSplitDoublePage(!options.Manga),
)
for _, g := range gifts {
for i, g := range gifts {
if options.Contrast != 0 {
g.Add(gift.Contrast(float32(options.Contrast)))
}
@ -44,8 +45,14 @@ 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),
)
}

View File

@ -53,36 +53,6 @@ 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)
}

View File

@ -10,6 +10,7 @@ import (
func NewBar(max int, description string, currentJob, totalJob int) *progressbar.ProgressBar {
fmtJob := fmt.Sprintf("%%0%dd", len(fmt.Sprint(totalJob)))
fmtDesc := fmt.Sprintf("[%s/%s] %%-15s", fmtJob, fmtJob)
return progressbar.DefaultBytesSilent(int64(max))
return progressbar.NewOptions(max,
progressbar.OptionSetWriter(os.Stderr),
progressbar.OptionOnCompletion(func() {

View File

@ -0,0 +1,21 @@
package epub
import _ "embed"
//go:embed "templates/container.xml.tmpl"
var containerTmpl string
//go:embed "templates/applebooks.xml.tmpl"
var appleBooksTmpl string
//go:embed "templates/style.css.tmpl"
var styleTmpl string
//go:embed "templates/title.xhtml.tmpl"
var titleTmpl string
//go:embed "templates/text.xhtml.tmpl"
var textTmpl string
//go:embed "templates/blank.xhtml.tmpl"
var blankTmpl string

View File

@ -2,7 +2,6 @@
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops">
<head>
<meta charset="utf-8" />
<title>{{ .Title }}</title>
<link href="style.css" type="text/css" rel="stylesheet"/>
<meta name="viewport" content="{{ .ViewPort }}"/>

View File

@ -1,18 +1,13 @@
body {
color: #000;
background: #FFF;
top: 0;
left: 0;
margin: 0;
padding: 0;
width: {{ .PageWidth }}px;
height: {{ .PageHeight }}px;
text-align: center;
}
img {
position: absolute;
div {
margin:0;
padding:0;
z-index:0;
}

View File

@ -2,14 +2,13 @@
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops">
<head>
<meta charset="utf-8" />
<title>{{ .Title }}</title>
<link href="style.css" type="text/css" rel="stylesheet"/>
<meta name="viewport" content="{{ .ViewPort }}"/>
</head>
<body>
<div>
<img src="../{{ .ImagePath }}" alt="{{ .Title }}" style="{{ .ImageStyle }}"/>
<img style="{{ .ImageStyle }}" src="../{{ .ImagePath }}"/>
</div>
</body>
</html>

View File

@ -2,7 +2,6 @@
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops">
<head>
<meta charset="utf-8" />
<title>Part {{ .Part }}</title>
<link href="style.css" type="text/css" rel="stylesheet"/>
<meta name="viewport" content="width={{ .Info.ViewWidth }}, height={{ .Info.ViewHeight }}"/>

View File

@ -91,8 +91,6 @@ $ go install github.com/celogeek/go-comic-converter/v%d@%s
fmt.Fprintln(os.Stderr, cmd.Options)
profile := cmd.Options.GetProfile()
perfectWidth, perfectHeight := profile.PerfectDim()
if err := epub.NewEpub(&epub.EpubOptions{
Input: cmd.Options.Input,
Output: cmd.Options.Output,
@ -104,8 +102,8 @@ $ go install github.com/celogeek/go-comic-converter/v%d@%s
DryVerbose: cmd.Options.DryVerbose,
SortPathMode: cmd.Options.SortPathMode,
ImageOptions: &epub.ImageOptions{
ViewWidth: perfectWidth,
ViewHeight: perfectHeight,
ViewWidth: profile.Width,
ViewHeight: profile.Height,
Quality: cmd.Options.Quality,
Crop: cmd.Options.Crop,
Brightness: cmd.Options.Brightness,