Skip to main content

byob_docs/
pdf.rs

1//! PDF: text per page and pages as PNG, on hayro.
2//!
3//! Text comes from a [`Device`] that collects every glyph the page draws (invisible text
4//! too, which is how scanned PDFs carry their OCR layer) with its position, then groups
5//! glyphs into lines by baseline and puts spaces where the gap is wider than a glyph would
6//! leave. That is rough on multi-column layouts but needs no layout analysis.
7
8use std::sync::Arc;
9
10use hayro::hayro_interpret::font::GlyphRun;
11use hayro::hayro_interpret::hayro_syntax::Pdf;
12use hayro::hayro_interpret::hayro_syntax::page::Page;
13use hayro::hayro_interpret::kurbo::{Affine, BezPath, Point, Rect};
14use hayro::hayro_interpret::util::TransformExt;
15use hayro::hayro_interpret::{
16    BlendMode, ClipPath, Context, Device, DrawMode, DrawProps, Image, ImageDrawProps, InterpreterCache, InterpreterSettings, SoftMask, interpret_page,
17};
18use hayro::vello_cpu::color::palette::css::WHITE;
19use hayro::{PixmapSettings, RenderCache, RenderSettings};
20
21use crate::{MAX_FILE_BYTES, guarded};
22
23/// Most characters kept from one page (a page of dense text is ~5k).
24pub const MAX_PAGE_CHARS: usize = 40_000;
25/// Most glyphs looked at on one page (a hostile page could draw millions).
26const MAX_GLYPHS: usize = 200_000;
27/// Rendered pages are at most this many pixels on a side before downscaling.
28const MAX_RENDER_SIDE: f32 = 4096.0;
29
30/// An opened PDF.
31pub struct Document {
32    pdf: Pdf,
33}
34
35/// What one page holds.
36#[derive(Debug, Clone, Default, PartialEq)]
37pub struct PageText {
38    pub text: String,
39    /// Pictures drawn on the page (scans, photos, embedded charts).
40    pub images: usize,
41    /// Vector shapes drawn (lines, bars of a chart, table rules).
42    pub shapes: usize,
43    /// The text was cut at [`MAX_PAGE_CHARS`].
44    pub cut: bool,
45}
46
47impl Document {
48    /// Open PDF bytes. Encrypted PDFs that need a password are refused.
49    pub fn open(bytes: Vec<u8>) -> Result<Self, String> {
50        if bytes.len() as u64 > MAX_FILE_BYTES {
51            return Err(format!("the PDF is too big (over {} MB)", MAX_FILE_BYTES / (1024 * 1024)));
52        }
53        guarded("the PDF", || match Pdf::new(Arc::new(bytes)) {
54            Ok(pdf) => Ok(Self { pdf }),
55            Err(hayro::hayro_interpret::hayro_syntax::LoadPdfError::Decryption(_)) => Err("the PDF is password-protected".into()),
56            Err(_) => Err("the PDF is damaged or not really a PDF".into()),
57        })
58    }
59
60    pub fn page_count(&self) -> usize {
61        self.pdf.pages().len()
62    }
63
64    /// The text of page `n` (from 1).
65    pub fn page_text(&self, n: usize) -> Result<PageText, String> {
66        let page = self.page(n)?;
67        guarded(&format!("page {n}"), || {
68            let cache = InterpreterCache::new();
69            let settings = InterpreterSettings::default();
70            let (w, h) = page.render_dimensions();
71            let bbox = Rect::new(0.0, 0.0, f64::from(w), f64::from(h));
72            let mut ctx = Context::new(page.initial_transform(true).to_kurbo(), bbox, &cache, page.xref(), settings);
73            let mut dev = TextDevice::default();
74            interpret_page(page, &mut ctx, &mut dev);
75            Ok(dev.finish())
76        })
77    }
78
79    /// Page `n` (from 1) as a PNG whose longer side is about `max_side` pixels, on white.
80    pub fn render_png(&self, n: usize, max_side: u32) -> Result<Vec<u8>, String> {
81        let page = self.page(n)?;
82        guarded(&format!("page {n}"), || {
83            let (w, h) = page.render_dimensions();
84            let long = w.max(h);
85            if !(long.is_finite() && long > 0.0) {
86                return Err(format!("page {n} has no size"));
87            }
88            #[allow(clippy::cast_precision_loss)]
89            let scale = ((max_side as f32).min(MAX_RENDER_SIDE) / long).clamp(0.01, 10.0);
90            let cache = RenderCache::new();
91            let pixmap = hayro::render(
92                page,
93                &cache,
94                &InterpreterSettings::default(),
95                &RenderSettings::default(),
96                &PixmapSettings { x_scale: scale, y_scale: scale, bg_color: WHITE },
97            );
98            if pixmap.width() == 0 || pixmap.height() == 0 {
99                return Err(format!("page {n} rendered empty"));
100            }
101            pixmap.into_png().map_err(|e| format!("can't encode page {n}: {e}"))
102        })
103    }
104
105    fn page(&self, n: usize) -> Result<&Page<'_>, String> {
106        let total = self.page_count();
107        n.checked_sub(1).and_then(|i| self.pdf.pages().get(i)).ok_or_else(|| format!("there is no page {n}: the PDF has {total}"))
108    }
109}
110
111/// One glyph as placed on the page (y grows downwards).
112#[derive(Debug, Clone)]
113struct Placed {
114    x: f64,
115    /// Baseline.
116    y: f64,
117    /// Where the glyph ends (estimated when the font doesn't say).
118    end: f64,
119    /// Font size in page units.
120    size: f64,
121    text: String,
122}
123
124#[derive(Default)]
125struct TextDevice {
126    glyphs: Vec<Placed>,
127    images: usize,
128    shapes: usize,
129    seen: usize,
130}
131
132impl TextDevice {
133    fn glyph_run(&mut self, run: &GlyphRun<'_, '_>, props: &DrawProps<'_>) {
134        for g in run.glyphs() {
135            self.seen = self.seen.saturating_add(1);
136            if self.seen > MAX_GLYPHS {
137                return;
138            }
139            let Some(text) = g.as_unicode().map(|u| match u {
140                hayro::hayro_interpret::hayro_cmap::BfString::Char(c) => c.to_string(),
141                hayro::hayro_interpret::hayro_cmap::BfString::String(s) => s,
142            }) else {
143                continue;
144            };
145            // Glyph space is 1000 units per em.
146            let t: Affine = props.transform * g.transform();
147            let origin = t * Point::ZERO;
148            let up = t * Point::new(0.0, 1000.0);
149            let size = (up - origin).hypot();
150            if !(origin.x.is_finite() && origin.y.is_finite() && size.is_finite()) || size <= 0.0 {
151                continue;
152            }
153            let advance = match &**g {
154                hayro::hayro_interpret::font::Glyph::Outline(o) => o.advance_width(),
155                hayro::hayro_interpret::font::Glyph::Type3(t3) => t3.advance_width(),
156            };
157            let end = match advance.filter(|a| a.is_finite() && *a > 0.0) {
158                Some(a) => (t * Point::new(f64::from(a), 0.0)).x,
159                None => origin.x + size * 0.5,
160            };
161            self.glyphs.push(Placed { x: origin.x, y: origin.y, end, size, text });
162        }
163    }
164
165    /// Group glyphs into lines, top to bottom, and lines into text.
166    fn finish(mut self) -> PageText {
167        // Content order is reading order in most PDFs; sorting by line keeps that inside a line
168        // and puts lines in page order. Glyphs within a line are ordered by x.
169        let mut lines: Vec<(f64, f64, Vec<Placed>)> = Vec::new(); // (baseline, size, glyphs)
170        for g in self.glyphs.drain(..) {
171            let tolerance = g.size * 0.4;
172            match lines.iter_mut().rev().take(8).find(|(y, _, _)| (y - g.y).abs() <= tolerance) {
173                Some((_, size, glyphs)) => {
174                    *size = size.max(g.size);
175                    glyphs.push(g);
176                }
177                None => lines.push((g.y, g.size, vec![g])),
178            }
179        }
180        lines.sort_by(|a, b| a.0.total_cmp(&b.0));
181        let mut text = String::new();
182        let mut cut = false;
183        let mut prev: Option<(f64, f64)> = None;
184        for (y, size, mut glyphs) in lines {
185            glyphs.sort_by(|a, b| a.x.total_cmp(&b.x));
186            let line = join_line(&glyphs);
187            if line.trim().is_empty() {
188                continue;
189            }
190            if let Some((py, psize)) = prev {
191                text.push('\n');
192                // A gap of more than about a line and a half: a new paragraph.
193                if y - py > psize.max(size) * 1.9 {
194                    text.push('\n');
195                }
196            }
197            text.push_str(line.trim_end());
198            prev = Some((y, size));
199            if text.chars().count() > MAX_PAGE_CHARS {
200                text = text.chars().take(MAX_PAGE_CHARS).collect();
201                cut = true;
202                break;
203            }
204        }
205        PageText { text, images: self.images, shapes: self.shapes, cut }
206    }
207}
208
209/// The glyphs of one line (sorted by x) as text, with spaces at wide gaps.
210fn join_line(glyphs: &[Placed]) -> String {
211    let mut out = String::new();
212    let mut last_end: Option<f64> = None;
213    for g in glyphs {
214        if let Some(end) = last_end {
215            let gap = g.x - end;
216            // Overprinted glyphs (fake bold, shadows) repeat at the same place.
217            if gap < -g.size * 0.3 && out.ends_with(&g.text) {
218                continue;
219            }
220            if gap > g.size * 0.18 && !out.ends_with(' ') && g.text != " " {
221                // Very wide gaps are columns of a table or form.
222                out.push_str(if gap > g.size * 2.5 { "   " } else { " " });
223            }
224        }
225        if !(g.text == " " && out.ends_with(' ')) {
226            out.extend(g.text.chars().filter(|c| !c.is_control()));
227        }
228        last_end = Some(g.end.max(g.x));
229    }
230    out
231}
232
233impl<'a> Device<'a> for TextDevice {
234    fn draw_path(&mut self, _: &BezPath, _: DrawProps<'a>, _: &DrawMode) {
235        self.shapes = self.shapes.saturating_add(1);
236    }
237    fn push_clip_path(&mut self, _: &ClipPath) {}
238    fn push_transparency_group(&mut self, _: f32, _: Option<SoftMask<'a>>, _: BlendMode) {}
239    fn draw_glyph_run(&mut self, run: &GlyphRun<'_, 'a>, props: DrawProps<'a>, _: &DrawMode) {
240        self.glyph_run(run, &props);
241    }
242    fn draw_image(&mut self, _: Image<'a, '_>, _: ImageDrawProps<'a>) {
243        self.images = self.images.saturating_add(1);
244    }
245    fn pop_clip(&mut self) {}
246    fn pop_transparency_group(&mut self) {}
247}
248
249#[cfg(test)]
250mod tests {
251    use super::*;
252    use crate::fixtures::make_pdf;
253
254    #[test]
255    fn text_per_page_in_reading_order() {
256        let pdf = make_pdf(
257            &[
258                &[(72, 720, 20, "Quarterly report"), (72, 690, 11, "Sales grew by 12 percent."), (300, 690, 11, "See page 2.")],
259                &[(72, 720, 14, "Chart: revenue by region")],
260            ],
261            3,
262        );
263        let doc = Document::open(pdf).unwrap();
264        assert_eq!(doc.page_count(), 2);
265        let p1 = doc.page_text(1).unwrap();
266        assert_eq!(p1.text, "Quarterly report\nSales grew by 12 percent.   See page 2.");
267        assert_eq!((p1.images, p1.shapes, p1.cut), (0, 0, false));
268        let p2 = doc.page_text(2).unwrap();
269        assert_eq!(p2.text, "Chart: revenue by region");
270        assert_eq!(p2.shapes, 3);
271        assert!(doc.page_text(3).unwrap_err().contains("has 2"));
272        assert!(doc.page_text(0).is_err());
273    }
274
275    #[test]
276    fn renders_a_page_to_png() {
277        let doc = Document::open(make_pdf(&[&[(72, 720, 20, "Hello")]], 2)).unwrap();
278        let png = doc.render_png(1, 600).unwrap();
279        assert!(png.starts_with(b"\x89PNG"));
280        // 612x792 points scaled to a 600 px long side.
281        let h = u32::from_be_bytes(png[20..24].try_into().unwrap());
282        assert!((598..=601).contains(&h), "{h}");
283    }
284
285    #[test]
286    fn hostile_input_is_an_error_not_a_panic() {
287        assert!(Document::open(b"%PDF-1.4\ngarbage".to_vec()).is_err());
288        assert!(Document::open(Vec::new()).is_err());
289        let mut pdf = make_pdf(&[&[(72, 720, 12, "x")]], 0);
290        // Truncate in the middle: the xref is gone, hayro falls back to scanning.
291        pdf.truncate(pdf.len() / 2);
292        if let Ok(doc) = Document::open(pdf) {
293            for n in 1..=doc.page_count() {
294                let _ = doc.page_text(n);
295                let _ = doc.render_png(n, 100);
296            }
297        }
298    }
299}