stet-pdf-reader 0.7.0

PDF parser and renderer
Documentation
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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
// stet-pdf-reader
// Copyright (c) 2026 Scott Bowman
// SPDX-License-Identifier: Apache-2.0 OR MIT

//! Optional Content Group hierarchy and configurations.
//!
//! Defined in ISO 32000-2 §8.11.4. Each PDF carries one default
//! configuration (`/OCProperties /D`) and an optional array of
//! alternate configurations (`/OCProperties /Configs`); a configuration
//! says which layers are initially on/off, how they're presented in a
//! UI tree (`/Order`), how their visibility is grouped (`/RBGroups`),
//! and which layers cannot be toggled by the user (`/Locked`).
//!
//! This module turns each configuration dict into a typed
//! [`Configuration`] and exposes the hierarchy as a [`LayerTree`].

use crate::diagnostics::{ParsePhase, Severity, WarningSink};
use crate::metadata::pdf_string_to_rust_pub;
use crate::objects::{PdfDict, PdfObj};
use crate::resolver::Resolver;

use super::metadata::LayerIntent;

/// A presentation hierarchy for layers, parsed from `/Order`.
///
/// Empty when the configuration has no `/Order` entry — UI consumers
/// should fall back to a flat list of [`super::Layer`] in document
/// order.
#[derive(Debug, Clone, Default)]
pub struct LayerTree {
    /// Top-level nodes, in display order.
    pub nodes: Vec<LayerTreeNode>,
}

/// One node in the layer tree.
#[derive(Debug, Clone)]
#[non_exhaustive]
pub enum LayerTreeNode {
    /// A leaf referencing a single layer by OCG object number.
    Layer(u32),
    /// A labelled section. The label and header layer are both
    /// optional:
    ///
    /// - `label = Some(_), header_layer = None` — anonymous section
    ///   with a string-literal heading (e.g. `(Backgrounds)` followed
    ///   by a child array).
    /// - `header_layer = Some(_), label = None` — section whose
    ///   heading is the layer immediately preceding a child array.
    /// - both `None` — anonymous section (a bare nested array).
    Section {
        label: Option<String>,
        header_layer: Option<u32>,
        children: Vec<LayerTreeNode>,
    },
}

/// One configuration of layer state and presentation.
///
/// `index = 0` is the default configuration (`/OCProperties /D`);
/// indices 1..N correspond to entries 0..N-1 in `/OCProperties /Configs`.
#[derive(Debug, Clone)]
pub struct Configuration {
    /// 0 = default `/D`; 1..N = `/Configs[i-1]`.
    pub index: usize,
    /// `/Name` — display label for this configuration.
    pub name: Option<String>,
    /// `/Creator` — application that authored this configuration.
    pub creator: Option<String>,
    /// `/BaseState` — starting visibility for every layer before
    /// `/ON` and `/OFF` overrides apply.
    pub base_state: BaseState,
    /// `/ON` — layers explicitly turned on.
    pub on: Vec<u32>,
    /// `/OFF` — layers explicitly turned off.
    pub off: Vec<u32>,
    /// `/Intent` — author's hint about the audiences this
    /// configuration is meant for.
    pub intent: LayerIntent,
    /// `/AS` — automatic-state rules that re-apply `/Usage` hints
    /// under render intents.
    pub auto_state: Vec<AutoStateRule>,
    /// `/Order` — display hierarchy.
    pub order: LayerTree,
    /// `/ListMode` — whether the layer panel should show all pages or
    /// only the visible page's layers.
    pub list_mode: ListMode,
    /// `/RBGroups` — radio-button groups: turning one layer on in a
    /// group implies turning the others in that group off.
    pub rb_groups: Vec<Vec<u32>>,
    /// `/Locked` — layers the user is not allowed to toggle from a
    /// layer panel.
    pub locked: Vec<u32>,
}

/// `/BaseState` value.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum BaseState {
    /// `/ON` — all layers visible until `/OFF` flips them.
    On,
    /// `/OFF` — all layers hidden until `/ON` flips them.
    Off,
    /// `/Unchanged` — preserve the current visibility from a previous
    /// configuration. Only meaningful for alternate configurations.
    Unchanged,
}

impl BaseState {
    fn from_name(name: &[u8]) -> Option<Self> {
        match name {
            b"ON" => Some(BaseState::On),
            b"OFF" => Some(BaseState::Off),
            b"Unchanged" => Some(BaseState::Unchanged),
            _ => None,
        }
    }
}

