use super::ast::EqNode;
use super::layout::{LayoutBox, LayoutKind};
use super::symbols::FontStyleKind;
#[path = "legacy_coordinate_plan.rs"]
mod plan;
pub(super) fn group_boundary_gap(left: &EqNode, right: &EqNode) -> Option<f64> {
plan::group_boundary_gap(left, right)
}
const UPPER: [f64; 26] = [
3.92902788, 3.29155200, 3.29155200, 3.29155200, 3.29155200, 3.29155200, 3.29155200, 3.58579968,
1.64839447, 1.64839447, 3.58579968, 3.29154957, 3.92902109, 3.29154957, 3.29154957, 3.03858903,
3.29154957, 3.29154957, 2.78826602, 3.58579968, 3.58579968, 3.58579968, 3.58579968, 3.92902109,
3.58579968, 2.78826602,
];
const LOWER: [f64; 26] = [
2.49795890, 2.25592965, 2.25592965, 2.49795890, 2.25592965, 1.64839443, 2.49795890, 2.49795890,
1.39494582, 1.39494582, 2.25592965, 1.39494582, 3.92902788, 2.78826414, 2.25592965, 2.25592965,
2.25592965, 2.25592965, 1.64839443, 1.39494582, 2.78826414, 2.25592965, 3.29155200, 2.25592965,
2.25592965, 2.25592965,
];
const DIGITS: [f64; 10] = [
2.25593095, 2.25593095, 2.25593095, 2.25593095, 2.25593095, 2.25593095, 2.25593095, 2.25593095,
2.25593095, 2.25593095,
];
fn cjk_letter(character: char) -> bool {
character.is_alphabetic() && super::text_has_cjk(&character.to_string())
}
fn advance(character: char) -> Option<f64> {
match character {
'A'..='Z' => Some(UPPER[character as usize - 'A' as usize]),
'a'..='z' => Some(LOWER[character as usize - 'a' as usize]),
'0'..='9' => Some(DIGITS[character as usize - '0' as usize]),
'%' | '△' | '․' | '=' | '+' | '-' => Some(9.0 / 2.0),
'/' => Some(9.0 / 4.0),
c if cjk_letter(c) => Some(9.0 / 2.0),
_ => None,
}
}
fn linear_source(node: &EqNode) -> bool {
match node {
EqNode::Text(text) => !text.is_empty() && text.bytes().all(|ch| ch.is_ascii_alphabetic()),
EqNode::Number(text) => !text.is_empty() && text.bytes().all(|ch| ch.is_ascii_digit()),
EqNode::Row(items) => items.iter().all(linear_source),
EqNode::FontStyle {
style: FontStyleKind::Italic,
body,
} => linear_source(body),
EqNode::Empty => true,
_ => false,
}
}
fn observe_line(font_size: f64, baseline: f64, frame: &mut Option<(f64, f64)>) -> Option<()> {
if !font_size.is_finite() || font_size <= 0.0 || !baseline.is_finite() {
return None;
}
let expected = frame.get_or_insert((font_size, baseline));
((font_size - expected.0).abs() <= 1e-8 && (baseline - expected.1).abs() <= 1e-8).then_some(())
}
fn inspect(
value: &LayoutBox,
parent_y: f64,
frame: &mut Option<(f64, f64)>,
count: &mut usize,
) -> Option<f64> {
let y = parent_y + value.y;
match &value.kind {
LayoutKind::HancomGlyphs(run) => {
let characters = run.glyphs.chars().count();
if run.font_family != super::legacy_glyphs::FONT_FAMILY
|| run.positions.len() != characters
|| run.shears.len() != characters
{
return None;
}
let baseline = y + value.baseline + run.baseline_shift;
observe_line(value.font_size, baseline, frame)?;
let mut width = 0.0;
for (character, shear) in run.glyphs.chars().zip(&run.shears) {
let expected_shear = if character.is_ascii_alphabetic() {
0.333
} else {
0.0
};
if (*shear - expected_shear).abs() > 1e-8 {
return None;
}
width += advance(character)?;
}
*count += characters;
Some(width)
}
LayoutKind::Text(text) if !text.is_empty() && text.chars().all(cjk_letter) => {
observe_line(value.font_size, y + value.baseline, frame)?;
*count += text.chars().count();
text.chars()
.try_fold(0.0, |width, ch| Some(width + advance(ch)?))
}
LayoutKind::Row(children) => children.iter().try_fold(0.0, |width, child| {
Some(width + inspect(child, y, frame, count)?)
}),
LayoutKind::FontStyle {
style: FontStyleKind::Italic,
body,
} => inspect(body, y, frame, count),
LayoutKind::Empty => Some(0.0),
_ => None,
}
}
fn place(value: &mut LayoutBox, scale: f64) -> f64 {
let width = match &mut value.kind {
LayoutKind::HancomGlyphs(run) => {
let mut cursor = 0.0;
for (character, position) in run.glyphs.chars().zip(&mut run.positions) {
*position = cursor;
cursor += advance(character).expect("검사한 논리 문자") * scale;
}
run.width = cursor;
cursor
}
LayoutKind::Row(children) => {
let mut cursor = 0.0;
for child in children {
child.x = cursor;
cursor += place(child, scale);
}
cursor
}
LayoutKind::FontStyle { body, .. } => {
body.x = 0.0;
place(body, scale)
}
LayoutKind::Empty => 0.0,
_ => unreachable!("검사한 선형 표시 트리"),
};
value.width = width;
width
}
pub(super) fn project_linear(node: &EqNode, value: &mut LayoutBox, width: f64) -> bool {
if !linear_source(node) || !width.is_finite() || width <= 0.0 {
return false;
}
let mut count = 0;
let Some(advance) = inspect(value, 0.0, &mut None, &mut count) else {
return false;
};
if count == 0 || !advance.is_finite() || advance <= 0.0 {
return false;
}
let scale = width / (1.0 + advance);
let mut body = value.clone();
place(&mut body, scale);
body.x = scale;
body.y = 0.0;
value.width = width;
value.kind = LayoutKind::Row(vec![body]);
true
}
fn place_plan(
value: &mut LayoutBox,
positions: &mut impl Iterator<Item = (char, f64)>,
scale: f64,
) {
value.x = 0.0;
value.width = match &mut value.kind {
LayoutKind::HancomGlyphs(run) => {
let mut end = 0.0;
for (character, position) in run.glyphs.chars().zip(&mut run.positions) {
let (expected, x) = positions.next().expect("대조한 문자 소유");
debug_assert_eq!(character, expected);
*position = (1.0 + x) * scale;
end = *position + advance(character).expect("대조한 논리 전진") * scale;
}
run.width = end;
end
}
LayoutKind::Text(text) => {
let mut offsets = Vec::with_capacity(text.chars().count());
let mut end = 0.0;
for character in text.chars() {
let (expected, x) = positions.next().expect("대조한 CJK 문자 소유");
debug_assert_eq!(character, expected);
let origin = (1.0 + x) * scale;
offsets.push(origin);
end = origin + advance(character).expect("대조한 CJK 전진") * scale;
}
let glyphs = text.clone();
let shears = vec![0.0; offsets.len()];
value.kind = LayoutKind::HancomGlyphs(super::hancom_glyphs::GlyphRun {
font_family: "Haansoft Batang",
glyphs,
positions: offsets,
shears,
width: end,
baseline_shift: value.font_size * 0.06,
});
end
}
LayoutKind::Row(children) => {
let mut end: f64 = 0.0;
for child in children {
place_plan(child, positions, scale);
end = end.max(child.width);
}
end
}
LayoutKind::FontStyle { body, .. } => {
place_plan(body, positions, scale);
body.width
}
LayoutKind::Empty => 0.0,
_ => unreachable!("대조한 단일 글줄의 표시 트리"),
};
}
fn displayed_characters(value: &LayoutBox, out: &mut Vec<char>) {
match &value.kind {
LayoutKind::HancomGlyphs(run) => out.extend(run.glyphs.chars()),
LayoutKind::Text(text) => out.extend(text.chars()),
LayoutKind::Row(children) => {
for child in children {
displayed_characters(child, out);
}
}
LayoutKind::FontStyle { body, .. } => displayed_characters(body, out),
_ => {}
}
}
pub(super) fn project(node: &EqNode, value: &mut LayoutBox, width: f64) -> bool {
let Some(plan) = plan::Plan::source(node) else {
return project_linear(node, value, width);
};
if !width.is_finite() || width <= 0.0 || !plan.width.is_finite() || plan.width <= 0.0 {
return false;
}
let mut count = 0;
if inspect(value, 0.0, &mut None, &mut count).is_none() || count == 0 {
return false;
}
let mut actual = Vec::with_capacity(count);
displayed_characters(value, &mut actual);
if !actual
.iter()
.copied()
.eq(plan.glyphs.iter().map(|(ch, _)| *ch))
{
return false;
}
let scale = width / (1.0 + plan.width);
place_plan(value, &mut plan.glyphs.into_iter(), scale);
value.width = width;
true
}