Carry XDV device advances through packed box scopes
This commit is contained in:
1 parent
9da75c82f5
commit
9efededf6d
14 files changed
+252
-83
No files matched your search
@@ -180,7 +180,7 @@ struct WalkBudget<'a> {
|
||||
remaining: usize,
|
||||
cancellation: Option<&'a Cancellation>,
|
||||
leader_depth: usize,
|
||||
xdv_annotations: bool,
|
||||
xdv_scopes: bool,
|
||||
}
|
||||
|
||||
impl WalkBudget<'_> {
|
||||
@@ -189,7 +189,7 @@ impl WalkBudget<'_> {
|
||||
remaining: usize::MAX,
|
||||
cancellation: None,
|
||||
leader_depth: 0,
|
||||
xdv_annotations: false,
|
||||
xdv_scopes: false,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -296,7 +296,7 @@ impl NativeBox {
|
||||
remaining: limit,
|
||||
cancellation,
|
||||
leader_depth: 0,
|
||||
xdv_annotations: false,
|
||||
xdv_scopes: false,
|
||||
};
|
||||
self.walk_at(0, 0, 0, false, false, &mut budget, &mut |x, y, item| {
|
||||
if let BoxPaint::Output(o) = item {
|
||||
@@ -856,29 +856,37 @@ impl NativeBox {
|
||||
&self,
|
||||
cancellation: Option<&Cancellation>,
|
||||
mut emit: impl FnMut(GeometryItem),
|
||||
) -> Result<(), String> {
|
||||
self.visit_device_geometry(cancellation, false, |item, _| emit(item))
|
||||
}
|
||||
|
||||
pub(super) fn visit_device_geometry(
|
||||
&self,
|
||||
cancellation: Option<&Cancellation>,
|
||||
xdv: bool,
|
||||
mut emit: impl FnMut(GeometryItem, i64),
|
||||
) -> Result<(), String> {
|
||||
use crate::math_engine_tex::PositionedItem;
|
||||
let mut unsupported = false;
|
||||
let mut budget = WalkBudget { cancellation, ..WalkBudget::for_geometry() };
|
||||
self.walk_at(0, 0, 0, false, false, &mut budget, &mut |x, y, node| {
|
||||
self.walk_device(cancellation, xdv, &mut |x, y, node, mut shift| {
|
||||
let b = match node {
|
||||
BoxPaint::Box(b) => b,
|
||||
BoxPaint::PdfColor => {
|
||||
emit(GeometryItem::PdfColor { x, y });
|
||||
emit(GeometryItem::PdfColor { x, y }, shift);
|
||||
return;
|
||||
}
|
||||
BoxPaint::PdfGraphics => {
|
||||
emit(GeometryItem::PdfGraphics { x, y });
|
||||
emit(GeometryItem::PdfGraphics { x, y }, shift);
|
||||
return;
|
||||
}
|
||||
BoxPaint::PdfTextSpace(operation) => {
|
||||
emit(GeometryItem::PdfTextSpace { x, y, operation });
|
||||
emit(GeometryItem::PdfTextSpace { x, y, operation }, shift);
|
||||
return;
|
||||
}
|
||||
BoxPaint::Output(node) => {
|
||||
emit(GeometryItem::Output {
|
||||
special: matches!(node.operation, OutputOperation::Special { .. }),
|
||||
});
|
||||
}, shift);
|
||||
return;
|
||||
}
|
||||
BoxPaint::RowStart(_) | BoxPaint::ScopeStart(_) | BoxPaint::ScopeEnd(_)
|
||||
@@ -891,7 +899,10 @@ impl NativeBox {
|
||||
y: y + p.y,
|
||||
item: p.item.clone(),
|
||||
source: p.source.clone(),
|
||||
}));
|
||||
}), shift);
|
||||
if xdv {
|
||||
shift += xdv_width_difference(&p.item);
|
||||
}
|
||||
}
|
||||
} else if b.character.is_some() {
|
||||
unsupported = true;
|
||||
@@ -928,11 +939,11 @@ impl NativeBox {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub(super) fn positioned_outputs(&self) -> Vec<[i64; 2]> {
|
||||
pub(super) fn positioned_outputs(&self, xdv: bool) -> Vec<([i64; 2], i64)> {
|
||||
let mut positions = Vec::new();
|
||||
self.walk(0, 0, &mut |x, y, node| {
|
||||
self.walk_device(None, xdv, &mut |x, y, node, shift| {
|
||||
if matches!(node, BoxPaint::Output(node) if node.needs_shipout_position()) {
|
||||
positions.push([x, y]);
|
||||
positions.push(([x, y], shift));
|
||||
}
|
||||
})
|
||||
.expect("output traversal of an already measured list");
|
||||
@@ -953,17 +964,39 @@ impl NativeBox {
|
||||
.expect("link traversal of an already measured list");
|
||||
}
|
||||
|
||||
pub(super) fn walk_xdv_annotations(
|
||||
pub(super) fn walk_device(
|
||||
&self,
|
||||
cancellation: Option<&Cancellation>,
|
||||
emit: &mut dyn FnMut(i64, i64, BoxPaint<'_>),
|
||||
xdv: bool,
|
||||
emit: &mut dyn FnMut(i64, i64, BoxPaint<'_>, i64),
|
||||
) -> Result<(), &'static str> {
|
||||
let mut budget = WalkBudget {
|
||||
cancellation,
|
||||
xdv_annotations: true,
|
||||
xdv_scopes: xdv,
|
||||
..WalkBudget::for_geometry()
|
||||
};
|
||||
self.walk_at(0, 0, 0, false, false, &mut budget, emit)
|
||||
let mut shift = 0;
|
||||
let mut saved = Vec::new();
|
||||
self.walk_at(0, 0, 0, false, false, &mut budget, &mut |x, y, node| {
|
||||
let position = shift;
|
||||
if xdv {
|
||||
// The XDV driver adds its own TFM advances to the relative
|
||||
// movements in the stream. POP restores the enclosing cursor.
|
||||
match node {
|
||||
BoxPaint::ScopeStart(_) => saved.push(shift),
|
||||
BoxPaint::ScopeEnd(_) => shift = saved.pop().expect("balanced XDV box walk"),
|
||||
BoxPaint::Box(b) => {
|
||||
if let Some(geometry) = &b.math_geometry {
|
||||
shift += geometry.items.iter()
|
||||
.map(|p| xdv_width_difference(&p.item))
|
||||
.sum::<i64>();
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
emit(x, y, node, position);
|
||||
})
|
||||
}
|
||||
|
||||
fn walk_at(
|
||||
@@ -979,7 +1012,7 @@ impl NativeBox {
|
||||
if self.kind.is_unset() {
|
||||
return Ok(());
|
||||
}
|
||||
if budget.xdv_annotations {
|
||||
if budget.xdv_scopes {
|
||||
budget.charge(1)?;
|
||||
emit(x, y, BoxPaint::ScopeStart(level));
|
||||
}
|
||||
@@ -1097,7 +1130,7 @@ impl NativeBox {
|
||||
}
|
||||
}
|
||||
}
|
||||
if budget.xdv_annotations {
|
||||
if budget.xdv_scopes {
|
||||
emit(x, y, BoxPaint::ScopeEnd(level));
|
||||
}
|
||||
Ok(())
|
||||
@@ -1132,6 +1165,15 @@ impl NativeBox {
|
||||
result
|
||||
}
|
||||
}
|
||||
pub(super) fn xdv_width_difference(item: &crate::math_engine_tex::MathItem) -> i64 {
|
||||
match item {
|
||||
crate::math_engine_tex::MathItem::NativeGlyph { font, slot, .. } => {
|
||||
font.xdv_advance(*slot) - font.advance(*slot)
|
||||
}
|
||||
_ => 0,
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) fn bp(n: i64) -> i64 {
|
||||
if n < 0 {
|
||||
-((-n * 800 + 401) / 803)
|
||||
|
||||
@@ -125,7 +125,9 @@ impl NativeShipout {
|
||||
let mut missing_graphics_step = false;
|
||||
let mut output_index = 0;
|
||||
let mut reached_boundary = false;
|
||||
self.box_node.visit_geometry(|item| {
|
||||
let xdv = page.pdf.as_ref()
|
||||
.is_some_and(|pdf| pdf.driver == crate::pdf_device::PdfDriver::Xdv);
|
||||
self.box_node.visit_device_geometry(None, xdv, |item, shift| {
|
||||
if reached_boundary {
|
||||
return;
|
||||
}
|
||||
@@ -139,7 +141,7 @@ impl NativeShipout {
|
||||
boxes::GeometryItem::PdfColor { x, y } => {
|
||||
match color_steps.next() {
|
||||
Some(Some(color)) => {
|
||||
page.items.push(color.display_item(left + x, baseline + y))
|
||||
page.push_device_item(color.display_item(left + x + shift, baseline + y), shift)
|
||||
}
|
||||
Some(None) => {}
|
||||
None => missing_color_step = true,
|
||||
@@ -147,30 +149,30 @@ impl NativeShipout {
|
||||
return;
|
||||
}
|
||||
boxes::GeometryItem::PdfTextSpace { x, y, operation } => {
|
||||
page.items.push(DisplayItem::PdfTextSpace {
|
||||
x: self.device.horizontal_origin + x,
|
||||
page.push_device_item(DisplayItem::PdfTextSpace {
|
||||
x: self.device.horizontal_origin + x + shift,
|
||||
y: self.device.vertical_origin + self.box_node.height + y,
|
||||
operation,
|
||||
});
|
||||
}, shift);
|
||||
return;
|
||||
}
|
||||
boxes::GeometryItem::PdfGraphics { x, y } => {
|
||||
match graphics_steps.next() {
|
||||
Some((step, operation)) => {
|
||||
if let Some(operation) = operation {
|
||||
page.items.push(DisplayItem::PdfGraphics {
|
||||
x: self.device.horizontal_origin + x,
|
||||
page.push_device_item(DisplayItem::PdfGraphics {
|
||||
x: self.device.horizontal_origin + x + shift,
|
||||
y: self.device.vertical_origin + self.box_node.height + y,
|
||||
operation: operation.clone(),
|
||||
});
|
||||
}, shift);
|
||||
}
|
||||
if let Some(program) = self.xdv_color_steps.get(&step) {
|
||||
page.items.push(DisplayItem::PdfColor {
|
||||
x: left + x,
|
||||
page.push_device_item(DisplayItem::PdfColor {
|
||||
x: left + x + shift,
|
||||
y: baseline + y,
|
||||
program: program.clone(),
|
||||
mode: crate::pdf_device::PdfLiteralMode::Page,
|
||||
});
|
||||
}, shift);
|
||||
}
|
||||
if let Some((image, origin)) = self.pdf_special_images.get(&step) {
|
||||
let [left, bottom] = image.xdv.as_ref().unwrap().layout_offset;
|
||||
@@ -178,14 +180,14 @@ impl NativeShipout {
|
||||
page.items.len(),
|
||||
crate::provenance::PaintSource::token(*origin),
|
||||
);
|
||||
page.items.push(DisplayItem::PdfImage {
|
||||
x: self.device.horizontal_origin + x + left,
|
||||
page.push_device_item(DisplayItem::PdfImage {
|
||||
x: self.device.horizontal_origin + x + shift + left,
|
||||
y: self.device.vertical_origin + self.box_node.height + y
|
||||
- image.height - bottom,
|
||||
width: image.width,
|
||||
height: image.height,
|
||||
image: image.clone(),
|
||||
});
|
||||
}, shift);
|
||||
}
|
||||
}
|
||||
None => missing_graphics_step = true,
|
||||
@@ -196,18 +198,18 @@ impl NativeShipout {
|
||||
if !p.source.tokens.is_empty() {
|
||||
page.sources.insert(page.items.len(), p.source);
|
||||
}
|
||||
let x = self.device.horizontal_origin + p.x;
|
||||
let y = self.device.vertical_origin + self.box_node.height + p.y;
|
||||
let x = left + p.x + shift;
|
||||
let y = baseline + p.y;
|
||||
if let MathItem::OpenType(run) = p.item {
|
||||
run.paint(x, y, |value| value, |item, source| {
|
||||
if !matches!(item, DisplayItem::OpenTypeActualText(_) | DisplayItem::OpenTypeColor(_)) {
|
||||
page.sources.insert(page.items.len(), source);
|
||||
}
|
||||
page.items.push(item);
|
||||
page.push_device_item(item, shift);
|
||||
});
|
||||
return;
|
||||
}
|
||||
page.items.push(match p.item {
|
||||
let item = match p.item {
|
||||
MathItem::NativeGlyph {
|
||||
font,
|
||||
slot,
|
||||
@@ -245,7 +247,8 @@ impl NativeShipout {
|
||||
invalid_geometry = true;
|
||||
return;
|
||||
}
|
||||
});
|
||||
};
|
||||
page.push_device_item(item, shift);
|
||||
})?;
|
||||
if before_output.is_some() && !reached_boundary {
|
||||
return Err("missing deferred output boundary in its packed list".into());
|
||||
|
||||
@@ -48,6 +48,7 @@ enum XdvOutput {
|
||||
index: usize,
|
||||
graphics_step: usize,
|
||||
point: [i64; 2],
|
||||
device_position: [i64; 2],
|
||||
origin: Option<TokenOrigin>,
|
||||
content_origin: Option<[i64; 2]>,
|
||||
text: Option<crate::pdf_device::xdv_document::TextEncoding>,
|
||||
@@ -544,13 +545,13 @@ impl Executor {
|
||||
});
|
||||
self.begin_pdf_link_shipout(b, device, thread_rows)
|
||||
});
|
||||
let mut output_positions = if let Some(effects) = &leader_effects {
|
||||
effects.output_positions.clone().into_iter()
|
||||
let mut output_positions = if let Some(effects) = &leader_effects && !xdv {
|
||||
effects.output_positions.iter().map(|&point| (point, 0)).collect()
|
||||
} else if outputs.iter().any(|node| node.needs_shipout_position()) {
|
||||
b.positioned_outputs().into_iter()
|
||||
b.positioned_outputs(xdv)
|
||||
} else {
|
||||
Vec::new().into_iter()
|
||||
};
|
||||
Vec::new()
|
||||
}.into_iter();
|
||||
let mut font_uses = if device.pdf.is_some() {
|
||||
shipout.virtual_glyphs = Some(VirtualGlyphs::new());
|
||||
shipout_font_uses(b, &self.pdf_virtual_fonts, &self.state.fonts).map_err(|e| self.error(e, b.span))?
|
||||
@@ -577,7 +578,7 @@ impl Executor {
|
||||
layout.advance(self, (index, usage.before_paint), &outputs, &shipout.pdf_graphics_steps, files)?;
|
||||
}
|
||||
self.current = current;
|
||||
self.prepare_virtual_glyph(shipout.virtual_glyphs.as_mut().unwrap(), usage, files)?;
|
||||
self.prepare_virtual_glyph(shipout.virtual_glyphs.as_mut().unwrap(), usage, xdv, files)?;
|
||||
}
|
||||
if let Some(layout) = &mut pdf_link_shipout {
|
||||
layout.advance(self, (index, usize::MAX), &outputs, &shipout.pdf_graphics_steps, files)?;
|
||||
@@ -596,12 +597,13 @@ impl Executor {
|
||||
self.current = output.span;
|
||||
self.probe_pdf_space_font(&shipout, Some(index), files)?;
|
||||
}
|
||||
let position = if output.needs_shipout_position() {
|
||||
Some(output_positions.next().ok_or_else(|| {
|
||||
let (position, xdv_shift) = if output.needs_shipout_position() {
|
||||
let (position, shift) = output_positions.next().ok_or_else(|| {
|
||||
self.error("missing positioned native output node", output.span)
|
||||
})?)
|
||||
})?;
|
||||
(Some(position), shift)
|
||||
} else {
|
||||
None
|
||||
(None, 0)
|
||||
};
|
||||
if let OutputOperation::PdfObject(number) = output.operation {
|
||||
self.current = output.span;
|
||||
@@ -634,6 +636,8 @@ impl Executor {
|
||||
let special = self.resolve_special(
|
||||
output, position.expect("special needs a shipout position"), files,
|
||||
)?;
|
||||
let [x, y] = special.position;
|
||||
let device_position = [x + xdv_shift, y];
|
||||
let graphics_step = shipout.pdf_graphics_steps.len();
|
||||
let graphics = if let Some(pdf) = &device.pdf {
|
||||
if xdv && crate::pdf_device::xdv_color::ColorState::recognizes(&special.bytes) {
|
||||
@@ -647,14 +651,15 @@ impl Executor {
|
||||
None
|
||||
} else if xdv && let Some((kind, _)) = super::specials::xdv_navigation_operand(&special.bytes) {
|
||||
let point = xdv_transform.point(
|
||||
(special.position[0] + horizontal_origin) as f64,
|
||||
(special.position[1] + vertical_origin + b.height) as f64,
|
||||
(device_position[0] + horizontal_origin) as f64,
|
||||
(device_position[1] + vertical_origin + b.height) as f64,
|
||||
).map(|coordinate| coordinate.round() as i64);
|
||||
let text = matches!(kind, b"docview" | b"outline").then(|| self.xdv_text.clone());
|
||||
xdv_output.push(XdvOutput::Navigation {
|
||||
index: shipout.specials.len(),
|
||||
graphics_step,
|
||||
point,
|
||||
device_position,
|
||||
origin: *origin,
|
||||
content_origin: content_origins.last().copied(),
|
||||
text,
|
||||
@@ -671,7 +676,7 @@ impl Executor {
|
||||
use crate::pdf_device::{PdfGraphics, PdfLiteralMode};
|
||||
if let Some(PdfGraphics::Literal { mode, .. }) = &graphics {
|
||||
match mode {
|
||||
PdfLiteralMode::XdvBeginContent => content_origins.push(special.position),
|
||||
PdfLiteralMode::XdvBeginContent => content_origins.push(device_position),
|
||||
PdfLiteralMode::XdvEndContent => { content_origins.pop(); }
|
||||
_ => {}
|
||||
}
|
||||
@@ -686,7 +691,7 @@ impl Executor {
|
||||
xdv_graphics_stack.push((special.span, xdv_transform));
|
||||
xdv_output.push(XdvOutput::SaveColor);
|
||||
}
|
||||
let [x, y] = special.position;
|
||||
let [x, y] = device_position;
|
||||
let pivot = matrix.about(
|
||||
(x + horizontal_origin) as f64,
|
||||
(y + vertical_origin + b.height) as f64,
|
||||
@@ -805,7 +810,7 @@ impl Executor {
|
||||
if let Some(layout) = &mut pdf_link_shipout {
|
||||
layout.advance(self, (output_count, usage.before_paint), &outputs, &shipout.pdf_graphics_steps, files)?;
|
||||
}
|
||||
self.prepare_virtual_glyph(shipout.virtual_glyphs.as_mut().unwrap(), usage, files)?;
|
||||
self.prepare_virtual_glyph(shipout.virtual_glyphs.as_mut().unwrap(), usage, xdv, files)?;
|
||||
}
|
||||
if let Some(layout) = pdf_link_shipout {
|
||||
let mut links = layout.finish(self, &outputs, &shipout.pdf_graphics_steps, files)?;
|
||||
@@ -877,7 +882,7 @@ impl Executor {
|
||||
];
|
||||
let mut saved_colors = Vec::new();
|
||||
for operation in xdv_output {
|
||||
let (index, graphics_step, [x, y], origin, content_origin, text) = match operation {
|
||||
let (index, graphics_step, [x, y], device_position, origin, content_origin, text) = match operation {
|
||||
XdvOutput::SaveColor => {
|
||||
saved_colors.push(self.xdv_colors.current());
|
||||
continue;
|
||||
@@ -907,8 +912,8 @@ impl Executor {
|
||||
}
|
||||
continue;
|
||||
}
|
||||
XdvOutput::Navigation { index, graphics_step, point, origin, content_origin, text } => {
|
||||
(index, graphics_step, point, origin, content_origin, text)
|
||||
XdvOutput::Navigation { index, graphics_step, point, device_position, origin, content_origin, text } => {
|
||||
(index, graphics_step, point, device_position, origin, content_origin, text)
|
||||
}
|
||||
};
|
||||
let special = &shipout.specials[index];
|
||||
@@ -934,7 +939,7 @@ impl Executor {
|
||||
}
|
||||
} else {
|
||||
let [left, top] = content_origin.unwrap_or(default_content_origin);
|
||||
let [x, y] = special.position;
|
||||
let [x, y] = device_position;
|
||||
let capture_origin = [x - left, top - y]
|
||||
.map(|coordinate| crate::render::DisplayUnits::TeX.bp(coordinate));
|
||||
if let Some(operation) = self.ship_xdv_navigation(
|
||||
@@ -1040,7 +1045,7 @@ impl Executor {
|
||||
}
|
||||
|
||||
fn prepare_virtual_glyph(
|
||||
&mut self, glyphs: &mut VirtualGlyphs, usage: ShipoutFontUse,
|
||||
&mut self, glyphs: &mut VirtualGlyphs, usage: ShipoutFontUse, xdv: bool,
|
||||
files: &mut impl SourceProvider,
|
||||
) -> Result<(), Error> {
|
||||
self.check_cancellation()?;
|
||||
@@ -1052,7 +1057,7 @@ impl Executor {
|
||||
}
|
||||
let defaults = self.font_defaults();
|
||||
let (packet, work) = Arc::make_mut(&mut self.pdf_virtual_fonts).glyph(
|
||||
&mut self.state.fonts, defaults, files, usage.font.clone(), usage.slot,
|
||||
&mut self.state.fonts, defaults, files, usage.font.clone(), usage.slot, xdv,
|
||||
self.limits.steps.saturating_sub(self.steps), self.cancellation.as_ref(),
|
||||
).map_err(|e| self.error(e, self.current))?;
|
||||
self.steps += work;
|
||||
|
||||
@@ -39,11 +39,11 @@ impl Executor {
|
||||
let mut failure = None;
|
||||
shipout
|
||||
.box_node
|
||||
.walk_xdv_annotations(self.cancellation.as_ref(), &mut |x, y, node| {
|
||||
.walk_device(self.cancellation.as_ref(), true, &mut |x, y, node, shift| {
|
||||
if failure.is_some() {
|
||||
return;
|
||||
}
|
||||
let point = [(x + origin[0]) as f64, (y + origin[1]) as f64];
|
||||
let point = [(x + origin[0] + shift) as f64, (y + origin[1]) as f64];
|
||||
let result = match node {
|
||||
BoxPaint::ScopeStart(level) => {
|
||||
page.enter(level);
|
||||
@@ -238,8 +238,12 @@ impl Frame {
|
||||
items: &[PositionedItem],
|
||||
virtuals: Option<&VirtualGlyphs>,
|
||||
) -> Result<(), String> {
|
||||
let mut shift = 0;
|
||||
for item in items {
|
||||
let [x, y] = [origin_x + item.x as f64, origin_y + item.y as f64];
|
||||
let [x, y] = [origin_x + (item.x + shift) as f64, origin_y + item.y as f64];
|
||||
if virtuals.is_some() {
|
||||
shift += super::super::boxes::xdv_width_difference(&item.item);
|
||||
}
|
||||
match &item.item {
|
||||
MathItem::NativeGlyph { font, slot, .. } => {
|
||||
if let Some(packet) = virtuals
|
||||
@@ -253,7 +257,7 @@ impl Frame {
|
||||
self.expand(
|
||||
page,
|
||||
[x, y],
|
||||
font.advance(*slot),
|
||||
font.xdv_advance(*slot),
|
||||
metrics.height(character),
|
||||
metrics.depth(character),
|
||||
);
|
||||
|
||||
Reference in new issue
Block a user