Implement native PDF links and vertical discard lists

This commit is contained in:
gloamxun committed 2026-11-18 23:39:45 +08:00
1 parent b6567f67e2
commit a53d9c9bf9
29 files changed
+980 -109

No files matched your search

+53 -10
View File
@@ -135,16 +135,29 @@ fn text_node_count(nodes: &[TextNode]) -> usize {
})
.sum()
}
enum PackedHorizontal {
enum PackedHorizontal<'a> {
Item(HorizontalItem),
PdfColor,
PdfGraphics,
PdfLink(&'a OutputNode),
}
enum BoxPaint<'a> {
pub(super) enum BoxPaint<'a> {
Box(&'a HorizontalBox),
PdfColor,
PdfGraphics,
RowStart(RowLayout),
PdfLink(&'a OutputNode, Option<RowLayout>),
}
#[derive(Clone, Copy)]
pub(super) struct RowLayout {
pub left: i64,
pub baseline: i64,
pub width: i64,
pub height: i64,
pub depth: i64,
pub level: usize,
}
pub(super) enum GeometryItem {
@@ -200,13 +213,14 @@ impl NativeBox {
.into_iter()
.filter_map(|item| match item {
PackedHorizontal::Item(value) => Some(value),
PackedHorizontal::PdfColor | PackedHorizontal::PdfGraphics => None,
PackedHorizontal::PdfColor | PackedHorizontal::PdfGraphics
| PackedHorizontal::PdfLink(_) => None,
})
.collect(),
)
}
fn packed_horizontal(&self) -> Option<Vec<PackedHorizontal>> {
fn packed_horizontal(&self) -> Option<Vec<PackedHorizontal<'_>>> {
let BoxContent::Horizontal(nodes) = &self.content else {
return None;
};
@@ -252,6 +266,10 @@ impl NativeBox {
items.push(PackedHorizontal::PdfGraphics);
continue;
}
TextNode::Output(node) if node.is_pdf_link() => {
items.push(PackedHorizontal::PdfLink(node));
continue;
}
TextNode::Output(node) => {
let Some(image) = node.image_box() else {
continue;
@@ -614,6 +632,7 @@ impl NativeBox {
emit(GeometryItem::PdfGraphics { x, y });
return;
}
BoxPaint::RowStart(_) | BoxPaint::PdfLink(_, _) => return,
};
if let Some(child) = &b.math_geometry {
for p in &child.items {
@@ -646,11 +665,27 @@ impl NativeBox {
}
fn walk(&self, x: i64, y: i64, emit: &mut dyn FnMut(i64, i64, BoxPaint<'_>)) {
self.walk_at(x, y, 0, false, emit);
}
pub(super) fn walk_pdf_links(&self, emit: &mut dyn FnMut(i64, i64, BoxPaint<'_>)) {
self.walk_at(0, 0, 0, true, emit);
}
fn walk_at(&self, x: i64, y: i64, level: usize, links: bool,
emit: &mut dyn FnMut(i64, i64, BoxPaint<'_>)) {
if self.kind.is_unset() {
return;
}
match &self.content {
BoxContent::Horizontal(_) => walk_row(&self.packed_horizontal().unwrap(), x, y, emit),
BoxContent::Horizontal(_) => {
let row = RowLayout { left: x, baseline: y, width: self.width,
height: self.height, depth: self.depth, level };
if links {
emit(x, y, BoxPaint::RowStart(row));
}
walk_row(&self.packed_horizontal().unwrap(), row, links, emit);
}
BoxContent::Vertical(nodes) => {
let mut cursor = y - self.height;
let (mut cumulative, mut previous) = (0, 0);
@@ -661,7 +696,7 @@ impl NativeBox {
continue;
}
cursor += b.height;
b.walk(x + b.shift, cursor, emit);
b.walk_at(x + b.shift, cursor, level + 1, links, emit);
cursor += b.depth;
}
NativeVerticalNode::Glue(g) | NativeVerticalNode::NamedGlue(g, _) => {
@@ -685,6 +720,11 @@ impl NativeBox {
NativeVerticalNode::Output(node) if node.is_pdf_graphics() => {
emit(x, cursor, BoxPaint::PdfGraphics);
}
NativeVerticalNode::Output(node) if node.is_pdf_link() => {
if links {
emit(x, cursor, BoxPaint::PdfLink(node, None));
}
}
NativeVerticalNode::Output(node) => {
if let Some(image) = node.image_box() {
cursor += image.height;
@@ -707,16 +747,18 @@ pub(super) fn bp(n: i64) -> i64 {
}
}
fn walk_row(
items: &[PackedHorizontal],
mut x: i64,
y: i64,
items: &[PackedHorizontal<'_>],
row: RowLayout,
links: bool,
emit: &mut dyn FnMut(i64, i64, BoxPaint<'_>),
) {
let mut x = row.left;
let y = row.baseline;
for item in items {
match item {
PackedHorizontal::Item(HorizontalItem::Box(b)) => {
if let Some(child) = &b.native_box {
child.walk(x, y + child.shift, emit);
child.walk_at(x, y + child.shift, row.level + 1, links, emit);
} else {
emit(x, y, BoxPaint::Box(b));
}
@@ -725,6 +767,7 @@ fn walk_row(
PackedHorizontal::Item(HorizontalItem::Glue { width, .. }) => x += width,
PackedHorizontal::PdfColor => emit(x, y, BoxPaint::PdfColor),
PackedHorizontal::PdfGraphics => emit(x, y, BoxPaint::PdfGraphics),
PackedHorizontal::PdfLink(node) if links => emit(x, y, BoxPaint::PdfLink(node, Some(row))),
_ => {}
}
}