laminapdf_render/backend.rs
1//! The two traits every renderer implements (ADR 0004 §1).
2
3use std::sync::Arc;
4
5use laminapdf_cos::ByteSource;
6use laminapdf_syntax::CancelToken;
7
8use crate::error::RenderError;
9use crate::request::{RenderLimits, RenderOpenOptions, RenderPage, RenderRequest, TilePlan};
10
11/// A rendering engine: opens documents. Object-safe, so shells hold `Box<dyn RenderBackend>`.
12///
13/// A renderer only produces pixels: text, glyph quads, structure, annotation and form
14/// geometry, links and outlines always come from Lamina PDF's model, never from the renderer
15/// (ADR 0004 §1).
16///
17/// Added in 0.2.0.
18pub trait RenderBackend: Send {
19 /// `"pdfium"` or `"hayro"`; with [`RenderBackend::version`], part of every golden's key.
20 ///
21 /// Added in 0.2.0.
22 fn name(&self) -> &'static str;
23
24 /// The engine's version (a crate version or a PDFium build tag).
25 ///
26 /// Added in 0.2.0.
27 fn version(&self) -> &'static str;
28
29 /// Opens a document from `source` (the same source Lamina PDF reads, never a second copy).
30 ///
31 /// # Errors
32 /// `password-required`, `wrong-password`, `data-pending` (with the ranges to fetch),
33 /// `source`, `render-failed` (the engine can't open the file),
34 /// `render-backend-unavailable`.
35 ///
36 /// Added in 0.2.0.
37 fn open(
38 &self,
39 source: Arc<dyn ByteSource>,
40 options: &RenderOpenOptions,
41 ) -> Result<Box<dyn RenderDocument>, RenderError>;
42}
43
44/// One open document. `Send` but not `Sync`: one call at a time on a document. A backend
45/// whose engine isn't thread-safe (PDFium) also serialises calls across its documents.
46///
47/// Added in 0.2.0.
48pub trait RenderDocument: Send {
49 /// The number of pages.
50 ///
51 /// Added in 0.2.0.
52 fn page_count(&self) -> u32;
53
54 /// Page `index`'s displayed size and `/Rotate` (ADR 0027: 0-based).
55 ///
56 /// # Errors
57 /// `page-index-out-of-range`, `data-pending`, `render-failed`.
58 ///
59 /// Added in 0.2.0.
60 fn page(&self, index: u32) -> Result<RenderPage, RenderError>;
61
62 /// The limits this document was opened with.
63 ///
64 /// Added in 0.2.0.
65 fn limits(&self) -> &RenderLimits;
66
67 /// Validates `request` for this document: the tile geometry and the bytes it needs.
68 ///
69 /// # Errors
70 /// As [`RenderDocument::page`], plus [`TilePlan::new`]'s.
71 ///
72 /// Added in 0.2.0.
73 fn plan(&self, request: &RenderRequest) -> Result<TilePlan, RenderError> {
74 TilePlan::new(&self.page(request.page)?, request, self.limits())
75 }
76
77 /// Renders `request` into `out`, which must hold exactly [`TilePlan::byte_len`] bytes, in
78 /// straight-alpha RGBA8. Checks `cancel` before and after the engine call (a running
79 /// single-threaded render can't see a cancel, ADR 0004 §1).
80 ///
81 /// # Errors
82 /// As [`RenderDocument::plan`], plus `invalid-argument` for a wrong buffer size,
83 /// `render-failed` and `cancelled`.
84 ///
85 /// Added in 0.2.0.
86 fn render(&self, request: &RenderRequest, out: &mut [u8], cancel: &CancelToken) -> Result<TilePlan, RenderError>;
87}