exprim 0.4.1

A concurrent, memory-bounded PDF reader and renderer: parse, extract text, and rasterize pages on an immutable document snapshot
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
//! Diagnostic per-pixel attribution for differential rendering.
//!
//! This records the topmost prepared operation whose geometric coverage
//! reaches a pixel. It deliberately ignores paint alpha, blend modes, soft
//! masks, and knockout semantics: the result is a triage aid, not proof that
//! the attributed PDF object caused a visible difference.

use crate::page_ir::{DeviceRect, PaintLeaf, PaintOrigin};

use crate::render_cpu::kernels::mul_div_255;
use crate::render_cpu::mask::{ClipGeom, ClipMask, build_clip_mask};
use crate::render_cpu::prepared::{CpuPreparedPage, PreparedOp};
use crate::render_cpu::raster::RasterKernel;

/// Default minimum geometric coverage accepted as painted, in 8-bit alpha.
pub const DEFAULT_COVERAGE_THRESHOLD: u8 = 8;

/// Two canonical, row-major diagnostic planes.
#[derive(Debug, Clone)]
pub struct AttributionMap {
    pub width: u32,
    pub height: u32,
    /// `PaintLeaf as u8`; zero is `Unpainted`.
    pub leaf: Vec<u8>,
    /// `PaintOrigin as u8`; meaningful wherever `leaf != 0`.
    pub origin: Vec<u8>,
    pub coverage_threshold: u8,
}

/// Render attribution with [`DEFAULT_COVERAGE_THRESHOLD`].
pub fn render_attribution(page: &CpuPreparedPage) -> AttributionMap {
    render_attribution_with_threshold(page, DEFAULT_COVERAGE_THRESHOLD)
}

