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`.
This commit is contained in:
Andy Ching
2026-11-16 15:51:31 +08:00
parent 489f3cbc12
commit 5033f10bf2
2 changed files with 31 additions and 3 deletions
+5 -3
View File
@@ -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}"));
+26
View File
@@ -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<i64> = 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]