mirror of
https://github.com/celogeek/go-comic-converter.git
synced 2025-05-24 15:52:38 +02:00
Compare commits
4 Commits
55b8bbe6b3
...
8beb5c114b
Author | SHA1 | Date | |
---|---|---|---|
8beb5c114b | |||
7a761178e8 | |||
cd34e4b962 | |||
db58244946 |
@ -15,11 +15,11 @@ import (
|
||||
"text/template"
|
||||
"time"
|
||||
|
||||
epubimage "github.com/celogeek/go-comic-converter/v2/pkg/epub/image"
|
||||
epubimageprocessor "github.com/celogeek/go-comic-converter/v2/pkg/epub/imageprocessor"
|
||||
epubtemplates "github.com/celogeek/go-comic-converter/v2/pkg/epub/templates"
|
||||
"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/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"
|
||||
@ -36,8 +36,8 @@ type ePub struct {
|
||||
}
|
||||
|
||||
type epubPart struct {
|
||||
Cover *epubimage.Image
|
||||
Images []*epubimage.Image
|
||||
Cover *epubimage.EPUBImage
|
||||
Images []*epubimage.EPUBImage
|
||||
Reader *zip.ReadCloser
|
||||
}
|
||||
|
||||
@ -71,7 +71,7 @@ func (e *ePub) render(templateString string, data map[string]any) string {
|
||||
}
|
||||
|
||||
// write image to the zip
|
||||
func (e *ePub) writeImage(wz *epubzip.EPUBZip, img *epubimage.Image, zipImg *zip.File) error {
|
||||
func (e *ePub) writeImage(wz *epubzip.EPUBZip, img *epubimage.EPUBImage, zipImg *zip.File) error {
|
||||
err := wz.WriteContent(
|
||||
img.EPUBPagePath(),
|
||||
[]byte(e.render(epubtemplates.Text, map[string]any{
|
||||
@ -89,7 +89,7 @@ func (e *ePub) writeImage(wz *epubzip.EPUBZip, img *epubimage.Image, zipImg *zip
|
||||
}
|
||||
|
||||
// write blank page
|
||||
func (e *ePub) writeBlank(wz *epubzip.EPUBZip, img *epubimage.Image) error {
|
||||
func (e *ePub) writeBlank(wz *epubzip.EPUBZip, img *epubimage.EPUBImage) error {
|
||||
return wz.WriteContent(
|
||||
img.EPUBSpacePath(),
|
||||
[]byte(e.render(epubtemplates.Blank, map[string]any{
|
||||
@ -100,7 +100,7 @@ func (e *ePub) writeBlank(wz *epubzip.EPUBZip, img *epubimage.Image) error {
|
||||
}
|
||||
|
||||
// write title image
|
||||
func (e *ePub) writeCoverImage(wz *epubzip.EPUBZip, img *epubimage.Image, part, totalParts int) error {
|
||||
func (e *ePub) writeCoverImage(wz *epubzip.EPUBZip, img *epubimage.EPUBImage, part, totalParts int) error {
|
||||
title := "Cover"
|
||||
text := ""
|
||||
if totalParts > 1 {
|
||||
@ -143,7 +143,7 @@ func (e *ePub) writeCoverImage(wz *epubzip.EPUBZip, img *epubimage.Image, part,
|
||||
}
|
||||
|
||||
// write title image
|
||||
func (e *ePub) writeTitleImage(wz *epubzip.EPUBZip, img *epubimage.Image, title string) error {
|
||||
func (e *ePub) writeTitleImage(wz *epubzip.EPUBZip, img *epubimage.EPUBImage, title string) error {
|
||||
titleAlign := ""
|
||||
if !e.Image.View.PortraitOnly {
|
||||
if e.Image.Manga {
|
||||
@ -240,7 +240,7 @@ func (e *ePub) getParts() (parts []*epubPart, imgStorage *epubzip.EPUBZipImageRe
|
||||
baseSize := uint64(128*1024) + imgStorage.Size(cover.EPUBImgPath())*2
|
||||
|
||||
currentSize := baseSize
|
||||
currentImages := make([]*epubimage.Image, 0)
|
||||
currentImages := make([]*epubimage.EPUBImage, 0)
|
||||
part := 1
|
||||
|
||||
for _, img := range images {
|
||||
@ -252,7 +252,7 @@ func (e *ePub) getParts() (parts []*epubPart, imgStorage *epubzip.EPUBZipImageRe
|
||||
})
|
||||
part += 1
|
||||
currentSize = baseSize
|
||||
currentImages = make([]*epubimage.Image, 0)
|
||||
currentImages = make([]*epubimage.EPUBImage, 0)
|
||||
}
|
||||
currentSize += imgSize
|
||||
currentImages = append(currentImages, img)
|
||||
@ -270,7 +270,7 @@ func (e *ePub) getParts() (parts []*epubPart, imgStorage *epubzip.EPUBZipImageRe
|
||||
// create a tree from the directories.
|
||||
//
|
||||
// this is used to simulate the toc.
|
||||
func (e *ePub) getTree(images []*epubimage.Image, skip_files bool) string {
|
||||
func (e *ePub) getTree(images []*epubimage.EPUBImage, skip_files bool) string {
|
||||
t := epubtree.New()
|
||||
for _, img := range images {
|
||||
if skip_files {
|
||||
@ -350,7 +350,7 @@ func (e *ePub) Write() error {
|
||||
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))
|
||||
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))
|
||||
}
|
||||
|
@ -1,23 +0,0 @@
|
||||
/*
|
||||
Templates use to create xml files of the EPUB.
|
||||
*/
|
||||
package epubtemplates
|
||||
|
||||
import _ "embed"
|
||||
|
||||
var (
|
||||
//go:embed "epub_templates_container.xml.tmpl"
|
||||
Container string
|
||||
|
||||
//go:embed "epub_templates_applebooks.xml.tmpl"
|
||||
AppleBooks string
|
||||
|
||||
//go:embed "epub_templates_style.css.tmpl"
|
||||
Style string
|
||||
|
||||
//go:embed "epub_templates_text.xhtml.tmpl"
|
||||
Text string
|
||||
|
||||
//go:embed "epub_templates_blank.xhtml.tmpl"
|
||||
Blank string
|
||||
)
|
@ -9,7 +9,7 @@ import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
type Image struct {
|
||||
type EPUBImage struct {
|
||||
Id int
|
||||
Part int
|
||||
Raw image.Image
|
||||
@ -27,47 +27,47 @@ type Image struct {
|
||||
}
|
||||
|
||||
// key name of the blank plage after the image
|
||||
func (i *Image) SpaceKey() string {
|
||||
func (i *EPUBImage) SpaceKey() string {
|
||||
return fmt.Sprintf("space_%d", i.Id)
|
||||
}
|
||||
|
||||
// path of the blank page
|
||||
func (i *Image) SpacePath() string {
|
||||
func (i *EPUBImage) SpacePath() string {
|
||||
return fmt.Sprintf("Text/%s.xhtml", i.SpaceKey())
|
||||
}
|
||||
|
||||
// path of the blank page into the EPUB
|
||||
func (i *Image) EPUBSpacePath() string {
|
||||
func (i *EPUBImage) EPUBSpacePath() string {
|
||||
return fmt.Sprintf("OEBPS/%s", i.SpacePath())
|
||||
}
|
||||
|
||||
// key for page
|
||||
func (i *Image) PageKey() string {
|
||||
func (i *EPUBImage) PageKey() string {
|
||||
return fmt.Sprintf("page_%d_p%d", i.Id, i.Part)
|
||||
}
|
||||
|
||||
// page path linked to the image
|
||||
func (i *Image) PagePath() string {
|
||||
func (i *EPUBImage) PagePath() string {
|
||||
return fmt.Sprintf("Text/%s.xhtml", i.PageKey())
|
||||
}
|
||||
|
||||
// page path into the EPUB
|
||||
func (i *Image) EPUBPagePath() string {
|
||||
func (i *EPUBImage) EPUBPagePath() string {
|
||||
return fmt.Sprintf("OEBPS/%s", i.PagePath())
|
||||
}
|
||||
|
||||
// key for image
|
||||
func (i *Image) ImgKey() string {
|
||||
func (i *EPUBImage) ImgKey() string {
|
||||
return fmt.Sprintf("img_%d_p%d", i.Id, i.Part)
|
||||
}
|
||||
|
||||
// image path
|
||||
func (i *Image) ImgPath() string {
|
||||
func (i *EPUBImage) ImgPath() string {
|
||||
return fmt.Sprintf("Images/%s.%s", i.ImgKey(), i.Format)
|
||||
}
|
||||
|
||||
// image path into the EPUB
|
||||
func (i *Image) EPUBImgPath() string {
|
||||
func (i *EPUBImage) EPUBImgPath() string {
|
||||
return fmt.Sprintf("OEBPS/%s", i.ImgPath())
|
||||
}
|
||||
|
||||
@ -75,7 +75,7 @@ func (i *Image) EPUBImgPath() string {
|
||||
//
|
||||
// center by default.
|
||||
// align to left or right if it's part of the splitted double page.
|
||||
func (i *Image) ImgStyle(viewWidth, viewHeight int, align string) string {
|
||||
func (i *EPUBImage) ImgStyle(viewWidth, viewHeight int, align string) string {
|
||||
relWidth, relHeight := i.RelSize(viewWidth, viewHeight)
|
||||
marginW, marginH := float64(viewWidth-relWidth)/2, float64(viewHeight-relHeight)/2
|
||||
|
||||
@ -100,7 +100,7 @@ func (i *Image) ImgStyle(viewWidth, viewHeight int, align string) string {
|
||||
return strings.Join(style, "; ")
|
||||
}
|
||||
|
||||
func (i *Image) RelSize(viewWidth, viewHeight int) (relWidth, relHeight int) {
|
||||
func (i *EPUBImage) RelSize(viewWidth, viewHeight int) (relWidth, relHeight int) {
|
||||
w, h := viewWidth, viewHeight
|
||||
srcw, srch := i.Width, i.Height
|
||||
|
@ -6,13 +6,12 @@ package epubimageprocessor
|
||||
import (
|
||||
"fmt"
|
||||
"image"
|
||||
"image/color"
|
||||
"image/draw"
|
||||
"os"
|
||||
"sync"
|
||||
|
||||
epubimage "github.com/celogeek/go-comic-converter/v2/pkg/epub/image"
|
||||
epubimagefilters "github.com/celogeek/go-comic-converter/v2/pkg/epub/imagefilters"
|
||||
"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/pkg/epuboptions"
|
||||
"github.com/celogeek/go-comic-converter/v2/pkg/epubprogress"
|
||||
"github.com/celogeek/go-comic-converter/v2/pkg/epubzip"
|
||||
@ -28,8 +27,8 @@ func New(o *epuboptions.EPUBOptions) *EPUBImageProcessor {
|
||||
}
|
||||
|
||||
// extract and convert images
|
||||
func (e *EPUBImageProcessor) Load() (images []*epubimage.Image, err error) {
|
||||
images = make([]*epubimage.Image, 0)
|
||||
func (e *EPUBImageProcessor) Load() (images []*epubimage.EPUBImage, err error) {
|
||||
images = make([]*epubimage.EPUBImage, 0)
|
||||
imageCount, imageInput, err := e.load()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@ -38,7 +37,7 @@ func (e *EPUBImageProcessor) Load() (images []*epubimage.Image, err error) {
|
||||
// dry run, skip convertion
|
||||
if e.Dry {
|
||||
for img := range imageInput {
|
||||
images = append(images, &epubimage.Image{
|
||||
images = append(images, &epubimage.EPUBImage{
|
||||
Id: img.Id,
|
||||
Path: img.Path,
|
||||
Name: img.Name,
|
||||
@ -49,7 +48,7 @@ func (e *EPUBImageProcessor) Load() (images []*epubimage.Image, err error) {
|
||||
return images, nil
|
||||
}
|
||||
|
||||
imageOutput := make(chan *epubimage.Image)
|
||||
imageOutput := make(chan *epubimage.EPUBImage)
|
||||
|
||||
// processing
|
||||
bar := epubprogress.New(epubprogress.Options{
|
||||
@ -86,7 +85,7 @@ func (e *EPUBImageProcessor) Load() (images []*epubimage.Image, err error) {
|
||||
raw = dst
|
||||
}
|
||||
|
||||
img := &epubimage.Image{
|
||||
img := &epubimage.EPUBImage{
|
||||
Id: input.Id,
|
||||
Part: part,
|
||||
Raw: raw,
|
||||
@ -289,55 +288,3 @@ func (e *EPUBImageProcessor) transformImage(src image.Image, srcId int) []image.
|
||||
|
||||
return images
|
||||
}
|
||||
|
||||
type CoverTitleDataOptions struct {
|
||||
Src image.Image
|
||||
Name string
|
||||
Text string
|
||||
Align string
|
||||
PctWidth int
|
||||
PctMargin int
|
||||
MaxFontSize int
|
||||
BorderSize int
|
||||
}
|
||||
|
||||
func (e *EPUBImageProcessor) Cover16LevelOfGray(bounds image.Rectangle) draw.Image {
|
||||
return image.NewPaletted(bounds, color.Palette{
|
||||
color.Gray{0x00},
|
||||
color.Gray{0x11},
|
||||
color.Gray{0x22},
|
||||
color.Gray{0x33},
|
||||
color.Gray{0x44},
|
||||
color.Gray{0x55},
|
||||
color.Gray{0x66},
|
||||
color.Gray{0x77},
|
||||
color.Gray{0x88},
|
||||
color.Gray{0x99},
|
||||
color.Gray{0xAA},
|
||||
color.Gray{0xBB},
|
||||
color.Gray{0xCC},
|
||||
color.Gray{0xDD},
|
||||
color.Gray{0xEE},
|
||||
color.Gray{0xFF},
|
||||
})
|
||||
}
|
||||
|
||||
// create a title page with the cover
|
||||
func (e *EPUBImageProcessor) CoverTitleData(o *CoverTitleDataOptions) (*epubzip.EPUBZipImage, error) {
|
||||
// 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 {
|
||||
dst = e.Cover16LevelOfGray(o.Src.Bounds())
|
||||
} else {
|
||||
dst = e.createImage(o.Src, g.Bounds(o.Src.Bounds()))
|
||||
}
|
||||
g.Draw(dst, o.Src)
|
||||
|
||||
return epubzip.CompressImage(
|
||||
fmt.Sprintf("OEBPS/Images/%s.%s", o.Name, e.Image.Format),
|
||||
e.Image.Format,
|
||||
dst,
|
||||
e.Image.Quality,
|
||||
)
|
||||
}
|
@ -0,0 +1,61 @@
|
||||
package epubimageprocessor
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"image"
|
||||
"image/color"
|
||||
"image/draw"
|
||||
|
||||
"github.com/celogeek/go-comic-converter/v2/pkg/epubimagefilters"
|
||||
"github.com/celogeek/go-comic-converter/v2/pkg/epubzip"
|
||||
"github.com/disintegration/gift"
|
||||
)
|
||||
|
||||
type CoverTitleDataOptions struct {
|
||||
Src image.Image
|
||||
Name string
|
||||
Text string
|
||||
Align string
|
||||
PctWidth int
|
||||
PctMargin int
|
||||
MaxFontSize int
|
||||
BorderSize int
|
||||
}
|
||||
|
||||
// create a title page with the cover
|
||||
func (e *EPUBImageProcessor) CoverTitleData(o *CoverTitleDataOptions) (*epubzip.EPUBZipImage, error) {
|
||||
// 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 {
|
||||
// 16 shade of gray
|
||||
dst = image.NewPaletted(o.Src.Bounds(), color.Palette{
|
||||
color.Gray{0x00},
|
||||
color.Gray{0x11},
|
||||
color.Gray{0x22},
|
||||
color.Gray{0x33},
|
||||
color.Gray{0x44},
|
||||
color.Gray{0x55},
|
||||
color.Gray{0x66},
|
||||
color.Gray{0x77},
|
||||
color.Gray{0x88},
|
||||
color.Gray{0x99},
|
||||
color.Gray{0xAA},
|
||||
color.Gray{0xBB},
|
||||
color.Gray{0xCC},
|
||||
color.Gray{0xDD},
|
||||
color.Gray{0xEE},
|
||||
color.Gray{0xFF},
|
||||
})
|
||||
} else {
|
||||
dst = e.createImage(o.Src, g.Bounds(o.Src.Bounds()))
|
||||
}
|
||||
g.Draw(dst, o.Src)
|
||||
|
||||
return epubzip.CompressImage(
|
||||
fmt.Sprintf("OEBPS/Images/%s.%s", o.Name, e.Image.Format),
|
||||
e.Image.Format,
|
||||
dst,
|
||||
e.Image.Quality,
|
||||
)
|
||||
}
|
23
pkg/epubtemplates/epubtemplates.go
Normal file
23
pkg/epubtemplates/epubtemplates.go
Normal file
@ -0,0 +1,23 @@
|
||||
/*
|
||||
Templates use to create xml files of the EPUB.
|
||||
*/
|
||||
package epubtemplates
|
||||
|
||||
import _ "embed"
|
||||
|
||||
var (
|
||||
//go:embed "epubtemplates_container.xml.tmpl"
|
||||
Container string
|
||||
|
||||
//go:embed "epubtemplates_applebooks.xml.tmpl"
|
||||
AppleBooks string
|
||||
|
||||
//go:embed "epubtemplates_style.css.tmpl"
|
||||
Style string
|
||||
|
||||
//go:embed "epubtemplates_text.xhtml.tmpl"
|
||||
Text string
|
||||
|
||||
//go:embed "epubtemplates_blank.xhtml.tmpl"
|
||||
Blank string
|
||||
)
|
@ -4,7 +4,7 @@ import (
|
||||
"fmt"
|
||||
|
||||
"github.com/beevik/etree"
|
||||
epubimage "github.com/celogeek/go-comic-converter/v2/pkg/epub/image"
|
||||
"github.com/celogeek/go-comic-converter/v2/pkg/epubimage"
|
||||
"github.com/celogeek/go-comic-converter/v2/pkg/epuboptions"
|
||||
)
|
||||
|
||||
@ -16,8 +16,8 @@ type ContentOptions struct {
|
||||
Publisher string
|
||||
UpdatedAt string
|
||||
ImageOptions *epuboptions.Image
|
||||
Cover *epubimage.Image
|
||||
Images []*epubimage.Image
|
||||
Cover *epubimage.EPUBImage
|
||||
Images []*epubimage.EPUBImage
|
||||
Current int
|
||||
Total int
|
||||
}
|
||||
@ -140,7 +140,7 @@ func getMeta(o *ContentOptions) []tag {
|
||||
|
||||
func getManifest(o *ContentOptions) []tag {
|
||||
var imageTags, pageTags, spaceTags []tag
|
||||
addTag := func(img *epubimage.Image, withSpace bool) {
|
||||
addTag := func(img *epubimage.EPUBImage, withSpace bool) {
|
||||
imageTags = append(imageTags,
|
||||
tag{"item", tagAttrs{"id": img.ImgKey(), "href": img.ImgPath(), "media-type": fmt.Sprintf("image/%s", o.ImageOptions.Format)}, ""},
|
||||
)
|
@ -5,11 +5,11 @@ import (
|
||||
"strings"
|
||||
|
||||
"github.com/beevik/etree"
|
||||
epubimage "github.com/celogeek/go-comic-converter/v2/pkg/epub/image"
|
||||
"github.com/celogeek/go-comic-converter/v2/pkg/epubimage"
|
||||
)
|
||||
|
||||
// create toc
|
||||
func Toc(title string, hasTitle bool, stripFirstDirectoryFromToc bool, images []*epubimage.Image) string {
|
||||
func Toc(title string, hasTitle bool, stripFirstDirectoryFromToc bool, images []*epubimage.EPUBImage) string {
|
||||
doc := etree.NewDocument()
|
||||
doc.CreateProcInst("xml", `version="1.0" encoding="UTF-8"`)
|
||||
doc.CreateDirective("DOCTYPE html")
|
Loading…
x
Reference in New Issue
Block a user