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
//! # `ObjectGraph` — one read view over "the document as it is right now"
//!
//! Pass 3.1 could get away without this. Pass 3.2 cannot, and the reason
//! is recorded verbatim in `pdfcer_core::edit`'s own module docs:
//!
//! > ⚠️ Pass 3.2 must replace this with an overlay-aware walk. The moment
//! > an edit can add or remove a `Kids` entry, patching a base-derived
//! > list is not an approximation — it is **wrong**.
//!
//! Pass 3.1's mutations (`/Info`, `/Rotate`) changed dictionary *values*
//! only, so [`crate::page_tree::pages`] could walk the **base** document
//! and then patch each leaf's rotation from the overlay. A structural
//! operation changes the page tree's *shape*: after a delete, the base
//! walk still returns the deleted page; after a reorder, it returns the
//! old order. Patching cannot fix either.
//!
//! ## The shape of the fix, and why it is a trait
//!
//! Every consumer of the object graph — the page-tree walk, the outline
//! walk, the cross-document copier, the dangling-reference census —
//! needs exactly three primitives:
//!
//! 1. fetch an indirect object by identity;
//! 2. follow a reference chain to a non-reference value (§7.3.10);
//! 3. read a trailer entry (in practice only `/Root`).
//!
//! [`Document`](crate::document::Document) provides all three, and so
//! does an `EditSession` *with its overlay
//! applied*. Making that a trait rather than duplicating each walk means
//! there is exactly **one** page-tree walk in pdfcer, and it is
//! automatically correct for both views. A second copy specialised to
//! sessions is precisely the kind of drift that eventually renders a
//! deleted page.
//!
//! ## Why the resolution rules live here as provided methods
//!
//! §7.3.10's rules — dangling reference yields `null` and *"shall not be
//! considered an error"*, a generation mismatch is a dangling reference,
//! a reference chain is depth-guarded — are **spec behaviour, not view
//! behaviour**. Implementing them once as provided methods on the trait
//! makes it impossible for a new view to get them subtly different; an
//! implementor supplies only [`ObjectGraph::value`] and
//! [`ObjectGraph::trailer_entry`], which are the parts that genuinely
//! differ.
//!
//! ## Spec sources
//!
//! - `iso32000__s__7.3.10.md` — indirect objects, dangling references,
//! substitutability
//! - `iso32000__s__7.7.2.md` — the document catalog, reached via the
//! trailer's `/Root`
use crateMAX_RESOLVE_DEPTH;
use crate;
/// A read-only view of one PDF document's object graph.
///
/// Implemented by [`Document`](crate::document::Document) (the file as
/// loaded) and by `EditSession`'s overlay
/// view (the file as the operator currently has it). See the module
/// docs for why this exists at all.
///
/// # Examples
///
/// ```
/// use pdfcer_model::document::Document;
/// use pdfcer_model::graph::ObjectGraph;
///
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let doc = Document::from_bytes(
/// include_bytes!("../../../fixtures/synthetic/hello.pdf").to_vec(),
/// )?;
/// // The catalog is reached through the trailer's /Root, whichever
/// // view is doing the reaching.
/// assert!(doc.catalog_dict().is_some());
/// # Ok(())
/// # }
/// ```
///
/// # Thread safety — why this trait requires `Send + Sync`
///
/// **The bound exists so a page can be rasterized off the UI thread.**
/// A GUI that rasterizes inline freezes for the length of the render,
/// and on a real CAD sheet that is ~10 s at 1× and ~58 s at 2× — not a
/// slow redraw but a dead application, with no repaint, no progress and
/// no way to cancel. Moving that work to a worker requires the rendered
/// value to cross a thread boundary, and
/// [`DocumentView`](crate::view::DocumentView) is what crosses.
///
/// **Only the trait object was ever the obstacle.** `Document`,
/// `EditSession`, `SessionGraph`, `PendingGraph` and the test graphs
/// were all already thread-safe; `DocumentView<'a>` holds
/// `&'a dyn ObjectGraph`, and an unbounded `dyn Trait` is neither
/// `Send` nor `Sync` regardless of what implements it. Adding the
/// supertrait compiled with **zero errors** across the workspace — it
/// records a property every implementor already had rather than
/// imposing a new one.
///
/// **Why now.** Adding a supertrait bound to a public trait is a
/// breaking change for downstream implementors. pdfcer has no remote and
/// no release (`LEGAL.md` rule 8 — publishing awaits an explicit
/// go-ahead), so the set of affected implementors is exactly the ones
/// in this repository, and it is empty. This is the cheapest moment
/// this bound will ever have.
///
/// **The cost, stated.** Any future `ObjectGraph` implementor must be
/// thread-safe. That forecloses one holding an `Rc`, a `RefCell`, or a
/// non-thread-safe foreign handle. Rust API guideline **`C-SEND-SYNC`**
/// asks for types to be `Send`/`Sync` where possible and for deliberate
/// exceptions to be documented; an object graph is plain data behind an
/// accessor, so an implementor that could not meet this would be the
/// surprising one.
/// The loaded file, as a graph.
///
/// `Document` already has inherent `resolve`/`catalog` methods with the
/// same semantics; those keep working and keep winning method
/// resolution (inherent methods are preferred), so this impl adds a
/// generic entry point without changing a single existing call site.