/// Render attribution using an explicit geometric-coverage threshold.
pub fn render_attribution_with_threshold(
    page: &CpuPreparedPage,
    coverage_threshold: u8,
) -> AttributionMap {
    let len = page.size.width as usize * page.size.height as usize;
    let mut map = AttributionMap {
        width: page.size.width,
        height: page.size.height,
        leaf: vec![PaintLeaf::Unpainted as u8; len],
        origin: vec![PaintOrigin::PageContent as u8; len],
        coverage_threshold,
    };
    let mut raster = RasterKernel::default();
    let mut clips: Vec<Option<ClipMask>> = (0..page.clips.len()).map(|_| None).collect();

    let mut i = 0usize;
    while i < page.ops.len() {
        match &page.ops[i] {
            PreparedOp::Draw(cmd) => {
                let leaf = if cmd.shading.is_some() {
                    PaintLeaf::Shading
                } else {
                    PaintLeaf::Path
                };
                stamp_path(
                    page,
                    &mut map,
                    &mut raster,
                    &mut clips,
                    cmd.subpath_range,
                    cmd.rule,
                    cmd.bounds,
                    cmd.clip.filter(|_| cmd.clip_has_mask),
                    leaf,
                    cmd.origin,
                );
                i += 1;
            }
            PreparedOp::TiledFill(t) => {
                stamp_path(
                    page,
                    &mut map,
                    &mut raster,
                    &mut clips,
                    t.fill_subpaths,
                    t.fill_rule,
                    t.bounds,
                    t.clip.filter(|_| t.clip_has_mask),
                    PaintLeaf::TilingPattern,
                    // The visible paint comes from replicated pattern-cell
                    // content; this is the innermost containing construct.
                    PaintOrigin::TilingPatternCell,
                );
                i += 1;
            }
            PreparedOp::GlyphRun(run) => {
                ensure_clip(
                    page,
                    &mut raster,
                    &mut clips,
                    run.clip.filter(|_| run.clip_has_mask),
                );
                for placement in &run.placements {
                    let bitmap = &placement.bitmap;
                    for by in 0..bitmap.height as i32 {
                        let y = placement.dy + by;
                        if y < run.bounds.y
                            || y >= run.bounds.y + run.bounds.height as i32
                            || y < 0
                            || y >= map.height as i32
                        {
                            continue;
                        }
                        for bx in 0..bitmap.width as i32 {
                            let x = placement.dx + bx;
                            if x < run.bounds.x
                                || x >= run.bounds.x + run.bounds.width as i32
                                || x < 0
                                || x >= map.width as i32
                            {
                                continue;
                            }
                            let glyph =
                                bitmap.cov[by as usize * bitmap.width as usize + bx as usize];
                            let coverage = combined_coverage(
                                glyph,
                                clip_at(&clips, run.clip.filter(|_| run.clip_has_mask), x, y),
                            );
                            if coverage >= coverage_threshold {
                                map.stamp(x as u32, y as u32, PaintLeaf::Text, run.origin);
                            }
                        }
                    }
                }
                i += 1;
            }
            PreparedOp::Image(image) => {
                ensure_clip(
                    page,
                    &mut raster,
                    &mut clips,
                    image.clip.filter(|_| image.clip_has_mask),
                );
                for_each_pixel(image.bounds, map.width, map.height, |x, y| {
                    let dx = x as f64 + 0.5;
                    let dy = y as f64 + 0.5;
                    let u = image.inv.a * dx + image.inv.c * dy + image.inv.e;
                    let v = image.inv.b * dx + image.inv.d * dy + image.inv.f;
                    if (0.0..1.0).contains(&u) && (0.0..1.0).contains(&v) {
                        let coverage = clip_at(
                            &clips,
                            image.clip.filter(|_| image.clip_has_mask),
                            x as i32,
                            y as i32,
                        );
                        if coverage >= coverage_threshold {
                            map.stamp(x, y, PaintLeaf::Image, image.origin);
                        }
                    }
                });
                i += 1;
            }
            PreparedOp::PushSoftMask { content_end, .. } => {
                // The enclosed ops paint only into the mask's offscreen.
                i = (*content_end as usize).max(i + 1);
            }
            PreparedOp::BeginGroup { .. }
            | PreparedOp::EndGroup
            | PreparedOp::PushSoftMaskNone
            | PreparedOp::PopSoftMask => i += 1,
        }
    }
    map
}

/// Where the text pass stamped: one flag per device pixel, row-major, true
/// where a glyph run's geometric coverage reached the pixel.
///
/// This mirrors the glyph-run arm of [`render_attribution_with_threshold`] —
/// same coverage threshold, same run-bounds gating, same clip-mask
/// combination, same soft-mask scope skipping — recording *that* the text
/// pass stamped instead of *what* won. Forensic occlusion analysis
/// intersects this mask with the attribution map: a glyph pixel whose winner
/// is later path or image paint is covered text, and the painter order is
/// proven rather than assumed — the glyph stamped first, so whatever won
/// over it painted later. Text lowered as outlines or placement boxes
/// (non-fill render modes, rotated or oversized runs, fonts without outline
/// programs) stamps `Path`, not `Text`, and never appears here; that bounds
/// this mask's recall, never its precision.
pub fn glyph_stamp_mask(page: &CpuPreparedPage, threshold: u8) -> Vec<bool> {
    let width = page.size.width as usize;
    let height = page.size.height as usize;
    let mut mask = vec![false; width.saturating_mul(height)];
    let mut raster = RasterKernel::default();
    let mut clips: Vec<Option<ClipMask>> = (0..page.clips.len()).map(|_| None).collect();

    let mut i = 0usize;
    while i < page.ops.len() {
        match &page.ops[i] {
            PreparedOp::GlyphRun(run) => {
                ensure_clip(
                    page,
                    &mut raster,
                    &mut clips,
                    run.clip.filter(|_| run.clip_has_mask),
                );
                for placement in &run.placements {
                    let bitmap = &placement.bitmap;
                    for by in 0..bitmap.height as i32 {
                        let y = placement.dy + by;
                        if y < run.bounds.y
                            || y >= run.bounds.y + run.bounds.height as i32
                            || y < 0
                            || y >= page.size.height as i32
                        {
                            continue;
                        }
                        for bx in 0..bitmap.width as i32 {
                            let x = placement.dx + bx;
                            if x < run.bounds.x
                                || x >= run.bounds.x + run.bounds.width as i32
                                || x < 0
                                || x >= page.size.width as i32
                            {
                                continue;
                            }
                            let glyph =
                                bitmap.cov[by as usize * bitmap.width as usize + bx as usize];
                            let coverage = combined_coverage(
                                glyph,
                                clip_at(&clips, run.clip.filter(|_| run.clip_has_mask), x, y),
                            );
                            if coverage >= threshold {
                                mask[y as usize * width + x as usize] = true;
                            }
                        }
                    }
                }
                i += 1;
            }
            PreparedOp::PushSoftMask { content_end, .. } => {
                // The enclosed ops paint only into the mask's offscreen.
                i = (*content_end as usize).max(i + 1);
            }
            _ => i += 1,
        }
    }
    mask
}