/// `/ListMode` — layer-panel visibility scope.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
#[non_exhaustive]
pub enum ListMode {
    /// `/AllPages` — show every layer regardless of which pages use
    /// it. Default.
    #[default]
    AllPages,
    /// `/VisiblePages` — show only layers that appear on the
    /// currently displayed page.
    VisiblePages,
}

impl ListMode {
    fn from_name(name: &[u8]) -> Option<Self> {
        match name {
            b"AllPages" => Some(ListMode::AllPages),
            b"VisiblePages" => Some(ListMode::VisiblePages),
            _ => None,
        }
    }
}

/// One `/AS` automatic-state rule.
#[derive(Debug, Clone)]
pub struct AutoStateRule {
    /// `/Event` — render intent the rule applies to.
    pub event: AutoStateEvent,
    /// `/Category` — `/Usage` sub-dict names this rule consults.
    pub categories: Vec<String>,
    /// `/OCGs` — layers this rule applies to.
    pub ocgs: Vec<u32>,
}

/// `/Event` value on an `/AS` rule.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum AutoStateEvent {
    View,
    Print,
    Export,
}

impl AutoStateEvent {
    fn from_name(name: &[u8]) -> Option<Self> {
        match name {
            b"View" => Some(AutoStateEvent::View),
            b"Print" => Some(AutoStateEvent::Print),
            b"Export" => Some(AutoStateEvent::Export),
            _ => None,
        }
    }
}

/// Walk `/OCProperties` and produce one [`Configuration`] for the
/// default `/D` plus one for each entry in `/Configs`.
///
/// Returns an empty `Vec` when the document has no `/OCProperties`.
/// The default configuration is always at index 0; alternate configs
/// follow at indices 1..N preserving the order in `/Configs`.
pub fn parse_configurations(resolver: &Resolver, sink: &WarningSink) -> Vec<Configuration> {
    let Some(catalog) = catalog_dict(resolver) else {
        return Vec::new();
    };
    let Some(oc_props_obj) = catalog.get(b"OCProperties") else {
        return Vec::new();
    };
    let Ok(oc_props) = resolver.deref(oc_props_obj) else {
        return Vec::new();
    };
    let Some(oc_dict) = oc_props.as_dict() else {
        return Vec::new();
    };

    let mut configs = Vec::new();

    // /D — default configuration. Required when /OCProperties exists.
    if let Some(d_obj) = oc_dict.get(b"D")
        && let Ok(d_resolved) = resolver.deref(d_obj)
        && let Some(d_dict) = d_resolved.as_dict()
    {
        configs.push(parse_configuration(resolver, d_dict, 0, sink));
    } else {
        sink.record(
            ParsePhase::Layers,
            None,
            Severity::Warning,
            "/OCProperties missing required /D configuration; skipped",
        );
    }

    // /Configs — alternate configurations.
    if let Some(configs_obj) = oc_dict.get(b"Configs")
        && let Ok(configs_resolved) = resolver.deref(configs_obj)
        && let Some(arr) = configs_resolved.as_array()
    {
        for (i, entry) in arr.iter().enumerate() {
            let Ok(resolved) = resolver.deref(entry) else {
                sink.record(
                    ParsePhase::Layers,
                    None,
                    Severity::Warning,
                    format!("/Configs[{i}] could not be resolved; skipped"),
                );
                continue;
            };
            let Some(dict) = resolved.as_dict() else {
                sink.record(
                    ParsePhase::Layers,
                    None,
                    Severity::Warning,
                    format!("/Configs[{i}] is not a dict; skipped"),
                );
                continue;
            };
            configs.push(parse_configuration(resolver, dict, i + 1, sink));
        }
    }

    configs
}

/// Parse a single configuration dict (the `/D` dict or one
/// `/Configs[i]`).
pub fn parse_configuration(
    resolver: &Resolver,
    dict: &PdfDict,
    index: usize,
    sink: &WarningSink,
) -> Configuration {
    let name = dict.get(b"Name").and_then(pdf_string_to_rust_pub);
    let creator = dict.get(b"Creator").and_then(pdf_string_to_rust_pub);

    let base_state = dict
        .get_name(b"BaseState")
        .and_then(BaseState::from_name)
        .unwrap_or(BaseState::On);

    let on = collect_ocg_refs(resolver, dict, b"ON");
    let off = collect_ocg_refs(resolver, dict, b"OFF");
    let locked = collect_ocg_refs(resolver, dict, b"Locked");

    let intent = dict
        .get(b"Intent")
        .map(|obj| LayerIntent::from_obj(resolver, obj))
        .unwrap_or(LayerIntent::View);

    let order = dict
        .get(b"Order")
        .and_then(|obj| resolver.deref(obj).ok())
        .map(|resolved| parse_order(resolver, &resolved, sink))
        .unwrap_or_default();

    let list_mode = dict
        .get_name(b"ListMode")
        .and_then(ListMode::from_name)
        .unwrap_or_default();

    let auto_state = dict
        .get(b"AS")
        .and_then(|obj| resolver.deref(obj).ok())
        .map(|resolved| parse_auto_state(resolver, &resolved, sink))
        .unwrap_or_default();

    let rb_groups = dict
        .get(b"RBGroups")
        .and_then(|obj| resolver.deref(obj).ok())
        .map(|resolved| parse_rb_groups(resolver, &resolved))
        .unwrap_or_default();

    Configuration {
        index,
        name,
        creator,
        base_state,
        on,
        off,
        intent,
        auto_state,
        order,
        list_mode,
        rb_groups,
        locked,
    }
}

