From caea4b6ed58b2d555b8b6e0c241f3cf216d0f25d Mon Sep 17 00:00:00 2001 From: Leo Gaskin Date: Thu, 26 Dec 2024 19:46:30 +0100 Subject: [PATCH] Change "--autosplit" option to "--widepage" --- README.md | 6 +++--- cmd/business.go | 2 +- cmd/custom_args.go | 32 ++++++++++++++++------------ cmd/formats/kindle/crop_and_split.go | 25 ++++++++++++++-------- cmd/formats/kindle/mobi.go | 4 ++-- cmd/root.go | 4 ++-- 6 files changed, 42 insertions(+), 31 deletions(-) diff --git a/README.md b/README.md index 012f306..48c1763 100644 --- a/README.md +++ b/README.md @@ -75,14 +75,14 @@ This may be useful if your e-reader has a small screen. kojirou d86cf65b-5f6c-437d-a0af-19a31f94ec55 -l en --autocrop ``` -### Split panorama pages automatically +### Split wide pages automatically Kojirou has the ability to split panorama pages into two separate pages for better viewing. It is also possible to include both the split pages and the original page. -Legal arguments to this option are "preserve", "split" and "both". +Legal arguments to this option are "preserve", "split", "preserve-and-split" and "split-and-preserve". ``` shell -kojirou d86cf65b-5f6c-437d-a0af-19a31f94ec55 -l en --autosplit=both +kojirou d86cf65b-5f6c-437d-a0af-19a31f94ec55 -l en --widepage=preserve-and-split ``` ### Change reading direction diff --git a/cmd/business.go b/cmd/business.go index 97b3130..1bd772b 100644 --- a/cmd/business.go +++ b/cmd/business.go @@ -60,7 +60,7 @@ func handleVolume(skeleton md.Manga, volume md.Volume, dir kindle.NormalizedDire mangaForVolume := skeleton.WithChapters(volume.Sorted()).WithPages(pages) mobi := kindle.GenerateMOBI( mangaForVolume, - kindle.AutosplitPolicy(autosplitArg), + kindle.WidepagePolicy(widepageArg), autocropArg, leftToRightArg, ) diff --git a/cmd/custom_args.go b/cmd/custom_args.go index 1f1923d..f313315 100644 --- a/cmd/custom_args.go +++ b/cmd/custom_args.go @@ -41,29 +41,33 @@ func (p *DataSaverPolicyArg) Type() string { return "data-saver policy" } -type AutosplitPolicyArg kindle.AutosplitPolicy +type WidepagePolicyArg kindle.WidepagePolicy -func (p *AutosplitPolicyArg) String() string { - switch kindle.AutosplitPolicy(*p) { - case kindle.AutosplitPolicyPreserve: +func (p *WidepagePolicyArg) String() string { + switch kindle.WidepagePolicy(*p) { + case kindle.WidepagePolicyPreserve: return "preserve" - case kindle.AutosplitPolicySplit: + case kindle.WidepagePolicySplit: return "split" - case kindle.AutosplitPolicyBoth: - return "both" + case kindle.WidepagePolicyPreserveAndSplit: + return "preserve-and-split" + case kindle.WidepagePolicySplitAndPreserve: + return "split-and-preserve" default: panic("unreachable") } } -func (p *AutosplitPolicyArg) Set(v string) error { +func (p *WidepagePolicyArg) Set(v string) error { switch v { case "preserve": - *p = AutosplitPolicyArg(kindle.AutosplitPolicyPreserve) + *p = WidepagePolicyArg(kindle.WidepagePolicyPreserve) case "split": - *p = AutosplitPolicyArg(kindle.AutosplitPolicySplit) - case "both": - *p = AutosplitPolicyArg(kindle.AutosplitPolicyBoth) + *p = WidepagePolicyArg(kindle.WidepagePolicySplit) + case "preserve-and-split": + *p = WidepagePolicyArg(kindle.WidepagePolicyPreserveAndSplit) + case "split-and-preserve": + *p = WidepagePolicyArg(kindle.WidepagePolicySplitAndPreserve) default: return fmt.Errorf(`must be one of: "preserve", "split", or "both"`) } @@ -71,6 +75,6 @@ func (p *AutosplitPolicyArg) Set(v string) error { return nil } -func (p *AutosplitPolicyArg) Type() string { - return "auto-split policy" +func (p *WidepagePolicyArg) Type() string { + return "wide-page policy" } diff --git a/cmd/formats/kindle/crop_and_split.go b/cmd/formats/kindle/crop_and_split.go index 914d4d8..b20a0a4 100644 --- a/cmd/formats/kindle/crop_and_split.go +++ b/cmd/formats/kindle/crop_and_split.go @@ -6,15 +6,16 @@ import ( "github.com/leotaku/kojirou/cmd/crop" ) -type AutosplitPolicy int +type WidepagePolicy int const ( - AutosplitPolicyPreserve AutosplitPolicy = iota - AutosplitPolicySplit - AutosplitPolicyBoth + WidepagePolicyPreserve WidepagePolicy = iota + WidepagePolicySplit + WidepagePolicyPreserveAndSplit + WidepagePolicySplitAndPreserve ) -func cropAndSplit(img image.Image, autosplit AutosplitPolicy, autocrop bool, ltr bool) []image.Image { +func cropAndSplit(img image.Image, widepage WidepagePolicy, autocrop bool, ltr bool) []image.Image { if autocrop { croppedImg, err := crop.Crop(img, crop.Bounds(img)) if err != nil { @@ -23,20 +24,26 @@ func cropAndSplit(img image.Image, autosplit AutosplitPolicy, autocrop bool, ltr img = croppedImg } - if autosplit != AutosplitPolicyPreserve && crop.ShouldSplit(img) { + if widepage != WidepagePolicyPreserve && crop.ShouldSplit(img) { left, right, err := crop.Split(img) if err != nil { panic("unsupported image type for splitting") } - switch autosplit { - case AutosplitPolicySplit: + switch widepage { + case WidepagePolicySplit: if ltr { return []image.Image{left, right} } else { return []image.Image{right, left} } - case AutosplitPolicyBoth: + case WidepagePolicyPreserveAndSplit: + if ltr { + return []image.Image{img, left, right} + } else { + return []image.Image{img, right, left} + } + case WidepagePolicySplitAndPreserve: if ltr { return []image.Image{left, right, img} } else { diff --git a/cmd/formats/kindle/mobi.go b/cmd/formats/kindle/mobi.go index f1d5afd..9d78b7d 100644 --- a/cmd/formats/kindle/mobi.go +++ b/cmd/formats/kindle/mobi.go @@ -32,7 +32,7 @@ img { var pageTemplate = template.Must(template.New("page").Parse(pageTemplateString)) -func GenerateMOBI(manga mangadex.Manga, split AutosplitPolicy, crop bool, ltr bool) mobi.Book { +func GenerateMOBI(manga mangadex.Manga, widepage WidepagePolicy, crop bool, ltr bool) mobi.Book { chapters := make([]mobi.Chapter, 0) images := make([]image.Image, 0) pageImageIndex := 1 @@ -43,7 +43,7 @@ func GenerateMOBI(manga mangadex.Manga, split AutosplitPolicy, crop bool, ltr bo groupNames = append(groupNames, chap.Info.GroupNames...) pages := make([]string, 0) for _, img := range chap.Sorted() { - images = append(images, cropAndSplit(img, split, crop, ltr)...) + images = append(images, cropAndSplit(img, widepage, crop, ltr)...) pages = append(pages, templateToString(pageTemplate, records.To32(pageImageIndex))) pageImageIndex++ } diff --git a/cmd/root.go b/cmd/root.go index 0fef7fa..ffa4171 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -12,7 +12,7 @@ var ( languageArg string rankArg string autocropArg bool - autosplitArg AutosplitPolicyArg + widepageArg WidepagePolicyArg kindleFolderModeArg bool dryRunArg bool outArg string @@ -170,7 +170,7 @@ func init() { rootCmd.Flags().StringVarP(&languageArg, "language", "l", "en", "language for chapter downloads") rootCmd.Flags().StringVarP(&rankArg, "rank", "r", "most", "chapter ranking method to use") rootCmd.Flags().BoolVarP(&autocropArg, "autocrop", "a", false, "crop whitespace from pages automatically") - rootCmd.Flags().VarP(&autosplitArg, "autosplit", "w", "split wide pages automatically") + rootCmd.Flags().VarP(&widepageArg, "widepage", "w", "split wide pages automatically") rootCmd.Flags().BoolVarP(&kindleFolderModeArg, "kindle-folder-mode", "k", false, "generate folder structure for Kindle devices") rootCmd.Flags().BoolVarP(&leftToRightArg, "left-to-right", "p", false, "make reading direction left to right") rootCmd.Flags().IntVarP(&fillVolumeNumberArg, "fill-volume-number", "n", 0, "fill volume number with leading zeros in title")