use super::layout::{LayoutBox, LayoutKind};
use super::logical_font_frame::{contains_prime, font_line};
const FRAMES: [[u32; 2]; 15] = [
[600, 1465],
[700, 1666],
[800, 2016],
[900, 2217],
[1000, 2417],
[1100, 2768],
[1200, 2968],
[1300, 3169],
[1400, 3519],
[1500, 3720],
[1600, 3920],
[1800, 4434],
[2000, 4985],
[2200, 5386],
[2400, 5937],
];
fn contains_cjk(value: &LayoutBox) -> bool {
match &value.kind {
LayoutKind::Text(text)
| LayoutKind::Function(text)
| LayoutKind::FontGlyph { text, .. } => super::text_has_cjk(text),
LayoutKind::Row(items) => items.iter().any(contains_cjk),
LayoutKind::Color { body, .. }
| LayoutKind::FontStyle { body, .. }
| LayoutKind::Paren { body, .. } => contains_cjk(body),
_ => false,
}
}
pub(super) fn ordinary_line(value: &LayoutBox, fs: f64) -> bool {
font_line(value, fs) && !contains_prime(value) && !contains_cjk(value)
}
pub(super) fn line_extension(value: &LayoutBox, fs: f64, dpi: f64) -> Option<f64> {
if ordinary_line(value, fs) {
return Some(0.0);
}
match &value.kind {
LayoutKind::Sqrt { index, body }
if index
.as_deref()
.is_none_or(|index| index.y >= 0.0 && index.y + index.height <= value.height)
&& (value.font_size - fs).abs() <= f64::EPSILON * fs * 4.0 =>
{
line_extension(body, fs, dpi)?;
let (line_height, _) = super::font_frame_metrics::line_at_dpi(fs, dpi)?;
let extension = value.height - line_height;
(extension.is_finite() && extension >= 0.0).then_some(extension)
}
LayoutKind::Color { body, .. } | LayoutKind::FontStyle { body, .. } => {
line_extension(body, fs, dpi)
}
LayoutKind::Row(items) if items.len() == 1 => {
let child = &items[0];
let extension = line_extension(child, fs, dpi)?;
if extension == 0.0 {
let (height, baseline) = super::font_frame_metrics::line_at_dpi(fs, dpi)?;
let tolerance = f64::EPSILON * fs * 16.0;
if (value.height - height).abs() > tolerance
|| (value.baseline - baseline).abs() > tolerance
|| (child.y + child.baseline - baseline).abs() > tolerance
{
return None;
}
} else {
let tolerance = f64::EPSILON * value.height.abs().max(fs) * 16.0;
if (value.height - child.height).abs() > tolerance
|| (value.baseline - child.baseline).abs() > tolerance
|| child.y.abs() > tolerance
{
return None;
}
}
Some(extension)
}
_ => None,
}
}
pub(super) fn source_fraction_frame(
numer: &LayoutBox,
denom: &LayoutBox,
fs: f64,
dpi: f64,
) -> Option<(f64, f64)> {
let above = line_extension(numer, fs, dpi)?;
let below = line_extension(denom, fs, dpi)?;
let (height, baseline) = super::font_frame_metrics::fraction_at_dpi(fs, dpi)?;
Some((height + above + below, baseline + above))
}
fn fraction_extensions(value: &LayoutBox, fs: f64, dpi: f64) -> Option<(f64, f64)> {
match &value.kind {
LayoutKind::Fraction { numer, denom, .. } => Some((
line_extension(numer, fs, dpi)?,
line_extension(denom, fs, dpi)?,
)),
LayoutKind::Color { body, .. }
| LayoutKind::FontStyle { body, .. }
| LayoutKind::Paren { body, .. } => fraction_extensions(body, fs, dpi),
LayoutKind::Row(items) if items.len() == 1 => fraction_extensions(&items[0], fs, dpi),
_ => None,
}
}
pub(super) fn frame(base: &LayoutBox, sup: &LayoutBox, fs: f64, dpi: f64) -> Option<(f64, f64)> {
let (above, below) = fraction_extensions(base, fs, dpi)?;
if !ordinary_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 = FRAMES.iter().find(|row| row[0] == unit)?;
let height = crate::renderer::hwpunit_to_px(row[1] as i32, dpi);
Some((height + above + below, height * 0.68 + above))
}
pub(super) fn shift(base: &LayoutBox, sup: &LayoutBox, fs: f64, dpi: f64) -> Option<f64> {
let (_, fraction_baseline) = frame(base, sup, fs, dpi)?;
let (sup_height, sup_baseline) = super::font_frame_metrics::superscript_at_dpi(fs, dpi)?;
let (line_height, line_baseline) = super::font_frame_metrics::line_at_dpi(fs, dpi)?;
let plain_shift = sup_height - line_height + line_baseline * (1.0 - sup.font_size / fs);
Some(fraction_baseline - sup_baseline + plain_shift)
}
pub(super) fn subscript_frame(
base: &LayoutBox,
sub: &LayoutBox,
fs: f64,
dpi: f64,
) -> Option<(f64, f64)> {
if !ordinary_line(sub, sub.font_size)
|| (sub.font_size - fs * 0.68).abs() > f64::EPSILON * fs * 4.0
{
return None;
}
let (above, below) = fraction_extensions(base, fs, dpi)?;
let (fraction_height, fraction_baseline) = super::font_frame_metrics::fraction_at_dpi(fs, dpi)?;
let (sub_height, _) = super::font_frame_metrics::subscript_at_dpi(fs, dpi)?;
let (line_height, _) = super::font_frame_metrics::line_at_dpi(fs, dpi)?;
Some((
fraction_height + above + below + sub_height - line_height,
fraction_baseline + above,
))
}
pub(super) fn subscript_geometry(
base: &LayoutBox,
sub: &LayoutBox,
fs: f64,
dpi: f64,
) -> Option<(f64, f64)> {
let (height, _) = subscript_frame(base, sub, fs, dpi)?;
let (line_height, line_baseline) = super::font_frame_metrics::line_at_dpi(fs, dpi)?;
let sub_depth = (line_height - line_baseline) * sub.font_size / fs;
Some((height - sub_depth - sub.baseline, height))
}
pub(super) fn subsup_frame(
base: &LayoutBox,
sub: &LayoutBox,
sup: &LayoutBox,
fs: f64,
dpi: f64,
) -> Option<(f64, f64)> {
subscript_frame(base, sub, fs, dpi)?;
let (height, baseline) = frame(base, sup, fs, dpi)?;
let (sub_height, _) = super::font_frame_metrics::subscript_at_dpi(fs, dpi)?;
let (line_height, _) = super::font_frame_metrics::line_at_dpi(fs, dpi)?;
Some((height + sub_height - line_height, baseline))
}