use super::{font_metrics_data, TextStyle};
#[path = "arabic_context/face_0.rs"]
mod face_0;
#[path = "arabic_context/face_1.rs"]
mod face_1;
#[path = "arabic_context/face_2.rs"]
mod face_2;
#[path = "arabic_context/face_3.rs"]
mod face_3;
#[path = "arabic_context/joining.rs"]
mod joining;
type Row = (u32, [u16; 4], [u16; 4], u8);
type Rule = (u16, &'static [u16], u16, u16, bool, bool);
struct Source {
rows: &'static [Row],
rules: &'static [Rule],
}
impl Source {
fn row(&self, ch: char) -> Option<&'static Row> {
self.rows
.binary_search_by_key(&(ch as u32), |row| row.0)
.ok()
.map(|i| &self.rows[i])
}
}
fn source(style: &TextStyle) -> Option<Source> {
let family = style
.metric_font_family
.as_deref()
.unwrap_or(&style.font_family);
let primary = family.split(',').next()?.trim().trim_matches(['\'', '"']);
if !matches!(
primary,
"HCR Batang" | "함초롬바탕" | "HCR Dotum" | "함초롬돋움"
) {
return None;
}
let metric = font_metrics_data::find_metric(primary, style.bold, style.italic)?.metric;
if metric.em_size != 1000 {
return None;
}
let (rows, rules) = match (primary, metric.bold) {
("HCR Batang" | "함초롬바탕", false) => (face_0::ROWS, face_0::RULES),
("HCR Batang" | "함초롬바탕", true) => (face_1::ROWS, face_1::RULES),
("HCR Dotum" | "함초롬돋움", false) => (face_2::ROWS, face_2::RULES),
("HCR Dotum" | "함초롬돋움", true) => (face_3::ROWS, face_3::RULES),
_ => return None,
};
Some(Source { rows, rules })
}
fn neighbor_kind(chars: &[char], index: usize, forward: bool, source: &Source) -> u8 {
let mut at = index;
loop {
at = if forward {
let Some(next) = at.checked_add(1).filter(|&i| i < chars.len()) else {
return 0;
};
next
} else {
let Some(previous) = at.checked_sub(1) else {
return 0;
};
previous
};
let ch = chars[at];
let kind = joining::kind(ch);
if kind == 4 {
continue;
}
return if ch == '\u{200d}' || source.row(ch).is_some() {
kind
} else {
0
};
}
}
struct Glyph {
id: Option<u16>,
width: f64,
mark: bool,
owners: Vec<(usize, f64)>,
}
fn apply_lookup(glyphs: &mut Vec<Glyph>, rules: &[Rule]) {
match apply_lookup_with_storage(glyphs, rules, &crate::storage::Unbounded) {
Ok(()) => (),
Err(never) => match never {},
}
}
fn apply_lookup_with_storage<S: crate::storage::Storage>(
glyphs: &mut Vec<Glyph>,
rules: &[Rule],
storage: &S,
) -> Result<(), S::Error> {
let mut index = 0;
while index < glyphs.len() {
for &(_, inputs, output, width, ignore_marks, mark) in rules {
if glyphs[index].id != inputs.first().copied() {
continue;
}
let mut matched = storage.collect(std::iter::once(index))?;
let mut at = index + 1;
for input in &inputs[1..] {
while ignore_marks && glyphs.get(at).is_some_and(|glyph| glyph.mark) {
at += 1;
}
if glyphs.get(at).and_then(|glyph| glyph.id) != Some(*input) {
break;
}
storage.push(&mut matched, at)?;
at += 1;
}
if matched.len() != inputs.len() {
continue;
}
let mut owners = Vec::new();
for &at in &matched {
storage.extend(&mut owners, glyphs[at].owners.iter().copied())?;
}
glyphs[index] = Glyph {
id: Some(output),
width: f64::from(width),
mark,
owners,
};
for &at in matched.iter().skip(1).rev() {
glyphs.remove(at);
}
break;
}
index += 1;
}
Ok(())
}
pub(crate) fn advances(chars: &[char], style: &TextStyle) -> Option<Vec<Option<f64>>> {
match advances_with_storage(chars, style, &crate::storage::Unbounded) {
Ok(value) => value,
Err(never) => match never {},
}
}
pub(crate) fn advances_with_storage<S: crate::storage::Storage>(
chars: &[char],
style: &TextStyle,
storage: &S,
) -> Result<Option<Vec<Option<f64>>>, S::Error> {
crate::storage::with_optional(|failure| {
if !chars.iter().any(|ch| matches!(*ch as u32, 0x0600..=0x08ff)) {
return None;
}
let source = source(style)?;
let mut glyphs = Vec::new();
for (index, &ch) in chars.iter().enumerate() {
if ch == '\u{200d}' {
continue;
}
let Some(row) = source.row(ch) else {
crate::storage::optional(
storage.push(
&mut glyphs,
Glyph {
id: None,
width: 0.0,
mark: joining::kind(ch) == 4,
owners: Vec::new(),
},
),
failure,
)?;
continue;
};
let kind = joining::kind(ch);
let previous = kind & 1 != 0 && neighbor_kind(chars, index, false, &source) & 2 != 0;
let next = kind & 2 != 0 && neighbor_kind(chars, index, true, &source) & 1 != 0;
let form = match (previous, next) {
(false, false) => 0,
(false, true) => 1,
(true, true) => 2,
(true, false) => 3,
};
let width = f64::from(row.2[form]);
crate::storage::optional(
storage.push(
&mut glyphs,
Glyph {
id: Some(row.1[form]),
width,
mark: row.3 & (1 << form) != 0,
owners: crate::storage::optional(
storage.collect(std::iter::once((index, width))),
failure,
)?,
},
),
failure,
)?;
}
let mut start = 0;
while start < source.rules.len() {
let end = start
+ source.rules[start..].partition_point(|rule| rule.0 == source.rules[start].0);
crate::storage::optional(
apply_lookup_with_storage(&mut glyphs, &source.rules[start..end], storage),
failure,
)?;
start = end;
}
let mut advances = crate::storage::optional(storage.filled(chars.len(), None), failure)?;
for glyph in glyphs {
let sum: f64 = glyph.owners.iter().map(|(_, width)| *width).sum();
for &(owner, original_width) in &glyph.owners {
let share = if sum > 0.0 {
original_width / sum
} else {
1.0 / glyph.owners.len() as f64
};
advances[owner] = Some(glyph.width * share / 1000.0);
}
}
Some(advances)
})
}