mirror of
https://github.com/storytold/cadcraft.git
synced 2026-11-29 17:53:59 +01:00
* #1336: test that the DXF LAYER table stops at the record cap The LAYER, LTYPE, STYLE and DIMSTYLE tables had no record cap, unlike VIEW, UCS and VPORT. The test reads a DXF with over a million LAYER records and checks the layer list stays at the cap; without the guard it holds every record (1000102 layers). The other three tables scan their lists linearly per record, so a million-record file is too slow to build in a test and they stay uncovered. * #651: test that small filled text is simplified on screen and keeps full detail in plots Filled TrueType text was triangulated at full outline detail at every zoom, which froze the canvas on pages of small text. The test draws four O glyphs in a font built in the test itself (a 128-point ring registered through `cadcraft_fonts::ttf::register`), so it needs no installed font and no font file in the repository, and it never skips. At 10 px per cap height the rings are simplified and the shapes keep their extent; with an em under two pixels each glyph is the ink's box, two triangles; a plot keeps every point. Without the fix the on-screen build has the plot's 1024 triangles. `crates/render/src/text.rs` gains the same policy without any font: `detail` returns Full for a plot, Box for an em under two pixels and Reduced for a small one, and `simplify` drops the points of a flattened curve at a coarse band and keeps them at a fine one. * #618: test that OFFSET takes a clicked distance and picks its source under grid snap OFFSET only accepted a typed distance, and with grid snap on a click meant to pick the source object was moved to the nearest grid point. The engine test clicks two points for the distance and checks the second-point prompt and its base, the guide line in the preview, OFFSETDIST and the resulting offset. The canvas test does the same through grid-snapped clicks, then picks the source line and checks the side click puts the copy 5 units up, that OFFSET asks for the next object and that Enter ends it. Both live in the per-crate integration-test binary (#1989). Without the fix the engine and canvas tests stay at the distance prompt; with only the cursor's point-prompt test reverted the canvas test never picks the line. * Tests: one synthetic TrueType builder for render and PDF, and the exact LAYER cap Sonar's duplicate-lines gate flags 84 of the 334 new lines (25.15% against a 3% limit): the render level-of-detail fixture's `fixture_font_bytes` and the PDF writer's `test_font` build the same sfnt tables, so the two suites carried a copy each. Both now build their fonts with `crates/fonts/tests/support/sfnt.rs`, which the two suites include with `#[path]`. The fonts layer owns the table format it reads (`crates/fonts/src/ttf.rs`), so the skeleton lives beside its tests and neither consumer is turned into a published API or a dependency; no Cargo entry changes. The glyph shapes (`ring`), the cmap ranges and the metrics each font records stay at the caller: the render fixture keeps its two 128-point contours with the counter wound the other way, its 1000-unit em and 700-unit cap height, and the PDF fixtures keep their three fonts. The font files are byte-identical before and after the refactor: render 1704 bytes, sha256 83b9914cdf6e704df1ca5851ecefc98708c947b0d5be818cf5b6282761d31db0; PDF embeddable 484 bytes 2711cc7394b2057ba7e6bee22bb25a6c25edd4ecec0d35ba326759a04fcdefb9 and `fsType=2` restricted 484 bytes 3e6b534115429e8cbfdefd97e40ebdeb874222fcd7929f69b02d261cef622db4, one byte apart at OS/2 `fsType`; and the deliberately short `hmtx` 476 bytes dbcc548f59a4d3e414deb959014b8548632a7fa66fe12201895cdcbe6e66a000, whose `hhea` still declares three metrics while `hmtx` holds one. The DXF LAYER cap test asserted `<= 1_000_000` and `> 900_000`, which lets up to 99,999 valid records go missing unnoticed. It now asserts exactly the million layers the parser keeps, written as a literal so a changed constant fails the test instead of following it, with a records-specific message. Removing the guard reads 1000101 layers; a cap of 950,000 reads 950,000.
123 lines
5.1 KiB
Rust
123 lines
5.1 KiB
Rust
//! OFFSET Erase/Layer/Multiple/Undo and the MOVE/COPY Displacement option at the command line.
|
|
|
|
use cadcraft_engine::Input;
|
|
use cadcraft_engine::Session;
|
|
use cadcraft_engine::doc::EntityKind;
|
|
use cadcraft_engine::geom::Vec2;
|
|
use serde_json::json;
|
|
|
|
/// Every line as ((ax, ay), (bx, by), layer), sorted by the first y.
|
|
fn lines(s: &Session) -> Vec<((f64, f64), (f64, f64), String)> {
|
|
let mut v: Vec<_> = s
|
|
.doc()
|
|
.unwrap()
|
|
.model
|
|
.iter()
|
|
.filter_map(|e| if let EntityKind::Line(l) = &e.kind { Some(((l.a.x, l.a.y), (l.b.x, l.b.y), e.common.layer.clone())) } else { None })
|
|
.collect();
|
|
v.sort_by(|a, b| a.0.1.total_cmp(&b.0.1).then(a.0.0.total_cmp(&b.0.0)));
|
|
v
|
|
}
|
|
|
|
fn one_line() -> Session {
|
|
let mut s = Session::new();
|
|
s.cmdline("LINE 0,0 10,0").unwrap();
|
|
s.cmdline("").unwrap();
|
|
s
|
|
}
|
|
|
|
fn l(a: (f64, f64), b: (f64, f64), layer: &str) -> ((f64, f64), (f64, f64), String) {
|
|
(a, b, layer.to_string())
|
|
}
|
|
|
|
#[test]
|
|
fn offset_erase_source() {
|
|
let mut s = one_line();
|
|
s.script("OFFSET E Y 2 5,0 5,1\n\n").unwrap();
|
|
assert!(s.running.is_none(), "{}", s.prompt_text());
|
|
assert_eq!(lines(&s), vec![l((0.0, 2.0), (10.0, 2.0), "0")]);
|
|
// The choice is remembered: the next OFFSET erases the source too, until No.
|
|
s.script("OFFSET 2 5,2 5,3\n\n").unwrap();
|
|
assert_eq!(lines(&s), vec![l((0.0, 4.0), (10.0, 4.0), "0")]);
|
|
s.script("OFFSET E N 2 5,4 5,5\n\n").unwrap();
|
|
assert_eq!(lines(&s).len(), 2);
|
|
}
|
|
|
|
#[test]
|
|
fn offset_layer_current() {
|
|
let mut s = one_line();
|
|
s.execute("layer.new", &json!({ "name": "Other", "current": true })).unwrap();
|
|
s.script("OFFSET L C 2 5,0 5,1\n\n").unwrap();
|
|
assert!(s.running.is_none(), "{}", s.prompt_text());
|
|
assert_eq!(lines(&s), vec![l((0.0, 0.0), (10.0, 0.0), "0"), l((0.0, 2.0), (10.0, 2.0), "Other")]);
|
|
// Source (the default) keeps the source object's layer.
|
|
s.script("OFFSET L S 2 5,0 5,-1\n\n").unwrap();
|
|
assert_eq!(lines(&s).first(), Some(&l((0.0, -2.0), (10.0, -2.0), "0")));
|
|
}
|
|
|
|
#[test]
|
|
fn offset_multiple_and_undo() {
|
|
let mut s = one_line();
|
|
// Multiple: each side point offsets the newest copy again; Undo takes the last one back.
|
|
s.script("OFFSET 2 5,0 M 5,1 5,3 5,5 U\n\n\n").unwrap();
|
|
assert!(s.running.is_none(), "{}", s.prompt_text());
|
|
assert_eq!(lines(&s), vec![l((0.0, 0.0), (10.0, 0.0), "0"), l((0.0, 2.0), (10.0, 2.0), "0"), l((0.0, 4.0), (10.0, 4.0), "0")]);
|
|
}
|
|
|
|
// Regression test for #618: OFFSET's distance prompt took only a typed number; clicking two points
|
|
// (the distance between them) was rejected, and the second click had no distance guide.
|
|
#[test]
|
|
fn offset_distance_from_two_clicked_points() {
|
|
let mut s = one_line();
|
|
s.start("offset").unwrap();
|
|
s.input(Input::Point(Vec2::new(20.0, 20.0))).unwrap();
|
|
let p = s.current_prompt().unwrap();
|
|
assert!(p.message.contains("second point"), "{}", s.prompt_text());
|
|
assert!(p.base.is_some_and(|b| b.near(Vec2::new(20.0, 20.0), 1e-9)), "the second click is measured from the first");
|
|
// The guide from the first click to the cursor is the previewed distance.
|
|
let guide = s.preview(Vec2::new(20.0, 22.5));
|
|
assert!(matches!(guide.first().map(|e| &e.kind), Some(EntityKind::Line(g)) if g.a.xy().near(Vec2::new(20.0, 20.0), 1e-9)), "{guide:?}");
|
|
s.input(Input::Point(Vec2::new(20.0, 22.5))).unwrap();
|
|
assert!(s.current_prompt().unwrap().message.contains("Select object"), "{}", s.prompt_text());
|
|
assert_eq!(s.doc().unwrap().header.f64("OFFSETDIST", -1.0), 2.5);
|
|
s.script("5,0 5,1\n\n").unwrap();
|
|
assert!(s.running.is_none(), "{}", s.prompt_text());
|
|
assert_eq!(lines(&s), vec![l((0.0, 0.0), (10.0, 0.0), "0"), l((0.0, 2.5), (10.0, 2.5), "0")]);
|
|
}
|
|
|
|
#[test]
|
|
fn offset_json_erase_and_layer() {
|
|
let mut s = one_line();
|
|
s.execute("layer.new", &json!({ "name": "Other", "current": true })).unwrap();
|
|
let h = s.doc().unwrap().model.iter().next().unwrap().handle.hex();
|
|
s.execute("offset", &json!({ "handle": h, "distance": 2, "side": [5, 1], "erase": true, "layer": "current" })).unwrap();
|
|
assert_eq!(lines(&s), vec![l((0.0, 2.0), (10.0, 2.0), "Other")]);
|
|
}
|
|
|
|
#[test]
|
|
fn move_displacement() {
|
|
// The Displacement keyword asks for the displacement directly.
|
|
let mut s = one_line();
|
|
s.script("MOVE 5,0 D 3,4\n").unwrap();
|
|
assert!(s.running.is_none(), "{}", s.prompt_text());
|
|
assert_eq!(lines(&s), vec![l((3.0, 4.0), (13.0, 4.0), "0")]);
|
|
// Enter at the base point prompt does the same.
|
|
let mut s = one_line();
|
|
s.script("MOVE 5,0\n\n\n3,4\n").unwrap();
|
|
assert!(s.running.is_none(), "{}", s.prompt_text());
|
|
assert_eq!(lines(&s), vec![l((3.0, 4.0), (13.0, 4.0), "0")]);
|
|
// Base and second point still work.
|
|
let mut s = one_line();
|
|
s.script("MOVE 5,0 1,1 2,3\n").unwrap();
|
|
assert!(s.running.is_none(), "{}", s.prompt_text());
|
|
assert_eq!(lines(&s), vec![l((1.0, 2.0), (11.0, 2.0), "0")]);
|
|
}
|
|
|
|
#[test]
|
|
fn copy_displacement() {
|
|
let mut s = one_line();
|
|
s.script("COPY 5,0 D 0,3\n").unwrap();
|
|
assert!(s.running.is_none(), "{}", s.prompt_text());
|
|
assert_eq!(lines(&s), vec![l((0.0, 0.0), (10.0, 0.0), "0"), l((0.0, 3.0), (10.0, 3.0), "0")]);
|
|
}
|