/// Parse `/Order` according to ISO 32000-2 §8.11.4.3.
///
/// The array is walked left-to-right with one element of look-back
/// state to classify each item:
///
/// - **OCG ref** — leaf [`LayerTreeNode::Layer`].
/// - **OCG ref immediately followed by a nested array** — the array
///   is consumed as that layer's section (header-layer section).
/// - **String literal followed by a nested array** — labelled section.
/// - **Bare nested array** — anonymous section.
/// - **String not followed by an array** — dropped with a warning.
pub fn parse_order(resolver: &Resolver, obj: &PdfObj, sink: &WarningSink) -> LayerTree {
    let Some(arr) = obj.as_array() else {
        return LayerTree::default();
    };
    LayerTree {
        nodes: parse_order_nodes(resolver, arr, sink),
    }
}

fn parse_order_nodes(
    resolver: &Resolver,
    items: &[PdfObj],
    sink: &WarningSink,
) -> Vec<LayerTreeNode> {
    let mut nodes = Vec::new();
    let mut i = 0;
    while i < items.len() {
        let item = &items[i];

        // Resolve refs to inspect type, but keep the original ref's
        // (num, gen) for layer leaves.
        let resolved = resolver.deref(item).ok();
        let view = resolved.as_ref().unwrap_or(item);

        match view {
            // String literal: labelled section. The next item must
            // be a nested array; if it isn't, drop the string.
            PdfObj::Str(s) => {
                let label = crate::metadata::decode_pdf_text_string_pub(s);
                if let Some(next) = items.get(i + 1) {
                    let next_resolved = resolver.deref(next).ok();
                    let next_view = next_resolved.as_ref().unwrap_or(next);
                    if let PdfObj::Array(children_arr) = next_view {
                        let children = parse_order_nodes(resolver, children_arr, sink);
                        nodes.push(LayerTreeNode::Section {
                            label: Some(label),
                            header_layer: None,
                            children,
                        });
                        i += 2;
                        continue;
                    }
                }
                sink.record(
                    ParsePhase::Layers,
                    None,
                    Severity::Warning,
                    format!("/Order string label {label:?} not followed by a child array; dropped"),
                );
                i += 1;
            }

            // Bare nested array: anonymous section.
            PdfObj::Array(children_arr) => {
                let children = parse_order_nodes(resolver, children_arr, sink);
                nodes.push(LayerTreeNode::Section {
                    label: None,
                    header_layer: None,
                    children,
                });
                i += 1;
            }

            // OCG dict (we resolved a ref to a dict). Decide whether
            // the *next* item is a nested array, in which case this
            // layer is a section header.
            PdfObj::Dict(_) => {
                let Some((ocg_id, _gen)) = item.as_ref() else {
                    sink.record(
                        ParsePhase::Layers,
                        None,
                        Severity::Warning,
                        "/Order layer entry is an inline OCG dict (not a ref); skipped",
                    );
                    i += 1;
                    continue;
                };
                if let Some(next) = items.get(i + 1) {
                    let next_resolved = resolver.deref(next).ok();
                    let next_view = next_resolved.as_ref().unwrap_or(next);
                    if let PdfObj::Array(children_arr) = next_view {
                        let children = parse_order_nodes(resolver, children_arr, sink);
                        nodes.push(LayerTreeNode::Section {
                            label: None,
                            header_layer: Some(ocg_id),
                            children,
                        });
                        i += 2;
                        continue;
                    }
                }
                nodes.push(LayerTreeNode::Layer(ocg_id));
                i += 1;
            }

            _ => {
                sink.record(
                    ParsePhase::Layers,
                    None,
                    Severity::Warning,
                    "/Order item is neither layer ref, string, nor array; skipped",
                );
                i += 1;
            }
        }
    }
    nodes
}

