Skip to content

Commit 187afbd

Browse files
Reuse shared RTF text escaping and Windows-1252 decoding
Keep outline parser and formatting policy local. Preserve exact escaping with explicit compatibility policy and exhaustive scalar/byte oracles. Original text writer and preserved oracle by Nikita Rakitin (beb883d). Co-authored-by: Nikita Rakitin <nnnrrr93@gmail.com>
1 parent 1af5712 commit 187afbd

4 files changed

Lines changed: 99 additions & 29 deletions

File tree

‎Cargo.lock‎

Lines changed: 15 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎Cargo.toml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ vello_cpu = "0.2"
4949
vello_common = { version = "0.2", default-features = false }
5050
image = { version = "0.25", default-features = false, features = ["png", "jpeg", "webp", "gif", "tiff", "bmp"] }
5151
# craft-media, craft-audio and craft-video share types and stay on one rev.
52+
craft-rtf = { git = "https://github.com/storytold/craft-libs", rev = "63196e66f3033a6623eb629af95a104d26986725", version = "=0.1.0" }
5253
craft-media = { git = "https://github.com/storytold/craft-libs", rev = "63196e66f3033a6623eb629af95a104d26986725", version = "=0.1.0", default-features = false }
5354
craft-audio = { git = "https://github.com/storytold/craft-libs", rev = "63196e66f3033a6623eb629af95a104d26986725", version = "=0.1.0", default-features = false, features = ["opus-decode"] }
5455
# H.264, HEVC, VP9 and AV1 decoders.

‎crates/format/Cargo.toml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ description = "DeckCraft native file format (.deckcraft: zip of a JSON manifest
99
workspace = true
1010

1111
[dependencies]
12+
craft-rtf = { workspace = true }
1213
deckcraft-model = { workspace = true }
1314
serde = { workspace = true }
1415
serde_json = { workspace = true }

‎crates/format/src/rtf.rs‎

Lines changed: 82 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -10,24 +10,24 @@ use deckcraft_model::text::{Paragraph, RunKind};
1010

