← Back to Pull requests

feat(usvgr): measure text from shaping alone and share shaped glyphs with the layout #26

Open
unknown opened this pull request with 347 lines diff across 4 files
View on GitHub
unknown unknown commented on Nov 15, 2026 01:49 UTC
Author

Follow-up to your review of dmtrKovalenko/fframes#256: measuring text there built a whole Tree. This adds what fframes needs to measure with kerning and layout only, and lets the layout reuse that work.

  • usvgr::text_advance(text, &font, font_size, &resolver, &fontdb, cache) returns the advance width of a string. It runs the shaping the layout runs (bidi runs, kerning, font variations, automatic opsz, fallback fonts) and nothing else: no XML, no tree, no outlines.

  • UsvgrTextShapeCache keeps shaped glyphs per (text, font, size, features). text_advance and layout_text read and fill the same cache, so text that was measured is not shaped again when it is converted, and text drawn at a new position on every frame is shaped once. The handle is cheap to clone. Cache::new_with_text_caches and the public usvgr_shape_cache field attach it to a converter cache.

  • Font::new builds a font the way a <text> element does. The mapping of weight, stretch and style to the wght, wdth, ital and slnt axes moved there from the parser, unchanged.

  • In process_chunk the first span's glyphs stay shared with the cache and are copied only when another span overwrites some of them.
  • Without a shape cache nothing changes: Cache::default() has none.

    Numbers (Ryzen 7 9800X3D, release, Inter ExtraBold 46 px, measured from fframes):

    | Case | Tree per measurement (#256) | This change |
    |---|---|---|
    | Measure a 38-character line, nothing cached | 118.6 µs | 30.2 µs |
    | Measure the same line again | 118.6 µs | 0.10 µs |
    | Measure the 21 words of a caption, per frame | 1069 µs | 3.2 µs |
    | Convert a frame of 21 words that move every frame | 519 µs | 135 µs with the shared cache |

    Tests: five unit tests in text/mod.rs. The advance equals what the layout gives for kerned Latin, Arabic through a fallback font, mixed direction, marks, and Roboto Flex at weights 100 and 900. Measured text is not shaped again by a conversion that holds the same cache.

    Checked: cargo test -p usvgr and the 1705 render tests in svgr pass. svgr-capi does not compile and two svgrtypes tests fail on main as well, so I left them out.

    <!-- This is an auto-generated comment: release notes by coderabbit.ai -->

    <!-- coderabbit-ai-generated:v1 -->

    Summary by CodeRabbit

    New Features
    Added text-advance measurement, including support for empty text and reporting when font selection or shaping fails.
    Added an optional shared cache for shaped text, allowing text measurement and rendering to reuse results.
    Variable-font settings now derive default weight, width, and style axes from font properties while retaining explicitly specified axis values.

    <!-- end of auto-generated comment: release notes by coderabbit.ai -->

    Diff: 347 lines (+260 / -87) across 4 files Reviewer: coderabbitai[bot] Review duration: 10m
    All Vouch review confidence checks passed
    Residual risk 0.24 = Change Risk (0.42) × (1 − Review Confidence (0.43)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.42 · 347 lines diff across 4 files
    0.42
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.02 · 1 substantive comment(s)
    0.02
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    42%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.350
    Shallow Review Depth shallow_review_depth
    0.344
    Real PR Conversation Timeline
    1 comments
    "Review active on #26"
    general weight: 0.35
    Assignees
    Unassigned
    Labels
    risk: low confidence: 43.0% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 42.4% (MEDIUM) · Higher = larger blast radius
    42.4%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 43.0% · Lower = rushed or shallow review
    43.0%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 24.2% · Safe to merge
    24.2%