From 56388075aeb7c1fb40740649640a2f2b8995232c Mon Sep 17 00:00:00 2001 From: mo <131036780+mbolling@users.noreply.github.com> Date: Tue, 1 Dec 2026 03:29:28 +0000 Subject: [PATCH] Text that draws nothing has an empty box A value of nothing but underline and overline toggles (%%u%%u) has no drawn character, and the box floored the count at one: Aligned over a span of 20 that was a box of 20 by 120 for text that draws nothing. The box is empty when no character is drawn, for an empty value too. Co-Authored-By: Claude Fable 5.2 --- crates/doc/src/extents.rs | 18 +++++++++++------- crates/render/src/tests.rs | 30 ++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 7 deletions(-) diff --git a/crates/doc/src/extents.rs b/crates/doc/src/extents.rs index d8e2a12bf..39e23da84 100644 --- a/crates/doc/src/extents.rs +++ b/crates/doc/src/extents.rs @@ -279,11 +279,15 @@ fn drawn_chars(value: &str) -> usize { drawn } -/// Bounds of a single-line TEXT / ATTDEF / ATTRIB of `len` characters, following its justification -/// the same way the renderer places it (the stroke font is about 0.88 of the height per character). +/// Bounds of a single-line TEXT / ATTDEF / ATTRIB of `len` drawn characters, following its +/// justification the same way the renderer places it (the stroke font is about 0.88 of the height +/// per character). Empty when nothing is drawn. fn text_box(t: &crate::Text, len: usize) -> Bounds2 { use crate::{HAlign, VAlign}; - let natural = t.height * 0.9 * t.width_factor.abs().max(0.01) * len.max(1) as f64; + if len == 0 { + return Bounds2::EMPTY; + } + let natural = t.height * 0.9 * t.width_factor.abs().max(0.01) * len as f64; // Justified text (TL, ML and BL too) hangs from its alignment point (DXF group 11). let origin = t.justify_point(); // Aligned and Fit text span their two points (groups 10 and 11) whatever rotation is stored: @@ -297,7 +301,7 @@ fn text_box(t: &crate::Text, len: usize) -> Bounds2 { let usable = t.width_factor.is_finite() && t.width_factor.abs() > 1e-6; let wf = if usable { t.width_factor.abs() } else { 1.0 }; let height = if t.height.is_finite() && t.height > 0.0 { t.height } else { 1.0 }; - let shaped = height * 0.9 * wf * len.max(1) as f64; + let shaped = height * 0.9 * wf * len as f64; // Fit text is stretched by scaling its width factor by span / shaped width and shaping again. // When the scaled factor is one the fonts cannot use (the stored one is, or the span is a tiny // part of the text's width), the text is not stretched: it is drawn at a factor of 1. @@ -309,7 +313,7 @@ fn text_box(t: &crate::Text, len: usize) -> Bounds2 { let fitted = wf * d / shaped; usable && fitted.is_finite() && fitted > 1e-6 * MARGIN }; - let unstretched = height * 0.9 * len.max(1) as f64; + let unstretched = height * 0.9 * len as f64; // The fonts leave a line of next to no width as it is, neither stretched nor scaled: the same // cutoff as theirs, with no margin, since a box scaled for text that is left alone is // enormous. @@ -318,10 +322,10 @@ fn text_box(t: &crate::Text, len: usize) -> Bounds2 { // drawn. The least width the stroke font gives a line: its narrowest characters are 1 unit // wide on a cap height of 6, with 1.6 units between characters. The height from that width // holds whatever the line is made of, also shrunk into a span shorter than its width. - let narrowest = height * wf * (2.6 * len.max(1) as f64 - 1.6) / 6.0; + let narrowest = height * wf * (2.6 * len as f64 - 1.6) / 6.0; // The most width it gives one (its widest characters are 4 units wide), for the least height // the line can be drawn at. - let widest = height * wf * (5.6 * len.max(1) as f64 - 1.6) / 6.0; + let widest = height * wf * (5.6 * len as f64 - 1.6) / 6.0; // Width, the most and the least height the line is drawn at, and its direction. let (w, h, low, rotation) = match (span, second) { (Some(_), Some(p)) if no_width => (shaped, height, height, origin.angle_to(p)), diff --git a/crates/render/src/tests.rs b/crates/render/src/tests.rs index 2d9f2b14c..cdd9f3b4e 100644 --- a/crates/render/src/tests.rs +++ b/crates/render/src/tests.rs @@ -347,6 +347,36 @@ fn unfitted_text_of_next_to_no_width_keeps_extents_of_its_own_size() { } } +/// A value of nothing but underline and overline toggles draws nothing and has no extents: not +/// those of one character, which Aligned text would stretch over its span. +#[test] +fn text_that_draws_nothing_has_no_extents() { + use cadcraft_doc::HAlign::{Aligned, Fit, Left}; + for halign in [Left, Aligned, Fit] { + for value in ["%%u%%u", "%%o", ""] { + let mut d = Drawing::new_imperial(); + let text = cadcraft_doc::Text { + insert: Vec3::ZERO, + align_pt: Some(Vec3::new(20.0, 0.0, 0.0)), + height: 1.0, + value: value.into(), + rotation: 0.0, + width_factor: 1.0, + oblique: 0.0, + style: "Standard".into(), + halign, + valign: Default::default(), + backwards: false, + upside_down: false, + }; + d.add(&Space::Model, Common::default(), EntityKind::Text(text)).unwrap(); + assert!(build(&d, &Space::Model, &Options::default()).prims.is_empty(), "{halign:?} {value:?}: nothing is drawn"); + let extents = d.extents(&Space::Model); + assert!(extents.is_empty(), "{halign:?} {value:?}: extents {extents:?}"); + } + } +} + #[test] fn hatch_pattern_and_solid() { let mut d = Drawing::new_imperial();