Skip to content

fix(text): measure text width from shaping, shared with the renderer - #270

Draft
silouanwright wants to merge 6 commits into
dmtrKovalenko:mainfrom
silouanwright:feat/shaped-text-measure
Draft

silouanwright wants to merge 6 commits into
dmtrKovalenko:mainfrom
silouanwright:feat/shaped-text-measure

Conversation

@silouanwright

@silouanwright silouanwright commented Nov 15, 2026 •

Copy link
Copy Markdown
Contributor

Second attempt at #255, replacing #256 (merged by accident, reverted in #269). Draft: it needs dmtrKovalenko/svgr#26 and a usvgr release. Until then Cargo.toml points at that PR's branch, in a separate commit to drop.

The first commit re-applies #256 as it was merged, so the commits after it are what is new.

What changed since #256, following your review:

  • No usvgr::Tree is built to measure. text_width, text_fit and line breaking call usvgr::text_advance, which only shapes the text.
  • The shaped glyphs are shared with the renderer. TextCache owns a UsvgrTextShapeCache, and every converter cache is created from it (TextCache::converter_cache), so a string is shaped once for measuring and rendering.
  • The measuring helper is private. FontSource::measure_text is gone; a font source now only says which font database it uses (FontSource::fontdb, default None, which keeps per-character advances).
  • Captions measure through the frame's shape cache too.
  • Tabs and line breaks are measured as spaces. The SVG parser did that for fix(text): measure shaped text width instead of summing rounded advances #256; with no tree it is done by hand, with a test.

Widths are the same as in #256: its tests pass as they were, and the text-width example and its snapshots are untouched.

Nothing cached on either side:

Case #256 This PR
Measure a 38-character line 118.6 µs 30.2 µs
Measure one word 57.3 µs 18.3 µs
text_fit on a 113-character string (8 measurements) 1002 µs 256 µs

With the shared cache, converting a frame of 21 words that move on every frame goes from 519 µs to 135 µs. That is a best case: the outline cache is off, so shaping is the only work that can be reused.

Ryzen 7 9800X3D, release build, Inter ExtraBold 46 px. A timing loop in a test, not a Criterion benchmark; a full frame render was not measured.

Checked: cargo test -p fframes (105 passed), cargo clippy -p fframes -p text-width-example -- -D warnings and cargo fmt --check on Linux with Rust 1.100. Editor bridges for hello-world and teej-podcast check for wasm32. The e2e snapshots and the other editor bridges were not run.

Measuring no longer builds a usvgr tree. text_width, text_fit and line
breaking call usvgr::text_advance, which only shapes the text, and the
shaped glyphs go into a cache that every converter cache reads, so a
string is shaped once for measuring and rendering.

The measuring helper is private: a font source now only says which font
database it uses. Captions measure through the frame shape cache too.
Drop before merging, once usvgr with text_advance is released.
The SVG parser turns tabs and line breaks into spaces before the
renderer shapes the text. Shaping them directly gave a missing glyph
for a tab and no advance for a line break.
Drop before merging, once usvgr with text_advance is released.
@coderabbitai

coderabbitai Bot commented Nov 15, 2026

Copy link
Copy Markdown

Important

Draft PR not reviewed

Draft PRs are not automatically reviewed by default.

  • Trigger a manual review

To automatically review draft PRs, update your CodeRabbit configuration:

reviews:
  auto_review:
    drafts: true
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

The outro centres the "fframes" wordmark with text_width. Measuring
from shaping gives the exact advance, so the wordmark moves a few
pixels. The frame was rendered by CI on Linux with skia-cpu.
@silouanwright

Copy link
Copy Markdown
Contributor Author

Updated fframes-intro/outroscene_7s.png in f84c21a. The outro centres the wordmark with text_width, and exact widths move it a few pixels (about 2% of the frame). This frame already failed on main after #256, which never updated the snapshot. The new frame is the Linux skia-cpu render from CI; it is within 0.12% of the GPU render.

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