Merge branch 'main' of https://github.com/storytold/craft-libs into feat/lottie-raw-gradient

# Conflicts:
#	ATTRIBUTION.md
This commit is contained in:
Dylan M. Taylor
2026-11-17 02:18:09 -05:00
38 changed files with 1812 additions and 52 deletions
+7
View File
@@ -104,6 +104,10 @@ and the exact transfer revision are recorded in each vendored crate's SOURCE.jso
`crates/tga/src/metadata_tests.rs` and metadata-boundary helpers in `crates/tga/src/lib.rs` are adapted from nuclearspike's FilmCraft #1932 at `e7842f76f887005c460c64a8ab51d59d086e6f27` (MIT OR Apache-2.0). Synthetic fixtures are original contributor test data; primary Git author retained.
The XML reader in `crates/plist/src/xml.rs` and its base64 compatibility helper are ported from Craft Anything `8f3b775157d331dd31cfa4da911e26d8856b8c77` (same MIT OR Apache-2.0 source licenses). Brandon Thomas is the primary import author; retained hardening credits include Afloat, BFlat, Rahul, RRFZ and NetterSilver. The finite-real fix preserves gmrmad1 as author from Craft Anything #1905 (`d85e104f`); quoted-attribute/line-ending regressions preserve RRFZ. Bounds/adaptation and bounded entity scanning are subsequent Dylan M. Taylor changes. App-specific JSON projections and keyed-archive interpretation remain outside this crate.
## Score serializers
`crates/score` extracts SoundCraft MusicXML and SVG score writing from `crates/engine/src/score.rs` at main `648ed9db9a0e98cc55643babffac381521a556c8`, with per-pitch ties adapted from [SoundCraft #182](https://github.com/storytold/soundcraft/pull/182) at `21cb618d93ec0e8d4771ac534994de8d5996ff10`. Source is MIT OR Apache-2.0. Import commits preserve Brandon Thomas, NetterSilver, Samuel Bacaro and Vikram Bhaskaran as primary authors of their respective changes. The consumer retains session mapping and layout. Shared API wiring, validation and focused tests are subsequent Dylan M. Taylor changes.
## Source-timed MIDI compatibility
`crates/midi/src/source_timed.rs` is extracted from LoopCraft `crates/midi/src/lib.rs` at `884e67c24b9b68badf6a4a4f7d62492f3673d354` (MIT OR Apache-2.0). The recorded source history for the extracted file is Dylan M. Taylor (`0d150c1f`, `440bc7ed`, `9e50523d`); Git authorship is preserved. It includes the original hostile-file, SMPTE, timing and round-trip tests. UTF-8 truncation and event-budget suffix repairs are subsequent changes. The module is an application-independent source-timed model; it does not depend on LoopCraft.
@@ -118,3 +122,6 @@ The XML reader in `crates/plist/src/xml.rs` and its base64 compatibility helper
The raw gradient wire ordering in `craft-lottie::Gradient::flat_values` follows
AniCraft's Lottie exporter by Tanish Manku (`63b39355`, `b9258b7b`), MIT OR Apache-2.0.
Sanitization, animation fitting and numeric output policy remain caller-owned.
## Screen Video key frames
`crates/video/src/screen_video.rs` extracts AniCraft `crates/xfl/src/video.rs` codec-3 decoding and its synthetic partial-block fixture, introduced by Tanish Manku in `1f3e603b7ff53a3da364e41ec4da8d43dc83aab0` with pixel-loop cleanup `2b7571363b4b647266df758f95b6e397021cfd4d` (MIT OR Apache-2.0). Import commits preserve Tanish as primary author. Caller-owned bounds and neutral RGBA output separate codec work from the consuming app's FLV first-frame/report policy. Boundary tests and integration are subsequent Dylan M. Taylor changes.
Generated
+8
View File
@@ -2943,6 +2943,13 @@ dependencies = [
"proptest",
]
[[package]]
name = "craft-score"
version = "0.1.0"
dependencies = [
"craft-xml",
]
[[package]]
name = "craft-scraft-format"
version = "0.1.0"
@@ -3286,6 +3293,7 @@ dependencies = [
"craft-jpeg",
"craft-media",
"craft-media-testkit",
"miniz_oxide 0.8.9",
"rav1e",
"rayon",
"thiserror 2.0.21",
+1
View File
@@ -12,6 +12,7 @@ repository = "https://github.com/storytold/craft-libs"
[workspace.dependencies]
craft-palette = { path = "crates/palette" }
craft-score = { path = "crates/score" }
craft-shx = { path = "crates/shx" }
craft-http-url = { path = "crates/http-url" }
craft-usd = { path = "crates/usd" }
+94
View File
@@ -12,6 +12,9 @@ pub const MAX_PART: u64 = 256 * 1024 * 1024;
pub const MAX_TOTAL: u64 = 512 * 1024 * 1024;
/// Most zip entries we look at.
pub const MAX_ENTRIES: usize = 20_000;
// ZIP names have at most u16::MAX raw bytes. CP437/lossy UTF-8 decoding can
// expand each byte to three UTF-8 bytes. Bound from_parts lookup copies too.
const MAX_SCAN_NAME: usize = 3 * u16::MAX as usize;
/// Relationship type suffixes (after `.../relationships/`).
pub mod rt {
@@ -270,6 +273,61 @@ impl Package {
}
}
/// Read only XML attributes, once per canonical package part. This never invokes
/// an altChunk importer or fetches additional data. Non-XML extensions are valid OPC
/// targets too. Errors end a lenient part exactly as the normal XML reader does;
/// the subsequent strict main-document parse remains authoritative.
pub(crate) fn file_orders(&self) -> Result<craft_wordmodel::para::FileOrders, DocxError> {
use craft_wordmodel::para::FileOrderScan;
let mut bytes = 0u64;
for (i, name) in self.opc.names().enumerate() {
if name.len() > MAX_SCAN_NAME {
return Err(DocxError::Limit("stacking-order source part name is too long".into()));
}
if i >= MAX_ENTRIES {
return Err(DocxError::Limit("too many stacking-order source parts".into()));
}
bytes = bytes
.checked_add(self.opc.get(name).map_or(0, |b| b.len() as u64))
.ok_or_else(|| DocxError::Limit("stacking-order source size overflow".into()))?;
if bytes > FileOrderScan::MAX_SOURCE_BYTES {
return Err(DocxError::Limit("stacking-order source exceeds 2 GiB".into()));
}
}
let mut scan = FileOrderScan::new(bytes).map_err(DocxError::Limit)?;
let limits = craft_xml::Limits {
max_input: 2 << 30,
max_depth: xml::MAX_DEPTH + 1,
max_nodes: 2 * xml::MAX_ELEMENTS,
max_elements: xml::MAX_ELEMENTS,
max_attrs: xml::MAX_ATTRS,
max_attrs_total: 2 * xml::MAX_ELEMENTS,
};
let opts = craft_xml::Options::default().with_limits(limits);
for name in self.opc.names() {
let Some(bytes) = self.opc.get(name) else { continue };
let text = craft_xml::to_utf8(bytes);
// Attribute names cannot contain entity references. This only skips parts
// that cannot possibly supply an order, including ordinary binary media.
if !text.windows(b"relativeHeight".len()).any(|w| w == b"relativeHeight") {
continue;
}
let mut events = opts.events(&text);
while let Ok(Some(ev)) = events.next() {
if let craft_xml::Event::Start { attrs, .. } = ev {
for (name, value) in attrs {
if name.rsplit(':').next() == Some("relativeHeight")
&& let Ok(raw) = value.trim().parse::<u32>()
{
scan.observe(raw);
}
}
}
}
}
scan.finish().map_err(DocxError::Limit)
}
/// Relationships of `part` (from `dir/_rels/name.rels`). Internal targets name the part as
/// stored, so a part reached under several spellings is kept, cached and written once.
pub fn rels(&self, part: &str) -> Rels {
@@ -391,3 +449,39 @@ mod tests {
assert!(junk.parts.is_empty());
}
}
#[cfg(test)]
mod file_order_tests {
use super::*;
#[test]
fn file_order_scan_bounds_decoded_part_names_before_lookup() {
let at_limit = "n".repeat(MAX_SCAN_NAME);
assert!(Package::from_parts(BTreeMap::from([(at_limit, Vec::new())])).file_orders().is_ok());
let too_long = "n".repeat(MAX_SCAN_NAME + 1);
assert!(Package::from_parts(BTreeMap::from([(too_long, Vec::new())])).file_orders().is_err());
}
fn scan(xml: &str) -> craft_wordmodel::para::FileOrders {
Package::from_parts(BTreeMap::from([("odd.part".into(), xml.as_bytes().to_vec())])).file_orders().unwrap()
}
#[test]
fn file_order_scan_matches_lenient_attributes_entities_and_parser_limits() {
let malformed = r#"<r><x alien:relativeHeight="429496729&#53;"></wrong><x relativeHeight="4294967294"/></r>"#;
let tree = xml::parse(malformed.as_bytes()).unwrap();
assert_eq!(tree.find("?:x").or_else(|| tree.find("x")).unwrap().attr("relativeHeight"), Some("4294967295"));
let map = scan(malformed);
assert_eq!((map.model_order(Some(u32::MAX - 1)), map.model_order(Some(u32::MAX))), (u32::MAX - 1, u32::MAX));
let custom = r#"<!DOCTYPE r [<!ENTITY rank "4294967295">]><r relativeHeight="&rank;"/>"#;
assert_ne!(xml::parse(custom.as_bytes()).unwrap().attr("relativeHeight"), Some("4294967295"));
assert_eq!(scan(custom).model_order(Some(u32::MAX - 1)), u32::MAX);
let too_many =
format!(r#"<r {} relativeHeight="4294967295"/>"#, (0..xml::MAX_ATTRS).map(|i| format!(r#"a{i}="x""#)).collect::<Vec<_>>().join(" "));
assert!(xml::parse(too_many.as_bytes()).is_err());
assert_eq!(scan(&too_many).model_order(Some(u32::MAX - 1)), u32::MAX);
let too_deep = format!(r#"{}<r relativeHeight="4294967295"/>{}"#, "<r>".repeat(xml::MAX_DEPTH + 1), "</r>".repeat(xml::MAX_DEPTH + 1));
assert!(xml::parse(too_deep.as_bytes()).is_err());
assert_eq!(scan(&too_deep).model_order(Some(u32::MAX - 1)), u32::MAX);
}
}
+3
View File
@@ -70,6 +70,7 @@ const MAX_TEMPLATE_PATH: usize = 32 * 1024;
pub(crate) struct Reader<'p> {
pkg: &'p Package,
file_orders: craft_wordmodel::para::FileOrders,
pub doc: Document,
pub pc: PropCtx,
revs: HashMap<(u8, String, String, Option<String>), u32>,
@@ -167,7 +168,9 @@ fn read_opened_in(pkg: &Package, chunks: ChunkState) -> Result<(Document, usize,
let rels = pkg.rels(&main);
let mut doc = Document::new();
doc.body.clear();
let file_orders = pkg.file_orders()?;
let mut r = Reader {
file_orders,
pkg,
doc,
pc: PropCtx::default(),
+8 -7
View File
@@ -12,7 +12,7 @@ impl Reader<'_> {
/// text and drawing ids are carried into the object. Returns `None` when the frame or payload
/// cannot be read. Ink written by WordCraft keeps its tool's caps rather than an outline cap.
pub(super) fn read_drawing(&mut self, sc: &mut StoryCtx, d: &El, rels: &Rels) -> Option<InlineObject> {
let Frame { c, anchored, w, h, alt, mut float } = drawing_frame(d)?;
let Frame { c, anchored, w, h, alt, mut float } = drawing_frame(d, self.file_orders)?;
let gd = c.child("a:graphic").and_then(|g| g.child("a:graphicData"))?;
// Checked by URI before the blip search: a diagram's data can hold a blip further down.
let uri = gd.attr("uri").unwrap_or("");
@@ -195,7 +195,7 @@ impl Reader<'_> {
if fallback.find("a:graphic").is_some() || !fallback.els().all(|e| RUN_CONTENT.contains(&e.name.as_str())) {
return None;
}
let Frame { c, w, h, alt, mut float, .. } = drawing_frame(choice.child("w:drawing")?)?;
let Frame { c, w, h, alt, mut float, .. } = drawing_frame(choice.child("w:drawing")?, self.file_orders)?;
let part = c
.child("a:graphic")
.and_then(|g| g.child("a:graphicData"))
@@ -627,7 +627,7 @@ struct Frame<'a> {
float: Float,
}
fn drawing_frame(d: &El) -> Option<Frame<'_>> {
fn drawing_frame(d: &El, file_orders: craft_wordmodel::para::FileOrders) -> Option<Frame<'_>> {
let (c, anchored) = match d.child("wp:inline") {
Some(c) => (c, false),
None => (d.child("wp:anchor")?, true),
@@ -636,7 +636,7 @@ fn drawing_frame(d: &El) -> Option<Frame<'_>> {
let dim = |n: &str| ext.and_then(|e| e.attr(n)).and_then(|v| measure(v, 12_700.0)).unwrap_or(0.0).clamp(0.0, crate::units::MAX_LEN_PT);
let (w, h) = (dim("cx"), dim("cy"));
let (alt, meta) = nv_alt(c.child("wp:docPr"));
let mut float = if anchored { anchor_float(c) } else { Float::default() };
let mut float = if anchored { anchor_float(c, file_orders) } else { Float::default() };
float.alt = meta;
float.hidden = c.child("wp:docPr").is_some_and(|p| on_off_attr(p, "hidden"));
if let Some(e) = c.child("wp:effectExtent") {
@@ -670,10 +670,11 @@ fn nv_alt(p: Option<&El>) -> (String, AltText) {
(descr, AltText { text: String::new(), title, decorative })
}
fn anchor_float(c: &El) -> Float {
fn anchor_float(c: &El, orders: craft_wordmodel::para::FileOrders) -> Float {
// `relativeHeight`: the stacking order Word draws floating objects in (higher on top), which
// need not be their order in the file.
let z = c.attr("relativeHeight").and_then(|v| v.trim().parse::<u32>().ok()).unwrap_or(0);
// need not be their order in the file. Kept one more (see `Float::z`): 0, the lowest a file
// can give, isn't the 0 of an object without one, which stacks above them all.
let z = orders.model_order(c.attr("relativeHeight").and_then(|v| v.trim().parse::<u32>().ok()));
let mut f = Float { wrap: Wrap::Square, z, ..Float::default() };
let behind = on_off_attr(c, "behindDoc");
// Word wraps a position relative to the page's size (`wp14:pctPosVOffset`) in
+27
View File
@@ -83,6 +83,26 @@ impl PartRels {
}
}
/// Where Word's stacking orders (`relativeHeight`) start for objects made in Word.
const FIRST_Z: u32 = 251_658_240;
/// The `relativeHeight` the objects of `doc` that have none count up from, and the `relativeHeight`
/// each stored order is written as when it isn't its own (wordcraft#2638). A stored order
/// ([`craft_wordmodel::para::Float::z`]) is one more than the file's, and the new objects go above
/// every stored one. When the stored orders leave no room for them below `u32::MAX` (a file using
/// values that high; `u32::MAX` itself would read back as the order below it), the stored ones are
/// numbered again from [`FIRST_Z`] in their order, so no two objects share an order and none goes
/// out of range.
pub(crate) fn stacking(doc: &Document) -> (u32, BTreeMap<u32, u32>) {
let (orders, unset) = doc.stacking_orders();
let top = orders.last().map_or(0, |z| z.saturating_sub(1)).max(FIRST_Z);
if u32::try_from(unset).ok().and_then(|n| top.checked_add(n)).is_some_and(|last| last < u32::MAX) {
return (top, BTreeMap::new());
}
let zmap: BTreeMap<u32, u32> = orders.iter().zip(FIRST_Z..=u32::MAX).map(|(&z, to)| (z, to)).collect();
(zmap.values().last().copied().unwrap_or(FIRST_Z), zmap)
}
pub(crate) struct Writer<'d> {
doc: &'d Document,
/// Numbered display equations written so far (automatic numbers are written as text).
@@ -96,7 +116,14 @@ pub(crate) struct Writer<'d> {
docpr: u32,
/// Shapes' own drawing ids written so far (connectors refer to them).
shape_ids: std::collections::HashSet<u32>,
/// The last stacking order (`relativeHeight`) given to an object that had none.
z: u32,
/// Stored stacking orders written as other `relativeHeight`s (see [`stacking`]); empty: each
/// as its own.
zmap: BTreeMap<u32, u32>,
/// 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<u32>,
endnotes: Vec<u32>,
+31 -1
View File
@@ -342,7 +342,16 @@ impl super::Writer<'_> {
/// the object's current size and position into its own markup (a chart or diagram's frame is
/// written by the caller). `None` when it can't be written back (its parts are gone).
pub(super) fn embedded_xml(&mut self, src: &Embedded, rels: &mut PartRels, resize: Option<(f32, f32, &Float, &str)>) -> Option<String> {
self.embedded_tree(src, rels, resize).map(|root| root.to_xml())
let mut root = self.embedded_tree(src, rels, resize)?;
// read_object prefers a direct VML preview. Its signed z-index is not modeled, so a
// coexisting retained DrawingML frame cannot safely be assigned the model's order.
let vml_preview = root.els().any(|e| e.name.starts_with("v:") && e.local() != "shapetype");
if let Some((_, _, float, _)) = resize
&& !vml_preview
{
self.retained_anchor_order(&mut root, float, &mut None, 0);
}
Some(root.to_xml())
}
/// The same bounded preservation operation, retaining the parsed tree for a caller that
@@ -481,6 +490,27 @@ impl super::Writer<'_> {
/// Write an OLE object's size (and position, when floating), rotation and flips into its VML
/// shape and DrawingML picture, with the effect extent covering the rotated bounds as for
/// other drawings, its alt text into both, and give its drawing a fresh `wp:docPr` id.
/// Retained DrawingML frames of one OLE object share one allocated order.
/// This does not normalize VML z-index: its signed order is not represented by the reader.
/// Stop at each outer anchor or inline frame: nested drawings have their own placement.
fn retained_anchor_order(&mut self, e: &mut El, float: &Float, order: &mut Option<u32>, depth: usize) {
if depth > MAX_DEPTH {
return;
}
if e.name == "wp:inline" {
return;
}
if e.name == "wp:anchor" {
let z = *order.get_or_insert_with(|| self.drawing_order(float));
set_attr(e, "relativeHeight", &z.to_string());
return;
}
for child in &mut e.kids {
if let xml::Node::El(child) = child {
self.retained_anchor_order(child, float, order, depth + 1);
}
}
}
fn resize_object(&mut self, e: &mut El, w: f32, h: f32, float: &Float, alt: &str, depth: usize) {
if depth > MAX_DEPTH {
return;
+4 -1
View File
@@ -32,7 +32,9 @@ pub(crate) fn write_parts(doc: &Document, flavor: Flavor) -> Result<Vec<(String,
// Fresh drawing ids start past the shapes' own (connectors refer to those).
docpr: doc.next_shape_id().saturating_sub(1),
shape_ids: std::collections::HashSet::new(),
z: 251_658_240,
z: 0,
zmap: BTreeMap::new(),
anchor_z: 0,
footnotes: Vec::new(),
endnotes: Vec::new(),
hf: Vec::new(),
@@ -58,6 +60,7 @@ pub(crate) fn write_parts(doc: &Document, flavor: Flavor) -> Result<Vec<(String,
.flat_map(|(n, c)| c.iter().enumerate().map(move |(seq, id)| (*id, (n as u32 + 1, seq as u32))))
.collect(),
};
(wr.z, wr.zmap) = super::stacking(doc);
wr.assign_media();
wr.embeds.reserve_media(wr.media_files.values());
let mut entries: Vec<(String, Vec<u8>)> = Vec::new();
+15 -4
View File
@@ -263,6 +263,17 @@ impl Writer<'_> {
w.close("w:drawing");
}
/// One order for an anchored object, shared by generated and retained drawings.
pub(in crate::write) fn drawing_order(&mut self, float: &Float) -> u32 {
let z = if float.z > 0 {
self.zmap.get(&float.z).copied().unwrap_or(float.z.saturating_sub(1))
} else {
self.z = self.z.saturating_add(1);
self.z
};
self.anchor_z = z;
z
}
pub(super) fn drawing_open(&mut self, w: &mut W, info: DrawingInfo<'_>) {
let DrawingInfo { float, size: (iw, ih), docpr, name, alt, link } = info;
let (cx, cy) = (emu(iw.max(0.0)), emu(ih.max(0.0)));
@@ -271,9 +282,9 @@ impl Writer<'_> {
} else {
let d = emu(float.dist.clamp(0.0, 1584.0));
let (dt, db) = (emu(float.dist_top.clamp(0.0, 1584.0)), emu(float.dist_bottom.clamp(0.0, 1584.0)));
// The stacking order read from the file is kept; objects without one stack in
// document order above it.
self.z = if float.z > 0 { float.z } else { self.z.saturating_add(1) };
// A stacking order read from a file is kept (one less: see `Float::z`); objects made
// here go above those.
let z = self.drawing_order(float);
let behind = if float.wrap == Wrap::BehindText { "1" } else { "0" };
w.open(
"wp:anchor",
@@ -283,7 +294,7 @@ impl Writer<'_> {
("distL", &d),
("distR", &d),
("simplePos", "0"),
("relativeHeight", &self.z.to_string()),
("relativeHeight", &z.to_string()),
("behindDoc", behind),
("locked", "0"),
("layoutInCell", if float.outside_cell { "0" } else { "1" }),
+3 -2
View File
@@ -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")]);
+264 -2
View File
@@ -1282,10 +1282,11 @@ fn anchor_stacking_order_is_read_and_kept() {
paras(d)[0].objects.iter().filter_map(|o| if let InlineObject::Shape { float, .. } = o { Some(float.z) } else { None }).collect()
};
let d = read_body(&body);
assert_eq!(zs(&d), [251660288, 251657214, 0]);
// Kept one more than the file's (`Float::z`), so a file's 0 isn't "none".
assert_eq!(zs(&d), [251660289, 251657215, 0]);
let again = craft_docx::read(&craft_docx::write(&d).unwrap()).unwrap();
let z = zs(&again);
assert_eq!(&z[..2], &[251660288, 251657214]);
assert_eq!(&z[..2], &[251660289, 251657215]);
assert!(z[2] > 0, "an object without one gets an order on saving: {z:?}");
}
@@ -2697,6 +2698,100 @@ fn anchored_wsp(name: &str, cx: u32, cy: u32, sppr: &str, style: &str, extra: &s
)
}
/// wordcraft#2638: Word stacks floating objects by `relativeHeight`, not by their order in the text (a
/// shape sent to back, then copied with the shape over it, comes first in the text but stays
/// behind). The order is read and kept on save, and an object made here is saved above them.
#[test]
fn floating_objects_keep_their_stacking_order() {
let shape = |name: &str, z: u32| {
anchored_wsp(name, 127000, 127000, r#"<a:prstGeom prst="rect"><a:avLst/></a:prstGeom>"#, "", "<wps:cNvSpPr/>")
.replace(r#"relativeHeight="1""#, &format!(r#"relativeHeight="{z}""#))
};
let body = format!("<w:p>{}{}</w:p>", shape("Canopy", 251662336), shape("Trunk", 251661312));
let mut d = read_body(&body);
let stacking = |d: &Document| paras(d)[0].objects.iter().map(|o| o.frame().map(|(_, _, f)| f.z)).collect::<Vec<_>>();
assert_eq!(stacking(&d), [Some(251662337), Some(251661313)]);
let mut made = paras(&d)[0].objects[0].clone();
if let InlineObject::Shape { float, .. } = &mut made {
float.z = 0;
}
let Some(Block::Para(p)) = d.body.first_mut().map(std::sync::Arc::make_mut) else { panic!("a paragraph") };
p.insert_object(0, made, &Default::default()).unwrap();
let files = unzip(&craft_docx::write(&d).unwrap());
let written = attr_values(&text(&files, "word/document.xml"), "wp:anchor", "relativeHeight");
assert_eq!(written, ["251662337", "251662336", "251661312"]);
let back = craft_docx::read(&craft_docx::write(&d).unwrap()).unwrap();
assert_eq!(stacking(&back), [Some(251662338), Some(251662337), Some(251661313)]);
}
/// wordcraft#2638: a file whose stacking orders reach `u32::MAX` leaves no room above them for an object
/// made here. The stored orders are then numbered again, in their order, so the new object still
/// goes over all of them and no two objects share an order (it was written as `u32::MAX` too).
#[test]
fn stacking_orders_at_the_top_of_the_range_are_numbered_again() {
let shape = |name: &str, z: u32| {
anchored_wsp(name, 127000, 127000, r#"<a:prstGeom prst="rect"><a:avLst/></a:prstGeom>"#, "", "<wps:cNvSpPr/>")
.replace(r#"relativeHeight="1""#, &format!(r#"relativeHeight="{z}""#))
};
let body = format!("<w:p>{}{}</w:p>", shape("Top", u32::MAX), shape("Low", 7));
let mut d = read_body(&body);
let stacking = |d: &Document| paras(d)[0].objects.iter().map(|o| o.frame().map(|(_, _, f)| f.z)).collect::<Vec<_>>();
assert_eq!(stacking(&d), [Some(u32::MAX), Some(8)]);
let mut made = paras(&d)[0].objects[0].clone();
if let InlineObject::Shape { float, .. } = &mut made {
float.z = 0;
}
let Some(Block::Para(p)) = d.body.first_mut().map(std::sync::Arc::make_mut) else { panic!("a paragraph") };
p.insert_object(0, made, &Default::default()).unwrap();
let back = craft_docx::read(&craft_docx::write(&d).unwrap()).unwrap();
let z: Vec<u32> = stacking(&back).into_iter().map(|z| z.expect("every anchor has an order")).collect();
assert!(z[0] > z[1] && z[1] > z[2], "new over Top over Low, all different: {z:?}");
// The note separators are saved with the document: an order there counts too.
let mut d = read_body(&format!("<w:p>{}</w:p>", shape("Low", 7)));
let mut sep = read_body(&format!("<w:p>{}</w:p>", shape("Separator", u32::MAX)));
d.footnote_separators.separator = Some(std::mem::take(&mut sep.body));
let (orders, unset) = d.stacking_orders();
assert_eq!((orders.last().copied(), unset), (Some(u32::MAX), 0), "the separator's order is seen");
let mut made = paras(&d)[0].objects[0].clone();
if let InlineObject::Shape { float, .. } = &mut made {
float.z = 0;
}
let Some(Block::Para(p)) = d.body.first_mut().map(std::sync::Arc::make_mut) else { panic!("a paragraph") };
p.insert_object(0, made, &Default::default()).unwrap();
let files = unzip(&craft_docx::write(&d).unwrap());
let body: Vec<u64> = attr_values(&text(&files, "word/document.xml"), "wp:anchor", "relativeHeight").iter().map(|v| v.parse().unwrap()).collect();
let seps: Vec<u64> = attr_values(&text(&files, "word/footnotes.xml"), "wp:anchor", "relativeHeight").iter().map(|v| v.parse().unwrap()).collect();
assert_eq!(seps.len(), 1, "the separator's shape is saved");
assert!(body[0] > seps[0] && seps[0] > body[1], "new over the separator's over Low: {body:?} {seps:?}");
// So does one in a building block of the glossary, saved with the same numbering (wordcraft#2638 review).
let mut d = read_body(&format!("<w:p>{}</w:p>", shape("Low", 7)));
let entry = read_body(&format!("<w:p>{}</w:p>", shape("Entry", u32::MAX)));
d.glossary.push(craft_wordmodel::BuildingBlock { name: "Entry".into(), blocks: entry.body, ..Default::default() });
assert_eq!(d.stacking_orders().0.last().copied(), Some(u32::MAX), "the glossary's order is seen");
// With room above, the stored orders are written as they are.
let room = read_body(&format!("<w:p>{}</w:p>", shape("Top", u32::MAX - 5)));
let back = craft_docx::read(&craft_docx::write(&room).unwrap()).unwrap();
assert_eq!(stacking(&back), [Some(u32::MAX - 4)]);
}
/// wordcraft#2638 (review): `relativeHeight="0"`, the lowest a file can give, is a stacking order like
/// any other: under a shape with 1, not taken for an object without one (made here, which goes
/// over everything), and saved back as 0.
#[test]
fn a_stacking_order_of_zero_stays_the_lowest() {
let shape = |name: &str, z: u32| {
anchored_wsp(name, 127000, 127000, r#"<a:prstGeom prst="rect"><a:avLst/></a:prstGeom>"#, "", "<wps:cNvSpPr/>")
.replace(r#"relativeHeight="1""#, &format!(r#"relativeHeight="{z}""#))
};
let d = read_body(&format!("<w:p>{}{}</w:p>", shape("Bottom", 0), shape("Top", 1)));
let floats: Vec<_> = paras(&d)[0].objects.iter().filter_map(|o| o.frame().map(|(_, _, f)| f.clone())).collect();
assert_eq!(floats.iter().map(|f| f.z).collect::<Vec<_>>(), [1, 2]);
assert!(floats[0].stack() < floats[1].stack(), "0 stacks under 1");
assert!(floats[1].stack() < craft_wordmodel::para::Float::default().stack(), "an object made here goes over both");
let files = unzip(&craft_docx::write(&d).unwrap());
assert_eq!(attr_values(&text(&files, "word/document.xml"), "wp:anchor", "relativeHeight"), ["0", "1"]);
}
/// #611: diagrams drawn with Word's shapes were mostly missing. Connectors and boxes take their
/// outline and fill from the theme through their style references, or name preset and scheme
/// colours; all of these are read, with line widths and arrowheads.
@@ -4911,3 +5006,170 @@ fn vml_horizontal_rules_are_read_and_kept() {
let d = read_body(&body);
assert_eq!(rules(&d).iter().map(|r| r.0).collect::<Vec<_>>(), [None, None]);
}
#[test]
fn extreme_raw_orders_share_one_map_across_parts_without_replaying_chunks() {
let shape = |name: &str, z: &str| {
anchored_wsp(name, 127000, 127000, r#"<a:prstGeom prst="rect"><a:avLst/></a:prstGeom>"#, "", "<wps:cNvSpPr/>")
.replace(r#"relativeHeight="1""#, &format!(r#"relativeHeight="{z}""#))
};
let doc = format!(
r#"<w:document {W_NS}><w:body><w:p>{}{}{}</w:p><w:altChunk r:id="chunk"/><w:sectPr><w:headerReference w:type="default" r:id="header"/></w:sectPr></w:body></w:document>"#,
shape("zero", "0"),
shape("lower", "4294967292"),
shape("high", "4294967294")
);
let header = format!(
r#"<w:hdr {W_NS}><w:p>{}{}</w:p></w:hdr>"#,
shape("maximum", "429496729&#53;").replace("relativeHeight", "alien:relativeHeight"),
shape("equal", "4294967295")
)
.replace("xmlns:wp=", "xmlns:position=")
.replace("wp:", "position:");
let header: Vec<u8> = [vec![0xff, 0xfe], header.encode_utf16().flat_map(u16::to_le_bytes).collect()].concat();
let ignored = format!(r#"<w:hdr {W_NS}><w:p>{}</w:p></w:hdr>"#, shape("wrong-case duplicate", "4294967293"));
let rel = rels(&[("header", "header", "header.part"), ("chunk", "aFChunk", "chunk.txt")]);
let bytes = zip(&[
("_rels/.rels", ROOT_RELS.as_bytes()),
("word/document.xml", doc.as_bytes()),
("word/_rels/document.xml.rels", rel.as_bytes()),
("word/header.part", &header),
("WORD/HEADER.PART", ignored.as_bytes()),
("word/chunk.txt", b"external chunk"),
]);
let calls = std::cell::Cell::new(0);
let importer = |_: &craft_docx::AltChunk| {
calls.set(calls.get() + 1);
Some(Document::from_text("Imported once"))
};
let mut d = craft_docx::read_with(&bytes, &craft_docx::ReadOptions { chunks: Some(&importer), ..Default::default() }).unwrap();
assert_eq!(calls.get(), 1);
let orders = |d: &Document| {
let body: Vec<u32> = paras(d).iter().flat_map(|p| &p.objects).filter_map(|o| o.frame().map(|(_, _, f)| f.z)).collect();
let header = &d.parts[&d.last_section.headers.default.unwrap()];
let header: Vec<u32> =
header.blocks.iter().filter_map(|b| b.as_para()).flat_map(|p| &p.objects).filter_map(|o| o.frame().map(|(_, _, f)| f.z)).collect();
(body, header)
};
let expected = (vec![1, u32::MAX - 2, u32::MAX - 1], vec![u32::MAX, u32::MAX]);
assert_eq!(orders(&d), expected);
for _ in 0..2 {
d = craft_docx::read(&craft_docx::write(&d).unwrap()).unwrap();
assert_eq!(orders(&d), expected);
}
assert_eq!(calls.get(), 1);
let malformed = doc.replace("</w:body>", "");
assert!(craft_docx::read(&zip(&[("_rels/.rels", ROOT_RELS.as_bytes()), ("word/document.xml", malformed.as_bytes())])).is_err());
}
#[test]
fn retained_drawingml_ole_uses_the_shared_stacking_map() {
let source = r##"<w:p><w:r><w:object w:dxaOrig="1440" w:dyaOrig="720"><w:drawing><wp:inline distT="0" distB="0" distL="0" distR="0"><wp:extent cx="914400" cy="457200"/><wp:docPr id="7" name="Object 7"/><a:graphic><a:graphicData uri="http://schemas.openxmlformats.org/drawingml/2006/picture"><pic:pic><pic:nvPicPr><pic:cNvPr id="0" name=""/><pic:cNvPicPr/></pic:nvPicPr><pic:blipFill><a:blip r:embed="rId8"/><a:stretch><a:fillRect/></a:stretch></pic:blipFill><pic:spPr><a:xfrm flipH="1"><a:off x="0" y="0"/><a:ext cx="914400" cy="457200"/></a:xfrm><a:prstGeom prst="rect"><a:avLst/></a:prstGeom></pic:spPr></pic:pic></a:graphicData></a:graphic></wp:inline></w:drawing><o:OLEObject Type="Embed" ProgID="Package" DrawAspect="Content" ObjectID="_1000000002" r:id="rId9"/></w:object></w:r></w:p>"##;
let picture = "\u{89}PNG stand-in";
let ole = "\u{d0}\u{cf} opaque OLE storage";
let types = r#"<Types xmlns="http://schemas.openxmlformats.org/package/2006/content-types"><Default Extension="png" ContentType="image/png"/><Default Extension="bin" ContentType="application/vnd.openxmlformats-officedocument.oleObject"/></Types>"#;
for raw in [u32::MAX - 2, u32::MAX] {
let object = source
.replace("<wp:inline ", &format!("<wp:anchor relativeHeight=\"{raw}\" behindDoc=\"0\" odd=\"retain &amp; this\" "))
.replace("</wp:inline>", "</wp:anchor>");
let shape = anchored_wsp("Other", 127000, 127000, r#"<a:prstGeom prst="rect"><a:avLst/></a:prstGeom>"#, "", "<wps:cNvSpPr/>")
.replace(r#"relativeHeight="1""#, r#"relativeHeight="4294967294""#);
let body = format!("{object}<w:p>{shape}</w:p>");
let mut d = craft_docx::read(&docx(
&body,
&[("rId8", "image", "media/image1.png"), ("rId9", "oleObject", "embeddings/oleObject1.bin")],
&[("[Content_Types].xml", types), ("word/media/image1.png", picture), ("word/embeddings/oleObject1.bin", ole)],
))
.unwrap();
assert!(matches!(paras(&d)[0].objects[0], InlineObject::Image { ole: Some(_), .. }));
let mut made = paras(&d)[0].objects[0].clone();
made.float_mut().unwrap().z = 0;
let mut p = Paragraph::new();
p.insert_object(0, made, &Default::default()).unwrap();
d.body.push(craft_wordmodel::para_block(p));
for _ in 0..3 {
let out = craft_docx::write(&d).unwrap();
let files = unzip(&out);
let xml = text(&files, "word/document.xml");
assert_eq!(attr_values(&xml, "wp:anchor", "odd"), ["retain &amp; this", "retain &amp; this"]);
assert_eq!(attr_values(&xml, "o:OLEObject", "ProgID"), ["Package", "Package"]);
let z: Vec<u32> = attr_values(&xml, "wp:anchor", "relativeHeight").iter().map(|v| v.parse().unwrap()).collect();
assert_eq!(z.len(), 3);
assert!(z[2] > z[0] && z[2] > z[1], "{z:?}");
assert_eq!(z[0] < z[1], raw < u32::MAX - 1, "{z:?}");
d = craft_docx::read(&out).unwrap();
assert!(matches!(paras(&d)[0].objects[0], InlineObject::Image { ole: Some(_), .. }));
}
}
// An inline OLE frame may retain an opaque nested drawing. Its child's rank is not the
// outer object's rank, and the inline frame must not allocate an anchored rank at all.
let nested = source.replace("</wp:inline>", r#"<a:extLst><wp:anchor relativeHeight="17" odd="nested"/></a:extLst></wp:inline>"#);
let mut d = craft_docx::read(&docx(
&nested,
&[("rId8", "image", "media/image1.png"), ("rId9", "oleObject", "embeddings/oleObject1.bin")],
&[("[Content_Types].xml", types), ("word/media/image1.png", picture), ("word/embeddings/oleObject1.bin", ole)],
))
.unwrap();
for _ in 0..3 {
let out = craft_docx::write(&d).unwrap();
let files = unzip(&out);
let xml = text(&files, "word/document.xml");
assert_eq!(attr_values(&xml, "wp:anchor", "relativeHeight"), ["17"], "{xml}");
assert_eq!(attr_values(&xml, "wp:anchor", "odd"), ["nested"]);
assert!(attr_values(&xml, "wp:inline", "relativeHeight").is_empty());
d = craft_docx::read(&out).unwrap();
assert!(matches!(paras(&d)[0].objects[0], InlineObject::Image { ole: Some(_), .. }));
}
// A direct VML preview wins in read_object, even when DrawingML is retained beside it.
// Its signed z-index is not a modeled stacking rank. Preserve both representations rather
// than assigning a fresh DrawingML rank from the VML-derived model's unset z.
for signed in [-17, 17] {
let vml = format!(
r#"<v:shape style="position:absolute;width:72pt;height:36pt;z-index:{signed}" odd="vml"><v:imagedata r:id="rId8"/></v:shape><w:drawing>"#
);
let mixed =
source.replace("<w:drawing>", &vml).replace("<wp:inline ", r#"<wp:anchor relativeHeight="21" "#).replace("</wp:inline>", "</wp:anchor>");
let mut d = craft_docx::read(&docx(
&mixed,
&[("rId8", "image", "media/image1.png"), ("rId9", "oleObject", "embeddings/oleObject1.bin")],
&[("[Content_Types].xml", types), ("word/media/image1.png", picture), ("word/embeddings/oleObject1.bin", ole)],
))
.unwrap();
assert_eq!(paras(&d)[0].objects[0].frame().unwrap().2.z, 0);
for _ in 0..3 {
let out = craft_docx::write(&d).unwrap();
let files = unzip(&out);
let xml = text(&files, "word/document.xml");
assert_eq!(attr_values(&xml, "wp:anchor", "relativeHeight"), ["21"]);
assert_eq!(attr_values(&xml, "v:shape", "odd"), ["vml"]);
let styles = attr_values(&xml, "v:shape", "style");
assert_eq!(styles.len(), 1);
assert!(styles[0].split(';').any(|p| p == format!("z-index:{signed}")));
d = craft_docx::read(&out).unwrap();
assert_eq!(paras(&d)[0].objects[0].frame().unwrap().2.z, 0);
}
}
}
#[test]
fn extreme_chart_with_text_fallback_uses_the_package_order_map() {
// Both callers need the package map. MAX alone would still saturate to MAX without it;
// placing MAX-1 on each representation in turn catches a missing map at either caller.
for (chart_raw, shape_raw) in [(u32::MAX, u32::MAX - 1), (u32::MAX - 1, u32::MAX)] {
let mut files = unzip(&chartex_text_fallback_docx("<w:t>Chart not available.</w:t>"));
let shape = anchored_wsp("Other", 127000, 127000, r#"<a:prstGeom prst="rect"><a:avLst/></a:prstGeom>"#, "", "<wps:cNvSpPr/>")
.replace(r#"relativeHeight="1""#, &format!(r#"relativeHeight="{shape_raw}""#));
let xml = text(&files, "word/document.xml")
.replace("<wp:inline ", &format!(r#"<wp:anchor relativeHeight="{chart_raw}" "#))
.replace("</wp:inline>", "</wp:anchor>")
.replace("</w:body>", &format!("<w:p>{shape}</w:p></w:body>"));
files.insert("word/document.xml".to_string(), xml.into_bytes());
let entries: Vec<_> = files.iter().map(|(name, bytes)| (name.as_str(), bytes.as_slice())).collect();
let d = craft_docx::read(&zip(&entries)).unwrap();
let g = graphics(&d);
let [InlineObject::Graphic { float, .. }] = g.as_slice() else { panic!("chart placeholder missing") };
assert_eq!(float.z, chart_raw);
let other = paras(&d).iter().flat_map(|p| &p.objects).find(|o| matches!(o, InlineObject::Shape { .. })).unwrap();
assert_eq!(other.frame().unwrap().2.z, shape_raw);
}
}
+6 -10
View File
@@ -23,18 +23,14 @@ fn paras(doc: &Document) -> Vec<&Paragraph> {
doc.body.iter().filter_map(|b| b.as_para()).collect()
}
/// `doc` with the stacking order of its body's floating objects cleared: saving gives objects
/// without one an order (`relativeHeight`), so tests about other properties compare without it.
/// `doc` without the stacking order a save gives objects made in the app (`relativeHeight`,
/// read back as `Float::z`), to compare them with the objects as made.
fn unstacked(mut doc: Document) -> Document {
for b in &mut doc.body {
if let Block::Para(p) = std::sync::Arc::make_mut(b) {
for o in &mut p.objects {
if let Some(f) = o.float_mut() {
f.z = 0;
}
}
doc.objects_mut(&|_| true, &mut |o| {
if let Some(f) = o.float_mut() {
f.z = 0;
}
}
});
doc
}
+4 -2
View File
@@ -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#"<v:textbox style="layout-flow:vertical;mso-layout-flow-alt:bottom-to-top">"#).expect("the outer box's text, turned");
let rect = &xml[xml[..tb].rfind("<v:rect ").expect("its v:rect")..tb];
assert!(rect.contains("z-index:10;"), "{rect}");
// Its anchor's `relativeHeight`: the stored order, one less (see `Float::z`).
assert!(rect.contains("z-index:9;"), "{rect}");
assert!(rect.contains("visibility:hidden"), "{rect}");
}
@@ -134,7 +135,8 @@ fn behind_text_shapes_keep_their_order() {
.collect();
got.sort_unstable();
let low = -(1i64 << 31);
assert_eq!(got, vec![low + 10, low + 20, -1], "{xml}");
// The anchors' `relativeHeight`s (one less than `z`), the top one past `i32::MAX` kept at -1.
assert_eq!(got, vec![low + 9, low + 19, -1], "{xml}");
}
/// A shape VML has no simple form for stays a bare `wps:wsp`: guarded without a fallback, an old
+18 -2
View File
@@ -432,10 +432,11 @@ fn drawing_inline(doc: &Document, o: &InlineObject, opts: FlowOpts, depth: usize
}
}
/// The placement a flow keeps for a drawing: when it floats, or turns or mirrors it in line.
/// Keep placement, transforms, and explicit stacking metadata even for inline drawings.
/// Presence alone does not imply anchoring: the retained Float keeps its original wrap.
fn kept_float(f: &craft_wordmodel::para::Float) -> Option<Box<craft_wordmodel::para::Float>> {
let turned = f.rot.is_finite() && f.rot.rem_euclid(360.0) > 0.001 && f.rot.rem_euclid(360.0) < 359.999;
(f.wrap != craft_wordmodel::para::Wrap::Inline || turned || f.flip_h || f.flip_v).then(|| Box::new(f.clone()))
(f.wrap != craft_wordmodel::para::Wrap::Inline || turned || f.flip_h || f.flip_v || f.z != 0).then(|| Box::new(f.clone()))
}
fn flow_comment(doc: &Document, c: &craft_wordmodel::Comment, parent: Option<u32>, opts: FlowOpts) -> FComment {
@@ -1282,3 +1283,18 @@ impl ListCounter {
}
}
}
#[cfg(test)]
mod stacking_tests {
#[test]
fn explicit_inline_stacking_preserves_wrap_without_creating_an_anchor() {
use craft_wordmodel::para::{Float, Wrap};
let f = Float { z: u32::MAX, ..Float::default() };
assert_eq!(f.wrap, Wrap::Inline);
let kept = super::kept_float(&f).unwrap();
assert_eq!(kept.wrap, Wrap::Inline);
assert_eq!(kept.z, u32::MAX);
assert_eq!(kept.rot, f.rot);
assert!(super::kept_float(&Float::default()).is_none());
}
}
+3 -2
View File
@@ -170,8 +170,9 @@ pub struct Img {
pub w: f32,
pub h: f32,
pub alt: String,
/// How a floating picture is placed and wrapped, for formats that keep it (ODT); `None`: in
/// line with the text.
/// Placement and drawing metadata for formats that keep it (ODT). `None` is inline;
/// `Some` may also be inline, retaining rotation, mirroring or explicit stacking order.
/// The Float wrap, not the Option, determines anchoring.
pub float: Option<Box<craft_wordmodel::para::Float>>,
/// The embedded object this picture shows, kept whole (ODT only; see [`OdfObject`]).
pub object: Option<Arc<OdfObject>>,
+1
View File
@@ -1268,6 +1268,7 @@ impl Body<'_> {
self.group = Some(OpenGroup {
anchor: get(a, "text:anchor-type").map(String::from),
page: get(a, "text:anchor-page-number").map(String::from),
z: crate::text::frames::z_of(a),
style: get(a, "draw:style-name").map(String::from),
members: Vec::new(),
});
+6 -3
View File
@@ -67,6 +67,7 @@ pub(in crate::text) struct OpenGroup {
pub(in crate::text) anchor: Option<String>,
/// Its `text:anchor-page-number`.
pub(in crate::text) page: Option<String>,
pub(in crate::text) z: u32,
pub(in crate::text) style: Option<String>,
pub(in crate::text) members: Vec<model::Member>,
}
@@ -103,8 +104,9 @@ impl Body<'_> {
f.flip_v ^= v;
}
// Kept when it floats, or turns or mirrors the object in line.
let float = float
.filter(|f| f.wrap != craft_wordmodel::para::Wrap::Inline || crate::text::frames::turned(f) || f.flip_h || f.flip_v || f.y != 0.0);
let float = float.filter(|f| {
f.wrap != craft_wordmodel::para::Wrap::Inline || crate::text::frames::turned(f) || f.flip_h || f.flip_v || f.y != 0.0 || f.z != 0
});
if let Some(blocks) = &boxed
&& self.take_watermark(blocks)
{
@@ -353,7 +355,8 @@ impl Body<'_> {
let mut float =
crate::text::frames::float_of(g.anchor.as_deref(), Some(&crate::text::writer::pt(x0)), Some(&crate::text::writer::pt(y0)), &graphic);
float.page = crate::text::frames::page_of(g.anchor.as_deref(), g.page.as_deref(), &graphic);
let float = (float.wrap != craft_wordmodel::para::Wrap::Inline).then(|| Box::new(float));
float.z = g.z;
let float = (float.wrap != craft_wordmodel::para::Wrap::Inline || float.z != 0).then(|| Box::new(float));
let fit = |v: f32| if v.is_finite() { v.clamp(1.0, MAX_LEN) } else { 1.0 };
self.push_inline(Inline::Group { w: fit(x1 - x0), h: fit(y1 - y0), float, members });
}
+87 -3
View File
@@ -443,9 +443,59 @@ 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))
}
/// `float`'s place in the stacking order as `draw:z-index` (see [`z_of`]): 0 when unknown.
pub(crate) fn z_index(float: Option<&Float>) -> u32 {
float.map_or(0, |f| f.z.saturating_sub(1))
/// 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,
}
/// 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,
_ => stacking.unset,
}
}
/// Include group members and inline frames too: the ODF writer emits their orders.
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>) {
for c in children {
if let Some((_, _, f)) = c.obj.frame()
&& f.z > 0
{
orders.insert(f.z);
}
if let InlineObject::Group { children, .. } = &c.obj
&& depth < craft_flow::MAX_GROUP_DEPTH
{
members(children, depth + 1, orders);
}
}
}
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| {
for o in &p.objects {
if let Some((_, _, f)) = o.frame()
&& f.z > 0
{
orders.insert(f.z);
}
if let InlineObject::Group { children, .. } = o {
members(children, 0, &mut orders);
}
}
});
}
}
// 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
@@ -656,3 +706,37 @@ pub(super) fn frame_of(f: &Float, w: f32, h: f32) -> (&'static str, String, Stri
}
(anchor, props, xy)
}
/// Scan the same already-inflated content/style streams as the body and header readers.
/// The existing event parser supplies the same entity, malformed-input and 128-attribute
/// truncation behavior; no picture fetch or embedded-object callback is invoked.
pub(super) fn read_orders(content: &[u8], styles: Option<&[u8]>) -> Result<craft_wordmodel::para::FileOrders, String> {
use craft_wordmodel::para::FileOrderScan;
let bytes = (content.len() as u64).checked_add(styles.map_or(0, |s| s.len() as u64)).ok_or("stacking-order source size overflow")?;
let mut scan = FileOrderScan::new(bytes)?;
for xml in std::iter::once(content).chain(styles) {
super::xml::events(xml, |ev| {
if let super::xml::Ev::Start(_, attrs) = ev
&& let Some(raw) = get(&attrs, "draw:z-index").and_then(|s| s.trim().parse::<u32>().ok())
{
scan.observe(raw);
}
});
}
scan.finish()
}
#[cfg(test)]
mod file_order_tests {
#[test]
fn file_order_scan_uses_the_existing_odf_attribute_and_entity_rules() {
let ignored = format!(r#"<r {} draw:z-index="4294967295"/>"#, (0..128).map(|i| format!(r#"a{i}="x""#)).collect::<Vec<_>>().join(" "));
let map = super::read_orders(ignored.as_bytes(), None).unwrap();
assert_eq!(map.model_order(Some(u32::MAX - 1)), u32::MAX);
let custom = br#"<!DOCTYPE r [<!ENTITY rank "4294967295">]><r draw:z-index="&rank;"/>"#;
assert_eq!(super::read_orders(custom, None).unwrap().model_order(Some(u32::MAX - 1)), u32::MAX);
let numeric = br#"<r draw:z-index="429496729&#53;"><x draw:z-index="4294967294"/></r>"#;
let map = super::read_orders(numeric, None).unwrap();
assert_eq!((map.model_order(Some(u32::MAX - 1)), map.model_order(Some(u32::MAX))), (u32::MAX - 1, u32::MAX));
}
}
+7
View File
@@ -875,6 +875,12 @@ fn parse_tracked(bytes: &[u8], paper: (f32, f32)) -> Result<Parsed, String> {
None if styles_xml.is_some() => EMPTY_CONTENT.as_bytes().to_vec(),
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 {
@@ -931,6 +937,7 @@ fn parse_tracked(bytes: &[u8], paper: (f32, f32)) -> Result<Parsed, String> {
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.entries, body.manifest) = (objects.0, Some(objects.1));
body.raw = &content;
body.markup = &content;
+10 -2
View File
@@ -1058,6 +1058,7 @@ fn export_as(doc: &Document, mime: &str) -> Result<Vec<u8>, String> {
// Paragraphs kept as read name their own automatic styles: WordCraft's take another prefix.
let kept = doc.kept.of("odt");
let mut w = if kept.is_some() { Writer::with_prefix(KEPT_PREFIX) } else { Writer::default() };
w.stacking = super::frames::stacking(doc)?;
w.note_numbers = Some([false, true].map(|endnote| {
let o = doc.note_options(&doc.last_section, endnote);
(o.num_fmt, o.num_start)
@@ -1186,6 +1187,7 @@ fn export_as(doc: &Document, mime: &str) -> Result<Vec<u8>, String> {
let mut hw = Writer::with_prefix(if kept.is_some() { "wcM" } else { "M" });
// Headers' and footers' new pictures and objects skip the kept files' names too.
hw.reserved = std::sync::Arc::clone(&w.reserved);
hw.stacking = w.stacking;
hw.accents = accents;
hw.variables.clone_from(&w.variables);
let parts: Vec<PageParts> = pages.iter().map(|(_, sec)| page_parts(doc, sec, &mut hw)).collect();
@@ -1275,10 +1277,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()),
] {
+192 -1
View File
@@ -362,12 +362,65 @@ fn a_frame_keeps_its_place_in_the_stacking_order() {
let saved = crate::text::tests::export_model(&d).unwrap();
assert!(crate::text::tests::entry(&saved, "content.xml").contains(r#"draw:z-index="3""#));
assert_eq!(float(&import(&saved).unwrap()).z, 4);
// Unknown (inserted in WordCraft): at the bottom, in document order.
// Unknown (inserted in WordCraft), with nothing else stored: 0.
let d = open(r#"style:wrap="run-through""#, r#"text:anchor-type="paragraph""#);
assert_eq!(float(&d).z, 0);
assert!(crate::text::tests::entry(&crate::text::tests::export_model(&d).unwrap(), "content.xml").contains(r#"draw:z-index="0""#));
}
/// An object without a stored order (made in WordCraft) stacks above every object that has one
/// (`Float::z`), so it is written above them, not at the bottom.
#[test]
fn an_object_made_here_is_written_above_the_stored_orders() {
let png = [b"\x89PNG\r\n\x1a\n".as_slice(), &[7u8; 64]].concat();
let frame = |z: &str| {
format!(
r#"<draw:frame draw:style-name="fr1" svg:width="2in" svg:height="1in" text:anchor-type="paragraph"{z}><draw:image xlink:href="Pictures/a.png"/></draw:frame>"#
)
};
let content = format!(
r#"<office:document-content {NS_T} {NS_D}><office:automatic-styles><style:style style:name="fr1" style:family="graphic"><style:graphic-properties style:wrap="run-through"/></style:style></office:automatic-styles><office:body><office:text><text:p>{}{}Text beside.</text:p></office:text></office:body></office:document-content>"#,
frame(r#" draw:z-index="3""#),
frame("")
);
let d = import(&zip_of(&[("content.xml", content.as_bytes()), ("Pictures/a.png", &png)])).unwrap();
let zs: Vec<u32> = d.body[0]
.as_para()
.unwrap()
.objects
.iter()
.filter_map(|o| if let InlineObject::Image { float, .. } = o { Some(float.z) } else { None })
.collect();
assert_eq!(zs, [4, 0], "one stored, one made here");
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, ["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#"<office:document-content {NS_T} {NS_D}><office:body><office:text><text:p><draw:g text:anchor-type="paragraph"><draw:rect svg:width="1in" svg:height="1in" svg:x="0in" svg:y="0in"/><draw:ellipse svg:width="1in" svg:height="1in" svg:x="1in" svg:y="0in"/></draw:g>Text beside.</text:p></office:text></office:body></office:document-content>"#
);
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
@@ -456,3 +509,141 @@ fn a_frame_offering_several_formats_shows_the_first_it_can_read() {
let d = import(&zip_of(&[("content.xml", only.as_bytes()), ("Pictures/a.svm", &svm)])).unwrap();
assert_eq!(pictures(&d), 0);
}
/// Export must leave room for import's one-more encoding, including group members.
#[test]
fn extreme_stacking_orders_remain_distinct_after_repeated_roundtrips() {
for (grouped, location) in [(false, 0), (true, 0), (false, 1), (true, 1), (false, 2), (true, 2), (false, 3)] {
let shapes = r#"<draw:rect svg:width="1in" svg:height="1in"/>"#.repeat(5);
let objects = if grouped {
format!(r#"<draw:g text:anchor-type="paragraph">{shapes}</draw:g>"#)
} else {
shapes.replace("<draw:rect ", r#"<draw:rect text:anchor-type="paragraph" "#)
};
let objects = if location == 3 { objects.replacen(r#"text:anchor-type="paragraph""#, r#"text:anchor-type="as-char""#, 1) } else { objects };
let content = format!(
r#"<office:document-content {NS_T} {NS_D}><office:body><office:text><text:p>{objects}</text:p></office:text></office:body></office:document-content>"#
);
let mut d = import(&zip_of(&[("content.xml", content.as_bytes())])).unwrap();
let zs = [1, u32::MAX - 1, u32::MAX, u32::MAX, 0];
if grouped {
d.objects_mut(&|o| matches!(o, InlineObject::Group { .. }), &mut |o| {
let InlineObject::Group { float, children, .. } = o else { return };
float.z = 1;
for (child, z) in children.iter_mut().zip(zs) {
child.obj.float_mut().unwrap().z = z;
}
});
} else {
let mut orders = zs.into_iter();
d.objects_mut(&|o| o.frame().is_some(), &mut |o| {
let f = o.float_mut().unwrap();
f.z = orders.next().unwrap();
if location == 3 && f.wrap == Wrap::Inline {
f.y = 6.0;
}
});
}
let mut expected =
vec!["0".to_string(), (u32::MAX - 3).to_string(), (u32::MAX - 2).to_string(), (u32::MAX - 2).to_string(), (u32::MAX - 1).to_string()];
if grouped {
expected.insert(0, "0".into());
}
if location == 1 || location == 2 {
let blocks = std::mem::replace(&mut d.body, vec![craft_wordmodel::para_block(craft_wordmodel::Paragraph::new())]);
let kind = if location == 1 { craft_wordmodel::PartKind::Header } else { craft_wordmodel::PartKind::Footer };
d.parts.insert(1, craft_wordmodel::Part { kind, blocks, body: Default::default() });
if location == 1 {
d.last_section.headers.default = Some(1);
} else {
d.last_section.footers.default = Some(1);
}
}
for pass in 0..3 {
let bytes = crate::text::tests::export_model(&d).unwrap();
let content = crate::text::tests::entry(&bytes, if location == 1 || location == 2 { "styles.xml" } else { "content.xml" });
let actual: Vec<&str> = content.split(r#"draw:z-index=""#).skip(1).filter_map(|s| s.split('"').next()).collect();
assert_eq!(actual, expected, "grouped={grouped}, location={location}, pass={pass}: {content}");
if location == 3 {
assert!(content.contains(r#"text:anchor-type="as-char""#), "inline must remain inline");
assert!(content.contains(r#"svg:y="-78pt""#), "baseline raise must survive: {content}");
}
d = import(&bytes).unwrap();
}
}
}
#[test]
fn extreme_raw_orders_are_normalized_once_across_body_and_headers() {
let shape = |z: &str| format!(r#"<draw:rect text:anchor-type="paragraph" svg:width="1in" svg:height="1in" draw:z-index="{z}"/>"#);
let content = format!(
r#"<office:document-content {NS_T} {NS_D}><office:body><office:text><text:p>{}{}</text:p></office:text></office:body></office:document-content>"#,
shape("0"),
shape("4294967294")
);
let styles = format!(
r#"<office:document-styles {NS_T} {NS_D}><office:automatic-styles><style:page-layout style:name="pm1"><style:page-layout-properties fo:page-width="21cm" fo:page-height="29.7cm"/></style:page-layout></office:automatic-styles><office:master-styles><style:master-page style:name="Standard" style:page-layout-name="pm1"><style:header><text:p>{}{}</text:p></style:header></style:master-page></office:master-styles></office:document-styles>"#,
shape("429496729&#53;"),
shape("4294967295")
);
let mut d = import(&zip_of(&[("content.xml", content.as_bytes()), ("styles.xml", styles.as_bytes())])).unwrap();
for _ in 0..3 {
let body: 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();
let header = &d.parts[&d.last_section.headers.default.unwrap()];
let head: Vec<u32> =
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(&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]);
}
}
+2
View File
@@ -176,6 +176,8 @@ pub(super) struct Writer {
pub(super) named_text: std::collections::BTreeSet<String>,
/// Text boxes written so far (their frames' names).
boxes: usize,
/// Export stacking orders, also used by the header/footer writer.
pub(super) stacking: super::frames::Stacking,
/// Notes written so far (their ids), and footnotes and endnotes among them (their numbers).
notes: usize,
footnotes: u32,
+1 -1
View File
@@ -200,7 +200,7 @@ impl Writer {
// Always placed by a style (see `image_frame`).
let (anchor, props, xy) = placement(place, c.w, c.h);
let (anchor, style) = (anchor, format!(" draw:style-name=\"{}\"{xy}", self.graphic_style(props)));
let z = place.z_index();
let z = place.z_index(self.stacking);
out.push_str(&format!(
"<draw:frame draw:name=\"{}Chart{n}\"{style}{anchor} svg:width=\"{}\" svg:height=\"{}\" draw:z-index=\"{z}\">{inner}",
self.prefix,
+9 -7
View File
@@ -76,9 +76,10 @@ pub(in crate::text) enum Place<'a> {
}
impl<'a> Place<'a> {
/// Its place in the stacking order (§19.231 `draw:z-index`, see `frames::z_of`).
pub(in crate::text) fn z_index(self) -> u32 {
crate::text::frames::z_index(self.float())
/// Its place in the stacking order (§19.231 `draw:z-index`, see `frames::z_of`), `unset`
/// without a stored one.
pub(in crate::text) fn z_index(self, stacking: crate::text::frames::Stacking) -> u32 {
crate::text::frames::z_index(self.float(), stacking)
}
/// What turns or mirrors the object, and places it in the text.
@@ -329,7 +330,7 @@ impl Writer {
let (anchor, props, xy) = placement(place, img.w, img.h);
let (anchor, style) =
(anchor, format!(" draw:style-name=\"{}\"{xy}", self.graphic_style(format!("{}{props}{clip}", super::GRAPHICS_PARENT))));
let z = place.z_index();
let z = place.z_index(self.stacking);
out.push_str(&format!(
"<draw:frame draw:name=\"{}Image{n}\"{style}{anchor} svg:width=\"{}\" svg:height=\"{}\" draw:z-index=\"{z}\">{inner}",
self.prefix,
@@ -366,7 +367,8 @@ impl Writer {
let (anchor, props, _) = placement(Place::Text(float.filter(|f| f.wrap != craft_wordmodel::para::Wrap::Inline)), size.0, size.1);
let style = self.graphic_style(props);
self.boxes += 1;
out.push_str(&format!("<draw:g draw:name=\"{}Group{}\" draw:style-name=\"{style}\"{anchor} draw:z-index=\"0\">", self.prefix, self.boxes));
let z = crate::text::frames::z_index(float, self.stacking);
out.push_str(&format!("<draw:g draw:name=\"{}Group{}\" draw:style-name=\"{style}\"{anchor} draw:z-index=\"{z}\">", self.prefix, self.boxes));
let at = Place::Text(float).origin();
self.members(members, at, 0, out);
out.push_str("</draw:g>");
@@ -425,7 +427,7 @@ impl Writer {
.and_then(|c| c.next.as_deref())
.map(|n| format!(" draw:chain-next-name=\"{}{}\"", self.prefix, x(n)))
.unwrap_or_default();
let z = place.z_index();
let z = place.z_index(self.stacking);
out.push_str(&format!(
"<draw:frame draw:name=\"{}\" draw:style-name=\"{style}\"{anchor} svg:width=\"{}\"{height}{xy} draw:z-index=\"{z}\"><draw:text-box{min}{next}>{body}</draw:text-box></draw:frame>",
x(&name),
@@ -528,7 +530,7 @@ impl Writer {
})
.unwrap_or_default();
let conn = self.conn_attrs(kind, look);
let z = place.z_index();
let z = place.z_index(self.stacking);
out.push_str(&format!(
"<{element} draw:name=\"{}Shape{}\" draw:style-name=\"{style}\"{anchor}{at}{conn} draw:z-index=\"{z}\">{body}{geometry}</{element}>",
self.prefix, self.boxes,
+138
View File
@@ -123,3 +123,141 @@ pub(super) fn holds_words(xml: &[u8], from: usize) -> bool {
}
true
}
/// 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'/>");
}
}
+14
View File
@@ -0,0 +1,14 @@
[package]
name = "craft-score"
description = "Application-independent MusicXML and printable SVG score writers"
version.workspace = true
edition.workspace = true
license.workspace = true
rust-version.workspace = true
repository.workspace = true
[dependencies]
craft-xml.workspace = true
[lints]
workspace = true
+485
View File
@@ -0,0 +1,485 @@
#![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable)]
//! MusicXML and SVG score writing over application-independent notation data.
use std::fmt::Write as _;
const MAX_OUTPUT_BYTES: usize = 256 * 1024 * 1024; // 256 MiB
fn over_budget(buf: &str, budget: usize) -> Result<(), String> {
if buf.len() > budget {
return Err(format!("score too large: over {} bytes", budget));
}
Ok(())
}
#[derive(Debug, Clone, PartialEq)]
pub struct ScoreSetup {
/// Software name recorded in the MusicXML encoding metadata.
pub software: String,
pub title: String,
pub composer: String,
pub bars_per_system: u32,
pub show_track_names: bool,
}
/// One measure: its meter and its items (notes, chords or rests), in sixteenths.
#[derive(Debug, Clone, PartialEq)]
pub struct Measure {
pub numerator: u32,
pub denominator: u32,
pub items: Vec<Item>,
}
/// A notehead group (chord) or a rest (no pitches).
#[derive(Debug, Clone, PartialEq)]
pub struct Item {
pub pitches: Vec<u8>,
/// Duration in sixteenths; always one of the supported duration values.
pub units: u32,
/// Pitches continuing into the next item.
pub tie_start: Vec<u8>,
/// Pitches continuing from the previous item.
pub tie_stop: Vec<u8>,
}
#[derive(Debug, Clone, PartialEq)]
pub struct Part {
pub name: String,
/// True when the part reads best in bass clef.
pub bass: bool,
pub measures: Vec<Measure>,
}
fn spell(pitch: u8) -> (char, i32, i32) {
const STEPS: [(char, i32); 12] =
[('C', 0), ('C', 1), ('D', 0), ('D', 1), ('E', 0), ('F', 0), ('F', 1), ('G', 0), ('G', 1), ('A', 0), ('A', 1), ('B', 0)];
let (step, alter) = STEPS[usize::from(pitch % 12)];
(step, alter, i32::from(pitch / 12) - 1)
}
fn xml_escape(s: &str) -> String {
// Keep the score export's existing literal whitespace and quote spelling. The
// shared attribute writer owns XML legality/escaping; only its generated
// whitespace references are restored (input ampersands were escaped first).
craft_xml::esc_attr(s).replace("&#9;", "\t").replace("&#10;", "\n").replace("&#13;", "\r")
}
fn note_type(units: u32) -> (&'static str, bool) {
match units {
16 => ("whole", false),
12 => ("half", true),
8 => ("half", false),
6 => ("quarter", true),
4 => ("quarter", false),
3 => ("eighth", true),
2 => ("eighth", false),
_ => ("16th", false),
}
}
/// MusicXML 4.0 (partwise) for `parts`.
pub fn musicxml(parts: &[Part], setup: &ScoreSetup) -> Result<String, String> {
musicxml_budgeted(parts, setup, MAX_OUTPUT_BYTES)
}
/// MusicXML with an explicit byte budget, capped at 256 MiB.
pub fn musicxml_budgeted(parts: &[Part], setup: &ScoreSetup, budget: usize) -> Result<String, String> {
validate(parts, setup)?;
let budget = budget.min(MAX_OUTPUT_BYTES);
let mut x = String::new();
x.push_str("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n");
x.push_str("<!DOCTYPE score-partwise PUBLIC \"-//Recordare//DTD MusicXML 4.0 Partwise//EN\" \"http://www.musicxml.org/dtds/partwise.dtd\">\n");
x.push_str("<score-partwise version=\"4.0\">\n");
// The header (and an oversized title/composer, escaped) can itself exceed the budget even with
// no measures, so check before the per-measure loop too.
over_budget(&x, budget)?;
let _ = writeln!(x, " <work><work-title>{}</work-title></work>", xml_escape(&setup.title));
x.push_str(" <identification>\n");
if !setup.composer.is_empty() {
let _ = writeln!(x, " <creator type=\"composer\">{}</creator>", xml_escape(&setup.composer));
}
let _ = writeln!(x, " <encoding><software>{}</software></encoding>", xml_escape(&setup.software));
x.push_str(" </identification>\n <part-list>\n");
for (i, p) in parts.iter().enumerate() {
let _ = writeln!(x, " <score-part id=\"P{}\"><part-name>{}</part-name></score-part>", i + 1, xml_escape(&p.name));
}
x.push_str(" </part-list>\n");
over_budget(&x, budget)?;
for (i, p) in parts.iter().enumerate() {
let _ = writeln!(x, " <part id=\"P{}\">", i + 1);
let mut meter = (0, 0);
for (mi, m) in p.measures.iter().enumerate() {
let _ = writeln!(x, " <measure number=\"{}\">", mi + 1);
if mi == 0 || meter != (m.numerator, m.denominator) {
x.push_str(" <attributes>");
if mi == 0 {
x.push_str("<divisions>4</divisions><key><fifths>0</fifths></key>");
}
let _ = write!(x, "<time><beats>{}</beats><beat-type>{}</beat-type></time>", m.numerator, m.denominator);
if mi == 0 {
x.push_str(if p.bass { "<clef><sign>F</sign><line>4</line></clef>" } else { "<clef><sign>G</sign><line>2</line></clef>" });
}
x.push_str("</attributes>\n");
meter = (m.numerator, m.denominator);
}
for it in &m.items {
let (ty, dot) = note_type(it.units);
if it.pitches.is_empty() {
let _ = writeln!(
x,
" <note><rest/><duration>{}</duration><voice>1</voice><type>{ty}</type>{}</note>",
it.units,
if dot { "<dot/>" } else { "" }
);
continue;
}
for (k, pitch) in it.pitches.iter().enumerate() {
let (step, alter, octave) = spell(*pitch);
x.push_str(" <note>");
if k > 0 {
x.push_str("<chord/>");
}
let _ = write!(x, "<pitch><step>{step}</step>");
if alter != 0 {
let _ = write!(x, "<alter>{alter}</alter>");
}
let _ = write!(x, "<octave>{octave}</octave></pitch><duration>{}</duration>", it.units);
if it.tie_stop.contains(pitch) {
x.push_str("<tie type=\"stop\"/>");
}
if it.tie_start.contains(pitch) {
x.push_str("<tie type=\"start\"/>");
}
let _ = write!(x, "<voice>1</voice><type>{ty}</type>");
if dot {
x.push_str("<dot/>");
}
if alter != 0 {
x.push_str("<accidental>sharp</accidental>");
}
if it.tie_stop.contains(pitch) || it.tie_start.contains(pitch) {
x.push_str("<notations>");
if it.tie_stop.contains(pitch) {
x.push_str("<tied type=\"stop\"/>");
}
if it.tie_start.contains(pitch) {
x.push_str("<tied type=\"start\"/>");
}
x.push_str("</notations>");
}
x.push_str("</note>\n");
}
}
x.push_str(" </measure>\n");
over_budget(&x, budget)?;
}
x.push_str(" </part>\n");
}
x.push_str("</score-partwise>\n");
over_budget(&x, budget)?;
Ok(x)
}
/// Staff position of `pitch` in half-spaces above the bottom line (treble E4 / bass G2).
fn staff_step(pitch: u8, bass: bool) -> i32 {
let (step, _, octave) = spell(pitch);
let idx = "CDEFGAB".find(step).map_or(0, |i| i as i32);
let diatonic = octave * 7 + idx;
diatonic - if bass { 2 * 7 + 4 } else { 4 * 7 + 2 }
}
/// An engraved score as a printable SVG page (A4 portrait width, as tall as needed).
pub fn svg(parts: &[Part], setup: &ScoreSetup) -> Result<String, String> {
svg_budgeted(parts, setup, MAX_OUTPUT_BYTES)
}
/// SVG with an explicit byte budget, capped at 256 MiB.
pub fn svg_budgeted(parts: &[Part], setup: &ScoreSetup, budget: usize) -> Result<String, String> {
validate(parts, setup)?;
let budget = budget.min(MAX_OUTPUT_BYTES);
const W: f64 = 794.0;
const MARGIN: f64 = 48.0;
const SP: f64 = 7.0; // staff space
const STAFF_GAP: f64 = 70.0;
const SYSTEM_GAP: f64 = 40.0;
let n_measures = parts.iter().map(|p| p.measures.len()).max().unwrap_or(0);
// The layout range the setup offers (1 to 16): a caller's own value can't multiply the
// barlines drawn per system beyond it.
let per = setup.bars_per_system.clamp(1, 16) as usize;
let systems = n_measures.div_ceil(per).max(1);
let label_w = if setup.show_track_names { 70.0 } else { 0.0 };
let staff_x0 = MARGIN + label_w + 34.0;
let staff_x1 = W - MARGIN;
let sys_h = parts.len().max(1) as f64 * STAFF_GAP + SYSTEM_GAP;
let top = 110.0;
let h = top + systems as f64 * sys_h + MARGIN;
let mut o = String::new();
let _ =
writeln!(o, "<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"{W}\" height=\"{h:.0}\" viewBox=\"0 0 {W} {h:.0}\" font-family=\"serif\">");
let _ = writeln!(o, "<rect width=\"100%\" height=\"100%\" fill=\"white\"/>");
let _ = writeln!(o, "<text x=\"{}\" y=\"56\" font-size=\"24\" text-anchor=\"middle\">{}</text>", W / 2.0, xml_escape(&setup.title));
if !setup.composer.is_empty() {
let _ = writeln!(o, "<text x=\"{}\" y=\"84\" font-size=\"13\" text-anchor=\"end\">{}</text>", W - MARGIN, xml_escape(&setup.composer));
over_budget(&o, budget)?;
}
over_budget(&o, budget)?;
let bar_w = (staff_x1 - staff_x0) / per as f64;
for sys in 0..systems {
let y_sys = top + sys as f64 * sys_h;
for (pi, p) in parts.iter().enumerate() {
let y0 = y_sys + pi as f64 * STAFF_GAP; // top line
let bottom = y0 + 4.0 * SP;
if setup.show_track_names && sys == 0 {
let _ = writeln!(o, "<text x=\"{MARGIN}\" y=\"{:.1}\" font-size=\"11\">{}</text>", y0 + 2.0 * SP + 4.0, xml_escape(&p.name));
}
for l in 0..5 {
let y = y0 + f64::from(l) * SP;
let _ = writeln!(
o,
"<line x1=\"{:.1}\" y1=\"{y:.1}\" x2=\"{staff_x1:.1}\" y2=\"{y:.1}\" stroke=\"black\" stroke-width=\"0.8\"/>",
staff_x0 - 30.0
);
}
let clef = if p.bass { "𝄢" } else { "𝄞" };
let _ = writeln!(
o,
"<text x=\"{:.1}\" y=\"{:.1}\" font-size=\"{}\">{clef}</text>",
staff_x0 - 29.0,
bottom + if p.bass { -SP } else { SP * 0.6 },
if p.bass { 26 } else { 36 }
);
for k in 0..per {
let mi = sys * per + k;
let x0 = staff_x0 + k as f64 * bar_w;
let _ = writeln!(
o,
"<line x1=\"{:.1}\" y1=\"{y0:.1}\" x2=\"{:.1}\" y2=\"{bottom:.1}\" stroke=\"black\" stroke-width=\"1\"/>",
x0 + bar_w,
x0 + bar_w
);
let Some(m) = p.measures.get(mi) else { continue };
if pi == 0 {
let _ = writeln!(o, "<text x=\"{:.1}\" y=\"{:.1}\" font-size=\"9\">{}</text>", x0 + 2.0, y0 - 8.0, mi + 1);
}
let total: u32 = m.items.iter().map(|i| i.units).sum::<u32>().max(1);
let mut acc = 0u32;
for it in &m.items {
let x = x0 + 10.0 + (bar_w - 16.0) * f64::from(acc) / f64::from(total);
acc += it.units;
let (_, dot) = note_type(it.units);
if it.pitches.is_empty() {
rest(&mut o, x, y0, SP, it.units);
if dot {
let _ = writeln!(o, "<circle cx=\"{:.1}\" cy=\"{:.1}\" r=\"1.3\"/>", x + 7.0, y0 + 1.5 * SP);
}
continue;
}
let steps: Vec<i32> = it.pitches.iter().map(|p2| staff_step(*p2, p.bass)).collect();
let filled = it.units < 8;
for (pitch, st) in it.pitches.iter().zip(&steps) {
let y = bottom - f64::from(*st) * SP / 2.0;
// Ledger lines.
let mut l = if *st < 0 { -2 } else { 10 };
while (*st < 0 && l >= *st) || (*st > 8 && l <= *st) {
let ly = bottom - f64::from(l) * SP / 2.0;
let _ = writeln!(
o,
"<line x1=\"{:.1}\" y1=\"{ly:.1}\" x2=\"{:.1}\" y2=\"{ly:.1}\" stroke=\"black\" stroke-width=\"0.8\"/>",
x - 7.0,
x + 7.0
);
l += if *st < 0 { -2 } else { 2 };
}
let fill = if filled { "black" } else { "white" };
let _ = writeln!(
o,
"<ellipse cx=\"{x:.1}\" cy=\"{y:.1}\" rx=\"4.6\" ry=\"3.3\" transform=\"rotate(-20 {x:.1} {y:.1})\" fill=\"{fill}\" stroke=\"black\" stroke-width=\"1.1\"/>"
);
if spell(*pitch).1 != 0 {
let _ = writeln!(o, "<text x=\"{:.1}\" y=\"{:.1}\" font-size=\"12\">♯</text>", x - 14.0, y + 4.0);
}
if dot {
let _ = writeln!(o, "<circle cx=\"{:.1}\" cy=\"{:.1}\" r=\"1.3\"/>", x + 8.0, y - 1.5);
}
}
if it.units < 16 {
let (lo, hi) = (steps.iter().copied().min().unwrap_or(4), steps.iter().copied().max().unwrap_or(4));
let up = (lo + hi) < 8;
let (sx, y_from, y_to) = if up {
(x + 4.3, bottom - f64::from(lo) * SP / 2.0, bottom - f64::from(hi) * SP / 2.0 - 3.5 * SP)
} else {
(x - 4.3, bottom - f64::from(hi) * SP / 2.0, bottom - f64::from(lo) * SP / 2.0 + 3.5 * SP)
};
let _ = writeln!(
o,
"<line x1=\"{sx:.1}\" y1=\"{y_from:.1}\" x2=\"{sx:.1}\" y2=\"{y_to:.1}\" stroke=\"black\" stroke-width=\"1\"/>"
);
let flags = match it.units {
1 => 2,
2 | 3 => 1,
_ => 0,
};
for f in 0..flags {
let fy = y_to + f64::from(f) * if up { 5.0 } else { -5.0 };
let dy = if up { 9.0 } else { -9.0 };
let _ = writeln!(
o,
"<path d=\"M{sx:.1} {fy:.1} q 7 {:.1} 5 {dy:.1}\" fill=\"none\" stroke=\"black\" stroke-width=\"1.4\"/>",
dy * 0.5
);
}
}
for pitch in &it.tie_start {
let above = it.pitches.first().is_some_and(|low| pitch > low);
let direction = if above { -1.0 } else { 1.0 };
let y = bottom - f64::from(staff_step(*pitch, p.bass)) * SP / 2.0 + direction * 6.0;
let _ = writeln!(
o,
"<path d=\"M{:.1} {y:.1} q 8 {} 16 0\" fill=\"none\" stroke=\"black\" stroke-width=\"0.9\"/>",
x + 3.0,
direction * 5.0
);
}
}
// Check each measure, so a run of rest-only or missing measures that only draws
// bar lines cannot grow the output past the budget untouched.
over_budget(&o, budget)?;
}
// The staff markup (lines, clef, track names) itself grows with the part count even
// without any items, so check once per staff too.
over_budget(&o, budget)?;
}
// System bracket.
let y_last = y_sys + (parts.len().max(1) - 1) as f64 * STAFF_GAP + 4.0 * SP;
let _ = writeln!(
o,
"<line x1=\"{:.1}\" y1=\"{y_sys:.1}\" x2=\"{:.1}\" y2=\"{y_last:.1}\" stroke=\"black\" stroke-width=\"1.5\"/>",
staff_x0 - 30.0,
staff_x0 - 30.0
);
over_budget(&o, budget)?;
}
o.push_str("</svg>\n");
over_budget(&o, budget)?;
Ok(o)
}
fn rest(o: &mut String, x: f64, y0: f64, sp: f64, units: u32) {
let mid = y0 + 2.0 * sp;
match units {
16 => {
let _ = writeln!(o, "<rect x=\"{:.1}\" y=\"{:.1}\" width=\"9\" height=\"{:.1}\"/>", x - 4.5, y0 + sp, sp / 2.0);
}
8 | 12 => {
let _ = writeln!(o, "<rect x=\"{:.1}\" y=\"{:.1}\" width=\"9\" height=\"{:.1}\"/>", x - 4.5, mid - sp / 2.0, sp / 2.0);
}
4 | 6 => {
let _ = writeln!(
o,
"<path d=\"M{:.1} {:.1} l 4 5 l -4 4 l 4 5 q -6 -2 -2 5\" fill=\"none\" stroke=\"black\" stroke-width=\"1.8\"/>",
x - 2.0,
mid - 9.0
);
}
_ => {
let _ = writeln!(o, "<circle cx=\"{:.1}\" cy=\"{:.1}\" r=\"2\"/>", x - 2.0, mid - 3.0);
let _ = writeln!(
o,
"<line x1=\"{:.1}\" y1=\"{:.1}\" x2=\"{:.1}\" y2=\"{:.1}\" stroke=\"black\" stroke-width=\"1.2\"/>",
x + 2.5,
mid - 4.0,
x - 1.0,
mid + 7.0
);
if units == 1 {
let _ = writeln!(o, "<circle cx=\"{:.1}\" cy=\"{:.1}\" r=\"2\"/>", x - 3.5, mid + 2.0);
}
}
}
}
// Public notation data can come from callers other than SoundCraft's bounded layout.
// Validate before writing/escaping so malicious dimensions cannot overflow sums or create
// an unbounded temporary string before the output-budget checks run.
fn validate(parts: &[Part], setup: &ScoreSetup) -> Result<(), String> {
let invalid = || "score exceeds supported notation limits".to_string();
if parts.len() > 4096 {
return Err(invalid());
}
let mut text = setup.title.len().checked_add(setup.composer.len()).and_then(|n| n.checked_add(setup.software.len())).ok_or_else(invalid)?;
for part in parts {
text = text.checked_add(part.name.len()).ok_or_else(invalid)?;
if text > 16 * 1024 * 1024 || part.measures.len() > 2000 {
return Err(invalid());
}
for measure in &part.measures {
if measure.items.len() > 128 {
return Err(invalid());
}
for item in &measure.items {
if ![1, 2, 3, 4, 6, 8, 12, 16].contains(&item.units)
|| item.pitches.len() > 16
|| item.tie_start.len() > 16
|| item.tie_stop.len() > 16
|| item.pitches.iter().chain(&item.tie_start).chain(&item.tie_stop).any(|p| *p > 127)
{
return Err(invalid());
}
}
}
}
if text > 16 * 1024 * 1024 {
return Err(invalid());
}
Ok(())
}
#[cfg(test)]
#[allow(clippy::unwrap_used, clippy::indexing_slicing)]
mod tests {
use super::*;
fn setup() -> ScoreSetup {
ScoreSetup { software: "SoundCraft".into(), title: "A<&\u{1}".into(), composer: "B".into(), bars_per_system: 4, show_track_names: true }
}
fn parts() -> Vec<Part> {
vec![Part {
name: "Keys".into(),
bass: false,
measures: vec![Measure {
numerator: 4,
denominator: 4,
items: vec![
Item { pitches: vec![60, 64], units: 4, tie_start: vec![64], tie_stop: vec![] },
Item { pitches: vec![64], units: 12, tie_start: vec![], tie_stop: vec![64] },
],
}],
}]
}
#[test]
fn per_pitch_ties_and_escaping_survive_serialization() {
let parts = parts();
let xml = musicxml(&parts, &setup()).unwrap();
assert!(xml.contains("<work-title>A&lt;&amp;</work-title>"));
let notes: Vec<_> = xml.split("<note>").skip(1).map(|n| n.split("</note>").next().unwrap()).collect();
assert!(notes[0].contains("<duration>4</duration>"));
assert!(!notes[0].contains("<tie "));
assert!(notes[1].contains("<tie type=\"start\"/>") && notes[1].contains("<chord/>"));
assert!(notes[2].contains("<duration>12</duration>") && notes[2].contains("<tie type=\"stop\"/>"));
assert_eq!(svg(&parts, &setup()).unwrap().matches("stroke-width=\"0.9\"").count(), 1);
}
#[test]
fn byte_budgets_and_hostile_durations_fail() {
assert!(musicxml_budgeted(&parts(), &setup(), 32).is_err());
assert!(svg_budgeted(&parts(), &setup(), 32).is_err());
let mut parts = parts();
parts[0].measures[0].items[0].units = u32::MAX;
assert!(musicxml(&parts, &setup()).is_err());
assert!(svg(&parts, &setup()).is_err());
}
#[test]
fn spelling_and_staff_steps() {
assert_eq!(spell(60), ('C', 0, 4));
assert_eq!(spell(61), ('C', 1, 4));
assert_eq!(staff_step(64, false), 0); // E4: bottom treble line
assert_eq!(staff_step(77, false), 8); // F5: top treble line
assert_eq!(staff_step(43, true), 0); // G2: bottom bass line
}
}
+3
View File
@@ -13,3 +13,6 @@ and its regression test preserve NetterSilver's
`10e173c0e49696ad335e3da8a069889c089b09a6`; both contributors retain primary Git authorship
for their respective port commits. Source and destination use MIT OR Apache-2.0.
The shared adapter adds an aggregate 64 MiB stylesheet-text limit and idempotent indexing.
### Transform compatibility
`transform_compat::parse_transform` preserves EffectCraft transform-list recovery and exact composition. It retains six numeric arguments per operation; scanning is not time/byte capped and matrix overflow behavior is unchanged. The strict geometry reader is unchanged. Source: Brandon Thomas, EffectCraft94e1c86d89a866771f80ad1610afda9aa12e85d8; bounded safe argument matching by Altynzhan Karybai,4035d181329e847d7a57bf3d059502ea2c91b560.
+3
View File
@@ -35,3 +35,6 @@ pub mod write;
pub mod prefix_path;
/// Compatibility CSS subset for simple SVG importers.
pub mod simple_css;
/// Prefix-preserving SVG transform-list compatibility.
pub mod transform_compat;
+75
View File
@@ -0,0 +1,75 @@
//! Prefix-preserving transform-list compatibility for existing SVG importers.
//! At most six numbers are retained per operation; input scanning is not byte-capped.
//! Unknown/invalid-arity operations are ignored, unterminated suffixes stop parsing.
//! Matrix overflow behavior is unchanged; callers retain their geometry validation.
use crate::prefix_path as pathdata;
use kurbo::Affine;
/// Parse transforms using the established EffectCraft recovery and composition order.
pub fn parse_transform(s: &str) -> Affine {
let mut m = Affine::IDENTITY;
let mut rest = s;
while let Some(open) = rest.find('(') {
let name = rest[..open].trim().trim_start_matches(',').trim();
let Some(close) = rest[open..].find(')') else { break };
let (a, too_many) = pathdata::numbers_bounded(&rest[open + 1..open + close], 6);
rest = &rest[open + close + 1..];
let t = if too_many {
Affine::IDENTITY
} else {
match (name, a.as_slice()) {
("matrix", [a0, a1, a2, a3, a4, a5]) => Affine::new([*a0, *a1, *a2, *a3, *a4, *a5]),
("translate", [x]) => Affine::translate((*x, 0.0)),
("translate", [x, y]) => Affine::translate((*x, *y)),
("scale", [scale]) => Affine::scale(*scale),
("scale", [x, y]) => Affine::scale_non_uniform(*x, *y),
("rotate", [angle]) => Affine::rotate(angle.to_radians()),
("rotate", [angle, x, y]) => Affine::translate((*x, *y)) * Affine::rotate(angle.to_radians()) * Affine::translate((-*x, -*y)),
("skewX", [angle]) => Affine::new([1.0, 0.0, angle.to_radians().tan(), 1.0, 0.0, 0.0]),
("skewY", [angle]) => Affine::new([1.0, angle.to_radians().tan(), 0.0, 1.0, 0.0, 0.0]),
_ => Affine::IDENTITY,
}
};
m *= t;
}
m
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn operations_keep_composition_order() {
let cases = [
("matrix(1 2 3 4 5 6)", Affine::new([1., 2., 3., 4., 5., 6.])),
("translate(2)", Affine::translate((2., 0.))),
("translate(2,3)", Affine::translate((2., 3.))),
("scale(2)", Affine::scale(2.)),
("scale(2 3)", Affine::scale_non_uniform(2., 3.)),
("rotate(30)", Affine::rotate(30f64.to_radians())),
("rotate(30 2 3)", Affine::translate((2., 3.)) * Affine::rotate(30f64.to_radians()) * Affine::translate((-2., -3.))),
("skewX(30)", Affine::new([1., 0., 30f64.to_radians().tan(), 1., 0., 0.])),
("skewY(30)", Affine::new([1., 30f64.to_radians().tan(), 0., 1., 0., 0.])),
];
for (text, expected) in cases {
assert_eq!(parse_transform(text).as_coeffs(), expected.as_coeffs());
assert_eq!(parse_transform(&format!("translate(7,9), {text}")).as_coeffs(), (Affine::translate((7., 9.)) * expected).as_coeffs());
}
}
#[test]
fn malformed_and_oversized_operations_keep_accepted_prefix() {
for text in ["", "unknown(1)", "translate()", "scale(1 2 3)", "matrix(1 2 3 4 5 6 7)", "rotate(2 3)"] {
assert_eq!(parse_transform(text), Affine::IDENTITY);
}
for suffix in ["translate(", "unknown(1)", "matrix(1 2 3 4 5 6 7)"] {
assert_eq!(parse_transform(&format!("translate(3,4){suffix}")), Affine::translate((3., 4.)));
}
assert_eq!(parse_transform("unknown(1) scale(2)"), Affine::scale(2.));
assert_eq!(parse_transform("translate(3 4oops)"), Affine::translate((3., 4.)));
let huge = format!("scale({}) translate(2)", "1 ".repeat(10_000));
assert_eq!(parse_transform(&huge), Affine::translate((2., 0.)));
}
}
+3
View File
@@ -14,6 +14,8 @@ all-features = true
workspace = true
[features]
# FLV codec 3 / SWF Screen Video key frames.
screen-video = ["dep:miniz_oxide"]
# Every codec feature turns on one codec module; none is on by default.
# DNxHD / DNxHR (SMPTE ST 2019-1): decoder and encoder.
dnx = []
@@ -64,6 +66,7 @@ av1-large-encode = ["av1-encode", "dep:rav1e"]
threads = ["dep:rayon", "rav1e?/threading"]
[dependencies]
miniz_oxide = { workspace = true, optional = true }
craft-media = { workspace = true }
craft-jpeg = { workspace = true, optional = true }
thiserror = { workspace = true }
+4
View File
@@ -74,3 +74,7 @@ pub mod av1;
/// Uncompressed AVI and QuickTime pixel layouts.
pub mod raw_video;
/// Bounded Screen Video key-frame decoding (FLV codec 3).
#[cfg(feature = "screen-video")]
pub mod screen_video;
+130
View File
@@ -0,0 +1,130 @@
//! Screen Video (FLV codec 3 / SWF SCREENVIDEOPACKET) key-frame decoding.
//! The packet excludes the FLV video-tag codec byte. Blocks and rows are bottom-up;
//! output is straight RGBA8 in top-down row order. No inter-frame state or container I/O.
/// One decoded top-down RGBA8 picture.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Frame {
/// Width in pixels.
pub width: u32,
/// Height in pixels.
pub height: u32,
/// Four bytes per pixel, alpha always 255.
pub rgba: Vec<u8>,
}
/// Decodes a complete key frame within caller axis and RGBA allocation limits.
/// Returns `None` for malformed/truncated data, unchanged blocks or exceeded limits.
/// Format dimensions are at most 4095 per axis; each block inflation is capped to
/// exactly its expected BGR byte count. Legacy trailing packet bytes are tolerated.
pub fn decode_keyframe(p: &[u8], max_edge: u32, max_rgba_bytes: usize) -> Option<Frame> {
let h0 = u16::from_be_bytes([*p.first()?, *p.get(1)?]);
let h1 = u16::from_be_bytes([*p.get(2)?, *p.get(3)?]);
let (bw, w) = ((usize::from(h0 >> 12) + 1) * 16, usize::from(h0 & 0x0fff));
let (bh, h) = ((usize::from(h1 >> 12) + 1) * 16, usize::from(h1 & 0x0fff));
if w == 0 || h == 0 || w > max_edge as usize || h > max_edge as usize {
return None;
}
let bytes = w.checked_mul(h)?.checked_mul(4)?;
if bytes > max_rgba_bytes {
return None;
}
let mut rgba = vec![0u8; bytes];
let mut at = 4usize;
// Block rows from the bottom of the picture.
for by in 0..h.div_ceil(bh) {
for bx in 0..w.div_ceil(bw) {
let (x0, y0) = (bx * bw, by * bh);
let (cw, ch) = (bw.min(w - x0), bh.min(h - y0));
let size = usize::from(u16::from_be_bytes([*p.get(at)?, *p.get(at + 1)?]));
let packed = p.get(at + 2..at + 2 + size)?;
at += 2 + size;
if size == 0 {
// An unchanged block can't be in a key frame.
return None;
}
let want = cw * ch * 3;
let bgr = miniz_oxide::inflate::decompress_to_vec_zlib_with_limit(packed, want).ok()?;
if bgr.len() != want {
return None;
}
for (row, line) in bgr.chunks_exact(cw * 3).enumerate() {
// Rows count up from the bottom; the bitmap's rows count down from the top.
let y = h - 1 - (y0 + row);
for (col, px) in line.as_chunks::<3>().0.iter().enumerate() {
let i = (y * w + x0 + col) * 4;
rgba.get_mut(i..i + 4)?.copy_from_slice(&[px[2], px[1], px[0], 255]);
}
}
}
}
Some(Frame { width: w as u32, height: h as u32, rgba })
}
#[cfg(test)]
#[allow(clippy::unwrap_used, clippy::indexing_slicing)]
mod tests {
use super::*;
fn packet(w: usize, h: usize, block: usize, px: impl Fn(usize, usize) -> [u8; 3]) -> Vec<u8> {
let mut packet = Vec::new();
let n = (block / 16 - 1) as u16;
packet.extend_from_slice(&((n << 12) | w as u16).to_be_bytes());
packet.extend_from_slice(&((n << 12) | h as u16).to_be_bytes());
for by in 0..h.div_ceil(block) {
for bx in 0..w.div_ceil(block) {
let (x0, y0) = (bx * block, by * block);
let mut bgr = Vec::new();
for row in 0..block.min(h - y0) {
for col in 0..block.min(w - x0) {
let [r, g, b] = px(x0 + col, h - 1 - (y0 + row));
bgr.extend_from_slice(&[b, g, r]);
}
}
let z = miniz_oxide::deflate::compress_to_vec_zlib(&bgr, 6);
packet.extend_from_slice(&(z.len() as u16).to_be_bytes());
packet.extend_from_slice(&z);
}
}
packet
}
#[test]
fn a_screen_video_key_frame_decodes_with_its_blocks_in_place() {
// 40 × 20 in 16-pixel blocks: partial blocks on the right and at the top.
let px = |x: usize, y: usize| [(x * 6) as u8, (y * 12) as u8, if x < 20 { 255 } else { 0 }];
let Frame { width, height, rgba } = decode_keyframe(&packet(40, 20, 16, px), 8192, 64 << 20).unwrap();
assert_eq!((width, height), (40, 20));
for (x, y) in [(0, 0), (39, 0), (0, 19), (39, 19), (17, 3), (21, 18)] {
let i = (y * 40 + x) * 4;
let [r, g, b] = px(x, y);
assert_eq!(&rgba[i..i + 4], &[r, g, b, 255], "({x}, {y})");
}
}
#[test]
fn limits_truncation_and_unchanged_blocks_are_refused() {
let p = packet(40, 20, 16, |x, y| [x as u8, y as u8, 7]);
assert!(decode_keyframe(&p, 40, 40 * 20 * 4).is_some());
assert!(decode_keyframe(&p, 39, usize::MAX).is_none());
assert!(decode_keyframe(&p, 40, 40 * 20 * 4 - 1).is_none());
for cut in 0..p.len() {
let _ = decode_keyframe(&p[..cut], 40, 40 * 20 * 4);
}
let mut unchanged = p.clone();
unchanged[4..6].fill(0);
assert!(decode_keyframe(&unchanged, 40, 40 * 20 * 4).is_none());
assert!(decode_keyframe(&[0, 0, 0, 0], 40, usize::MAX).is_none());
let mut trailing = p.clone();
trailing.extend_from_slice(&[1, 2, 3]);
assert_eq!(decode_keyframe(&trailing, 40, 3200), decode_keyframe(&p, 40, 3200));
}
#[test]
fn inflated_block_must_have_exactly_its_expected_pixel_bytes() {
for length in [16 * 16 * 3 - 1, 16 * 16 * 3, 16 * 16 * 3 + 1] {
let packed = miniz_oxide::deflate::compress_to_vec_zlib(&vec![7; length], 6);
let mut p = vec![0, 16, 0, 16];
p.extend_from_slice(&(packed.len() as u16).to_be_bytes());
p.extend_from_slice(&packed);
assert_eq!(decode_keyframe(&p, 16, 1024).is_some(), length == 16 * 16 * 3);
assert!(decode_keyframe(&p[..6], 16, 1024).is_none());
}
}
}
+31
View File
@@ -725,6 +725,37 @@ impl Document {
max.saturating_add(1).max(1)
}
/// The stacking orders ([`para::Float::z`]) the floating objects of every story have, and how
/// many floating objects have none (objects made in the app). Every story saved with the document
/// counts: the body, the parts, the note separators and the glossary's building blocks (saved
/// with the same numbering, so none of their orders is left out when it is renumbered).
pub fn stacking_orders(&self) -> (std::collections::BTreeSet<u32>, usize) {
let (mut orders, mut unset) = (std::collections::BTreeSet::new(), 0usize);
let separators = [&self.footnote_separators, &self.endnote_separators]
.into_iter()
.flat_map(|s| [&s.separator, &s.continuation_separator, &s.continuation_notice])
.flatten();
let stories = std::iter::once(&self.body)
.chain(self.parts.values().map(|p| &p.blocks))
.chain(separators)
.chain(self.glossary.iter().map(|g| &g.blocks));
for blocks in stories {
for b in blocks {
crate::walk::each_para(b, 0, &mut |p| {
for (_, _, f) in p.objects.iter().filter_map(InlineObject::frame).filter(|(_, _, f)| f.wrap != para::Wrap::Inline) {
match f.z {
0 => unset = unset.saturating_add(1),
z => {
orders.insert(z);
}
}
}
});
}
}
(orders, unset)
}
/// Change every inline object in every story (paragraphs inside tables too), copying only
/// the blocks whose paragraphs `wants` says have objects to change.
pub fn objects_mut(&mut self, wants: &dyn Fn(&InlineObject) -> bool, f: &mut dyn FnMut(&mut InlineObject)) {
+2
View File
@@ -13,11 +13,13 @@ use crate::props::{CharProps, ParaProps};
use crate::section::SectionProps;
use crate::{DocError, Result};
mod file_orders;
mod float;
mod inline_kinds;
mod inline_object;
mod run;
pub use file_orders::{FileOrderScan, FileOrders};
pub use float::{AltText, Anchor, Float, FloatAlign, MAX_ALT_TEXT, Wrap, WrapText, cap_alt};
pub use inline_kinds::{NoteKind, PTabAlign, PTabBase, RubyAlign, ShapeKind, ruby_offset, ruby_raise};
pub use inline_object::{CanvasStyle, GroupChild, InlineObject, MAX_GROUP_CHILDREN};
+102
View File
@@ -0,0 +1,102 @@
//! Bounded conversion of unsigned file stacking orders to Float's zero-is-unset model.
/// A file-order mapping. Ordinary files retain their exact one-more encoding.
#[derive(Clone, Copy, Debug, Default)]
pub struct FileOrders {
gap: Option<u32>,
}
impl FileOrders {
/// A file order adjusted to leave room for its one-more model representation.
pub fn file_order(self, raw: u32) -> u32 {
raw - u32::from(self.gap.is_some_and(|gap| raw > gap))
}
/// An absent order remains unset; a present file order remains present.
pub fn model_order(self, raw: Option<u32>) -> u32 {
raw.map_or(0, |raw| self.file_order(raw).saturating_add(1))
}
}
/// Collect every raw order before decoding any. No document or callback is replayed.
///
/// The upper window has more slots than the source can contain distinct ten-digit
/// orders. Only this window can be shifted. XML callers must count all source bytes,
/// decode attributes exactly as their reader does, and never expand custom entities.
pub struct FileOrderScan {
seen: Vec<u8>,
slots: usize,
}
impl FileOrderScan {
/// Most source bytes scanned. This also caps the bitmap below 26 MiB.
pub const MAX_SOURCE_BYTES: u64 = 2 * 1024 * 1024 * 1024;
/// Allocate fallibly; reject an unbounded source rather than truncating the scan.
pub fn new(source_bytes: u64) -> Result<Self, String> {
if source_bytes > Self::MAX_SOURCE_BYTES {
return Err("stacking-order source exceeds 2 GiB".into());
}
let slots = (source_bytes / 10 + 1) as usize;
let size = slots.div_ceil(8);
let mut seen = Vec::new();
seen.try_reserve_exact(size).map_err(|_| "stacking-order bitmap allocation failed")?;
seen.resize(size, 0);
Ok(Self { seen, slots })
}
/// Observe a decoded unsigned order. Repeated values cost no extra memory.
pub fn observe(&mut self, raw: u32) {
let distance = (u32::MAX - raw) as usize;
if distance < self.slots
&& let Some(byte) = self.seen.get_mut(distance / 8)
{
*byte |= 1 << (distance % 8);
}
}
/// Finish after the complete source traversal, before constructing model floats.
pub fn finish(self) -> Result<FileOrders, String> {
if self.seen.first().is_none_or(|b| b & 1 == 0) {
return Ok(FileOrders::default());
}
for (i, byte) in self.seen.into_iter().enumerate() {
if byte == u8::MAX {
continue;
}
let distance = i * 8 + (!byte).trailing_zeros() as usize;
if distance < self.slots {
return Ok(FileOrders { gap: Some(u32::MAX - distance as u32) });
}
}
Err("stacking-order scan did not cover its source".into())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn upper_tail_is_injective_and_leaves_ordinary_orders_alone() {
let raw = [0, 1, 251658240, u32::MAX - 2, u32::MAX - 1, u32::MAX, u32::MAX];
let mut scan = FileOrderScan::new(raw.len() as u64 * 10).unwrap();
for raw in raw {
scan.observe(raw);
}
let map = scan.finish().unwrap();
assert_eq!(raw.map(|raw| map.model_order(Some(raw))), [1, 2, 251658241, u32::MAX - 2, u32::MAX - 1, u32::MAX, u32::MAX]);
assert_eq!(map.model_order(None), 0);
}
#[test]
fn no_max_means_no_renumbering_and_source_limits_fail_closed() {
let mut scan = FileOrderScan::new(100).unwrap();
scan.observe(u32::MAX - 1);
assert_eq!(scan.finish().unwrap().model_order(Some(u32::MAX - 1)), u32::MAX);
assert!(FileOrderScan::new(FileOrderScan::MAX_SOURCE_BYTES + 1).is_err());
let mut incomplete = FileOrderScan::new(0).unwrap();
incomplete.observe(u32::MAX);
assert!(incomplete.finish().is_err());
}
}
+11 -2
View File
@@ -182,8 +182,11 @@ pub struct Float {
/// off, any VML shape in a header or footer): a picture watermark in a header that starts with a table.
#[serde(skip_serializing_if = "is_false")]
pub outside_cell: bool,
/// Stacking order among floating objects (`wp:anchor/@relativeHeight`): higher is drawn on
/// top. 0 = unknown, drawn in document order (objects inserted here, or from other formats).
/// Stacking order among the floating objects of its layer, one more than the file's
/// (`wp:anchor/@relativeHeight`, ODF's `draw:z-index`), so a file's lowest, 0, is 1: a higher
/// one is drawn over a lower one, whatever their order in the text, as Word does. 0 = none
/// (objects inserted by the app, or from other formats): stacks above every object that has
/// one, in text order.
#[serde(skip_serializing_if = "is_zero_u32")]
pub z: u32,
/// The page a drawing anchored to a page is on, from 1 (ODF's `text:anchor-page-number`,
@@ -242,6 +245,12 @@ fn is_zero_u32(v: &u32) -> bool {
}
impl Float {
/// Where it stacks among the floating objects of its layer: see [`Float::z`]. None (0) is
/// past every stored order, `u32::MAX` included.
pub fn stack(&self) -> u64 {
if self.z > 0 { u64::from(self.z) } else { u64::from(u32::MAX) + 1 }
}
/// The rotation and flips, the angle normalised to `0..360` (a NaN angle is no rotation).
pub fn spin(&self) -> crate::geom::Spin {
crate::geom::Spin::new(self.rot, self.flip_h, self.flip_v)