diff --git a/docs/format/idml-values.md b/docs/format/idml-values.md index 6ed37b5..aa1d06c 100644 --- a/docs/format/idml-values.md +++ b/docs/format/idml-values.md @@ -351,36 +351,49 @@ reads from the INDD (`objects.md`, text frame preferences). ### Export options of page items -`ObjectExportOption` of page items has different attributes in -different versions. Kept for each kind of item: +`ObjectExportOption` of page items has different attributes in different +INDD versions, major and minor. Over the 92,409 page items of the +trustworthy pairs there is no exception inside a version: -| Values | Versions | +| INDD version | Attributes in addition to the common set | |---|---| -| 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 | +| 7 | no `ObjectExportOption` | +| 8.0–9.1 | `CustomImageConversion="false"`, `CustomImageSizeOption="SizeRelativeToPageWidth"` | +| 9.2 | also `UseOriginalImage="false"` | +| 10.0 | `EpubType="$ID/"`, `UseExistingImage="false"`, `CustomHeightType="DefaultHeight"`, `CustomHeight="$ID/"`, `CustomWidthType="DefaultWidth"`, `CustomWidth="$ID/"`, `CustomImageConversion="false"`, `UseOriginalImage="false"` | +| 10.1 on | `EpubType="$ID/"`, `SizeType="DefaultSize"`, `CustomSize="$ID/"`, `PreserveAppearanceFromLayout="PreserveAppearanceDefault"` | +| 21.0 on | also `EpubAriaRole="$ID/"` | +| 21.1 on | also `EpubAriaLabel="$ID/"`, `EpubAriaLabelSourceType="AutomaticARIALabel"` | +| 21.4 on | also `AIGeneratedAltText="false"`, `AltTextGenerationError="false"`, `AltTextCropSyncRect=""` | -`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 common set, on every page item from version 8 on: the alternative +text and tagging values (`AltTextSourceType`, `ActualTextSourceType`, +`CustomAltText`, `CustomActualText`, `ApplyTagType`, read from chunk +0x1E206, `objects.md`), `ImageConversionType="JPEG"`, +`ImageExportResolution="Ppi300"`, `GIFOptionsPalette="AdaptivePalette"`, +`GIFOptionsInterlaced="true"`, `JPEGOptionsQuality="High"`, +`JPEGOptionsFormat="BaselineEncoding"`, `ImageAlignment="AlignLeft"`, +`ImageSpaceBefore="0"`, `ImageSpaceAfter="0"`, +`UseImagePageBreak="false"`, `ImagePageBreak="PageBreakBefore"`, +`CustomImageAlignment="false"`, `SpaceUnit="CssPixel"`, +`CustomLayout="false"`, `CustomLayoutType="AlignmentAndSpacing"`, and +the `Properties` children `AltMetadataProperty` and +`ActualMetadataProperty` (their values also from chunk 0x1E206). These +are constant in all 93,701 elements except `ImageAlignment` (3 +`AlignCenter`) and `SizeType` (3 `RelativeToTextFlow`), all in the same 3 +items of one document; their INDD field is not identified. -Object styles other than `[None]` (2,657 `ObjectExportOption` -elements) have 22 of the same values from DOM 9 on, `EpubType` from 10 -and `EpubAriaRole` from 21; the converter writes them on those styles, -in addition to the object style values above. +Object styles, `[None]` included, have `ObjectExportOption` from version +9 on (all 1,882 styles of those versions), with the common set (all +values constant) and the same version-dependent attributes, except that +21.4 on adds only `AltTextCropSyncRect=""`. -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. +The converter writes these values by the version of the INDD file. It +leaves out `CustomImageSizeOption`: the IDML schema the output is +checked against does not allow that value. The `ObjectExportOption` +blocks of `element_values.xml`, `root_values.xml` and +`object_style_values.xml` are not used, because those files are keyed +by major version only. 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 4b16d0f..678e448 100644 --- a/docs/format/objects.md +++ b/docs/format/objects.md @@ -621,6 +621,39 @@ attribute, 625 with 1 and 30 with other values, in 200 of the pairs) and get none. The converter writes 1 for items other than groups with the style `[None]`. +## Object export options + +Page items (class 0x6201 and groups, 0x401) may have chunk 0x1E206, the +alternative text and tagging settings that IDML writes in +`ObjectExportOption`: + +| Size | Contents | IDML `ObjectExportOption` | +|---|---|---| +| 4 | u32 alternative text source: 0 `SourceCustom`, 5 `SourceXMLStructure`, 8 `SourceDecorativeImage` | `AltTextSourceType` | +| flagged string | custom alternative text | `CustomAltText` | +| flagged string | | `Properties/AltMetadataProperty NamespacePrefix` | +| flagged string | | `Properties/AltMetadataProperty PropertyPath` | +| 4 | u32 actual text source: 0 `SourceCustom`, 5 `SourceXMLStructure`, 6 `SourceXMPAltText` | `ActualTextSourceType` | +| flagged string | custom actual text | `CustomActualText` | +| flagged string ×2 | | `Properties/ActualMetadataProperty` (as above) | +| 4 | u32 tagging: 0 `TagFromStructure`, 1 `TagArtifact` | `ApplyTagType` | +| rest | 64 bytes (DOM 8–9), 83 (10–20), 88 (21) | not decoded | + +A flagged string is a u8 (1: a built-in key, written `$ID/` and the +string; 2: a plain string) and an in-object string. An empty key is +`$ID/`. + +Evidence: 69,814 page items with the chunk in the trustworthy pairs; all +eight values match in 69,814 of 69,814 (other than the default: 30 +custom and 199 decorative alternative text sources, 197 custom and 14 +XMP actual text sources, 205 artifacts, 31 custom alternative texts, 197 +custom actual texts). The 22,005 items without the chunk all have the +values `SourceXMLStructure`, `$ID/` and `TagFromStructure`. The chunk +parses in all 363,145 page items of the distinct little-endian corpus +files. The converter leaves out an attribute whose code is not in the +table. Which other attributes `ObjectExportOption` has depends on the +version (`idml-values.md`, export options of page items). + ## Text wrap Page items (class 0x6201 and groups, 0x401) and placed graphics have diff --git a/src/idml/export.rs b/src/idml/export.rs new file mode 100644 index 0000000..e3f80dc --- /dev/null +++ b/src/idml/export.rs @@ -0,0 +1,242 @@ +//! `ObjectExportOption` of page items and object styles. +//! +//! The attributes IDML writes depend on the INDD version, major and minor +//! (`docs/format/idml-values.md`, export options of page items); the +//! alternative text and tagging values come from chunk 0x1E206 of a page +//! item (`docs/format/objects.md`, object export options). + +use super::values::Node; +use crate::header::Version; +use crate::model::{ExportOptions, Name}; + +/// The values every `ObjectExportOption` has, in IDML order, after the +/// alternative text and tagging values. +const COMMON: [(&str, &str); 15] = [ + ("ImageConversionType", "JPEG"), + ("ImageExportResolution", "Ppi300"), + ("GIFOptionsPalette", "AdaptivePalette"), + ("GIFOptionsInterlaced", "true"), + ("JPEGOptionsQuality", "High"), + ("JPEGOptionsFormat", "BaselineEncoding"), + ("ImageAlignment", "AlignLeft"), + ("ImageSpaceBefore", "0"), + ("ImageSpaceAfter", "0"), + ("UseImagePageBreak", "false"), + ("ImagePageBreak", "PageBreakBefore"), + ("CustomImageAlignment", "false"), + ("SpaceUnit", "CssPixel"), + ("CustomLayout", "false"), + ("CustomLayoutType", "AlignmentAndSpacing"), +]; + +fn alt_source(code: u32) -> Option<&'static str> { + match code { + 0 => Some("SourceCustom"), + 5 => Some("SourceXMLStructure"), + 8 => Some("SourceDecorativeImage"), + _ => None, + } +} + +fn actual_source(code: u32) -> Option<&'static str> { + match code { + 0 => Some("SourceCustom"), + 5 => Some("SourceXMLStructure"), + 6 => Some("SourceXMPAltText"), + _ => None, + } +} + +fn tag_type(code: u32) -> Option<&'static str> { + match code { + 0 => Some("TagFromStructure"), + 1 => Some("TagArtifact"), + _ => None, + } +} + +/// A flagged string as IDML text: an empty key is `$ID/`. +fn text(n: &Name) -> String { + n.idml() +} + +/// The `ObjectExportOption` of a page item (`style` false) or an object +/// style, or `None` for versions whose IDML has none: page items before +/// 8, object styles before 9. `options` are the item's values from chunk +/// 0x1E206; without them the values of an item without the chunk apply. +/// Codes not identified leave their attribute out. +pub(super) fn object_export_option( + version: Version, + options: Option<&ExportOptions>, + style: bool, +) -> Option { + let v = (version.major, version.minor); + if v < (8, 0) || style && v < (9, 0) { + return None; + } + let default = ExportOptions { + alt_source: 5, + actual_source: 5, + alt_text: Name { + builtin: true, + name: String::new(), + }, + actual_text: Name { + builtin: true, + name: String::new(), + }, + ..ExportOptions::default() + }; + let o = options.unwrap_or(&default); + let mut attrs: Vec<(String, String)> = Vec::new(); + let mut add = |name: &str, value: String| attrs.push((name.into(), value)); + if let Some(s) = alt_source(o.alt_source) { + add("AltTextSourceType", s.into()); + } + if let Some(s) = actual_source(o.actual_source) { + add("ActualTextSourceType", s.into()); + } + add("CustomAltText", text(&o.alt_text)); + add("CustomActualText", text(&o.actual_text)); + if let Some(s) = tag_type(o.tag_type) { + add("ApplyTagType", s.into()); + } + // 8.0 to 10.0 have CustomImageConversion; 8 and 9 also have + // CustomImageSizeOption, which the schema the output is checked + // against does not allow (idml-values.md), so it is left out. + let old = v <= (10, 0); + if old { + add("CustomImageConversion", "false".into()); + } + for (name, value) in COMMON { + add(name, value.into()); + } + if v == (10, 0) { + for (name, value) in [ + ("EpubType", "$ID/"), + ("UseExistingImage", "false"), + ("CustomHeightType", "DefaultHeight"), + ("CustomHeight", "$ID/"), + ("CustomWidthType", "DefaultWidth"), + ("CustomWidth", "$ID/"), + ] { + add(name, value.into()); + } + } else if v >= (10, 1) { + for (name, value) in [ + ("EpubType", "$ID/"), + ("SizeType", "DefaultSize"), + ("CustomSize", "$ID/"), + ("PreserveAppearanceFromLayout", "PreserveAppearanceDefault"), + ] { + add(name, value.into()); + } + } + if v >= (21, 0) { + add("EpubAriaRole", "$ID/".into()); + } + if v >= (21, 1) { + add("EpubAriaLabel", "$ID/".into()); + add("EpubAriaLabelSourceType", "AutomaticARIALabel".into()); + } + if v >= (21, 4) { + if !style { + add("AIGeneratedAltText", "false".into()); + add("AltTextGenerationError", "false".into()); + } + add("AltTextCropSyncRect", String::new()); + } + if v >= (9, 2) && old { + add("UseOriginalImage", "false".into()); + } + let metadata = |tag: &str, m: &[Name; 2]| Node { + tag: tag.into(), + attrs: vec![ + ("NamespacePrefix".into(), text(&m[0])), + ("PropertyPath".into(), text(&m[1])), + ], + text: None, + children: Vec::new(), + }; + let key = Name { + builtin: true, + name: String::new(), + }; + let empty = [key.clone(), key]; + let (alt, actual) = match options { + Some(o) => (&o.alt_metadata, &o.actual_metadata), + None => (&empty, &empty), + }; + Some(Node { + tag: "ObjectExportOption".into(), + attrs, + text: None, + children: vec![Node { + tag: "Properties".into(), + attrs: Vec::new(), + text: None, + children: vec![ + metadata("AltMetadataProperty", alt), + metadata("ActualMetadataProperty", actual), + ], + }], + }) +} + +#[cfg(test)] +mod tests { + use super::*; + + fn version(major: u32, minor: u32) -> Version { + Version { major, minor } + } + + fn names(n: &Node) -> Vec<&str> { + n.attrs.iter().map(|(k, _)| k.as_str()).collect() + } + + #[test] + fn writes_attributes_by_version() { + assert!(object_export_option(version(7, 5), None, false).is_none()); + assert!(object_export_option(version(8, 0), None, true).is_none()); + let n = object_export_option(version(9, 2), None, true).unwrap(); + assert!(names(&n).contains(&"CustomImageConversion")); + assert_eq!(names(&n).last(), Some(&"UseOriginalImage")); + let n = object_export_option(version(10, 0), None, false).unwrap(); + assert!(names(&n).contains(&"CustomWidthType")); + assert!(!names(&n).contains(&"SizeType")); + let n = object_export_option(version(10, 1), None, false).unwrap(); + assert!(names(&n).contains(&"SizeType")); + assert!(!names(&n).contains(&"CustomImageConversion")); + assert!(!names(&n).contains(&"EpubAriaRole")); + let n = object_export_option(version(21, 4), None, true).unwrap(); + assert!(names(&n).contains(&"AltTextCropSyncRect")); + assert!(!names(&n).contains(&"AIGeneratedAltText")); + let n = object_export_option(version(21, 4), None, false).unwrap(); + assert!(names(&n).contains(&"AIGeneratedAltText")); + assert_eq!(n.attr("CustomAltText"), Some("$ID/")); + assert_eq!(n.attr("AltTextSourceType"), Some("SourceXMLStructure")); + } + + #[test] + fn writes_the_values_of_the_chunk() { + let o = ExportOptions { + alt_source: 8, + alt_text: Name { + builtin: false, + name: "A tree".into(), + }, + actual_source: 6, + tag_type: 1, + ..ExportOptions::default() + }; + let n = object_export_option(version(20, 5), Some(&o), false).unwrap(); + assert_eq!(n.attr("AltTextSourceType"), Some("SourceDecorativeImage")); + assert_eq!(n.attr("ActualTextSourceType"), Some("SourceXMPAltText")); + assert_eq!(n.attr("CustomAltText"), Some("A tree")); + assert_eq!(n.attr("ApplyTagType"), Some("TagArtifact")); + let o = ExportOptions { alt_source: 3, ..o }; + let n = object_export_option(version(20, 5), Some(&o), false).unwrap(); + assert_eq!(n.attr("AltTextSourceType"), None); + } +} diff --git a/src/idml/mod.rs b/src/idml/mod.rs index d448323..5d10ff4 100644 --- a/src/idml/mod.rs +++ b/src/idml/mod.rs @@ -9,6 +9,7 @@ mod attrs; mod designmap; +mod export; mod format; mod kind; mod pages; diff --git a/src/idml/spread.rs b/src/idml/spread.rs index beece26..f4469b3 100644 --- a/src/idml/spread.rs +++ b/src/idml/spread.rs @@ -407,20 +407,8 @@ impl Writer<'_> { if frame { self.frame_fitting(x, item); } - // 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") + if let Some(n) = export::object_export_option(self.doc.version, item.export.as_ref(), false) { - 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 { diff --git a/src/idml/styles.rs b/src/idml/styles.rs index 6c8158a..f99ff48 100644 --- a/src/idml/styles.rs +++ b/src/idml/styles.rs @@ -328,21 +328,14 @@ impl Writer<'_> { let mut node = if root { values::root_style("ObjectStyle", major) } else { - let mut n = values::object_style(major); - // The export options every object style of the version has, - // which also covers versions before 12 (idml-values.md). - if let Some(e) = values::element("ObjectStyle/ObjectExportOption", major) { - match n - .children - .iter_mut() - .find(|c| c.tag == "ObjectExportOption") - { - Some(c) => c.merge(&e), - None => n.children.push(e), - } - } - n + values::object_style(major) }; + // The export options of the version (idml-values.md) in place of + // those of the value files. + node.children.retain(|c| c.tag != "ObjectExportOption"); + if let Some(e) = export::object_export_option(self.doc.version, None, true) { + node.children.insert(0, e); + } let mut attrs = self.item_attr_values(&os.attrs); for (id, name) in [ (0x551E, "GradientFillAngle"), diff --git a/src/model/ids.rs b/src/model/ids.rs index eb7fb80..87c68c7 100644 --- a/src/model/ids.rs +++ b/src/model/ids.rs @@ -146,6 +146,8 @@ pub mod chunk { pub const ITEM_UPDATED_CHANGES: u32 = 0x21D53; /// u8 layout constraint flags. pub const ITEM_LAYOUT_CONSTRAINTS: u32 = 0x22228; + /// Export options: alternative text, actual text, tagging. + pub const ITEM_EXPORT: u32 = 0x1E206; pub const ROOT_GROUP_KIND: u32 = 0x28C2; pub const SECTION_INFO: u32 = 0x4C02; pub const DOCUMENT_PREFERENCES: u32 = 0x533; diff --git a/src/model/item.rs b/src/model/item.rs index 57fe160..2ce7fc9 100644 --- a/src/model/item.rs +++ b/src/model/item.rs @@ -81,6 +81,25 @@ pub struct PageItem { /// anchored in text. pub anchor: Option, pub props: ItemProps, + /// Export options (chunk 0x1E206); `None` without the chunk. + pub export: Option, +} + +/// Alternative text and tagging settings of a page item (chunk 0x1E206). +/// See `docs/format/objects.md`, object export options. +#[derive(Debug, Clone, PartialEq, Default)] +pub struct ExportOptions { + /// Source codes: 0 custom, 5 XML structure, 6 XMP (actual text only), + /// 8 decorative image (alternative text only). + pub alt_source: u32, + pub alt_text: Name, + /// The namespace prefix and property path of the metadata property. + pub alt_metadata: [Name; 2], + pub actual_source: u32, + pub actual_text: Name, + pub actual_metadata: [Name; 2], + /// 0 tag from structure, 1 artifact. + pub tag_type: u32, } /// Settings every page item has. See `docs/format/objects.md`, page item @@ -423,6 +442,32 @@ impl<'a> Reader<'a> { }) } + /// The export options of a page item (chunk 0x1E206): the alternative + /// text and actual text, each a u32 source and three flagged strings + /// (text, metadata namespace prefix, property path), then the u32 + /// tagging type. The rest of the chunk is not decoded. + pub(super) fn export_options(&self, uid: u32) -> Result, Error> { + let Some(d) = self.chunk(uid, chunk::ITEM_EXPORT)? else { + return Ok(None); + }; + let mut c = self.cursor(&d); + let alt_source = c.u32()?; + let alt_text = c.name()?; + let alt_metadata = [c.name()?, c.name()?]; + let actual_source = c.u32()?; + let actual_text = c.name()?; + let actual_metadata = [c.name()?, c.name()?]; + Ok(Some(ExportOptions { + alt_source, + alt_text, + alt_metadata, + actual_source, + actual_text, + actual_metadata, + tag_type: c.u32()?, + })) + } + /// Text wrap of a page item or graphic, from chunk 0x3703: u32 mode, /// u32 contour path object, four f64 offsets, u32 flags. pub(super) fn text_wrap(&self, uid: u32) -> Result, Error> { @@ -633,6 +678,10 @@ impl<'a> Reader<'a> { self.warn(format!("item {uid}: settings left out: {e}")); ItemProps::default() }), + export: self.export_options(uid).unwrap_or_else(|e| { + self.warn(format!("item {uid}: export options left out: {e}")); + None + }), })) }