Skip to content

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

Ignore out-of-range font sizes in RTF import

NetterSilver3823d7d9420b
diff --git a/crates/textimport/src/rtf.rs b/crates/textimport/src/rtf.rs
index f336b7f36..baf065c30 100644
--- a/crates/textimport/src/rtf.rs
+++ b/crates/textimport/src/rtf.rs
@@ -216,7 +216,10 @@ pub fn import(bytes: &[u8]) -> Result<Imported, ImportError> {
"ul" => restyle(&|s| s.underline = on),
"ulnone" => restyle(&|s| s.underline = false),
"strike" => restyle(&|s| s.strike = on),
- "fs" => restyle(&|s| s.size = param.map(|p| p as f64 / 2.0)),
+ // Half-points; sizes outside the engine's 0.1–1296 pt range fall back to the default.
+ "fs" => restyle(&|s| {
+ s.size = param.map(|p| p as f64 / 2.0).filter(|pt| (0.1..=1296.0).contains(pt));
+ }),
"plain" => restyle(&|s| {
s.bold = false;
s.italic = false;
@@ -297,6 +300,18 @@ Caf\'e9 \u8364? price\tab tab\line next\par
assert!(!st.text.contains("Times"), "font table skipped");
}
+ /// `\fs` sizes outside the engine's 0.1–1296 pt character size range are not stored.
+ #[test]
+ fn font_size_out_of_range_is_not_stored() {
+ let i = import(br"{\rtf1\ansi\fs0 A\fs-8 B\fs999999999 C\fs24 D}").unwrap();
+ let st = &i.story;
+ for ch in ["A", "B", "C"] {
+ let size = st.format_after(st.text.find(ch).unwrap()).over.size;
+ assert!(size.is_none_or(|s| (0.1..=1296.0).contains(&s)), "{ch}: {size:?}");
+ }
+ assert_eq!(st.format_after(st.text.find('D').unwrap()).over.size, Some(12.0));
+ }
+
/// A '-' right after a control word is text unless a digit follows it (then it is a negative
/// parameter). It used to be swallowed.
#[test]