226 lines
9.5 KiB
Rust
226 lines
9.5 KiB
Rust
//! Owned make_op / clean_box / rebox lists, including displaced limits.
|
|
use super::{math::*, *};
|
|
use crate::math_engine_tex::{MathGeometry, MathStyle};
|
|
impl Converter<'_> {
|
|
pub(super) fn rebox_math(&self, mut b: NativeBox, width: i64) -> Result<NativeBox, String> {
|
|
let empty = match &b.content {
|
|
BoxContent::Horizontal(v) => v.is_empty(),
|
|
BoxContent::Vertical(v) => v.is_empty(),
|
|
};
|
|
if b.width == width || empty {
|
|
b.width = width;
|
|
return Ok(b);
|
|
}
|
|
if b.kind != BoxKind::Horizontal {
|
|
b = self.pack_field(vec![TextNode::Box(b.horizontal_item())])?;
|
|
// rebox wraps a vbox in a new hbox before removing that wrapper.
|
|
if b.kind != BoxKind::Horizontal {
|
|
b = super::boxes::hpack(
|
|
vec![TextNode::Box(b.horizontal_item())],
|
|
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));
|
|
}
|
|
}
|
|
let BoxContent::Horizontal(mut nodes) = b.content else {
|
|
unreachable!()
|
|
};
|
|
if let [TextNode::MathChar(c)] = nodes.as_slice() {
|
|
let difference = b.width - c.width;
|
|
if difference != 0 {
|
|
nodes.push(TextNode::Kern {
|
|
width: i32::try_from(difference)
|
|
.map_err(|_| "native operator italic overflow")?,
|
|
explicit: false,
|
|
});
|
|
}
|
|
}
|
|
let ss = TextNode::Glue(Glue {
|
|
canonical_zero: false,
|
|
width: 0,
|
|
stretch: Flex {
|
|
amount: 65536,
|
|
order: 1,
|
|
},
|
|
shrink: Flex {
|
|
amount: 65536,
|
|
order: 1,
|
|
},
|
|
});
|
|
nodes.insert(0, ss.clone());
|
|
nodes.push(ss);
|
|
let b = super::boxes::hpack(
|
|
nodes,
|
|
super::boxes::PackSpec::To(
|
|
i32::try_from(width).map_err(|_| "native operator width overflow")?,
|
|
),
|
|
Span::default(),
|
|
self.state,
|
|
)?;
|
|
self.horizontal_packs.set(self.horizontal_packs.get() + 1);
|
|
self.last_pack_badness.set(Some(b.badness));
|
|
Ok(b)
|
|
}
|
|
pub(super) fn operator_nodes(
|
|
&self,
|
|
n: &Noad,
|
|
style: MathStyle,
|
|
) -> Result<Vec<TextNode>, String> {
|
|
let params = self.params(style)?;
|
|
let start_font = self.current_math_font.get();
|
|
let limits = n.limits == OperatorLimits::AboveBelow
|
|
|| (n.limits == OperatorLimits::Display && style.display());
|
|
// TeX cleans the upper limit first for a sub-mlist nucleus, but fetches
|
|
// a character nucleus before cleaning its limits.
|
|
let early_upper = if limits && !matches!(n.nucleus, MathField::Char(_)) {
|
|
Some(
|
|
n.sup
|
|
.as_ref()
|
|
.map(|f| self.owned_field(f, style.sup(), &n.source.sup))
|
|
.transpose()?,
|
|
)
|
|
} else {
|
|
None
|
|
};
|
|
let (mut body, italic) = if let MathField::Char(mut c) = n.nucleus {
|
|
let id = self.family_font(c.family, style)?;
|
|
let mut source = n.source.nucleus.clone();
|
|
let font = self.state.fonts.font(id)?;
|
|
self.current_math_font.set(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 {
|
|
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)
|
|
};
|
|
if n.sub.is_some() && !limits {
|
|
b.width -= italic;
|
|
}
|
|
b.shift = (b.height - b.depth + 1).div_euclid(2) - params.axis_height;
|
|
(b, italic)
|
|
} else {
|
|
(self.owned_field(&n.nucleus, style, &n.source.nucleus)?, 0)
|
|
};
|
|
if !limits {
|
|
if n.sup.is_none() && n.sub.is_none() {
|
|
return Ok(vec![TextNode::Box(body.horizontal_item())]);
|
|
}
|
|
let mut side = n.clone();
|
|
side.class = NoadClass::Ord;
|
|
side.vcenter = false;
|
|
side.nucleus = MathField::Box(Box::new(body));
|
|
return self.script_noad(&side, style, false, italic);
|
|
}
|
|
let limits_font = if matches!(n.nucleus, MathField::Char(_)) {
|
|
self.current_math_font.get()
|
|
} else {
|
|
start_font
|
|
};
|
|
let mut upper = if let Some(upper) = early_upper {
|
|
upper
|
|
} else {
|
|
n.sup
|
|
.as_ref()
|
|
.map(|f| self.owned_field(f, style.sup(), &n.source.sup))
|
|
.transpose()?
|
|
};
|
|
// clean_box wraps a shifted character operator, keeping its axis shift
|
|
// in the nested owned hbox rather than baking it into glyph positions.
|
|
if body.shift != 0 {
|
|
body = self.pack_field(vec![TextNode::Box(body.horizontal_item())])?;
|
|
}
|
|
let mut lower = n
|
|
.sub
|
|
.as_ref()
|
|
.map(|f| self.owned_field(f, style.sub(), &n.source.sub))
|
|
.transpose()?;
|
|
self.current_math_font.set(limits_font);
|
|
let width = body
|
|
.width
|
|
.max(upper.as_ref().map_or(0, |b| b.width))
|
|
.max(lower.as_ref().map_or(0, |b| b.width));
|
|
let mut height = body.height;
|
|
let mut depth = body.depth;
|
|
body = self.rebox_math(body, width)?;
|
|
upper = upper.map(|b| self.rebox_math(b, width)).transpose()?;
|
|
lower = lower.map(|b| self.rebox_math(b, width)).transpose()?;
|
|
let [space1, space2, space3, space4, space5] = params.big_op_spacing;
|
|
let kern = |n: i64| -> Result<NativeVerticalNode, String> {
|
|
Ok(NativeVerticalNode::ImplicitKern(
|
|
i32::try_from(n).map_err(|_| "native operator kern overflow")?,
|
|
))
|
|
};
|
|
let mut nodes = Vec::new();
|
|
if let Some(mut upper) = upper {
|
|
let gap = space1.max(space3 - upper.depth);
|
|
height += space5 + upper.height + upper.depth + gap;
|
|
upper.shift = (italic + 1).div_euclid(2);
|
|
nodes.push(kern(space5)?);
|
|
nodes.push(NativeVerticalNode::Box(Box::new(upper)));
|
|
nodes.push(kern(gap)?);
|
|
}
|
|
nodes.push(NativeVerticalNode::Box(Box::new(body)));
|
|
if let Some(mut lower) = lower {
|
|
let gap = space2.max(space4 - lower.height);
|
|
depth += space5 + lower.height + lower.depth + gap;
|
|
lower.shift = -(italic + 1).div_euclid(2);
|
|
nodes.push(kern(gap)?);
|
|
nodes.push(NativeVerticalNode::Box(Box::new(lower)));
|
|
nodes.push(kern(space5)?);
|
|
}
|
|
let mut b = super::boxes::vpack(
|
|
nodes,
|
|
super::boxes::PackSpec::Natural,
|
|
1_073_741_823,
|
|
BoxKind::Vertical,
|
|
n.span,
|
|
)?;
|
|
// make_op fixes these declared dimensions, including negative live
|
|
// spacing; the union of painted glyph bounds is not the vlist size.
|
|
b.width = width;
|
|
b.height = height;
|
|
b.depth = depth;
|
|
Ok(vec![TextNode::Box(b.horizontal_item())])
|
|
}
|
|
pub(super) fn operator(&self, n: &Noad, style: MathStyle) -> Result<MathGeometry, String> {
|
|
self.clean(&self.operator_nodes(n, style)?)
|
|
}
|
|
}
|