diff --git a/src/calibre/ebooks/oeb/stylizer.py b/src/calibre/ebooks/oeb/stylizer.py index fdb5e44195..4f662ecdf8 100644 --- a/src/calibre/ebooks/oeb/stylizer.py +++ b/src/calibre/ebooks/oeb/stylizer.py @@ -428,10 +428,23 @@ class Stylizer: no_important_properties = frozenset() +svg_text_tags = tuple(map(SVG, ('text', 'textPath', 'tref', 'tspan'))) + + +def is_only_number(x: str) -> bool: + try: + float(x) + return True + except Exception: + return False + +def is_svg_text_tag(x): + return getattr(x, 'tag', '') in svg_text_tags class Style: MS_PAT = re.compile(r'^\s*(mso-|panose-|text-underline|tab-interval)') + viewport_relative_font_size: str = '' def __init__(self, element, stylizer): self._element = element @@ -628,6 +641,8 @@ class Style: base = self._profile.fbase if 'font-size' in self._style: size = self._style['font-size'] + if is_svg_text_tag(self._element) and (size.endswith('px') or is_only_number(size)): + self.viewport_relative_font_size = size result = normalize_fontsize(size, base) else: result = base diff --git a/src/calibre/ebooks/oeb/transforms/flatcss.py b/src/calibre/ebooks/oeb/transforms/flatcss.py index fae357e8d3..76ca6fd9e6 100644 --- a/src/calibre/ebooks/oeb/transforms/flatcss.py +++ b/src/calibre/ebooks/oeb/transforms/flatcss.py @@ -453,7 +453,9 @@ class CSSFlattener: if dp.tag and dp.tag.endswith('}div') and len(dp) == 1 and not dp.text: if stylizer.style(dp).cssdict().get('float', None) == 'left': is_drop_cap = True - if not self.context.disable_font_rescaling and not is_drop_cap: + if style.viewport_relative_font_size: + cssdict['font-size'] = style.viewport_relative_font_size + elif not self.context.disable_font_rescaling and not is_drop_cap: _sbase = self.sbase if self.sbase is not None else \ self.context.source.fbase dyn_rescale = node.attrib.pop('data-calibre-rescale', None)