Compare commits

..

No commits in common. "4e4245fd211f2f3c2b817c65248c3cdb147e12f3" and "1ad8f9ddd0073be8e7177c848f83be420041f21a" have entirely different histories.

19 changed files with 244 additions and 268 deletions

View File

@ -10,13 +10,27 @@ import (
"text/template"
"time"
epubimage "github.com/celogeek/go-comic-converter/v2/internal/epub/image"
epubtemplates "github.com/celogeek/go-comic-converter/v2/internal/epub/templates"
epubzip "github.com/celogeek/go-comic-converter/v2/internal/epub/zip"
"github.com/celogeek/go-comic-converter/v2/internal/epub/templates"
"github.com/gofrs/uuid"
)
type Options struct {
type ImageOptions struct {
Crop bool
ViewWidth int
ViewHeight int
Quality int
Algo string
Brightness int
Contrast int
AutoRotate bool
AutoSplitDoublePage bool
NoBlankPage bool
Manga bool
HasCover bool
Workers int
}
type EpubOptions struct {
Input string
Output string
Title string
@ -27,12 +41,12 @@ type Options struct {
DryVerbose bool
SortPathMode int
Quiet bool
Workers int
Image *epubimage.Options
*ImageOptions
}
type ePub struct {
*Options
*EpubOptions
UID string
Publisher string
UpdatedAt string
@ -41,11 +55,11 @@ type ePub struct {
}
type epubPart struct {
Cover *epubimage.Image
Images []*epubimage.Image
Cover *Image
Images []*Image
}
func New(options *Options) *ePub {
func NewEpub(options *EpubOptions) *ePub {
uid, err := uuid.NewV4()
if err != nil {
panic(err)
@ -58,7 +72,7 @@ func New(options *Options) *ePub {
})
return &ePub{
Options: options,
EpubOptions: options,
UID: uid.String(),
Publisher: "GO Comic Converter",
UpdatedAt: time.Now().UTC().Format("2006-01-02T15:04:05Z"),
@ -81,14 +95,14 @@ func (e *ePub) render(templateString string, data any) string {
return stripBlank.ReplaceAllString(result.String(), "\n")
}
func (e *ePub) writeImage(wz *epubzip.EpubZip, img *epubimage.Image) error {
func (e *ePub) writeImage(wz *epubZip, img *Image) error {
err := wz.WriteFile(
fmt.Sprintf("OEBPS/%s", img.TextPath()),
e.render(epubtemplates.Text, map[string]any{
e.render(templates.Text, map[string]any{
"Title": fmt.Sprintf("Image %d Part %d", img.Id, img.Part),
"ViewPort": fmt.Sprintf("width=%d,height=%d", e.Image.ViewWidth, e.Image.ViewHeight),
"ViewPort": fmt.Sprintf("width=%d,height=%d", e.ViewWidth, e.ViewHeight),
"ImagePath": img.ImgPath(),
"ImageStyle": img.ImgStyle(e.Image.ViewWidth, e.Image.ViewHeight, e.Image.Manga),
"ImageStyle": img.ImgStyle(e.ViewWidth, e.ViewHeight, e.Manga),
}),
)
@ -99,12 +113,12 @@ func (e *ePub) writeImage(wz *epubzip.EpubZip, img *epubimage.Image) error {
return err
}
func (e *ePub) writeBlank(wz *epubzip.EpubZip, img *epubimage.Image) error {
func (e *ePub) writeBlank(wz *epubZip, img *Image) error {
return wz.WriteFile(
fmt.Sprintf("OEBPS/Text/%d_sp.xhtml", img.Id),
e.render(epubtemplates.Blank, map[string]any{
e.render(templates.Blank, map[string]any{
"Title": fmt.Sprintf("Blank Page %d", img.Id),
"ViewPort": fmt.Sprintf("width=%d,height=%d", e.Image.ViewWidth, e.Image.ViewHeight),
"ViewPort": fmt.Sprintf("width=%d,height=%d", e.ViewWidth, e.ViewHeight),
}),
)
}
@ -128,7 +142,7 @@ func (e *ePub) getParts() ([]*epubPart, error) {
parts := make([]*epubPart, 0)
cover := images[0]
if e.Image.HasCover {
if e.HasCover {
images = images[1:]
}
@ -145,12 +159,12 @@ func (e *ePub) getParts() ([]*epubPart, error) {
xhtmlSize := uint64(1024)
// descriptor files + title
baseSize := uint64(16*1024) + cover.Data.CompressedSize()
if e.Image.HasCover {
if e.HasCover {
baseSize += cover.Data.CompressedSize()
}
currentSize := baseSize
currentImages := make([]*epubimage.Image, 0)
currentImages := make([]*Image, 0)
part := 1
for _, img := range images {
@ -162,10 +176,10 @@ func (e *ePub) getParts() ([]*epubPart, error) {
})
part += 1
currentSize = baseSize
if !e.Image.HasCover {
if !e.HasCover {
currentSize += cover.Data.CompressedSize()
}
currentImages = make([]*epubimage.Image, 0)
currentImages = make([]*Image, 0)
}
currentSize += imgSize
currentImages = append(currentImages, img)
@ -195,8 +209,8 @@ func (e *ePub) Write() error {
p := epubParts[0]
fmt.Fprintf(os.Stderr, "TOC:\n - %s\n%s\n", e.Title, e.getTree(p.Images, true))
if e.DryVerbose {
if e.Image.HasCover {
fmt.Fprintf(os.Stderr, "Cover:\n%s\n", e.getTree([]*epubimage.Image{p.Cover}, false))
if e.HasCover {
fmt.Fprintf(os.Stderr, "Cover:\n%s\n", e.getTree([]*Image{p.Cover}, false))
}
fmt.Fprintf(os.Stderr, "Files:\n%s\n", e.getTree(p.Images, false))
}
@ -205,7 +219,7 @@ func (e *ePub) Write() error {
totalParts := len(epubParts)
bar := e.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 := ""
@ -216,7 +230,7 @@ func (e *ePub) Write() error {
}
path := fmt.Sprintf("%s%s%s", e.Output[0:len(e.Output)-len(ext)], suffix, ext)
wz, err := epubzip.New(path)
wz, err := newEpubZip(path)
if err != nil {
return err
}
@ -228,19 +242,19 @@ func (e *ePub) Write() error {
}
content := []zipContent{
{"META-INF/container.xml", epubtemplates.Container},
{"META-INF/com.apple.ibooks.display-options.xml", epubtemplates.AppleBooks},
{"META-INF/container.xml", templates.Container},
{"META-INF/com.apple.ibooks.display-options.xml", templates.AppleBooks},
{"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(epubtemplates.Style, map[string]any{
"PageWidth": e.Image.ViewWidth,
"PageHeight": e.Image.ViewHeight,
{"OEBPS/Text/style.css", e.render(templates.Style, map[string]any{
"PageWidth": e.ViewWidth,
"PageHeight": e.ViewHeight,
})},
{"OEBPS/Text/title.xhtml", e.render(epubtemplates.Text, map[string]any{
{"OEBPS/Text/title.xhtml", e.render(templates.Text, map[string]any{
"Title": title,
"ViewPort": fmt.Sprintf("width=%d,height=%d", e.Image.ViewWidth, e.Image.ViewHeight),
"ViewPort": fmt.Sprintf("width=%d,height=%d", e.ViewWidth, e.ViewHeight),
"ImagePath": "Images/title.jpg",
"ImageStyle": part.Cover.ImgStyle(e.Image.ViewWidth, e.Image.ViewHeight, e.Image.Manga),
"ImageStyle": part.Cover.ImgStyle(e.ViewWidth, e.ViewHeight, e.Manga),
})},
}
@ -252,13 +266,13 @@ func (e *ePub) Write() error {
return err
}
}
if err := wz.WriteImage(e.createTitleImageData(title, part.Cover, i+1, totalParts)); err != nil {
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.Image.HasCover || i > 0 {
if e.HasCover || i > 0 {
if err := e.writeImage(wz, part.Cover); err != nil {
return err
}

View File

@ -4,19 +4,12 @@ import (
"fmt"
"github.com/beevik/etree"
epubimage "github.com/celogeek/go-comic-converter/v2/internal/epub/image"
)
type Content struct {
doc *etree.Document
}
func (c *Content) String() string {
c.doc.Indent(2)
r, _ := c.doc.WriteToString()
return r
}
type TagAttrs map[string]string
type Tag struct {
@ -38,7 +31,7 @@ func (e *ePub) getMeta(title string, part *epubPart, currentPart, totalPart int)
{"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.Image.ViewWidth, e.Image.ViewHeight)}, ""},
{"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"},
@ -48,7 +41,7 @@ func (e *ePub) getMeta(title string, part *epubPart, currentPart, totalPart int)
{"dc:date", TagAttrs{}, e.UpdatedAt},
}
if e.Image.Manga {
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"}, ""})
@ -70,13 +63,13 @@ func (e *ePub) getMeta(title string, part *epubPart, currentPart, totalPart int)
}
func (e *ePub) getManifest(title string, part *epubPart, currentPart, totalPart int) []Tag {
iTag := func(img *epubimage.Image) Tag {
iTag := func(img *Image) Tag {
return Tag{"item", TagAttrs{"id": img.Key("img"), "href": img.ImgPath(), "media-type": "image/jpeg"}, ""}
}
hTag := func(img *epubimage.Image) Tag {
hTag := func(img *Image) Tag {
return Tag{"item", TagAttrs{"id": img.Key("page"), "href": img.TextPath(), "media-type": "application/xhtml+xml"}, ""}
}
sTag := func(img *epubimage.Image) Tag {
sTag := func(img *Image) Tag {
return Tag{"item", TagAttrs{"id": img.SpaceKey("page"), "href": img.SpacePath(), "media-type": "application/xhtml+xml"}, ""}
}
items := []Tag{
@ -86,7 +79,7 @@ func (e *ePub) getManifest(title string, part *epubPart, currentPart, totalPart
{"item", TagAttrs{"id": "img_title", "href": "Images/title.jpg", "media-type": "image/jpeg"}, ""},
}
if e.Image.HasCover || currentPart > 1 {
if e.HasCover || currentPart > 1 {
items = append(items, iTag(part.Cover), hTag(part.Cover))
}
@ -102,12 +95,12 @@ func (e *ePub) getManifest(title string, part *epubPart, currentPart, totalPart
}
func (e *ePub) getSpine(title string, part *epubPart, currentPart, totalPart int) []Tag {
isOnTheRight := !e.Image.Manga
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.Image.Manga
isOnTheRight = !e.Manga
return "rendition:page-spread-center"
}
if isOnTheRight {
@ -123,10 +116,10 @@ func (e *ePub) getSpine(title string, part *epubPart, currentPart, totalPart int
for _, img := range part.Images {
spine = append(spine, Tag{
"itemref",
TagAttrs{"idref": img.Key("page"), "properties": getSpread(img.DoublePage && e.Image.NoBlankPage)},
TagAttrs{"idref": img.Key("page"), "properties": getSpread(img.DoublePage && e.NoBlankPage)},
"",
})
if img.DoublePage && isOnTheRight && !e.Image.NoBlankPage {
if img.DoublePage && isOnTheRight && !e.NoBlankPage {
spine = append(spine, Tag{
"itemref",
TagAttrs{"idref": img.SpaceKey("page"), "properties": getSpread(false)},
@ -134,7 +127,7 @@ func (e *ePub) getSpine(title string, part *epubPart, currentPart, totalPart int
})
}
}
if e.Image.Manga == isOnTheRight {
if e.Manga == isOnTheRight {
spine = append(spine, Tag{
"itemref",
TagAttrs{"idref": part.Images[len(part.Images)-1].SpaceKey("page"), "properties": getSpread(false)},
@ -186,7 +179,7 @@ func (e *ePub) getContent(title string, part *epubPart, currentPart, totalPart i
addToElement(manifest, e.getManifest)
spine := pkg.CreateElement("spine")
if e.Image.Manga {
if e.Manga {
spine.CreateAttr("page-progression-direction", "rtl")
} else {
spine.CreateAttr("page-progression-direction", "ltr")
@ -200,3 +193,9 @@ func (e *ePub) getContent(title string, part *epubPart, currentPart, totalPart i
doc,
}
}
func (c *Content) String() string {
c.doc.Indent(2)
r, _ := c.doc.WriteToString()
return r
}

View File

@ -1,4 +1,4 @@
package epubimagedata
package epub
import (
"archive/zip"
@ -20,12 +20,12 @@ func (img *ImageData) CompressedSize() uint64 {
return img.Header.CompressedSize64 + 30 + uint64(len(img.Header.Name))
}
func New(id int, part int, img image.Image, quality int) *ImageData {
func newImageData(id int, part int, img image.Image, quality int) *ImageData {
name := fmt.Sprintf("OEBPS/Images/%d_p%d.jpg", id, part)
return NewRaw(name, img, quality)
return newData(name, img, quality)
}
func NewRaw(name string, img image.Image, quality int) *ImageData {
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

@ -1,16 +1,16 @@
package epubimage
package epub
import (
epubfilters "github.com/celogeek/go-comic-converter/v2/internal/epub/filters"
"github.com/celogeek/go-comic-converter/v2/internal/epub/filters"
"github.com/disintegration/gift"
)
func NewGift(options *Options) *gift.GIFT {
func NewGift(options *ImageOptions) *gift.GIFT {
g := gift.New()
g.SetParallelization(false)
if options.AutoRotate {
g.Add(epubfilters.AutoRotate(options.ViewWidth, options.ViewHeight))
g.Add(filters.AutoRotate(options.ViewWidth, options.ViewHeight))
}
if options.Contrast != 0 {
g.Add(gift.Contrast(float32(options.Contrast)))
@ -19,21 +19,21 @@ func NewGift(options *Options) *gift.GIFT {
g.Add(gift.Brightness(float32(options.Brightness)))
}
g.Add(
epubfilters.Resize(options.ViewWidth, options.ViewHeight, gift.LanczosResampling),
epubfilters.Pixel(),
filters.Resize(options.ViewWidth, options.ViewHeight, gift.LanczosResampling),
filters.Pixel(),
)
return g
}
func NewGiftSplitDoublePage(options *Options) []*gift.GIFT {
func NewGiftSplitDoublePage(options *ImageOptions) []*gift.GIFT {
gifts := make([]*gift.GIFT, 2)
gifts[0] = gift.New(
epubfilters.CropSplitDoublePage(options.Manga),
filters.CropSplitDoublePage(options.Manga),
)
gifts[1] = gift.New(
epubfilters.CropSplitDoublePage(!options.Manga),
filters.CropSplitDoublePage(!options.Manga),
)
for _, g := range gifts {
@ -45,7 +45,7 @@ func NewGiftSplitDoublePage(options *Options) []*gift.GIFT {
}
g.Add(
epubfilters.Resize(options.ViewWidth, options.ViewHeight, gift.LanczosResampling),
filters.Resize(options.ViewWidth, options.ViewHeight, gift.LanczosResampling),
)
}

View File

@ -17,9 +17,7 @@ import (
"strings"
"sync"
epubimage "github.com/celogeek/go-comic-converter/v2/internal/epub/image"
epubimagedata "github.com/celogeek/go-comic-converter/v2/internal/epub/imagedata"
"github.com/celogeek/go-comic-converter/v2/internal/sortpath"
"github.com/celogeek/go-comic-converter/v2/internal/epub/sortpath"
"github.com/disintegration/gift"
"github.com/golang/freetype"
"github.com/golang/freetype/truetype"
@ -32,6 +30,69 @@ import (
_ "golang.org/x/image/webp"
)
type Image struct {
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 {
Id int
Reader io.ReadCloser
@ -90,8 +151,8 @@ BOTTOM:
return imgArea
}
func (e *ePub) LoadImages() ([]*epubimage.Image, error) {
images := make([]*epubimage.Image, 0)
func (e *ePub) LoadImages() ([]*Image, error) {
images := make([]*Image, 0)
fi, err := os.Stat(e.Input)
if err != nil {
@ -124,7 +185,7 @@ func (e *ePub) LoadImages() ([]*epubimage.Image, error) {
if e.Dry {
for img := range imageInput {
img.Reader.Close()
images = append(images, &epubimage.Image{
images = append(images, &Image{
Id: img.Id,
Path: img.Path,
Name: img.Name,
@ -134,13 +195,13 @@ func (e *ePub) LoadImages() ([]*epubimage.Image, error) {
return images, nil
}
imageOutput := make(chan *epubimage.Image)
imageOutput := make(chan *Image)
// processing
bar := e.NewBar(imageCount, "Processing", 1, 2)
bar := NewBar(e.Quiet, imageCount, "Processing", 1, 2)
wg := &sync.WaitGroup{}
for i := 0; i < e.Workers; i++ {
for i := 0; i < e.ImageOptions.Workers; i++ {
wg.Add(1)
go func() {
defer wg.Done()
@ -155,14 +216,14 @@ func (e *ePub) LoadImages() ([]*epubimage.Image, error) {
os.Exit(1)
}
if e.Image.Crop {
if e.ImageOptions.Crop {
g := gift.New(gift.Crop(findMarging(src)))
newSrc := image.NewNRGBA(g.Bounds(src.Bounds()))
g.Draw(newSrc, src)
src = newSrc
}
g := epubimage.NewGift(e.Options.Image)
g := NewGift(e.ImageOptions)
// Convert image
dst := image.NewGray(g.Bounds(src.Bounds()))
@ -173,17 +234,17 @@ func (e *ePub) LoadImages() ([]*epubimage.Image, error) {
raw = dst
}
imageOutput <- &epubimage.Image{
imageOutput <- &Image{
Id: img.Id,
Part: 0,
Raw: raw,
Data: epubimagedata.New(img.Id, 0, dst, e.Image.Quality),
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.Image.ViewHeight &&
src.Bounds().Dy() > e.Image.ViewWidth,
src.Bounds().Dx() > e.ImageOptions.ViewHeight &&
src.Bounds().Dy() > e.ImageOptions.ViewWidth,
Path: img.Path,
Name: img.Name,
}
@ -191,21 +252,21 @@ func (e *ePub) LoadImages() ([]*epubimage.Image, error) {
// Auto split double page
// Except for cover
// Only if the src image have width > height and is bigger than the view
if (!e.Image.HasCover || img.Id > 0) &&
e.Image.AutoSplitDoublePage &&
if (!e.ImageOptions.HasCover || img.Id > 0) &&
e.ImageOptions.AutoSplitDoublePage &&
src.Bounds().Dx() > src.Bounds().Dy() &&
src.Bounds().Dx() > e.Image.ViewHeight &&
src.Bounds().Dy() > e.Image.ViewWidth {
gifts := epubimage.NewGiftSplitDoublePage(e.Options.Image)
src.Bounds().Dx() > e.ImageOptions.ViewHeight &&
src.Bounds().Dy() > e.ImageOptions.ViewWidth {
gifts := NewGiftSplitDoublePage(e.ImageOptions)
for i, g := range gifts {
part := i + 1
dst := image.NewGray(g.Bounds(src.Bounds()))
g.Draw(dst, src)
imageOutput <- &epubimage.Image{
imageOutput <- &Image{
Id: img.Id,
Part: part,
Data: epubimagedata.New(img.Id, part, dst, e.Image.Quality),
Data: newImageData(img.Id, part, dst, e.ImageOptions.Quality),
Width: dst.Bounds().Dx(),
Height: dst.Bounds().Dy(),
IsCover: false,
@ -224,11 +285,11 @@ func (e *ePub) LoadImages() ([]*epubimage.Image, error) {
close(imageOutput)
}()
for img := range imageOutput {
if !(e.Image.NoBlankPage && img.Width == 1 && img.Height == 1) {
images = append(images, img)
for image := range imageOutput {
if !(e.ImageOptions.NoBlankPage && image.Width == 1 && image.Height == 1) {
images = append(images, image)
}
if img.Part == 0 {
if image.Part == 0 {
bar.Add(1)
}
}
@ -458,7 +519,7 @@ func loadPdf(input string) (int, chan *imageTask, error) {
return nbPages, output, nil
}
func (e *ePub) createTitleImageData(title string, img *epubimage.Image, currentPart, totalPart int) *epubimagedata.ImageData {
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()))
@ -515,5 +576,5 @@ func (e *ePub) createTitleImageData(title string, img *epubimage.Image, currentP
}
c.DrawString(title, freetype.Pt(textLeft, img.Height/2+textHeight/4))
return epubimagedata.NewRaw("OEBPS/Images/title.jpg", dst, e.Image.Quality)
return newData("OEBPS/Images/title.jpg", dst, e.Quality)
}

View File

@ -7,8 +7,8 @@ import (
"github.com/schollz/progressbar/v3"
)
func (e *ePub) NewBar(max int, description string, currentJob, totalJob int) *progressbar.ProgressBar {
if e.Quiet {
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)))

View File

@ -5,10 +5,9 @@ import (
"strings"
"github.com/beevik/etree"
epubimage "github.com/celogeek/go-comic-converter/v2/internal/epub/image"
)
func (e *ePub) getToc(title string, images []*epubimage.Image) string {
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")

View File

@ -2,13 +2,58 @@ package epub
import (
"path/filepath"
epubimage "github.com/celogeek/go-comic-converter/v2/internal/epub/image"
epubtree "github.com/celogeek/go-comic-converter/v2/internal/epub/tree"
"strings"
)
func (e *ePub) getTree(images []*epubimage.Image, skip_files bool) string {
t := epubtree.New()
type Tree struct {
Nodes map[string]*Node
}
type Node struct {
Value string
Children []*Node
}
func NewTree() *Tree {
return &Tree{map[string]*Node{
".": {".", []*Node{}},
}}
}
func (n *Tree) Root() *Node {
return n.Nodes["."]
}
func (n *Tree) Add(filename string) {
cn := n.Root()
cp := ""
for _, p := range strings.Split(filepath.Clean(filename), string(filepath.Separator)) {
cp = filepath.Join(cp, p)
if _, ok := n.Nodes[cp]; !ok {
n.Nodes[cp] = &Node{Value: p, Children: []*Node{}}
cn.Children = append(cn.Children, n.Nodes[cp])
}
cn = n.Nodes[cp]
}
}
func (n *Node) toString(indent string) string {
r := strings.Builder{}
if indent != "" {
r.WriteString(indent)
r.WriteString("- ")
r.WriteString(n.Value)
r.WriteString("\n")
}
indent += " "
for _, c := range n.Children {
r.WriteString(c.toString(indent))
}
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)
@ -21,5 +66,5 @@ func (e *ePub) getTree(images []*epubimage.Image, skip_files bool) string {
c = c.Children[0]
}
return c.ToString("")
return c.toString("")
}

View File

@ -1,36 +1,34 @@
package epubzip
package epub
import (
"archive/zip"
"fmt"
"os"
"time"
epubimagedata "github.com/celogeek/go-comic-converter/v2/internal/epub/imagedata"
)
type EpubZip struct {
type epubZip struct {
w *os.File
wz *zip.Writer
}
func New(path string) (*EpubZip, error) {
func newEpubZip(path string) (*epubZip, error) {
w, err := os.Create(path)
if err != nil {
return nil, err
}
wz := zip.NewWriter(w)
return &EpubZip{w, wz}, nil
return &epubZip{w, wz}, nil
}
func (e *EpubZip) Close() error {
func (e *epubZip) Close() error {
if err := e.wz.Close(); err != nil {
return err
}
return e.w.Close()
}
func (e *EpubZip) WriteMagic() error {
func (e *epubZip) WriteMagic() error {
t := time.Now()
fh := &zip.FileHeader{
Name: "mimetype",
@ -52,7 +50,7 @@ func (e *EpubZip) WriteMagic() error {
return err
}
func (e *EpubZip) WriteImage(image *epubimagedata.ImageData) error {
func (e *epubZip) WriteImage(image *ImageData) error {
m, err := e.wz.CreateRaw(image.Header)
if err != nil {
return err
@ -61,7 +59,7 @@ func (e *EpubZip) WriteImage(image *epubimagedata.ImageData) error {
return err
}
func (e *EpubZip) WriteFile(file string, data any) error {
func (e *epubZip) WriteFile(file string, data any) error {
var content []byte
switch b := data.(type) {
case string:

View File

@ -1,4 +1,4 @@
package epubfilters
package filters
import (
"image"

View File

@ -1,4 +1,4 @@
package epubfilters
package filters
import (
"image"

View File

@ -1,4 +1,4 @@
package epubfilters
package filters
import (
"image"

View File

@ -1,4 +1,4 @@
package epubfilters
package filters
import (
"image"

View File

@ -1,71 +0,0 @@
package epubimage
import (
"fmt"
"image"
epubimagedata "github.com/celogeek/go-comic-converter/v2/internal/epub/imagedata"
)
type Image struct {
Id int
Part int
Raw image.Image
Data *epubimagedata.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)
}

View File

@ -1,15 +0,0 @@
package epubimage
type Options struct {
Crop bool
ViewWidth int
ViewHeight int
Quality int
Brightness int
Contrast int
AutoRotate bool
AutoSplitDoublePage bool
NoBlankPage bool
Manga bool
HasCover bool
}

View File

@ -16,7 +16,7 @@ type part struct {
number float64
}
func (a part) compare(b part) float64 {
func (a part) Compare(b part) float64 {
if a.number == 0 || b.number == 0 {
return float64(strings.Compare(a.fullname, b.fullname))
}
@ -70,7 +70,7 @@ func comparePart(a, b []part) float64 {
m = len(b)
}
for i := 0; i < m; i++ {
c := a[i].compare(b[i])
c := a[i].Compare(b[i])
if c != 0 {
return c
}

View File

@ -1,4 +1,4 @@
package epubtemplates
package templates
import _ "embed"

View File

@ -1,53 +0,0 @@
package epubtree
import (
"path/filepath"
"strings"
)
type Tree struct {
Nodes map[string]*Node
}
type Node struct {
Value string
Children []*Node
}
func New() *Tree {
return &Tree{map[string]*Node{
".": {".", []*Node{}},
}}
}
func (n *Tree) Root() *Node {
return n.Nodes["."]
}
func (n *Tree) Add(filename string) {
cn := n.Root()
cp := ""
for _, p := range strings.Split(filepath.Clean(filename), string(filepath.Separator)) {
cp = filepath.Join(cp, p)
if _, ok := n.Nodes[cp]; !ok {
n.Nodes[cp] = &Node{Value: p, Children: []*Node{}}
cn.Children = append(cn.Children, n.Nodes[cp])
}
cn = n.Nodes[cp]
}
}
func (n *Node) ToString(indent string) string {
r := strings.Builder{}
if indent != "" {
r.WriteString(indent)
r.WriteString("- ")
r.WriteString(n.Value)
r.WriteString("\n")
}
indent += " "
for _, c := range n.Children {
r.WriteString(c.ToString(indent))
}
return r.String()
}

View File

@ -7,7 +7,6 @@ import (
"github.com/celogeek/go-comic-converter/v2/internal/converter"
"github.com/celogeek/go-comic-converter/v2/internal/epub"
epubimage "github.com/celogeek/go-comic-converter/v2/internal/epub/image"
"github.com/tcnksm/go-latest"
)
@ -94,7 +93,7 @@ $ go install github.com/celogeek/go-comic-converter/v%d@%s
profile := cmd.Options.GetProfile()
perfectWidth, perfectHeight := profile.PerfectDim()
if err := epub.New(&epub.Options{
if err := epub.NewEpub(&epub.EpubOptions{
Input: cmd.Options.Input,
Output: cmd.Options.Output,
LimitMb: cmd.Options.LimitMb,
@ -102,7 +101,7 @@ $ go install github.com/celogeek/go-comic-converter/v%d@%s
Author: cmd.Options.Author,
StripFirstDirectoryFromToc: cmd.Options.StripFirstDirectoryFromToc,
SortPathMode: cmd.Options.SortPathMode,
Image: &epubimage.Options{
ImageOptions: &epub.ImageOptions{
ViewWidth: perfectWidth,
ViewHeight: perfectHeight,
Quality: cmd.Options.Quality,
@ -114,8 +113,8 @@ $ go install github.com/celogeek/go-comic-converter/v%d@%s
NoBlankPage: cmd.Options.NoBlankPage,
Manga: cmd.Options.Manga,
HasCover: cmd.Options.HasCover,
Workers: cmd.Options.Workers,
},
Workers: cmd.Options.Workers,
Dry: cmd.Options.Dry,
DryVerbose: cmd.Options.DryVerbose,
Quiet: cmd.Options.Quiet,