feat: write alternate glyphs (text attribute 0x42AE)

Text attribute 0x42AE (value type 0x42DF) holds the OpenType feature of
a glyph chosen from its alternates: u16 count, then per feature four tag
bytes and a u32 number. IDML writes it as Properties/OpenTypeFeatures,
a list of [$ID/<tag>, number]. The converter writes values with one
feature and leaves out the empty lists that styles store.

Evidence (docs/format/attributes.md, alternate glyphs): 127 of 127 runs
in the trustworthy pairs, 11 tags; tag byte order checked on 7
big-endian files.

compare.py --exclude own/: trustworthy values reproduced 28,698,890 ->
28,699,017 (+127, all CSR.P.OpenTypeFeatures gaps); extras 92,883
unchanged; no pair loses values; 0 conversion failures.
This commit is contained in:
aiongg committed 2026-10-10 21:20:38 +00:00
1 parent d46a160c6b
commit 8c4cf32d51
6 files changed
+157

No files matched your search

+31
View File
@@ -1035,6 +1035,37 @@ The converter leaves out records with an empty name. It reproduces 1,499
of 1,499 values in the trustworthy pairs, with no extra value. The glyph
ID is not used.
**Alternate glyphs (0x42AE).** A glyph chosen from the alternates of an
OpenType feature (for example in the Glyphs panel) is stored on its run
as attribute 0x42AE, value type 0x42DF: u16 count *n*, then per feature
four bytes of the feature tag in reading order and a u32 number, in the
file's byte order. IDML writes `Properties/OpenTypeFeatures` (type
`list`) on the character range, after `AppliedFont`: one `ListItem` of
type `list` per feature, holding a `ListItem` of type `string` with
`$ID/` and the tag and a `ListItem` of type `long` with the number:
```xml
<OpenTypeFeatures type="list">
<ListItem type="list">
<ListItem type="string">$ID/aalt</ListItem>
<ListItem type="long">1</ListItem>
</ListItem>
</OpenTypeFeatures>
```
Evidence, in the corpus of 2026-10: 127 of 127 runs in the trustworthy
pairs (27 files) have the IDML value at the same story offset, with 11
tags (`ss01` to `ss07`, `aalt`, `ccmp`, `liga`, `nalt`) and numbers 1,
2 and 8; no IDML range has `OpenTypeFeatures` without the attribute.
Every run in the corpus stores one feature (*n* = 1). The tag order in
both byte orders comes from 7 big-endian files (29 runs, no IDML): the
count and number are big-endian there, the tag bytes are not reversed.
Styles store the attribute with *n* = 0 (2,230 styles of the paired
files); no IDML style has `OpenTypeFeatures`, and the converter writes
nothing for them. The IDML form of several features is not shown, so
the converter leaves out values with *n* other than 1, and values whose
tag is not printable ASCII or that do not fill the value exactly.
### Ruby, kenten and warichu
These attributes are in story runs (character and paragraph runs, chunk
+1
View File
@@ -56,6 +56,7 @@ Status values:
| u16 0 and u16 1 after `SeparatorStyle` | TOC entry, chunk 0x11605 | – | open: never varies | one may be `SortAlphabet` (`false` in all 255 entries) | `objects.md`, table of contents styles |
| u16 after `Level` | TOC entry, chunk 0x11605 | – | open: no pair | 0 in every paired entry, 1 in some entries of files without a pair | `objects.md`, table of contents styles |
| Value 2 of 0x1B09 with no IDML attribute | text attribute | 2 | open: no field found | `NextPageNumber` stored where IDML writes no attribute | `attributes.md`, page number type |
| Several alternate glyph features | text attribute 0x42AE | 0 | open: no sample | every run stores one feature; the IDML form of more than one is not shown, so such values are left out | `attributes.md`, alternate glyphs |
| `ResultText` of other text variables | `TextVariableInstance` | 200 | open: not stored | page numbers, dates and cross-reference page numbers depend on the layout or the export time; the last page number shows `1` in every sample | `text-variables.md` |
| Saving edition rule exceptions | tints, users, endnote and index titles | 9 | not needed | the IDML was exported by another edition than the one that saved the INDD (one trustworthy document, 8 stale pairs) | `objects.md`, saving edition |
| `ColorGroup` `Self` | `ColorGroup` | 2 | open: not examined | 6 extra values: groups whose `Self` differs from the one the DOM version gives | `objects.md`, colour groups |
+27
View File
@@ -315,6 +315,7 @@ pub(super) const TEXT_ATTRS: &[(u32, &str, Kind, bool)] = &[
(0x1B29, "TabList", Kind::TabList, true),
(0x1B2A, "Underline", Kind::Equals(1), false),
(0x1B2B, "AppliedFont", Kind::Font, true),
(0x42AE, "OpenTypeFeatures", Kind::Features, true),
(
0x1B2C,
"OTFFigureStyle",
@@ -964,6 +965,9 @@ pub(super) enum PropValue {
Text(String),
/// Records, each written as a `ListItem` (type `list`).
List(Vec<Vec<Field>>),
/// Lists, each written as a `ListItem` of type `list` holding one
/// `ListItem` per value (type, text).
Lists(Vec<Vec<(&'static str, String)>>),
/// An empty element with these attributes and no `type`.
Attributes(Vec<(&'static str, String)>),
}
@@ -2043,6 +2047,29 @@ mod tests {
assert!(matches!(&props[0].2, PropValue::Text(t) if t == "Nothing"));
}
#[test]
fn writes_one_alternate_glyph_feature() {
let doc = Document::default();
let w = Writer::for_test(&doc);
let mut a = Attrs::default();
a.values = vec![(0x42AE, Value::Features(vec![("nalt".into(), 8)]))];
let (_, props) = w.text_attrs(&a);
assert_eq!(props.len(), 1);
assert_eq!((props[0].0, props[0].1), ("OpenTypeFeatures", "list"));
let PropValue::Lists(lists) = &props[0].2 else {
panic!("not a list of lists");
};
assert_eq!(
lists,
&[vec![("string", "$ID/nalt".into()), ("long", "8".into())]]
);
// An empty list (as styles store) and several features are left out.
for f in [vec![], vec![("aalt".into(), 1), ("ss01".into(), 1)]] {
a.values = vec![(0x42AE, Value::Features(f))];
assert!(w.text_attrs(&a).1.is_empty());
}
}
#[test]
fn writes_cell_edges_along_the_edge() {
use crate::model::table::{Cell, CellFormat, CellKind, Row, Table};
+27
View File
@@ -141,6 +141,24 @@ pub(super) fn prop_node(p: &Property) -> Node {
});
}
}
PropValue::Lists(lists) => {
for list in lists {
n.children.push(Node {
tag: "ListItem".into(),
attrs: vec![("type".into(), "list".into())],
children: list
.iter()
.map(|(t, text)| Node {
tag: "ListItem".into(),
attrs: vec![("type".into(), t.to_string())],
text: Some(text.clone()),
children: Vec::new(),
})
.collect(),
text: None,
});
}
}
}
n
}
@@ -386,6 +404,15 @@ impl Writer<'_> {
x.end();
}
}
PropValue::Lists(lists) => {
for list in lists {
x.start("ListItem").attr("type", "list");
for (ty, text) in list {
x.start("ListItem").attr("type", *ty).text(text).end();
}
x.end();
}
}
}
x.end();
}
+13
View File
@@ -74,6 +74,9 @@ pub(super) enum Kind {
String,
/// A glyph name, written as `$ID/<name>`; left out when empty.
GlyphName,
/// One OpenType feature of a single glyph, written as a list of the
/// tag (`$ID/<tag>`) and the number; other counts are left out.
Features,
/// Dash and gap lengths, separated by spaces.
Dashes,
/// The corner adjustment of a dash list: 3 `DashesAndGaps`.
@@ -276,6 +279,16 @@ impl Writer<'_> {
}
_ => None,
},
Kind::Features => match v {
Value::Features(f) if f.len() == 1 => Some((
"list",
PropValue::Lists(vec![vec![
("string", format!("$ID/{}", f[0].0)),
("long", f[0].1.to_string()),
]]),
)),
_ => None,
},
Kind::StringOrNothing => v.as_string().map(|s| {
if s.is_empty() {
text("enumeration", "Nothing".into())
+58
View File
@@ -56,6 +56,9 @@ pub enum Value {
Dashes(Vec<f64>, u16),
/// A custom glyph: its name (the glyph ID that follows is not used).
Glyph(String),
/// OpenType features chosen for single glyphs: four-character feature
/// tag and the number of the alternate (attribute 0x42AE).
Features(Vec<(String, u32)>),
/// Opacity gradient stops: location (0–1), absolute position of the
/// midpoint to the next stop (0–1), opacity in percent.
Stops(Vec<[f64; 3]>),
@@ -364,6 +367,8 @@ enum Layout {
RefOrCode,
/// u16 length, that many bytes of a glyph name, u32 glyph ID.
Glyph,
/// u16 count, then per feature four tag bytes and a u32 number.
Features,
}
fn text_layout(id: u32) -> Option<Layout> {
@@ -382,6 +387,7 @@ fn text_layout(id: u32) -> Option<Layout> {
0x1BBB => Layout::LineStyles,
0x1A406 => Layout::BulletChar,
0x1B5E => Layout::Glyph,
0x42AE => Layout::Features,
// Stroke types of cell edges, table borders, rows and columns
// (tables.md).
0xB64D | 0xB64E | 0xB64F | 0xB650 | 0xB655 | 0xB658 | 0xB65B | 0xB65E | 0xB688 | 0xB689
@@ -451,10 +457,29 @@ fn decode_text(enc: Encoding, id: u32, t: u32, data: &[u8]) -> Value {
String::from_utf8(name).ok()
})()
.map(Value::Glyph),
Layout::Features => features(&mut c).map(Value::Features),
};
value.unwrap_or_else(|| Value::Other(t, data.to_vec()))
}
/// The features of attribute 0x42AE (`docs/format/attributes.md`,
/// alternate glyphs): the tag bytes are in reading order in either byte
/// order. `None` unless every tag is printable ASCII and the features
/// fill the value exactly.
fn features(c: &mut Cursor) -> Option<Vec<(String, u32)>> {
let n = c.u16().ok()? as usize;
let mut out = Vec::with_capacity(n.min(c.remaining() / 8));
for _ in 0..n {
let tag = c.bytes(4).ok()?;
if !tag.iter().all(|b| (0x20..0x7F).contains(b)) {
return None;
}
let tag = String::from_utf8(tag.to_vec()).ok()?;
out.push((tag, c.u32().ok()?));
}
(c.remaining() == 0).then_some(out)
}
/// A tab list: u16 count, then per stop f64 position, u16 alignment (for
/// code 3 followed by the alignment character as a UTF-16 unit), u16
/// leader length and the leader in UTF-16 code units. `None` unless the
@@ -639,6 +664,39 @@ mod tests {
));
}
#[test]
fn decodes_alternate_glyph_features_in_both_byte_orders() {
use crate::header::{ByteOrder, Version};
let le = Encoding::default();
let mut d = 1u16.to_le_bytes().to_vec();
d.extend_from_slice(b"aalt");
d.extend_from_slice(&2u32.to_le_bytes());
let aalt = Value::Features(vec![("aalt".into(), 2)]);
assert_eq!(decode_text(le, 0x42AE, 0x42DF, &d), aalt);
let be = Encoding::new(ByteOrder::Big, Version { major: 4, minor: 0 });
let mut b = 1u16.to_be_bytes().to_vec();
b.extend_from_slice(b"aalt");
b.extend_from_slice(&2u32.to_be_bytes());
assert_eq!(decode_text(be, 0x42AE, 0x42DF, &b), aalt);
// A style stores an empty list.
assert_eq!(
decode_text(le, 0x42AE, 0x42DF, &[0, 0]),
Value::Features(Vec::new())
);
// A count larger than the data, or a tag that is not text.
d[0] = 2;
assert!(matches!(
decode_text(le, 0x42AE, 0x42DF, &d),
Value::Other(..)
));
d[0] = 1;
d[2] = 0;
assert!(matches!(
decode_text(le, 0x42AE, 0x42DF, &d),
Value::Other(..)
));
}
#[test]
fn parses_a_stroke_type_code() {
// 0x6E6E: reference 0, code 0x5A39, then the 6-byte trailer value.