From 5d6d01b98944d4ad6077b99c4255423e3381abc7 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Thu, 31 Jul 2025 08:08:29 +0530 Subject: [PATCH] Cleanup previous PR --- src/calibre/ebooks/oeb/stylizer.py | 33 ++++++++++++++++++++---------- 1 file changed, 22 insertions(+), 11 deletions(-) diff --git a/src/calibre/ebooks/oeb/stylizer.py b/src/calibre/ebooks/oeb/stylizer.py index 8b62f2b829..4ae3631db5 100644 --- a/src/calibre/ebooks/oeb/stylizer.py +++ b/src/calibre/ebooks/oeb/stylizer.py @@ -115,6 +115,27 @@ class style_map(dict): self.important_properties = set() +def epub_prefix_properties() -> dict[str, tuple[str, str]]: + ans = getattr(epub_prefix_properties, 'ans', None) + if ans is None: + ans = {} + for unprefixed in ( + 'writing-mode', 'text-emphasis', 'text-emphasis-color', 'text-emphasis-position', 'text-emphasis-style' + ): + ans[f'-epub-{unprefixed}'] = f'-webkit-{unprefixed}', unprefixed + setattr(epub_prefix_properties, 'ans', ans) + return ans + + +def cleanup_epub_prefixed_properties(style: dict[str, str]) -> None: + epub_prefixed = epub_prefix_properties() + for x in tuple(filter(epub_prefixed.__contains__, style)): + val = style.pop(x) + for key in epub_prefixed[x]: + if key not in style: + style[key] = val + + class StylizerRules: def __init__(self, opts, profile, stylesheets): @@ -185,17 +206,7 @@ class StylizerRules: size = 'xx-small' if size in FONT_SIZE_NAMES: style['font-size'] = f'{self.profile.fnames[size]/float(self.profile.fbase):.1f}rem' - UNPREFIXED_PROPERTIES = ( - ['writing-mode'] + - [f'text-emphasis{x}' for x in ('', '-color', '-position', '-style')] - ) - for unprefixed_property in UNPREFIXED_PROPERTIES: - epub_property = f'-epub-{unprefixed_property}' - webkit_property = f'-webkit-{unprefixed_property}' - if epub_property in style: - value = style[epub_property] - style[unprefixed_property] = style.get(unprefixed_property, value) - style[webkit_property] = style.get(webkit_property, value) + cleanup_epub_prefixed_properties(style) return style def _apply_text_align(self, text):