From abaccb0d386c81c31edbab89b4933fdec31eaf94 Mon Sep 17 00:00:00 2001 From: mmazzieri Date: Thu, 19 Nov 2026 22:33:53 +0100 Subject: [PATCH] craft-docx: a VML shape in a body table cell keeps Layout in table cell off `o:allowincell="f"` on a VML shape anchored in a body table cell now sets `Float::outside_cell`, as `layoutInCell="0"` already does for DrawingML. Word places such a shape outside its cell, and the cell's text does not wrap around it. Without the attribute, or with it on, the shape stays in its cell. The writer already saved `o:allowincell` from `outside_cell`, so the setting now survives open and save. --- crates/docx/src/read/story/vml.rs | 7 +++++-- crates/docx/tests/it/fixtures.rs | 33 +++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 2 deletions(-) diff --git a/crates/docx/src/read/story/vml.rs b/crates/docx/src/read/story/vml.rs index 51add2d6ad..2460be72b2 100644 --- a/crates/docx/src/read/story/vml.rs +++ b/crates/docx/src/read/story/vml.rs @@ -616,8 +616,11 @@ fn vml_float(shape: &El, style: &str, in_hf: bool) -> Float { dist_bottom: dist("mso-wrap-distance-bottom"), hidden, // Word for Mac draws a VML shape anchored in a header table cell on the page whatever - // `o:allowincell` says (user-checked), as for Word's picture and text watermarks. - outside_cell: in_hf, + // `o:allowincell` says (user-checked), as for Word's picture and text watermarks. In the + // body, Layout in table cell off (`o:allowincell="f"`) places the shape outside its cell, + // and the cell's text doesn't wrap around it (user-checked in Word for Windows); without + // the attribute the shape is laid out in its cell. + outside_cell: in_hf || shape.attr("o:allowincell").is_some_and(|v| matches!(v.trim(), "f" | "false" | "0")), ..Float::default() }; f.set_spin(vml_spin(style)); diff --git a/crates/docx/tests/it/fixtures.rs b/crates/docx/tests/it/fixtures.rs index 1e472fa8a0..926ff1ac7d 100644 --- a/crates/docx/tests/it/fixtures.rs +++ b/crates/docx/tests/it/fixtures.rs @@ -1641,6 +1641,39 @@ fn body_cell_vml_picture_stays_in_its_cell() { assert_eq!(floats, vec![false]); } +/// A VML shape in a body table cell with Layout in table cell off (`o:allowincell="f"`, which +/// Word writes for such a shape in a document in Word 2003 compatibility mode, user-checked) is +/// placed outside its cell, as Word draws it: beside the table, and the cell's text doesn't wrap +/// around it. Without the attribute, or with it on, the shape stays laid out in its cell. Both +/// survive save and reopen. +#[test] +fn body_cell_vml_shape_with_layout_in_cell_off_is_placed_outside_the_cell() { + let shape = |allow: &str| { + format!( + r##"ICON"## + ) + }; + let floats = |d: &Document| -> Vec<(bool, Wrap)> { + let Block::Table(t) = &*d.body[0] else { panic!("table first") }; + t.rows[0].cells[0] + .blocks + .iter() + .filter_map(|b| b.as_para()) + .flat_map(|p| p.objects.iter().filter_map(|o| o.frame()).map(|(_, _, f)| (f.outside_cell, f.wrap))) + .collect() + }; + for (allow, outside) in [(r#"o:allowincell="f""#, true), (r#"o:allowincell="false""#, true), (r#"o:allowincell="t""#, false), ("", false)] { + let run = shape(allow); + let body = format!( + r#"{run}Box titleBox text"# + ); + let d = craft_docx::read(&docx(&body, &[], &[])).unwrap(); + assert_eq!(floats(&d), vec![(outside, Wrap::TopAndBottom)], "{allow}"); + let d2 = craft_docx::read(&craft_docx::write(&d).unwrap()).unwrap(); + assert_eq!(floats(&d2), vec![(outside, Wrap::TopAndBottom)], "saved: {allow}"); + } +} + /// Issue #149: an absolutely positioned VML text box in a header floats where its style puts it /// (relative to the page here, behind the text at a negative z-index, no wrapping) instead of /// sitting inline in the header's flow, and it keeps that placement when saved and read back.