mirror of
https://github.com/storytold/indd-utils.git
synced 2026-11-23 18:50:45 +01:00
feat: convert page item transparency: opacity, blend mode and effects
This commit is contained in:
1 parent
7c3c38446b
commit
ccda8898e9
4 files changed
+492
No files matched your search
@@ -49,6 +49,9 @@ list and absent from the IDML.
|
||||
| 0x6E6E | `StrokeType` (built-in stroke style code, below) | 51 of 51 items, 576 of 576 object styles |
|
||||
| 0x6E8C | `StrokeAlignment`: 0 `CenterAlignment`, 1 `InsideAlignment` (below) | 1 item, 576 object styles |
|
||||
|
||||
Transparency attributes (IDs 0x108xx and 0x1EBxx) are described in
|
||||
`transparency.md`.
|
||||
|
||||
### Strokes
|
||||
|
||||
Object styles (class 0x1B901) hold a full page item attribute list in
|
||||
|
||||
@@ -0,0 +1,129 @@
|
||||
# Transparency
|
||||
|
||||
Implemented in `src/idml/transparency.rs`.
|
||||
|
||||
Page items keep their transparency settings in their attribute list
|
||||
(chunk 0x6E03, see `attributes.md`), with attribute IDs in two ranges:
|
||||
0x108xx and 0x1EBxx. Object styles keep a full list of the same
|
||||
attributes in chunk 0x1B92C (u16 count, then records).
|
||||
|
||||
IDML writes them as child elements of the page item: `TransparencySetting`
|
||||
(the whole object), `StrokeTransparencySetting` and
|
||||
`FillTransparencySetting`, each holding one element per effect
|
||||
(`BlendingSetting`, `DropShadowSetting`, `InnerShadowSetting`,
|
||||
`GradientFeatherSetting`, ...).
|
||||
|
||||
## How the mapping was checked
|
||||
|
||||
IDML writes a setting on a page item only when it differs from the item's
|
||||
object style. So each INDD value was compared with the value in effect:
|
||||
|
||||
1. the value written on the IDML item, if there is one;
|
||||
2. otherwise the value of its object style, following `BasedOn`;
|
||||
3. otherwise the default in `Resources/Preferences.xml`
|
||||
(`TransparencyDefaultContainerObject`).
|
||||
|
||||
The tables count page items of the pairs whose IDML is from the same
|
||||
InDesign version as the INDD. "On the item" means the IDML item states the
|
||||
value itself, which is the evidence that tells the attribute apart. The
|
||||
576 object styles of the pairs (matched by name) agree with their IDML
|
||||
style on every attribute below.
|
||||
|
||||
## Object transparency (`TransparencySetting`)
|
||||
|
||||
| ID | IDML | Encoding | On the item |
|
||||
|---|---|---|---|
|
||||
| 0x10816 | `BlendingSetting/Opacity` | f64, percent | 57 items in 4 files (40, 49, 50, 57, 69, 70) |
|
||||
| 0x10817 | `BlendingSetting/BlendMode` | i32 code (below) | 56 items in 4 files |
|
||||
| 0x1081A | `DropShadowSetting/Mode` | i32: 0 `None`, 1 `Drop` | 8 items with 1 in 2 files |
|
||||
| 0x10820 | `DropShadowSetting/Size` | f64 | 6 items, two values |
|
||||
| 0x1084D | `InnerShadowSetting/Applied` | u16: 1 true, 0 false | 10 items with 1 |
|
||||
| 0x1084E | `InnerShadowSetting/EffectColor` | swatch UID | 10 items (below) |
|
||||
| 0x10852 | `InnerShadowSetting/Distance` | f64 | 10 items with 0 |
|
||||
| 0x10855 | `InnerShadowSetting/Size` | f64 | 10 items with 2.83 |
|
||||
| 0x1EB8A | `GradientFeatherSetting/Applied` | u16: 1 true, 0 false | 56 items in 2 files |
|
||||
| 0x1EB8C | `GradientFeatherSetting` stops | list (below) | 59 items, 170 stops |
|
||||
| 0x1EB8D | `GradientFeatherSetting/Angle` | f64 | 1 item (−90) |
|
||||
| 0x1EB8E | `GradientFeatherSetting/Length` | f64 | 96 items in 3 files |
|
||||
| 0x1EB8F | `GradientFeatherSetting/GradientStart` | two f64 | 96 items in 3 files |
|
||||
| 0x1EB91 | `GradientFeatherSetting/HiliteAngle` | f64 | 1 item (−62.2) |
|
||||
| 0x1EB92 | `GradientFeatherSetting/HiliteLength` | f64 | 1 item (1) |
|
||||
|
||||
All items whose value is not on the IDML item (it comes from the style
|
||||
or the default) match as well: for example 743 items for
|
||||
`InnerShadowSetting` and the gradient feather attributes, with the
|
||||
default values (`Applied="false"`, `Distance="7"`, `Size="7"`, `Length="0"`).
|
||||
|
||||
In pairs whose IDML is from an older version, 14 more items match on
|
||||
opacity and blend mode. One item there has 50 where the IDML says 100;
|
||||
that file was probably saved again after the export.
|
||||
|
||||
**Blend mode codes.** 1 `Multiply` (3 items), 3 `Overlay` (62),
|
||||
9 `Lighten` (5); 0 `Normal` in all 576 object styles, whose IDML blend mode
|
||||
is `Normal`. These codes are the positions of the values in the IDML
|
||||
schema's `BlendMode` enumeration, but no other code occurs in the corpus,
|
||||
so the converter writes only these four.
|
||||
|
||||
**Inner shadow colour.** The 10 items with the effect applied name the
|
||||
swatch that IDML writes as `EffectColor`. Items without the effect store
|
||||
UID 0xB, where the IDML default is `n` (no colour). The converter writes
|
||||
the colour only when the effect is applied.
|
||||
|
||||
## Stroke and fill transparency
|
||||
|
||||
Only the gradient feather has values that tell its attributes apart:
|
||||
|
||||
| ID | IDML | On the item |
|
||||
|---|---|---|
|
||||
| 0x1EB93 | `StrokeTransparencySetting/GradientFeatherSetting/Applied` | 2 items through their style, and the style |
|
||||
| 0x1EB95 | stroke gradient feather stops | 2 items |
|
||||
| 0x1EB96 | `StrokeTransparencySetting/GradientFeatherSetting/Angle` | 2 items through their style (90), and the style |
|
||||
| 0x1EB97 | `StrokeTransparencySetting/GradientFeatherSetting/Length` | 4 items in 2 files |
|
||||
| 0x1EB98 | `StrokeTransparencySetting/GradientFeatherSetting/GradientStart` | 4 items in 2 files |
|
||||
| 0x1EB9C | `FillTransparencySetting/GradientFeatherSetting/Applied` | 10 items, 1 through its style |
|
||||
| 0x1EB9E | fill gradient feather stops | 11 items |
|
||||
| 0x1EB9F | `FillTransparencySetting/GradientFeatherSetting/Angle` | 10 items (−90) |
|
||||
| 0x1EBA0 | `FillTransparencySetting/GradientFeatherSetting/Length` | 3 items in 2 files |
|
||||
| 0x1EBA1 | `FillTransparencySetting/GradientFeatherSetting/GradientStart` | 3 items in 2 files |
|
||||
|
||||
The stroke, fill and object gradient feathers of the same item can hold
|
||||
the same length and start, so each ID was taken from items where only one
|
||||
of the three is set (for example 0x1EB97 matches the stroke value in 4
|
||||
items and the fill value in only 2).
|
||||
|
||||
## Opacity gradient stops
|
||||
|
||||
Value type 0x1085F. A u32 stop count, then three f64 per stop:
|
||||
|
||||
| Field | Contents |
|
||||
|---|---|
|
||||
| f64 | Location, 0–1 (IDML `Location` in percent) |
|
||||
| f64 | Position of the midpoint between this stop and the next, 0–1, measured from the start of the gradient; 1 for the last stop |
|
||||
| f64 | Opacity in percent (IDML `Opacity`) |
|
||||
|
||||
IDML writes a `Midpoint` on every stop but the first: the midpoint
|
||||
between the previous stop and this one, in percent of the distance
|
||||
between them. So for stop *i*, `Midpoint` = (midpoint of stop *i*−1 −
|
||||
location of stop *i*−1) / (location of stop *i* − location of stop *i*−1)
|
||||
× 100. A list with count 0 is stored as a 4-byte value.
|
||||
|
||||
All 170 stops of the items above match on `Opacity`, `Location` and
|
||||
`Midpoint`; so do the 5 stops of two object styles. IDML names an item's
|
||||
stops `<item>TransparencySetting1GradientFeatherSetting1OpacityGradientStop<i>`
|
||||
(with `StrokeTransparencySetting` or `FillTransparencySetting` for those
|
||||
settings), 170 of 170.
|
||||
|
||||
## Not converted
|
||||
|
||||
- `DropShadowSetting/XOffset` and `YOffset`: attributes 0x1081B and
|
||||
0x1081C hold the offsets, but every item and style has the same value in
|
||||
both, so which is which is not known.
|
||||
- Stroke, fill and content blending (opacity and blend mode) and the
|
||||
other effects (feather, outer and inner glow, bevel and emboss, satin,
|
||||
directional feather): no item or style in the pairs has a value other
|
||||
than the default, so their attributes cannot be told apart. One text
|
||||
frame has `ContentTransparencySetting` with an inner glow and a satin
|
||||
effect applied, and two attributes set to 1, so it cannot show which is
|
||||
which.
|
||||
- `GradientFeatherSetting/Type`: `Linear` in every sample.
|
||||
- Transparency of placed graphics and object styles.
|
||||
@@ -1,5 +1,6 @@
|
||||
//! IDML package writer.
|
||||
|
||||
mod transparency;
|
||||
mod values;
|
||||
mod xml;
|
||||
pub mod zip;
|
||||
@@ -1848,6 +1849,7 @@ impl Writer<'_> {
|
||||
Self::text_frame_preference(x, p);
|
||||
}
|
||||
Self::text_wrap_preference(x, item.text_wrap.as_ref(), None);
|
||||
transparency::write(x, &item.attrs, &uref(Some(item.uid)), &self.doc.swatches);
|
||||
for child in &item.children {
|
||||
self.page_item(x, child);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,358 @@
|
||||
//! Transparency settings of page items. See `docs/format/transparency.md`.
|
||||
|
||||
use std::collections::BTreeMap;
|
||||
|
||||
use super::num;
|
||||
use super::xml::Xml;
|
||||
use crate::model::{Attrs, Value};
|
||||
|
||||
#[derive(Clone, Copy)]
|
||||
enum Kind {
|
||||
Number,
|
||||
Bool,
|
||||
Point,
|
||||
Enum(&'static [(u32, &'static str)]),
|
||||
/// A swatch UID, written only when the effect is applied.
|
||||
Swatch,
|
||||
}
|
||||
|
||||
/// Blend mode codes seen in the corpus.
|
||||
const BLEND_MODES: &[(u32, &str)] = &[
|
||||
(0, "Normal"),
|
||||
(1, "Multiply"),
|
||||
(3, "Overlay"),
|
||||
(9, "Lighten"),
|
||||
];
|
||||
|
||||
/// Transparency elements on a page item, in the order the schema lists them.
|
||||
const SETTINGS: &[&str] = &[
|
||||
"TransparencySetting",
|
||||
"StrokeTransparencySetting",
|
||||
"FillTransparencySetting",
|
||||
];
|
||||
|
||||
/// Effect elements of a setting, in schema order.
|
||||
const EFFECTS: &[&str] = &[
|
||||
"BlendingSetting",
|
||||
"DropShadowSetting",
|
||||
"InnerShadowSetting",
|
||||
"GradientFeatherSetting",
|
||||
];
|
||||
|
||||
/// Attribute-list ID, setting, effect, IDML attribute, value kind; in the
|
||||
/// schema's attribute order within each effect.
|
||||
const ATTRS: &[(u32, &str, &str, &str, Kind)] = &[
|
||||
(
|
||||
0x10817,
|
||||
"TransparencySetting",
|
||||
"BlendingSetting",
|
||||
"BlendMode",
|
||||
Kind::Enum(BLEND_MODES),
|
||||
),
|
||||
(
|
||||
0x10816,
|
||||
"TransparencySetting",
|
||||
"BlendingSetting",
|
||||
"Opacity",
|
||||
Kind::Number,
|
||||
),
|
||||
(
|
||||
0x1081A,
|
||||
"TransparencySetting",
|
||||
"DropShadowSetting",
|
||||
"Mode",
|
||||
Kind::Enum(&[(0, "None"), (1, "Drop")]),
|
||||
),
|
||||
(
|
||||
0x10820,
|
||||
"TransparencySetting",
|
||||
"DropShadowSetting",
|
||||
"Size",
|
||||
Kind::Number,
|
||||
),
|
||||
(
|
||||
0x1084D,
|
||||
"TransparencySetting",
|
||||
"InnerShadowSetting",
|
||||
"Applied",
|
||||
Kind::Bool,
|
||||
),
|
||||
(
|
||||
0x1084E,
|
||||
"TransparencySetting",
|
||||
"InnerShadowSetting",
|
||||
"EffectColor",
|
||||
Kind::Swatch,
|
||||
),
|
||||
(
|
||||
0x10852,
|
||||
"TransparencySetting",
|
||||
"InnerShadowSetting",
|
||||
"Distance",
|
||||
Kind::Number,
|
||||
),
|
||||
(
|
||||
0x10855,
|
||||
"TransparencySetting",
|
||||
"InnerShadowSetting",
|
||||
"Size",
|
||||
Kind::Number,
|
||||
),
|
||||
(
|
||||
0x1EB8A,
|
||||
"TransparencySetting",
|
||||
"GradientFeatherSetting",
|
||||
"Applied",
|
||||
Kind::Bool,
|
||||
),
|
||||
(
|
||||
0x1EB8D,
|
||||
"TransparencySetting",
|
||||
"GradientFeatherSetting",
|
||||
"Angle",
|
||||
Kind::Number,
|
||||
),
|
||||
(
|
||||
0x1EB8E,
|
||||
"TransparencySetting",
|
||||
"GradientFeatherSetting",
|
||||
"Length",
|
||||
Kind::Number,
|
||||
),
|
||||
(
|
||||
0x1EB8F,
|
||||
"TransparencySetting",
|
||||
"GradientFeatherSetting",
|
||||
"GradientStart",
|
||||
Kind::Point,
|
||||
),
|
||||
(
|
||||
0x1EB91,
|
||||
"TransparencySetting",
|
||||
"GradientFeatherSetting",
|
||||
"HiliteAngle",
|
||||
Kind::Number,
|
||||
),
|
||||
(
|
||||
0x1EB92,
|
||||
"TransparencySetting",
|
||||
"GradientFeatherSetting",
|
||||
"HiliteLength",
|
||||
Kind::Number,
|
||||
),
|
||||
(
|
||||
0x1EB93,
|
||||
"StrokeTransparencySetting",
|
||||
"GradientFeatherSetting",
|
||||
"Applied",
|
||||
Kind::Bool,
|
||||
),
|
||||
(
|
||||
0x1EB96,
|
||||
"StrokeTransparencySetting",
|
||||
"GradientFeatherSetting",
|
||||
"Angle",
|
||||
Kind::Number,
|
||||
),
|
||||
(
|
||||
0x1EB97,
|
||||
"StrokeTransparencySetting",
|
||||
"GradientFeatherSetting",
|
||||
"Length",
|
||||
Kind::Number,
|
||||
),
|
||||
(
|
||||
0x1EB98,
|
||||
"StrokeTransparencySetting",
|
||||
"GradientFeatherSetting",
|
||||
"GradientStart",
|
||||
Kind::Point,
|
||||
),
|
||||
(
|
||||
0x1EB9C,
|
||||
"FillTransparencySetting",
|
||||
"GradientFeatherSetting",
|
||||
"Applied",
|
||||
Kind::Bool,
|
||||
),
|
||||
(
|
||||
0x1EB9F,
|
||||
"FillTransparencySetting",
|
||||
"GradientFeatherSetting",
|
||||
"Angle",
|
||||
Kind::Number,
|
||||
),
|
||||
(
|
||||
0x1EBA0,
|
||||
"FillTransparencySetting",
|
||||
"GradientFeatherSetting",
|
||||
"Length",
|
||||
Kind::Number,
|
||||
),
|
||||
(
|
||||
0x1EBA1,
|
||||
"FillTransparencySetting",
|
||||
"GradientFeatherSetting",
|
||||
"GradientStart",
|
||||
Kind::Point,
|
||||
),
|
||||
];
|
||||
|
||||
/// Opacity gradient stops of a gradient feather, per setting.
|
||||
const STOPS: &[(u32, &str)] = &[
|
||||
(0x1EB8C, "TransparencySetting"),
|
||||
(0x1EB95, "StrokeTransparencySetting"),
|
||||
(0x1EB9E, "FillTransparencySetting"),
|
||||
];
|
||||
|
||||
/// Applied flag of an effect whose colour is written only when applied.
|
||||
const INNER_SHADOW_APPLIED: u32 = 0x1084D;
|
||||
|
||||
/// An effect element: name, attributes and opacity stops.
|
||||
type Effect = (&'static str, Vec<(&'static str, String)>, Vec<Stop>);
|
||||
|
||||
/// One opacity gradient stop, as IDML writes it.
|
||||
#[derive(Debug, PartialEq)]
|
||||
struct Stop {
|
||||
opacity: f64,
|
||||
location: f64,
|
||||
/// Relative position of the midpoint between the previous stop and
|
||||
/// this one, in percent; `None` for the first stop.
|
||||
midpoint: Option<f64>,
|
||||
}
|
||||
|
||||
/// Opacity stops: u32 count, then per stop f64 location (0–1), f64
|
||||
/// position of the midpoint to the next stop (0–1, absolute), f64 opacity
|
||||
/// in percent.
|
||||
fn stops(data: &[u8]) -> Option<Vec<Stop>> {
|
||||
let n = u32::from_le_bytes(data.get(..4)?.try_into().ok()?) as usize;
|
||||
if n == 0 || data.len() != 4 + n * 24 {
|
||||
return None;
|
||||
}
|
||||
let f = |i: usize| f64::from_le_bytes(data[4 + i * 8..12 + i * 8].try_into().unwrap());
|
||||
Some(
|
||||
(0..n)
|
||||
.map(|i| {
|
||||
let location = f(3 * i);
|
||||
let midpoint = (i > 0).then(|| {
|
||||
let (prev, mid) = (f(3 * (i - 1)), f(3 * (i - 1) + 1));
|
||||
(mid - prev) / (location - prev) * 100.0
|
||||
});
|
||||
Stop {
|
||||
opacity: f(3 * i + 2),
|
||||
location: super::round(location * 100.0),
|
||||
midpoint,
|
||||
}
|
||||
})
|
||||
.collect(),
|
||||
)
|
||||
}
|
||||
|
||||
fn value(v: &Value, kind: Kind, swatches: &BTreeMap<u32, String>) -> Option<String> {
|
||||
match kind {
|
||||
Kind::Number => v.as_f64().map(num),
|
||||
Kind::Bool => match v.as_u32()? {
|
||||
0 => Some("false".into()),
|
||||
1 => Some("true".into()),
|
||||
_ => None,
|
||||
},
|
||||
Kind::Point => match v {
|
||||
Value::Point(a, b) => Some(format!("{} {}", num(*a), num(*b))),
|
||||
_ => None,
|
||||
},
|
||||
Kind::Enum(map) => {
|
||||
let code = v.as_u32()?;
|
||||
map.iter()
|
||||
.find(|(k, _)| *k == code)
|
||||
.map(|(_, n)| n.to_string())
|
||||
}
|
||||
Kind::Swatch => v.as_ref().and_then(|u| swatches.get(&u).cloned()),
|
||||
}
|
||||
}
|
||||
|
||||
/// Write the transparency settings found in a page item's attribute list.
|
||||
/// `item` is the item's `Self`, which names the opacity stops.
|
||||
pub(super) fn write(x: &mut Xml, attrs: &Attrs, item: &str, swatches: &BTreeMap<u32, String>) {
|
||||
let inner_shadow_applied = attrs.get(INNER_SHADOW_APPLIED).and_then(Value::as_u32) == Some(1);
|
||||
for &setting in SETTINGS {
|
||||
let mut effects: Vec<Effect> = Vec::new();
|
||||
for &effect in EFFECTS {
|
||||
let mut values = Vec::new();
|
||||
for &(id, s, e, name, kind) in ATTRS {
|
||||
if s != setting || e != effect {
|
||||
continue;
|
||||
}
|
||||
if matches!(kind, Kind::Swatch) && !inner_shadow_applied {
|
||||
continue;
|
||||
}
|
||||
if let Some(t) = attrs.get(id).and_then(|v| value(v, kind, swatches)) {
|
||||
values.push((name, t));
|
||||
}
|
||||
}
|
||||
let stop_list = if effect == "GradientFeatherSetting" {
|
||||
STOPS
|
||||
.iter()
|
||||
.find(|(_, s)| *s == setting)
|
||||
.and_then(|(id, _)| match attrs.get(*id) {
|
||||
Some(Value::Other(_, data)) => stops(data),
|
||||
_ => None,
|
||||
})
|
||||
.unwrap_or_default()
|
||||
} else {
|
||||
Vec::new()
|
||||
};
|
||||
if !values.is_empty() || !stop_list.is_empty() {
|
||||
effects.push((effect, values, stop_list));
|
||||
}
|
||||
}
|
||||
if effects.is_empty() {
|
||||
continue;
|
||||
}
|
||||
x.start(setting);
|
||||
for (effect, values, stop_list) in effects {
|
||||
x.start(effect);
|
||||
for (name, v) in values {
|
||||
x.attr(name, v);
|
||||
}
|
||||
for (i, s) in stop_list.iter().enumerate() {
|
||||
x.start("OpacityGradientStop")
|
||||
.attr(
|
||||
"Self",
|
||||
format!("{item}{setting}1{effect}1OpacityGradientStop{i}"),
|
||||
)
|
||||
.attr("Opacity", num(s.opacity))
|
||||
.attr("Location", num(s.location));
|
||||
if let Some(m) = s.midpoint {
|
||||
x.attr("Midpoint", num(m));
|
||||
}
|
||||
x.end();
|
||||
}
|
||||
x.end();
|
||||
}
|
||||
x.end();
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn decodes_opacity_stops() {
|
||||
// Two stops: location 0.15, midpoint at 0.5, opacity 100; location
|
||||
// 0.85, midpoint 1, opacity 0.
|
||||
let mut d = 2u32.to_le_bytes().to_vec();
|
||||
for v in [0.15, 0.5, 100.0, 0.85, 1.0, 0.0f64] {
|
||||
d.extend_from_slice(&v.to_le_bytes());
|
||||
}
|
||||
let s = stops(&d).unwrap();
|
||||
assert_eq!(s.len(), 2);
|
||||
assert_eq!(
|
||||
(s[0].opacity, s[0].location, s[0].midpoint),
|
||||
(100.0, 15.0, None)
|
||||
);
|
||||
assert_eq!(s[1].location, 85.0);
|
||||
assert!((s[1].midpoint.unwrap() - 50.0).abs() < 1e-9);
|
||||
assert_eq!(stops(&0u32.to_le_bytes()), None);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user