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,91 @@
//! TeX make_math_accent over actual font state and measured native noads.
use super::{math::*, *};
use crate::math_engine_tex::{MathGeometry, MathStyle, place_scripts};
impl Converter<'_> {
pub(super) fn accent(&self, original: &Noad, style: MathStyle) -> Result<MathGeometry, String> {
let mut n = original.clone();
let mut accent = n.accent.take().unwrap();
let id = self.font(accent.family, style)?;
let font = self
.state
.math_font(style.size_index() as u8, accent.family);
let mut skew = 0;
if let MathField::Char(c) = n.nucleus {
let base_id = self.font(c.family, style)?;
let base_font = self.state.math_font(style.size_index() as u8, c.family);
if !self.state.font_has_slot(base_font, c.slot) {
self.missing.borrow_mut().push((base_font, c.slot));
n.nucleus = MathField::Empty;
} else if let Ok(code) = u8::try_from(self.state.font_integers(base_font).skew) {
skew = base_id.metrics().kern(char::from(c.slot), char::from(code));
}
}
let mut base = self.field(&n.nucleus, style.cramped_style(), &n.source.nucleus)?;
let width = base.metrics.width;
let mut height = base.metrics.height;
// Larger accents are chosen by actual TFM list tags and advance widths.
let metrics = id.metrics();
let mut visited = std::collections::BTreeSet::new();
while let Some(next) = metrics.next_larger(char::from(accent.slot)) {
if !visited.insert(accent.slot) {
return Err("cyclic native accent size list".into());
}
if !self.state.font_has_slot(font, next) || metrics.width(char::from(next)) > width {
break;
}
accent.slot = next;
}
let mut delta = height.min(i64::from(self.state.fonts.font(font)?.x_height()));
if matches!(n.nucleus, MathField::Char(_)) && (n.sub.is_some() || n.sup.is_some()) {
// TeX moves scripts inside the nucleus, preserving the original
// un-scripted width for accent centering and choosing the variant.
base = self.clean(&self.convert(&[MathNode::Noad(n.clone())], style, false)?.0)?;
delta += base.metrics.height - height;
height = base.metrics.height;
n.sub = None;
n.sup = None;
}
let source = n
.source
.operator
.clone()
.derived(crate::provenance::Derivation::Accent);
let (mark, _) = self.glyph(accent, style, &source)?;
let x = skew + (width - mark.metrics.width + 1).div_euclid(2);
let y = -base.metrics.height + delta - mark.metrics.depth;
let result_width = base.metrics.width;
let result_depth = base.metrics.depth;
let result_height =
height.max(base.metrics.height + mark.metrics.height + mark.metrics.depth - delta);
let mut result = MathGeometry::default();
result.append(mark, x, y);
result.append(base, 0, 0);
result.metrics.width = result_width;
result.metrics.height = result_height;
result.metrics.depth = result_depth;
if n.sub.is_none() && n.sup.is_none() {
return Ok(result);
}
let upper = n
.sup
.as_ref()
.map(|f| self.field(f, style.sup(), &n.source.sup))
.transpose()?;
let lower = n
.sub
.as_ref()
.map(|f| self.field(f, style.sub(), &n.source.sub))
.transpose()?;
Ok(place_scripts(
result,
upper,
lower,
0,
false,
self.params(style)?,
self.params(style.sup())?,
style,
self.state.parameter(Parameter::Scriptspace).width() as i64,
))
}
}