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
//! The caches one run of many pages reuses.
use BuildContext;
use RenderCaches;
/// Everything a run of many pages can reuse between them: the resources a
/// page is *built* from, and the glyph outlines it is *drawn* with.
///
/// Two caches, both public: [`BuildContext`] holds fonts, colour spaces,
/// functions and decoded images; [`RenderCaches`] holds the flattened glyph
/// outlines the rasterizer draws.
///
/// Reached through [`Page::render_on`](crate::Page::render_on) and
/// [`Page::text_on`](crate::Page::text_on); extraction moves only the `build`
/// half, so one session serves a run that does both. It is used through
/// `&mut`, so under `rayon` each worker keeps its own:
/// `pages.par_iter().map_init(RenderSession::new, |session, page| …)`.
///
/// ```
/// use pdfrum::{Document, RenderOptions, RenderSession, VelloCpuBackend};
///
/// let doc = Document::open("tests/fixtures/bookmarks.pdf")?;
/// let backend = VelloCpuBackend::new();
/// let mut session = RenderSession::new();
///
/// // Both pages share one set of caches: the fonts are parsed once, and so
/// // are the glyph outlines drawn from them.
/// for page in doc.pages() {
/// let pixmap = page.render_on(&backend, &RenderOptions::default(), &mut session)?;
/// assert!(pixmap.width() > 0);
/// }
/// # Ok::<(), pdfrum::Error>(())
/// ```
///
/// # When not to use it
///
/// Type-3 glyph snapping is order-dependent by design, so a page drawn with a
/// warm cache can differ by a snapped pixel from the same page drawn cold.
/// For a byte-identical per-page baseline call
/// [`Page::render`](crate::Page::render), which gives every page fresh caches.