From 0749deb6891e898054c985b9877adbca00ee07bc Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Wed, 25 Nov 2026 20:22:38 -0500 Subject: [PATCH 1/2] stencilcraft-document: optional line width, isometric grid, text wrap width and underline, and an image's original pixels --- crates/stencilcraft-document/src/canvas.rs | 17 ++++++- crates/stencilcraft-document/src/lib.rs | 51 ++++++++++++++++++- .../stencilcraft-document/src/production.rs | 4 ++ 3 files changed, 70 insertions(+), 2 deletions(-) diff --git a/crates/stencilcraft-document/src/canvas.rs b/crates/stencilcraft-document/src/canvas.rs index 69878cb5e8..3978f92348 100644 --- a/crates/stencilcraft-document/src/canvas.rs +++ b/crates/stencilcraft-document/src/canvas.rs @@ -12,6 +12,10 @@ use crate::{Board, DocError, Document, Shape}; pub const MAX_PAGES: usize = 100; /// Most ruler guides a document may hold. pub const MAX_GUIDES: usize = 1000; +/// Widest line weight a shape may have, millimetres. +pub const MAX_STROKE_MM: f64 = 100.0; +/// Widest text wrap width, millimetres (a 10 m roll). +pub const MAX_WRAP_MM: f64 = 10_000.0; /// Most groups a document may hold. pub const MAX_GROUPS: usize = 100_000; /// Deepest group nesting accepted. @@ -162,11 +166,14 @@ pub struct Grid { pub subdivisions: u32, #[serde(default)] pub visible: bool, + /// Isometric lines (at 30° and 150°, plus verticals) instead of a square grid. + #[serde(default, skip_serializing_if = "crate::is_false")] + pub isometric: bool, } impl Default for Grid { fn default() -> Self { - Self { spacing_mm: 10.0, subdivisions: 1, visible: false } + Self { spacing_mm: 10.0, subdivisions: 1, visible: false, isometric: false } } } @@ -322,6 +329,14 @@ impl Document { for layer in &self.layers { check_name(&layer.name, "layer name")?; check_color(layer.color.as_deref(), "layer colour")?; + for shape in &layer.shapes { + if shape.production.stroke_width_mm.is_some_and(|w| !(w.is_finite() && (0.0..=MAX_STROKE_MM).contains(&w))) { + return bad(format!("shape {}: line width must be 0-{MAX_STROKE_MM} mm", shape.id)); + } + if shape.text.as_ref().and_then(|t| t.wrap_width_mm).is_some_and(|w| !(w.is_finite() && w > 0.0 && w <= MAX_WRAP_MM)) { + return bad(format!("shape {}: text wrap width must be above 0 and at most {MAX_WRAP_MM} mm", shape.id)); + } + } } check_name(&self.meta.title, "title")?; if self.meta.notes.len() > MAX_NOTES_BYTES { diff --git a/crates/stencilcraft-document/src/lib.rs b/crates/stencilcraft-document/src/lib.rs index 6e04be5b53..ab0848acf0 100644 --- a/crates/stencilcraft-document/src/lib.rs +++ b/crates/stencilcraft-document/src/lib.rs @@ -204,7 +204,7 @@ pub enum ToolOp { Punch { diameter_mm: f64 }, } -fn is_false(value: &bool) -> bool { +pub(crate) fn is_false(value: &bool) -> bool { !*value } @@ -245,6 +245,13 @@ pub struct TextSpec { /// the shape since its text was laid out. #[serde(default = "identity", skip_serializing_if = "is_identity")] pub transform: [f64; 6], + /// Area text: lines wrap at this width in millimetres (Silhouette Studio's wrap bar). + /// `None` is point text, which breaks only at newlines. + #[serde(default, skip_serializing_if = "Option::is_none")] + pub wrap_width_mm: Option, + /// An underline under every line, part of the cut outline. + #[serde(default, skip_serializing_if = "is_false")] + pub underline: bool, } /// A font by family and style, optionally from a font file. @@ -361,6 +368,10 @@ pub struct RasterImage { pub width_px: u32, pub height_px: u32, pub png_base64: std::sync::Arc, + /// The pixels as placed, before any erase or clean-up, so a restore brush can bring them + /// back. Absent when the image was never edited (and in older files). + #[serde(default, skip_serializing_if = "Option::is_none")] + pub original_png_base64: Option>, } impl RasterImage { @@ -374,6 +385,9 @@ impl RasterImage { if self.png_base64.len() > MAX_IMAGE_DATA { return Err(DocError::Model(format!("image {}: pixel data exceeds the 128 MiB limit", self.id))); } + if self.original_png_base64.as_ref().is_some_and(|o| o.len() > MAX_IMAGE_DATA) { + return Err(DocError::Model(format!("image {}: original pixel data exceeds the 128 MiB limit", self.id))); + } Ok(()) } } @@ -649,6 +663,8 @@ mod tests { flow: TextFlow::Curve { diameter_mm: 50.0 }, letters: vec![LetterOffset { index: 1, dx_mm: 1.0, ..LetterOffset::default() }], transform: IDENTITY_TRANSFORM, + wrap_width_mm: Some(80.0), + underline: true, })); let back = Document::load_json(&doc.save_json().unwrap()).unwrap(); assert_eq!(doc, back); @@ -694,6 +710,38 @@ mod tests { )); } + #[test] + fn parity_fields_round_trip_stay_out_of_plain_files_and_are_checked() { + let mut doc = sample(); + let plain = doc.save_json().unwrap(); + for key in ["stroke_width_mm", "isometric", "wrap_width_mm", "underline", "original_png_base64"] { + assert!(!plain.contains(key), "a plain document saves as before: {key}"); + } + doc.layers[0].shapes[0].production.stroke_width_mm = Some(0.5); + doc.grid.isometric = true; + doc.push_image(RasterImage { + id: 0, + name: "photo".to_string(), + x_mm: 0.0, + y_mm: 0.0, + width_mm: 10.0, + height_mm: 5.0, + width_px: 2, + height_px: 1, + png_base64: "AAAA".into(), + original_png_base64: Some("BBBB".into()), + }) + .unwrap(); + let back = Document::load_json(&doc.save_json().unwrap()).unwrap(); + assert_eq!(back, doc); + // Out-of-range values are refused, not saved. + let mut bad = doc.clone(); + bad.layers[0].shapes[0].production.stroke_width_mm = Some(f64::NAN); + assert!(bad.validate().is_err()); + bad.layers[0].shapes[0].production.stroke_width_mm = Some(-1.0); + assert!(bad.validate().is_err()); + } + #[test] fn images_round_trip_and_share_the_id_counter() { let mut doc = canvas_sample(); @@ -708,6 +756,7 @@ mod tests { width_px: 2, height_px: 1, png_base64: "AAAA".into(), + original_png_base64: None, }) .unwrap(); assert_eq!(id, 3); diff --git a/crates/stencilcraft-document/src/production.rs b/crates/stencilcraft-document/src/production.rs index c03717c0bb..b775e73873 100644 --- a/crates/stencilcraft-document/src/production.rs +++ b/crates/stencilcraft-document/src/production.rs @@ -165,6 +165,10 @@ pub struct ShapeProduction { pub hidden_contours: Vec, #[serde(default, skip_serializing_if = "Option::is_none")] pub group: Option>, + /// Line weight in millimetres (Silhouette Studio's line width): how thick the line draws + /// and prints, and the width Outline Stroke outlines at. `None` is a hairline. + #[serde(default, skip_serializing_if = "Option::is_none")] + pub stroke_width_mm: Option, } impl ShapeProduction { From 63e3d020107672565d852057e688ba5524b0bcd7 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Wed, 25 Nov 2026 20:42:51 -0500 Subject: [PATCH 2/2] stencilcraft-document: check the widths of a Combine's or Flatten's kept members too --- crates/stencilcraft-document/src/canvas.rs | 31 +++++++++++++++++----- crates/stencilcraft-document/src/lib.rs | 10 +++++++ 2 files changed, 35 insertions(+), 6 deletions(-) diff --git a/crates/stencilcraft-document/src/canvas.rs b/crates/stencilcraft-document/src/canvas.rs index 3978f92348..6f2c6f0f19 100644 --- a/crates/stencilcraft-document/src/canvas.rs +++ b/crates/stencilcraft-document/src/canvas.rs @@ -298,6 +298,30 @@ fn check_setup(setup: &PageSetup, what: &str) -> Result<(), DocError> { check_color(setup.color.as_deref(), what) } +/// How deep a Combine's or Flatten's stored members may nest. +const MAX_MEMBER_DEPTH: usize = 64; + +/// A shape's line width and text wrap width are in range, and so are those of the shapes a +/// Combine or Flatten keeps inside it. +fn check_widths(shape: &crate::Shape, depth: usize) -> Result<(), DocError> { + let bad = |message: String| Err(DocError::Model(message)); + if depth > MAX_MEMBER_DEPTH { + return bad(format!("shape {}: grouped members nest deeper than {MAX_MEMBER_DEPTH}", shape.id)); + } + if shape.production.stroke_width_mm.is_some_and(|w| !(w.is_finite() && (0.0..=MAX_STROKE_MM).contains(&w))) { + return bad(format!("shape {}: line width must be 0-{MAX_STROKE_MM} mm", shape.id)); + } + if shape.text.as_ref().and_then(|t| t.wrap_width_mm).is_some_and(|w| !(w.is_finite() && w > 0.0 && w <= MAX_WRAP_MM)) { + return bad(format!("shape {}: text wrap width must be above 0 and at most {MAX_WRAP_MM} mm", shape.id)); + } + if let Some(group) = &shape.production.group { + for member in &group.members { + check_widths(member, depth + 1)?; + } + } + Ok(()) +} + impl Document { /// Pages, guides, grid, snapping, colours, metadata and groups: finite, within caps, /// and groups form a forest (unknown shape members are allowed: they are pruned). @@ -330,12 +354,7 @@ impl Document { check_name(&layer.name, "layer name")?; check_color(layer.color.as_deref(), "layer colour")?; for shape in &layer.shapes { - if shape.production.stroke_width_mm.is_some_and(|w| !(w.is_finite() && (0.0..=MAX_STROKE_MM).contains(&w))) { - return bad(format!("shape {}: line width must be 0-{MAX_STROKE_MM} mm", shape.id)); - } - if shape.text.as_ref().and_then(|t| t.wrap_width_mm).is_some_and(|w| !(w.is_finite() && w > 0.0 && w <= MAX_WRAP_MM)) { - return bad(format!("shape {}: text wrap width must be above 0 and at most {MAX_WRAP_MM} mm", shape.id)); - } + check_widths(shape, 0)?; } } check_name(&self.meta.title, "title")?; diff --git a/crates/stencilcraft-document/src/lib.rs b/crates/stencilcraft-document/src/lib.rs index ab0848acf0..d6ba2c7c3e 100644 --- a/crates/stencilcraft-document/src/lib.rs +++ b/crates/stencilcraft-document/src/lib.rs @@ -740,6 +740,16 @@ mod tests { assert!(bad.validate().is_err()); bad.layers[0].shapes[0].production.stroke_width_mm = Some(-1.0); assert!(bad.validate().is_err()); + // A bad width inside a Combine's kept members is caught too. + let mut nested = doc.clone(); + let mut member = nested.layers[0].shapes[0].clone(); + member.production.stroke_width_mm = Some(-1.0); + nested.layers[0].shapes[0].production.group = Some(Box::new(SourceGroup { + kind: GroupKind::Combine(CombineOp::Unite), + members: vec![member], + path: nested.layers[0].shapes[0].path.clone(), + })); + assert!(nested.validate().is_err()); } #[test]