use f_office_fonts::{FaceClassification, FamilyName};
use crate::params::{
PANOSE_FIRST_SANS_SERIF, PANOSE_LAST_SANS_SERIF, PANOSE_LATIN_PICTORIAL, PANOSE_LATIN_TEXT,
PANOSE_MONOSPACED,
};
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(super) enum PanoseClass {
Monospaced,
Sans,
Roman,
}
impl PanoseClass {
pub(super) fn of(classification: Option<FaceClassification>) -> Self {
let Some(FaceClassification { panose, .. }) = classification else {
return Self::Roman;
};
let [family_kind, serif_style, _, proportion, ..] = panose;
if proportion == PANOSE_MONOSPACED {
Self::Monospaced
} else if family_kind == PANOSE_LATIN_TEXT
&& (PANOSE_FIRST_SANS_SERIF..=PANOSE_LAST_SANS_SERIF).contains(&serif_style)
{
Self::Sans
} else {
Self::Roman
}
}
}
struct Row {
blocks: &'static [(char, char)],
own: &'static [(&'static str, &'static str)],
by_class: ByClass,
}
struct ByClass {
monospaced: &'static str,
sans: &'static str,
roman: &'static str,
}
impl ByClass {
const fn all(family: &'static str) -> Self {
Self {
monospaced: family,
sans: family,
roman: family,
}
}
const fn of(&self, class: PanoseClass) -> &'static str {
match class {
PanoseClass::Monospaced => self.monospaced,
PanoseClass::Sans => self.sans,
PanoseClass::Roman => self.roman,
}
}
}
const ROWS: [Row; 7] = [
Row {
blocks: &[
('\u{1100}', '\u{11FF}'),
('\u{3130}', '\u{318F}'),
('\u{AC00}', '\u{D7A3}'),
],
own: &[
("Times New Roman", "Batang"),
("SimSun", "Batang"),
("DengXian", "Batang"),
("PMingLiU", "Batang"),
("MS PMincho", "Batang"),
],
by_class: ByClass::all("Malgun Gothic"),
},
Row {
blocks: &[('\u{0900}', '\u{097F}')],
own: &[],
by_class: ByClass::all("Nirmala UI"),
},
Row {
blocks: &[('\u{2460}', '\u{2473}')],
own: &[],
by_class: ByClass::all("Cambria Math"),
},
Row {
blocks: &[('\u{2474}', '\u{249B}')],
own: &[
("Times New Roman", "Yu Gothic"),
("Batang", "Yu Gothic"),
("PMingLiU", "Yu Gothic"),
],
by_class: ByClass {
monospaced: "MS Mincho",
sans: "MS Gothic",
roman: "MS Mincho",
},
},
Row {
blocks: &[
('\u{0590}', '\u{05FF}'),
('\u{0600}', '\u{06FF}'),
('\u{1F00}', '\u{1FFF}'),
],
own: &[],
by_class: ByClass {
monospaced: "Courier New",
sans: "Arial",
roman: "Times New Roman",
},
},
Row {
blocks: &[
('\u{0100}', '\u{017F}'),
('\u{1E00}', '\u{1EFF}'),
('\u{0400}', '\u{04FF}'),
],
own: &[],
by_class: ByClass {
monospaced: "Calibri",
sans: "Calibri",
roman: "Cambria",
},
},
Row {
blocks: &[('\u{2600}', '\u{26FF}'), ('\u{2700}', '\u{27BF}')],
own: &[],
by_class: ByClass::all("Segoe UI Symbol"),
},
];
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum Step {
Class,
CambriaMath,
EastAsian,
SegoeUiSymbol,
}
impl Step {
const fn face(self, class: PanoseClass) -> &'static str {
match self {
Self::Class => CLASS_FACES.of(class),
Self::CambriaMath => "Cambria Math",
Self::EastAsian => EAST_ASIAN_FACES.of(class),
Self::SegoeUiSymbol => "Segoe UI Symbol",
}
}
}
const CLASS_FACES: ByClass = ByClass {
monospaced: "Courier New",
sans: "Arial",
roman: "Times New Roman",
};
const EAST_ASIAN_FACES: ByClass = ByClass {
monospaced: "MS Mincho",
sans: "MS Gothic",
roman: "MS Mincho",
};
struct Group {
blocks: &'static [(char, char)],
steps: &'static [Step],
}
const GROUPS: [Group; 4] = [
Group {
blocks: &[('\u{2023}', '\u{2023}'), ('\u{2043}', '\u{2043}')],
steps: &[],
},
Group {
blocks: &[FIXED_SPACES],
steps: &[Step::Class, Step::CambriaMath, Step::SegoeUiSymbol],
},
Group {
blocks: &[
('\u{02B0}', '\u{02FF}'),
('\u{2190}', '\u{21FF}'),
('\u{2500}', '\u{25FF}'),
],
steps: &[Step::Class, Step::CambriaMath, Step::SegoeUiSymbol],
},
Group {
blocks: &[
('\u{2000}', '\u{206F}'),
('\u{2100}', '\u{218F}'),
('\u{2200}', '\u{23FF}'),
],
steps: &[Step::CambriaMath, Step::EastAsian, Step::SegoeUiSymbol],
},
];
const FIXED_SPACES: (char, char) = ('\u{2002}', '\u{2003}');
pub(super) fn kept_in_form_text(ch: char) -> bool {
(FIXED_SPACES.0..=FIXED_SPACES.1).contains(&ch)
}
fn left_alone(ch: char) -> bool {
(ch.is_whitespace()
&& !matches!(ch, '\u{2028}' | '\u{2029}')
&& !(FIXED_SPACES.0..=FIXED_SPACES.1).contains(&ch))
|| ch.is_control()
|| matches!(ch, '\u{200B}'..='\u{200F}' | '\u{202A}'..='\u{202E}' | '\u{2060}'..='\u{206F}')
|| ('\u{E000}'..='\u{F8FF}').contains(&ch)
}
pub(super) fn substitutes(
(family, classification): (&str, Option<FaceClassification>),
ch: char,
latin: bool,
) -> Vec<FamilyName> {
let holds = |blocks: &[(char, char)]| {
blocks
.iter()
.any(|&(first, last)| (first..=last).contains(&ch))
};
let class = PanoseClass::of(classification);
if let Some(row) = ROWS.iter().find(|row| holds(row.blocks)) {
let own = row
.own
.iter()
.find(|(named, _)| FamilyName::new(family).is(named))
.map(|&(_, substitute)| substitute);
return vec![FamilyName::from_static(
own.unwrap_or_else(|| row.by_class.of(class)),
)];
}
let pictorial = classification.is_some_and(|c| c.panose[0] == PANOSE_LATIN_PICTORIAL);
if !latin || pictorial || left_alone(ch) {
return Vec::new();
}
let steps = GROUPS
.iter()
.find(|group| holds(group.blocks))
.map_or(&[][..], |group| group.steps);
steps
.iter()
.map(|step| FamilyName::from_static(step.face(class)))
.collect()
}
pub(super) fn keeps_east_asian(family: &str, classification: Option<FaceClassification>) -> bool {
classification.is_some_and(|c| c.east_asian) || FamilyName::new(family).is("Times New Roman")
}
pub(super) const EAST_ASIAN_SUBSTITUTE: FamilyName = FamilyName::from_static("MS Mincho");
const LABEL_DEVANAGARI_SUBSTITUTE: FamilyName = FamilyName::from_static("Mangal");
pub(super) fn label_substitute(ch: char) -> Option<FamilyName> {
('\u{0900}'..='\u{097F}')
.contains(&ch)
.then_some(LABEL_DEVANAGARI_SUBSTITUTE)
}
#[cfg(test)]
mod tests {
use super::*;
const fn classified(panose: [u8; 10], east_asian: bool) -> FaceClassification {
FaceClassification { panose, east_asian }
}
fn steps(family: &str, panose: [u8; 10], ch: char) -> Vec<String> {
substitutes((family, Some(classified(panose, false))), ch, true)
.iter()
.map(|f| f.as_str().to_owned())
.collect()
}
fn take(family: &str, panose: [u8; 10], ch: char) -> Option<String> {
steps(family, panose, ch).into_iter().next()
}
fn class(panose: [u8; 10]) -> PanoseClass {
PanoseClass::of(Some(classified(panose, false)))
}
const SANS: [u8; 10] = [2, 11, 6, 4, 2, 2, 2, 2, 2, 4];
const ROMAN: [u8; 10] = [2, 4, 5, 3, 5, 4, 6, 3, 2, 4];
const MONOSPACED: [u8; 10] = [2, 7, 3, 9, 2, 2, 5, 2, 4, 4];
const SCRIPT: [u8; 10] = [3, 15, 7, 2, 3, 3, 2, 2, 2, 4];
#[test]
fn the_class_reads_the_proportion_before_the_serif_style() {
assert_eq!(class(SANS), PanoseClass::Sans);
assert_eq!(class(ROMAN), PanoseClass::Roman);
let monospaced_sans = [2, 11, 6, 9, 2, 2, 4, 3, 2, 4];
assert_eq!(class(monospaced_sans), PanoseClass::Monospaced);
assert_eq!(class(MONOSPACED), PanoseClass::Monospaced);
assert_eq!(class(SCRIPT), PanoseClass::Roman);
assert_eq!(PanoseClass::of(None), PanoseClass::Roman);
}
#[test]
fn latin_and_complex_blocks_take_the_face_of_the_class() {
assert_eq!(take("Segoe UI", SANS, 'č').as_deref(), Some("Calibri"));
assert_eq!(take("Georgia", ROMAN, 'Ж').as_deref(), Some("Cambria"));
assert_eq!(
take("Courier New", MONOSPACED, 'ě').as_deref(),
Some("Calibri")
);
assert_eq!(take("Segoe UI", SANS, 'א').as_deref(), Some("Arial"));
assert_eq!(
take("Courier New", MONOSPACED, 'ש').as_deref(),
Some("Courier New")
);
}
#[test]
fn other_blocks_take_their_face() {
assert_eq!(
take("Georgia", ROMAN, '\u{2474}').as_deref(),
Some("MS Mincho")
);
assert_eq!(
take("Segoe UI", SANS, '\u{2488}').as_deref(),
Some("MS Gothic")
);
assert_eq!(
take("Segoe UI", SANS, '한').as_deref(),
Some("Malgun Gothic")
);
assert_eq!(take("Georgia", ROMAN, 'क').as_deref(), Some("Nirmala UI"));
assert_eq!(take("Georgia", ROMAN, '①').as_deref(), Some("Cambria Math"));
assert_eq!(take("Georgia", ROMAN, 'ก'), None);
}
#[test]
fn symbols_and_dingbats_take_segoe_ui_symbol() {
assert_eq!(take("Arial", SANS, '❏').as_deref(), Some("Segoe UI Symbol"));
assert_eq!(
take("Courier New", MONOSPACED, '☎').as_deref(),
Some("Segoe UI Symbol")
);
}
#[test]
fn shapes_take_the_class_face_then_cambria_math() {
assert_eq!(
steps("Open Sans", SANS, '\u{25CF}'),
["Arial", "Cambria Math", "Segoe UI Symbol"]
);
assert_eq!(
take("Roboto", ROMAN, '\u{25CB}').as_deref(),
Some("Times New Roman")
);
assert_eq!(
take("Consolas", MONOSPACED, '\u{2193}').as_deref(),
Some("Courier New")
);
assert_eq!(take("Verdana", SANS, '\u{02CD}').as_deref(), Some("Arial"));
}
#[test]
fn symbols_take_cambria_math_then_the_east_asian_face_of_the_class() {
assert_eq!(
steps("Century Gothic", SANS, '\u{2028}'),
["Cambria Math", "MS Gothic", "Segoe UI Symbol"]
);
assert_eq!(
steps("Garamond", ROMAN, '\u{203B}'),
["Cambria Math", "MS Mincho", "Segoe UI Symbol"]
);
assert_eq!(
take("Rockwell", ROMAN, '\u{2103}').as_deref(),
Some("Cambria Math")
);
assert_eq!(
take("Consolas", MONOSPACED, '\u{2160}').as_deref(),
Some("Cambria Math")
);
}
#[test]
fn en_and_em_spaces_take_the_class_face_then_cambria_math() {
assert_eq!(
steps("Arial Narrow", SANS, '\u{2002}'),
["Arial", "Cambria Math", "Segoe UI Symbol"]
);
assert_eq!(
take("Comic Sans MS", SCRIPT, '\u{2003}').as_deref(),
Some("Times New Roman")
);
assert_eq!(
steps("Courier New", MONOSPACED, '\u{2002}'),
["Courier New", "Cambria Math", "Segoe UI Symbol"]
);
}
#[test]
fn the_bullets_of_general_punctuation_take_no_substitute() {
assert_eq!(take("Calibri", SANS, '\u{2023}'), None);
assert_eq!(take("Garamond", ROMAN, '\u{2043}'), None);
}
#[test]
fn spaces_controls_private_use_and_pictorial_faces_stay_out_of_the_groups() {
assert_eq!(take("Arial Narrow", SANS, '\u{2009}'), None);
assert_eq!(take("Arial Narrow", SANS, '\u{200C}'), None);
assert_eq!(take("Arial", SANS, '\u{0009}'), None);
assert_eq!(take("Times New Roman", ROMAN, '\u{F02A}'), None);
let pictorial = [5, 5, 1, 2, 1, 7, 6, 2, 5, 7];
assert_eq!(take("Symbol", pictorial, '\u{2022}'), None);
assert_eq!(take("Arial", SANS, '\u{20AC}'), None);
let east_asian_slot =
substitutes(("SimSun", Some(classified(ROMAN, true))), '\u{2010}', false);
assert_eq!(east_asian_slot.len(), 0);
}
#[test]
fn a_face_of_its_own_row_takes_its_own_substitute() {
assert_eq!(
take("Times New Roman", ROMAN, '한').as_deref(),
Some("Batang")
);
assert_eq!(take("simsun", ROMAN, '한').as_deref(), Some("Batang"));
let parenthesized = take("Times New Roman", ROMAN, '\u{2474}');
assert_eq!(parenthesized.as_deref(), Some("Yu Gothic"));
assert_eq!(
take("Times New Roman", ROMAN, 'č').as_deref(),
Some("Cambria")
);
}
#[test]
fn east_asian_faces_and_times_new_roman_keep_east_asian_characters() {
assert!(keeps_east_asian("SimSun", Some(classified(ROMAN, true))));
assert!(keeps_east_asian(
"Times New Roman",
Some(classified(ROMAN, false))
));
assert!(!keeps_east_asian("Calibri", Some(classified(SANS, false))));
assert!(!keeps_east_asian("Calibri", None));
}
}