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

+11 -3
View File
@@ -31,7 +31,7 @@ struct PdfPreview<'a> {
}
impl PdfPreview<'_> {
fn render_page(&self, index: usize) -> Result<Arc<str>, String> {
fn render_page(&self, index: usize, unit: f64) -> Result<Arc<str>, String> {
let document = self
.document
.get_or_init(|| {
@@ -48,11 +48,19 @@ impl PdfPreview<'_> {
.iter()
.nth(index)
.ok_or("preview PDF page is missing")?;
Ok(Arc::from(hayro_svg::convert(
let svg = hayro_svg::convert(
page,
&RenderCache::new(),
&InterpreterSettings::default(),
&SvgRenderSettings::default(),
);
if unit == 1.0 {
return Ok(Arc::from(svg));
}
let (width, height) = page.render_dimensions();
let (width, height) = (f64::from(width) * unit, f64::from(height) * unit);
Ok(Arc::from(format!(
"<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"{width}pt\" height=\"{height}pt\" viewBox=\"0 0 {width} {height}\"><g transform=\"scale({unit})\">{svg}</g></svg>"
)))
}
}
@@ -137,7 +145,7 @@ impl PreviewState {
let svg = self.slots[index]
.get_or_init(|| {
if page.has_embedded_pdf() {
pdf.render_page(index)
pdf.render_page(index, page.pdf_user_unit())
} else {
page.try_to_svg().map(Arc::from)
}