use super::{EqLayout, EqNode, FontGlyphPaint, FontStyleKind, LayoutBox, LayoutKind};
use crate::model::control::Equation;
const FAMILY: &str = "RHWP Legacy Equation, HyhwpEQ, serif";
pub(super) fn matches_version(equation: &Equation) -> bool {
if equation.script.trim().is_empty()
|| !equation.version_info.is_empty()
|| equation.attr & 1 != 0
|| equation.font_size == 0
|| !(equation.font_name.eq_ignore_ascii_case("HYhwpEQ")
|| equation.font_name.eq_ignore_ascii_case("HancomEQN"))
{
return false;
}
let tokens = super::super::document_script::tokenize(&equation.script);
!tokens.windows(2).enumerate().any(|(index, pair)| {
index > 0
&& pair[0].ty == super::super::tokenizer::TokenType::Symbol
&& matches!(pair[0].value.as_str(), "+" | "-" | "=" | "<" | ">")
&& !pair[0].space_before
&& pair[1].space_before
})
}
fn glyph_width(character: char) -> Option<f64> {
match character {
'0'..='9' | '.' | '<' | '>' => Some(0.5),
'+' | '-' | '−' | '=' | '%' => Some(1.0),
'×' => Some(0.616),
'÷' => Some(0.623),
_ => None,
}
}
fn cjk(character: char) -> bool {
super::super::text_has_cjk(&character.to_string())
}
fn line_node(node: &EqNode) -> bool {
match node.ungrouped() {
EqNode::Text(text) | EqNode::Number(text) => {
text.chars().all(|ch| cjk(ch) || glyph_width(ch).is_some())
}
EqNode::Symbol(text) | EqNode::MathSymbol(text) => {
text.chars().count() == 1 && text.chars().all(|ch| glyph_width(ch).is_some())
}
EqNode::Row(items) => items.iter().all(line_node),
EqNode::FontStyle {
style: FontStyleKind::Roman,
body,
} => line_node(body),
EqNode::Color { body, .. } => line_node(body),
EqNode::Empty => true,
_ => false,
}
}
pub(super) fn supports(node: &EqNode) -> bool {
match node.ungrouped() {
EqNode::Fraction { numer, denom } => line_node(numer) && line_node(denom),
EqNode::Paren { left, right, body } => {
matches!((left.as_str(), right.as_str()), ("(", ")") | ("[", "]")) && supports(body)
}
EqNode::Row(items) => items.iter().all(supports),
EqNode::FontStyle {
style: FontStyleKind::Roman,
body,
} => supports(body),
EqNode::Color { body, .. } => supports(body),
_ => line_node(node),
}
}
pub(super) fn compound_frame(node: &EqNode) -> bool {
fn fraction(node: &EqNode) -> bool {
match node.ungrouped() {
EqNode::Fraction { .. } => true,
EqNode::Row(items) => items.iter().any(fraction),
EqNode::Paren { body, .. }
| EqNode::FontStyle { body, .. }
| EqNode::Color { body, .. } => fraction(body),
_ => false,
}
}
match node.ungrouped() {
EqNode::Paren { body, .. } => fraction(body),
EqNode::Row(items) => items.iter().any(compound_frame),
EqNode::FontStyle { body, .. } | EqNode::Color { body, .. } => compound_frame(body),
_ => false,
}
}
fn line_frame(fs: f64) -> (f64, f64) {
let height = fs * 1.2;
(height, height * 0.84)
}
fn row(fs: f64, width: f64, height: f64, baseline: f64, children: Vec<LayoutBox>) -> LayoutBox {
LayoutBox {
font_size: fs,
x: 0.0,
y: 0.0,
width,
height,
baseline,
kind: LayoutKind::Row(children),
}
}
fn font_glyph(text: String, fs: f64, width: f64, height: f64, baseline: f64) -> LayoutBox {
LayoutBox {
font_size: fs,
x: 0.0,
y: 0.0,
width,
height,
baseline,
kind: LayoutKind::FontGlyph {
text,
font_family: FAMILY,
outline: None,
paint: None,
},
}
}
pub(super) fn text(text: &str, fs: f64) -> LayoutBox {
let (height, baseline) = line_frame(fs);
let mut children = Vec::new();
let mut x = 0.0;
for ch in text.chars() {
let width = fs
* if cjk(ch) {
1.0
} else {
glyph_width(ch).unwrap_or(0.0)
};
let mut child = if cjk(ch) {
LayoutBox {
font_size: fs,
x: 0.0,
y: 0.0,
width,
height,
baseline,
kind: LayoutKind::Text(ch.to_string()),
}
} else {
font_glyph(
if ch == '−' {
"-".to_owned()
} else {
ch.to_string()
},
fs,
width,
height,
baseline - fs * 0.06,
)
};
child.x = x;
x += width;
children.push(child);
}
row(fs, x, height, baseline, children)
}
fn operator(node: &EqNode) -> Option<&str> {
match node.ungrouped() {
EqNode::Symbol(text) | EqNode::MathSymbol(text) => Some(text),
EqNode::FontStyle { body, .. } | EqNode::Color { body, .. } => operator(body),
_ => None,
}
}
pub(super) fn row_children(layout: &EqLayout, nodes: &[EqNode], fs: f64) -> Vec<LayoutBox> {
nodes
.iter()
.enumerate()
.map(|(index, node)| {
let mut value = layout.layout_node(node, fs);
let Some(op) = operator(node) else {
return value;
};
let (left, right) = match op {
"+" | "-" | "−" => (
if index > 0 { fs / 9.0 } else { 0.0 },
if index + 1 < nodes.len()
&& !matches!(nodes[index + 1].ungrouped(), EqNode::Paren { .. })
{
fs / 9.0
} else {
0.0
},
),
"=" | "<" | ">" => (
if index > 0 { fs * 2.0 / 15.0 } else { 0.0 },
if index + 1 < nodes.len() {
fs / 6.0
} else {
0.0
},
),
_ => (0.0, 0.0),
};
let width = left + value.width + right;
let (height, baseline) = (value.height, value.baseline);
value.x = left;
row(fs, width, height, baseline, vec![value])
})
.collect()
}
pub(super) fn root(mut value: LayoutBox, fs: f64, dpi: f64) -> LayoutBox {
let pad_hu = (fs * 7200.0 / dpi * 2.0 / 9.0).floor();
let pad = pad_hu * dpi / 7200.0;
value.x = pad;
row(
fs,
value.width + pad,
value.height,
value.baseline,
vec![value],
)
}
pub(super) fn fraction(mut numer: LayoutBox, mut denom: LayoutBox, fs: f64) -> LayoutBox {
let (line_height, line_baseline) = line_frame(fs);
let height = fs * 2.34;
let baseline = height * 0.67;
let width = numer.width.max(denom.width) + fs * 5.0 / 6.0;
numer.x = (width - numer.width) / 2.0;
denom.x = (width - denom.width) / 2.0;
numer.y = line_baseline - numer.baseline;
denom.y = height - line_height + line_baseline - denom.baseline;
LayoutBox {
font_size: fs,
x: 0.0,
y: 0.0,
width,
height,
baseline,
kind: LayoutKind::Fraction {
numer: Box::new(numer),
denom: Box::new(denom),
axis_height: fs * 5.0 / 12.0,
rule_thickness: fs * 0.04,
rule_start: fs * 0.125,
rule_end: width - fs * 0.125,
},
}
}
pub(super) fn parentheses(left: &str, right: &str, mut body: LayoutBox, fs: f64) -> LayoutBox {
let scale = if body.height > fs * 1.2 { 1.421 } else { 1.0 };
let glyph_size = fs * scale;
let slot = glyph_size * if left == "[" { 0.387 } else { 0.375 };
let (height, baseline) = (body.height, body.baseline);
let mut opening = font_glyph(
left.to_owned(),
glyph_size,
slot,
height,
baseline + fs * 0.15,
);
opening.x = if scale != 1.0 { fs / 9.0 } else { 0.0 };
body.x = if scale != 1.0 { fs * 0.6 } else { slot };
let width = body.width + slot * 2.0;
let mut closing = font_glyph(
right.to_owned(),
glyph_size,
slot,
height,
baseline + fs * 0.15,
);
closing.x = body.x + body.width;
if scale != 1.0 {
for glyph in [&mut opening, &mut closing] {
glyph.font_size = fs * 2.03;
glyph.width = glyph.font_size * 0.49 * if left == "[" { 0.387 } else { 0.375 };
if let LayoutKind::FontGlyph { paint, .. } = &mut glyph.kind {
*paint = Some(FontGlyphPaint {
scale_x: 0.49,
stroke_width: 0.0,
});
}
}
}
row(fs, width, height, baseline, vec![opening, body, closing])
}