Skip to content

A fast, helpful, and open-source document parser

wip

Logan Markewich9452dde70912
diff --git a/Cargo.lock b/Cargo.lock
index 4d2817a..8712b9a 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -2099,6 +2099,7 @@ dependencies = [
"log",
"quick-xml",
"rustc-hash",
+ "rustybuzz",
"serde",
"serde_json",
"skrifa",
diff --git a/crates/liteparse-ooxml/Cargo.toml b/crates/liteparse-ooxml/Cargo.toml
index d702bd2..1c8f879 100644
--- a/crates/liteparse-ooxml/Cargo.toml
+++ b/crates/liteparse-ooxml/Cargo.toml
@@ -21,6 +21,9 @@ rustc-hash = "2"
# fontdb is pinned to the 0.22 line resvg/usvg already bring into the tree.
fontdb = "0.22"
skrifa = "0.45"
+# Pair-kerning for text measurement/drawing — pinned to the rustybuzz line
+# usvg 0.44 already brings into the tree.
+rustybuzz = "0.18"
unicode-segmentation = "1.13"
unicode-properties = "0.1"
# `raster` feature only — pinned to the tiny-skia line resvg 0.44 already
diff --git a/crates/liteparse-ooxml/src/docx/parse/body.rs b/crates/liteparse-ooxml/src/docx/parse/body.rs
index 02edd8e..86f5156 100644
--- a/crates/liteparse-ooxml/src/docx/parse/body.rs
+++ b/crates/liteparse-ooxml/src/docx/parse/body.rs
@@ -227,10 +227,17 @@ fn extend_from_run(r: RunXml, out: &mut Vec<Inline>, ctx: &mut ConvertCtx) {
// delimits begin/separate/end), so drop the marker entirely.
let Some(ty) = fc.fld_char_type else { continue };
flush(&mut acc, out);
+ // §17.16.7: `<w:checked>` reflects a user toggle; absent, the
+ // authored `<w:default>` is the rendered state.
+ let form_checkbox = fc
+ .ff_data
+ .and_then(|ff| ff.check_box)
+ .map(|cb| cb.checked.or(cb.default).map(|v| v.0).unwrap_or(false));
out.push(Inline::FieldChar(FieldChar {
field_char_type: FieldCharType::from(ty),
dirty: fc.dirty.map(|b| b.0),
fld_lock: fc.fld_lock.map(|b| b.0),
+ form_checkbox,
}));
}
RunChildXml::FootnoteRef(n) => {
@@ -328,6 +335,13 @@ fn append_para_children(
| ParaChildXml::MoveTo(w)
| ParaChildXml::SmartTag(w)
| ParaChildXml::CustomXml(w) => append_para_children(w.content, content, ctx),
+ // §17.5.2.31: run-level SDT (checkbox/dropdown/date/text content
+ // controls) — render the display content in place.
+ ParaChildXml::Sdt(s) => {
+ if let Some(inner) = s.content {
+ append_para_children(inner.content, content, ctx);
+ }
+ }
// Delete-side revision wrappers: content is deleted — drop it.
ParaChildXml::Del(_) | ParaChildXml::MoveFrom(_) => {}
ParaChildXml::PPr(_) => {} // already captured on the parent
@@ -600,6 +614,81 @@ mod tests {
collect_text(&para.content)
}
+ /// §17.16.7: a FORMCHECKBOX begin marker carries its rendered state in
+ /// `<w:ffData><w:checkBox>` — `<w:checked>` wins over `<w:default>`,
+ /// and an ffData without a checkBox member stays `None`.
+ #[test]
+ fn fld_char_ffdata_checkbox_state() {
+ let checkbox_state = |xml: &str| -> Option<bool> {
+ let p: ParaXml = quick_xml::de::from_str(xml).unwrap();
+ let mut ctx = ConvertCtx::new();
+ let (para, _) = convert_paragraph(p, &mut ctx);
+ para.content.iter().find_map(|i| match i {
+ Inline::FieldChar(fc) => Some(fc.form_checkbox),
+ _ => None,
+ })?
+ };
+ let field = |ff_data: &str| {
+ format!(
+ r#"<w:p xmlns:w="x"><w:r>
+ <w:fldChar w:fldCharType="begin">{ff_data}</w:fldChar>
+ </w:r></w:p>"#
+ )
+ };
+ assert_eq!(
+ checkbox_state(&field(
+ r#"<w:ffData><w:name w:val="C1"/><w:checkBox><w:sizeAuto/><w:default w:val="0"/></w:checkBox></w:ffData>"#
+ )),
+ Some(false)
+ );
+ assert_eq!(
+ checkbox_state(&field(
+ r#"<w:ffData><w:checkBox><w:default w:val="0"/><w:checked w:val="1"/></w:checkBox></w:ffData>"#
+ )),
+ Some(true)
+ );
+ // FORMTEXT-style ffData (no checkBox): not a checkbox.
+ assert_eq!(
+ checkbox_state(&field(r#"<w:ffData><w:name w:val="T1"/></w:ffData>"#)),
+ None
+ );
+ assert_eq!(checkbox_state(&field("")), None);
+ }
+
+ /// Run-level `<w:sdt>` (CT_SdtRun) — the display run inside
+ /// `<w:sdtContent>` must render in place, not vanish into the `Other`
+ /// catch-all. This is how `w14:checkbox` content controls carry their
+ /// ☐/☒ glyph, so dropping it silently loses form-checkbox state.
+ #[test]
+ fn run_level_sdt_content_renders_in_place() {
+ let text = para_text(
+ r#"<w:p xmlns:w="x">
+ <w:r><w:t>Answer: </w:t></w:r>
+ <w:sdt>
+ <w:sdtPr><w:id w:val="1"/></w:sdtPr>
+ <w:sdtContent><w:r><w:t>&#x2610;</w:t></w:r></w:sdtContent>
+ </w:sdt>
+ <w:r><w:t> Yes</w:t></w:r>
+ </w:p>"#,
+ );
+ assert_eq!(text, "Answer: ☐ Yes");
+ }
+
+ /// A `<w:sdt>` nested inside another `<w:sdt>`'s content (CT_SdtContentRun
+ /// is EG_PContent) flattens recursively.
+ #[test]
+ fn nested_run_level_sdt_flattens() {
+ let text = para_text(
+ r#"<w:p xmlns:w="x">
+ <w:sdt><w:sdtContent>
+ <w:r><w:t>a</w:t></w:r>
+ <w:sdt><w:sdtContent><w:r><w:t>b</w:t></w:r></w:sdtContent></w:sdt>
+ </w:sdtContent></w:sdt>
+ </w:p>"#,
+ );
+ assert_eq!(text, "ab");
+ }
+
/// Parse a `<w:p>` and return the run-elements of its first `TextRun`.
fn first_run_elements(xml: &str) -> Vec<RunElement> {
let p: ParaXml = quick_xml::de::from_str(xml).unwrap();
diff --git a/crates/liteparse-ooxml/src/docx/parse/body_schema.rs b/crates/liteparse-ooxml/src/docx/parse/body_schema.rs
index 7340ba8..dfbd8b4 100644
--- a/crates/liteparse-ooxml/src/docx/parse/body_schema.rs
+++ b/crates/liteparse-ooxml/src/docx/parse/body_schema.rs
@@ -150,6 +150,13 @@ pub(crate) enum ParaChildXml {
/// §17.5.1.6 `<w:customXml>` (CT_CustomXmlRun) — content is flattened.
#[serde(rename = "customXml")]
CustomXml(RunTrackChangeXml),
+ /// `<w:sdt>` (CT_SdtRun §17.5.2.31) — run-level structured document tag
+ /// (checkbox / dropdown / date / plain-text content controls). The
+ /// display content lives in `<w:sdtContent>` (EG_PContent) and is
+ /// flattened in place; dropping it loses real text (e.g. the ☐/☒ glyph
+ /// run of a `w14:checkbox`).
+ #[serde(rename = "sdt")]
+ Sdt(Box<SdtRunXml>),
/// `<w:pPr>` is captured on `ParaXml` directly, but serde's untagged
/// enum still has to handle it if it appears in `$value` ordering.
#[serde(rename = "pPr")]
@@ -158,6 +165,22 @@ pub(crate) enum ParaChildXml {
Other,
}
+/// `<w:sdt>` at run level (CT_SdtRun). Mirrors [`SdtBlockXml`]: only
+/// `<w:sdtContent>` matters — `<w:sdtPr>`/`<w:sdtEndPr>` are ignored.
+#[derive(Deserialize, Default)]
+pub(crate) struct SdtRunXml {
+ #[serde(rename = "sdtContent", default)]
+ pub content: Option<SdtRunContentXml>,
+}
+
+/// CT_SdtContentRun — the same `EG_PContent` children as `<w:p>`, so a
+/// nested `<w:sdt>` recurses through the same variant.
+#[derive(Deserialize, Default)]
+pub(crate) struct SdtRunContentXml {
+ #[serde(rename = "$value", default)]
+ pub content: Vec<ParaChildXml>,
+}
+
/// A run-level revision (`ins`/`del`/`moveFrom`/`moveTo`, CT_RunTrackChange)
/// or structural (`smartTag`/`customXml`) wrapper. Its content model is
/// `EG_PContent` — the same children as `<w:p>` — so it nests recursively.
@@ -332,6 +355,29 @@ pub(crate) struct FldCharXml {
pub dirty: Option<AttrBool>,
#[serde(rename = "@fldLock", default)]
pub fld_lock: Option<AttrBool>,
+ /// §17.16.17 `<w:ffData>` — legacy form-field data on the *begin* marker.
+ #[serde(rename = "ffData", default)]
+ pub ff_data: Option<FfDataXml>,
+}
+
+/// §17.16.17 CT_FFData — only the checkbox member is modelled; text/dropdown
+/// form fields carry their display value in the result zone and need nothing
+/// from here.
+#[derive(Deserialize, Default)]
+pub(crate) struct FfDataXml {
+ #[serde(rename = "checkBox", default)]
+ pub check_box: Option<FfCheckBoxXml>,
+}
+
+/// §17.16.7 CT_FFCheckBox. A FORMCHECKBOX field has no result-zone text —
+/// its rendered state lives here: `<w:checked>` when the user toggled it,
+/// `<w:default>` otherwise.
+#[derive(Deserialize, Default)]
+pub(crate) struct FfCheckBoxXml {
+ #[serde(rename = "default", default)]
+ pub default: Option<OnOff>,
+ #[serde(rename = "checked", default)]
+ pub checked: Option<OnOff>,
}
#[derive(Deserialize)]
@@ -686,7 +732,7 @@ pub(crate) struct TableCellXml {
// ── helpers ────────────────────────────────────────────────────────────────
-pub(crate) use crate::docx::parse::primitives::AttrBool;
+pub(crate) use crate::docx::parse::primitives::{AttrBool, OnOff};
#[cfg(test)]
mod tests {
diff --git a/crates/liteparse-ooxml/src/model/types/content.rs b/crates/liteparse-ooxml/src/model/types/content.rs
index e29a69d..aebc5d0 100644
--- a/crates/liteparse-ooxml/src/model/types/content.rs
+++ b/crates/liteparse-ooxml/src/model/types/content.rs
@@ -78,6 +78,20 @@ pub struct FieldChar {
pub dirty: Option<bool>,
/// Field is locked from updates.
pub fld_lock: Option<bool>,
+ /// §17.16.7: legacy FORMCHECKBOX state from the begin marker's
+ /// `<w:ffData><w:checkBox>`. `Some(checked)` — the field renders as a
+ /// ☒/☐ glyph and has no result-zone text, so consumers must substitute
+ /// from here or the checkbox vanishes.
+ pub form_checkbox: Option<bool>,
+}
+
+impl FieldChar {
+ /// The ballot-box glyph a FORMCHECKBOX begin marker renders as, matching
+ /// the U+2610/U+2612 pair `w14:checkbox` content controls use.
+ pub fn form_checkbox_glyph(&self) -> Option<&'static str> {
+ self.form_checkbox
+ .map(|checked| if checked { "☒" } else { "☐" })
+ }
}
/// §17.18.29 ST_FldCharType
diff --git a/crates/liteparse-ooxml/src/render/fonts.rs b/crates/liteparse-ooxml/src/render/fonts.rs
index 4ba5391..9464704 100644
--- a/crates/liteparse-ooxml/src/render/fonts.rs
+++ b/crates/liteparse-ooxml/src/render/fonts.rs
@@ -6,7 +6,10 @@
//! Skia — everything else in `render/layout/` is vendored verbatim.
//!
//! Where both engines have the requested face, results agree closely on
-//! widths and are bit-exact on line metrics; divergences beyond that trace to
+//! widths (now pair-kerned via [`kerned_char_advances`]); line metrics
+//! deliberately diverge from upstream's hhea numbers — the measurer derives
+//! them Word-style from OS/2 win metrics (see
+//! `layout::measurer::word_text_metrics`). Divergences beyond that trace to
//! a *resolution* difference, which is why this file is mostly resolution
//! policy and hardly any arithmetic.
//!
@@ -941,6 +944,71 @@ pub fn wght_location(font: &skrifa::FontRef<'_>, wght: u16) -> skrifa::instance:
font.axes().location([("wght", f32::from(wght))])
}
+/// Per-char advances for `text` at `size_px`, with pair kerning applied.
+///
+/// Shapes through rustybuzz with every ligature feature disabled
+/// (`liga`/`clig`/`dlig`/`hlig`/`calt`), so substitutions that merge
+/// characters are off and the glyph stream stays 1:1 with the cmap walk the
+/// measurer and the raster both do — the kerned advance for char *i* is
+/// exactly the pen delta the drawing loop should apply after drawing char
+/// *i*'s cmap glyph. Word applies the same model: kerning adjusts pen
+/// positions, it never re-substitutes glyphs.
+///
+/// A shaping-driven merge that survives anyway (e.g. a *required* ligature)
+/// accumulates its whole advance on the cluster's first char and leaves the
+/// rest at 0.0, so the *sum* — what line fitting consumes — is still right.
+///
+/// `None` when the face doesn't parse for shaping; callers keep their
+/// unshaped cmap walk as the fallback so a broken font measures as before.
+pub fn kerned_char_advances(
+ data: &[u8],
+ index: u32,
+ wght: u16,
+ size_px: f32,
+ text: &str,
+) -> Option<Vec<f32>> {
+ let mut face = rustybuzz::Face::from_slice(data, index)?;
+ // Mirrors `wght_location`: set only `wght`; rustybuzz clamps to the
+ // axis range and ignores the variation on a static face.
+ face.set_variations(&[rustybuzz::Variation {
+ tag: rustybuzz::ttf_parser::Tag::from_bytes(b"wght"),
+ value: f32::from(wght),
+ }]);
+ let upem = face.units_per_em() as f32;
+ if upem <= 0.0 {
+ return None;
+ }
+ let scale = size_px / upem;
+
+ let mut buffer = rustybuzz::UnicodeBuffer::new();
+ buffer.push_str(text);
+ // Clusters default to UTF-8 byte offsets; guess script/direction/language
+ // from content like every other shaping consumer does.
+ buffer.guess_segment_properties();
+
+ const OFF: &[&[u8; 4]] = &[b"liga", b"clig", b"dlig", b"hlig", b"calt"];
+ let features: Vec<rustybuzz::Feature> = OFF
+ .iter()
+ .map(|tag| rustybuzz::Feature::new(rustybuzz::ttf_parser::Tag::from_bytes(tag), 0, ..))
+ .collect();
+ let glyphs = rustybuzz::shape(&face, &features, buffer);
+
+ // Map cluster byte offsets back to char ordinals.
+ let byte_starts: Vec<usize> = text.char_indices().map(|(i, _)| i).collect();
+ let mut advances = vec![0.0f32; byte_starts.len()];
+ for (info, pos) in glyphs
+ .glyph_infos()
+ .iter()
+ .zip(glyphs.glyph_positions().iter())
+ {
+ let ord = byte_starts
+ .binary_search(&(info.cluster as usize))
+ .unwrap_or_else(|next| next.saturating_sub(1));
+ advances[ord] += pos.x_advance as f32 * scale;
+ }
+ Some(advances)
+}
+
fn system_entry(id: fontdb::ID, wght: u16) -> TypefaceEntry {
TypefaceEntry {
id,
diff --git a/crates/liteparse-ooxml/src/render/layout/fragment/collect.rs b/crates/liteparse-ooxml/src/render/layout/fragment/collect.rs
index 86d0af2..19bf007 100644
--- a/crates/liteparse-ooxml/src/render/layout/fragment/collect.rs
+++ b/crates/liteparse-ooxml/src/render/layout/fragment/collect.rs
@@ -484,6 +484,9 @@ where
// Emitted = substitution was rendered, skip remaining result TextRuns until End.
let mut field_sub_pending: Option<String> = None;
let mut field_sub_emitted = false;
+ // §17.16.7: FORMCHECKBOX glyph carried by the current field's begin
+ // marker; substituted at Separate/End when no other result exists.
+ let mut field_checkbox: Option<&'static str> = None;
// §17.16.19 MERGEFORMAT — pre-resolve formatting for each complex
// field's substitution against raw inlines, so empty placeholder
@@ -783,6 +786,10 @@ where
field_instr.clear();
field_sub_pending = None;
field_sub_emitted = false;
+ // §17.16.7: a FORMCHECKBOX field's rendered state
+ // rides on the begin marker's ffData — there is no
+ // result-zone text to fall back to.
+ field_checkbox = fc.form_checkbox_glyph();
}
FieldCharType::Separate => {
// §17.16.4.1: parse accumulated instruction, evaluate
@@ -790,6 +797,11 @@ where
if let Ok(parsed) = crate::field::parse(&field_instr) {
field_sub_pending = evaluate_field_instruction(&parsed, field_ctx);
}
+ if field_sub_pending.is_none()
+ && let Some(glyph) = field_checkbox.take()
+ {
+ field_sub_pending = Some(glyph.to_string());
+ }
// §17.16.19: bind the formatting source resolved
// against raw inlines, so the End fallback path
// can recover an empty placeholder run's rPr
@@ -800,6 +812,15 @@ where
field_depth -= 1; // now collect result runs (unless substituted)
}
FieldCharType::End => {
+ // A FORMCHECKBOX authored without a Separate
+ // marker never reached the Separate arm; its
+ // glyph is still waiting here.
+ if field_sub_pending.is_none()
+ && !field_sub_emitted
+ && let Some(glyph) = field_checkbox.take()
+ {
+ field_sub_pending = Some(glyph.to_string());
+ }
// Substitution still pending at End: the unit
// stream never carried a result run (either the
// placeholder was empty and got swallowed by
@@ -1287,18 +1308,21 @@ mod tests {
field_char_type: FieldCharType::Begin,
dirty: None,
fld_lock: None,
+ form_checkbox: None,
}),
Inline::InstrText("PAGE".into()),
Inline::FieldChar(FieldChar {
field_char_type: FieldCharType::Separate,
dirty: None,
fld_lock: None,
+ form_checkbox: None,
}),
text_run("3"),
Inline::FieldChar(FieldChar {
field_char_type: FieldCharType::End,
dirty: None,
fld_lock: None,
+ form_checkbox: None,
}),
];
let ctx = default_ctx(12.0);
@@ -1603,6 +1627,7 @@ mod tests {
field_char_type: kind,
dirty: None,
fld_lock: None,
+ form_checkbox: None,
})
}
diff --git a/crates/liteparse-ooxml/src/render/layout/fragment/segment.rs b/crates/liteparse-ooxml/src/render/layout/fragment/segment.rs
index 3b5cd13..746ec59 100644
--- a/crates/liteparse-ooxml/src/render/layout/fragment/segment.rs
+++ b/crates/liteparse-ooxml/src/render/layout/fragment/segment.rs
@@ -450,6 +450,7 @@ mod tests {
field_char_type: FieldCharType::Begin,
dirty: None,
fld_lock: None,
+ form_checkbox: None,
}),
text_run("instr"),
];
@@ -748,6 +749,7 @@ mod tests {
field_char_type: FieldCharType::Begin,
dirty: None,
fld_lock: None,
+ form_checkbox: None,
}),
text_run("c"),
text_run("d"),
diff --git a/crates/liteparse-ooxml/src/render/layout/line.rs b/crates/liteparse-ooxml/src/render/layout/line.rs
index 24a1ad5..1cd6469 100644
--- a/crates/liteparse-ooxml/src/render/layout/line.rs
+++ b/crates/liteparse-ooxml/src/render/layout/line.rs
@@ -219,7 +219,15 @@ pub fn fit_lines_with_first(
line_text_height = Pt::ZERO;
line_ascent = Pt::ZERO;
last_break_point = None;
- // Don't advance i — re-evaluate this fragment on the new line.
+ // Rewind to the break point — not to `i` — so every fragment the
+ // break moved onto the new line is re-accumulated. Re-evaluating
+ // only fragment `i` leaves the widths of fragments in
+ // `break_at..i` out of `line_width`, and the under-counted line
+ // then accepts words past the margin. Reachable whenever several
+ // non-breakpoint fragments follow the last break: a per-character
+ // split of an over-wide word (narrow table cells), or a word
+ // split across `<w:r>` runs.
+ i = break_at;
continue;
}
@@ -281,6 +289,28 @@ pub fn fit_lines_with_first(
});
}
+ if std::env::var_os("LITEPARSE_TRACE_OVERFLOW").is_some() {
+ for l in &lines {
+ let interesting = fragments[l.start..l.end].iter().any(|f| matches!(f, Fragment::Text { text, .. } if text.contains("(Date)") || text.contains("Completion") || text.contains("C")));
+ if interesting || l.width > remaining_width.max(first_line_width) + Pt::new(0.1) {
+ let texts: Vec<&str> = fragments[l.start..l.end]
+ .iter()
+ .map(|f| match f {
+ Fragment::Text { text, .. } => &text[..],
+ _ => "<frag>",
+ })
+ .collect();
+ eprintln!(
+ " overflow line: width={:.1} max={:.1}/{:.1} frags={:?}",
+ l.width.raw(),
+ first_line_width.raw(),
+ remaining_width.raw(),
+ texts
+ );
+ }
+ }
+ }
+
lines
}
@@ -423,6 +453,82 @@ mod tests {
assert_eq!(lines[0].end, 1);
}
+ /// Regression: when an overflow breaks at an earlier break point, the
+ /// fragments between the break point and the overflowing fragment move to
+ /// the new line and must be re-accumulated into its width. The old code
+ /// resumed at the overflowing fragment, so a run of non-breakpoint
+ /// fragments (a per-character split word: "C","o","m",…) was carried at
+ /// width 0 and the next word rode past the margin ("Completion (Date)"
+ /// escaping its table cell).
+ #[test]
+ fn break_rewinds_to_break_point_and_reaccumulates() {
+ // max 61: "for " starts line 2; chars overflow at 't' → line 2 becomes
+ // ["for "], chars restart line 3; "(Date)" must then wrap to line 4.
+ let mut frags = vec![text_frag("for ", 18.0)];
+ for ch in ["C", "o", "m", "p", "l", "e", "t", "i", "o", "n"] {
+ frags.push(text_frag(ch, 6.3));
+ }
+ frags.push(text_frag(" ", 3.0));
+ frags.push(text_frag("(Date)", 34.0));
+ let lines = fit_lines(&frags, Pt::new(61.0));
+
+ // Every produced line's width must equal the sum of its fragments —
+ // the old code dropped `break_at..i` widths, reporting 23.8 for a
+ // 63.4pt line and letting "(Date)" ride past the cell border.
+ for l in &lines {
+ let sum: f32 = frags[l.start..l.end].iter().map(|f| f.width().raw()).sum();
+ assert!(
+ (l.width.raw() - sum).abs() < 0.01,
+ "line {}..{} width {} != fragment sum {}",
+ l.start,
+ l.end,
+ l.width.raw(),
+ sum
+ );
+ }
+ // And no multi-fragment line may exceed the max (a single oversized
+ // fragment is the only sanctioned overflow).
+ for l in &lines {
+ if l.end - l.start > 1 {
+ assert!(
+ l.width <= Pt::new(61.0),
+ "multi-fragment line {}..{} exceeds max: {lines:?}",
+ l.start,
+ l.end
+ );
+ }
+ }
+ }
+
+ /// An oversized first word overflows its line (Word behavior), but the
+ /// NEXT word must still wrap — it may not ride along past the margin.
+ #[test]
+ fn word_after_oversized_word_wraps() {
+ let frags = vec![
+ text_frag("Deadline ", 40.0),
+ text_frag("for ", 18.0),
+ text_frag("Completion ", 61.0),
+ text_frag("(Date)", 30.0),
+ ];
+ let lines = fit_lines(&frags, Pt::new(56.0));
+ // Expect: "Deadline " / "for " ... wait "Deadline "+"for " = 58 > 56 →
+ // "Deadline " / "for " / "Completion " / "(Date)" = 4 lines, or
+ // "Deadline " then "for " joins? 40+18=58>56 → separate. Key claim:
+ // "(Date)" is NOT on the same line as "Completion ".
+ let completion_line = lines
+ .iter()
+ .position(|l| (l.start..l.end).contains(&2))
+ .unwrap();
+ let date_line = lines
+ .iter()
+ .position(|l| (l.start..l.end).contains(&3))
+ .unwrap();
+ assert_ne!(
+ completion_line, date_line,
+ "word after an oversized word must wrap to the next line: {lines:?}"
+ );
+ }
+
#[test]
fn line_break_forces_new_line() {
let frags = vec![
diff --git a/crates/liteparse-ooxml/src/render/layout/measurer.rs b/crates/liteparse-ooxml/src/render/layout/measurer.rs
index f1d488d..325f555 100644
--- a/crates/liteparse-ooxml/src/render/layout/measurer.rs
+++ b/crates/liteparse-ooxml/src/render/layout/measurer.rs
@@ -1,8 +1,9 @@
//! Text measurer — resolves text widths through a `FontRegistry`.
//!
-//! A port of the upstream (dxpdf) Skia measurer over fontdb + skrifa. The
-//! measurement arithmetic matches upstream: `measure_str` is a cmap →
-//! sum-of-`hmtx`-advances walk with no shaping or kerning, then §17.3.2.45
+//! A port of the upstream (dxpdf) Skia measurer over fontdb + skrifa, with
+//! pair kerning layered on top via `fonts::kerned_char_advances`. The
+//! measurement arithmetic otherwise matches upstream: `measure_str` is a
+//! cmap → sum-of-`hmtx`-advances walk (the un-kerned fallback), then §17.3.2.45
//! `text_scale` on the advances and §17.3.2.35 `char_spacing` per character. An
//! unmapped codepoint contributes glyph 0's (.notdef) advance, which is what a
//! cmap lookup yields and what Skia does.
@@ -130,13 +131,7 @@ impl<'r> TextMeasurer<'r> {
let location = wght_location(&font, wght);
let m = font.metrics(Size::new(px), &location);
Some((
- TextMetrics {
- ascent: Pt::new(m.ascent),
- // skrifa reports descent negative-down; TextMetrics wants
- // positive-down.
- descent: Pt::new(-m.descent),
- leading: Pt::new(m.leading.max(0.0)),
- },
+ word_text_metrics(&font, px, &m),
m.underline.map(|u| (u.offset, u.thickness)),
))
});
@@ -161,14 +156,29 @@ impl<'r> TextMeasurer<'r> {
}
}
- /// Raw cmap advance sum for `text` at the slot's size — upstream's
- /// `measure_str` equivalent (no shaping, no kerning). An unmapped
- /// codepoint takes glyph 0 (.notdef), which is what a cmap lookup
- /// genuinely yields — matching Skia rather than skipping the character.
+ /// Raw advance sum for `text` at the slot's size, pair-kerned.
+ ///
+ /// The primary path is the shared shaped walk
+ /// ([`crate::render::fonts::kerned_char_advances`]) — the same per-char
+ /// advances the raster's pen uses, so ink lands inside the measured
+ /// extent by construction. A face that fails shaping falls back to
+ /// upstream's `measure_str` equivalent (cmap walk, no kerning). An
+ /// unmapped codepoint takes glyph 0 (.notdef) in both paths, which is
+ /// what a cmap lookup genuinely yields — matching Skia rather than
+ /// skipping the character.
fn raw_advance(&self, face: fontdb::ID, wght: u16, size: Pt, text: &str) -> f32 {
self.registry
.db()
.with_face_data(face, |data, index| {
+ if let Some(advances) = crate::render::fonts::kerned_char_advances(
+ data,
+ index,
+ wght,
+ f32::from(size),
+ text,
+ ) {
+ return advances.iter().sum();
+ }
let Ok(font) = skrifa::FontRef::from_index(data, index) else {
return 0.0;
};
@@ -305,11 +315,7 @@ impl<'r> TextMeasurer<'r> {
let font = skrifa::FontRef::from_index(data, index).ok()?;
let location = wght_location(&font, typeface.wght);
let m = font.metrics(Size::new(px), &location);
- Some(TextMetrics {
- ascent: Pt::new(m.ascent),
- descent: Pt::new(-m.descent),
- leading: Pt::new(m.leading.max(0.0)),
- })
+ Some(word_text_metrics(&font, px, &m))
})
.flatten()
.unwrap_or(TextMetrics {
@@ -344,6 +350,49 @@ impl<'r> TextMeasurer<'r> {
}
}
+/// Vertical metrics the way Word computes a line's height: OS/2
+/// `usWinAscent`/`usWinDescent`, **no** line gap.
+///
+/// Word's auto line height is win-metric based; hhea's
+/// ascent/descent/lineGap (what skrifa's `metrics()` reports, and what CSS
+/// uses) runs taller on most fonts — Arial is 1.150 em vs 1.117 em. Half a
+/// point per 11 pt line sounds free until it compounds: ~40 lines a page is
+/// ~18 pt, one swallowed paragraph, and every page break after it lands one
+/// paragraph early (the 138.docx cascade — Word's own `lastRenderedPageBreak`
+/// markers disagree with the hhea-derived breaks). LibreOffice made the same
+/// choice for the same reason.
+///
+/// Falls back to skrifa's hhea-derived numbers when OS/2 or head is absent
+/// or degenerate (upem 0, zero win metrics) — bitmap-ish or broken faces —
+/// so a face that measured before still measures.
+fn word_text_metrics(
+ font: &skrifa::FontRef<'_>,
+ px: f32,
+ m: &skrifa::metrics::Metrics,
+) -> TextMetrics {
+ use skrifa::raw::TableProvider;
+ if let (Ok(os2), Ok(head)) = (font.os2(), font.head()) {
+ let upem = f32::from(head.units_per_em());
+ let win_ascent = f32::from(os2.us_win_ascent());
+ let win_descent = f32::from(os2.us_win_descent());
+ if upem > 0.0 && win_ascent > 0.0 {
+ let scale = px / upem;
+ return TextMetrics {
+ ascent: Pt::new(win_ascent * scale),
+ descent: Pt::new(win_descent * scale),
+ leading: Pt::ZERO,
+ };
+ }
+ }
+ TextMetrics {
+ ascent: Pt::new(m.ascent),
+ // skrifa reports descent negative-down; TextMetrics wants
+ // positive-down.
+ descent: Pt::new(-m.descent),
+ leading: Pt::new(m.leading.max(0.0)),
+ }
+}
+
#[cfg(test)]
mod tests {
use super::*;
diff --git a/crates/liteparse-ooxml/src/render/layout/section/layout.rs b/crates/liteparse-ooxml/src/render/layout/section/layout.rs
index ad09ca4..90c2a45 100644
--- a/crates/liteparse-ooxml/src/render/layout/section/layout.rs
+++ b/crates/liteparse-ooxml/src/render/layout/section/layout.rs
@@ -1515,6 +1515,18 @@ pub(crate) fn layout_section_with_clearance(
let collapse = state.prev_space_after.min(effective_style.space_before);
state.cursor_y -= collapse;
}
+ if std::env::var_os("LITEPARSE_TRACE_SPACING").is_some() {
+ eprintln!(
+ " para block={} style={:?} cursor={:.1} before={:.1} prev_after={:.1} after={:.1} line_spacing={:?}",
+ block_idx,
+ effective_style.style_id,
+ state.cursor_y.raw(),
+ effective_style.space_before.raw(),
+ state.prev_space_after.raw(),
+ effective_style.space_after.raw(),
+ effective_style.line_spacing,
+ );
+ }
// Register floating images (both relative and absolute).
// §20.4.2.18: wrapTopAndBottom images are emitted immediately
diff --git a/crates/liteparse-ooxml/src/render/raster.rs b/crates/liteparse-ooxml/src/render/raster.rs
index f6a2e1a..9bfd0e7 100644
--- a/crates/liteparse-ooxml/src/render/raster.rs
+++ b/crates/liteparse-ooxml/src/render/raster.rs
@@ -487,10 +487,20 @@ fn draw_text_run(
let outlines = font.outline_glyphs();
let location = wght_location(&font, entry.wght);
let metrics = font.glyph_metrics(Size::new(font_size.raw()), &location);
+ // Kerned per-char advances from the same shaped walk the measurer
+ // sums, so the pen stays inside the measured extent. Fallback: the
+ // unshaped cmap advance, matching the measurer's own fallback.
+ let kerned = crate::render::fonts::kerned_char_advances(
+ data,
+ index,
+ entry.wght,
+ font_size.raw(),
+ text,
+ );
let mut pb = PathBuilder::new();
let mut pen_x = position.x.raw();
- for ch in text.chars() {
+ for (i, ch) in text.chars().enumerate() {
let gid = charmap.map(ch).unwrap_or(skrifa::GlyphId::NOTDEF);
if let Some(glyph) = outlines.get(gid) {
let mut pen = GlyphPen {
@@ -504,7 +514,11 @@ fn draw_text_run(
// still moves the pen so the rest of the run stays aligned.
let _ = glyph.draw(settings, &mut pen);
}
- pen_x += metrics.advance_width(gid).unwrap_or(0.0) * text_scale + char_spacing.raw();
+ let advance = kerned
+ .as_ref()
+ .and_then(|a| a.get(i).copied())
+ .unwrap_or_else(|| metrics.advance_width(gid).unwrap_or(0.0));
+ pen_x += advance * text_scale + char_spacing.raw();
}
pb.finish()
});
diff --git a/crates/liteparse/src/office/docx.rs b/crates/liteparse/src/office/docx.rs
index 4d516f6..85da6c1 100644
--- a/crates/liteparse/src/office/docx.rs
+++ b/crates/liteparse/src/office/docx.rs
@@ -333,6 +333,19 @@ impl<'a> Emitter<'a> {
};
self.collect(para, &f.content, url.or(link), out);
}
+ // §17.16.7: a FORMCHECKBOX field renders as its ffData state
+ // (☐/☒) — it has no result-zone runs to pick up. Other field
+ // markers stay invisible: the instruction is InstrText (never
+ // a TextRun) and result runs flow through on their own.
+ Inline::FieldChar(fc) => {
+ if let Some(glyph) = fc.form_checkbox_glyph() {
+ out.push(Chunk {
+ fmt: Fmt::default(),
+ link: link.map(str::to_string),
+ text: glyph.to_string(),
+ });
+ }
+ }
Inline::Image(img) => {
// Pop the layout-assigned id for this media's next
// placement. The queue is keyed by media identity and