mirror of
https://github.com/storytold/indd-utils.git
synced 2026-11-19 16:50:48 +01:00
feat: write swatch library, alternate colour and guide settings
Colours get SwatchCreatorID and their alternate colour from the INDD, HSB colours are written as RGB with ConvertToHsb, and every swatch names its colour group swatch from DOM 12 on. Guides get Locked, GuideZone, OverriddenPageItemProps and PageIndex, which counts pages from the spine. Tints, gradients and the None swatch get the values every IDML has on them. Value coverage on trustworthy pairs: 83.37 % to 84.07 %.
This commit is contained in:
1 parent
09782f6af0
commit
463fef540a
5 files changed
+241
-31
No files matched your search
@@ -265,6 +265,17 @@ 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.
|
||||
|
||||
### Swatches
|
||||
|
||||
| Element | Values kept |
|
||||
|---|---|
|
||||
| `Tint` | `ColorOverride="Normal"`, `AlternateSpace="NoAlternateColor"`, `AlternateColorValue=""`, `ColorEditable`, `ColorRemovable`, `Visible` (`true`) and `SwatchCreatorID="7937"` from DOM 8 (37 tints in 26 files); `ConvertToHsb="false"` from 18 |
|
||||
| `Gradient` | `SwatchCreatorID="7937"` (and the flags, which the converter reads) from DOM 7 |
|
||||
| `Swatch` (`None`) | `SwatchCreatorID="7937"` from DOM 7 |
|
||||
|
||||
Colours have their creator and alternate colour in the INDD
|
||||
(`objects.md`, colours).
|
||||
|
||||
### Trap presets
|
||||
|
||||
All 683 IDML files list the same two `TrapPreset` elements at the end of
|
||||
|
||||
+40
-5
@@ -183,11 +183,21 @@ no field separates them in the objects searched (every class with at most
|
||||
`ViewPreference` with `RulerOrigin="SpreadOrigin"` and measures guide
|
||||
locations from the spread, so the two agree.
|
||||
|
||||
**Not converted.** `Locked` (false for all 81 IDML guides),
|
||||
`GuideZone` (1) and `OverriddenPageItemProps` (empty) have no identified
|
||||
field. `PageIndex` is 0 for all 22 spread guides and the page's position
|
||||
in the spread plus 1 for 46 of the 49 page guides; the other 3 do not
|
||||
follow from the owner, so it is left out.
|
||||
**Other settings.** A guide has chunks 0x2C2D (`Locked`) and 0x1424
|
||||
(`OverriddenPageItemProps`) as page items do (`Page item settings`),
|
||||
and the f64 at offset 44 of the 52-byte record is `GuideZone`. Over the
|
||||
7,003 guides of the trustworthy pairs: `Locked` 7,003 of 7,003 (3
|
||||
locked), `GuideZone` 6,988 of the 6,988 with a 52-byte record (464 with
|
||||
0), `OverriddenPageItemProps` 6,988 of 6,988; the 15 guides with a
|
||||
40-byte record have neither attribute in IDML.
|
||||
|
||||
`PageIndex` counts from the spine of the spread: 0 for a guide of the
|
||||
spread itself; for a page guide, the pages left of the spine are −1,
|
||||
−2, … (counting outwards) and those right of it 1, 2, …, the spine being
|
||||
after `BindingLocation` pages; on master spreads it is at the left edge.
|
||||
This gives 6,998 of 7,003 (1,215 spread guides 0; 2,266 of 2,269 page
|
||||
guides on document spreads, 473 of them −1 and 12 −2;
|
||||
3,517 of 3,519 on master spreads).
|
||||
|
||||
## Layers (0x302)
|
||||
|
||||
@@ -1178,6 +1188,31 @@ Built-in tables have only `Self` and `Name` in IDML. Mojikumi chunk
|
||||
| 0x1F01 | u32 space (5 RGB, 6 CMYK), u16 count, f64 components as fractions |
|
||||
| 0x1F09 | u32 model: 0 Process, 1 Spot, 2 Registration (below) |
|
||||
| 0x1F24 | f64 tint value (−1 for a colour, see tints below), then u32 `ColorOverride`: 0 Normal, 1 Specialpaper, 2 Specialblack, 3 Specialregistration, 4 Hiddenreserved |
|
||||
| 0x1F0A | Alternate colour, as 0x1F01: space 3 none, 6 CMYK, 7 LAB |
|
||||
|
||||
After the flags, chunk 0x1F10 has two u32 and the `SwatchCreatorID`
|
||||
(7937 for colours made in the document, other numbers for colours from a
|
||||
swatch library, such as 31527). `AlternateSpace` is `NoAlternateColor`
|
||||
with `AlternateColorValue=""` without chunk 0x1F0A or with space 3;
|
||||
otherwise the space and the values, CMYK as percentages. Evidence over
|
||||
the 12,197 colours of the trustworthy pairs whose IDML colour has the
|
||||
same name: `SwatchCreatorID` 12,197 of 12,197; alternate space and
|
||||
values 12,197 of 12,197 (7,527 without the chunk, 4,624 with space 3,
|
||||
33 LAB and 13 CMYK alternates, all library colours).
|
||||
|
||||
Space 14 holds hue, saturation and brightness as fractions. IDML writes
|
||||
such a colour as `RGB` with `ConvertToHsb="true"`; the 9 such colours in
|
||||
the pairs (all hue 0, saturation 1, brightness 1) are `255 0 0`. The
|
||||
converter converts with the usual hexagon formula. Every other colour
|
||||
has `ConvertToHsb="false"` (4,695 of 4,695 colours of the IDML files
|
||||
from DOM 16 on, where the attribute exists).
|
||||
|
||||
**Colour groups.** From DOM 12 on, every swatch (colour, tint, gradient,
|
||||
`None`) has `SwatchColorGroupReference`: the `ColorGroupSwatch` of the
|
||||
colour group that lists it, or `n`. Over the trustworthy pairs from DOM
|
||||
12 on: colours 9,135 of 9,135, gradients 450 of 450, `None` 353 of 353,
|
||||
tints 24 of 24. DOM 11 files have the attribute on some swatches only
|
||||
(639 of 1,584 colours), and the converter writes it from DOM 12 on.
|
||||
|
||||
Unnamed colours are referenced by UID (`Color/u93`). All 1,296 colours in
|
||||
the pairs match on `Model`, `Space`, `ColorValue`, `ColorOverride`,
|
||||
|
||||
+97
-25
@@ -1677,9 +1677,35 @@ impl Writer<'_> {
|
||||
x.end();
|
||||
}
|
||||
|
||||
/// `SwatchColorGroupReference` of each swatch in a colour group: the
|
||||
/// `ColorGroupSwatch` that names it. See `docs/format/objects.md`.
|
||||
fn group_swatches(&self) -> std::collections::HashMap<String, String> {
|
||||
let mut out = std::collections::HashMap::new();
|
||||
for g in &self.doc.color_groups {
|
||||
for (n, s) in g.swatches.iter().enumerate() {
|
||||
if let Some(r) = self.doc.swatches.get(s) {
|
||||
out.entry(r.clone())
|
||||
.or_insert_with(|| format!("{}ColorGroupSwatch{n:x}", uref(Some(g.uid))));
|
||||
}
|
||||
}
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
fn graphic(&self) -> String {
|
||||
let mut x = Xml::new();
|
||||
self.package_root(&mut x, "Graphic");
|
||||
let groups = self.group_swatches();
|
||||
// IDML names the colour group swatch of every swatch from DOM 12
|
||||
// on (`n` for none).
|
||||
let group_ref = |x: &mut Xml, reference: &str| {
|
||||
if self.doc.version.major >= 12 {
|
||||
x.attr(
|
||||
"SwatchColorGroupReference",
|
||||
groups.get(reference).map_or("n", String::as_str),
|
||||
);
|
||||
}
|
||||
};
|
||||
for c in &self.doc.colors {
|
||||
let name = c.idml_name();
|
||||
let mut attrs = vec![("Self", c.reference())];
|
||||
@@ -1695,7 +1721,23 @@ impl Writer<'_> {
|
||||
("ColorRemovable", c.removable.to_string()),
|
||||
("Visible", c.visible.to_string()),
|
||||
]);
|
||||
x.empty("Color", &attrs);
|
||||
if let Some((space, values)) = c.idml_alternate() {
|
||||
attrs.push(("AlternateSpace", space.into()));
|
||||
attrs.push(("AlternateColorValue", nums(&values)));
|
||||
}
|
||||
if let Some(id) = c.creator {
|
||||
attrs.push(("SwatchCreatorID", id.to_string()));
|
||||
}
|
||||
if self.doc.version.major >= 16 {
|
||||
let hsb = c.space == crate::model::color::Space::Hsb;
|
||||
attrs.push(("ConvertToHsb", hsb.to_string()));
|
||||
}
|
||||
x.start("Color");
|
||||
for (k, v) in &attrs {
|
||||
x.attr(k, v);
|
||||
}
|
||||
group_ref(&mut x, &c.reference());
|
||||
x.end();
|
||||
}
|
||||
// The schema puts inks after the colours.
|
||||
for i in &self.doc.inks {
|
||||
@@ -1720,27 +1762,25 @@ impl Writer<'_> {
|
||||
}
|
||||
for (t, reference, name) in &self.doc.tints {
|
||||
let base = self.doc.swatches.get(&t.base).cloned().unwrap_or_default();
|
||||
x.empty(
|
||||
"Tint",
|
||||
&[
|
||||
("Self", reference.clone()),
|
||||
("TintValue", num(t.value)),
|
||||
("BaseColor", base),
|
||||
("Name", name.clone()),
|
||||
("ColorOverride", t.override_name().into()),
|
||||
],
|
||||
);
|
||||
x.start("Tint")
|
||||
.attr("Self", reference)
|
||||
.attr("TintValue", num(t.value))
|
||||
.attr("BaseColor", base)
|
||||
.attr("Name", name)
|
||||
.attr("ColorOverride", t.override_name());
|
||||
group_ref(&mut x, reference);
|
||||
x.attrs_missing(self.observed("Tint").iter());
|
||||
x.end();
|
||||
}
|
||||
x.empty(
|
||||
"Swatch",
|
||||
&[
|
||||
("Self", "Swatch/None".into()),
|
||||
("Name", "None".into()),
|
||||
("ColorEditable", "false".into()),
|
||||
("ColorRemovable", "false".into()),
|
||||
("Visible", "true".into()),
|
||||
],
|
||||
);
|
||||
x.start("Swatch")
|
||||
.attr("Self", "Swatch/None")
|
||||
.attr("Name", "None")
|
||||
.attr("ColorEditable", "false")
|
||||
.attr("ColorRemovable", "false")
|
||||
.attr("Visible", "true");
|
||||
group_ref(&mut x, "Swatch/None");
|
||||
x.attrs_missing(self.observed("Swatch").iter());
|
||||
x.end();
|
||||
// The schema requires gradients after the swatches.
|
||||
for g in &self.doc.gradients {
|
||||
x.start("Gradient")
|
||||
@@ -1750,6 +1790,8 @@ impl Writer<'_> {
|
||||
.attr("ColorEditable", g.editable.to_string())
|
||||
.attr("ColorRemovable", g.removable.to_string())
|
||||
.attr("Visible", g.visible.to_string());
|
||||
group_ref(&mut x, &g.reference());
|
||||
x.attrs_missing(self.observed("Gradient").iter());
|
||||
for (i, stop) in g.stops.iter().enumerate() {
|
||||
let color = self
|
||||
.doc
|
||||
@@ -3315,7 +3357,8 @@ impl Writer<'_> {
|
||||
/// A ruler guide. `origin` is the top-left corner of the spread's
|
||||
/// pages, from which IDML measures `Location` with the ruler origin
|
||||
/// `SpreadOrigin`. See `docs/format/objects.md`.
|
||||
fn guide(x: &mut Xml, g: &Guide, origin: (f64, f64)) {
|
||||
/// `page_index` is IDML `PageIndex` (see `docs/format/objects.md`).
|
||||
fn guide(x: &mut Xml, g: &Guide, origin: (f64, f64), page_index: i64) {
|
||||
let (left, top) = origin;
|
||||
x.start("Guide")
|
||||
.attr("Self", uref(Some(g.uid)))
|
||||
@@ -3348,6 +3391,21 @@ impl Writer<'_> {
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
x.attr("Locked", g.locked.to_string())
|
||||
.attr("PageIndex", page_index.to_string());
|
||||
if let Some(z) = g.zone {
|
||||
x.attr("GuideZone", num(z));
|
||||
}
|
||||
if let Some((master, ids)) = &g.overridden {
|
||||
let ids = if *master == 0 {
|
||||
String::new()
|
||||
} else {
|
||||
join_numbers(ids)
|
||||
};
|
||||
x.attr("OverriddenPageItemProps", ids);
|
||||
} else if g.zone.is_some() {
|
||||
x.attr("OverriddenPageItemProps", "");
|
||||
}
|
||||
if g.color == 6 {
|
||||
Self::properties(
|
||||
x,
|
||||
@@ -3580,10 +3638,20 @@ impl Writer<'_> {
|
||||
// A guide belongs to a page, or to the spread; IDML writes a
|
||||
// spread's guides in its first page.
|
||||
let first = p.uid == s.pages[0].uid;
|
||||
// Pages count from the spine: left of it -1, -2, ..., right of
|
||||
// it 1, 2, ...; a master spread's spine is at its left edge.
|
||||
let position = s.pages.iter().position(|q| q.uid == p.uid).unwrap_or(0) as i64;
|
||||
let binding = if master { 0 } else { s.binding_location as i64 };
|
||||
let from_spine = if position < binding {
|
||||
position - binding
|
||||
} else {
|
||||
position - binding + 1
|
||||
};
|
||||
for g in &s.guides {
|
||||
let own_page = s.pages.iter().any(|q| q.uid == g.owner);
|
||||
if g.owner == p.uid || (first && !own_page) {
|
||||
Self::guide(&mut x, g, origin);
|
||||
let index = if own_page { from_spine } else { 0 };
|
||||
Self::guide(&mut x, g, origin, index);
|
||||
}
|
||||
}
|
||||
self.page_layout(&mut x, p);
|
||||
@@ -4247,9 +4315,12 @@ mod tests {
|
||||
color: 6,
|
||||
guide_type: Some(0),
|
||||
layer: 0xcc,
|
||||
locked: false,
|
||||
zone: Some(1.0),
|
||||
overridden: None,
|
||||
};
|
||||
let mut x = Xml::new();
|
||||
Writer::guide(&mut x, &guide, origin);
|
||||
Writer::guide(&mut x, &guide, origin, -1);
|
||||
let out = x.finish();
|
||||
assert!(
|
||||
out.contains("Orientation=\"Horizontal\" Location=\"735.5\""),
|
||||
@@ -4260,13 +4331,14 @@ mod tests {
|
||||
"{out}"
|
||||
);
|
||||
assert!(out.contains("<GuideColor type=\"enumeration\">Cyan</GuideColor>"));
|
||||
assert!(out.contains("PageIndex=\"-1\" GuideZone=\"1\""), "{out}");
|
||||
let vertical = Guide {
|
||||
horizontal: false,
|
||||
position: 28.0,
|
||||
..guide
|
||||
};
|
||||
let mut x = Xml::new();
|
||||
Writer::guide(&mut x, &vertical, origin);
|
||||
Writer::guide(&mut x, &vertical, origin, 1);
|
||||
assert!(x.finish().contains("Location=\"640\""));
|
||||
}
|
||||
|
||||
|
||||
+75
-1
@@ -15,6 +15,8 @@ pub mod chunk {
|
||||
pub const COLOR_VALUE: u32 = 0x1F01;
|
||||
pub const COLOR_MODEL: u32 = 0x1F09;
|
||||
pub const COLOR_NAME: u32 = 0x1F10;
|
||||
/// Alternate colour: as 0x1F01.
|
||||
pub const COLOR_ALTERNATE: u32 = 0x1F0A;
|
||||
/// f64 tint value (−1 for colours), then u32 colour override.
|
||||
pub const COLOR_OVERRIDE: u32 = 0x1F24;
|
||||
/// Base colour of a tint.
|
||||
@@ -195,6 +197,9 @@ pub enum Space {
|
||||
Rgb,
|
||||
Cmyk,
|
||||
Lab,
|
||||
/// Hue, saturation and brightness as fractions; IDML writes the colour
|
||||
/// as RGB with `ConvertToHsb="true"`.
|
||||
Hsb,
|
||||
Other(u32),
|
||||
}
|
||||
|
||||
@@ -213,6 +218,28 @@ pub struct Color {
|
||||
pub removable: bool,
|
||||
pub visible: bool,
|
||||
pub color_override: u32,
|
||||
/// `SwatchCreatorID`: the u32 at the end of chunk 0x1F10.
|
||||
pub creator: Option<u32>,
|
||||
/// The alternate colour (chunk 0x1F0A): space code and components.
|
||||
pub alternate: Option<(u32, Vec<f64>)>,
|
||||
}
|
||||
|
||||
/// Red, green and blue fractions of a colour given as hue (a fraction of
|
||||
/// a full turn), saturation and brightness: the hue picks the side of the
|
||||
/// colour hexagon, saturation mixes towards white and brightness scales.
|
||||
fn hsb_to_rgb(h: f64, s: f64, b: f64) -> [f64; 3] {
|
||||
let h6 = (h.rem_euclid(1.0)) * 6.0;
|
||||
let sector = h6.floor();
|
||||
let f = h6 - sector;
|
||||
let (p, q, t) = (b * (1.0 - s), b * (1.0 - s * f), b * (1.0 - s * (1.0 - f)));
|
||||
match sector as u32 {
|
||||
0 => [b, t, p],
|
||||
1 => [q, b, p],
|
||||
2 => [p, b, t],
|
||||
3 => [p, q, b],
|
||||
4 => [t, p, b],
|
||||
_ => [b, p, q],
|
||||
}
|
||||
}
|
||||
|
||||
impl Color {
|
||||
@@ -250,7 +277,7 @@ impl Color {
|
||||
|
||||
pub fn space_name(&self) -> &'static str {
|
||||
match self.space {
|
||||
Space::Rgb => "RGB",
|
||||
Space::Rgb | Space::Hsb => "RGB",
|
||||
Space::Lab => "LAB",
|
||||
_ => "CMYK",
|
||||
}
|
||||
@@ -262,6 +289,10 @@ impl Color {
|
||||
match self.space {
|
||||
Space::Cmyk => self.values.iter().map(|v| v * 100.0).collect(),
|
||||
Space::Rgb => self.values.iter().map(|v| v * 255.0).collect(),
|
||||
Space::Hsb => match self.values[..] {
|
||||
[h, s, b] => hsb_to_rgb(h, s, b).iter().map(|v| v * 255.0).collect(),
|
||||
_ => self.values.clone(),
|
||||
},
|
||||
_ => self.values.clone(),
|
||||
}
|
||||
}
|
||||
@@ -280,11 +311,31 @@ impl Color {
|
||||
let builtin_name = c.flag()? == 1;
|
||||
let name_str = c.string()?;
|
||||
let flags = c.u32()?;
|
||||
// Two u32, then the creator.
|
||||
let creator = (c.remaining() >= 12)
|
||||
.then(|| -> Result<u32, Error> {
|
||||
c.skip(8)?;
|
||||
c.u32()
|
||||
})
|
||||
.transpose()?;
|
||||
let alternate = match obj.chunk(chunk::COLOR_ALTERNATE) {
|
||||
Some(d) => {
|
||||
let mut a = Cursor::new(d);
|
||||
let space = a.u32()?;
|
||||
let n = a.u16()?;
|
||||
Some((
|
||||
space,
|
||||
(0..n).map(|_| a.f64()).collect::<Result<Vec<_>, _>>()?,
|
||||
))
|
||||
}
|
||||
None => None,
|
||||
};
|
||||
let mut v = Cursor::new(value);
|
||||
let space = match v.u32()? {
|
||||
5 => Space::Rgb,
|
||||
6 => Space::Cmyk,
|
||||
7 => Space::Lab,
|
||||
14 => Space::Hsb,
|
||||
other => Space::Other(other),
|
||||
};
|
||||
let n = v.u16()?;
|
||||
@@ -308,8 +359,22 @@ impl Color {
|
||||
visible: flags & 2 != 0,
|
||||
editable: flags & 4 != 0,
|
||||
color_override,
|
||||
creator,
|
||||
alternate,
|
||||
}))
|
||||
}
|
||||
|
||||
/// IDML `AlternateSpace` and `AlternateColorValue`: space code 3 or no
|
||||
/// chunk is none; 6 is CMYK (percentages), 7 LAB. `None` for another
|
||||
/// code.
|
||||
pub fn idml_alternate(&self) -> Option<(&'static str, Vec<f64>)> {
|
||||
match &self.alternate {
|
||||
None | Some((3, _)) => Some(("NoAlternateColor", Vec::new())),
|
||||
Some((6, v)) => Some(("CMYK", v.iter().map(|x| x * 100.0).collect())),
|
||||
Some((7, v)) => Some(("LAB", v.clone())),
|
||||
Some(_) => None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
@@ -328,9 +393,18 @@ mod tests {
|
||||
removable: false,
|
||||
visible: true,
|
||||
color_override,
|
||||
creator: None,
|
||||
alternate: None,
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn converts_hsb_to_rgb() {
|
||||
assert_eq!(hsb_to_rgb(0.0, 1.0, 1.0), [1.0, 0.0, 0.0]);
|
||||
assert_eq!(hsb_to_rgb(0.5, 1.0, 1.0), [0.0, 1.0, 1.0]);
|
||||
assert_eq!(hsb_to_rgb(0.25, 0.0, 0.5), [0.5, 0.5, 0.5]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn names_tints_after_their_base() {
|
||||
let t = Tint {
|
||||
|
||||
@@ -519,6 +519,12 @@ pub struct Guide {
|
||||
pub guide_type: Option<u32>,
|
||||
/// Document layer.
|
||||
pub layer: u32,
|
||||
/// Chunk 0x2C2D, as for page items.
|
||||
pub locked: bool,
|
||||
/// f64 at 44 of the 52-byte record (`GuideZone`).
|
||||
pub zone: Option<f64>,
|
||||
/// Chunk 0x1424, as for page items.
|
||||
pub overridden: Option<(u32, Vec<u32>)>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
@@ -1808,6 +1814,18 @@ impl<'a> Reader<'a> {
|
||||
fit_to_page: h(26)? == 1,
|
||||
guide_type: if d.len() >= 52 { Some(u(40)?) } else { None },
|
||||
layer,
|
||||
locked: self
|
||||
.chunk(uid, chunk::ITEM_LOCKED)?
|
||||
.is_some_and(|d| u32_at(&d, 0) == Some(1)),
|
||||
zone: if d.len() >= 52 { Some(f(44)?) } else { None },
|
||||
overridden: match self.chunk(uid, chunk::ITEM_OVERRIDE)? {
|
||||
Some(o) => {
|
||||
let mut c = Cursor::new(&o);
|
||||
let master = c.u32()?;
|
||||
Some((master, c.u32_list()?))
|
||||
}
|
||||
None => None,
|
||||
},
|
||||
}))
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user