fix(docx): integrate VML fallbacks without breaking linked text-box stories (#1864)

* craft-docx: write Word 2010 shapes inside mc:AlternateContent with a VML fallback

A shape was written as a bare `wps:wsp` in `w:drawing`. Word 2007, and
Word 2000-2003 with the Office Compatibility Pack, don't know `wps` and
refuse the whole file. Word writes such shapes in `mc:AlternateContent`.

`write_shape` now guards the drawing with `mc:Choice Requires="wps"` and,
for rectangles, text boxes, rounded rectangles and ellipses, adds an
`mc:Fallback` with the shape as VML: size, position, z-order, wrapping,
fill, outline and the text box's text (copied from the choice's output,
so the story isn't written twice). Other kinds get no fallback; an old
reader skips that shape and still opens the file.

Refs storytold/wordcraft#6849

* craft-docx: guard a shape only when it gets a VML fallback; keep more in the fallback

A shape guarded by `mc:AlternateContent` with no fallback is skipped by
a reader that doesn't know `wps`, and dropped on that reader's next save:
worse than refusing the file. Such shapes are written bare again; only
shapes with a VML form are guarded.

The VML fallback also keeps the shape's own stacking order (a floating
object in its text box moved `self.z` on), its wrap distances, its hidden
flag and its text box's text direction.

* craft-docx: keep behind-text shapes' order in the VML fallback

A behind-text shape's fallback `z-index` was the negated `z`, which
reverses the order of overlapping behind-text shapes. It is now `z`
shifted below zero within signed 32-bit range, so a higher `z` still
stacks higher; other shapes' `z-index` is capped at `i32::MAX`.

* fix(docx): preserve linked text boxes without an incomplete VML fallback

---------

Co-authored-by: Andy Ching <53651198+andyching168@users.noreply.github.com>
This commit is contained in:
Dylan M. Taylor
2026-11-16 08:16:40 -05:00
committed by GitHub
co-authored by Andy Ching
parent dd345db898
commit 046a7229af
2 changed files with 36 additions and 2 deletions
+6 -2
View File
@@ -362,7 +362,7 @@ impl Writer<'_> {
}
fn write_shape(&mut self, w: &mut W, o: &InlineObject, props: &CharProps, rels: &mut PartRels, depth: usize) {
let InlineObject::Shape { kind, w: sw, h: sh, float, effects, freeform, extra, .. } = o else { return };
let InlineObject::Shape { kind, w: sw, h: sh, float, effects, freeform, extra, story, .. } = o else { return };
self.rev_open(w, props);
w.open("w:r", &[]);
self.rpr(w, props);
@@ -371,7 +371,11 @@ impl Writer<'_> {
// VML fallback instead of refusing the file. A shape without one stays bare: guarded with
// no fallback, that reader would open the file without the shape and drop it on its next
// save, which is worse than refusing the file.
let fallback = freeform.is_none() && vml_tag(*kind).is_some();
// VML currently has no chain IDs or mso-next-textbox links. Keep every member of a
// linked story bare, including its head: an unlinked fallback would silently lose
// overflow text in older readers. Unlinked boxes still get the compatibility fallback.
let linked = story.is_some_and(|id| self.txbx_links.contains_key(&id));
let fallback = freeform.is_none() && vml_tag(*kind).is_some() && !linked;
if fallback {
w.open("mc:AlternateContent", &[]);
w.open("mc:Choice", &[("Requires", "wps")]);
+30
View File
@@ -156,3 +156,33 @@ fn a_shape_without_a_vml_form_is_not_guarded() {
});
assert_eq!(kinds, vec![ShapeKind::Star]);
}
/// A partial fallback must not turn a linked story into independent boxes in older readers.
#[test]
fn linked_boxes_stay_bare_while_unlinked_boxes_keep_their_fallback() {
let mut d = Document::from_text("a\nb\nc\nd");
let mut ids = Vec::new();
for (i, text) in ["Flows on", "", "", "Independent"].into_iter().enumerate() {
let id = d.add_part(PartKind::TextBox, vec![para_block(craft_wordmodel::Paragraph::with_text(text, Default::default()))]);
ids.push(id);
d.insert_object(&craft_wordmodel::Pos::body(i, 1), shape(ShapeKind::TextBox, Some(id)), &Default::default()).unwrap();
}
d.parts.get_mut(&ids[0]).unwrap().body.next = Some(ids[1]);
d.parts.get_mut(&ids[1]).unwrap().body.next = Some(ids[2]);
let bytes = craft_docx::write(&d).unwrap();
let xml = document_xml(&bytes);
assert_eq!(xml.matches("<mc:AlternateContent>").count(), 1, "{xml}");
assert_eq!(xml.matches("<mc:Fallback>").count(), 1, "{xml}");
let fallback = xml.split("<mc:Fallback>").nth(1).unwrap().split("</mc:Fallback>").next().unwrap();
assert!(fallback.contains(">Independent</w:t>"), "{fallback}");
assert!(!fallback.contains("Flows on"), "the chain head must not get an unlinked fallback");
assert_eq!(xml.matches(">Flows on</w:t>").count(), 1);
assert!(xml.contains(r#"<wps:txbx id="1">"#), "{xml}");
assert!(xml.contains(r#"<wps:linkedTxbx id="1" seq="1"/>"#), "{xml}");
assert!(xml.contains(r#"<wps:linkedTxbx id="1" seq="2"/>"#), "{xml}");
let read = craft_docx::read(&bytes).unwrap();
let chains = read.text_box_chains();
assert_eq!(chains.len(), 1);
assert_eq!(chains[0].len(), 3);
assert_eq!(read.plain_text(craft_wordmodel::StoryRef::Part(chains[0][0])), "Flows on");
}