From 865ecbd071fd7006c6c50fd3d2c7b998dc2587b0 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Thu, 19 Nov 2026 16:25:18 -0500 Subject: [PATCH 1/2] doc: delegate .lin and .pat parsing to craft-dxf::lin_pat --- Cargo.lock | 7 +- Cargo.toml | 2 +- crates/doc/Cargo.toml | 1 + crates/doc/src/library/files.rs | 360 ++++---------------------------- 4 files changed, 52 insertions(+), 318 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 22346f852..4c45b7083 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -717,6 +717,7 @@ version = "0.21.0" dependencies = [ "cadcraft-color", "cadcraft-geom", + "craft-dxf", "serde", "serde_json", "thiserror 2.0.21", @@ -1183,7 +1184,7 @@ source = "git+https://github.com/storytold/craft-libs?rev=0325b2d73676d736e94097 [[package]] name = "craft-codepage" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=9ed120e6b1bd5116a4649c578d1272d60cf932b9#9ed120e6b1bd5116a4649c578d1272d60cf932b9" +source = "git+https://github.com/storytold/craft-libs?rev=6f6c3a3d5e4b770082106aacc8b3821d6e8aa37a#6f6c3a3d5e4b770082106aacc8b3821d6e8aa37a" [[package]] name = "craft-docpath" @@ -1202,9 +1203,9 @@ dependencies = [ [[package]] name = "craft-dxf" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=9ed120e6b1bd5116a4649c578d1272d60cf932b9#9ed120e6b1bd5116a4649c578d1272d60cf932b9" +source = "git+https://github.com/storytold/craft-libs?rev=6f6c3a3d5e4b770082106aacc8b3821d6e8aa37a#6f6c3a3d5e4b770082106aacc8b3821d6e8aa37a" dependencies = [ - "craft-codepage 0.1.0 (git+https://github.com/storytold/craft-libs?rev=9ed120e6b1bd5116a4649c578d1272d60cf932b9)", + "craft-codepage 0.1.0 (git+https://github.com/storytold/craft-libs?rev=6f6c3a3d5e4b770082106aacc8b3821d6e8aa37a)", "encoding_rs", "kurbo", "log", diff --git a/Cargo.toml b/Cargo.toml index 8e9a02ea6..2d6ca7d8c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,7 +14,7 @@ repository = "https://github.com/storytold/cadcraft" # internal cadcraft-geom = { path = "crates/geom" } # DXF tags: the shared reader and writer (moved from crates/dxf; craft-libs#791). -craft-dxf = { git = "https://github.com/storytold/craft-libs", rev = "9ed120e6b1bd5116a4649c578d1272d60cf932b9" } +craft-dxf = { git = "https://github.com/storytold/craft-libs", rev = "6f6c3a3d5e4b770082106aacc8b3821d6e8aa37a" } craft-dwg = { git = "https://github.com/storytold/craft-libs", rev = "7bb6c90076a0177d01cc4c482e7fd8dc9f31a8bf" } craft-shx = { git = "https://github.com/storytold/craft-libs", rev = "2f2d1d84e3a36b3f93e81507aeff3f0da97a7162" } craft-image-codecs = { git = "https://github.com/storytold/craft-libs", rev = "ae5020c1ed922cf7976a3a5545f05b07f466be12" } diff --git a/crates/doc/Cargo.toml b/crates/doc/Cargo.toml index 7046fd408..d456d6d99 100644 --- a/crates/doc/Cargo.toml +++ b/crates/doc/Cargo.toml @@ -11,6 +11,7 @@ workspace = true [dependencies] cadcraft-geom = { workspace = true } cadcraft-color = { workspace = true } +craft-dxf = { workspace = true } serde = { workspace = true } serde_json = { workspace = true } thiserror = { workspace = true } diff --git a/crates/doc/src/library/files.rs b/crates/doc/src/library/files.rs index 5e555af6c..5055dc007 100644 --- a/crates/doc/src/library/files.rs +++ b/crates/doc/src/library/files.rs @@ -1,32 +1,14 @@ //! Readers for the user's own linetype (`.lin`) and hatch pattern (`.pat`) files. //! -//! Both follow the publicly documented text formats: a header line `*NAME[,description]`, then -//! the definition (`A,dash,gap,…` for a linetype, `angle, x, y, dx, dy[, dash, …]` lines for -//! a pattern), with `;` starting a comment line. CADCraft ships no such file; these read the -//! files users bring. Everything here treats the text as hostile: lines, definitions, dashes, -//! families and warnings are capped, numbers must be finite and reasonable, and nothing panics. -//! Problems become warnings and the definitions that can be read are kept. +//! Grammar and hostile-input capping live in [`craft_dxf::lin_pat`]; this module maps the +//! parsed definitions into CADCraft's [`Linetype`] and [`UserPattern`] document types. + +use cadcraft_geom::Vec2; +pub use craft_dxf::lin_pat::{MAX_DEFINITIONS, MAX_LINE_CHARS, MAX_LINETYPE_ELEMENTS, MAX_PATTERN_DASHES, MAX_PATTERN_FAMILIES, MAX_VALUE, Parsed}; use super::PatternLine; use crate::{DashElement, Linetype}; -/// Longest line read; longer lines are skipped with a warning. -pub const MAX_LINE_CHARS: usize = 4096; -/// Most definitions (linetypes or patterns) read from one file. -pub const MAX_DEFINITIONS: usize = 4096; -/// Most elements in one linetype. -pub const MAX_LINETYPE_ELEMENTS: usize = 256; -/// Most line families in one hatch pattern (what the renderer draws, too). -pub const MAX_PATTERN_FAMILIES: usize = 1024; -/// Most dashes in one pattern line family (as for a pattern definition read from DXF). -pub const MAX_PATTERN_DASHES: usize = 256; -/// Largest magnitude accepted for a length, offset or scale. -pub const MAX_VALUE: f64 = 1.0e6; -/// Longest name or description kept. -const MAX_NAME_CHARS: usize = 255; -/// Most warnings collected; the rest are counted in a last line. -const MAX_WARNINGS: usize = 100; - /// A hatch pattern read from a user's `.pat` file. #[derive(Clone, Debug, PartialEq, serde::Serialize, serde::Deserialize)] pub struct UserPattern { @@ -35,307 +17,57 @@ pub struct UserPattern { pub lines: Vec, } -/// What a file held: the definitions that could be read, and why anything else was skipped. -#[derive(Clone, Debug, PartialEq)] -pub struct Parsed { - pub items: Vec, - pub warnings: Vec, -} - -impl Default for Parsed { - fn default() -> Self { - Parsed { items: Vec::new(), warnings: Vec::new() } - } -} - -impl Parsed { - fn warn(&mut self, line: usize, msg: impl Into) { - match self.warnings.len() { - n if n < MAX_WARNINGS => self.warnings.push(format!("line {line}: {}", msg.into())), - MAX_WARNINGS => self.warnings.push("more problems not listed".into()), - _ => {} - } - } -} - -/// A header line `*NAME[,description]`: the name and description, or why it can't be used. -fn header(rest: &str) -> std::result::Result<(String, String), String> { - let (name, desc) = rest.split_once(',').unwrap_or((rest, "")); - let name = name.trim(); - if name.is_empty() { - return Err("a definition without a name".into()); - } - if name.chars().count() > MAX_NAME_CHARS || name.chars().any(char::is_control) { - return Err("a definition name that is too long or has control characters".into()); - } - let desc: String = desc.trim().chars().filter(|c| !c.is_control()).take(MAX_NAME_CHARS).collect(); - Ok((name.to_string(), desc)) -} - -/// A finite number within ±[`MAX_VALUE`]. -fn number(t: &str) -> Option { - t.trim().parse::().ok().filter(|v| v.is_finite() && v.abs() <= MAX_VALUE) -} - -/// The file's text lines with 1-based numbers, without a leading byte-order mark. -fn lines(text: &str) -> impl Iterator { - text.trim_start_matches('\u{feff}').lines().enumerate().map(|(i, l)| (i.saturating_add(1), l.trim())) -} - -// ---------- LIN ---------- - /// Read the linetypes of a `.lin` file. Simple linetypes and complex ones with embedded text /// (`["TEXT",STYLE,S=…,R=…,A=…,U=…,X=…,Y=…]`) are read; shape elements (`[SHAPE,file.shx,…]`) /// need SHX shape fonts, so they are left out with a warning and the dashes around them kept. /// Lengths are in drawing units as written: a user's file is never rescaled. pub fn parse_lin(text: &str) -> Parsed { - let mut out = Parsed::default(); - // The definition being read: name, description, header line. - let mut pending: Option<(String, String, usize)> = None; - for (no, line) in lines(text) { - if line.is_empty() || line.starts_with(';') { - continue; - } - if line.chars().count() > MAX_LINE_CHARS { - out.warn(no, "line too long; skipped"); - continue; - } - if let Some(rest) = line.strip_prefix('*') { - if let Some((name, _, at)) = pending.take() { - out.warn(at, format!("linetype {name} has no pattern line; skipped")); - } - match header(rest) { - Ok((name, desc)) => pending = Some((name, desc, no)), - Err(e) => out.warn(no, format!("{e}; skipped")), - } - continue; - } - let Some((name, description, _)) = pending.take() else { - out.warn(no, "pattern line without a *NAME line before it; skipped"); - continue; - }; - if out.items.len() >= MAX_DEFINITIONS { - out.warn(no, format!("more than {MAX_DEFINITIONS} linetypes; the rest are skipped")); - break; - } - if out.items.iter().any(|l: &Linetype| l.name.eq_ignore_ascii_case(&name)) { - out.warn(no, format!("linetype {name} is defined twice; the first definition is kept")); - continue; - } - match lin_pattern(line, no, &name, &mut out) { - Ok(pattern) => out.items.push(Linetype { name, description, pattern }), - Err(e) => out.warn(no, format!("linetype {name}: {e}; skipped")), - } + let parsed = craft_dxf::lin_pat::parse_lin(text); + Parsed { + items: parsed + .items + .into_iter() + .map(|d| Linetype { + name: d.name, + description: d.description, + pattern: d + .pattern + .into_iter() + .map(|e| DashElement { + length: e.length, + text: e.text, + shape: None, + style: e.style, + scale: e.scale, + rotation: e.rotation, + offset: Vec2::new(e.offset[0], e.offset[1]), + absolute: e.absolute, + }) + .collect(), + }) + .collect(), + warnings: parsed.warnings, } - if let Some((name, _, at)) = pending { - out.warn(at, format!("linetype {name} has no pattern line; skipped")); - } - out } -/// Split a pattern line at commas outside `[…]` and `"…"`. -fn lin_fields(line: &str) -> std::result::Result, String> { - let mut out = Vec::new(); - let (mut depth, mut quoted, mut start) = (0usize, false, 0usize); - for (i, c) in line.char_indices() { - match c { - '"' => quoted = !quoted, - '[' if !quoted => depth = depth.saturating_add(1), - ']' if !quoted => depth = depth.checked_sub(1).ok_or("a ] without its [")?, - ',' if !quoted && depth == 0 => { - out.push(line.get(start..i).unwrap_or("").trim()); - start = i.saturating_add(1); - } - _ => {} - } - if out.len() > MAX_LINETYPE_ELEMENTS.saturating_mul(2) { - return Err(format!("more than {MAX_LINETYPE_ELEMENTS} elements")); - } - } - if quoted || depth != 0 { - return Err("an unclosed [ or \"".into()); - } - out.push(line.get(start..).unwrap_or("").trim()); - Ok(out) -} - -/// The elements of a pattern line `A,dash,gap,[…],…`. -fn lin_pattern(line: &str, no: usize, name: &str, out: &mut Parsed) -> std::result::Result, String> { - let mut fields = lin_fields(line)?; - // The alignment field ("A") comes first; a line of plain numbers is read as well. - if fields.first().is_some_and(|f| f.eq_ignore_ascii_case("a")) { - fields.remove(0); - } else if fields.first().is_some_and(|f| number(f).is_none() && !f.starts_with('[')) { - return Err(format!("unknown alignment `{}` (expected A)", fields.first().copied().unwrap_or(""))); - } - let mut pattern: Vec = Vec::new(); - for f in fields.into_iter().filter(|f| !f.is_empty()) { - if pattern.len() >= MAX_LINETYPE_ELEMENTS { - return Err(format!("more than {MAX_LINETYPE_ELEMENTS} elements")); - } - let Some(inner) = f.strip_prefix('[') else { - let len = number(f).ok_or_else(|| format!("`{f}` is not a dash length"))?; - pattern.push(DashElement::dash(len)); - continue; - }; - let inner = inner.strip_suffix(']').ok_or_else(|| format!("`{f}` is not a complete [ ] element"))?; - match lin_text(inner)? { - Some(text) => { - // The text sits at the end of the element before it (a zero-length one if none). - if pattern.last().is_none_or(|e| e.text.is_some()) { - pattern.push(DashElement::dash(0.0)); - } - if let Some(last) = pattern.last_mut() { - *last = DashElement { length: last.length, ..text }; - } - } - None => { - let shape = inner.split(',').next().unwrap_or("").trim(); - out.warn(no, format!("linetype {name}: shape {shape} needs an SHX shape file, which CADCraft can't read; left out")); - } - } - } - if pattern.is_empty() { - return Err("no dashes".into()); - } - if pattern.iter().all(|e| e.length == 0.0) && pattern.iter().all(|e| e.text.is_none()) { - return Err("every element has zero length".into()); - } - Ok(pattern) -} - -/// A `"TEXT",STYLE,key=value…` element (the inside of `[…]`), or `None` for a shape element. -fn lin_text(inner: &str) -> std::result::Result, String> { - let inner = inner.trim(); - let Some(after) = inner.strip_prefix('"') else { return Ok(None) }; - let (text, rest) = after.split_once('"').ok_or("unclosed text quote")?; - if text.chars().count() > MAX_NAME_CHARS || text.chars().any(char::is_control) { - return Err("embedded text that is too long or has control characters".into()); - } - let mut parts = rest.split(',').map(str::trim); - if !parts.next().unwrap_or("").is_empty() { - return Err("text element: expected a comma after the text".into()); - } - let style = parts.next().unwrap_or("").trim(); - if style.is_empty() || style.contains('=') { - return Err("text element: the text style is missing".into()); - } - let mut e = DashElement { text: Some(text.to_string()), style: Some(style.to_string()), ..DashElement::dash(0.0) }; - for kv in parts.filter(|p| !p.is_empty()) { - let (k, v) = kv.split_once('=').ok_or_else(|| format!("text element: `{kv}` is not key=value"))?; - match k.trim().to_ascii_uppercase().as_str() { - "S" => e.scale = number(v).filter(|s| *s > 0.0).ok_or_else(|| format!("text element: bad scale `{v}`"))?, - "X" => e.offset.x = number(v).ok_or_else(|| format!("text element: bad X offset `{v}`"))?, - "Y" => e.offset.y = number(v).ok_or_else(|| format!("text element: bad Y offset `{v}`"))?, - // Upright text keeps a rotation relative to the line, like R. - k @ ("R" | "A" | "U") => { - e.rotation = angle(v).ok_or_else(|| format!("text element: bad rotation `{v}`"))?; - e.absolute = k == "A"; - } - other => return Err(format!("text element: unknown option `{other}`")), - } - } - Ok(Some(e)) -} - -/// An angle in degrees, or with a `d` (degrees), `r` (radians) or `g` (grads) suffix; radians out. -fn angle(t: &str) -> Option { - let t = t.trim().to_ascii_lowercase(); - let (num, unit) = match t.char_indices().last() { - Some((i, c @ ('d' | 'r' | 'g'))) => (t.get(..i)?, c), - _ => (t.as_str(), 'd'), - }; - let v = num.trim().parse::().ok().filter(|v| v.is_finite() && v.abs() <= 1.0e5)?; - Some(match unit { - 'r' => v, - 'g' => v * std::f64::consts::PI / 200.0, - _ => v.to_radians(), - }) -} - -// ---------- PAT ---------- - /// Read the hatch patterns of a `.pat` file. Values are used as written (an inch file for inch /// drawings, a millimetre file for metric ones). pub fn parse_pat(text: &str) -> Parsed { - let mut out: Parsed = Parsed::default(); - // The pattern being read, its header line, and whether it already failed. - let mut cur: Option<(UserPattern, usize, bool)> = None; - let finish = |cur: Option<(UserPattern, usize, bool)>, out: &mut Parsed| { - let Some((p, at, failed)) = cur else { return }; - if failed { - return; - } - if p.lines.is_empty() { - out.warn(at, format!("pattern {} has no line families; skipped", p.name)); - } else if out.items.len() >= MAX_DEFINITIONS { - out.warn(at, format!("more than {MAX_DEFINITIONS} patterns; {} skipped", p.name)); - } else if out.items.iter().any(|q| q.name.eq_ignore_ascii_case(&p.name)) { - out.warn(at, format!("pattern {} is defined twice; the first definition is kept", p.name)); - } else { - out.items.push(p); - } - }; - for (no, line) in lines(text) { - // A comment runs to the end of the line. - let line = line.split(';').next().unwrap_or("").trim(); - if line.is_empty() { - continue; - } - if line.chars().count() > MAX_LINE_CHARS { - out.warn(no, "line too long; skipped"); - continue; - } - if let Some(rest) = line.strip_prefix('*') { - finish(cur.take(), &mut out); - match header(rest) { - Ok((name, description)) => cur = Some((UserPattern { name: name.to_ascii_uppercase(), description, lines: Vec::new() }, no, false)), - Err(e) => out.warn(no, format!("{e}; skipped")), - } - continue; - } - let Some((p, _, failed)) = cur.as_mut() else { - out.warn(no, "line family without a *NAME line before it; skipped"); - continue; - }; - if *failed { - continue; - } - if p.lines.len() >= MAX_PATTERN_FAMILIES { - out.warn(no, format!("pattern {}: more than {MAX_PATTERN_FAMILIES} line families; skipped", p.name)); - *failed = true; - continue; - } - match pat_family(line) { - Ok(l) => p.lines.push(l), - Err(e) => { - out.warn(no, format!("pattern {}: {e}; skipped", p.name)); - *failed = true; - } - } + let parsed = craft_dxf::lin_pat::parse_pat(text); + Parsed { + items: parsed + .items + .into_iter() + .map(|p| UserPattern { + name: p.name, + description: p.description, + lines: p + .lines + .into_iter() + .map(|l| PatternLine { angle: l.angle, origin: (l.origin[0], l.origin[1]), delta: (l.delta[0], l.delta[1]), dashes: l.dashes }) + .collect(), + }) + .collect(), + warnings: parsed.warnings, } - finish(cur, &mut out); - out -} - -/// One line family: `angle, x-origin, y-origin, delta-x, delta-y[, dash-1, dash-2, …]`. -fn pat_family(line: &str) -> std::result::Result { - let mut v = Vec::new(); - for f in line.split(',') { - if v.len() >= MAX_PATTERN_DASHES.saturating_add(5) { - return Err(format!("more than {MAX_PATTERN_DASHES} dashes in a line family")); - } - let f = f.trim(); - if f.is_empty() && v.len() >= 5 { - continue; - } - v.push(number(f).ok_or_else(|| format!("`{f}` is not a number"))?); - } - let (Some(&angle), Some(&x), Some(&y), Some(&dx), Some(&dy)) = (v.first(), v.get(1), v.get(2), v.get(3), v.get(4)) else { - return Err("a line family needs angle, x, y, delta-x and delta-y".into()); - }; - if dy.abs() < 1e-9 { - return Err("a line family with zero spacing (delta-y)".into()); - } - Ok(PatternLine { angle, origin: (x, y), delta: (dx, dy), dashes: v.get(5..).unwrap_or(&[]).to_vec() }) } From 7c98bb26172872b62be0089fdae5778a593886df Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Thu, 19 Nov 2026 16:27:00 -0500 Subject: [PATCH 2/2] chore: pin craft-dxf to merged main rev 219c1d0b --- Cargo.lock | 6 +++--- Cargo.toml | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 4c45b7083..794c49ba9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1184,7 +1184,7 @@ source = "git+https://github.com/storytold/craft-libs?rev=0325b2d73676d736e94097 [[package]] name = "craft-codepage" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=6f6c3a3d5e4b770082106aacc8b3821d6e8aa37a#6f6c3a3d5e4b770082106aacc8b3821d6e8aa37a" +source = "git+https://github.com/storytold/craft-libs?rev=219c1d0b14177af6f291395d6587f61a5e09043a#219c1d0b14177af6f291395d6587f61a5e09043a" [[package]] name = "craft-docpath" @@ -1203,9 +1203,9 @@ dependencies = [ [[package]] name = "craft-dxf" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=6f6c3a3d5e4b770082106aacc8b3821d6e8aa37a#6f6c3a3d5e4b770082106aacc8b3821d6e8aa37a" +source = "git+https://github.com/storytold/craft-libs?rev=219c1d0b14177af6f291395d6587f61a5e09043a#219c1d0b14177af6f291395d6587f61a5e09043a" dependencies = [ - "craft-codepage 0.1.0 (git+https://github.com/storytold/craft-libs?rev=6f6c3a3d5e4b770082106aacc8b3821d6e8aa37a)", + "craft-codepage 0.1.0 (git+https://github.com/storytold/craft-libs?rev=219c1d0b14177af6f291395d6587f61a5e09043a)", "encoding_rs", "kurbo", "log", diff --git a/Cargo.toml b/Cargo.toml index 2d6ca7d8c..c491ce1cb 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,7 +14,7 @@ repository = "https://github.com/storytold/cadcraft" # internal cadcraft-geom = { path = "crates/geom" } # DXF tags: the shared reader and writer (moved from crates/dxf; craft-libs#791). -craft-dxf = { git = "https://github.com/storytold/craft-libs", rev = "6f6c3a3d5e4b770082106aacc8b3821d6e8aa37a" } +craft-dxf = { git = "https://github.com/storytold/craft-libs", rev = "219c1d0b14177af6f291395d6587f61a5e09043a" } craft-dwg = { git = "https://github.com/storytold/craft-libs", rev = "7bb6c90076a0177d01cc4c482e7fd8dc9f31a8bf" } craft-shx = { git = "https://github.com/storytold/craft-libs", rev = "2f2d1d84e3a36b3f93e81507aeff3f0da97a7162" } craft-image-codecs = { git = "https://github.com/storytold/craft-libs", rev = "ae5020c1ed922cf7976a3a5545f05b07f466be12" }