//! Custom geometry (`a:custGeom`): guide formula evaluation and conversion of `a:pathLst` to //! an `M L C Q Z` path syntax ([`CustomPath`]), and back. //! //! Elements are matched by local name, so the reader takes a tree parsed with any //! [`craft_xml::Names`] mode. The writer uses the `a:` prefix. //! //! Formulas follow the ECMA-376 shape guide semantics (§20.1.9.11): angles are in 1/60 000 degree, //! and every operand is a guide name, a built-in variable or an integer literal. use std::collections::HashMap; use std::f64::consts::PI; use std::fmt::Write as _; use craft_xml::{El, Local, Writer}; use serde::{Deserialize, Serialize}; use crate::preset::FillMode; /// A connection site of a custom geometry (`a:cxnLst`): where a connector end glues. `x` and `y` /// are fractions of the shape's box (0 = left/top, 1 = right/bottom; outside that range is /// allowed), so the site keeps its place when the shape is resized. #[derive(Clone, Copy, Debug, Default, PartialEq, Serialize, Deserialize)] #[serde(default, rename_all = "camelCase")] pub struct ConnectionSite { pub x: f64, pub y: f64, } impl ConnectionSite { pub const fn new(x: f64, y: f64) -> Self { ConnectionSite { x, y } } } /// One sub-path of a custom geometry: SVG-like commands in path units. #[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] #[serde(default, rename_all = "camelCase")] pub struct CustomPath { pub w: f64, pub h: f64, /// `M x y`, `L x y`, `C x1 y1 x2 y2 x y`, `Q x1 y1 x y`, `Z`. pub d: String, pub fill: FillMode, pub stroke: bool, } impl CustomPath { /// A path from a `kurbo` path given in a `w` × `h` box (points), its coordinates kept as /// they are. Quadratic segments become cubics: not every reader handles `a:quadBezTo`. pub fn from_bez(bp: &kurbo::BezPath, w: f64, h: f64, filled: bool) -> CustomPath { let mut d = String::new(); let mut last = kurbo::Point::ZERO; let mut put = |c: char, pts: &[kurbo::Point]| { if !d.is_empty() { d.push(' '); } d.push(c); for p in pts { let _ = write!(d, " {} {}", finite(p.x), finite(p.y)); } }; for el in bp.elements() { match *el { kurbo::PathEl::MoveTo(a) => { put('M', &[a]); last = a; } kurbo::PathEl::LineTo(a) => { put('L', &[a]); last = a; } kurbo::PathEl::QuadTo(a, b) => { let c1 = last + (a - last) * (2.0 / 3.0); let c2 = b + (a - b) * (2.0 / 3.0); put('C', &[c1, c2, b]); last = b; } kurbo::PathEl::CurveTo(a, b, c) => { put('C', &[a, b, c]); last = c; } kurbo::PathEl::ClosePath => put('Z', &[]), } } CustomPath { w, h, d, fill: if filled { FillMode::Norm } else { FillMode::None }, stroke: true } } } impl Default for CustomPath { fn default() -> Self { CustomPath { w: 0.0, h: 0.0, d: String::new(), fill: FillMode::Norm, stroke: true } } } const DEG: f64 = 60_000.0; /// Distinct path coordinate spaces whose guides are evaluated for one custom geometry; paths in /// further spaces are left out. const MAX_SPACES: usize = 16; /// Guide-name bytes all cached spaces may hold between them; the first space counts towards it but is always kept. /// Every space copies the names, which the XML limits bound only once. const MAX_SPACE_NAME_BYTES: usize = 4 << 20; /// Guide variables for one shape. pub struct Guides { vars: HashMap, } fn to_rad(a: f64) -> f64 { a / DEG * PI / 180.0 } fn to_ang(r: f64) -> f64 { r * 180.0 / PI * DEG } fn finite(v: f64) -> f64 { if v.is_finite() { v.clamp(-1.0e15, 1.0e15) } else { 0.0 } } impl Guides { /// Built-in variables for a shape `w` × `h` (in the shape's coordinate units). pub fn new(w: f64, h: f64) -> Self { let (w, h) = (finite(w), finite(h)); let ss = w.min(h); let ls = w.max(h); let mut v = HashMap::new(); let mut set = |k: &str, x: f64| { v.insert(k.to_string(), x); }; set("w", w); set("h", h); set("l", 0.0); set("t", 0.0); set("r", w); set("b", h); set("hc", w / 2.0); set("vc", h / 2.0); set("ss", ss); set("ls", ls); for d in [2.0, 3.0, 4.0, 5.0, 6.0, 8.0, 10.0, 12.0, 16.0, 32.0] { set(&format!("wd{d}"), w / d); set(&format!("hd{d}"), h / d); set(&format!("ssd{d}"), ss / d); } set("cd2", 10_800_000.0); set("cd4", 5_400_000.0); set("cd8", 2_700_000.0); set("3cd4", 16_200_000.0); set("3cd8", 8_100_000.0); set("5cd8", 13_500_000.0); set("7cd8", 18_900_000.0); Guides { vars: v } } /// Bytes of the guide names held. fn name_bytes(&self) -> usize { self.vars.keys().map(String::len).sum() } pub fn get(&self, name: &str) -> f64 { let name = name.trim(); if let Some(v) = self.vars.get(name) { return *v; } name.parse::().ok().map(finite).unwrap_or(0.0) } pub fn set(&mut self, name: &str, v: f64) { if self.vars.len() < 10_000 { self.vars.insert(name.to_string(), finite(v)); } } /// Evaluate one formula (`*/ w 1 2`). pub fn eval(&self, fmla: &str) -> f64 { let mut it = fmla.split_whitespace(); let op = it.next().unwrap_or(""); let args: Vec = it.take(3).map(|a| self.get(a)).collect(); let a = |i: usize| args.get(i).copied().unwrap_or(0.0); let div = |n: f64, d: f64| if d == 0.0 { 0.0 } else { n / d }; let r = match op { "*/" => div(a(0) * a(1), a(2)), "+-" => a(0) + a(1) - a(2), "+/" => div(a(0) + a(1), a(2)), "?:" => { if a(0) > 0.0 { a(1) } else { a(2) } } "abs" => a(0).abs(), "at2" => to_ang(a(1).atan2(a(0))), "cat2" => a(0) * a(2).atan2(a(1)).cos(), "sat2" => a(0) * a(2).atan2(a(1)).sin(), "cos" => a(0) * to_rad(a(1)).cos(), "sin" => a(0) * to_rad(a(1)).sin(), "tan" => a(0) * to_rad(a(1)).tan(), "max" => a(0).max(a(1)), "min" => a(0).min(a(1)), "mod" => (a(0) * a(0) + a(1) * a(1) + a(2) * a(2)).sqrt(), "pin" => { if a(1) < a(0) { a(0) } else if a(1) > a(2) { a(2) } else { a(1) } } "sqrt" => a(0).max(0.0).sqrt(), "val" => a(0), _ => 0.0, }; finite(r) } /// Evaluate a list of `a:gd` elements in order. pub fn eval_list(&mut self, list: Option<&El>) { let Some(list) = list else { return }; for gd in list.children(Local("gd")).take(5000) { let (Some(name), Some(f)) = (gd.attr("name"), gd.attr("fmla")) else { continue }; let v = self.eval(f); self.set(name, v); } } } fn num(s: &mut String, v: f64) { let v = finite(v); let r = (v * 1000.0).round() / 1000.0; if r == r.trunc() { let _ = write!(s, "{}", r as i64); } else { let _ = write!(s, "{r}"); } } fn pt(g: &Guides, e: &El) -> (f64, f64) { (g.get(e.attr("x").unwrap_or("0")), g.get(e.attr("y").unwrap_or("0"))) } /// Convert `a:custGeom` to custom paths. `w`/`h` is the shape size in EMU (used for guides and for /// paths that don't give their own coordinate space). pub fn read_cust_geom(cg: &El, w_emu: f64, h_emu: f64) -> Vec { let mut out = vec![]; let Some(pl) = cg.child(Local("pathLst")) else { return out }; let mut spaces: HashMap<(u64, u64), Guides> = HashMap::new(); let mut name_bytes = 0usize; let mut budget_full = false; // The cap on cached spaces saves guide evaluation: a geometry without guides has none to save. let guided = ["avLst", "gdLst"].iter().any(|n| cg.child(Local(n)).is_some_and(|l| l.children(Local("gd")).next().is_some())); for p in pl.children(Local("path")).take(1000) { let pw = p.attr_f64("w").unwrap_or(0.0); let ph = p.attr_f64("h").unwrap_or(0.0); // Guides are evaluated in the path's own space when it has one. let (gw, gh) = if pw > 0.0 || ph > 0.0 { (pw, ph) } else { (w_emu, h_emu) }; // The guide lists are evaluated once per coordinate space, not once per path. let key = (gw.to_bits(), gh.to_bits()); if !spaces.contains_key(&key) { if guided && (budget_full || spaces.len() >= MAX_SPACES) { continue; } let mut g = Guides::new(gw, gh); g.eval_list(cg.child(Local("avLst"))); g.eval_list(cg.child(Local("gdLst"))); // The first space is always kept; later ones only while the cache stays within budget. let bytes = g.name_bytes(); if guided && !spaces.is_empty() && name_bytes.saturating_add(bytes) > MAX_SPACE_NAME_BYTES { // Every space holds the same names, so no later space fits either. budget_full = true; continue; } name_bytes = name_bytes.saturating_add(bytes); spaces.insert(key, g); } let Some(g) = spaces.get(&key) else { continue }; let mut d = String::new(); let mut cur = (0.0, 0.0); let mut start = (0.0, 0.0); for c in p.elements().take(100_000) { match c.local() { "moveTo" => { if let Some(q) = c.child(Local("pt")) { cur = pt(g, q); start = cur; d.push_str("M "); num(&mut d, cur.0); d.push(' '); num(&mut d, cur.1); d.push(' '); } } "lnTo" => { if let Some(q) = c.child(Local("pt")) { cur = pt(g, q); d.push_str("L "); num(&mut d, cur.0); d.push(' '); num(&mut d, cur.1); d.push(' '); } } "cubicBezTo" => { let pts: Vec<(f64, f64)> = c.children(Local("pt")).map(|q| pt(g, q)).collect(); if let [a, b, e] = pts.as_slice() { d.push_str("C "); for (x, y) in [a, b, e] { num(&mut d, *x); d.push(' '); num(&mut d, *y); d.push(' '); } cur = *e; } } "quadBezTo" => { let pts: Vec<(f64, f64)> = c.children(Local("pt")).map(|q| pt(g, q)).collect(); if let [a, e] = pts.as_slice() { d.push_str("Q "); for (x, y) in [a, e] { num(&mut d, *x); d.push(' '); num(&mut d, *y); d.push(' '); } cur = *e; } } "arcTo" => { let wr = g.get(c.attr("wR").unwrap_or("0")); let hr = g.get(c.attr("hR").unwrap_or("0")); let st = g.get(c.attr("stAng").unwrap_or("0")); let sw = g.get(c.attr("swAng").unwrap_or("0")); cur = arc_to(&mut d, cur, wr, hr, st, sw); } "close" => { d.push_str("Z "); cur = start; } _ => {} } } let fill = match p.attr("fill").unwrap_or("norm") { "none" => FillMode::None, "lighten" => FillMode::Lighten, "lightenLess" => FillMode::LightenLess, "darken" => FillMode::Darken, "darkenLess" => FillMode::DarkenLess, _ => FillMode::Norm, }; let (ow, oh) = if pw > 0.0 || ph > 0.0 { (pw, ph) } else { (w_emu.max(1.0), h_emu.max(1.0)) }; out.push(CustomPath { w: ow, h: oh, d: d.trim_end().to_string(), fill, stroke: p.attr_bool("stroke").unwrap_or(true) }); } out } /// Ellipse parametric angle for a visual angle `a` (radians). fn param(a: f64, wr: f64, hr: f64) -> f64 { (wr * a.sin()).atan2(hr * a.cos()) } /// Append an elliptical arc as cubic Béziers; returns the end point. fn arc_to(d: &mut String, cur: (f64, f64), wr: f64, hr: f64, st: f64, sw: f64) -> (f64, f64) { if wr.abs() < 1e-9 || hr.abs() < 1e-9 || sw == 0.0 { return cur; } let st_r = to_rad(st); let sw_r = to_rad(sw).clamp(-4.0 * PI, 4.0 * PI); let t0 = param(st_r, wr, hr); let mut t1 = param(st_r + sw_r, wr, hr); // Keep the sweep direction and size of the visual angle. let full = sw_r.abs() >= 2.0 * PI - 1e-9; if full { t1 = t0 + sw_r.signum() * 2.0 * PI; } else { while sw_r > 0.0 && t1 < t0 { t1 += 2.0 * PI; } while sw_r < 0.0 && t1 > t0 { t1 -= 2.0 * PI; } } let cx = cur.0 - wr * t0.cos(); let cy = cur.1 - hr * t0.sin(); let total = t1 - t0; let n = ((total.abs() / (PI / 2.0)).ceil() as usize).clamp(1, 16); let step = total / n as f64; let k = 4.0 / 3.0 * (step / 4.0).tan(); let mut a = t0; let mut end = cur; for _ in 0..n { let b = a + step; let (ca, sa, cb, sb) = (a.cos(), a.sin(), b.cos(), b.sin()); let p1 = (cx + wr * (ca - k * sa), cy + hr * (sa + k * ca)); let p2 = (cx + wr * (cb + k * sb), cy + hr * (sb - k * cb)); end = (cx + wr * cb, cy + hr * sb); d.push_str("C "); for (x, y) in [p1, p2, end] { num(d, x); d.push(' '); num(d, y); d.push(' '); } a = b; } end } /// Parse the [`CustomPath::d`] syntax into commands. pub fn parse_path(d: &str) -> Vec<(char, Vec)> { let mut out = vec![]; let mut cmd: Option = None; let mut nums: Vec = vec![]; let flush = |cmd: Option, nums: &mut Vec, out: &mut Vec<(char, Vec)>| { if let Some(c) = cmd { let n = match c { 'M' | 'L' => 2, 'C' => 6, 'Q' => 4, _ => 0, }; if n == 0 { out.push((c, vec![])); } else { for (i, chunk) in nums.chunks(n).enumerate() { if chunk.len() == n && out.len() < 1_000_000 { // Repeated coordinates after M continue as L. let c2 = if c == 'M' && i > 0 { 'L' } else { c }; out.push((c2, chunk.to_vec())); } } } } nums.clear(); }; let mut token = String::new(); let push_tok = |token: &mut String, nums: &mut Vec| { if !token.is_empty() { if let Ok(v) = token.parse::() && v.is_finite() { nums.push(v); } token.clear(); } }; let cs: Vec = d.chars().collect(); for (i, &ch) in cs.iter().enumerate() { match ch { // An 'E' inside a number followed by a digit (or a sign and a digit) is an exponent, // not the end command. 'E' if !token.is_empty() && { let n1 = cs.get(i + 1).copied().unwrap_or(' '); let n2 = cs.get(i + 2).copied().unwrap_or(' '); n1.is_ascii_digit() || ((n1 == '-' || n1 == '+') && n2.is_ascii_digit()) } => { token.push(ch) } 'M' | 'L' | 'C' | 'Q' | 'Z' | 'm' | 'l' | 'c' | 'q' | 'z' | 'E' => { push_tok(&mut token, &mut nums); flush(cmd, &mut nums, &mut out); cmd = Some(ch.to_ascii_uppercase()); } ' ' | ',' | '\t' | '\n' => push_tok(&mut token, &mut nums), '-' if !token.is_empty() && !token.ends_with(['e', 'E']) => { push_tok(&mut token, &mut nums); token.push(ch); } c => token.push(c), } } push_tok(&mut token, &mut nums); flush(cmd, &mut nums, &mut out); out } /// Write custom paths as `a:custGeom`. Coordinates are integers in the file, so small path spaces /// are scaled up. /// The extent a shape without a transform of its own (a placeholder) is read and written with, /// in EMU: its sites keep their fractions through a round trip. pub const INHERITED_EXTENT_EMU: f64 = 21600.0; /// `a:cxnLst`: the connection sites as fractions of the shape's box, in file order (connectors /// refer to them by index, so a `cxn` without a `pos` keeps its place as the box's corner). /// Positions are in the shape's own coordinate space, `w_emu` × `h_emu` (the extent, where the /// guides are evaluated), not in a path's `w`/`h` space. pub fn read_cxn_lst(cg: &El, w_emu: f64, h_emu: f64) -> Vec { let Some(list) = cg.child(Local("cxnLst")) else { return vec![] }; let mut g = Guides::new(w_emu, h_emu); g.eval_list(cg.child(Local("avLst"))); g.eval_list(cg.child(Local("gdLst"))); let frac = |v: f64, size: f64| if size > 0.0 { finite(v / size) } else { 0.0 }; list.children(Local("cxn")) .take(1000) .map(|c| { c.child(Local("pos")) .map(|pos| { let (x, y) = pt(&g, pos); ConnectionSite::new(frac(x, w_emu), frac(y, h_emu)) }) .unwrap_or_default() }) .collect() } /// Write custom paths as `a:custGeom`. Coordinates are integers in the file, so small path spaces /// are scaled up. `sites` go out as `a:cxnLst` in the shape's space `ext` (EMU). pub fn write_cust_geom(w: &mut Writer, paths: &[CustomPath], sites: &[ConnectionSite], ext: (i64, i64)) { w.open("a:custGeom", &[]); for list in ["a:avLst", "a:gdLst", "a:ahLst"] { w.empty(list, &[]); } if sites.is_empty() { w.empty("a:cxnLst", &[]); } else { w.open("a:cxnLst", &[]); let emu = |f: f64, size: i64| (finite(f) * size as f64).round().clamp(-2.0e9, 2.0e9) as i64; for s in sites.iter().take(1000) { w.tag("a:cxn").attr("ang", "0").open(); w.tag("a:pos").attr("x", emu(s.x, ext.0)).attr("y", emu(s.y, ext.1)).empty(); w.close(); } w.close(); } w.empty("a:rect", &[("l", "l"), ("t", "t"), ("r", "r"), ("b", "b")]); w.open("a:pathLst", &[]); for p in paths { let (pw, ph) = (finite(p.w).abs(), finite(p.h).abs()); let m = pw.max(ph); let k = if m > 0.0 && m < 20_000.0 { 100_000.0 / m } else { 1.0 }; let r = |v: f64| (finite(v) * k).round().clamp(-2.0e9, 2.0e9) as i64; let fill = match p.fill { FillMode::Norm => None, FillMode::None => Some("none"), FillMode::Lighten => Some("lighten"), FillMode::LightenLess => Some("lightenLess"), FillMode::Darken => Some("darken"), FillMode::DarkenLess => Some("darkenLess"), }; w.tag("a:path").attr("w", r(pw).max(0)).attr("h", r(ph).max(0)).attr_opt("fill", fill).attr_opt("stroke", (!p.stroke).then_some("0")).open(); for (c, v) in parse_path(&p.d) { let pts: Vec<(i64, i64)> = v .chunks(2) .filter_map(|c| match c { [x, y] => Some((r(*x), r(*y))), _ => None, }) .collect(); let tag = match c { 'M' => "a:moveTo", 'L' => "a:lnTo", 'C' => "a:cubicBezTo", 'Q' => "a:quadBezTo", 'Z' => { w.empty("a:close", &[]); continue; } _ => continue, }; w.open(tag, &[]); for (x, y) in pts { w.tag("a:pt").attr("x", x).attr("y", y).empty(); } w.close(); } w.close(); } w.close(); w.close(); } #[cfg(test)] mod tests { use super::*; use craft_xml::parse; #[test] fn formulas() { let mut g = Guides::new(1000.0, 500.0); assert_eq!(g.eval("*/ w 1 2"), 500.0); assert_eq!(g.eval("+- w h 100"), 1400.0); assert_eq!(g.eval("+/ w h 3"), 500.0); assert_eq!(g.eval("?: -1 5 7"), 7.0); assert_eq!(g.eval("?: 1 5 7"), 5.0); assert_eq!(g.eval("abs -42"), 42.0); assert_eq!(g.eval("max 3 9"), 9.0); assert_eq!(g.eval("min 3 9"), 3.0); assert_eq!(g.eval("mod 3 4 0"), 5.0); assert_eq!(g.eval("pin 0 150 100"), 100.0); assert_eq!(g.eval("pin 0 -5 100"), 0.0); assert_eq!(g.eval("sqrt 16"), 4.0); assert_eq!(g.eval("val ss"), 500.0); assert!((g.eval("at2 1 1") - 2_700_000.0).abs() < 1e-6); assert!((g.eval("cos 100 5400000")).abs() < 1e-9); assert!((g.eval("sin 100 5400000") - 100.0).abs() < 1e-9); assert!((g.eval("tan 100 2700000") - 100.0).abs() < 1e-6); assert!((g.eval("cat2 10 1 0") - 10.0).abs() < 1e-9); assert!((g.eval("sat2 10 0 1") - 10.0).abs() < 1e-9); assert_eq!(g.eval("*/ 1 1 0"), 0.0, "division by zero is 0"); assert_eq!(g.eval("bogus 1 2"), 0.0); assert_eq!(g.eval(""), 0.0); g.set("x1", 7.0); assert_eq!(g.eval("+- x1 0 2"), 5.0); assert_eq!(g.get("hc"), 500.0); assert_eq!(g.get("cd4"), 5_400_000.0); } #[test] fn custgeom_with_guides_and_arc() { let x = br#" "#; let d = parse(x).unwrap(); let paths = read_cust_geom(&d, 1000.0, 1000.0); assert_eq!(paths.len(), 2); assert!(paths[0].d.starts_with("M 0 0 L 100 100 C"), "{}", paths[0].d); assert!(paths[0].d.ends_with('Z')); // The quarter arc from (100,100) with centre (50,100) ends at (50,150). assert!(paths[0].d.contains("50 150"), "{}", paths[0].d); assert_eq!(paths[1].fill, FillMode::None); assert!(!paths[1].stroke); assert_eq!(paths[1].d, "M 1 1 Q 2 2 3 1"); } #[test] fn paths_share_guides_and_distinct_spaces_are_capped() { let path = |w: usize| format!(r#""#); let paths: String = (1..=40).map(|i| path(10 * i)).collect(); let x = format!( r#"{paths}{}"#, path(10) ); let d = parse(x.as_bytes()).unwrap(); let out = read_cust_geom(&d, 1.0, 1.0); assert_eq!(out.len(), MAX_SPACES + 1, "16 spaces plus the repeat of the first"); assert!(out[0].d.starts_with("M 5 1"), "{}", out[0].d); assert!(out[MAX_SPACES].d.starts_with("M 5 1"), "{}", out[MAX_SPACES].d); } #[test] fn guide_name_bytes_across_spaces_are_budgeted() { let name = "n".repeat(3 << 20); let path = |w: usize| format!(r#""#); let x = format!( r#"{}{}{}"#, path(10), path(20), path(10) ); let d = parse(x.as_bytes()).unwrap(); let out = read_cust_geom(&d, 1.0, 1.0); assert_eq!(out.len(), 2, "the second 3 MiB space is over budget and left out; the first space's repeat stays"); } #[test] fn guide_free_geometries_are_not_capped_by_space() { let paths: String = (1..=40).map(|i| format!(r#""#, 10 * i)).collect(); let x = format!(r#"{paths}"#); let d = parse(x.as_bytes()).unwrap(); assert_eq!(read_cust_geom(&d, 1.0, 1.0).len(), 40); } #[test] fn write_then_read_path() { let p = CustomPath { w: 100.0, h: 50.0, d: "M 0 0 L 100 0 C 100 10 90 50 50 50 Q 0 50 0 0 Z".into(), fill: FillMode::Norm, stroke: true }; let mut w = Writer::new(); write_cust_geom(&mut w, std::slice::from_ref(&p), &[], (0, 0)); let d = parse(w.as_str().as_bytes()).unwrap(); let back = read_cust_geom(&d, 1.0, 1.0); assert_eq!(back.len(), 1); // Scaled by 1000 (100 → 100000). assert_eq!(back[0].w, 100_000.0); assert_eq!(back[0].d, "M 0 0 L 100000 0 C 100000 10000 90000 50000 50000 50000 Q 0 50000 0 0 Z"); } #[test] fn parse_path_uppercase_exponent() { assert_eq!(parse_path("M 1E-5 2 L 3 4"), vec![('M', vec![1e-5, 2.0]), ('L', vec![3.0, 4.0])]); assert_eq!(parse_path("M 1.5E3 2"), vec![('M', vec![1500.0, 2.0])]); assert_eq!(parse_path("M 1e-5 2"), vec![('M', vec![1e-5, 2.0])]); // An 'E' right after a number with no exponent digits is the end command. assert_eq!(parse_path("M 0 0E"), vec![('M', vec![0.0, 0.0]), ('E', vec![])]); } #[test] fn parse_path_tokens() { let v = parse_path("M0,0 L10-5 Z"); assert_eq!(v.len(), 3); assert_eq!(v[1], ('L', vec![10.0, -5.0])); assert!(parse_path("garbage ### C 1").is_empty() || parse_path("garbage").is_empty()); } fn geom(inner: &str) -> El { parse(format!(r#"{inner}"#).as_bytes()).unwrap() } #[test] fn paths_per_geometry_are_capped() { let paths = r#""#.repeat(1001); assert_eq!(read_cust_geom(&geom(&format!("{paths}")), 1.0, 1.0).len(), 1000); } #[test] fn commands_per_path_are_capped() { let cmds = "".repeat(100_001); let out = read_cust_geom(&geom(&format!(r#"{cmds}"#)), 1.0, 1.0); assert_eq!(out[0].d.matches('Z').count(), 100_000); } #[test] fn guides_per_list_and_per_shape_are_capped() { let gds: String = (0..5001).map(|i| format!(r#""#)).collect(); let list = parse(format!("{gds}").as_bytes()).unwrap(); let mut g = Guides::new(1.0, 1.0); g.eval_list(Some(&list)); assert_eq!((g.get("g4999"), g.get("g5000")), (7.0, 0.0), "the 5001st guide of a list is not read"); let mut g = Guides::new(1.0, 1.0); let built_in = g.vars.len(); for i in 0..10_001 { g.set(&format!("v{i}"), 1.0); } assert_eq!(g.vars.len(), 10_000); assert_eq!((g.get(&format!("v{}", 9_999 - built_in)), g.get(&format!("v{}", 10_000 - built_in))), (1.0, 0.0)); } #[test] fn values_are_finite_and_clamped() { let g = Guides::new(f64::NAN, f64::INFINITY); assert_eq!((g.get("w"), g.get("h")), (0.0, 0.0)); assert_eq!(g.get("1e300"), 1.0e15); assert_eq!(g.eval("*/ 1e15 1e15 1"), 1.0e15); assert_eq!(g.eval("tan 1 5400000").abs(), 1.0e15, "tan 90° is clamped, not infinite"); } #[test] fn an_arc_of_many_turns_draws_one_turn() { let out = read_cust_geom( &geom( r#""#, ), 1.0, 1.0, ); assert_eq!(out[0].d.matches('C').count(), 4); } #[test] fn parsed_coordinate_commands_are_capped() { let d = format!("M{}", " 1 2".repeat(1_000_001)); assert_eq!(parse_path(&d).len(), 1_000_000); } #[test] fn a_kurbo_path_becomes_a_custom_path_with_quads_as_cubics() { let mut bp = kurbo::BezPath::new(); bp.move_to((0.0, 0.0)); bp.line_to((30.0, 0.0)); bp.quad_to((30.0, 30.0), (0.0, 30.0)); bp.close_path(); let p = CustomPath::from_bez(&bp, 30.0, 30.0, false); assert_eq!(p.d, "M 0 0 L 30 0 C 30 20 20 30 0 30 Z"); assert_eq!(p.fill, FillMode::None); let mut w = Writer::new(); write_cust_geom(&mut w, std::slice::from_ref(&p), &[], (30, 30)); let back = read_cust_geom(&parse(w.finish().as_bytes()).unwrap(), 1.0, 1.0); assert_eq!(parse_path(&back[0].d).len(), 4); } /// `a:cxnLst` positions are in the shape's space (the extent, with its guides), not the /// path's own `w`/`h`; they come back as fractions of the box and go out again in EMU. #[test] fn connection_sites_read_in_the_shape_space_and_write_back() { let x = br#" "#; let d = parse(x).unwrap(); let sites = read_cxn_lst(&d, 1000.0, 500.0); assert_eq!( sites, vec![ConnectionSite::new(0.0, 0.5), ConnectionSite::new(1.0, 0.5), ConnectionSite::new(0.0, 0.0), ConnectionSite::new(0.5, 1.25)], "a cxn without a pos keeps its index (a connector may point past it)" ); assert!(read_cxn_lst(&d, 0.0, 0.0).iter().all(|s| s.x == 0.0 && s.y == 0.0), "a zero extent gives no division by zero"); let paths = read_cust_geom(&d, 1000.0, 500.0); let mut w = Writer::new(); write_cust_geom(&mut w, &paths, &sites, (1000, 500)); let out = w.finish(); assert!(out.contains(r#""#), "{out}"); let mut w = Writer::new(); write_cust_geom(&mut w, &paths, &[], (1000, 500)); assert!(w.finish().contains(""), "no sites: the empty list as before"); } }