diff --git a/crates/docx/src/read/props.rs b/crates/docx/src/read/props.rs index 182ee179f2..2a218b2c47 100644 --- a/crates/docx/src/read/props.rs +++ b/crates/docx/src/read/props.rs @@ -15,7 +15,7 @@ use wordcraft_doc::section::{ use super::MAX_FONT_NAME; use super::drawing_color::adjust_lum; -use crate::units::{int, line_multiple, measure, on_off, tw, u32_of}; +use crate::units::{int, line_multiple, measure, on_off, tw, tw_i16, tw_u16, u32_of}; /// A text frame (`w:framePr` without a drop cap, ECMA-376 §17.3.1.11), sizes and offsets in /// points (clamped), names kept as OOXML writes them. @@ -407,10 +407,10 @@ impl PropCtx { p.tabs = Some(tabs); } "w:spacing" => { - if let Some(v) = tw(k, "w:before") { + if let Some(v) = tw_u16(k, "w:before") { p.space_before = Some(v.max(0.0)); } - if let Some(v) = tw(k, "w:after") { + if let Some(v) = tw_u16(k, "w:after") { p.space_after = Some(v.max(0.0)); } // HTML-style automatic spacing; "0" switches an inherited one off. @@ -433,15 +433,15 @@ impl PropCtx { } } "w:ind" => { - if let Some(v) = tw(k, "w:left").or_else(|| tw(k, "w:start")) { + if let Some(v) = tw_i16(k, "w:left").or_else(|| tw_i16(k, "w:start")) { p.indent_left = Some(v); } - if let Some(v) = tw(k, "w:right").or_else(|| tw(k, "w:end")) { + if let Some(v) = tw_i16(k, "w:right").or_else(|| tw_i16(k, "w:end")) { p.indent_right = Some(v); } - if let Some(v) = tw(k, "w:hanging") { + if let Some(v) = tw_i16(k, "w:hanging") { p.indent_first = Some(-v); - } else if let Some(v) = tw(k, "w:firstLine") { + } else if let Some(v) = tw_i16(k, "w:firstLine") { p.indent_first = Some(v); } // Character units (hundredths of a character), which win when non-zero. diff --git a/crates/docx/src/read/styles.rs b/crates/docx/src/read/styles.rs index 878edddf98..6e972a7604 100644 --- a/crates/docx/src/read/styles.rs +++ b/crates/docx/src/read/styles.rs @@ -2,6 +2,7 @@ use super::{ AbstractNum, DocxError, El, Level, LevelSuffix, Num, NumFormat, PartFormat, Reader, Style, StyleKind, StyleSheet, TablePart, TableStyleParts, VAlign, flag, int, props, tw, u32_of, }; +use crate::units::tw_i16; use std::sync::Arc; /// Built-in style names Word stores in lower case, and how the UI shows them. @@ -317,12 +318,12 @@ impl Reader<'_> { lv.align = props::align(j); } if let Some(ind) = l.child("w:pPr").and_then(|p| p.child("w:ind")) { - if let Some(v) = tw(ind, "w:left").or_else(|| tw(ind, "w:start")) { + if let Some(v) = tw_i16(ind, "w:left").or_else(|| tw_i16(ind, "w:start")) { lv.indent = v; } - if let Some(h) = tw(ind, "w:hanging") { + if let Some(h) = tw_i16(ind, "w:hanging") { lv.hanging = h; - } else if let Some(f) = tw(ind, "w:firstLine") { + } else if let Some(f) = tw_i16(ind, "w:firstLine") { lv.hanging = -f; } } diff --git a/crates/docx/src/units.rs b/crates/docx/src/units.rs index c9862fb047..2e5dfb0cd5 100644 --- a/crates/docx/src/units.rs +++ b/crates/docx/src/units.rs @@ -116,6 +116,27 @@ pub fn tw(e: &El, attr: &str) -> Option { e.attr(attr).and_then(|v| measure(v, 20.0)) } +/// Signed twips Word keeps in 16 bits (paragraph and list indents, ECMA-376 Part 1 §17.3.1.12): +/// a plain integer outside `i16` keeps its low 16 bits, two's complement, as Word reads it +/// (66256 → 720). Values with a unit or a fraction read as [`tw`]. +pub fn tw_i16(e: &El, attr: &str) -> Option { + let v = e.attr(attr)?; + match v.trim().parse::() { + Ok(i) => Some(f32::from(i as i16) / 20.0), + Err(_) => measure(v, 20.0), + } +} + +/// Unsigned twips Word keeps in 16 bits (paragraph spacing, §17.3.1.33): a plain integer above +/// 65535 keeps its low 16 bits (65537 → 1). Other values read as [`tw`]. +pub fn tw_u16(e: &El, attr: &str) -> Option { + let v = e.attr(attr)?; + match v.trim().parse::() { + Ok(i) if i > 0xFFFF => Some(f32::from((i & 0xFFFF) as u16) / 20.0), + _ => measure(v, 20.0), + } +} + /// ST_OnOff: absent `w:val` = on; `0`, `false`, `off` = off. pub fn on_off(e: &El) -> bool { match e.attr("w:val") { @@ -148,6 +169,24 @@ pub fn emu(pt: f32) -> String { mod tests { use super::*; + #[test] + fn twips_beyond_16_bits_wrap() { + let el = |a: &str| El { attrs: vec![("w:v".into(), a.into())], ..El::default() }; + let s = |a: &str| tw_i16(&el(a), "w:v"); + let u = |a: &str| tw_u16(&el(a), "w:v"); + assert_eq!(s("66256"), Some(36.0)); + assert_eq!(s("-65516"), Some(1.0)); + assert_eq!(s("98304"), Some(-1638.4)); + assert_eq!(s("32767"), Some(1638.35)); + assert_eq!(s("-32768"), Some(-1638.4)); + assert_eq!(s("1in"), Some(72.0)); + assert_eq!(s("x"), None); + assert_eq!(u("65537"), Some(0.05)); + assert_eq!(u("65535"), Some(3276.75)); + assert_eq!(u("12pt"), Some(12.0)); + assert_eq!(u("-20"), Some(-1.0)); + } + #[test] fn measures() { assert_eq!(measure("1440", 20.0), Some(72.0)); diff --git a/crates/docx/tests/it/main.rs b/crates/docx/tests/it/main.rs index 68caa1f5be..df7e28fdff 100644 --- a/crates/docx/tests/it/main.rs +++ b/crates/docx/tests/it/main.rs @@ -40,6 +40,7 @@ mod strict_drawings; mod text_alignment; mod text_boxes; mod toc_ranges; +mod twips_wrap; mod undefined_style; mod vml_shapes; mod word_xml; diff --git a/crates/docx/tests/it/twips_wrap.rs b/crates/docx/tests/it/twips_wrap.rs new file mode 100644 index 0000000000..a560070428 --- /dev/null +++ b/crates/docx/tests/it/twips_wrap.rs @@ -0,0 +1,55 @@ +//! Paragraph indents and spacing written as plain twips beyond 16 bits: Word keeps only the low +//! 16 bits (ECMA-376 Part 1 §17.3.1.12 `w:ind`, §17.3.1.33 `w:spacing`). Measured in Word: a +//! `w:left` of 66256 starts the text 36 pt in, a `w:right` of 66976 leaves a 72 pt right indent. +//! Fixtures are hand-written. + +use wordcraft_doc::Document; +use wordcraft_doc::props::ParaProps; + +use crate::fixtures::zip; + +/// A package whose body is `body`. +fn docx(body: &str) -> Vec { + let root = r#""#; + let doc = format!( + r#"{body}"# + ); + zip(&[("_rels/.rels", root.as_bytes()), ("word/document.xml", doc.as_bytes())]) +} + +fn props(body: &str) -> Vec { + let d: Document = wordcraft_docx::read(&docx(body)).unwrap(); + d.body.iter().filter_map(|b| b.as_para()).map(|p| p.props.clone()).collect() +} + +#[test] +fn indents_beyond_16_bits_wrap() { + let p = props( + r#"a + b + c + d"#, + ); + assert_eq!(p[0].indent_left, Some(36.0)); + assert_eq!(p[1].indent_right, Some(72.0)); + // 65556 -> 20 twips; -65516 -> 20 twips. + assert_eq!((p[2].indent_left, p[2].indent_right), (Some(1.0), Some(1.0))); + // 98304 = 0x18000 -> 0x8000 = -32768 as a signed 16-bit value; hanging 65896 -> 360. + assert_eq!((p[3].indent_left, p[3].indent_first), (Some(-1638.4), Some(-18.0))); +} + +#[test] +fn spacing_beyond_16_bits_wraps() { + let p = props(r#"a"#); + assert_eq!((p[0].space_before, p[0].space_after), (Some(0.05), Some(12.0))); +} + +#[test] +fn in_range_and_unit_values_are_unchanged() { + let p = props( + r#"a + b"#, + ); + assert_eq!((p[0].indent_left, p[0].indent_right, p[0].space_before), (Some(1638.35), Some(-1638.4), Some(3276.75))); + assert_eq!((p[1].indent_left, p[1].indent_right, p[1].space_before), (Some(72.0), Some(36.0), Some(12.0))); +} diff --git a/docs/file-format-parity.md b/docs/file-format-parity.md index f0022bfe8b..76ec3c9bfd 100644 --- a/docs/file-format-parity.md +++ b/docs/file-format-parity.md @@ -683,3 +683,4 @@ For field results, the engine parser treats a backslash in quoted arguments as e | 2026-10-27 | trivial | 2026-10-27 · #4533 review: numeric REF updates lazily index body bookmarks once per update, retaining the first body bookmark for duplicate names. Unsupported switches and ordinary REF/PAGEREF behavior are unchanged; a synthetic 64-reference regression covers repeated updates and duplicate names. Estimates unchanged. | | 2026-10-27 | trivial | 2026-10-27 · #4533: numeric REF fields use paragraph list numbers for no/full/relative context, reusing bounded numbering. Shared context already embedded in a level label is removed; merged-away cells do not count. Unsupported separator/text/position switches and ambiguous contexts or delimiter-free joins retain cached results. Synthetic OOXML regressions; no native Word or real-document validation, estimates unchanged. | | 2026-11-05 | trivial | DOCX: a block-level content control (including `w15:repeatingSection` / `w15:repeatingSectionItem` nested around one paragraph) followed by body-level bookmark, comment or permission markup (Word's `_GoBack` bookmark) is written back around its paragraph instead of inside it, so Word opens the saved file; the markers are kept at the end of that paragraph (P0069). | +| 2026-11-04 | trivial | DOCX: paragraph and list-level indents (`w:ind`) and paragraph spacing (`w:spacing` before/after) written as plain twips beyond 16 bits keep their low 16 bits, as Word reads them (`w:left="66256"` is a 36 pt indent), instead of pushing the text off the page (`twips_wrap.rs`, P0059) |