This commit is contained in:
bflatastic committed 2026-11-18 19:30:36 +07:00
commit a97e4ffa7d
10 files changed
+97 -3

No files matched your search

@@ -270,6 +270,7 @@ fn save_writes_svg_and_remembers_its_options() {
s.execute("document.save", &json!({})).unwrap();
let text = std::fs::read_to_string(&path).unwrap();
assert!(text.contains("<style>") && vectorcraft_testkit::svg_shapes(&text) == 2, "Save reuses the options: {text}");
assert!(text.contains("<rect") && text.contains("<circle"), "simple shapes stay elements: {text}");
assert!(s.execute("document.save", &json!({"path": dir.join("x.png").to_string_lossy()})).is_err(), "PNG is an export");
// A native save switches back.
let native = dir.join("doc.vectorcraft").to_string_lossy().to_string();
+1 -1
View File
@@ -63,7 +63,7 @@ fn a_chosen_artboard_s_svg_holds_only_its_own_art() {
let (first, third) = (x(&s, 0), x(&s, 2));
s.execute("shape.rectangle", &json!({"x": first + 10.0, "y": 10, "width": 30, "height": 30})).unwrap();
s.execute("shape.ellipse", &json!({"x": third + 10.0, "y": 10, "width": 30, "height": 30})).unwrap();
// A rectangle has straight sides only; the ellipse is curves.
// A rectangle has straight sides only; the ellipse is a circle or curves.
let shapes = |svg: &str| (vectorcraft_testkit::svg_shapes(svg), svg.contains(" C") || svg.contains("<ellipse") || svg.contains("<circle"));
let text = |v: &serde_json::Value| v["text"].as_str().unwrap().to_string();
let third_only = text(&s.execute("document.serialize", &json!({"format": "svg", "useArtboards": true, "range": "3"})).unwrap());
+2
View File
@@ -70,6 +70,8 @@ fn a_clipping_paths_fill_and_stroke_show_in_render_svg_and_pdf() {
assert_eq!(rgb(&doc, 45), [255, 255, 255]);
let svg = vectorcraft_svg::export(&doc, &Default::default());
assert_eq!(vectorcraft_testkit::svg_shapes(&svg), 2, "the clip region and the red art: {svg}");
assert_eq!(svg.matches("<path").count(), 1, "the clip region stays a path: {svg}");
assert!(svg.contains("<rect"), "the red art: {svg}");
}
#[test]
+2
View File
@@ -439,6 +439,8 @@ fn export_selection_crops_to_the_selection() {
let svg = s.execute("document.exportSelection", &json!({"format": "svg"})).unwrap();
let text = String::from_utf8(vectorcraft_format::base64_decode(svg["dataBase64"].as_str().unwrap()).unwrap()).unwrap();
assert_eq!(vectorcraft_testkit::svg_shapes(&text), 1, "{text}");
assert_eq!(text.matches("<rect").count(), 1, "{text}");
assert!(!text.contains("<path"), "{text}");
}
/// The crop of a selection export takes in the object's effects: a drop shadow, an outer glow and
+1 -1
View File
@@ -1078,7 +1078,7 @@ fn svg_export_writes_pattern_def() {
// The tile art is inside the pattern.
let start = svg.find("<pattern").unwrap();
let end = svg.find("</pattern>").unwrap();
assert!(vectorcraft_testkit::svg_shapes(&svg[start..end]) > 0);
assert!(vectorcraft_testkit::svg_shapes(&svg[start..end]) > 0, "{svg}");
}
/// A pattern fill is written as a PDF tiling pattern, which PDF import reads back as a pattern
+2
View File
@@ -160,6 +160,8 @@ fn crop_marks_effect_follows_the_object() {
// SVG export and Expand Appearance draw the marks as shapes.
let svg = vectorcraft_svg::export(&s.doc().unwrap().doc, &Default::default());
assert_eq!(vectorcraft_testkit::svg_shapes(&svg), 9, "{svg}");
assert_eq!(svg.matches("<line").count(), 8, "eight trim marks: {svg}");
assert_eq!(svg.matches("<rect").count(), 1, "the marked object: {svg}");
run(&mut s, "effect.expandAppearance", json!({}));
let n = node(&s, a);
assert!(n.appearance.effects.is_empty() && n.children().unwrap().len() == 2, "a group of the object and its marks");
+1 -1
View File
@@ -48,7 +48,7 @@ fn many_css_styles_preserve_first_use_numbers() {
for (i, path) in paths.iter().enumerate() {
let number = if i < 256 { i + 1 } else { [1, 129, 256][i - 256] };
let expected = if styling == Styling::InternalCss { format!("class=\"cls-{number}\"") } else { format!("style=\"&st{number};\"") };
assert!(path.contains(&expected), "path {i} should reference {expected}: {path}");
assert!(path.contains(&expected), "shape {i} should reference {expected}: {path}");
}
assert_eq!(svg.matches(definition_marker).count(), 256, "{styling:?}");
}
+7
View File
@@ -747,6 +747,13 @@ fn import_basic_shapes() {
assert_eq!(a[3].appearance.fill_paint().color().unwrap().to_hex(), "#008000");
assert!(!a[4].path_data().unwrap().is_closed());
assert_eq!(a[5].path_data().unwrap().anchor_count(), 2);
let s = export(&d, &ExportOptions::default());
assert!(s.contains("<rect "), "{s}");
assert!(s.contains("<circle "), "{s}");
assert!(s.contains("<ellipse "), "{s}");
assert!(s.contains("<polygon "), "a straight triangle stays a polygon: {s}");
assert!(s.contains("<polyline "), "{s}");
assert!(s.contains("<line "), "{s}");
}
#[test]
+79
View File
@@ -0,0 +1,79 @@
//! A rectangle, circle, or ellipse exports as that element only when the path is that shape.
//! A bowtie and a repeated corner are straight segments, so they are polygons, not rectangles.
//! A near-ellipse stays a path. resvg must paint the real shapes the way the canvas does.
#![allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)]
use kurbo::Vec2;
use vectorcraft_color::{Color, Paint};
use vectorcraft_doc::{Appearance, AppearanceItem, Document, FillLayer, Node};
use vectorcraft_geom::{PathData, Point, Rect, SubPath, shapes};
use vectorcraft_svg::{ExportOptions, export};
use vectorcraft_testkit::raster::{Image, assert_similar, render_artboard};
fn filled(path: PathData) -> Document {
let mut d = Document::new(120.0, 90.0);
let n = Node::path(
d.alloc_id(),
path,
Appearance { items: vec![AppearanceItem::Fill(FillLayer::new(Paint::solid(Color::BLACK)))], ..Default::default() },
);
let layer = d.layers[0].id;
d.insert(Some(layer), 0, n).unwrap();
d
}
fn resvg_render(svg: &str) -> Image {
let tree = resvg::usvg::Tree::from_str(svg, &resvg::usvg::Options::default()).expect("parse");
let mut pm = resvg::tiny_skia::Pixmap::new(120, 90).unwrap();
pm.fill(resvg::tiny_skia::Color::WHITE);
resvg::render(&tree, resvg::tiny_skia::Transform::identity(), &mut pm.as_mut());
Image { width: 120, height: 90, rgba: pm.data().to_vec() }
}
fn paints_like_the_canvas(doc: &Document, svg: &str) {
assert_similar(&render_artboard(doc), &resvg_render(svg), 24.0, 0.002);
}
#[test]
fn a_rectangle_circle_and_ellipse_export_as_those_elements() {
let rect = filled(shapes::rectangle(Rect::new(10.0, 10.0, 60.0, 40.0)));
let rect_svg = export(&rect, &ExportOptions::default());
assert!(rect_svg.contains("<rect "), "{rect_svg}");
paints_like_the_canvas(&rect, &rect_svg);
let circle = filled(shapes::ellipse(Rect::new(20.0, 15.0, 70.0, 65.0)));
let circle_svg = export(&circle, &ExportOptions::default());
assert!(circle_svg.contains("<circle "), "{circle_svg}");
paints_like_the_canvas(&circle, &circle_svg);
let ellipse = filled(shapes::ellipse(Rect::new(15.0, 20.0, 100.0, 60.0)));
let ellipse_svg = export(&ellipse, &ExportOptions::default());
assert!(ellipse_svg.contains("<ellipse "), "{ellipse_svg}");
paints_like_the_canvas(&ellipse, &ellipse_svg);
}
#[test]
fn a_bowtie_and_a_repeated_corner_are_polygons_and_a_near_ellipse_stays_a_path() {
let bowtie = filled(PathData::single(SubPath::polyline(
&[Point::new(10.0, 10.0), Point::new(60.0, 40.0), Point::new(60.0, 10.0), Point::new(10.0, 40.0)],
true,
)));
let bowtie_svg = export(&bowtie, &ExportOptions::default());
assert!(bowtie_svg.contains("<polygon "), "{bowtie_svg}");
assert!(!bowtie_svg.contains("<rect ") && !bowtie_svg.contains("<path "), "{bowtie_svg}");
let triangle = filled(PathData::single(SubPath::polyline(
&[Point::new(10.0, 10.0), Point::new(60.0, 10.0), Point::new(60.0, 40.0), Point::new(60.0, 40.0)],
true,
)));
let triangle_svg = export(&triangle, &ExportOptions::default());
assert!(triangle_svg.contains("<polygon "), "{triangle_svg}");
assert!(!triangle_svg.contains("<rect "), "{triangle_svg}");
let mut path = shapes::ellipse(Rect::new(40.0, 40.0, 42.0, 42.0));
path.subpaths[0].anchors[0].h_out += Vec2::new(0.75, 0.0);
let near = filled(path);
let near_svg = export(&near, &ExportOptions::default());
assert!(near_svg.contains("<path "), "{near_svg}");
assert!(!near_svg.contains("<circle ") && !near_svg.contains("<ellipse "), "{near_svg}");
}
@@ -568,6 +568,7 @@ mod tests {
assert!(app.ui.dialog.is_none());
assert_eq!(written.borrow()[1].0, "/tmp/doc.svg");
assert!(text(&written, 1).contains("style=\"fill:") && vectorcraft_testkit::svg_shapes(&text(&written, 1)) == 2);
assert!(text(&written, 1).contains("<rect") && text(&written, 1).contains("<circle"));
// Save a Copy (options given) leaves the document's path alone.
app.run("file.saveCopy", json!({"path": "/tmp/copy.svg", "svg": {"minify": true}})).unwrap();
assert!(!text(&written, 2).trim_end().contains('\n') && !text(&written, 2).contains("<?xml"), "minified");