Advance native LaTeX execution and preview
This commit is contained in:
1 parent
91f9d76b16
commit
2741546ca8
414 files changed
+99872
-933
No files matched your search
@@ -0,0 +1,940 @@
|
||||
use super::super::boxes::{self, PackSpec, VerticalBuilder};
|
||||
use super::*;
|
||||
#[derive(Clone, Copy)]
|
||||
pub(super) enum Destination {
|
||||
Append,
|
||||
Register(u16, bool),
|
||||
Shift(i32),
|
||||
Shipout,
|
||||
Adjustment { pre: bool },
|
||||
Insertion { class: u8 },
|
||||
}
|
||||
pub(super) struct BoxFrame {
|
||||
kind: BoxKind,
|
||||
spec: PackSpec,
|
||||
group: GroupKind,
|
||||
depth: usize,
|
||||
destination: Destination,
|
||||
paragraph: Option<super::super::paragraph::Builder>,
|
||||
vertical: VerticalBuilder,
|
||||
span: Span,
|
||||
discretionary: Option<DiscretionaryFrame>,
|
||||
math: Vec<super::super::math::MathBuilder>,
|
||||
// An inner $...$ belongs to this box, even inside an outer display.
|
||||
display: Option<super::display::DisplayFrame>,
|
||||
}
|
||||
struct DiscretionaryFrame {
|
||||
parts: Vec<Vec<TextNode>>,
|
||||
font: FontId,
|
||||
span: Span,
|
||||
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> {
|
||||
let vertical = matches!(p, Primitive::Unvbox | Primitive::Unvcopy);
|
||||
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)?;
|
||||
}
|
||||
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),
|
||||
BoxContent::Vertical(nodes) => {
|
||||
let start = self.vertical.nodes.len();
|
||||
self.vertical.extend(nodes);
|
||||
// unpackage transfers nodes without append_to_vlist. In
|
||||
// particular it neither inserts glue nor updates prev_depth.
|
||||
self.emit_vertical(start);
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
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::Kern { .. }
|
||||
| TextNode::Box(_)
|
||||
| TextNode::Indent(_)
|
||||
| TextNode::Rule(_)
|
||||
)
|
||||
}) {
|
||||
return self.fail(
|
||||
"improper native discretionary material: only glyphs, boxes, rules and kerns are allowed",
|
||||
);
|
||||
}
|
||||
self.box_nodes_created = self.box_nodes_created.saturating_add(part.len());
|
||||
if self.box_nodes_created > self.limits.tokens {
|
||||
return self.fail("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.box_nodes_created = self.box_nodes_created.saturating_add(b.node_count());
|
||||
if self.box_nodes_created > self.limits.tokens {
|
||||
return self.fail("native box node capacity exceeded");
|
||||
};
|
||||
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 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));
|
||||
// Splitting clears all split classes globally, including a void box.
|
||||
self.state.split_marks.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 tail = super::super::vertical_break::prune_top(
|
||||
tail,
|
||||
skip,
|
||||
&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(),
|
||||
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
|
||||
};
|
||||
boxes::hpack(nodes, frame.spec, frame.span, &self.state)
|
||||
} else {
|
||||
boxes::vpack(
|
||||
self.vertical.take_nodes(),
|
||||
frame.spec,
|
||||
max_depth.expect("vertical box maximum depth"),
|
||||
frame.kind,
|
||||
frame.span,
|
||||
)
|
||||
};
|
||||
let b = result.map_err(|e| self.error(e, frame.span))?;
|
||||
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::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(())
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user