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
//! Link annotations (ISO 32000-2 §12.5.6.5): where each link on a page sits and what it does,
//! read without running anything. A link goes to a destination (`/Dest`, or a `GoTo` action's
//! `/D`, §12.6.4.2), a URI (§12.6.4.8), another file (`GoToR`, §12.6.4.3) or a named action
//! (§12.6.4.11); any other action is reported by its type so a viewer can say it won't run it.
//!
//! Reading is tolerant (ADR 0003): a link without a usable `/Rect` is left out, a malformed
//! `/QuadPoints` falls back to the rectangle, and an unreadable action leaves the link with
//! [`LinkAction::None`].
use laminapdf_cos::{Dict, Object, Result};
use crate::document::Document;
use crate::geometry::{UserSpacePoint, UserSpaceRect};
use crate::id::AnnotId;
use crate::outline::{ActionKind, Destination, Outline};
use crate::page::{HIDDEN_ANNOT_FLAGS, Page, number, read_rect};
use crate::values::{file_spec_name, read_name, text_of};
/// One link annotation on a page.
///
/// Added in 0.2.0.
#[derive(Debug, Clone, PartialEq)]
#[non_exhaustive]
pub struct Link {
/// Its ID, when the annotation is an object of its own (a direct dictionary in `/Annots`
/// has none, and the link ops can't name it).
pub id: Option<AnnotId>,
/// `/Rect`, in user space.
pub rect: UserSpaceRect,
/// `/QuadPoints` (§12.5.6.5, Table 179), four corners each, in user space: the areas that
/// follow the link when the file gives them; empty when absent or malformed.
pub quads: Vec<[UserSpacePoint; 4]>,
/// `/Contents`, decoded (§7.9.2.2): the link's alternate description.
pub contents: Option<String>,
/// What following the link does.
pub action: LinkAction,
}
/// What following a link does.
///
/// Added in 0.2.0.
#[derive(Debug, Clone, PartialEq)]
#[non_exhaustive]
pub enum LinkAction {
/// A destination in this document (`/Dest` or `GoTo`), resolved as far as the file allows:
/// [`Destination::page_index`] is `None` for a broken one.
GoTo(Destination),
/// `URI`: the address as stored (§12.6.4.8: 7-bit ASCII), not resolved against the
/// catalog's `/URI /Base`.
Uri(Vec<u8>),
/// `GoToR`: a destination in another file, named by its file specification (`/UF`, else
/// `/F`), when readable.
OtherFile(Option<String>),
/// `Named`: the action's name as stored (`NextPage`, `PrevPage`, `FirstPage`, `LastPage`
/// are the standard ones, Table 207).
Named(Vec<u8>),
/// Any other action (`Launch`, `JavaScript`, `SubmitForm`…), which a reader never runs.
Other(ActionKind),
/// No action, or one that can't be read.
None,
}
impl Outline<'_> {
/// The visible link annotations of `page` ([`crate::Pages::links`]), destinations resolved
/// through this outline's named destinations.
pub(crate) fn links(&self, page: &Page<'_>) -> Result<Vec<Link>> {
let doc = self.doc();
let Some(annots) = page.dict().get("Annots") else {
return Ok(Vec::new());
};
let Some(annots) = doc.read(annots)? else {
return Ok(Vec::new());
};
let mut links = Vec::new();
for entry in annots.as_array().unwrap_or_default() {
let Some(annot) = doc.read(entry)? else { continue };
let Some(annot) = annot.as_dict() else { continue };
if let Some(link) = self.link(annot, entry.as_ref().map(AnnotId::object))? {
links.push(link);
}
}
Ok(links)
}
fn link(&self, annot: &Dict, id: Option<AnnotId>) -> Result<Option<Link>> {
let doc = self.doc();
if !matches!(read_name(doc, annot.get("Subtype"))?.as_deref(), Some(b"Link")) {
return Ok(None);
}
let flags = match annot.get("F") {
Some(f) => doc.read(f)?.and_then(|f| f.as_int()).unwrap_or(0),
None => 0,
};
if flags & HIDDEN_ANNOT_FLAGS != 0 {
return Ok(None);
}
let Some(rect) = (match annot.get("Rect") {
Some(rect) => read_rect(doc, rect)?,
None => None,
}) else {
return Ok(None);
};
let contents = match annot.get("Contents") {
Some(c) => doc
.read(c)?
.as_deref()
.and_then(text_of)
.filter(|s| !s.trim().is_empty()),
None => None,
};
Ok(Some(Link {
id,
rect,
quads: quads(doc, annot.get("QuadPoints"))?,
contents,
action: self.link_action(annot)?,
}))
}
/// `/Dest` first, as for outline items (§12.5.6.5: `/Dest` and `/A` are exclusive, and a
/// `/Dest` that can't be read counts as absent), then `/A`.
fn link_action(&self, annot: &Dict) -> Result<LinkAction> {
let doc = self.doc();
if let Some(dest) = annot.get("Dest")
&& let Some(dest) = self.destination(dest)?
{
return Ok(LinkAction::GoTo(dest));
}
let Some(action) = annot.get("A") else {
return Ok(LinkAction::None);
};
let Some(action) = doc.read(action)? else {
return Ok(LinkAction::None);
};
let Some(action) = action.as_dict() else {
return Ok(LinkAction::None);
};
let Some(kind) = read_name(doc, action.get("S"))? else {
return Ok(LinkAction::None);
};
Ok(match ActionKind::from_name(&kind) {
ActionKind::GoTo => match action.get("D") {
Some(d) => self.destination(d)?.map_or(LinkAction::None, LinkAction::GoTo),
None => LinkAction::None,
},
ActionKind::Uri => match read_bytes(doc, action.get("URI"))? {
Some(uri) => LinkAction::Uri(uri),
None => LinkAction::None,
},
ActionKind::GoToR => LinkAction::OtherFile(file_name(doc, action.get("F"))?),
ActionKind::Named => match read_name(doc, action.get("N"))? {
Some(name) => LinkAction::Named(name),
None => LinkAction::None,
},
other => LinkAction::Other(other),
})
}
}
/// A string's bytes (some writers store a URI as a name, which is read too).
fn read_bytes(doc: &Document, value: Option<&Object>) -> Result<Option<Vec<u8>>> {
let Some(value) = value else { return Ok(None) };
Ok(match doc.read(value)?.as_deref() {
Some(Object::String(s)) => Some(s.bytes.clone()),
Some(Object::Name(n)) => Some(n.as_bytes().to_vec()),
_ => None,
})
}
/// The file a file specification (§7.11) names: a string, or a dictionary's `/UF`, else `/F`.
fn file_name(doc: &Document, value: Option<&Object>) -> Result<Option<String>> {
let Some(value) = value else { return Ok(None) };
let Some(spec) = doc.read(value)? else { return Ok(None) };
let name = match &*spec {
Object::Dict(d) => file_spec_name(doc, d)?,
other => text_of(other),
};
Ok(name.filter(|n| !n.is_empty()))
}
/// `/QuadPoints`: groups of eight numbers, each the four corners of one area. Anything else
/// (a count not a multiple of eight, a value that isn't a number) gives none, so the link
/// falls back to its rectangle.
pub(crate) fn quads(doc: &Document, value: Option<&Object>) -> Result<Vec<[UserSpacePoint; 4]>> {
let Some(value) = value else { return Ok(Vec::new()) };
let Some(array) = doc.read(value)? else {
return Ok(Vec::new());
};
let Some(items) = array.as_array() else {
return Ok(Vec::new());
};
if items.is_empty() || items.len() % 8 != 0 {
return Ok(Vec::new());
}
let mut values = Vec::with_capacity(items.len());
for item in items {
match number(doc, item)? {
Some(v) => values.push(v),
None => return Ok(Vec::new()),
}
}
let (groups, _) = values.as_chunks::<8>();
Ok(groups
.iter()
.map(|&[x0, y0, x1, y1, x2, y2, x3, y3]| {
[
UserSpacePoint::new(x0, y0),
UserSpacePoint::new(x1, y1),
UserSpacePoint::new(x2, y2),
UserSpacePoint::new(x3, y3),
]
})
.collect())
}