/// Parse `/AS` — an array of rule dicts.
pub fn parse_auto_state(
    resolver: &Resolver,
    obj: &PdfObj,
    sink: &WarningSink,
) -> Vec<AutoStateRule> {
    let Some(arr) = obj.as_array() else {
        return Vec::new();
    };
    let mut rules = Vec::with_capacity(arr.len());
    for entry in arr {
        let Ok(resolved) = resolver.deref(entry) else {
            continue;
        };
        let Some(dict) = resolved.as_dict() else {
            continue;
        };
        let Some(event) = dict.get_name(b"Event").and_then(AutoStateEvent::from_name) else {
            sink.record(
                ParsePhase::Layers,
                None,
                Severity::Warning,
                "/AS rule missing or unknown /Event; skipped",
            );
            continue;
        };

        let categories = dict
            .get_array(b"Category")
            .map(|arr| {
                arr.iter()
                    .filter_map(|o| o.as_name())
                    .map(|n| String::from_utf8_lossy(n).into_owned())
                    .collect()
            })
            .unwrap_or_default();

        let ocgs = dict
            .get(b"OCGs")
            .and_then(|obj| resolver.deref(obj).ok())
            .and_then(|resolved| resolved.as_array().map(<[PdfObj]>::to_vec))
            .map(|arr| {
                arr.iter()
                    .filter_map(|o| o.as_ref().map(|(n, _)| n))
                    .collect()
            })
            .unwrap_or_default();

        rules.push(AutoStateRule {
            event,
            categories,
            ocgs,
        });
    }
    rules
}

/// Parse `/RBGroups` — an array of nested arrays, each containing
/// OCG refs.
pub fn parse_rb_groups(resolver: &Resolver, obj: &PdfObj) -> Vec<Vec<u32>> {
    let Some(arr) = obj.as_array() else {
        return Vec::new();
    };
    let mut groups = Vec::with_capacity(arr.len());
    for entry in arr {
        let Ok(resolved) = resolver.deref(entry) else {
            continue;
        };
        let Some(group_arr) = resolved.as_array() else {
            continue;
        };
        let group: Vec<u32> = group_arr
            .iter()
            .filter_map(|o| o.as_ref().map(|(n, _)| n))
            .collect();
        if !group.is_empty() {
            groups.push(group);
        }
    }
    groups
}

/// Read `<key>` on a configuration dict as a flat list of OCG object
/// numbers. The value can be an indirect-referenced array.
fn collect_ocg_refs(resolver: &Resolver, dict: &PdfDict, key: &[u8]) -> Vec<u32> {
    let Some(obj) = dict.get(key) else {
        return Vec::new();
    };
    let Ok(resolved) = resolver.deref(obj) else {
        return Vec::new();
    };
    let Some(arr) = resolved.as_array() else {
        return Vec::new();
    };
    arr.iter()
        .filter_map(|o| o.as_ref().map(|(n, _)| n))
        .collect()
}

fn catalog_dict(resolver: &Resolver) -> Option<PdfDict> {
    if let Some((num, gen_num)) = resolver.trailer().get_ref(b"Root")
        && let Ok(obj) = resolver.resolve(num, gen_num)
        && let Some(dict) = obj.as_dict()
    {
        return Some(dict.clone());
    }
    crate::find_catalog(resolver).and_then(|obj| obj.as_dict().cloned())
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn base_state_names() {
        assert_eq!(BaseState::from_name(b"ON"), Some(BaseState::On));
        assert_eq!(BaseState::from_name(b"OFF"), Some(BaseState::Off));
        assert_eq!(
            BaseState::from_name(b"Unchanged"),
            Some(BaseState::Unchanged)
        );
        assert_eq!(BaseState::from_name(b"on"), None);
    }

    #[test]
    fn list_mode_names() {
        assert_eq!(ListMode::from_name(b"AllPages"), Some(ListMode::AllPages));
        assert_eq!(
            ListMode::from_name(b"VisiblePages"),
            Some(ListMode::VisiblePages)
        );
        assert_eq!(ListMode::from_name(b"Other"), None);
    }

    #[test]
    fn auto_state_event_names() {
        assert_eq!(
            AutoStateEvent::from_name(b"View"),
            Some(AutoStateEvent::View)
        );
        assert_eq!(
            AutoStateEvent::from_name(b"Print"),
            Some(AutoStateEvent::Print)
        );
        assert_eq!(
            AutoStateEvent::from_name(b"Export"),
            Some(AutoStateEvent::Export)
        );
        assert_eq!(AutoStateEvent::from_name(b"Save"), None);
    }
}