Integrate native repeat boxes and PDF bookmarks

This commit is contained in:
gloamxun committed 2026-11-19 16:06:28 +08:00
1 parent 5163fc9854
commit 38e4822370
33 files changed
+1208 -143

No files matched your search

@@ -817,14 +817,20 @@ fn set_alignment_cell(
match &cell.content {
BoxContent::Horizontal(v) => {
for n in v {
if let TextNode::Glue(g) | TextNode::NamedGlue(g, _) = n {
if let TextNode::Glue(g)
| TextNode::NamedGlue(g, _)
| TextNode::Leaders(LeaderGlue { glue: g, .. }) = n
{
add(*g)
}
}
}
BoxContent::Vertical(v) => {
for n in v {
if let NativeVerticalNode::Glue(g) | NativeVerticalNode::NamedGlue(g, _) = n {
if let NativeVerticalNode::Glue(g)
| NativeVerticalNode::NamedGlue(g, _)
| NativeVerticalNode::Leaders(LeaderGlue { glue: g, .. }) = n
{
add(*g)
}
}
@@ -985,14 +991,20 @@ fn mark_unset(b: &mut NativeBox, span: u8) -> Result<(), String> {
match &b.content {
BoxContent::Horizontal(v) => {
for n in v {
if let TextNode::Glue(g) | TextNode::NamedGlue(g, _) = n {
if let TextNode::Glue(g)
| TextNode::NamedGlue(g, _)
| TextNode::Leaders(LeaderGlue { glue: g, .. }) = n
{
add(*g);
}
}
}
BoxContent::Vertical(v) => {
for n in v {
if let NativeVerticalNode::Glue(g) | NativeVerticalNode::NamedGlue(g, _) = n {
if let NativeVerticalNode::Glue(g)
| NativeVerticalNode::NamedGlue(g, _)
| NativeVerticalNode::Leaders(LeaderGlue { glue: g, .. }) = n
{
add(*g);
}
}
@@ -6,6 +6,7 @@ pub(super) enum Destination {
Register(u16, bool),
Shift(i32),
Shipout,
Leaders(LeaderMode),
Adjustment { pre: bool },
Insertion { class: u8 },
}
@@ -340,6 +341,74 @@ impl Executor {
.ok_or_else(|| self.error("expected native box constructor", token.span))?;
self.box_command(p, destination, token.span, files)
}
pub(super) fn scan_leaders(
&mut self,
primitive: Primitive,
files: &mut impl SourceProvider,
) -> Result<(), Error> {
let mode = match primitive {
Primitive::Leaders => LeaderMode::Aligned,
Primitive::Cleaders => LeaderMode::Centered,
Primitive::Xleaders => LeaderMode::Expanded,
_ => unreachable!("leader primitive"),
};
let token = loop {
let t = self.expanded_non_space(files)?.token;
if self.primitive(&t) != Some(Primitive::Relax) {
break t;
}
};
match self.primitive(&token) {
Some(rule @ (Primitive::Hrule | Primitive::Vrule)) => {
let rule =
self.scan_rule(rule == Primitive::Hrule, TokenOrigin::from(&token), files)?;
self.finish_leaders(mode, Some(LeaderPattern::Rule(rule)), files)
}
Some(p) => self.box_command(p, Destination::Leaders(mode), token.span, files),
None => self.fail("a box or rule is required for native leaders"),
}
}
fn finish_leaders(
&mut self,
mode: LeaderMode,
pattern: Option<LeaderPattern>,
files: &mut impl SourceProvider,
) -> Result<(), Error> {
use Primitive::*;
let vertical = self.paragraph.is_none() && self.math.is_empty();
let token = self.expanded_non_space(files)?.token;
let glue = match self.primitive(&token) {
Some(p @ (Vskip | Vfil | Vfill | Vss | Vfilneg)) if vertical => {
self.skip_glue(p, files)?
}
Some(p @ (Hskip | Hfil | Hfill | Hss | Hfilneg)) if !vertical => {
self.skip_glue(p, files)?
}
_ => return Err(self.error("leaders not followed by proper glue", token.span)),
};
let leader = pattern.map(|pattern| LeaderGlue {
glue,
mode,
pattern,
});
if let Some(math) = self.math.last_mut() {
math.nodes
.push(leader.map_or(MathNode::ExplicitSpace(glue, None), MathNode::Leaders));
} else if let Some(paragraph) = self.paragraph.as_mut() {
paragraph
.nodes
.push(leader.map_or(TextNode::Glue(glue), TextNode::Leaders));
} else {
let start = self.vertical.nodes.len();
self.vertical
.push(leader.map_or(NativeVerticalNode::Glue(glue), NativeVerticalNode::Leaders));
self.emit_vertical(start);
self.request_page();
}
Ok(())
}
pub(super) fn box_command(
&mut self,
p: Primitive,
@@ -853,6 +922,9 @@ impl Executor {
self.pending_shipouts.push_back(b);
}
}
Destination::Leaders(mode) => {
self.finish_leaders(mode, b.map(|b| LeaderPattern::Box(Box::new(b))), files)?;
}
Destination::Append | Destination::Shift(_) => {
if let Some(b) = b {
if !self.math.is_empty() {
@@ -152,7 +152,10 @@ impl Executor {
if b.width + q > width {
let mut shrink = [0i64; 4];
for node in &material {
if let TextNode::Glue(g) | TextNode::NamedGlue(g, _) = node {
if let TextNode::Glue(g)
| TextNode::NamedGlue(g, _)
| TextNode::Leaders(LeaderGlue { glue: g, .. }) = node
{
shrink[usize::from(g.shrink.order)] += i64::from(g.shrink.amount);
}
}
@@ -177,7 +180,7 @@ impl Executor {
displacement = (width - b.width - e + 1).div_euclid(2);
if matches!(
material.first(),
Some(TextNode::Glue(_) | TextNode::NamedGlue(_, _))
Some(TextNode::Glue(_) | TextNode::NamedGlue(_, _) | TextNode::Leaders(_))
) {
displacement = 0;
}
@@ -17,7 +17,9 @@ impl Tail {
}
fn text(node: &TextNode) -> Self {
match node {
TextNode::Glue(g) | TextNode::NamedGlue(g, _) => Self::Glue(*g, false),
TextNode::Glue(g)
| TextNode::NamedGlue(g, _)
| TextNode::Leaders(LeaderGlue { glue: g, .. }) => Self::Glue(*g, false),
TextNode::Kern { width, .. } => Self::Kern(*width),
TextNode::Penalty(n) => Self::Penalty(*n),
TextNode::Glyph { ligature, .. } => Self::Other(if *ligature { 7 } else { 0 }),
@@ -43,9 +45,9 @@ impl Tail {
}
fn vertical(node: &NativeVerticalNode) -> Self {
match node {
NativeVerticalNode::Glue(g) | NativeVerticalNode::NamedGlue(g, _) => {
Self::Glue(*g, false)
}
NativeVerticalNode::Glue(g)
| NativeVerticalNode::NamedGlue(g, _)
| NativeVerticalNode::Leaders(LeaderGlue { glue: g, .. }) => Self::Glue(*g, false),
NativeVerticalNode::Kern(n) | NativeVerticalNode::ImplicitKern(n) => Self::Kern(*n),
NativeVerticalNode::Penalty(n) => Self::Penalty(*n),
_ => Self::Other(node.node_type()),
@@ -53,6 +55,7 @@ impl Tail {
}
fn math(node: &MathNode) -> Self {
match node {
MathNode::Leaders(leader) => Self::Glue(leader.glue, false),
MathNode::Glue(g) => Self::Glue(*g, true),
MathNode::ExplicitSpace(g, _) => Self::Glue(*g, false),
MathNode::Kern { amount, .. } => Self::Kern(*amount),
@@ -547,7 +547,7 @@ impl Executor {
Vcenter => self.box_command(*p, boxes::Destination::Append, token.span, files)?,
Hbox | Vbox | Vtop | Box | Copy | Setbox | Wd | Ht | Dp | Lastbox | Raise
| Lower | Vsplit | Mark | Marks | Vadjust | Insert | Shipout | Openout
| Closeout | Write | Pdfcolorstack | Pdfsave | Pdfsetmatrix | Pdfrestore | Pdfcatalog
| Closeout | Write | Pdfcolorstack | Pdfsave | Pdfsetmatrix | Pdfrestore | Pdfcatalog | Pdfoutline
| Ignorespaces | Unskip | Unkern | Unpenalty => {
return Ok(false);
}
@@ -690,6 +690,7 @@ impl Executor {
.nodes
.push(MathNode::ExplicitSpace(g, None));
}
Leaders | Cleaders | Xleaders => self.scan_leaders(*p, files)?,
Mskip => {
let g = self.skip_glue(*p, files)?;
self.math.last_mut().unwrap().nodes.push(MathNode::Glue(g));
@@ -272,10 +272,29 @@ impl Executor {
let mut pdf_color_steps = Vec::new();
let mut pdf_graphics_steps = Vec::new();
let mut graphics_stack = Vec::new();
let outputs = b.output_nodes();
let (mut outputs, has_leaders) = b.output_nodes();
let leader_effects = if has_leaders {
let limit = self
.limits
.steps
.saturating_sub(self.steps)
.min(self.limits.tokens);
let effects = b
.leader_effects(limit, self.cancellation.as_ref())
.map_err(|error| self.error(error, b.span))?;
self.steps += effects.work;
Some(effects)
} else {
None
};
if let Some(effects) = &leader_effects {
outputs = effects.outputs.iter().collect();
}
let mut link_depth = self.pdf_link_stack.len();
let has_link_material = link_depth != 0 || outputs.iter().any(|node| node.is_pdf_link());
let mut graphics_positions = if outputs.iter().any(|node| node.is_pdf_graphics()) {
let mut graphics_positions = if let Some(effects) = &leader_effects {
effects.graphics_positions.clone().into_iter()
} else if outputs.iter().any(|node| node.is_pdf_graphics()) {
b.pdf_graphics_positions().into_iter()
} else {
Vec::new().into_iter()
@@ -1,6 +1,6 @@
use super::*;
use crate::pdf_device::{
catalog::{CatalogAction, CatalogTarget},
catalog::{CatalogAction, CatalogTarget, PdfOutline},
links::DestinationId,
};
@@ -204,7 +204,49 @@ impl Executor {
Ok(())
}
pub(super) fn scan_pdf_outline(&mut self, files: &mut impl SourceProvider) -> Result<(), Error> {
if self.read_parameter(Parameter::Pdfoutput).width() <= 0 {
return self.fail("\\pdfoutline not allowed in DVI mode");
}
let attributes = if self.keyword("attr", files)? {
self.scan_pdf_tokens(files)?
} else {
Vec::new()
};
let action = self.scan_catalog_action(files)?;
let count = if self.keyword("count", files)? {
self.integer(files)?
} else {
0
};
let title = self.scan_pdf_bytes(files)?;
let payload = 1 + title.len() + attributes.len() + action.payload_len();
if payload.saturating_add(self.pdf_outline_payload) > self.limits.tokens {
return self.fail("native PDF outline material limit exceeded");
}
self.pdf_page_data(files)?;
self.pdf_outline_payload += payload;
self.pdf_outlines.push(PdfOutline { title, attributes, action, count });
Ok(())
}
pub(super) fn finish_pdf_dictionaries(&mut self) -> Result<(), Error> {
if !self.pdf_outlines.is_empty() {
let pending = std::mem::take(&mut self.pdf_outlines);
let mut outlines = Vec::with_capacity(pending.len());
for PdfOutline { title, attributes, action, count } in pending {
outlines.push(PdfOutline {
title,
attributes: self.pdf_token_bytes(&attributes)?,
action,
count,
});
}
self.state
.pdf_catalog
.get_or_insert_with(Default::default)
.outlines.extend(outlines);
}
if !self.pdf_catalog_tokens.is_empty() {
let tokens = std::mem::take(&mut self.pdf_catalog_tokens);
let bytes = self.pdf_token_bytes(&tokens)?;
@@ -88,7 +88,8 @@ impl Executor {
if self.after_assignment.is_some() {
c.usage.token_list_cells += 1;
}
c.usage.token_list_cells += self.pdf_catalog_tokens.len();
c.usage.token_list_cells += self.pdf_catalog_tokens.len()
+ self.pdf_outlines.iter().map(|entry| entry.attributes.len()).sum::<usize>();
for input in &self.inputs {
match input {
InputFrame::Source { mouth, .. } => {