Skip to content

DesignCraft: An open-source page and layout design editor written by scratch in pure Rust

Skip control words and symbols used as RTF \uN fallbacks

NetterSilverbbb95930a267
diff --git a/crates/textimport/src/rtf.rs b/crates/textimport/src/rtf.rs
index baf065c30..410455669 100644
--- a/crates/textimport/src/rtf.rs
+++ b/crates/textimport/src/rtf.rs
@@ -134,6 +134,11 @@ pub fn import(bytes: &[u8]) -> Result<Imported, ImportError> {
}
if !next.is_ascii_alphabetic() {
// Control symbols: \\ \{ \} \~ (nbsp) \- (soft hyphen) \_ (nb hyphen).
+ i += 2;
+ if pending > 0 {
+ pending -= 1;
+ continue;
+ }
let skip = stack.last().is_some_and(|s| s.skip);
if !skip {
match next {
@@ -149,7 +154,6 @@ pub fn import(bytes: &[u8]) -> Result<Imported, ImportError> {
_ => {}
}
}
- i += 2;
continue;
}
// A control word with an optional numeric parameter and one delimiting space.
@@ -172,6 +176,11 @@ pub fn import(bytes: &[u8]) -> Result<Imported, ImportError> {
k += 1;
}
i = k;
+ // A control word is one character of the `\uN` fallback (a following `\u` is not).
+ if pending > 0 && word != "u" {
+ pending -= 1;
+ continue;
+ }
let skip = stack.last().is_some_and(|s| s.skip);
if SKIP.contains(&word.as_str()) {
flush(&mut st, &mut buf, &fmt);
@@ -320,6 +329,16 @@ Caf\'e9 \u8364? price\tab tab\line next\par
assert_eq!(i.story.text, "Rock\u{2014}-roll, x\t-y, ab");
}
+ /// A control word or control symbol is one character of the `\uN` fallback, so it is skipped
+ /// like a plain character and does not eat the text after it.
+ #[test]
+ fn control_fallback_after_unicode_is_skipped() {
+ let i = import(br"{\rtf1 \u8220\ldblquote Hi\u8221\rdblquote x\u8212\emdash y}").unwrap();
+ assert_eq!(i.story.text, "\u{201C}Hi\u{201D} x\u{2014}y");
+ let i = import(br"{\rtf1 \u8220\{Hi}").unwrap();
+ assert_eq!(i.story.text, "\u{201C}Hi");
+ }
+
/// Characters past U+FFFF are written as two `\uN` words, a UTF-16 surrogate pair (the form
/// our own exporter and Word write). Each half was converted alone and dropped.
#[test]