1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
//! Presentations (ISO 32000-1 §12.4.4): a page's display duration and its
//! transition dictionary, read as data with the table's defaults filled in.
use crate::document::Page;
use crate::object::{Dict, Object};
use crate::source::AsyncObjectSource;
use crate::tree::{resolved_dict, Entries};
/// The transition styles of ISO 32000-1 Table 162: how a presentation
/// reveals the page it moves to.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum TransitionStyle {
/// Two lines sweep across the screen, along `Dimension` and `Motion`.
Split,
/// Evenly spaced lines sweep in the same direction, along `Dimension`.
Blinds,
/// A rectangle sweeps inward from the edges or outward from the centre.
Box,
/// One line sweeps from one edge to the other, in `TransitionDirection`.
Wipe,
/// The old page dissolves into the new one.
Dissolve,
/// A dissolve that sweeps across the page as a band.
Glitter,
/// `/R`: the new page replaces the old one with no effect, the default.
#[default]
Replace,
/// PDF 1.5: changes fly in or out, scaled from `Transition::scale`.
Fly,
/// PDF 1.5: the new page pushes the old one off the screen.
Push,
/// PDF 1.5: the new page slides over the old one.
Cover,
/// PDF 1.5: the old page slides off, uncovering the new one.
Uncover,
/// PDF 1.5: the new page fades in through the old one.
Fade,
}
impl TransitionStyle {
/// The style `/S` names; `None` for a name the table does not list.
pub fn from_name(name: &str) -> Option<TransitionStyle> {
Some(match name {
"Split" => TransitionStyle::Split,
"Blinds" => TransitionStyle::Blinds,
"Box" => TransitionStyle::Box,
"Wipe" => TransitionStyle::Wipe,
"Dissolve" => TransitionStyle::Dissolve,
"Glitter" => TransitionStyle::Glitter,
"R" => TransitionStyle::Replace,
"Fly" => TransitionStyle::Fly,
"Push" => TransitionStyle::Push,
"Cover" => TransitionStyle::Cover,
"Uncover" => TransitionStyle::Uncover,
"Fade" => TransitionStyle::Fade,
_ => return None,
})
}
}
/// `/Dm`: the dimension a Split or Blinds transition moves along.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum Dimension {
/// `/H`, the default.
#[default]
Horizontal,
/// `/V`.
Vertical,
}
impl Dimension {
/// The dimension `/Dm` names; `None` for any other name.
pub fn from_name(name: &str) -> Option<Dimension> {
match name {
"H" => Some(Dimension::Horizontal),
"V" => Some(Dimension::Vertical),
_ => None,
}
}
}
/// `/M`: the direction of motion of a Split, Box or Fly transition.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum Motion {
/// `/I`: inward from the edges of the page, the default.
#[default]
Inward,
/// `/O`: outward from the centre of the page.
Outward,
}
impl Motion {
/// The motion `/M` names; `None` for any other name.
pub fn from_name(name: &str) -> Option<Motion> {
match name {
"I" => Some(Motion::Inward),
"O" => Some(Motion::Outward),
_ => None,
}
}
}
/// `/Di`: the direction a Wipe, Glitter, Fly, Cover, Uncover or Push
/// transition moves in.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TransitionDirection {
/// Degrees counterclockwise from left to right: 0, 90, 180, 270 or 315
/// in the table, 0 being the default.
Angle(u32),
/// The name `None`, meaningful for a Fly transition whose scale is not 1.
None,
}
impl Default for TransitionDirection {
fn default() -> TransitionDirection {
TransitionDirection::Angle(0)
}
}
/// A transition dictionary (ISO 32000-1 §12.4.4, Table 162) with every
/// absent entry at its default.
#[derive(Debug, Clone, PartialEq)]
pub struct Transition {
/// `/S`, `Replace` when absent or not a listed style.
pub style: TransitionStyle,
/// `/D`: the effect's duration in seconds, 1 when absent.
pub duration: f64,
/// `/Dm`, horizontal when absent.
pub dimension: Dimension,
/// `/M`, inward when absent.
pub motion: Motion,
/// `/Di`, left to right when absent.
pub direction: TransitionDirection,
/// `/SS`: the starting or ending scale of a Fly transition, 1 when absent.
pub scale: f64,
/// `/B`: whether a Fly transition's area is rectangular and opaque.
pub opaque: bool,
}
impl Default for Transition {
fn default() -> Transition {
Transition {
style: TransitionStyle::default(),
duration: 1.0,
dimension: Dimension::default(),
motion: Motion::default(),
direction: TransitionDirection::default(),
scale: 1.0,
opaque: false,
}
}
}
/// How a page takes part in a presentation (ISO 32000-1 §12.4.4): its
/// display duration and the transition that reveals it.
#[derive(Debug, Clone, PartialEq)]
pub struct Presentation {
/// `/Dur`: the seconds the page shows before the presentation advances
/// on its own; `None` leaves the page waiting for the user.
pub duration: Option<f64>,
/// `/Trans`: the transition into this page; `None` without one.
pub transition: Option<Transition>,
}
/// The presentation entries of `page`: `None` when it carries neither
/// `/Dur` nor a `/Trans` dictionary, so a plain page is no slide.
///
/// Covers ISO 32000-1 §12.4.4.
pub async fn presentation_with<S: AsyncObjectSource>(src: &S, page: &Page) -> Option<Presentation> {
let dict = page.dict();
let duration = match dict.get("Dur") {
Some(value) => src
.resolve(value)
.await
.ok()
.and_then(|value| value.as_f64()),
None => None,
};
let transition = match dict.get("Trans") {
Some(value) => match resolved_dict(src, value).await {
Some(trans) => Some(transition_from(src, &trans).await),
None => None,
},
None => None,
};
if duration.is_none() && transition.is_none() {
return None;
}
Some(Presentation {
duration,
transition,
})
}
/// Table 162 with defaults: an entry that is absent, or a name the table
/// does not list, takes the default.
async fn transition_from<S: AsyncObjectSource>(src: &S, dict: &Dict) -> Transition {
let entries = Entries { src, dict };
let defaults = Transition::default();
let direction = match entries.value("Di").await {
Some(Object::Name(name)) if name.0 == "None" => TransitionDirection::None,
Some(value) => value
.as_f64()
.map(|degrees| TransitionDirection::Angle(degrees as u32))
.unwrap_or_default(),
None => TransitionDirection::default(),
};
Transition {
style: entries
.named("S", TransitionStyle::from_name)
.await
.unwrap_or_default(),
duration: entries
.value("D")
.await
.and_then(|value| value.as_f64())
.unwrap_or(defaults.duration),
dimension: entries
.named("Dm", Dimension::from_name)
.await
.unwrap_or_default(),
motion: entries
.named("M", Motion::from_name)
.await
.unwrap_or_default(),
direction,
scale: entries
.value("SS")
.await
.and_then(|value| value.as_f64())
.unwrap_or(defaults.scale),
opaque: entries.flag("B").await.unwrap_or(defaults.opaque),
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::document::Document;
use pdfboss_testkit::PdfBuilder;
/// A one-page document whose page carries `page_extra`.
fn doc_with(page_extra: &str) -> Document {
let mut b = PdfBuilder::new();
b.object(1, "<< /Type /Catalog /Pages 2 0 R >>");
b.object(2, "<< /Type /Pages /Kids [3 0 R] /Count 1 >>");
b.object(
3,
&format!("<< /Type /Page /Parent 2 0 R /MediaBox [0 0 10 10] {page_extra} >>"),
);
Document::load(b.build(1)).unwrap()
}
fn presentation(page_extra: &str) -> Option<Presentation> {
let doc = doc_with(page_extra);
doc.presentation(&doc.page(0).unwrap())
}
/// The clause's EXAMPLE: a five-second page reached through a
/// 3.5-second vertical split moving outward; the entries the example
/// leaves out take Table 162's defaults.
// Covers ISO 32000-1 §12.4.4.
#[test]
fn reads_the_duration_and_the_transition_with_the_tables_defaults() {
let shown =
presentation("/Dur 5 /Trans << /Type /Trans /D 3.5 /S /Split /Dm /V /M /O >>").unwrap();
assert_eq!(shown.duration, Some(5.0));
assert_eq!(
shown.transition,
Some(Transition {
style: TransitionStyle::Split,
duration: 3.5,
dimension: Dimension::Vertical,
motion: Motion::Outward,
direction: TransitionDirection::Angle(0),
scale: 1.0,
opaque: false,
})
);
let fly = presentation("/Trans << /S /Fly /Di /None /SS 0.5 /B true >>").unwrap();
assert_eq!(fly.duration, None);
let fly = fly.transition.unwrap();
assert_eq!(fly.style, TransitionStyle::Fly);
assert_eq!(fly.direction, TransitionDirection::None);
assert_eq!((fly.scale, fly.opaque), (0.5, true));
let wipe = presentation("/Trans << /S /Wipe /Di 270 >>")
.unwrap()
.transition
.unwrap();
assert_eq!(wipe.style, TransitionStyle::Wipe);
assert_eq!(wipe.direction, TransitionDirection::Angle(270));
assert_eq!(
presentation("/Trans << >>").unwrap().transition,
Some(Transition::default())
);
assert_eq!(Transition::default().style, TransitionStyle::Replace);
assert_eq!(Transition::default().duration, 1.0);
}
/// A page with neither entry is no slide; `/Dur` alone times the page
/// without a transition; a `/Trans` that is no dictionary reads as no
/// transition; an unknown style reads as the effect-free default.
// Covers ISO 32000-1 §12.4.4.
#[test]
fn pages_without_the_entries_have_no_presentation_and_odd_values_fall_back() {
assert_eq!(presentation(""), None);
assert_eq!(
presentation("/Dur 7"),
Some(Presentation {
duration: Some(7.0),
transition: None,
})
);
assert_eq!(presentation("/Trans 5"), None);
assert_eq!(
presentation("/Dur 2 /Trans 5"),
Some(Presentation {
duration: Some(2.0),
transition: None,
})
);
let odd = presentation("/Trans << /S /Sparkle /Dm /Q /M /Z /Di 45 >>")
.unwrap()
.transition
.unwrap();
assert_eq!(odd.style, TransitionStyle::Replace);
assert_eq!(odd.dimension, Dimension::Horizontal);
assert_eq!(odd.motion, Motion::Inward);
assert_eq!(odd.direction, TransitionDirection::Angle(45));
}
}