From bb53f8c74177cba181651f6b0e8b5e1f56c34603 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=B0=D0=B3=D0=B4=D0=B0=20=D0=9C=2E=D0=93=2E?= Date: Mon, 12 Oct 2026 17:09:14 +0300 Subject: [PATCH] Text: honour spcFirstLastPara (space before the first paragraph) bodyPr spcFirstLastPara="1" makes the first paragraph's space before and the last paragraph's space after count, which PowerPoint (and Google Slides exports) use; we always dropped them, so such text sat a paragraph gap too high (9.5 pt on a real deck measured against PowerPoint's PDF). Read, written, inherited from the layout like the other body properties, and applied in the layout. --- crates/model/src/resolve.rs | 1 + crates/model/src/text.rs | 3 +++ crates/pptx/src/read/dml.rs | 4 ++++ crates/pptx/src/write/dml.rs | 3 ++- crates/text/src/lib.rs | 7 +++++-- crates/text/src/tests.rs | 15 +++++++++++++++ 6 files changed, 30 insertions(+), 3 deletions(-) diff --git a/crates/model/src/resolve.rs b/crates/model/src/resolve.rs index 8769b571f..2c269251f 100644 --- a/crates/model/src/resolve.rs +++ b/crates/model/src/resolve.rs @@ -221,6 +221,7 @@ pub fn body(ctx: &Ctx, shape: &Shape) -> BodyProps { warp: None, rtl_col: Some(false), upright: Some(false), + space_first_last: Some(false), }); b } diff --git a/crates/model/src/text.rs b/crates/model/src/text.rs index addbdff11..dc268f102 100644 --- a/crates/model/src/text.rs +++ b/crates/model/src/text.rs @@ -101,6 +101,8 @@ pub struct BodyProps { pub rtl_col: Option, /// Keep text upright when the shape rotates. pub upright: Option, + /// Space before the first paragraph and after the last count too (`spcFirstLastPara`). + pub space_first_last: Option, } impl BodyProps { @@ -122,6 +124,7 @@ impl BodyProps { } self.rtl_col = self.rtl_col.or(p.rtl_col); self.upright = self.upright.or(p.upright); + self.space_first_last = self.space_first_last.or(p.space_first_last); } } diff --git a/crates/pptx/src/read/dml.rs b/crates/pptx/src/read/dml.rs index b4f08e0dd..a1e4950a0 100644 --- a/crates/pptx/src/read/dml.rs +++ b/crates/pptx/src/read/dml.rs @@ -303,6 +303,7 @@ pub fn body_pr(b: &El) -> BodyProps { col_spacing: pt(b, "spcCol"), rtl_col: b.bool("rtlCol"), upright: b.bool("upright"), + space_first_last: b.bool("spcFirstLastPara"), ..Default::default() }; for e in b.elements() { @@ -568,5 +569,8 @@ mod tests { assert_eq!(b.vert, Some(TextDir::Vertical270)); assert_eq!(b.columns, Some(2)); assert_eq!(b.autofit, Some(AutoFit::Shrink { font_scale: 0.625, line_reduction: 0.2 })); + assert_eq!(b.space_first_last, None); + let d = parse(br#""#).unwrap(); + assert_eq!(body_pr(&d.root).space_first_last, Some(true)); } } diff --git a/crates/pptx/src/write/dml.rs b/crates/pptx/src/write/dml.rs index eddb618e3..e0234b6ce 100644 --- a/crates/pptx/src/write/dml.rs +++ b/crates/pptx/src/write/dml.rs @@ -383,7 +383,8 @@ pub fn body_pr(w: &mut W, b: &BodyProps) { .b("rtlCol", b.rtl_col) .o("anchor", b.anchor.map(|a| a.xml())) .b("anchorCtr", b.anchor_ctr) - .b("upright", b.upright); + .b("upright", b.upright) + .b("spcFirstLastPara", b.space_first_last); let warp = b.warp.as_deref().filter(|s| !s.is_empty() && s.chars().all(|c| c.is_ascii_alphanumeric())); if warp.is_none() && b.autofit.is_none() { w.empty("a:bodyPr", a); diff --git a/crates/text/src/lib.rs b/crates/text/src/lib.rs index 225f4afb9..09e337274 100644 --- a/crates/text/src/lib.rs +++ b/crates/text/src/lib.rs @@ -879,6 +879,7 @@ fn layout_scaled(ctx: &Ctx, shape: &Shape, body: &TextBody, bp: &BodyProps, opts let mut numbering: Vec = vec![0; 9]; let mut all_lines: Vec<(LineInfo, LinePlace, f64, Option<(String, f64, f64)>)> = vec![]; let mut prev_after = 0.0; + let first_last = bp.space_first_last.unwrap_or(false); let mut shaped_paras: Vec = Vec::with_capacity(body.paragraphs.len()); for (pi, para) in body.paragraphs.iter().enumerate() { let lvl = para.level.min(8) as usize; @@ -918,7 +919,8 @@ fn layout_scaled(ctx: &Ctx, shape: &Shape, body: &TextBody, bp: &BodyProps, opts let first_w = (col_w - first_x - mr).max(1.0); let rest_w = (col_w - rest_x - mr).max(1.0); let raw = break_lines(p, first_w, rest_w, wrap, &tabs, props.default_tab.unwrap_or(72.0), first_x, rest_x); - let before = if pi == 0 { 0.0 } else { spacing_pts(props.space_before, p.lead_size, reduce) }; + // The first paragraph's space before counts only when the body says so. + let before = if pi == 0 && !first_last { 0.0 } else { spacing_pts(props.space_before, p.lead_size, reduce) }; y += before.max(0.0) + prev_after; let first_line_idx = all_lines.len(); for (li, rl) in raw.iter().enumerate() { @@ -1084,7 +1086,8 @@ fn layout_scaled(ctx: &Ctx, shape: &Shape, body: &TextBody, bp: &BodyProps, opts out.para_lines.push((first_line_idx, raw.len())); prev_after = spacing_pts(props.space_after, p.lead_size, reduce).max(0.0); } - let total = y; + // So does the last paragraph's space after. + let total = y + if first_last { prev_after } else { 0.0 }; out.content_height = total; // Columns: distribute lines by height. let col_h = if ncols > 1 { inner.height() } else { f64::INFINITY }; diff --git a/crates/text/src/tests.rs b/crates/text/src/tests.rs index f3d5db5d8..e0be4cb23 100644 --- a/crates/text/src/tests.rs +++ b/crates/text/src/tests.rs @@ -54,6 +54,21 @@ fn shrink_on_overflow() { assert!(!l.overflow || l.font_scale <= 0.11); } +#[test] +fn space_before_the_first_paragraph_with_spc_first_last_para() { + // 10 pt before every paragraph: skipped on the first one, unless the body asks for it. + let (p, mut sh) = setup("One\nTwo", 600.0, 300.0); + for para in &mut sh.text.as_mut().unwrap().paragraphs { + para.props.space_before = Some(Spacing::Pts(10.0)); + para.props.space_after = Some(Spacing::Pts(6.0)); + } + let plain = lay(&p, &sh, 600.0, 300.0); + sh.text.as_mut().unwrap().body.space_first_last = Some(true); + let both = lay(&p, &sh, 600.0, 300.0); + assert!((both.lines[0].top - plain.lines[0].top - 10.0).abs() < 1e-6); + assert!((both.content_height - plain.content_height - 16.0).abs() < 1e-6); +} + #[test] fn caret_and_hit_roundtrip() { let (p, sh) = setup("Hello world", 600.0, 100.0);