From bac18cb2eb4c5f031a41d45f7535278241d3a230 Mon Sep 17 00:00:00 2001 From: Eric Kaden <608341+EricKaden@users.noreply.github.com> Date: Mon, 19 Oct 2026 14:33:24 -0400 Subject: [PATCH] M12: a negative XFA text indent hangs the first line A negative textIndent was clamped to 0, so a hanging indent was lost and every line started at the left margin. The first line now starts left of the others by the indent, never left of the box: with no left margin the other lines move in to make room, as other viewers lay DA Form 31's '10a. ACCRUED / LEAVE / (CR BAL)' out. --- crates/xfa/src/tests.rs | 42 +++++++++++++++++++++++++++++++++++++++++ crates/xfa/src/text.rs | 14 ++++++++++---- 2 files changed, 52 insertions(+), 4 deletions(-) diff --git a/crates/xfa/src/tests.rs b/crates/xfa/src/tests.rs index 03eb2c67ad..245b988927 100644 --- a/crates/xfa/src/tests.rs +++ b/crates/xfa/src/tests.rs @@ -139,6 +139,48 @@ fn layout_places_captions_fields_and_sizes_containers_to_their_content() { ); } +/// The left edges of the lines of a draw `w` points wide holding `text` in Arial 8 pt, with +/// `para` attributes, from the content area's left edge (0.5in). +fn line_starts(text: &str, w: f64, para: &str) -> Vec { + let xdp = format!( + r##""## + ); + let form = layout_xml(&xdp).unwrap(); + let mut lines: Vec<(i64, f64)> = Vec::new(); + for item in &form.pages[0].items { + let Item::Text(s) = item else { continue }; + let key = (s.baseline * 100.0).round() as i64; + match lines.iter_mut().find(|(b, _)| *b == key) { + Some((_, x)) => *x = x.min(s.x), + None => lines.push((key, s.x)), + } + } + lines.sort_by_key(|(b, _)| *b); + lines.into_iter().map(|(_, x)| x - 36.0).collect() +} + +#[test] +fn a_negative_text_indent_hangs_the_first_line_and_indents_the_rest() { + let text = "11. SERVICE MEMBER BALANCE (TOTAL)"; + // No left margin: the first line starts at the box's edge and the others 18pt in. + let starts = line_starts(text, 58.0, r#"textIndent="-18pt""#); + assert!(starts.len() >= 3, "{starts:?}"); + assert!(starts[0].abs() < 1e-6, "first line at the edge: {starts:?}"); + assert!(starts[1..].iter().all(|x| (x - 18.0).abs() < 1e-6), "the rest hang 18pt in: {starts:?}"); + // With a left margin wider than the hang, the first line starts at the margin less the hang. + let starts = line_starts(text, 90.0, r#"marginLeft="30pt" textIndent="-18pt""#); + assert!(starts.len() >= 2, "{starts:?}"); + assert!((starts[0] - 12.0).abs() < 1e-6 && starts[1..].iter().all(|x| (x - 30.0).abs() < 1e-6), "{starts:?}"); + // A positive indent still moves only the first line in. + let starts = line_starts(text, 58.0, r#"textIndent="10pt""#); + assert!((starts[0] - 10.0).abs() < 1e-6 && starts[1..].iter().all(|x| x.abs() < 1e-6), "{starts:?}"); +} + #[test] fn tables_break_across_pages_and_repeat_their_header_row() { let form = layout_xml(&template(40)).unwrap(); diff --git a/crates/xfa/src/text.rs b/crates/xfa/src/text.rs index 6db0777c06..1b0a0b5492 100644 --- a/crates/xfa/src/text.rs +++ b/crates/xfa/src/text.rs @@ -188,14 +188,20 @@ pub fn layout(rich: &RichText, rect: Rect, para: &Para, base: &Face, embed: &dyn size: f64, first: bool, } - let avail = (rect.w - para.margin_left - para.margin_right).max(1.0); + // A negative textIndent is a hanging indent: the first line starts left of the others. It + // never starts left of the box, so the others move in to make room, as other viewers lay + // such labels out. + let margin_left = para.margin_left.max(-para.text_indent); + // Where the first line starts, from the others. + let first_indent = para.text_indent.max(-margin_left); + let avail = (rect.w - margin_left - para.margin_right).max(1.0); let mut lines: Vec<(Line, Option)> = Vec::new(); let mut total = 0.0; for (pi, p) in rich.paragraphs.iter().enumerate() { let ws = words(&p.runs, base, embed); let mut line = Line { words: Vec::new(), width: 0.0, size: base.size, first: true }; let mut first_in_para = true; - let indent = |first: bool| if first { para.text_indent.max(0.0) } else { 0.0 }; + let indent = |first: bool| if first { first_indent } else { 0.0 }; for w in ws { let fits = line.width + w.width <= avail - indent(line.first) || line.words.is_empty() && w.width <= avail - indent(line.first); if !fits && !line.words.is_empty() { @@ -247,11 +253,11 @@ pub fn layout(rich: &RichText, rect: Rect, para: &Para, base: &Face, embed: &dyn let mut spans = Vec::new(); let mut y = start_y + para.space_above; for ((line, align), lh) in lines.into_iter().zip(heights) { - let indent = if line.first { para.text_indent.max(0.0) } else { 0.0 }; + let indent = if line.first { first_indent } else { 0.0 }; // Trailing spaces don't count for alignment. let trailing = line.words.last().map_or(0.0, |w| if w.text.ends_with(' ') { w.face.width(" ") } else { 0.0 }); let used = (line.width - trailing).max(0.0); - let mut x = rect.x + para.margin_left + indent; + let mut x = rect.x + margin_left + indent; x += match align.unwrap_or(para.h_align) { HAlign::Left | HAlign::Justify => 0.0, HAlign::Center => ((avail - indent - used) / 2.0).max(0.0),