Files
texrra-engine/crates/texrra-tex-core/src/execution/math_fraction.rs
T

147 lines
5.6 KiB
Rust

//! TeX make_fraction: clean/rebox actual fields, then retain the vlist and delimiters.
use super::{math::*, *};
use crate::{math_engine_tex::MathStyle, provenance::PaintSource};
impl Converter<'_> {
pub(super) fn fraction(
&self,
numerator: &[MathNode],
denominator: &[MathNode],
spec: &FractionSpec,
style: MathStyle,
span: Span,
source: &PaintSource,
) -> Result<NativeBox, String> {
let params = self.params(style)?;
let thickness = spec.thickness.map_or(params.rule_thickness, i64::from);
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)?;
// clean_box restores the math style and leaves the last fetched font.
// XeTeX takes the two fraction clearances from that font.
let current = self.state.fonts.font(self.current_math_font.get())?;
let constants = current
.opentype()
.map(|font| crate::opentype::MathConstants::read(font))
.transpose()?
.flatten();
if upper.width < lower.width {
upper = self.rebox_math(upper, lower.width)?;
} else {
lower = self.rebox_math(lower, upper.width)?;
}
let mut up = if style.display() {
params.num1
} else if thickness != 0 {
params.num2
} else {
params.num3
};
let mut down = if style.display() {
params.denom1
} else {
params.denom2
};
let half = (thickness + 1).div_euclid(2);
if thickness == 0 {
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 {
up += delta;
down += delta;
}
} else {
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 {
let clearance = if style.display() {
3 * thickness
} else {
thickness
};
(clearance, 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;
let depth = lower.depth + down;
let kern = |n: i64| -> Result<NativeVerticalNode, String> {
Ok(NativeVerticalNode::ImplicitKern(
i32::try_from(n).map_err(|_| "native fraction kern overflow")?,
))
};
let mut nodes = Vec::new();
let upper_bottom = up - upper.depth;
let lower_top = lower.height - down;
nodes.push(NativeVerticalNode::Box(Box::new(upper)));
if thickness == 0 {
nodes.push(kern(upper_bottom - lower_top)?);
} else {
nodes.push(kern(upper_bottom - (params.axis_height + half))?);
nodes.push(NativeVerticalNode::Rule(NativeRule {
span,
origin: source.tokens.first().copied(),
derivation: Some(crate::provenance::Derivation::Fraction),
width: None,
height: Some(
i32::try_from(thickness).map_err(|_| "native fraction thickness overflow")?,
),
depth: Some(0),
}));
nodes.push(kern((params.axis_height - half) - lower_top)?);
}
nodes.push(NativeVerticalNode::Box(Box::new(lower)));
// make_fraction declares these dimensions directly; odd thickness can
// place the denominator one sp above its declared baseline/depth.
let body = NativeBox {
kind: BoxKind::Vertical,
display: false,
width,
height,
depth,
shift: 0,
content: BoxContent::Vertical(nodes),
diagnostic: Vec::new(),
setting: GlueSetting::default(),
badness: 0,
diagnostic_eligible: false,
excess: 0,
span,
};
let target = if style.display() {
params.delim1
} else {
params.delim2
};
let left = self.delimiter(spec.left, style, target, &spec.left_source)?;
let right = self.delimiter(spec.right, style, target, &spec.right_source)?;
self.measure(&[
TextNode::Box(left.horizontal_item()),
TextNode::Box(body.horizontal_item()),
TextNode::Box(right.horizontal_item()),
])
}
}