Carry XDV device advances through packed box scopes

This commit is contained in:
gloamxun committed 2026-11-29 03:42:11 +08:00
1 parent 9da75c82f5
commit 9efededf6d
14 files changed
+252 -83

No files matched your search

+61 -19
View File
@@ -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)