mirror of
https://github.com/storytold/indd-utils.git
synced 2026-11-21 17:50:47 +01:00
feat: write mixed inks, mixed ink groups and the alternate colour of tints
A colour object with model code 3 is a mixed ink: chunk 0x1F09 lists its inks, chunk 0x1F01 (colour space 9) their fractions and chunk 0x102D the spot colours. With chunk 0x1F24 it is a mixed ink group; colours of space 9 whose chunk 0x117 names a group are its members and take the group's lists. They are written as MixedInkGroup and MixedInk elements after the inks (mixed inks sorted by name) instead of Color elements, and fills and colour group swatches refer to them. A tint gets the alternate colour of its base colour. Evidence in objects.md. compare.py --exclude own/, trustworthy pairs: 81 of 81 mixed inks and groups with every attribute (+1,140 values), Tint AlternateSpace and AlternateColorValue 0 -> 171 of 171, ColorGroupSwatch and fill colour references +132; 0 lost on any key. Extra values: the 1,077 values of Color elements written for mixed inks are gone; 66 more on tints the reference IDML leaves out (their two new attributes).
This commit is contained in:
1 parent
d34092793e
commit
265cf651c6
5 files changed
+231
-6
No files matched your search
+41
-5
@@ -2874,11 +2874,42 @@ out, with a warning, for any other code.
|
||||
|
||||
**Code 3: mixed inks.** The corpus after 2026-10 has 47 colours with
|
||||
code 3 in 27 files, 16 of them pairs (InDesign 12.1 to 17.0, typeset in
|
||||
Korean). Their IDML has no `Color` element for them; it lists
|
||||
`MixedInk` elements in `Resources/Graphic.xml` (`Model="Mixedinkmodel"`,
|
||||
`Space="MixedInk"`, `InkList`, `InkPercentages`, `BaseColor` and the
|
||||
spot colour lists). The fields of the ink list are not decoded, so the
|
||||
converter writes neither element for these colours.
|
||||
Korean). Their IDML has no `Color` element for them; it lists `MixedInk`
|
||||
and `MixedInkGroup` elements in `Resources/Graphic.xml`. A colour object
|
||||
with model code 3 is a mixed ink:
|
||||
|
||||
| Chunk | Layout | IDML |
|
||||
|---|---|---|
|
||||
| 0x1F09 | u32 3, u32 *n*, *n* ink UIDs (class 0x1F07) | `InkList` (`Ink/` and each ink's `Self` name), `InkNameList` (the ink names, `$ID/` for built-in ones) |
|
||||
| 0x1F01 | u32 9 (colour space), u16 *n*, *n* f64 fractions | `InkPercentages`: each fraction × 100 in the shortest form that reads back (`30.000000000000004` where 0.3 × 100 gives it) |
|
||||
| 0x102D | u32 *m*, *m* colour UIDs (the spot colours) | `MixedInkSpotColorList` (`Color/` and the name), `MixedInkSpotColorNameList` (the names) |
|
||||
| 0x1F10 | as for colours | `Name`, `ColorEditable`, `ColorRemovable`, `Visible`, `SwatchCreatorID` |
|
||||
|
||||
In these lists a space of a name is written `%20`. A **mixed ink
|
||||
group** is a code 3 colour that also has chunk 0x1F24 (f64 −1, u32 5);
|
||||
its percentages are 0. IDML writes it as `MixedInkGroup` with `Self`,
|
||||
`Model="Mixedinkmodel"`, the four lists, `Name`, the three flags,
|
||||
`SwatchCreatorID` and `SwatchColorGroupReference`. The **members** of a
|
||||
group are colour objects with colour space 9 in chunk 0x1F01, their own
|
||||
percentages and name, chunk 0x117 the group's UID, and no chunk 0x1F09 or
|
||||
0x102D: they take the ink and spot lists from the group and have
|
||||
`BaseColor="MixedInkGroup/<group name>"`. Other mixed inks have
|
||||
`BaseColor="n"`.
|
||||
|
||||
`MixedInk` writes `Self`, `Model="Mixedinkmodel"`, `Space="MixedInk"`,
|
||||
`InkList`, `InkPercentages`, `BaseColor`, `InkNameList`,
|
||||
`MixedInkSpotColorNameList`, `MixedInkSpotColorList`, `Name`,
|
||||
`ColorEditable`, `ColorRemovable`, `Visible`, `SwatchCreatorID` and
|
||||
`SwatchColorGroupReference`, in this order. `Self` is `MixedInk/` or
|
||||
`MixedInkGroup/` and the name, escaped as colour names. In `Graphic.xml`
|
||||
the groups follow the inks, then the mixed inks sorted by `Name` in code
|
||||
point order (`… 1`, `… 10`, `… 11`, `… 2`; 16 of 16 files, UID order
|
||||
fails in 3). Fill colours and colour group swatches refer to them by
|
||||
`Self`.
|
||||
|
||||
Evidence: the 16 trustworthy pairs (DOM 12.1 to 17.0): 30 mixed inks
|
||||
without a group, 48 group members and 3 groups; every attribute above
|
||||
matches 81 of 81. No tint has a mixed ink as its base colour.
|
||||
|
||||
**Tints** are objects of the same class without chunks 0x1F10 and
|
||||
0x1F01. Chunk 0x117 is the UID of the base colour, and chunk 0x1F24 holds
|
||||
@@ -2896,6 +2927,11 @@ in 24 files, all with this layout and a named base colour. The pairs have
|
||||
(`FillColor` of 15 page items, `StrokeColor` of 3, one gradient stop)
|
||||
match too.
|
||||
|
||||
A tint's `AlternateSpace` and `AlternateColorValue` are those of its
|
||||
base colour (`NoAlternateColor` and an empty list where the colour has
|
||||
none): 260 of 260 IDML tints of the corpus of 2026-10 (204 in
|
||||
trustworthy pairs; 9 with `LAB`).
|
||||
|
||||
Tints have no flags chunk, so `ColorEditable`, `ColorRemovable` and
|
||||
`Visible` (true in all 3 IDML tints) are not written. Brackets are known
|
||||
only for black; other reserved colours as a tint base do not occur in the
|
||||
|
||||
@@ -180,6 +180,77 @@ impl Writer<'_> {
|
||||
x.attrs_missing(self.observed("Ink").iter());
|
||||
x.end();
|
||||
}
|
||||
// Mixed ink groups, then mixed inks sorted by name in code point
|
||||
// order (objects.md, mixed inks).
|
||||
let mut mixed: Vec<_> = self.doc.mixed_inks.iter().collect();
|
||||
mixed.sort_by(|a, b| (!a.group, &a.name).cmp(&(!b.group, &b.name)));
|
||||
let list = |items: Vec<String>| items.join(" ");
|
||||
for m in mixed {
|
||||
let source = m
|
||||
.base
|
||||
.and_then(|g| self.doc.mixed_inks.iter().find(|n| n.uid == g))
|
||||
.unwrap_or(m);
|
||||
let inks: Vec<&crate::model::Ink> = source
|
||||
.inks
|
||||
.iter()
|
||||
.filter_map(|u| self.doc.inks.iter().find(|i| i.uid == *u))
|
||||
.collect();
|
||||
let spots: Vec<&crate::model::Color> = source
|
||||
.spots
|
||||
.iter()
|
||||
.filter_map(|u| self.doc.colors.iter().find(|c| c.uid == *u))
|
||||
.collect();
|
||||
if inks.len() != source.inks.len() || spots.len() != source.spots.len() {
|
||||
continue;
|
||||
}
|
||||
let reference = m.reference();
|
||||
x.start(if m.group { "MixedInkGroup" } else { "MixedInk" })
|
||||
.attr("Self", &reference)
|
||||
.attr("Model", "Mixedinkmodel");
|
||||
if !m.group {
|
||||
x.attr("Space", "MixedInk");
|
||||
}
|
||||
x.attr(
|
||||
"InkList",
|
||||
list(
|
||||
inks.iter()
|
||||
.map(|i| format!("Ink/{}", self_name(&i.name.idml()).replace(' ', "%20")))
|
||||
.collect(),
|
||||
),
|
||||
);
|
||||
if !m.group {
|
||||
x.attr(
|
||||
"InkPercentages",
|
||||
list(m.percentages.iter().map(|p| format!("{}", p * 100.0)).collect()),
|
||||
);
|
||||
let base = m
|
||||
.base
|
||||
.and_then(|g| self.doc.swatches.get(&g).cloned())
|
||||
.unwrap_or_else(|| "n".into());
|
||||
x.attr("BaseColor", base);
|
||||
}
|
||||
x.attr(
|
||||
"InkNameList",
|
||||
list(inks.iter().map(|i| i.name.idml().replace(' ', "%20")).collect()),
|
||||
)
|
||||
.attr(
|
||||
"MixedInkSpotColorNameList",
|
||||
list(spots.iter().map(|c| c.idml_name().replace(' ', "%20")).collect()),
|
||||
)
|
||||
.attr(
|
||||
"MixedInkSpotColorList",
|
||||
list(spots.iter().map(|c| c.reference().replace(' ', "%20")).collect()),
|
||||
)
|
||||
.attr("Name", &m.name)
|
||||
.attr("ColorEditable", m.editable.to_string())
|
||||
.attr("ColorRemovable", m.removable.to_string())
|
||||
.attr("Visible", m.visible.to_string());
|
||||
if let Some(id) = m.creator {
|
||||
x.attr("SwatchCreatorID", id.to_string());
|
||||
}
|
||||
group_ref(&mut x, &reference);
|
||||
x.end();
|
||||
}
|
||||
// The pasted smooth shades, in UID order: the document's constant
|
||||
// shade, and each axial shade that something refers to or the
|
||||
// page item defaults name. Each has the values every IDML has on
|
||||
@@ -251,6 +322,17 @@ impl Writer<'_> {
|
||||
.attr("BaseColor", base)
|
||||
.attr("Name", name)
|
||||
.attr("ColorOverride", t.override_name());
|
||||
// The alternate colour of the base colour (objects.md, tints).
|
||||
if let Some((space, values)) = self
|
||||
.doc
|
||||
.colors
|
||||
.iter()
|
||||
.find(|c| c.uid == t.base)
|
||||
.and_then(|c| c.idml_alternate())
|
||||
{
|
||||
x.attr("AlternateSpace", space)
|
||||
.attr("AlternateColorValue", nums(&values));
|
||||
}
|
||||
group_ref(&mut x, reference);
|
||||
x.attrs_missing(self.observed("Tint").iter());
|
||||
x.end();
|
||||
|
||||
@@ -12,6 +12,8 @@ pub mod class {
|
||||
}
|
||||
|
||||
pub mod chunk {
|
||||
/// Spot colours of a mixed ink: u32 count, then colour UIDs.
|
||||
pub const MIXED_INK_SPOTS: u32 = 0x102D;
|
||||
pub const COLOR_VALUE: u32 = 0x1F01;
|
||||
pub const COLOR_MODEL: u32 = 0x1F09;
|
||||
pub const COLOR_NAME: u32 = 0x1F10;
|
||||
@@ -200,6 +202,90 @@ fn override_name(code: u32) -> &'static str {
|
||||
}
|
||||
}
|
||||
|
||||
/// A mixed ink (a colour of model code 3, or a member of a mixed ink
|
||||
/// group: colour space 9 and chunk 0x117) or a mixed ink group (model code
|
||||
/// 3 with chunk 0x1F24). See `docs/format/objects.md`, mixed inks.
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub struct MixedInk {
|
||||
pub uid: u32,
|
||||
pub name: String,
|
||||
pub group: bool,
|
||||
/// The group of a member.
|
||||
pub base: Option<u32>,
|
||||
/// Ink UIDs (chunk 0x1F09 after u32 3: count and UIDs), of the group
|
||||
/// for a member.
|
||||
pub inks: Vec<u32>,
|
||||
/// The fraction of each ink (chunk 0x1F01, colour space 9).
|
||||
pub percentages: Vec<f64>,
|
||||
/// Spot colour UIDs (chunk 0x102D), of the group for a member.
|
||||
pub spots: Vec<u32>,
|
||||
pub editable: bool,
|
||||
pub removable: bool,
|
||||
pub visible: bool,
|
||||
pub creator: Option<u32>,
|
||||
}
|
||||
|
||||
impl MixedInk {
|
||||
/// The mixed ink of a colour read as `color`; `group` is the object of
|
||||
/// a member's group. `None` if `color` is not a mixed ink.
|
||||
pub fn read(
|
||||
color: &Color,
|
||||
obj: &crate::Object,
|
||||
group: Option<&crate::Object>,
|
||||
) -> Result<Option<MixedInk>, Error> {
|
||||
let enc = obj.encoding;
|
||||
let base = match obj.chunk(chunk::TINT_BASE) {
|
||||
Some(d) if color.space == Space::Other(9) => Some(enc.cursor(d).u32()?),
|
||||
_ => None,
|
||||
};
|
||||
if color.model != 3 && base.is_none() {
|
||||
return Ok(None);
|
||||
}
|
||||
let source = if base.is_some() { group } else { Some(obj) };
|
||||
let Some(source) = source else {
|
||||
return Ok(None);
|
||||
};
|
||||
let Some(model) = source.chunk(chunk::COLOR_MODEL) else {
|
||||
return Ok(None);
|
||||
};
|
||||
let mut c = enc.cursor(model);
|
||||
if c.u32()? != 3 {
|
||||
return Ok(None);
|
||||
}
|
||||
let inks = c.u32_list()?;
|
||||
let spots = match source.chunk(chunk::MIXED_INK_SPOTS) {
|
||||
Some(d) => enc.cursor(d).u32_list()?,
|
||||
None => Vec::new(),
|
||||
};
|
||||
Ok(Some(MixedInk {
|
||||
uid: color.uid,
|
||||
name: color.name.clone(),
|
||||
group: base.is_none() && obj.chunk(chunk::COLOR_OVERRIDE).is_some(),
|
||||
base,
|
||||
inks,
|
||||
percentages: color.values.clone(),
|
||||
spots,
|
||||
editable: color.editable,
|
||||
removable: color.removable,
|
||||
visible: color.visible,
|
||||
creator: color.creator,
|
||||
}))
|
||||
}
|
||||
|
||||
/// The IDML reference: `MixedInk/<name>` or `MixedInkGroup/<name>`,
|
||||
/// escaped as colour names.
|
||||
pub fn reference(&self) -> String {
|
||||
let kind = if self.group { "MixedInkGroup" } else { "MixedInk" };
|
||||
format!(
|
||||
"{kind}/{}",
|
||||
self.name
|
||||
.replace('%', "%25")
|
||||
.replace(':', "%3a")
|
||||
.replace('\r', "%0d")
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum Space {
|
||||
Rgb,
|
||||
|
||||
@@ -56,6 +56,8 @@ pub struct Document {
|
||||
pub styles: BTreeMap<u32, Style>,
|
||||
pub toc_styles: Vec<TocStyle>,
|
||||
pub colors: Vec<Color>,
|
||||
/// Mixed inks and mixed ink groups, in UID order.
|
||||
pub mixed_inks: Vec<MixedInk>,
|
||||
/// Tint swatches, with their IDML reference and name.
|
||||
pub tints: Vec<(Tint, String, String)>,
|
||||
pub gradients: Vec<Gradient>,
|
||||
@@ -115,6 +117,7 @@ pub type XmlTag = (String, Option<[f64; 3]>);
|
||||
struct ClassObjects {
|
||||
styles: BTreeMap<u32, Style>,
|
||||
colors: Vec<Color>,
|
||||
mixed_inks: Vec<MixedInk>,
|
||||
tints: Vec<Tint>,
|
||||
gradients: Vec<Gradient>,
|
||||
swatches: BTreeMap<u32, String>,
|
||||
@@ -257,6 +260,7 @@ impl<'a> Reader<'a> {
|
||||
stories,
|
||||
styles: objects.styles,
|
||||
colors: objects.colors,
|
||||
mixed_inks: objects.mixed_inks,
|
||||
tints,
|
||||
gradients: objects.gradients,
|
||||
swatches: objects.swatches,
|
||||
@@ -527,6 +531,23 @@ impl<'a> Reader<'a> {
|
||||
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",
|
||||
|
||||
+1
-1
@@ -44,7 +44,7 @@ use std::collections::{BTreeMap, HashMap};
|
||||
pub use crate::object::Name;
|
||||
pub use attrs::{Attrs, Value};
|
||||
pub use cjk::{CjkTable, CompositeFont, CompositeFontEntry};
|
||||
pub use color::{Color, ColorGroup, Gradient, Ink, Tint};
|
||||
pub use color::{Color, ColorGroup, Gradient, Ink, MixedInk, Tint};
|
||||
pub use font::{Font, FontFamily};
|
||||
pub use hyperlink::{
|
||||
Alternative, Bookmark, Destination, DestinationKind, Hyperlink, PageItemSource, SourceRange,
|
||||
|
||||
Reference in new issue
Block a user