mirror of
https://github.com/storytold/indd-utils.git
synced 2026-11-29 17:43:59 +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
+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