From fd11b47915190aa0eb7cbf3846a0017f32a84b12 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 15:47:03 +0000 Subject: [PATCH 1/3] Use shared SHP to SHX compiler --- crates/fonts/src/shx/compile.rs | 313 -------------------------------- 1 file changed, 313 deletions(-) delete mode 100644 crates/fonts/src/shx/compile.rs diff --git a/crates/fonts/src/shx/compile.rs b/crates/fonts/src/shx/compile.rs deleted file mode 100644 index 2e8cb3c25..000000000 --- a/crates/fonts/src/shx/compile.rs +++ /dev/null @@ -1,313 +0,0 @@ -//! Shape source (`.shp`) to a compiled SHX file, as COMPILE does. -//! -//! A source is a list of definitions: a header line `*number,defbytes,NAME` followed by its -//! specification bytes (on as many lines as needed, ending with 0). Numbers are decimal, or -//! hexadecimal after a leading `0` (`012`, `08cfb`); parentheses only group; `;` starts a -//! comment. A big font starts with `*BIGFONT nchars,nranges,first,last,…` and a Unicode font -//! with `*UNIFONT,6,name` followed by `above,below,modes,encoding,type,0`. Shape 0 is a text -//! font's record (`above,below,modes,0`). -//! -//! Operands are encoded by what they are: a subshape number takes two bytes in a Unicode font -//! (and after `7,0` in a big font), displacements and bulges are two's complement, and an arc's -//! direction byte (`-043`) keeps its sign in the high bit. `defbytes` is not checked: the -//! program's own length is written. - -use super::ShxError; -use super::parse::{BIGFONT, SHAPES_10, UNIFONT}; - -/// Largest source compiled. -const MAX_SOURCE: usize = 8 << 20; - -#[derive(Clone, Copy, PartialEq)] -enum Kind { - Shapes, - Big, - Uni, -} - -struct Shape { - number: u16, - name: String, - program: Vec, -} - -/// A number token: value (sign included). -fn number(tok: &str, line: usize) -> Result { - let t: String = tok.chars().filter(|c| !matches!(c, '(' | ')') && !c.is_whitespace()).collect(); - let (neg, digits) = match t.strip_prefix('-') { - Some(d) => (true, d), - None => (false, t.strip_prefix('+').unwrap_or(&t)), - }; - let err = || ShxError::Source(line, format!("not a number: {tok:?}")); - let v = if digits.len() > 1 && digits.starts_with('0') { - i64::from_str_radix(digits.get(1..).unwrap_or_default(), 16).map_err(|_| err())? - } else { - digits.parse::().map_err(|_| err())? - }; - if v > 0xFFFF { - return Err(err()); - } - Ok(if neg { -v } else { v }) -} - -/// The comma-separated numbers of a line (comment removed, blank fields skipped). -fn numbers(text: &str, line: usize) -> Result, ShxError> { - text.split(',').map(str::trim).filter(|t| !t.is_empty() && *t != "(" && *t != ")").map(|t| number(t, line)).collect() -} - -/// The spec-byte encoder: walks the tokens as the reader walks the bytes. -struct Enc<'a> { - toks: &'a [i64], - i: usize, - out: Vec, - kind: Kind, - line: usize, -} - -impl Enc<'_> { - fn next(&mut self) -> Result { - let v = self.toks.get(self.i).copied().ok_or_else(|| ShxError::Source(self.line, "the shape ends inside a command".into()))?; - self.i += 1; - Ok(v) - } - fn byte(&mut self) -> Result<(), ShxError> { - let v = self.next()?; - let b = u8::try_from(v).map_err(|_| ShxError::Source(self.line, format!("{v} is not a byte")))?; - self.out.push(b); - Ok(()) - } - /// Two's complement −128..127. - fn signed(&mut self) -> Result { - let v = self.next()?; - let b = i8::try_from(v).map_err(|_| ShxError::Source(self.line, format!("{v} is not −128..127")))?; - self.out.extend(b.to_le_bytes()); - Ok(v) - } - /// Sign in the high bit (an arc's direction byte). - fn sign_magnitude(&mut self) -> Result<(), ShxError> { - let v = self.next()?; - let m = u8::try_from(v.unsigned_abs()) - .ok() - .filter(|m| *m < 0x80) - .ok_or_else(|| ShxError::Source(self.line, format!("{v} is not an octant byte")))?; - self.out.push(if v < 0 { m | 0x80 } else { m }); - Ok(()) - } - fn word(&mut self) -> Result<(), ShxError> { - let v = self.next()?; - let w = u16::try_from(v).map_err(|_| ShxError::Source(self.line, format!("{v} is not a shape number")))?; - self.out.extend(w.to_be_bytes()); - Ok(()) - } - - fn program(mut self) -> Result, ShxError> { - while self.i < self.toks.len() { - let code = self.next()?; - let c = u8::try_from(code).map_err(|_| ShxError::Source(self.line, format!("{code} is not a code")))?; - self.out.push(c); - match c { - 0 => break, - 3 | 4 => self.byte()?, - 7 => match self.kind { - Kind::Uni => self.word()?, - Kind::Big => { - let first = self.toks.get(self.i).copied().unwrap_or(0); - if first == 0 { - self.byte()?; - self.word()?; - for _ in 0..4 { - self.byte()?; - } - } else { - self.byte()?; - } - } - Kind::Shapes => self.byte()?, - }, - 8 => { - self.signed()?; - self.signed()?; - } - 9 => loop { - let (x, y) = (self.signed()?, self.signed()?); - if x == 0 && y == 0 { - break; - } - }, - 10 => { - self.byte()?; - self.sign_magnitude()?; - } - 11 => { - for _ in 0..4 { - self.byte()?; - } - self.sign_magnitude()?; - } - 12 => { - for _ in 0..3 { - self.signed()?; - } - } - 13 => loop { - let (x, y) = (self.signed()?, self.signed()?); - if x == 0 && y == 0 { - break; - } - self.signed()?; - }, - _ => {} - } - } - if self.out.last() != Some(&0) { - self.out.push(0); - } - Ok(self.out) - } -} - -/// Compile shape source into an SHX file (a shape file or text font, a big font, or a Unicode -/// font, as its first definition says). -pub fn compile(source: &str) -> Result, ShxError> { - if source.len() > MAX_SOURCE { - return Err(ShxError::TooLarge); - } - let mut kind = Kind::Shapes; - let mut ranges: Vec<(u16, u16)> = Vec::new(); - let mut uni_info: Option<(String, Vec)> = None; - // Definitions: header (number, name, line) and the numbers that follow it. - let mut defs: Vec<(i64, String, usize, Vec)> = Vec::new(); - let mut uni_header_pending = false; - for (no, raw) in source.lines().enumerate() { - let line = no + 1; - let text = raw.split(';').next().unwrap_or_default().trim(); - if text.is_empty() { - continue; - } - if let Some(head) = text.strip_prefix('*') { - let upper = head.trim_start().to_ascii_uppercase(); - if let Some(rest) = upper.strip_prefix("BIGFONT") { - kind = Kind::Big; - let v = numbers(rest, line)?; - let n = usize::try_from(v.get(1).copied().unwrap_or(0)).unwrap_or(0); - for k in 0..n { - let lo = v.get(2 + 2 * k).copied().unwrap_or(0); - let hi = v.get(3 + 2 * k).copied().unwrap_or(lo); - let to = |x: i64| u16::try_from(x).map_err(|_| ShxError::Source(line, "bad escape range".into())); - ranges.push((to(lo)?, to(hi)?)); - } - continue; - } - if upper.starts_with("UNIFONT") { - kind = Kind::Uni; - let name = head.splitn(3, ',').nth(2).unwrap_or_default().trim().to_string(); - uni_info = Some((name, Vec::new())); - uni_header_pending = true; - continue; - } - uni_header_pending = false; - let mut parts = head.splitn(3, ','); - let num = number(parts.next().unwrap_or_default().trim(), line)?; - let _defbytes = parts.next(); - let name = parts.next().unwrap_or_default().trim().to_string(); - defs.push((num, name, line, Vec::new())); - continue; - } - let v = numbers(text, line)?; - if uni_header_pending { - if let Some((_, info)) = uni_info.as_mut() { - for x in v { - info.push(u8::try_from(x).map_err(|_| ShxError::Source(line, "bad font record".into()))?); - } - } - continue; - } - match defs.last_mut() { - Some(d) => d.3.extend(v), - None => return Err(ShxError::Source(line, "specification bytes before any *number,defbytes,NAME line".into())), - } - } - let mut shapes: Vec = Vec::new(); - for (num, name, line, toks) in defs { - let number = u16::try_from(num).map_err(|_| ShxError::Source(line, format!("shape number {num} out of range")))?; - let program = if number == 0 { - // The font record: plain bytes. - toks.iter().map(|x| u8::try_from(*x).map_err(|_| ShxError::Source(line, "bad font record".into()))).collect::, _>>()? - } else { - Enc { toks: &toks, i: 0, out: Vec::new(), kind, line }.program()? - }; - if shapes.iter().any(|s| s.number == number) { - return Err(ShxError::Source(line, format!("shape {number} is defined twice"))); - } - shapes.push(Shape { number, name, program }); - } - if shapes.is_empty() { - return Err(ShxError::Empty); - } - shapes.sort_by_key(|s| s.number); - let record = |s: &Shape| -> Result, ShxError> { - let mut r: Vec = s.name.bytes().filter(|b| *b != 0).collect(); - r.push(0); - r.extend(&s.program); - if r.len() > usize::from(u16::MAX) { Err(ShxError::TooLarge) } else { Ok(r) } - }; - let count = u16::try_from(shapes.len()).map_err(|_| ShxError::TooLarge)?; - let len16 = |r: &[u8]| u16::try_from(r.len()).unwrap_or(u16::MAX); - let mut out = Vec::new(); - match kind { - Kind::Shapes => { - out.extend(SHAPES_10); - out.extend(shapes.first().map_or(0, |s| s.number).to_le_bytes()); - out.extend(shapes.last().map_or(0, |s| s.number).to_le_bytes()); - out.extend(count.to_le_bytes()); - let recs: Vec> = shapes.iter().map(record).collect::>()?; - for (s, r) in shapes.iter().zip(&recs) { - out.extend(s.number.to_le_bytes()); - out.extend(len16(r).to_le_bytes()); - } - for r in &recs { - out.extend(r); - } - out.extend(b"EOF"); - } - Kind::Big => { - out.extend(BIGFONT); - let nranges = u16::try_from(ranges.len()).map_err(|_| ShxError::TooLarge)?; - out.extend(count.to_le_bytes()); - out.extend(count.to_le_bytes()); - out.extend(nranges.to_le_bytes()); - for (lo, hi) in &ranges { - out.extend(lo.to_le_bytes()); - out.extend(hi.to_le_bytes()); - } - let recs: Vec> = shapes.iter().map(record).collect::>()?; - let mut off = out.len() + 8 * recs.len(); - for (s, r) in shapes.iter().zip(&recs) { - out.extend(s.number.to_le_bytes()); - out.extend(len16(r).to_le_bytes()); - out.extend(u32::try_from(off).map_err(|_| ShxError::TooLarge)?.to_le_bytes()); - off += r.len(); - } - for r in &recs { - out.extend(r); - } - } - Kind::Uni => { - out.extend(UNIFONT); - let (name, info) = uni_info.unwrap_or_default(); - out.extend((u32::from(count) + 1).to_le_bytes()); - let mut rec: Vec = name.bytes().filter(|b| *b != 0).collect(); - rec.push(0); - rec.extend(&info); - out.extend(len16(&rec).to_le_bytes()); - out.extend(&rec); - for s in &shapes { - let r = record(s)?; - out.extend(s.number.to_le_bytes()); - out.extend(len16(&r).to_le_bytes()); - out.extend(r); - } - } - } - Ok(out) -} From 78bcdce7949e7820d2abf02c8a9b981fff8e6419 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 15:47:14 +0000 Subject: [PATCH 2/3] Wire shared compiler into CAD font API --- Cargo.lock | 6 ++++++ Cargo.toml | 1 + crates/fonts/Cargo.toml | 1 + crates/fonts/src/shx/mod.rs | 32 +------------------------------- 4 files changed, 9 insertions(+), 31 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 41e541936..400bf85c0 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -714,6 +714,7 @@ version = "0.21.0" dependencies = [ "cadcraft-geom", "craft-dxf", + "craft-shx", "encoding_rs", "harfrust 0.14.0", "icu_segmenter", @@ -1193,6 +1194,11 @@ dependencies = [ "thiserror 2.0.21", ] +[[package]] +name = "craft-shx" +version = "0.1.0" +source = "git+https://github.com/storytold/craft-libs?rev=b3307f7814629961841b80ab907116252e41b34d#b3307f7814629961841b80ab907116252e41b34d" + [[package]] name = "craft-ui" version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml index b22766049..eae09c01a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -16,6 +16,7 @@ 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 = "006e4df225fca57e95ead0fb1f91920900798a99" } craft-dwg = { git = "https://github.com/storytold/craft-libs", rev = "7bb6c90076a0177d01cc4c482e7fd8dc9f31a8bf" } +craft-shx = { git = "https://github.com/storytold/craft-libs", rev = "b3307f7814629961841b80ab907116252e41b34d" } cadcraft-color = { path = "crates/color" } cadcraft-doc = { path = "crates/doc" } cadcraft-constraints = { path = "crates/constraints" } diff --git a/crates/fonts/Cargo.toml b/crates/fonts/Cargo.toml index e049844eb..33067fc90 100644 --- a/crates/fonts/Cargo.toml +++ b/crates/fonts/Cargo.toml @@ -11,6 +11,7 @@ workspace = true [dependencies] cadcraft-geom = { workspace = true } craft-dxf = { workspace = true } +craft-shx = { workspace = true } skrifa = { workspace = true } harfrust = { workspace = true } unicode-bidi = { workspace = true } diff --git a/crates/fonts/src/shx/mod.rs b/crates/fonts/src/shx/mod.rs index 016fe9821..08d679fbf 100644 --- a/crates/fonts/src/shx/mod.rs +++ b/crates/fonts/src/shx/mod.rs @@ -26,7 +26,6 @@ //! above, below, modes, encoding, embedding, 0) and then each shape as (number, length, //! record). -mod compile; mod parse; mod program; mod table; @@ -39,7 +38,7 @@ use std::sync::{Arc, PoisonError, RwLock}; use cadcraft_geom::Vec2; -pub use compile::compile; +pub use craft_shx::{ShxError, compile}; pub use table::{ FontProvider, add_drawing_dir, clear_registered, drawing_dirs, find, font_map, generation, installed, register, search_path, set_font_dirs, set_font_map, set_provider, set_search_path, @@ -47,35 +46,6 @@ pub use table::{ pub use text::ShxText; pub(crate) use text::{shape, width}; -/// Why a font file can't be read or a shape source can't be compiled. -#[derive(Clone, Debug, PartialEq, Eq)] -pub enum ShxError { - /// Not an SHX file (unknown signature). - NotShx, - /// The file ends inside its header or index. - Truncated, - /// The file is larger, or has more of something, than CADCraft reads. - TooLarge, - /// The file is well-formed but holds no shape. - Empty, - /// Shape source error: line and message. - Source(usize, String), -} - -impl std::fmt::Display for ShxError { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - match self { - ShxError::NotShx => f.write_str("not an SHX shape or font file"), - ShxError::Truncated => f.write_str("the SHX file is truncated"), - ShxError::TooLarge => f.write_str("the SHX file is too large"), - ShxError::Empty => f.write_str("the SHX file has no shapes"), - ShxError::Source(line, msg) => write!(f, "line {line}: {msg}"), - } - } -} - -impl std::error::Error for ShxError {} - /// The kind of SHX file. #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub enum ShxKind { From db70717001f744bf1460dff9a4b6f50f2bf605e2 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 16:06:13 +0000 Subject: [PATCH 3/3] Use shared MTEXT lexical tokens --- Cargo.lock | 4 +- Cargo.toml | 2 +- crates/fonts/src/mtext.rs | 304 +++++++++------------------------ crates/fonts/src/mtext/para.rs | 60 ++----- 4 files changed, 103 insertions(+), 267 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index c676a3293..e199ec5e3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1134,7 +1134,7 @@ dependencies = [ [[package]] name = "craft-codepage" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=6345459e1297ee85a7ca62b428a440691bd062c1#6345459e1297ee85a7ca62b428a440691bd062c1" +source = "git+https://github.com/storytold/craft-libs?rev=b1d4c67196ee7c6b668428c8d211e7d5171dff88#b1d4c67196ee7c6b668428c8d211e7d5171dff88" [[package]] name = "craft-docpath" @@ -1153,7 +1153,7 @@ dependencies = [ [[package]] name = "craft-dxf" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=6345459e1297ee85a7ca62b428a440691bd062c1#6345459e1297ee85a7ca62b428a440691bd062c1" +source = "git+https://github.com/storytold/craft-libs?rev=b1d4c67196ee7c6b668428c8d211e7d5171dff88#b1d4c67196ee7c6b668428c8d211e7d5171dff88" dependencies = [ "craft-codepage", "encoding_rs", diff --git a/Cargo.toml b/Cargo.toml index 4780e653d..023c6ef9b 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 = "6345459e1297ee85a7ca62b428a440691bd062c1" } +craft-dxf = { git = "https://github.com/storytold/craft-libs", rev = "b1d4c67196ee7c6b668428c8d211e7d5171dff88" } craft-dwg = { git = "https://github.com/storytold/craft-libs", rev = "7bb6c90076a0177d01cc4c482e7fd8dc9f31a8bf" } cadcraft-color = { path = "crates/color" } cadcraft-doc = { path = "crates/doc" } diff --git a/crates/fonts/src/mtext.rs b/crates/fonts/src/mtext.rs index 06c420aa6..bc7f88eba 100644 --- a/crates/fonts/src/mtext.rs +++ b/crates/fonts/src/mtext.rs @@ -12,79 +12,22 @@ pub fn plain_mtext(s: &str) -> String { let s = unescape_codes(s); let s = s.as_ref(); let mut out = String::with_capacity(s.len()); - let chars: Vec = s.chars().collect(); - let mut i = 0; - while let Some(&c) = chars.get(i) { - match c { - '\\' => { - let code = chars.get(i + 1).copied().unwrap_or(' '); - match code { - 'P' => { - out.push('\n'); - i += 2; - } - 'N' => { - out.push('\n'); - i += 2; - } - '~' => { - out.push(' '); - i += 2; - } - '\\' | '{' | '}' => { - out.push(code); - i += 2; - } - 'S' => { - // \Snum^den; \Snum/den; \Snum#den; - let mut j = i + 2; - let mut stack = String::new(); - while let Some(&ch) = chars.get(j) { - if ch == ';' { - break; - } - stack.push(match ch { - '^' | '#' => '/', - c => c, - }); - j += 1; - } - // "6\S1/2;" reads as "6 1/2". - if out.chars().last().is_some_and(|c| c.is_ascii_digit()) && stack.starts_with(|c: char| c.is_ascii_digit()) { - out.push(' '); - } - out.push_str(stack.trim_end_matches('/')); - i = j + 1; - } - 'L' | 'l' | 'O' | 'o' | 'K' | 'k' => i += 2, - // `\U+XXXX`: a character by its Unicode code, as the layout reads it. - 'U' if crate::unicode_escape(&chars, i).is_some() => { - if let Some(u) = crate::unicode_escape(&chars, i) { - out.push(u); - } - i += 7; - } - // Codes with an argument terminated by ';' (\f, \F, \H, \W, \Q, \T, \A, \C, \c, \p). - _ if takes_arg(code) => { - let mut j = i + 2; - while let Some(&ch) = chars.get(j) { - if ch == ';' { - break; - } - j += 1; - } - i = j + 1; - } - // An unknown code has no argument: drop the backslash and the code only. - _ => i += 2, + use craft_dxf::mtext_tokens::{StackMode, Token, Tokens}; + for token in Tokens::new(s, StackMode::Plain) { + match token { + Token::Control('P' | 'N') => out.push('\n'), + Token::Control('~') => out.push(' '), + Token::Control(c @ ('\\' | '{' | '}')) => out.push(c), + Token::Stack(raw) => { + let stack: String = raw.chars().map(|c| if matches!(c, '^' | '#') { '/' } else { c }).collect(); + if out.chars().last().is_some_and(|c| c.is_ascii_digit()) && stack.starts_with(|c: char| c.is_ascii_digit()) { + out.push(' '); } + out.push_str(stack.trim_end_matches('/')); } - '{' | '}' => i += 1, - '\r' => i += 1, - _ => { - out.push(c); - i += 1; - } + Token::Char('\r') => {} + Token::Char(c) => out.push(c), + _ => {} } } out @@ -226,7 +169,7 @@ const MAX_ATOMS: usize = 200_000; const STACK_SCALE: f64 = 0.7; // Shared DXF escape handling and parameterized-command classification. -pub(crate) use craft_dxf::mtext::{takes_arg, unescape_codes}; +pub(crate) use craft_dxf::mtext::unescape_codes; /// The line-spacing factor to lay out with: AutoCAD's 0.25..=4 range, 1 for a missing or /// non-finite value. @@ -234,19 +177,6 @@ pub(crate) fn sane_line_spacing(v: f64) -> f64 { if v.is_finite() && v > 0.0 { v.clamp(0.25, 4.0) } else { 1.0 } } -fn arg(chars: &[char], from: usize) -> (String, usize) { - let mut j = from; - let mut a = String::new(); - while let Some(&ch) = chars.get(j) { - if ch == ';' { - return (a, j + 1); - } - a.push(ch); - j += 1; - } - (a, j) -} - fn num(a: &str) -> Option { a.trim().parse::().ok().filter(|v| v.is_finite()) } @@ -284,7 +214,7 @@ fn font_switch(a: &str) -> Option { /// Parse MTEXT contents into words, spaces and paragraph breaks with their formats. fn parse(contents: &str, base: Fmt) -> (Vec, Vec) { - let chars: Vec = contents.chars().collect(); + use craft_dxf::mtext_tokens::{StackMode, Token, Tokens, split_stack}; let h_min = base.height * 1e-3; let h_max = if (base.height * 1e3).is_finite() { base.height * 1e3 } else { f64::MAX }; let mut fmts = vec![base]; @@ -324,158 +254,94 @@ fn parse(contents: &str, base: Fmt) -> (Vec, Vec) { *cur = fmts.len() - 1; } }; - let mut i = 0; - while let Some(&c) = chars.get(i) { + for token in Tokens::new(contents, StackMode::Structured) { if atoms.len() > MAX_ATOMS { break; } - match c { - '\\' => { - let code = chars.get(i + 1).copied().unwrap_or(' '); - i += 2; - match code { - 'P' | 'N' => { - end_word(&mut atoms, &mut word); - atoms.push(Atom::Para(cur)); - } - '~' => push_char(&mut word, '\u{a0}', cur), - '\\' | '{' | '}' => push_char(&mut word, code, cur), - 'L' => derive(&mut fmts, &mut cur, &|f| f.under = true), - 'l' => derive(&mut fmts, &mut cur, &|f| f.under = false), - 'O' => derive(&mut fmts, &mut cur, &|f| f.over = true), - 'o' => derive(&mut fmts, &mut cur, &|f| f.over = false), - 'K' | 'k' => {} - // `\U+XXXX`: a character by its Unicode code. - 'U' if i >= 2 && crate::unicode_escape(&chars, i - 2).is_some() => { - if let Some(u) = crate::unicode_escape(&chars, i - 2) { - push_char(&mut word, u, cur); - } - i += 5; - } - 'S' => { - let mut j = i; - let (mut top, mut bottom, mut sep) = (String::new(), String::new(), None); - while let Some(&ch) = chars.get(j) { - if ch == ';' { - break; - } - if ch == '\\' { - if let Some(&n) = chars.get(j + 1) { - if sep.is_none() { top.push(n) } else { bottom.push(n) } - } - j += 2; - continue; - } - if sep.is_none() && matches!(ch, '/' | '#' | '^') { - sep = Some(ch); - } else if sep.is_none() { - top.push(ch); - } else { - bottom.push(ch); - } - j += 1; - } - i = j + 1; - word.push((Seg::Stack(top, bottom, sep.unwrap_or('/')), cur)); - } - 'f' | 'F' => { - let (a, n) = arg(&chars, i); - i = n; - if let Some(font) = font_switch(&a) { - derive(&mut fmts, &mut cur, &|f| f.font = font.clone()); - } - } - 'H' => { - let (a, n) = arg(&chars, i); - i = n; - let (rel, v) = match a.trim().strip_suffix(['x', 'X']) { - Some(r) => (true, num(r)), - None => (false, num(&a)), - }; - if let Some(v) = v.filter(|v| *v > 0.0 && *v < 1e6) { - // Relative heights compound; keep the result within 1e-3..1e3 times the - // MTEXT's own height, never infinite. - derive(&mut fmts, &mut cur, &|f| f.height = if rel { (f.height * v).min(h_max).max(h_min) } else { v }); - } - } - 'W' => { - let (a, n) = arg(&chars, i); - i = n; - // `\W2x;` multiplies the current factor (like `\H2x;`); `\W2;` sets it. - let (rel, v) = match a.trim().strip_suffix(['x', 'X']) { - Some(r) => (true, num(r)), - None => (false, num(&a)), - }; - if let Some(v) = v.filter(|v| *v > 0.01 && *v < 100.0) { - // Relative factors compound; keep the result within the absolute range. - derive(&mut fmts, &mut cur, &|f| f.wf = if rel { (f.wf * v).clamp(0.01, 100.0) } else { v }); - } - } - 'Q' => { - let (a, n) = arg(&chars, i); - i = n; - if let Some(v) = num(&a).filter(|v| v.abs() < 85.0) { - derive(&mut fmts, &mut cur, &|f| f.oblique = v.to_radians()); - } - } - 'C' => { - let (a, n) = arg(&chars, i); - i = n; - if let Ok(v) = a.trim().parse::() { - let col = if v == 0 || v >= 256 { None } else { Some(MTextColor::Aci(v)) }; - derive(&mut fmts, &mut cur, &|f| f.color = col); - } - } - 'c' => { - let (a, n) = arg(&chars, i); - i = n; - if let Ok(v) = a.trim().parse::() { - derive(&mut fmts, &mut cur, &|f| f.color = Some(MTextColor::Rgb(v & 0xFF_FFFF))); - } - } - 'A' => { - let (a, n) = arg(&chars, i); - i = n; - if let Ok(v @ 0..=2) = a.trim().parse::() { - derive(&mut fmts, &mut cur, &|f| f.valign = v); - } - } - _ if takes_arg(code) => { - // \T and \p: argument up to ';'. - let (_, n) = arg(&chars, i); - i = n; - } - // An unknown code has no argument: only the backslash and the code are dropped. - _ => {} + match token { + Token::Control('P' | 'N') => { + end_word(&mut atoms, &mut word); + atoms.push(Atom::Para(cur)); + } + Token::Control('~') => push_char(&mut word, '\u{a0}', cur), + Token::Control(c @ ('\\' | '{' | '}')) => push_char(&mut word, c, cur), + Token::Control('L') => derive(&mut fmts, &mut cur, &|f| f.under = true), + Token::Control('l') => derive(&mut fmts, &mut cur, &|f| f.under = false), + Token::Control('O') => derive(&mut fmts, &mut cur, &|f| f.over = true), + Token::Control('o') => derive(&mut fmts, &mut cur, &|f| f.over = false), + Token::Stack(raw) => { + let (top, bottom, sep) = split_stack(raw); + word.push((Seg::Stack(top, bottom, sep.unwrap_or('/')), cur)); + } + Token::Argument('f' | 'F', a) => { + if let Some(font) = font_switch(a) { + derive(&mut fmts, &mut cur, &|f| f.font = font.clone()); } } - '{' => { + Token::Argument('H', a) => { + let (rel, v) = match a.trim().strip_suffix(['x', 'X']) { + Some(r) => (true, num(r)), + None => (false, num(a)), + }; + if let Some(v) = v.filter(|v| *v > 0.0 && *v < 1e6) { + // Relative heights compound; keep the result within 1e-3..1e3 times the + // MTEXT's own height, never infinite. + derive(&mut fmts, &mut cur, &|f| f.height = if rel { (f.height * v).min(h_max).max(h_min) } else { v }); + } + } + Token::Argument('W', a) => { + // `\W2x;` multiplies the current factor (like `\H2x;`); `\W2;` sets it. + let (rel, v) = match a.trim().strip_suffix(['x', 'X']) { + Some(r) => (true, num(r)), + None => (false, num(a)), + }; + if let Some(v) = v.filter(|v| *v > 0.01 && *v < 100.0) { + // Relative factors compound; keep the result within the absolute range. + derive(&mut fmts, &mut cur, &|f| f.wf = if rel { (f.wf * v).clamp(0.01, 100.0) } else { v }); + } + } + Token::Argument('Q', a) => { + if let Some(v) = num(a).filter(|v| v.abs() < 85.0) { + derive(&mut fmts, &mut cur, &|f| f.oblique = v.to_radians()); + } + } + Token::Argument('C', a) => { + if let Ok(v) = a.trim().parse::() { + let col = if v == 0 || v >= 256 { None } else { Some(MTextColor::Aci(v)) }; + derive(&mut fmts, &mut cur, &|f| f.color = col); + } + } + Token::Argument('c', a) => { + if let Ok(v) = a.trim().parse::() { + derive(&mut fmts, &mut cur, &|f| f.color = Some(MTextColor::Rgb(v & 0xFF_FFFF))); + } + } + Token::Argument('A', a) => { + if let Ok(v @ 0..=2) = a.trim().parse::() { + derive(&mut fmts, &mut cur, &|f| f.valign = v); + } + } + Token::OpenGroup => { if stack.len() < 256 { stack.push(cur); } - i += 1; } - '}' => { + Token::CloseGroup => { if let Some(f) = stack.pop() { cur = f; } - i += 1; } - '\r' => i += 1, - '\n' => { + Token::Char('\r') => {} + Token::Char('\n') => { end_word(&mut atoms, &mut word); atoms.push(Atom::Para(cur)); - i += 1; } - ' ' => { + Token::Char(' ') => { end_word(&mut atoms, &mut word); atoms.push(Atom::Space(cur)); - i += 1; - } - _ => { - push_char(&mut word, c, cur); - i += 1; } + Token::Char(c) => push_char(&mut word, c, cur), + _ => {} } } end_word(&mut atoms, &mut word); diff --git a/crates/fonts/src/mtext/para.rs b/crates/fonts/src/mtext/para.rs index 63b431610..7499cdce5 100644 --- a/crates/fonts/src/mtext/para.rs +++ b/crates/fonts/src/mtext/para.rs @@ -7,7 +7,7 @@ use cadcraft_geom::{Bounds2, Vec2}; -use super::{Atom, Fmt, MTextLayout, MTextParams, MTextPiece, Seg, arg, num, parse, seg_plain, seg_shape, seg_width, split_thai}; +use super::{Atom, Fmt, MTextLayout, MTextParams, MTextPiece, Seg, num, parse, seg_plain, seg_shape, seg_width, split_thai}; /// Columns an MTEXT's text flows through, each as wide as the MTEXT's width. #[derive(Clone, Debug, Default, PartialEq)] @@ -265,58 +265,28 @@ pub struct Paragraphs { impl Paragraphs { pub fn scan(contents: &str) -> Paragraphs { let contents = super::unescape_codes(contents); - let chars: Vec = contents.chars().collect(); + use craft_dxf::mtext_tokens::{StackMode, Token, Tokens}; let mut out = Paragraphs { states: vec![ParaProps::default()], ..Default::default() }; let mut cur = 0usize; - let mut i = 0; let brk = |out: &mut Paragraphs, cur: usize, column: bool| { out.para.push(cur); out.column_break.push(column); }; - while let Some(&c) = chars.get(i) { - match c { - '\\' => { - let code = chars.get(i + 1).copied().unwrap_or(' '); - i += 2; - match code { - 'P' => brk(&mut out, cur, false), - 'N' => brk(&mut out, cur, true), - '~' | '\\' | '{' | '}' | 'L' | 'l' | 'O' | 'o' | 'K' | 'k' => {} - 'S' => { - let mut j = i; - while let Some(&ch) = chars.get(j) { - if ch == ';' { - break; - } - j += if ch == '\\' { 2 } else { 1 }; - } - i = j + 1; - } - 'p' => { - let (a, n) = arg(&chars, i); - i = n; - if out.states.len() < MAX_STATES - && let Some(st) = out.states.get(cur) - { - let mut st = st.clone(); - st.apply(&a); - out.states.push(st); - cur = out.states.len() - 1; - } - } - _ if super::takes_arg(code) => { - let (_, n) = arg(&chars, i); - i = n; - } - // An unknown code has no argument: only the backslash and the code go. - _ => {} + for token in Tokens::new(&contents, StackMode::Structured) { + match token { + Token::Control('P') | Token::Char('\n') => brk(&mut out, cur, false), + Token::Control('N') => brk(&mut out, cur, true), + Token::Argument('p', a) => { + if out.states.len() < MAX_STATES + && let Some(st) = out.states.get(cur) + { + let mut st = st.clone(); + st.apply(a); + out.states.push(st); + cur = out.states.len() - 1; } } - '\n' => { - brk(&mut out, cur, false); - i += 1; - } - _ => i += 1, + _ => {} } } // A `\p` anywhere in a paragraph applies to all of it: the break pushed the state at