diff --git a/Cargo.lock b/Cargo.lock index 17a5f4b1a..20723f8a5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -858,6 +858,11 @@ name = "craft-codepage" version = "0.1.0" source = "git+https://github.com/storytold/craft-libs?rev=0325b2d73676d736e94097dbcef01219cf25c7db#0325b2d73676d736e94097dbcef01219cf25c7db" +[[package]] +name = "craft-codepage" +version = "0.1.0" +source = "git+https://github.com/storytold/craft-libs?rev=63196e66f3033a6623eb629af95a104d26986725#63196e66f3033a6623eb629af95a104d26986725" + [[package]] name = "craft-fonts-build" version = "0.1.0" @@ -911,7 +916,7 @@ version = "0.1.0" source = "git+https://github.com/storytold/craft-libs?rev=0325b2d73676d736e94097dbcef01219cf25c7db#0325b2d73676d736e94097dbcef01219cf25c7db" dependencies = [ "chrono", - "craft-codepage", + "craft-codepage 0.1.0 (git+https://github.com/storytold/craft-libs?rev=0325b2d73676d736e94097dbcef01219cf25c7db)", "encoding_rs", ] @@ -924,6 +929,15 @@ dependencies = [ "thiserror 2.0.21", ] +[[package]] +name = "craft-rtf" +version = "0.1.0" +source = "git+https://github.com/storytold/craft-libs?rev=63196e66f3033a6623eb629af95a104d26986725#63196e66f3033a6623eb629af95a104d26986725" +dependencies = [ + "craft-codepage 0.1.0 (git+https://github.com/storytold/craft-libs?rev=63196e66f3033a6623eb629af95a104d26986725)", + "encoding_rs", +] + [[package]] name = "craft-ui" version = "0.1.0" @@ -1163,6 +1177,7 @@ dependencies = [ name = "deckcraft-format" version = "0.19.0" dependencies = [ + "craft-rtf", "deckcraft-model", "serde", "serde_json", diff --git a/Cargo.toml b/Cargo.toml index 548ad1165..bc9e057d4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -50,6 +50,7 @@ vello_common = { version = "0.2", default-features = false } image = { version = "0.25", default-features = false, features = ["png", "jpeg", "webp", "gif", "tiff", "bmp"] } # craft-media, craft-audio and craft-video share types and stay on one rev. craft-mime = { git = "https://github.com/storytold/craft-libs", rev = "0325b2d73676d736e94097dbcef01219cf25c7db", version = "=0.1.0" } +craft-rtf = { git = "https://github.com/storytold/craft-libs", rev = "63196e66f3033a6623eb629af95a104d26986725", version = "=0.1.0" } craft-media = { git = "https://github.com/storytold/craft-libs", rev = "63196e66f3033a6623eb629af95a104d26986725", version = "=0.1.0", default-features = false } craft-audio = { git = "https://github.com/storytold/craft-libs", rev = "63196e66f3033a6623eb629af95a104d26986725", version = "=0.1.0", default-features = false, features = ["opus-decode"] } # H.264, HEVC, VP9 and AV1 decoders. diff --git a/crates/format/Cargo.toml b/crates/format/Cargo.toml index 252529eeb..2dc1a545c 100644 --- a/crates/format/Cargo.toml +++ b/crates/format/Cargo.toml @@ -9,6 +9,7 @@ description = "DeckCraft native file format (.deckcraft: zip of a JSON manifest workspace = true [dependencies] +craft-rtf = { workspace = true } deckcraft-model = { workspace = true } serde = { workspace = true } serde_json = { workspace = true } diff --git a/crates/format/src/rtf.rs b/crates/format/src/rtf.rs index 5eac5ade9..202a80deb 100644 --- a/crates/format/src/rtf.rs +++ b/crates/format/src/rtf.rs @@ -10,24 +10,24 @@ use deckcraft_model::text::{Paragraph, RunKind}; /// RTF-escape `s` (non-ASCII as `\uN?`, UTF-16, signed as RTF wants). fn escape(out: &mut String, s: &str) { - for c in s.chars() { - match c { - '\\' | '{' | '}' => { - out.push('\\'); - out.push(c); - } - '\t' => out.push_str("\\tab "), - '\u{b}' | '\n' => out.push_str("\\line "), - c if c.is_ascii() && !c.is_ascii_control() => out.push(c), - c if c.is_ascii() => {} - c => { - let mut buf = [0u16; 2]; - for u in c.encode_utf16(&mut buf) { - out.push_str(&format!("\\u{}?", *u as i16)); - } - } - } + // Keep outline text literal: the shared writer also supports page/column breaks and + // shorthand controls that this app's deliberately small outline reader does not consume. + // Delegate ordinary spans, including UTF-16 escaping, without allocating a second string. + let mut start = 0; + for (i, c) in s.char_indices() { + let replacement = match c { + '\u{000C}' | '\u{000E}' => "", + '\u{00A0}' => "\\u160?", + '\u{00AD}' => "\\u173?", + '\u{2011}' => "\\u8209?", + '\u{2028}' => "\\u8232?", + _ => continue, + }; + craft_rtf::push_esc(out, &s[start..i]); + out.push_str(replacement); + start = i + c.len_utf8(); } + craft_rtf::push_esc(out, &s[start..]); } fn runs(out: &mut String, p: &Paragraph) { @@ -297,7 +297,7 @@ impl Outline<'_> { && let Some(h) = hex && h != 0 { - self.line.push(cp1252(h)); + self.line.push(craft_rtf::codepage::cp1252(h)); } } @@ -384,22 +384,75 @@ fn finish_surrogates(s: String) -> String { out } -fn cp1252(b: u8) -> char { - const HIGH: [char; 32] = [ - '€', '\u{81}', '‚', 'ƒ', '„', '…', '†', '‡', 'ˆ', '‰', 'Š', '‹', 'Œ', '\u{8d}', 'Ž', '\u{8f}', '\u{90}', '‘', '’', '“', '”', '•', '–', '—', - '˜', '™', 'š', '›', 'œ', '\u{9d}', 'ž', 'Ÿ', - ]; - match b { - 0x80..=0x9f => HIGH[(b - 0x80) as usize], - _ => b as char, - } -} - #[cfg(test)] mod tests { #![allow(clippy::unwrap_used)] use super::*; + // Frozen pre-cutover implementations are test oracles, not production parsers. + fn legacy_escape(out: &mut String, s: &str) { + for c in s.chars() { + match c { + '\\' | '{' | '}' => { + out.push('\\'); + out.push(c); + } + '\t' => out.push_str("\\tab "), + '\u{b}' | '\n' => out.push_str("\\line "), + c if c.is_ascii() && !c.is_ascii_control() => out.push(c), + c if c.is_ascii() => {} + c => { + let mut buf = [0u16; 2]; + for u in c.encode_utf16(&mut buf) { + out.push_str(&format!("\\u{}?", *u as i16)); + } + } + } + } + } + + fn legacy_cp1252(b: u8) -> char { + const HIGH: [char; 32] = [ + '€', '\u{81}', '‚', 'ƒ', '„', '…', '†', '‡', 'ˆ', '‰', 'Š', '‹', 'Œ', '\u{8d}', 'Ž', '\u{8f}', '\u{90}', '‘', '’', '“', '”', '•', '–', + '—', '˜', '™', 'š', '›', 'œ', '\u{9d}', 'ž', 'Ÿ', + ]; + match b { + 0x80..=0x9f => HIGH[(b - 0x80) as usize], + _ => b as char, + } + } + + #[test] + fn shared_escape_preserves_every_unicode_scalar() { + for value in 0..=0x10ffff { + let Some(c) = char::from_u32(value) else { continue }; + let mut buf = [0; 4]; + let text = c.encode_utf8(&mut buf); + let mut expected = String::new(); + let mut actual = String::new(); + legacy_escape(&mut expected, text); + escape(&mut actual, text); + assert_eq!(actual, expected, "U+{value:04X}"); + } + } + + #[test] + fn shared_escape_preserves_mixed_spans_and_append() { + let text = "a\t中\u{a0}b\u{ad}c\u{2011}d\u{2028}e\u{c}f\u{e}g\n😀{}\\"; + let mut expected = String::from("prefix:"); + let mut actual = expected.clone(); + legacy_escape(&mut expected, text); + escape(&mut actual, text); + assert_eq!(actual, expected); + } + + #[test] + fn shared_cp1252_preserves_all_bytes() { + for b in 0..=u8::MAX { + assert_eq!(craft_rtf::codepage::cp1252(b), legacy_cp1252(b)); + } + } + /// A sign in a `\'hh` escape is not a hex digit and must be ignored. #[test] fn hex_escape_rejects_sign() {