use super::{variable_style, LayoutBox, LayoutKind};
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_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 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,
];
pub(super) fn logical_advance(units: u16, fs: f64, dpi: f64) -> f64 {
logical_em_advance(f64::from(units) / 1024.0, fs, dpi)
}
pub(super) fn logical_em_advance(em: f64, fs: f64, dpi: f64) -> f64 {
const ADVANCE_QUANTUM_HWP: f64 = 75.0;
let font_size_hwp = fs * 7200.0 / dpi;
let advance_hwp = (em * font_size_hwp / ADVANCE_QUANTUM_HWP).round() * ADVANCE_QUANTUM_HWP;
advance_hwp * dpi / 7200.0
}
pub(super) fn layout(
text: &str,
fs: f64,
dpi: f64,
italic: bool,
bold: bool,
outline_contract: bool,
) -> Option<LayoutBox> {
if text.is_empty()
|| !text.bytes().all(|ch| ch.is_ascii_alphabetic())
|| (bold && !outline_contract)
{
return None;
}
let paint = if bold {
Some(super::source_variable_style::bold_paint(fs, dpi)?)
} else {
None
};
let mut x = 0.0;
let mut children = Vec::with_capacity(text.len());
for ch in text.bytes() {
let (advances, first) = match (italic, ch.is_ascii_uppercase()) {
(true, true) => (&ITALIC_UPPER, b'A'),
(true, false) => (&ITALIC_LOWER, b'a'),
(false, true) => (&ROMAN_UPPER, b'A'),
(false, false) => (&ROMAN_LOWER, b'a'),
};
let width = if !italic {
super::source_roman_advance::measured(&char::from(ch).to_string(), fs, dpi)
} else {
super::source_italic_advance::measured(ch, fs, dpi).or_else(|| {
(ch == b'f')
.then(|| super::source_font_advance::measured(fs, dpi, 3))
.flatten()
})
}
.unwrap_or_else(|| {
let em = super::source_roman_advance::em_advance(fs, dpi).unwrap_or(fs);
logical_advance(advances[usize::from(ch - first)], em, dpi)
}) + if bold {
super::source_variable_style::bold_extra_advance(dpi)
} else {
0.0
};
let display = variable_style::display_text(&char::from(ch).to_string(), italic, bold);
let outline = outline_contract
.then(|| {
super::super::variable_outline::commands(char::from(ch), fs, dpi, fs * 0.8, italic)
})
.flatten();
let kind = if let Some(outline) = outline {
LayoutKind::FontGlyph {
text: display,
font_family: "Latin Modern Math, STIX Two Math, serif",
outline: Some(outline),
paint,
}
} else {
LayoutKind::Text(display)
};
children.push(LayoutBox {
font_size: fs,
x,
y: 0.0,
width,
height: fs,
baseline: fs * 0.8,
kind,
});
x += width;
}
Some(LayoutBox {
font_size: fs,
x: 0.0,
y: 0.0,
width: x,
height: fs,
baseline: fs * 0.8,
kind: LayoutKind::Row(children),
})
}