mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-30 18:27:00 +01:00
Merge document parity fields with pattern and gradient metadata
This commit is contained in:
commit
0706be37ff
3 files changed
+96
-2
No files matched your search
@@ -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 }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -291,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).
|
||||
@@ -319,6 +350,9 @@ impl Document {
|
||||
if !(0.0..=1000.0).contains(&self.snap.tolerance_mm) {
|
||||
return bad("snap: tolerance must be 0-1000 mm".to_string());
|
||||
}
|
||||
for shape in &self.shapes {
|
||||
check_widths(shape, 0)?;
|
||||
}
|
||||
for layer in &self.layers {
|
||||
check_name(&layer.name, "layer name")?;
|
||||
}
|
||||
|
||||
@@ -133,7 +133,7 @@ pub enum Tip {
|
||||
Wide,
|
||||
}
|
||||
|
||||
fn is_false(value: &bool) -> bool {
|
||||
pub(crate) fn is_false(value: &bool) -> bool {
|
||||
!*value
|
||||
}
|
||||
|
||||
@@ -174,6 +174,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<f64>,
|
||||
/// 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.
|
||||
@@ -290,6 +297,10 @@ pub struct RasterImage {
|
||||
pub width_px: u32,
|
||||
pub height_px: u32,
|
||||
pub png_base64: std::sync::Arc<str>,
|
||||
/// 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<std::sync::Arc<str>>,
|
||||
}
|
||||
|
||||
impl RasterImage {
|
||||
@@ -303,6 +314,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(())
|
||||
}
|
||||
}
|
||||
@@ -818,6 +832,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);
|
||||
@@ -861,6 +877,45 @@ 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.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.shapes[0].production.stroke_width_mm = Some(f64::NAN);
|
||||
assert!(bad.validate().is_err());
|
||||
bad.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.shapes[0].clone();
|
||||
member.production.stroke_width_mm = Some(-1.0);
|
||||
nested.shapes[0].production.group =
|
||||
Some(Box::new(SourceGroup { kind: GroupKind::Combine(CombineOp::Unite), members: vec![member], path: nested.shapes[0].path.clone() }));
|
||||
assert!(nested.validate().is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn images_round_trip_and_share_the_id_counter() {
|
||||
let mut doc = canvas_sample();
|
||||
@@ -875,6 +930,7 @@ mod tests {
|
||||
width_px: 2,
|
||||
height_px: 1,
|
||||
png_base64: "AAAA".into(),
|
||||
original_png_base64: None,
|
||||
})
|
||||
.unwrap();
|
||||
assert_eq!(id, 3);
|
||||
|
||||
@@ -369,6 +369,10 @@ pub struct ShapeProduction {
|
||||
pub hidden_contours: Vec<usize>,
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub group: Option<Box<SourceGroup>>,
|
||||
/// 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<f64>,
|
||||
/// A picture repeated inside the shape instead of a plain fill (Silhouette's pattern
|
||||
/// fill). Printed and shown; the cut follows the path as always.
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
|
||||
Reference in new issue
Block a user