Advance native LaTeX execution and preview

This commit is contained in:
gloamxun committed 2026-11-18 22:17:21 +08:00
1 parent 91f9d76b16
commit 2741546ca8
414 files changed
+99872 -933

No files matched your search

@@ -0,0 +1,178 @@
//! Owned make_scripts lists. The same layout decisions drive paint and boxes.
use super::{math::*, *};
use crate::math_engine_tex::{self as geometry, MathGeometry, MathStyle};
impl Converter<'_> {
pub(super) fn pack_field(&self, nodes: Vec<TextNode>) -> Result<NativeBox, String> {
// clean_box reuses a sole unshifted box, including a vertical one.
if nodes.len() == 1 {
if let TextNode::Box(b) = &nodes[0] {
if let Some(b) = &b.native_box {
if b.shift == 0 {
return Ok((**b).clone());
}
}
}
}
let mut b = super::boxes::hpack(
nodes,
super::boxes::PackSpec::Natural,
Span::default(),
self.state,
)?;
self.horizontal_packs.set(self.horizontal_packs.get() + 1);
self.last_pack_badness.set(Some(b.badness));
// clean_box removes a single character's trailing italic kern while
// preserving the packed declared width (tex.web clean_box).
if let BoxContent::Horizontal(nodes) = &mut b.content {
if nodes.len() == 2
&& matches!(nodes[0], TextNode::MathChar(_))
&& matches!(nodes[1], TextNode::Kern { .. })
{
nodes.pop();
b.diagnostic = super::diagnostics::text_nodes(nodes);
}
}
Ok(b)
}
pub(super) fn owned_field(
&self,
field: &MathField,
style: MathStyle,
source: &FieldSource,
) -> Result<NativeBox, String> {
match field {
MathField::Empty => self.pack_field(Vec::new()),
MathField::Radical(..) => self.pack_field(vec![TextNode::Box(super::math::math_box(
self.field(field, style, source)?,
))]),
MathField::Char(c) | MathField::TextChar(c) => {
let (mut g, italic) = self.glyph(*c, style, &source.paint)?;
g.metrics.width -= italic;
let mut nodes = Vec::new();
if !g.items.is_empty() {
nodes.push(TextNode::MathChar(super::math::math_box(g)));
if italic != 0 {
nodes.push(TextNode::Kern {
width: i32::try_from(italic)
.map_err(|_| "native math italic overflow")?,
explicit: false,
});
}
}
self.pack_field(nodes)
}
MathField::Box(b) if b.shift == 0 => Ok((**b).clone()),
MathField::Box(b) => {
self.pack_field(vec![TextNode::Box((**b).clone().horizontal_item())])
}
MathField::List(nodes) => self.pack_field(self.convert(nodes, style, false)?.0),
}
}
pub(super) fn script_noad(
&self,
n: &Noad,
style: MathStyle,
suppress: bool,
box_italic: i64,
) -> Result<Vec<TextNode>, String> {
let mut nodes = Vec::new();
let (body, italic) = match n.nucleus {
MathField::Char(c) | MathField::TextChar(c) => {
let (mut g, mut italic) = self.glyph(c, style, &n.source.nucleus.paint)?;
let mut character = g.clone();
character.metrics.width -= italic;
if suppress {
g.metrics.width -= italic;
italic = 0;
} else if n.sub.is_some() {
g.metrics.width -= italic;
}
if !character.items.is_empty() {
nodes.push(TextNode::MathChar(super::math::math_box(character)));
if italic != 0 && n.sub.is_none() {
nodes.push(TextNode::Kern {
width: i32::try_from(italic)
.map_err(|_| "native math italic overflow")?,
explicit: false,
});
}
}
(g, italic)
}
MathField::Empty => (MathGeometry::default(), 0),
_ => {
// The nucleus is already new_hlist, not a clean_box script
// field: retain its original shift and box kind. make_scripts
// packs a temporary hlist only to measure its physical extent.
let b = match &n.nucleus {
MathField::Box(b) => (**b).clone(),
MathField::List(list) => self.measure(&self.convert(list, style, false)?.0)?,
_ => self.owned_field(&n.nucleus, style, &n.source.nucleus)?,
};
let g = self
.measure(&[TextNode::Box(b.clone().horizontal_item())])?
.geometry()?;
nodes.push(TextNode::Box(b.horizontal_item()));
(g, box_italic)
}
};
let upper = n
.sup
.as_ref()
.map(|f| self.owned_field(f, style.sup(), &n.source.sup))
.transpose()?;
let lower = n
.sub
.as_ref()
.map(|f| self.owned_field(f, style.sub(), &n.source.sub))
.transpose()?;
let (_, placement) = geometry::place_scripts_with_layout(
body,
upper.as_ref().map(NativeBox::geometry).transpose()?,
lower.as_ref().map(NativeBox::geometry).transpose()?,
italic,
matches!(n.nucleus, MathField::Char(_) | MathField::TextChar(_)),
self.params(style)?,
self.params(style.sup())?,
style,
i64::from(self.state.parameter(Parameter::Scriptspace).width()),
);
let space = i64::from(self.state.parameter(Parameter::Scriptspace).width());
let scripts = match (upper, lower) {
(Some(mut upper), Some(mut lower)) => {
upper.width += space;
lower.width += space;
upper.shift = italic;
let gap = placement.up - upper.depth - lower.height + placement.down;
let mut b = super::boxes::vpack(
vec![
NativeVerticalNode::Box(Box::new(upper)),
NativeVerticalNode::ImplicitKern(
i32::try_from(gap).map_err(|_| "native math script gap overflow")?,
),
NativeVerticalNode::Box(Box::new(lower)),
],
super::boxes::PackSpec::Natural,
1_073_741_823,
BoxKind::Vertical,
Span::default(),
)?;
b.shift = placement.down;
b
}
(Some(mut upper), None) => {
upper.width += space;
upper.shift = -placement.up;
upper
}
(None, Some(mut lower)) => {
lower.width += space;
lower.shift = placement.down;
lower
}
(None, None) => return Ok(nodes),
};
nodes.push(TextNode::Box(scripts.horizontal_item()));
Ok(nodes)
}
}