Apply native PDF page dictionaries and viewport projection

This commit is contained in:
gloamxun committed 2026-11-21 03:31:00 +08:00
1 parent a6ba70aa46
commit d1ddf4bfeb
19 files changed
+410 -40

No files matched your search

+50 -6
View File
@@ -84,6 +84,19 @@ impl Bounds {
}
}
impl Bounds {
fn clipped_by(self, width: i64, height: i64) -> Option<Self> {
let x = self.x.max(0);
let y = self.y.max(0);
let right = self.x.saturating_add(self.width).min(width);
let bottom = self.y.saturating_add(self.height).min(height);
(right >= x && bottom >= y && width > 0 && height > 0).then_some(Self {
x,
y,
width: right.saturating_sub(x),
height: bottom.saturating_sub(y),
})
}
pub fn contains(self, x: i64, y: i64) -> bool {
let right = i128::from(self.x) + i128::from(self.width);
let bottom = i128::from(self.y) + i128::from(self.height);
@@ -117,6 +130,7 @@ pub struct MappedPaint {
pub enum UnmappedReason {
MissingProvenance,
UnsupportedPrimitive,
ClippedByPage,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct UnmappedPaint {
@@ -296,6 +310,19 @@ impl SourceMap {
entries: Vec::new(),
unmapped: Vec::new(),
};
let viewport = page.pdf_resources.viewport.as_ref().map(|viewport| {
let unit = page.units.bp(1) * f64::from(page.magnification) / 1000.0;
let dimensions = viewport.dimensions().map(|value| (value / unit).ceil());
if dimensions.iter().any(|value| !value.is_finite() || *value >= i64::MAX as f64) {
return Err("native PDF viewport exceeds source map dimensions");
}
[map.width, map.height] = dimensions.map(|value| value as i64);
let mut transform = viewport.transform(page.height as f64 * unit);
for distance in &mut transform.0[4..] {
*distance /= unit;
}
Ok(transform)
}).transpose()?;
let mut transform = Affine::IDENTITY;
let mut graphics_stack = Vec::new();
let mut pdf_projection = PdfProjection::for_page(page);
@@ -303,6 +330,11 @@ impl SourceMap {
let placement = pdf_projection
.as_mut()
.map_or(Some(transform), |projection| projection.observe(item));
let placement = if page.pdf_resources.overrides_paint {
None
} else {
placement.map(|graphics| viewport.map_or(graphics, |frame| frame.concatenate(graphics)))
};
if let DisplayItem::PdfGraphics { x, y, operation } = item {
if pdf_projection.is_some() {
continue;
@@ -414,12 +446,24 @@ impl SourceMap {
.filter(|s| !s.tokens.is_empty()),
placement,
) {
(Some((kind, bounds)), Some(source), Some(transform)) => map.entries.push(MappedPaint {
item_index,
kind,
bounds: bounds.transformed(transform)?,
source: source.clone(),
}),
(Some((kind, bounds)), Some(source), Some(transform)) => {
let bounds = bounds.transformed(transform)?;
let visible = if viewport.is_some() {
bounds.clipped_by(map.width, map.height)
} else {
Some(bounds)
};
if let Some(bounds) = visible {
map.entries.push(MappedPaint {
item_index, kind, bounds, source: source.clone(),
});
} else {
map.unmapped.push(UnmappedPaint {
item_index, reason: UnmappedReason::ClippedByPage,
});
}
}
(geometry, _, placement) => map.unmapped.push(UnmappedPaint {
item_index,
reason: if geometry.is_some() && placement.is_some() {