support comic without cover

This commit is contained in:
Celogeek 2023-03-04 23:14:43 +01:00
parent ef8e88c56b
commit 6f74f959fd
Signed by: celogeek
GPG Key ID: E6B7BDCFC446233A
6 changed files with 30 additions and 14 deletions

View File

@ -96,6 +96,8 @@ Usage of go-comic-converter:
Manga mode (right to left)
-noblankpage
Remove blank pages
-nocover
Indicate if your comic doesn't have a cover. The first page will be used as a cover and include after the title.
-nocrop
Disable cropping
-output string

View File

@ -25,6 +25,7 @@ type ImageOptions struct {
AutoSplitDoublePage bool
NoBlankPage bool
Manga bool
HasCover bool
Workers int
}
@ -113,7 +114,9 @@ func (e *ePub) getParts() ([]*epubPart, error) {
parts := make([]*epubPart, 0)
cover := images[0]
if e.HasCover {
images = images[1:]
}
if e.LimitMb == 0 {
parts = append(parts, &epubPart{
Cover: cover,
@ -185,7 +188,7 @@ func (e *ePub) Write() error {
content := []zipContent{
{"META-INF/container.xml", containerTmpl},
{"OEBPS/content.opf", e.render(contentTmpl, map[string]any{"Info": e, "Images": part.Images})},
{"OEBPS/content.opf", e.render(contentTmpl, map[string]any{"Info": e, "Cover": part.Cover, "Images": part.Images})},
{"OEBPS/toc.ncx", e.render(tocTmpl, map[string]any{"Info": e})},
{"OEBPS/nav.xhtml", e.render(navTmpl, map[string]any{"Info": e})},
{"OEBPS/Text/style.css", styleTmpl},
@ -204,7 +207,12 @@ func (e *ePub) Write() error {
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)
}
for _, img := range part.Images {
if err := wz.WriteFile(

View File

@ -22,10 +22,6 @@ 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)
if id == 0 {
name = "OEBPS/Images/cover.jpg"
}
data := bytes.NewBuffer([]byte{})
if err := jpeg.Encode(data, img, &jpeg.Options{Quality: quality}); err != nil {
panic(err)

View File

@ -27,6 +27,7 @@ type Image struct {
Data *ImageData
Width int
Height int
IsCover bool
}
type imageTask struct {
@ -152,12 +153,13 @@ func LoadImages(path string, options *ImageOptions) ([]*Image, error) {
newImageData(img.Id, 0, dst, options.Quality),
dst.Bounds().Dx(),
dst.Bounds().Dy(),
img.Id == 0,
}
// Auto split double page
// Except for cover
// Only if the src image have width > height and is bigger than the view
if img.Id > 0 &&
if (!options.HasCover || img.Id > 0) &&
options.AutoSplitDoublePage &&
src.Bounds().Dx() > src.Bounds().Dy() &&
src.Bounds().Dx() > options.ViewHeight &&
@ -173,6 +175,7 @@ func LoadImages(path string, options *ImageOptions) ([]*Image, error) {
newImageData(img.Id, part, dst, options.Quality),
dst.Bounds().Dx(),
dst.Bounds().Dy(),
false,
}
}
}

View File

@ -23,12 +23,14 @@
<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="cover" href="Images/cover.jpg" media-type="image/jpeg" properties="cover-image"/>
<item id="cover" href="Images/{{ .Cover.Id }}_p{{ .Cover.Part }}.jpg" media-type="image/jpeg" properties="cover-image"/>
<item id="css" href="Text/style.css" media-type="text/css"/>
<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 .IsCover false }}
<item id="img_{{ .Id }}_p{{ .Part}}" href="Images/{{ .Id }}_p{{ .Part}}.jpg" media-type="image/jpeg"/>
{{ end }}
{{ if eq $info.NoBlankPage false }}
{{ if eq .Part 1 }}
<item id="page_{{ .Id }}_sp" href="Text/{{ .Id }}_sp.xhtml" media-type="application/xhtml+xml"/>

View File

@ -71,6 +71,7 @@ type Option struct {
AutoSplitDoublePage bool
NoBlankPage bool
Manga bool
NoCover bool
Workers int
LimitMb int
}
@ -106,6 +107,7 @@ Options:
AutoSplitDoublePage: %v
NoBlankPage : %v
Manga : %v
HasCover : %v
LimitMb : %s
Workers : %d
`,
@ -122,6 +124,7 @@ Options:
o.AutoSplitDoublePage,
o.NoBlankPage,
o.Manga,
!o.NoCover,
limitmb,
o.Workers,
)
@ -154,6 +157,7 @@ func main() {
flag.BoolVar(&opt.AutoSplitDoublePage, "autosplitdoublepage", false, "Auto Split double page when width > height")
flag.BoolVar(&opt.NoBlankPage, "noblankpage", false, "Remove blank pages")
flag.BoolVar(&opt.Manga, "manga", false, "Manga mode (right to left)")
flag.BoolVar(&opt.NoCover, "nocover", false, "Indicate if your comic doesn't have a cover. The first page will be used as a cover and include after the title.")
flag.IntVar(&opt.LimitMb, "limitmb", 0, "Limit size of the ePub: Default nolimit (0), Minimum 20")
flag.IntVar(&opt.Workers, "workers", runtime.NumCPU(), "Number of workers")
flag.Usage = func() {
@ -261,6 +265,7 @@ func main() {
AutoSplitDoublePage: opt.AutoSplitDoublePage,
NoBlankPage: opt.NoBlankPage,
Manga: opt.Manga,
HasCover: !opt.NoCover,
Workers: opt.Workers,
},
}).Write(); err != nil {