Ship XDV annotations from measured page ranges
This commit is contained in:
1 parent
515dbe6181
commit
20986b2fd5
16 files changed
+873
-54
No files matched your search
@@ -400,6 +400,7 @@ impl Executor {
|
||||
let mut has_space_nodes = false;
|
||||
let mut has_explicit_space = false;
|
||||
let mut pdf_objects = Vec::new();
|
||||
let mut xdv_annotations = std::collections::BTreeMap::new();
|
||||
for (index, output) in outputs.into_iter().enumerate() {
|
||||
while font_uses.peek().is_some_and(|usage| usage.before_output == index) {
|
||||
self.current = current;
|
||||
@@ -461,6 +462,9 @@ impl Executor {
|
||||
shipout.xdv_color_steps.insert(graphics_step, program);
|
||||
}
|
||||
None
|
||||
} else if xdv && crate::pdf_device::xdv_annotation::Command::recognize(&special.bytes).is_some() {
|
||||
xdv_annotations.insert(graphics_step, shipout.specials.len());
|
||||
None
|
||||
} else if xdv && self.xdv_page_special(
|
||||
&special,
|
||||
crate::pdf_device::xdv_object::Position {
|
||||
@@ -469,6 +473,7 @@ impl Executor {
|
||||
height: device.page_height,
|
||||
magnification: device.magnification,
|
||||
},
|
||||
graphics_step,
|
||||
&mut shipout.pdf_links,
|
||||
files,
|
||||
)? {
|
||||
@@ -633,6 +638,9 @@ impl Executor {
|
||||
links.xdv_destinations = std::mem::take(&mut shipout.pdf_links.xdv_destinations);
|
||||
shipout.pdf_links = links;
|
||||
}
|
||||
if xdv && (!xdv_annotations.is_empty() || self.xdv_annotations.is_active()) {
|
||||
shipout.pdf_links.xdv_annotations = self.ship_xdv_annotations(&shipout, &xdv_annotations, files)?;
|
||||
}
|
||||
if let Some(pdf) = &mut shipout.device.pdf && xdv {
|
||||
shipout.pdf_background = self.xdv_colors.background();
|
||||
self.state.xdv_setup.apply_version(&mut pdf.file);
|
||||
|
||||
@@ -53,6 +53,7 @@ impl Executor {
|
||||
&mut self,
|
||||
special: &ShipoutSpecial,
|
||||
position: crate::pdf_device::xdv_object::Position,
|
||||
graphics_step: usize,
|
||||
links: &mut crate::pdf_device::links::PageLinks,
|
||||
files: &mut impl SourceProvider,
|
||||
) -> Result<bool, Error> {
|
||||
@@ -82,7 +83,7 @@ impl Executor {
|
||||
let warnings = if catalog.is_some() {
|
||||
navigation.merge_catalog(operands, position, self.limits.tokens)
|
||||
} else {
|
||||
navigation.add_outline(operands, position, self.limits.tokens)
|
||||
navigation.add_outline(operands, position, (position.page, graphics_step, 1), self.limits.tokens)
|
||||
}.map_err(|error| self.error(error, special.span))?;
|
||||
for warning in warnings {
|
||||
files.observe_diagnostic(WriteDestination::TerminalAndLog, &warning)
|
||||
|
||||
@@ -0,0 +1,244 @@
|
||||
//! Measure XDV annotation ranges from the packed lists actually shipped.
|
||||
use super::*;
|
||||
use crate::{
|
||||
execution::boxes::BoxPaint,
|
||||
math_engine_tex::{MathItem, PositionedItem},
|
||||
native_font::VirtualGlyphs,
|
||||
pdf_device::{
|
||||
Affine, PdfGraphics,
|
||||
xdv_annotation::{Annotation, Bounds, Command, Page},
|
||||
xdv_object::Position,
|
||||
},
|
||||
};
|
||||
use std::collections::BTreeMap;
|
||||
|
||||
impl Executor {
|
||||
pub(super) fn ship_xdv_annotations(
|
||||
&mut self,
|
||||
shipout: &NativeShipout,
|
||||
commands: &BTreeMap<usize, usize>,
|
||||
files: &mut impl SourceProvider,
|
||||
) -> Result<Vec<Annotation>, Error> {
|
||||
let mut state = self.xdv_annotations.clone();
|
||||
let mut page = state.page();
|
||||
let page_number = self.shipped_pages + 1;
|
||||
let device = &shipout.device;
|
||||
let origin = [
|
||||
device.horizontal_origin,
|
||||
device.vertical_origin + shipout.box_node.height,
|
||||
];
|
||||
let mut frame = Frame {
|
||||
transform: Affine::IDENTITY,
|
||||
height: device.page_height,
|
||||
magnification: device.magnification,
|
||||
};
|
||||
let mut saved = Vec::new();
|
||||
let mut graphics = 0;
|
||||
let mut span = shipout.box_node.span;
|
||||
let mut failure = None;
|
||||
shipout
|
||||
.box_node
|
||||
.walk_xdv_annotations(self.cancellation.as_ref(), &mut |x, y, node| {
|
||||
if failure.is_some() {
|
||||
return;
|
||||
}
|
||||
let point = [(x + origin[0]) as f64, (y + origin[1]) as f64];
|
||||
let result = match node {
|
||||
BoxPaint::ScopeStart(level) => {
|
||||
page.enter(level);
|
||||
Ok(())
|
||||
}
|
||||
BoxPaint::ScopeEnd(level) => {
|
||||
page.leave(level, (page_number, graphics, 0));
|
||||
Ok(())
|
||||
}
|
||||
BoxPaint::PdfGraphics => {
|
||||
let step = graphics;
|
||||
graphics += 1;
|
||||
if let Some(&index) = commands.get(&step) {
|
||||
let special = &shipout.specials[index];
|
||||
span = special.span;
|
||||
let (command, operands) =
|
||||
Command::recognize(&special.bytes).expect("annotation special");
|
||||
let position = Position {
|
||||
page: page_number,
|
||||
coordinates: frame
|
||||
.transform
|
||||
.point(point[0], point[1])
|
||||
.map(|value| value.round() as i64),
|
||||
height: frame.height,
|
||||
magnification: frame.magnification,
|
||||
};
|
||||
page.apply(
|
||||
command,
|
||||
operands,
|
||||
point,
|
||||
frame.transform,
|
||||
position,
|
||||
(page_number, step, 1),
|
||||
)
|
||||
.and_then(|warnings| {
|
||||
for warning in warnings {
|
||||
files.observe_diagnostic(
|
||||
WriteDestination::TerminalAndLog,
|
||||
&warning,
|
||||
)?;
|
||||
}
|
||||
Ok(())
|
||||
})
|
||||
} else if let Some(operation) = shipout.pdf_graphics_steps.get(step) {
|
||||
match operation {
|
||||
Some(PdfGraphics::Save) => saved.push(frame.transform),
|
||||
Some(
|
||||
operation @ (PdfGraphics::SaveMatrix(matrix)
|
||||
| PdfGraphics::Matrix(matrix)),
|
||||
) => {
|
||||
if matches!(operation, PdfGraphics::SaveMatrix(_)) {
|
||||
saved.push(frame.transform);
|
||||
}
|
||||
frame.transform = frame
|
||||
.transform
|
||||
.concatenate(matrix.about(point[0], point[1], true));
|
||||
}
|
||||
Some(PdfGraphics::Restore { .. }) => {
|
||||
frame.transform =
|
||||
saved.pop().expect("validated graphics stack");
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
if page.tracking()
|
||||
&& let Some((image, _)) = shipout.pdf_special_images.get(&step)
|
||||
{
|
||||
frame.raster(&mut page, point, image);
|
||||
}
|
||||
Ok(())
|
||||
} else {
|
||||
Err("missing XDV annotation graphic position".into())
|
||||
}
|
||||
}
|
||||
BoxPaint::Box(material) if page.tracking() => {
|
||||
if let Some(geometry) = &material.math_geometry {
|
||||
frame.material(
|
||||
&mut page,
|
||||
point,
|
||||
&geometry.items,
|
||||
shipout.virtual_glyphs.as_ref(),
|
||||
)
|
||||
} else {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
_ => Ok(()),
|
||||
};
|
||||
failure = result.err();
|
||||
})
|
||||
.map_err(|error| self.error(error, span))?;
|
||||
if let Some(error) = failure {
|
||||
return Err(self.error(error, span));
|
||||
}
|
||||
if graphics != shipout.pdf_graphics_steps.len() {
|
||||
return Err(self.error("XDV annotation order does not match its packed list", span));
|
||||
}
|
||||
let annotations = page.finish();
|
||||
self.xdv_annotations = state;
|
||||
Ok(annotations)
|
||||
}
|
||||
}
|
||||
|
||||
struct Frame {
|
||||
transform: Affine,
|
||||
height: i64,
|
||||
magnification: i32,
|
||||
}
|
||||
|
||||
impl Frame {
|
||||
fn expand(&self, page: &mut Page<'_>, [x, y]: [f64; 2], width: i64, height: i64, depth: i64) {
|
||||
page.expand(Bounds::from_logical(
|
||||
[x, y - height as f64, x + width as f64, y + depth as f64],
|
||||
self.transform,
|
||||
self.height,
|
||||
self.magnification,
|
||||
));
|
||||
}
|
||||
|
||||
fn raster(&self, page: &mut Page<'_>, [x, y]: [f64; 2], image: &crate::pdf_image::PdfImageUse) {
|
||||
let placement = image.xdv.as_ref().expect("XDV special image placement");
|
||||
// The driver obtains the link rectangle by converting the lower
|
||||
// corner and each extent to DVI units before the image transform.
|
||||
let unit = f64::from(self.magnification) / 1000.0 * (800.0 / 803.0) / 65536.0;
|
||||
let [left, bottom, width, height] = placement
|
||||
.annotation_box()
|
||||
.map(|coordinate| (coordinate / unit).round() * unit);
|
||||
let [right, top] = [left + width, bottom + height];
|
||||
let placement = Affine(placement.paint_matrix());
|
||||
let corners = [[left, bottom], [left, top], [right, bottom], [right, top]].map(|[u, v]| {
|
||||
let [dx, dy] = placement.point(u, v);
|
||||
[
|
||||
x + dx * 65536.0 * 803.0 / 800.0,
|
||||
y - dy * 65536.0 * 803.0 / 800.0,
|
||||
]
|
||||
});
|
||||
page.expand(Bounds::from_corners(
|
||||
corners,
|
||||
self.transform,
|
||||
self.height,
|
||||
self.magnification,
|
||||
));
|
||||
}
|
||||
|
||||
fn material(
|
||||
&self,
|
||||
page: &mut Page<'_>,
|
||||
[origin_x, origin_y]: [f64; 2],
|
||||
items: &[PositionedItem],
|
||||
virtuals: Option<&VirtualGlyphs>,
|
||||
) -> Result<(), String> {
|
||||
for item in items {
|
||||
let [x, y] = [origin_x + item.x as f64, origin_y + item.y as f64];
|
||||
match &item.item {
|
||||
MathItem::NativeGlyph { font, slot, .. } => {
|
||||
if let Some(packet) = virtuals
|
||||
.and_then(|fonts| fonts.get(&font.id))
|
||||
.and_then(|glyphs| glyphs.get(slot))
|
||||
{
|
||||
self.material(page, [x, y], packet, None)?;
|
||||
} else {
|
||||
let character = char::from(*slot);
|
||||
let metrics = font.metrics();
|
||||
self.expand(
|
||||
page,
|
||||
[x, y],
|
||||
font.advance(*slot),
|
||||
metrics.height(character),
|
||||
metrics.depth(character),
|
||||
);
|
||||
}
|
||||
}
|
||||
MathItem::OpenType(run) => {
|
||||
let face = run.font.face()?;
|
||||
let hhea = face.tables().hhea;
|
||||
let scale = |value| {
|
||||
(run.font.size as f64 * (f64::from(value) / f64::from(face.units_per_em())))
|
||||
as i64
|
||||
};
|
||||
let height = scale(i32::from(hhea.ascender));
|
||||
let depth = -scale(i32::from(hhea.descender));
|
||||
for glyph in &run.glyphs {
|
||||
let width = face
|
||||
.glyph_hor_advance(rustybuzz::ttf_parser::GlyphId(glyph.id))
|
||||
.unwrap_or(0);
|
||||
self.expand(
|
||||
page,
|
||||
[x + glyph.x as f64, y + glyph.y as f64],
|
||||
scale(i32::from(width)),
|
||||
height,
|
||||
depth,
|
||||
);
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user