//! The `Document` model and `Reader::document`, which reads the document //! object and every class the converter knows into it. //! //! Evidence: `docs/format/objects.md` (document object and classes). use super::*; /// The XML structure of the document. #[derive(Debug, Clone, Default, PartialEq)] pub struct XmlStructure { /// The backing story, IDML `XmlStory`. pub story: Option, pub elements: BTreeMap, } #[derive(Debug, Clone, Default, PartialEq)] pub struct Document { pub version: Version, pub layers: Vec, pub active_layer: Option, /// Document users (chunk 0xA443): flag byte and name. pub users: Vec<(u8, String)>, /// The script byte of the last document user's name. pub users_script: Option, /// The code of the last session in the save history (chunk 0x1D8): /// 0x0101 for a session of a Japanese or Chinese edition. pub last_session_code: Option, /// Major and minor version of the application of the last session in /// the save history (`16.1.0.20` gives 16, 1). pub last_session_version: Option<(u32, u32)>, /// The document's label (chunk 0x1630B): key and value, as IDML /// writes them. pub label: Vec<(String, String)>, /// The numbering lists, the default list first. pub numbering_lists: Vec, /// Index sort groups (preferences chunk 0x1307E), in order: name, /// include flag and header variant. pub index_groups: Vec<(String, bool, u16)>, /// The index (class 0x13004) and its topics. pub index: Option, /// The constant shade the document lists (the class 0x5533 object of /// lowest UID with a constant shade in chunk 0x5532): its UID and its /// u32 and three f64; and its name (chunk 0x5531: a flag byte, 1 for /// a built-in key, and the name). pub constant_shade: Option, /// The pasted smooth shades with an axial shading, in UID order. pub axial_shades: Vec, /// Assignment objects (class 0x1BE01), in UID order. pub assignments: Vec, /// Named grids (class 0xCD12, chunk 0xCD28: u32, a flag byte, 1 for /// a built-in key, and the name), in UID order. pub named_grids: Vec, pub spreads: Vec, pub master_spreads: Vec, pub stories: Vec, pub styles: BTreeMap, pub toc_styles: Vec, pub colors: Vec, /// Mixed inks and mixed ink groups, in UID order. pub mixed_inks: Vec, /// Tint swatches, with their IDML reference and name. pub tints: Vec<(Tint, String, String)>, pub gradients: Vec, /// IDML reference (`Color/...`, `Swatch/None`) for each swatch UID. pub swatches: BTreeMap, /// Font families by UID. pub fonts: BTreeMap, /// The language objects (class 0x2D07) in UID order. pub language_list: Vec, /// Language name (IDML `AppliedLanguage` without `$ID/`) for each /// language UID. pub languages: BTreeMap, /// Style groups, including the root groups (empty name). pub style_groups: BTreeMap, pub object_styles: BTreeMap, pub cell_styles: BTreeMap, pub table_styles: BTreeMap, pub sections: Vec
, pub text_variables: Vec, pub hyperlinks: Vec, pub text_sources: BTreeMap, pub page_item_sources: Vec, pub destinations: Vec, pub bookmarks: BTreeMap, /// All bookmarks in document order (document chunk 0x13501). pub bookmark_order: Vec, /// Cross-reference formats, by UID. pub cross_reference_formats: BTreeMap, /// Problems that did not stop the conversion (content left out). pub warnings: Vec, pub preferences: Option, /// Other preference values (`prefs.rs`). pub prefs: prefs::Prefs, pub composite_fonts: Vec, /// Kinsoku and mojikumi tables, in UID order. pub cjk_tables: Vec, /// Inks, in UID order. pub inks: Vec, /// Colour groups in document order, the root group first. pub color_groups: Vec, /// The bullet characters offered for lists (preferences chunk 0x1A488). pub bullets: Vec, /// XML tags (class 0xBF19). pub xml_tags: Vec, pub xml: XmlStructure, /// The dates of the XMP packet that have a UTC offset; link times are /// written with their offsets (`objects.md`, link times). pub xmp_dates: Vec, } /// An XML tag: its name and colour (red, green, blue). pub type XmlTag = (String, Option<[f64; 3]>); /// The objects read from the class list, before they are put together /// into the `Document`. #[derive(Default)] struct ClassObjects { styles: BTreeMap, colors: Vec, mixed_inks: Vec, tints: Vec, gradients: Vec, swatches: BTreeMap, fonts: BTreeMap, languages: BTreeMap, language_list: Vec, style_groups: BTreeMap, object_styles: BTreeMap, cell_styles: BTreeMap, table_styles: BTreeMap, text_variables: Vec, hyperlinks: Vec, text_sources: BTreeMap, page_item_sources: Vec, destinations: Vec, bookmarks: BTreeMap, cross_reference_formats: BTreeMap, /// Composite fonts: UID, name and entry UIDs. composite_fonts: Vec<(u32, Name, Vec)>, composite_entries: HashMap, cjk_tables: Vec, xml_tags: Vec, xml_tag_names: HashMap, inks: Vec, color_groups: HashMap, } /// The UID of the document object. const DOC: u32 = 1; impl<'a> Reader<'a> { /// Read the document object and every class the converter knows. /// An object that cannot be read is left out with a warning; the /// warnings are in `Document::warnings`. pub fn document(&self, version: Version) -> Result { self.major.set(version.major); // The first layer of the list is the internal layer, whatever its // name (objects.md, layers). let layers = self .uid_list(DOC, chunk::DOC_LAYERS)? .into_iter() .enumerate() .map(|(i, uid)| self.layer(uid, i == 0)) .collect::, _>>()?; let active_layer = self .chunk(DOC, chunk::DOC_ACTIVE_LAYER)? .map(|d| self.cursor(&d).u32()) .transpose()?; let (spreads, master_spreads) = self.document_spreads()?; let mut stories = self.document_stories()?; let mut objects = self.class_objects()?; // Cell spans depend on insets that cells inherit from styles. let table_styles = table::TableStyles::new(&objects.cell_styles, &objects.table_styles); for story in &mut stories { for t in story.tables.values_mut() { t.resolve_spans(&table_styles); } } let xml_story = self.xml_story()?; for story in &mut stories { story.orientation = self.story_orientation(story.uid); } let xml = XmlStructure { story: xml_story, elements: self.xml_elements(&objects.xml_tag_names)?, }; let tints = self.tints(&mut objects); prune_style_groups(&mut objects.style_groups, |m| self.warn(m)); let preferences = self.document_preferences()?; let prefs = self.prefs(version)?; let toc_styles = self.toc_styles(); let named_grids = self.named_grids(); let index_groups = self.index_groups(); let index = self.index().unwrap_or_else(|e| { self.warn(format!("index left out: {e}")); None }); let constant_shade = self.constant_shade(); let axial_shades = self.axial_shades(); // Page item fills refer to axial shades by UID (objects.md, // pasted smooth shades). for a in &axial_shades { objects .swatches .insert(a.uid, format!("PastedSmoothShade/u{:x}", a.uid)); } let assignments = self.assignments(); let users_script = self.users_script(DOC); let last_session = self.last_session(DOC); let last_session_code = last_session.as_ref().map(|(c, _)| *c); let last_session_version = last_session.and_then(|(_, v)| { let mut it = v.split('.').map(|n| n.parse::().ok()); Some((it.next()??, it.next()??)) }); let numbering_lists = self.numbering_lists().unwrap_or_else(|e| { self.warn(format!("numbering lists left out: {e}")); Vec::new() }); let label = self.label(DOC).unwrap_or_else(|e| { self.warn(format!("document label left out: {e}")); Vec::new() }); let users = self.users(DOC).unwrap_or_else(|e| { self.warn(format!("document users left out: {e}")); Vec::new() }); let sections = self .uid_list(DOC, chunk::DOC_SECTIONS)? .into_iter() .map(|uid| self.section(uid)) .collect::, _>>()?; let bookmark_order = match self.chunk(DOC, hyperlink::chunk::DOCUMENT_LISTS)? { Some(d) => hyperlink::document_bookmarks(self.enc(), &d, version.major)?, None => Vec::new(), }; let composite_fonts = objects .composite_fonts .into_iter() .map(|(uid, name, entries)| CompositeFont { uid, name, entries: entries .iter() .filter_map(|e| objects.composite_entries.get(e).cloned()) .collect(), }) .collect(); let color_groups = self .color_group_order()? .into_iter() .filter_map(|u| objects.color_groups.remove(&u)) .collect(); let bullets = self.bullets()?; Ok(Document { version, layers, active_layer, spreads, master_spreads, stories, styles: objects.styles, colors: objects.colors, mixed_inks: objects.mixed_inks, tints, gradients: objects.gradients, swatches: objects.swatches, fonts: objects.fonts, languages: objects.languages, language_list: objects.language_list, toc_styles, named_grids, index_groups, index, constant_shade, axial_shades, assignments, style_groups: objects.style_groups, object_styles: objects.object_styles, cell_styles: objects.cell_styles, table_styles: objects.table_styles, users, users_script, last_session_code, last_session_version, label, numbering_lists, sections, text_variables: objects.text_variables, hyperlinks: objects.hyperlinks, text_sources: objects.text_sources, page_item_sources: objects.page_item_sources, destinations: objects.destinations, bookmarks: objects.bookmarks, bookmark_order, cross_reference_formats: objects.cross_reference_formats, preferences, prefs, composite_fonts, cjk_tables: objects.cjk_tables, inks: objects.inks, color_groups, bullets, xml_tags: objects.xml_tags, xml, xmp_dates: Vec::new(), // Last, so that it holds every warning of the model. warnings: self.warnings.borrow().clone(), }) } /// The spreads and master spreads of the document, with the page /// layout of each page resolved. A spread that cannot be read is left /// out with a warning. fn document_spreads(&self) -> Result<(Vec, Vec), Error> { let mut spreads = self.each_or_warn("spread", chunk::DOC_SPREADS, |u| self.spread(u).map(Some))?; let mut master_spreads = self.each_or_warn("master spread", chunk::DOC_MASTER_SPREADS, |u| { self.spread(u).map(Some) })?; resolve_page_layout(&mut spreads, &mut master_spreads); Ok((spreads, master_spreads)) } /// The stories the document lists. A story that cannot be read is /// left out with a warning. fn document_stories(&self) -> Result, Error> { self.each_or_warn("story", chunk::DOC_STORIES, |uid| match self.class(uid) { Some(class::STORY | class::ENDNOTE_STORY) => self.story(uid).map(Some), _ => Ok(None), }) } /// Read each object of the document's UID list `list` with `read`. /// An object that cannot be read is left out with a warning that /// calls it `what`. fn each_or_warn( &self, what: &str, list: u32, read: impl Fn(u32) -> Result, Error>, ) -> Result, Error> { let mut out = Vec::new(); for uid in self.uid_list(DOC, list)? { match read(uid) { Ok(Some(t)) => out.push(t), Ok(None) => {} Err(e) => self.warn(format!("{what} {uid} left out: {e}")), } } Ok(out) } /// The backing story of the XML structure: the document's chunk /// 0xBF14 names it and the document node. fn xml_story(&self) -> Result, Error> { Ok(match self.chunk(DOC, xml::chunk::NODE_REF)? { Some(d) if d.len() >= 4 => { let s = self.cursor(&d).u32()?; match self.class(s) { Some(class::STORY) => self .story(s) .map_err(|e| self.warn(format!("XML story {s} left out: {e}"))) .ok(), _ => None, } } _ => None, }) } /// Assignment objects (class 0x1BE01), in UID order. fn assignments(&self) -> Vec { let mut a: Vec = self .db .classes() .iter() .filter(|&&(_, c)| c == class::ASSIGNMENT) .map(|&(u, _)| u) .collect(); a.sort_unstable(); a } /// Read every object of the class list whose class the converter /// knows. An object that cannot be read is left out with a warning. fn class_objects(&self) -> Result { let mut out = ClassObjects::default(); for &(uid, cls) in self.db.classes() { if self.db.object(uid)?.is_none() { continue; } if let Err(e) = self.class_object(&mut out, uid, cls) { self.warn(format!("object {uid} (class {cls:#x}) left out: {e}")); } } Ok(out) } /// Read one object of the class list into `out`. fn class_object(&self, out: &mut ClassObjects, uid: u32, cls: u32) -> Result<(), Error> { match cls { class::STYLE_ROOT_GROUP | class::OBJECT_STYLE_ROOT_GROUP | class::CELL_STYLE_ROOT_GROUP | class::TABLE_STYLE_ROOT_GROUP => { out.style_groups.insert(uid, self.style_root_group(uid)?); } class::STYLE_GROUP => { out.style_groups.insert(uid, self.style_group(uid)?); } class::OBJECT_STYLE => { if let Some(s) = self.object_style(uid)? { out.object_styles.insert(uid, s); } } class::STYLE => { if let Some(style) = self.style(uid)? { out.styles.insert(uid, style); } } table::class::CELL_STYLE => { if let Some(s) = self.table_style(uid, table::chunk::CELL_STYLE_ATTRS)? { out.cell_styles.insert(uid, s); } } table::class::TABLE_STYLE => { if let Some(s) = self.table_style(uid, table::chunk::TABLE_STYLE_ATTRS)? { out.table_styles.insert(uid, s); } } color::class::COLOR => self.color_object(out, uid)?, color::class::SWATCH_NONE => { out.swatches.insert(uid, "Swatch/None".into()); } color::class::GRADIENT => { if let Some(g) = self.gradient(uid)? { out.swatches.insert(uid, g.reference()); out.gradients.push(g); } } class::FONT_FAMILY => { if let Some(f) = self.font_family(uid)? { out.fonts.insert(uid, f); } } class::TEXT_VARIABLE => { if let Some(v) = TextVariable::read(uid, &*self.object(uid)?)? { out.text_variables.push(v); } } hyperlink::class::HYPERLINK => { if let Some(h) = Hyperlink::read(uid, &*self.object(uid)?, self.major.get())? { out.hyperlinks.push(h); } } hyperlink::class::TEXT_SOURCE => { if let Some(s) = TextSource::read(uid, &*self.object(uid)?)? { out.text_sources.insert(uid, s); } } hyperlink::class::PAGE_ITEM_SOURCE => { if let Some(s) = PageItemSource::read(uid, &*self.object(uid)?)? { out.page_item_sources.push(s); } } hyperlink::class::PAGE_DESTINATION | hyperlink::class::URL_DESTINATION | hyperlink::class::EXTERNAL_PAGE_DESTINATION | hyperlink::class::TEXT_DESTINATION => { if let Some(d) = self.destination(uid, cls)? { out.destinations.push(d); } } xref::CLASS => { if let Some(f) = CrossReferenceFormat::read(uid, &*self.object(uid)?)? { out.cross_reference_formats.insert(uid, f); } } hyperlink::class::BOOKMARK => { if let Some(b) = Bookmark::read(uid, &*self.object(uid)?)? { out.bookmarks.insert(uid, b); } } cjk::class::COMPOSITE_FONT => { if let Some((name, entries)) = CompositeFont::read(&*self.object(uid)?)? { out.composite_fonts.push((uid, name, entries)); } } cjk::class::COMPOSITE_FONT_ENTRY => { if let Some(e) = CompositeFontEntry::read(uid, &*self.object(uid)?)? { out.composite_entries.insert(uid, e); } } c if c == cjk::class::MOJIKUMI || c == cjk::class::CUSTOM_MOJIKUMI || cjk::class::KINSOKU.contains(&c) => { if let Some(t) = CjkTable::read(uid, c, &*self.object(uid)?)? { out.cjk_tables.push(t); } } color::class::INK => { if let Some(i) = self.ink(uid)? { out.inks.push(i); } } color::class::COLOR_GROUP => { if let Some(g) = ColorGroup::read(uid, &*self.object(uid)?)? { out.color_groups.insert(uid, g); } } class::XML_TAG => { if let Some((name, color)) = self.xml_tag(uid)? { out.xml_tag_names.insert(uid, name.clone()); out.xml_tags.push((name, color)); } } class::LANGUAGE => { if let Some((name, language)) = self.language(uid)? { out.languages.insert(uid, name); out.language_list.extend(language); } } _ => {} } Ok(()) } /// A colour object (class 0x200): a colour or a tint. fn color_object(&self, out: &mut ClassObjects, uid: u32) -> Result<(), Error> { let obj = self.object(uid)?; if let Some(c) = Color::read(uid, &obj)? { // A member of a mixed ink group takes its inks from the group. let group = match obj.chunk(color::chunk::TINT_BASE) { Some(d) if c.space == color::Space::Other(9) => { let g = self.cursor(d).u32()?; if g != uid && self.class(g) == Some(color::class::COLOR) { Some(self.object(g)?) } else { None } } _ => None, }; if let Some(m) = MixedInk::read(&c, &obj, group.as_deref())? { out.swatches.insert(uid, m.reference()); out.mixed_inks.push(m); return Ok(()); } if c.model_name().is_none() { self.warn(format!( "colour {uid}: colour model code {} is not known; left out", c.model )); } out.swatches.insert(uid, c.reference()); out.colors.push(c); } else if let Some(t) = Tint::read(uid, &obj)? { out.tints.push(t); } Ok(()) } /// A gradient, with a warning for each midpoint IDML cannot hold. fn gradient(&self, uid: u32) -> Result, Error> { let Some(g) = Gradient::read(uid, &*self.object(uid)?)? else { return Ok(None); }; for i in 1..g.stops.len() { if g.idml_midpoint(i).is_none() { self.warn(format!( "gradient {uid}: midpoint before stop {i} is not \ within 13–87 %; left out" )); } } Ok(Some(g)) } /// A font family. If its fonts cannot be read, the family keeps its /// name, which text formatting refers to. fn font_family(&self, uid: u32) -> Result, Error> { // A family that refers to another (fonts.md). if let Some((target, count)) = self .chunk(uid, font::chunk::FAMILY)? .and_then(|d| font::reference(self.enc(), &d)) { let family = match self.class(target) { Some(class::FONT_FAMILY) if target != uid => { let object = self.object(target)?; match object.chunk(font::chunk::FAMILY) { Some(d) if font::reference(self.enc(), d).is_none() => { FontFamily::read(target, &object).ok().flatten() } _ => None, } } _ => None, }; let Some(family) = family else { self.warn(format!( "font family {uid}: refers to family {target}, which cannot be read; left out" )); return Ok(None); }; if count != 0 { self.warn(format!( "font family {uid}: refers to family {target} and lists {count} \ fonts of its own; fonts left out" )); return Ok(Some(FontFamily { fonts: Vec::new(), ..family.referring(uid) })); } return Ok(Some(family.referring(uid))); } match FontFamily::read(uid, &*self.object(uid)?) { Ok(Some(f)) => { if i32::try_from(f.writing_script).is_err() { self.warn(format!( "font family {uid}: writing script {:#x} is not an IDML \ integer; left out", f.writing_script )); } Ok(Some(f)) } Ok(None) => Ok(None), Err(e) => { self.warn(format!("font family {uid}: fonts left out: {e}")); Ok(match self.chunk(uid, font::chunk::FAMILY)? { Some(d) => Some(FontFamily { uid, name: find_string(self.enc(), &d, 0)?, builtin: false, native_name: String::new(), fonts: Vec::new(), writing_script: 0, }), None => None, }) } } } /// A page, URL, external page or text destination. fn destination(&self, uid: u32, cls: u32) -> Result, Error> { let mut d = Destination::read(uid, cls, &*self.object(uid)?)?; // The file name of an external page destination is the last part // of its link's URI (hyperlinks.md). if let Some(hyperlink::DestinationKind::ExternalPage { link: Some(link), file_name, .. }) = d.as_mut().map(|d| &mut d.kind) && let Some((l, _)) = self.link(*link)? { *file_name = l .uri .rsplit('/') .next() .filter(|n| !n.is_empty()) .map(percent_decode); } if let Some(hyperlink::DestinationKind::Page { zoom: None, .. }) = d.as_ref().map(|d| &d.kind) { self.warn(format!( "destination {uid}: view zoom is outside 5–4000 %; left out" )); } Ok(d) } /// An ink, with a warning if its neutral density cannot be written. fn ink(&self, uid: u32) -> Result, Error> { let i = Ink::read(uid, &*self.object(uid)?)?; if i.as_ref().is_some_and(|i| i.neutral_density.is_none()) { self.warn(format!( "ink {uid}: neutral density is outside 0.001–10; left out" )); } Ok(i) } /// The tint swatches, with their IDML reference and name. Each tint /// is added to `objects.swatches`; a tint whose base colour has no /// name is left out with a warning. fn tints(&self, objects: &mut ClassObjects) -> Vec<(Tint, String, String)> { let mut tints = Vec::new(); for t in std::mem::take(&mut objects.tints) { match objects .colors .iter() .find(|c| c.uid == t.base && !c.name.is_empty()) { Some(base) => { let reference = t.reference(base); objects.swatches.insert(t.uid, reference.clone()); tints.push((t.clone(), reference, t.idml_name(base))); } None => self.warn(format!( "tint {}: left out: base colour {} has no name", t.uid, t.base )), } } tints } /// An XML tag (class 0xBF19): its name and colour. Chunk 0xBF2F: u32 /// length, then the name as text segments; chunk 0x117: the UID of /// the tag's colour. fn xml_tag(&self, uid: u32) -> Result, Error> { let Some(d) = self.chunk(uid, chunk::XML_TAG_NAME)? else { return Ok(None); }; let mut c = self.cursor(&d); let n = c.u32()? as usize; // Files from InDesign 2.0 to 4.0 hold a flag byte and an in-object // string instead. let name = match c.segments(n) { Ok(name) => name, Err(_) => { let mut c = self.cursor(&d); c.flag()?; c.string()? } }; let color = match self.chunk(uid, chunk::XML_TAG_COLOR)? { Some(d) if d.len() >= 4 => self.ui_color(self.cursor(&d).u32()?)?, _ => None, }; Ok(Some((name, color))) } } /// `s` with `%xx` escapes replaced by their bytes, read as UTF-8. fn percent_decode(s: &str) -> String { let b = s.as_bytes(); let mut out = Vec::with_capacity(b.len()); let mut i = 0; while i < b.len() { if b[i] == b'%' && let Some(v) = s .get(i + 1..i + 3) .and_then(|h| u8::from_str_radix(h, 16).ok()) { out.push(v); i += 3; } else { out.push(b[i]); i += 1; } } String::from_utf8_lossy(&out).into_owned() } #[cfg(test)] mod percent_tests { use super::percent_decode; #[test] fn decodes_percent_escapes() { assert_eq!(percent_decode("Ch%201%20%C3%A9.indd"), "Ch 1 é.indd"); assert_eq!(percent_decode("100%"), "100%"); } }