Render XeTeX OpenType math accents and stretch constructions
This commit is contained in:
1 parent
01b3931ae0
commit
821631778e
16 files changed
+825
-144
No files matched your search
@@ -1,41 +1,51 @@
|
||||
//! TeX make_math_accent over actual font state and measured native noads.
|
||||
use super::{math::*, *};
|
||||
use crate::math_engine_tex::{MathGeometry, MathStyle, place_scripts};
|
||||
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 id = self.font(accent.family, style)?;
|
||||
let font = self
|
||||
.state
|
||||
.math_font(style.size_index() as u8, accent.family);
|
||||
let font = self.family_font(accent.family, style)?;
|
||||
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_id = self.font(c.family, style)?;
|
||||
let base_font = self.state.math_font(style.size_index() as u8, c.family);
|
||||
let base_font = self.family_font(c.family, style)?;
|
||||
if !self.has_math_char(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 as u8), char::from(code));
|
||||
} 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;
|
||||
// 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 as u8)) {
|
||||
if !visited.insert(accent.slot) {
|
||||
return Err("cyclic native accent size list".into());
|
||||
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())
|
||||
}
|
||||
if !self.state.font_has_slot(font, next) || metrics.width(char::from(next)) > width {
|
||||
break;
|
||||
}
|
||||
accent.slot = u32::from(next);
|
||||
}
|
||||
let mut delta = height.min(i64::from(self.state.fonts.font(font)?.x_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.
|
||||
@@ -50,16 +60,78 @@ impl Converter<'_> {
|
||||
.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 (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)
|
||||
};
|
||||
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_depth = base.metrics.depth;
|
||||
let result_height =
|
||||
height.max(base.metrics.height + mark.metrics.height + mark.metrics.depth - delta);
|
||||
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();
|
||||
result.append(mark, x, y);
|
||||
result.append(base, 0, 0);
|
||||
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;
|
||||
@@ -88,4 +160,36 @@ impl Converter<'_> {
|
||||
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 (geometry, _) = self.glyph(base, style, &source.paint)?;
|
||||
opentype::single_glyph(&geometry)
|
||||
.map(|glyph| opentype::accent_attachment(face, glyph))
|
||||
.transpose()
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user