From 4b00f39ef019fbb175036bb227655bd0875121ce Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Mon, 6 Jan 2025 21:29:50 +0530 Subject: [PATCH] =?UTF-8?q?Conversion:=20Automatically=20set=20the=20page?= =?UTF-8?q?=20progression=20direction=20for=20books=20that=20do=20not=20ha?= =?UTF-8?q?ve=20it=20set=20and=20have=20their=20primary=20language=20eithe?= =?UTF-8?q?r=20Arabic=20or=20Hebrew.=20Fixes=20#2592=20(Automatically=20in?= =?UTF-8?q?serting=20page-progression-direction=3D"rtl"=20in=20rtl=20langu?= =?UTF-8?q?ages=20=E2=80=8B=E2=80=8B(Arabic=20and=20Hebrew))?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/calibre/ebooks/conversion/plugins/epub_output.py | 1 + src/calibre/ebooks/conversion/plugins/mobi_output.py | 2 ++ src/calibre/ebooks/oeb/base.py | 12 +++++++++++- src/calibre/utils/localization.py | 4 ++++ 4 files changed, 18 insertions(+), 1 deletion(-) diff --git a/src/calibre/ebooks/conversion/plugins/epub_output.py b/src/calibre/ebooks/conversion/plugins/epub_output.py index 4443e10409..39ce9c7b4c 100644 --- a/src/calibre/ebooks/conversion/plugins/epub_output.py +++ b/src/calibre/ebooks/conversion/plugins/epub_output.py @@ -205,6 +205,7 @@ class EPUBOutput(OutputFormatPlugin): from calibre.ebooks.oeb.transforms.filenames import UniqueFilenames UniqueFilenames()(oeb, opts) + self.oeb.set_page_progression_direction_if_needed() self.workaround_ade_quirks() self.workaround_webkit_quirks() self.upshift_markup() diff --git a/src/calibre/ebooks/conversion/plugins/mobi_output.py b/src/calibre/ebooks/conversion/plugins/mobi_output.py index 6afd976b69..15278d2838 100644 --- a/src/calibre/ebooks/conversion/plugins/mobi_output.py +++ b/src/calibre/ebooks/conversion/plugins/mobi_output.py @@ -188,6 +188,7 @@ class MOBIOutput(OutputFormatPlugin): mobi_type = 'old' # Amazon does not support KF8 periodicals create_kf8 = mobi_type in ('new', 'both') + self.oeb.set_page_progression_direction_if_needed() remove_html_cover(self.oeb, self.log) resources = Resources(oeb, opts, self.is_periodical, add_fonts=create_kf8) @@ -313,6 +314,7 @@ class AZW3Output(OutputFormatPlugin): self.oeb, self.opts, self.log = oeb, opts, log opts.mobi_periodical = self.is_periodical passthrough = getattr(opts, 'mobi_passthrough', False) + self.oeb.set_page_progression_direction_if_needed() remove_duplicate_anchors(oeb) resources = Resources(self.oeb, self.opts, self.is_periodical, diff --git a/src/calibre/ebooks/oeb/base.py b/src/calibre/ebooks/oeb/base.py index 04733e021a..27b9fda208 100644 --- a/src/calibre/ebooks/oeb/base.py +++ b/src/calibre/ebooks/oeb/base.py @@ -27,7 +27,7 @@ from calibre.translations.dynamic import translate from calibre.utils.cleantext import clean_xml_chars from calibre.utils.icu import numeric_sort_key from calibre.utils.icu import title_case as icu_title -from calibre.utils.localization import __ +from calibre.utils.localization import __, is_rtl_lang from calibre.utils.short_uuid import uuid4 from calibre.utils.xml_parse import safe_xml_fromstring from polyglot.builtins import codepoint_to_chr, iteritems, itervalues, string_or_bytes @@ -1823,6 +1823,16 @@ class OEBBook: self.auto_generated_toc = True self._temp_files = [] + def set_page_progression_direction_if_needed(self): + if not self.spine.page_progression_direction: + try: + lang = self.metadata.language[0].value + if is_rtl_lang(lang): + self.spine.page_progression_direction = 'rtl' + except Exception: + raise + pass + def clean_temp_files(self): for path in self._temp_files: try: diff --git a/src/calibre/utils/localization.py b/src/calibre/utils/localization.py index 75bb53936a..9d33a07f62 100644 --- a/src/calibre/utils/localization.py +++ b/src/calibre/utils/localization.py @@ -610,3 +610,7 @@ def localize_website_link(url): parts = list(parts) parts[2] = path return urlunparse(parts) + + +def is_rtl_lang(lang): + return lang[:2].lower() in ('ar', 'he')