feat: write style import flags, unique IDs and common style values

Paragraph and character styles get Imported (the u16 after the style
kind) and StyleUniqueId (the GUID after the name) from the INDD, and the
values every non-root style has in IDML, such as PreviewColor, from
observation. The values generator leaves root styles out of the style
paths.

Value coverage on trustworthy pairs: 84.07 % to 84.29 %.
This commit is contained in:
aiongg committed 2026-10-07 12:14:48 +00:00
1 parent 463fef540a
commit e6dd2d629f
7 files changed
+103 -8

No files matched your search

+20
View File
@@ -265,6 +265,26 @@ same value, in at least 3 files (a `Keyed` block of the values file):
63 languages. Romanian and Japanese have two variants each and are left
out.
### Paragraph and character styles
The styles other than the root styles (5,505 paragraph and 2,284
character styles in the IDML files):
| Values kept | Styles | Versions |
|---|---|---|
| `PreviewColor` `Nothing` | paragraph and character | 7 on |
| `EpubAriaRole=""` | paragraph and character | 21 on |
| `IncludeClass="true"` | character | 13 on |
| `ExtendedKeyboardShortcut="0 0 0"` | character | 15 on |
| `EmitCss="true"` where written | paragraph and character | 10 on |
| `SplitDocument="false"` where written | character | 10 on |
`KeyboardShortcut` and the paragraph styles' `ExtendedKeyboardShortcut`,
`SplitDocument` and `IncludeClass` are left out: a few styles have
other values (keyboard shortcuts such as `16 82`), whose INDD field was
not found. `Imported` and `StyleUniqueId` come from the INDD
(`objects.md`, styles).
### Swatches
| Element | Values kept |
+14 -4
View File
@@ -788,14 +788,24 @@ last paragraph return of a story is not written to IDML.
Paragraph and character styles share the class. Chunk 0x230: u32 next
style (0 = itself), u32 based-on style, fields not yet identified, u16 1
for paragraph styles or 0 for character styles, u16 not identified, u8 1
if the name is a built-in key (`$ID/` in IDML), the name as an in-object
string, then a GUID string in newer files. The name's offset varies (23–26
for paragraph styles or 0 for character styles, u16 `Imported` (1 =
true), u8 1 if the name is a built-in key (`$ID/` in IDML), the name as
an in-object string, then, in newer files, a GUID string
(`StyleUniqueId`). The name's offset varies (23–26
bytes), so the converter locates it as a flag byte followed by a valid
in-object string.
All 486 style names and 291 `NextStyle` values in the pairs match.
**Kind field.** The kind and the unidentified u16 after it can be read
**Imported and unique ID.** Over the 4,694 paragraph and character
styles of the trustworthy pairs whose IDML style has the same name, the
u16 after the kind equals `Imported` in 4,694 (180 imported). The GUID
is a 36-character in-object string after the name; where it is stored,
IDML has it as `StyleUniqueId` in 2,769 of 2,793 styles (DOM 11 on; the
other 24 have another GUID in IDML). Styles without a stored GUID
(1,108 from DOM 11 on) have a `StyleUniqueId` in IDML that the INDD does
not hold, which the converter leaves out.
**Kind field.** The kind and the `Imported` u16 after it can be read
as one u32 in most files, because the second u16 is 0. The InDesign 7.5
template fixture `scml-template/scml.indt` (no IDML) has 1 there in 600
of its styles: 151 have `00 00 01 00` before the flag and 449 have
+20
View File
@@ -35,6 +35,16 @@ tools/element_values.py; evidence in docs/format/idml-values.md.
<Layer ShowGuides="true" Expendable="true" />
<Gradient ColorEditable="true" ColorRemovable="true" SwatchCreatorID="7937" />
<Swatch ColorEditable="false" ColorRemovable="false" Visible="true" SwatchCreatorID="7937" />
<ParagraphStyle>
<Properties>
<PreviewColor type="enumeration">Nothing</PreviewColor>
</Properties>
</ParagraphStyle>
<CharacterStyle>
<Properties>
<PreviewColor type="enumeration">Nothing</PreviewColor>
</Properties>
</CharacterStyle>
<Document>
<ConditionalTextPreference Present="true" ShowConditionIndicators="ShowIndicators" ActiveConditionSet="n" />
<LinkedStoryOption UpdateWhileSaving="false" WarnOnUpdateOfEditedStory="true" RemoveForcedLineBreaks="false" />
@@ -145,6 +155,7 @@ tools/element_values.py; evidence in docs/format/idml-values.md.
</Document>
</Values>
<Values MinimumVersion="13">
<CharacterStyle IncludeClass="true" />
<Document>
<EndnoteOption Present="true" EndnoteTitleStyle="ParagraphStyle/$ID/NormalParagraphStyle" StartEndnoteNumberAt="1" EndnotePrefix="" EndnoteSuffix="">
<Properties>
@@ -162,6 +173,9 @@ tools/element_values.py; evidence in docs/format/idml-values.md.
<AdjustLayoutPreference Present="true" AllowLockedObjectsToAdjust="true" ImposeFontSizeRestriction="false" MinimumFontSize="6" MaximumFontSize="324" />
</Document>
</Values>
<Values MinimumVersion="15">
<CharacterStyle ExtendedKeyboardShortcut="0 0 0" />
</Values>
<Values MinimumVersion="18">
<Tint ConvertToHsb="false" />
</Values>
@@ -190,6 +204,8 @@ tools/element_values.py; evidence in docs/format/idml-values.md.
<ObjectStyle>
<ObjectExportOption EpubAriaRole="$ID/" />
</ObjectStyle>
<ParagraphStyle EpubAriaRole="" />
<CharacterStyle EpubAriaRole="" />
</Values>
<WhenWritten MinimumVersion="7">
<TextFrame>
@@ -201,6 +217,10 @@ tools/element_values.py; evidence in docs/format/idml-values.md.
<TextFramePreference UseFlexibleColumnWidth="false" />
</TextFrame>
</WhenWritten>
<WhenWritten MinimumVersion="10">
<ParagraphStyle EmitCss="true" />
<CharacterStyle SplitDocument="false" EmitCss="true" />
</WhenWritten>
<WhenWritten MinimumVersion="13">
<TextFrame>
<TextFramePreference FootnotesEnableOverrides="false" FootnotesSpanAcrossColumns="false" />
+14 -1
View File
@@ -2817,6 +2817,10 @@ impl Writer<'_> {
for (k, v) in &plain {
x.attr(k, v);
}
x.attr("Imported", s.imported.to_string());
if let Some(id) = &s.unique_id {
x.attr("StyleUniqueId", id);
}
// Root styles also get the values every exported IDML has on them;
// values read from the INDD take precedence.
let mut extra = Vec::new();
@@ -2824,9 +2828,18 @@ impl Writer<'_> {
let written: Vec<&str> = plain.iter().map(|(k, _)| *k).collect();
let (attrs, more, _) = self.root_values(tag, &written, &props);
for (k, v) in &attrs {
x.attr(k, v);
if !x.has_attr(k) {
x.attr(k, v);
}
}
extra = more;
} else if let Some(n) = values::element(tag, doc.version.major) {
// Other styles get the values every IDML has on them.
x.attrs_missing(n.attrs.iter());
x.attrs_missing(values::when_written(tag, doc.version.major).iter());
if let Some(p) = n.child("Properties") {
extra = p.children.clone();
}
}
if let Some(base) = s.based_on.and_then(|b| doc.styles.get(&b)) {
// The root "[No ... style]" is written as a string.
+17
View File
@@ -537,6 +537,10 @@ pub struct Style {
pub based_on: Option<u32>,
pub next: Option<u32>,
pub attrs: Attrs,
/// The u16 after the kind, before the name (IDML `Imported`).
pub imported: bool,
/// The GUID string after the name in newer files (`StyleUniqueId`).
pub unique_id: Option<String>,
}
/// A stretch of story text with one paragraph style and one character style.
@@ -2377,6 +2381,17 @@ impl<'a> Reader<'a> {
return Err(Error::Corrupt(format!("style {uid}: no name")));
};
let paragraph = Cursor::new(&data[at - 4..]).u16()? != 0;
let imported = Cursor::new(&data[at - 2..]).u16()? != 0;
// A 36-character in-object string after the name: a GUID.
const GUID: [u8; 6] = [2, 0, 36, 0, 36, 0x40];
let unique_id = if crate::object::big_endian() {
None
} else {
data[at..]
.windows(GUID.len())
.position(|w| w == GUID)
.and_then(|i| Cursor::new(&data[at + i..]).string().ok())
};
let attrs = match self.chunk(uid, chunk::STYLE_ATTRS)? {
Some(d) if d.len() >= 2 => {
let mut c = Cursor::new(&d);
@@ -2393,6 +2408,8 @@ impl<'a> Reader<'a> {
based_on: uid_or_none(based_on),
next: uid_or_none(next),
attrs,
imported,
unique_id,
}))
}
+5 -2
View File
@@ -186,7 +186,8 @@ fn little_endian_fixtures_convert() {
}
/// The InDesign 7.5 template has character styles whose kind field is
/// followed by a non-zero u16 (`docs/format/objects.md`, Styles).
/// followed by a non-zero u16, the imported flag
/// (`docs/format/objects.md`, Styles).
#[test]
fn style_kind_is_a_u16() {
let Some(root) = fixtures() else {
@@ -196,7 +197,9 @@ fn style_kind_is_a_u16() {
let mut out = Vec::new();
indd::convert(&bytes, "test.indd", &mut out).unwrap();
let text = String::from_utf8_lossy(&out);
assert!(text.contains(r#"<CharacterStyle Self="CharacterStyle/abbr" Name="abbr">"#));
assert!(text.contains(r#"<CharacterStyle Self="CharacterStyle/abbr" Name="abbr" "#));
// The u16 after the kind is the imported flag.
assert!(text.contains(r#"Name="abbr" Imported="true""#));
assert!(!text.contains(r#"<CharacterStyle Self="ParagraphStyle/"#));
}
+13 -1
View File
@@ -67,6 +67,7 @@ ELEMENTS = [
"Spread", "MasterSpread", "Page", "Spread/FlattenerPreference", "Layer",
"TextFrame/TextFramePreference", "TextFrame/TextFrameFootnoteOptionsObject",
"Color", "Tint", "Gradient", "Swatch", "Guide", "ObjectStyle/ObjectExportOption",
"ParagraphStyle", "CharacterStyle",
"Document/ConditionalTextPreference", "Document/EndnoteOption",
"Document/TextFrameFootnoteOptionsObject", "Document/LinkedStoryOption",
"Document/LinkedPageItemOption", "Document/WatermarkPreference",
@@ -96,11 +97,22 @@ WHEN_WRITTEN = {
"VerticalThreshold", "UseFlexibleColumnWidth",
"MinimumFirstBaselineOffset",
],
"ParagraphStyle": ["EmitCss"],
"CharacterStyle": ["EmitCss", "SplitDocument"],
}
# Values that are nearly constant; the exceptions are read from the INDD.
# (path, key) -> reason, as recorded in docs/format/idml-values.md.
EXPLAINED = {}
EXPLAINED_SHARE = 0.995
# The root styles, whose values tools/root_values.py collects; they are
# left out of the style paths here.
ROOT_STYLES = {
"ParagraphStyle/$ID/[No paragraph style]",
"CharacterStyle/$ID/[No character style]",
"ObjectStyle/$ID/[None]",
"CellStyle/$ID/[None]",
"TableStyle/$ID/[No table style]",
}
# Attributes that are names or references of the element itself, or
# written by the converter from the document structure.
SKIP = {"Self", "Name"}
@@ -168,7 +180,7 @@ def scan(arg):
lists["keyed:" + el.tag].append(dict(el.attrib))
if el.get("Self") is None:
continue
if el.tag in wanted:
if el.tag in wanted and el.get("Self") not in ROOT_STYLES:
found[el.tag].append(flatten(el))
for ch in el:
path = f"{el.tag}/{ch.tag}"