mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-22 10:23:16 +01:00
Write simple rectangles, circles, and ellipses back to SVG as those elements.
Import turns those elements into paths, so export now recognizes the upright geometry and writes the original element.
This commit is contained in:
1 parent
6a16627de6
commit
7bed916595
2 files changed
+112
-4
No files matched your search
@@ -955,7 +955,102 @@ impl Writer<'_> {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// An upright rectangle or a full axis-aligned ellipse, in the same user space `path_d` writes.
|
||||
fn primitive_tag(path: &PathData, xf: Affine, mut num: impl FnMut(f64) -> String) -> Option<String> {
|
||||
let [_, b, c, _, _, _] = xf.as_coeffs();
|
||||
if b.abs() > 1e-6 || c.abs() > 1e-6 {
|
||||
return None;
|
||||
}
|
||||
let map = |p: Point| xf * p;
|
||||
if let Some(bounds) = upright_rect(path) {
|
||||
let (p0, p1) = (map(Point::new(bounds.x0, bounds.y0)), map(Point::new(bounds.x1, bounds.y1)));
|
||||
let (x, y) = (p0.x.min(p1.x), p0.y.min(p1.y));
|
||||
return Some(format!(
|
||||
"rect x=\"{}\" y=\"{}\" width=\"{}\" height=\"{}\"",
|
||||
num(x),
|
||||
num(y),
|
||||
num((p1.x - p0.x).abs()),
|
||||
num((p1.y - p0.y).abs())
|
||||
));
|
||||
}
|
||||
let (cx, cy, rx, ry) = axis_ellipse(path)?;
|
||||
let c = map(Point::new(cx, cy));
|
||||
let [a, b, c0, d, _, _] = xf.as_coeffs();
|
||||
let rx = (a * rx).hypot(b * rx);
|
||||
let ry = (c0 * ry).hypot(d * ry);
|
||||
if (rx - ry).abs() <= 1e-3 * rx.max(ry).max(1.0) {
|
||||
Some(format!("circle cx=\"{}\" cy=\"{}\" r=\"{}\"", num(c.x), num(c.y), num((rx + ry) / 2.0)))
|
||||
} else {
|
||||
Some(format!("ellipse cx=\"{}\" cy=\"{}\" rx=\"{}\" ry=\"{}\"", num(c.x), num(c.y), num(rx), num(ry)))
|
||||
}
|
||||
}
|
||||
|
||||
fn upright_rect(path: &PathData) -> Option<Rect> {
|
||||
let ([sp], Some(b)) = (path.subpaths.as_slice(), path.bounds()) else { return None };
|
||||
let corner = |p: Point| ((p.x - b.x0).abs() < 1e-3 || (p.x - b.x1).abs() < 1e-3) && ((p.y - b.y0).abs() < 1e-3 || (p.y - b.y1).abs() < 1e-3);
|
||||
(sp.closed
|
||||
&& sp.anchors.len() == 4
|
||||
&& (0..sp.segment_count()).all(|i| sp.segment_is_line(i))
|
||||
&& sp.anchors.iter().all(|a| corner(a.p))
|
||||
&& b.width() > 0.0
|
||||
&& b.height() > 0.0)
|
||||
.then_some(b)
|
||||
}
|
||||
|
||||
/// A closed four-cubic ellipse whose anchors sit on the cardinal points.
|
||||
fn axis_ellipse(path: &PathData) -> Option<(f64, f64, f64, f64)> {
|
||||
let [sp] = path.subpaths.as_slice() else { return None };
|
||||
if !sp.closed || sp.anchors.len() != 4 || (0..4).any(|i| sp.segment_is_line(i)) {
|
||||
return None;
|
||||
}
|
||||
let b = path.bounds()?;
|
||||
let (cx, cy, rx, ry) = (b.center().x, b.center().y, b.width() / 2.0, b.height() / 2.0);
|
||||
if rx < 1e-6 || ry < 1e-6 {
|
||||
return None;
|
||||
}
|
||||
let card = [Point::new(cx + rx, cy), Point::new(cx, cy + ry), Point::new(cx - rx, cy), Point::new(cx, cy - ry)];
|
||||
let tol = 0.02 * rx.max(ry);
|
||||
let mut used = [false; 4];
|
||||
let mut order = [0usize; 4];
|
||||
for (i, a) in sp.anchors.iter().enumerate() {
|
||||
let j = card.iter().position(|c| c.distance(a.p) <= tol.max(0.05))?;
|
||||
if used[j] {
|
||||
return None;
|
||||
}
|
||||
used[j] = true;
|
||||
order[i] = j;
|
||||
}
|
||||
let k = vectorcraft_geom::shapes::KAPPA;
|
||||
for i in 0..4 {
|
||||
let step = (order[(i + 1) % 4] as i32 - order[i] as i32).rem_euclid(4);
|
||||
if step != 1 && step != 3 {
|
||||
return None;
|
||||
}
|
||||
let a = &sp.anchors[i];
|
||||
let toward = |to: usize| {
|
||||
let (ox, oy) = match (order[i], to) {
|
||||
(0, 1) => (0.0, k * ry),
|
||||
(0, 3) => (0.0, -k * ry),
|
||||
(2, 3) => (0.0, -k * ry),
|
||||
(2, 1) => (0.0, k * ry),
|
||||
(1, 2) => (-k * rx, 0.0),
|
||||
(1, 0) => (k * rx, 0.0),
|
||||
(3, 0) => (k * rx, 0.0),
|
||||
(3, 2) => (-k * rx, 0.0),
|
||||
_ => (f64::NAN, f64::NAN),
|
||||
};
|
||||
Point::new(card[order[i]].x + ox, card[order[i]].y + oy)
|
||||
};
|
||||
if a.h_out.distance(toward(order[(i + 1) % 4])) > tol.max(0.75) || a.h_in.distance(toward(order[(i + 3) % 4])) > tol.max(0.75) {
|
||||
return None;
|
||||
}
|
||||
}
|
||||
Some((cx, cy, rx, ry))
|
||||
}
|
||||
|
||||
impl Writer<'_> {
|
||||
/// Paint a shape (`d`) with a node's appearance stack. `paths` is the same shape in document
|
||||
/// space, for strokes that aren't plain (it may be empty when all are).
|
||||
fn shape(&mut self, n: &Node, d: &str, paths: &[&PathData], rule: FillRule, bounds: Option<Rect>) {
|
||||
@@ -1006,6 +1101,14 @@ impl Writer<'_> {
|
||||
}
|
||||
p.extend(css::transparency(n));
|
||||
let a = self.attrs(&p);
|
||||
let dashed = items.iter().any(|i| matches!(i, AppearanceItem::Stroke(s) if s.dash.is_some()));
|
||||
if !dashed
|
||||
&& paths.len() == 1
|
||||
&& let Some(tag) = primitive_tag(paths[0], self.xf, |n| self.num(n))
|
||||
{
|
||||
self.line(&format!("<{tag}{id}{a}/>"));
|
||||
return;
|
||||
}
|
||||
self.line(&format!("<path{id} d=\"{d}\"{a}/>"));
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -69,7 +69,7 @@ fn curves_roundtrip() {
|
||||
let n = Node::path(id, shapes::ellipse(Rect::new(20.0, 30.0, 120.0, 90.0)), Appearance::default_art());
|
||||
let d = doc_with(vec![n]);
|
||||
let s = export(&d, &ExportOptions::default());
|
||||
assert!(s.contains(" C"), "{s}");
|
||||
assert!(s.contains("<ellipse "), "{s}");
|
||||
let r = roundtrip(&d);
|
||||
let a = art(&r);
|
||||
assert!(close_rect(a[0].geometric_bounds().unwrap(), Rect::new(20.0, 30.0, 120.0, 90.0), 0.01));
|
||||
@@ -326,7 +326,7 @@ fn hidden_and_guides_omitted() {
|
||||
a.visible = false;
|
||||
let b = rect_node(&mut d, Rect::new(0.0, 0.0, 20.0, 20.0), Appearance::default_art());
|
||||
let s = export(&doc_with(vec![a, b]), &ExportOptions::default());
|
||||
assert_eq!(s.matches("<path").count(), 1);
|
||||
assert_eq!(s.matches("<rect").count(), 1);
|
||||
let r = import(&s).unwrap();
|
||||
assert_eq!(art(&r).len(), 1);
|
||||
}
|
||||
@@ -501,9 +501,9 @@ fn options_decimals_minify_responsive_artboard() {
|
||||
assert!(!s.contains("<?xml"));
|
||||
assert!(!s.contains(" width=\"200\""));
|
||||
assert!(s.contains("viewBox=\"0 0 200 100\""), "{s}");
|
||||
assert!(s.contains("M10.12 20"), "{s}");
|
||||
assert!(s.contains("x=\"10.12\"") && s.contains("y=\"20\""), "{s}");
|
||||
let s = export(&d, &ExportOptions::default());
|
||||
assert!(s.contains("width=\"200\" height=\"100\"") && s.contains("M10.123 20"), "{s}");
|
||||
assert!(s.contains("width=\"200\" height=\"100\"") && s.contains("x=\"10.123\"") && s.contains("y=\"20\""), "{s}");
|
||||
// All art bounds: the rectangle and half its 1 pt stroke (right-angle miters stay inside).
|
||||
let s = export(&d, &ExportOptions { artboard: None, ..Default::default() });
|
||||
assert!(s.contains("viewBox=\"0 0 40.877 31\""), "{s}");
|
||||
@@ -560,6 +560,11 @@ 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("<path "), "a polygon stays a path: {s}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
Reference in new issue
Block a user