impl AttributionMap {
    #[inline]
    fn stamp(&mut self, x: u32, y: u32, leaf: PaintLeaf, origin: PaintOrigin) {
        let index = y as usize * self.width as usize + x as usize;
        self.leaf[index] = leaf as u8;
        self.origin[index] = origin as u8;
    }
}

#[allow(clippy::too_many_arguments)]
fn stamp_path(
    page: &CpuPreparedPage,
    map: &mut AttributionMap,
    raster: &mut RasterKernel,
    clips: &mut [Option<ClipMask>],
    subpath_range: (u32, u32),
    rule: crate::render_cpu::raster::FillRule,
    bounds: DeviceRect,
    clip: Option<u32>,
    leaf: PaintLeaf,
    origin: PaintOrigin,
) {
    ensure_clip(page, raster, clips, clip);
    let subpaths = &page.subpaths[subpath_range.0 as usize..subpath_range.1 as usize];
    let width = map.width as usize;
    let height = map.height as usize;
    let threshold = map.coverage_threshold;
    raster.fill(
        &page.points,
        subpaths,
        width,
        height,
        rule,
        |y, x0, x1, coverage| {
            if y < bounds.y.max(0) as usize
                || y >= (bounds.y + bounds.height as i32).max(0) as usize
            {
                return;
            }
            let left = x0.max(bounds.x.max(0) as usize);
            let right_exclusive = (bounds.x + bounds.width as i32).max(0) as usize;
            if left >= right_exclusive {
                return;
            }
            let right = x1.min(right_exclusive - 1);
            for x in left..=right {
                let combined =
                    combined_coverage(coverage[x], clip_at(clips, clip, x as i32, y as i32));
                if combined >= threshold {
                    map.stamp(x as u32, y as u32, leaf, origin);
                }
            }
        },
    );
}

fn ensure_clip(
    page: &CpuPreparedPage,
    raster: &mut RasterKernel,
    clips: &mut [Option<ClipMask>],
    clip: Option<u32>,
) {
    if let Some(id) = clip
        && clips[id as usize].is_none()
    {
        clips[id as usize] = Some(build_clip_mask(raster, ClipGeom::of(page), id));
    }
}

#[inline]
fn combined_coverage(paint: u8, clip: u8) -> u8 {
    mul_div_255(paint as u16, clip as u16) as u8
}

#[inline]
fn clip_at(clips: &[Option<ClipMask>], clip: Option<u32>, x: i32, y: i32) -> u8 {
    let Some(mask) = clip.and_then(|id| clips[id as usize].as_ref()) else {
        return 255;
    };
    if x < mask.bounds.x
        || y < mask.bounds.y
        || x >= mask.bounds.x + mask.bounds.width as i32
        || y >= mask.bounds.y + mask.bounds.height as i32
    {
        return mask.outside;
    }
    let lx = (x - mask.bounds.x) as usize;
    let ly = (y - mask.bounds.y) as usize;
    mask.data[ly * mask.stride() + lx]
}

