Skip to content

Support RTL SVG text anchoring#2755

Open
moreaki wants to merge 2 commits into
Kozea:mainfrom
moreaki:svg-text-bidi-positioning
Open

Support RTL SVG text anchoring#2755
moreaki wants to merge 2 commits into
Kozea:mainfrom
moreaki:svg-text-bidi-positioning

Conversation

@moreaki
Copy link
Copy Markdown
Contributor

@moreaki moreaki commented Apr 22, 2026

What changed

SVG text now passes direction="ltr"/direction="rtl" to the Pango text layout used for SVG text drawing.

text-anchor handling also now follows the SVG inline-base direction:

  • LTR start keeps the current behavior and extends right from x.
  • RTL start treats x as the right edge and extends left.
  • RTL end treats x as the left edge and extends right.

This is a small step toward #106 and is stacked on #2754.

Why

SVG 2 defines the direction property as the inline-base direction for SVG text. It determines the start/end points used by text-anchor, and it can affect text positioning when bidi properties are involved:

https://svgwg.org/svg2-draft/text.html#DirectionProperty

Before this change, SVG direction attributes were cascaded but ignored when building the text layout style, so SVG text anchoring stayed effectively LTR.

Visual proof

Source SVG:

svg-rtl-anchor-proof.svg

Rendered PNG:

svg-rtl-anchor-proof.png

The HTML wrapper used to render the proof is:

<!doctype html>
<meta charset="utf-8">
<style>
  @page {
    size: 1180px 560px;
    margin: 0;
  }
  html,
  body {
    margin: 0;
  }
  img {
    display: block;
    width: 1180px;
    height: 560px;
  }
</style>
<img src="svg-rtl-anchor-proof.svg" alt="">

Scope

This does not implement full RTL/bidi support. In particular, it does not solve the SVG 2 mapping of per-character x, y, dx, dy, and rotate lists onto shaped visual glyph clusters after bidi reordering.

Tests

venv/bin/python -m pytest tests/draw/svg/test_text.py -q
venv/bin/python -m pytest tests/draw/svg -q
venv/bin/python -m ruff check weasyprint/svg/__init__.py weasyprint/svg/text.py tests/draw/svg/test_text.py

@moreaki moreaki marked this pull request as ready for review April 22, 2026 19:49
This was referenced Apr 22, 2026
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.

1 participant