use super::{is_integral_symbol, EqLayout, EqNode, LayoutBox, LayoutKind};
const SUPERSCRIPT_SHIFT_UP: f64 = 0.363;
const SUPERSCRIPT_BOTTOM_MIN: f64 = 0.108;
fn ends_with_uppercase_letter(node: &EqNode) -> bool {
match node.ungrouped() {
EqNode::Text(text) => text
.chars()
.last()
.is_some_and(|ch| ch.is_ascii_uppercase()),
EqNode::FontStyle { body, .. } => ends_with_uppercase_letter(body),
EqNode::Row(items) => items.last().is_some_and(ends_with_uppercase_letter),
_ => false,
}
}
impl EqLayout {
pub(super) fn layout_script_base(&self, base: &EqNode, fs: f64) -> LayoutBox {
let mut value = if self.hancom_math_face && matches!(base.ungrouped(), EqNode::Empty) {
self.layout_row(&[], fs)
} else {
self.layout_node(base, fs)
};
if self.legacy_math_face && !self.implicit_legacy_font {
return self.original_script_base(value, base);
}
value.width += self.script_base_operator_gap(base, fs);
value
}
fn script_uppercase_gap(&self, base: &EqNode, fs: f64) -> f64 {
if self.hancom_math_face && !self.legacy_math_face && ends_with_uppercase_letter(base) {
fs * 0.15
} else {
0.0
}
}
pub(super) fn script_superscript_shift(
&self,
base: &LayoutBox,
sup: &LayoutBox,
fs: f64,
) -> f64 {
self.hancom_math_face
.then(|| super::super::logical_font_frame::superscript_shift(base, sup, fs, self.dpi))
.flatten()
.unwrap_or_else(|| {
(fs * SUPERSCRIPT_SHIFT_UP)
.max(sup.height - sup.baseline + fs * SUPERSCRIPT_BOTTOM_MIN)
})
}
pub(super) fn script_subscript_geometry(
&self,
base: &LayoutBox,
sub: &LayoutBox,
fs: f64,
) -> (f64, f64) {
if self.implicit_legacy_font {
let shift = if super::implicit_legacy::has_cjk(base) {
0.27
} else {
0.25
};
let y = base.baseline + fs * shift - sub.baseline;
return (y, base.height.max(y + sub.height));
}
if self.legacy_math_face
&& super::super::legacy_glyphs::is_line(base, fs)
&& super::super::legacy_glyphs::is_line(sub, fs * 0.64)
{
let y = base.baseline + fs * 0.28 - sub.baseline;
return (y, base.height.max(y + sub.height));
}
if self.hancom_eqn
&& super::super::logical_font_frame::font_line(base, fs)
&& super::super::logical_font_frame::font_line(sub, sub.font_size)
{
let y = base.baseline + fs * 0.25 - sub.baseline;
return (y, base.height.max(y + sub.height));
}
let source = (self.hancom_math_face || self.hancom_eqn)
.then(|| super::super::logical_font_frame::subscript_geometry(base, sub, fs, self.dpi))
.flatten();
let y = source.map_or(base.baseline * 0.4, |(y, _)| y);
let height = source
.map_or(base.height, |(_, height)| height)
.max(y + sub.height);
(y, height)
}
pub(super) fn layout_superscript(&self, base: &EqNode, sup: &EqNode, fs: f64) -> LayoutBox {
if matches!(base.ungrouped(), EqNode::MathSymbol(symbol) if is_integral_symbol(symbol)) {
return self
.integral_placement(base, None, Some(sup), fs)
.into_script(fs);
}
let mut base_box = self.layout_script_base(base, fs);
let mut sup_box = self.layout_node(sup, self.script_font_size(fs));
let letter_gap = self.script_uppercase_gap(base, fs);
let shift = self.script_superscript_shift(&base_box, &sup_box, fs);
let baseline = base_box.baseline.max(sup_box.baseline + shift);
base_box.x = 0.0;
base_box.y = baseline - base_box.baseline;
sup_box.x = base_box.width + letter_gap;
sup_box.y = baseline - shift - sup_box.baseline;
LayoutBox {
font_size: fs,
x: 0.0,
y: 0.0,
width: sup_box.x + sup_box.width,
height: (base_box.y + base_box.height).max(sup_box.y + sup_box.height),
baseline,
kind: LayoutKind::Superscript {
base: Box::new(base_box),
sup: Box::new(sup_box),
},
}
}
pub(super) fn layout_subscript(&self, base: &EqNode, sub: &EqNode, fs: f64) -> LayoutBox {
if matches!(base.ungrouped(), EqNode::MathSymbol(symbol) if is_integral_symbol(symbol)) {
return self
.integral_placement(base, Some(sub), None, fs)
.into_script(fs);
}
let mut b = self.layout_script_base(base, fs);
let s = self.layout_node(sub, self.script_font_size(fs));
if self.implicit_legacy_font {
b.width += fs
* if super::implicit_legacy::has_cjk(&b) {
1.0 / 3.0
} else {
0.25
};
}
let (sub_shift, total_h) = self.script_subscript_geometry(&b, &s, fs);
let mut base_box = b;
base_box.x = 0.0;
base_box.y = 0.0;
let mut sub_box = s;
sub_box.x = base_box.width;
sub_box.y = sub_shift;
let total_w = base_box.width
+ sub_box.width
+ if self.implicit_legacy_font {
fs * 0.1
} else {
0.0
};
LayoutBox {
font_size: fs,
x: 0.0,
y: 0.0,
width: total_w,
height: total_h,
baseline: base_box.baseline,
kind: LayoutKind::Subscript {
base: Box::new(base_box),
sub: Box::new(sub_box),
},
}
}
pub(super) fn layout_subsup(
&self,
base: &EqNode,
sub: &EqNode,
sup: &EqNode,
fs: f64,
) -> LayoutBox {
if matches!(base.ungrouped(), EqNode::MathSymbol(symbol) if is_integral_symbol(symbol)) {
return self
.integral_placement(base, Some(sub), Some(sup), fs)
.into_script(fs);
}
let b = self.layout_script_base(base, fs);
let sb = self.layout_node(sub, self.script_font_size(fs));
let sp = self.layout_node(sup, self.script_font_size(fs));
let shift = self.script_superscript_shift(&b, &sp, fs);
let sup_y = b.baseline - shift - sp.baseline;
let letter_gap = self.script_uppercase_gap(base, fs);
let (sub_y, sub_height) = self.script_subscript_geometry(&b, &sb, fs);
let base_y = (-sup_y).max(-sub_y).max(0.0);
let mut base_box = b;
base_box.x = 0.0;
base_box.y = base_y;
let mut sup_box = sp;
sup_box.x = base_box.width + letter_gap;
sup_box.y = base_y + sup_y;
let mut sub_box = sb;
sub_box.x = base_box.width;
sub_box.y = base_y + sub_y;
let total_h = (base_y + sub_height)
.max(base_box.y + base_box.height)
.max(sup_box.y + sup_box.height)
.max(sub_box.y + sub_box.height);
let total_w = (sup_box.x + sup_box.width).max(sub_box.x + sub_box.width);
LayoutBox {
font_size: fs,
x: 0.0,
y: 0.0,
width: total_w,
height: total_h,
baseline: base_box.y + base_box.baseline,
kind: LayoutKind::SubSup {
base: Box::new(base_box),
sub: Box::new(sub_box),
sup: Box::new(sup_box),
},
}
}
}