Skip to content

SVG: resolve a relative font-size against the inherited one - #2155

Merged
andersonhc merged 5 commits into
py-pdf:masterfrom
RavSinghChandan:svg-relative-font-size
Nov 12, 2026
Merged

andersonhc merged 5 commits into
py-pdf:masterfrom
RavSinghChandan:svg-relative-font-size

Conversation

@RavSinghChandan

Copy link
Copy Markdown

A font-size in em, ex or % fails with ValueError: 0.8em uses unsupported relative length em. spaCy's displaCy writes <text dy="1.25em" style="font-size: 0.8em"> for its dependency arc labels, under a root <svg style="font-size: 20px">, so its diagrams cannot be converted; seven of the displaCy images in spaCy's website (website/public/images/displacy*.svg) fail this way.

dx and dy in em were already supported. A new _resolve_relative_font_size() turns a relative font-size into an absolute one, against the inherited font size (16px if none, the default fpdf2 already uses for text), where a group, a <switch>, a <text> or a <tspan> merges its style over the inherited one. Descendants then inherit the computed size, as in CSS, so nested relative sizes multiply.

With this change, the seven displaCy images render.

Added test_svg_relative_font_size (displaCy's case, a percentage, nested em, ex in a <tspan>, and inheritance from a group); it fails on master. The full test suite passes except test_image_with_explicit_dimensions, which also fails on master in my environment; black, pylint, mypy and pyright pass

Comment thread fpdf/svg.py Resolved
@RavSinghChandan

Copy link
Copy Markdown
Author

Thanks, you're right: _get_pattern_paint() collects the font properties of the pattern's ancestors as they are written, so the root's font-size="2em" reached build_group() still relative. Each ancestor's font-size is now resolved against the size inherited so far, as in build_group(). Your SVG works now, and test_svg_relative_font_size_in_pattern covers it, plus a text in the pattern under 2em and 50% ancestors, which gets 6pt; both fail without the change. Rebased on master.

Comment thread test/svg/test_svg.py Resolved
@andersonhc
andersonhc merged commit ed8b51f into py-pdf:master Nov 12, 2026
41 of 42 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants