From b8ef0d51d89458b0712f27fb4dbcda3c54b4e08f Mon Sep 17 00:00:00 2001 From: aiongg <67543387+aiongg@users.noreply.github.com> Date: Thu, 8 Oct 2026 06:27:03 +0000 Subject: [PATCH] feat: decode font name flags, all font types and missing-font records - FontType codes 0 Type1, 3 ATC and 0xFFFFFFFF Unknown. - TypekitID from INDD version 9.2 (was 12). - The key flag of the native style name gives FontStyleNameNative a $ID/ prefix; the key flag of the family name gives $ID/ to the family name in FontFamily, Font Name and every AppliedFont. - Missing-font records (kind 3) parse: no fonts, or short font records with style, platform name and Unknown type. - The family name in the font's own script replaces the name when the family's writing script is the script byte of the document users. compare.py --exclude own/: - trustworthy value coverage 97.36% -> 97.42% (+7,627 values); all pairs 95.35% -> 95.41% (+9,808) - TypekitID +3,483, FontStyleNameNative +1,262, FontType +664, 196 more Font elements matched, AppliedFont and BulletsFont +235 - extra values 26,348 -> 24,144 (Font elements whose Self now matches) - warnings 413 -> 406 (missing-font records now parse) - compare.py --all with schemas: 0 schema failures on pairs; files without a reference as before --- docs/format/fonts.md | 74 +++++++++++++++++++---- docs/format/idml-values.md | 6 +- docs/format/objects.md | 10 +++ src/idml/designmap.rs | 2 +- src/idml/kind.rs | 4 +- src/idml/mod.rs | 20 ++++++ src/idml/resources.rs | 38 ++++++++---- src/idml/spread.rs | 5 +- src/model/document.rs | 6 ++ src/model/font.rs | 121 ++++++++++++++++++++++++++++++++++--- src/model/settings.rs | 19 ++++++ 11 files changed, 270 insertions(+), 35 deletions(-) diff --git a/docs/format/fonts.md b/docs/format/fonts.md index 8d90fbf..90e7aae 100644 --- a/docs/format/fonts.md +++ b/docs/format/fonts.md @@ -16,10 +16,12 @@ the 251 distinct little-endian files. | Field | Contents | IDML | |---|---|---| -| u8, u16 | Not identified | | +| u8 | Record kind: 1 or 4 for the layout here, 3 for missing fonts (below), 2 for an 8-byte record without fonts | | +| u8 | 0 | | +| u8 | Key flag of the family name: 1 for a built-in key | `$ID/` before the name (below) | | string | Family name | `FontFamily/@Name`, `Font/@FontFamily` | -| u8 | Not identified | | -| string | Family name in the font's own script | not written | +| u8 | Key flag of the native name | | +| string | Family name in the font's own script ("native name") | used in place of the name in some documents (below) | | 6 bytes | Not identified | | | u16 | Font count | | | font records | See below | one `Font` each, in the same order | @@ -35,9 +37,9 @@ Strings are in-object strings (`objects.md`). | string | Style name | `FontStyleName` | | u16 *n*, *n* bytes | PostScript name, one byte per character | `PostScriptName` | | u8, string | Full name | `FullName` | -| u8, string | Style name in the font's own script | `FontStyleNameNative` | +| u8 key flag, string | Style name in the font's own script | `FontStyleNameNative`, with `$ID/` first when the flag is 1 | | u8, string | Full name in the font's own script | `FullNameNative` | -| u32 | Font type: 1 `TrueType`, 6 `OpenTypeCFF`, 7 `OpenTypeCID`, 8 `OpenTypeTT` | `FontType` | +| u32 | Font type: 0 `Type1`, 1 `TrueType`, 3 `ATC`, 6 `OpenTypeCFF`, 7 `OpenTypeCID`, 8 `OpenTypeTT`, 0xFFFFFFFF `Unknown` | `FontType` | | u32 *n*, segments | Version: *n* UTF-16 code units as text segments | `Version` | In files from InDesign 3.0 and 4.0 the PostScript name is a byte and an @@ -55,9 +57,14 @@ in-object string, and InDesign 3.0 has no version (`big-endian.md`). and `Version`. In the other two, matched by name, 6 fonts of each have another `Version` in IDML. That document's IDML gives its other font families other UIDs, so it was probably exported from another save. -- `FontType`: 4,464 of 4,464 (code 6: 3,043, 1: 905, 7: 341, 8: 175). - Codes 0 (54 fonts) and 3 (1 font) occur in files without an IDML; the - converter leaves out `FontType` for them. +- `FontType`: over the 31,969 fonts of all pairs whose INDD family has + as many fonts as the IDML family: code 0 `Type1` 799, 1 `TrueType` + 3,338, 3 `ATC` 21, 6 `OpenTypeCFF` 22,325, 7 `OpenTypeCID` 2,559, 8 + `OpenTypeTT` 2,893, 0xFFFFFFFF `Unknown` 34. The converted `FontType` + equals the IDML for all 32,002 matched fonts of all pairs. +- Key flag of the native style: 1 gives `$ID/` and the string in 1,364 + of 1,364 fonts, 0 the plain string in 30,591 of 30,605 (the other 14 + are in stale pairs whose fonts differ). - A font with an empty style name: in all 21 such IDML fonts over all pairs (12 in the trustworthy pairs), `Name` is the family name and ` Regular`, and `Self` is the family's `Self`, `Fontn` and the family @@ -109,6 +116,50 @@ was exported, as `Status` does (below). Text formatting refers to a family by UID (`AppliedFont`, `BulletsFont`; see `attributes.md`) and IDML writes the family name there. +**Built-in family names.** The family-name key flag is 1 in 9 families +of 8 pairs. IDML then writes `$ID/` and the name in `FontFamily/@Name`, +in `Font/@Name` (`$ID/`, name, space, style) and in every `AppliedFont` +that names the family; `Font/@FontFamily` and `Font/@Self` have the +plain name. No family with flag 0 gets `$ID/`. + +**Native family names.** IDML writes the native name in place of the +family name when the family's writing script equals the script byte of +the document users (`objects.md`, document users), the byte is not 0, +and the native name is not empty and differs from the name. It is then +used wherever the name would be: `FontFamily/@Name`, `Font/@FontFamily`, +`Font/@Name`, `Font/@Self` and every `AppliedFont`. + +| Users' script byte | Family writing script | Native name in IDML | Families (documents), all pairs | +|---|---|---|---| +| 1 | 1 | yes | 43 of 46 (9 of 10) | +| 25 | 25 | yes | 2 of 2 (1 of 1) | +| 1 | 2, 3 or 25 | no | 5 of 5 | +| 0, 7, 29 | any | no | 604 of 604 | + +The 3 misses are in one trustworthy DOM 16 pair whose IDML has a third +document user that the INDD lacks, so it was exported on another +computer. + +**Missing fonts (record kind 3).** After the native name come either 6 +zero bytes and the end of the chunk (no fonts; IDML writes a +`FontFamily` without `Font`), or 4 bytes, a u16 font count and short +font records: + +| Field | IDML | +|---|---| +| u8 key flag, string | `FontStyleName` (`$ID/Regular` for flag 1); the plain style in `Name` and `Self` | +| u8 key flag, string (the family name) | `FontStyleNameNative` and `PlatformName` (`$ID/` and the name for flag 1) | +| u8 key flag, string | the style again; not mapped | +| u32 style index, u32 0, u16 2 | not mapped | + +The other attributes are `PostScriptName=""`, `FontType="Unknown"`, +`WritingScript="0"`, `FullName=""`, `FullNameNative=""`, `Version=""`, +`TypekitID="$ID/"`. Evidence: 3 families with fonts in 2 trustworthy +pairs (5 fonts), every attribute equal; 3 families without fonts in 3 +trustworthy pairs. Over the corpus, 14 records without fonts and 49 +with fonts parse to their end. Kind 2 records (7 in 5 files) do not +parse; their family keeps its name only. + ## Typekit IDs (chunk 0x3EEB) u32 count *n*, then *n* entries: u8 1 if the string is a built-in key @@ -118,9 +169,10 @@ u32 count *n*, then *n* entries: u8 1 if the string is a built-in key - 945 of 945 fonts in families where *n* equals the font count. - In the 38 families where *n* is smaller, the entries match the first *n* fonts and all other fonts have `$ID/` (38 of 38). -- Families with *n* = 0 or without the chunk: all 3,020 fonts in IDML - from DOM 12 on have `$ID/`. The one DOM 7 IDML (32 fonts) has no - `TypekitID`; the converter writes it for InDesign 12 and later. +- Families with *n* = 0 or without the chunk: every font has `$ID/`. +- IDML from INDD files of version 9.0 (231 fonts) and 9.1 (29) and + earlier has no `TypekitID`; from 9.2 (169 fonts) on, every font has + it. The converter writes it from version 9.2. ## Not converted diff --git a/docs/format/idml-values.md b/docs/format/idml-values.md index 3bfa1b5..cb18dca 100644 --- a/docs/format/idml-values.md +++ b/docs/format/idml-values.md @@ -453,8 +453,10 @@ the converter writes that value on every `MarginPreference` ## Font platform name -`PlatformName` is `$ID/` in all 4,464 `Font` elements of the corpus IDML -files (`fonts.md`). The converter writes that value on every font. +`PlatformName` is `$ID/` in every `Font` element of the corpus IDML +files except the fonts of missing-font records, where it is read from +the record (`fonts.md`, missing fonts). The converter writes `$ID/` on +every other font. ## Number format diff --git a/docs/format/objects.md b/docs/format/objects.md index e230671..0caecdd 100644 --- a/docs/format/objects.md +++ b/docs/format/objects.md @@ -66,6 +66,16 @@ are in `big-endian.md`. | 0x4C01 | UID list: sections | | 0xA443 | Document users: u32 count, then per user a flag byte, the name as an in-object string, u32 colour | +**Script byte of the user names.** The second byte of each user name's +in-object string (the byte after the string tag) is a script code: 0 +in documents in Latin script, 1 in all 9 trustworthy pairs with +Japanese user or assignment names, 7 in a Russian, 25 in a Simplified +Chinese and 29 in a Czech document. The same codes are font writing +scripts (1 for Japanese fonts, 25 for Simplified Chinese fonts). All +users of a document have the same byte, except in one pair with users +of bytes 0 and 7. The converter uses the byte of the last user to +choose font family names (`fonts.md`, native family names). + **Document users.** IDML lists one `DocumentUser` per user in chunk 0xA443, `Self` being `dDocumentUser` and the index in hexadecimal (491 of 495 trustworthy pairs have as many users as the chunk; the other 4 are diff --git a/src/idml/designmap.rs b/src/idml/designmap.rs index d020cbb..28be76d 100644 --- a/src/idml/designmap.rs +++ b/src/idml/designmap.rs @@ -287,7 +287,7 @@ impl Writer<'_> { let font = match b.font { 0 => "$ID/".to_string(), f => match doc.fonts.get(&f) { - Some(f) => f.name.clone(), + Some(f) => self.family_names(f).0, None => continue, }, }; diff --git a/src/idml/kind.rs b/src/idml/kind.rs index 8d83921..a3aef34 100644 --- a/src/idml/kind.rs +++ b/src/idml/kind.rs @@ -184,14 +184,14 @@ impl Writer<'_> { Kind::Font => v .as_u32() .and_then(|u| self.doc.fonts.get(&u)) - .map(|f| text("string", f.name.clone())), + .map(|f| text("string", self.family_names(f).0)), Kind::FontOrNone => match v.as_u32()? { 0 => Some(text("string", "$ID/".into())), u => self .doc .fonts .get(&u) - .map(|f| text("string", f.name.clone())), + .map(|f| text("string", self.family_names(f).0)), }, Kind::String => v.as_string().map(|s| text("string", s)), Kind::StringOrNothing => v.as_string().map(|s| { diff --git a/src/idml/mod.rs b/src/idml/mod.rs index 1cb3881..213f33d 100644 --- a/src/idml/mod.rs +++ b/src/idml/mod.rs @@ -117,6 +117,26 @@ impl Writer<'_> { } } + /// The name IDML gives a font family, and the name its fonts' names + /// are made from: the native name when the family's writing script is + /// the script of the document users, and `$ID/` first for a built-in + /// name (`docs/format/fonts.md`, family names). + fn family_names(&self, f: &crate::model::font::FontFamily) -> (String, String) { + let native = self + .doc + .users_script + .is_some_and(|s| s != 0 && u32::from(s) == f.writing_script) + && !f.native_name.is_empty() + && f.native_name != f.name; + let base = if native { &f.native_name } else { &f.name }; + let idml = if f.builtin { + builtin_key(base) + } else { + base.clone() + }; + (idml, base.clone()) + } + fn style_ref(&self, uid: Option, paragraph: bool) -> String { let prefix = if paragraph { "ParagraphStyle" diff --git a/src/idml/resources.rs b/src/idml/resources.rs index e67ba0c..6359940 100644 --- a/src/idml/resources.rs +++ b/src/idml/resources.rs @@ -264,24 +264,32 @@ impl Writer<'_> { self.package_root(&mut x, "Fonts"); for f in self.doc.fonts.values() { let id = format!("di{:x}", f.uid); + let (family, base) = self.family_names(f); x.start("FontFamily") .attr("Self", &id) - .attr("Name", &f.name); + .attr("Name", &family); for font in &f.fonts { // A font with an empty style name is named `Regular`, and // its `Self` has the family name alone (fonts.md). let (name, key) = if font.style.is_empty() { - (format!("{} Regular", f.name), f.name.clone()) + (format!("{family} Regular"), base.clone()) } else { - let name = format!("{} {}", f.name, font.style); - (name.clone(), name) + ( + format!("{family} {}", font.style), + format!("{base} {}", font.style), + ) + }; + let style = if font.style_builtin { + builtin_key(&font.style) + } else { + font.style.clone() }; x.start("Font") .attr("Self", format!("{id}Fontn{key}")) - .attr("FontFamily", &f.name) + .attr("FontFamily", &base) .attr("Name", &name) .attr("PostScriptName", &font.postscript_name) - .attr("FontStyleName", &font.style); + .attr("FontStyleName", &style); if let Some(t) = font.type_name() { x.attr("FontType", t); } @@ -292,11 +300,16 @@ impl Writer<'_> { x.attr("FullName", &font.full_name) .attr("FullNameNative", &font.full_name_native) .attr("FontStyleNameNative", &font.style_native) - // `$ID/` in every Font of the corpus IDML files. - .attr("PlatformName", "$ID/") + // `$ID/` in every Font of the corpus IDML files but + // those of missing fonts (idml-values.md). + .attr( + "PlatformName", + font.platform_name.as_deref().unwrap_or("$ID/"), + ) .attr("Version", &font.version); - // IDML from InDesign 7 has no TypekitID. - if self.doc.version.major >= 12 { + // IDML from InDesign before 9.2 has no TypekitID (fonts.md). + let v = &self.doc.version; + if (v.major, v.minor) >= (9, 2) { x.attr("TypekitID", font.typekit_id.idml()); } x.end(); @@ -345,7 +358,10 @@ impl Writer<'_> { x.attr("BaselineShift", "0"); } if let Some(f) = self.doc.fonts.get(&e.font_family) { - Self::properties(&mut x, &[("AppliedFont", "string", f.name.clone().into())]); + Self::properties( + &mut x, + &[("AppliedFont", "string", self.family_names(f).0.into())], + ); } x.end(); } diff --git a/src/idml/spread.rs b/src/idml/spread.rs index 238ecf4..e79fdfa 100644 --- a/src/idml/spread.rs +++ b/src/idml/spread.rs @@ -718,7 +718,10 @@ impl Writer<'_> { .attr("CharacterAlignment", "AlignEmCenter"); } if let Some(f) = self.doc.fonts.get(&g.font) { - Self::properties(x, &[("AppliedFont", "string", f.name.clone().into())]); + Self::properties( + x, + &[("AppliedFont", "string", self.family_names(f).0.into())], + ); } x.end(); } diff --git a/src/model/document.rs b/src/model/document.rs index deace02..c3b4e41 100644 --- a/src/model/document.rs +++ b/src/model/document.rs @@ -20,6 +20,8 @@ pub struct Document { 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, /// Index sort groups (preferences chunk 0x1307E), in order: name, /// include flag and header variant. pub index_groups: Vec<(String, bool, u16)>, @@ -179,6 +181,7 @@ impl<'a> Reader<'a> { }); let constant_shade = self.constant_shade(); let assignments = self.assignments(); + let users_script = self.users_script(DOC); let users = self.users(DOC).unwrap_or_else(|e| { self.warn(format!("document users left out: {e}")); Vec::new() @@ -236,6 +239,7 @@ impl<'a> Reader<'a> { cell_styles: objects.cell_styles, table_styles: objects.table_styles, users, + users_script, sections, text_variables: objects.text_variables, hyperlinks: objects.hyperlinks, @@ -531,6 +535,8 @@ impl<'a> Reader<'a> { Some(d) => Some(FontFamily { uid, name: find_string(self.enc(), &d, 0)?, + builtin: false, + native_name: String::new(), fonts: Vec::new(), writing_script: 0, }), diff --git a/src/model/font.rs b/src/model/font.rs index 1e85373..f0e0612 100644 --- a/src/model/font.rs +++ b/src/model/font.rs @@ -1,7 +1,7 @@ //! Font families (class 0x3E03). See `docs/format/fonts.md`. use crate::Error; -use crate::object::{Encoding, Name, Object}; +use crate::object::{Encoding, Name, Object, builtin_key}; pub mod chunk { /// Family name and font records. @@ -15,6 +15,10 @@ pub mod chunk { pub struct FontFamily { pub uid: u32, pub name: String, + /// The name is a built-in key (written with `$ID/`). + pub builtin: bool, + /// The family name in the font's own script. + pub native_name: String, pub fonts: Vec, /// IDML `WritingScript` of every font in the family. pub writing_script: u32, @@ -23,8 +27,11 @@ pub struct FontFamily { #[derive(Debug, Clone, PartialEq)] pub struct Font { pub style: String, + /// The style is a built-in key (a missing font's `FontStyleName`). + pub style_builtin: bool, pub postscript_name: String, pub full_name: String, + /// `FontStyleNameNative` as IDML writes it. pub style_native: String, pub full_name_native: String, /// Font type code (see [`Font::type_name`]). @@ -32,16 +39,21 @@ pub struct Font { pub version: String, /// IDML `TypekitID`; the empty built-in key when the font has none. pub typekit_id: Name, + /// IDML `PlatformName` of a font in a missing-font record. + pub platform_name: Option, } impl Font { /// IDML `FontType` for the type code, if known. pub fn type_name(&self) -> Option<&'static str> { match self.font_type { + 0 => Some("Type1"), 1 => Some("TrueType"), + 3 => Some("ATC"), 6 => Some("OpenTypeCFF"), 7 => Some("OpenTypeCID"), 8 => Some("OpenTypeTT"), + 0xFFFF_FFFF => Some("Unknown"), _ => None, } } @@ -79,14 +91,31 @@ enum Record { Version3, } -/// Chunk 0x3E05: u8, u16, name, u8, native name, 6 bytes, u16 font count, -/// font records, u32 writing script. +/// Record kind (byte 0 of chunk 0x3E05) of a family whose fonts are +/// missing. +const MISSING: u8 = 3; + +/// Chunk 0x3E05: u8 record kind, u8, flagged name, flagged native name, 6 +/// bytes, u16 font count, font records, u32 writing script. A missing-font +/// record has other font records (`missing_fonts`). fn parse(enc: Encoding, uid: u32, data: &[u8], record: Record) -> Result { let mut c = enc.cursor(data); - c.skip(3)?; + let kind = c.u8()?; + c.u8()?; + let builtin = c.flag()? == 1; let name = c.string()?; - c.skip(1)?; - c.string()?; // native family name; IDML does not write it + c.flag()?; + let native_name = c.string()?; + if kind == MISSING && record == Record::Current { + return Ok(FontFamily { + uid, + name, + builtin, + native_name, + fonts: missing_fonts(&mut c)?, + writing_script: 0, + }); + } c.skip(6)?; let count = c.u16()?; let mut fonts = Vec::with_capacity(count as usize); @@ -102,8 +131,13 @@ fn parse(enc: Encoding, uid: u32, data: &[u8], record: Record) -> Result Result Result Result Result, Error> { + if c.remaining() == 6 { + c.skip(6)?; + return Ok(Vec::new()); + } + c.skip(4)?; + let count = c.u16()?; + let mut fonts = Vec::new(); + for _ in 0..count { + let style = c.name()?; + let platform = c.name()?; + c.name()?; + c.skip(10)?; + fonts.push(Font { + style: style.name, + style_builtin: style.builtin, + postscript_name: String::new(), + full_name: String::new(), + style_native: platform.idml(), + full_name_native: String::new(), + font_type: 0xFFFF_FFFF, + version: String::new(), + typekit_id: Name { + builtin: true, + name: String::new(), + }, + platform_name: Some(platform.idml()), + }); + } + if c.remaining() != 0 { + return Err(Error::Corrupt("missing-font record: bytes left".into())); + } + Ok(fonts) +} + /// Chunk 0x3EEB: u32 count, then per font a flag byte (1 = `$ID/` key) /// and a string. fn typekit_ids(enc: Encoding, data: &[u8]) -> Result, Error> { @@ -217,4 +294,34 @@ mod tests { ["$ID/", "TkD-1-ab"] ); } + + #[test] + fn parses_a_missing_font_record() { + let mut d = vec![3, 0, 1]; + d.extend(string("Aptos")); + d.push(1); + d.extend(string("Aptos")); + d.extend([0; 4]); + d.extend(1u16.to_le_bytes()); + for s in ["Regular", "Aptos", "Regular"] { + d.push(1); + d.extend(string(s)); + } + d.extend([0; 8]); + d.extend(2u16.to_le_bytes()); + let f = parse(Encoding::default(), 0x9E, &d, Record::Current).unwrap(); + assert!(f.builtin); + let font = &f.fonts[0]; + assert_eq!((font.style.as_str(), font.style_builtin), ("Regular", true)); + assert_eq!(font.platform_name.as_deref(), Some("$ID/Aptos")); + assert_eq!(font.type_name(), Some("Unknown")); + // Without fonts: 6 zero bytes end the record. + let mut d = vec![3, 0, 0]; + d.extend(string("Gone")); + d.push(0); + d.extend(string("")); + d.extend([0; 6]); + let f = parse(Encoding::default(), 0x9E, &d, Record::Current).unwrap(); + assert!(f.fonts.is_empty()); + } } diff --git a/src/model/settings.rs b/src/model/settings.rs index 91c7d12..f387ae2 100644 --- a/src/model/settings.rs +++ b/src/model/settings.rs @@ -173,6 +173,25 @@ impl<'a> Reader<'a> { .collect() } + /// The script byte of the last document user's name: the second byte + /// of its in-object string (`objects.md`, document users). Only read + /// in little-endian files. + pub(super) fn users_script(&self, doc: u32) -> Option { + if self.enc().big_endian() { + return None; + } + let d = self.chunk(doc, chunk::DOC_USERS).ok()??; + let mut c = self.cursor(&d); + let mut last = None; + for _ in 0..c.u32().ok()? { + c.flag().ok()?; + last = d.get(c.pos() + 1).copied(); + c.string().ok()?; + c.u32().ok()?; + } + last + } + /// The index sort groups of the preferences object, chunk 0x1307E: /// name, include flag and header variant of each group, in stored /// order. Empty when the chunk does not parse to its end. See