mirror of
https://github.com/storytold/deckcraft.git
synced 2026-11-17 07:34:35 +00:00
Merge PR #254 from xmaks82: Text: honour spcFirstLastPara (space before the first paragraph)
# Conflicts: # crates/pptx/src/read/dml.rs # crates/text/src/lib.rs # crates/text/src/tests.rs
This commit is contained in:
commit
126029db40
6 files changed
+31
-4
No files matched your search
@@ -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
|
||||
}
|
||||
|
||||
@@ -113,6 +113,8 @@ pub struct BodyProps {
|
||||
pub rtl_col: Option<bool>,
|
||||
/// Keep text upright when the shape rotates.
|
||||
pub upright: Option<bool>,
|
||||
/// Space before the first paragraph and after the last count too (`spcFirstLastPara`).
|
||||
pub space_first_last: Option<bool>,
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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#"<a:bodyPr spcFirstLastPara="1"/>"#).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#"<a:bodyPr vert="mongolianVert"/>"#).unwrap();
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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<ParaShaped> = 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).
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in new issue
Block a user