use super::FontSide;
use crate::character_class::CharacterClass;
use crate::props::EastAsianScript;
use crate::styles::EastAsianCharset;
pub(super) fn symbol_stand_in(ch: char) -> Option<char> {
match ch {
'\u{F0B7}' => Some('\u{2022}'),
'\u{F0A7}' => Some('\u{25AA}'),
'\u{F0A8}' => Some('\u{25A1}'),
'\u{F0D8}' => Some('\u{27A2}'),
'\u{F076}' => Some('\u{2756}'),
'\u{F0FC}' => Some('\u{2713}'),
'\u{F06F}' => Some('o'),
'\u{F02D}' => Some('-'),
_ => None,
}
}
pub(super) use f_office_fonts::SymbolFont;
pub(super) fn mirrored(text: &str, right_to_left: bool) -> std::borrow::Cow<'_, str> {
if right_to_left && text.chars().any(|ch| mirror(ch) != ch) {
std::borrow::Cow::Owned(text.chars().map(mirror).collect())
} else {
std::borrow::Cow::Borrowed(text)
}
}
pub(super) fn mirror(ch: char) -> char {
match ch {
'(' => ')',
')' => '(',
'[' => ']',
']' => '[',
'{' => '}',
'}' => '{',
'<' => '>',
'>' => '<',
'\u{AB}' => '\u{BB}',
'\u{BB}' => '\u{AB}',
other => other,
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(super) enum FaceSlot {
Ascii,
Other,
SmallAscii,
SmallOther,
Complex,
EastAsian,
}
pub(super) fn face_slot(
ch: char,
small: bool,
(all_complex, hint): (bool, Option<HintedSet>),
) -> FaceSlot {
if all_complex || complex_script(ch) {
FaceSlot::Complex
} else if takes_east_asian_face(ch, hint) {
FaceSlot::EastAsian
} else {
match (ch.is_ascii(), small) {
(true, false) => FaceSlot::Ascii,
(false, false) => FaceSlot::Other,
(true, true) => FaceSlot::SmallAscii,
(false, true) => FaceSlot::SmallOther,
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(super) enum HintedSet {
Punctuation,
Symbols,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(super) enum HintedLatin {
Open,
ByLanguage(HintLanguage),
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(super) enum HintLanguage {
Chinese,
ChineseSet,
Other,
}
impl HintLanguage {
pub(super) fn of(script: Option<EastAsianScript>, charset: Option<EastAsianCharset>) -> Self {
if matches!(
script,
Some(EastAsianScript::Simplified | EastAsianScript::Traditional)
) {
Self::Chinese
} else if matches!(
charset,
Some(EastAsianCharset::Gb2312 | EastAsianCharset::Big5)
) {
Self::ChineseSet
} else {
Self::Other
}
}
fn side(self, ch: char) -> FontSide {
let east = if first_list(ch) {
true
} else if letter_for_chinese(ch) || ('\u{1E00}'..='\u{1EFF}').contains(&ch) {
self == Self::Chinese
} else if ('\u{0100}'..='\u{02AF}').contains(&ch) {
self != Self::Other
} else {
false
};
if east {
FontSide::EastAsian
} else {
FontSide::Other
}
}
}
const fn first_list(ch: char) -> bool {
matches!(
ch,
'\u{00A1}'
| '\u{00A4}'
| '\u{00A7}'
| '\u{00A8}'
| '\u{00AA}'
| '\u{00AD}'
| '\u{00AF}'
| '\u{00B0}'..='\u{00B4}'
| '\u{00B6}'..='\u{00BA}'
| '\u{00BC}'..='\u{00BF}'
| '\u{00D7}'
| '\u{00F7}'
)
}
const fn letter_for_chinese(ch: char) -> bool {
matches!(
ch,
'\u{00E0}'
| '\u{00E1}'
| '\u{00E8}'
| '\u{00E9}'
| '\u{00EA}'
| '\u{00EC}'
| '\u{00ED}'
| '\u{00F2}'
| '\u{00F3}'
| '\u{00F9}'
| '\u{00FA}'
| '\u{00FC}'
)
}
pub(super) fn font_side(
ch: char,
all_complex: bool,
hint: Option<HintedLatin>,
ascii_hansi_differ: bool,
has_glyph: impl FnOnce(FontSide) -> bool,
) -> FontSide {
if all_complex || complex_script(ch) {
return FontSide::Open;
}
if ch == ' ' {
return if ascii_hansi_differ {
FontSide::Other
} else {
FontSide::Open
};
}
if CharacterClass::EastAsianFont.contains(ch) {
return FontSide::EastAsian;
}
let non_ascii = if ascii_hansi_differ {
FontSide::HighAnsi
} else {
FontSide::Other
};
let Some(hint) = hint else {
return if ch.is_ascii() {
FontSide::Other
} else {
non_ascii
};
};
hinted_font_side(ch, hint, non_ascii, has_glyph)
}
fn hinted_font_side(
ch: char,
hint: HintedLatin,
non_ascii: FontSide,
has_glyph: impl FnOnce(FontSide) -> bool,
) -> FontSide {
if ch.is_ascii() {
FontSide::Other
} else if matches!(ch, '\u{00A7}' | '\u{00B7}') {
FontSide::EastAsian
} else if open_with_hint(ch) {
match hint {
HintedLatin::Open => FontSide::Open,
HintedLatin::ByLanguage(language) => {
let side = match language.side(ch) {
FontSide::Other => non_ascii,
other => other,
};
if has_glyph(side) {
side
} else {
FontSide::Open
}
}
}
} else {
FontSide::EastAsian
}
}
const fn open_with_hint(ch: char) -> bool {
matches!(
ch,
'\u{0080}'..='\u{02AF}' | '\u{1E00}'..='\u{1EFF}' | '\u{1F00}'..='\u{1FFF}'
)
}
pub(super) fn numeric_at(chars: &[char], index: usize) -> bool {
let digit = |at: Option<usize>| {
at.and_then(|at| chars.get(at))
.is_some_and(char::is_ascii_digit)
};
match chars.get(index) {
Some(ch) if ch.is_ascii_digit() => true,
Some('.' | ',' | ':') => digit(index.checked_sub(1)) && digit(index.checked_add(1)),
_ => false,
}
}
pub(super) fn takes_east_asian_face(ch: char, hint: Option<HintedSet>) -> bool {
CharacterClass::EastAsianFont.contains(ch)
|| match hint {
None => false,
Some(HintedSet::Punctuation) => east_asian_shared(ch),
Some(HintedSet::Symbols) => east_asian_shared(ch) || hinted_symbol(ch),
}
}
fn east_asian_shared(ch: char) -> bool {
matches!(ch, '\u{00B7}' | '\u{2010}'..='\u{2027}' | '\u{2030}'..='\u{205E}')
}
fn hinted_symbol(ch: char) -> bool {
matches!(ch, '\u{02B0}'..='\u{02FF}' | '\u{0370}'..='\u{04FF}' | '\u{2070}'..='\u{27BF}')
}
pub(super) fn complex_script(ch: char) -> bool {
matches!(
ch,
'\u{0590}'..='\u{08FF}'
| '\u{0900}'..='\u{0DFF}'
| '\u{0E00}'..='\u{0E7F}'
| '\u{FB1D}'..='\u{FDFF}'
| '\u{FE70}'..='\u{FEFF}'
)
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum InvisibleKind {
Format,
Operator,
Selector,
}
pub(crate) const fn invisible_kind(ch: char) -> Option<InvisibleKind> {
match ch {
'\u{200B}'..='\u{200F}' | '\u{202A}'..='\u{202E}' | '\u{FEFF}' => {
Some(InvisibleKind::Format)
}
'\u{2060}'..='\u{2064}' => Some(InvisibleKind::Operator),
'\u{FE00}'..='\u{FE0F}' => Some(InvisibleKind::Selector),
_ => None,
}
}
pub(super) fn upper(ch: char, caps: bool) -> Vec<char> {
if caps {
ch.to_uppercase().collect()
} else {
vec![ch]
}
}