1111
/// RTF-escape `s` (non-ASCII as `\uN?`, UTF-16, signed as RTF wants).
1212
fn escape(out: &mut String, s: &str) {
13-
for c in s.chars() {
14-
match c {
15-
'\\' | '{' | '}' => {
16-
out.push('\\');
17-
out.push(c);
18-
}
19-
'\t' => out.push_str("\\tab "),
20-
'\u{b}' | '\n' => out.push_str("\\line "),
21-
c if c.is_ascii() && !c.is_ascii_control() => out.push(c),
22-
c if c.is_ascii() => {}
23-
c => {
24-
let mut buf = [0u16; 2];
25-
for u in c.encode_utf16(&mut buf) {
26-
out.push_str(&format!("\\u{}?", *u as i16));
27-
}
28-
}
29-
}
13+
// Keep outline text literal: the shared writer also supports page/column breaks and
14+
// shorthand controls that this app's deliberately small outline reader does not consume.
15+
// Delegate ordinary spans, including UTF-16 escaping, without allocating a second string.
16+
let mut start = 0;
17+
for (i, c) in s.char_indices() {
18+
let replacement = match c {
19+
'\u{000C}' | '\u{000E}' => "",
20+
'\u{00A0}' => "\\u160?",
21+
'\u{00AD}' => "\\u173?",
22+
'\u{2011}' => "\\u8209?",
23+
'\u{2028}' => "\\u8232?",
24+
_ => continue,
25+
};
26+
craft_rtf::push_esc(out, &s[start..i]);
27+
out.push_str(replacement);
28+
start = i + c.len_utf8();
3029
}
30+
craft_rtf::push_esc(out, &s[start..]);
3131
}
3232

3333
fn runs(out: &mut String, p: &Paragraph) {
@@ -297,7 +297,7 @@ impl Outline<'_> {
297297
&& let Some(h) = hex
298298
&& h != 0
299299
{
300-
self.line.push(cp1252(h));
300+
self.line.push(craft_rtf::codepage::cp1252(h));
301301
}
302302
}
303303

@@ -384,22 +384,75 @@ fn finish_surrogates(s: String) -> String {
384384
out
385385
}
386386

387-
fn cp1252(b: u8) -> char {
388-
const HIGH: [char; 32] = [
389-
'€', '\u{81}', '‚', 'ƒ', '„', '…', '†', '‡', 'ˆ', '‰', 'Š', '‹', 'Œ', '\u{8d}', 'Ž', '\u{8f}', '\u{90}', '‘', '’', '“', '”', '•', '–', '—',
390-
'˜', '™', 'š', '›', 'œ', '\u{9d}', 'ž', 'Ÿ',
391-
];
392-
match b {
393-
0x80..=0x9f => HIGH[(b - 0x80) as usize],
394-
_ => b as char,
395-
}
396-
}
397-
398387
#[cfg(test)]
399388
mod tests {
400389
#![allow(clippy::unwrap_used)]
401390
use super::*;
402391

392+
// Frozen pre-cutover implementations are test oracles, not production parsers.
393+
fn legacy_escape(out: &mut String, s: &str) {
394+
for c in s.chars() {
395+
match c {
396+
'\\' | '{' | '}' => {
397+
out.push('\\');
398+
out.push(c);
399+
}
400+
'\t' => out.push_str("\\tab "),
401+
'\u{b}' | '\n' => out.push_str("\\line "),
402+
c if c.is_ascii() && !c.is_ascii_control() => out.push(c),
403+
c if c.is_ascii() => {}
404+
c => {
405+
let mut buf = [0u16; 2];
406+
for u in c.encode_utf16(&mut buf) {
407+
out.push_str(&format!("\\u{}?", *u as i16));
408+
}
409+
}
410+
}
411+
}
412+
}
413+
414+
fn legacy_cp1252(b: u8) -> char {
415+
const HIGH: [char; 32] = [
416+
'€', '\u{81}', '‚', 'ƒ', '„', '…', '†', '‡', 'ˆ', '‰', 'Š', '‹', 'Œ', '\u{8d}', 'Ž', '\u{8f}', '\u{90}', '‘', '’', '“', '”', '•', '–',
417+
'—', '˜', '™', 'š', '›', 'œ', '\u{9d}', 'ž', 'Ÿ',
418+
];
419+
match b {
420+
0x80..=0x9f => HIGH[(b - 0x80) as usize],
421+
_ => b as char,
422+
}
423+
}
424+
425+
#[test]
426+
fn shared_escape_preserves_every_unicode_scalar() {
427+
for value in 0..=0x10ffff {
428+
let Some(c) = char::from_u32(value) else { continue };
429+
let mut buf = [0; 4];
430+
let text = c.encode_utf8(&mut buf);
431+
let mut expected = String::new();
432+
let mut actual = String::new();
433+
legacy_escape(&mut expected, text);
434+
escape(&mut actual, text);
435+
assert_eq!(actual, expected, "U+{value:04X}");
436+
}
437+
}
438+
439+
#[test]
440+
fn shared_escape_preserves_mixed_spans_and_append() {
441+
let text = "a\t中\u{a0}b\u{ad}c\u{2011}d\u{2028}e\u{c}f\u{e}g\n😀{}\\";
442+
let mut expected = String::from("prefix:");
443+
let mut actual = expected.clone();
444+
legacy_escape(&mut expected, text);
445+
escape(&mut actual, text);
446+
assert_eq!(actual, expected);
447+
}
448+
449+
#[test]
450+
fn shared_cp1252_preserves_all_bytes() {
451+
for b in 0..=u8::MAX {
452+
assert_eq!(craft_rtf::codepage::cp1252(b), legacy_cp1252(b));
453+
}
454+
}
455+
403456
/// A sign in a `\'hh` escape is not a hex digit and must be ignored.
404457
#[test]
405458
fn hex_escape_rejects_sign() {

0 commit comments

Comments
 (0)