Skip to content

Drawing dimensions: their numbers on the sheet, set and measured in the drawing's font (#4896) - #12356

Closed
CamWight wants to merge 3 commits into
tangency-labs:drawingsfrom
CamWight:dimension-text-4896
Closed

CamWight wants to merge 3 commits into
tangency-labs:drawingsfrom
CamWight:dimension-text-4896

Conversation

@CamWight

@CamWight CamWight commented Nov 5, 2026 •

Copy link
Copy Markdown
Contributor

Change

A dimension on a drawing sheet has had its lines and arrows since #11867, but not its number. Now each dimension's text is set in the drawing's font and language and measured there, so the arrows go inside or outside by the text's real width, not by an estimate per character. A text the font can't set keeps its lines and says why.

Part of #4896, into drawings. Stacked on #12113 (the paper scene's font count, a run's reach, and text in the reply's schema): this PR holds #12113's two commits below its own, so review only its own top commit. Merge order: #12113, then this PR, then #12357, #12776 and #12778.

Replayed on 6 Nov onto drawings f50edc1, after #12423 (a view draws again only when what it shows changes), #12449 (a deferred drawing keeps its lines until it is updated), #12516 (a view's lines come in line groups) and #11126 (a view's dimensions are worked out beside the window) merged. They change the same scene files, and each merge keeps both sides. The sheet scene's step keeps #12423's check for views that still show the same thing and #12449's hold for a deferred drawing, then sets the dimensions in the drawing's lettering. #11126's job carries that lettering in its ticket in place of the units alone, so a dimension worked out beside the window is set in the same font and language as one set in the step. Dimensions keep #12516's line groups and add their text. The scene and inspect command descriptions keep #12449's and #12516's words beside this stack's. #12449 added out_of_date to the scene reply, so the schema test that builds one sets it (out_of_date: Vec::new()). The replay of 5 Nov onto 41dcdd1 needed one change too: #12396 gave a view's drawn state an owners field, set to None in the dimension text test. Checks (Windows, --profile ci): cargo check --all-targets and cargo fmt --check of geom, sketch, doc, io and engine at each commit. At the head of the title block text PR (#12357), which holds all three, on drawings f50edc1: clippy with warnings denied; engine drawing 365, title_block_text 6 and schema 191 passed, with #11126's two job tests (a_job_beside_the_window_draws_as_a_step_does, seeded_edits_steps_and_jobs_always_end_at_the_built_scene). On drawings 1b069e1, the same code before these merges also passed sketch paper_text 47, io paper 45, geom paper 72, doc drawing 90 and engine hostile 72, and the wasm check of the engine.

Changes a default: no
No setting, command or saved data changes; text is worked out from the dimension, never saved.

What it does

In crates/engine/src/drawing_dimension_paper.rs:

  • paper_group builds one text setter, Text::set(text, font, language), and passes it to the pieces that place text (pieces, chain), not the font and language separately. Text::set sets the text with sketch::paper_text, measures its half size with TextRun::reach and the lines below the first baseline, and keeps a why when it can't be set.
  • placed() puts the runs on the layout's text point, running along its text direction, so a dimension reads from the bottom or the right as sketch dimensions do.
  • paper_group returns OnPaper { group, fonts, why }: the group with its text, the fonts its runs use, and the reason if the text couldn't be set.

In crates/engine/src/drawing_scene.rs:

drawing.scene's help says dimension groups now hold text items and the reply a fonts table (glyphs, never a font's bytes).

