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 <[email protected]>
This commit is contained in:
moandClaude Fable 5.2 committed 2026-12-01 03:29:28 +00:00
1 parent 165287474e
commit 56388075ae
2 files changed
+41 -7

No files matched your search

+11 -7
View File
@@ -279,11 +279,15 @@ fn drawn_chars(value: &str) -> usize {
drawn drawn
} }
/// Bounds of a single-line TEXT / ATTDEF / ATTRIB of `len` characters, following its justification /// Bounds of a single-line TEXT / ATTDEF / ATTRIB of `len` drawn characters, following its
/// the same way the renderer places it (the stroke font is about 0.88 of the height per character). /// 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 { fn text_box(t: &crate::Text, len: usize) -> Bounds2 {
use crate::{HAlign, VAlign}; 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). // Justified text (TL, ML and BL too) hangs from its alignment point (DXF group 11).
let origin = t.justify_point(); let origin = t.justify_point();
// Aligned and Fit text span their two points (groups 10 and 11) whatever rotation is stored: // 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 usable = t.width_factor.is_finite() && t.width_factor.abs() > 1e-6;
let wf = if usable { t.width_factor.abs() } else { 1.0 }; 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 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. // 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 // 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. // 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; let fitted = wf * d / shaped;
usable && fitted.is_finite() && fitted > 1e-6 * MARGIN 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 // 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 // cutoff as theirs, with no margin, since a box scaled for text that is left alone is
// enormous. // 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 // 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 // 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. // 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 most width it gives one (its widest characters are 4 units wide), for the least height
// the line can be drawn at. // 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. // Width, the most and the least height the line is drawn at, and its direction.
let (w, h, low, rotation) = match (span, second) { let (w, h, low, rotation) = match (span, second) {
(Some(_), Some(p)) if no_width => (shaped, height, height, origin.angle_to(p)), (Some(_), Some(p)) if no_width => (shaped, height, height, origin.angle_to(p)),
+30
View File
@@ -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] #[test]
fn hatch_pattern_and_solid() { fn hatch_pattern_and_solid() {
let mut d = Drawing::new_imperial(); let mut d = Drawing::new_imperial();