//! 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}, opentype::{self, OpenTypeFont, ScriptSide}, }; impl Converter<'_> { pub(super) fn pack_field(&self, nodes: Vec) -> Result { // 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 { 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, 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 base_font = self.current_math_font.get(); 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()?; 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() && let Some(math) = crate::opentype::MathConstants::read(face)? { Some(geometry::ScriptBounds { 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()), }) } else { None } } 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()?, 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()), 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 - kern; 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) } fn script_kern( &self, base: (&OpenTypeFont, u16), script: Option<&MathField>, style: MathStyle, side: ScriptSide, shift: i64, ) -> Result { 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, } } }