use super::hancom_glyphs::{GlyphRun, Role};
pub(super) const FONT_FAMILY: &str = "RHWP Hancom Legacy Equation";
const UPPER: [u16; 26] = [
875, 750, 750, 750, 750, 750, 750, 875, 375, 500, 875, 750, 1000, 875, 750, 750, 750, 750, 625,
813, 875, 875, 1000, 813, 813, 625,
];
const LOWER: [u16; 26] = [
500, 563, 500, 563, 500, 375, 625, 563, 313, 313, 625, 313, 875, 563, 500, 563, 563, 375, 500,
375, 563, 625, 875, 625, 563, 500,
];
const ITALIC_UPPER: [u16; 26] = [
875, 750, 750, 750, 750, 750, 750, 813, 375, 375, 813, 750, 875, 750, 750, 688, 750, 750, 625,
813, 813, 813, 813, 875, 813, 625,
];
const ITALIC_LOWER: [u16; 26] = [
563, 500, 500, 563, 500, 375, 563, 563, 313, 313, 500, 313, 875, 625, 500, 500, 500, 500, 375,
313, 625, 500, 750, 500, 500, 500,
];
fn advance(character: char, role: Role) -> Option<u16> {
match role {
Role::Variable { italic: true } if character.is_ascii_uppercase() => {
Some(ITALIC_UPPER[character as usize - 'A' as usize])
}
Role::Variable { italic: true } if character.is_ascii_lowercase() => {
Some(ITALIC_LOWER[character as usize - 'a' as usize])
}
Role::Variable { .. } if character.is_ascii_uppercase() => {
Some(UPPER[character as usize - 'A' as usize])
}
Role::Variable { .. } if character.is_ascii_lowercase() => {
Some(LOWER[character as usize - 'a' as usize])
}
Role::Number if character.is_ascii_digit() || character == '.' => Some(500),
Role::LegacyLiteral if matches!(character, '%' | '△' | '․') => Some(1000),
Role::Operator => match character {
'+' | '-' | '=' | '⋅' => Some(1000),
',' | '.' | '/' | '<' | '>' => Some(500),
':' | '(' | ')' => Some(375),
'[' | ']' => Some(387),
_ => None,
},
_ => None,
}
}
pub(super) fn measure(text: &str, role: Role, fs: f64, dpi: f64, pad: f64) -> Option<GlyphRun> {
if text.is_empty()
|| !fs.is_finite()
|| fs <= 0.0
|| !dpi.is_finite()
|| dpi <= 0.0
|| !pad.is_finite()
|| pad < 0.0
{
return None;
}
let mut positions = Vec::with_capacity(text.len());
let mut shears = Vec::with_capacity(text.len());
let mut width = pad;
for character in text.chars() {
let units = advance(character, role)?;
positions.push(width);
shears.push(if matches!(role, Role::Variable { italic: true }) {
0.333
} else {
0.0
});
let logical_font_pixels = (fs * 96.0 / dpi).round();
width += (f64::from(units) * logical_font_pixels / 1000.0).round() * dpi / 96.0;
}
Some(GlyphRun {
font_family: FONT_FAMILY,
glyphs: text.to_owned(),
positions,
shears,
width: width + pad,
baseline_shift: 0.0,
})
}
pub(super) fn is_line(value: &super::layout::LayoutBox, fs: f64) -> bool {
if (value.font_size - fs).abs() > 1e-8 {
return false;
}
match &value.kind {
super::layout::LayoutKind::HancomGlyphs(run) => {
run.font_family == FONT_FAMILY && !run.glyphs.is_empty()
}
super::layout::LayoutKind::Row(items) => {
!items.is_empty()
&& items.iter().all(|item| {
(item.y + item.baseline - value.baseline).abs() < 1e-8 && is_line(item, fs)
})
}
super::layout::LayoutKind::FontStyle { body, .. }
| super::layout::LayoutKind::Color { body, .. } => {
(body.y + body.baseline - value.baseline).abs() < 1e-8 && is_line(body, fs)
}
_ => false,
}
}