[pdf] Redaction: rebuild redacted pages as one image, scrub hidden data, prune; reader and AES-256 protect/unlock

This commit is contained in:
2026-11-16 13:26:02 +01:00
parent b554d760e2
commit 2c1611b34a
14 changed files with 1452 additions and 125 deletions
+92
View File
@@ -0,0 +1,92 @@
//! View space ↔ PDF user space.
//!
//! View space: points from the top-left corner of the page *as displayed* — the crop box, turned by `/Rotate`
//! (clockwise). That is what the UI draws and what the user drags on. PDF user space: the page's own coordinates,
//! origin bottom-left of the media box, before rotation.
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
pub struct ViewPoint {
pub x: f32,
pub y: f32,
}
#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
pub struct ViewRect {
pub x: f32,
pub y: f32,
pub w: f32,
pub h: f32,
}
impl ViewRect {
pub fn is_empty(&self) -> bool {
!(self.w > 0.0 && self.h > 0.0) || !self.x.is_finite() || !self.y.is_finite()
}
}
/// What view space needs to know about a page.
#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
pub struct PageFrame {
/// The crop box in user space: x0, y0, x1, y1 (normalised so x0 < x1, y0 < y1).
pub crop: [f32; 4],
/// `/Rotate`, normalised to 0, 90, 180 or 270.
pub rotate: u16,
}
impl PageFrame {
/// The displayed size in points (width, height).
pub fn view_size(&self) -> (f32, f32) {
let (w, h) = (self.crop[2] - self.crop[0], self.crop[3] - self.crop[1]);
if self.rotate % 180 == 90 { (h, w) } else { (w, h) }
}
/// A view-space point → user space.
pub fn to_user(&self, p: ViewPoint) -> (f32, f32) {
let [x0, y0, x1, y1] = self.crop;
match self.rotate {
90 => (x0 + p.y, y0 + p.x),
180 => (x1 - p.x, y0 + p.y),
270 => (x1 - p.y, y1 - p.x),
_ => (x0 + p.x, y1 - p.y),
}
}
/// A view-space rectangle → a user-space rectangle [x0, y0, x1, y1].
pub fn rect_to_user(&self, r: ViewRect) -> [f32; 4] {
let (ax, ay) = self.to_user(ViewPoint { x: r.x, y: r.y });
let (bx, by) = self.to_user(ViewPoint { x: r.x + r.w, y: r.y + r.h });
[ax.min(bx), ay.min(by), ax.max(bx), ay.max(by)]
}
}
pub(crate) fn normalise_rotate(r: i64) -> u16 {
(((r % 360) + 360) % 360 / 90 * 90) as u16
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn corners_map_for_every_rotation() {
let crop = [10.0, 20.0, 110.0, 220.0]; // 100 × 200
let top_left = ViewPoint { x: 0.0, y: 0.0 };
let f = |r| PageFrame { crop, rotate: r };
assert_eq!(f(0).to_user(top_left), (10.0, 220.0));
assert_eq!(f(90).to_user(top_left), (10.0, 20.0));
assert_eq!(f(180).to_user(top_left), (110.0, 20.0));
assert_eq!(f(270).to_user(top_left), (110.0, 220.0));
assert_eq!(f(90).view_size(), (200.0, 100.0));
// The far corner of the view is the opposite user corner.
let (w, h) = f(90).view_size();
assert_eq!(f(90).to_user(ViewPoint { x: w, y: h }), (110.0, 220.0));
}
#[test]
fn rotate_is_normalised() {
assert_eq!(normalise_rotate(-90), 270);
assert_eq!(normalise_rotate(450), 90);
assert_eq!(normalise_rotate(45), 0);
}
}