fn for_each_pixel(bounds: DeviceRect, width: u32, height: u32, mut f: impl FnMut(u32, u32)) {
    let x0 = bounds.x.max(0) as u32;
    let y0 = bounds.y.max(0) as u32;
    let x1 = (bounds.x + bounds.width as i32).max(0).min(width as i32) as u32;
    let y1 = (bounds.y + bounds.height as i32).max(0).min(height as i32) as u32;
    for y in y0..y1 {
        for x in x0..x1 {
            f(x, y);
        }
    }
}

#[cfg(all(test, not(feature = "profiling")))]
mod tests {
    use super::*;
    use crate::page_ir::{BlendMode, DeviceSize};
    use crate::render_cpu::prepared::{DrawClass, PreparedCommand, RenderDiagnostics};

    fn page(
        ops: Vec<PreparedOp>,
        points: Vec<[f32; 2]>,
        subpaths: Vec<(usize, usize)>,
    ) -> CpuPreparedPage {
        CpuPreparedPage {
            size: DeviceSize {
                width: 4,
                height: 4,
            },
            ops,
            clips: Vec::new(),
            points,
            subpaths,
            codecs: crate::image::CodecRegistry::default(),
            decode_limits: crate::image::DecodeLimits::default(),
            hinting: crate::font::HintingPolicy::None,
            color_policy: crate::render_api::RenderColorPolicy::Original,
            diagnostics: RenderDiagnostics::default(),
            image_cache: None,
            decode_floor: None,
            sanity_extent: 4.0,
            rows: (0, u32::MAX),
            op_extent: None,
            strip_decodes: None,
        }
    }

    fn draw(range: (u32, u32), bounds: DeviceRect, origin: PaintOrigin) -> PreparedOp {
        PreparedOp::Draw(PreparedCommand {
            origin,
            class: DrawClass::SolidPath,
            subpath_range: range,
            rule: crate::render_cpu::raster::FillRule::NonZero,
            rgb: [0, 0, 0],
            premul: [0, 0, 0, 255],
            alpha: 255,
            opaque: true,
            bounds,
            clip: None,
            clip_has_mask: false,
            blend: BlendMode::Normal,
            shading: None,
            stencil: None,
        })
    }

    #[test]
    fn blank_page_is_entirely_unpainted() {
        let map = render_attribution(&page(Vec::new(), Vec::new(), Vec::new()));
        assert!(
            map.leaf
                .iter()
                .all(|&value| value == PaintLeaf::Unpainted as u8)
        );
    }

    #[test]
    fn later_painter_replaces_origin() {
        let points = vec![
            [0.0, 0.0],
            [4.0, 0.0],
            [4.0, 4.0],
            [0.0, 4.0],
            [1.0, 1.0],
            [3.0, 1.0],
            [3.0, 3.0],
            [1.0, 3.0],
        ];
        let ops = vec![
            draw(
                (0, 1),
                DeviceRect {
                    x: 0,
                    y: 0,
                    width: 4,
                    height: 4,
                },
                PaintOrigin::FormXObject,
            ),
            draw(
                (1, 2),
                DeviceRect {
                    x: 1,
                    y: 1,
                    width: 2,
                    height: 2,
                },
                PaintOrigin::AnnotationAppearance,
            ),
        ];
        let map = render_attribution(&page(ops, points, vec![(0, 4), (4, 8)]));
        assert_eq!(map.leaf[0], PaintLeaf::Path as u8);
        assert_eq!(map.origin[0], PaintOrigin::FormXObject as u8);
        assert_eq!(
            map.origin[1 * 4 + 1],
            PaintOrigin::AnnotationAppearance as u8
        );
    }
}