diff --git a/docs/format/idml-values.md b/docs/format/idml-values.md index 4eaeef1..9f1965e 100644 --- a/docs/format/idml-values.md +++ b/docs/format/idml-values.md @@ -118,6 +118,17 @@ alignment is off in every corpus story, so the field that turns it on is not known and a document that uses it is converted without it. `AppliedTOCStyle` is left out: it names a TOC style in 4 stories. +## Graphic settings without an INDD field + +All 496 `Image` elements in the 240 corpus IDML files (119 files) have +`ClippingPathSettings` with `InvertPath="false"`, +`IncludeInsideEdges="false"`, `RestrictToFrame="false"` and +`AppliedPathName="$ID/"`, and `ImageIOPreference` with +`AllowAutoEmbedding="true"` and `AlphaChannelName="$ID/"`. The 386 PDF +and 49 EPS graphics with `ClippingPathSettings` have the same four +values. The converter writes them (`objects.md`, clipping path +settings). + ## Column direction of pages All 832 `MarginPreference` elements in the 240 corpus IDML files have diff --git a/docs/format/objects.md b/docs/format/objects.md index 0b491cd..a303f61 100644 --- a/docs/format/objects.md +++ b/docs/format/objects.md @@ -325,6 +325,88 @@ shown, so the converter writes the side and inverse settings only when the u32 at 40 is 1. Modes other than 0, 1, 3 and 6 have no IDML evidence; the converter leaves out `TextWrapPreference` for them. +## Frame fitting + +The frame fitting settings are attributes in the page item attribute list +(chunk 0x6E03, `attributes.md`). Object styles hold all of them in chunk +0x1B956 (a u16 count, then records of the same layout). + +| ID | IDML `FrameFittingOption` attribute | Encoding | +|---|---|---| +| 0x6E83 | `AutoFit` | u32, 0 in every sample (`false`) | +| 0x6E7E | `LeftCrop` | f64 | +| 0x6E7F | `TopCrop` | f64 | +| 0x6E80 | `RightCrop` | f64 | +| 0x6E81 | `BottomCrop` | f64 | +| 0x6E7C | `FittingOnEmptyFrame` | u32: 0 `None`, 1 `ContentToFrame`, 2 `Proportionally`, 3 `FillProportionally` | +| 0x6E7D | `FittingAlignment` | u32: 0 `TopLeftAnchor`, 4 `CenterAnchor` | + +Evidence, from the same-version pairs: + +- **Object styles.** All 337 object styles paired by name hold all seven + IDs in chunk 0x1B956, and all seven match the style's IDML + `FrameFittingOption` in 337 of 337 (`FittingAlignment` 4 in 313 + styles, 0 in 24). +- **Crops.** Each crop ID is told apart by non-zero values: wherever IDML + writes the attribute, the frame's own value, or its style's if the + frame has none, matches: left 429, top 397, right 433, bottom 451, no + mismatch. Non-zero values: left 235, top 204, right 239, bottom 258. +- **Fitting on an empty frame.** 301 of 301; codes 0, 1, 2 and 3 occur + (1,154, 9, 77 and 16 values). +- **Alignment.** 200 of 205. The other 5 are frames in one file with the + style `[None]`, no local value and a style value of 0, where IDML says + `CenterAnchor`; not explained. Code 0 is shown in 9 frames, code 4 in + object styles. Other codes are left out. +- **AutoFit.** 0 and `false` wherever IDML writes it (200 of 200); other + values are left out. + +**Where IDML writes it.** Only on rectangles, ovals and polygons, never +on text frames (1,154) or lines (136), even when their list has these +attributes. Let D be the item's local attributes whose value differs +from its object style's. IDML writes D if it is not empty; otherwise all +seven of the style's values, unless the style is the root `[None]`, in +which case it writes no element. This gives the IDML element exactly +for 1,849 of 1,864 frames (2,582 of 2,599 with the pairs whose IDML is +from an older version); in the other 15, IDML writes all seven values +although D is not empty. The converter follows the rule, and writes the +seven values of every object style. + +## Clipping path settings + +Images, PDF and EPS graphics may have chunk 0x2C1A (37 bytes): + +| Offset | Contents | IDML `ClippingPathSettings` | +|---|---|---| +| 0 | u32, 0 in every sample | `ClippingType="None"` | +| 4 | f64 | `Tolerance` | +| 12 | f64 | `InsetFrame` | +| 20 | u8 | `Threshold` | +| 23 | i16 | `Index` | +| 25 | u8: 2 true, 0 false | `UseHighResolutionImage` | + +Evidence: 75 of 235 images in the same-version pairs have the chunk, and +all 75 match on these fields (tolerance 2 or 0, threshold 25 or 0). The +byte at 25 changes together with the threshold and tolerance in the same +5 images, so it is identified only as the field that is left. PDF (153 +of 206) and EPS (6 of 18) graphics have the chunk with the same values as +images that use the defaults. Graphics without the chunk (160 images, 53 +PDF, 12 EPS) have `UseHighResolutionImage="true"`, `Threshold="25"`, +`Tolerance="2"`, `InsetFrame="0"` and `Index="-1"` in IDML, and the +converter writes those values for them. The converter leaves out the +element for a type other than 0. + +`InvertPath="false"`, `IncludeInsideEdges="false"`, +`RestrictToFrame="false"` and `AppliedPathName="$ID/"` have no +identified field. All 496 images, 386 PDF and 49 EPS graphics with the +element in the corpus IDML files have these values, and the converter +writes them (`idml-values.md`). + +**ImageIOPreference.** Chunk 0x8C39 of an image, u16 at 0: +`ApplyPhotoshopClippingPath` (1 true, 0 false); 235 of 235 images (293 +of 293 with the older-version pairs, 11 of them false). +`AllowAutoEmbedding="true"` and `AlphaChannelName="$ID/"` are in all 496 +images of the corpus IDML files and are written from that observation. + ## Placed graphics Images (0x1702), PDF (0x2501), EPS (0x6601) and SVG (0x6639) are children diff --git a/src/idml/mod.rs b/src/idml/mod.rs index b763811..cfea48d 100644 --- a/src/idml/mod.rs +++ b/src/idml/mod.rs @@ -26,6 +26,48 @@ enum AttrKind { } /// Codes of built-in stroke styles. See `docs/format/attributes.md`. +/// Frame fitting attributes of page items and object styles, in the +/// order IDML writes them (`docs/format/objects.md`). +const FITTING_ATTRS: [(u32, &str); 7] = [ + (0x6E83, "AutoFit"), + (0x6E7E, "LeftCrop"), + (0x6E7F, "TopCrop"), + (0x6E80, "RightCrop"), + (0x6E81, "BottomCrop"), + (0x6E7C, "FittingOnEmptyFrame"), + (0x6E7D, "FittingAlignment"), +]; + +/// The IDML value of frame fitting attribute `id`; `None` for codes +/// without evidence. +fn fitting_value(id: u32, v: &Value) -> Option { + match id { + 0x6E83 => (v.as_u32()? == 0).then(|| "false".to_string()), + 0x6E7C => match v.as_u32()? { + 0 => Some("None".into()), + 1 => Some("ContentToFrame".into()), + 2 => Some("Proportionally".into()), + 3 => Some("FillProportionally".into()), + _ => None, + }, + 0x6E7D => match v.as_u32()? { + 0 => Some("TopLeftAnchor".into()), + 4 => Some("CenterAnchor".into()), + _ => None, + }, + _ => v.as_f64().map(num), + } +} + +/// Frame fitting attributes of `attrs` with IDs in `ids`, in IDML order. +fn fitting_attrs(attrs: &Attrs, ids: &[u32]) -> Vec<(&'static str, String)> { + FITTING_ATTRS + .iter() + .filter(|(id, _)| ids.contains(id)) + .filter_map(|&(id, name)| Some((name, fitting_value(id, attrs.get(id)?)?))) + .collect() +} + const STROKE_TYPES: &[(u32, &str)] = &[ (0x5A29, "Solid"), (0x5A38, "Canned Dashed 3x2"), @@ -1822,12 +1864,26 @@ impl Writer<'_> { x.start("ObjectStyle") .attr("Self", format!("ObjectStyle/{}", self_name(&name))) .attr("Name", &name); + let all: Vec = FITTING_ATTRS.iter().map(|(id, _)| *id).collect(); + let fitting = fitting_attrs(&os.fitting, &all); if os.builtin && os.name == "[None]" && os.based_on.is_none() { - let (attrs, props, children) = self.root_values("ObjectStyle", &[], &[]); + let (attrs, props, mut children) = self.root_values("ObjectStyle", &[], &[]); for (k, v) in &attrs { x.attr(k, v); } Self::properties_with(&mut x, &[], &props); + // Values read from the INDD take precedence. + if let Some(c) = children.iter_mut().find(|c| c.tag == "FrameFittingOption") { + let rest: Vec<_> = std::mem::take(&mut c.attrs) + .into_iter() + .filter(|(k, _)| !fitting.iter().any(|(f, _)| f == k)) + .collect(); + c.attrs = fitting + .iter() + .map(|(k, v)| (k.to_string(), v.clone())) + .chain(rest) + .collect(); + } for c in &children { c.write(&mut x); } @@ -1849,6 +1905,10 @@ impl Writer<'_> { }; Self::properties(&mut x, &[prop]); } + if !(os.builtin && os.name == "[None]" && os.based_on.is_none()) && !fitting.is_empty() + { + x.empty("FrameFittingOption", &fitting); + } x.end(); } x.end(); @@ -2031,6 +2091,65 @@ impl Writer<'_> { x.end(); } + /// `ClippingPathSettings` of an image, PDF or EPS graphic, and + /// `ImageIOPreference` of an image. Values without an INDD field are + /// those every exported IDML has, and a graphic without chunk 0x2C1A + /// has the values every such graphic has in IDML. See + /// `docs/format/objects.md`. + fn clipping(x: &mut Xml, g: &Graphic) { + let (high_resolution, threshold, tolerance, inset, index) = match &g.clipping { + Some(c) if c.kind == 0 => ( + match c.high_resolution { + 2 => Some("true"), + 0 => Some("false"), + _ => None, + }, + c.threshold.to_string(), + num(c.tolerance), + num(c.inset), + c.index.to_string(), + ), + Some(_) => return, + None => ( + Some("true"), + "25".into(), + "2".into(), + "0".into(), + "-1".into(), + ), + }; + x.start("ClippingPathSettings") + .attr("ClippingType", "None") + .attr("InvertPath", "false") + .attr("IncludeInsideEdges", "false") + .attr("RestrictToFrame", "false"); + if let Some(h) = high_resolution { + x.attr("UseHighResolutionImage", h); + } + x.attr("Threshold", threshold) + .attr("Tolerance", tolerance) + .attr("InsetFrame", inset) + .attr("AppliedPathName", "$ID/") + .attr("Index", index); + x.end(); + if g.kind != GraphicKind::Image { + return; + } + x.start("ImageIOPreference"); + match g.photoshop_clipping { + Some(1) => { + x.attr("ApplyPhotoshopClippingPath", "true"); + } + Some(0) => { + x.attr("ApplyPhotoshopClippingPath", "false"); + } + _ => {} + } + x.attr("AllowAutoEmbedding", "true") + .attr("AlphaChannelName", "$ID/"); + x.end(); + } + fn placed_graphic(x: &mut Xml, g: &Graphic) { let tag = match g.kind { GraphicKind::Image => "Image", @@ -2058,6 +2177,9 @@ impl Writer<'_> { ], ); x.end(); + if g.kind != GraphicKind::Svg { + Self::clipping(x, g); + } Self::text_wrap_preference(x, g.text_wrap.as_ref(), g.contour_type); if let Some(link) = &g.link { x.empty( @@ -2131,6 +2253,12 @@ impl Writer<'_> { { Self::text_frame_preference(x, p); } + if matches!( + item.kind, + ItemKind::Shape(Shape::Rectangle | Shape::Oval | Shape::Polygon) + ) { + self.frame_fitting(x, item); + } Self::text_wrap_preference(x, item.text_wrap.as_ref(), None); transparency::write(x, &item.attrs, &uref(Some(item.uid)), &self.doc.swatches); for child in &item.children { @@ -2142,6 +2270,39 @@ impl Writer<'_> { x.end(); } + /// `FrameFittingOption` of a rectangle, oval or polygon. IDML writes + /// the local values that differ from the object style's; with none, + /// all of the style's values unless the style is the root `[None]`. + /// See `docs/format/objects.md`. + fn frame_fitting(&self, x: &mut Xml, item: &PageItem) { + let style = item + .object_style + .and_then(|u| self.doc.object_styles.get(&u)); + let differ: Vec = FITTING_ATTRS + .iter() + .map(|(id, _)| *id) + .filter(|&id| { + item.attrs + .get(id) + .is_some_and(|v| style.and_then(|s| s.fitting.get(id)) != Some(v)) + }) + .collect(); + let attrs = if !differ.is_empty() { + fitting_attrs(&item.attrs, &differ) + } else { + match style { + Some(s) if !(s.builtin && s.name == "[None]" && s.based_on.is_none()) => { + let all: Vec = FITTING_ATTRS.iter().map(|(id, _)| *id).collect(); + fitting_attrs(&s.fitting, &all) + } + _ => Vec::new(), + } + }; + if !attrs.is_empty() { + x.empty("FrameFittingOption", &attrs); + } + } + /// A ruler guide. `origin` is the top-left corner of the spread's /// pages, from which IDML measures `Location` with the ruler origin /// `SpreadOrigin`. See `docs/format/objects.md`. diff --git a/src/model/attrs.rs b/src/model/attrs.rs index 4a1b2a4..658f92e 100644 --- a/src/model/attrs.rs +++ b/src/model/attrs.rs @@ -82,6 +82,13 @@ impl Attrs { Attrs::records(&mut c, n, decode) } + /// A page item attribute list with a u16 count, as object styles hold. + pub fn parse_short(data: &[u8]) -> Result { + let mut c = Cursor::new(data); + let n = c.u16()? as usize; + Attrs::records(&mut c, n, decode) + } + /// A text attribute list: `count` records at the cursor. Text value /// types differ per attribute, so values are decoded by length. pub fn parse_text(c: &mut Cursor, count: usize) -> Result { diff --git a/src/model/mod.rs b/src/model/mod.rs index a191979..040e2a5 100644 --- a/src/model/mod.rs +++ b/src/model/mod.rs @@ -117,6 +117,10 @@ pub mod chunk { pub const SWATCH_NAME: u32 = 0x1F30; pub const TEXT_WRAP: u32 = 0x3703; pub const CONTOUR_OPTION: u32 = 0x373D; + pub const CLIPPING_PATH: u32 = 0x2C1A; + pub const PHOTOSHOP_CLIPPING: u32 = 0x8C39; + /// Frame fitting attributes of an object style (u16 count, records). + pub const OBJECT_STYLE_FITTING: u32 = 0x1B956; pub const GUIDE: u32 = 0x3308; } @@ -314,6 +318,24 @@ pub struct Graphic { pub text_wrap: Option, /// Contour type code of the text wrap (chunk 0x373D): 5 = SameAsClipping. pub contour_type: Option, + /// Clipping path settings (chunk 0x2C1A), if stored. + pub clipping: Option, + /// Chunk 0x8C39, u16 at 0: apply the Photoshop clipping path (1 true). + pub photoshop_clipping: Option, +} + +/// Clipping path settings of a graphic (chunk 0x2C1A, 37 bytes): u32 type +/// at 0, f64 tolerance at 4, f64 inset at 12, u8 threshold at 20, i16 +/// index at 23, u8 at 25 (2 = use the high-resolution image). See +/// `docs/format/objects.md`. +#[derive(Debug, Clone, PartialEq)] +pub struct ClippingPath { + pub kind: u32, + pub tolerance: f64, + pub inset: f64, + pub threshold: u8, + pub index: i16, + pub high_resolution: u8, } #[derive(Debug, Clone, PartialEq)] @@ -501,6 +523,8 @@ pub struct ObjectStyle { pub name: String, pub builtin: bool, pub based_on: Option, + /// Frame fitting attributes (chunk 0x1B956), same IDs as on page items. + pub fitting: Attrs, } /// Reads typed objects from a database, caching them. @@ -669,6 +693,10 @@ impl<'a> Reader<'a> { name, builtin, based_on: uid_or_none(based_on), + fitting: match self.chunk(uid, chunk::OBJECT_STYLE_FITTING)? { + Some(d) => Attrs::parse_short(&d).unwrap_or_default(), + None => Attrs::default(), + }, }, ); } @@ -1284,6 +1312,24 @@ impl<'a> Reader<'a> { Some(d) if d.len() >= 4 => Some(Cursor::new(&d).u32()?), _ => None, }, + clipping: match self.chunk(uid, chunk::CLIPPING_PATH)? { + Some(d) if d.len() >= 26 => { + let f = |o: usize| Cursor::new(&d[o..]).f64(); + Some(ClippingPath { + kind: Cursor::new(&d).u32()?, + tolerance: f(4)?, + inset: f(12)?, + threshold: d[20], + index: i16::from_le_bytes([d[23], d[24]]), + high_resolution: d[25], + }) + } + _ => None, + }, + photoshop_clipping: match self.chunk(uid, chunk::PHOTOSHOP_CLIPPING)? { + Some(d) if d.len() >= 2 => Some(Cursor::new(&d).u16()?), + _ => None, + }, })) }