mirror of
https://github.com/celogeek/go-comic-converter.git
synced 2025-05-24 15:52:38 +02:00
Compare commits
2 Commits
8beb5c114b
...
4c9644e9b3
Author | SHA1 | Date | |
---|---|---|---|
4c9644e9b3 | |||
483e9152d5 |
@ -10,16 +10,16 @@ import (
|
||||
"os"
|
||||
"sync"
|
||||
|
||||
"github.com/celogeek/go-comic-converter/v2/pkg/epubimage"
|
||||
"github.com/celogeek/go-comic-converter/v2/pkg/epubimagefilters"
|
||||
"github.com/celogeek/go-comic-converter/v2/internal/epubimage"
|
||||
"github.com/celogeek/go-comic-converter/v2/internal/epubimagefilters"
|
||||
"github.com/celogeek/go-comic-converter/v2/internal/epubprogress"
|
||||
"github.com/celogeek/go-comic-converter/v2/internal/epubzip"
|
||||
"github.com/celogeek/go-comic-converter/v2/pkg/epuboptions"
|
||||
"github.com/celogeek/go-comic-converter/v2/pkg/epubprogress"
|
||||
"github.com/celogeek/go-comic-converter/v2/pkg/epubzip"
|
||||
"github.com/disintegration/gift"
|
||||
)
|
||||
|
||||
type EPUBImageProcessor struct {
|
||||
*epuboptions.EPUBOptions
|
||||
options *epuboptions.EPUBOptions
|
||||
}
|
||||
|
||||
func New(o *epuboptions.EPUBOptions) *EPUBImageProcessor {
|
||||
@ -35,13 +35,13 @@ func (e *EPUBImageProcessor) Load() (images []*epubimage.EPUBImage, err error) {
|
||||
}
|
||||
|
||||
// dry run, skip convertion
|
||||
if e.Dry {
|
||||
if e.options.Dry {
|
||||
for img := range imageInput {
|
||||
images = append(images, &epubimage.EPUBImage{
|
||||
Id: img.Id,
|
||||
Path: img.Path,
|
||||
Name: img.Name,
|
||||
Format: e.Image.Format,
|
||||
Format: e.options.Image.Format,
|
||||
})
|
||||
}
|
||||
|
||||
@ -52,8 +52,8 @@ func (e *EPUBImageProcessor) Load() (images []*epubimage.EPUBImage, err error) {
|
||||
|
||||
// processing
|
||||
bar := epubprogress.New(epubprogress.Options{
|
||||
Quiet: e.Quiet,
|
||||
Json: e.Json,
|
||||
Quiet: e.options.Quiet,
|
||||
Json: e.options.Json,
|
||||
Max: imageCount,
|
||||
Description: "Processing",
|
||||
CurrentJob: 1,
|
||||
@ -61,17 +61,17 @@ func (e *EPUBImageProcessor) Load() (images []*epubimage.EPUBImage, err error) {
|
||||
})
|
||||
wg := &sync.WaitGroup{}
|
||||
|
||||
imgStorage, err := epubzip.NewImageWriter(e.ImgStorage(), e.Image.Format)
|
||||
imgStorage, err := epubzip.NewImageWriter(e.options.ImgStorage(), e.options.Image.Format)
|
||||
if err != nil {
|
||||
bar.Close()
|
||||
return nil, err
|
||||
}
|
||||
|
||||
wr := 50
|
||||
if e.Image.Format == "png" {
|
||||
if e.options.Image.Format == "png" {
|
||||
wr = 100
|
||||
}
|
||||
for i := 0; i < e.WorkersRatio(wr); i++ {
|
||||
for i := 0; i < e.options.WorkersRatio(wr); i++ {
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
@ -96,7 +96,7 @@ func (e *EPUBImageProcessor) Load() (images []*epubimage.EPUBImage, err error) {
|
||||
DoublePage: part == 0 && src.Bounds().Dx() > src.Bounds().Dy(),
|
||||
Path: input.Path,
|
||||
Name: input.Name,
|
||||
Format: e.Image.Format,
|
||||
Format: e.options.Image.Format,
|
||||
OriginalAspectRatio: float64(src.Bounds().Dy()) / float64(src.Bounds().Dx()),
|
||||
Error: input.Error,
|
||||
}
|
||||
@ -104,12 +104,12 @@ func (e *EPUBImageProcessor) Load() (images []*epubimage.EPUBImage, err error) {
|
||||
// do not keep double page if requested
|
||||
if !img.IsCover &&
|
||||
img.DoublePage &&
|
||||
e.EPUBOptions.Image.AutoSplitDoublePage &&
|
||||
!e.EPUBOptions.Image.KeepDoublePageIfSplitted {
|
||||
e.options.Image.AutoSplitDoublePage &&
|
||||
!e.options.Image.KeepDoublePageIfSplitted {
|
||||
continue
|
||||
}
|
||||
|
||||
if err = imgStorage.Add(img.EPUBImgPath(), dst, e.Image.Quality); err != nil {
|
||||
if err = imgStorage.Add(img.EPUBImgPath(), dst, e.options.Image.Quality); err != nil {
|
||||
bar.Close()
|
||||
fmt.Fprintf(os.Stderr, "error with %s: %s", input.Name, err)
|
||||
os.Exit(1)
|
||||
@ -130,7 +130,7 @@ func (e *EPUBImageProcessor) Load() (images []*epubimage.EPUBImage, err error) {
|
||||
if img.Part == 0 {
|
||||
bar.Add(1)
|
||||
}
|
||||
if e.Image.NoBlankImage && img.IsBlank {
|
||||
if e.options.Image.NoBlankImage && img.IsBlank {
|
||||
continue
|
||||
}
|
||||
images = append(images, img)
|
||||
@ -145,7 +145,7 @@ func (e *EPUBImageProcessor) Load() (images []*epubimage.EPUBImage, err error) {
|
||||
}
|
||||
|
||||
func (e *EPUBImageProcessor) createImage(src image.Image, r image.Rectangle) draw.Image {
|
||||
if e.EPUBOptions.Image.GrayScale {
|
||||
if e.options.Image.GrayScale {
|
||||
return image.NewGray(r)
|
||||
}
|
||||
|
||||
@ -182,13 +182,13 @@ func (e *EPUBImageProcessor) transformImage(src image.Image, srcId int) []image.
|
||||
var images []image.Image
|
||||
|
||||
// Lookup for margin if crop is enable or if we want to remove blank image
|
||||
if e.Image.Crop.Enabled || e.Image.NoBlankImage {
|
||||
if e.options.Image.Crop.Enabled || e.options.Image.NoBlankImage {
|
||||
f := epubimagefilters.AutoCrop(
|
||||
src,
|
||||
e.Image.Crop.Left,
|
||||
e.Image.Crop.Up,
|
||||
e.Image.Crop.Right,
|
||||
e.Image.Crop.Bottom,
|
||||
e.options.Image.Crop.Left,
|
||||
e.options.Image.Crop.Up,
|
||||
e.options.Image.Crop.Right,
|
||||
e.options.Image.Crop.Bottom,
|
||||
)
|
||||
|
||||
// detect if blank image
|
||||
@ -196,42 +196,42 @@ func (e *EPUBImageProcessor) transformImage(src image.Image, srcId int) []image.
|
||||
isBlank := size.Dx() == 0 && size.Dy() == 0
|
||||
|
||||
// crop is enable or if blank image with noblankimage options
|
||||
if e.Image.Crop.Enabled || (e.Image.NoBlankImage && isBlank) {
|
||||
if e.options.Image.Crop.Enabled || (e.options.Image.NoBlankImage && isBlank) {
|
||||
filters = append(filters, f)
|
||||
splitFilters = append(splitFilters, f)
|
||||
}
|
||||
}
|
||||
|
||||
if e.Image.AutoRotate && src.Bounds().Dx() > src.Bounds().Dy() {
|
||||
if e.options.Image.AutoRotate && src.Bounds().Dx() > src.Bounds().Dy() {
|
||||
filters = append(filters, gift.Rotate90())
|
||||
}
|
||||
|
||||
if e.Image.AutoContrast {
|
||||
if e.options.Image.AutoContrast {
|
||||
f := epubimagefilters.AutoContrast()
|
||||
filters = append(filters, f)
|
||||
splitFilters = append(splitFilters, f)
|
||||
}
|
||||
|
||||
if e.Image.Contrast != 0 {
|
||||
f := gift.Contrast(float32(e.Image.Contrast))
|
||||
if e.options.Image.Contrast != 0 {
|
||||
f := gift.Contrast(float32(e.options.Image.Contrast))
|
||||
filters = append(filters, f)
|
||||
splitFilters = append(splitFilters, f)
|
||||
}
|
||||
|
||||
if e.Image.Brightness != 0 {
|
||||
f := gift.Brightness(float32(e.Image.Brightness))
|
||||
if e.options.Image.Brightness != 0 {
|
||||
f := gift.Brightness(float32(e.options.Image.Brightness))
|
||||
filters = append(filters, f)
|
||||
splitFilters = append(splitFilters, f)
|
||||
}
|
||||
|
||||
if e.Image.Resize {
|
||||
f := gift.ResizeToFit(e.Image.View.Width, e.Image.View.Height, gift.LanczosResampling)
|
||||
if e.options.Image.Resize {
|
||||
f := gift.ResizeToFit(e.options.Image.View.Width, e.options.Image.View.Height, gift.LanczosResampling)
|
||||
filters = append(filters, f)
|
||||
}
|
||||
|
||||
if e.Image.GrayScale {
|
||||
if e.options.Image.GrayScale {
|
||||
var f gift.Filter
|
||||
switch e.Image.GrayScaleMode {
|
||||
switch e.options.Image.GrayScaleMode {
|
||||
case 1: // average
|
||||
f = gift.ColorFunc(func(r0, g0, b0, a0 float32) (r float32, g float32, b float32, a float32) {
|
||||
y := (r0 + g0 + b0) / 3
|
||||
@ -260,7 +260,7 @@ func (e *EPUBImageProcessor) transformImage(src image.Image, srcId int) []image.
|
||||
}
|
||||
|
||||
// auto split off
|
||||
if !e.Image.AutoSplitDoublePage {
|
||||
if !e.options.Image.AutoSplitDoublePage {
|
||||
return images
|
||||
}
|
||||
|
||||
@ -270,16 +270,16 @@ func (e *EPUBImageProcessor) transformImage(src image.Image, srcId int) []image.
|
||||
}
|
||||
|
||||
// cover
|
||||
if e.Image.HasCover && srcId == 0 {
|
||||
if e.options.Image.HasCover && srcId == 0 {
|
||||
return images
|
||||
}
|
||||
|
||||
// convert double page
|
||||
for _, b := range []bool{e.Image.Manga, !e.Image.Manga} {
|
||||
for _, b := range []bool{e.options.Image.Manga, !e.options.Image.Manga} {
|
||||
g := gift.New(splitFilters...)
|
||||
g.Add(epubimagefilters.CropSplitDoublePage(b))
|
||||
if e.Image.Resize {
|
||||
g.Add(gift.ResizeToFit(e.Image.View.Width, e.Image.View.Height, gift.LanczosResampling))
|
||||
if e.options.Image.Resize {
|
||||
g.Add(gift.ResizeToFit(e.options.Image.View.Width, e.options.Image.View.Height, gift.LanczosResampling))
|
||||
}
|
||||
dst := e.createImage(src, g.Bounds(src.Bounds()))
|
||||
g.Draw(dst, src)
|
@ -6,8 +6,8 @@ import (
|
||||
"image/color"
|
||||
"image/draw"
|
||||
|
||||
"github.com/celogeek/go-comic-converter/v2/pkg/epubimagefilters"
|
||||
"github.com/celogeek/go-comic-converter/v2/pkg/epubzip"
|
||||
"github.com/celogeek/go-comic-converter/v2/internal/epubimagefilters"
|
||||
"github.com/celogeek/go-comic-converter/v2/internal/epubzip"
|
||||
"github.com/disintegration/gift"
|
||||
)
|
||||
|
||||
@ -27,7 +27,7 @@ func (e *EPUBImageProcessor) CoverTitleData(o *CoverTitleDataOptions) (*epubzip.
|
||||
// Create a blur version of the cover
|
||||
g := gift.New(epubimagefilters.CoverTitle(o.Text, o.Align, o.PctWidth, o.PctMargin, o.MaxFontSize, o.BorderSize))
|
||||
var dst draw.Image
|
||||
if o.Name == "cover" && e.Image.GrayScale {
|
||||
if o.Name == "cover" && e.options.Image.GrayScale {
|
||||
// 16 shade of gray
|
||||
dst = image.NewPaletted(o.Src.Bounds(), color.Palette{
|
||||
color.Gray{0x00},
|
||||
@ -53,9 +53,9 @@ func (e *EPUBImageProcessor) CoverTitleData(o *CoverTitleDataOptions) (*epubzip.
|
||||
g.Draw(dst, o.Src)
|
||||
|
||||
return epubzip.CompressImage(
|
||||
fmt.Sprintf("OEBPS/Images/%s.%s", o.Name, e.Image.Format),
|
||||
e.Image.Format,
|
||||
fmt.Sprintf("OEBPS/Images/%s.%s", o.Name, e.options.Image.Format),
|
||||
e.options.Image.Format,
|
||||
dst,
|
||||
e.Image.Quality,
|
||||
e.options.Image.Quality,
|
||||
)
|
||||
}
|
@ -20,7 +20,7 @@ import (
|
||||
"golang.org/x/image/font/gofont/gomonobold"
|
||||
_ "golang.org/x/image/webp"
|
||||
|
||||
"github.com/celogeek/go-comic-converter/v2/pkg/sortpath"
|
||||
"github.com/celogeek/go-comic-converter/v2/internal/sortpath"
|
||||
"github.com/fogleman/gg"
|
||||
"github.com/golang/freetype/truetype"
|
||||
"github.com/nwaples/rardecode/v2"
|
||||
@ -51,7 +51,7 @@ func (e *EPUBImageProcessor) isSupportedImage(path string) bool {
|
||||
|
||||
// Load images from input
|
||||
func (e *EPUBImageProcessor) load() (totalImages int, output chan *tasks, err error) {
|
||||
fi, err := os.Stat(e.Input)
|
||||
fi, err := os.Stat(e.options.Input)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
@ -60,7 +60,7 @@ func (e *EPUBImageProcessor) load() (totalImages int, output chan *tasks, err er
|
||||
if fi.IsDir() {
|
||||
return e.loadDir()
|
||||
} else {
|
||||
switch ext := strings.ToLower(filepath.Ext(e.Input)); ext {
|
||||
switch ext := strings.ToLower(filepath.Ext(e.options.Input)); ext {
|
||||
case ".cbz", ".zip":
|
||||
return e.loadCbz()
|
||||
case ".cbr", ".rar":
|
||||
@ -101,7 +101,7 @@ func (e *EPUBImageProcessor) corruptedImage(path, name string) image.Image {
|
||||
func (e *EPUBImageProcessor) loadDir() (totalImages int, output chan *tasks, err error) {
|
||||
images := make([]string, 0)
|
||||
|
||||
input := filepath.Clean(e.Input)
|
||||
input := filepath.Clean(e.options.Input)
|
||||
err = filepath.WalkDir(input, func(path string, d fs.DirEntry, err error) error {
|
||||
if err != nil {
|
||||
return err
|
||||
@ -123,7 +123,7 @@ func (e *EPUBImageProcessor) loadDir() (totalImages int, output chan *tasks, err
|
||||
return
|
||||
}
|
||||
|
||||
sort.Sort(sortpath.By(images, e.SortPathMode))
|
||||
sort.Sort(sortpath.By(images, e.options.SortPathMode))
|
||||
|
||||
// Queue all file with id
|
||||
type job struct {
|
||||
@ -139,16 +139,16 @@ func (e *EPUBImageProcessor) loadDir() (totalImages int, output chan *tasks, err
|
||||
}()
|
||||
|
||||
// read in parallel and get an image
|
||||
output = make(chan *tasks, e.Workers)
|
||||
output = make(chan *tasks, e.options.Workers)
|
||||
wg := &sync.WaitGroup{}
|
||||
for j := 0; j < e.WorkersRatio(50); j++ {
|
||||
for j := 0; j < e.options.WorkersRatio(50); j++ {
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
for job := range jobs {
|
||||
var img image.Image
|
||||
var err error
|
||||
if !e.Dry {
|
||||
if !e.options.Dry {
|
||||
var f *os.File
|
||||
f, err = os.Open(job.Path)
|
||||
if err == nil {
|
||||
@ -188,7 +188,7 @@ func (e *EPUBImageProcessor) loadDir() (totalImages int, output chan *tasks, err
|
||||
|
||||
// load a zip file that include images
|
||||
func (e *EPUBImageProcessor) loadCbz() (totalImages int, output chan *tasks, err error) {
|
||||
r, err := zip.OpenReader(e.Input)
|
||||
r, err := zip.OpenReader(e.options.Input)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
@ -212,7 +212,7 @@ func (e *EPUBImageProcessor) loadCbz() (totalImages int, output chan *tasks, err
|
||||
for _, img := range images {
|
||||
names = append(names, img.Name)
|
||||
}
|
||||
sort.Sort(sortpath.By(names, e.SortPathMode))
|
||||
sort.Sort(sortpath.By(names, e.options.SortPathMode))
|
||||
|
||||
indexedNames := make(map[string]int)
|
||||
for i, name := range names {
|
||||
@ -231,16 +231,16 @@ func (e *EPUBImageProcessor) loadCbz() (totalImages int, output chan *tasks, err
|
||||
}
|
||||
}()
|
||||
|
||||
output = make(chan *tasks, e.Workers)
|
||||
output = make(chan *tasks, e.options.Workers)
|
||||
wg := &sync.WaitGroup{}
|
||||
for j := 0; j < e.WorkersRatio(50); j++ {
|
||||
for j := 0; j < e.options.WorkersRatio(50); j++ {
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
for job := range jobs {
|
||||
var img image.Image
|
||||
var err error
|
||||
if !e.Dry {
|
||||
if !e.options.Dry {
|
||||
var f io.ReadCloser
|
||||
f, err = job.F.Open()
|
||||
if err == nil {
|
||||
@ -275,7 +275,7 @@ func (e *EPUBImageProcessor) loadCbz() (totalImages int, output chan *tasks, err
|
||||
// load a rar file that include images
|
||||
func (e *EPUBImageProcessor) loadCbr() (totalImages int, output chan *tasks, err error) {
|
||||
var isSolid bool
|
||||
files, err := rardecode.List(e.Input)
|
||||
files, err := rardecode.List(e.options.Input)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
@ -296,7 +296,7 @@ func (e *EPUBImageProcessor) loadCbr() (totalImages int, output chan *tasks, err
|
||||
return
|
||||
}
|
||||
|
||||
sort.Sort(sortpath.By(names, e.SortPathMode))
|
||||
sort.Sort(sortpath.By(names, e.options.SortPathMode))
|
||||
|
||||
indexedNames := make(map[string]int)
|
||||
for i, name := range names {
|
||||
@ -312,10 +312,10 @@ func (e *EPUBImageProcessor) loadCbr() (totalImages int, output chan *tasks, err
|
||||
jobs := make(chan *job)
|
||||
go func() {
|
||||
defer close(jobs)
|
||||
if isSolid && !e.Dry {
|
||||
r, rerr := rardecode.OpenReader(e.Input)
|
||||
if isSolid && !e.options.Dry {
|
||||
r, rerr := rardecode.OpenReader(e.options.Input)
|
||||
if rerr != nil {
|
||||
fmt.Fprintf(os.Stderr, "\nerror processing image %s: %s\n", e.Input, rerr)
|
||||
fmt.Fprintf(os.Stderr, "\nerror processing image %s: %s\n", e.options.Input, rerr)
|
||||
os.Exit(1)
|
||||
}
|
||||
defer r.Close()
|
||||
@ -350,16 +350,16 @@ func (e *EPUBImageProcessor) loadCbr() (totalImages int, output chan *tasks, err
|
||||
}()
|
||||
|
||||
// send file to the queue
|
||||
output = make(chan *tasks, e.Workers)
|
||||
output = make(chan *tasks, e.options.Workers)
|
||||
wg := &sync.WaitGroup{}
|
||||
for j := 0; j < e.WorkersRatio(50); j++ {
|
||||
for j := 0; j < e.options.WorkersRatio(50); j++ {
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
for job := range jobs {
|
||||
var img image.Image
|
||||
var err error
|
||||
if !e.Dry {
|
||||
if !e.options.Dry {
|
||||
var f io.ReadCloser
|
||||
f, err = job.Open()
|
||||
if err == nil {
|
||||
@ -391,7 +391,7 @@ func (e *EPUBImageProcessor) loadCbr() (totalImages int, output chan *tasks, err
|
||||
|
||||
// extract image from a pdf
|
||||
func (e *EPUBImageProcessor) loadPdf() (totalImages int, output chan *tasks, err error) {
|
||||
pdf := pdfread.Load(e.Input)
|
||||
pdf := pdfread.Load(e.options.Input)
|
||||
if pdf == nil {
|
||||
err = fmt.Errorf("can't read pdf")
|
||||
return
|
||||
@ -406,7 +406,7 @@ func (e *EPUBImageProcessor) loadPdf() (totalImages int, output chan *tasks, err
|
||||
for i := 0; i < totalImages; i++ {
|
||||
var img image.Image
|
||||
var err error
|
||||
if !e.Dry {
|
||||
if !e.options.Dry {
|
||||
img, err = pdfimage.Extract(pdf, i+1)
|
||||
}
|
||||
|
@ -4,7 +4,7 @@ import (
|
||||
"fmt"
|
||||
|
||||
"github.com/beevik/etree"
|
||||
"github.com/celogeek/go-comic-converter/v2/pkg/epubimage"
|
||||
"github.com/celogeek/go-comic-converter/v2/internal/epubimage"
|
||||
"github.com/celogeek/go-comic-converter/v2/pkg/epuboptions"
|
||||
)
|
||||
|
@ -5,7 +5,7 @@ import (
|
||||
"strings"
|
||||
|
||||
"github.com/beevik/etree"
|
||||
"github.com/celogeek/go-comic-converter/v2/pkg/epubimage"
|
||||
"github.com/celogeek/go-comic-converter/v2/internal/epubimage"
|
||||
)
|
||||
|
||||
// create toc
|
119
pkg/epub/epub.go
119
pkg/epub/epub.go
@ -15,21 +15,22 @@ import (
|
||||
"text/template"
|
||||
"time"
|
||||
|
||||
"github.com/celogeek/go-comic-converter/v2/pkg/epubimage"
|
||||
"github.com/celogeek/go-comic-converter/v2/pkg/epubimageprocessor"
|
||||
"github.com/celogeek/go-comic-converter/v2/internal/epubimage"
|
||||
"github.com/celogeek/go-comic-converter/v2/internal/epubimageprocessor"
|
||||
"github.com/celogeek/go-comic-converter/v2/internal/epubprogress"
|
||||
"github.com/celogeek/go-comic-converter/v2/internal/epubtemplates"
|
||||
"github.com/celogeek/go-comic-converter/v2/internal/epubtree"
|
||||
"github.com/celogeek/go-comic-converter/v2/internal/epubzip"
|
||||
"github.com/celogeek/go-comic-converter/v2/pkg/epuboptions"
|
||||
"github.com/celogeek/go-comic-converter/v2/pkg/epubprogress"
|
||||
"github.com/celogeek/go-comic-converter/v2/pkg/epubtemplates"
|
||||
"github.com/celogeek/go-comic-converter/v2/pkg/epubtree"
|
||||
"github.com/celogeek/go-comic-converter/v2/pkg/epubzip"
|
||||
"github.com/gofrs/uuid"
|
||||
)
|
||||
|
||||
type ePub struct {
|
||||
*epuboptions.EPUBOptions
|
||||
UID string
|
||||
Publisher string
|
||||
UpdatedAt string
|
||||
options *epuboptions.EPUBOptions
|
||||
|
||||
uid string
|
||||
publisher string
|
||||
updatedAt string
|
||||
|
||||
templateProcessor *template.Template
|
||||
imageProcessor *epubimageprocessor.EPUBImageProcessor
|
||||
@ -51,10 +52,10 @@ func New(options *epuboptions.EPUBOptions) *ePub {
|
||||
})
|
||||
|
||||
return &ePub{
|
||||
EPUBOptions: options,
|
||||
UID: uid.String(),
|
||||
Publisher: "GO Comic Converter",
|
||||
UpdatedAt: time.Now().UTC().Format("2006-01-02T15:04:05Z"),
|
||||
options: options,
|
||||
uid: uid.String(),
|
||||
publisher: "GO Comic Converter",
|
||||
updatedAt: time.Now().UTC().Format("2006-01-02T15:04:05Z"),
|
||||
templateProcessor: tmpl,
|
||||
imageProcessor: epubimageprocessor.New(options),
|
||||
}
|
||||
@ -76,9 +77,9 @@ func (e *ePub) writeImage(wz *epubzip.EPUBZip, img *epubimage.EPUBImage, zipImg
|
||||
img.EPUBPagePath(),
|
||||
[]byte(e.render(epubtemplates.Text, map[string]any{
|
||||
"Title": fmt.Sprintf("Image %d Part %d", img.Id, img.Part),
|
||||
"ViewPort": fmt.Sprintf("width=%d,height=%d", e.Image.View.Width, e.Image.View.Height),
|
||||
"ViewPort": fmt.Sprintf("width=%d,height=%d", e.options.Image.View.Width, e.options.Image.View.Height),
|
||||
"ImagePath": img.ImgPath(),
|
||||
"ImageStyle": img.ImgStyle(e.Image.View.Width, e.Image.View.Height, ""),
|
||||
"ImageStyle": img.ImgStyle(e.options.Image.View.Width, e.options.Image.View.Height, ""),
|
||||
})),
|
||||
)
|
||||
if err == nil {
|
||||
@ -94,7 +95,7 @@ func (e *ePub) writeBlank(wz *epubzip.EPUBZip, img *epubimage.EPUBImage) error {
|
||||
img.EPUBSpacePath(),
|
||||
[]byte(e.render(epubtemplates.Blank, map[string]any{
|
||||
"Title": fmt.Sprintf("Blank Page %d", img.Id),
|
||||
"ViewPort": fmt.Sprintf("width=%d,height=%d", e.Image.View.Width, e.Image.View.Height),
|
||||
"ViewPort": fmt.Sprintf("width=%d,height=%d", e.options.Image.View.Width, e.options.Image.View.Height),
|
||||
})),
|
||||
)
|
||||
}
|
||||
@ -112,9 +113,9 @@ func (e *ePub) writeCoverImage(wz *epubzip.EPUBZip, img *epubimage.EPUBImage, pa
|
||||
"OEBPS/Text/cover.xhtml",
|
||||
[]byte(e.render(epubtemplates.Text, map[string]any{
|
||||
"Title": title,
|
||||
"ViewPort": fmt.Sprintf("width=%d,height=%d", e.Image.View.Width, e.Image.View.Height),
|
||||
"ImagePath": fmt.Sprintf("Images/cover.%s", e.Image.Format),
|
||||
"ImageStyle": img.ImgStyle(e.Image.View.Width, e.Image.View.Height, ""),
|
||||
"ViewPort": fmt.Sprintf("width=%d,height=%d", e.options.Image.View.Width, e.options.Image.View.Height),
|
||||
"ImagePath": fmt.Sprintf("Images/cover.%s", e.options.Image.Format),
|
||||
"ImageStyle": img.ImgStyle(e.options.Image.View.Width, e.options.Image.View.Height, ""),
|
||||
})),
|
||||
); err != nil {
|
||||
return err
|
||||
@ -145,20 +146,20 @@ func (e *ePub) writeCoverImage(wz *epubzip.EPUBZip, img *epubimage.EPUBImage, pa
|
||||
// write title image
|
||||
func (e *ePub) writeTitleImage(wz *epubzip.EPUBZip, img *epubimage.EPUBImage, title string) error {
|
||||
titleAlign := ""
|
||||
if !e.Image.View.PortraitOnly {
|
||||
if e.Image.Manga {
|
||||
if !e.options.Image.View.PortraitOnly {
|
||||
if e.options.Image.Manga {
|
||||
titleAlign = "right:0"
|
||||
} else {
|
||||
titleAlign = "left:0"
|
||||
}
|
||||
}
|
||||
|
||||
if !e.Image.View.PortraitOnly {
|
||||
if !e.options.Image.View.PortraitOnly {
|
||||
if err := wz.WriteContent(
|
||||
"OEBPS/Text/space_title.xhtml",
|
||||
[]byte(e.render(epubtemplates.Blank, map[string]any{
|
||||
"Title": "Blank Page Title",
|
||||
"ViewPort": fmt.Sprintf("width=%d,height=%d", e.Image.View.Width, e.Image.View.Height),
|
||||
"ViewPort": fmt.Sprintf("width=%d,height=%d", e.options.Image.View.Width, e.options.Image.View.Height),
|
||||
})),
|
||||
); err != nil {
|
||||
return err
|
||||
@ -169,9 +170,9 @@ func (e *ePub) writeTitleImage(wz *epubzip.EPUBZip, img *epubimage.EPUBImage, ti
|
||||
"OEBPS/Text/title.xhtml",
|
||||
[]byte(e.render(epubtemplates.Text, map[string]any{
|
||||
"Title": title,
|
||||
"ViewPort": fmt.Sprintf("width=%d,height=%d", e.Image.View.Width, e.Image.View.Height),
|
||||
"ImagePath": fmt.Sprintf("Images/title.%s", e.Image.Format),
|
||||
"ImageStyle": img.ImgStyle(e.Image.View.Width, e.Image.View.Height, titleAlign),
|
||||
"ViewPort": fmt.Sprintf("width=%d,height=%d", e.options.Image.View.Width, e.options.Image.View.Height),
|
||||
"ImagePath": fmt.Sprintf("Images/title.%s", e.options.Image.Format),
|
||||
"ImageStyle": img.ImgStyle(e.options.Image.View.Width, e.options.Image.View.Height, titleAlign),
|
||||
})),
|
||||
); err != nil {
|
||||
return err
|
||||
@ -216,11 +217,11 @@ func (e *ePub) getParts() (parts []*epubPart, imgStorage *epubzip.EPUBZipImageRe
|
||||
|
||||
parts = make([]*epubPart, 0)
|
||||
cover := images[0]
|
||||
if e.Image.HasCover {
|
||||
if e.options.Image.HasCover {
|
||||
images = images[1:]
|
||||
}
|
||||
|
||||
if e.Dry {
|
||||
if e.options.Dry {
|
||||
parts = append(parts, &epubPart{
|
||||
Cover: cover,
|
||||
Images: images,
|
||||
@ -228,13 +229,13 @@ func (e *ePub) getParts() (parts []*epubPart, imgStorage *epubzip.EPUBZipImageRe
|
||||
return parts, nil, nil
|
||||
}
|
||||
|
||||
imgStorage, err = epubzip.NewImageReader(e.ImgStorage())
|
||||
imgStorage, err = epubzip.NewImageReader(e.options.ImgStorage())
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
// compute size of the EPUB part and try to be as close as possible of the target
|
||||
maxSize := uint64(e.LimitMb * 1024 * 1024)
|
||||
maxSize := uint64(e.options.LimitMb * 1024 * 1024)
|
||||
xhtmlSize := uint64(1024)
|
||||
// descriptor files + title + cover
|
||||
baseSize := uint64(128*1024) + imgStorage.Size(cover.EPUBImgPath())*2
|
||||
@ -280,7 +281,7 @@ func (e *ePub) getTree(images []*epubimage.EPUBImage, skip_files bool) string {
|
||||
}
|
||||
}
|
||||
c := t.Root()
|
||||
if skip_files && e.StripFirstDirectoryFromToc && len(c.Children) == 1 {
|
||||
if skip_files && e.options.StripFirstDirectoryFromToc && len(c.Children) == 1 {
|
||||
c = c.Children[0]
|
||||
}
|
||||
|
||||
@ -315,21 +316,21 @@ func (e *ePub) computeAspectRatio(epubParts []*epubPart) float64 {
|
||||
}
|
||||
|
||||
func (e *ePub) computeViewPort(epubParts []*epubPart) {
|
||||
if e.Image.View.AspectRatio == -1 {
|
||||
if e.options.Image.View.AspectRatio == -1 {
|
||||
return //keep device size
|
||||
}
|
||||
|
||||
// readjusting view port
|
||||
bestAspectRatio := e.Image.View.AspectRatio
|
||||
bestAspectRatio := e.options.Image.View.AspectRatio
|
||||
if bestAspectRatio == 0 {
|
||||
bestAspectRatio = e.computeAspectRatio(epubParts)
|
||||
}
|
||||
|
||||
viewWidth, viewHeight := int(float64(e.Image.View.Height)/bestAspectRatio), int(float64(e.Image.View.Width)*bestAspectRatio)
|
||||
if viewWidth > e.Image.View.Width {
|
||||
e.Image.View.Height = viewHeight
|
||||
viewWidth, viewHeight := int(float64(e.options.Image.View.Height)/bestAspectRatio), int(float64(e.options.Image.View.Width)*bestAspectRatio)
|
||||
if viewWidth > e.options.Image.View.Width {
|
||||
e.options.Image.View.Height = viewHeight
|
||||
} else {
|
||||
e.Image.View.Width = viewWidth
|
||||
e.options.Image.View.Width = viewWidth
|
||||
}
|
||||
}
|
||||
|
||||
@ -345,11 +346,11 @@ func (e *ePub) Write() error {
|
||||
return err
|
||||
}
|
||||
|
||||
if e.Dry {
|
||||
if e.options.Dry {
|
||||
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, "TOC:\n - %s\n%s\n", e.options.Title, e.getTree(p.Images, true))
|
||||
if e.options.DryVerbose {
|
||||
if e.options.Image.HasCover {
|
||||
fmt.Fprintf(os.Stderr, "Cover:\n%s\n", e.getTree([]*epubimage.EPUBImage{p.Cover}, false))
|
||||
}
|
||||
fmt.Fprintf(os.Stderr, "Files:\n%s\n", e.getTree(p.Images, false))
|
||||
@ -368,14 +369,14 @@ func (e *ePub) Write() error {
|
||||
Description: "Writing Part",
|
||||
CurrentJob: 2,
|
||||
TotalJob: 2,
|
||||
Quiet: e.Quiet,
|
||||
Json: e.Json,
|
||||
Quiet: e.options.Quiet,
|
||||
Json: e.options.Json,
|
||||
})
|
||||
|
||||
e.computeViewPort(epubParts)
|
||||
hasTitlePage := e.TitlePage == 1 || (e.TitlePage == 2 && totalParts > 1)
|
||||
hasTitlePage := e.options.TitlePage == 1 || (e.options.TitlePage == 2 && totalParts > 1)
|
||||
for i, part := range epubParts {
|
||||
ext := filepath.Ext(e.Output)
|
||||
ext := filepath.Ext(e.options.Output)
|
||||
suffix := ""
|
||||
if totalParts > 1 {
|
||||
fmtLen := len(fmt.Sprint(totalParts))
|
||||
@ -383,14 +384,14 @@ func (e *ePub) Write() error {
|
||||
suffix = fmt.Sprintf(fmtPart, i+1, totalParts)
|
||||
}
|
||||
|
||||
path := fmt.Sprintf("%s%s%s", e.Output[0:len(e.Output)-len(ext)], suffix, ext)
|
||||
path := fmt.Sprintf("%s%s%s", e.options.Output[0:len(e.options.Output)-len(ext)], suffix, ext)
|
||||
wz, err := epubzip.New(path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer wz.Close()
|
||||
|
||||
title := e.Title
|
||||
title := e.options.Title
|
||||
if totalParts > 1 {
|
||||
title = fmt.Sprintf("%s [%d/%d]", title, i+1, totalParts)
|
||||
}
|
||||
@ -401,19 +402,19 @@ func (e *ePub) Write() error {
|
||||
{"OEBPS/content.opf", epubtemplates.Content(&epubtemplates.ContentOptions{
|
||||
Title: title,
|
||||
HasTitlePage: hasTitlePage,
|
||||
UID: e.UID,
|
||||
Author: e.Author,
|
||||
Publisher: e.Publisher,
|
||||
UpdatedAt: e.UpdatedAt,
|
||||
ImageOptions: e.Image,
|
||||
UID: e.uid,
|
||||
Author: e.options.Author,
|
||||
Publisher: e.publisher,
|
||||
UpdatedAt: e.updatedAt,
|
||||
ImageOptions: e.options.Image,
|
||||
Cover: part.Cover,
|
||||
Images: part.Images,
|
||||
Current: i + 1,
|
||||
Total: totalParts,
|
||||
})},
|
||||
{"OEBPS/toc.xhtml", epubtemplates.Toc(title, hasTitlePage, e.StripFirstDirectoryFromToc, part.Images)},
|
||||
{"OEBPS/toc.xhtml", epubtemplates.Toc(title, hasTitlePage, e.options.StripFirstDirectoryFromToc, part.Images)},
|
||||
{"OEBPS/Text/style.css", e.render(epubtemplates.Style, map[string]any{
|
||||
"View": e.Image.View,
|
||||
"View": e.options.Image.View,
|
||||
})},
|
||||
}
|
||||
|
||||
@ -443,9 +444,9 @@ func (e *ePub) Write() error {
|
||||
}
|
||||
|
||||
// Double Page or Last Image that is not a double page
|
||||
if !e.Image.View.PortraitOnly &&
|
||||
if !e.options.Image.View.PortraitOnly &&
|
||||
(img.DoublePage ||
|
||||
(!e.Image.KeepDoublePageIfSplitted && img.Part == 1) ||
|
||||
(!e.options.Image.KeepDoublePageIfSplitted && img.Part == 1) ||
|
||||
(img.Part == 0 && img == lastImage)) {
|
||||
if err := e.writeBlank(wz, img); err != nil {
|
||||
return err
|
||||
@ -455,14 +456,14 @@ func (e *ePub) Write() error {
|
||||
bar.Add(1)
|
||||
}
|
||||
bar.Close()
|
||||
if !e.Json {
|
||||
if !e.options.Json {
|
||||
fmt.Fprintln(os.Stderr)
|
||||
}
|
||||
|
||||
// display corrupted images
|
||||
hasError := false
|
||||
for pId, part := range epubParts {
|
||||
if pId == 0 && e.Image.HasCover && part.Cover.Error != nil {
|
||||
if pId == 0 && e.options.Image.HasCover && part.Cover.Error != nil {
|
||||
hasError = true
|
||||
fmt.Fprintf(os.Stderr, "Error on image %s: %v\n", filepath.Join(part.Cover.Path, part.Cover.Name), part.Cover.Error)
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user