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
//! Document requirements (ISO 32000-1 §12.10): the catalog's `/Requirements`
//! array, one dictionary per feature a reader needs to show the document as
//! intended.
use crate::object::{Dict, Object};
use crate::source::AsyncObjectSource;
use crate::tree::{resolved_dict, Entries};
/// One entry of the catalog's `/Requirements` array (ISO 32000-1 §12.10.1,
/// Table 266): a feature the document needs a reader to support.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Requirement {
/// `/S`: the requirement type. `EnableJavaScripts` is the one type ISO
/// 32000-1 defines; any other name is kept as written.
pub kind: String,
/// `/RH`: the handlers a reader that does not meet the requirement
/// runs, one dictionary or an array of them, in order (§12.10.2).
pub handlers: Vec<RequirementHandler>,
}
/// One requirement handler (ISO 32000-1 §12.10.2, Table 267), read as data:
/// pdfboss runs no JavaScript, so the handler is reported, not invoked.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct RequirementHandler {
/// `/S`: `JS` for a document-level JavaScript, `NoOp` for doing nothing;
/// any other name is kept as written.
pub kind: String,
/// `/Script`: the name of the document-level JavaScript a `JS` handler
/// runs, as the catalog's `/Names` `/JavaScript` tree lists it.
pub script: Option<String>,
}
/// The requirements the catalog's `/Requirements` array lists, in order:
/// empty when the catalog has no array. An entry that is no dictionary, or
/// whose `/S` is no name, is skipped.
///
/// Covers ISO 32000-1 §12.10.1.
pub async fn requirements_with<S: AsyncObjectSource>(src: &S, trailer: &Dict) -> Vec<Requirement> {
let Some(root) = trailer.get("Root") else {
return Vec::new();
};
let Some(catalog) = resolved_dict(src, root).await else {
return Vec::new();
};
let Some(entry) = catalog.get("Requirements") else {
return Vec::new();
};
let Ok(Object::Array(entries)) = src.resolve(entry).await else {
return Vec::new();
};
let mut requirements = Vec::new();
for entry in &entries {
let Some(dict) = resolved_dict(src, entry).await else {
continue;
};
let Some(kind) = Entries { src, dict: &dict }.value("S").await else {
continue;
};
let Some(kind) = kind.as_name() else {
continue;
};
requirements.push(Requirement {
kind: kind.0.clone(),
handlers: handlers_with(src, &dict).await,
});
}
requirements
}
/// The `/RH` handlers of one requirement dictionary, in order: empty when
/// the entry is missing or neither a dictionary nor an array. A handler
/// that is no dictionary, or whose `/S` is no name, is skipped.
///
/// Covers ISO 32000-1 §12.10.2.
async fn handlers_with<S: AsyncObjectSource>(
src: &S,
requirement: &Dict,
) -> Vec<RequirementHandler> {
let Some(entry) = requirement.get("RH") else {
return Vec::new();
};
let entries = match src.resolve(entry).await {
Ok(Object::Array(entries)) => entries,
Ok(handler @ Object::Dict(_)) => vec![handler],
_ => return Vec::new(),
};
let mut handlers = Vec::new();
for entry in &entries {
let Some(dict) = resolved_dict(src, entry).await else {
continue;
};
let entries = Entries { src, dict: &dict };
let Some(kind) = entries.value("S").await else {
continue;
};
let Some(kind) = kind.as_name() else {
continue;
};
handlers.push(RequirementHandler {
kind: kind.0.clone(),
script: entries.text("Script").await,
});
}
handlers
}
#[cfg(test)]
mod tests {
use super::RequirementHandler;
use crate::document::Document;
use pdfboss_testkit::PdfBuilder;
/// A one-page document whose catalog carries `catalog_extra`; object 20
/// is an EnableJavaScripts requirement reachable by reference, object 21
/// a JavaScript requirement handler.
fn doc_with(catalog_extra: &str) -> Document {
let mut b = PdfBuilder::new();
b.object(
1,
&format!("<< /Type /Catalog /Pages 2 0 R {catalog_extra} >>"),
);
b.object(2, "<< /Type /Pages /Kids [] /Count 0 >>");
b.object(
20,
"<< /Type /Requirement /S /EnableJavaScripts \
/RH << /Type /ReqHandler /S /JS /Script (init) >> >>",
);
b.object(21, "<< /Type /ReqHandler /S /JS /Script (fallback) >>");
Document::load(b.build(1)).unwrap()
}
/// `/RH` as one handler dictionary or as an array of them reads in
/// order: the `/S` kind and, for `/JS`, the `/Script` name of the
/// document-level JavaScript that handles the requirement.
// Covers ISO 32000-1 §12.10.2.
#[test]
fn reads_the_requirement_handlers() {
let doc = doc_with(
"/Requirements [20 0 R << /S /Custom /RH [ << /Type /ReqHandler /S /NoOp >> 21 0 R ] >>]",
);
let requirements = doc.requirements();
assert_eq!(
requirements[0].handlers,
[RequirementHandler {
kind: "JS".into(),
script: Some("init".into()),
}]
);
assert_eq!(
requirements[1].handlers,
[
RequirementHandler {
kind: "NoOp".into(),
script: None,
},
RequirementHandler {
kind: "JS".into(),
script: Some("fallback".into()),
},
]
);
}
/// A requirement without `/RH`, or whose `/RH` is neither a dictionary
/// nor an array, has no handlers; a handler that is no dictionary or has
/// no `/S` name is skipped.
// Covers ISO 32000-1 §12.10.2.
#[test]
fn missing_or_malformed_handlers_read_as_none() {
let doc = doc_with(
"/Requirements [ << /S /A >> << /S /B /RH 7 >> \
<< /S /C /RH [ 7 (text) << /Type /ReqHandler >> << /S /NoOp >> ] >> ]",
);
let counts: Vec<usize> = doc
.requirements()
.iter()
.map(|requirement| requirement.handlers.len())
.collect();
assert_eq!(counts, [0, 0, 1]);
}
/// The array's dictionaries read in order, by reference or written
/// directly, each with its `/S` type name.
// Covers ISO 32000-1 §12.10.1.
#[test]
fn reads_the_requirements_in_order() {
let doc = doc_with("/Requirements [20 0 R << /Type /Requirement /S /Custom >>]");
let requirements = doc.requirements();
let kinds: Vec<&str> = requirements
.iter()
.map(|requirement| requirement.kind.as_str())
.collect();
assert_eq!(kinds, ["EnableJavaScripts", "Custom"]);
}
/// A catalog without `/Requirements`, or whose entry is no array, has
/// none; an entry that is no dictionary or has no `/S` name is skipped.
// Covers ISO 32000-1 §12.10.1.
#[test]
fn missing_or_malformed_requirements_read_as_none() {
assert!(doc_with("").requirements().is_empty());
assert!(doc_with("/Requirements 20 0 R").requirements().is_empty());
let odd =
doc_with("/Requirements [7 (text) << /Type /Requirement >> 20 0 R]").requirements();
assert_eq!(odd.len(), 1);
assert_eq!(odd[0].kind, "EnableJavaScripts");
}
}