Merge pull request #10 from celogeek/fix-rendering

Fix rendering
This commit is contained in:
Celogeek 2023-04-22 21:19:44 +02:00 committed by GitHub
commit 7a4cc1cec5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
38 changed files with 630 additions and 594 deletions

View File

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

3
go.mod
View File

@ -3,8 +3,10 @@ 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/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
@ -20,6 +22,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

18
go.sum
View File

@ -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=
@ -5,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=
@ -31,15 +35,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 +54,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 +67,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 +80,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 +89,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=

View File

@ -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()))
@ -95,7 +92,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")
@ -106,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")
}

View File

@ -4,6 +4,7 @@ import (
"fmt"
"os"
"path/filepath"
"strings"
"github.com/celogeek/go-comic-converter/v2/internal/converter/profiles"
"gopkg.in/yaml.v3"
@ -11,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"`
@ -26,12 +23,12 @@ 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"`
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"`
@ -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
@ -61,7 +62,6 @@ func New() *Options {
NoBlankPage: false,
Manga: false,
HasCover: true,
AddPanelView: false,
LimitMb: 0,
StripFirstDirectoryFromToc: false,
SortPathMode: 1,
@ -113,7 +113,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(
@ -123,6 +123,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 {
@ -141,6 +148,8 @@ func (o *Options) ShowDefault() string {
return fmt.Sprintf(`
Profile : %s
ViewRatio : 1:%s
View : %s
Quality : %d
Crop : %v
Brightness : %d
@ -150,11 +159,12 @@ func (o *Options) ShowDefault() string {
NoBlankPage : %v
Manga : %v
HasCover : %v
AddPanelView : %v
LimitMb : %s
StripFirstDirectoryFromToc: %v
SortPathMode : %s`,
profileDesc,
strings.TrimRight(fmt.Sprintf("%f", profiles.PerfectRatio), "0"),
viewDesc,
o.Quality,
o.Crop,
o.Brightness,
@ -164,7 +174,6 @@ func (o *Options) ShowDefault() string {
o.NoBlankPage,
o.Manga,
o.HasCover,
o.AddPanelView,
limitmb,
o.StripFirstDirectoryFromToc,
sortpathmode,

View File

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

View File

@ -1,7 +1,6 @@
package epub
import (
"encoding/xml"
"fmt"
"os"
"path/filepath"
@ -27,7 +26,6 @@ type ImageOptions struct {
NoBlankPage bool
Manga bool
HasCover bool
AddPanelView bool
Workers int
}
@ -41,6 +39,7 @@ type EpubOptions struct {
Dry bool
DryVerbose bool
SortPathMode int
Quiet bool
*ImageOptions
}
@ -95,6 +94,34 @@ 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),
"ImagePath": img.ImgPath(),
"ImageStyle": img.ImgStyle(e.ViewWidth, e.ViewHeight, e.Manga),
}),
)
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()
@ -129,13 +156,15 @@ 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)
part := 1
imgIsOnRightSide := false
for _, img := range images {
imgSize := img.Data.CompressedSize() + xhtmlSize
@ -145,14 +174,14 @@ func (e *ePub) getParts() ([]*epubPart, error) {
Images: currentImages,
})
part += 1
imgIsOnRightSide = false
currentSize = baseSize
if !e.HasCover {
currentSize += cover.Data.CompressedSize()
}
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{
@ -164,59 +193,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
@ -242,7 +218,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 := ""
@ -264,42 +240,21 @@ 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,
{"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", e.render(styleTmpl, map[string]any{
"PageWidth": e.ViewWidth,
"PageHeight": e.ViewHeight,
})},
{"OEBPS/toc.ncx", e.render(tocTmpl, map[string]any{
"Info": e,
"Title": title,
{"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),
})},
{"OEBPS/nav.xhtml", e.render(navTmpl, map[string]any{
"Title": title,
"TOC": string(toc),
})},
{"OEBPS/Text/style.css", styleTmpl},
{"OEBPS/Text/part.xhtml", e.render(partTmpl, map[string]any{
"Info": e,
"Part": i + 1,
"Total": totalParts,
})},
}
if e.AddPanelView {
content = append(content, zipContent{"OEBPS/Text/panelview.css", panelViewTmpl})
}
if err = wz.WriteMagic(); err != nil {
@ -310,45 +265,29 @@ func (e *ePub) Write() error {
return err
}
}
if err := wz.WriteImage(e.createTitleImageDate(title, 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
if e.HasCover || i > 0 {
wz.WriteImage(part.Cover.Data)
if err := e.writeImage(wz, part.Cover); err != nil {
return err
}
}
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 := e.writeImage(wz, img); err != nil {
return err
}
if img.NeedSpace {
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 {
// Double Page or Last Image
if img.DoublePage || (i+1 == len(part.Images)) {
if err := e.writeBlank(wz, img); err != nil {
return err
}
}
if err := wz.WriteImage(img.Data); err != nil {
return err
}
}
bar.Add(1)
}

View File

@ -0,0 +1,201 @@
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": "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"}, ""},
{"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 {
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 {
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"
}
}
spine := []Tag{
{"itemref", TagAttrs{"idref": "page_title", "properties": getSpread(true)}, ""},
}
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/#")
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
}

View File

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

View File

@ -28,14 +28,12 @@ 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 {
@ -45,8 +43,9 @@ func NewGiftSplitDoublePage(options *ImageOptions) []*gift.GIFT {
if options.Brightness != 0 {
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),
)
}

View File

@ -6,6 +6,7 @@ import (
"fmt"
"image"
"image/color"
"image/draw"
_ "image/jpeg"
_ "image/png"
"io"
@ -18,23 +19,78 @@ 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"
)
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
Raw image.Image
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) 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)
}
type imageTask struct {
@ -130,15 +186,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,
NeedSpace: false, // NeedSpace reajust during parts computation
Path: img.Path,
Name: img.Name,
Id: img.Id,
Path: img.Path,
Name: img.Name,
})
}
@ -148,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++ {
@ -179,16 +229,24 @@ 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,
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,
Raw: raw,
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
@ -204,16 +262,17 @@ 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,
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,
}
}
}
@ -459,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)
}

