DesignCraft: An open-source page and layout design editor written by scratch in pure Rust
Swatches: Load Swatches refuses .ase files over 100,000 colors and merges in linear time
dotCooCoo591cb579b3bf
diff --git a/ROADMAP.md b/ROADMAP.mdindex d33353caa..f4deeef4d 100644--- a/ROADMAP.md+++ b/ROADMAP.md@@ -130,6 +130,7 @@ list) · [roadmap.md](docs/roadmap.md) (milestones, plan) · [parity-checklist.mNewest first.- **2026-10-12** · Words in capitals hyphenate like capitalized words (FERRUM COM-MODI), as in InDesign; Hyphenate Capitalized Words off keeps them whole. No readiness percentage change.+- **2026-10-12** · Load Swatches refuses an `.ase` file with more than 100,000 colors, and `swatch.load` merges a file of 100,000 colors in under 0.1 s (it took 9.6 s of CPU in a test build). No readiness percentage change.- **2026-10-11** · `transform.move {copy}`, Edit › Duplicate and Step and Repeat return the copies' ids, so a script can go on with them. No readiness percentage change.- **2026-10-11** · `object.fill` and `object.stroke` refuse a swatch name the document doesn't have (it painted nothing) and suggest the name meant (`Paper` → `[Paper]`). No readiness percentage change.- **2026-10-11 (printing):** `file.printers` and `file.print` use craft-libs/craft-print for CUPS and Windows. DesignCraft retains PDF export, page selection, spreads, marks and bleed; dry runs create no temporary PDF or print job. Windows selects matching or default driver paper and shrinks oversized sheets without enlarging smaller ones; queue duplex defaults are preserved. Headless tests exercise exported bytes, job settings, dry runs and refusals; shared native Windows CI passes, including paused-queue submission and cleanup. PostScript, production print controls and physical output validation remain open. No readiness percentage change.diff --git a/crates/color/src/ase.rs b/crates/color/src/ase.rsindex ed592bf58..ecae906fd 100644--- a/crates/color/src/ase.rs+++ b/crates/color/src/ase.rs@@ -11,8 +11,12 @@ pub enum AseError {Signature,#[error("the file ends early")]Truncated,+ #[error("the file holds more than {MAX_COLORS} colors")]+ TooManyColors,}+/// The most colors [`read`] accepts in one file.+pub const MAX_COLORS: usize = 100_000;const COLOR: u16 = 0x0001;#[cfg(test)]const GROUP_START: u16 = 0xC001;@@ -56,6 +60,7 @@ impl R<'_> {}/// The colour swatches in an `.ase` file (groups are flattened).+/// A file with more than [`MAX_COLORS`] colors is an error.pub fn read(bytes: &[u8]) -> Result<Vec<Swatch>, AseError> {let mut r = R { b: bytes, i: 0 };if r.take(4)? != b"ASEF" {@@ -63,7 +68,7 @@ pub fn read(bytes: &[u8]) -> Result<Vec<Swatch>, AseError> {}r.take(4)?; // versionlet blocks = r.u32()?;- let mut out = Vec::new();+ let (mut out, mut colors) = (Vec::new(), 0usize);for _ in 0..blocks {let kind = r.u16()?;let len = r.u32()? as usize;@@ -72,6 +77,10 @@ pub fn read(bytes: &[u8]) -> Result<Vec<Swatch>, AseError> {if kind != COLOR {continue;}+ colors += 1;+ if colors > MAX_COLORS {+ return Err(AseError::TooManyColors);+ }let mut b = R { b: body, i: 0 };let name = b.name()?;let model = b.take(4)?.to_vec();@@ -191,4 +200,18 @@ mod tests {let SwatchValue::Color { color, .. } = &w[0].value else { panic!() };assert!(color.to_rgb().iter().all(|v| *v > 0.98), "Lab white: {color:?}");}++ #[test]+ fn more_than_max_colors_is_an_error() {+ let gray = Swatch {+ name: "G".into(),+ value: SwatchValue::Color { color: Color::Gray { k: 0.5 }, color_type: ColorType::Process },+ locked: false,+ named: true,+ hidden: false,+ };+ let sw = vec![gray; MAX_COLORS + 1];+ assert_eq!(read(&write(&sw)).err(), Some(AseError::TooManyColors));+ assert_eq!(read(&write(&sw[1..])).map(|w| w.len()), Ok(MAX_COLORS), "up to the limit");+ }}diff --git a/crates/engine/src/cmd/style.rs b/crates/engine/src/cmd/style.rsindex 8a520b685..69e58b24d 100644--- a/crates/engine/src/cmd/style.rs+++ b/crates/engine/src/cmd/style.rs@@ -815,9 +815,14 @@ fn load_swatches(s: &mut Session, p: &Value) -> Result<Value> {let incoming = designcraft_color::ase::read(&bytes).map_err(|e| bad("swatch.load", e.to_string()))?;let replace = p.get("replace").and_then(Value::as_bool).unwrap_or(false);s.edit(|d, _| {+ // The position of the first swatch with each name, kept up to date as the file's colors are added.+ let mut at: std::collections::HashMap<String, usize> = std::collections::HashMap::new();+ for (i, x) in d.swatches.iter().enumerate() {+ at.entry(x.name.clone()).or_insert(i);+ }let (mut added, mut replaced) = (0, 0);for w in incoming {- match d.swatches.iter_mut().find(|x| x.name == w.name) {+ match at.get(&w.name).and_then(|&i| d.swatches.get_mut(i)) {Some(x) if x.locked => {}Some(x) => {if replace {@@ -827,6 +832,7 @@ fn load_swatches(s: &mut Session, p: &Value) -> Result<Value> {}}None => {+ at.insert(w.name.clone(), d.swatches.len());d.swatches.push(w);added += 1;}@@ -1314,6 +1320,39 @@ mod color_tests {assert!(s.execute("swatch.load", &json!({"base64": "AAAA"})).is_err());}+ #[test]+ fn loading_swatches_matches_the_first_swatch_of_each_name() {+ use designcraft_color::{Color, ColorType};+ let ink = |k: f32| SwatchValue::Color { color: Color::cmyk(0.0, 0.0, 0.0, k), color_type: ColorType::Process };+ let sw = |name: &str, k: f32| Swatch { name: name.into(), value: ink(k), locked: false, named: true, hidden: false };+ let mut s = Session::new();+ s.execute("file.new", &json!({})).unwrap();+ // Two swatches share a name, and an unnamed color is hidden.+ s.edit(|d, _| {+ d.swatches.extend([sw("Twin", 0.1), sw("Twin", 0.2), Swatch { named: false, hidden: true, ..sw("Unnamed", 0.3) }]);+ Ok(())+ })+ .unwrap();+ let file = designcraft_color::ase::write(&[sw("New", 0.4), sw("New", 0.5), sw("Twin", 0.6), sw("Unnamed", 0.7), sw("[Black]", 0.8)]);+ let b64 = crate::cmd::base64_encode(&file);+ let named = |s: &Session, name: &str| -> Vec<(SwatchValue, bool)> {+ s.doc().unwrap().doc.swatches.iter().filter(|w| w.name == name).map(|w| (w.value.clone(), w.hidden)).collect()+ };+ // With `replace`, the file's second "New" replaces its first, only the first "Twin" changes, the hidden+ // swatch becomes visible, and the locked [Black] stays.+ let r = s.execute("swatch.load", &json!({"base64": b64, "replace": true})).unwrap();+ assert_eq!((r["added"].as_u64(), r["replaced"].as_u64()), (Some(1), Some(3)));+ assert_eq!(named(&s, "New"), [(ink(0.5), false)]);+ assert_eq!(named(&s, "Twin"), [(ink(0.6), false), (ink(0.2), false)]);+ assert_eq!(named(&s, "Unnamed"), [(ink(0.7), false)]);+ assert_eq!(named(&s, "[Black]"), [(ink(1.0), false)]);+ // Without it, every name is already there and nothing changes.+ let before = s.doc().unwrap().doc.swatches.clone();+ let r = s.execute("swatch.load", &json!({"base64": b64})).unwrap();+ assert_eq!((r["added"].as_u64(), r["replaced"].as_u64()), (Some(0), Some(0)));+ assert_eq!(s.doc().unwrap().doc.swatches, before);+ }+#[test]fn unnamed_colors_then_add_to_swatches() {let mut s = Session::new();