craft-docx: a locked canvas's paragraph falls back on its level's alignment and the list style's defaults; bold or italic set off stays off

This commit is contained in:
Diego Guerini committed 2026-11-16 08:52:44 +01:00
1 parent 6fde933469
commit ce8ab5cc6d
2 files changed
+43 -12

No files matched your search

+23 -12
View File
@@ -102,17 +102,22 @@ fn picture(pic: &El) -> El {
fn paragraphs(body: &El, theme: &[Rgb]) -> Vec<Node> {
body.children("a:p")
.map(|p| {
// A run's properties default to its paragraph's own (`a:pPr/a:defRPr`), then to the
// list style's for the paragraph's level (`lvl`, 0–8), property by property.
// A paragraph's properties default to the list style's for its level (`lvl`, 0–8),
// then to the list style's defaults (`a:defPPr`), property by property; a run's to its
// paragraph's own (`a:pPr/a:defRPr`), then to those two's.
let ppr_el = p.child("a:pPr");
let level = ppr_el.and_then(|pp| pp.attr("lvl")).and_then(|v| v.trim().parse::<u8>().ok()).filter(|l| *l <= 8).unwrap_or(0);
let level_style = body.child("a:lstStyle").and_then(|l| l.child(&format!("a:lvl{}pPr", level + 1)));
let defaults: Vec<&El> =
[ppr_el.and_then(|pp| pp.child("a:defRPr")), level_style.and_then(|l| l.child("a:defRPr"))].into_iter().flatten().collect();
let list_style = body.child("a:lstStyle");
let paragraph_styles: Vec<&El> =
[ppr_el, list_style.and_then(|l| l.child(&format!("a:lvl{}pPr", level + 1))), list_style.and_then(|l| l.child("a:defPPr"))]
.into_iter()
.flatten()
.collect();
let defaults: Vec<&El> = paragraph_styles.iter().filter_map(|pp| pp.child("a:defRPr")).collect();
let defaults = defaults.as_slice();
let mut ppr =
vec![Node::El(el("w:spacing", attrs(&[("w:before", "0"), ("w:after", "0"), ("w:line", "240"), ("w:lineRule", "auto")]), Vec::new()))];
let jc = match ppr_el.and_then(|pp| pp.attr("algn")) {
let jc = match paragraph_styles.iter().find_map(|pp| pp.attr("algn")) {
Some("ctr") => Some("center"),
Some("r") => Some("right"),
Some("just") => Some("both"),
@@ -151,7 +156,12 @@ fn paragraphs(body: &El, theme: &[Rgb]) -> Vec<Node> {
fn run_props(rpr: Option<&El>, defaults: &[&El], theme: &[Rgb]) -> El {
let get = |n: &str| rpr.and_then(|r| r.attr(n)).or_else(|| defaults.iter().find_map(|d| d.attr(n)));
let child = |n: &str| rpr.and_then(|r| r.child(n)).or_else(|| defaults.iter().find_map(|d| d.child(n)));
let on = |n: &str| get(n).is_some_and(|v| v == "1" || v == "true");
// On, off (kept, so the document's style doesn't turn it back on), or not given.
let toggle = |n: &str| match get(n) {
Some("1" | "true") => Some(true),
Some("0" | "false") => Some(false),
_ => None,
};
let mut kids = Vec::new();
let mut push = |name: &str, a: &[(&str, &str)]| kids.push(Node::El(el(name, attrs(a), Vec::new())));
// Theme fonts (`+mn-lt`) are left to the document's.
@@ -169,11 +179,12 @@ fn run_props(rpr: Option<&El>, defaults: &[&El], theme: &[Rgb]) -> El {
if !fonts.is_empty() {
push("w:rFonts", &fonts);
}
if on("b") {
push("w:b", &[]);
}
if on("i") {
push("w:i", &[]);
for (drawingml, word) in [("b", "w:b"), ("i", "w:i")] {
match toggle(drawingml) {
Some(true) => push(word, &[]),
Some(false) => push(word, &[("w:val", "0")]),
None => {}
}
}
match get("cap") {
Some("all") => push("w:caps", &[]),
+20
View File
@@ -122,6 +122,26 @@ fn a_locked_canvas_text_shape_and_paragraph_defaults() {
}
}
/// A paragraph's alignment and its runs' properties fall back on its level's style, then on the
/// list style's defaults (`a:defPPr`); a run's bold set off stays off.
#[test]
fn a_locked_canvas_paragraph_takes_its_level_and_the_list_defaults() {
use craft_wordmodel::props::Align;
let text = r#"<a:txSp><a:txBody><a:bodyPr/><a:lstStyle><a:defPPr algn="r"><a:defRPr sz="1600" b="1"/></a:defPPr><a:lvl2pPr algn="ctr"/></a:lstStyle><a:p><a:r><a:rPr lang="en-US" b="0"/><a:t>Plain</a:t></a:r></a:p><a:p><a:pPr lvl="1"/><a:r><a:rPr lang="en-US"/><a:t>Centred</a:t></a:r></a:p><a:p><a:pPr lvl="1" algn="l"/><a:r><a:rPr lang="en-US"/><a:t>Own</a:t></a:r></a:p></a:txBody><a:useSpRect/><a:xfrm><a:off x="635000" y="381000"/><a:ext cx="1270000" cy="508000"/></a:xfrm></a:txSp>"#;
let d = craft_docx::read(&docx(&drawing(text))).unwrap();
for d in [&d, &craft_docx::read(&craft_docx::write(&d).unwrap()).unwrap()] {
let (_, _, children) = group(d);
let [shape] = &children[..] else { panic!("{children:?}") };
let InlineObject::Shape { story: Some(id), .. } = &shape.obj else { panic!("{:?}", shape.obj) };
let blocks = &d.parts.get(id).unwrap().blocks;
let para = |i: usize| blocks[i].as_para().unwrap();
let aligns: Vec<Option<Align>> = (0..3).map(|i| para(i).props.align).collect();
assert_eq!(aligns, [Some(Align::Right), Some(Align::Center), Some(Align::Left)], "the list defaults', the level's, its own");
let runs: Vec<(Option<f32>, Option<bool>)> = (0..3).map(|i| (para(i).runs[0].props.size, para(i).runs[0].props.bold)).collect();
assert_eq!(runs, [(Some(16.0), Some(false)), (Some(16.0), Some(true)), (Some(16.0), Some(true))]);
}
}
/// Before wordcraft#5060 only the canvas's first picture showed: the shapes and their text were dropped.
#[test]
fn a_locked_canvas_is_not_read_as_its_picture_alone() {