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
@@ -245,6 +245,7 @@ impl Executor {
|
||||
class,
|
||||
limits: OperatorLimits::Display,
|
||||
accent: None,
|
||||
accent_style: AccentStyle::default(),
|
||||
rule: None,
|
||||
vcenter: false,
|
||||
nucleus,
|
||||
@@ -706,12 +707,28 @@ impl Executor {
|
||||
n.source.operator = PaintSource::token(TokenOrigin::from(token));
|
||||
b.pending = Some(FieldTarget::Nucleus(i));
|
||||
}
|
||||
Mathaccent => {
|
||||
let code = self.integer(files)?;
|
||||
if !(0..32768).contains(&code) {
|
||||
return self.fail("mathaccent must be in 0..32767");
|
||||
}
|
||||
let (_, accent) = self.decode_math_char(code as u16);
|
||||
Mathaccent | Umathaccent => {
|
||||
let (accent, accent_style) = if *p == Umathaccent {
|
||||
let mode = if self.keyword("fixed", files)? {
|
||||
AccentStyle::Fixed
|
||||
} else if self.keyword("bottom", files)? {
|
||||
if self.keyword("fixed", files)? {
|
||||
AccentStyle::BottomFixed
|
||||
} else {
|
||||
AccentStyle::Bottom
|
||||
}
|
||||
} else {
|
||||
AccentStyle::Top
|
||||
};
|
||||
let code = self.scan_unicode_math_code(true, files)?;
|
||||
(self.explicit_math_code(code)?.1, mode)
|
||||
} else {
|
||||
let code = self.integer(files)?;
|
||||
if !(0..32768).contains(&code) {
|
||||
return self.fail("mathaccent must be in 0..32767");
|
||||
}
|
||||
(self.decode_math_char(code as u16).1, AccentStyle::Top)
|
||||
};
|
||||
self.new_noad(NoadClass::Ord, MathField::Empty, token.span);
|
||||
let b = self.math.last_mut().unwrap();
|
||||
let i = b.nodes.len() - 1;
|
||||
@@ -719,6 +736,7 @@ impl Executor {
|
||||
unreachable!()
|
||||
};
|
||||
n.accent = Some(accent);
|
||||
n.accent_style = accent_style;
|
||||
n.source.operator = PaintSource::token(TokenOrigin::from(token));
|
||||
b.pending = Some(FieldTarget::Nucleus(i));
|
||||
}
|
||||
|
||||
@@ -8,9 +8,9 @@ use std::{
|
||||
sync::{Arc, Mutex, OnceLock, Weak},
|
||||
};
|
||||
|
||||
const SCHEMA: u32 = 66;
|
||||
const COMPATIBLE_SCHEMAS: [u32; 33] = [
|
||||
27, 28, 31, 32, 33, 34, 35, 36, 37, 38, 39, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 64, 65, SCHEMA,
|
||||
const SCHEMA: u32 = 67;
|
||||
const COMPATIBLE_SCHEMAS: [u32; 34] = [
|
||||
27, 28, 31, 32, 33, 34, 35, 36, 37, 38, 39, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 64, 65, 66, SCHEMA,
|
||||
];
|
||||
const ENGINE: &str = concat!("texrra-native/", env!("CARGO_PKG_VERSION"));
|
||||
|
||||
|
||||
@@ -149,12 +149,37 @@ pub enum MathRulePosition {
|
||||
Over,
|
||||
Under,
|
||||
}
|
||||
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
|
||||
pub enum AccentStyle {
|
||||
#[default]
|
||||
Top,
|
||||
Fixed,
|
||||
Bottom,
|
||||
BottomFixed,
|
||||
}
|
||||
|
||||
impl AccentStyle {
|
||||
fn is_default(&self) -> bool {
|
||||
*self == Self::Top
|
||||
}
|
||||
|
||||
pub(super) fn bottom(self) -> bool {
|
||||
matches!(self, Self::Bottom | Self::BottomFixed)
|
||||
}
|
||||
|
||||
pub(super) fn fixed(self) -> bool {
|
||||
matches!(self, Self::Fixed | Self::BottomFixed)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
|
||||
pub struct Noad {
|
||||
pub source: NoadSource,
|
||||
pub class: NoadClass,
|
||||
pub limits: OperatorLimits,
|
||||
pub accent: Option<MathChar>,
|
||||
#[serde(default, skip_serializing_if = "AccentStyle::is_default")]
|
||||
pub accent_style: AccentStyle,
|
||||
pub rule: Option<MathRulePosition>,
|
||||
/// A vcenter noad retains its natural vertical box until math conversion,
|
||||
/// when the current style selects the symbol font's mathematical axis.
|
||||
@@ -363,11 +388,7 @@ pub(super) struct Converter<'a> {
|
||||
pub node_limit: usize,
|
||||
}
|
||||
impl Converter<'_> {
|
||||
pub(super) fn font(
|
||||
&self,
|
||||
family: u8,
|
||||
style: MathStyle,
|
||||
) -> Result<&std::sync::Arc<crate::native_font::NativeFont>, String> {
|
||||
pub(super) fn family_font(&self, family: u8, style: MathStyle) -> Result<FontId, String> {
|
||||
let id = self.state.math_font(style.size_index() as u8, family);
|
||||
if id == FontId::NULL {
|
||||
return Err(format!(
|
||||
@@ -375,38 +396,49 @@ impl Converter<'_> {
|
||||
style.size_index()
|
||||
));
|
||||
}
|
||||
self.state.fonts.glyph_font(id).map_err(|_| {
|
||||
format!("native math family {family} size {} requires OpenType math layout here", style.size_index())
|
||||
})
|
||||
Ok(id)
|
||||
}
|
||||
|
||||
pub(super) fn font(
|
||||
&self,
|
||||
family: u8,
|
||||
style: MathStyle,
|
||||
) -> Result<&std::sync::Arc<crate::native_font::NativeFont>, String> {
|
||||
self.state.fonts.glyph_font(self.family_font(family, style)?)
|
||||
}
|
||||
|
||||
pub fn validate(&self) -> Result<(), String> {
|
||||
for s in [MathStyle::Text, MathStyle::Script, MathStyle::ScriptScript] {
|
||||
if self
|
||||
.state
|
||||
.fonts
|
||||
.font(self.font(2, s)?.id)?
|
||||
.parameters()
|
||||
.len()
|
||||
< 22
|
||||
|| self
|
||||
.state
|
||||
.fonts
|
||||
.font(self.font(3, s)?.id)?
|
||||
.parameters()
|
||||
.len()
|
||||
< 13
|
||||
{
|
||||
return Err("insufficient native symbol/extension font parameters".into());
|
||||
for style in [MathStyle::Text, MathStyle::Script, MathStyle::ScriptScript] {
|
||||
for (family, required) in [(2, 22), (3, 13)] {
|
||||
let font = self.state.fonts.font(self.family_font(family, style)?)?;
|
||||
let sufficient = if let Some(font) = font.opentype() {
|
||||
crate::opentype::MathConstants::read(font)?.is_some()
|
||||
} else {
|
||||
font.parameters().len() >= required
|
||||
};
|
||||
if !sufficient {
|
||||
return Err("insufficient native symbol/extension font parameters".into());
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub(super) fn params(&self, style: MathStyle) -> Result<MathParams, String> {
|
||||
let symbol = self.state.fonts.font(self.font(2, style)?.id)?.parameters();
|
||||
let extension = self.state.fonts.font(self.font(3, style)?.id)?.parameters();
|
||||
use crate::opentype::MathConstants;
|
||||
let symbol = self.state.fonts.font(self.family_font(2, style)?)?;
|
||||
let extension = self.state.fonts.font(self.family_font(3, style)?)?;
|
||||
let symbol_math = symbol.opentype().map(|font| MathConstants::read(font)).transpose()?.flatten();
|
||||
let extension_math = extension.opentype().map(|font| MathConstants::read(font)).transpose()?.flatten();
|
||||
Ok(MathParams::from_parameters(
|
||||
|n| i64::from(symbol[n]),
|
||||
|n| i64::from(extension[n]),
|
||||
|n| symbol_math.as_ref().map_or_else(
|
||||
|| i64::from(symbol.parameters()[n]),
|
||||
|math| math.symbol_parameter(n),
|
||||
),
|
||||
|n| extension_math.as_ref().map_or_else(
|
||||
|| i64::from(extension.parameters()[n]),
|
||||
|math| math.extension_parameter(n),
|
||||
),
|
||||
))
|
||||
}
|
||||
pub(super) fn has_math_char(&self, id: FontId, slot: u32) -> Result<bool, String> {
|
||||
@@ -427,7 +459,7 @@ impl Converter<'_> {
|
||||
let font = self.state.math_font(style.size_index() as u8, c.family);
|
||||
if font == FontId::NULL {
|
||||
// Keep the same diagnostic as the legacy fetch path.
|
||||
self.font(c.family, style)?;
|
||||
self.family_font(c.family, style)?;
|
||||
}
|
||||
if !self.has_math_char(font, c.slot)? {
|
||||
self.missing.borrow_mut().push((font, c.slot));
|
||||
@@ -580,10 +612,7 @@ impl Converter<'_> {
|
||||
// A missing accent leaves an ordinary fetched nucleus,
|
||||
// including its character/trailing-kern visibility.
|
||||
if let Some(accent) = n.accent {
|
||||
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)?;
|
||||
if !self.has_math_char(font, accent.slot)? {
|
||||
self.missing.borrow_mut().push((font, accent.slot));
|
||||
n.accent = None;
|
||||
|
||||
@@ -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()
|
||||
}
|
||||
}
|
||||
@@ -51,7 +51,7 @@ impl Converter<'_> {
|
||||
metrics.depth,
|
||||
))
|
||||
}
|
||||
fn reserve_delimiter_glyphs(&self, n: usize) -> Result<(), String> {
|
||||
pub(super) fn reserve_delimiter_glyphs(&self, n: usize) -> Result<(), String> {
|
||||
let used = self
|
||||
.delimiter_glyphs
|
||||
.get()
|
||||
@@ -95,6 +95,7 @@ impl Converter<'_> {
|
||||
source: &PaintSource,
|
||||
) -> Result<NativeBox, String> {
|
||||
let mut best = None;
|
||||
let mut opentype_best = None;
|
||||
let mut largest = 0;
|
||||
'search: for c in [value.small, value.large] {
|
||||
// (family 0, slot 0) is the null delimiter, not a Gamma request.
|
||||
@@ -106,8 +107,22 @@ impl Converter<'_> {
|
||||
if font == FontId::NULL {
|
||||
continue;
|
||||
}
|
||||
if self.state.fonts.font(font)?.opentype().is_some() {
|
||||
return Err("native OpenType delimiter requires MATH variants and assemblies".into());
|
||||
if let Some(face) = self.state.fonts.font(font)?.opentype() {
|
||||
let Some(character) = char::from_u32(c.slot) else { continue };
|
||||
let Some(glyph) = face.face()?.glyph_index(character) else { continue };
|
||||
let shaped = crate::opentype::stretch_glyph(
|
||||
face, glyph.0, character, source, crate::opentype::Stretch::Vertical, target,
|
||||
self.delimiter_glyph_limit.saturating_sub(self.delimiter_glyphs.get()),
|
||||
)?;
|
||||
let metrics = shaped.geometry.metrics;
|
||||
let enough = shaped.sufficient;
|
||||
largest = metrics.height + metrics.depth;
|
||||
best = None;
|
||||
opentype_best = Some(shaped.geometry);
|
||||
if enough {
|
||||
break 'search;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
let id = self.state.fonts.glyph_font(font)?;
|
||||
let m = id.metrics();
|
||||
@@ -120,12 +135,14 @@ impl Converter<'_> {
|
||||
return Err("cyclic native delimiter size list".into());
|
||||
}
|
||||
if m.extensible_recipe(char::from(slot)).is_some() {
|
||||
opentype_best = None;
|
||||
best = Some((id, slot));
|
||||
break 'search;
|
||||
}
|
||||
let extent = m.height(char::from(slot)) + m.depth(char::from(slot));
|
||||
if extent > largest {
|
||||
largest = extent;
|
||||
opentype_best = None;
|
||||
best = Some((id, slot));
|
||||
if extent >= target {
|
||||
break 'search;
|
||||
@@ -138,7 +155,14 @@ impl Converter<'_> {
|
||||
}
|
||||
}
|
||||
}
|
||||
let result = if let Some((id, slot)) = best {
|
||||
let result = if let Some(geometry) = opentype_best {
|
||||
self.reserve_delimiter_glyphs(geometry.items.len())?;
|
||||
let metrics = geometry.metrics;
|
||||
delimiter_native_box(
|
||||
BoxContent::Horizontal(vec![TextNode::Box(super::math::math_box(geometry))]),
|
||||
metrics.width, metrics.height, metrics.depth,
|
||||
)
|
||||
} else if let Some((id, slot)) = best {
|
||||
if let Some(recipe) = id.metrics().extensible_recipe(char::from(slot)) {
|
||||
let repeat = character(id, recipe.repeat, source)?;
|
||||
let unit = repeat.metrics.height + repeat.metrics.depth;
|
||||
@@ -219,13 +243,26 @@ impl Converter<'_> {
|
||||
source.radical.as_deref().unwrap_or(&FieldSource::UNKNOWN),
|
||||
)?;
|
||||
let extent = body.metrics.height + body.metrics.depth;
|
||||
let thickness = params.rule_thickness;
|
||||
let mut clearance = thickness
|
||||
+ if style.display() {
|
||||
let id = self.state.math_font(style.size_index() as u8, delimiter.small.family);
|
||||
let selected = self.state.fonts.font(id)?;
|
||||
let constants = selected.opentype().map(|font| crate::opentype::MathConstants::read(font))
|
||||
.transpose()?.flatten();
|
||||
let (thickness, mut clearance) = if let Some(math) = &constants {
|
||||
let gap = if style.display() {
|
||||
math.values.radical_display_style_vertical_gap()
|
||||
} else {
|
||||
math.values.radical_vertical_gap()
|
||||
};
|
||||
(math.measure(math.values.radical_rule_thickness()), math.measure(gap))
|
||||
} else {
|
||||
let thickness = params.rule_thickness;
|
||||
let clearance = thickness + if style.display() {
|
||||
params.x_height.abs()
|
||||
} else {
|
||||
thickness.abs()
|
||||
} / 4;
|
||||
(thickness, clearance)
|
||||
};
|
||||
let sign_source = source.paint.clone().derived(Derivation::Radical);
|
||||
let mut sign = self.delimiter_box(
|
||||
delimiter,
|
||||
@@ -233,6 +270,14 @@ impl Converter<'_> {
|
||||
extent + clearance + thickness,
|
||||
&sign_source,
|
||||
)?;
|
||||
if constants.is_some() {
|
||||
let baseline = sign.metrics.height - thickness;
|
||||
for item in &mut sign.items {
|
||||
item.y += baseline;
|
||||
}
|
||||
sign.metrics.depth += baseline;
|
||||
sign.metrics.height = thickness;
|
||||
}
|
||||
let excess = sign.metrics.depth - extent - clearance;
|
||||
if excess > 0 {
|
||||
clearance += (excess + 1) / 2;
|
||||
|
||||
@@ -14,6 +14,12 @@ impl Converter<'_> {
|
||||
) -> Result<NativeBox, String> {
|
||||
let params = self.params(style)?;
|
||||
let thickness = spec.thickness.map_or(params.rule_thickness, i64::from);
|
||||
let extension = self.state.fonts.font(self.family_font(3, style)?)?;
|
||||
let constants = extension
|
||||
.opentype()
|
||||
.map(|font| crate::opentype::MathConstants::read(font))
|
||||
.transpose()?
|
||||
.flatten();
|
||||
let mut upper = self.pack_field(self.convert(numerator, style.numerator(), false)?.0)?;
|
||||
let mut lower =
|
||||
self.pack_field(self.convert(denominator, style.denominator(), false)?.0)?;
|
||||
@@ -36,7 +42,15 @@ impl Converter<'_> {
|
||||
};
|
||||
let half = (thickness + 1).div_euclid(2);
|
||||
if thickness == 0 {
|
||||
let clearance = if style.display() { 7 } else { 3 } * params.rule_thickness;
|
||||
let clearance = if let Some(math) = &constants {
|
||||
math.measure(if style.display() {
|
||||
math.values.stack_display_style_gap_min()
|
||||
} else {
|
||||
math.values.stack_gap_min()
|
||||
})
|
||||
} else {
|
||||
(if style.display() { 7 } else { 3 }) * params.rule_thickness
|
||||
};
|
||||
let delta =
|
||||
(clearance - ((up - upper.depth) - (lower.height - down)) + 1).div_euclid(2);
|
||||
if delta > 0 {
|
||||
@@ -44,13 +58,29 @@ impl Converter<'_> {
|
||||
down += delta;
|
||||
}
|
||||
} else {
|
||||
let clearance = if style.display() {
|
||||
3 * thickness
|
||||
let (above, below) = if let Some(math) = &constants {
|
||||
let (above, below) = if style.display() {
|
||||
(
|
||||
math.values.fraction_num_display_style_gap_min(),
|
||||
math.values.fraction_denom_display_style_gap_min(),
|
||||
)
|
||||
} else {
|
||||
(
|
||||
math.values.fraction_numerator_gap_min(),
|
||||
math.values.fraction_denominator_gap_min(),
|
||||
)
|
||||
};
|
||||
(math.measure(above), math.measure(below))
|
||||
} else {
|
||||
thickness
|
||||
let clearance = if style.display() {
|
||||
3 * thickness
|
||||
} else {
|
||||
thickness
|
||||
};
|
||||
(clearance, clearance)
|
||||
};
|
||||
up = up.max(upper.depth + params.axis_height + half + clearance);
|
||||
down = down.max(lower.height - params.axis_height + half + clearance);
|
||||
up = up.max(upper.depth + params.axis_height + half + above);
|
||||
down = down.max(lower.height - params.axis_height + half + below);
|
||||
}
|
||||
let width = upper.width;
|
||||
let height = up + upper.height;
|
||||
|
||||
@@ -85,27 +85,48 @@ impl Converter<'_> {
|
||||
None
|
||||
};
|
||||
let (mut body, italic) = if let MathField::Char(mut c) = n.nucleus {
|
||||
let face = self.font(c.family, style)?;
|
||||
let id = self.family_font(c.family, style)?;
|
||||
let mut source = n.source.nucleus.clone();
|
||||
let (mut b, italic) = if !self.has_math_char(face.id, c.slot)? {
|
||||
self.missing.borrow_mut().push((face.id, c.slot));
|
||||
let font = self.state.fonts.font(id)?;
|
||||
let (mut b, italic) = if !self.has_math_char(id, c.slot)? {
|
||||
self.missing.borrow_mut().push((id, c.slot));
|
||||
(self.pack_field(Vec::new())?, 0)
|
||||
} else if let Some(face) = font.opentype() {
|
||||
use crate::opentype::{self, MathConstants, Stretch};
|
||||
let (mut body, mut italic) = self.glyph(c, style, &source.paint)?;
|
||||
body.metrics.width -= italic;
|
||||
if style.display() && let Some(math) = MathConstants::read(face)? {
|
||||
let min = crate::opentype::scale_units(
|
||||
i64::from(math.values.display_operator_min_height()),
|
||||
face.size, face.face()?.units_per_em(),
|
||||
);
|
||||
let target = min.max((body.metrics.height + body.metrics.depth) * 5 / 4);
|
||||
let glyph = opentype::single_glyph(&body).ok_or("missing OpenType operator glyph")?;
|
||||
let character = char::from_u32(c.slot).ok_or("invalid OpenType operator scalar")?;
|
||||
let variant = opentype::stretch_glyph(
|
||||
face, glyph, character,
|
||||
&source.paint.clone().derived(crate::provenance::Derivation::MathVariant),
|
||||
Stretch::Vertical, target,
|
||||
self.delimiter_glyph_limit.saturating_sub(self.delimiter_glyphs.get()),
|
||||
)?;
|
||||
self.reserve_delimiter_glyphs(variant.geometry.items.len())?;
|
||||
body = variant.geometry;
|
||||
italic = variant.italic;
|
||||
}
|
||||
(self.pack_field(vec![TextNode::MathChar(super::math::math_box(body))])?, italic)
|
||||
} else {
|
||||
if style.display() {
|
||||
if let Some(next) = face.metrics().next_larger(char::from(c.slot as u8)) {
|
||||
if self.state.font_has_slot(face.id, next) {
|
||||
c.slot = u32::from(next);
|
||||
source.paint = source
|
||||
.paint
|
||||
.derived(crate::provenance::Derivation::MathVariant);
|
||||
}
|
||||
}
|
||||
let face = self.font(c.family, style)?;
|
||||
if style.display()
|
||||
&& let Some(next) = face.metrics().next_larger(char::from(c.slot as u8))
|
||||
&& self.state.font_has_slot(id, next)
|
||||
{
|
||||
c.slot = u32::from(next);
|
||||
source.paint = source
|
||||
.paint
|
||||
.derived(crate::provenance::Derivation::MathVariant);
|
||||
}
|
||||
let italic = face.metrics().italic(char::from(c.slot as u8));
|
||||
(
|
||||
self.owned_field(&MathField::Char(c), style, &source)?,
|
||||
italic,
|
||||
)
|
||||
(self.owned_field(&MathField::Char(c), style, &source)?, italic)
|
||||
};
|
||||
if n.sub.is_some() && !limits {
|
||||
b.width -= italic;
|
||||
|
||||
@@ -101,6 +101,7 @@ impl Converter<'_> {
|
||||
class: NoadClass::Ord,
|
||||
limits: OperatorLimits::Display,
|
||||
accent: None,
|
||||
accent_style: AccentStyle::default(),
|
||||
rule: None,
|
||||
vcenter: false,
|
||||
nucleus: if operation == 11 {
|
||||
|
||||
@@ -126,6 +126,25 @@ impl Converter<'_> {
|
||||
.as_ref()
|
||||
.map(|f| self.owned_field(f, style.sub(), &n.source.sub))
|
||||
.transpose()?;
|
||||
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 (_, placement) = geometry::place_scripts_with_layout(
|
||||
body,
|
||||
upper.as_ref().map(NativeBox::geometry).transpose()?,
|
||||
@@ -136,6 +155,7 @@ impl Converter<'_> {
|
||||
self.params(style.sup())?,
|
||||
style,
|
||||
i64::from(self.state.parameter(Parameter::Scriptspace).width()),
|
||||
script_bounds,
|
||||
);
|
||||
let space = i64::from(self.state.parameter(Parameter::Scriptspace).width());
|
||||
let scripts = match (upper, lower) {
|
||||
|
||||
@@ -72,7 +72,7 @@ pub use job::{IoState, JobParameter, JobTime, OutputNode, OutputOperation, Write
|
||||
pub use marks::{NativeAdjustment, NativeMark, PageMarks, SplitMarks};
|
||||
pub(crate) use math::paint_native_math;
|
||||
pub use math::{
|
||||
Delimiter, FieldSource, FractionSpec, MathChar, MathField, MathNode, MathRulePosition,
|
||||
AccentStyle, Delimiter, FieldSource, FractionSpec, MathChar, MathField, MathNode, MathRulePosition,
|
||||
NativeDisplay, NativeMath, Noad, NoadClass, NoadSource, OperatorLimits,
|
||||
};
|
||||
pub use math_code::MathCode;
|
||||
|
||||
@@ -380,6 +380,7 @@ pub enum Primitive {
|
||||
Udelcodenum,
|
||||
Udelimiter,
|
||||
Uradical,
|
||||
Umathaccent,
|
||||
}
|
||||
|
||||
impl Primitive {
|
||||
@@ -882,6 +883,8 @@ const XETEX_PRIMITIVE_ALIASES: &[(&str, Primitive)] = {
|
||||
("XeTeXdelimiter", Udelimiter),
|
||||
("Uradical", Uradical),
|
||||
("XeTeXradical", Uradical),
|
||||
("Umathaccent", Umathaccent),
|
||||
("XeTeXmathaccent", Umathaccent),
|
||||
]
|
||||
};
|
||||
|
||||
|
||||
Reference in new issue
Block a user