Integrate native repeat boxes and PDF bookmarks
This commit is contained in:
1 parent
5163fc9854
commit
38e4822370
33 files changed
+1208
-143
No files matched your search
@@ -3,6 +3,8 @@ use super::*;
|
||||
use crate::{HorizontalBox, HorizontalItem, Paragraph, ParagraphFont};
|
||||
mod tail;
|
||||
pub(super) use tail::TailNodeRef;
|
||||
mod leaders;
|
||||
pub use leaders::{LeaderGlue, LeaderMode, LeaderPattern};
|
||||
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
|
||||
pub enum BoxKind {
|
||||
@@ -65,6 +67,7 @@ pub enum NativeVerticalNode {
|
||||
Line(Box<NativeBox>),
|
||||
Glue(Glue),
|
||||
NamedGlue(Glue, Parameter),
|
||||
Leaders(LeaderGlue),
|
||||
Kern(i32),
|
||||
/// A normal (engine-generated) kern, distinct from an explicit user kern.
|
||||
ImplicitKern(i32),
|
||||
@@ -90,7 +93,7 @@ impl NativeVerticalNode {
|
||||
Self::Insertion(_) => 4,
|
||||
Self::Mark(_) => 5,
|
||||
Self::Output(_) => 9,
|
||||
Self::Glue(_) | Self::NamedGlue(_, _) => 11,
|
||||
Self::Glue(_) | Self::NamedGlue(_, _) | Self::Leaders(_) => 11,
|
||||
Self::Kern(_) | Self::ImplicitKern(_) => 12,
|
||||
Self::Penalty(_) => 13,
|
||||
}
|
||||
@@ -109,6 +112,7 @@ impl NativeVerticalNode {
|
||||
Self::Box(b) => b.node_count(),
|
||||
Self::Line(b) => b.node_count(),
|
||||
Self::Insertion(n) => 1 + n.nodes.iter().map(Self::node_count).sum::<usize>(),
|
||||
Self::Leaders(leader) => 1 + leader.pattern.node_count(),
|
||||
_ => 1,
|
||||
}
|
||||
}
|
||||
@@ -130,6 +134,7 @@ fn text_node_count(nodes: &[TextNode]) -> usize {
|
||||
TextNode::Insertion(value) => {
|
||||
value.nodes.iter().map(NativeVerticalNode::node_count).sum()
|
||||
}
|
||||
TextNode::Leaders(leader) => leader.pattern.node_count(),
|
||||
_ => 0,
|
||||
}
|
||||
})
|
||||
@@ -137,9 +142,8 @@ fn text_node_count(nodes: &[TextNode]) -> usize {
|
||||
}
|
||||
enum PackedHorizontal<'a> {
|
||||
Item(HorizontalItem),
|
||||
PdfColor,
|
||||
PdfGraphics,
|
||||
PdfLink(&'a OutputNode),
|
||||
Leaders(&'a LeaderGlue, i64),
|
||||
Output(&'a OutputNode),
|
||||
}
|
||||
|
||||
pub(super) enum BoxPaint<'a> {
|
||||
@@ -148,6 +152,47 @@ pub(super) enum BoxPaint<'a> {
|
||||
PdfGraphics,
|
||||
RowStart(RowLayout),
|
||||
PdfLink(&'a OutputNode, Option<RowLayout>),
|
||||
Output(&'a OutputNode),
|
||||
}
|
||||
|
||||
struct WalkBudget<'a> {
|
||||
remaining: usize,
|
||||
cancellation: Option<&'a Cancellation>,
|
||||
leader_depth: usize,
|
||||
}
|
||||
|
||||
impl WalkBudget<'_> {
|
||||
fn for_geometry() -> Self {
|
||||
Self {
|
||||
remaining: usize::MAX,
|
||||
cancellation: None,
|
||||
leader_depth: 0,
|
||||
}
|
||||
}
|
||||
|
||||
fn charge(&mut self, count: usize) -> Result<(), &'static str> {
|
||||
if self.cancellation.is_some_and(Cancellation::is_cancelled) {
|
||||
return Err("native compilation cancelled");
|
||||
}
|
||||
self.remaining = self
|
||||
.remaining
|
||||
.checked_sub(count)
|
||||
.ok_or("native leader placement or material capacity exceeded")?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn inside_leader(&mut self, count: usize) -> Result<(), &'static str> {
|
||||
if self.leader_depth > 0 {
|
||||
self.charge(count)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) struct LeaderEffects {
|
||||
pub outputs: Vec<OutputNode>,
|
||||
pub graphics_positions: Vec<[i64; 2]>,
|
||||
pub work: usize,
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy)]
|
||||
@@ -167,8 +212,8 @@ pub(super) enum GeometryItem {
|
||||
}
|
||||
|
||||
impl NativeBox {
|
||||
pub(super) fn output_nodes(&self) -> Vec<&OutputNode> {
|
||||
fn walk<'a>(b: &'a NativeBox, out: &mut Vec<&'a OutputNode>) {
|
||||
pub(super) fn output_nodes(&self) -> (Vec<&OutputNode>, bool) {
|
||||
fn walk<'a>(b: &'a NativeBox, out: &mut Vec<&'a OutputNode>, leaders: &mut bool) {
|
||||
if b.kind.is_unset() {
|
||||
return;
|
||||
}
|
||||
@@ -177,11 +222,12 @@ impl NativeBox {
|
||||
for n in nodes {
|
||||
match n {
|
||||
TextNode::Output(o) => out.push(o),
|
||||
TextNode::Leaders(_) => *leaders = true,
|
||||
TextNode::Box(b)
|
||||
| TextNode::MathChar(b)
|
||||
| TextNode::Glyph { glyph: b, .. } => {
|
||||
if let Some(b) = &b.native_box {
|
||||
walk(b, out)
|
||||
walk(b, out, leaders)
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
@@ -192,8 +238,9 @@ impl NativeBox {
|
||||
for n in nodes {
|
||||
match n {
|
||||
NativeVerticalNode::Output(o) => out.push(o),
|
||||
NativeVerticalNode::Leaders(_) => *leaders = true,
|
||||
NativeVerticalNode::Box(b) | NativeVerticalNode::Line(b) => {
|
||||
walk(b, out)
|
||||
walk(b, out, leaders)
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
@@ -202,8 +249,48 @@ impl NativeBox {
|
||||
}
|
||||
}
|
||||
let mut out = Vec::new();
|
||||
walk(self, &mut out);
|
||||
out
|
||||
let mut leaders = false;
|
||||
walk(self, &mut out, &mut leaders);
|
||||
(out, leaders)
|
||||
}
|
||||
|
||||
pub(super) fn leader_effects(
|
||||
&self,
|
||||
limit: usize,
|
||||
cancellation: Option<&Cancellation>,
|
||||
) -> Result<LeaderEffects, &'static str> {
|
||||
let mut outputs = Vec::new();
|
||||
let mut graphics_positions = Vec::new();
|
||||
let mut budget = WalkBudget {
|
||||
remaining: limit,
|
||||
cancellation,
|
||||
leader_depth: 0,
|
||||
};
|
||||
self.walk_at(0, 0, 0, false, &mut budget, &mut |x, y, item| {
|
||||
if let BoxPaint::Output(o) = item {
|
||||
if o.is_pdf_graphics() {
|
||||
graphics_positions.push([x, y]);
|
||||
}
|
||||
outputs.push(o.clone());
|
||||
}
|
||||
})?;
|
||||
Ok(LeaderEffects {
|
||||
outputs,
|
||||
graphics_positions,
|
||||
work: limit - budget.remaining,
|
||||
})
|
||||
}
|
||||
|
||||
pub(super) fn contains_output(&self) -> Result<bool, &'static str> {
|
||||
let (nodes, leaders) = self.output_nodes();
|
||||
if !nodes.is_empty() {
|
||||
return Ok(true);
|
||||
}
|
||||
Ok(leaders
|
||||
&& !self
|
||||
.leader_effects(Limits::default().tokens, None)?
|
||||
.outputs
|
||||
.is_empty())
|
||||
}
|
||||
/// Derive positioned material from the original owned list and glue set.
|
||||
/// The box never stores a second recursive copy of its children for paint.
|
||||
@@ -213,8 +300,13 @@ impl NativeBox {
|
||||
.into_iter()
|
||||
.filter_map(|item| match item {
|
||||
PackedHorizontal::Item(value) => Some(value),
|
||||
PackedHorizontal::PdfColor | PackedHorizontal::PdfGraphics
|
||||
| PackedHorizontal::PdfLink(_) => None,
|
||||
PackedHorizontal::Leaders(_, width) => Some(HorizontalItem::Glue {
|
||||
width,
|
||||
stretch: 0,
|
||||
shrink: 0,
|
||||
order: 0,
|
||||
}),
|
||||
PackedHorizontal::Output(node) => node.image_box().map(HorizontalItem::Box),
|
||||
})
|
||||
.collect(),
|
||||
)
|
||||
@@ -256,25 +348,16 @@ impl NativeBox {
|
||||
shrink: 0,
|
||||
order: 0,
|
||||
},
|
||||
TextNode::Output(node)
|
||||
if matches!(node.operation, OutputOperation::PdfColor { .. }) =>
|
||||
{
|
||||
items.push(PackedHorizontal::PdfColor);
|
||||
continue;
|
||||
}
|
||||
TextNode::Output(node) if node.is_pdf_graphics() => {
|
||||
items.push(PackedHorizontal::PdfGraphics);
|
||||
continue;
|
||||
}
|
||||
TextNode::Output(node) if node.is_pdf_link() => {
|
||||
items.push(PackedHorizontal::PdfLink(node));
|
||||
TextNode::Leaders(leader) => {
|
||||
let g = leader.glue;
|
||||
let width = i64::from(g.width)
|
||||
+ self.setting.adjustment(g, &mut cumulative, &mut previous);
|
||||
items.push(PackedHorizontal::Leaders(leader, width));
|
||||
continue;
|
||||
}
|
||||
TextNode::Output(node) => {
|
||||
let Some(image) = node.image_box() else {
|
||||
continue;
|
||||
};
|
||||
HorizontalItem::Box(image)
|
||||
items.push(PackedHorizontal::Output(node));
|
||||
continue;
|
||||
}
|
||||
_ => continue,
|
||||
};
|
||||
@@ -443,6 +526,13 @@ pub(super) fn vpack(
|
||||
depth = 0;
|
||||
flex(*g, &mut stretch, &mut shrink);
|
||||
}
|
||||
NativeVerticalNode::Leaders(leader) => {
|
||||
let g = leader.glue;
|
||||
height += depth + i64::from(g.width);
|
||||
depth = 0;
|
||||
width = width.max(leader.pattern_dimensions().0);
|
||||
flex(g, &mut stretch, &mut shrink);
|
||||
}
|
||||
NativeVerticalNode::Kern(n) | NativeVerticalNode::ImplicitKern(n) => {
|
||||
height += depth + *n as i64;
|
||||
depth = 0;
|
||||
@@ -527,6 +617,13 @@ pub(super) fn hpack(
|
||||
width += g.width as i64;
|
||||
flex(*g, &mut stretch, &mut shrink);
|
||||
}
|
||||
TextNode::Leaders(leader) => {
|
||||
width += i64::from(leader.glue.width);
|
||||
let (_, h, d) = leader.pattern_dimensions();
|
||||
height = height.max(h);
|
||||
depth = depth.max(d);
|
||||
flex(leader.glue, &mut stretch, &mut shrink);
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
@@ -597,7 +694,8 @@ impl NativeBox {
|
||||
font: crate::render::FontFace::Roman,
|
||||
});
|
||||
}
|
||||
});
|
||||
})
|
||||
.expect("paint traversal of an already measured list");
|
||||
}
|
||||
/// Walk original packed lists in TeX sp, before the single display export.
|
||||
pub fn geometry(&self) -> Result<crate::math_engine_tex::MathGeometry, String> {
|
||||
@@ -632,7 +730,7 @@ impl NativeBox {
|
||||
emit(GeometryItem::PdfGraphics { x, y });
|
||||
return;
|
||||
}
|
||||
BoxPaint::RowStart(_) | BoxPaint::PdfLink(_, _) => return,
|
||||
BoxPaint::RowStart(_) | BoxPaint::PdfLink(_, _) | BoxPaint::Output(_) => return,
|
||||
};
|
||||
if let Some(child) = &b.math_geometry {
|
||||
for p in &child.items {
|
||||
@@ -646,7 +744,7 @@ impl NativeBox {
|
||||
} else if b.character.is_some() {
|
||||
unsupported = true;
|
||||
}
|
||||
});
|
||||
})?;
|
||||
if unsupported {
|
||||
Err("native math box requires actual native glyph geometry".into())
|
||||
} else {
|
||||
@@ -660,49 +758,100 @@ impl NativeBox {
|
||||
if matches!(node, BoxPaint::PdfGraphics) {
|
||||
all.push([x, y]);
|
||||
}
|
||||
});
|
||||
})
|
||||
.expect("graphics traversal of an already measured list");
|
||||
all
|
||||
}
|
||||
|
||||
fn walk(&self, x: i64, y: i64, emit: &mut dyn FnMut(i64, i64, BoxPaint<'_>)) {
|
||||
self.walk_at(x, y, 0, false, emit);
|
||||
fn walk(
|
||||
&self,
|
||||
x: i64,
|
||||
y: i64,
|
||||
emit: &mut dyn FnMut(i64, i64, BoxPaint<'_>),
|
||||
) -> Result<(), &'static str> {
|
||||
self.walk_at(x, y, 0, false, &mut WalkBudget::for_geometry(), emit)
|
||||
}
|
||||
|
||||
pub(super) fn walk_pdf_links(&self, emit: &mut dyn FnMut(i64, i64, BoxPaint<'_>)) {
|
||||
self.walk_at(0, 0, 0, true, emit);
|
||||
self.walk_at(0, 0, 0, true, &mut WalkBudget::for_geometry(), emit)
|
||||
.expect("link traversal of an already measured list");
|
||||
}
|
||||
|
||||
fn walk_at(&self, x: i64, y: i64, level: usize, links: bool,
|
||||
emit: &mut dyn FnMut(i64, i64, BoxPaint<'_>)) {
|
||||
fn walk_at(
|
||||
&self,
|
||||
x: i64,
|
||||
y: i64,
|
||||
level: usize,
|
||||
links: bool,
|
||||
budget: &mut WalkBudget<'_>,
|
||||
emit: &mut dyn FnMut(i64, i64, BoxPaint<'_>),
|
||||
) -> Result<(), &'static str> {
|
||||
if self.kind.is_unset() {
|
||||
return;
|
||||
return Ok(());
|
||||
}
|
||||
match &self.content {
|
||||
BoxContent::Horizontal(_) => {
|
||||
BoxContent::Horizontal(nodes) => {
|
||||
budget.inside_leader(nodes.len())?;
|
||||
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);
|
||||
walk_row(&self.packed_horizontal().unwrap(), row, links, budget, emit)?;
|
||||
}
|
||||
BoxContent::Vertical(nodes) => {
|
||||
let mut cursor = y - self.height;
|
||||
let (mut cumulative, mut previous) = (0, 0);
|
||||
for node in nodes {
|
||||
budget.inside_leader(1)?;
|
||||
match node {
|
||||
NativeVerticalNode::Box(b) | NativeVerticalNode::Line(b) => {
|
||||
if b.kind.is_unset() {
|
||||
continue;
|
||||
}
|
||||
cursor += b.height;
|
||||
b.walk_at(x + b.shift, cursor, level + 1, links, emit);
|
||||
b.walk_at(x + b.shift, cursor, level + 1, links, budget, emit)?;
|
||||
cursor += b.depth;
|
||||
}
|
||||
NativeVerticalNode::Glue(g) | NativeVerticalNode::NamedGlue(g, _) => {
|
||||
cursor += i64::from(g.width)
|
||||
+ self.setting.adjustment(*g, &mut cumulative, &mut previous)
|
||||
}
|
||||
NativeVerticalNode::Leaders(leader) => {
|
||||
let g = leader.glue;
|
||||
let length = i64::from(g.width)
|
||||
+ self.setting.adjustment(g, &mut cumulative, &mut previous);
|
||||
match &leader.pattern {
|
||||
LeaderPattern::Box(b) => {
|
||||
for pos in leader.positions(
|
||||
y - self.height,
|
||||
cursor,
|
||||
length,
|
||||
b.height + b.depth,
|
||||
) {
|
||||
b.walk_leader(
|
||||
x + b.shift,
|
||||
pos + b.height,
|
||||
level + 1,
|
||||
links,
|
||||
budget,
|
||||
emit,
|
||||
)?;
|
||||
}
|
||||
}
|
||||
LeaderPattern::Rule(r) if length > 0 => {
|
||||
let r = NativeRule {
|
||||
height: Some(length as i32),
|
||||
depth: Some(0),
|
||||
..*r
|
||||
};
|
||||
let paint = r.horizontal_item(self.width, 0, 0);
|
||||
emit(x, cursor + length, BoxPaint::Box(&paint));
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
cursor += length;
|
||||
}
|
||||
NativeVerticalNode::Kern(n) | NativeVerticalNode::ImplicitKern(n) => {
|
||||
cursor += i64::from(*n)
|
||||
}
|
||||
@@ -712,20 +861,8 @@ impl NativeBox {
|
||||
emit(x, cursor, BoxPaint::Box(&b));
|
||||
cursor += b.depth;
|
||||
}
|
||||
NativeVerticalNode::Output(node)
|
||||
if matches!(node.operation, OutputOperation::PdfColor { .. }) =>
|
||||
{
|
||||
emit(x, cursor, BoxPaint::PdfColor);
|
||||
}
|
||||
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) => {
|
||||
walk_output(node, x, cursor, None, links, emit);
|
||||
if let Some(image) = node.image_box() {
|
||||
cursor += image.height;
|
||||
emit(x, cursor, BoxPaint::Box(&image));
|
||||
@@ -737,6 +874,28 @@ impl NativeBox {
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn walk_leader(
|
||||
&self,
|
||||
x: i64,
|
||||
y: i64,
|
||||
level: usize,
|
||||
links: bool,
|
||||
budget: &mut WalkBudget<'_>,
|
||||
emit: &mut dyn FnMut(i64, i64, BoxPaint<'_>),
|
||||
) -> Result<(), &'static str> {
|
||||
budget.charge(1)?;
|
||||
budget.leader_depth += 1;
|
||||
let result = self.walk_at(x, y, level, links, budget, &mut |x, y, paint| {
|
||||
if !matches!(paint, BoxPaint::Output(o) if matches!(o.operation,
|
||||
OutputOperation::Open { .. } | OutputOperation::Write { .. } | OutputOperation::Close { .. })) {
|
||||
emit(x, y, paint);
|
||||
}
|
||||
});
|
||||
budget.leader_depth -= 1;
|
||||
result
|
||||
}
|
||||
}
|
||||
pub(super) fn bp(n: i64) -> i64 {
|
||||
@@ -750,27 +909,73 @@ fn walk_row(
|
||||
items: &[PackedHorizontal<'_>],
|
||||
row: RowLayout,
|
||||
links: bool,
|
||||
budget: &mut WalkBudget<'_>,
|
||||
emit: &mut dyn FnMut(i64, i64, BoxPaint<'_>),
|
||||
) {
|
||||
) -> Result<(), &'static str> {
|
||||
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_at(x, y + child.shift, row.level + 1, links, emit);
|
||||
child.walk_at(x, y + child.shift, row.level + 1, links, budget, emit)?;
|
||||
} else {
|
||||
if let Some(geometry) = &b.math_geometry {
|
||||
budget.inside_leader(geometry.items.len())?;
|
||||
}
|
||||
emit(x, y, BoxPaint::Box(b));
|
||||
}
|
||||
x += b.width;
|
||||
}
|
||||
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))),
|
||||
PackedHorizontal::Leaders(leader, width) => {
|
||||
match &leader.pattern {
|
||||
LeaderPattern::Box(b) => {
|
||||
for pos in leader.positions(row.left, x, *width, b.width) {
|
||||
b.walk_leader(pos, y + b.shift, row.level + 1, links, budget, emit)?;
|
||||
}
|
||||
}
|
||||
LeaderPattern::Rule(r) if *width > 0 => {
|
||||
let r = NativeRule {
|
||||
width: Some(*width as i32),
|
||||
..*r
|
||||
};
|
||||
let paint = r.horizontal_item(row.width, row.height, row.depth);
|
||||
emit(x, y, BoxPaint::Box(&paint));
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
x += width;
|
||||
}
|
||||
PackedHorizontal::Output(node) => {
|
||||
walk_output(node, x, y, Some(row), links, emit);
|
||||
if let Some(image) = node.image_box() {
|
||||
emit(x, y, BoxPaint::Box(&image));
|
||||
x += image.width;
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn walk_output(
|
||||
node: &OutputNode,
|
||||
x: i64,
|
||||
y: i64,
|
||||
row: Option<RowLayout>,
|
||||
links: bool,
|
||||
emit: &mut dyn FnMut(i64, i64, BoxPaint<'_>),
|
||||
) {
|
||||
emit(x, y, BoxPaint::Output(node));
|
||||
if matches!(node.operation, OutputOperation::PdfColor { .. }) {
|
||||
emit(x, y, BoxPaint::PdfColor);
|
||||
} else if node.is_pdf_graphics() {
|
||||
emit(x, y, BoxPaint::PdfGraphics);
|
||||
} else if links && node.is_pdf_link() {
|
||||
emit(x, y, BoxPaint::PdfLink(node, row));
|
||||
}
|
||||
}
|
||||
pub(super) fn box_paragraph(mut b: NativeBox) -> Paragraph {
|
||||
let (width, height, depth, shift) = (b.width, b.height, b.depth, b.shift);
|
||||
|
||||
Reference in new issue
Block a user