View File

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

View File

@ -0,0 +1,18 @@
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_text.xhtml.tmpl"
var textTmpl string
//go:embed "templates/epub_templates_blank.xhtml.tmpl"
var blankTmpl string

65
internal/epub/epub_toc.go Normal file
View File

@ -0,0 +1,65 @@
package epub
import (
"path/filepath"
"strings"
"github.com/beevik/etree"
)
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")
html := doc.CreateElement("html")
html.CreateAttr("xmlns", "http://www.w3.org/1999/xhtml")
html.CreateAttr("xmlns:epub", "http://www.idpf.org/2007/ops")
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.CreateElement("ol")
}
}
if len(ol.ChildElements()) == 1 && e.StripFirstDirectoryFromToc {
ol = ol.FindElement("/li/ol")
}
for _, v := range ol.FindElements("//ol") {
if len(v.ChildElements()) == 0 {
v.Parent().RemoveChild(v)
}
}
beginning := etree.NewElement("li")
beginningLink := beginning.CreateElement("a")
beginningLink.CreateAttr("href", "Text/title.xhtml")
beginningLink.CreateText(title)
ol.InsertChildAt(0, beginning)
nav.AddChild(ol)
doc.Indent(2)
r, _ := doc.WriteToString()
return r
}

View File

@ -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("")
}

View File

@ -1,33 +0,0 @@
package epub
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/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

View File

@ -1,6 +0,0 @@
<?xml version="1.0"?>
<container version="1.0" xmlns="urn:oasis:names:tc:opendocument:xmlns:container">
<rootfiles>
<rootfile full-path="OEBPS/content.opf" media-type="application/oebps-package+xml"/>
</rootfiles>
</container>

View File

