diff --git a/crates/svg/src/export.rs b/crates/svg/src/export.rs index bfd0100316..b642446c83 100644 --- a/crates/svg/src/export.rs +++ b/crates/svg/src/export.rs @@ -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 { + 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 { + 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) { @@ -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!("")); return; } diff --git a/crates/svg/tests/roundtrip.rs b/crates/svg/tests/roundtrip.rs index 8439e720f3..8d45a689c8 100644 --- a/crates/svg/tests/roundtrip.rs +++ b/crates/svg/tests/roundtrip.rs @@ -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("