Preserve retained ODF markup while normalizing stacking orders

This commit is contained in:
FredApps
2026-11-16 20:01:11 +01:00
parent c2068fc90b
commit 98f50bc151
7 changed files with 214 additions and 60 deletions
-21
View File
@@ -35,7 +35,6 @@ type Frame =
(Option<f32>, Option<f32>, Option<Vec<u8>>, String, Option<(String, bool)>, Option<craft_wordmodel::chart::ChartSpec>, Option<model::OdfObject>);
pub(super) struct Body<'a> {
pub(super) file_orders: craft_wordmodel::para::FileOrders,
styles: &'a Styles,
zip: &'a mut dyn FnMut(&str) -> Option<Vec<u8>>,
/// The package's entry names and its manifest's media types, for embedded objects.
@@ -220,7 +219,6 @@ impl<'a> Body<'a> {
pub(super) fn new(styles: &'a Styles, pages: &'a Pages, zip: &'a mut dyn FnMut(&str) -> Option<Vec<u8>>) -> Self {
let page = pages.first.clone().unwrap_or_default();
Body {
file_orders: Default::default(),
styles,
zip,
entries: &[],
@@ -1166,25 +1164,6 @@ impl Body<'_> {
}
fn start(&mut self, q: &str, a: &Attrs) {
// Keep all existing attribute readers unchanged. Only the occupied upper tail
// is adjusted, before any frame/group/note reader can saturate its one-more z.
let adjusted = a
.iter()
.enumerate()
.find(|(_, (name, _))| name == "draw:z-index")
.and_then(|(i, (_, value))| value.trim().parse::<u32>().ok().map(|raw| (i, raw)))
.and_then(|(i, raw)| {
let mapped = self.file_orders.file_order(raw);
(mapped != raw).then_some((i, mapped))
});
let mut normalized;
let a = if let Some((i, mapped)) = adjusted {
normalized = a.clone();
normalized[i].1 = mapped.to_string();
&normalized
} else {
a
};
self.depth += 1;
if q == "office:text" {
self.in_text = true;
+10 -29
View File
@@ -443,18 +443,17 @@ pub(super) fn z_of(a: &Attrs) -> u32 {
get(a, "draw:z-index").and_then(|v| v.trim().parse::<u32>().ok()).map_or(0, |z| z.saturating_add(1))
}
/// Export orders leave room for the one-more representation used on import.
/// At the upper boundary, remove an unused model order rather than saturating.
/// Raw export orders. The package writer normalizes their upper boundary once,
/// after adding all retained markup, leaving room for one-more model orders.
#[derive(Clone, Copy, Default)]
pub(crate) struct Stacking {
unset: u32,
gap: Option<u32>,
}
/// A stored order stays below every unset order; equal stored orders remain equal.
pub(crate) fn z_index(float: Option<&Float>, stacking: Stacking) -> u32 {
match float.map(|f| f.z) {
Some(z) if z > 0 => z - 1 - u32::from(stacking.gap.is_some_and(|gap| z > gap)),
Some(z) if z > 0 => z - 1,
_ => stacking.unset,
}
}
@@ -463,25 +462,21 @@ pub(crate) fn z_index(float: Option<&Float>, stacking: Stacking) -> u32 {
pub(crate) fn stacking(doc: &craft_wordmodel::Document) -> Result<Stacking, String> {
use craft_wordmodel::para::{GroupChild, InlineObject};
use std::collections::BTreeSet;
fn members(children: &[GroupChild], depth: usize, orders: &mut BTreeSet<u32>, unset: &mut bool) {
fn members(children: &[GroupChild], depth: usize, orders: &mut BTreeSet<u32>) {
for c in children {
if let Some((_, _, f)) = c.obj.frame()
&& f.z > 0
{
orders.insert(f.z);
}
if let Some((_, _, f)) = c.obj.frame() {
*unset |= f.z == 0;
}
if let InlineObject::Group { children, .. } = &c.obj
&& depth < craft_flow::MAX_GROUP_DEPTH
{
members(children, depth + 1, orders, unset);
members(children, depth + 1, orders);
}
}
}
let (mut orders, unset_count) = doc.stacking_orders();
let mut unset = unset_count > 0;
let (mut orders, _) = doc.stacking_orders();
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| {
@@ -491,30 +486,16 @@ pub(crate) fn stacking(doc: &craft_wordmodel::Document) -> Result<Stacking, Stri
{
orders.insert(f.z);
}
if let Some((_, _, f)) = o.frame() {
unset |= f.z == 0;
}
if let InlineObject::Group { children, .. } = o {
members(children, 0, &mut orders, &mut unset);
members(children, 0, &mut orders);
}
}
});
}
}
let top = orders.last().copied().unwrap_or(0);
if top < u32::MAX || !unset {
return Ok(Stacking { unset: top, gap: None });
}
// Find a missing positive value in linear time in the number of distinct orders,
// not the numeric range. This needs no second map and preserves relative order.
let mut gap = 1u32;
for z in orders {
if z != gap {
break;
}
gap = gap.checked_add(1).ok_or("too many distinct ODF stacking orders")?;
}
Ok(Stacking { unset: top - 1, gap: Some(gap) })
// Final content/styles normalization includes retained XML, not just modeled frames.
// Keep the distinct raw MAX slot here; the package writer removes an unused slot once.
Ok(Stacking { unset: orders.last().copied().unwrap_or(0) })
}
/// How far a frame `h` points tall in line with the text is raised from standing on the
+8 -5
View File
@@ -876,6 +876,11 @@ fn parse_tracked(bytes: &[u8], paper: (f32, f32)) -> Result<Parsed, String> {
None => return Err("ODT: content.xml is missing".into()),
};
let file_orders = super::frames::read_orders(&content, styles_xml.as_deref())?;
// Normalize source values before both model parsing and retained-markup capture.
// Rewriting values only keeps unknown XML and the model on the same order scale.
let content = super::xml::remap_orders(&content, |raw| file_orders.file_order(raw))?;
let styles_normalized = styles_xml.as_deref().map(|xml| super::xml::remap_orders(xml, |raw| file_orders.file_order(raw))).transpose()?;
let styles_xml = styles_normalized;
let mut styles = Styles::default();
let mut pages = Pages::default();
if let Some(s) = &styles_xml {
@@ -917,13 +922,11 @@ fn parse_tracked(bytes: &[u8], paper: (f32, f32)) -> Result<Parsed, String> {
let objects = (entries.as_slice(), &manifest);
// One `text:s` budget for the headers and footers and then the body.
let mut spaces_left = MAX_EXTRA_SPACES;
let mut parts = styles_xml
.as_deref()
.map(|x| page_parts::read(x, &styles, &mut pages, &mut fetch, objects, &mut spaces_left, file_orders))
.unwrap_or_default();
let mut parts =
styles_xml.as_deref().map(|x| page_parts::read(x, &styles, &mut pages, &mut fetch, objects, &mut spaces_left)).unwrap_or_default();
first_pages(&mut pages);
let mut body = Body::new(&styles, &pages, &mut fetch);
body.file_orders = file_orders;
(body.entries, body.manifest) = (objects.0, Some(objects.1));
body.raw = &content;
body.spaces_left = spaces_left;
+8 -2
View File
@@ -1257,10 +1257,16 @@ fn export_as(doc: &Document, mime: &str) -> Result<Vec<u8>, String> {
None => styles,
};
let (content, styles) = with_kept_defs(content, styles, &w.kept_used, &w.kept_defs);
// Choose the gap from everything actually written, including unknown retained
// drawings and master pages. Remapping modeled frames alone can alias an order
// present only in retained XML. Values are spliced without reserializing XML.
let orders = super::frames::read_orders(content.as_bytes(), Some(styles.as_bytes()))?;
let content = super::xml::remap_orders(content.as_bytes(), |raw| orders.file_order(raw))?;
let styles = super::xml::remap_orders(styles.as_bytes(), |raw| orders.file_order(raw))?;
// The manifest lists every file already: the writer adds the files only.
for (name, data) in [
("content.xml", content.as_bytes()),
("styles.xml", styles.as_bytes()),
("content.xml", content.as_ref()),
("styles.xml", styles.as_ref()),
("meta.xml", meta.as_bytes()),
("settings.xml", settings_xml(doc).as_bytes()),
] {
-2
View File
@@ -78,7 +78,6 @@ pub(super) fn read(
fetch: &mut dyn FnMut(&str) -> Option<Vec<u8>>,
objects: (&[String], &std::collections::HashMap<String, String>),
spaces_left: &mut usize,
file_orders: craft_wordmodel::para::FileOrders,
) -> Parts {
let mut found: Vec<(String, Slot, Vec<Ev>)> = Vec::new();
let mut master: Option<String> = None;
@@ -128,7 +127,6 @@ pub(super) fn read(
let mut out = Parts::default();
for (name, kind, evs) in found {
let mut reader = Body::new(styles, &none, fetch);
reader.file_orders = file_orders;
reader.in_text = true;
(reader.entries, reader.manifest) = (objects.0, Some(objects.1));
reader.spaces_left = *spaces_left;
+50 -1
View File
@@ -595,6 +595,55 @@ fn extreme_raw_orders_are_normalized_once_across_body_and_headers() {
header.blocks.iter().filter_map(|b| b.as_para()).flat_map(|p| &p.objects).filter_map(|o| o.frame().map(|(_, _, f)| f.z)).collect();
assert_eq!(body, [1, u32::MAX - 1]);
assert_eq!(head, [u32::MAX, u32::MAX]);
d = import(&crate::text::tests::export_model(&d).unwrap()).unwrap();
d = import(&export(&d).unwrap()).unwrap();
}
}
#[test]
fn kept_stacking_markup_remaps_without_losing_unknown_attributes() {
for raw in ["4294967294", "4294967295"] {
let kept = format!(
r#"<text:p strange="retain &amp; &#x61;"><draw:rect text:anchor-type="paragraph" svg:width="1in" svg:height="1in" draw:z-index = '{raw}' strange="untouched"/></text:p>"#
);
let content = format!(
r#"<office:document-content {NS_T} {NS_D}><office:body><office:text>{kept}</office:text></office:body></office:document-content>"#
);
let mut d = import(&zip_of(&[("content.xml", content.as_bytes())])).unwrap();
let extra = format!(
r#"<office:document-content {NS_T} {NS_D}><office:body><office:text><text:p><draw:rect text:anchor-type="paragraph" svg:width="1in" svg:height="1in"/></text:p></office:text></office:body></office:document-content>"#
);
let mut added = import(&zip_of(&[("content.xml", extra.as_bytes())])).unwrap();
added.objects_mut(&|o| o.frame().is_some(), &mut |o| o.float_mut().unwrap().z = 0);
d.body.extend(added.body);
for pass in 0..3 {
let bytes = export(&d).unwrap();
let xml = crate::text::tests::entry(&bytes, "content.xml");
let expected = kept.replace(raw, "4294967293");
assert!(xml.contains(&expected), "kept XML lost or order not remapped, {raw}, pass {pass}: {xml}");
d = import(&bytes).unwrap();
let orders: Vec<u32> =
d.body.iter().filter_map(|b| b.as_para()).flat_map(|p| &p.objects).filter_map(|o| o.frame().map(|(_, _, f)| f.z)).collect();
assert_eq!(orders, [u32::MAX - 1, u32::MAX], "{raw}, pass {pass}");
}
}
}
#[test]
fn retained_unknown_orders_participate_in_the_export_gap() {
let kept = r#"<text:p strange="preserve"><alien draw:z-index='4294967293' keep="yes"/><draw:rect text:anchor-type="paragraph" svg:width="1in" svg:height="1in" draw:z-index="4294967294"/></text:p>"#;
let content =
format!(r#"<office:document-content {NS_T} {NS_D}><office:body><office:text>{kept}</office:text></office:body></office:document-content>"#);
let mut d = import(&zip_of(&[("content.xml", content.as_bytes())])).unwrap();
let mut extra = d.body[0].as_para().unwrap().clone();
extra.objects[0].float_mut().unwrap().z = 0;
d.body.push(craft_wordmodel::para_block(extra));
for pass in 0..3 {
let bytes = export(&d).unwrap();
let xml = crate::text::tests::entry(&bytes, "content.xml");
assert!(xml.contains("<alien draw:z-index='4294967292' keep=\"yes\"/>"), "pass {pass}: {xml}");
d = import(&bytes).unwrap();
let orders: Vec<u32> =
d.body.iter().filter_map(|b| b.as_para()).flat_map(|p| &p.objects).filter_map(|o| o.frame().map(|(_, _, f)| f.z)).collect();
assert_eq!(orders, [u32::MAX - 1, u32::MAX]);
}
}
+138
View File
@@ -70,3 +70,141 @@ pub(super) fn events_at(xml: &[u8], mut f: impl FnMut(Ev, (usize, usize))) {
}
}
}
/// Rewrite only the first interpreted draw:z-index value in each start tag.
/// Event spans exclude comments/CDATA and retain the reader's first-128-attribute
/// policy. All bytes outside changed values (including unknown markup) survive.
pub(super) fn remap_orders(xml: &[u8], map: impl Fn(u32) -> u32) -> Result<std::borrow::Cow<'_, [u8]>, String> {
// Callers only remove one unused upper-order slot: unchanged MAX means identity.
if map(u32::MAX) == u32::MAX {
return Ok(std::borrow::Cow::Borrowed(xml));
}
let mut out = Vec::new();
let mut copied = 0;
let mut error = None;
events_at(xml, |ev, (start, end)| {
if error.is_some() {
return;
}
let Ev::Start(_, attrs) = ev else { return };
let Some(raw) = get(&attrs, "draw:z-index").and_then(|s| s.trim().parse::<u32>().ok()) else { return };
let mapped = map(raw);
if mapped == raw {
return;
}
let Some(tag) = xml.get(start..end) else { return };
let Some(range) = order_value(tag) else {
error = Some("ODT: cannot locate retained stacking attribute".to_string());
return;
};
let from = start + range.start;
let to = start + range.end;
// Maps never increase a value; decimal replacement cannot exceed its source bytes.
if out.is_empty() && out.try_reserve_exact(xml.len()).is_err() {
error = Some("ODT: cannot allocate normalized stacking markup".to_string());
return;
}
out.extend_from_slice(&xml[copied..from]);
out.extend_from_slice(mapped.to_string().as_bytes());
copied = to;
});
if let Some(error) = error {
return Err(error);
}
if copied == 0 {
return Ok(std::borrow::Cow::Borrowed(xml));
}
out.extend_from_slice(&xml[copied..]);
Ok(std::borrow::Cow::Owned(out))
}
/// Locate a quoted value in an already parsed start tag, not in arbitrary XML.
/// Skip entire quoted values so attribute-looking text inside them is untouched.
fn order_value(tag: &[u8]) -> Option<std::ops::Range<usize>> {
let mut i = 1;
while i < tag.len() && !tag[i].is_ascii_whitespace() && !matches!(tag[i], b'/' | b'>') {
i += 1;
}
while i < tag.len() {
while i < tag.len() && tag[i].is_ascii_whitespace() {
i += 1;
}
let begin = i;
while i < tag.len() && !tag[i].is_ascii_whitespace() && !matches!(tag[i], b'=' | b'/' | b'>') {
i += 1;
}
let name = tag.get(begin..i)?;
if name.is_empty() {
// The lenient parser can recover after a stray delimiter before an attribute.
i += 1;
continue;
}
while i < tag.len() && tag[i].is_ascii_whitespace() {
i += 1;
}
if tag.get(i) != Some(&b'=') {
continue;
}
i += 1;
while i < tag.len() && tag[i].is_ascii_whitespace() {
i += 1;
}
let quote = *tag.get(i)?;
if !matches!(quote, b'\'' | b'"') {
while i < tag.len() && !tag[i].is_ascii_whitespace() {
i += 1;
}
continue;
}
i += 1;
let from = i;
while i < tag.len() && tag[i] != quote {
i += 1;
}
if i == tag.len() {
return None;
}
if name == b"draw:z-index" {
return Some(from..i);
}
i += 1;
}
None
}
#[cfg(test)]
mod order_tests {
use super::*;
#[test]
fn order_remap_preserves_every_other_byte() {
let xml = br#"<p a="draw:z-index='99'"><!-- draw:z-index="99" --><![CDATA[draw:z-index="99"]]><x q='&amp;' draw:z-index = ' 9&#57; ' weird="stay" draw:z-index="88"/><y draw:z-index="bad"/><z draw:z-index="0"/></p>"#;
let expected = br#"<p a="draw:z-index='99'"><!-- draw:z-index="99" --><![CDATA[draw:z-index="99"]]><x q='&amp;' draw:z-index = '98' weird="stay" draw:z-index="88"/><y draw:z-index="bad"/><z draw:z-index="0"/></p>"#;
assert_eq!(remap_orders(xml, |n| n.saturating_sub(1)).unwrap().as_ref(), expected);
assert!(matches!(remap_orders(xml, |n| n).unwrap(), std::borrow::Cow::Borrowed(_)));
let late = format!("<x {} draw:z-index='99'/>", (0..128).map(|i| format!("a{i}='x'")).collect::<Vec<_>>().join(" "));
assert_eq!(remap_orders(late.as_bytes(), |n| n - 1).unwrap().as_ref(), late.as_bytes());
for broken in ["=bad", "x=bad", "x==bad", "x", "x='a' x", "x=\"bad\"", "'odd'", "/odd", "x='a>b'"] {
let input = format!("<x {broken} draw:z-index='99'/>");
let mut seen = false;
events(input.as_bytes(), |e| {
if let Ev::Start(_, a) = e {
seen |= get(&a, "draw:z-index") == Some("99");
}
});
if seen {
let changed = remap_orders(input.as_bytes(), |n| n - 1).unwrap_or_else(|e| panic!("{input}: {e}"));
let mut found = false;
events(&changed, |e| {
if let Ev::Start(_, a) = e {
found |= get(&a, "draw:z-index") == Some("98");
}
});
assert!(found, "{input}");
}
}
// Lenient malformed attributes are skipped just as in the reader.
let malformed = b"<x broken draw:z-index='99'/>";
assert_eq!(remap_orders(malformed, |n| n - 1).unwrap().as_ref(), b"<x broken draw:z-index='98'/>");
}
}