Skip to content

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

import: a value a file gives is one the engine can mean (#1593)

Hamza7808816e9a5b
diff --git a/crates/doc/src/attrs.rs b/crates/doc/src/attrs.rs
index 7c57736ef..43cc06aec 100644
--- a/crates/doc/src/attrs.rs
+++ b/crates/doc/src/attrs.rs
@@ -650,6 +650,34 @@ pub const MAX_TAB_LEADER: usize = 8;
/// Stops closer than this share a position.
pub const SAME_TAB_POSITION: f64 = 0.01;
+/// Smallest type size the engine accepts, in points.
+pub const MIN_TYPE_SIZE: f64 = 0.1;
+/// Largest type size the engine accepts, in points, as InDesign's own field allows.
+pub const MAX_TYPE_SIZE: f64 = 1296.0;
+/// Largest measure an imported indent, spacing or offset can carry, in points: the furthest tab
+/// stop, which is the widest page.
+pub const MAX_MEASURE: f64 = MAX_TAB_POSITION;
+
+/// A type size read from a file, or `None` when it is not a size the engine can mean.
+///
+/// A file can hold any number, including `NaN`, `inf`, `0`, a negative or `1e300`. Every importer
+/// had its own spelling of this test, or only checked that the value was finite -- so tagged text
+/// took `<cSize:1e300>` and set a 1e300 pt size on the text (#1593). A size outside the range is
+/// dropped rather than clamped, so the text keeps the size its style gives it.
+pub fn imported_type_size(pt: f64) -> Option<f64> {
+ (pt.is_finite() && (MIN_TYPE_SIZE..=MAX_TYPE_SIZE).contains(&pt)).then_some(pt)
+}
+
+/// An indent, spacing or offset read from a file, clamped to ±[`MAX_MEASURE`]; `None` when the
+/// value is not finite.
+///
+/// Unlike a type size this is clamped rather than dropped: a file that says "indent everything"
+/// still indents to the widest page the engine has, where dropping the value would quietly
+/// restore the style's own indent and hide what the file asked for (#1593).
+pub fn imported_measure(pt: f64) -> Option<f64> {
+ pt.is_finite().then(|| pt.clamp(-MAX_MEASURE, MAX_MEASURE))
+}
+
impl TabStop {
/// A finite position clamped to `0..=MAX_TAB_POSITION`.
pub fn clamp_position(v: f64) -> Result<f64, String> {
diff --git a/crates/textimport/src/docx.rs b/crates/textimport/src/docx.rs
index 87aaa3359..faa086435 100644
--- a/crates/textimport/src/docx.rs
+++ b/crates/textimport/src/docx.rs
@@ -186,8 +186,8 @@ fn run_attrs(rpr: Option<&El>) -> CharAttrs {
if let Some(s) = r.on("strike") {
a.strikethrough = Some(s);
}
- // Half-points; sizes outside the engine's 0.1–1296 pt range (or non-finite) are dropped.
- if let Some(pt) = r.val("sz").and_then(|v| v.parse::<f64>().ok()).map(|sz| sz / 2.0).filter(|pt| (0.1..=1296.0).contains(pt)) {
+ // Half-points; a size the engine cannot mean is dropped (#1593).
+ if let Some(pt) = r.val("sz").and_then(|v| v.parse::<f64>().ok()).map(|sz| sz / 2.0).and_then(designcraft_doc::attrs::imported_type_size) {
a.size = Some(pt);
}
if let Some(f) = r.child("rFonts").and_then(|f| f.attr("ascii").or(f.attr("hAnsi"))) {
@@ -210,7 +210,8 @@ fn run_attrs(rpr: Option<&El>) -> CharAttrs {
fn para_attrs(ppr: Option<&El>) -> ParaAttrs {
let mut a = ParaAttrs::default();
let Some(p) = ppr else { return a };
- let tw = |v: Option<&str>| v.and_then(|v| v.parse::<f64>().ok()).filter(|v| v.is_finite()).map(|v| v / 20.0);
+ // Twips, clamped: a file can hold 1e300, which became a 5e298 pt indent (#1593).
+ let tw = |v: Option<&str>| v.and_then(|v| v.parse::<f64>().ok()).map(|v| v / 20.0).and_then(designcraft_doc::attrs::imported_measure);
if let Some(sp) = p.child("spacing") {
a.space_before = tw(sp.attr("before"));
a.space_after = tw(sp.attr("after"));
@@ -633,6 +634,29 @@ mod tests {
assert_eq!(part_capped(&mut zip, "word/missing.xml", 4096).unwrap(), None);
}
+ /// A DOCX can hold any number where a measure belongs: `1e300` twips became a 5e298 pt
+ /// indent, and `w:sz` of `1e300` a 5e299 pt type size. Measures are clamped to the widest
+ /// page and a size the engine cannot mean is dropped (#1593).
+ #[test]
+ fn a_measure_or_size_a_file_cannot_mean_is_bounded() {
+ use designcraft_doc::attrs::MAX_MEASURE;
+ let doc = format!(
+ r#"<w:document {W}><w:body><w:p><w:pPr>
+ <w:spacing w:before="1e300" w:after="-1e300"/><w:ind w:left="1e300" w:firstLine="-1e300"/>
+ </w:pPr><w:r><w:rPr><w:sz w:val="1e300"/></w:rPr><w:t>big</w:t></w:r></w:p></w:body></w:document>"#
+ );
+ let i = import(&docx(&doc, "", "")).unwrap();
+ let p = &i.story.paras[0].para;
+ for (what, v) in
+ [("space before", p.space_before), ("space after", p.space_after), ("left indent", p.left_indent), ("first line", p.first_line_indent)]
+ {
+ let v = v.expect("the value is kept, bounded");
+ assert!(v.is_finite() && v.abs() <= MAX_MEASURE, "{what} is within the widest page: {v}");
+ }
+ let size = i.story.char_format_at(i.story.text.find("big").unwrap() + 1).over.size;
+ assert_eq!(size, None, "a size the engine cannot mean is dropped, so the style's size stands");
+ }
+
#[test]
fn imports_styles_runs_footnotes_and_tables() {
let styles = format!(
diff --git a/crates/textimport/src/docx_numbering.rs b/crates/textimport/src/docx_numbering.rs
index e3837e720..2a7d7873a 100644
--- a/crates/textimport/src/docx_numbering.rs
+++ b/crates/textimport/src/docx_numbering.rs
@@ -129,7 +129,8 @@ fn number_style(fmt: &str) -> Format {
}
fn parse_level(e: &El) -> Level {
- let tw = |v: Option<&str>| v.and_then(|v| v.trim().parse::<f64>().ok()).filter(|v| v.is_finite()).map(|v| v / 20.0);
+ // Twips, clamped as elsewhere: a level's indent is a measure, not any number (#1593).
+ let tw = |v: Option<&str>| v.and_then(|v| v.trim().parse::<f64>().ok()).map(|v| v / 20.0).and_then(designcraft_doc::attrs::imported_measure);
let ind = e.child("pPr").and_then(|p| p.child("ind"));
Level {
format: e.val("numFmt").map_or(Format::Number(NumberStyle::Arabic), |f| number_style(&f)),
diff --git a/crates/textimport/src/rtf.rs b/crates/textimport/src/rtf.rs
index 782071798..6254ca668 100644
--- a/crates/textimport/src/rtf.rs
+++ b/crates/textimport/src/rtf.rs
@@ -229,7 +229,7 @@ pub fn import(bytes: &[u8]) -> Result<Imported, ImportError> {
"strike" => restyle(&|s| s.strike = on),
// 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));
+ s.size = param.map(|p| p as f64 / 2.0).and_then(designcraft_doc::attrs::imported_type_size);
}),
"plain" => restyle(&|s| {
s.bold = false;
diff --git a/crates/textimport/src/tagged.rs b/crates/textimport/src/tagged.rs
index 3a890734a..74806ae4c 100644
--- a/crates/textimport/src/tagged.rs
+++ b/crates/textimport/src/tagged.rs
@@ -201,7 +201,9 @@ pub fn import(bytes: &[u8]) -> Result<Imported, ImportError> {
}
"cFont" => fmt.over.font_family = (!value.is_empty()).then(|| value.to_string()),
"cTypeface" => fmt.over.font_style = (!value.is_empty()).then(|| value.to_string()),
- "cSize" => fmt.over.size = value.trim().parse().ok().filter(|s: &f64| s.is_finite()),
+ // A size the engine cannot mean (0, negative, 1e300) is dropped, so the text
+ // keeps its style's size (#1593).
+ "cSize" => fmt.over.size = value.trim().parse().ok().and_then(designcraft_doc::attrs::imported_type_size),
"DefineParaStyle" | "DefineCharStyle" => {
let nm = value.split('=').next().unwrap_or("").trim().to_string();
let list = if name == "DefineParaStyle" { &mut para_styles } else { &mut char_styles };
@@ -276,6 +278,24 @@ mod tests {
assert_eq!(import("<ASCII-WIN>\r\n<ParaStyle:Body>A<0xD83D>B<0xDE00>C".as_bytes()).unwrap().story.text, "ABC");
}
+ /// A size does not have to be non-finite to be nonsense: a file can hold `1e300`, `0` or a
+ /// negative, and tagged text took all three and set them on the text (#1593). They are now
+ /// dropped, as RTF and DOCX already dropped them, so the text keeps its style's size.
+ #[test]
+ fn a_char_size_the_engine_cannot_mean_is_dropped() {
+ for v in ["1e300", "-5", "0", "1297", "0.09"] {
+ let src = format!("<ASCII-WIN>\r\n<ParaStyle:Body><cSize:{v}>big<cSize:>");
+ let i = import(src.as_bytes()).unwrap();
+ let size = i.story.char_format_at(i.story.text.find("big").unwrap() + 1).over.size;
+ assert_eq!(size, None, "cSize {v} is not a size the engine can mean");
+ }
+ // A size inside the range still arrives.
+ let src = "<ASCII-WIN>\r\n<ParaStyle:Body><cSize:18>big<cSize:>";
+ let i = import(src.as_bytes()).unwrap();
+ let size = i.story.char_format_at(i.story.text.find("big").unwrap() + 1).over.size;
+ assert_eq!(size, Some(18.0));
+ }
+
/// `f64::from_str` accepts "NaN" and "inf"; a non-finite size must not reach the story.
#[test]
fn non_finite_char_size_is_ignored() {