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);
|
||||
|
||||
@@ -0,0 +1,74 @@
|
||||
//! Owned leader glue. The pattern is kept once even when it paints many times.
|
||||
use super::*;
|
||||
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
|
||||
pub enum LeaderMode {
|
||||
Aligned,
|
||||
Centered,
|
||||
Expanded,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
|
||||
pub enum LeaderPattern {
|
||||
Box(Box<NativeBox>),
|
||||
Rule(NativeRule),
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
|
||||
pub struct LeaderGlue {
|
||||
pub glue: Glue,
|
||||
pub mode: LeaderMode,
|
||||
pub pattern: LeaderPattern,
|
||||
}
|
||||
|
||||
impl LeaderGlue {
|
||||
pub(super) fn pattern_dimensions(&self) -> (i64, i64, i64) {
|
||||
match &self.pattern {
|
||||
LeaderPattern::Box(b) => (b.width, b.height - b.shift, b.depth + b.shift),
|
||||
LeaderPattern::Rule(r) => r.dimensions(),
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) fn positions(
|
||||
&self,
|
||||
origin: i64,
|
||||
start: i64,
|
||||
available: i64,
|
||||
tile: i64,
|
||||
) -> impl Iterator<Item = i64> {
|
||||
let (first, stride, count) = if tile <= 0 || available <= 0 {
|
||||
(start, 0, 0)
|
||||
} else {
|
||||
// TeX includes 10 sp in its fit check to absorb glue rounding.
|
||||
// The caller still advances by the original adjusted glue length.
|
||||
let length = available + 10;
|
||||
let copies = length / tile;
|
||||
let remaining = length % tile;
|
||||
let (offset, gap) = match self.mode {
|
||||
LeaderMode::Aligned => ((origin - start).rem_euclid(tile), 0),
|
||||
LeaderMode::Centered => (remaining / 2, 0),
|
||||
LeaderMode::Expanded => {
|
||||
let gap = remaining / (copies + 1);
|
||||
((remaining - (copies - 1) * gap) / 2, gap)
|
||||
}
|
||||
};
|
||||
let stride = tile + gap;
|
||||
let count = if length - offset >= tile {
|
||||
1 + (length - offset - tile) / stride
|
||||
} else {
|
||||
0
|
||||
};
|
||||
(start + offset, stride, count)
|
||||
};
|
||||
(0..count).map(move |copy| first + copy * stride)
|
||||
}
|
||||
}
|
||||
|
||||
impl LeaderPattern {
|
||||
pub(in crate::execution) fn node_count(&self) -> usize {
|
||||
match self {
|
||||
Self::Box(b) => b.node_count(),
|
||||
Self::Rule(_) => 1,
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -80,6 +80,7 @@ fn horizontal(n: NativeVerticalNode) -> TextNode {
|
||||
}),
|
||||
NativeVerticalNode::Glue(g) => TextNode::Glue(g),
|
||||
NativeVerticalNode::NamedGlue(g, p) => TextNode::NamedGlue(g, p),
|
||||
NativeVerticalNode::Leaders(l) => TextNode::Leaders(l),
|
||||
NativeVerticalNode::Kern(width) => TextNode::Kern {
|
||||
width,
|
||||
explicit: true,
|
||||
|
||||
@@ -15,6 +15,11 @@ pub enum DiagnosticNode {
|
||||
value: Glue,
|
||||
name: Option<Parameter>,
|
||||
},
|
||||
Leaders {
|
||||
value: Glue,
|
||||
mode: LeaderMode,
|
||||
pattern: Box<DiagnosticNode>,
|
||||
},
|
||||
Kern {
|
||||
width: i32,
|
||||
explicit: bool,
|
||||
@@ -74,6 +79,18 @@ pub(super) fn box_view(b: &NativeBox) -> DiagnosticBox {
|
||||
nodes,
|
||||
}
|
||||
}
|
||||
|
||||
fn leader_node(leader: &LeaderGlue) -> DiagnosticNode {
|
||||
let pattern = match &leader.pattern {
|
||||
LeaderPattern::Box(b) => DiagnosticNode::Box(box_view(b)),
|
||||
LeaderPattern::Rule(r) => DiagnosticNode::Rule(*r),
|
||||
};
|
||||
DiagnosticNode::Leaders {
|
||||
value: leader.glue,
|
||||
mode: leader.mode,
|
||||
pattern: Box::new(pattern),
|
||||
}
|
||||
}
|
||||
fn vertical_nodes(nodes: &[NativeVerticalNode]) -> Vec<DiagnosticNode> {
|
||||
nodes
|
||||
.iter()
|
||||
@@ -88,6 +105,7 @@ fn vertical_nodes(nodes: &[NativeVerticalNode]) -> Vec<DiagnosticNode> {
|
||||
value: *g,
|
||||
name: Some(*p),
|
||||
},
|
||||
NativeVerticalNode::Leaders(leader) => leader_node(leader),
|
||||
NativeVerticalNode::Kern(k) => DiagnosticNode::Kern {
|
||||
width: *k,
|
||||
explicit: true,
|
||||
@@ -190,6 +208,7 @@ pub(super) fn text_nodes(nodes: &[TextNode]) -> Vec<DiagnosticNode> {
|
||||
value: *g,
|
||||
name: None,
|
||||
},
|
||||
TextNode::Leaders(leader) => leader_node(leader),
|
||||
TextNode::Kern { width, explicit } => DiagnosticNode::Kern {
|
||||
width: *width,
|
||||
explicit: *explicit,
|
||||
@@ -435,6 +454,23 @@ impl Printer<'_> {
|
||||
self.number(*w as i64);
|
||||
}
|
||||
DiagnosticNode::Glue { value, name } => self.glue(*value, *name),
|
||||
DiagnosticNode::Leaders {
|
||||
value,
|
||||
mode,
|
||||
pattern,
|
||||
} => {
|
||||
self.command(match mode {
|
||||
LeaderMode::Aligned => "leaders",
|
||||
LeaderMode::Centered => "cleaders",
|
||||
LeaderMode::Expanded => "xleaders",
|
||||
});
|
||||
self.literal(" ");
|
||||
self.glue_spec(*value);
|
||||
self.nodes(
|
||||
std::slice::from_ref(pattern.as_ref()),
|
||||
&format!("{prefix}."),
|
||||
)?;
|
||||
}
|
||||
DiagnosticNode::Kern { width, explicit } => {
|
||||
self.command("kern");
|
||||
if *explicit {
|
||||
|
||||
@@ -167,6 +167,8 @@ pub struct Executor {
|
||||
pdf_colors: crate::pdf_device::PdfColorStacks,
|
||||
pdf_link_stack: Vec<pdf_links::ActiveLink>,
|
||||
pdf_catalog_tokens: Vec<Token>,
|
||||
pdf_outlines: Vec<crate::pdf_device::catalog::PdfOutline<Vec<Token>>>,
|
||||
pdf_outline_payload: usize,
|
||||
pdf_info_tokens: Vec<Token>,
|
||||
pdf_images: Vec<pdf_image::RegisteredImage>,
|
||||
input_position: Option<(usize, usize)>,
|
||||
@@ -294,6 +296,8 @@ impl Executor {
|
||||
pdf_colors: Default::default(),
|
||||
pdf_link_stack: Vec::new(),
|
||||
pdf_catalog_tokens: Vec::new(),
|
||||
pdf_outlines: Vec::new(),
|
||||
pdf_outline_payload: 0,
|
||||
pdf_info_tokens: Vec::new(),
|
||||
pdf_images: Vec::new(),
|
||||
input_position: None,
|
||||
@@ -3460,6 +3464,7 @@ impl Executor {
|
||||
Unskip | Unkern | Unpenalty => self.delete_last(primitive)?,
|
||||
ItalicCorrection => self.append_italic_correction()?,
|
||||
Shipout => self.scan_box(boxes::Destination::Shipout, files)?,
|
||||
Leaders | Cleaders | Xleaders => self.scan_leaders(primitive, files)?,
|
||||
Vrule => {
|
||||
let r = self.scan_rule(false, TokenOrigin::from(&token), files)?;
|
||||
let p = self.paragraph.as_mut().unwrap();
|
||||
@@ -3550,6 +3555,7 @@ impl Executor {
|
||||
self.scan_pdf_link(primitive, token.span, files)?;
|
||||
}
|
||||
Pdfcatalog => self.scan_pdf_catalog(files)?,
|
||||
Pdfoutline => self.scan_pdf_outline(files)?,
|
||||
Pdfinfo => self.scan_pdf_info(files)?,
|
||||
Pdfximage => self.scan_pdf_image(&token, false, files)?,
|
||||
Pdfrefximage => self.reference_pdf_image(&token, files)?,
|
||||
|
||||
@@ -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, .. } => {
|
||||
|
||||
@@ -7,7 +7,7 @@ use std::{
|
||||
path::Path,
|
||||
};
|
||||
|
||||
const SCHEMA: u32 = 25;
|
||||
const SCHEMA: u32 = 27;
|
||||
const ENGINE: &str = concat!("texrra-native/", env!("CARGO_PKG_VERSION"));
|
||||
|
||||
#[derive(serde::Serialize, serde::Deserialize)]
|
||||
|
||||
@@ -164,6 +164,7 @@ pub enum MathNode {
|
||||
Glue(Glue),
|
||||
/// Normal-unit glue, including explicit spaces and horizontal skip commands.
|
||||
ExplicitSpace(Glue, Option<Parameter>),
|
||||
Leaders(LeaderGlue),
|
||||
Kern {
|
||||
amount: i32,
|
||||
mu: bool,
|
||||
@@ -441,7 +442,7 @@ impl Converter<'_> {
|
||||
}
|
||||
pub(super) fn clean(&self, nodes: &[TextNode]) -> Result<MathGeometry, String> {
|
||||
let b = self.measure(nodes)?;
|
||||
if !b.output_nodes().is_empty() {
|
||||
if b.contains_output()? {
|
||||
return Err("deferred output in flattened native math fields requires retained semantic math boxes".into());
|
||||
}
|
||||
b.geometry()
|
||||
@@ -462,7 +463,7 @@ impl Converter<'_> {
|
||||
self.clean(&[TextNode::Box(math_box(g))])
|
||||
}
|
||||
MathField::Box(b) => {
|
||||
if !b.output_nodes().is_empty() {
|
||||
if b.contains_output()? {
|
||||
return Err("deferred output in flattened native math fields requires retained semantic math boxes".into());
|
||||
}
|
||||
if b.shift == 0 {
|
||||
@@ -701,6 +702,7 @@ impl Converter<'_> {
|
||||
Some(parameter) => TextNode::NamedGlue(*glue, *parameter),
|
||||
None => TextNode::Glue(*glue),
|
||||
}],
|
||||
MathNode::Leaders(leader) => vec![TextNode::Leaders(leader.clone())],
|
||||
MathNode::Glue(g) => vec![TextNode::Glue(self.glue(*g, style)?)],
|
||||
MathNode::Kern {
|
||||
amount,
|
||||
|
||||
@@ -46,7 +46,10 @@ pub use statistics::{
|
||||
use std::collections::BTreeSet;
|
||||
use std::fmt;
|
||||
|
||||
pub use boxes::{BoxContent, BoxKind, GlueSetting, NativeBox, NativeVerticalNode};
|
||||
pub use boxes::{
|
||||
BoxContent, BoxKind, GlueSetting, LeaderGlue, LeaderMode, LeaderPattern, NativeBox,
|
||||
NativeVerticalNode,
|
||||
};
|
||||
pub use cancellation::Cancellation;
|
||||
pub use case::CaseCode;
|
||||
pub use context::{ContextFrame, ContextKind, InputContext};
|
||||
@@ -744,7 +747,9 @@ impl Execution {
|
||||
&boxes::box_paragraph(*b.clone()),
|
||||
)));
|
||||
}
|
||||
NativeVerticalNode::Glue(g) | NativeVerticalNode::NamedGlue(g, _) => {
|
||||
NativeVerticalNode::Glue(g)
|
||||
| NativeVerticalNode::NamedGlue(g, _)
|
||||
| NativeVerticalNode::Leaders(LeaderGlue { glue: g, .. }) => {
|
||||
items.push(VerticalItem::StretchGlue(VerticalGlue::with_order(
|
||||
bp(g.width as i64),
|
||||
bp(g.stretch.amount as i64),
|
||||
|
||||
@@ -184,7 +184,9 @@ impl NativePageState {
|
||||
return Err("native page construction step limit exceeded".into());
|
||||
}
|
||||
self.last_glue = match front {
|
||||
NativeVerticalNode::Glue(g) | NativeVerticalNode::NamedGlue(g, _) => Some(*g),
|
||||
NativeVerticalNode::Glue(g)
|
||||
| NativeVerticalNode::NamedGlue(g, _)
|
||||
| NativeVerticalNode::Leaders(LeaderGlue { glue: g, .. }) => Some(*g),
|
||||
_ => None,
|
||||
};
|
||||
self.last_node_type = Some(front.node_type());
|
||||
@@ -202,6 +204,7 @@ impl NativePageState {
|
||||
front,
|
||||
NativeVerticalNode::Glue(_)
|
||||
| NativeVerticalNode::NamedGlue(_, _)
|
||||
| NativeVerticalNode::Leaders(_)
|
||||
| NativeVerticalNode::Kern(_)
|
||||
| NativeVerticalNode::ImplicitKern(_)
|
||||
| NativeVerticalNode::Penalty(_)
|
||||
@@ -226,29 +229,34 @@ impl NativePageState {
|
||||
continue;
|
||||
}
|
||||
let penalty = match front {
|
||||
NativeVerticalNode::Glue(_) | NativeVerticalNode::NamedGlue(_, _) => {
|
||||
self.nodes
|
||||
.last()
|
||||
.filter(|n| {
|
||||
matches!(
|
||||
n,
|
||||
NativeVerticalNode::Box(_)
|
||||
| NativeVerticalNode::Line(_)
|
||||
| NativeVerticalNode::Rule(_)
|
||||
| NativeVerticalNode::Mark(_)
|
||||
| NativeVerticalNode::Insertion(_)
|
||||
| NativeVerticalNode::Output(_)
|
||||
)
|
||||
})
|
||||
.map(|_| 0)
|
||||
}
|
||||
NativeVerticalNode::Glue(_)
|
||||
| NativeVerticalNode::NamedGlue(_, _)
|
||||
| NativeVerticalNode::Leaders(_) => self
|
||||
.nodes
|
||||
.last()
|
||||
.filter(|n| {
|
||||
matches!(
|
||||
n,
|
||||
NativeVerticalNode::Box(_)
|
||||
| NativeVerticalNode::Line(_)
|
||||
| NativeVerticalNode::Rule(_)
|
||||
| NativeVerticalNode::Mark(_)
|
||||
| NativeVerticalNode::Insertion(_)
|
||||
| NativeVerticalNode::Output(_)
|
||||
)
|
||||
})
|
||||
.map(|_| 0),
|
||||
NativeVerticalNode::Kern(_) | NativeVerticalNode::ImplicitKern(_) => {
|
||||
if self.contributions.len() == 1 {
|
||||
return Ok(None);
|
||||
}
|
||||
matches!(
|
||||
self.contributions.get(1),
|
||||
Some(NativeVerticalNode::Glue(_) | NativeVerticalNode::NamedGlue(_, _))
|
||||
Some(
|
||||
NativeVerticalNode::Glue(_)
|
||||
| NativeVerticalNode::NamedGlue(_, _)
|
||||
| NativeVerticalNode::Leaders(_)
|
||||
)
|
||||
)
|
||||
.then_some(0)
|
||||
}
|
||||
@@ -347,7 +355,9 @@ impl NativePageState {
|
||||
self.dimensions[7] = depth;
|
||||
} else {
|
||||
match &n {
|
||||
NativeVerticalNode::Glue(g) | NativeVerticalNode::NamedGlue(g, _) => {
|
||||
NativeVerticalNode::Glue(g)
|
||||
| NativeVerticalNode::NamedGlue(g, _)
|
||||
| NativeVerticalNode::Leaders(LeaderGlue { glue: g, .. }) => {
|
||||
if g.shrink.order != 0 && g.shrink.amount != 0 {
|
||||
return Err(
|
||||
"infinite glue shrinkage found on native current page".into()
|
||||
|
||||
@@ -53,6 +53,7 @@ pub enum TextNode {
|
||||
},
|
||||
Glue(Glue),
|
||||
NamedGlue(Glue, Parameter),
|
||||
Leaders(LeaderGlue),
|
||||
Kern {
|
||||
width: i32,
|
||||
explicit: bool,
|
||||
@@ -365,6 +366,7 @@ fn discardable(n: &TextNode) -> bool {
|
||||
n,
|
||||
TextNode::Glue(_)
|
||||
| TextNode::NamedGlue(_, _)
|
||||
| TextNode::Leaders(_)
|
||||
| TextNode::Kern { .. }
|
||||
| TextNode::MathBoundary { .. }
|
||||
| TextNode::Penalty(_)
|
||||
@@ -375,7 +377,9 @@ fn width(n: &TextNode) -> i64 {
|
||||
TextNode::Glyph { glyph, .. } | TextNode::Box(glyph) | TextNode::MathChar(glyph) => {
|
||||
glyph.width
|
||||
}
|
||||
TextNode::Glue(g) | TextNode::NamedGlue(g, _) => g.width as i64,
|
||||
TextNode::Glue(g)
|
||||
| TextNode::NamedGlue(g, _)
|
||||
| TextNode::Leaders(LeaderGlue { glue: g, .. }) => g.width as i64,
|
||||
TextNode::Rule(r) => r.dimensions().0,
|
||||
TextNode::Output(node) => node.dimensions().map_or(0, |(width, _, _)| width),
|
||||
TextNode::Kern { width: n, .. }
|
||||
@@ -516,7 +520,10 @@ fn metrics(nodes: &[TextNode], left: Glue, right: Glue) -> (i64, [i64; 4], i64)
|
||||
let mut shrink = 0;
|
||||
for g in std::iter::once(left)
|
||||
.chain(nodes.iter().filter_map(|n| {
|
||||
if let TextNode::Glue(g) | TextNode::NamedGlue(g, _) = n {
|
||||
if let TextNode::Glue(g)
|
||||
| TextNode::NamedGlue(g, _)
|
||||
| TextNode::Leaders(LeaderGlue { glue: g, .. }) = n
|
||||
{
|
||||
Some(*g)
|
||||
} else {
|
||||
None
|
||||
@@ -530,7 +537,12 @@ fn metrics(nodes: &[TextNode], left: Glue, right: Glue) -> (i64, [i64; 4], i64)
|
||||
}
|
||||
natural += nodes
|
||||
.iter()
|
||||
.filter(|n| !matches!(n, TextNode::Glue(_) | TextNode::NamedGlue(_, _)))
|
||||
.filter(|n| {
|
||||
!matches!(
|
||||
n,
|
||||
TextNode::Glue(_) | TextNode::NamedGlue(_, _) | TextNode::Leaders(_)
|
||||
)
|
||||
})
|
||||
.map(width)
|
||||
.sum::<i64>();
|
||||
(natural, stretch, shrink)
|
||||
@@ -968,7 +980,10 @@ fn pre_display_line_size(
|
||||
| TextNode::Indent(_)
|
||||
| TextNode::Rule(_)
|
||||
);
|
||||
if let TextNode::Glue(g) | TextNode::NamedGlue(g, _) = n {
|
||||
if let TextNode::Glue(g)
|
||||
| TextNode::NamedGlue(g, _)
|
||||
| TextNode::Leaders(LeaderGlue { glue: g, .. }) = n
|
||||
{
|
||||
let f = if set.sign > 0 { g.stretch } else { g.shrink };
|
||||
if set.sign != 0 && f.order == set.order && f.amount != 0 {
|
||||
v = 1073741823;
|
||||
@@ -996,10 +1011,12 @@ pub(super) fn finish(
|
||||
cache: Option<&LineBreakCache>,
|
||||
) -> Result<NativeParagraph, String> {
|
||||
b.finish_text(true)?;
|
||||
if b.nodes
|
||||
.last()
|
||||
.is_some_and(|n| matches!(n, TextNode::Glue(_) | TextNode::NamedGlue(_, _)))
|
||||
{
|
||||
if b.nodes.last().is_some_and(|n| {
|
||||
matches!(
|
||||
n,
|
||||
TextNode::Glue(_) | TextNode::NamedGlue(_, _) | TextNode::Leaders(_)
|
||||
)
|
||||
}) {
|
||||
b.nodes.pop();
|
||||
}
|
||||
for n in &b.nodes {
|
||||
@@ -1020,7 +1037,9 @@ pub(super) fn finish(
|
||||
.collect();
|
||||
let shape = state.paragraph_shape();
|
||||
for n in &b.nodes {
|
||||
if let TextNode::Glue(g) | TextNode::NamedGlue(g, _) = n
|
||||
if let TextNode::Glue(g)
|
||||
| TextNode::NamedGlue(g, _)
|
||||
| TextNode::Leaders(LeaderGlue { glue: g, .. }) = n
|
||||
&& g.shrink.order != 0
|
||||
&& g.shrink.amount != 0
|
||||
{
|
||||
@@ -1260,7 +1279,12 @@ fn add_hyphens(
|
||||
.is_some_and(|c| state.case_code(super::CaseCode::Lower, c) != 0),
|
||||
_ => true,
|
||||
})
|
||||
.is_some_and(|n| matches!(n, TextNode::Glue(_) | TextNode::NamedGlue(_, _)))
|
||||
.is_some_and(|n| {
|
||||
matches!(
|
||||
n,
|
||||
TextNode::Glue(_) | TextNode::NamedGlue(_, _) | TextNode::Leaders(_)
|
||||
)
|
||||
})
|
||||
&& !word.is_empty()
|
||||
&& (state.parameter(Parameter::Uchyph).width() > 0
|
||||
|| word
|
||||
@@ -1290,6 +1314,7 @@ fn add_hyphens(
|
||||
| TextNode::Output(_)
|
||||
| TextNode::Glue(_)
|
||||
| TextNode::NamedGlue(_, _)
|
||||
| TextNode::Leaders(_)
|
||||
| TextNode::Penalty(_)
|
||||
| TextNode::Kern { explicit: true, .. }
|
||||
)
|
||||
|
||||
@@ -143,7 +143,9 @@ impl BreakNode {
|
||||
| TextNode::Output(_)
|
||||
| TextNode::MathChar(_)
|
||||
| TextNode::Indent(_) => Self::Content(width(node)),
|
||||
TextNode::Glue(glue) | TextNode::NamedGlue(glue, _) => Self::Glue(*glue),
|
||||
TextNode::Glue(glue)
|
||||
| TextNode::NamedGlue(glue, _)
|
||||
| TextNode::Leaders(LeaderGlue { glue, .. }) => Self::Glue(*glue),
|
||||
TextNode::Kern { width, explicit } => Self::Kern {
|
||||
width: *width,
|
||||
explicit: *explicit,
|
||||
|
||||
@@ -212,7 +212,9 @@ impl<'a> Tracer<'a> {
|
||||
| TextNode::Mark(_)
|
||||
| TextNode::Output(_) => out.push_str("[]"),
|
||||
TextNode::Rule(_) => out.push('|'),
|
||||
TextNode::Glue(g) | TextNode::NamedGlue(g, _) => {
|
||||
TextNode::Glue(g)
|
||||
| TextNode::NamedGlue(g, _)
|
||||
| TextNode::Leaders(LeaderGlue { glue: g, .. }) => {
|
||||
if !g.canonical_zero {
|
||||
out.push(' ');
|
||||
}
|
||||
|
||||
@@ -109,6 +109,7 @@ pub enum Primitive {
|
||||
Pdfendlink,
|
||||
Pdfdest,
|
||||
Pdfcatalog,
|
||||
Pdfoutline,
|
||||
Pdfinfo,
|
||||
Pdfsetmatrix,
|
||||
Pdfrestore,
|
||||
@@ -162,6 +163,9 @@ pub enum Primitive {
|
||||
Indent,
|
||||
Noindent,
|
||||
Hskip,
|
||||
Leaders,
|
||||
Cleaders,
|
||||
Xleaders,
|
||||
Hfil,
|
||||
Hfill,
|
||||
Hss,
|
||||
@@ -862,6 +866,7 @@ impl Default for State {
|
||||
("pdfendlink", Pdfendlink),
|
||||
("pdfdest", Pdfdest),
|
||||
("pdfcatalog", Pdfcatalog),
|
||||
("pdfoutline", Pdfoutline),
|
||||
("pdfinfo", Pdfinfo),
|
||||
("pdfsetmatrix", Pdfsetmatrix),
|
||||
("pdfrestore", Pdfrestore),
|
||||
@@ -870,6 +875,9 @@ impl Default for State {
|
||||
("indent", Indent),
|
||||
("noindent", Noindent),
|
||||
("hskip", Hskip),
|
||||
("leaders", Leaders),
|
||||
("cleaders", Cleaders),
|
||||
("xleaders", Xleaders),
|
||||
("hfil", Hfil),
|
||||
("hfill", Hfill),
|
||||
("hss", Hss),
|
||||
|
||||
@@ -194,6 +194,12 @@ impl Collector {
|
||||
self.material(o).geometry_items += g.items.len();
|
||||
}
|
||||
}
|
||||
fn leader_pattern(&mut self, leader: &LeaderGlue, o: Ownership) {
|
||||
match &leader.pattern {
|
||||
LeaderPattern::Box(b) => self.native_box(b, o),
|
||||
LeaderPattern::Rule(_) => self.material(o).geometry_items += 1,
|
||||
}
|
||||
}
|
||||
pub fn text(&mut self, nodes: &[TextNode], o: Ownership) {
|
||||
self.material(o).horizontal_nodes += nodes.len();
|
||||
for n in nodes {
|
||||
@@ -213,6 +219,7 @@ impl Collector {
|
||||
TextNode::Adjustment(value) => self.vertical(&value.nodes, o),
|
||||
TextNode::Insertion(n) => self.vertical(&n.nodes, o),
|
||||
TextNode::Output(v) => self.output(v),
|
||||
TextNode::Leaders(leader) => self.leader_pattern(leader, o),
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
@@ -229,6 +236,7 @@ impl Collector {
|
||||
NativeVerticalNode::Mark(m) => self.shared_tokens(&m.tokens),
|
||||
NativeVerticalNode::Insertion(n) => self.vertical(&n.nodes, o),
|
||||
NativeVerticalNode::Output(v) => self.output(v),
|
||||
NativeVerticalNode::Leaders(leader) => self.leader_pattern(leader, o),
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
@@ -264,6 +272,7 @@ impl Collector {
|
||||
MathNode::Adjustment(value) => self.vertical(&value.nodes, o),
|
||||
MathNode::Insertion(n) => self.vertical(&n.nodes, o),
|
||||
MathNode::Output(v) => self.output(v),
|
||||
MathNode::Leaders(leader) => self.leader_pattern(leader, o),
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
@@ -292,6 +301,9 @@ impl Collector {
|
||||
for n in nodes {
|
||||
match n {
|
||||
D::Box(b) => self.diagnostics(&b.nodes, o),
|
||||
D::Leaders { pattern, .. } => {
|
||||
self.diagnostics(std::slice::from_ref(pattern.as_ref()), o)
|
||||
}
|
||||
D::Discretionary { pre, post, .. } => {
|
||||
self.diagnostics(pre, o);
|
||||
self.diagnostics(post, o);
|
||||
|
||||
@@ -41,7 +41,11 @@ pub(super) fn find_break(
|
||||
depth = d;
|
||||
None
|
||||
}
|
||||
Some(NativeVerticalNode::Glue(_) | NativeVerticalNode::NamedGlue(_, _)) => (i > 0
|
||||
Some(
|
||||
NativeVerticalNode::Glue(_)
|
||||
| NativeVerticalNode::NamedGlue(_, _)
|
||||
| NativeVerticalNode::Leaders(_),
|
||||
) => (i > 0
|
||||
&& matches!(
|
||||
nodes[i - 1],
|
||||
NativeVerticalNode::Box(_)
|
||||
@@ -54,7 +58,11 @@ pub(super) fn find_break(
|
||||
.then_some(0),
|
||||
Some(NativeVerticalNode::Kern(_) | NativeVerticalNode::ImplicitKern(_)) => matches!(
|
||||
nodes.get(i + 1),
|
||||
Some(NativeVerticalNode::Glue(_) | NativeVerticalNode::NamedGlue(_, _))
|
||||
Some(
|
||||
NativeVerticalNode::Glue(_)
|
||||
| NativeVerticalNode::NamedGlue(_, _)
|
||||
| NativeVerticalNode::Leaders(_)
|
||||
)
|
||||
)
|
||||
.then_some(0),
|
||||
Some(NativeVerticalNode::Penalty(p)) => Some(*p),
|
||||
@@ -98,7 +106,11 @@ pub(super) fn find_break(
|
||||
}
|
||||
}
|
||||
match node {
|
||||
Some(NativeVerticalNode::Glue(g) | NativeVerticalNode::NamedGlue(g, _)) => {
|
||||
Some(
|
||||
NativeVerticalNode::Glue(g)
|
||||
| NativeVerticalNode::NamedGlue(g, _)
|
||||
| NativeVerticalNode::Leaders(LeaderGlue { glue: g, .. }),
|
||||
) => {
|
||||
if g.shrink.order != 0 && g.shrink.amount != 0 {
|
||||
return Err("infinite glue shrinkage found in native box being split".into());
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user