use super::layout::{LayoutBox, LayoutKind};
const PARENT: [[u32; 3]; 7] = [
[600, 1172, 93],
[612, 1325, 94],
[900, 1834, 93],
[1020, 2083, 92],
[1496, 3155, 93],
[1500, 3156, 93],
[2200, 4537, 93],
];
const REDUCED: [[u32; 5]; 7] = [
[408, 375, 88, 872, 67],
[416, 450, 83, 1025, 65],
[612, 600, 86, 1384, 66],
[694, 675, 86, 1558, 66],
[1017, 1050, 84, 2405, 65],
[1020, 1050, 85, 2406, 65],
[1496, 1500, 85, 3449, 66],
];
pub(super) fn reduced(fs: f64, dpi: f64, fraction: bool) -> Option<(f64, f64)> {
let unit = super::font_frame_metrics::base_unit_at_dpi(fs, dpi);
let row = REDUCED.iter().find(|row| row[0] == unit)?;
let index = if fraction { 3 } else { 1 };
let height = crate::renderer::hwpunit_to_px(row[index] as i32, dpi);
Some((height, height * f64::from(row[index + 1]) / 100.0))
}
fn fraction_line(value: &LayoutBox, fs: f64) -> bool {
match &value.kind {
LayoutKind::Fraction { numer, denom, .. } => {
super::fraction_sup_frame::ordinary_line(numer, fs)
&& super::fraction_sup_frame::ordinary_line(denom, fs)
}
LayoutKind::Row(items) => {
let count = items.iter().filter(|v| fraction_line(v, fs)).count();
count == 1
&& items.iter().all(|v| {
fraction_line(v, fs) || super::fraction_sup_frame::ordinary_line(v, fs)
})
}
_ => false,
}
}
fn trailing_space(value: &LayoutBox) -> bool {
match &value.kind {
LayoutKind::Space(_) | LayoutKind::Empty => true,
LayoutKind::Row(items) => items.last().is_some_and(trailing_space),
_ => false,
}
}
pub(super) fn frame(base: &LayoutBox, sup: &LayoutBox, fs: f64, dpi: f64) -> Option<(f64, f64)> {
if !(super::fraction_sup_frame::ordinary_line(base, fs)
|| matches!(base.kind, LayoutKind::Empty))
|| !fraction_line(sup, sup.font_size)
|| (sup.font_size - fs * 0.68).abs() > f64::EPSILON * fs * 4.0
{
return None;
}
let unit = super::font_frame_metrics::base_unit_at_dpi(fs, dpi);
let row = PARENT.iter().find(|row| row[0] == unit)?;
let line_height = super::font_frame_metrics::line_at_dpi(fs, dpi)?.0;
let strut = if trailing_space(base) {
((f64::from(unit) - line_height * crate::renderer::HWPUNIT_PER_INCH / dpi).max(0.0) / 2.0)
.floor() as i32
} else {
0
};
let height = crate::renderer::hwpunit_to_px(row[1] as i32 + strut, dpi);
Some((height, height * f64::from(row[2]) / 100.0))
}
pub(super) fn shift(base: &LayoutBox, sup: &LayoutBox, fs: f64, dpi: f64) -> Option<f64> {
let (_, baseline) = frame(base, sup, fs, dpi)?;
Some(baseline - sup.baseline)
}