mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-12-04 13:02:50 -05:00
Tests: cover aligned and pattern strokes in EPS, live art in legacy .ai and top-object grids
No test failed when these writer paths broke: - EPS export of an inside or outside stroke: the clip to the path (or, even-odd, to everything outside it) before the stroke drawn at twice its width. - EPS export of a pattern stroke, written as its outline filled with the tiles instead of as a stroke. - The legacy .ai writer's live-object arm: a distorted image on a layer is written as its pieces with the live-art warning. - The .ai writer's top-object grid: the points inside the grid blended from the four sides, checked on a 2 x 2 and a 3 x 2 outline so a swapped row and column weight fails too. Each test fails with the code it covers broken and passes with it restored.
This commit is contained in:
1 parent
4002b2b66c
commit
6fc066f6b2
3 files changed
+127
No files matched your search
@@ -497,3 +497,60 @@ impl Writer<'_> {
|
||||
s.push_str("/End X!\n");
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
/// A closed outline through `points`, each side a straight segment.
|
||||
fn outline(points: &[(f64, f64)]) -> PathData {
|
||||
let mut bp = kurbo::BezPath::new();
|
||||
for (i, &(x, y)) in points.iter().enumerate() {
|
||||
if i == 0 {
|
||||
bp.move_to((x, y));
|
||||
} else {
|
||||
bp.line_to((x, y));
|
||||
}
|
||||
}
|
||||
bp.close_path();
|
||||
PathData::from_bezpath(&bp)
|
||||
}
|
||||
|
||||
fn assert_near(got: Option<Point>, want: (f64, f64)) {
|
||||
let Some(got) = got else { panic!("no grid point") };
|
||||
assert!(got.distance(Point::new(want.0, want.1)) < 1e-9, "{got:?} is not {want:?}");
|
||||
}
|
||||
|
||||
/// A top object with two segments on each side is written as a 2 × 2 grid whose middle point
|
||||
/// blends the four sides' middles less the four corners.
|
||||
#[test]
|
||||
fn a_top_object_with_two_segments_a_side_gets_its_middle_point_from_the_sides() {
|
||||
let path = outline(&[(30.0, 10.0), (100.0, 0.0), (170.0, 10.0), (180.0, 50.0), (170.0, 90.0), (100.0, 90.0), (30.0, 90.0), (30.0, 50.0)]);
|
||||
let Some((rows, cols, pts)) = top_object_grid(&path, Some([0, 2, 4, 6])) else { panic!("the outline is written as a grid") };
|
||||
assert_eq!((rows, cols), (2, 2));
|
||||
// (100, 0) and (100, 90) halved, (30, 50) and (180, 50) halved, less the corners' mean.
|
||||
assert_near(pts.get(4).map(|q| q.0), (105.0, 45.0));
|
||||
}
|
||||
|
||||
/// Three segments across and two down: each middle point is weighted by its column across and
|
||||
/// its row down, not the other way around.
|
||||
#[test]
|
||||
fn a_top_object_wider_than_it_is_tall_blends_by_column_across_and_row_down() {
|
||||
let path = outline(&[
|
||||
(0.0, 0.0),
|
||||
(40.0, -6.0),
|
||||
(80.0, 4.0),
|
||||
(120.0, 0.0),
|
||||
(126.0, 40.0),
|
||||
(120.0, 80.0),
|
||||
(80.0, 86.0),
|
||||
(40.0, 84.0),
|
||||
(0.0, 80.0),
|
||||
(-4.0, 40.0),
|
||||
]);
|
||||
let Some((rows, cols, pts)) = top_object_grid(&path, Some([0, 3, 5, 8])) else { panic!("the outline is written as a grid") };
|
||||
assert_eq!((rows, cols), (2, 3));
|
||||
assert_near(pts.get(5).map(|q| q.0), (39.0 + 1.0 / 3.0, 39.0));
|
||||
assert_near(pts.get(6).map(|q| q.0), (82.0 + 2.0 / 3.0, 45.0));
|
||||
}
|
||||
}
|
||||
@@ -722,6 +722,21 @@ fn a_distorted_image_nested_as_deep_as_the_limit_is_written_as_its_nearest_place
|
||||
assert_eq!(text.lines().filter(|l| l.ends_with(" XI")).count(), 1, "the one image");
|
||||
}
|
||||
|
||||
/// A distorted image on a layer, which the format has no form for, is written as the pieces it is
|
||||
/// drawn as, with a warning.
|
||||
#[test]
|
||||
fn a_distorted_image_on_a_layer_is_written_as_its_pieces() {
|
||||
let mut doc = Document::new(200.0, 100.0);
|
||||
let mut art = picture(&mut doc, Rect::new(10.0, 10.0, 90.0, 90.0));
|
||||
art.set_image_corners([(40.0, 10.0), (60.0, 10.0), (90.0, 90.0), (10.0, 90.0)].map(|(x, y)| vectorcraft_geom::Point::new(x, y))).unwrap();
|
||||
doc.insert(doc.default_layer(), usize::MAX, art).unwrap();
|
||||
let out = write(&doc);
|
||||
assert!(out.warnings.iter().any(|w| w == LIVE), "{:?}", out.warnings);
|
||||
assert!(!out.warnings.iter().any(|w| w == DEEP_DISTORTED), "{:?}", out.warnings);
|
||||
let text = String::from_utf8(out.bytes).unwrap();
|
||||
assert!(text.lines().filter(|l| l.ends_with(" XI")).count() > 1, "the pieces, not one image");
|
||||
}
|
||||
|
||||
/// A layer `Below` holding a clipping group (a circle clipping an image, as in #2692), a group
|
||||
/// holding an image and an image; a layer `Above` holding an orange square. Without `images`,
|
||||
/// `Below` is empty.
|
||||
|
||||
@@ -426,6 +426,61 @@ fn strokes_keep_their_caps_joins_and_dashes() {
|
||||
assert!(page.contains("0 0 0 rg\n20 20 m\n80 20 l\n80 80 l\n20 80 l\n20 20 l\nh\nS\n"), "{page}");
|
||||
}
|
||||
|
||||
/// The page for a square stroked 4 pt with `paint`, aligned `align`.
|
||||
fn aligned_stroke_page(align: vectorcraft_doc::StrokeAlign, paint: impl FnOnce(&mut Document) -> Paint) -> String {
|
||||
let d = doc_with(|d| {
|
||||
let paint = paint(d);
|
||||
let mut n = path(d, shapes::rectangle(Rect::new(20.0, 20.0, 80.0, 80.0)), Paint::None, paint, 4.0);
|
||||
for i in &mut n.appearance.items {
|
||||
if let AppearanceItem::Stroke(s) = i {
|
||||
s.align = align;
|
||||
}
|
||||
}
|
||||
vec![n]
|
||||
});
|
||||
page_of(&d, &opts(&d))
|
||||
}
|
||||
|
||||
/// Where the first line of `page` that `matches` is.
|
||||
fn line_at(page: &str, matches: impl Fn(&str) -> bool) -> Option<usize> {
|
||||
page.lines().position(matches)
|
||||
}
|
||||
|
||||
/// A stroke inside or outside a closed path is drawn at twice its width, clipped first: inside to
|
||||
/// the path, outside to everything but the path (even-odd), so only its own half shows.
|
||||
#[test]
|
||||
fn inside_and_outside_strokes_are_clipped_to_their_side_and_drawn_twice_as_wide() {
|
||||
use vectorcraft_doc::StrokeAlign;
|
||||
let black = |_: &mut Document| Paint::solid(Color::BLACK);
|
||||
for (align, clip) in [(StrokeAlign::Inside, "W"), (StrokeAlign::Outside, "W*")] {
|
||||
let page = aligned_stroke_page(align, black);
|
||||
let clip_at = line_at(&page, |l| l == clip);
|
||||
let width_at = line_at(&page, |l| l.starts_with("8 w "));
|
||||
let stroke_at = line_at(&page, |l| l == "S");
|
||||
assert!(clip_at.is_some() && clip_at < width_at && width_at < stroke_at, "{align:?}: {page}");
|
||||
let other = if clip == "W" { "W*" } else { "W" };
|
||||
assert_eq!(line_at(&page, |l| l == other), None, "{align:?}: {page}");
|
||||
}
|
||||
// Centered: the width as set, with no clip.
|
||||
let page = aligned_stroke_page(StrokeAlign::Center, black);
|
||||
assert!(line_at(&page, |l| l.starts_with("4 w ")).is_some(), "{page}");
|
||||
assert_eq!(line_at(&page, |l| l == "W" || l == "W*"), None, "{page}");
|
||||
}
|
||||
|
||||
/// A stroke painted with a pattern is written as the area it covers, filled with the pattern's
|
||||
/// tiles, not as a stroke.
|
||||
#[test]
|
||||
fn a_pattern_stroke_is_written_as_its_filled_outline() {
|
||||
let pattern = |d: &mut Document| {
|
||||
let square = path(d, shapes::rectangle(Rect::new(0.0, 0.0, 10.0, 10.0)), Paint::solid(Color::rgb(0.0, 0.0, 1.0)), Paint::None, 0.0);
|
||||
d.patterns.push(vectorcraft_doc::PatternDef::new("tile", vec![Arc::new(square)]));
|
||||
Paint::Pattern { pattern: "tile".into(), xf: Affine::IDENTITY }
|
||||
};
|
||||
let page = aligned_stroke_page(vectorcraft_doc::StrokeAlign::Center, pattern);
|
||||
assert!(page.contains("0 0 1 rg"), "the tile is painted: {page}");
|
||||
assert_eq!(line_at(&page, |l| l == "S" || l.contains("strokepath") || l.contains(" w ")), None, "{page}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn transparency_is_written_opaque_with_a_warning() {
|
||||
let d = doc_with(|d| {
|
||||
|
||||
Reference in new issue
Block a user