@ -1,57 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<package version="3.0" unique-identifier="BookID" xmlns="http://www.idpf.org/2007/opf">
{{ $info := .Info }}
<metadata xmlns:opf="http://www.idpf.org/2007/opf" xmlns:dc="http://purl.org/dc/elements/1.1/">
<dc:title>{{ .Title }}</dc:title>
<dc:language>en-US</dc:language>
<dc:identifier id="BookID">urn:uuid:{{ $info.UID }}</dc:identifier>
<dc:contributor id="contributor">{{ $info.Publisher }}</dc:contributor>
<dc:publisher>{{ $info.Publisher }}</dc:publisher>
<dc:date>{{ $info.UpdatedAt }}</dc:date>
<dc:creator>{{ $info.Author }}</dc:creator>
<meta property="dcterms:modified">{{ $info.UpdatedAt }}</meta>
<meta name="original-resolution" content="{{ $info.ViewWidth }}x{{ $info.ViewHeight }}"/>
<meta name="book-type" content="comic"/>
<meta name="primary-writing-mode" content="horizontal-{{ if $info.Manga }}rl{{ else }}lr{{ end }}"/>
<meta property="rendition:layout">pre-paginated</meta>
<meta property="rendition:orientation">portrait</meta>
<meta name="orientation-lock" content="portrait"/>
{{ if eq $info.AddPanelView true }}
<meta name="region-mag" content="true"/>
{{ end }}
{{ if gt .Total 1 }}
<meta name="calibre:series" content="{{ $info.Title }}"/>
<meta name="calibre:series_index" content="{{ .Part }}"/>
{{ end }}
</metadata>
<manifest>
<item id="ncx" href="toc.ncx" media-type="application/x-dtbncx+xml"/>
<item id="nav" href="nav.xhtml" properties="nav" media-type="application/xhtml+xml"/>
<item id="style_css" href="Text/style.css" media-type="text/css"/>
{{ if eq $info.AddPanelView true }}
<item id="panelview_css" href="Text/panelview.css" media-type="text/css"/>
{{ end }}
<item id="cover" href="Images/{{ .Cover.Id }}_p{{ .Cover.Part }}.jpg" media-type="image/jpeg" properties="cover-image"/>
{{ range .Images }}
{{ if eq .IsCover false }}
<item id="img_{{ .Id }}_p{{ .Part}}" href="Images/{{ .Id }}_p{{ .Part}}.jpg" media-type="image/jpeg"/>
{{ end }}
{{ end }}
<item id="page_part" href="Text/part.xhtml" media-type="application/xhtml+xml"/>
{{ range .Images }}
<item id="page_{{ .Id }}_p{{ .Part}}" href="Text/{{ .Id }}_p{{ .Part}}.xhtml" media-type="application/xhtml+xml"/>
{{ if eq .NeedSpace true }}
<item id="page_{{ .Id }}_sp" href="Text/{{ .Id }}_sp.xhtml" media-type="application/xhtml+xml"/>
{{ end }}
{{ end }}
</manifest>
<spine toc="ncx" page-progression-direction="{{ if $info.Manga }}rtl{{ else }}ltr{{ end }}">
<itemref idref="page_part" linear="yes"/>
{{ range .Images }}
{{ if eq .NeedSpace true }}
<itemref idref="page_{{ .Id }}_sp" linear="yes"/>
{{ end }}
<itemref idref="page_{{ .Id }}_p{{ .Part }}" linear="yes"/>
{{ end }}
</spine>
</package>

View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<display_options>
<platform name="*">
<option name="fixed-layout">true</option>
</platform>
</display_options>

View File

@ -2,11 +2,11 @@
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops">
<head>
<title>Page {{ .Image.Id }} Space</title>
<meta charset="utf-8" />
<title>{{ .Title }}</title>
<link href="style.css" type="text/css" rel="stylesheet"/>
<meta name="viewport" content="width={{ .Info.ViewWidth }}, height={{ .Info.ViewHeight }}"/>
<meta name="viewport" content="{{ .ViewPort }}"/>
</head>
<body>
<h1>{{ if .Info.Manga }}&#8592;{{ else }}&#8594;{{ end }}</h1>
</body>
</html>

View File

@ -0,0 +1,7 @@
<?xml version="1.0"?>
<container version="1.0"
xmlns="urn:oasis:names:tc:opendocument:xmlns:container">
<rootfiles>
<rootfile full-path="OEBPS/content.opf" media-type="application/oebps-package+xml"/>
</rootfiles>
</container>

View File

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

View File

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

View File

@ -1,16 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops">
<head>
<title>{{ .Title }}</title>
</head>
<body>
<nav epub:type="toc" id="toc">
<h2>Table of content</h2>
<ol>
<li><a href="Text/part.xhtml">{{ .Title }}</a></li>
{{ .TOC }}
</ol>
</nav>
</body>
</html>

View File

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

View File

