Skip to content

PDF export: wrap text with running widths - #3208

Merged
dylanmtaylor merged 1 commit into
mainfrom
fix/sec-print-wrap-linear
Nov 3, 2026
Merged

dylanmtaylor merged 1 commit into
mainfrom
fix/sec-print-wrap-linear

Conversation

@dylanmtaylor

@dylanmtaylor dylanmtaylor commented Nov 3, 2026 •

Copy link
Copy Markdown
Member

Addresses CodeRabbit security findings (CWE-407).

What was wrong
wrap_lines (PDF export of shape text and wrapped cells) split an over-long word by re-measuring and copying the rest of the word for each piece it cut. It also checked its 2,000-line limit only after finishing a word, so one long word in a narrow text box cost word length x pieces. Each new word also re-measured the whole line so far.

What changed

  • Line and piece widths are running sums of glyph widths, the same integer sum text_width makes, so each character is measured once.
  • The line limit is checked while a word is being split.

Blast radius: PDF text wrapping. The lines produced are unchanged; a test compares them with the old wrapper across fonts, sizes and widths.

Tests: wrap_lines_matches_the_remeasuring_wrapper, wrapping_a_long_word_in_a_narrow_box_stops_at_the_line_limit, existing bands_unit.
Checks run

  • cargo test -p gridcraft-engine -p gridcraft-chart on a local branch combining this PR with the other security fixes opened alongside it (all pass: engine 817 unit, 101 integration; chart 46)
  • cargo check -p gridcraft-engine -p gridcraft-chart --tests on this branch alone
  • rustfmt --check on the changed files

Checks not run

  • cargo clippy, the full workspace test suite, the UI crate, macOS and Windows builds

Summary by CodeRabbit

  • Improvements
    • Text wrapping handles long words and large paragraphs more efficiently, helping maintain responsiveness when printing unusually large text.
    • Extremely long wrapped output is limited to a maximum number of lines.
  • Tests
    • Added coverage for wrapping across different text, font, and width combinations, as well as stress tests for very long words and paragraphs.

wrap_lines split an over-long word by re-measuring and copying the
rest of the word for every piece it cut, and only checked its line
limit once the word was done, so one long word in a narrow text box
cost word length x pieces. Each new word also re-measured the whole
line built so far.

Line and piece widths are now running sums of glyph widths (the same
sum text_width makes), so every character is measured once, and the
line limit is checked while a word is being split. The lines produced
are unchanged; a test compares them with the old wrapper.
@coderabbitai

coderabbitai Bot commented Nov 3, 2026 •

Copy link
Copy Markdown

Review in Change Stack →

No actionable comments were generated in the recent review.

Walkthrough

wrap_lines now accumulates glyph widths and splits overlong words in a character pass. New tests compare its output with a remeasuring reference and exercise large inputs.

Changes: Text wrapping

Layer / File(s) Summary
Accumulate widths and split overlong words
crates/engine/src/cmd/print/text.rs
wrap_lines uses accumulated glyph widths, splits overlong words one character at a time, and checks the named 2,000-line limit during splitting and word processing.
Compare output and test large inputs
crates/engine/src/cmd/print/tests.rs
Tests compare wrap_lines with a remeasuring reference across varied text, fonts, sizes, and widths. Stress tests cover a 100,000-character word and a 50,000-word paragraph.

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Refactor

Possibly related issues

  • storytold/gridcraft#2333 — The reported PDF text cutoff relates to cell text wrapping; this change affects PDF wrapping, but the issue may require additional work.
  • storytold/gridcraft#2855 — The reported non-ASCII spacing relates to PDF text layout; this change affects glyph-width accumulation, but does not establish that the issue is fixed.

Suggested reviewers: aiongg

Merge Risk: Minimal · up to 0c2e5

PDF wrapping retains the previous width calculation while limiting long-word splitting. No merge-blocking defect was established; the SonarCloud scan needs a successful rerun.

Pre-merge checks: Passed 4 · Failed 1

Failed Failed checks: 1 warning

Check name Status Explanation Resolution
Docstring Coverage Warning Docstring coverage is 66.67% 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 "PDF export: wrap text with running widths" matches the main change. The text.rs changes make wrap_lines measure glyph widths once per word and keep running width totals, which is the core o…
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 "PDF export: wrap text with running widths" matches the main change. The text.rs changes make wrap_lines measure glyph widths once per word and keep running width totals, which is the core of the PR. The title does not cover the split of overlong words, the MAX_WRAP_LINES limit, or the new tests, but the changeset does not need every detail in the title.

Full details: Docstring Coverage

Explanation

Docstring coverage is 66.67% 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 3 functions across 2 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.

Clippy (1.99.0)

Clippy execution timed out

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 85e51e0 and 0c2e523.

Files selected for processing: 2
  • crates/engine/src/cmd/print/tests.rs
  • crates/engine/src/cmd/print/text.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

Copy link
Copy Markdown
Member Author

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Nov 3, 2026

Copy link
Copy Markdown

Rate Limit Exceeded

@dylanmtaylor have exceeded the limit for the number of chat messages per hour. Please wait 6 minutes and 54 seconds before sending another message.

@dylanmtaylor

Copy link
Copy Markdown
Member Author

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Nov 3, 2026 •

Copy link
Copy Markdown
Action performed

Review finished.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@dylanmtaylor
dylanmtaylor merged commit a305229 into main Nov 3, 2026
8 of 9 checks passed
@dylanmtaylor
dylanmtaylor deleted the fix/sec-print-wrap-linear branch November 3, 2026 21:11
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