use super::{LayoutBox, FRAC_LINE_THICK};
fn hancom_position(
numer: &mut LayoutBox,
denom: &mut LayoutBox,
fs: f64,
dpi: f64,
) -> Option<(f64, f64)> {
let frame = super::super::hancom_frame::fraction(numer, denom, fs, dpi)?;
let (line_height, line_baseline) = super::super::font_frame_metrics::line_at_dpi(fs, dpi)?;
let (_, numer_baseline) = super::super::hancom_frame::metrics(numer, fs, dpi)?;
let (denom_height, denom_baseline) = super::super::hancom_frame::metrics(denom, fs, dpi)?;
let above = (numer_baseline - line_baseline).max(0.0);
let below = (denom_height - denom_baseline - (line_height - line_baseline)).max(0.0);
numer.y = line_baseline + above - numer.baseline;
denom.y = frame.0 - line_height + line_baseline - below - denom.baseline;
Some(frame)
}
fn source_position(
numer: &mut LayoutBox,
denom: &mut LayoutBox,
fs: f64,
dpi: f64,
) -> Option<(f64, f64)> {
let (height, baseline) =
super::super::fraction_sup_frame::source_fraction_frame(numer, denom, fs, dpi)?;
let above = super::super::fraction_sup_frame::line_extension(numer, fs, dpi)?;
let (line_height, line_baseline) = super::super::font_frame_metrics::line_at_dpi(fs, dpi)?;
numer.y = line_baseline + above - numer.baseline;
denom.y = height - line_height + line_baseline - denom.baseline;
Some((height, baseline))
}
pub(super) fn position(
numer: &mut LayoutBox,
denom: &mut LayoutBox,
fs: f64,
axis: f64,
) -> (f64, f64) {
let half_rule = fs * super::FRAC_LINE_THICK * 0.5;
let gap = fs * 120.0 / 1000.0;
let numer_shift =
(fs * 677.0 / 1000.0).max(axis + half_rule + gap + numer.height - numer.baseline);
let denom_shift = (fs * 686.0 / 1000.0).max(half_rule + gap + denom.baseline - axis);
let ascent = (numer_shift + numer.baseline)
.max(denom.baseline - denom_shift)
.max(axis + half_rule);
let descent = (numer.height - numer.baseline - numer_shift)
.max(denom_shift + denom.height - denom.baseline)
.max(half_rule - axis)
.max(0.0);
numer.y = ascent - numer_shift - numer.baseline;
denom.y = ascent + denom_shift - denom.baseline;
(ascent + descent, ascent)
}
use super::{
EqLayout, EqNode, LayoutKind, AXIS_HEIGHT, FRAC_LINE_PAD, RADICAL_DEGREE_SCALE, SQRT_PAD,
};
pub(super) fn legacy_operand_frame(value: &LayoutBox, fs: f64) -> Option<(f64, f64)> {
match &value.kind {
LayoutKind::Text(_)
| LayoutKind::Number(_)
| LayoutKind::Symbol(_)
| LayoutKind::MathSymbol(_)
| LayoutKind::Function(_)
| LayoutKind::FontGlyph { .. }
| LayoutKind::HancomGlyphs(_) => Some((fs * 1.2, fs * 1.2 * 0.84)),
LayoutKind::Fraction { .. } => Some((value.height, value.baseline)),
LayoutKind::Color { body, .. }
| LayoutKind::FontStyle { body, .. }
| LayoutKind::Paren { body, .. } => legacy_operand_frame(body, fs),
LayoutKind::Row(items) if !items.is_empty() => {
let mut ascent = 0.0_f64;
let mut descent = 0.0_f64;
for item in items {
let (height, baseline) = legacy_operand_frame(item, fs)?;
ascent = ascent.max(baseline);
descent = descent.max(height - baseline);
}
Some((ascent + descent, ascent))
}
_ => None,
}
}
impl EqLayout {
pub(super) fn layout_fraction(&self, numer: &EqNode, denom: &EqNode, fs: f64) -> LayoutBox {
if self.legacy_hft_metrics {
return self.layout_legacy_fraction(numer, denom, fs);
}
let n = self.layout_node(numer, fs);
let d = self.layout_node(denom, fs);
let pad = fs
* if self.hancom_math_face || self.hancom_eqn {
0.25
} else {
FRAC_LINE_PAD
};
let w = n.width.max(d.width) + pad * 2.0;
let mut n_box = n;
n_box.x = (w - n_box.width) / 2.0;
let mut d_box = d;
d_box.x = (w - d_box.width) / 2.0;
let axis_height = self.math_axis_height(fs);
let source = if self.hancom_eqn {
hancom_position(&mut n_box, &mut d_box, fs, self.dpi)
} else {
self.hancom_math_face
.then(|| source_position(&mut n_box, &mut d_box, fs, self.dpi))
.flatten()
};
let (total_h, baseline) =
source.unwrap_or_else(|| position(&mut n_box, &mut d_box, fs, axis_height));
LayoutBox {
font_size: fs,
x: 0.0,
y: 0.0,
width: w,
height: total_h,
baseline,
kind: LayoutKind::Fraction {
numer: Box::new(n_box),
denom: Box::new(d_box),
axis_height,
rule_start: fs * 0.05,
rule_end: w - fs * 0.05,
rule_thickness: fs
* if self.hancom_eqn {
32.0 / 1024.0
} else {
super::FRAC_LINE_THICK
},
},
}
}
pub(super) fn layout_atop(&self, top: &EqNode, bottom: &EqNode, fs: f64) -> LayoutBox {
if self.hancom_math_face {
return self.layout_source_atop(top, bottom, fs);
}
let t = self.layout_node(top, fs);
let b = self.layout_node(bottom, fs);
let pad = fs * FRAC_LINE_PAD;
let axis = fs * AXIS_HEIGHT;
let w = t.width.max(b.width) + pad * 2.0;
let top_h = t.height + pad;
let bottom_h = b.height + pad;
let baseline = top_h + axis;
let total_h = top_h + bottom_h;
let mut top_box = t;
top_box.x = (w - top_box.width) / 2.0;
top_box.y = pad;
let mut bottom_box = b;
bottom_box.x = (w - bottom_box.width) / 2.0;
bottom_box.y = top_h;
LayoutBox {
font_size: fs,
x: 0.0,
y: 0.0,
width: w,
height: total_h,
baseline,
kind: LayoutKind::Atop {
top: Box::new(top_box),
bottom: Box::new(bottom_box),
},
}
}
pub(super) fn layout_sqrt(
&self,
index: &Option<Box<EqNode>>,
body: &EqNode,
fs: f64,
) -> LayoutBox {
let raw_body = self.layout_node(body, fs);
let b = if self.hancom_math_face {
self.matrix_row_box(raw_body, fs)
} else {
raw_body
};
let pad = fs * SQRT_PAD;
let sign_w = if self.hancom_math_face && !self.legacy_math_face {
super::source_roman_advance::em_advance(fs, self.dpi).unwrap_or(fs)
} else {
fs * 0.833
};
let right_pad = if self.hancom_math_face {
fs * (0.17 + if ends_round_delimiter(&b) { 0.07 } else { 0.0 })
} else {
pad
};
let body_w = b.width + right_pad;
let top_band = if self.hancom_math_face {
super::super::radical_line_frame::top_band(&b, fs, self.dpi).unwrap_or_else(|| {
super::super::font_frame_metrics::line_at_dpi(fs, self.dpi)
.map_or(fs, |(height, _)| height)
* 0.15
})
} else {
pad
};
let body_h = b.height
+ if self.hancom_math_face {
top_band
} else {
pad * 2.0
};
let idx = index.as_ref().map(|i| {
let mut ib = self.layout_node(i, fs * RADICAL_DEGREE_SCALE);
ib.x = if self.hancom_math_face {
(sign_w / 2.0 - ib.width).max(0.0)
} else {
fs * 0.278
};
ib.y = body_h * 0.4
- if self.hancom_math_face {
ib.baseline
} else {
ib.height
};
ib
});
let degree_extension = idx
.as_ref()
.map(|i| {
let clearance = if self.hancom_math_face && ends_round_delimiter(i) {
i.font_size * 0.07
} else {
0.0
};
let overlap = if self.hancom_math_face {
sign_w / 2.0
} else {
fs * 0.556
};
(i.x + i.width + clearance - overlap).max(0.0)
})
.unwrap_or(0.0);
let total_w = degree_extension + sign_w + body_w;
let mut body_box = b;
body_box.x = total_w - body_w;
body_box.y = top_band;
LayoutBox {
font_size: fs,
x: 0.0,
y: 0.0,
width: total_w,
height: body_h,
baseline: body_box.y + body_box.baseline,
kind: LayoutKind::Sqrt {
index: idx.map(Box::new),
body: Box::new(body_box),
},
}
}
}
fn ends_round_delimiter(value: &LayoutBox) -> bool {
match &value.kind {
LayoutKind::Paren { right, .. } => right == ")",
LayoutKind::Color { body, .. } | LayoutKind::FontStyle { body, .. } => {
ends_round_delimiter(body)
}
LayoutKind::Row(items) => items.last().is_some_and(ends_round_delimiter),
_ => false,
}
}
impl EqLayout {
fn layout_legacy_fraction(&self, numer: &EqNode, denom: &EqNode, fs: f64) -> LayoutBox {
let (n, n_registration) = self.layout_fraction_operand(numer, fs);
let (d, d_registration) = self.layout_fraction_operand(denom, fs);
let pad = fs * FRAC_LINE_PAD;
let side_pad = if self.legacy_hft_metrics {
fs * 0.24
} else {
pad
};
let line_thick = fs * FRAC_LINE_THICK;
let mut axis = fs * AXIS_HEIGHT;
let w = n.width.max(d.width) + side_pad * 2.0;
let numer_h = n.height + pad;
let denom_h = d.height + pad;
let frac_line_from_top = numer_h + line_thick / 2.0;
let mut baseline = frac_line_from_top + axis;
let mut total_h = numer_h + line_thick + denom_h;
let mut n_box = n;
n_box.x = (w - n_box.width) / 2.0;
n_box.y = pad;
let mut d_box = d;
d_box.x = (w - d_box.width) / 2.0;
d_box.y = numer_h + line_thick;
if let (Some((nh, nb)), Some((dh, db))) = (
legacy_operand_frame(&n_box, fs),
legacy_operand_frame(&d_box, fs),
) {
let line_height = fs * 1.2;
let above = (nh - line_height).max(0.0);
let below = (dh - line_height).max(0.0);
total_h = fs * 2.34 + above + below;
let percent = ((fs * 2.34 * 0.67 + above) / total_h * 100.0).round();
baseline = total_h * percent / 100.0;
n_box.y = nb - n_box.baseline;
d_box.y = total_h - dh + db - d_box.baseline;
if self.legacy_math_face {
if super::super::legacy_glyphs::is_line(&n_box, fs) {
n_box.y -= fs * 1.2 * (0.84 - 0.8);
}
if super::super::legacy_glyphs::is_line(&d_box, fs) {
d_box.y -= fs * 1.2 * (0.84 - 0.8);
}
}
axis = baseline - line_height - above;
}
let rule_start = (n_box.x + n_registration).min(d_box.x + d_registration) - fs * 0.18;
let rule_end = (n_box.x + n_box.width).max(d_box.x + d_box.width) + fs * 0.18;
LayoutBox {
font_size: fs,
x: 0.0,
y: 0.0,
width: w,
height: total_h,
baseline,
kind: LayoutKind::Fraction {
axis_height: axis,
rule_thickness: line_thick,
rule_start,
rule_end,
numer: Box::new(n_box),
denom: Box::new(d_box),
},
}
}
}