Emit native PDF page resources and named transparency states
This commit is contained in:
1 parent
2bced813a2
commit
4f4a4567ea
23 files changed
+791
-37
No files matched your search
@@ -247,6 +247,7 @@ pub struct DisplayList {
|
||||
pub units: DisplayUnits,
|
||||
pub pdf: Option<crate::pdf_device::PdfPageData>,
|
||||
pub pdf_links: crate::pdf_device::links::PageLinks,
|
||||
pub pdf_resources: crate::pdf_device::page_resources::PageResources,
|
||||
}
|
||||
impl Default for DisplayList {
|
||||
fn default() -> Self {
|
||||
@@ -260,6 +261,35 @@ impl Default for DisplayList {
|
||||
units: DisplayUnits::PostScript,
|
||||
pdf: None,
|
||||
pdf_links: Default::default(),
|
||||
pdf_resources: Default::default(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Default)]
|
||||
struct SvgTransparency {
|
||||
fill: String,
|
||||
stroke: String,
|
||||
rule_stroke: String,
|
||||
image: String,
|
||||
}
|
||||
|
||||
impl SvgTransparency {
|
||||
fn apply(&mut self, transparency: &crate::pdf_device::page_resources::Transparency) {
|
||||
let attribute = |name, alpha: &str| {
|
||||
if alpha == "1" {
|
||||
String::new()
|
||||
} else {
|
||||
format!(" {name}=\"{alpha}\"")
|
||||
}
|
||||
};
|
||||
if let Some(alpha) = &transparency.fill {
|
||||
self.fill = attribute("fill-opacity", alpha);
|
||||
self.image = attribute("opacity", alpha);
|
||||
}
|
||||
if let Some(alpha) = &transparency.stroke {
|
||||
self.stroke = attribute("stroke-opacity", alpha);
|
||||
self.rule_stroke = attribute("fill-opacity", alpha);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -267,9 +297,11 @@ impl Default for DisplayList {
|
||||
impl DisplayList {
|
||||
/// Whether this page needs the complete PDF device output for SVG preview.
|
||||
pub fn has_embedded_pdf(&self) -> bool {
|
||||
self.items
|
||||
.iter()
|
||||
.any(|item| matches!(item, DisplayItem::PdfImage { .. }))
|
||||
self.pdf_resources.needs_pdf_for_preview
|
||||
|| self
|
||||
.items
|
||||
.iter()
|
||||
.any(|item| matches!(item, DisplayItem::PdfImage { .. }))
|
||||
}
|
||||
|
||||
/// Compare the inputs consumed by SVG rendering. Font data for glyphs not
|
||||
@@ -280,6 +312,8 @@ impl DisplayList {
|
||||
&& self.units == other.units
|
||||
&& self.magnification == other.magnification
|
||||
&& self.images == other.images
|
||||
&& self.pdf_resources.opacity == other.pdf_resources.opacity
|
||||
&& self.pdf_resources.needs_pdf_for_preview == other.pdf_resources.needs_pdf_for_preview
|
||||
&& self.items.len() == other.items.len()
|
||||
&& self.items.iter().zip(&other.items).all(|(left, right)| {
|
||||
match (left.native_svg_glyph(), right.native_svg_glyph()) {
|
||||
@@ -357,9 +391,17 @@ impl DisplayList {
|
||||
if !(1..=32768).contains(&self.magnification) {
|
||||
return Err("invalid display-list magnification".into());
|
||||
}
|
||||
if self.has_embedded_pdf() {
|
||||
return Err("this page requires a PDF renderer".into());
|
||||
}
|
||||
for item in &self.items {
|
||||
if matches!(item, DisplayItem::PdfImage { .. }) {
|
||||
return Err("embedded PDF page requires a PDF renderer".into());
|
||||
if let DisplayItem::PdfColor(program) = item
|
||||
&& program
|
||||
.graphics_states
|
||||
.iter()
|
||||
.any(|name| !self.pdf_resources.opacity.contains_key(name))
|
||||
{
|
||||
return Err("unresolved PDF graphics state in SVG page".into());
|
||||
}
|
||||
if let Some(glyph) = item.native_svg_glyph() {
|
||||
glyph?;
|
||||
@@ -405,9 +447,12 @@ impl DisplayList {
|
||||
let mut fill = [0u8, 0, 0];
|
||||
let mut rule_fill = [0u8; 3];
|
||||
let mut stroke = [0u8; 3];
|
||||
let mut transparency = SvgTransparency::default();
|
||||
let mut graphics = Vec::new();
|
||||
let mut open_transforms = 0;
|
||||
for item in &self.items {
|
||||
let fill_opacity = &transparency.fill;
|
||||
let stroke_opacity = &transparency.stroke;
|
||||
match item {
|
||||
DisplayItem::NativeGlyph { .. } => {
|
||||
let NativeSvgGlyph {
|
||||
@@ -436,7 +481,7 @@ impl DisplayList {
|
||||
} else {
|
||||
String::new()
|
||||
};
|
||||
out.push_str(&format!("<g data-font=\"{}\" data-glyph=\"{}\" data-slot=\"{slot}\" aria-label=\"{}\"><path d=\"{}\" transform=\"translate({x:.5} {y:.5}) scale({horizontal_scale:.8} -{scale:.8})\" fill=\"#{:02x}{:02x}{:02x}\"/></g>\n", escape(font), escape(glyph), escape(&label), path, fill[0],fill[1],fill[2]));
|
||||
out.push_str(&format!("<g data-font=\"{}\" data-glyph=\"{}\" data-slot=\"{slot}\" aria-label=\"{}\"><path d=\"{}\" transform=\"translate({x:.5} {y:.5}) scale({horizontal_scale:.8} -{scale:.8})\" fill=\"#{:02x}{:02x}{:02x}\"{fill_opacity}/></g>\n", escape(font), escape(glyph), escape(&label), path, fill[0],fill[1],fill[2]));
|
||||
}
|
||||
DisplayItem::MathGlyph {
|
||||
x,
|
||||
@@ -450,7 +495,7 @@ impl DisplayList {
|
||||
let y = *y as f64 / 65_536.0;
|
||||
let scale = *size as f64 / 65_536_000.0;
|
||||
out.push_str(&format!(
|
||||
"<g data-font=\"{}\" data-slot=\"{slot}\" aria-label=\"{}\"><use href=\"#math-{}-{slot}\" transform=\"translate({x:.5} {y:.5}) scale({scale:.8} -{scale:.8})\" fill=\"#{:02x}{:02x}{:02x}\"/><text x=\"{x:.5}\" y=\"{y:.5}\" opacity=\"0\" aria-hidden=\"true\">{}</text></g>\n",
|
||||
"<g data-font=\"{}\" data-slot=\"{slot}\" aria-label=\"{}\"><use href=\"#math-{}-{slot}\" transform=\"translate({x:.5} {y:.5}) scale({scale:.8} -{scale:.8})\" fill=\"#{:02x}{:02x}{:02x}\"{fill_opacity}/><text x=\"{x:.5}\" y=\"{y:.5}\" opacity=\"0\" aria-hidden=\"true\">{}</text></g>\n",
|
||||
face.name(), escape(&character.to_string()), face.0,
|
||||
fill[0], fill[1], fill[2], escape(&character.to_string())
|
||||
));
|
||||
@@ -515,7 +560,7 @@ impl DisplayList {
|
||||
format!(" fill=\"#{:02x}{:02x}{:02x}\"", fill[0], fill[1], fill[2])
|
||||
};
|
||||
out.push_str(&format!(
|
||||
"<text x=\"{x:.3}\" y=\"{y:.3}\" font-size=\"{size:.3}\" font-family=\"{family}\"{weight}{style}{fill_attr}>{}</text>\n",
|
||||
"<text x=\"{x:.3}\" y=\"{y:.3}\" font-size=\"{size:.3}\" font-family=\"{family}\"{weight}{style}{fill_attr}{fill_opacity}>{}</text>\n",
|
||||
escape(text)
|
||||
));
|
||||
}
|
||||
@@ -527,7 +572,7 @@ impl DisplayList {
|
||||
color,
|
||||
} => {
|
||||
out.push_str(&format!(
|
||||
"<rect x=\"{:.3}\" y=\"{:.3}\" width=\"{:.3}\" height=\"{:.3}\" fill=\"#{:02x}{:02x}{:02x}\"/>\n",
|
||||
"<rect x=\"{:.3}\" y=\"{:.3}\" width=\"{:.3}\" height=\"{:.3}\" fill=\"#{:02x}{:02x}{:02x}\"{fill_opacity}/>\n",
|
||||
*x as f64 / 65_536.0,
|
||||
*y as f64 / 65_536.0,
|
||||
*width as f64 / 65_536.0,
|
||||
@@ -547,14 +592,21 @@ impl DisplayList {
|
||||
let y = *y as f64 / 65_536.0;
|
||||
let width = *width as f64 / 65_536.0;
|
||||
let height = *height as f64 / 65_536.0;
|
||||
let color = if rule_fill == [0; 3] {
|
||||
let stroked = self.units == DisplayUnits::TeX
|
||||
&& (height <= 65_782.0 / 65_536.0 || width <= 65_782.0 / 65_536.0);
|
||||
let (rgb, rule_alpha) = if stroked {
|
||||
(stroke, &transparency.rule_stroke)
|
||||
} else {
|
||||
(rule_fill, fill_opacity)
|
||||
};
|
||||
let color = if rgb == [0; 3] {
|
||||
"black".to_owned()
|
||||
} else {
|
||||
let [r, g, b] = rule_fill;
|
||||
let [r, g, b] = rgb;
|
||||
format!("#{r:02x}{g:02x}{b:02x}")
|
||||
};
|
||||
out.push_str(&format!(
|
||||
"<rect x=\"{x:.3}\" y=\"{y:.3}\" width=\"{width:.3}\" height=\"{height:.3}\" fill=\"{color}\"/>\n"
|
||||
"<rect x=\"{x:.3}\" y=\"{y:.3}\" width=\"{width:.3}\" height=\"{height:.3}\" fill=\"{color}\"{rule_alpha}/>\n"
|
||||
));
|
||||
}
|
||||
// Link annotations are PDF metadata; the SVG preview omits them.
|
||||
@@ -567,7 +619,7 @@ impl DisplayList {
|
||||
thickness,
|
||||
} => {
|
||||
out.push_str(&format!(
|
||||
"<rect x=\"{:.3}\" y=\"{:.3}\" width=\"{:.3}\" height=\"{:.3}\" fill=\"none\" stroke=\"#{:02x}{:02x}{:02x}\" stroke-width=\"{:.3}\"/>\n",
|
||||
"<rect x=\"{:.3}\" y=\"{:.3}\" width=\"{:.3}\" height=\"{:.3}\" fill=\"none\" stroke=\"#{:02x}{:02x}{:02x}\" stroke-width=\"{:.3}\"{stroke_opacity}/>\n",
|
||||
*x as f64 / 65_536.0,
|
||||
(*y - *height) as f64 / 65_536.0,
|
||||
*width as f64 / 65_536.0,
|
||||
@@ -594,12 +646,21 @@ impl DisplayList {
|
||||
if let Some(rgb) = program.stroke {
|
||||
stroke = rgb;
|
||||
}
|
||||
for name in &program.graphics_states {
|
||||
transparency.apply(&self.pdf_resources.opacity[name]);
|
||||
}
|
||||
}
|
||||
DisplayItem::PdfGraphics { x, y, operation } => {
|
||||
use crate::pdf_device::PdfGraphics;
|
||||
match operation {
|
||||
PdfGraphics::Save => {
|
||||
graphics.push((open_transforms, fill, rule_fill, stroke));
|
||||
graphics.push((
|
||||
open_transforms,
|
||||
fill,
|
||||
rule_fill,
|
||||
stroke,
|
||||
transparency.clone(),
|
||||
));
|
||||
}
|
||||
PdfGraphics::Matrix(matrix) => {
|
||||
let [a, b, c, d, e, f] = matrix
|
||||
@@ -611,12 +672,15 @@ impl DisplayList {
|
||||
open_transforms += 1;
|
||||
}
|
||||
PdfGraphics::Restore { shift: [dx, dy] } => {
|
||||
if let Some((depth, old_fill, old_rule, old_stroke)) = graphics.pop() {
|
||||
if let Some((depth, old_fill, old_rule, old_stroke, old_alpha)) =
|
||||
graphics.pop()
|
||||
{
|
||||
for _ in depth..open_transforms {
|
||||
out.push_str("</g>\n");
|
||||
}
|
||||
(open_transforms, fill, rule_fill, stroke) =
|
||||
(depth, old_fill, old_rule, old_stroke);
|
||||
transparency = old_alpha;
|
||||
}
|
||||
if *dx != 0 || *dy != 0 {
|
||||
let dx = *dx as f64 / 65_536.0;
|
||||
@@ -641,7 +705,7 @@ impl DisplayList {
|
||||
let width = *width as f64 / 65_536.0;
|
||||
let [r, g, b] = stroke;
|
||||
out.push_str(&format!(
|
||||
"<line x1=\"{x1:.3}\" y1=\"{y1:.3}\" x2=\"{x2:.3}\" y2=\"{y2:.3}\" stroke=\"#{r:02x}{g:02x}{b:02x}\" stroke-width=\"{width:.3}\"/>\n"
|
||||
"<line x1=\"{x1:.3}\" y1=\"{y1:.3}\" x2=\"{x2:.3}\" y2=\"{y2:.3}\" stroke=\"#{r:02x}{g:02x}{b:02x}\" stroke-width=\"{width:.3}\"{stroke_opacity}/>\n"
|
||||
));
|
||||
}
|
||||
DisplayItem::PdfImage { .. } => unreachable!("checked before SVG export"),
|
||||
@@ -661,8 +725,9 @@ impl DisplayList {
|
||||
&& image.width as u64 * image.height as u64 <= 4_000_000
|
||||
{
|
||||
let uri = png_data_uri(image);
|
||||
let image_opacity = &transparency.image;
|
||||
out.push_str(&format!(
|
||||
"<image x=\"{x:.3}\" y=\"{y:.3}\" width=\"{width:.3}\" height=\"{height:.3}\" href=\"{uri}\"/>\n"
|
||||
"<image x=\"{x:.3}\" y=\"{y:.3}\" width=\"{width:.3}\" height=\"{height:.3}\" href=\"{uri}\"{image_opacity}/>\n"
|
||||
));
|
||||
} else {
|
||||
out.push_str(&format!(
|
||||
|
||||
Reference in new issue
Block a user