diff --git a/crates/model/src/resolve.rs b/crates/model/src/resolve.rs index a329f291a..a52e8a679 100644 --- a/crates/model/src/resolve.rs +++ b/crates/model/src/resolve.rs @@ -241,6 +241,7 @@ pub fn body(ctx: &Ctx, shape: &Shape) -> BodyProps { warp_adj: Vec::new(), 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 81ae282f0..44a0b7ac0 100644 --- a/crates/model/src/text.rs +++ b/crates/model/src/text.rs @@ -113,6 +113,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 { @@ -135,6 +137,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 c4e01c708..c8da00f21 100644 --- a/crates/pptx/src/read/dml.rs +++ b/crates/pptx/src/read/dml.rs @@ -320,6 +320,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() { @@ -633,6 +634,9 @@ 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)); // mongolianVert is its own direction (PowerPoint runs its lines left to right) and is // written back as it was read. let d = parse(br#""#).unwrap(); diff --git a/crates/pptx/src/write/dml.rs b/crates/pptx/src/write/dml.rs index f407a62ea..6e433391d 100644 --- a/crates/pptx/src/write/dml.rs +++ b/crates/pptx/src/write/dml.rs @@ -440,7 +440,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 5c965fa0d..b9e28fb1c 100644 --- a/crates/text/src/lib.rs +++ b/crates/text/src/lib.rs @@ -1640,6 +1640,7 @@ fn layout_scaled(ctx: &Ctx, shape: &Shape, body: &TextBody, bp: &BodyProps, opts let mut prev_after = 0.0; // How far the last line's full pitch reaches past the text (exact spacing; see below). let mut tail = 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; @@ -1683,7 +1684,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) }; + // 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) }; y += before.max(0.0) + prev_after; let first_line_idx = all_lines.len(); for (li, rl) in raw.iter().enumerate() { @@ -1867,8 +1869,9 @@ 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).max(0.0); } - // The text ends at the last line's descent under exact spacing. - let total = (y - tail).max(0.0); + // The text ends at the last line's descent under exact spacing; the last paragraph's space + // after counts only when the body says so. + let total = (y - tail).max(0.0) + if first_last { prev_after } else { 0.0 }; out.content_height = total; // Where column `c` starts: from the left, or from the right with `rtlCol` (PowerPoint puts the // first column on the right). diff --git a/crates/text/src/tests.rs b/crates/text/src/tests.rs index 05608478c..452e46b54 100644 --- a/crates/text/src/tests.rs +++ b/crates/text/src/tests.rs @@ -280,6 +280,21 @@ fn shrink_gives_up_at_once_when_even_the_smallest_scale_overflows() { assert!(l.font_scale > 0.11 && l.font_scale < 1.0 && !l.overflow, "scale {} overflow {}", l.font_scale, l.overflow); } +#[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);