206 lines
8.0 KiB
Rust
206 lines
8.0 KiB
Rust
//! TeX make_math_accent over actual font state and measured native noads.
|
|
use super::{math::*, *};
|
|
use crate::{
|
|
math_engine_tex::{MathGeometry, MathStyle, place_scripts},
|
|
opentype::{self, MathConstants, Stretch},
|
|
};
|
|
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 font = self.family_font(accent.family, style)?;
|
|
self.current_math_font.set(font);
|
|
let accent_font = self.state.fonts.font(font)?;
|
|
let opentype = accent_font.opentype();
|
|
let bottom = n.accent_style.bottom();
|
|
let mut skew = 0;
|
|
if let MathField::Char(c) = n.nucleus {
|
|
let base_font = self.family_font(c.family, style)?;
|
|
self.current_math_font.set(base_font);
|
|
if !self.has_math_char(base_font, c.slot)? {
|
|
self.missing.borrow_mut().push((base_font, c.slot));
|
|
n.nucleus = MathField::Empty;
|
|
} else if opentype.is_none()
|
|
&& let Ok(code) = u8::try_from(self.state.font_integers(base_font).skew)
|
|
&& let Ok(base_id) = self.state.fonts.glyph_font(base_font)
|
|
&& let Ok(slot) = u8::try_from(c.slot)
|
|
{
|
|
skew = base_id.metrics().kern(char::from(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;
|
|
let attachment = if opentype.is_some() && !bottom {
|
|
self.accent_base_attachment(&n.nucleus, style, &n.source.nucleus)?
|
|
} else {
|
|
None
|
|
};
|
|
let accent_height = if let Some(face) = opentype
|
|
&& let Some(math) = MathConstants::read(face)?
|
|
{
|
|
if bottom {
|
|
0
|
|
} else {
|
|
math.measure(math.values.accent_base_height())
|
|
}
|
|
} else {
|
|
i64::from(accent_font.x_height())
|
|
};
|
|
let mut delta = height.min(accent_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 nucleus_font = self.current_math_font.get();
|
|
let (mark, x) = if let Some(face) = opentype {
|
|
let (mut glyph, italic) = self.glyph(accent, style, &source)?;
|
|
glyph.metrics.width -= italic;
|
|
let original = opentype::single_glyph(&glyph).ok_or("missing OpenType accent glyph")?;
|
|
let positioned = if n.accent_style.fixed() {
|
|
(glyph, Some(original))
|
|
} else {
|
|
let character =
|
|
char::from_u32(accent.slot).ok_or("invalid OpenType accent scalar")?;
|
|
let stretched = opentype::stretch_glyph(
|
|
face,
|
|
original,
|
|
character,
|
|
&source,
|
|
Stretch::Horizontal,
|
|
width,
|
|
self.delimiter_glyph_limit
|
|
.saturating_sub(self.delimiter_glyphs.get()),
|
|
)?;
|
|
self.reserve_delimiter_glyphs(stretched.geometry.items.len())?;
|
|
(stretched.geometry, stretched.glyph)
|
|
};
|
|
let (mut mark, selected) = positioned;
|
|
let anchor = selected
|
|
.map(|glyph| opentype::accent_attachment(face, glyph))
|
|
.transpose()?
|
|
.unwrap_or((mark.metrics.width + 1).div_euclid(2));
|
|
if bottom {
|
|
mark.metrics.height = mark.metrics.height.max(0);
|
|
} else {
|
|
mark.metrics.depth = mark.metrics.depth.max(0);
|
|
}
|
|
let x = attachment.unwrap_or((width + 1).div_euclid(2)) - anchor;
|
|
(mark, x)
|
|
} else {
|
|
// Classic accents choose the last TFM list tag that fits the base.
|
|
let id = self.font(accent.family, style)?;
|
|
let metrics = id.metrics();
|
|
let mut visited = std::collections::BTreeSet::new();
|
|
while let Some(next) = metrics.next_larger(char::from(accent.slot as u8)) {
|
|
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 = u32::from(next);
|
|
}
|
|
let (mark, _) = self.glyph(accent, style, &source)?;
|
|
let x = skew + (width - mark.metrics.width + 1).div_euclid(2);
|
|
(mark, x)
|
|
};
|
|
// Drawing the already fetched accent does not replace the font left
|
|
// by clean_box on the nucleus.
|
|
self.current_math_font.set(nucleus_font);
|
|
let y = if bottom {
|
|
base.metrics.depth + mark.metrics.height
|
|
} else {
|
|
-base.metrics.height + delta - mark.metrics.depth
|
|
};
|
|
let result_width = base.metrics.width;
|
|
let (result_height, result_depth) = if bottom {
|
|
(base.metrics.height, y + mark.metrics.depth)
|
|
} else {
|
|
(height.max(mark.metrics.height - y), base.metrics.depth)
|
|
};
|
|
let mut result = MathGeometry::default();
|
|
if bottom {
|
|
result.append(base, 0, 0);
|
|
result.append(mark, x, y);
|
|
} else {
|
|
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()?;
|
|
self.current_math_font.set(nucleus_font);
|
|
Ok(place_scripts(
|
|
result,
|
|
upper,
|
|
lower,
|
|
0,
|
|
false,
|
|
self.params(style)?,
|
|
self.params(style.sup())?,
|
|
style,
|
|
self.state.parameter(Parameter::Scriptspace).width() as i64,
|
|
))
|
|
}
|
|
|
|
fn accent_base_attachment<'a>(
|
|
&self,
|
|
mut field: &'a MathField,
|
|
style: MathStyle,
|
|
mut source: &'a FieldSource,
|
|
) -> Result<Option<i64>, String> {
|
|
while let MathField::List(list) = field {
|
|
let Some(MathNode::Noad(noad)) = list.first() else {
|
|
return Ok(None);
|
|
};
|
|
if noad.accent.is_none() {
|
|
return Ok(None);
|
|
}
|
|
field = &noad.nucleus;
|
|
source = &noad.source.nucleus;
|
|
}
|
|
let MathField::Char(base) = *field else {
|
|
return Ok(None);
|
|
};
|
|
let font = self
|
|
.state
|
|
.fonts
|
|
.font(self.family_font(base.family, style)?)?;
|
|
let Some(face) = font.opentype() else {
|
|
return Ok(None);
|
|
};
|
|
let Some(character) = char::from_u32(base.slot) else {
|
|
return Ok(None);
|
|
};
|
|
let (geometry, _) = opentype::math_glyph(face, character, &source.paint)?;
|
|
opentype::single_glyph(&geometry)
|
|
.map(|glyph| opentype::accent_attachment(face, glyph))
|
|
.transpose()
|
|
}
|
|
}
|