From 046a7229afa01d3ceea44e490acfffcede255840 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Mon, 16 Nov 2026 08:16:40 -0500 Subject: [PATCH] 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> --- crates/docx/src/write/story/object.rs | 8 +++++-- crates/docx/tests/it/wps_fallback.rs | 30 +++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/crates/docx/src/write/story/object.rs b/crates/docx/src/write/story/object.rs index 820b85ee7..4b0ae856a 100644 --- a/crates/docx/src/write/story/object.rs +++ b/crates/docx/src/write/story/object.rs @@ -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")]); diff --git a/crates/docx/tests/it/wps_fallback.rs b/crates/docx/tests/it/wps_fallback.rs index e2d960cf9..9b209082b 100644 --- a/crates/docx/tests/it/wps_fallback.rs +++ b/crates/docx/tests/it/wps_fallback.rs @@ -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("").count(), 1, "{xml}"); + assert_eq!(xml.matches("").count(), 1, "{xml}"); + let fallback = xml.split("").nth(1).unwrap().split("").next().unwrap(); + assert!(fallback.contains(">Independent"), "{fallback}"); + assert!(!fallback.contains("Flows on"), "the chain head must not get an unlinked fallback"); + assert_eq!(xml.matches(">Flows on").count(), 1); + assert!(xml.contains(r#""#), "{xml}"); + assert!(xml.contains(r#""#), "{xml}"); + assert!(xml.contains(r#""#), "{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"); +}