Skip to main content

pdfrum_edit/
build_graph.rs

1//! Interpreting a page's content stream into an object graph.
2//!
3//! Read through any `Resolve`: the base document for the page as it was
4//! opened, or an editing session's overlay for the page as that session's
5//! edits leave it, so a stream an earlier edit appended is a clean stream of
6//! the graph and the names it uses are kept.
7
8use pdfrum_common::{Diagnostics, Limits};
9use pdfrum_object::{Name, Resolve};
10use pdfrum_page::BuildContext;
11use pdfrum_parser::{PageDict, content_segments};
12
13/// The object graph of `dict`'s page for editing, read through `r`: the
14/// base document for a page as it was opened, or an editing session's
15/// overlay for the page as that session's edits leave it — so a stream an
16/// earlier edit appended is a clean stream of the graph, and the names it
17/// uses are kept.
18pub fn build_graph(
19    dict: &PageDict,
20    r: &impl Resolve,
21    limits: &Limits,
22    ctx: &mut BuildContext,
23    diags: &mut Diagnostics,
24) -> pdfrum_page::Page {
25    let (bytes, ends) = content_segments(dict, r, limits, diags);
26    let ops = pdfrum_page::parse_content(&bytes, limits, diags);
27    let bounds = pdfrum_page::StreamBounds::from_joined(&bytes, ops.len(), &ends, limits);
28    let resources = pdfrum_page::Resources::for_page(
29        dict.inherited(&Name::from("Resources"), r)
30            .and_then(|object| object.resolve(r).ok()?.as_dict().cloned()),
31    );
32    pdfrum_page::build_page_streams(
33        &ops,
34        &bounds,
35        &dict.dict,
36        |key| dict.inherited(key, r),
37        &resources,
38        r,
39        ctx,
40        limits,
41        diags,
42    )
43}