diff --git a/crates/odf/src/text/frames.rs b/crates/odf/src/text/frames.rs index 86e11b73c..44a31357a 100644 --- a/crates/odf/src/text/frames.rs +++ b/crates/odf/src/text/frames.rs @@ -454,9 +454,36 @@ pub(crate) fn z_index(float: Option<&Float>, unset: u32) -> u32 { } /// The `draw:z-index` the objects of `doc` without a stored order take: past the highest stored -/// one (a stored order is one more than its `draw:z-index`), 0 when none has one. +/// one (a stored order is one more than its `draw:z-index`), 0 when none has one. A group's +/// members are written with their own orders too, so theirs count as well as the floating +/// objects' ([`craft_wordmodel::Document::stacking_orders`]). pub(crate) fn unset_z_index(doc: &craft_wordmodel::Document) -> u32 { - doc.stacking_orders().0.last().copied().unwrap_or(0) + use craft_wordmodel::para::{GroupChild, InlineObject}; + fn members(children: &[GroupChild], depth: usize, top: &mut u32) { + for c in children { + if let Some((_, _, f)) = c.obj.frame() { + *top = (*top).max(f.z); + } + if let InlineObject::Group { children, .. } = &c.obj + && depth < craft_flow::MAX_GROUP_DEPTH + { + members(children, depth + 1, top); + } + } + } + let mut top = doc.stacking_orders().0.last().copied().unwrap_or(0); + for blocks in std::iter::once(&doc.body).chain(doc.parts.values().map(|p| &p.blocks)) { + for b in blocks { + craft_wordmodel::walk::each_para(b, 0, &mut |p| { + for o in &p.objects { + if let InlineObject::Group { children, .. } = o { + members(children, 0, &mut top); + } + } + }); + } + } + top } /// How far a frame `h` points tall in line with the text is raised from standing on the diff --git a/crates/odf/src/text/tests_more/floats.rs b/crates/odf/src/text/tests_more/floats.rs index ab0c0bf26..0fff3797a 100644 --- a/crates/odf/src/text/tests_more/floats.rs +++ b/crates/odf/src/text/tests_more/floats.rs @@ -397,6 +397,30 @@ fn an_object_made_here_is_written_above_the_stored_orders() { assert_eq!(z, ["3", "4"], "{content}"); } +/// A group's members are written with their own stacking orders, so a member without one (made +/// in WordCraft) goes above a member's stored order too, not just above the floating objects'. +#[test] +fn an_object_made_here_is_written_above_the_group_members_stored_orders() { + let content = format!( + r#"Text beside."# + ); + let mut d = import(&zip_of(&[("content.xml", content.as_bytes())])).unwrap(); + // The group stored at the bottom, its first member stored above it, its second made here. + d.objects_mut(&|o| matches!(o, InlineObject::Group { .. }), &mut |o| { + let InlineObject::Group { float, children, .. } = o else { return }; + float.z = 1; + let wrap = float.wrap; + for (c, z) in children.iter_mut().zip([4, 0]) { + if let Some(f) = c.obj.float_mut() { + (f.wrap, f.z) = (wrap, z); + } + } + }); + let content = crate::text::tests::entry(&crate::text::tests::export_model(&d).unwrap(), "content.xml"); + let z: Vec<&str> = content.split(r#"draw:z-index=""#).skip(1).filter_map(|s| s.split('"').next()).collect(); + assert_eq!(z, ["0", "3", "4"], "the group's, the stored member's, the member made here above it: {content}"); +} + #[test] fn a_frame_anchored_to_a_page_stays_on_that_page() { // LibreOffice's page-anchored frames name their page. Written without it, a frame on page 3