From b9115f7f6f7cf6a01bb62ca27a37bb89bb0b8d57 Mon Sep 17 00:00:00 2001 From: celogeek <65178+celogeek@users.noreply.github.com> Date: Sun, 16 Apr 2023 11:37:40 +0200 Subject: [PATCH 01/24] add etree to build xml --- go.mod | 2 ++ go.sum | 16 +++++++++------- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/go.mod b/go.mod index 6b6696e..5ccb349 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,7 @@ module github.com/celogeek/go-comic-converter/v2 go 1.19 require ( + github.com/beevik/etree v1.1.0 github.com/disintegration/gift v1.2.1 github.com/gofrs/uuid v4.4.0+incompatible github.com/nwaples/rardecode v1.1.3 @@ -20,6 +21,7 @@ require ( github.com/mattn/go-runewidth v0.0.14 // indirect github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db // indirect github.com/rivo/uniseg v0.4.4 // indirect + github.com/stretchr/testify v1.8.2 // indirect golang.org/x/net v0.9.0 // indirect golang.org/x/sys v0.7.0 // indirect golang.org/x/term v0.7.0 // indirect diff --git a/go.sum b/go.sum index d664ac8..189f181 100644 --- a/go.sum +++ b/go.sum @@ -1,3 +1,5 @@ +github.com/beevik/etree v1.1.0 h1:T0xke/WvNtMoCqgzPhkX2r4rjY3GDZFi+FjpRZY2Jbs= +github.com/beevik/etree v1.1.0/go.mod h1:r8Aw8JqVegEf0w2fDnATrX9VpkMcyFeM0FhwO62wh+A= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= @@ -31,15 +33,18 @@ github.com/rivo/uniseg v0.4.4/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUc github.com/schollz/progressbar/v3 v3.13.1 h1:o8rySDYiQ59Mwzy2FELeHY5ZARXZTVJC7iHD6PEFUiE= github.com/schollz/progressbar/v3 v3.13.1/go.mod h1:xvrbki8kfT1fzWzBT/UZd9L6GA+jdL7HAgq2RFnO6fQ= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= -github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q= +github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= +github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= +github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= +github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8= +github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= github.com/tcnksm/go-latest v0.0.0-20170313132115-e3007ae9052e h1:IWllFTiDjjLIf2oeKxpIUmtiDV5sn71VgeQgg6vcE7k= github.com/tcnksm/go-latest v0.0.0-20170313132115-e3007ae9052e/go.mod h1:d7u6HkTYKSv5m6MCKkOQlHwaShTMl3HjqSGW3XtVhXM= github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= -golang.org/x/image v0.6.0 h1:bR8b5okrPI3g/gyZakLZHeWxAR8Dn5CyxXv1hLH5g/4= -golang.org/x/image v0.6.0/go.mod h1:MXLdDR43H7cDJq5GEGXEVeeNhPgi+YYEQ2pC1byI1x0= golang.org/x/image v0.7.0 h1:gzS29xtG1J5ybQlv0PuyfE3nmc6R4qB73m6LUUmvFuw= golang.org/x/image v0.7.0/go.mod h1:nd/q4ef1AKKYl/4kft7g+6UyGbdiqWqTP1ZAbRoV7Rg= golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= @@ -47,7 +52,6 @@ golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c= -golang.org/x/net v0.6.0 h1:L4ZwwTvKW9gr0ZMS1yrHD9GZhIuVjOBBnaKH+SPQK0Q= golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs= golang.org/x/net v0.9.0 h1:aWJ/m6xSmxWBx+V0XRHTlrYrPG56jKsLdTFmsSsCzOM= golang.org/x/net v0.9.0/go.mod h1:d48xBJpPfHeWQsugry2m+kC02ZBRGRgulfHnEXEuWns= @@ -61,14 +65,12 @@ golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBc golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.6.0 h1:MVltZSvRTcU2ljQOhs94SXPftV6DCNnZViHeQps87pQ= golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.7.0 h1:3jlCCIQZPdOYu1h8BkNvLz8Kgwtae2cagcG/VamtZRU= golang.org/x/sys v0.7.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k= -golang.org/x/term v0.6.0 h1:clScbb1cHjoCkyRbWwBEUZ5H/tIFu5TAXIqaZD0Gcjw= golang.org/x/term v0.6.0/go.mod h1:m6U89DPEgQRMq3DNkDClhWw02AUbt2daBVO4cn4Hv9U= golang.org/x/term v0.7.0 h1:BEvjmm5fURWqcfbSKTdpkDXYBrUS1c0m8agp14W48vQ= golang.org/x/term v0.7.0/go.mod h1:P32HKFT3hSsZrRxla30E9HqToFYAQPCMs/zFMBUFqPY= @@ -76,7 +78,6 @@ golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= -golang.org/x/text v0.8.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8= golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= @@ -86,5 +87,6 @@ golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8T golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= From ed1a312027a2a54ac31dcf4cb3c04491c06d0cf3 Mon Sep 17 00:00:00 2001 From: celogeek <65178+celogeek@users.noreply.github.com> Date: Sun, 16 Apr 2023 11:44:03 +0200 Subject: [PATCH 02/24] remove panelview --- README.md | 8 -- internal/converter/core.go | 1 - internal/converter/options/core.go | 4 - internal/epub/core.go | 35 ++---- internal/epub/templates.go | 6 - internal/epub/templates/panelview.css.tmpl | 103 ------------------ internal/epub/templates/text.xhtml.tmpl | 31 +----- .../epub/templates/textnopanel.xhtml.tmpl | 14 --- main.go | 1 - 9 files changed, 14 insertions(+), 189 deletions(-) delete mode 100644 internal/epub/templates/panelview.css.tmpl delete mode 100644 internal/epub/templates/textnopanel.xhtml.tmpl diff --git a/README.md b/README.md index 713c584..92309be 100644 --- a/README.md +++ b/README.md @@ -124,7 +124,6 @@ Options: NoBlankPage : false Manga : true HasCover : true - AddPanelView : false LimitMb : 200 Mb StripFirstDirectoryFromToc: true SortPathMode : path=alphanum, file=alpha @@ -162,7 +161,6 @@ Options: NoBlankPage : false Manga : true HasCover : true - AddPanelView : false LimitMb : 200 Mb StripFirstDirectoryFromToc: true SortPathMode : path=alphanum, file=alphanum @@ -210,7 +208,6 @@ Options: NoBlankPage : false Manga : false HasCover : true - AddPanelView : false LimitMb : nolimit StripFirstDirectoryFromToc: false SortPathMode : path=alphanum, file=alpha @@ -233,7 +230,6 @@ Options: NoBlankPage : false Manga : true HasCover : true - AddPanelView : false LimitMb : 200 Mb StripFirstDirectoryFromToc: false SortPathMode : path=alphanum, file=alpha @@ -258,7 +254,6 @@ Options: NoBlankPage : false Manga : false HasCover : true - AddPanelView : false LimitMb : 200 Mb StripFirstDirectoryFromToc: false SortPathMode : path=alphanum, file=alpha @@ -284,7 +279,6 @@ Options: NoBlankPage : false Manga : false HasCover : true - AddPanelView : false LimitMb : nolimit Reset default to ~/.go-comic-converter.yaml @@ -360,8 +354,6 @@ Config: Manga mode (right to left) -hascover (default true) Has cover. Indicate if your comic have a cover. The first page will be used as a cover and include after the title. - -addpanelview - Add an embeded panel view. On kindle you may not need this option as it is handled by the kindle. -limitmb int Limit size of the ePub: Default nolimit (0), Minimum 20 -strip diff --git a/internal/converter/core.go b/internal/converter/core.go index 0a01c78..1e758d0 100644 --- a/internal/converter/core.go +++ b/internal/converter/core.go @@ -95,7 +95,6 @@ func (c *Converter) InitParse() { c.AddBoolParam(&c.Options.NoBlankPage, "noblankpage", c.Options.NoBlankPage, "Remove blank pages") c.AddBoolParam(&c.Options.Manga, "manga", c.Options.Manga, "Manga mode (right to left)") c.AddBoolParam(&c.Options.HasCover, "hascover", c.Options.HasCover, "Has cover. Indicate if your comic have a cover. The first page will be used as a cover and include after the title.") - c.AddBoolParam(&c.Options.AddPanelView, "addpanelview", c.Options.AddPanelView, "Add an embeded panel view. On kindle you may not need this option as it is handled by the kindle.") c.AddIntParam(&c.Options.LimitMb, "limitmb", c.Options.LimitMb, "Limit size of the ePub: Default nolimit (0), Minimum 20") c.AddBoolParam(&c.Options.StripFirstDirectoryFromToc, "strip", c.Options.StripFirstDirectoryFromToc, "Strip first directory from the TOC if only 1") c.AddIntParam(&c.Options.SortPathMode, "sort", c.Options.SortPathMode, "Sort path mode\n0 = alpha for path and file\n1 = alphanum for path and alpha for file\n2 = alphanum for path and file") diff --git a/internal/converter/options/core.go b/internal/converter/options/core.go index bf45a4f..2e372b9 100644 --- a/internal/converter/options/core.go +++ b/internal/converter/options/core.go @@ -31,7 +31,6 @@ type Options struct { NoBlankPage bool `yaml:"no_blank_page"` Manga bool `yaml:"manga"` HasCover bool `yaml:"has_cover"` - AddPanelView bool `yaml:"add_panel_view"` LimitMb int `yaml:"limit_mb"` StripFirstDirectoryFromToc bool `yaml:"strip_first_directory_from_toc"` SortPathMode int `yaml:"sort_path_mode"` @@ -61,7 +60,6 @@ func New() *Options { NoBlankPage: false, Manga: false, HasCover: true, - AddPanelView: false, LimitMb: 0, StripFirstDirectoryFromToc: false, SortPathMode: 1, @@ -150,7 +148,6 @@ func (o *Options) ShowDefault() string { NoBlankPage : %v Manga : %v HasCover : %v - AddPanelView : %v LimitMb : %s StripFirstDirectoryFromToc: %v SortPathMode : %s`, @@ -164,7 +161,6 @@ func (o *Options) ShowDefault() string { o.NoBlankPage, o.Manga, o.HasCover, - o.AddPanelView, limitmb, o.StripFirstDirectoryFromToc, sortpathmode, diff --git a/internal/epub/core.go b/internal/epub/core.go index da0ff2c..d8e9eae 100644 --- a/internal/epub/core.go +++ b/internal/epub/core.go @@ -27,7 +27,6 @@ type ImageOptions struct { NoBlankPage bool Manga bool HasCover bool - AddPanelView bool Workers int } @@ -298,9 +297,6 @@ func (e *ePub) Write() error { "Total": totalParts, })}, } - if e.AddPanelView { - content = append(content, zipContent{"OEBPS/Text/panelview.css", panelViewTmpl}) - } if err = wz.WriteMagic(); err != nil { return err @@ -317,24 +313,21 @@ func (e *ePub) Write() error { wz.WriteImage(part.Cover.Data) } - for _, img := range part.Images { - var content string - if e.AddPanelView { - content = e.render(textTmpl, map[string]any{ - "Image": img, - "Manga": e.Manga, - }) - } else { - content = e.render(textNoPanelTmpl, map[string]any{ - "Image": img, - }) - } - - if err := wz.WriteFile(fmt.Sprintf("OEBPS/Text/%d_p%d.xhtml", img.Id, img.Part), content); err != nil { + for i, img := range part.Images { + if err := wz.WriteFile(fmt.Sprintf("OEBPS/Text/%d_p%d.xhtml", img.Id, img.Part), e.render(textTmpl, map[string]any{ + "Info": e, + "Image": img, + "Top": fmt.Sprintf("%d", (e.ViewHeight-img.Height)/2), + })); err != nil { return err } - if img.NeedSpace { + if err := wz.WriteImage(img.Data); err != nil { + return err + } + + // Double Page or Last Image + if img.DoublePage || (i+1 == len(part.Images)) { if err := wz.WriteFile( fmt.Sprintf("OEBPS/Text/%d_sp.xhtml", img.Id), e.render(blankTmpl, map[string]any{ @@ -345,10 +338,6 @@ func (e *ePub) Write() error { return err } } - - if err := wz.WriteImage(img.Data); err != nil { - return err - } } bar.Add(1) } diff --git a/internal/epub/templates.go b/internal/epub/templates.go index ad5f61d..f0dea83 100644 --- a/internal/epub/templates.go +++ b/internal/epub/templates.go @@ -17,17 +17,11 @@ var navTmpl string //go:embed "templates/style.css.tmpl" var styleTmpl string -//go:embed "templates/panelview.css.tmpl" -var panelViewTmpl string - //go:embed "templates/part.xhtml.tmpl" var partTmpl string //go:embed "templates/text.xhtml.tmpl" var textTmpl string -//go:embed "templates/textnopanel.xhtml.tmpl" -var textNoPanelTmpl string - //go:embed "templates/blank.xhtml.tmpl" var blankTmpl string diff --git a/internal/epub/templates/panelview.css.tmpl b/internal/epub/templates/panelview.css.tmpl deleted file mode 100644 index 88d9feb..0000000 --- a/internal/epub/templates/panelview.css.tmpl +++ /dev/null @@ -1,103 +0,0 @@ -a.app-amzn-magnify { - display: inline-block; - width: 100%; - height: 100%; -} - -#PV { - position: absolute; - width: 100%; - height: 100%; - top: 0; - left: 0; -} - -#PV-T { - top: 0; - width: 100%; - height: 50%; -} - -#PV-B { - bottom: 0; - width: 100%; - height: 50%; -} - -#PV-L { - left: 0; - width: 49.5%; - height: 100%; - float: left; -} - -#PV-R { - right: 0; - width: 49.5%; - height: 100%; - float: right; -} - -#PV-TL { - top: 0; - left: 0; - width: 49.5%; - height: 50%; - float: left; -} - -#PV-TR { - top: 0; - right: 0; - width: 49.5%; - height: 50%; - float: right; -} - -#PV-BL { - bottom: 0; - left: 0; - width: 49.5%; - height: 50%; - float: left; -} - -#PV-BR { - bottom: 0; - right: 0; - width: 49.5%; - height: 50%; - float: right; -} - -.PV-P { - width: 100%; - height: 100%; - top: 0; - position: absolute; - display: none; -} - -div#PV-TL-P img { - position: absolute; - left: 0; - top: 0; -} - -div#PV-TR-P img { - position: absolute; - right: 0; - top: 0; -} - -div#PV-BL-P img { - position: absolute; - left: 0; - bottom: 0; -} - -div#PV-BR-P img { - position: absolute; - right: 0; - bottom: 0; -} \ No newline at end of file diff --git a/internal/epub/templates/text.xhtml.tmpl b/internal/epub/templates/text.xhtml.tmpl index d92b6f6..f22efeb 100644 --- a/internal/epub/templates/text.xhtml.tmpl +++ b/internal/epub/templates/text.xhtml.tmpl @@ -4,38 +4,11 @@ Page {{ .Image.Id }}_p{{ .Image.Part}} - - + -
+
-
-
- -
-
- -
-
- -
-
- -
-
-
- -
-
- -
-
- -
-
- -
\ No newline at end of file diff --git a/internal/epub/templates/textnopanel.xhtml.tmpl b/internal/epub/templates/textnopanel.xhtml.tmpl deleted file mode 100644 index 0350f5a..0000000 --- a/internal/epub/templates/textnopanel.xhtml.tmpl +++ /dev/null @@ -1,14 +0,0 @@ - - - - - Page {{ .Image.Id }}_p{{ .Image.Part}} - - - - -
- -
- - \ No newline at end of file diff --git a/main.go b/main.go index 51c6b0f..116227b 100644 --- a/main.go +++ b/main.go @@ -113,7 +113,6 @@ $ go install github.com/celogeek/go-comic-converter/v%d@%s NoBlankPage: cmd.Options.NoBlankPage, Manga: cmd.Options.Manga, HasCover: cmd.Options.HasCover, - AddPanelView: cmd.Options.AddPanelView, Workers: cmd.Options.Workers, }, }).Write(); err != nil { From a7a29d6326ef9da16dfae3d6afb838d5a8552f7b Mon Sep 17 00:00:00 2001 From: celogeek <65178+celogeek@users.noreply.github.com> Date: Sun, 16 Apr 2023 11:45:00 +0200 Subject: [PATCH 03/24] rebuild toc and content handle landscape mode better handling double page with epub 3.3 center spread support improve support for apple book --- internal/epub/content.go | 197 ++++++++++++++++++++ internal/epub/core.go | 98 ++-------- internal/epub/image_processing.go | 94 ++++++---- internal/epub/templates.go | 10 +- internal/epub/templates/applebooks.xml.tmpl | 6 + internal/epub/templates/blank.xhtml.tmpl | 1 - internal/epub/templates/container.xml.tmpl | 9 +- internal/epub/templates/content.opf.tmpl | 57 ------ internal/epub/templates/nav.xhtml.tmpl | 16 -- internal/epub/templates/part.xhtml.tmpl | 4 +- internal/epub/templates/style.css.tmpl | 18 +- internal/epub/templates/toc.ncx.tmpl | 14 -- internal/epub/toc.go | 60 ++++-- internal/epub/tree.go | 17 ++ 14 files changed, 361 insertions(+), 240 deletions(-) create mode 100644 internal/epub/content.go create mode 100644 internal/epub/templates/applebooks.xml.tmpl delete mode 100644 internal/epub/templates/content.opf.tmpl delete mode 100644 internal/epub/templates/nav.xhtml.tmpl delete mode 100644 internal/epub/templates/toc.ncx.tmpl diff --git a/internal/epub/content.go b/internal/epub/content.go new file mode 100644 index 0000000..5310c7b --- /dev/null +++ b/internal/epub/content.go @@ -0,0 +1,197 @@ +package epub + +import ( + "fmt" + + "github.com/beevik/etree" +) + +type Content struct { + doc *etree.Document +} + +type TagAttrs map[string]string + +type Tag struct { + name string + attrs TagAttrs + value string +} + +func (e *ePub) getMeta(title string, part *epubPart, currentPart, totalPart int) []Tag { + metas := []Tag{ + {"meta", TagAttrs{"property": "dcterms:modified"}, e.UpdatedAt}, + {"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"}, + {"meta", TagAttrs{"property": "schema:accessibilityHazard"}, "noMotionSimulationHazard"}, + {"meta", TagAttrs{"property": "schema:accessibilityHazard"}, "noSoundHazard"}, + {"meta", TagAttrs{"name": "book-type", "content": "comic"}, ""}, + {"opf:meta", TagAttrs{"name": "fixed-layout", "content": "true"}, ""}, + {"opf:meta", TagAttrs{"name": "original-resolution", "content": fmt.Sprintf("%dx%d", e.ViewWidth, e.ViewHeight)}, ""}, + {"dc:title", TagAttrs{}, title}, + {"dc:identifier", TagAttrs{"id": "ean"}, fmt.Sprintf("urn:uuid:%s", e.UID)}, + {"dc:language", TagAttrs{}, "en"}, + {"dc:creator", TagAttrs{}, e.Author}, + {"dc:publisher", TagAttrs{}, e.Publisher}, + {"dc:contributor", TagAttrs{}, "Go Comic Convertor"}, + {"dc:date", TagAttrs{}, e.UpdatedAt}, + } + + if e.Manga { + metas = append(metas, Tag{"meta", TagAttrs{"name": "primary-writing-mode", "content": "horizontal-rl"}, ""}) + } else { + metas = append(metas, Tag{"meta", TagAttrs{"name": "primary-writing-mode", "content": "horizontal-lr"}, ""}) + } + + if part.Cover != nil { + metas = append(metas, Tag{"meta", TagAttrs{"name": "cover", "content": part.Cover.Key("img")}, ""}) + } + + if totalPart > 1 { + metas = append( + metas, + Tag{"meta", TagAttrs{"name": "calibre:series", "content": e.Title}, ""}, + Tag{"meta", TagAttrs{"name": "calibre:series_index", "content": fmt.Sprint(currentPart)}, ""}, + ) + } + + return metas +} + +func (e *ePub) getManifest(title string, part *epubPart, currentPart, totalPart int) []Tag { + iTag := func(img *Image) Tag { + return Tag{"item", TagAttrs{"id": img.Key("img"), "href": img.ImgPath(), "media-type": "image/jpeg"}, ""} + } + hTag := func(img *Image) Tag { + return Tag{"item", TagAttrs{"id": img.Key("page"), "href": img.TextPath(), "media-type": "application/xhtml+xml"}, ""} + } + sTag := func(img *Image) Tag { + return Tag{"item", TagAttrs{"id": img.SpaceKey("page"), "href": img.SpacePath(), "media-type": "application/xhtml+xml"}, ""} + } + items := []Tag{ + {"item", TagAttrs{"id": "toc", "href": "toc.xhtml", "properties": "nav", "media-type": "application/xhtml+xml"}, ""}, + {"item", TagAttrs{"id": "css", "href": "Text/style.css", "media-type": "text/css"}, ""}, + } + + if part.Cover != nil { + items = append(items, iTag(part.Cover), hTag(part.Cover)) + } + + for _, img := range part.Images { + if img.Part == 1 { + items = append(items, sTag(img)) + } + items = append(items, iTag(img), hTag(img)) + } + items = append(items, sTag(part.Images[len(part.Images)-1])) + + return items +} + +func (e *ePub) getSpine(title string, part *epubPart, currentPart, totalPart int) []Tag { + spine := []Tag{} + isOnTheRight := !e.Manga + getSpread := func(doublePageNoBlank bool) string { + isOnTheRight = !isOnTheRight + if doublePageNoBlank { + // Center the double page then start back to comic mode (mange/normal) + isOnTheRight = !e.Manga + return "rendition:page-spread-center" + } + if isOnTheRight { + return "rendition:page-spread-right" + } else { + return "rendition:page-spread-left" + } + } + for _, img := range part.Images { + spine = append(spine, Tag{ + "itemref", + TagAttrs{"idref": img.Key("page"), "properties": getSpread(img.DoublePage && e.NoBlankPage)}, + "", + }) + if img.DoublePage && isOnTheRight && !e.NoBlankPage { + spine = append(spine, Tag{ + "itemref", + TagAttrs{"idref": img.SpaceKey("page"), "properties": getSpread(false)}, + "", + }) + } + } + if e.Manga == isOnTheRight { + spine = append(spine, Tag{ + "itemref", + TagAttrs{"idref": part.Images[len(part.Images)-1].SpaceKey("page"), "properties": getSpread(false)}, + "", + }) + } + + return spine +} + +func (e *ePub) getGuide(title string, part *epubPart, currentPart, totalPart int) []Tag { + guide := []Tag{} + if part.Cover != nil { + guide = append(guide, Tag{"reference", TagAttrs{"type": "cover", "title": "cover", "href": part.Cover.TextPath()}, ""}) + } + guide = append(guide, Tag{"reference", TagAttrs{"type": "text", "title": "content", "href": part.Images[0].TextPath()}, ""}) + return guide +} + +func (e *ePub) getContent(title string, part *epubPart, currentPart, totalPart int) *Content { + doc := etree.NewDocument() + doc.CreateProcInst("xml", `version="1.0" encoding="UTF-8"`) + + pkg := doc.CreateElement("package") + 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/# 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) { + meta := elm.CreateElement(p.name) + for k, v := range p.attrs { + meta.CreateAttr(k, v) + } + meta.SortAttrs() + if p.value != "" { + meta.CreateText(p.value) + } + } + } + + metadata := pkg.CreateElement("metadata") + metadata.CreateAttr("xmlns:dc", "http://purl.org/dc/elements/1.1/") + metadata.CreateAttr("xmlns:opf", "http://www.idpf.org/2007/opf") + addToElement(metadata, e.getMeta) + + manifest := pkg.CreateElement("manifest") + addToElement(manifest, e.getManifest) + + spine := pkg.CreateElement("spine") + if e.Manga { + spine.CreateAttr("page-progression-direction", "rtl") + } else { + spine.CreateAttr("page-progression-direction", "ltr") + } + addToElement(spine, e.getSpine) + + guide := pkg.CreateElement("guide") + addToElement(guide, e.getGuide) + + return &Content{ + doc, + } +} + +func (c *Content) String() string { + c.doc.Indent(2) + r, _ := c.doc.WriteToString() + return r +} diff --git a/internal/epub/core.go b/internal/epub/core.go index d8e9eae..21dd5a4 100644 --- a/internal/epub/core.go +++ b/internal/epub/core.go @@ -1,7 +1,6 @@ package epub import ( - "encoding/xml" "fmt" "os" "path/filepath" @@ -134,7 +133,6 @@ func (e *ePub) getParts() ([]*epubPart, error) { currentSize := baseSize currentImages := make([]*Image, 0) part := 1 - imgIsOnRightSide := false for _, img := range images { imgSize := img.Data.CompressedSize() + xhtmlSize @@ -144,14 +142,11 @@ func (e *ePub) getParts() ([]*epubPart, error) { Images: currentImages, }) part += 1 - imgIsOnRightSide = false currentSize = baseSize currentImages = make([]*Image, 0) } currentSize += imgSize - img.NeedSpace = img.Part == 1 && imgIsOnRightSide currentImages = append(currentImages, img) - imgIsOnRightSide = !imgIsOnRightSide } if len(currentImages) > 0 { parts = append(parts, &epubPart{ @@ -163,59 +158,6 @@ func (e *ePub) getParts() ([]*epubPart, error) { return parts, nil } -func (e *ePub) getToc(images []*Image) *TocChildren { - paths := map[string]*TocPart{ - ".": {}, - } - for _, img := range images { - currentPath := "." - for _, path := range strings.Split(img.Path, string(filepath.Separator)) { - parentPath := currentPath - currentPath = filepath.Join(currentPath, path) - if _, ok := paths[currentPath]; ok { - continue - } - part := &TocPart{ - Title: TocTitle{ - Value: path, - Link: fmt.Sprintf("Text/%d_p%d.xhtml", img.Id, img.Part), - }, - } - paths[currentPath] = part - if paths[parentPath].Children == nil { - paths[parentPath].Children = &TocChildren{} - } - paths[parentPath].Children.Tags = append(paths[parentPath].Children.Tags, part) - } - } - - children := paths["."].Children - - if children != nil && e.StripFirstDirectoryFromToc && len(children.Tags) == 1 { - children = children.Tags[0].Children - } - - return children - -} - -func (e *ePub) getTree(images []*Image, skip_files bool) string { - t := NewTree() - for _, img := range images { - if skip_files { - t.Add(img.Path) - } else { - t.Add(filepath.Join(img.Path, img.Name)) - } - } - c := t.Root() - if skip_files && e.StripFirstDirectoryFromToc && len(c.Children) == 1 { - c = c.Children[0] - } - - return c.toString("") -} - func (e *ePub) Write() error { type zipContent struct { Name string @@ -263,33 +205,11 @@ func (e *ePub) Write() error { title = fmt.Sprintf("%s [%d/%d]", title, i+1, totalParts) } - tocChildren := e.getToc(part.Images) - toc := []byte{} - if tocChildren != nil { - toc, err = xml.MarshalIndent(tocChildren.Tags, " ", " ") - if err != nil { - return err - } - } - content := []zipContent{ {"META-INF/container.xml", containerTmpl}, - {"OEBPS/content.opf", e.render(contentTmpl, map[string]any{ - "Info": e, - "Cover": part.Cover, - "Images": part.Images, - "Title": title, - "Part": i + 1, - "Total": totalParts, - })}, - {"OEBPS/toc.ncx", e.render(tocTmpl, map[string]any{ - "Info": e, - "Title": title, - })}, - {"OEBPS/nav.xhtml", e.render(navTmpl, map[string]any{ - "Title": title, - "TOC": string(toc), - })}, + {"META-INF/com.apple.ibooks.display-options.xml", appleBooksTmpl}, + {"OEBPS/content.opf", e.getContent(title, part, i+1, totalParts).String()}, + {"OEBPS/toc.xhtml", e.getToc(title, part.Images)}, {"OEBPS/Text/style.css", styleTmpl}, {"OEBPS/Text/part.xhtml", e.render(partTmpl, map[string]any{ "Info": e, @@ -310,7 +230,17 @@ func (e *ePub) Write() error { // Cover exist or part > 1 // If no cover, part 2 and more will include the image as a cover if e.HasCover || i > 0 { - wz.WriteImage(part.Cover.Data) + if err := wz.WriteFile(fmt.Sprintf("OEBPS/%s", part.Cover.TextPath()), e.render(textTmpl, map[string]any{ + "Info": e, + "Image": part.Cover, + "Manga": e.Manga, + "Top": fmt.Sprintf("%d", (e.ViewHeight-part.Cover.Height)/2), + })); err != nil { + return err + } + if err := wz.WriteImage(part.Cover.Data); err != nil { + return err + } } for i, img := range part.Images { diff --git a/internal/epub/image_processing.go b/internal/epub/image_processing.go index f1e345a..f9f6018 100644 --- a/internal/epub/image_processing.go +++ b/internal/epub/image_processing.go @@ -26,15 +26,35 @@ import ( ) type Image struct { - Id int - Part int - Data *ImageData - Width int - Height int - IsCover bool - NeedSpace bool - Path string - Name string + Id int + Part int + Data *ImageData + Width int + Height int + IsCover bool + DoublePage bool + Path string + Name string +} + +func (i *Image) Key(prefix string) string { + return fmt.Sprintf("%s_%d_p%d", prefix, i.Id, i.Part) +} + +func (i *Image) SpaceKey(prefix string) string { + return fmt.Sprintf("%s_%d_sp", prefix, i.Id) +} + +func (i *Image) TextPath() string { + return fmt.Sprintf("Text/%d_p%d.xhtml", i.Id, i.Part) +} + +func (i *Image) ImgPath() string { + return fmt.Sprintf("Images/%d_p%d.jpg", i.Id, i.Part) +} + +func (i *Image) SpacePath() string { + return fmt.Sprintf("Text/%d_sp.xhtml", i.Id) } type imageTask struct { @@ -130,15 +150,15 @@ func (e *ePub) LoadImages() ([]*Image, error) { for img := range imageInput { img.Reader.Close() images = append(images, &Image{ - Id: img.Id, - Part: 0, - Data: nil, - Width: 0, - Height: 0, - IsCover: false, - NeedSpace: false, // NeedSpace reajust during parts computation - Path: img.Path, - Name: img.Name, + Id: img.Id, + Part: 0, + Data: nil, + Width: 0, + Height: 0, + IsCover: false, + DoublePage: false, + Path: img.Path, + Name: img.Name, }) } @@ -180,15 +200,17 @@ func (e *ePub) LoadImages() ([]*Image, error) { g.Draw(dst, src) imageOutput <- &Image{ - Id: img.Id, - Part: 0, - Data: newImageData(img.Id, 0, dst, e.ImageOptions.Quality), - Width: dst.Bounds().Dx(), - Height: dst.Bounds().Dy(), - IsCover: img.Id == 0, - NeedSpace: false, - Path: img.Path, - Name: img.Name, + Id: img.Id, + Part: 0, + Data: newImageData(img.Id, 0, dst, e.ImageOptions.Quality), + Width: dst.Bounds().Dx(), + Height: dst.Bounds().Dy(), + IsCover: img.Id == 0, + DoublePage: src.Bounds().Dx() > src.Bounds().Dy() && + src.Bounds().Dx() > e.ImageOptions.ViewHeight && + src.Bounds().Dy() > e.ImageOptions.ViewWidth, + Path: img.Path, + Name: img.Name, } // Auto split double page @@ -205,15 +227,15 @@ func (e *ePub) LoadImages() ([]*Image, error) { dst := image.NewGray(g.Bounds(src.Bounds())) g.Draw(dst, src) imageOutput <- &Image{ - Id: img.Id, - Part: part, - Data: newImageData(img.Id, part, dst, e.ImageOptions.Quality), - Width: dst.Bounds().Dx(), - Height: dst.Bounds().Dy(), - IsCover: false, - NeedSpace: false, // NeedSpace reajust during parts computation - Path: img.Path, - Name: img.Name, + Id: img.Id, + Part: part, + Data: newImageData(img.Id, part, dst, e.ImageOptions.Quality), + Width: dst.Bounds().Dx(), + Height: dst.Bounds().Dy(), + IsCover: false, + DoublePage: false, + Path: img.Path, + Name: img.Name, } } } diff --git a/internal/epub/templates.go b/internal/epub/templates.go index f0dea83..f885161 100644 --- a/internal/epub/templates.go +++ b/internal/epub/templates.go @@ -5,14 +5,8 @@ import _ "embed" //go:embed "templates/container.xml.tmpl" var containerTmpl string -//go:embed "templates/content.opf.tmpl" -var contentTmpl string - -//go:embed "templates/toc.ncx.tmpl" -var tocTmpl string - -//go:embed "templates/nav.xhtml.tmpl" -var navTmpl string +//go:embed "templates/applebooks.xml.tmpl" +var appleBooksTmpl string //go:embed "templates/style.css.tmpl" var styleTmpl string diff --git a/internal/epub/templates/applebooks.xml.tmpl b/internal/epub/templates/applebooks.xml.tmpl new file mode 100644 index 0000000..aad0f8c --- /dev/null +++ b/internal/epub/templates/applebooks.xml.tmpl @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/internal/epub/templates/blank.xhtml.tmpl b/internal/epub/templates/blank.xhtml.tmpl index 0252f8b..4a81627 100644 --- a/internal/epub/templates/blank.xhtml.tmpl +++ b/internal/epub/templates/blank.xhtml.tmpl @@ -7,6 +7,5 @@ -

{{ if .Info.Manga }}←{{ else }}→{{ end }}

\ No newline at end of file diff --git a/internal/epub/templates/container.xml.tmpl b/internal/epub/templates/container.xml.tmpl index e1d3db9..c0eba46 100644 --- a/internal/epub/templates/container.xml.tmpl +++ b/internal/epub/templates/container.xml.tmpl @@ -1,6 +1,7 @@ - - - - + + + + \ No newline at end of file diff --git a/internal/epub/templates/content.opf.tmpl b/internal/epub/templates/content.opf.tmpl deleted file mode 100644 index 730190c..0000000 --- a/internal/epub/templates/content.opf.tmpl +++ /dev/null @@ -1,57 +0,0 @@ - - -{{ $info := .Info }} - - {{ .Title }} - en-US - urn:uuid:{{ $info.UID }} - {{ $info.Publisher }} - {{ $info.Publisher }} - {{ $info.UpdatedAt }} - {{ $info.Author }} - {{ $info.UpdatedAt }} - - - - pre-paginated - portrait - -{{ if eq $info.AddPanelView true }} - -{{ end }} -{{ if gt .Total 1 }} - - -{{ end }} - - - - - -{{ if eq $info.AddPanelView true }} - -{{ end }} - -{{ range .Images }} -{{ if eq .IsCover false }} - -{{ end }} -{{ end }} - -{{ range .Images }} - -{{ if eq .NeedSpace true }} - -{{ end }} -{{ end }} - - - -{{ range .Images }} -{{ if eq .NeedSpace true }} - -{{ end }} - -{{ end }} - - diff --git a/internal/epub/templates/nav.xhtml.tmpl b/internal/epub/templates/nav.xhtml.tmpl deleted file mode 100644 index 115bd66..0000000 --- a/internal/epub/templates/nav.xhtml.tmpl +++ /dev/null @@ -1,16 +0,0 @@ - - - - - {{ .Title }} - - - - - \ No newline at end of file diff --git a/internal/epub/templates/part.xhtml.tmpl b/internal/epub/templates/part.xhtml.tmpl index 15dfed0..c152015 100644 --- a/internal/epub/templates/part.xhtml.tmpl +++ b/internal/epub/templates/part.xhtml.tmpl @@ -7,9 +7,9 @@ -

{{ .Info.Title }}

+

{{ .Info.Title }}

{{ if gt .Total 1 }} -

Part {{ .Part }} / {{ .Total }}

+

Part {{ .Part }} / {{ .Total }}

{{ end }} \ No newline at end of file diff --git a/internal/epub/templates/style.css.tmpl b/internal/epub/templates/style.css.tmpl index 2bbe2ef..da8ed34 100644 --- a/internal/epub/templates/style.css.tmpl +++ b/internal/epub/templates/style.css.tmpl @@ -6,10 +6,8 @@ html { } body { - font-size: 16px; + font-size: 1em; text-align: center; - width: 100%; - height: 100%; } body, @@ -64,10 +62,22 @@ h3, h4, h5, h6 { - font-size: 150%; + -webkit-hyphens:none; font-weight: normal; } +h1 { + font-size: 200%; +} + +h2 { + font-size: 150%; +} + +h3 { + font-size: 120%; +} + sup { vertical-align: text-top; } diff --git a/internal/epub/templates/toc.ncx.tmpl b/internal/epub/templates/toc.ncx.tmpl deleted file mode 100644 index 42fec3f..0000000 --- a/internal/epub/templates/toc.ncx.tmpl +++ /dev/null @@ -1,14 +0,0 @@ - - - - - - - - - -{{ .Title }} - -{{ .Title }} - - \ No newline at end of file diff --git a/internal/epub/toc.go b/internal/epub/toc.go index 7bda2ef..d354457 100644 --- a/internal/epub/toc.go +++ b/internal/epub/toc.go @@ -1,22 +1,54 @@ package epub import ( - "encoding/xml" + "path/filepath" + "strings" + + "github.com/beevik/etree" ) -type TocTitle struct { - XMLName xml.Name `xml:"a"` - Value string `xml:",innerxml"` - Link string `xml:"href,attr"` -} +func (e *ePub) getToc(title string, images []*Image) string { + doc := etree.NewDocument() + doc.CreateProcInst("xml", `version="1.0" encoding="UTF-8"`) + doc.CreateDirective("DOCTYPE html") -type TocChildren struct { - XMLName xml.Name `xml:"ol"` - Tags []*TocPart -} + html := doc.CreateElement("html") + html.CreateAttr("xmlns", "http://www.w3.org/1999/xhtml") + html.CreateAttr("xmlns:epub", "http://www.idpf.org/2007/ops") -type TocPart struct { - XMLName xml.Name `xml:"li"` - Title TocTitle - Children *TocChildren `xml:",omitempty"` + html.CreateElement("head").CreateElement("title").CreateText(title) + body := html.CreateElement("body") + nav := body.CreateElement("nav") + nav.CreateAttr("epub:type", "toc") + nav.CreateAttr("id", "toc") + nav.CreateElement("h2").CreateText(title) + + ol := etree.NewElement("ol") + paths := map[string]*etree.Element{".": ol} + for _, img := range images { + currentPath := "." + for _, path := range strings.Split(img.Path, string(filepath.Separator)) { + parentPath := currentPath + currentPath = filepath.Join(currentPath, path) + if _, ok := paths[currentPath]; ok { + continue + } + t := paths[parentPath].CreateElement("li") + link := t.CreateElement("a") + link.CreateAttr("href", img.TextPath()) + link.CreateText(path) + paths[currentPath] = t + } + } + + if len(ol.ChildElements()) == 1 && e.StripFirstDirectoryFromToc { + ol = ol.ChildElements()[0] + } + if len(ol.ChildElements()) > 0 { + nav.AddChild(ol) + } + + doc.Indent(2) + r, _ := doc.WriteToString() + return r } diff --git a/internal/epub/tree.go b/internal/epub/tree.go index 7543985..cac65f0 100644 --- a/internal/epub/tree.go +++ b/internal/epub/tree.go @@ -51,3 +51,20 @@ func (n *Node) toString(indent string) string { } return r.String() } + +func (e *ePub) getTree(images []*Image, skip_files bool) string { + t := NewTree() + for _, img := range images { + if skip_files { + t.Add(img.Path) + } else { + t.Add(filepath.Join(img.Path, img.Name)) + } + } + c := t.Root() + if skip_files && e.StripFirstDirectoryFromToc && len(c.Children) == 1 { + c = c.Children[0] + } + + return c.toString("") +} From 8d24f95579494bf43e1fd78d90d4390adbee0870 Mon Sep 17 00:00:00 2001 From: celogeek <65178+celogeek@users.noreply.github.com> Date: Sun, 16 Apr 2023 15:19:57 +0200 Subject: [PATCH 04/24] simplify css --- internal/epub/templates/style.css.tmpl | 87 ++----------------------- internal/epub/templates/text.xhtml.tmpl | 4 +- 2 files changed, 9 insertions(+), 82 deletions(-) diff --git a/internal/epub/templates/style.css.tmpl b/internal/epub/templates/style.css.tmpl index da8ed34..606e95c 100644 --- a/internal/epub/templates/style.css.tmpl +++ b/internal/epub/templates/style.css.tmpl @@ -1,87 +1,14 @@ -@charset "UTF-8"; - -html { +body { color: #000; background: #FFF; -} - -body { - font-size: 1em; - text-align: center; -} - -body, -div, -dl, -dt, -dd, -ul, -ol, -li, -h1, -h2, -h3, -h4, -h5, -h6, -th, -td { margin: 0; - padding: 0 + padding: 0; } -table { - border-collapse: collapse; - border-spacing: 0; -} - -fieldset, img { - border: 0; -} - -caption, -th, -var { - font-style: normal; - font-weight: normal; -} - -li { - list-style: none; -} - -caption, -th { - text-align: left; -} - -h1, -h2, -h3, -h4, -h5, -h6 { - -webkit-hyphens:none; - font-weight: normal; -} - -h1 { - font-size: 200%; -} - -h2 { - font-size: 150%; -} - -h3 { - font-size: 120%; -} - -sup { - vertical-align: text-top; -} - -sub { - vertical-align: text-bottom; + position: absolute; + margin: 0; + padding: 0; + top: 0; + left: 0; } \ No newline at end of file diff --git a/internal/epub/templates/text.xhtml.tmpl b/internal/epub/templates/text.xhtml.tmpl index f22efeb..18e7200 100644 --- a/internal/epub/templates/text.xhtml.tmpl +++ b/internal/epub/templates/text.xhtml.tmpl @@ -7,8 +7,8 @@ -
- +
+
\ No newline at end of file From 02583cc715120f3efcd299290804233072679c02 Mon Sep 17 00:00:00 2001 From: celogeek <65178+celogeek@users.noreply.github.com> Date: Sun, 16 Apr 2023 18:02:31 +0200 Subject: [PATCH 05/24] factor rendering --- internal/epub/core.go | 67 +++++++++++-------- internal/epub/templates.go | 4 +- internal/epub/templates/blank.xhtml.tmpl | 4 +- internal/epub/templates/style.css.tmpl | 11 ++- internal/epub/templates/text.xhtml.tmpl | 6 +- .../{part.xhtml.tmpl => title.xhtml.tmpl} | 0 6 files changed, 52 insertions(+), 40 deletions(-) rename internal/epub/templates/{part.xhtml.tmpl => title.xhtml.tmpl} (100%) diff --git a/internal/epub/core.go b/internal/epub/core.go index 21dd5a4..5a04503 100644 --- a/internal/epub/core.go +++ b/internal/epub/core.go @@ -93,6 +93,38 @@ func (e *ePub) render(templateString string, data any) string { return stripBlank.ReplaceAllString(result.String(), "\n") } +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), + "ImageStyle": fmt.Sprintf( + "width:%dpx; height:%dpx;", + img.Width, + img.Height, + ), + "ImagePath": img.ImgPath(), + }), + ) + + if err == nil { + err = wz.WriteImage(img.Data) + } + + return err +} + +func (e *ePub) writeBlank(wz *epubZip, img *Image) error { + return wz.WriteFile( + 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), + }), + ) +} + func (e *ePub) getParts() ([]*epubPart, error) { images, err := e.LoadImages() @@ -210,8 +242,11 @@ func (e *ePub) Write() error { {"META-INF/com.apple.ibooks.display-options.xml", appleBooksTmpl}, {"OEBPS/content.opf", e.getContent(title, part, i+1, totalParts).String()}, {"OEBPS/toc.xhtml", e.getToc(title, part.Images)}, - {"OEBPS/Text/style.css", styleTmpl}, - {"OEBPS/Text/part.xhtml", e.render(partTmpl, map[string]any{ + {"OEBPS/Text/style.css", e.render(styleTmpl, map[string]any{ + "PageWidth": e.ViewWidth, + "PageHeight": e.ViewHeight, + })}, + {"OEBPS/Text/title.xhtml", e.render(titleTmpl, map[string]any{ "Info": e, "Part": i + 1, "Total": totalParts, @@ -230,41 +265,19 @@ func (e *ePub) Write() error { // Cover exist or part > 1 // If no cover, part 2 and more will include the image as a cover if e.HasCover || i > 0 { - if err := wz.WriteFile(fmt.Sprintf("OEBPS/%s", part.Cover.TextPath()), e.render(textTmpl, map[string]any{ - "Info": e, - "Image": part.Cover, - "Manga": e.Manga, - "Top": fmt.Sprintf("%d", (e.ViewHeight-part.Cover.Height)/2), - })); err != nil { - return err - } - if err := wz.WriteImage(part.Cover.Data); err != nil { + if err := e.writeImage(wz, part.Cover); err != nil { return err } } for i, img := range part.Images { - if err := wz.WriteFile(fmt.Sprintf("OEBPS/Text/%d_p%d.xhtml", img.Id, img.Part), e.render(textTmpl, map[string]any{ - "Info": e, - "Image": img, - "Top": fmt.Sprintf("%d", (e.ViewHeight-img.Height)/2), - })); err != nil { - return err - } - - if err := wz.WriteImage(img.Data); err != nil { + if err := e.writeImage(wz, img); err != nil { return err } // Double Page or Last Image if img.DoublePage || (i+1 == len(part.Images)) { - if err := wz.WriteFile( - fmt.Sprintf("OEBPS/Text/%d_sp.xhtml", img.Id), - e.render(blankTmpl, map[string]any{ - "Info": e, - "Image": img, - }), - ); err != nil { + if err := e.writeBlank(wz, img); err != nil { return err } } diff --git a/internal/epub/templates.go b/internal/epub/templates.go index f885161..e25212b 100644 --- a/internal/epub/templates.go +++ b/internal/epub/templates.go @@ -11,8 +11,8 @@ var appleBooksTmpl string //go:embed "templates/style.css.tmpl" var styleTmpl string -//go:embed "templates/part.xhtml.tmpl" -var partTmpl string +//go:embed "templates/title.xhtml.tmpl" +var titleTmpl string //go:embed "templates/text.xhtml.tmpl" var textTmpl string diff --git a/internal/epub/templates/blank.xhtml.tmpl b/internal/epub/templates/blank.xhtml.tmpl index 4a81627..e3ba233 100644 --- a/internal/epub/templates/blank.xhtml.tmpl +++ b/internal/epub/templates/blank.xhtml.tmpl @@ -2,9 +2,9 @@ - Page {{ .Image.Id }} Space + {{ .Title }} - + diff --git a/internal/epub/templates/style.css.tmpl b/internal/epub/templates/style.css.tmpl index 606e95c..2cac722 100644 --- a/internal/epub/templates/style.css.tmpl +++ b/internal/epub/templates/style.css.tmpl @@ -3,12 +3,11 @@ body { background: #FFF; margin: 0; padding: 0; + width: {{ .PageWidth }}px; + height: {{ .PageHeight }}px; } -img { - position: absolute; - margin: 0; - padding: 0; - top: 0; - left: 0; +div { + margin:0; + padding:0; } \ No newline at end of file diff --git a/internal/epub/templates/text.xhtml.tmpl b/internal/epub/templates/text.xhtml.tmpl index 18e7200..5741e3d 100644 --- a/internal/epub/templates/text.xhtml.tmpl +++ b/internal/epub/templates/text.xhtml.tmpl @@ -2,13 +2,13 @@ - Page {{ .Image.Id }}_p{{ .Image.Part}} + {{ .Title }} - +
- +
\ No newline at end of file diff --git a/internal/epub/templates/part.xhtml.tmpl b/internal/epub/templates/title.xhtml.tmpl similarity index 100% rename from internal/epub/templates/part.xhtml.tmpl rename to internal/epub/templates/title.xhtml.tmpl From 17d8161d995531d0b95a01592122c6ef57b88369 Mon Sep 17 00:00:00 2001 From: celogeek <65178+celogeek@users.noreply.github.com> Date: Sun, 16 Apr 2023 18:02:55 +0200 Subject: [PATCH 06/24] fix filter double page --- internal/epub/image_filters.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/epub/image_filters.go b/internal/epub/image_filters.go index 058f5e1..450ec43 100644 --- a/internal/epub/image_filters.go +++ b/internal/epub/image_filters.go @@ -46,7 +46,7 @@ func NewGiftSplitDoublePage(options *ImageOptions) []*gift.GIFT { g.Add(gift.Brightness(float32(options.Brightness))) } g.Add( - gift.ResizeToFit(options.ViewWidth, options.ViewHeight, gift.LanczosResampling), + filters.Resize(options.ViewWidth, options.ViewHeight, gift.LanczosResampling), ) } From a2eeda847931829fa4cec5e3937ee958959caa40 Mon Sep 17 00:00:00 2001 From: celogeek <65178+celogeek@users.noreply.github.com> Date: Sun, 16 Apr 2023 18:25:41 +0200 Subject: [PATCH 07/24] use gift filters to create aligned images --- internal/epub/filters/position.go | 55 +++++++++++++++++++++++++++++++ internal/epub/image_filters.go | 16 ++++++--- internal/epub/image_processing.go | 1 + 3 files changed, 67 insertions(+), 5 deletions(-) create mode 100644 internal/epub/filters/position.go diff --git a/internal/epub/filters/position.go b/internal/epub/filters/position.go new file mode 100644 index 0000000..3c3d3b4 --- /dev/null +++ b/internal/epub/filters/position.go @@ -0,0 +1,55 @@ +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 := (p.viewWidth-srcBounds.Dx())/2, (p.viewHeight-srcBounds.Dy())/2 + if p.align == PositionLeft { + left = 0 + } + + 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, + ) +} diff --git a/internal/epub/image_filters.go b/internal/epub/image_filters.go index 450ec43..0eaa039 100644 --- a/internal/epub/image_filters.go +++ b/internal/epub/image_filters.go @@ -21,6 +21,7 @@ func NewGift(options *ImageOptions) *gift.GIFT { g.Add( filters.Resize(options.ViewWidth, options.ViewHeight, gift.LanczosResampling), filters.Pixel(), + filters.Position(options.ViewWidth, options.ViewHeight, filters.PositionCenter), ) return g } @@ -28,25 +29,30 @@ func NewGift(options *ImageOptions) *gift.GIFT { func NewGiftSplitDoublePage(options *ImageOptions) []*gift.GIFT { gifts := make([]*gift.GIFT, 2) - rightFirst := options.Manga - gifts[0] = gift.New( - filters.CropSplitDoublePage(rightFirst), + filters.CropSplitDoublePage(options.Manga), ) gifts[1] = gift.New( - filters.CropSplitDoublePage(!rightFirst), + filters.CropSplitDoublePage(!options.Manga), ) - for _, g := range gifts { + for i, g := range gifts { if options.Contrast != 0 { g.Add(gift.Contrast(float32(options.Contrast))) } if options.Brightness != 0 { 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/image_processing.go b/internal/epub/image_processing.go index f9f6018..10d6147 100644 --- a/internal/epub/image_processing.go +++ b/internal/epub/image_processing.go @@ -226,6 +226,7 @@ func (e *ePub) LoadImages() ([]*Image, error) { part := i + 1 dst := image.NewGray(g.Bounds(src.Bounds())) g.Draw(dst, src) + imageOutput <- &Image{ Id: img.Id, Part: part, From 7f195a0b791ade18eae91ee6480ab5bd62bc7425 Mon Sep 17 00:00:00 2001 From: celogeek <65178+celogeek@users.noreply.github.com> Date: Sat, 22 Apr 2023 10:24:47 +0200 Subject: [PATCH 08/24] use ratio 1.5 for perfect both side rendering --- internal/converter/profiles/core.go | 13 +++++++++++++ main.go | 6 ++++-- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/internal/converter/profiles/core.go b/internal/converter/profiles/core.go index 0a5a2fd..e370196 100644 --- a/internal/converter/profiles/core.go +++ b/internal/converter/profiles/core.go @@ -12,6 +12,19 @@ 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 { diff --git a/main.go b/main.go index 116227b..e2cb6b0 100644 --- a/main.go +++ b/main.go @@ -91,6 +91,8 @@ $ 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, @@ -102,8 +104,8 @@ $ go install github.com/celogeek/go-comic-converter/v%d@%s DryVerbose: cmd.Options.DryVerbose, SortPathMode: cmd.Options.SortPathMode, ImageOptions: &epub.ImageOptions{ - ViewWidth: profile.Width, - ViewHeight: profile.Height, + ViewWidth: perfectWidth, + ViewHeight: perfectHeight, Quality: cmd.Options.Quality, Crop: cmd.Options.Crop, Brightness: cmd.Options.Brightness, From bc19e5e4ce5810ac54c9926f73ee22994d86966e Mon Sep 17 00:00:00 2001 From: celogeek <65178+celogeek@users.noreply.github.com> Date: Sat, 22 Apr 2023 10:25:09 +0200 Subject: [PATCH 09/24] remove ibook meta, not used --- internal/epub/content.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/internal/epub/content.go b/internal/epub/content.go index 5310c7b..d5b7b18 100644 --- a/internal/epub/content.go +++ b/internal/epub/content.go @@ -24,7 +24,6 @@ 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"}, @@ -151,7 +150,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/# ibooks: http://vocabulary.itunes.apple.com/rdf/ibooks/vocabulary-extensions-1.0/") + pkg.CreateAttr("prefix", "rendition: http://www.idpf.org/vocab/rendition/#") addToElement := func(elm *etree.Element, meth func(title string, part *epubPart, currentPart, totalPart int) []Tag) { for _, p := range meth(title, part, currentPart, totalPart) { From 4a437605350cae11c9fd0131952ab4d3ba558f45 Mon Sep 17 00:00:00 2001 From: celogeek <65178+celogeek@users.noreply.github.com> Date: Sat, 22 Apr 2023 10:26:11 +0200 Subject: [PATCH 10/24] add at least one child in the toc --- internal/epub/image_filters.go | 2 +- internal/epub/toc.go | 11 ++++++++--- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/internal/epub/image_filters.go b/internal/epub/image_filters.go index 0eaa039..2714565 100644 --- a/internal/epub/image_filters.go +++ b/internal/epub/image_filters.go @@ -20,8 +20,8 @@ func NewGift(options *ImageOptions) *gift.GIFT { } g.Add( filters.Resize(options.ViewWidth, options.ViewHeight, gift.LanczosResampling), - filters.Pixel(), filters.Position(options.ViewWidth, options.ViewHeight, filters.PositionCenter), + filters.Pixel(), ) return g } diff --git a/internal/epub/toc.go b/internal/epub/toc.go index d354457..7c625b3 100644 --- a/internal/epub/toc.go +++ b/internal/epub/toc.go @@ -44,9 +44,14 @@ func (e *ePub) getToc(title string, images []*Image) string { if len(ol.ChildElements()) == 1 && e.StripFirstDirectoryFromToc { ol = ol.ChildElements()[0] } - if len(ol.ChildElements()) > 0 { - nav.AddChild(ol) - } + + beginning := etree.NewElement("li") + beginningLink := beginning.CreateElement("a") + beginningLink.CreateAttr("href", images[0].TextPath()) + beginningLink.CreateText("Start of the book") + ol.InsertChildAt(0, beginning) + + nav.AddChild(ol) doc.Indent(2) r, _ := doc.WriteToString() From b27a826cd233c26ee10747f38e7775ced5455b2c Mon Sep 17 00:00:00 2001 From: celogeek <65178+celogeek@users.noreply.github.com> Date: Sat, 22 Apr 2023 10:28:53 +0200 Subject: [PATCH 11/24] improve positioning --- internal/epub/filters/position.go | 26 +++++++++++++++++++------- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/internal/epub/filters/position.go b/internal/epub/filters/position.go index 3c3d3b4..d36e212 100644 --- a/internal/epub/filters/position.go +++ b/internal/epub/filters/position.go @@ -24,20 +24,32 @@ type positionFilter struct { } func (p *positionFilter) Bounds(srcBounds image.Rectangle) image.Rectangle { - return image.Rect(0, 0, p.viewWidth, p.viewHeight) + 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 := (p.viewWidth-srcBounds.Dx())/2, (p.viewHeight-srcBounds.Dy())/2 - if p.align == PositionLeft { - left = 0 + 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 = p.viewWidth - srcBounds.Dx() + left = dst.Bounds().Dx() - srcBounds.Dx() } draw.Draw( @@ -45,8 +57,8 @@ func (p *positionFilter) Draw(dst draw.Image, src image.Image, options *gift.Opt image.Rect( left, top, - p.viewWidth, - p.viewHeight, + dst.Bounds().Dx(), + dst.Bounds().Dy(), ), src, srcBounds.Min, From d96e63bc50cc6a22c120c0d24aee264bf9b7b07a Mon Sep 17 00:00:00 2001 From: celogeek <65178+celogeek@users.noreply.github.com> Date: Sat, 22 Apr 2023 10:29:36 +0200 Subject: [PATCH 12/24] improve style --- internal/epub/core.go | 11 +++-------- internal/epub/templates/blank.xhtml.tmpl | 1 + internal/epub/templates/style.css.tmpl | 11 ++++++++++- internal/epub/templates/text.xhtml.tmpl | 3 ++- internal/epub/templates/title.xhtml.tmpl | 1 + 5 files changed, 17 insertions(+), 10 deletions(-) diff --git a/internal/epub/core.go b/internal/epub/core.go index 5a04503..b0f4c5b 100644 --- a/internal/epub/core.go +++ b/internal/epub/core.go @@ -97,13 +97,8 @@ 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), - "ImageStyle": fmt.Sprintf( - "width:%dpx; height:%dpx;", - img.Width, - img.Height, - ), + "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(), }), ) @@ -120,7 +115,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), }), ) } diff --git a/internal/epub/templates/blank.xhtml.tmpl b/internal/epub/templates/blank.xhtml.tmpl index e3ba233..0722dfc 100644 --- a/internal/epub/templates/blank.xhtml.tmpl +++ b/internal/epub/templates/blank.xhtml.tmpl @@ -2,6 +2,7 @@ + {{ .Title }} diff --git a/internal/epub/templates/style.css.tmpl b/internal/epub/templates/style.css.tmpl index 2cac722..a8d41d9 100644 --- a/internal/epub/templates/style.css.tmpl +++ b/internal/epub/templates/style.css.tmpl @@ -1,13 +1,22 @@ body { color: #000; background: #FFF; + top: 0; + left: 0; margin: 0; padding: 0; width: {{ .PageWidth }}px; height: {{ .PageHeight }}px; + text-align: center; } -div { +img { + position: absolute; 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/text.xhtml.tmpl b/internal/epub/templates/text.xhtml.tmpl index 5741e3d..6a509d8 100644 --- a/internal/epub/templates/text.xhtml.tmpl +++ b/internal/epub/templates/text.xhtml.tmpl @@ -2,13 +2,14 @@ + {{ .Title }}
- + {{ .Title }}
\ No newline at end of file diff --git a/internal/epub/templates/title.xhtml.tmpl b/internal/epub/templates/title.xhtml.tmpl index c152015..037886c 100644 --- a/internal/epub/templates/title.xhtml.tmpl +++ b/internal/epub/templates/title.xhtml.tmpl @@ -2,6 +2,7 @@ + Part {{ .Part }} From 51c04fecb0410eb539d251f2a07ffb77c9bccf38 Mon Sep 17 00:00:00 2001 From: celogeek <65178+celogeek@users.noreply.github.com> Date: Sat, 22 Apr 2023 10:53:15 +0200 Subject: [PATCH 13/24] display perfect dim --- internal/converter/options/core.go | 14 +++++++++++++- internal/converter/profiles/core.go | 4 ++-- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/internal/converter/options/core.go b/internal/converter/options/core.go index 2e372b9..522eea3 100644 --- a/internal/converter/options/core.go +++ b/internal/converter/options/core.go @@ -4,6 +4,7 @@ import ( "fmt" "os" "path/filepath" + "strings" "github.com/celogeek/go-comic-converter/v2/internal/converter/profiles" "gopkg.in/yaml.v3" @@ -111,7 +112,7 @@ func (o *Options) LoadDefault() error { } func (o *Options) ShowDefault() string { - var profileDesc string + var profileDesc, viewDesc string profile := o.GetProfile() if profile != nil { profileDesc = fmt.Sprintf( @@ -121,6 +122,13 @@ 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 { @@ -139,6 +147,8 @@ func (o *Options) ShowDefault() string { return fmt.Sprintf(` Profile : %s + ViewRatio : 1:%s + View : %s Quality : %d Crop : %v Brightness : %d @@ -152,6 +162,8 @@ 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, diff --git a/internal/converter/profiles/core.go b/internal/converter/profiles/core.go index e370196..49c68a0 100644 --- a/internal/converter/profiles/core.go +++ b/internal/converter/profiles/core.go @@ -12,11 +12,11 @@ type Profile struct { Height int } -const perfectRatio = 1.5 +const PerfectRatio = 1.5 func (p Profile) PerfectDim() (int, int) { width, height := float64(p.Width), float64(p.Height) - perfectWidth, perfectHeight := height/perfectRatio, width*perfectRatio + perfectWidth, perfectHeight := height/PerfectRatio, width*PerfectRatio if perfectWidth > width { perfectWidth = width } else { From 88eea07747ce1dcfbfa1b4b3f68a8132f230179f Mon Sep 17 00:00:00 2001 From: celogeek <65178+celogeek@users.noreply.github.com> Date: Sat, 22 Apr 2023 10:57:50 +0200 Subject: [PATCH 14/24] rename files --- internal/converter/{core.go => converter.go} | 0 .../{order.go => converter_order.go} | 0 .../options/{core.go => converter_options.go} | 0 .../{core.go => converter_profiles.go} | 0 internal/epub/{core.go => epub.go} | 0 internal/epub/{content.go => epub_content.go} | 0 .../{image_data.go => epub_image_data.go} | 0 ...image_filters.go => epub_image_filters.go} | 0 ...processing.go => epub_image_processing.go} | 0 .../epub/{progress.go => epub_progress.go} | 0 internal/epub/epub_templates.go | 21 +++++++++++++++++++ internal/epub/{toc.go => epub_toc.go} | 0 internal/epub/{tree.go => epub_tree.go} | 0 internal/epub/{zip.go => epub_zip.go} | 0 ...torotate.go => epub_filters_autorotate.go} | 0 .../filters/{crop.go => epub_filters_crop.go} | 0 .../{pixel.go => epub_filters_pixel.go} | 0 .../{position.go => epub_filters_position.go} | 0 .../{resize.go => epub_filters_resize.go} | 0 .../sortpath/{core.go => epub_sortpath.go} | 0 internal/epub/templates.go | 21 ------------------- ...mpl => epub_templates_applebooks.xml.tmpl} | 0 ...l.tmpl => epub_templates_blank.xhtml.tmpl} | 0 ...tmpl => epub_templates_container.xml.tmpl} | 0 ...css.tmpl => epub_templates_style.css.tmpl} | 0 ...ml.tmpl => epub_templates_text.xhtml.tmpl} | 0 ...l.tmpl => epub_templates_title.xhtml.tmpl} | 0 27 files changed, 21 insertions(+), 21 deletions(-) rename internal/converter/{core.go => converter.go} (100%) rename internal/converter/{order.go => converter_order.go} (100%) rename internal/converter/options/{core.go => converter_options.go} (100%) rename internal/converter/profiles/{core.go => converter_profiles.go} (100%) rename internal/epub/{core.go => epub.go} (100%) rename internal/epub/{content.go => epub_content.go} (100%) rename internal/epub/{image_data.go => epub_image_data.go} (100%) rename internal/epub/{image_filters.go => epub_image_filters.go} (100%) rename internal/epub/{image_processing.go => epub_image_processing.go} (100%) rename internal/epub/{progress.go => epub_progress.go} (100%) create mode 100644 internal/epub/epub_templates.go rename internal/epub/{toc.go => epub_toc.go} (100%) rename internal/epub/{tree.go => epub_tree.go} (100%) rename internal/epub/{zip.go => epub_zip.go} (100%) rename internal/epub/filters/{autorotate.go => epub_filters_autorotate.go} (100%) rename internal/epub/filters/{crop.go => epub_filters_crop.go} (100%) rename internal/epub/filters/{pixel.go => epub_filters_pixel.go} (100%) rename internal/epub/filters/{position.go => epub_filters_position.go} (100%) rename internal/epub/filters/{resize.go => epub_filters_resize.go} (100%) rename internal/epub/sortpath/{core.go => epub_sortpath.go} (100%) delete mode 100644 internal/epub/templates.go rename internal/epub/templates/{applebooks.xml.tmpl => epub_templates_applebooks.xml.tmpl} (100%) rename internal/epub/templates/{blank.xhtml.tmpl => epub_templates_blank.xhtml.tmpl} (100%) rename internal/epub/templates/{container.xml.tmpl => epub_templates_container.xml.tmpl} (100%) rename internal/epub/templates/{style.css.tmpl => epub_templates_style.css.tmpl} (100%) rename internal/epub/templates/{text.xhtml.tmpl => epub_templates_text.xhtml.tmpl} (100%) rename internal/epub/templates/{title.xhtml.tmpl => epub_templates_title.xhtml.tmpl} (100%) diff --git a/internal/converter/core.go b/internal/converter/converter.go similarity index 100% rename from internal/converter/core.go rename to internal/converter/converter.go diff --git a/internal/converter/order.go b/internal/converter/converter_order.go similarity index 100% rename from internal/converter/order.go rename to internal/converter/converter_order.go diff --git a/internal/converter/options/core.go b/internal/converter/options/converter_options.go similarity index 100% rename from internal/converter/options/core.go rename to internal/converter/options/converter_options.go diff --git a/internal/converter/profiles/core.go b/internal/converter/profiles/converter_profiles.go similarity index 100% rename from internal/converter/profiles/core.go rename to internal/converter/profiles/converter_profiles.go diff --git a/internal/epub/core.go b/internal/epub/epub.go similarity index 100% rename from internal/epub/core.go rename to internal/epub/epub.go diff --git a/internal/epub/content.go b/internal/epub/epub_content.go similarity index 100% rename from internal/epub/content.go rename to internal/epub/epub_content.go diff --git a/internal/epub/image_data.go b/internal/epub/epub_image_data.go similarity index 100% rename from internal/epub/image_data.go rename to internal/epub/epub_image_data.go diff --git a/internal/epub/image_filters.go b/internal/epub/epub_image_filters.go similarity index 100% rename from internal/epub/image_filters.go rename to internal/epub/epub_image_filters.go diff --git a/internal/epub/image_processing.go b/internal/epub/epub_image_processing.go similarity index 100% rename from internal/epub/image_processing.go rename to internal/epub/epub_image_processing.go diff --git a/internal/epub/progress.go b/internal/epub/epub_progress.go similarity index 100% rename from internal/epub/progress.go rename to internal/epub/epub_progress.go diff --git a/internal/epub/epub_templates.go b/internal/epub/epub_templates.go new file mode 100644 index 0000000..5c33487 --- /dev/null +++ b/internal/epub/epub_templates.go @@ -0,0 +1,21 @@ +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 diff --git a/internal/epub/toc.go b/internal/epub/epub_toc.go similarity index 100% rename from internal/epub/toc.go rename to internal/epub/epub_toc.go diff --git a/internal/epub/tree.go b/internal/epub/epub_tree.go similarity index 100% rename from internal/epub/tree.go rename to internal/epub/epub_tree.go diff --git a/internal/epub/zip.go b/internal/epub/epub_zip.go similarity index 100% rename from internal/epub/zip.go rename to internal/epub/epub_zip.go diff --git a/internal/epub/filters/autorotate.go b/internal/epub/filters/epub_filters_autorotate.go similarity index 100% rename from internal/epub/filters/autorotate.go rename to internal/epub/filters/epub_filters_autorotate.go diff --git a/internal/epub/filters/crop.go b/internal/epub/filters/epub_filters_crop.go similarity index 100% rename from internal/epub/filters/crop.go rename to internal/epub/filters/epub_filters_crop.go diff --git a/internal/epub/filters/pixel.go b/internal/epub/filters/epub_filters_pixel.go similarity index 100% rename from internal/epub/filters/pixel.go rename to internal/epub/filters/epub_filters_pixel.go diff --git a/internal/epub/filters/position.go b/internal/epub/filters/epub_filters_position.go similarity index 100% rename from internal/epub/filters/position.go rename to internal/epub/filters/epub_filters_position.go diff --git a/internal/epub/filters/resize.go b/internal/epub/filters/epub_filters_resize.go similarity index 100% rename from internal/epub/filters/resize.go rename to internal/epub/filters/epub_filters_resize.go diff --git a/internal/epub/sortpath/core.go b/internal/epub/sortpath/epub_sortpath.go similarity index 100% rename from internal/epub/sortpath/core.go rename to internal/epub/sortpath/epub_sortpath.go diff --git a/internal/epub/templates.go b/internal/epub/templates.go deleted file mode 100644 index e25212b..0000000 --- a/internal/epub/templates.go +++ /dev/null @@ -1,21 +0,0 @@ -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 diff --git a/internal/epub/templates/applebooks.xml.tmpl b/internal/epub/templates/epub_templates_applebooks.xml.tmpl similarity index 100% rename from internal/epub/templates/applebooks.xml.tmpl rename to internal/epub/templates/epub_templates_applebooks.xml.tmpl diff --git a/internal/epub/templates/blank.xhtml.tmpl b/internal/epub/templates/epub_templates_blank.xhtml.tmpl similarity index 100% rename from internal/epub/templates/blank.xhtml.tmpl rename to internal/epub/templates/epub_templates_blank.xhtml.tmpl diff --git a/internal/epub/templates/container.xml.tmpl b/internal/epub/templates/epub_templates_container.xml.tmpl similarity index 100% rename from internal/epub/templates/container.xml.tmpl rename to internal/epub/templates/epub_templates_container.xml.tmpl diff --git a/internal/epub/templates/style.css.tmpl b/internal/epub/templates/epub_templates_style.css.tmpl similarity index 100% rename from internal/epub/templates/style.css.tmpl rename to internal/epub/templates/epub_templates_style.css.tmpl diff --git a/internal/epub/templates/text.xhtml.tmpl b/internal/epub/templates/epub_templates_text.xhtml.tmpl similarity index 100% rename from internal/epub/templates/text.xhtml.tmpl rename to internal/epub/templates/epub_templates_text.xhtml.tmpl diff --git a/internal/epub/templates/title.xhtml.tmpl b/internal/epub/templates/epub_templates_title.xhtml.tmpl similarity index 100% rename from internal/epub/templates/title.xhtml.tmpl rename to internal/epub/templates/epub_templates_title.xhtml.tmpl From 418d51662c56b7a741fce564ff2a537526e5e61b Mon Sep 17 00:00:00 2001 From: celogeek <65178+celogeek@users.noreply.github.com> Date: Sat, 22 Apr 2023 12:14:49 +0200 Subject: [PATCH 15/24] positioning image instead of creating view size image --- internal/epub/epub.go | 7 +- internal/epub/epub_image_filters.go | 9 +-- internal/epub/epub_image_processing.go | 30 +++++++++ .../epub/filters/epub_filters_position.go | 67 ------------------- .../templates/epub_templates_style.css.tmpl | 4 -- .../templates/epub_templates_text.xhtml.tmpl | 2 +- 6 files changed, 36 insertions(+), 83 deletions(-) delete mode 100644 internal/epub/filters/epub_filters_position.go 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 @@
- {{ .Title }} + {{ .Title }}
\ No newline at end of file From a3851475d243c44a6107541c0f934ece8e11c73b Mon Sep 17 00:00:00 2001 From: celogeek <65178+celogeek@users.noreply.github.com> Date: Sat, 22 Apr 2023 15:26:03 +0200 Subject: [PATCH 16/24] fix duplicate cover --- internal/epub/epub_content.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/epub/epub_content.go b/internal/epub/epub_content.go index d5b7b18..6fce0d5 100644 --- a/internal/epub/epub_content.go +++ b/internal/epub/epub_content.go @@ -77,7 +77,7 @@ func (e *ePub) getManifest(title string, part *epubPart, currentPart, totalPart {"item", TagAttrs{"id": "css", "href": "Text/style.css", "media-type": "text/css"}, ""}, } - if part.Cover != nil { + if e.HasCover || currentPart > 1 { items = append(items, iTag(part.Cover), hTag(part.Cover)) } From 9cf733b55361e79d1c7db849557f6e796b66e811 Mon Sep 17 00:00:00 2001 From: celogeek <65178+celogeek@users.noreply.github.com> Date: Sat, 22 Apr 2023 15:42:39 +0200 Subject: [PATCH 17/24] add quiet mode --- internal/converter/converter.go | 7 ++++--- .../converter/options/converter_options.go | 21 ++++++++++--------- internal/epub/epub.go | 3 ++- internal/epub/epub_image_processing.go | 2 +- internal/epub/epub_progress.go | 5 ++++- main.go | 5 +++-- 6 files changed, 25 insertions(+), 18 deletions(-) diff --git a/internal/converter/converter.go b/internal/converter/converter.go index 1e758d0..c6262d8 100644 --- a/internal/converter/converter.go +++ b/internal/converter/converter.go @@ -79,9 +79,6 @@ func (c *Converter) InitParse() { c.AddStringParam(&c.Options.Output, "output", "", "Output of the epub (directory or epub): (default [INPUT].epub)") c.AddStringParam(&c.Options.Author, "author", "GO Comic Converter", "Author of the epub") c.AddStringParam(&c.Options.Title, "title", "", "Title of the epub") - c.AddIntParam(&c.Options.Workers, "workers", runtime.NumCPU(), "Number of workers") - c.AddBoolParam(&c.Options.Dry, "dry", false, "Dry run to show all options") - c.AddBoolParam(&c.Options.DryVerbose, "dry-verbose", false, "Display also sorted files after the TOC") c.AddSection("Config") c.AddStringParam(&c.Options.Profile, "profile", c.Options.Profile, fmt.Sprintf("Profile to use: \n%s", c.Options.AvailableProfiles())) @@ -105,6 +102,10 @@ func (c *Converter) InitParse() { c.AddBoolParam(&c.Options.Reset, "reset", false, "Reset your parameters to default") c.AddSection("Other") + c.AddIntParam(&c.Options.Workers, "workers", runtime.NumCPU(), "Number of workers") + c.AddBoolParam(&c.Options.Dry, "dry", false, "Dry run to show all options") + c.AddBoolParam(&c.Options.DryVerbose, "dry-verbose", false, "Display also sorted files after the TOC") + c.AddBoolParam(&c.Options.Quiet, "quiet", false, "Disable progress bar") c.AddBoolParam(&c.Options.Version, "version", false, "Show current and available version") c.AddBoolParam(&c.Options.Help, "help", false, "Show this help message") } diff --git a/internal/converter/options/converter_options.go b/internal/converter/options/converter_options.go index 522eea3..782b6b6 100644 --- a/internal/converter/options/converter_options.go +++ b/internal/converter/options/converter_options.go @@ -12,14 +12,10 @@ import ( type Options struct { // Output - Input string `yaml:"-"` - Output string `yaml:"-"` - Author string `yaml:"-"` - Title string `yaml:"-"` - Auto bool `yaml:"-"` - Workers int `yaml:"-"` - Dry bool `yaml:"-"` - DryVerbose bool `yaml:"-"` + Input string `yaml:"-"` + Output string `yaml:"-"` + Author string `yaml:"-"` + Title string `yaml:"-"` // Config Profile string `yaml:"profile"` @@ -27,6 +23,7 @@ type Options struct { Crop bool `yaml:"crop"` Brightness int `yaml:"brightness"` Contrast int `yaml:"contrast"` + Auto bool `yaml:"-"` AutoRotate bool `yaml:"auto_rotate"` AutoSplitDoublePage bool `yaml:"auto_split_double_page"` NoBlankPage bool `yaml:"no_blank_page"` @@ -42,8 +39,12 @@ type Options struct { Reset bool `yaml:"-"` // Other - Version bool `yaml:"-"` - Help bool `yaml:"-"` + Workers int `yaml:"-"` + Dry bool `yaml:"-"` + DryVerbose bool `yaml:"-"` + Quiet bool `yaml:"-"` + Version bool `yaml:"-"` + Help bool `yaml:"-"` // Internal profiles profiles.Profiles diff --git a/internal/epub/epub.go b/internal/epub/epub.go index 8063577..a1ba9fa 100644 --- a/internal/epub/epub.go +++ b/internal/epub/epub.go @@ -39,6 +39,7 @@ type EpubOptions struct { Dry bool DryVerbose bool SortPathMode int + Quiet bool *ImageOptions } @@ -211,7 +212,7 @@ func (e *ePub) Write() error { totalParts := len(epubParts) - bar := NewBar(totalParts, "Writing Part", 2, 2) + bar := NewBar(e.Quiet, totalParts, "Writing Part", 2, 2) for i, part := range epubParts { ext := filepath.Ext(e.Output) suffix := "" diff --git a/internal/epub/epub_image_processing.go b/internal/epub/epub_image_processing.go index 0dc4360..580d04b 100644 --- a/internal/epub/epub_image_processing.go +++ b/internal/epub/epub_image_processing.go @@ -198,7 +198,7 @@ func (e *ePub) LoadImages() ([]*Image, error) { imageOutput := make(chan *Image) // processing - bar := NewBar(imageCount, "Processing", 1, 2) + bar := NewBar(e.Quiet, imageCount, "Processing", 1, 2) wg := &sync.WaitGroup{} for i := 0; i < e.ImageOptions.Workers; i++ { diff --git a/internal/epub/epub_progress.go b/internal/epub/epub_progress.go index f188d90..d9d1790 100644 --- a/internal/epub/epub_progress.go +++ b/internal/epub/epub_progress.go @@ -7,7 +7,10 @@ import ( "github.com/schollz/progressbar/v3" ) -func NewBar(max int, description string, currentJob, totalJob int) *progressbar.ProgressBar { +func NewBar(quiet bool, max int, description string, currentJob, totalJob int) *progressbar.ProgressBar { + if quiet { + return progressbar.DefaultSilent(int64(max)) + } fmtJob := fmt.Sprintf("%%0%dd", len(fmt.Sprint(totalJob))) fmtDesc := fmt.Sprintf("[%s/%s] %%-15s", fmtJob, fmtJob) return progressbar.NewOptions(max, diff --git a/main.go b/main.go index e2cb6b0..7916dd1 100644 --- a/main.go +++ b/main.go @@ -100,8 +100,6 @@ $ go install github.com/celogeek/go-comic-converter/v%d@%s Title: cmd.Options.Title, Author: cmd.Options.Author, StripFirstDirectoryFromToc: cmd.Options.StripFirstDirectoryFromToc, - Dry: cmd.Options.Dry, - DryVerbose: cmd.Options.DryVerbose, SortPathMode: cmd.Options.SortPathMode, ImageOptions: &epub.ImageOptions{ ViewWidth: perfectWidth, @@ -117,6 +115,9 @@ $ go install github.com/celogeek/go-comic-converter/v%d@%s HasCover: cmd.Options.HasCover, Workers: cmd.Options.Workers, }, + Dry: cmd.Options.Dry, + DryVerbose: cmd.Options.DryVerbose, + Quiet: cmd.Options.Quiet, }).Write(); err != nil { fmt.Fprintf(os.Stderr, "Error: %v\n", err) os.Exit(1) From e79cd346c4165320e37ab6e165ff51a8511c5c25 Mon Sep 17 00:00:00 2001 From: celogeek <65178+celogeek@users.noreply.github.com> Date: Sat, 22 Apr 2023 15:52:37 +0200 Subject: [PATCH 18/24] fix toc --- internal/epub/epub_toc.go | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/internal/epub/epub_toc.go b/internal/epub/epub_toc.go index 7c625b3..0824ca3 100644 --- a/internal/epub/epub_toc.go +++ b/internal/epub/epub_toc.go @@ -37,12 +37,18 @@ func (e *ePub) getToc(title string, images []*Image) string { link := t.CreateElement("a") link.CreateAttr("href", img.TextPath()) link.CreateText(path) - paths[currentPath] = t + paths[currentPath] = t.CreateElement("ol") } } if len(ol.ChildElements()) == 1 && e.StripFirstDirectoryFromToc { - ol = ol.ChildElements()[0] + ol = ol.FindElement("/li/ol") + } + + for _, v := range ol.FindElements("//ol") { + if len(v.ChildElements()) == 0 { + v.Parent().RemoveChild(v) + } } beginning := etree.NewElement("li") From 5960c569386e57b487ddc1b797d23db422643927 Mon Sep 17 00:00:00 2001 From: celogeek <65178+celogeek@users.noreply.github.com> Date: Sat, 22 Apr 2023 16:34:18 +0200 Subject: [PATCH 19/24] create title page --- internal/epub/epub.go | 16 ++++++++++++---- internal/epub/epub_content.go | 7 ++++++- internal/epub/epub_image_data.go | 4 ++++ internal/epub/epub_image_processing.go | 19 ++++++++++--------- internal/epub/epub_templates.go | 3 --- internal/epub/epub_toc.go | 4 ++-- .../templates/epub_templates_title.xhtml.tmpl | 16 ---------------- 7 files changed, 34 insertions(+), 35 deletions(-) delete mode 100644 internal/epub/templates/epub_templates_title.xhtml.tmpl diff --git a/internal/epub/epub.go b/internal/epub/epub.go index a1ba9fa..00cb6eb 100644 --- a/internal/epub/epub.go +++ b/internal/epub/epub.go @@ -122,6 +122,10 @@ func (e *ePub) writeBlank(wz *epubZip, img *Image) error { ) } +func (e ePub) getTitleImageData(img *Image, currentPart, totalPart int) *ImageData { + return newData("OEBPS/Images/title.jpg", img.Raw, e.Quality) +} + func (e *ePub) getParts() ([]*epubPart, error) { images, err := e.LoadImages() @@ -243,10 +247,11 @@ func (e *ePub) Write() error { "PageWidth": e.ViewWidth, "PageHeight": e.ViewHeight, })}, - {"OEBPS/Text/title.xhtml", e.render(titleTmpl, map[string]any{ - "Info": e, - "Part": i + 1, - "Total": totalParts, + {"OEBPS/Text/title.xhtml", e.render(textTmpl, map[string]any{ + "Title": title, + "ViewPort": fmt.Sprintf("width=%d,height=%d", e.ViewWidth, e.ViewHeight), + "ImagePath": "Images/title.jpg", + "ImageStyle": part.Cover.ImgStyle(e.ViewWidth, e.ViewHeight, e.Manga), })}, } @@ -258,6 +263,9 @@ func (e *ePub) Write() error { return err } } + if err := wz.WriteImage(e.getTitleImageData(part.Cover, i+1, totalParts)); err != nil { + return err + } // Cover exist or part > 1 // If no cover, part 2 and more will include the image as a cover diff --git a/internal/epub/epub_content.go b/internal/epub/epub_content.go index 6fce0d5..3c95291 100644 --- a/internal/epub/epub_content.go +++ b/internal/epub/epub_content.go @@ -75,6 +75,8 @@ func (e *ePub) getManifest(title string, part *epubPart, currentPart, totalPart items := []Tag{ {"item", TagAttrs{"id": "toc", "href": "toc.xhtml", "properties": "nav", "media-type": "application/xhtml+xml"}, ""}, {"item", TagAttrs{"id": "css", "href": "Text/style.css", "media-type": "text/css"}, ""}, + {"item", TagAttrs{"id": "page_title", "href": "Text/title.xhtml", "media-type": "application/xhtml+xml"}, ""}, + {"item", TagAttrs{"id": "img_title", "href": "Images/title.jpg", "media-type": "image/jpeg"}, ""}, } if e.HasCover || currentPart > 1 { @@ -93,7 +95,6 @@ func (e *ePub) getManifest(title string, part *epubPart, currentPart, totalPart } func (e *ePub) getSpine(title string, part *epubPart, currentPart, totalPart int) []Tag { - spine := []Tag{} isOnTheRight := !e.Manga getSpread := func(doublePageNoBlank bool) string { isOnTheRight = !isOnTheRight @@ -108,6 +109,10 @@ func (e *ePub) getSpine(title string, part *epubPart, currentPart, totalPart int return "rendition:page-spread-left" } } + + spine := []Tag{ + {"itemref", TagAttrs{"idref": "page_title", "properties": getSpread(false)}, ""}, + } for _, img := range part.Images { spine = append(spine, Tag{ "itemref", diff --git a/internal/epub/epub_image_data.go b/internal/epub/epub_image_data.go index ebd8538..bda346c 100644 --- a/internal/epub/epub_image_data.go +++ b/internal/epub/epub_image_data.go @@ -22,6 +22,10 @@ func (img *ImageData) CompressedSize() uint64 { func newImageData(id int, part int, img image.Image, quality int) *ImageData { name := fmt.Sprintf("OEBPS/Images/%d_p%d.jpg", id, part) + return newData(name, img, quality) +} + +func newData(name string, img image.Image, quality int) *ImageData { data := bytes.NewBuffer([]byte{}) if err := jpeg.Encode(data, img, &jpeg.Options{Quality: quality}); err != nil { panic(err) diff --git a/internal/epub/epub_image_processing.go b/internal/epub/epub_image_processing.go index 580d04b..d368735 100644 --- a/internal/epub/epub_image_processing.go +++ b/internal/epub/epub_image_processing.go @@ -28,6 +28,7 @@ import ( type Image struct { Id int Part int + Raw image.Image Data *ImageData Width int Height int @@ -180,15 +181,9 @@ func (e *ePub) LoadImages() ([]*Image, error) { for img := range imageInput { img.Reader.Close() images = append(images, &Image{ - Id: img.Id, - Part: 0, - Data: nil, - Width: 0, - Height: 0, - IsCover: false, - DoublePage: false, - Path: img.Path, - Name: img.Name, + Id: img.Id, + Path: img.Path, + Name: img.Name, }) } @@ -229,9 +224,15 @@ func (e *ePub) LoadImages() ([]*Image, error) { dst := image.NewGray(g.Bounds(src.Bounds())) g.Draw(dst, src) + var raw image.Image + if img.Id == 0 { + raw = dst + } + imageOutput <- &Image{ Id: img.Id, Part: 0, + Raw: raw, Data: newImageData(img.Id, 0, dst, e.ImageOptions.Quality), Width: dst.Bounds().Dx(), Height: dst.Bounds().Dy(), diff --git a/internal/epub/epub_templates.go b/internal/epub/epub_templates.go index 5c33487..e15e798 100644 --- a/internal/epub/epub_templates.go +++ b/internal/epub/epub_templates.go @@ -11,9 +11,6 @@ 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 diff --git a/internal/epub/epub_toc.go b/internal/epub/epub_toc.go index 0824ca3..ef79cfd 100644 --- a/internal/epub/epub_toc.go +++ b/internal/epub/epub_toc.go @@ -53,8 +53,8 @@ func (e *ePub) getToc(title string, images []*Image) string { beginning := etree.NewElement("li") beginningLink := beginning.CreateElement("a") - beginningLink.CreateAttr("href", images[0].TextPath()) - beginningLink.CreateText("Start of the book") + beginningLink.CreateAttr("href", "Text/title.xhtml") + beginningLink.CreateText(title) ol.InsertChildAt(0, beginning) nav.AddChild(ol) diff --git a/internal/epub/templates/epub_templates_title.xhtml.tmpl b/internal/epub/templates/epub_templates_title.xhtml.tmpl deleted file mode 100644 index 037886c..0000000 --- a/internal/epub/templates/epub_templates_title.xhtml.tmpl +++ /dev/null @@ -1,16 +0,0 @@ - - - - - - Part {{ .Part }} - - - - -

{{ .Info.Title }}

-{{ if gt .Total 1 }} -

Part {{ .Part }} / {{ .Total }}

-{{ end }} - - \ No newline at end of file From 82aa66c363a0e213dfda6fdae4f830611fbb7c08 Mon Sep 17 00:00:00 2001 From: celogeek <65178+celogeek@users.noreply.github.com> Date: Sat, 22 Apr 2023 16:53:45 +0200 Subject: [PATCH 20/24] include title for epubpart --- internal/epub/epub.go | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/internal/epub/epub.go b/internal/epub/epub.go index 00cb6eb..3c24aa0 100644 --- a/internal/epub/epub.go +++ b/internal/epub/epub.go @@ -160,8 +160,11 @@ func (e *ePub) getParts() ([]*epubPart, error) { maxSize := uint64(e.LimitMb * 1024 * 1024) xhtmlSize := uint64(1024) - // descriptor files + image + // descriptor files + title baseSize := uint64(16*1024) + cover.Data.CompressedSize() + if e.HasCover { + baseSize += cover.Data.CompressedSize() + } currentSize := baseSize currentImages := make([]*Image, 0) @@ -176,6 +179,9 @@ func (e *ePub) getParts() ([]*epubPart, error) { }) part += 1 currentSize = baseSize + if !e.HasCover { + currentSize += cover.Data.CompressedSize() + } currentImages = make([]*Image, 0) } currentSize += imgSize From 9c93b4f1d4650ec2716aeedd1801c64a98eb43ce Mon Sep 17 00:00:00 2001 From: celogeek <65178+celogeek@users.noreply.github.com> Date: Sat, 22 Apr 2023 19:07:53 +0200 Subject: [PATCH 21/24] create title page --- go.mod | 1 + go.sum | 2 ++ internal/epub/epub.go | 69 +++++++++++++++++++++++++++++++++++++++++-- 3 files changed, 69 insertions(+), 3 deletions(-) diff --git a/go.mod b/go.mod index 5ccb349..7e6174f 100644 --- a/go.mod +++ b/go.mod @@ -6,6 +6,7 @@ require ( github.com/beevik/etree v1.1.0 github.com/disintegration/gift v1.2.1 github.com/gofrs/uuid v4.4.0+incompatible + github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 github.com/nwaples/rardecode v1.1.3 github.com/raff/pdfreader v0.0.0-20220308062436-033e8ac577f0 github.com/schollz/progressbar/v3 v3.13.1 diff --git a/go.sum b/go.sum index 189f181..54216f9 100644 --- a/go.sum +++ b/go.sum @@ -7,6 +7,8 @@ github.com/disintegration/gift v1.2.1 h1:Y005a1X4Z7Uc+0gLpSAsKhWi4qLtsdEcMIbbdvd github.com/disintegration/gift v1.2.1/go.mod h1:Jh2i7f7Q2BM7Ezno3PhfezbR1xpUg9dUg3/RlKGr4HI= github.com/gofrs/uuid v4.4.0+incompatible h1:3qXRTX8/NbyulANqlc0lchS1gqAVxRgsuW1YrTJupqA= github.com/gofrs/uuid v4.4.0+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRxnplIgP/c0N/04lM= +github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 h1:DACJavvAHhabrF08vX0COfcOBJRhZ8lUbR+ZWIs0Y5g= +github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0/go.mod h1:E/TSTwGwJL78qG/PmXZO1EjYhfJinVAhrmmHX6Z8B9k= github.com/google/go-cmp v0.5.2 h1:X2ev0eStA3AbceY54o37/0PQ/UWqKEiiO2dKL5OPaFM= github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-github v17.0.0+incompatible h1:N0LgJ1j65A7kfXrZnUDaYCs/Sf4rEjNlfyDHW9dolSY= diff --git a/internal/epub/epub.go b/internal/epub/epub.go index 3c24aa0..10247a0 100644 --- a/internal/epub/epub.go +++ b/internal/epub/epub.go @@ -2,6 +2,8 @@ package epub import ( "fmt" + "image" + "image/draw" "os" "path/filepath" "regexp" @@ -10,7 +12,12 @@ import ( "text/template" "time" + "github.com/disintegration/gift" "github.com/gofrs/uuid" + "github.com/golang/freetype" + "github.com/golang/freetype/truetype" + "golang.org/x/image/font" + "golang.org/x/image/font/gofont/gomonobold" ) type ImageOptions struct { @@ -122,8 +129,64 @@ func (e *ePub) writeBlank(wz *epubZip, img *Image) error { ) } -func (e ePub) getTitleImageData(img *Image, currentPart, totalPart int) *ImageData { - return newData("OEBPS/Images/title.jpg", img.Raw, e.Quality) +func (e ePub) getTitleImageData(title string, img *Image, currentPart, totalPart int) *ImageData { + // Create a blur version of the cover + g := gift.New(gift.GaussianBlur(8)) + dst := image.NewGray(g.Bounds(img.Raw.Bounds())) + g.Draw(dst, img.Raw) + + // Calculate size of title + f, _ := truetype.Parse(gomonobold.TTF) + borderSize := 4 + var fontSize, textWidth, textHeight int + for fontSize = 64; fontSize >= 12; fontSize -= 1 { + face := truetype.NewFace(f, &truetype.Options{Size: float64(fontSize), DPI: 72}) + textWidth = font.MeasureString(face, title).Ceil() + textHeight = face.Metrics().Ascent.Ceil() + face.Metrics().Descent.Ceil() + if textWidth+2*borderSize < img.Width && 3*textHeight+2*borderSize < img.Height { + break + } + } + + // Draw rectangle in the middle of the image + textPosStart := img.Height/2 - textHeight/2 + textPosEnd := img.Height/2 + textHeight/2 + marginSize := fontSize + borderArea := image.Rect(0, textPosStart-borderSize-marginSize, img.Width, textPosEnd+borderSize+marginSize) + textArea := image.Rect(borderSize, textPosStart-marginSize, img.Width-borderSize, textPosEnd+marginSize) + + draw.Draw( + dst, + borderArea, + image.Black, + image.Point{}, + draw.Over, + ) + + draw.Draw( + dst, + textArea, + image.White, + image.Point{}, + draw.Over, + ) + + // Draw text + c := freetype.NewContext() + c.SetDPI(72) + c.SetFontSize(float64(fontSize)) + c.SetFont(f) + c.SetClip(textArea) + c.SetDst(dst) + c.SetSrc(image.Black) + + textLeft := img.Width/2 - textWidth/2 + if textLeft < borderSize { + textLeft = borderSize + } + c.DrawString(title, freetype.Pt(textLeft, img.Height/2+textHeight/4)) + + return newData("OEBPS/Images/title.jpg", dst, e.Quality) } func (e *ePub) getParts() ([]*epubPart, error) { @@ -269,7 +332,7 @@ func (e *ePub) Write() error { return err } } - if err := wz.WriteImage(e.getTitleImageData(part.Cover, i+1, totalParts)); err != nil { + if err := wz.WriteImage(e.getTitleImageData(title, part.Cover, i+1, totalParts)); err != nil { return err } From 0014ba7d866fb0551a58ed8de2ba6a6858f4c6ab Mon Sep 17 00:00:00 2001 From: celogeek <65178+celogeek@users.noreply.github.com> Date: Sat, 22 Apr 2023 19:13:50 +0200 Subject: [PATCH 22/24] put title alone on the left always --- internal/epub/epub_content.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/epub/epub_content.go b/internal/epub/epub_content.go index 3c95291..0afb09d 100644 --- a/internal/epub/epub_content.go +++ b/internal/epub/epub_content.go @@ -95,7 +95,7 @@ func (e *ePub) getManifest(title string, part *epubPart, currentPart, totalPart } func (e *ePub) getSpine(title string, part *epubPart, currentPart, totalPart int) []Tag { - isOnTheRight := !e.Manga + isOnTheRight := true getSpread := func(doublePageNoBlank bool) string { isOnTheRight = !isOnTheRight if doublePageNoBlank { From aae62f62c31f6f9c04a5899d6d1963563ffee101 Mon Sep 17 00:00:00 2001 From: celogeek <65178+celogeek@users.noreply.github.com> Date: Sat, 22 Apr 2023 21:11:30 +0200 Subject: [PATCH 23/24] center title --- internal/epub/epub_content.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/internal/epub/epub_content.go b/internal/epub/epub_content.go index 0afb09d..ab7e754 100644 --- a/internal/epub/epub_content.go +++ b/internal/epub/epub_content.go @@ -95,7 +95,7 @@ func (e *ePub) getManifest(title string, part *epubPart, currentPart, totalPart } func (e *ePub) getSpine(title string, part *epubPart, currentPart, totalPart int) []Tag { - isOnTheRight := true + isOnTheRight := !e.Manga getSpread := func(doublePageNoBlank bool) string { isOnTheRight = !isOnTheRight if doublePageNoBlank { @@ -111,7 +111,7 @@ func (e *ePub) getSpine(title string, part *epubPart, currentPart, totalPart int } spine := []Tag{ - {"itemref", TagAttrs{"idref": "page_title", "properties": getSpread(false)}, ""}, + {"itemref", TagAttrs{"idref": "page_title", "properties": getSpread(true)}, ""}, } for _, img := range part.Images { spine = append(spine, Tag{ From 42c58c733b3e999bcfdff0ca215abe8342c52c65 Mon Sep 17 00:00:00 2001 From: celogeek <65178+celogeek@users.noreply.github.com> Date: Sat, 22 Apr 2023 21:16:51 +0200 Subject: [PATCH 24/24] move create title --- internal/epub/epub.go | 69 +------------------------- internal/epub/epub_image_processing.go | 65 ++++++++++++++++++++++++ 2 files changed, 66 insertions(+), 68 deletions(-) diff --git a/internal/epub/epub.go b/internal/epub/epub.go index 10247a0..7c8c0a7 100644 --- a/internal/epub/epub.go +++ b/internal/epub/epub.go @@ -2,8 +2,6 @@ package epub import ( "fmt" - "image" - "image/draw" "os" "path/filepath" "regexp" @@ -12,12 +10,7 @@ import ( "text/template" "time" - "github.com/disintegration/gift" "github.com/gofrs/uuid" - "github.com/golang/freetype" - "github.com/golang/freetype/truetype" - "golang.org/x/image/font" - "golang.org/x/image/font/gofont/gomonobold" ) type ImageOptions struct { @@ -129,66 +122,6 @@ func (e *ePub) writeBlank(wz *epubZip, img *Image) error { ) } -func (e ePub) getTitleImageData(title string, img *Image, currentPart, totalPart int) *ImageData { - // Create a blur version of the cover - g := gift.New(gift.GaussianBlur(8)) - dst := image.NewGray(g.Bounds(img.Raw.Bounds())) - g.Draw(dst, img.Raw) - - // Calculate size of title - f, _ := truetype.Parse(gomonobold.TTF) - borderSize := 4 - var fontSize, textWidth, textHeight int - for fontSize = 64; fontSize >= 12; fontSize -= 1 { - face := truetype.NewFace(f, &truetype.Options{Size: float64(fontSize), DPI: 72}) - textWidth = font.MeasureString(face, title).Ceil() - textHeight = face.Metrics().Ascent.Ceil() + face.Metrics().Descent.Ceil() - if textWidth+2*borderSize < img.Width && 3*textHeight+2*borderSize < img.Height { - break - } - } - - // Draw rectangle in the middle of the image - textPosStart := img.Height/2 - textHeight/2 - textPosEnd := img.Height/2 + textHeight/2 - marginSize := fontSize - borderArea := image.Rect(0, textPosStart-borderSize-marginSize, img.Width, textPosEnd+borderSize+marginSize) - textArea := image.Rect(borderSize, textPosStart-marginSize, img.Width-borderSize, textPosEnd+marginSize) - - draw.Draw( - dst, - borderArea, - image.Black, - image.Point{}, - draw.Over, - ) - - draw.Draw( - dst, - textArea, - image.White, - image.Point{}, - draw.Over, - ) - - // Draw text - c := freetype.NewContext() - c.SetDPI(72) - c.SetFontSize(float64(fontSize)) - c.SetFont(f) - c.SetClip(textArea) - c.SetDst(dst) - c.SetSrc(image.Black) - - textLeft := img.Width/2 - textWidth/2 - if textLeft < borderSize { - textLeft = borderSize - } - c.DrawString(title, freetype.Pt(textLeft, img.Height/2+textHeight/4)) - - return newData("OEBPS/Images/title.jpg", dst, e.Quality) -} - func (e *ePub) getParts() ([]*epubPart, error) { images, err := e.LoadImages() @@ -332,7 +265,7 @@ func (e *ePub) Write() error { return err } } - if err := wz.WriteImage(e.getTitleImageData(title, part.Cover, i+1, totalParts)); err != nil { + if err := wz.WriteImage(e.createTitleImageDate(title, part.Cover, i+1, totalParts)); err != nil { return err } diff --git a/internal/epub/epub_image_processing.go b/internal/epub/epub_image_processing.go index d368735..f77b017 100644 --- a/internal/epub/epub_image_processing.go +++ b/internal/epub/epub_image_processing.go @@ -6,6 +6,7 @@ import ( "fmt" "image" "image/color" + "image/draw" _ "image/jpeg" _ "image/png" "io" @@ -18,9 +19,13 @@ import ( "github.com/celogeek/go-comic-converter/v2/internal/epub/sortpath" "github.com/disintegration/gift" + "github.com/golang/freetype" + "github.com/golang/freetype/truetype" "github.com/nwaples/rardecode" pdfimage "github.com/raff/pdfreader/image" "github.com/raff/pdfreader/pdfread" + "golang.org/x/image/font" + "golang.org/x/image/font/gofont/gomonobold" "golang.org/x/image/tiff" _ "golang.org/x/image/webp" ) @@ -513,3 +518,63 @@ func loadPdf(input string) (int, chan *imageTask, error) { return nbPages, output, nil } + +func (e *ePub) createTitleImageDate(title string, img *Image, currentPart, totalPart int) *ImageData { + // Create a blur version of the cover + g := gift.New(gift.GaussianBlur(8)) + dst := image.NewGray(g.Bounds(img.Raw.Bounds())) + g.Draw(dst, img.Raw) + + // Calculate size of title + f, _ := truetype.Parse(gomonobold.TTF) + borderSize := 4 + var fontSize, textWidth, textHeight int + for fontSize = 64; fontSize >= 12; fontSize -= 1 { + face := truetype.NewFace(f, &truetype.Options{Size: float64(fontSize), DPI: 72}) + textWidth = font.MeasureString(face, title).Ceil() + textHeight = face.Metrics().Ascent.Ceil() + face.Metrics().Descent.Ceil() + if textWidth+2*borderSize < img.Width && 3*textHeight+2*borderSize < img.Height { + break + } + } + + // Draw rectangle in the middle of the image + textPosStart := img.Height/2 - textHeight/2 + textPosEnd := img.Height/2 + textHeight/2 + marginSize := fontSize + borderArea := image.Rect(0, textPosStart-borderSize-marginSize, img.Width, textPosEnd+borderSize+marginSize) + textArea := image.Rect(borderSize, textPosStart-marginSize, img.Width-borderSize, textPosEnd+marginSize) + + draw.Draw( + dst, + borderArea, + image.Black, + image.Point{}, + draw.Over, + ) + + draw.Draw( + dst, + textArea, + image.White, + image.Point{}, + draw.Over, + ) + + // Draw text + c := freetype.NewContext() + c.SetDPI(72) + c.SetFontSize(float64(fontSize)) + c.SetFont(f) + c.SetClip(textArea) + c.SetDst(dst) + c.SetSrc(image.Black) + + textLeft := img.Width/2 - textWidth/2 + if textLeft < borderSize { + textLeft = borderSize + } + c.DrawString(title, freetype.Pt(textLeft, img.Height/2+textHeight/4)) + + return newData("OEBPS/Images/title.jpg", dst, e.Quality) +}