pub const FONT_FAMILY: &str = "HyhwpEQ";
#[derive(Debug, Clone, serde::Serialize)]
pub struct GlyphRun {
pub font_family: &'static str,
pub glyphs: String,
pub positions: Vec<f64>,
pub shears: Vec<f64>,
pub width: f64,
pub baseline_shift: f64,
}
#[derive(Clone, Copy)]
pub(super) enum Role {
Variable {
italic: bool,
},
Number,
Operator,
Greek,
BigOperator,
SmallOperator,
LegacyLiteral,
QuotedLiteral,
}
pub(super) fn is_operator(text: &str) -> bool {
matches!(
text,
"+" | "-" | "=" | ":" | "<" | ">" | "×" | "÷" | "≤" | "≥"
)
}
const ITALIC_LOWER: [u16; 26] = [
541, 439, 442, 532, 476, 553, 487, 590, 352, 421, 532, 305, 899, 614, 496, 515, 457, 462, 480,
369, 585, 496, 732, 585, 502, 476,
];
const ITALIC_UPPER: [u16; 26] = [
768, 725, 739, 782, 696, 667, 803, 768, 369, 526, 795, 640, 938, 768, 795, 696, 795, 753, 568,
739, 768, 768, 1051, 768, 768, 625,
];
const ROMAN_LOWER: [u16; 26] = [
512, 554, 512, 554, 554, 384, 554, 554, 298, 298, 554, 298, 853, 554, 554, 554, 554, 426, 512,
384, 554, 554, 810, 597, 597, 469,
];
const ROMAN_UPPER: [u16; 26] = [
768, 682, 682, 725, 682, 640, 725, 768, 384, 469, 768, 640, 938, 768, 725, 640, 725, 682, 640,
768, 768, 725, 981, 682, 682, 640,
];
const NUMBERS: [u16; 10] = [512, 512, 512, 512, 512, 512, 512, 512, 512, 512];
fn glyph(ch: char, role: Role) -> Option<(char, u16)> {
let (code, advance) = match role {
Role::Variable { italic } if ch.is_ascii_lowercase() => {
let index = ch as usize - 'a' as usize;
if italic {
(0xe0e5 + index as u32, ITALIC_LOWER[index])
} else {
(ch as u32, ROMAN_LOWER[index])
}
}
Role::Variable { italic } if ch.is_ascii_uppercase() => {
let index = ch as usize - 'A' as usize;
if italic {
(0xe000 + index as u32, ITALIC_UPPER[index])
} else {
(ch as u32, ROMAN_UPPER[index])
}
}
Role::Number if ch.is_ascii_digit() => {
let index = ch as usize - '0' as usize;
let code = if index == 0 {
0xe03d
} else {
0xe033 + index as u32
};
(code, NUMBERS[index])
}
Role::Operator => match ch {
'+' => (0xe048, 795),
'-' => (0xe046, 774),
'=' => (0xe047, 795),
':' => (0xe04f, 283),
'<' => (0xe055, 864),
'>' => (0xe056, 864),
'×' => (0xd7, 831),
'÷' => (0xf7, 831),
'≤' => (0x2264, 1024),
'≥' => (0x2265, 1024),
'∴' => (0x2234, 1024),
'∵' => (0x2235, 1024),
',' => (0xe052, 283),
'(' => (0xe044, 398),
')' => (0xe045, 398),
_ => return None,
},
Role::Greek => {
if let Some(index) = "αβγδεζηθικλμνξοπρστυφχψω".chars().position(|c| c == ch)
{
(0xe09d + index as u32, 0)
} else {
let index = "ΑΒΓΔΕΖΗΘΙΚΛΜΝΞΟΠΡΣΤΥΦΧΨΩ".chars().position(|c| c == ch)?;
(0xe085 + index as u32, 0)
}
}
Role::BigOperator if ch == '∑' => (0xe067, 739),
Role::SmallOperator if ch == '∏' => (0x220f, 1024),
Role::QuotedLiteral if ch == '\'' => (0xe050, 163),
Role::LegacyLiteral => match ch {
'△' => (0x25b3, 1024),
'․' => (0x22c5, 1024),
'%' => (0xe042, 1024),
_ => return None,
},
_ => return None,
};
Some((char::from_u32(code)?, advance))
}
#[cfg(feature = "native-skia")]
pub(crate) fn semantic_fallback_glyph(family: &str, encoded: char) -> Option<(char, bool)> {
use std::collections::BTreeMap;
use std::sync::OnceLock;
type Mappings = BTreeMap<char, (char, bool)>;
static MAPPINGS: OnceLock<(Mappings, Mappings)> = OnceLock::new();
if !(0xe000..=0xf8ff).contains(&(encoded as u32)) {
return None;
}
let hancom_eqn = family.eq_ignore_ascii_case("HancomEQN");
if !hancom_eqn && !family.eq_ignore_ascii_case(FONT_FAMILY) {
return None;
}
let maps = MAPPINGS.get_or_init(|| {
let build = |extended: bool| {
let mut map = Mappings::new();
let characters = ('A'..='Z').chain('a'..='z').chain('0'..='9').chain(
"+-= :<>×÷≤≥∴∵,()αβγδεζηθικλμνξοπρστυφχψωΑΒΓΔΕΖΗΘΙΚΛΜΝΞΟΠΡΣΤΥΦΧΨΩ∑∏△․%".chars(),
);
for character in characters {
for role in [
Role::Variable { italic: false },
Role::Variable { italic: true },
Role::Number,
Role::Operator,
Role::Greek,
Role::BigOperator,
Role::SmallOperator,
Role::LegacyLiteral,
] {
if !extended && matches!(role, Role::Greek | Role::BigOperator) {
continue;
}
if let Some((code, _)) = glyph(character, role) {
if (0xe000..=0xf8ff).contains(&(code as u32)) {
let intrinsic_italic = matches!(role, Role::Variable { italic: true })
&& character.is_ascii_lowercase();
map.insert(code, (character, intrinsic_italic));
}
}
}
}
map
};
(build(false), build(true))
});
(if hancom_eqn { &maps.1 } else { &maps.0 })
.get(&encoded)
.copied()
}
pub(super) fn measure(
text: &str,
role: Role,
fs: f64,
dpi: f64,
pad: f64,
font_family: &'static str,
) -> Option<GlyphRun> {
if font_family != "HancomEQN" && matches!(role, Role::Greek | Role::BigOperator) {
return None;
}
if text.is_empty() || !fs.is_finite() || fs <= 0.0 || !dpi.is_finite() || dpi <= 0.0 {
return None;
}
let logical_size = (fs * 48.0 / dpi).round().max(1.0) * 2.0;
let mut glyphs = String::new();
let mut positions = Vec::new();
let mut shears = Vec::new();
let mut width = pad;
for ch in text.chars() {
let (rendered, advance) = glyph(ch, role)?;
positions.push(width);
shears.push(
if matches!(role, Role::Variable { italic: true }) && ch.is_ascii_uppercase() {
0.333
} else {
0.0
},
);
glyphs.push(rendered);
width += if matches!(role, Role::SmallOperator) {
(logical_size * f64::from(advance) / 1024.0).round() * dpi / 96.0
} else if font_family == "HancomEQN" {
super::hancom_eqn_metrics::advance(rendered, fs, dpi)?
} else if matches!(role, Role::Variable { italic: false }) {
super::layout::measured_roman_advance(&ch.to_string(), fs, dpi).unwrap_or_else(|| {
(logical_size * f64::from(advance) / 1024.0).round() * dpi / 96.0
})
} else {
(logical_size * f64::from(advance) / 1024.0).round() * dpi / 96.0
};
if matches!(role, Role::Operator) && ch == ',' {
width += fs * 0.07;
}
}
Some(GlyphRun {
font_family,
glyphs,
positions,
shears,
width: width + pad,
baseline_shift: if matches!(role, Role::Variable { italic: false })
|| matches!(role, Role::LegacyLiteral) && text != "%"
|| matches!(role, Role::Operator) && text.is_ascii()
{
fs * if font_family == "HancomEQN" {
-0.05
} else {
820.0 / 1024.0 - 0.85
}
} else {
0.0
},
})
}
pub(crate) fn is_linear(layout: &super::layout::LayoutBox) -> bool {
use super::layout::LayoutKind;
match &layout.kind {
LayoutKind::HancomGlyphs(_) => true,
LayoutKind::Row(children) => !children.is_empty() && children.iter().all(is_linear),
LayoutKind::FontStyle {
style: super::symbols::FontStyleKind::Roman | super::symbols::FontStyleKind::Italic,
body,
} => is_linear(body),
_ => false,
}
}
pub(crate) fn is_used(layout: &super::layout::LayoutBox) -> bool {
!used_font_families(layout).is_empty()
}
pub(crate) fn used_font_families(
layout: &super::layout::LayoutBox,
) -> std::collections::BTreeSet<&'static str> {
use super::layout::LayoutKind;
let mut pending = vec![layout];
let mut families = std::collections::BTreeSet::new();
while let Some(item) = pending.pop() {
match &item.kind {
LayoutKind::HancomGlyphs(run) => {
families.insert(run.font_family);
}
LayoutKind::FontGlyph { font_family, .. }
if *font_family == super::legacy_glyphs::FONT_FAMILY =>
{
families.insert(*font_family);
}
LayoutKind::Row(children) => pending.extend(children),
LayoutKind::Fraction { numer, denom, .. } => {
pending.push(numer);
pending.push(denom);
}
LayoutKind::Atop { top, bottom } => {
pending.push(top);
pending.push(bottom);
}
LayoutKind::Sqrt { index, body } => {
pending.extend(index.as_deref());
pending.push(body);
}
LayoutKind::Superscript { base, sup } => {
pending.push(base);
pending.push(sup);
}
LayoutKind::Subscript { base, sub } => {
pending.push(base);
pending.push(sub);
}
LayoutKind::SubSup { base, sub, sup } => {
pending.push(base);
pending.push(sub);
pending.push(sup);
}
LayoutKind::BigOp { sub, sup, .. } => {
pending.extend(sub.as_deref());
pending.extend(sup.as_deref());
}
LayoutKind::Limit { name, sub, .. } => {
pending.extend(name.as_deref());
pending.extend(sub.as_deref());
}
LayoutKind::Matrix { cells, .. } => pending.extend(cells.iter().flatten()),
LayoutKind::Rel { arrow, over, under } => {
pending.push(arrow);
pending.push(over);
pending.extend(under.as_deref());
}
LayoutKind::EqAlign { rows } => {
for (left, right) in rows {
pending.push(left);
pending.push(right);
}
}
LayoutKind::Paren { body, .. }
| LayoutKind::Decoration { body, .. }
| LayoutKind::FontStyle { body, .. } => pending.push(body),
_ => {}
}
}
families
}