Files
cadcraft/crates/render/tests/it/annotative_viewport.rs
T
SlopapaloozaandClaude Opus 5.5 c6016720ee TEXT's own Backwards and Upside down (DXF 71 generation flags)
TEXT, ATTDEF and ATTRIB have their own Backwards and Upside down, as AutoCAD's do (DXF group
71: 2 backwards, 4 upside down). New TEXT and attribute definitions take them from their style;
changing the style afterwards leaves existing text as it is. They were the style's only, so
text read from a file with its own flags was drawn as its style said.

- Model: `Text::backwards` and `Text::upside_down` (serde default false).
- DXF: read from 71 (the object's own groups, before an embedded MTEXT) and written when set.
- Render: drawn from the text's flags; the extents box mirrors with them, so zooming to
  extents frames mirrored text. Linetype text keeps its style's.
- Properties: a Misc group with Upside down and Backwards (`properties.describe`, and
  `properties.set` takes `backwards` and `upsideDown`), for several texts at once with *VARIES*.
- ATTDEF builds its text as TEXT does, so it also takes the style's width factor and obliquing.
- Round trip: #2237's matrices compare both flags, with DXF and DWG cases for them. Its
  mirrored-style cases mirror the text by the text's own flags, and each mirrored style
  also has text whose flags differ, so a style's flags can't leak into its text's.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-30 09:19:23 +01:00

89 lines
3.4 KiB
Rust
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
//! Annotative objects seen through layout viewports (#468): they draw at their paper size in
//! every viewport whose scale they support, and not at all in others when ANNOALLVISIBLE is 0.
use cadcraft_doc::{AnnoScale, Common, Drawing, EntityKind, Space, Text, Viewport};
use cadcraft_geom::{Bounds2, Vec3};
use cadcraft_render::{Options, VIEWPORT_CONTENT, build};
/// A viewport on Layout1, 100 × 100 paper units, showing a model window `100 × scale`.
fn viewport(x: f64, scale: f64) -> Viewport {
serde_json::from_value(serde_json::json!({
"center": [x, 50.0, 0.0],
"width": 100.0,
"height": 100.0,
"viewCenter": [0.0, 0.0],
"viewHeight": 100.0 * scale,
"id": 2,
}))
.unwrap()
}
/// A metric drawing with annotative text 2.5 high on paper supporting 1:50 and 1:100, seen
/// through one viewport at `scale`.
fn drawing(scale: f64, all_visible: bool) -> Drawing {
let mut d = Drawing::new_metric();
d.header.set_i64("ANNOALLVISIBLE", i64::from(all_visible));
let anno = Common { anno_scales: vec![AnnoScale::new("1:50", 1.0, 50.0), AnnoScale::new("1:100", 1.0, 100.0)], ..Default::default() };
let text = Text {
insert: Vec3::ZERO,
align_pt: None,
height: 2.5,
value: "ROOM".into(),
rotation: 0.0,
width_factor: 1.0,
oblique: 0.0,
style: "Standard".into(),
halign: Default::default(),
valign: Default::default(),
backwards: false,
upside_down: false,
};
d.add(&Space::Model, anno, EntityKind::Text(text)).unwrap();
d.add(&Space::Paper("Layout1".into()), Common::default(), EntityKind::Viewport(viewport(50.0, scale))).unwrap();
d
}
/// Bounds of the model-space content drawn in the viewport, in paper units.
fn content(d: &Drawing) -> Bounds2 {
let l = build(d, &Space::Paper("Layout1".into()), &Options::default());
let mut b = Bounds2::EMPTY;
for p in l.prims.iter().filter(|p| p.handle == VIEWPORT_CONTENT) {
for q in l.points(p) {
b.add(*q);
}
}
b
}
#[test]
fn same_paper_height_at_every_supported_scale() {
let at_50 = content(&drawing(50.0, false));
let at_100 = content(&drawing(100.0, false));
assert!(!at_50.is_empty() && !at_100.is_empty());
assert!((at_50.height() / at_100.height() - 1.0).abs() < 1e-9, "{} vs {}", at_50.height(), at_100.height());
assert!((at_50.width() / at_100.width() - 1.0).abs() < 1e-9);
// Glyphs span most of the 2.5 text height on paper.
assert!(at_50.height() > 1.5 && at_50.height() < 3.5, "{}", at_50.height());
}
#[test]
fn unsupported_viewport_scale_hides_or_shows_at_the_objects_scale() {
assert!(content(&drawing(20.0, false)).is_empty());
// ANNOALLVISIBLE 1: drawn at its first scale (1:50), so 50/20 = 2.5× its paper size here.
let shown = content(&drawing(20.0, true));
let at_50 = content(&drawing(50.0, false));
assert!((shown.height() / at_50.height() - 2.5).abs() < 1e-9, "{} vs {}", shown.height(), at_50.height());
}
#[test]
fn model_space_uses_cannoscale() {
let mut d = drawing(50.0, false);
let list = |d: &Drawing| build(d, &Space::Model, &Options::default()).prims.len();
// CANNOSCALE 1:1 isn't supported: hidden.
assert_eq!(list(&d), 0);
d.header.set_str("CANNOSCALE", "1:100");
assert!(list(&d) > 0);
let b = build(&d, &Space::Model, &Options::default()).bounds;
assert!(b.height() > 150.0 && b.height() < 350.0, "{}", b.height());
}