From 5033f10bf2127693d2d56fda2d86ffb6d4b46758 Mon Sep 17 00:00:00 2001 From: Andy Ching <53651198+andyching168@users.noreply.github.com> Date: Mon, 16 Nov 2026 15:51:31 +0800 Subject: [PATCH] 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`. --- crates/docx/src/write/story/object.rs | 8 +++++--- crates/docx/tests/it/wps_fallback.rs | 26 ++++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 3 deletions(-) diff --git a/crates/docx/src/write/story/object.rs b/crates/docx/src/write/story/object.rs index 6b73aacd8..820b85ee7 100644 --- a/crates/docx/src/write/story/object.rs +++ b/crates/docx/src/write/story/object.rs @@ -442,9 +442,11 @@ impl Writer<'_> { (false, false) => {} } if float.wrap != Wrap::Inline { - // The stacking order the choice was given; behind the text is below it. - let z = i64::from(z); - style.push(format!("z-index:{}", if float.wrap == Wrap::BehindText { -z } else { z })); + // The stacking order the choice was given, as a signed 32-bit `z-index`. Behind the text + // is negative, shifted down rather than negated, so a higher `z` still stacks higher. + let z = i64::from(z.min(i32::MAX as u32)); + let z = if float.wrap == Wrap::BehindText { z - (i64::from(i32::MAX) + 1) } else { z }; + style.push(format!("z-index:{z}")); // The same clearances from the text as the choice's `distL`/`distR`/`distT`/`distB`. let d = pt(float.dist.clamp(0.0, 1584.0)); style.push(format!("mso-wrap-distance-left:{d}")); diff --git a/crates/docx/tests/it/wps_fallback.rs b/crates/docx/tests/it/wps_fallback.rs index c22a31b4e..e2d960cf9 100644 --- a/crates/docx/tests/it/wps_fallback.rs +++ b/crates/docx/tests/it/wps_fallback.rs @@ -111,6 +111,32 @@ fn the_fallback_keeps_order_visibility_and_direction() { assert!(rect.contains("visibility:hidden"), "{rect}"); } +/// Shapes behind the text keep their order in the fallback: a higher `z` stacks higher, below +/// the text (a negative `z-index` in signed 32-bit range). +#[test] +fn behind_text_shapes_keep_their_order() { + let mut d = Document::from_text("a"); + for z in [10, 20, u32::MAX] { + let mut o = shape(ShapeKind::Rectangle, None); + if let InlineObject::Shape { float, .. } = &mut o { + float.wrap = Wrap::BehindText; + float.z = z; + } + d.insert_object(&craft_wordmodel::Pos::body(0, 1), o, &Default::default()).unwrap(); + } + let xml = document_xml(&craft_docx::write(&d).unwrap()); + let mut got: Vec = xml + .match_indices("z-index:") + .map(|(i, m)| { + let v = &xml[i + m.len()..]; + v[..v.find([';', '"']).unwrap()].parse().unwrap() + }) + .collect(); + got.sort_unstable(); + let low = -(1i64 << 31); + assert_eq!(got, vec![low + 10, low + 20, -1], "{xml}"); +} + /// A shape VML has no simple form for stays a bare `wps:wsp`: guarded without a fallback, an old /// reader would open the file without it and drop it on its next save. #[test]