1063 lines
40 KiB
Rust
1063 lines
40 KiB
Rust
use super::super::boxes::{self, PackSpec, VerticalBuilder};
|
|
use super::*;
|
|
#[derive(Clone, Copy)]
|
|
pub(super) enum Destination {
|
|
Append,
|
|
Register(u16, bool),
|
|
Shift(i32),
|
|
Shipout,
|
|
Leaders(LeaderMode),
|
|
Adjustment { pre: bool },
|
|
Insertion { class: u8 },
|
|
}
|
|
#[derive(Clone)]
|
|
pub(super) struct BoxFrame {
|
|
pub(super) kind: BoxKind,
|
|
pub(super) spec: PackSpec,
|
|
group: GroupKind,
|
|
pub(super) depth: usize,
|
|
pub(super) destination: Destination,
|
|
pub(super) paragraph: Option<super::super::paragraph::Builder>,
|
|
pub(super) vertical: VerticalBuilder,
|
|
pub(super) span: Span,
|
|
pub(super) discretionary: Option<DiscretionaryFrame>,
|
|
pub(super) math: Vec<super::super::math::MathBuilder>,
|
|
// An inner $...$ belongs to this box, even inside an outer display.
|
|
pub(super) display: Option<super::display::DisplayFrame>,
|
|
}
|
|
#[derive(Clone)]
|
|
pub(super) struct DiscretionaryFrame {
|
|
parts: Vec<Vec<TextNode>>,
|
|
font: FontId,
|
|
span: Span,
|
|
pub(super) math: Vec<super::super::math::MathBuilder>,
|
|
}
|
|
impl BoxFrame {
|
|
pub(super) fn collect_statistics(&self, c: &mut super::super::statistics::Collector) {
|
|
use super::super::statistics::Ownership::Active;
|
|
if let Some(p) = &self.paragraph {
|
|
c.paragraph_builder(p);
|
|
}
|
|
c.vertical(&self.vertical.nodes, Active);
|
|
if let Some(d) = &self.display {
|
|
d.collect_statistics(c);
|
|
}
|
|
for m in &self.math {
|
|
c.math_builder(m);
|
|
}
|
|
if let Some(d) = &self.discretionary {
|
|
for p in &d.parts {
|
|
c.text(p, Active);
|
|
}
|
|
for m in &d.math {
|
|
c.math_builder(m);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
impl Executor {
|
|
pub(super) fn scan_rule(
|
|
&mut self,
|
|
horizontal: bool,
|
|
origin: TokenOrigin,
|
|
files: &mut impl SourceProvider,
|
|
) -> Result<NativeRule, Error> {
|
|
let span = origin.span;
|
|
let mut r = if horizontal {
|
|
NativeRule {
|
|
span,
|
|
origin: Some(origin),
|
|
derivation: None,
|
|
width: None,
|
|
height: Some(26214),
|
|
depth: Some(0),
|
|
}
|
|
} else {
|
|
NativeRule {
|
|
span,
|
|
origin: Some(origin),
|
|
derivation: None,
|
|
width: Some(26214),
|
|
height: None,
|
|
depth: None,
|
|
}
|
|
};
|
|
loop {
|
|
if self.keyword("width", files)? {
|
|
r.width = Some(self.dimension(false, false, files)?.amount);
|
|
} else if self.keyword("height", files)? {
|
|
r.height = Some(self.dimension(false, false, files)?.amount);
|
|
} else if self.keyword("depth", files)? {
|
|
r.depth = Some(self.dimension(false, false, files)?.amount);
|
|
} else {
|
|
break;
|
|
}
|
|
}
|
|
Ok(r)
|
|
}
|
|
pub(super) fn shift_box(
|
|
&mut self,
|
|
p: Primitive,
|
|
files: &mut impl SourceProvider,
|
|
) -> Result<(), Error> {
|
|
let horizontal = matches!(p, Primitive::Moveleft | Primitive::Moveright);
|
|
if horizontal != (self.paragraph.is_none() && self.math.is_empty()) {
|
|
return self.fail("native box shift is incompatible with the current mode");
|
|
}
|
|
let n = self.dimension(false, false, files)?.amount;
|
|
let shift = if matches!(p, Primitive::Raise | Primitive::Moveleft) {
|
|
-n
|
|
} else {
|
|
n
|
|
};
|
|
self.scan_box(Destination::Shift(shift), files)
|
|
}
|
|
pub(super) fn unpackage(
|
|
&mut self,
|
|
p: Primitive,
|
|
files: &mut impl SourceProvider,
|
|
) -> Result<(), Error> {
|
|
use Primitive::*;
|
|
let vertical = matches!(p, Unvbox | Unvcopy | Pagediscards | Splitdiscards);
|
|
if vertical && self.paragraph.is_some() {
|
|
if self.restricted() || !self.math.is_empty() {
|
|
return self.fail("native vertical unboxing requires vertical mode");
|
|
}
|
|
self.end_paragraph(files)?;
|
|
}
|
|
if matches!(p, Pagediscards | Splitdiscards) {
|
|
if !self.math.is_empty() {
|
|
return self.fail("native vertical discards require vertical mode");
|
|
}
|
|
let nodes = if p == Pagediscards {
|
|
self.reads.insert(Dependency::PageState);
|
|
self.native_page
|
|
.as_mut()
|
|
.map(|page| std::mem::take(&mut page.discards))
|
|
.unwrap_or_default()
|
|
} else {
|
|
self.reads.insert(Dependency::SplitDiscards);
|
|
std::mem::take(&mut self.split_discards)
|
|
};
|
|
self.append_unboxed_vertical(nodes);
|
|
return Ok(());
|
|
}
|
|
let index = self.register_index(files)?;
|
|
self.reads.insert(Dependency::BoxRegister(index));
|
|
let Some(b) = self.state.box_register(index) else {
|
|
return Ok(());
|
|
};
|
|
if !self.math.is_empty() || b.kind.is_unset() || (b.kind == BoxKind::Horizontal) == vertical
|
|
{
|
|
return self.fail("incompatible native box cannot be unboxed in this mode");
|
|
}
|
|
let copy = matches!(p, Primitive::Unhcopy | Primitive::Unvcopy);
|
|
let b = if copy {
|
|
let b = b.clone();
|
|
self.account_box(&b)?;
|
|
b
|
|
} else {
|
|
self.state.take_box(index).unwrap()
|
|
};
|
|
match b.content {
|
|
BoxContent::Horizontal(nodes) => self
|
|
.paragraph
|
|
.as_mut()
|
|
.expect("unhbox starts an actual paragraph")
|
|
.nodes
|
|
.extend(
|
|
nodes.into_iter().filter(|node| !matches!(node, TextNode::MarginKern { .. })),
|
|
),
|
|
BoxContent::Vertical(nodes) => self.append_unboxed_vertical(nodes),
|
|
}
|
|
Ok(())
|
|
}
|
|
fn append_unboxed_vertical(&mut self, nodes: Vec<NativeVerticalNode>) {
|
|
let start = self.vertical.nodes.len();
|
|
self.vertical.extend(nodes);
|
|
// Transferred list nodes leave prev_depth unchanged.
|
|
self.emit_vertical(start);
|
|
}
|
|
pub(super) fn start_discretionary(
|
|
&mut self,
|
|
span: Span,
|
|
files: &mut impl SourceProvider,
|
|
) -> Result<(), Error> {
|
|
if self.paragraph.is_none() && self.math.is_empty() {
|
|
self.begin_paragraph(span, true)?;
|
|
}
|
|
self.finish_text(true)?;
|
|
self.begin_discretionary_part(
|
|
DiscretionaryFrame {
|
|
parts: Vec::new(),
|
|
font: self.state.text_font(),
|
|
span,
|
|
math: Vec::new(),
|
|
},
|
|
files,
|
|
)
|
|
}
|
|
fn begin_discretionary_part(
|
|
&mut self,
|
|
mut disc: DiscretionaryFrame,
|
|
files: &mut impl SourceProvider,
|
|
) -> Result<(), Error> {
|
|
if self.box_frames.len() >= self.limits.input_depth.min(32) {
|
|
return self.fail("native discretionary nesting capacity exceeded");
|
|
}
|
|
self.begin_group(GroupKind::Discretionary, files)?;
|
|
let open = loop {
|
|
let t = self.expanded_non_space(files)?.token;
|
|
if self.primitive(&t) != Some(Primitive::Relax) {
|
|
break t;
|
|
}
|
|
};
|
|
if !matches!(
|
|
self.meaning(&open),
|
|
Meaning::Character(TokenKind::Character(_, CatCode::BeginGroup))
|
|
) {
|
|
return self.fail("expected opening brace for native discretionary part");
|
|
}
|
|
disc.math = std::mem::take(&mut self.math);
|
|
self.box_frames.push(BoxFrame {
|
|
kind: BoxKind::Horizontal,
|
|
spec: PackSpec::Natural,
|
|
group: GroupKind::Discretionary,
|
|
depth: self.state.group_depth(),
|
|
destination: Destination::Append,
|
|
paragraph: self.paragraph.take(),
|
|
vertical: self.vertical.enter_nested(true),
|
|
span: disc.span,
|
|
discretionary: Some(disc),
|
|
math: Vec::new(),
|
|
display: self.display.take(),
|
|
});
|
|
let mut b = super::super::paragraph::Builder::default();
|
|
b.start(&self.state, open.span, false);
|
|
b.restricted = true;
|
|
b.font_step_limit = self.limits.steps.saturating_sub(self.steps);
|
|
b.ligature_carry = self.ligature_carry;
|
|
self.paragraph = Some(b);
|
|
Ok(())
|
|
}
|
|
fn finish_discretionary_part(
|
|
&mut self,
|
|
mut disc: DiscretionaryFrame,
|
|
part: Vec<TextNode>,
|
|
files: &mut impl SourceProvider,
|
|
) -> Result<(), Error> {
|
|
if part.iter().any(|n| {
|
|
!matches!(
|
|
n,
|
|
TextNode::Glyph { .. }
|
|
| TextNode::OpenType(_)
|
|
| TextNode::ExplicitOpenTypeGlyph(_)
|
|
| TextNode::Kern { .. }
|
|
| TextNode::AccentKern(_)
|
|
| TextNode::AutoKern(_)
|
|
| TextNode::Box(_)
|
|
| TextNode::Indent(_)
|
|
| TextNode::Rule(_)
|
|
)
|
|
}) {
|
|
return self.fail(
|
|
"improper native discretionary material: only glyphs, boxes, rules and kerns are allowed",
|
|
);
|
|
}
|
|
self.charge_box_material(part.len(), "native discretionary node capacity exceeded")?;
|
|
disc.parts.push(part);
|
|
if disc.parts.len() < 3 {
|
|
return self.begin_discretionary_part(disc, files);
|
|
}
|
|
let replacement = disc.parts.pop().unwrap();
|
|
if !self.math.is_empty() && !replacement.is_empty() {
|
|
return self.fail("native math discretionary replacement must be empty");
|
|
}
|
|
let replacement_count = u8::try_from(replacement.len()).ok().ok_or_else(|| {
|
|
self.error(
|
|
"native discretionary replacement exceeds 255 nodes",
|
|
disc.span,
|
|
)
|
|
})?;
|
|
let post_break = disc.parts.pop().unwrap();
|
|
let pre_break = disc.parts.pop().unwrap();
|
|
let node = TextNode::Discretionary {
|
|
font: disc.font,
|
|
automatic: false,
|
|
hyphen_char: None,
|
|
pre_break,
|
|
post_break,
|
|
replacement_count,
|
|
};
|
|
if let Some(m) = self.math.last_mut() {
|
|
m.nodes.push(MathNode::Discretionary(Box::new(node)));
|
|
} else {
|
|
let parent = self
|
|
.paragraph
|
|
.as_mut()
|
|
.expect("discretionary parent horizontal list");
|
|
parent.nodes.push(node);
|
|
parent.nodes.extend(replacement);
|
|
}
|
|
Ok(())
|
|
}
|
|
pub(super) fn restricted(&self) -> bool {
|
|
self.paragraph.as_ref().is_some_and(|p| p.restricted)
|
|
}
|
|
pub(super) fn emit_vertical(&mut self, start: usize) {
|
|
if self.outer_vertical() && start < self.vertical.nodes.len() {
|
|
self.events.push(Event::VerticalMaterial(
|
|
self.vertical.nodes[start..].to_vec(),
|
|
));
|
|
}
|
|
if self.outer_vertical() {
|
|
if let Some(page) = self.native_page.as_mut() {
|
|
page.contributions.extend(self.vertical.take_nodes());
|
|
}
|
|
}
|
|
}
|
|
pub(super) fn account_box(&mut self, b: &NativeBox) -> Result<(), Error> {
|
|
self.charge_box_material(b.node_count(), "native box node capacity exceeded")
|
|
}
|
|
pub(super) fn charge_box_material(
|
|
&mut self,
|
|
nodes: usize,
|
|
error: &'static str,
|
|
) -> Result<(), Error> {
|
|
self.box_nodes_created = self.box_nodes_created.saturating_add(nodes);
|
|
if nodes > self.limits.tokens {
|
|
return self.fail(error);
|
|
}
|
|
self.material_charge = self.material_charge.saturating_add(nodes);
|
|
Ok(())
|
|
}
|
|
pub(super) fn scan_box(
|
|
&mut self,
|
|
destination: Destination,
|
|
files: &mut impl SourceProvider,
|
|
) -> Result<(), Error> {
|
|
let token = loop {
|
|
let t = self.expanded_non_space(files)?.token;
|
|
if self.primitive(&t) != Some(Primitive::Relax) {
|
|
break t;
|
|
}
|
|
};
|
|
let p = self
|
|
.primitive(&token)
|
|
.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,
|
|
destination: Destination,
|
|
span: Span,
|
|
files: &mut impl SourceProvider,
|
|
) -> Result<(), Error> {
|
|
use Primitive::*;
|
|
if p == Vsplit {
|
|
let index = self.register_index(files)?;
|
|
if !self.keyword("to", files)? {
|
|
return self.fail("expected to in native vsplit");
|
|
}
|
|
let height = self.dimension(false, false, files)?.amount;
|
|
self.reads.insert(Dependency::BoxRegister(index));
|
|
// Even splitting a void box clears results from the previous split.
|
|
self.state.split_marks.clear();
|
|
self.split_discards.clear();
|
|
let Some(original) = self.state.box_register(index) else {
|
|
return self.deliver_box(None, destination, files);
|
|
};
|
|
if !matches!(original.kind, BoxKind::Vertical | BoxKind::Vtop) {
|
|
return self.fail("native vsplit needs a vbox");
|
|
}
|
|
let max_depth = self.read_parameter(Parameter::Splitmaxdepth).width();
|
|
let skip = self.read_parameter(Parameter::Splittopskip).glue();
|
|
let original = self.state.take_box(index).unwrap();
|
|
let BoxContent::Vertical(mut nodes) = original.content else {
|
|
unreachable!()
|
|
};
|
|
let cut = super::super::vertical_break::find_break(
|
|
&nodes,
|
|
i64::from(height),
|
|
max_depth,
|
|
&mut self.steps,
|
|
self.limits.steps,
|
|
)
|
|
.map_err(|e| self.error(e, span))?;
|
|
let tail = nodes.split_off(cut.index);
|
|
self.collect_split_marks(&nodes)?;
|
|
let save = self.read_parameter(Parameter::Savingvdiscards).width() > 0;
|
|
let tail = super::super::vertical_break::prune_top(
|
|
tail,
|
|
skip,
|
|
save.then_some(&mut self.split_discards),
|
|
&mut self.steps,
|
|
self.limits.steps,
|
|
)
|
|
.map_err(|e| self.error(e, span))?;
|
|
let remaining = if tail.is_empty() {
|
|
None
|
|
} else {
|
|
let b = boxes::vpack(
|
|
tail,
|
|
PackSpec::Natural,
|
|
1_073_741_823,
|
|
BoxKind::Vertical,
|
|
span,
|
|
)
|
|
.map_err(|e| self.error(e, span))?;
|
|
self.diagnose_box(&b, files)?;
|
|
self.account_box(&b)?;
|
|
Some(b)
|
|
};
|
|
// Mutate the source register without changing its saved eq level.
|
|
self.state.set_box(index, remaining);
|
|
let b = boxes::vpack(
|
|
nodes,
|
|
PackSpec::To(height),
|
|
max_depth,
|
|
BoxKind::Vertical,
|
|
span,
|
|
)
|
|
.map_err(|e| self.error(e, span))?;
|
|
self.diagnose_box(&b, files)?;
|
|
self.account_box(&b)?;
|
|
return self.deliver_box(Some(b), destination, files);
|
|
}
|
|
if p == Lastbox {
|
|
if !self.math.is_empty() {
|
|
return self.fail("lastbox is not allowed in native math mode");
|
|
}
|
|
let mut b = if let Some(par) = self.paragraph.as_mut() {
|
|
let protected=par.nodes.iter().enumerate().any(|(i,n)|matches!(n,TextNode::Discretionary{replacement_count,..}if *replacement_count>0 && i+usize::from(*replacement_count)+1==par.nodes.len()));
|
|
if protected {
|
|
None
|
|
} else {
|
|
match par.nodes.last() {
|
|
Some(TextNode::Box(b)) if b.native_box.is_some() => {
|
|
let Some(TextNode::Box(b)) = par.nodes.pop() else {
|
|
unreachable!()
|
|
};
|
|
b.native_box.map(|b| *b)
|
|
}
|
|
Some(TextNode::Indent(_)) => {
|
|
let Some(TextNode::Indent(width)) = par.nodes.pop() else {
|
|
unreachable!()
|
|
};
|
|
Some(NativeBox {
|
|
kind: BoxKind::Horizontal,
|
|
display: false,
|
|
width: i64::from(width),
|
|
height: 0,
|
|
depth: 0,
|
|
shift: 0,
|
|
content: BoxContent::Horizontal(Vec::new()),
|
|
diagnostic: Vec::new(),
|
|
setting: GlueSetting::default(),
|
|
interword_runs: Vec::new(),
|
|
badness: 0,
|
|
diagnostic_eligible: false,
|
|
excess: 0,
|
|
span: par.span,
|
|
})
|
|
}
|
|
_ => None,
|
|
}
|
|
}
|
|
} else {
|
|
if self.outer_vertical() && self.native_page.is_some() {
|
|
let page = self.native_page.as_mut().unwrap();
|
|
if page.contributions.is_empty() {
|
|
return self.fail("lastbox cannot take a box from the native current page");
|
|
}
|
|
let b = if matches!(
|
|
page.contributions.back(),
|
|
Some(NativeVerticalNode::Box(_) | NativeVerticalNode::Line(_))
|
|
) {
|
|
match page.contributions.pop_back().unwrap() {
|
|
NativeVerticalNode::Box(b) | NativeVerticalNode::Line(b) => Some(*b),
|
|
_ => unreachable!(),
|
|
}
|
|
} else {
|
|
None
|
|
};
|
|
let mut b = b;
|
|
if let Some(b) = &mut b {
|
|
b.shift = 0;
|
|
}
|
|
return self.deliver_box(b, destination, files);
|
|
}
|
|
if self.outer_vertical() {
|
|
return self.fail("lastbox cannot take material from the outer native page before native page ownership is implemented");
|
|
}
|
|
self.reads.insert(Dependency::VerticalState);
|
|
self.vertical
|
|
.take_last_box()
|
|
.map_err(|e| self.error(e, self.current))?
|
|
};
|
|
if let Some(b) = &mut b {
|
|
b.shift = 0;
|
|
}
|
|
return self.deliver_box(b, destination, files);
|
|
}
|
|
if matches!(p, Box | Copy) {
|
|
let index = self.register_index(files)?;
|
|
self.reads.insert(Dependency::BoxRegister(index));
|
|
let b = if p == Box {
|
|
self.state.take_box(index)
|
|
} else {
|
|
if let Some(b) = self.state.box_register(index).cloned() {
|
|
self.account_box(&b)?;
|
|
Some(b)
|
|
} else {
|
|
None
|
|
}
|
|
};
|
|
return self.deliver_box(b, destination, files);
|
|
}
|
|
if p == Vcenter && (!matches!(destination, Destination::Append) || self.math.is_empty()) {
|
|
return self.fail("vcenter requires native math mode and cannot be a box assignment");
|
|
}
|
|
let kind = match p {
|
|
Hbox => BoxKind::Horizontal,
|
|
Vbox | Vcenter => BoxKind::Vertical,
|
|
Vtop => BoxKind::Vtop,
|
|
_ => return self.fail("expected native box constructor"),
|
|
};
|
|
if self.box_frames.len() >= self.limits.input_depth.min(32) {
|
|
return self.fail("native box nesting capacity exceeded");
|
|
}
|
|
let spec = if self.keyword("to", files)? {
|
|
PackSpec::To(self.dimension(false, false, files)?.amount)
|
|
} else if self.keyword("spread", files)? {
|
|
PackSpec::Spread(self.dimension(false, false, files)?.amount)
|
|
} else {
|
|
PackSpec::Natural
|
|
};
|
|
let group = match kind {
|
|
BoxKind::Horizontal
|
|
if matches!(destination, Destination::Append | Destination::Shift(_))
|
|
&& self.paragraph.is_none()
|
|
&& self.math.is_empty() =>
|
|
{
|
|
GroupKind::AdjustedHbox
|
|
}
|
|
BoxKind::Horizontal => GroupKind::Hbox,
|
|
BoxKind::Vertical if p == Vcenter => GroupKind::Vcenter,
|
|
BoxKind::Vertical => GroupKind::Vbox,
|
|
BoxKind::Vtop => GroupKind::Vtop,
|
|
BoxKind::Unset { .. } => return self.fail("cannot package an unset box group"),
|
|
};
|
|
self.open_box_group(kind, spec, group, destination, span, files)
|
|
}
|
|
pub(super) fn start_adjustment(
|
|
&mut self,
|
|
span: Span,
|
|
files: &mut impl SourceProvider,
|
|
) -> Result<(), Error> {
|
|
if self.paragraph.is_none() && self.math.is_empty() {
|
|
return self.fail("vadjust requires native horizontal or math mode");
|
|
}
|
|
if self.box_frames.len() >= self.limits.input_depth.min(32) {
|
|
return self.fail("native box nesting capacity exceeded");
|
|
}
|
|
let pre = self.state.profile() == Profile::EtexBootstrap && self.keyword("pre", files)?;
|
|
self.open_box_group(
|
|
BoxKind::Vertical,
|
|
PackSpec::Natural,
|
|
GroupKind::Insertion,
|
|
Destination::Adjustment { pre },
|
|
span,
|
|
files,
|
|
)
|
|
}
|
|
pub(super) fn start_insertion(
|
|
&mut self,
|
|
span: Span,
|
|
files: &mut impl SourceProvider,
|
|
) -> Result<(), Error> {
|
|
let class = self.integer(files)?;
|
|
if !(0..255).contains(&class) {
|
|
return self.fail("native insertion class must be in 0..254");
|
|
}
|
|
if self.box_frames.len() >= self.limits.input_depth.min(32) {
|
|
return self.fail("native box nesting capacity exceeded");
|
|
}
|
|
self.open_box_group(
|
|
BoxKind::Vertical,
|
|
PackSpec::Natural,
|
|
GroupKind::Insertion,
|
|
Destination::Insertion { class: class as u8 },
|
|
span,
|
|
files,
|
|
)
|
|
}
|
|
fn open_box_group(
|
|
&mut self,
|
|
kind: BoxKind,
|
|
spec: PackSpec,
|
|
group: GroupKind,
|
|
destination: Destination,
|
|
span: Span,
|
|
files: &mut impl SourceProvider,
|
|
) -> Result<(), Error> {
|
|
// scan_spec opens the save level before scan_left_brace expands input.
|
|
self.begin_group(group, files)?;
|
|
let open = loop {
|
|
let t = self.expanded_non_space(files)?.token;
|
|
if self.primitive(&t) != Some(Primitive::Relax) {
|
|
break t;
|
|
}
|
|
};
|
|
if !matches!(
|
|
self.meaning(&open),
|
|
Meaning::Character(TokenKind::Character(_, CatCode::BeginGroup))
|
|
) {
|
|
return self.fail("expected opening brace for native box");
|
|
}
|
|
let frame = BoxFrame {
|
|
kind,
|
|
spec,
|
|
group,
|
|
depth: self.state.group_depth(),
|
|
destination,
|
|
paragraph: self.paragraph.take(),
|
|
vertical: self.vertical.enter_nested(kind == BoxKind::Horizontal),
|
|
span,
|
|
discretionary: None,
|
|
math: std::mem::take(&mut self.math),
|
|
display: self.display.take(),
|
|
};
|
|
self.box_frames.push(frame);
|
|
if kind == BoxKind::Horizontal {
|
|
let mut b = super::super::paragraph::Builder::default();
|
|
b.start(&self.state, span, false);
|
|
b.font_step_limit = self.limits.steps.saturating_sub(self.steps);
|
|
b.restricted = true;
|
|
self.paragraph = Some(b);
|
|
} else {
|
|
self.normal_paragraph(files)?;
|
|
}
|
|
if group == GroupKind::Insertion {
|
|
return Ok(());
|
|
}
|
|
self.push_mode_hook(if kind == BoxKind::Horizontal {
|
|
TokenParameter::Everyhbox
|
|
} else {
|
|
TokenParameter::Everyvbox
|
|
})
|
|
}
|
|
pub(super) fn close_box_group(
|
|
&mut self,
|
|
files: &mut impl SourceProvider,
|
|
) -> Result<bool, Error> {
|
|
if !self
|
|
.box_frames
|
|
.last()
|
|
.is_some_and(|f| f.depth == self.state.group_depth())
|
|
{
|
|
return Ok(false);
|
|
}
|
|
self.finish_text(true)?;
|
|
let horizontal = self.restricted();
|
|
if !horizontal {
|
|
self.end_paragraph(files)?;
|
|
}
|
|
// TeX package: capture inner boxmaxdepth, unsave, then hpack/vpackage.
|
|
let max_depth = if matches!(
|
|
self.box_frames.last().unwrap().group,
|
|
GroupKind::Vcenter | GroupKind::Insertion
|
|
) {
|
|
// TeX uses natural vpack's maximum depth for vcenter and adjustments.
|
|
Some(1_073_741_823)
|
|
} else if self.box_frames.last().unwrap().discretionary.is_none() {
|
|
Some(self.read_parameter(Parameter::Boxmaxdepth).width())
|
|
} else {
|
|
None
|
|
};
|
|
let insertion_specs = matches!(
|
|
self.box_frames.last().unwrap().destination,
|
|
Destination::Insertion { .. }
|
|
)
|
|
.then(|| {
|
|
(
|
|
self.read_parameter(Parameter::Splittopskip).glue(),
|
|
self.read_parameter(Parameter::Splitmaxdepth).width(),
|
|
self.read_parameter(Parameter::Floatingpenalty).width(),
|
|
)
|
|
});
|
|
let frame = self.box_frames.pop().unwrap();
|
|
self.finish_group(frame.group, self.current, files)?;
|
|
if let Some(mut disc) = frame.discretionary {
|
|
let part = self.paragraph.take().unwrap().nodes;
|
|
self.paragraph = frame.paragraph;
|
|
self.vertical.leave_nested(frame.vertical, true);
|
|
self.math = std::mem::take(&mut disc.math);
|
|
self.display = frame.display;
|
|
self.finish_discretionary_part(disc, part, files)?;
|
|
return Ok(true);
|
|
}
|
|
self.reads
|
|
.insert(Dependency::Parameter(Parameter::Overfullrule));
|
|
self.reads.insert(Dependency::Parameter(Parameter::Hfuzz));
|
|
let mut adjustments = super::super::marks::MigratingMaterial::default();
|
|
let result = if horizontal {
|
|
let b = self.paragraph.take().unwrap();
|
|
let nodes = if frame.group == GroupKind::AdjustedHbox {
|
|
let (nodes, extracted) = super::super::marks::extract(b.nodes);
|
|
adjustments = extracted;
|
|
nodes
|
|
} else {
|
|
b.nodes
|
|
};
|
|
if self.state.input_profile() == InputProfile::XeTeX {
|
|
self.reads.insert(Dependency::Parameter(Parameter::XeTeXuseglyphmetrics));
|
|
}
|
|
boxes::hpack_with_direction_report(
|
|
nodes, frame.spec, frame.span, &self.state,
|
|
Some((&mut self.steps, self.limits.steps)),
|
|
)
|
|
} else {
|
|
boxes::vpack(
|
|
self.vertical.take_nodes(),
|
|
frame.spec,
|
|
max_depth.expect("vertical box maximum depth"),
|
|
frame.kind,
|
|
frame.span,
|
|
)
|
|
.map(|packed| (packed, (0, 0)))
|
|
};
|
|
let (b, (missing, extra)) = result.map_err(|e| self.error(e, frame.span))?;
|
|
if missing != 0 || extra != 0 {
|
|
let line = self.error("", self.current).line;
|
|
files
|
|
.observe_message(&format!(
|
|
"\n\\endL or \\endR problem ({missing} missing, {extra} extra) detected at line {line}\n"
|
|
))
|
|
.map_err(|error| self.error(error, self.current))?;
|
|
}
|
|
self.diagnose_box(&b, files)?;
|
|
self.account_box(&b)?;
|
|
self.paragraph = frame.paragraph;
|
|
self.vertical.leave_nested(frame.vertical, horizontal);
|
|
self.math = frame.math;
|
|
self.display = frame.display;
|
|
if !adjustments.pre.is_empty() {
|
|
let start = self.vertical.nodes.len();
|
|
self.vertical.extend(adjustments.pre);
|
|
self.emit_vertical(start);
|
|
}
|
|
if frame.group == GroupKind::Vcenter {
|
|
self.accept_box_math_field(b)?;
|
|
let Some(MathNode::Noad(n)) = self.math.last_mut().unwrap().nodes.last_mut() else {
|
|
unreachable!()
|
|
};
|
|
n.vcenter = true;
|
|
n.span = frame.span;
|
|
} else if let Destination::Adjustment { pre } = frame.destination {
|
|
let BoxContent::Vertical(nodes) = b.content else {
|
|
unreachable!("vadjust owns a vertical list");
|
|
};
|
|
let adjustment = NativeAdjustment { pre, nodes };
|
|
if let Some(math) = self.math.last_mut() {
|
|
math.nodes.push(MathNode::Adjustment(adjustment));
|
|
} else {
|
|
self.paragraph
|
|
.as_mut()
|
|
.unwrap()
|
|
.nodes
|
|
.push(TextNode::Adjustment(adjustment));
|
|
}
|
|
} else if let Destination::Insertion { class } = frame.destination {
|
|
let BoxContent::Vertical(nodes) = b.content else {
|
|
unreachable!("insert owns a vertical list");
|
|
};
|
|
let (split_top, split_depth, float_cost) =
|
|
insertion_specs.expect("saved inner insertion parameters");
|
|
let insertion = NativeInsertion {
|
|
class,
|
|
natural_size: b.height + b.depth,
|
|
split_top,
|
|
split_depth,
|
|
float_cost,
|
|
nodes,
|
|
span: frame.span,
|
|
};
|
|
if let Some(math) = self.math.last_mut() {
|
|
math.nodes.push(MathNode::Insertion(insertion));
|
|
} else if let Some(paragraph) = self.paragraph.as_mut() {
|
|
paragraph.nodes.push(TextNode::Insertion(insertion));
|
|
} else {
|
|
let start = self.vertical.nodes.len();
|
|
self.vertical.push(NativeVerticalNode::Insertion(insertion));
|
|
self.emit_vertical(start);
|
|
self.request_page();
|
|
}
|
|
} else {
|
|
self.deliver_box(Some(b), frame.destination, files)?;
|
|
}
|
|
let start = self.vertical.nodes.len();
|
|
self.vertical.extend(adjustments.post);
|
|
self.emit_vertical(start);
|
|
Ok(true)
|
|
}
|
|
pub(super) fn diagnose_box(
|
|
&mut self,
|
|
b: &NativeBox,
|
|
files: &mut impl SourceProvider,
|
|
) -> Result<(), Error> {
|
|
self.state.last_badness = b.badness;
|
|
let vertical = b.kind != BoxKind::Horizontal;
|
|
let threshold = self
|
|
.read_parameter(if vertical {
|
|
Parameter::Vbadness
|
|
} else {
|
|
Parameter::Hbadness
|
|
})
|
|
.width();
|
|
let fuzz = self
|
|
.read_parameter(if vertical {
|
|
Parameter::Vfuzz
|
|
} else {
|
|
Parameter::Hfuzz
|
|
})
|
|
.width() as i64;
|
|
let report = b.diagnostic_eligible
|
|
&& if b.badness == 1_000_000 {
|
|
b.excess > fuzz || threshold < 100
|
|
} else {
|
|
b.badness > threshold
|
|
};
|
|
if report {
|
|
let label = if b.badness == 1_000_000 {
|
|
"Overfull"
|
|
} else if b.setting.sign < 0 {
|
|
"Tight"
|
|
} else if b.badness > 100 {
|
|
"Underfull"
|
|
} else {
|
|
"Loose"
|
|
};
|
|
let kind = if vertical { "vbox" } else { "hbox" };
|
|
files
|
|
.observe_message(&format!(
|
|
"\n{label} \\{kind} (badness {}, excess {}sp)\n",
|
|
b.badness, b.excess
|
|
))
|
|
.map_err(|e| self.error(e, b.span))?;
|
|
}
|
|
Ok(())
|
|
}
|
|
fn deliver_box(
|
|
&mut self,
|
|
mut b: Option<NativeBox>,
|
|
destination: Destination,
|
|
files: &mut impl SourceProvider,
|
|
) -> Result<(), Error> {
|
|
if let Destination::Shift(shift) = destination {
|
|
if let Some(b) = &mut b {
|
|
b.shift = i64::from(shift);
|
|
}
|
|
}
|
|
match destination {
|
|
Destination::Adjustment { .. } | Destination::Insertion { .. } => {
|
|
unreachable!("handled when the adjustment group closes")
|
|
}
|
|
Destination::Register(n, global) => {
|
|
let equivalent = b.is_none() && self.state.box_register(n).is_none();
|
|
self.assign_equivalence(Key::Box(n), global, equivalent, files, |state| {
|
|
state.assign_box(n, b, global);
|
|
})?;
|
|
}
|
|
Destination::Shipout => {
|
|
self.native_page_mut()?;
|
|
if let Some(b) = b {
|
|
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() {
|
|
self.accept_box_math_field(b)?;
|
|
} else if let Some(p) = self.paragraph.as_mut() {
|
|
p.nodes.push(TextNode::Box(b.horizontal_item()));
|
|
p.space_factor = 1000;
|
|
} else {
|
|
let start = self.vertical.nodes.len();
|
|
self.append_vertical_box(NativeVerticalNode::Box(Box::new(b)))?;
|
|
self.emit_vertical(start);
|
|
self.request_page();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
Ok(())
|
|
}
|
|
pub(super) fn append_vertical_box(&mut self, node: NativeVerticalNode) -> Result<(), Error> {
|
|
self.reads.insert(Dependency::VerticalState);
|
|
for p in [
|
|
Parameter::Baselineskip,
|
|
Parameter::Lineskip,
|
|
Parameter::Lineskiplimit,
|
|
] {
|
|
self.reads.insert(Dependency::Parameter(p));
|
|
}
|
|
self.vertical
|
|
.append_box(node, &self.state)
|
|
.map_err(|e| self.error(e, self.current))
|
|
}
|
|
pub(super) fn vertical_command(
|
|
&mut self,
|
|
p: Primitive,
|
|
origin: TokenOrigin,
|
|
files: &mut impl SourceProvider,
|
|
) -> Result<(), Error> {
|
|
use Primitive::*;
|
|
if self.paragraph.is_some() {
|
|
if self.restricted() {
|
|
return self.fail("vertical command in restricted horizontal mode");
|
|
};
|
|
self.end_paragraph(files)?;
|
|
}
|
|
let start = self.vertical.nodes.len();
|
|
let node = match p {
|
|
Vskip | Vfil | Vfill | Vss | Vfilneg => {
|
|
NativeVerticalNode::Glue(self.skip_glue(p, files)?)
|
|
}
|
|
Kern => NativeVerticalNode::Kern(self.dimension(false, false, files)?.amount),
|
|
Penalty => NativeVerticalNode::Penalty(self.integer(files)?),
|
|
Hrule => {
|
|
let r = self.scan_rule(true, origin, files)?;
|
|
if self.outer_vertical() && self.native_page.is_none() && r.width.is_none() {
|
|
return self
|
|
.fail("outer running rule requires native page packaging; specify width");
|
|
}
|
|
self.vertical.prev_depth = -1000 * 65536;
|
|
NativeVerticalNode::Rule(r)
|
|
}
|
|
_ => unreachable!(),
|
|
};
|
|
self.vertical.push(node);
|
|
self.emit_vertical(start);
|
|
if p == Penalty {
|
|
self.request_page();
|
|
}
|
|
Ok(())
|
|
}
|
|
pub(super) fn dimension_value(
|
|
&mut self,
|
|
p: Primitive,
|
|
files: &mut impl SourceProvider,
|
|
) -> Result<i32, Error> {
|
|
if p == Primitive::Prevdepth {
|
|
if self.writing || self.paragraph.is_some() || !self.math.is_empty() {
|
|
return self.fail("prevdepth requires vertical mode");
|
|
}
|
|
self.reads.insert(Dependency::VerticalState);
|
|
return Ok(self.vertical.prev_depth);
|
|
}
|
|
let index = self.register_index(files)?;
|
|
self.reads.insert(Dependency::BoxRegister(index));
|
|
Ok(self.state.box_register(index).map_or(0, |b| match p {
|
|
Primitive::Wd => b.width as i32,
|
|
Primitive::Ht => b.height as i32,
|
|
Primitive::Dp => b.depth as i32,
|
|
_ => unreachable!(),
|
|
}))
|
|
}
|
|
pub(super) fn assign_box_dimension(
|
|
&mut self,
|
|
p: Primitive,
|
|
files: &mut impl SourceProvider,
|
|
) -> Result<(), Error> {
|
|
let index = if p == Primitive::Prevdepth {
|
|
if self.paragraph.is_some() || !self.math.is_empty() {
|
|
return self.fail("prevdepth requires vertical mode");
|
|
};
|
|
None
|
|
} else {
|
|
Some(self.register_index(files)?)
|
|
};
|
|
self.optional_equals(files)?;
|
|
let n = self.dimension(false, false, files)?.amount;
|
|
if let Some(index) = index {
|
|
self.state.box_dimension(index, p, n)
|
|
} else {
|
|
self.vertical.prev_depth = n
|
|
};
|
|
Ok(())
|
|
}
|
|
}
|