Text generation flags: extents of mirrored Aligned and Fit text

A test that the extents of backwards and upside-down text over two
points are the plain text's extents reflected about the first point,
as the drawn text is.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
paco-velasco
2026-11-30 21:53:38 +01:00
co-authored by Claude Fable 5.2
parent 318cfc83fa
commit 0cfd8aac85
+36 -2
View File
@@ -79,6 +79,18 @@ fn drawn_and_extents(
height: f64,
width_factor: f64,
span: f64,
) -> (cadcraft_geom::Bounds2, cadcraft_geom::Bounds2) {
mirrored_drawn_and_extents(halign, value, height, width_factor, span, (false, false))
}
/// [`drawn_and_extents`] for a text with its generation flags (backwards, upside down) set.
fn mirrored_drawn_and_extents(
halign: cadcraft_doc::HAlign,
value: &str,
height: f64,
width_factor: f64,
span: f64,
(backwards, upside_down): (bool, bool),
) -> (cadcraft_geom::Bounds2, cadcraft_geom::Bounds2) {
let mut d = Drawing::new_imperial();
let text = cadcraft_doc::Text {
@@ -92,8 +104,8 @@ fn drawn_and_extents(
style: "Standard".into(),
halign,
valign: Default::default(),
backwards: false,
upside_down: false,
backwards,
upside_down,
};
d.add(&Space::Model, Common::default(), EntityKind::Text(text)).unwrap();
let l = build(&d, &Space::Model, &Options::default());
@@ -122,6 +134,28 @@ fn fit_text_extents_hold_the_drawn_text_when_the_span_is_too_short_to_fit() {
assert!(drawn_and_extents(cadcraft_doc::HAlign::Fit, "AB", 1.0, 1.0, 1e-7).0.width() > 1e-7 * 1000.0);
}
/// Backwards and upside-down text over two points is mirrored about its first point, along the
/// line through both. The drawing's extents are mirrored the same way: they are the plain text's
/// extents reflected, as what is drawn is the plain text reflected.
#[test]
fn mirrored_aligned_and_fit_text_extents_follow_the_drawn_text() {
use cadcraft_doc::HAlign::{Aligned, Fit};
use cadcraft_geom::{Bounds2, Vec2};
let near = |a: Bounds2, b: Bounds2| a.min.dist(b.min) < 1e-9 && a.max.dist(b.max) < 1e-9;
for halign in [Aligned, Fit] {
// The first point is the origin and the second is at +x.
let (plain_drawn, plain_extents) = drawn_and_extents(halign, "AB", 1.0, 1.0, 10.0);
assert!(plain_drawn.min.x > -1e-9 && plain_drawn.max.y > 0.5, "{halign:?}: drawn {plain_drawn:?}");
for flags in [(true, false), (false, true), (true, true)] {
let (mx, my) = (if flags.0 { -1.0 } else { 1.0 }, if flags.1 { -1.0 } else { 1.0 });
let reflect = |b: Bounds2| Bounds2::from_points([b.min, b.max].map(|p| Vec2::new(p.x * mx, p.y * my)));
let (drawn, extents) = mirrored_drawn_and_extents(halign, "AB", 1.0, 1.0, 10.0, flags);
assert!(near(drawn, reflect(plain_drawn)), "{halign:?} {flags:?}: drawn {drawn:?}, plain {plain_drawn:?}");
assert!(near(extents, reflect(plain_extents)), "{halign:?} {flags:?}: extents {extents:?}, plain {plain_extents:?}");
}
}
}
/// The extents estimate a line's width where the fonts measure it, so near the fonts' cutoffs
/// the two could decide differently. The extents then take the larger outcome, and stay near
/// what is drawn in each of these cases.