Skip to content

Commit

Permalink
Change "--autosplit" option to "--widepage"
Browse files Browse the repository at this point in the history
  • Loading branch information
leotaku committed Dec 26, 2024
1 parent 478c89a commit caea4b6
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 31 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion cmd/business.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
Expand Down
32 changes: 18 additions & 14 deletions cmd/custom_args.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,36 +41,40 @@ 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"`)
}

return nil
}

func (p *AutosplitPolicyArg) Type() string {
return "auto-split policy"
func (p *WidepagePolicyArg) Type() string {
return "wide-page policy"
}
25 changes: 16 additions & 9 deletions cmd/formats/kindle/crop_and_split.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions cmd/formats/kindle/mobi.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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++
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ var (
languageArg string
rankArg string
autocropArg bool
autosplitArg AutosplitPolicyArg
widepageArg WidepagePolicyArg
kindleFolderModeArg bool
dryRunArg bool
outArg string
Expand Down Expand Up @@ -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")
Expand Down

0 comments on commit caea4b6

Please sign in to comment.