mirror of
https://github.com/storytold/indd-utils.git
synced 2026-11-26 04:12:04 +01:00
feat: convert XML tags
XML tags (class 0xBF19) hold their name in chunk 0xBF2F and refer to an interface colour in chunk 0x117. Write them in XML/Tags.xml, sorted by name as IDML lists them.
This commit is contained in:
1 parent
7bde5098a6
commit
201697c779
3 files changed
+105
No files matched your search
@@ -877,3 +877,36 @@ IDML `ABullet` elements in 78 of 78 and the records end the chunk. All
|
||||
396 bullets match on type, value and font style, and 395 on the font;
|
||||
the other is in the pair whose IDML names the family `Minion Pro (OTF)`
|
||||
(`fonts.md`).
|
||||
|
||||
## XML tags (0xBF19)
|
||||
|
||||
Chunk 0xBF2F is a u32 length and the tag name as text segments; chunk
|
||||
0x117 is the UID of the tag's colour, an object of class 0x1F11 whose
|
||||
chunk 0x1F01 holds u32 colour space 5 (RGB), u16 3 and three f64
|
||||
fractions, as for swatches. IDML writes each tag as an `XMLTag` in
|
||||
`XML/Tags.xml`, with `Self="XMLTag/<Name>"` and the colour as the
|
||||
`TagColor` property. In all 78 pairs the number of tag objects equals the
|
||||
number of `XMLTag` elements and the names match (87 tags). IDML lists
|
||||
them by name, ignoring case (the one pair with more than one tag, 10
|
||||
tags).
|
||||
|
||||
| Red, green, blue | `TagColor` | Evidence |
|
||||
|---|---|---|
|
||||
| 0.31, 0.6, 1 | `LightBlue` | 78 tags (`Root`) |
|
||||
| 1, 0, 0 | `Red` | 1 |
|
||||
| 0.31, 1, 0.31 | `Green` | 1 |
|
||||
| 0, 0, 1 | `Blue` | 1 |
|
||||
| 1, 1, 0.31 | `Yellow` | 1 |
|
||||
| 1, 0.31, 1 | `Magenta` | 1 |
|
||||
| 0, 1, 1 | `Cyan` | 1 |
|
||||
| 0.5, 0.5, 0.5 | `Gray` | 1 |
|
||||
| 0, 0, 0 | `Black` | 1 |
|
||||
| 0.6, 0, 0 | `BrickRed` | 1 |
|
||||
|
||||
Each colour other than light blue occurs once, all in one file. The
|
||||
converter leaves out `TagColor` for other colours.
|
||||
|
||||
The XML structure itself is not converted: the backing story (named by
|
||||
document chunk 0xBF14 in 76 of 78 pairs) and its `XMLElement` elements,
|
||||
whose `Self` (`di2`, `di3`, `di2ib`, …) does not follow from a UID found
|
||||
so far.
|
||||
@@ -81,6 +81,27 @@ fn anchored_settings(d: &[u8]) -> Vec<(&'static str, String)> {
|
||||
out
|
||||
}
|
||||
|
||||
/// The IDML name of an XML tag colour (red, green, blue fractions);
|
||||
/// `None` for colours without evidence. See `docs/format/objects.md`.
|
||||
fn xml_tag_color(rgb: [f64; 3]) -> Option<&'static str> {
|
||||
const NAMES: [([f64; 3], &str); 10] = [
|
||||
([0.31, 0.6, 1.0], "LightBlue"),
|
||||
([1.0, 0.0, 0.0], "Red"),
|
||||
([0.31, 1.0, 0.31], "Green"),
|
||||
([0.0, 0.0, 1.0], "Blue"),
|
||||
([1.0, 1.0, 0.31], "Yellow"),
|
||||
([1.0, 0.31, 1.0], "Magenta"),
|
||||
([0.0, 1.0, 1.0], "Cyan"),
|
||||
([0.5, 0.5, 0.5], "Gray"),
|
||||
([0.0, 0.0, 0.0], "Black"),
|
||||
([0.6, 0.0, 0.0], "BrickRed"),
|
||||
];
|
||||
NAMES
|
||||
.iter()
|
||||
.find(|(c, _)| c.iter().zip(rgb).all(|(a, b)| (a - b).abs() < 1e-4))
|
||||
.map(|(_, n)| *n)
|
||||
}
|
||||
|
||||
/// Frame fitting attributes of `attrs` with IDs in `ids`, in IDML order.
|
||||
fn fitting_attrs(attrs: &Attrs, ids: &[u32]) -> Vec<(&'static str, String)> {
|
||||
FITTING_ATTRS
|
||||
@@ -3140,6 +3161,18 @@ impl Writer<'_> {
|
||||
fn tags(&self) -> String {
|
||||
let mut x = Xml::new();
|
||||
self.package_root(&mut x, "Tags");
|
||||
// IDML lists the tags by name, ignoring case.
|
||||
let mut tags: Vec<&(String, Option<[f64; 3]>)> = self.doc.xml_tags.iter().collect();
|
||||
tags.sort_by_key(|(name, _)| name.to_lowercase());
|
||||
for (name, color) in tags {
|
||||
x.start("XMLTag")
|
||||
.attr("Self", format!("XMLTag/{}", self_name(name)))
|
||||
.attr("Name", name);
|
||||
if let Some(c) = color.and_then(xml_tag_color) {
|
||||
Self::properties(&mut x, &[("TagColor", "enumeration", c.to_string().into())]);
|
||||
}
|
||||
x.end();
|
||||
}
|
||||
x.end();
|
||||
x.finish()
|
||||
}
|
||||
|
||||
@@ -61,6 +61,9 @@ pub mod class {
|
||||
/// Document-wide preferences.
|
||||
pub const PREFERENCES: u32 = 0x2202;
|
||||
pub const GUIDE: u32 = 0x3301;
|
||||
pub const XML_TAG: u32 = 0xBF19;
|
||||
/// A colour used in the interface (XML tags).
|
||||
pub const UI_COLOR: u32 = 0x1F11;
|
||||
}
|
||||
|
||||
/// Chunk IDs.
|
||||
@@ -108,6 +111,8 @@ pub mod chunk {
|
||||
pub const ROOT_GROUP_KIND: u32 = 0x28C2;
|
||||
pub const SECTION_INFO: u32 = 0x4C02;
|
||||
pub const DOCUMENT_PREFERENCES: u32 = 0x533;
|
||||
pub const XML_TAG_NAME: u32 = 0xBF2F;
|
||||
pub const XML_TAG_COLOR: u32 = 0x117;
|
||||
/// Bullet characters, in the preferences object.
|
||||
pub const BULLETS: u32 = 0x1A488;
|
||||
pub const FRAME_COLUMNS: u32 = 0x2D1;
|
||||
@@ -476,6 +481,8 @@ pub struct Document {
|
||||
pub color_groups: Vec<ColorGroup>,
|
||||
/// The bullet characters offered for lists (preferences chunk 0x1A488).
|
||||
pub bullets: Vec<Bullet>,
|
||||
/// XML tags (class 0xBF19): name and colour (red, green, blue).
|
||||
pub xml_tags: Vec<(String, Option<[f64; 3]>)>,
|
||||
}
|
||||
|
||||
/// Document setup, from chunk 0x533 of the preferences object.
|
||||
@@ -691,6 +698,7 @@ impl<'a> Reader<'a> {
|
||||
let mut composite_fonts = Vec::new();
|
||||
let mut composite_entries = HashMap::new();
|
||||
let mut cjk_tables = Vec::new();
|
||||
let mut xml_tags = Vec::new();
|
||||
let mut inks = Vec::new();
|
||||
let mut color_groups = HashMap::new();
|
||||
for &(uid, cls) in self.db.classes() {
|
||||
@@ -902,6 +910,20 @@ impl<'a> Reader<'a> {
|
||||
color_groups.insert(uid, g);
|
||||
}
|
||||
}
|
||||
class::XML_TAG => {
|
||||
// Chunk 0xBF2F: u32 length, then the name as text
|
||||
// segments; chunk 0x117: the UID of the tag's colour.
|
||||
if let Some(d) = self.chunk(uid, chunk::XML_TAG_NAME)? {
|
||||
let mut c = Cursor::new(&d);
|
||||
let n = c.u32()? as usize;
|
||||
let name = c.segments(n)?;
|
||||
let color = match self.chunk(uid, chunk::XML_TAG_COLOR)? {
|
||||
Some(d) if d.len() >= 4 => self.ui_color(Cursor::new(&d).u32()?)?,
|
||||
_ => None,
|
||||
};
|
||||
xml_tags.push((name, color));
|
||||
}
|
||||
}
|
||||
class::LANGUAGE => {
|
||||
if let Some(d) = self.chunk(uid, chunk::LANGUAGE_NAME)?
|
||||
&& d.len() > 1
|
||||
@@ -983,9 +1005,26 @@ impl<'a> Reader<'a> {
|
||||
.filter_map(|u| color_groups.remove(&u))
|
||||
.collect(),
|
||||
bullets: self.bullets()?,
|
||||
xml_tags,
|
||||
})
|
||||
}
|
||||
|
||||
/// An interface colour (class 0x1F11): chunk 0x1F01 holds u32 space
|
||||
/// (5 = RGB), u16 count and the components as f64 fractions.
|
||||
fn ui_color(&self, uid: u32) -> Result<Option<[f64; 3]>, Error> {
|
||||
if self.class(uid) != Some(class::UI_COLOR) {
|
||||
return Ok(None);
|
||||
}
|
||||
let Some(d) = self.chunk(uid, color::chunk::COLOR_VALUE)? else {
|
||||
return Ok(None);
|
||||
};
|
||||
let mut c = Cursor::new(&d);
|
||||
if c.u32()? != 5 || c.u16()? != 3 {
|
||||
return Ok(None);
|
||||
}
|
||||
Ok(Some([c.f64()?, c.f64()?, c.f64()?]))
|
||||
}
|
||||
|
||||
/// The bullet characters in the preferences object (chunk 0x1A488):
|
||||
/// u16 1, u32 count, then the bullets.
|
||||
fn bullets(&self) -> Result<Vec<Bullet>, Error> {
|
||||
|
||||
Reference in new issue
Block a user