diff --git a/crates/doc/src/extents.rs b/crates/doc/src/extents.rs index 760665441..5cb869992 100644 --- a/crates/doc/src/extents.rs +++ b/crates/doc/src/extents.rs @@ -261,9 +261,16 @@ fn text_box(t: &crate::Text, len: usize) -> Bounds2 { 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; + // 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. + let fits = |d: f64| { + let fitted = wf * d / shaped; + usable && fitted.is_finite() && fitted > 1e-6 + }; + let unstretched = height * 0.9 * len.max(1) as f64; let (w, h, rotation) = match (span, second) { - // Fit text with an unusable width factor is not stretched: it is drawn at its own width. - (Some(_), Some(p)) if t.halign == HAlign::Fit && !usable => (shaped, height, origin.angle_to(p)), + (Some(d), Some(p)) if t.halign == HAlign::Fit && !fits(d) => (unstretched, height, origin.angle_to(p)), (Some(d), Some(p)) if t.halign == HAlign::Fit => (d, height, origin.angle_to(p)), (Some(d), Some(p)) => (d, height * d / shaped, origin.angle_to(p)), _ => (natural, t.height, t.rotation), diff --git a/crates/render/src/tests.rs b/crates/render/src/tests.rs index 0b52012cb..a3c430fc0 100644 --- a/crates/render/src/tests.rs +++ b/crates/render/src/tests.rs @@ -70,6 +70,46 @@ fn text_produces_strokes() { assert!(l.prims.len() >= 3); } +/// Fit text whose two points are so close that the fitted width factor is too small to shape +/// is drawn unstretched, far wider than the span between its points: the drawing's extents hold +/// what is drawn, as they do for the same text fitted over a span it can be stretched to. +#[test] +fn fit_text_extents_hold_the_drawn_text_when_the_span_is_too_short_to_fit() { + let drawn_and_extents = |span: f64| { + let mut d = Drawing::new_imperial(); + let text = cadcraft_doc::Text { + insert: Vec3::ZERO, + align_pt: Some(Vec3::new(span, 0.0, 0.0)), + height: 1.0, + value: "AB".into(), + rotation: 0.0, + width_factor: 1.0, + oblique: 0.0, + style: "Standard".into(), + halign: cadcraft_doc::HAlign::Fit, + valign: Default::default(), + }; + d.add(&Space::Model, Common::default(), EntityKind::Text(text)).unwrap(); + let l = build(&d, &Space::Model, &Options::default()); + let drawn = cadcraft_geom::Bounds2::from_points(l.prims.iter().flat_map(|p| l.points(p).iter().copied())); + (drawn, d.extents(&Space::Model)) + }; + for span in [1e-7, 10.0] { + let (drawn, extents) = drawn_and_extents(span); + assert!(!drawn.is_empty(), "span {span}: the text is drawn"); + let slack = 1e-6; + assert!( + drawn.min.x >= extents.min.x - slack + && drawn.max.x <= extents.max.x + slack + && drawn.min.y >= extents.min.y - slack + && drawn.max.y <= extents.max.y + slack, + "span {span}: drawn {drawn:?} outside extents {extents:?}" + ); + } + // The short span is the case the fitted factor falls back in: the text is wider than its span. + assert!(drawn_and_extents(1e-7).0.width() > 1e-7 * 1000.0); +} + #[test] fn hatch_pattern_and_solid() { let mut d = Drawing::new_imperial();