Skip to content

Fonts: shape long items in bounded pieces - #1702

Merged
dylanmtaylor merged 1 commit into
mainfrom
fix/sec-fonts-shaping-budget
Nov 3, 2026
Merged

dylanmtaylor merged 1 commit into
mainfrom
fix/sec-fonts-shaping-budget

Conversation

@dylanmtaylor

@dylanmtaylor dylanmtaylor commented Nov 3, 2026 •

Copy link
Copy Markdown
Member

What was wrong

shape_range handed a whole item to HarfRust in one call. HarfRust lets a call grow its buffers to 256 glyphs per input character (minimum 65,536), and an embedded font with a default-on ccmp MultipleSubst lookup decides how far each character expands. Every resulting glyph was then copied into the output vector. A long run of text in such a font could therefore allocate gigabytes during layout. HarfRust has no public way to set a lower limit.

What changed

  • Items longer than 1 KiB (MAX_SHAPE_CHUNK) are shaped in pieces. A cut goes at a space near the end of the window, otherwise before a character that starts a cluster: never before a combining mark (bidi class NSM) and never after a joiner (BN). Each piece is shaped with the rest of the paragraph as pre- and post-context, and with the script guessed for the whole item, so pieces agree. This bounds each HarfRust call to about 262k glyphs.
  • If a piece expands past 32 glyphs per character (MAX_GLYPHS_PER_CHAR), its output is dropped in favour of one glyph per character from the cmap. This bounds what is retained.
  • Right-to-left pieces are concatenated in reverse, so the output stays in visual order.
  • The cmap path is factored into cmap_glyphs and reused for the no-shaper case.

Blast radius

deckcraft-fonts shaping, used by text layout. Items up to 1 KiB, which covers almost all runs, are shaped exactly as before. Longer items may differ at cut points only where a cross-space feature such as kerning against a space would have applied.

Checks run

  • cargo test -p deckcraft-fonts (new tests: piece boundaries, coverage and limits; long LTR and RTL items keep every cluster in visual order)
  • cargo clippy -p deckcraft-fonts --all-targets -- -D warnings
  • cargo fmt -p deckcraft-fonts --check
  • cargo check -p deckcraft-text (the main caller)

Checks not run

  • Full cargo xtask ci and visual text checks; craft-fonts was not present, so only bundled fonts were exercised.
  • No crafted ccmp expansion font fixture is in the tree.

Addresses CodeRabbit security findings (CWE-409).

Summary by CodeRabbit

  • Bug Fixes
    • Improved rendering of long text, including right-to-left scripts, and added a fallback for text that cannot be shaped normally.

The shaper lets one call grow to 256 glyphs per input character, as an
embedded font's substitutions decide, and every glyph was then copied out.
Items longer than 1 KiB are now shaped in pieces (cut at a space where one
is near, never before a combining mark or after a joiner), each with the
rest of the paragraph as context and the item's script, and a piece that
expands past 32 glyphs per character falls back to one glyph per character
from the cmap.
@coderabbitai

coderabbitai Bot commented Nov 3, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

No actionable comments were generated in the recent review.

Walkthrough

Long text is now shaped in 1024-byte pieces. The shaping path preserves paragraph context, applies cmap fallback in specified cases, and combines piece output in visual order.

Changes: Long-text shaping

Layer / File(s) Summary
Chunk boundaries and limits
crates/fonts/src/shape.rs
Shaping uses a 1024-byte chunk limit and selects boundaries that prefer spaces or avoid splitting combining marks and joiners. Tests check chunk coverage, contiguity, size limits, and boundary behavior.
Piece shaping and fallback
crates/fonts/src/shape.rs
Each piece is shaped with paragraph context and a shared guessed script when needed. Pieces that exceed the glyph budget use cmap fallback; unavailable HarfRust shaping also uses cmap fallback. Output is combined in visual order. Tests check glyph counts and cluster order.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Change: Bug fix

Sequence Diagram(s)

sequenceDiagram
  participant shape_range
  participant Chunk selection
  participant HarfRust
  participant cmap_fallback
  shape_range->>Chunk selection: Split selected text into bounded pieces
  Chunk selection-->>shape_range: Return text pieces
  loop For each piece
    shape_range->>HarfRust: Shape piece with paragraph context and script
    HarfRust-->>shape_range: Return piece glyphs
    alt Piece exceeds glyph budget
      shape_range->>cmap_fallback: Map piece characters to cmap glyphs
      cmap_fallback-->>shape_range: Return fallback glyphs
    end
  end
  shape_range->>shape_range: Combine piece glyphs in visual order
Loading

Suggested reviewers: hamzaelkachtaf4-star

Merge Risk: Minimal · up to 783b3

No actionable issue has been established that should block merging after normal checks.

Pre-merge checks: Passed 4 · Failed 1

Failed Failed checks: 1 warning

Check name Status Explanation Resolution
Docstring Coverage Warning Docstring coverage is 75.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documenta… Write docstrings for the functions missing them to satisfy the coverage threshold.
Passed Passed checks: 4 passed
Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title "Fonts: shape long items in bounded pieces" names the main change. The PR makes shape_range split items longer than 1024 bytes into bounded pieces, which the summary and objectives both desc…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Title check

Explanation

The title "Fonts: shape long items in bounded pieces" names the main change. The PR makes shape_range split items longer than 1024 bytes into bounded pieces, which the summary and objectives both describe. The title is specific and short. It does not mention the cmap fallback or the RTL ordering, but those are secondary details.

Full details: Docstring Coverage

Explanation

Docstring coverage is 75.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documentation-only edits and deletions within surviving functions are included; unchanged diff context and fully deleted functions are excluded. Analyzed 4 functions across 1 files.

Resolution

Write docstrings for the functions missing them to satisfy the coverage threshold.

Finishing Touches
Generate docstrings
  • Commit to this branch
  • Create a new PR
Generate unit tests (beta)
  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Warning

Some tools did not complete. Review the errors below.

ast-grep (0.51.1)

ast-grep could not parse rule config: /ast-grep-rules/ast-grep-essentials/rules/swift/security/insecure-storage-userdefaults-sensitive-swift.yml

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

CodeRabbit Verify

  • Run CodeRabbit Verify to test the changes in this pull request.
Recent review info
Run configuration
Commits

Reviewing files that changed from the base of the PR and between 3e178d8 and 783b35e.

Files selected for processing: 1
  • crates/fonts/src/shape.rs

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 4 remain after this review.

@dylanmtaylor
dylanmtaylor merged commit 8ec4f6c into main Nov 3, 2026
9 of 10 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.

1 participant