Skip to content

fix(fonts): MTEXT wraps, justifies and spaces lines as AutoCAD does (#3010) - #3241

Draft
whaslbeck wants to merge 4 commits into
storytold:mainfrom
whaslbeck:fix/3010-mtext-wrap-extents
Draft

whaslbeck wants to merge 4 commits into
storytold:mainfrom
whaslbeck:fix/3010-mtext-wrap-extents

Conversation

@whaslbeck

@whaslbeck whaslbeck commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #3010. MTEXT wraps, justifies and spaces its lines as AutoCAD does. Measured against AutoCAD Mechanical 2023 with synthetic drawings (summary in #3010 (comment)).

Draft: needs storytold/craft-libs#2036 (craft-shx reports the box AutoCAD measures a glyph by). Cargo.toml points craft-shx at that PR's commit for now; I'll switch it back to storytold/craft-libs once #2036 is merged.

What AutoCAD does, and what changes

  1. Wrapping (mtext.rs plain_lines, para.rs LineBuilder::place): a word stays on the line while the pen position where it starts plus the width of its measure box is below the column width; AutoCAD wraps on equality. Before, the word's advances were compared, so text wrapped too early (the trailing side bearing of the last character and the leading one of the word counted).
  2. Measure box (text_extent, new): SHX glyphs by craft_shx::program::Glyph::extent (arcs by their chord-and-sagitta box, which is what makes isocp/isoct wrap 0.5–1 % earlier than their ink), other fonts by their outlines; slanted text by slanting the measure points (a 15° style, as in most dimension styles).
  3. Justification: left/centre/right by the line's measure box, so the first character's side bearing is dropped. Exceptions measured: leading spaces keep the line's start (box from the start to the end of the drawing), a tab puts the drawing at the stop, and a line holding spaces formatted unlike both neighbouring words ({\C1;a} {\C3;b}) is justified by its drawing's width but drawn without the bearing correction.
  4. Spaces: every space of a run counts; leading spaces of a paragraph are kept and count as the line's first word (a word that doesn't fit after them wraps and leaves them on a line of their own). MTEXT: keep leading and repeated spaces; read a caret-I in the contents as a tab #950 does the first two parts; this measures them against AutoCAD and adds the rest.
  5. Vertical: the first baseline drops by the first line's height or its drawing's top if higher (accented capitals), middle/bottom attachment counts the last line's descender, and at "At least" spacing the step from line i to i+1 is (Hᵢ + h)/3 + max(Hᵢ₊₁, top of line i+1) — 5/3·h for uniform lines as before. mtext_exact_spacing_ignores_taller_lines expected 5/3 of the doubled height before and after a \H2x line; AutoCAD steps 2/3·h + 2h before it and h + h after it, so its at-least value changes to that (Exactly is unchanged).
  6. TrueType size (ttf.rs cap_height): by the drawn height of H before OS/2 sCapHeight (a cap height pinned by CADCRAFT_CAP_HEIGHT, fonts: a substitute's text scales by the cap height the installation pins for it, not the substitute's own #2261, still comes first). Arial's OS/2 value is 1467, its H 1466 units; with the H all 20 measured Arial wrap limits match, with the OS/2 value 13.

Evidence

Synthetic test drawings (one MTEXT per cell; isocp, isoct, romans, simplex, SIMPLEX8, txt, Arial; heights 2.5 and 12.5; width factors 1 and 0.75; all nine attachments; paragraphs, \P, \H, \W, \C, \Q, tabs, leading and repeated spaces; 15° styles), plotted by AutoCAD and by cadcraft-cli, rasterised and compared per line (line count, ink extents relative to the insertion point, tolerance 0.03·h):

set cells line count equal before → after position equal after
wrap limits (ink ± 0.004/0.02·h) 240 126 → 240 240
width factor 0.75 40 23 → 40 40
attachments 1–9 108 45 → 108 108
paragraphs 36 21 → 36 36
vertical (1–4 lines, accents, descenders, \H) 330 — 328
line starts (format codes, spaces, tab) 316 — 304 (\T, see below)
15° style, centred/left/right, wrapping 132 — 132

Wrap limits stepped by 0.02·h (120 font/text pairs) and 0.005·h (64 arc glyphs): all match except exact ties at the rounding of the stored width (≤ 5·10⁻⁵).

Not covered here: \T tracking (not supported; #2848), the underline offset of TrueType text, negative oblique angles and slanted TrueType at the right edge (small differences remain), and single-line TEXT.

Tests

New, with a test font compiled in the test (shx/tests.rs): mtext_wraps_a_word_by_its_drawing_not_its_advances, mtext_lines_are_justified_by_their_drawing, mtext_lines_are_placed_by_their_drawing_vertically; each also runs the paragraph path and checks both agree. All three fail on main and pass here.

Locally: cargo fmt --all --check, cargo clippy --workspace --all-targets -- -D warnings and cargo test --workspace (both without cadcraft-web) pass. I did not run the wasm build or the full cargo xtask ci.

On our 472 production drawings (not shareable) plotted against AutoCAD's own PDFs, 46 drawings get closer to AutoCAD and none moves away.

AutoCAD sizes TrueType text by the height of its H: Arial's OS/2 cap
height is 1467 of 2048 units, its H 1466 high, and AutoCAD's MTEXT wraps
Arial text 1467/1466 wider than the OS/2 value gives (all 20 measured
wrap limits match with the H, 13 with the OS/2 value).
…torytold#3010)

A word stays on the line while the pen position where it starts plus the
width of its measure box is below the column width (AutoCAD wraps on
equality); SHX glyphs are measured by craft-shx Glyph::extent (arcs by
their chord-and-sagitta box), slanted text by slanting its measure points.
Lines are justified by their drawing (the first character's side bearing
dropped; leading spaces, a tab and spaces formatted unlike both
neighbouring words keep the line's start), every space of a run counts,
leading spaces of a paragraph stay and count as a word. The first baseline
drops by the first line's drawing top when that is higher than the text,
middle and bottom attachment count the last line's descender, and the At
least step from line i to i+1 is (H_i + h)/3 + max(H_i+1, its top).

craft-shx points at storytold/craft-libs#2036 until that is merged.
@coderabbitai

coderabbitai Bot commented Nov 17, 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

CodeRabbit Verify

  • Run CodeRabbit Verify to test the changes in this pull request.

Conflicts: Cargo.toml/Cargo.lock (main's craft-libs revisions; craft-shx
stays on the craft-libs#2036 head), fonts/ttf.rs (storytold#2261's pinned cap height
comes first, then the drawn H, then OS/2).
@whaslbeck

Copy link
Copy Markdown
Contributor Author

Merged current main (a70b9b0). Conflicts: Cargo.toml/Cargo.lock (main's craft-libs revisions; craft-shx stays on the craft-libs#2036 head until that is merged) and crates/fonts/src/ttf.rs, where #2261 added pinned cap heights: a pinned value comes first, then the drawn H, then OS/2. The pin #2261 documents for Liberation Sans, 715.8, is Arial's drawn H (1466/2048), so the two agree.

Locally cargo fmt --all --check, cargo clippy --workspace --all-targets -- -D warnings and cargo test --workspace pass (without cadcraft-web), and the AutoCAD comparison suite gives the same result as before the merge in every set.

This branch has not been deployed

No deployments
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.

MTEXT in an SHX font is ~0.5-1 % wider than in GstarCAD, so text wraps where GstarCAD keeps one line

2 participants