Match XeTeX math script kerning and fraction font selection

This commit is contained in:
gloamxun committed 2026-11-21 15:33:05 +08:00
1 parent 821631778e
commit a31de14eb2
9 files changed
+255 -12

No files matched your search

@@ -1,6 +1,9 @@
//! 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};
use crate::{
math_engine_tex::{self as geometry, MathGeometry, MathStyle},
opentype::{self, OpenTypeFont, ScriptSide},
};
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.
@@ -116,6 +119,7 @@ impl Converter<'_> {
(g, box_italic)
}
};
let base_font = self.current_math_font.get();
let upper = n
.sup
.as_ref()
@@ -126,6 +130,7 @@ impl Converter<'_> {
.as_ref()
.map(|f| self.owned_field(f, style.sub(), &n.source.sub))
.transpose()?;
self.current_math_font.set(base_font);
let script_bounds = if let MathField::Char(c) | MathField::TextChar(c) = n.nucleus {
let font = self.state.fonts.font(self.family_font(c.family, style)?)?;
if let Some(face) = font.opentype()
@@ -135,9 +140,8 @@ impl Converter<'_> {
subscript_top_max: math.measure(math.values.subscript_top_max()),
superscript_bottom_min: math.measure(math.values.superscript_bottom_min()),
paired_gap: math.measure(math.values.sub_superscript_gap_min()),
paired_superscript_bottom: math.measure(
math.values.superscript_bottom_max_with_subscript(),
),
paired_superscript_bottom: math
.measure(math.values.superscript_bottom_max_with_subscript()),
})
} else {
None
@@ -145,6 +149,15 @@ impl Converter<'_> {
} else {
None
};
let base_glyph = if matches!(n.nucleus, MathField::Char(_) | MathField::TextChar(_)) {
self.state
.fonts
.font(base_font)?
.opentype()
.zip(opentype::single_glyph(&body))
} else {
None
};
let (_, placement) = geometry::place_scripts_with_layout(
body,
upper.as_ref().map(NativeBox::geometry).transpose()?,
@@ -157,12 +170,28 @@ impl Converter<'_> {
i64::from(self.state.parameter(Parameter::Scriptspace).width()),
script_bounds,
);
let kern = if let Some((font, glyph)) = base_glyph {
let (script, side, shift) = if lower.is_some() {
(n.sub.as_ref(), ScriptSide::Lower, placement.down)
} else {
(n.sup.as_ref(), ScriptSide::Upper, placement.up)
};
self.script_kern((font, glyph), script, style, side, shift)?
} else {
0
};
if kern != 0 {
nodes.push(TextNode::Kern {
width: i32::try_from(kern).map_err(|_| "native math script kern overflow")?,
explicit: false,
});
}
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;
upper.shift = italic - kern;
let gap = placement.up - upper.depth - lower.height + placement.down;
let mut b = super::boxes::vpack(
vec![
@@ -195,4 +224,63 @@ impl Converter<'_> {
nodes.push(TextNode::Box(scripts.horizontal_item()));
Ok(nodes)
}
fn script_kern(
&self,
base: (&OpenTypeFont, u16),
script: Option<&MathField>,
style: MathStyle,
side: ScriptSide,
shift: i64,
) -> Result<i64, String> {
let Some((character, style)) =
script.and_then(|field| first_script_char(field, style.sub()))
else {
return Ok(0);
};
let id = self.family_font(character.family, style)?;
let Some(font) = self.state.fonts.font(id)?.opentype() else {
return Ok(0);
};
let Some(scalar) = char::from_u32(character.slot) else {
return Ok(0);
};
if !self.has_math_char(id, character.slot)? {
return Ok(0);
}
let (geometry, _) = opentype::math_glyph(font, scalar, &Default::default())?;
let Some(glyph) = opentype::single_glyph(&geometry) else {
return Ok(0);
};
opentype::script_kern(base, (font, glyph), side, shift)
}
}
fn first_script_char(field: &MathField, mut style: MathStyle) -> Option<(MathChar, MathStyle)> {
let mut list = match field {
MathField::Char(c) => return Some((*c, style)),
MathField::List(list) => list.as_slice(),
_ => return None,
};
loop {
let (node, rest) = list.split_first()?;
list = rest;
match node {
MathNode::Style(next) => style = *next,
MathNode::Choice(branches) => list = &branches[usize::from(style as u8 / 2)],
MathNode::Kern { .. } | MathNode::Glue(_) | MathNode::ExplicitSpace(..) => {}
MathNode::Noad(n)
if n.class != NoadClass::Inner
&& n.accent.is_none()
&& n.rule.is_none()
&& !n.vcenter =>
{
return match n.nucleus {
MathField::Char(c) => Some((c, style)),
_ => None,
};
}
_ => return None,
}
}
}