mirror of
https://github.com/storytold/indd-utils.git
synced 2026-12-01 02:23:54 +01:00
feat: write timing lists, motion presets and animation settings
This commit is contained in:
1 parent
388cc0e3d4
commit
633a2ee402
13 files changed
+686
-3
No files matched your search
+2
-1
@@ -47,7 +47,8 @@ const CASE_LIMIT: Duration = Duration::from_secs(20);
|
||||
/// The model sources whose hexadecimal constants are the class, chunk and
|
||||
/// attribute IDs the synthetic objects are made of. Every hexadecimal
|
||||
/// literal of 3 to 6 digits counts: a superset of the IDs is harmless.
|
||||
const SOURCES: [&str; 22] = [
|
||||
const SOURCES: [&str; 23] = [
|
||||
include_str!("model/animation.rs"),
|
||||
include_str!("model/attrs.rs"),
|
||||
include_str!("model/cjk.rs"),
|
||||
include_str!("model/color.rs"),
|
||||
|
||||
@@ -0,0 +1,336 @@
|
||||
//! `MotionPreset`, `TimingSetting` and `AnimationSetting`.
|
||||
//!
|
||||
//! Evidence: `docs/format/objects.md`, animation and timing. Values with
|
||||
//! one value in every sample and no stored field found (`PlaysLoop`,
|
||||
//! `ReverseAnimation`, `InitiallyHidden`, `HiddenAfter`, the preset
|
||||
//! locks, `PathOpen`) are written as that value.
|
||||
|
||||
use super::*;
|
||||
use crate::model::{Animation, MotionPreset, TimingList};
|
||||
|
||||
/// `TriggerEvent` codes of timing lists.
|
||||
const TRIGGERS: [(u32, &str); 4] = [
|
||||
(1, "OnPageLoad"),
|
||||
(5, "OnClick"),
|
||||
(6, "OnRelease"),
|
||||
(7, "OnRollover"),
|
||||
];
|
||||
const DESIGN_OPTIONS: [(u32, &str); 2] = [(0, "FromCurrentAppearance"), (2, "ToCurrentLocation")];
|
||||
const EASE_TYPES: [(u32, &str); 2] = [(0, "NoEase"), (4, "CustomEase")];
|
||||
/// `Properties` children of an animation by stored property name, in
|
||||
/// IDML order.
|
||||
const PROPERTY_ARRAYS: [(&str, &str); 4] = [
|
||||
("Alpha_Amount", "OpacityArray"),
|
||||
("Rotation_Z", "RotationArray"),
|
||||
("Scale_X", "ScaleXArray"),
|
||||
("Scale_Y", "ScaleYArray"),
|
||||
];
|
||||
|
||||
fn code(table: &[(u32, &'static str)], v: u32) -> Option<&'static str> {
|
||||
table.iter().find(|(c, _)| *c == v).map(|(_, n)| *n)
|
||||
}
|
||||
|
||||
/// A number rounded to 6 significant digits, as IDML writes
|
||||
/// `TransformOffsets`.
|
||||
fn six_digits(v: f64) -> String {
|
||||
num(format!("{v:.5e}").parse().unwrap_or(v))
|
||||
}
|
||||
|
||||
fn point(x: f64, y: f64) -> String {
|
||||
format!("{} {}", num(x), num(y))
|
||||
}
|
||||
|
||||
/// CDATA sections holding `s`, split inside any `]]>`.
|
||||
fn cdata_text(x: &mut Xml, s: &str) {
|
||||
let s: String = s.chars().filter(|&c| xml::xml_char(c)).collect();
|
||||
let mut parts = s.split("]]>").peekable();
|
||||
while let Some(p) = parts.next() {
|
||||
if parts.peek().is_some() {
|
||||
x.cdata(&format!("{p}]]"), CDATA_SECTION);
|
||||
x.cdata(">", CDATA_SECTION);
|
||||
} else if !p.is_empty() {
|
||||
x.cdata(p, CDATA_SECTION);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Writer<'_> {
|
||||
/// A `MotionPreset` of designmap.xml.
|
||||
pub(super) fn motion_preset(x: &mut Xml, p: &MotionPreset) {
|
||||
x.start("MotionPreset")
|
||||
.attr("Self", uref(Some(p.uid)))
|
||||
.attr("Name", p.name.idml())
|
||||
.attr("EditLocked", "false")
|
||||
.attr("DeleteLocked", "false")
|
||||
.attr("NameLocked", "false");
|
||||
x.start("Properties").start("Contents");
|
||||
cdata_text(x, &p.contents);
|
||||
x.end().end().end();
|
||||
}
|
||||
|
||||
fn warn(&self, msg: String) {
|
||||
self.warnings.borrow_mut().push(msg);
|
||||
}
|
||||
|
||||
/// The `TimingSetting` of a spread or button whose `Self` is `owner`;
|
||||
/// nothing without timing lists.
|
||||
pub(super) fn timing_setting(&self, x: &mut Xml, owner: &str, lists: &[TimingList]) {
|
||||
if lists.is_empty() {
|
||||
return;
|
||||
}
|
||||
x.start("TimingSetting");
|
||||
for (i, l) in lists.iter().enumerate() {
|
||||
let list = format!("{owner}TimingSetting1TimingList{i}");
|
||||
x.start("TimingList").attr("Self", &list);
|
||||
match code(&TRIGGERS, l.trigger) {
|
||||
Some(t) => {
|
||||
x.attr("TriggerEvent", t);
|
||||
}
|
||||
None => self.warn(format!(
|
||||
"{owner}: trigger code {} is not known; TriggerEvent left out",
|
||||
l.trigger
|
||||
)),
|
||||
}
|
||||
for (j, g) in l.groups.iter().enumerate() {
|
||||
let group = format!("{list}TimingGroup{j}");
|
||||
x.start("TimingGroup").attr("Self", &group);
|
||||
if (1..=100).contains(&g.plays) {
|
||||
x.attr("Plays", g.plays.to_string());
|
||||
} else {
|
||||
self.warn(format!("{group}: Plays {} left out", g.plays));
|
||||
}
|
||||
x.attr("PlaysLoop", "false");
|
||||
for (k, t) in g.targets.iter().enumerate() {
|
||||
let target = format!("{group}i{:x}", t.uid);
|
||||
x.start("TimingTarget")
|
||||
.attr("Self", &target)
|
||||
.attr("DynamicTarget", uref(Some(t.uid)));
|
||||
if (0.0..=60.0).contains(&t.delay) {
|
||||
x.attr("DelaySeconds", num(t.delay));
|
||||
} else {
|
||||
self.warn(format!("{target}: DelaySeconds {} left out", t.delay));
|
||||
}
|
||||
x.attr("ReverseAnimation", "false")
|
||||
.attr("TargetRole", t.role.to_string())
|
||||
.attr("TargetAction", t.action.to_string())
|
||||
.attr("Placement", k.to_string())
|
||||
.end();
|
||||
}
|
||||
x.end();
|
||||
}
|
||||
x.end();
|
||||
}
|
||||
x.end();
|
||||
}
|
||||
|
||||
/// The `AnimationSetting` of the page item `uid`.
|
||||
pub(super) fn animation_setting(&self, x: &mut Xml, uid: u32, a: &Animation) {
|
||||
x.start("AnimationSetting").attr(
|
||||
"TransformOffsets",
|
||||
format!("{} {}", six_digits(a.offsets[0]), six_digits(a.offsets[1])),
|
||||
);
|
||||
if (0.125..=60.0).contains(&a.duration) {
|
||||
x.attr("Duration", num(a.duration));
|
||||
} else {
|
||||
self.warn(format!(
|
||||
"item {uid}: animation Duration {} left out",
|
||||
a.duration
|
||||
));
|
||||
}
|
||||
match code(&DESIGN_OPTIONS, a.design) {
|
||||
Some(d) => {
|
||||
x.attr("DesignOption", d);
|
||||
}
|
||||
None => self.warn(format!(
|
||||
"item {uid}: animation design option {} is not known; left out",
|
||||
a.design
|
||||
)),
|
||||
}
|
||||
match code(&EASE_TYPES, a.ease) {
|
||||
Some(e) => {
|
||||
x.attr("EaseType", e);
|
||||
}
|
||||
None => self.warn(format!(
|
||||
"item {uid}: animation ease code {} is not known; left out",
|
||||
a.ease
|
||||
)),
|
||||
}
|
||||
if (1..=100).contains(&a.plays) {
|
||||
x.attr("Plays", a.plays.to_string());
|
||||
} else {
|
||||
self.warn(format!("item {uid}: animation Plays {} left out", a.plays));
|
||||
}
|
||||
x.attr("PlaysLoop", "false")
|
||||
.attr("InitiallyHidden", "false")
|
||||
.attr("HiddenAfter", "false")
|
||||
.attr("HasCustomSettings", a.custom.to_string());
|
||||
x.start("Properties");
|
||||
if a.preset != 0 {
|
||||
x.start("Preset")
|
||||
.attr("type", "object")
|
||||
.text(&uref(Some(a.preset)))
|
||||
.end();
|
||||
}
|
||||
if !a.path.is_empty() {
|
||||
x.start("MotionPath");
|
||||
for &(frame, px, py) in &a.path {
|
||||
let p = point(px, py);
|
||||
x.start("AnimationDataPathKeyFrameType")
|
||||
.attr("KeyFrame", frame.to_string());
|
||||
x.empty(
|
||||
"PathPoint",
|
||||
&[
|
||||
("Anchor", p.clone()),
|
||||
("LeftDirection", p.clone()),
|
||||
("RightDirection", p),
|
||||
],
|
||||
);
|
||||
x.end();
|
||||
}
|
||||
x.end();
|
||||
} else if !a.points.is_empty() {
|
||||
x.start("MotionPathPoints")
|
||||
.attr("PathOpen", "true")
|
||||
.start("PathPointArray");
|
||||
for &(px, py) in &a.points {
|
||||
let p = point(px, py);
|
||||
x.empty(
|
||||
"PathPointType",
|
||||
&[
|
||||
("Anchor", p.clone()),
|
||||
("LeftDirection", p.clone()),
|
||||
("RightDirection", p),
|
||||
],
|
||||
);
|
||||
}
|
||||
x.end().end();
|
||||
}
|
||||
for (name, tag) in PROPERTY_ARRAYS {
|
||||
let Some((_, keys)) = a.properties.iter().find(|(n, _)| n == name) else {
|
||||
continue;
|
||||
};
|
||||
if keys.is_empty() {
|
||||
continue;
|
||||
}
|
||||
x.start(tag);
|
||||
for &(frame, v) in keys {
|
||||
x.empty(
|
||||
"AnimationDataKeyFrameType",
|
||||
&[("KeyFrame", frame.to_string()), ("Value", num(v))],
|
||||
);
|
||||
}
|
||||
x.end();
|
||||
}
|
||||
x.end().end();
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::model::{TimingGroup, TimingTarget};
|
||||
|
||||
#[test]
|
||||
fn writes_timing_and_animation() {
|
||||
let doc = Document::default();
|
||||
let w = Writer::for_test(&doc);
|
||||
let lists = [TimingList {
|
||||
trigger: 1,
|
||||
groups: vec![
|
||||
TimingGroup {
|
||||
plays: 1,
|
||||
targets: vec![
|
||||
TimingTarget {
|
||||
uid: 0xFE,
|
||||
role: 132355,
|
||||
action: 132353,
|
||||
delay: 0.0,
|
||||
},
|
||||
TimingTarget {
|
||||
uid: 0x608,
|
||||
role: 132355,
|
||||
action: 132353,
|
||||
delay: 0.0,
|
||||
},
|
||||
],
|
||||
},
|
||||
TimingGroup {
|
||||
plays: 2,
|
||||
targets: vec![TimingTarget {
|
||||
uid: 0x119,
|
||||
role: 132355,
|
||||
action: 132353,
|
||||
delay: 0.25,
|
||||
}],
|
||||
},
|
||||
],
|
||||
}];
|
||||
let mut x = Xml::new();
|
||||
w.timing_setting(&mut x, "uff", &lists);
|
||||
let out: String = x.finish().lines().map(str::trim).collect();
|
||||
assert!(out.contains(
|
||||
"<TimingList Self=\"uffTimingSetting1TimingList0\" TriggerEvent=\"OnPageLoad\">\
|
||||
<TimingGroup Self=\"uffTimingSetting1TimingList0TimingGroup0\" Plays=\"1\" PlaysLoop=\"false\">\
|
||||
<TimingTarget Self=\"uffTimingSetting1TimingList0TimingGroup0ife\" DynamicTarget=\"ufe\" \
|
||||
DelaySeconds=\"0\" ReverseAnimation=\"false\" TargetRole=\"132355\" TargetAction=\"132353\" \
|
||||
Placement=\"0\" />"
|
||||
));
|
||||
assert!(out.contains(
|
||||
"<TimingTarget Self=\"uffTimingSetting1TimingList0TimingGroup1i119\" DynamicTarget=\"u119\" \
|
||||
DelaySeconds=\"0.25\" ReverseAnimation=\"false\" TargetRole=\"132355\" TargetAction=\"132353\" \
|
||||
Placement=\"0\" />"
|
||||
));
|
||||
|
||||
// One key frame and one property with two keys, as stored.
|
||||
let mut d = Vec::new();
|
||||
d.extend_from_slice(&1u16.to_le_bytes());
|
||||
d.extend_from_slice(&0u16.to_le_bytes());
|
||||
d.extend_from_slice(&0x58Eu32.to_le_bytes());
|
||||
d.extend_from_slice(&1f64.to_le_bytes());
|
||||
for v in [24u32, 24, 0, 0, 1] {
|
||||
d.extend_from_slice(&v.to_le_bytes());
|
||||
}
|
||||
d.extend_from_slice(&0u16.to_le_bytes());
|
||||
d.extend_from_slice(&4u32.to_le_bytes());
|
||||
d.extend_from_slice(&0.49992400000001f64.to_le_bytes());
|
||||
d.extend_from_slice(&0.5f64.to_le_bytes());
|
||||
for v in [1u32, 0, 2] {
|
||||
d.extend_from_slice(&v.to_le_bytes());
|
||||
}
|
||||
d.extend_from_slice(&[0; 16]);
|
||||
d.extend_from_slice(&0u32.to_le_bytes());
|
||||
d.extend_from_slice(&1u32.to_le_bytes());
|
||||
for _ in 0..2 {
|
||||
d.extend_from_slice(&12u32.to_le_bytes());
|
||||
d.extend_from_slice(&[12, 0x40]);
|
||||
d.extend_from_slice(b"Alpha_Amount");
|
||||
}
|
||||
d.extend_from_slice(&2u32.to_le_bytes());
|
||||
for (frame, v) in [(0u32, 0f64), (23, 100.0)] {
|
||||
d.extend_from_slice(&frame.to_le_bytes());
|
||||
d.extend_from_slice(&6u32.to_le_bytes());
|
||||
d.extend_from_slice(&v.to_le_bytes());
|
||||
}
|
||||
let a = Animation::read(&mut crate::Encoding::default().cursor(&d)).unwrap();
|
||||
let mut x = Xml::new();
|
||||
w.animation_setting(&mut x, 1, &a);
|
||||
let out: String = x.finish().lines().map(str::trim).collect();
|
||||
assert_eq!(
|
||||
out.split_once("?>").map_or("", |(_, b)| b),
|
||||
"<AnimationSetting TransformOffsets=\"0.499924 0.5\" Duration=\"1\" \
|
||||
DesignOption=\"FromCurrentAppearance\" EaseType=\"CustomEase\" Plays=\"1\" \
|
||||
PlaysLoop=\"false\" InitiallyHidden=\"false\" HiddenAfter=\"false\" \
|
||||
HasCustomSettings=\"false\"><Properties><Preset type=\"object\">u58e</Preset>\
|
||||
<MotionPath><AnimationDataPathKeyFrameType KeyFrame=\"0\">\
|
||||
<PathPoint Anchor=\"0 0\" LeftDirection=\"0 0\" RightDirection=\"0 0\" />\
|
||||
</AnimationDataPathKeyFrameType></MotionPath><OpacityArray>\
|
||||
<AnimationDataKeyFrameType KeyFrame=\"0\" Value=\"0\" />\
|
||||
<AnimationDataKeyFrameType KeyFrame=\"23\" Value=\"100\" /></OpacityArray>\
|
||||
</Properties></AnimationSetting>"
|
||||
);
|
||||
// Truncated: no animation.
|
||||
for n in 0..d.len() {
|
||||
assert!(Animation::read(&mut crate::Encoding::default().cursor(&d[..n])).is_err());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -352,6 +352,9 @@ impl Writer<'_> {
|
||||
}
|
||||
x.end();
|
||||
}
|
||||
for p in &doc.motion_presets {
|
||||
Self::motion_preset(&mut x, p);
|
||||
}
|
||||
singleton(&mut x, "ConditionalTextPreference");
|
||||
x.empty(
|
||||
"idPkg:Preferences",
|
||||
|
||||
@@ -7,6 +7,7 @@
|
||||
//! the text forms of values, `pages` page numbering, `values` the values
|
||||
//! every IDML has, and `transparency` the effect settings.
|
||||
|
||||
mod animation;
|
||||
mod applied;
|
||||
mod attrs;
|
||||
mod designmap;
|
||||
|
||||
@@ -263,6 +263,7 @@ mod tests {
|
||||
show_master_items: None,
|
||||
primary_story: None,
|
||||
transition: None,
|
||||
timing: Vec::new(),
|
||||
}],
|
||||
// Listed out of page order, as in some samples.
|
||||
sections: vec![
|
||||
|
||||
@@ -1102,6 +1102,16 @@ impl Writer<'_> {
|
||||
item.uid
|
||||
));
|
||||
}
|
||||
// IDML writes a multi-state object's animation after its states
|
||||
// (objects.md, animation settings); the schema allows a button's
|
||||
// only before its timing and states.
|
||||
let multi_state = matches!(&item.kind, ItemKind::Form(f) if f.kind == FormKind::MultiState);
|
||||
if !multi_state && let Some(a) = &item.animation {
|
||||
self.animation_setting(x, item.uid, a);
|
||||
}
|
||||
if let ItemKind::Form(f) = &item.kind {
|
||||
self.timing_setting(x, &uref(Some(item.uid)), &f.timing);
|
||||
}
|
||||
if let Some(a) = &item.anchor {
|
||||
self.anchors.borrow_mut().push(a.clone());
|
||||
}
|
||||
@@ -1114,6 +1124,9 @@ impl Writer<'_> {
|
||||
if let ItemKind::Form(f) = &item.kind {
|
||||
self.form_states(x, item, f, &item.transform.then(outer));
|
||||
}
|
||||
if multi_state && let Some(a) = &item.animation {
|
||||
self.animation_setting(x, item.uid, a);
|
||||
}
|
||||
for g in &item.graphics {
|
||||
self.placed_graphic(x, g, &item.transform.then(outer));
|
||||
}
|
||||
@@ -1782,6 +1795,7 @@ impl Writer<'_> {
|
||||
}
|
||||
fp.write(&mut x);
|
||||
}
|
||||
self.timing_setting(&mut x, &uref(Some(s.uid)), &s.timing);
|
||||
for p in &s.pages {
|
||||
let name = if master {
|
||||
prefix.clone()
|
||||
@@ -1974,6 +1988,7 @@ mod tests {
|
||||
show_master_items: None,
|
||||
primary_story: None,
|
||||
transition: None,
|
||||
timing: Vec::new(),
|
||||
};
|
||||
assert_eq!(primary_text_frame(&s).as_deref(), Some("n"));
|
||||
s.primary_story = Some(0);
|
||||
|
||||
+1
-1
@@ -23,7 +23,7 @@ pub struct Xml {
|
||||
|
||||
/// Whether XML 1.0 allows `c` (control characters aside): not U+FFFE or
|
||||
/// U+FFFF. Rust `char`s are never surrogates.
|
||||
fn xml_char(c: char) -> bool {
|
||||
pub(super) fn xml_char(c: char) -> bool {
|
||||
!matches!(c, '\u{FFFE}' | '\u{FFFF}')
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,294 @@
|
||||
//! Animation: motion presets, timing lists of spreads and buttons, and
|
||||
//! the animation settings of page items.
|
||||
//!
|
||||
//! Evidence: `docs/format/objects.md`, animation and timing.
|
||||
|
||||
use super::*;
|
||||
|
||||
/// A motion preset (class 0x20517, chunk 0x20532).
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub struct MotionPreset {
|
||||
pub uid: u32,
|
||||
pub name: Name,
|
||||
/// The preset's XML (IDML `Properties/Contents`).
|
||||
pub contents: String,
|
||||
}
|
||||
|
||||
/// One timing list of a spread or button (chunks 0x2056D and 0x2056E).
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub struct TimingList {
|
||||
/// Trigger code: 1 on page load, 5 on click, 6 on release, 7 on
|
||||
/// rollover.
|
||||
pub trigger: u32,
|
||||
pub groups: Vec<TimingGroup>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub struct TimingGroup {
|
||||
pub plays: u32,
|
||||
pub targets: Vec<TimingTarget>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub struct TimingTarget {
|
||||
pub uid: u32,
|
||||
pub role: u32,
|
||||
pub action: u32,
|
||||
/// Delay in seconds.
|
||||
pub delay: f64,
|
||||
}
|
||||
|
||||
/// The animation of a page item (chunk 0x2055F).
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub struct Animation {
|
||||
pub custom: bool,
|
||||
/// The motion preset's UID.
|
||||
pub preset: u32,
|
||||
/// Seconds.
|
||||
pub duration: f64,
|
||||
/// Design option code: 0 from current appearance, 2 to current
|
||||
/// location.
|
||||
pub design: u32,
|
||||
pub plays: u32,
|
||||
/// Ease code: 0 none, 4 custom.
|
||||
pub ease: u32,
|
||||
pub offsets: [f64; 2],
|
||||
/// Motion path key frames: frame, x, y.
|
||||
pub path: Vec<(u32, f64, f64)>,
|
||||
/// Points of a motion path without key frames.
|
||||
pub points: Vec<(f64, f64)>,
|
||||
/// Animated properties by stored name, with their keys (frame,
|
||||
/// value).
|
||||
pub properties: Vec<(String, Vec<(u32, f64)>)>,
|
||||
}
|
||||
|
||||
fn at_end(c: &Cursor) -> Result<(), Error> {
|
||||
if c.remaining() == 0 {
|
||||
Ok(())
|
||||
} else {
|
||||
Err(Error::Corrupt(format!(
|
||||
"{} bytes left after the data at {}",
|
||||
c.remaining(),
|
||||
c.pos()
|
||||
)))
|
||||
}
|
||||
}
|
||||
|
||||
/// A text string: u32 length in UTF-16 code units, then segments.
|
||||
fn text(c: &mut Cursor) -> Result<String, Error> {
|
||||
let n = c.u32()? as usize;
|
||||
c.segments(n)
|
||||
}
|
||||
|
||||
impl TimingList {
|
||||
/// The timing lists of chunk 0x2056D or 0x2056E, which must parse to
|
||||
/// its end.
|
||||
pub fn read_all(c: &mut Cursor) -> Result<Vec<TimingList>, Error> {
|
||||
parse_timing(c)
|
||||
}
|
||||
}
|
||||
|
||||
impl Animation {
|
||||
/// The animation in chunk 0x2055F longer than two bytes, which must
|
||||
/// parse to its end.
|
||||
pub fn read(c: &mut Cursor) -> Result<Animation, Error> {
|
||||
parse_animation(c)
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_timing(c: &mut Cursor) -> Result<Vec<TimingList>, Error> {
|
||||
let lists = c.u32()?;
|
||||
let mut out = Vec::new();
|
||||
for _ in 0..lists {
|
||||
let trigger = c.u32()?;
|
||||
let groups = c.u32()?;
|
||||
let mut list = TimingList {
|
||||
trigger,
|
||||
groups: Vec::new(),
|
||||
};
|
||||
for _ in 0..groups {
|
||||
let n = c.u32()?;
|
||||
let mut targets = Vec::new();
|
||||
for _ in 0..n {
|
||||
let uid = c.u32()?;
|
||||
let role = c.u32()?;
|
||||
let action = c.u32()?;
|
||||
c.u32()?;
|
||||
let delay = c.f64()?;
|
||||
targets.push(TimingTarget {
|
||||
uid,
|
||||
role,
|
||||
action,
|
||||
delay,
|
||||
});
|
||||
}
|
||||
let plays = c.u32()?;
|
||||
c.u16()?;
|
||||
list.groups.push(TimingGroup { plays, targets });
|
||||
}
|
||||
out.push(list);
|
||||
}
|
||||
at_end(c)?;
|
||||
Ok(out)
|
||||
}
|
||||
|
||||
fn parse_animation(c: &mut Cursor) -> Result<Animation, Error> {
|
||||
c.u16()?;
|
||||
let custom = c.u16()? == 1;
|
||||
let preset = c.u32()?;
|
||||
let duration = c.f64()?;
|
||||
// Frames per second and frames.
|
||||
c.skip(8)?;
|
||||
let design = c.u32()?;
|
||||
c.u32()?;
|
||||
let plays = c.u32()?;
|
||||
c.u16()?;
|
||||
let ease = c.u32()?;
|
||||
let offsets = [c.f64()?, c.f64()?];
|
||||
let mut path = Vec::new();
|
||||
for _ in 0..c.u32()? {
|
||||
let frame = c.u32()?;
|
||||
c.u32()?;
|
||||
path.push((frame, c.f64()?, c.f64()?));
|
||||
}
|
||||
let mut points = Vec::new();
|
||||
for _ in 0..c.u32()? {
|
||||
c.u32()?;
|
||||
points.push((c.f64()?, c.f64()?));
|
||||
}
|
||||
let mut properties = Vec::new();
|
||||
for _ in 0..c.u32()? {
|
||||
let name = text(c)?;
|
||||
text(c)?;
|
||||
let mut keys = Vec::new();
|
||||
for _ in 0..c.u32()? {
|
||||
let frame = c.u32()?;
|
||||
c.u32()?;
|
||||
keys.push((frame, c.f64()?));
|
||||
}
|
||||
properties.push((name, keys));
|
||||
}
|
||||
at_end(c)?;
|
||||
Ok(Animation {
|
||||
custom,
|
||||
preset,
|
||||
duration,
|
||||
design,
|
||||
plays,
|
||||
ease,
|
||||
offsets,
|
||||
path,
|
||||
points,
|
||||
properties,
|
||||
})
|
||||
}
|
||||
|
||||
impl Reader<'_> {
|
||||
/// The timing lists of a spread or button in chunk `id`; none, with a
|
||||
/// warning, if the chunk does not parse.
|
||||
pub(super) fn timing(&self, uid: u32, id: u32) -> Vec<TimingList> {
|
||||
let d = match self.chunk(uid, id) {
|
||||
Ok(Some(d)) => d,
|
||||
Ok(None) => return Vec::new(),
|
||||
Err(e) => {
|
||||
self.warn(format!("object {uid}: timing left out: {e}"));
|
||||
return Vec::new();
|
||||
}
|
||||
};
|
||||
parse_timing(&mut self.cursor(&d)).unwrap_or_else(|e| {
|
||||
self.warn(format!("object {uid}: timing left out: {e}"));
|
||||
Vec::new()
|
||||
})
|
||||
}
|
||||
|
||||
/// The animation of a page item; `None` without one, or with a
|
||||
/// warning if chunk 0x2055F does not parse.
|
||||
pub(super) fn animation(&self, uid: u32) -> Option<Animation> {
|
||||
let d = match self.chunk(uid, chunk::ANIMATION) {
|
||||
Ok(Some(d)) if d.len() > 2 => d,
|
||||
Ok(_) => return None,
|
||||
Err(e) => {
|
||||
self.warn(format!("item {uid}: animation left out: {e}"));
|
||||
return None;
|
||||
}
|
||||
};
|
||||
parse_animation(&mut self.cursor(&d))
|
||||
.map_err(|e| self.warn(format!("item {uid}: animation left out: {e}")))
|
||||
.ok()
|
||||
}
|
||||
|
||||
/// The motion presets listed in chunk 0x2053E of the preferences
|
||||
/// object, in that order. A preset that cannot be read is left out
|
||||
/// with a warning.
|
||||
pub(super) fn motion_presets(&self) -> Vec<MotionPreset> {
|
||||
let Some(&(prefs, _)) = self
|
||||
.db
|
||||
.classes()
|
||||
.iter()
|
||||
.find(|(_, c)| *c == class::PREFERENCES)
|
||||
else {
|
||||
return Vec::new();
|
||||
};
|
||||
let uids = match self.uid_list(prefs, chunk::PRESET_LIST) {
|
||||
Ok(u) => u,
|
||||
Err(e) => {
|
||||
self.warn(format!("motion presets left out: {e}"));
|
||||
return Vec::new();
|
||||
}
|
||||
};
|
||||
let mut out = Vec::new();
|
||||
for uid in uids {
|
||||
let read = || -> Result<MotionPreset, Error> {
|
||||
if self.class(uid) != Some(class::MOTION_PRESET) {
|
||||
return Err(Error::Corrupt("not a motion preset".into()));
|
||||
}
|
||||
let d = self.required(uid, chunk::PRESET_DATA)?;
|
||||
let mut c = self.cursor(&d);
|
||||
let contents = text(&mut c)?;
|
||||
c.skip(4)?;
|
||||
c.name()?;
|
||||
let name = c.name()?;
|
||||
at_end(&c)?;
|
||||
Ok(MotionPreset {
|
||||
uid,
|
||||
name,
|
||||
contents,
|
||||
})
|
||||
};
|
||||
match read() {
|
||||
Ok(p) => out.push(p),
|
||||
Err(e) => self.warn(format!("motion preset {uid} left out: {e}")),
|
||||
}
|
||||
}
|
||||
out
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn truncated_chunks_do_not_parse() {
|
||||
let le = Encoding::default();
|
||||
// One list, one group, one target (objects.md).
|
||||
let mut t = Vec::new();
|
||||
for v in [1u32, 1, 1, 1, 0x463, 0x20503, 0x20501, 0] {
|
||||
t.extend_from_slice(&v.to_le_bytes());
|
||||
}
|
||||
t.extend_from_slice(&0f64.to_le_bytes());
|
||||
t.extend_from_slice(&1u32.to_le_bytes());
|
||||
t.extend_from_slice(&0u16.to_le_bytes());
|
||||
assert_eq!(parse_timing(&mut le.cursor(&t)).unwrap().len(), 1);
|
||||
for n in 0..t.len() {
|
||||
assert!(parse_timing(&mut le.cursor(&t[..n])).is_err());
|
||||
}
|
||||
let mut longer = t.clone();
|
||||
longer.push(0);
|
||||
assert!(parse_timing(&mut le.cursor(&longer)).is_err());
|
||||
// A count that the data cannot hold.
|
||||
let huge = [0xFF, 0xFF, 0xFF, 0xFF, 1, 0, 0, 0];
|
||||
assert!(parse_animation(&mut le.cursor(&huge)).is_err());
|
||||
assert!(parse_timing(&mut le.cursor(&huge)).is_err());
|
||||
}
|
||||
}
|
||||
@@ -57,6 +57,8 @@ pub struct Document {
|
||||
/// Named grids (class 0xCD12, chunk 0xCD28: u32, a flag byte, 1 for
|
||||
/// a built-in key, and the name), in UID order.
|
||||
pub named_grids: Vec<NamedGrid>,
|
||||
/// Motion presets, in the order of chunk 0x2053E of the preferences.
|
||||
pub motion_presets: Vec<MotionPreset>,
|
||||
pub spreads: Vec<Spread>,
|
||||
pub master_spreads: Vec<Spread>,
|
||||
pub stories: Vec<Story>,
|
||||
@@ -279,6 +281,7 @@ impl<'a> Reader<'a> {
|
||||
let toc_styles = self.toc_styles(version);
|
||||
let stroke_styles = self.stroke_styles();
|
||||
let named_grids = self.named_grids();
|
||||
let motion_presets = self.motion_presets();
|
||||
let index_groups = self.index_groups();
|
||||
let index = self.index().unwrap_or_else(|e| {
|
||||
self.warn(format!("index left out: {e}"));
|
||||
@@ -377,6 +380,7 @@ impl<'a> Reader<'a> {
|
||||
toc_styles,
|
||||
stroke_styles,
|
||||
named_grids,
|
||||
motion_presets,
|
||||
index_groups,
|
||||
index,
|
||||
constant_shade,
|
||||
|
||||
@@ -42,6 +42,8 @@ pub mod class {
|
||||
pub const DASHED_STROKE_STYLE: u32 = 0x5A43;
|
||||
pub const STRIPED_STROKE_STYLE: u32 = 0xB016;
|
||||
pub const NAMED_GRID: u32 = 0xCD12;
|
||||
/// A motion preset (IDML `MotionPreset`).
|
||||
pub const MOTION_PRESET: u32 = 0x20517;
|
||||
pub const ASSIGNMENT: u32 = 0x1BE01;
|
||||
pub const SMOOTH_SHADE: u32 = 0x5533;
|
||||
/// Form fields and multi-state objects (IDML `Button`, `CheckBox`,
|
||||
@@ -354,6 +356,16 @@ pub mod chunk {
|
||||
/// Size and position of an object style's transform attributes.
|
||||
pub const OBJECT_STYLE_TRANSFORM: u32 = 0x1E234;
|
||||
pub const GUIDE: u32 = 0x3308;
|
||||
/// Preferences: u32 count, then the motion preset UIDs.
|
||||
pub const PRESET_LIST: u32 = 0x2053E;
|
||||
/// Motion preset: contents, 4 bytes, an empty name, the name.
|
||||
pub const PRESET_DATA: u32 = 0x20532;
|
||||
/// Timing lists of a spread (and, empty, of most page items).
|
||||
pub const SPREAD_TIMING: u32 = 0x2056D;
|
||||
/// Timing lists of a button.
|
||||
pub const BUTTON_TIMING: u32 = 0x2056E;
|
||||
/// Animation of a page item; `00 00` for none.
|
||||
pub const ANIMATION: u32 = 0x2055F;
|
||||
}
|
||||
|
||||
/// Kinds of strand run data (first u32 of chunk 0x262).
|
||||
|
||||
+11
-1
@@ -135,6 +135,8 @@ pub struct Form {
|
||||
pub font_size: Option<f64>,
|
||||
/// Font family and style of a text box.
|
||||
pub font: Option<(String, Option<String>)>,
|
||||
/// Timing lists of a button (chunk 0x2056E).
|
||||
pub timing: Vec<TimingList>,
|
||||
}
|
||||
|
||||
/// One state of a form field.
|
||||
@@ -186,6 +188,8 @@ pub struct PageItem {
|
||||
pub text_paths: Vec<TextPath>,
|
||||
/// Script label: key and value pairs, in stored order.
|
||||
pub label: Vec<(String, String)>,
|
||||
/// Animation (chunk 0x2055F).
|
||||
pub animation: Option<Animation>,
|
||||
}
|
||||
|
||||
/// Text on the path of a shape or frame (class 0xB320). See
|
||||
@@ -1170,7 +1174,11 @@ impl<'a> Reader<'a> {
|
||||
} else {
|
||||
Vec::new()
|
||||
};
|
||||
ItemKind::Form(form_field(kind, states, &attrs, &children))
|
||||
let mut f = form_field(kind, states, &attrs, &children);
|
||||
if kind == FormKind::Button {
|
||||
f.timing = self.timing(uid, chunk::BUTTON_TIMING);
|
||||
}
|
||||
ItemKind::Form(f)
|
||||
} else if cls == Some(class::GROUP) {
|
||||
ItemKind::Group
|
||||
} else if cls == Some(class::EPS_TEXT) {
|
||||
@@ -1231,6 +1239,7 @@ impl<'a> Reader<'a> {
|
||||
self.warn(format!("item {uid}: label left out: {e}"));
|
||||
Vec::new()
|
||||
}),
|
||||
animation: self.animation(uid),
|
||||
}))
|
||||
}
|
||||
|
||||
@@ -1857,6 +1866,7 @@ fn form_field(
|
||||
.and_then(Value::as_f64)
|
||||
.filter(|_| matches!(kind, FormKind::TextBox | FormKind::ComboBox)),
|
||||
font,
|
||||
timing: Vec::new(),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -7,6 +7,7 @@
|
||||
//! `style`, `settings` (document-level settings and lists) and the other
|
||||
//! child modules; `ids` holds the class and chunk IDs.
|
||||
|
||||
mod animation;
|
||||
pub mod attrs;
|
||||
pub mod cjk;
|
||||
pub mod color;
|
||||
@@ -29,6 +30,7 @@ pub mod xml;
|
||||
pub mod xmp;
|
||||
pub mod xref;
|
||||
|
||||
pub use animation::{Animation, MotionPreset, TimingGroup, TimingList, TimingTarget};
|
||||
pub use document::*;
|
||||
pub use ids::*;
|
||||
pub use item::*;
|
||||
@@ -301,6 +303,7 @@ mod tests {
|
||||
show_master_items: None,
|
||||
primary_story: None,
|
||||
transition: None,
|
||||
timing: Vec::new(),
|
||||
};
|
||||
// Master B (pages listed right to left) is based on master A.
|
||||
let mut masters = vec![
|
||||
|
||||
@@ -162,6 +162,8 @@ pub struct Spread {
|
||||
/// Page transition type and direction codes (chunk 0x2050C, u32 at 0
|
||||
/// and 8); `None` without the chunk.
|
||||
pub transition: Option<(u32, u32)>,
|
||||
/// Timing lists (chunk 0x2056D).
|
||||
pub timing: Vec<TimingList>,
|
||||
}
|
||||
|
||||
/// A ruler guide (class 0x3301, chunk 0x3308). See `docs/format/objects.md`.
|
||||
@@ -488,6 +490,7 @@ impl<'a> Reader<'a> {
|
||||
.and_then(|d| Some((self.enc().u32_at(&d, 0)?, self.enc().u32_at(&d, 8)?)));
|
||||
Ok(Spread {
|
||||
transition,
|
||||
timing: self.timing(uid, chunk::SPREAD_TIMING),
|
||||
uid,
|
||||
master_name,
|
||||
transform,
|
||||
|
||||
Reference in new issue
Block a user