diff --git a/crates/docx/src/write.rs b/crates/docx/src/write.rs index 634f5f833..0391ba615 100644 --- a/crates/docx/src/write.rs +++ b/crates/docx/src/write.rs @@ -120,6 +120,9 @@ pub(crate) struct Writer<'d> { /// Stored stacking orders written as other `relativeHeight`s (see [`stacking`]); empty: each /// as its own. zmap: BTreeMap, + /// The `relativeHeight` the last `wp:anchor` was written with, which a shape's VML fallback + /// gives as its `z-index`. + anchor_z: u32, /// Note part id → file note id, in first-reference order. footnotes: Vec, endnotes: Vec, diff --git a/crates/docx/src/write/parts.rs b/crates/docx/src/write/parts.rs index 00826f4b5..fa6d6b693 100644 --- a/crates/docx/src/write/parts.rs +++ b/crates/docx/src/write/parts.rs @@ -33,6 +33,7 @@ pub(crate) fn write_parts(doc: &Document, flavor: Flavor) -> Result { self.z = self.z.saturating_add(1); self.z }; + self.anchor_z = z; let behind = if float.wrap == Wrap::BehindText { "1" } else { "0" }; w.open( "wp:anchor", diff --git a/crates/docx/src/write/story/object.rs b/crates/docx/src/write/story/object.rs index 4b0ae856a..3a42a8b04 100644 --- a/crates/docx/src/write/story/object.rs +++ b/crates/docx/src/write/story/object.rs @@ -398,8 +398,9 @@ impl Writer<'_> { } let float = &float; self.drawing_open(w, DrawingInfo { float, size: (*sw, *sh), docpr: &docpr, name: &name, alt: o.alt_text(), link: link.as_deref() }); - // This shape's stacking order: floating objects in its text box move `self.z` on. - let z = self.z; + // This shape's stacking order, as its anchor gave it: floating objects in its text box + // write anchors of their own. + let z = self.anchor_z; w.empty("wp:cNvGraphicFramePr", &[]); w.open("a:graphic", &[]); w.open("a:graphicData", &[("uri", "http://schemas.microsoft.com/office/word/2010/wordprocessingShape")]); diff --git a/crates/docx/tests/it/wps_fallback.rs b/crates/docx/tests/it/wps_fallback.rs index 9b209082b..6d3deddb1 100644 --- a/crates/docx/tests/it/wps_fallback.rs +++ b/crates/docx/tests/it/wps_fallback.rs @@ -107,7 +107,8 @@ fn the_fallback_keeps_order_visibility_and_direction() { // is copied into the outer one's text, so it comes after it). let tb = xml.find(r#""#).expect("the outer box's text, turned"); let rect = &xml[xml[..tb].rfind("