@ -1,77 +0,0 @@
@charset "UTF-8";
html {
color: #000;
background: #FFF;
}
body {
font-size: 16px;
text-align: center;
width: 100%;
height: 100%;
}
body,
div,
dl,
dt,
dd,
ul,
ol,
li,
h1,
h2,
h3,
h4,
h5,
h6,
th,
td {
margin: 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 {
font-size: 150%;
font-weight: normal;
}
sup {
vertical-align: text-top;
}
sub {
vertical-align: text-bottom;
}

View File

@ -1,41 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops">
<head>
<title>Page {{ .Image.Id }}_p{{ .Image.Part}}</title>
<link href="style.css" type="text/css" rel="stylesheet"/>
<link href="panelview.css" type="text/css" rel="stylesheet"/>
<meta name="viewport" content="width={{ .Image.Width }}, height={{ .Image.Height }}"/>
</head>
<body>
<div>
<img style="width:{{ .Image.Width }}px; height:{{ .Image.Height }}px" src="../Images/{{ .Image.Id }}_p{{ .Image.Part}}.jpg"/>
</div>
<div id="PV">
<div id="PV-TL">
<a class="app-amzn-magnify" data-app-amzn-magnify='{"targetId":"PV-TL-P", "ordinal":{{ if .Manga }}2{{ else }}1{{ end }}}'></a>
</div>
<div id="PV-TR">
<a class="app-amzn-magnify" data-app-amzn-magnify='{"targetId":"PV-TR-P", "ordinal":{{ if .Manga }}1{{ else }}2{{ end }}}'></a>
</div>
<div id="PV-BL">
<a class="app-amzn-magnify" data-app-amzn-magnify='{"targetId":"PV-BL-P", "ordinal":{{ if .Manga }}4{{ else }}3{{ end }}}'></a>
</div>
<div id="PV-BR">
<a class="app-amzn-magnify" data-app-amzn-magnify='{"targetId":"PV-BR-P", "ordinal":{{ if .Manga }}3{{ else }}4{{ end }}}'></a>
</div>
</div>
<div class="PV-P" id="PV-TL-P">
<img src="../Images/{{ .Image.Id }}_p{{ .Image.Part}}.jpg" width="{{ zoom .Image.Width 1.5 }}" height="{{ zoom .Image.Height 1.5 }}"/>
</div>
<div class="PV-P" id="PV-TR-P">
<img src="../Images/{{ .Image.Id }}_p{{ .Image.Part}}.jpg" width="{{ zoom .Image.Width 1.5 }}" height="{{ zoom .Image.Height 1.5 }}"/>
</div>
<div class="PV-P" id="PV-BL-P">
<img src="../Images/{{ .Image.Id }}_p{{ .Image.Part}}.jpg" width="{{ zoom .Image.Width 1.5 }}" height="{{ zoom .Image.Height 1.5 }}"/>
</div>
<div class="PV-P" id="PV-BR-P">
<img src="../Images/{{ .Image.Id }}_p{{ .Image.Part}}.jpg" width="{{ zoom .Image.Width 1.5 }}" height="{{ zoom .Image.Height 1.5 }}"/>
</div>
</body>
</html>

View File

@ -1,14 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops">
<head>
<title>Page {{ .Image.Id }}_p{{ .Image.Part}}</title>
<link href="style.css" type="text/css" rel="stylesheet"/>
<meta name="viewport" content="width={{ .Image.Width }}, height={{ .Image.Height }}"/>
</head>
<body>
<div>
<img style="width:{{ .Image.Width }}px; height:{{ .Image.Height }}px" src="../Images/{{ .Image.Id }}_p{{ .Image.Part}}.jpg"/>
</div>
</body>
</html>

View File

@ -1,14 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<ncx version="2005-1" xml:lang="en-US" xmlns="http://www.daisy.org/z3986/2005/ncx/">
<head>
<meta name="dtb:uid" content="urn:uuid:{{ .Info.UID }}"/>
<meta name="dtb:depth" content="1"/>
<meta name="dtb:totalPageCount" content="0"/>
<meta name="dtb:maxPageNumber" content="0"/>
<meta name="generated" content="true"/>
</head>
<docTitle><text>{{ .Title }}</text></docTitle>
<navMap>
<navPoint id="Text"><navLabel><text>{{ .Title }}</text></navLabel><content src="Text/part.xhtml"/></navPoint>
</navMap>
</ncx>

View File

@ -1,22 +0,0 @@
package epub
import (
"encoding/xml"
)
type TocTitle struct {
XMLName xml.Name `xml:"a"`
Value string `xml:",innerxml"`
Link string `xml:"href,attr"`
}
type TocChildren struct {
XMLName xml.Name `xml:"ol"`
Tags []*TocPart
}
type TocPart struct {
XMLName xml.Name `xml:"li"`
Title TocTitle
Children *TocChildren `xml:",omitempty"`
}

12
main.go
View File

@ -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,
@ -98,12 +100,10 @@ $ 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: profile.Width,
ViewHeight: profile.Height,
ViewWidth: perfectWidth,
ViewHeight: perfectHeight,
Quality: cmd.Options.Quality,
Crop: cmd.Options.Crop,
Brightness: cmd.Options.Brightness,
@ -113,9 +113,11 @@ $ 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,
},
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)