diff --git a/docs/format/objects.md b/docs/format/objects.md index 0caecdd..e4eb473 100644 --- a/docs/format/objects.md +++ b/docs/format/objects.md @@ -412,11 +412,15 @@ other colours, as a list of three numbers (0 to 255): | 0.6, 0, 0 | `BrickRed` | 0, 0, 0.53 | `DarkBlue` | | 0.6, 0.8, 0 | `GrassGreen` | 0.81, 0.51, 0.71 | `Lipstick` | | 1, 0.71, 0.42 | `GridOrange` | 0.97, 0.35, 0.42 | `Fiesta` | -| 0, 0.6, 0.6 | `Teal` | | | +| 0, 0.6, 0.6 | `Teal` | 0.73, 0.73, 0.73 | `LightGray` | +| 0.48, 0.73, 0.85 | `GridBlue` | 1, 0.6, 0.6 | `Peach` | +| 0.6, 0, 0.2 | `Burgundy` | | | Evidence: in the layers and pages of the trustworthy pairs every colour object with these components has this name (1,107 layers and pages; from -491 `LightBlue` down to 1 `Teal`). Two other colours occur, 1, 0.4863, +491 `LightBlue` down to 1 `Teal`). The last four are from the guide, +grid and pasteboard colours of the preferences (`preferences.md`), where +every colour with these components has this name. Two other colours occur, 1, 0.4863, 0.651 (3 layers) and 0.9412 three times (1 layer); IDML writes them as the lists 255, 124, 166 and 240, 240, 240, the components times 255. The converter writes such a list for a colour whose components are whole diff --git a/docs/format/preferences.md b/docs/format/preferences.md index 2a47b06..dbb5ca0 100644 --- a/docs/format/preferences.md +++ b/docs/format/preferences.md @@ -171,7 +171,11 @@ Chunk 0x28BE (14 bytes, 16 from InDesign 18), u8 flags: 4 620 files with the chunk (283 of 283 for the last). The 34 files without it have `SmartTextReflow="false"` and `LimitToMasterTextFrames="true"`. -Chunk 0x3768 (4 bytes), u8 at 2: `ZOrderTextWrap`; 654 of 654. +Chunk 0x3768 (4 or 6 bytes): u8 at 0 `AbutTextToTextWrap`, u8 at 2 +`ZOrderTextWrap`, and in 6-byte chunks u8 at 4 +`HonourTextIndentsWithTextWrap` (IDML has it only for those). Evidence: +`ZOrderTextWrap` 654 of 654; the other two 489 of 489 and 139 of 139 +trustworthy pairs. ## Margins and columns of new pages (`MarginPreference`) @@ -486,3 +490,56 @@ Without the chunk (469 of 469): - `Version` is `Epub2` for INDD files up to version 18.0 and `Epub3` from 18.1 (the 5 DOM 18.0 pairs whose INDD is 18.0 have `Epub2`, the 13 whose INDD is 18.1 to 18.4 have `Epub3`). + +## Default styles, grids and other settings + +Chunks of the preferences object, trustworthy pairs. A colour is the UID +of an interface colour (`objects.md`, interface colours). + +**Default styles.** Each chunk is a list of u32 UIDs; the third is the +default: + +| Chunk | u32 at 8 | u32 at 12 | u32 at 16 | +|---|---|---|---| +| 0x28D4 (12 bytes) | `TextDefault` `AppliedParagraphStyle` | | | +| 0x28D5 (12 bytes) | `TextDefault` `AppliedCharacterStyle` | | | +| 0x1B959 (20 bytes) | `PageItemDefault` `AppliedGraphicObjectStyle` | `AppliedTextObjectStyle` | `AppliedGridObjectStyle` | + +The first two UIDs are the root style and the root style group. The +reference is written as other style references; UID 0 is the root +style. Evidence: every pair has the three chunks. Paragraph 487 of 489 +(the other two name another style in IDML; not explained), character +489 of 489, graphic and text object styles 489 of 489. + +**Layout and story grids.** Chunks 0xCD2F (`LayoutGridDataInformation`) +and 0xCD2E (`StoryGridDataInformation`) start as page chunk 0xCD02 +(`objects.md`): u32 font family UID (`AppliedFont`), a flag byte and the +font style (`FontStyle`), five f64 (`PointSize`, `CharacterAki`, +`LineAki`, and `HorizontalScale` and `VerticalScale` as fractions of +100 %), four u32 codes. Chunk 0xCD2E continues with u32 0 and f64 +`CharacterCountSize`. Evidence: every pair has both chunks; all values +match in 489 of 489 but `AppliedFont` (488: one family that IDML names +in another form, `fonts.md`). + +**Single settings.** + +| Chunk | Layout | IDML | Without the chunk | Evidence | +|---|---|---|---|---| +| 0x59C | u16 1 | `DocumentPreference` `MasterTextFrame`, `CreatePrimaryTextFrame` (DOM 8 on) `true` | `false` | 14 with, 475 without; 489 of 489 | +| 0x1081F | u32: 2 `RGB`, 3 `CMYK` | `TransparencyPreference` `BlendingSpace` | `CMYK` | 62 of 62; 427 of 427 | +| 0x5A6 | u16: 1 `true`, 0 `false` | `DocumentPreference` `AllowPageShuffle` | (in every file) | 489 of 489 | +| 0x568 | u16 | `GuidePreference` `GuidesShown` | `true` | 114 of 114; 375 of 375 | +| 0x55A | u8 at 0, u8 at 2 | `GuidePreference` `GuidesSnapto`, `GridPreference` `DocumentGridSnapto` | `true`, `false` | 26 of 26; 463 of 463 | +| 0x53F (22 bytes) | u8 at 0; colour at 18 | `GuidePreference` `GuidesInBack`, `RulerGuidesColor` | (in every file) | 489 of 489 | +| 0x55F | colour at 30; u8 at 34: 0 `TopOfPageOfBaselineGridRelativeOption`, 1 `TopOfMarginOfBaselineGridRelativeOption` | `GridPreference` `BaselineColor`, `BaselineGridRelativeOption` | (in every file) | 489 of 489 | +| 0x545 | colour at 32 | `GridPreference` `GridColor` | `LightGray` | 14 of 14; 475 of 475 | +| 0x550 | colour at 36 | `DocumentPreference` `MarginGuideColor` | `Magenta` | 388 of 388; 101 of 101 | +| 0x555 | colour at 18 | `DocumentPreference` `ColumnGuideColor` | (observed value) | 72 of 72 | +| 0x5D2 | colours at 20, 24, 28 | `PasteboardPreference` `BleedGuideColor`, `SlugGuideColor`, `PreviewBackgroundColor` | `Fiesta`, `GridBlue`, `LightGray` | 80 of 80; 409 of 409 | +| 0x54A (18 bytes) | f64 at 0, f64 at 8 | `Document` `ZeroPoint` | `0 0` | 46 of 46; 443 of 443 | +| 0x7006 (20 bytes) | u8 at 0; f64 at 12 | `LayoutAdjustmentPreference` `EnableLayoutAdjustment`, `SnapZone` | `false` (observed), `SnapZone` not written | 24 of 24 | +| 0xCA0B | u16 1 | `ViewPreference` `ShowTextThreads` `true`, INDD version 21.1 on | `false` | IDML has the attribute only from version 21.1: 26 of 26 (28 of 28 in all pairs) | + +Without chunk 0x555, `ColumnGuideColor` keeps its observed value, which +4 of the 417 pairs without the chunk contradict. Without chunk 0x7006, +`SnapZone` varies and is not written. diff --git a/src/idml/format.rs b/src/idml/format.rs index 5118b92..bcc5d58 100644 --- a/src/idml/format.rs +++ b/src/idml/format.rs @@ -14,7 +14,7 @@ pub(super) fn xml_tag_color(rgb: [f64; 3]) -> Option<&'static str> { /// The IDML name of an interface colour (class 0x1F11) by its red, green /// and blue fractions. See `docs/format/objects.md`, interface colours. pub(super) fn ui_color_name(rgb: [f64; 3]) -> Option<&'static str> { - const NAMES: [([f64; 3], &str); 25] = [ + const NAMES: [([f64; 3], &str); 29] = [ ([0.31, 0.6, 1.0], "LightBlue"), ([1.0, 0.0, 0.0], "Red"), ([0.31, 1.0, 0.31], "Green"), @@ -40,6 +40,10 @@ pub(super) fn ui_color_name(rgb: [f64; 3]) -> Option<&'static str> { ([1.0, 0.71, 0.42], "GridOrange"), ([0.97, 0.35, 0.42], "Fiesta"), ([0.0, 0.6, 0.6], "Teal"), + ([0.73, 0.73, 0.73], "LightGray"), + ([0.48, 0.73, 0.85], "GridBlue"), + ([1.0, 0.6, 0.6], "Peach"), + ([0.6, 0.0, 0.2], "Burgundy"), ]; NAMES .iter() diff --git a/src/idml/resources.rs b/src/idml/resources.rs index 6359940..2a8105e 100644 --- a/src/idml/resources.rs +++ b/src/idml/resources.rs @@ -26,6 +26,8 @@ pub(super) const PREFERENCE_TAGS: &[&str] = &[ "ChapterNumberPreference", "DictionaryPreference", "EPubExportPreference", + "TransparencyPreference", + "LayoutAdjustmentPreference", ]; /// A tree of preference values as a `Node`. @@ -763,6 +765,74 @@ impl Writer<'_> { }); } } + // Default styles (preferences.md, default styles). + for (uid, paragraph, name) in [ + (prefs.default_styles[0], true, "AppliedParagraphStyle"), + (prefs.default_styles[1], false, "AppliedCharacterStyle"), + ] { + let value = match uid { + Some(u) + if self + .doc + .styles + .get(&u) + .is_some_and(|s| s.paragraph == paragraph) => + { + Some(self.style_ref(Some(u), paragraph)) + } + Some(0) if paragraph => Some("ParagraphStyle/$ID/[No paragraph style]".into()), + Some(0) => Some("CharacterStyle/$ID/[No character style]".into()), + _ => None, + }; + if let Some(v) = value { + let i = ours_of(&mut ours, "TextDefault"); + ours[i].attrs.push((name.into(), v)); + } + } + if let Some(uids) = prefs.default_object_styles { + for (u, name) in uids.into_iter().zip([ + "AppliedGraphicObjectStyle", + "AppliedTextObjectStyle", + "AppliedGridObjectStyle", + ]) { + if let Some(r) = self.object_style_ref(u) { + let i = ours_of(&mut ours, "PageItemDefault"); + ours[i].attrs.push((name.into(), r)); + } + } + } + for (tag, g, count) in &prefs.grids { + let i = ours_of(&mut ours, tag); + let n = &mut ours[i]; + n.attrs.push(("FontStyle".into(), g.font_style.clone())); + let [size, character_aki, line_aki, h_scale, v_scale] = g.numbers; + for (name, v) in [ + ("PointSize", size), + ("CharacterAki", character_aki), + ("LineAki", line_aki), + ("HorizontalScale", h_scale * 100.0), + ("VerticalScale", v_scale * 100.0), + ] { + n.attrs.push((name.into(), num(v))); + } + if let Some(c) = count { + n.attrs.push(("CharacterCountSize".into(), num(*c))); + } + if let Some(f) = self.doc.fonts.get(&g.font) { + let p = prop_node(&("AppliedFont", "string", self.family_names(f).0.into())); + match n.children.iter_mut().find(|c| c.tag == "Properties") { + Some(props) => props.children.push(p), + None => n.children.insert( + 0, + Node { + tag: "Properties".into(), + children: vec![p], + ..Node::default() + }, + ), + } + } + } if let Some(o) = &prefs.index_options { let i = ours_of(&mut ours, "IndexOptions"); ours[i].attrs.extend( diff --git a/src/idml/values.rs b/src/idml/values.rs index d599b51..40578f3 100644 --- a/src/idml/values.rs +++ b/src/idml/values.rs @@ -63,13 +63,17 @@ impl Node { } /// Add the attributes and children of `other` that `self` lacks. - /// Children with the same tag are merged. + /// Children with the same tag are merged. A typed value (an element + /// with a `type` attribute) keeps its own text and children. 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())); } } + if self.attr("type").is_some() { + return; + } if self.text.is_none() { self.text = other.text.clone(); } diff --git a/src/model/prefs.rs b/src/model/prefs.rs index 04ffb74..7d7d518 100644 --- a/src/model/prefs.rs +++ b/src/model/prefs.rs @@ -44,6 +44,14 @@ pub struct Prefs { pub endnotes: Option, /// Index options (chunk 0x13010). pub index_options: Option, + /// Default paragraph and character style UIDs (chunks 0x28D4 and + /// 0x28D5, u32 at 8). + pub default_styles: [Option; 2], + /// Default graphic, text and grid object style UIDs (chunk 0x1B959). + pub default_object_styles: Option<[u32; 3]>, + /// Layout and story grid defaults: element, grid settings and, for + /// the story grid, `CharacterCountSize`. + pub grids: Vec<(&'static str, super::GridData, Option)>, } /// The document's endnote options (preferences chunk 0x2261E). See @@ -749,6 +757,30 @@ mod id { pub const DICTIONARY: u32 = 0x2806; /// EPUB export options. pub const EPUB: u32 = 0x21A1A; + pub const DEFAULT_PARAGRAPH_STYLE: u32 = 0x28D4; + pub const DEFAULT_CHARACTER_STYLE: u32 = 0x28D5; + pub const DEFAULT_OBJECT_STYLES: u32 = 0x1B959; + pub const LAYOUT_GRID: u32 = 0xCD2F; + pub const STORY_GRID: u32 = 0xCD2E; + /// Present when new documents have a master text frame. + pub const MASTER_TEXT_FRAME: u32 = 0x59C; + pub const BLENDING_SPACE: u32 = 0x1081F; + pub const ALLOW_PAGE_SHUFFLE: u32 = 0x5A6; + pub const GUIDES_SHOWN: u32 = 0x568; + pub const SNAP: u32 = 0x55A; + pub const GUIDES: u32 = 0x53F; + pub const ZERO_POINT: u32 = 0x54A; + pub const LAYOUT_ADJUSTMENT: u32 = 0x7006; + pub const SHOW_TEXT_THREADS: u32 = 0xCA0B; +} + +/// Interface colours that preferences without their chunk have +/// (`docs/format/preferences.md`). +mod ui { + pub const LIGHT_GRAY: [f64; 3] = [0.73, 0.73, 0.73]; + pub const MAGENTA: [f64; 3] = [1.0, 0.31, 1.0]; + pub const FIESTA: [f64; 3] = [0.97, 0.35, 0.42]; + pub const GRID_BLUE: [f64; 3] = [0.48, 0.73, 0.85]; } /// The EPUB identifier of documents without EPUB export options. @@ -1117,9 +1149,21 @@ impl Reader<'_> { ); set("PasteboardPreference", "MinimumSpaceAboveAndBelow", num(v)); } - // Text wrap: u8 at 2. + // Text wrap: u8 at 0, 2 and, in 6-byte chunks, 4. if let Some(d) = get(id::TEXT_WRAP)?.filter(|d| d.len() >= 3) { + set( + "TextPreference", + "AbutTextToTextWrap", + (d[0] != 0).to_string(), + ); set("TextPreference", "ZOrderTextWrap", (d[2] != 0).to_string()); + if d.len() >= 6 { + set( + "TextPreference", + "HonourTextIndentsWithTextWrap", + (d[4] != 0).to_string(), + ); + } } // Default XML tags: name (u32 length, text segments) and u32 UID of // an interface colour, for story, table, (untagged), cell, image. @@ -1225,6 +1269,220 @@ impl Reader<'_> { } } + // Default styles: u32 UIDs; the default is the third. + let mut default_styles = [None, None]; + for (i, id) in [id::DEFAULT_PARAGRAPH_STYLE, id::DEFAULT_CHARACTER_STYLE] + .into_iter() + .enumerate() + { + default_styles[i] = get(id)?.and_then(|d| self.enc().u32_at(&d, 8)); + } + let default_object_styles = get(id::DEFAULT_OBJECT_STYLES)?.and_then(|d| { + let u = |o| self.enc().u32_at(&d, o); + Some([u(8)?, u(12)?, u(16)?]) + }); + // Layout and story grids: as page chunk 0xCD02; the story grid + // continues with u32 0 and f64 `CharacterCountSize`. + let mut grids = Vec::new(); + for (id, element) in [ + (id::LAYOUT_GRID, "LayoutGridDataInformation"), + (id::STORY_GRID, "StoryGridDataInformation"), + ] { + if let Some(d) = get(id)? { + let mut c = self.cursor(&d); + let read = (|| -> Result<(super::GridData, Option), Error> { + let font = c.u32()?; + c.u8()?; + let font_style = c.string()?; + let numbers = [c.f64()?, c.f64()?, c.f64()?, c.f64()?, c.f64()?]; + let codes = [c.u32()?, c.u32()?, c.u32()?, c.u32()?]; + let count = if element == "StoryGridDataInformation" { + c.u32()?; + Some(c.f64()?) + } else { + None + }; + Ok(( + super::GridData { + font, + font_style, + numbers, + codes, + }, + count, + )) + })(); + if let Ok((g, count)) = read { + grids.push((element, g, count)); + } + } + } + // Master text frame: the chunk is there when it is on. + let master = get(id::MASTER_TEXT_FRAME)?.is_some().to_string(); + set("DocumentPreference", "MasterTextFrame", master.clone()); + if major >= 8 { + set("DocumentPreference", "CreatePrimaryTextFrame", master); + } + match get(id::BLENDING_SPACE)? { + None => set("TransparencyPreference", "BlendingSpace", "CMYK".into()), + Some(d) => match self.enc().u32_at(&d, 0) { + Some(2) => set("TransparencyPreference", "BlendingSpace", "RGB".into()), + Some(3) => set("TransparencyPreference", "BlendingSpace", "CMYK".into()), + _ => {} + }, + } + // Guides, grids, pasteboard: flags and interface colours. + let bool_at = |d: &[u8], o: usize| match d.get(o) { + Some(0) => Some("false".to_string()), + Some(1) => Some("true".to_string()), + _ => None, + }; + if let Some(v) = get(id::ALLOW_PAGE_SHUFFLE)?.and_then(|d| bool_at(&d, 0)) { + set("DocumentPreference", "AllowPageShuffle", v); + } + match get(id::GUIDES_SHOWN)? { + None => set("GuidePreference", "GuidesShown", "true".into()), + Some(d) => { + if let Some(v) = bool_at(&d, 0) { + set("GuidePreference", "GuidesShown", v); + } + } + } + match get(id::SNAP)? { + None => { + set("GuidePreference", "GuidesSnapto", "true".into()); + set("GridPreference", "DocumentGridSnapto", "false".into()); + } + Some(d) => { + if let Some(v) = bool_at(&d, 0) { + set("GuidePreference", "GuidesSnapto", v); + } + if let Some(v) = bool_at(&d, 2) { + set("GridPreference", "DocumentGridSnapto", v); + } + } + } + let ui_color = |colors: &mut Vec<(&'static str, &'static str, [f64; 3])>, + element: &'static str, + name: &'static str, + uid: Option| { + if let Some(uid) = uid + && let Ok(Some(rgb)) = self.ui_color(uid) + { + colors.push((element, name, rgb)); + } + }; + if let Some(d) = get(id::GUIDES)? { + if let Some(v) = bool_at(&d, 0) { + set("GuidePreference", "GuidesInBack", v); + } + ui_color( + &mut colors, + "GuidePreference", + "RulerGuidesColor", + self.enc().u32_at(&d, 18), + ); + } + if let Some(d) = get(id::BASELINE_GRID)? { + ui_color( + &mut colors, + "GridPreference", + "BaselineColor", + self.enc().u32_at(&d, 30), + ); + match d.get(34) { + Some(0) => set( + "GridPreference", + "BaselineGridRelativeOption", + "TopOfPageOfBaselineGridRelativeOption".into(), + ), + Some(1) => set( + "GridPreference", + "BaselineGridRelativeOption", + "TopOfMarginOfBaselineGridRelativeOption".into(), + ), + _ => {} + } + } + for (id, at, element, name, absent) in [ + ( + id::DOCUMENT_GRID, + 32, + "GridPreference", + "GridColor", + Some(ui::LIGHT_GRAY), + ), + ( + id::MARGINS, + 36, + "DocumentPreference", + "MarginGuideColor", + Some(ui::MAGENTA), + ), + ( + id::COLUMNS, + 18, + "DocumentPreference", + "ColumnGuideColor", + None, + ), + ] { + match get(id)? { + Some(d) => ui_color(&mut colors, element, name, self.enc().u32_at(&d, at)), + None => { + if let Some(rgb) = absent { + colors.push((element, name, rgb)); + } + } + } + } + match get(id::PASTEBOARD)? { + Some(d) => { + for (at, name) in [ + (20, "BleedGuideColor"), + (24, "SlugGuideColor"), + (28, "PreviewBackgroundColor"), + ] { + ui_color( + &mut colors, + "PasteboardPreference", + name, + self.enc().u32_at(&d, at), + ); + } + } + None => { + for (name, rgb) in [ + ("BleedGuideColor", ui::FIESTA), + ("SlugGuideColor", ui::GRID_BLUE), + ("PreviewBackgroundColor", ui::LIGHT_GRAY), + ] { + colors.push(("PasteboardPreference", name, rgb)); + } + } + } + match get(id::ZERO_POINT)? { + None => set("Document", "ZeroPoint", "0 0".into()), + Some(d) => { + if let (Some(x), Some(y)) = (self.enc().f64_at(&d, 0), self.enc().f64_at(&d, 8)) { + set("Document", "ZeroPoint", format!("{} {}", num(x), num(y))); + } + } + } + if let Some(d) = get(id::LAYOUT_ADJUSTMENT)? { + if let Some(v) = bool_at(&d, 0) { + set("LayoutAdjustmentPreference", "EnableLayoutAdjustment", v); + } + if let Some(z) = self.enc().f64_at(&d, 12) { + set("LayoutAdjustmentPreference", "SnapZone", num(z)); + } + } + if (major, version.minor) >= (21, 1) { + let shown = + get(id::SHOW_TEXT_THREADS)?.is_some_and(|d| self.enc().u16_at(&d, 0) == Some(1)); + set("ViewPreference", "ShowTextThreads", shown.to_string()); + } + // Index header setting. if let Some(d) = get(id::INDEX_HEADER)? { match index_header(self.enc(), &d) { @@ -1394,6 +1652,9 @@ impl Reader<'_> { }, None => None, }, + default_styles, + default_object_styles, + grids, index_options: match get(id::INDEX_OPTIONS)? { Some(d) => match IndexOptions::read(self.enc(), &d) { Ok(o) => Some(o),