Repository navigation
Drawing dimensions: their numbers on the sheet, set and measured in the drawing's font (#4896) - #12356
Drawing dimensions: their numbers on the sheet, set and measured in the drawing's font (#4896)#12356CamWight wants to merge 3 commits into
Conversation
|
Important Review skippedAuto reviews are disabled on base/target branches other than the default branch. Please check the settings in the CodeRabbit UI or the
|
0f41fba to
ad32bbc
Compare
ad32bbc to
2b562f9
Compare
2b562f9 to
bb40250
Compare
bb40250 to
4f40d0c
Compare
…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>
4f40d0c to
40eb4fa
Compare
theironchef
left a comment
There was a problem hiding this comment.
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_textstarts every run of a line at the line's start (paper_text.rs:37), so the furthestreach()of the runs is the line's width, even when theØcomes from a fallback font.- In
placed(), a single line's baseline sitsTEXT_MM / 2below the text point, with the capitals reaching as far above, and later lines go down by theirorigin.y. Assembly::addclearsalone.fontson both refusals, so a refused group's fonts never reach the table.group_boundsonly 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:
Text::setcallspaper_text→answers→web_answers. While the script's font is still being fetched, that's an error, so the dimension'sDrawnDimensiongetswhy: Some(...)and no text runs.keep_dimensionscaches that result.stale_dimensionsonly reads it again when the record, the world state, the view's serial or theLetteringchange.- When the font arrives,
apps/solidcraft-web/src/main.rs:821callsfallback::provide_web_fontandrequest_repaint, and nothing else. None of the four keys changes, andcache.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.
|
A possible fix for the web-font caching gap I raised above, tested on this PR merged onto
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:
#12357's title block text would want the same key, since its scene cache also holds a 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 |
|
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. |
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
drawingsf50edc1, 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 addedout_of_dateto 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 anownersfield, set toNonein the dimension text test. Checks (Windows,--profile ci):cargo check --all-targetsandcargo fmt --checkof geom, sketch, doc, io and engine at each commit. At the head of the title block text PR (#12357), which holds all three, ondrawingsf50edc1: clippy with warnings denied; enginedrawing365,title_block_text6 andschema191 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). Ondrawings1b069e1, the same code before these merges also passed sketchpaper_text47, iopaper45, geompaper72, docdrawing90 and enginehostile72, 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_groupbuilds 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::setsets the text withsketch::paper_text, measures its half size withTextRun::reachand the lines below the first baseline, and keeps awhywhen 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_groupreturnsOnPaper { 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:Scene::font_records_beyond, from Drawing text: the paper scene counts a font's records, measures a run, and its reply schema allows text #12113) count toward the scene's record cap.group_bounds) measures its text by the outlines.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 whatpaper_group's callers pass is unchanged).Validation
Local, Windows,
--profile ci, ondrawings69709bd:cargo check --all-targetsof 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): sketchpaper_text47, iopaper45, geompaper72, docdrawing90, enginedrawing337,schema191, and the engine's wasm build. The engine'shostiletests (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 frombuildand 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
whylost; 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:
why. The sheet scene keeps that answer, and nothing reads the dimension again when the font arrives, so thewhystays until another edit. @theironchef found this in review. Thank you! Drawings: sheet text in a script the drawing's font lacks no longer holds up the window while a font is found #12778, later in this stack, closes it: text that waits for a font is pending, not refused, and it is read again until it is set. No window code draws sheet scenes yet, so nothing on screen shows the gap.paper_textlook through the fallback fonts on the thread that builds the scene. Drawings: sheet text in a script the drawing's font lacks no longer holds up the window while a font is found #12778 sets this text without waiting on that thread.Text::setmeasures a text's width as its runs' furthest reach. That is right for left-aligned text, which is the only style a dimension's text has. Centred or right-aligned text over more than one line would also need each run'sorigin.x, asplacedalready adds.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.
The whole sheet (the PNG):
Integration status
Three commits on
drawingsf50edc1 (the latest when pushed): #12113's two, then this one. When #12113 squash-merges, this is replayed onto the squash at once.drawing_scene.rsthat adds a dimension's group: this adds the lettering, Drawings: a view's dimensions change in the same step as its lines (#4896) #12287 addskeep_dimensions. Drawings: a view's dimensions change in the same step as its lines (#4896) #12287 merged first, so this replay carries its line:keep_dimensionswith the lettering, at both callers.reference_statusesto the same cache key that this turns fromunitsintolettering; whichever merges second keeps both side by side. Centre marks are lines, so they need no font counting.