Options considered: pass the font and language down to each piece that places text (as first built), or build the setter once in paper_group (chosen: two parameters fewer on two inner functions, and what paper_group's callers pass is unchanged).

Validation

Local, Windows, --profile ci, on drawings 69709bd: cargo check --all-targets of geom, sketch, doc, io and engine at each of the three commits; fmt; clippy -D warnings. The same code was checked again in a group with the text PRs above it (paper lines, PNG text, title block text): sketch paper_text 47, io paper 45, geom paper 72, doc drawing 90, engine drawing 337, schema 191, and the engine's wasm build. The engine's hostile tests (72) passed.

Tests (in the area's existing test files):

  • drawing_dimension_paper_tests.rs: a measured text's width puts the arrows inside or outside; the text is centred above its line and runs along it; it reads from the bottom or the right; a text the font can't set gives the lines and a reason.
  • drawing_scene_tests.rs: a dimension's runs are in its group and its fonts in the scene's table, the same from build and the provider; the record cap counts only the records a font adds.

Planted faults, 8 of 8 caught by a failing test, then a clean run: the measured width ignored; the why lost; the runs dropped; the text upside down; the runs level instead of along the line; a group's font records not counted; the fonts not added to the table; the cache ignoring the lettering.

What the tests don't reach:

Screenshots of the behavior

An L-shaped plate, front view at 2:1, with four length dimensions, exported as SVG and PNG at 300 dpi (the PNG with #12355, now merged, and the title block's text from #12357). The numbers are measured in the drawing's font: 80.00 fits between its arrows, and the vertical ones read from the right. Before this, the same sheet had the lines and arrows with no numbers.

Close-up of the four dimensions: 80.00 along the bottom, 60.00, 30.00 and 30.00 reading from the right

The whole sheet (the PNG):

The L-shaped plate's sheet exported as a PNG at 300 dpi: four dimensions with their numbers, and the title block with its labels and values

Integration status

Three commits on drawings f50edc1 (the latest when pushed): #12113's two, then this one. When #12113 squash-merges, this is replayed onto the squash at once.

@coderabbitai

coderabbitai Bot commented Nov 5, 2026 •

Copy link
Copy Markdown
Contributor

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

Run configuration

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • Trigger review
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

CamWight and others added 3 commits November 6, 2026 07:31
…the sheet reply's schema (tangency-labs#4890)

One rule for everything that puts text on a sheet: `Scene::font_records_beyond`
counts only what a scene's font table doesn't hold yet, `TextRun::reach` says
how far a run's glyphs go along its baseline, and `drawing.scene`'s schema has
text runs and fonts, never a font's bytes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…n the table

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…tangency-labs#4896 W19h)

A dimension on a sheet now carries its text: each value is set in the
drawing's font and language, measured, and placed along the dimension's
text direction, reading from the bottom or the right. Arrows flip by the
measured text width. A text the font can't set leaves the lines and says
why. The scene adds each dimension's fonts to its table once, counting their
records against the scene's limit, and measures text by its outlines for an
object's bounds. A dimension is read again when the drawing's units,
language or font change.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@theironchef theironchef left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Read at 40eb4fab9 (its own commit on top of #12113; source only, not built locally).

The main path looks right to me:

  • Text::set's width is right for runs that share a line. paper_text starts every run of a line at the line's start (paper_text.rs:37), so the furthest reach() of the runs is the line's width, even when the Ø comes from a fallback font.
  • In placed(), a single line's baseline sits TEXT_MM / 2 below the text point, with the capitals reaching as far above, and later lines go down by their origin.y.
  • Assembly::add clears alone.fonts on both refusals, so a refused group's fonts never reach the table. group_bounds only sees the joining group's own fonts.

Possible bug: on the web, a dimension whose script font is still loading may never get its text.

The description says a dimension in a script whose font loads on demand (#11790) "reports why until its font has loaded". As far as I can see, it keeps reporting it after the font loads too:

  1. Text::set calls paper_text → answers → web_answers. While the script's font is still being fetched, that's an error, so the dimension's DrawnDimension gets why: Some(...) and no text runs.
  2. keep_dimensions caches that result. stale_dimensions only reads it again when the record, the world state, the view's serial or the Lettering change.
  3. When the font arrives, apps/solidcraft-web/src/main.rs:821 calls fallback::provide_web_font and request_repaint, and nothing else. None of the four keys changes, and cache.last's check (extract, around line 474) still matches. So the sheet keeps serving the dimension without its text until some unrelated edit.

If that reading is right, one fix would be a small font generation in sketch::fonts::fallback, bumped by provide_web_font, web_font_failed and store, and added to Lettering. Then a font's arrival reads the dimensions again, like a change of the drawing's font does. Another would be not to cache a dimension whose text is pending. (On native, paper_text waits for the fallback search, so this shouldn't happen there.)

A test could build a dimension whose text needs a WebFont that isn't there yet, extract the sheet (expect a why with no text), provide_web_font, extract again, and expect the runs.

Minor: Text::set measures width as max(reach()) and ignores run.origin.x. That's right while dimension text is left-aligned (the default TextStyle). If a dimension's text is ever set centred or right-aligned over more than one line, the box would need max(origin.x + reach()), which placed() already assumes when it adds run.origin.x.

@theironchef

Copy link
Copy Markdown
Contributor

A possible fix for the web-font caching gap I raised above, tested on this PR merged onto drawings a7c0310ab (Windows, MSVC, --profile ci).

fallback counts the web fonts that have come or failed (provide_web_font, web_font_failed), and Lettering keys on that count, so a dimension set while its script's font was still loading is read again once the font settles, the same way a change of the drawing's font is handled. On native the count never moves, so nothing changes there.

diff --git a/crates/engine/src/drawing_scene.rs b/crates/engine/src/drawing_scene.rs
index 46466dcae..07af73aee 100644
--- a/crates/engine/src/drawing_scene.rs
+++ b/crates/engine/src/drawing_scene.rs
@@ -375,17 +375,20 @@ struct DrawnDimension {
 }
 
 /// What a drawing's dimensions read and are set in: its units (the design's, when it follows them),
-/// its language and its font. A dimension drawn in other ones is read again.
+/// its language and its font, and the web fonts fetched by then. A dimension drawn in other ones is
+/// read again, so text that waited for a web font is set once it comes.
 #[derive(Clone, Debug, PartialEq)]
 struct Lettering {
     units: String,
     language: String,
     font: String,
+    web_fonts: u64,
 }
 
 impl Lettering {
     fn of(s: &Session, d: &Drawing) -> Lettering {
-        Lettering { units: s.doc.drawing_units(d).to_string(), language: d.language.clone(), font: d.font.clone() }
+        let web_fonts = solidcraft_sketch::fallback::web_fonts_settled();
+        Lettering { units: s.doc.drawing_units(d).to_string(), language: d.language.clone(), font: d.font.clone(), web_fonts }
     }
 }
 
diff --git a/crates/engine/src/drawing_scene_tests.rs b/crates/engine/src/drawing_scene_tests.rs
index 8a7926106..427c6fe8c 100644
--- a/crates/engine/src/drawing_scene_tests.rs
+++ b/crates/engine/src/drawing_scene_tests.rs
@@ -2109,3 +2109,19 @@ fn seeded_edits_steps_and_jobs_always_end_at_the_built_scene() {
         assert_eq!(bytes(&end.scene), bytes(&built(&s, sheet).scene), "seed {seed}");
     }
 }
+
+/// A web font that comes (or fails) after a dimension's text was set reads the sheet's dimensions
+/// again, so text that waited for it is set without another edit; drawn again with nothing changed
+/// for it, the sheet is the same bytes.
+#[test]
+fn a_web_font_settling_reads_the_dimensions_again() {
+    let (mut s, sheet, [front, _]) = front_and_top();
+    let added = run(&mut s, "drawing.dimension.add", json!({"view": ids::encode(front), "kind": {"length": {"edge": [50, 0, 20]}}}));
+    let dimension = ids::decode(added["dimension"].as_str().unwrap()).unwrap();
+    let before = settled(&mut s, sheet);
+    assert!(extract(&mut s, sheet).unwrap().stale_dimensions.is_empty());
+    // Native text never asks for a web font, so settling one here changes no glyph.
+    solidcraft_sketch::fallback::web_font_failed(solidcraft_sketch::fallback::WebFont::Korean);
+    assert_eq!(extract(&mut s, sheet).unwrap().stale_dimensions, vec![dimension]);
+    assert_eq!(bytes(&settled(&mut s, sheet).scene), bytes(&before.scene));
+}
diff --git a/crates/sketch/src/fonts/fallback.rs b/crates/sketch/src/fonts/fallback.rs
index 67bb9e78d..1e1c3fb39 100644
--- a/crates/sketch/src/fonts/fallback.rs
+++ b/crates/sketch/src/fonts/fallback.rs
@@ -409,11 +409,22 @@ pub fn web_fonts_wanted() -> Vec<WebFont> {
 /// A font the web app fetched: text waiting for it is set on its next try.
 pub fn provide_web_font(font: WebFont, bytes: Vec<u8>) {
     web_fonts()[font as usize] = Fetch::Here(Arc::new(bytes));
+    FETCHES_SETTLED.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
 }
 
 /// A font's fetch failed: what it would set is refused by name.
 pub fn web_font_failed(font: WebFont) {
     web_fonts()[font as usize] = Fetch::Failed;
+    FETCHES_SETTLED.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
+}
+
+/// How many fetched fonts have come or failed. Text set while it waited for one may set otherwise
+/// once this moves, so what keeps set text keys it with this.
+static FETCHES_SETTLED: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0);
+
+/// [`FETCHES_SETTLED`]'s count.
+pub fn web_fonts_settled() -> u64 {
+    FETCHES_SETTLED.load(std::sync::atomic::Ordering::Relaxed)
 }
 
 /// A fetched font's bytes, when `path` stands for one.

Results:

  • The new test, a_web_font_settling_reads_the_dimensions_again (in drawing_scene_tests.rs), passes. With Lettering::of reading a constant instead of the count, it fails (stale_dimensions is [] instead of the dimension), so it pins the gap. It uses web_font_failed(WebFont::Korean) as the trigger, which changes no glyph natively, and checks that the sheet then settles to the same bytes.
  • All 85 drawing_scene and drawing_dimension_paper engine tests pass, and cargo clippy -p solidcraft-sketch -p solidcraft-engine --all-targets -- -D warnings is clean.

#12357's title block text would want the same key, since its scene cache also holds a Lettering.

One caveat on the test: it bumps a process-wide counter. Under nextest (one process per test, as CI runs) that's isolated, but in a plain threaded cargo test run a neighbouring scene test could, in principle, see its dimensions as out of date for one extract. If that's a worry, the count could be read through a #[cfg(test)] override instead.

@CamWight

CamWight commented Nov 6, 2026

Copy link
Copy Markdown
Contributor Author

Landed in drawings with #12778 (2a25449), which carried this commit

@CamWight CamWight closed this Nov 6, 2026
@CamWight

CamWight commented Nov 7, 2026

Copy link
Copy Markdown
Contributor Author

Thanks @theironchef, the web-font gap is real. The same gap was in the title block text in #12357. We chose not to add the counter. #12778, which merged with this stack, makes any text still waiting for a font "pending", so it's read again on each pass until it's set, and a web font still loading counts as waiting. That closes it with one rule for all sheet text, without a process-wide value in the cache key. The PR text there says so, with credit to you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:kernel B-rep kernel, booleans, blends, STEP/IGES

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants