Skip to content

Resolve em and ex in shape geometry against the font size of the element - #549

Merged
deeplook merged 1 commit into
deeplook:mainfrom
RavSinghChandan:em-length-font-size
Nov 13, 2026
Merged

deeplook merged 1 commit into
deeplook:mainfrom
RavSinghChandan:em-length-font-size

Conversation

@RavSinghChandan

Copy link
Copy Markdown
Contributor

em and ex in the geometry of a shape always use 16px, whatever the font-size:

<g font-size="10px">
  <rect width="2em" height="10"/>   <!-- 32 user units wide, 20 in browsers -->
</g>

convert_length_attrs() uses 16px for em unless the caller passes a base, and only text does. It now uses the font size of the node, from findFontSize(), when no base is given and one of the values is in em or ex. For the root <svg>, which is an lxml element without a parent, it uses root_font_size, so <svg width="1em" font-size="20px"> is 15pt wide instead of 12pt.

Lengths without em/ex don't call findFontSize(), so nothing changes for them. Tests: test_em_uses_font_size_of_shape (inherited and own font-size, em and ex) and test_em_on_root_uses_root_font_size; both fail without the change.

width="2em" in a group with font-size="10px" was 32 user units instead
of 20: convert_length_attrs() used 16px for em unless the caller passed
a base, and only text did. The root <svg> now uses its own font-size
for width and height in em too.
@deeplook
deeplook merged commit fd7e180 into deeplook:main Nov 13, 2026
16 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