use super::{EqLayout, EqNode, FontGlyphPaint, LayoutBox, LayoutKind};
impl EqLayout {
pub(super) fn original_fraction_delimiters(
&self,
left: &str,
right: &str,
body: &LayoutBox,
fs: f64,
) -> Option<LayoutBox> {
if !self.legacy_math_face || (left, right) != ("(", ")") {
return None;
}
let (height, baseline) = super::legacy_logical_frame(body, fs)?;
if (height - fs * 2.34).abs() > 1e-8 {
return None;
}
let glyph_size = fs * 2.03;
let slot = fs * 0.375;
let pad = fs * 0.115;
let mut inner = body.clone();
inner.x = slot + pad;
inner.y = baseline - inner.baseline;
let glyph_baseline = fs * 1.2 + glyph_size * super::AXIS_HEIGHT;
let glyph = |text: &str, x| LayoutBox {
font_size: glyph_size,
x,
y: 0.0,
width: slot,
height,
baseline: glyph_baseline,
kind: LayoutKind::FontGlyph {
text: text.to_owned(),
font_family: super::super::legacy_glyphs::FONT_FAMILY,
outline: None,
paint: Some(FontGlyphPaint {
scale_x: 0.49,
stroke_width: 0.0,
}),
},
};
let closing_x = inner.x + inner.width + pad;
Some(LayoutBox {
font_size: fs,
x: 0.0,
y: 0.0,
width: closing_x + slot,
height,
baseline,
kind: LayoutKind::Row(vec![glyph(left, 0.0), inner, glyph(right, closing_x)]),
})
}
pub(super) fn original_group_unit(&self) -> Option<f64> {
self.legacy_math_face.then(|| {
let logical_em = (self.font_size * 96.0 / self.dpi / 2.0).round() * 2.0;
logical_em * self.dpi / 96.0 * 2.0 / 9.0
})
}
pub(super) fn original_group_frame(&self, mut body: LayoutBox, empty: bool) -> LayoutBox {
if self.legacy_equation {
return body;
}
let Some(unit) = self.original_group_unit() else {
return body;
};
body.x += unit;
let width = unit + if empty { unit * 0.5 } else { body.width };
LayoutBox {
font_size: body.font_size,
x: 0.0,
y: 0.0,
width,
height: body.height,
baseline: body.baseline,
kind: LayoutKind::Row(vec![body]),
}
}
pub(super) fn original_script_base(&self, mut body: LayoutBox, source: &EqNode) -> LayoutBox {
let Some(unit) = self.original_group_unit() else {
return body;
};
if matches!(source, EqNode::Group(_)) {
body = self.original_group_frame(body, false);
}
body.width += unit * 0.5;
body
}
pub(super) fn original_group_boundary_gap(&self, left: &EqNode, right: &EqNode) -> Option<f64> {
let unit = self.original_group_unit()?;
let opening = |node: &EqNode| matches!(node, EqNode::Symbol(text) if text == "(");
if closing_character(left) && closing_character(right) {
return Some(0.0);
}
if opening(left) {
return Some(unit * if opening(right) { 1.0 } else { 0.5 });
}
super::super::legacy_coordinate_plane::group_boundary_gap(left, right)
.map(|gap| gap * unit)
.or_else(|| {
if starts_round_delimiter(right) {
Some(unit * 0.75)
} else if closing_character(left) {
Some(unit * 0.5)
} else if closing_character(right) {
Some(unit * 0.25)
} else {
None
}
})
}
pub(super) fn original_opening_inset(&self, source: &EqNode) -> f64 {
if starts_round_delimiter(source) {
self.original_group_unit().map_or(0.0, |unit| unit * 0.5)
} else {
0.0
}
}
}
fn starts_round_delimiter(node: &EqNode) -> bool {
match node {
EqNode::Symbol(text) => text == "(",
EqNode::Paren { left, .. } => left == "(",
EqNode::FontStyle { body, .. } | EqNode::Color { body, .. } => starts_round_delimiter(body),
EqNode::Subscript { base, .. }
| EqNode::Superscript { base, .. }
| EqNode::SubSup { base, .. } => starts_round_delimiter(base),
_ => false,
}
}
fn closing_character(node: &EqNode) -> bool {
match node {
EqNode::Symbol(text) => text == ")",
EqNode::FontStyle { body, .. } | EqNode::Color { body, .. } => closing_character(body),
_ => false,
}
}