From 3d968bea8aff63cce37b2ed7ea1f6a453fe86e64 Mon Sep 17 00:00:00 2001
From: aiongg <67543387+aiongg@users.noreply.github.com>
Date: Wed, 7 Oct 2026 10:09:02 +0000
Subject: [PATCH] feat: write the settings every page item has
Page items get Name, Visible, Locked, the interface change counts,
OverriddenPageItemProps, the layout constraints and the gradient angles
and highlights, read from their chunks and attribute lists, with the IDML
defaults where a chunk is absent. Groups take the gradient values their
children share. tools/element_values.py collects the values every IDML
of a version has on an element; the converter writes those of page items
and their export options, now for every version.
Value coverage on trustworthy pairs: 62.62 % to 80.03 %.
---
docs/format/idml-values.md | 70 ++++++-
docs/format/objects.md | 69 +++++++
src/idml/element_values.xml | 112 +++++++++++
src/idml/mod.rs | 166 +++++++++++++++-
src/idml/values.rs | 95 +++++++++-
src/idml/xml.rs | 35 ++++
src/model/mod.rs | 107 +++++++++++
tools/element_values.py | 368 ++++++++++++++++++++++++++++++++++++
8 files changed, 1003 insertions(+), 19 deletions(-)
create mode 100644 src/idml/element_values.xml
create mode 100644 tools/element_values.py
diff --git a/docs/format/idml-values.md b/docs/format/idml-values.md
index aae539f..1e36cbb 100644
--- a/docs/format/idml-values.md
+++ b/docs/format/idml-values.md
@@ -187,15 +187,69 @@ All 973 `Ink` elements of the 240 corpus IDML files have
No INDD field for them is identified; the converter writes them on every
ink (`objects.md`).
-## Export options of page items
+## Values of other elements
-All 6,151 `ObjectExportOption` elements of page items (text frames,
-rectangles, ovals, polygons, lines and groups) in the corpus IDML files
-with DOM 12 or later have the same values, which are also the values
-every object style other than `[None]` has (above). The converter writes
-that element on every page item of documents from InDesign 12 on. The 2
-elements in the DOM 7 file differ and that file has the element on few
-items, so nothing is written for older documents.
+`tools/element_values.py` collects the values of other elements the
+converter writes from every distinct IDML of the corpus (683 files
+exported by InDesign; one more IDML in the corpus lacks the `product` of
+InDesign's `aid` processing instruction and was written by a script, so
+it is left out). A value is kept for a range of DOM versions when every
+element of that kind in every file of those versions has it with the
+same value, and it is left out entirely when some version shows two
+values or elements without it. The values may differ between versions;
+each range needs 10 files. The kept values are in
+`src/idml/element_values.xml`, in blocks with `MinimumVersion` and
+`MaximumVersion`. The converter writes them after the values it reads
+from the INDD and only where the element lacks the attribute.
+
+Files per DOM version: 7: 9, 8: 67, 9: 8, 10: 52, 11: 48, 12: 39, 13: 79,
+14: 74, 15: 66, 16: 42, 17: 17, 18: 48, 19: 70, 20: 31, 21: 33.
+
+### Page items
+
+| Element | Elements | Values kept |
+|---|---|---|
+| `TextFrame` | 32,002 | `LocalDisplaySetting="Default"`, `GradientStrokeAngle`, `GradientStrokeHiliteLength`, `GradientStrokeHiliteAngle` (`0`) from DOM 7; `FlexItemWidthMode` and `FlexItemHeightMode` (`FlexFixed`) from 21 |
+| `Rectangle` | 24,314 | `LocalDisplaySetting`, `StoryTitle="$ID/"` from 7 |
+| `Oval` | 3,356 | `LocalDisplaySetting`, `StoryTitle` and the gradient highlights from 8 |
+| `Polygon` | 52,774 | `LocalDisplaySetting`, `StoryTitle` and the gradient highlights from 7 |
+| `GraphicLine` | 4,351 | `LocalDisplaySetting`, `LockState="None"` and the gradient highlights from 7 |
+| `Group` | 9,010 | `LocalDisplaySetting`, `GradientStrokeAngle` and the gradient highlights from 7 |
+
+Each kind also has `FlexItemWidthMode` and `FlexItemHeightMode` from
+DOM 21, and values that the converter reads from the INDD
+(`objects.md`, page item settings), which take precedence. Values such
+as `Name`, `Visible`, `Locked`, the gradient angles and the layout
+constraints are not kept: they differ in some elements, and those
+values come from the INDD.
+
+### Export options of page items
+
+`ObjectExportOption` of page items has different attributes in
+different versions. Kept for each kind of item:
+
+| Values | Versions |
+|---|---|
+| Image conversion, GIF, JPEG, spacing, page break, layout and metadata options (13 to 18 values, 22 for ovals) | 8 on (7 on for rectangles) |
+| `EpubType="$ID/"` | 10 on |
+| `EpubAriaRole="$ID/"` (ovals: also two ARIA label values) | 21 on |
+
+`CustomImageSizeOption="SizeRelativeToPageWidth"`, which every page item
+of DOM 8 and 9 has, is left out: the IDML schema the output is checked
+against does not allow that value. The elements are 31,857 text frames, 23,981 rectangles, 3,356 ovals,
+52,694 polygons, 4,332 lines and 8,949 groups. The alternative text and
+tagging values (`AltTextSourceType`, `ActualTextSourceType`,
+`CustomAltText`, `CustomActualText`, `ApplyTagType`) are kept only for
+ovals: in one DOM 20 document, text frames, rectangles, polygons and
+groups are tagged as artifacts with custom text. Their INDD objects hold
+these settings in chunk 0x1E206 (its first u32 is 5 in items with the
+usual values, 8 or 0 in those items), which is not decoded.
+
+The converter writes these values on every page item. From DOM 12 on it
+adds the values that every object style other than `[None]` has in the
+files the object style values were made from (above); all 6,151
+`ObjectExportOption` elements of page items in those files with DOM 12
+or later have them.
All 3,412 `InCopyExportOption` elements of rectangles, ovals and polygons
have `IncludeGraphicProxies="true"` and `IncludeAllResources="false"`,
diff --git a/docs/format/objects.md b/docs/format/objects.md
index 848d12d..4bd357b 100644
--- a/docs/format/objects.md
+++ b/docs/format/objects.md
@@ -259,6 +259,75 @@ frame list (class 0x228). Its chunk 0x205: u32 story, then a UID list of
the columns of all threaded frames in order, which gives
`PreviousTextFrame` and `NextTextFrame`.
+## Page item settings
+
+Every page item element in IDML (`TextFrame`, `Rectangle`, `Oval`,
+`Polygon`, `GraphicLine`, `Group`) has the attributes below. Each is
+stored in a chunk of the item (class 0x6201 or 0x401), or in its
+attribute list (chunk 0x6E03, `attributes.md`); without the chunk the
+IDML value is the default in the table.
+
+| Chunk | Contents | IDML | Without the chunk |
+|---|---|---|---|
+| 0x2C10 (groups: 0x418) | u8 1 if a built-in key, then an in-object string | `Name`: the string, or `$ID/` and the key | `$ID/` |
+| 0x2C32 | u16, 0 = hidden | `Visible` | `true` |
+| 0x2C2D | u32, 1 = locked | `Locked` | `false` |
+| 0x21D4E | u32 count *n*, *n* pairs of u32 | `ParentInterfaceChangeCount`: the numbers | empty |
+| 0x21D50 | same | `TargetInterfaceChangeCount` | empty |
+| 0x21D53 | same | `LastUpdatedInterfaceChangeCount` | empty |
+| 0x1424 | u32 master page item, UID list | `OverriddenPageItemProps`: the list in decimal; empty if the master item is 0 | empty |
+| 0x22228 | u8 flags | `HorizontalLayoutConstraints` (bits 4–6), `VerticalLayoutConstraints` (bits 0–2): per bit `FixedDimension` if set, else `FlexibleDimension` | as 0x22 |
+
+| Attribute | IDML | Without the attribute |
+|---|---|---|
+| 0x5520, 0x551F, 0x551E | `GradientFillStart`, `GradientFillLength`, `GradientFillAngle` | `0 0`, `0`, `0` |
+| 0x5526, 0x5525, 0x5524 | `GradientStrokeStart`, `GradientStrokeLength`, `GradientStrokeAngle` | same |
+| 0x5522, 0x5523 | `GradientFillHiliteLength`, `GradientFillHiliteAngle` | `0` |
+| 0x5528, 0x5529 | `GradientStrokeHiliteLength`, `GradientStrokeHiliteAngle` | `0` |
+
+Rules the IDML follows, over the 92,409 page item elements of the 495
+trustworthy pairs:
+
+- `Locked` is on the 36,817 items that are children of a spread or
+ anchored in text, and on none of the 55,592 items inside a group or
+ another page item.
+- The change counts, `OverriddenPageItemProps` and the layout
+ constraints are in IDML from DOM 8 on: every page item of the
+ distinct corpus IDML files from DOM 8 on has them, none of the DOM 7
+ files does (145 text frames, 335 rectangles, 61 groups).
+- A group has no gradient attributes of its own (no group in the pairs
+ has 0x551E to 0x5529). IDML gives it, for each gradient attribute, the
+ value all its child items have, and leaves the attribute out when they
+ differ: 58,068 of the 58,080 gradient values of the 5,808 groups.
+
+Evidence: the INDD values, decoded as above, against the IDML of the
+same item, over the page items of the trustworthy pairs that have an
+INDD object:
+
+| IDML attribute | Equal |
+|---|---|
+| `Name` | 92,264 of 92,402 |
+| `Visible` | 92,402 of 92,402 |
+| `Locked` | 36,810 of 36,810 |
+| The three change counts and `OverriddenPageItemProps` | 91,812 of 91,812 each |
+| Both layout constraints | 91,812 of 91,812 each |
+| Gradient start and length, fill and stroke (four attributes), items other than groups | 86,594 of 86,594 each |
+| Gradient angles and highlights (six attributes) | all, 92,396 to 92,402 per attribute (groups by the rule above) |
+
+The 138 names that differ belong to groups that are states of a button
+(their parent has class 0x1450D); they have no name chunk, and IDML
+names them `$ID/$$$/StateType/...`. The converter does not write such
+groups.
+
+Values other than the default: 206 items with a name given by the
+user, 176 hidden, 1,241 with change counts, 1,113 with overridden
+properties. Flags 0x22 are in 66,820 items, 0x55 (`FixedDimension
+FlexibleDimension FixedDimension` both ways) in 643 and 0x02
+(`FlexibleDimension` three times horizontally) in 13; with another bit
+set the converter leaves both attributes out. In 14,918 items with
+chunk 0x1424 the master item is 0; in 197 of them the list still holds
+IDs, and IDML writes the attribute empty.
+
## Text wrap
Page items (class 0x6201 and groups, 0x401) and placed graphics have
diff --git a/src/idml/element_values.xml b/src/idml/element_values.xml
new file mode 100644
index 0000000..063052c
--- /dev/null
+++ b/src/idml/element_values.xml
@@ -0,0 +1,112 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/src/idml/mod.rs b/src/idml/mod.rs
index 37236b5..75ac49a 100644
--- a/src/idml/mod.rs
+++ b/src/idml/mod.rs
@@ -457,6 +457,50 @@ const ITEM_ATTRS: &[(u32, &str, AttrKind)] = &[
AttrKind::Enum(&[(0, "CenterAlignment"), (1, "InsideAlignment")]),
),
];
+/// Gradient attributes every page item has: attribute-list ID (0 for
+/// none, `GradientStroke...` of groups), IDML name, value without the
+/// attribute. Fill and stroke start and length are in `ITEM_ATTRS`. See
+/// `docs/format/objects.md`, page item settings.
+const GRADIENT_ATTRS: &[(u32, &str, &str)] = &[
+ (0x5520, "GradientFillStart", "0 0"),
+ (0x551F, "GradientFillLength", "0"),
+ (0x551E, "GradientFillAngle", "0"),
+ (0x5526, "GradientStrokeStart", "0 0"),
+ (0x5525, "GradientStrokeLength", "0"),
+ (0x5524, "GradientStrokeAngle", "0"),
+ (0x5522, "GradientFillHiliteLength", "0"),
+ (0x5523, "GradientFillHiliteAngle", "0"),
+ (0x5528, "GradientStrokeHiliteLength", "0"),
+ (0x5529, "GradientStrokeHiliteAngle", "0"),
+];
+
+/// IDML `HorizontalLayoutConstraints` and `VerticalLayoutConstraints` of
+/// layout constraint flags (chunk 0x22228): bits 4 to 6 and 0 to 2, each
+/// set for `FixedDimension`. `None` if another bit is set.
+fn layout_constraints(flags: u8) -> Option<(String, String)> {
+ if flags & 0x88 != 0 {
+ return None;
+ }
+ let side = |bits: u8| {
+ (0..3)
+ .map(|i| {
+ if bits & (1 << i) != 0 {
+ "FixedDimension"
+ } else {
+ "FlexibleDimension"
+ }
+ })
+ .collect::>()
+ .join(" ")
+ };
+ Some((side(flags >> 4), side(flags & 7)))
+}
+
+/// Numbers separated by spaces.
+fn join_numbers(v: &[u32]) -> String {
+ v.iter().map(u32::to_string).collect::>().join(" ")
+}
+
/// Kinds of cell attribute values.
#[derive(Clone, Copy)]
enum CellKind {
@@ -900,8 +944,13 @@ struct Writer<'a> {
group_path: std::collections::HashMap>,
/// Values left out while writing, reported with the model's warnings.
warnings: std::cell::RefCell>,
+ /// Observed attributes by element path (`values::element_attrs`).
+ observed: std::cell::RefCell>,
}
+/// Attributes observed on an element path (`values::element_attrs`).
+type Observed = std::rc::Rc>;
+
fn group_paths(doc: &Document) -> std::collections::HashMap> {
fn walk(
doc: &Document,
@@ -927,6 +976,18 @@ fn group_paths(doc: &Document) -> std::collections::HashMap> {
}
impl Writer<'_> {
+ /// The attributes every IDML of the document's version has on the
+ /// elements on `path` (`docs/format/idml-values.md`).
+ fn observed(&self, path: &str) -> Observed {
+ self.observed
+ .borrow_mut()
+ .entry(path.to_string())
+ .or_insert_with(|| {
+ std::rc::Rc::new(values::element_attrs(path, self.doc.version.major))
+ })
+ .clone()
+ }
+
fn package_root(&self, x: &mut Xml, kind: &str) {
x.start(&format!("idPkg:{kind}"))
.attr("xmlns:idPkg", PACKAGING_NS)
@@ -2735,7 +2796,82 @@ impl Writer<'_> {
x.end();
}
- fn page_item(&self, x: &mut Xml, item: &PageItem) {
+ /// The gradient attributes of a page item: its own, or, for a group,
+ /// the values all its children have. See `docs/format/objects.md`.
+ fn gradients(item: &PageItem) -> Vec<(&'static str, String)> {
+ if item.kind == ItemKind::Group {
+ let kids: Vec<_> = item.children.iter().map(Self::gradients).collect();
+ return GRADIENT_ATTRS
+ .iter()
+ .filter_map(|&(_, name, _)| {
+ let mut values = kids
+ .iter()
+ .map(|k| k.iter().find(|(n, _)| *n == name).map(|(_, v)| v));
+ let first = values.next()??;
+ values
+ .all(|v| v == Some(first))
+ .then(|| (name, first.clone()))
+ })
+ .collect();
+ }
+ GRADIENT_ATTRS
+ .iter()
+ .map(|&(id, name, default)| {
+ let v = match item.attrs.get(id) {
+ Some(Value::Point(x, y)) => Some(nums(&[*x, *y])),
+ Some(v) => v.as_f64().map(num),
+ None => None,
+ };
+ (name, v.unwrap_or_else(|| default.to_string()))
+ })
+ .collect()
+ }
+
+ /// The name, visibility, lock and the other settings every page item
+ /// has; `nested` for an item inside another page item, which IDML
+ /// writes without `Locked`. See `docs/format/objects.md`.
+ fn item_settings(&self, x: &mut Xml, item: &PageItem, nested: bool) {
+ let p = &item.props;
+ let name = match &p.name {
+ Some(n) if n.builtin => format!("$ID/{}", n.name),
+ Some(n) => n.name.clone(),
+ None => "$ID/".into(),
+ };
+ x.attr("Name", name)
+ .attr("Visible", (!p.hidden).to_string());
+ if !nested {
+ x.attr("Locked", p.locked.to_string());
+ }
+ if self.doc.version.major >= 8 {
+ for (name, counts) in [
+ "ParentInterfaceChangeCount",
+ "TargetInterfaceChangeCount",
+ "LastUpdatedInterfaceChangeCount",
+ ]
+ .into_iter()
+ .zip(&p.change_counts)
+ {
+ x.attr(name, join_numbers(counts));
+ }
+ let overridden = match &p.overridden {
+ Some((master, ids)) if *master != 0 => join_numbers(ids),
+ _ => String::new(),
+ };
+ x.attr("OverriddenPageItemProps", overridden);
+ if let Some((h, v)) = layout_constraints(p.layout_constraints.unwrap_or(0x22)) {
+ x.attr("HorizontalLayoutConstraints", h)
+ .attr("VerticalLayoutConstraints", v);
+ }
+ }
+ for (name, v) in Self::gradients(item) {
+ if !x.has_attr(name) {
+ x.attr(name, v);
+ }
+ }
+ }
+
+ /// `nested` for an item inside another page item.
+ fn page_item(&self, x: &mut Xml, item: &PageItem, nested: bool) {
let tag = match &item.kind {
ItemKind::TextFrame { .. } => "TextFrame",
ItemKind::Group => "Group",
@@ -2783,6 +2919,8 @@ impl Writer<'_> {
x.attr("ItemLayer", uref(Some(layer)));
}
x.attr("ItemTransform", matrix(&item.transform));
+ self.item_settings(x, item, nested);
+ x.attrs_missing(self.observed(tag).iter());
Self::path_geometry(x, &item.paths);
if let ItemKind::TextFrame {
preferences: Some(p),
@@ -2798,12 +2936,20 @@ impl Writer<'_> {
if frame {
self.frame_fitting(x, item);
}
- // Every page item of IDML from DOM 12 on has the same export
- // options as the object styles (idml-values.md).
- if self.doc.version.major >= 12
- && let Some(n) =
- values::object_style(self.doc.version.major).child("ObjectExportOption")
+ // Export options every IDML of the version has on this kind of
+ // item; from DOM 12 on, also those of the object styles
+ // (idml-values.md).
+ let major = self.doc.version.major;
+ let mut export = values::element(&format!("{tag}/ObjectExportOption"), major);
+ if major >= 12
+ && let Some(n) = values::object_style(major).child("ObjectExportOption")
{
+ match &mut export {
+ Some(e) => e.merge(n),
+ None => export = Some(n.clone()),
+ }
+ }
+ if let Some(n) = export {
n.write(x);
}
if let Some(d) = &item.anchor {
@@ -2838,7 +2984,7 @@ impl Writer<'_> {
));
}
for child in &item.children {
- self.page_item(x, child);
+ self.page_item(x, child, true);
}
for g in &item.graphics {
Self::placed_graphic(x, g);
@@ -3026,7 +3172,7 @@ impl Writer<'_> {
x.end();
}
for item in &s.items {
- self.page_item(&mut x, item);
+ self.page_item(&mut x, item, false);
}
x.end();
x.finish()
@@ -3291,7 +3437,7 @@ impl Writer<'_> {
'\u{FFFC}' if story.anchors.contains_key(&pos) => {
flush(x, &mut buf);
for item in &story.anchors[&pos] {
- self.page_item(x, item);
+ self.page_item(x, item, false);
}
}
'\u{16}' if story.tables.contains_key(&pos) => {
@@ -3457,6 +3603,7 @@ pub fn write(doc: &Document, name: &str, out: impl std::io::Write) -> std::io::R
name: name.to_string(),
group_path: group_paths(doc),
warnings: Default::default(),
+ observed: Default::default(),
};
let mut files: BTreeMap = BTreeMap::new();
files.insert(
@@ -3559,6 +3706,7 @@ mod tests {
name: String::new(),
group_path: Default::default(),
warnings: Default::default(),
+ observed: Default::default(),
};
let out: String = w
.backing_story()
diff --git a/src/idml/values.rs b/src/idml/values.rs
index e29de47..2b0b523 100644
--- a/src/idml/values.rs
+++ b/src/idml/values.rs
@@ -1,7 +1,9 @@
//! Values written from IDML observation: what every InDesign-exported IDML
//! in the corpus has on the root styles (`root_values.xml`) and in its
//! preferences (`preference_values.xml`), both generated by
-//! `tools/root_values.py`. See `docs/format/idml-values.md`.
+//! `tools/root_values.py`, and on the other elements the converter writes
+//! (`element_values.xml`, generated by `tools/element_values.py`). See
+//! `docs/format/idml-values.md`.
use super::xml::Xml;
@@ -60,7 +62,7 @@ impl Node {
/// Add the attributes and children of `other` that `self` lacks.
/// Children with the same tag are merged.
- fn merge(&mut self, other: &Node) {
+ pub fn merge(&mut self, other: &Node) {
for (k, v) in &other.attrs {
if self.attr(k).is_none() {
self.attrs.push((k.clone(), v.clone()));
@@ -169,6 +171,77 @@ pub fn preferences(major: u32) -> Vec {
out
}
+const ELEMENT_VALUES: &str = include_str!("element_values.xml");
+
+fn element_values() -> &'static Node {
+ static PARSED: std::sync::OnceLock = std::sync::OnceLock::new();
+ PARSED.get_or_init(|| parse(ELEMENT_VALUES).expect("element_values.xml is well-formed"))
+}
+
+/// Whether a block of `element_values.xml` applies to InDesign version
+/// `major`.
+fn applies(block: &Node, major: u32) -> bool {
+ let bound = |name: &str| {
+ block
+ .attr(name)
+ .map(|v| v.parse::().expect("version bounds are numbers"))
+ };
+ bound("MinimumVersion").is_none_or(|v| v <= major)
+ && bound("MaximumVersion").is_none_or(|v| major <= v)
+}
+
+/// The observed values of the elements on `path` (a tag, or a tag and a
+/// child tag such as `Spread/FlattenerPreference`) for a document of
+/// InDesign version `major`, from `element_values.xml`. `None` if the
+/// file has no values for it.
+pub fn element(path: &str, major: u32) -> Option {
+ let (parent, child) = match path.split_once('/') {
+ Some((p, c)) => (p, Some(c)),
+ None => (path, None),
+ };
+ let mut out: Option = None;
+ for block in element_values()
+ .children
+ .iter()
+ .filter(|b| b.tag == "Values")
+ {
+ if !applies(block, major) {
+ continue;
+ }
+ let Some(mut n) = block.child(parent) else {
+ continue;
+ };
+ if let Some(c) = child {
+ match n.child(c) {
+ Some(cn) => n = cn,
+ None => continue,
+ }
+ }
+ let mut n = n.clone();
+ if child.is_none() {
+ // Children are other paths.
+ n.children.retain(|c| c.tag == "Properties");
+ }
+ match &mut out {
+ Some(o) => o.merge(&n),
+ None => out = Some(n),
+ }
+ }
+ out
+}
+
+/// The observed attributes of the elements on `path`, without `Present`.
+pub fn element_attrs(path: &str, major: u32) -> Vec<(String, String)> {
+ element(path, major)
+ .map(|n| {
+ n.attrs
+ .into_iter()
+ .filter(|(k, _)| k != "Present")
+ .collect()
+ })
+ .unwrap_or_default()
+}
+
fn unescape(s: &str) -> String {
s.replace("<", "<")
.replace(">", ">")
@@ -294,6 +367,24 @@ mod tests {
assert_eq!(wrap.children[0].tag, "Properties");
}
+ #[test]
+ fn gates_element_values_by_version() {
+ let old = element("TextFrame", 7).unwrap();
+ assert_eq!(old.attr("LocalDisplaySetting"), Some("Default"));
+ // Values of child paths are not attributes of the parent.
+ assert!(old.child("ObjectExportOption").is_none());
+ let new = element("Polygon/ObjectExportOption", 21).unwrap();
+ assert!(new.child("Properties").is_some());
+ // Export options differ between versions.
+ assert!(new.attr("EpubType").is_some());
+ assert!(
+ element("Polygon/ObjectExportOption", 8)
+ .unwrap()
+ .attr("EpubType")
+ .is_none()
+ );
+ }
+
#[test]
fn gates_preferences_by_version() {
let old = preferences(7);
diff --git a/src/idml/xml.rs b/src/idml/xml.rs
index cbbc5ce..b25dcfa 100644
--- a/src/idml/xml.rs
+++ b/src/idml/xml.rs
@@ -9,6 +9,8 @@ pub struct Xml {
open: bool,
/// The current element has text content, so its end tag is not indented.
inline: bool,
+ /// Names of the attributes written in the open start tag.
+ written: Vec,
}
/// Whether XML 1.0 allows `c` (control characters aside): not U+FFFE or
@@ -48,6 +50,7 @@ impl Xml {
stack: Vec::new(),
open: false,
inline: false,
+ written: Vec::new(),
}
}
@@ -85,12 +88,32 @@ impl Xml {
self.stack.push(name.to_string());
self.open = true;
self.inline = false;
+ self.written.clear();
self
}
pub fn attr(&mut self, name: &str, value: impl AsRef) -> &mut Self {
debug_assert!(self.open, "attribute outside a start tag");
let _ = write!(self.out, " {name}=\"{}\"", escape_attr(value.as_ref()));
+ self.written.push(name.to_string());
+ self
+ }
+
+ /// Whether the open start tag has attribute `name`.
+ pub fn has_attr(&self, name: &str) -> bool {
+ self.open && self.written.iter().any(|w| w == name)
+ }
+
+ /// Write the attributes the open start tag does not have yet.
+ pub fn attrs_missing<'a>(
+ &mut self,
+ attrs: impl IntoIterator,
+ ) -> &mut Self {
+ for (k, v) in attrs {
+ if !self.has_attr(k) {
+ self.attr(k, v);
+ }
+ }
self
}
@@ -181,6 +204,18 @@ mod tests {
);
}
+ #[test]
+ fn adds_only_missing_attributes() {
+ let mut x = Xml::new();
+ x.start("a").attr("k", "1");
+ let more = [
+ ("k".to_string(), "2".to_string()),
+ ("m".to_string(), "3".to_string()),
+ ];
+ x.attrs_missing(&more).end();
+ assert!(x.finish().ends_with(""));
+ }
+
#[test]
fn control_characters_become_processing_instructions() {
let mut x = Xml::new();
diff --git a/src/model/mod.rs b/src/model/mod.rs
index a2c9802..76cd1a6 100644
--- a/src/model/mod.rs
+++ b/src/model/mod.rs
@@ -112,6 +112,23 @@ pub mod chunk {
pub const OBJECT_STYLE_INFO: u32 = 0x1B907;
pub const OBJECT_STYLE_ROOT_CHILDREN: u32 = 0x1B95A;
pub const ITEM_OBJECT_STYLE: u32 = 0x1B916;
+ /// Page item name: u8 1 if a built-in key, then an in-object string.
+ pub const ITEM_NAME: u32 = 0x2C10;
+ /// The same for a group.
+ pub const GROUP_NAME: u32 = 0x418;
+ /// u32 1 if the page item is locked.
+ pub const ITEM_LOCKED: u32 = 0x2C2D;
+ /// u16 0 if the page item is hidden.
+ pub const ITEM_VISIBLE: u32 = 0x2C32;
+ /// Override of a master page item: u32 master item, then a list of
+ /// attribute IDs.
+ pub const ITEM_OVERRIDE: u32 = 0x1424;
+ /// Interface change counts: u32 count *n*, then *n* pairs of u32.
+ pub const ITEM_PARENT_CHANGES: u32 = 0x21D4E;
+ pub const ITEM_TARGET_CHANGES: u32 = 0x21D50;
+ pub const ITEM_UPDATED_CHANGES: u32 = 0x21D53;
+ /// u8 layout constraint flags.
+ pub const ITEM_LAYOUT_CONSTRAINTS: u32 = 0x22228;
pub const ROOT_GROUP_KIND: u32 = 0x28C2;
pub const SECTION_INFO: u32 = 0x4C02;
pub const DOCUMENT_PREFERENCES: u32 = 0x533;
@@ -291,6 +308,35 @@ pub struct PageItem {
/// Anchored object settings (chunk 0x2800 of the anchor), for an item
/// anchored in text.
pub anchor: Option>,
+ pub props: ItemProps,
+}
+
+/// Settings every page item has. See `docs/format/objects.md`, page item
+/// settings.
+#[derive(Debug, Clone, PartialEq, Default)]
+pub struct ItemProps {
+ /// The name (chunk 0x2C10, of a group 0x418); `None` without the
+ /// chunk.
+ pub name: Option,
+ /// Chunk 0x2C32 is 0.
+ pub hidden: bool,
+ /// Chunk 0x2C2D is 1.
+ pub locked: bool,
+ /// Layout constraint flags (chunk 0x22228).
+ pub layout_constraints: Option,
+ /// The parent, target and last updated interface change counts
+ /// (chunks 0x21D4E, 0x21D50, 0x21D53), each a list of numbers.
+ pub change_counts: [Vec; 3],
+ /// The overridden master page item (0 for none) and the IDs of the
+ /// attributes overridden (chunk 0x1424).
+ pub overridden: Option<(u32, Vec)>,
+}
+
+/// A page item name: a built-in key or a name given by the user.
+#[derive(Debug, Clone, PartialEq)]
+pub struct ItemName {
+ pub builtin: bool,
+ pub name: String,
}
/// Text wrap settings of a page item or graphic (chunk 0x3703).
@@ -1583,6 +1629,63 @@ impl<'a> Reader<'a> {
})
}
+ /// The settings every page item has (`ItemProps`).
+ fn item_props(&self, uid: u32) -> Result {
+ let name_chunk = if self.class(uid) == Some(class::GROUP) {
+ chunk::GROUP_NAME
+ } else {
+ chunk::ITEM_NAME
+ };
+ let name = match self.chunk(uid, name_chunk)? {
+ Some(d) => {
+ let mut c = Cursor::new(&d);
+ let builtin = c.flag()? == 1;
+ Some(ItemName {
+ builtin,
+ name: c.string()?,
+ })
+ }
+ None => None,
+ };
+ let counts = |id: u32| -> Result, Error> {
+ let Some(d) = self.chunk(uid, id)? else {
+ return Ok(Vec::new());
+ };
+ let mut c = Cursor::new(&d);
+ let n = c.u32()? as usize;
+ if n > d.len() / 8 {
+ return Err(Error::Corrupt(format!("item {uid}: {n} change counts")));
+ }
+ (0..2 * n).map(|_| c.u32()).collect()
+ };
+ let overridden = match self.chunk(uid, chunk::ITEM_OVERRIDE)? {
+ Some(d) => {
+ let mut c = Cursor::new(&d);
+ let master = c.u32()?;
+ Some((master, c.u32_list()?))
+ }
+ None => None,
+ };
+ Ok(ItemProps {
+ name,
+ hidden: self
+ .chunk(uid, chunk::ITEM_VISIBLE)?
+ .is_some_and(|d| d.len() >= 2 && Cursor::new(&d).u16().ok() == Some(0)),
+ locked: self
+ .chunk(uid, chunk::ITEM_LOCKED)?
+ .is_some_and(|d| d.len() >= 4 && Cursor::new(&d).u32().ok() == Some(1)),
+ layout_constraints: self
+ .chunk(uid, chunk::ITEM_LAYOUT_CONSTRAINTS)?
+ .and_then(|d| d.first().copied()),
+ change_counts: [
+ counts(chunk::ITEM_PARENT_CHANGES)?,
+ counts(chunk::ITEM_TARGET_CHANGES)?,
+ counts(chunk::ITEM_UPDATED_CHANGES)?,
+ ],
+ overridden,
+ })
+ }
+
/// Text wrap of a page item or graphic, from chunk 0x3703: u32 mode,
/// u32 contour path object, four f64 offsets, u32 flags.
fn text_wrap(&self, uid: u32) -> Result