use icu_locale_core::LanguageIdentifier;
use super::*;
static JOINING_TYPES: OnceLock<CodePointMapDataBorrowed<'static, JoiningType>> = OnceLock::new();
static JOIN_CONTROLS: OnceLock<CodePointSetDataBorrowed<'static>> = OnceLock::new();
static BIDI_CONTROLS: OnceLock<CodePointSetDataBorrowed<'static>> = OnceLock::new();
static BIDI_CLASSES: OnceLock<CodePointMapDataBorrowed<'static, BidiClass>> = OnceLock::new();
static BIDI_MIRRORING_GLYPHS: OnceLock<CodePointMapDataBorrowed<'static, BidiMirroringGlyph>> =
OnceLock::new();
static LINE_BREAK_CLASSES: OnceLock<CodePointMapDataBorrowed<'static, LineBreak>> = OnceLock::new();
static EAST_ASIAN_WIDTHS: OnceLock<CodePointMapDataBorrowed<'static, EastAsianWidth>> =
OnceLock::new();
static VERTICAL_ORIENTATIONS: OnceLock<CodePointMapDataBorrowed<'static, VerticalOrientation>> =
OnceLock::new();
static WORD_BREAK_CLASSES: OnceLock<CodePointMapDataBorrowed<'static, IcuWordBreak>> =
OnceLock::new();
static GENERAL_CATEGORIES: OnceLock<CodePointMapDataBorrowed<'static, GeneralCategory>> =
OnceLock::new();
static DEFAULT_IGNORABLE_CODE_POINTS: OnceLock<CodePointSetDataBorrowed<'static>> = OnceLock::new();
static EMOJI_CODE_POINTS: OnceLock<CodePointSetDataBorrowed<'static>> = OnceLock::new();
static EMOJI_PRESENTATION_CODE_POINTS: OnceLock<CodePointSetDataBorrowed<'static>> =
OnceLock::new();
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum ContentWritingSystem {
Unknown,
Chinese,
Japanese,
Korean,
Yi,
Other,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum TextSpacingPunctuationClass {
Opening,
Closing,
MiddleDot,
IdeographicSpace,
NarrowOpening,
NarrowClosing,
}
pub(crate) fn text_spacing_punctuation_class(
character: char,
language: Option<&str>,
vertical: bool,
) -> Option<TextSpacingPunctuationClass> {
if matches!(character, '(' | '[' | '{') {
return Some(TextSpacingPunctuationClass::NarrowOpening);
}
if matches!(character, ')' | ']' | '}') {
return Some(TextSpacingPunctuationClass::NarrowClosing);
}
if character == '\u{3000}' {
return Some(TextSpacingPunctuationClass::IdeographicSpace);
}
if matches!(character, '\u{2018}' | '\u{201c}') {
return Some(TextSpacingPunctuationClass::Opening);
}
if matches!(character, '\u{2019}' | '\u{201d}') {
return Some(TextSpacingPunctuationClass::Closing);
}
if matches!(character, '\u{00b7}' | '\u{2027}' | '\u{30fb}') {
return Some(TextSpacingPunctuationClass::MiddleDot);
}
if matches!(character, '\u{ff1a}' | '\u{ff1b}') {
return Some(colon_or_dot_spacing_class(language, vertical, true));
}
if matches!(character, '\u{3001}' | '\u{3002}' | '\u{ff0c}' | '\u{ff0e}') {
return Some(colon_or_dot_spacing_class(language, vertical, false));
}
let east_asian_width = EAST_ASIAN_WIDTHS
.get_or_init(CodePointMapData::<EastAsianWidth>::new)
.get(character);
let is_fullwidth_cjk_punctuation = matches!(east_asian_width, EastAsianWidth::Fullwidth)
|| ('\u{3000}'..='\u{303f}').contains(&character);
if !is_fullwidth_cjk_punctuation {
return None;
}
match general_category(character) {
GeneralCategory::OpenPunctuation => Some(TextSpacingPunctuationClass::Opening),
GeneralCategory::ClosePunctuation => Some(TextSpacingPunctuationClass::Closing),
_ => None,
}
}
fn colon_or_dot_spacing_class(
language: Option<&str>,
_vertical: bool,
colon: bool,
) -> TextSpacingPunctuationClass {
let language = language.unwrap_or_default().trim().to_ascii_lowercase();
if language.starts_with("zh-hant") || language.starts_with("zh-tw") {
TextSpacingPunctuationClass::MiddleDot
} else if language.starts_with("ja") || language.starts_with("ko") {
if colon {
TextSpacingPunctuationClass::MiddleDot
} else {
TextSpacingPunctuationClass::Closing
}
} else {
TextSpacingPunctuationClass::Closing
}
}
pub(crate) fn content_writing_system(language: Option<&str>) -> ContentWritingSystem {
let Some(language) = language else {
return ContentWritingSystem::Unknown;
};
let language = language.trim();
let normalized = language.contains('_').then(|| language.replace('_', "-"));
let language = normalized.as_deref().unwrap_or(language);
let Ok(identifier) = language.parse::<LanguageIdentifier>() else {
return ContentWritingSystem::Unknown;
};
if let Some(script) = identifier.script {
return match script.as_str() {
"Hant" | "Hans" | "Hani" | "Hanb" | "Bopo" => ContentWritingSystem::Chinese,
"Jpan" | "Hrkt" | "Hira" | "Kana" => ContentWritingSystem::Japanese,
"Kore" | "Hang" | "Jamo" => ContentWritingSystem::Korean,
"Yiii" => ContentWritingSystem::Yi,
"Zzzz" => ContentWritingSystem::Unknown,
_ => ContentWritingSystem::Other,
};
}
match identifier.language.as_str() {
"zh" => ContentWritingSystem::Chinese,
"ja" => ContentWritingSystem::Japanese,
"ko" => ContentWritingSystem::Korean,
"ii" => ContentWritingSystem::Yi,
"und" => ContentWritingSystem::Unknown,
_ => ContentWritingSystem::Other,
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum CursiveScript {
Arabic,
HanifiRohingya,
Mandaic,
Mongolian,
Nko,
PhagsPa,
Syriac,
}
fn cursive_script(character: char) -> Option<CursiveScript> {
if !character_is_unicode_letter(character) {
return None;
}
let scripts = ScriptWithExtensions::new();
[
(IcuScript::Arabic, CursiveScript::Arabic),
(IcuScript::HanifiRohingya, CursiveScript::HanifiRohingya),
(IcuScript::Mandaic, CursiveScript::Mandaic),
(IcuScript::Mongolian, CursiveScript::Mongolian),
(IcuScript::Nko, CursiveScript::Nko),
(IcuScript::PhagsPa, CursiveScript::PhagsPa),
(IcuScript::Syriac, CursiveScript::Syriac),
]
.into_iter()
.find_map(|(script, cursive)| scripts.has_script(character, script).then_some(cursive))
}
fn character_has_unicode_joining_behavior(character: char) -> bool {
matches!(
joining_type(character),
JoiningType::JoinCausing
| JoiningType::DualJoining
| JoiningType::LeftJoining
| JoiningType::RightJoining
)
}
pub(crate) fn character_has_cursive_shaping_behavior(character: char) -> bool {
character_has_unicode_joining_behavior(character) || cursive_script(character).is_some()
}
pub(crate) fn cursive_boundary_needs_context(left: char, right: char) -> bool {
match (cursive_script(left), cursive_script(right)) {
(Some(CursiveScript::Mongolian), Some(CursiveScript::Mongolian))
| (Some(CursiveScript::PhagsPa), Some(CursiveScript::PhagsPa)) => true,
(Some(left_script), Some(right_script)) if left_script == right_script => {
character_can_join_following(left) && character_can_join_preceding(right)
}
_ => false,
}
}
pub(super) fn character_can_join_following(character: char) -> bool {
matches!(
joining_type(character),
JoiningType::JoinCausing | JoiningType::DualJoining | JoiningType::LeftJoining
)
}
pub(super) fn character_can_join_preceding(character: char) -> bool {
matches!(
joining_type(character),
JoiningType::JoinCausing | JoiningType::DualJoining | JoiningType::RightJoining
)
}
pub(crate) fn character_is_join_control(character: char) -> bool {
JOIN_CONTROLS
.get_or_init(CodePointSetData::new::<JoinControl>)
.contains(character)
}
pub(crate) fn character_is_arabic_tatweel(character: char) -> bool {
character == '\u{0640}'
}
pub(super) fn line_break_class(character: char) -> LineBreak {
LINE_BREAK_CLASSES
.get_or_init(CodePointMapData::<LineBreak>::new)
.get(character)
}
pub(crate) fn character_is_mandatory_line_break(character: char) -> bool {
matches!(
line_break_class(character),
LineBreak::MandatoryBreak | LineBreak::NextLine
)
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum Uax14BoundaryProtection {
None,
Glue,
WordJoiner,
ZeroWidthJoiner,
}
pub(crate) fn uax14_atomic_boundary_protection(character: char) -> Uax14BoundaryProtection {
if character == '\u{00a0}' {
return Uax14BoundaryProtection::None;
}
match line_break_class(character) {
LineBreak::Glue => Uax14BoundaryProtection::Glue,
LineBreak::WordJoiner => Uax14BoundaryProtection::WordJoiner,
LineBreak::ZWJ => Uax14BoundaryProtection::ZeroWidthJoiner,
_ => Uax14BoundaryProtection::None,
}
}
#[derive(Clone, Copy, Debug)]
pub(crate) struct SegmentBreakContext<'a> {
pub(crate) before: char,
pub(crate) after: char,
pub(crate) before_is_currency_amount: bool,
pub(crate) language: Option<&'a str>,
}
pub(crate) fn segment_break_is_removable(context: SegmentBreakContext<'_>) -> bool {
let SegmentBreakContext {
before,
after,
before_is_currency_amount,
language,
} = context;
if before == '\u{200b}' || after == '\u{200b}' {
return true;
}
if before_is_currency_amount
|| character_is_currency_symbol(before)
|| character_is_currency_symbol(after)
|| character_is_hangul_for_segment_break(before)
|| character_is_hangul_for_segment_break(after)
{
return false;
}
if writing_system_omits_word_separators(language) {
return true;
}
east_asian_segment_break_width(before) && east_asian_segment_break_width(after)
}
fn writing_system_omits_word_separators(language: Option<&str>) -> bool {
matches!(
content_writing_system(language),
ContentWritingSystem::Chinese | ContentWritingSystem::Japanese | ContentWritingSystem::Yi
)
}
pub(crate) fn character_is_currency_symbol(character: char) -> bool {
matches!(general_category(character), GeneralCategory::CurrencySymbol)
}
fn east_asian_segment_break_width(character: char) -> bool {
matches!(
EAST_ASIAN_WIDTHS
.get_or_init(CodePointMapData::<EastAsianWidth>::new)
.get(character),
EastAsianWidth::Fullwidth | EastAsianWidth::Wide | EastAsianWidth::Halfwidth
)
}
fn character_is_hangul_for_segment_break(character: char) -> bool {
matches!(
line_break_class(character),
LineBreak::H2 | LineBreak::H3 | LineBreak::JL | LineBreak::JV | LineBreak::JT
)
}
pub(crate) fn character_vertical_orientation(character: char) -> VerticalOrientation {
VERTICAL_ORIENTATIONS
.get_or_init(CodePointMapData::<VerticalOrientation>::new)
.get(character)
}
pub(crate) fn character_is_native_vertical_script(character: char) -> bool {
let script = CodePointMapData::<IcuScript>::new().get(character);
PropertyNamesShort::<IcuScript>::new()
.get_locale_script(script)
.is_some_and(|script| script.into_raw() == *b"Mong" || script.into_raw() == *b"Phag")
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum MixedTextOrientation {
Upright,
Sideways,
}
pub(crate) fn typographic_unit_mixed_orientation(text: &str) -> MixedTextOrientation {
if text
.chars()
.find(|character| !character_inherits_vertical_orientation(*character))
.is_some_and(|character| {
matches!(
character_vertical_orientation(character),
VerticalOrientation::Upright
| VerticalOrientation::TransformedUpright
| VerticalOrientation::TransformedRotated
)
})
{
MixedTextOrientation::Upright
} else {
MixedTextOrientation::Sideways
}
}
pub(crate) fn typographic_unit_is_upright_in_mixed_orientation(text: &str) -> bool {
typographic_unit_mixed_orientation(text) == MixedTextOrientation::Upright
}
pub(crate) fn character_inherits_vertical_orientation(character: char) -> bool {
character_is_default_ignorable_code_point(character)
|| GeneralCategoryGroup::Mark.contains(general_category(character))
}
pub(crate) fn character_is_css_other_space_separator(character: char) -> bool {
character != '\u{00a0}'
&& !is_css_collapsible_whitespace(character)
&& matches!(
GENERAL_CATEGORIES
.get_or_init(CodePointMapData::<GeneralCategory>::new)
.get(character),
GeneralCategory::SpaceSeparator
)
}
pub(crate) fn character_is_css_word_separator(character: char) -> bool {
character == '\u{00a0}'
|| is_css_preserved_document_space(character)
|| character_is_css_other_space_separator(character)
|| matches!(
character,
'\u{1361}' | '\u{10100}' | '\u{10101}' | '\u{1039f}' | '\u{1091f}' )
}
pub(crate) fn character_is_text_decoration_spacer(character: char) -> bool {
if character == '\u{202f}' {
return false;
}
let category = general_category(character);
GeneralCategoryGroup::Separator.contains(category)
&& matches!(category, GeneralCategory::SpaceSeparator)
}
pub(crate) fn character_receives_text_emphasis_mark(character: char) -> bool {
let category = general_category(character);
!GeneralCategoryGroup::Separator
.union(GeneralCategoryGroup::Punctuation)
.union(GeneralCategoryGroup::Control)
.union(GeneralCategoryGroup::Format)
.union(GeneralCategoryGroup::Unassigned)
.contains(category)
}
pub(crate) fn character_is_last_hangable_punctuation(character: char) -> bool {
if matches!(character, '"' | '\'') {
return true;
}
matches!(
GeneralCategoryGroup::from(general_category(character)),
GeneralCategoryGroup::ClosePunctuation
| GeneralCategoryGroup::FinalPunctuation
| GeneralCategoryGroup::InitialPunctuation
)
}
pub(crate) fn character_is_first_hangable_punctuation(character: char) -> bool {
if matches!(character, '"' | '\'' | '\u{3000}') {
return true;
}
matches!(
GeneralCategoryGroup::from(general_category(character)),
GeneralCategoryGroup::OpenPunctuation
| GeneralCategoryGroup::FinalPunctuation
| GeneralCategoryGroup::InitialPunctuation
)
}
pub(crate) fn character_is_hangable_stop_or_comma(character: char) -> bool {
matches!(
character,
',' | '.'
| '\u{060c}'
| '\u{06d4}'
| '\u{3001}'
| '\u{3002}'
| '\u{ff0c}'
| '\u{ff0e}'
| '\u{fe50}'
| '\u{fe51}'
| '\u{fe52}'
| '\u{ff61}'
| '\u{ff64}'
)
}
pub(crate) fn character_is_unicode_letter(character: char) -> bool {
GeneralCategoryGroup::Letter.contains(general_category(character))
}
pub(crate) fn character_is_unicode_typographic_letter(character: char) -> bool {
character_is_unicode_letter(character)
|| matches!(general_category(character), GeneralCategory::LetterNumber)
}
pub(crate) fn character_is_unicode_alphanumeric(character: char) -> bool {
let category = general_category(character);
GeneralCategoryGroup::Letter.contains(category)
|| GeneralCategoryGroup::Number.contains(category)
}
pub(crate) fn character_is_unicode_punctuation(character: char) -> bool {
GeneralCategoryGroup::Punctuation.contains(general_category(character))
}
pub(crate) fn character_is_unicode_first_letter_base(character: char) -> bool {
character_is_unicode_alphanumeric(character) || character_is_unicode_symbol(character)
}
pub(crate) fn character_is_first_letter_associated_space(character: char) -> bool {
character != '\u{3000}'
&& matches!(general_category(character), GeneralCategory::SpaceSeparator)
}
pub(crate) fn character_is_first_letter_suffix_punctuation(character: char) -> bool {
matches!(
general_category(character),
GeneralCategory::ClosePunctuation
| GeneralCategory::ConnectorPunctuation
| GeneralCategory::FinalPunctuation
| GeneralCategory::InitialPunctuation
| GeneralCategory::OtherPunctuation
)
}
pub(crate) fn character_is_unicode_symbol(character: char) -> bool {
GeneralCategoryGroup::Symbol.contains(general_category(character))
}
pub(crate) fn character_is_unicode_mark(character: char) -> bool {
GeneralCategoryGroup::Mark.contains(general_category(character))
}
pub(crate) fn character_is_autospace_ideograph(character: char) -> bool {
!GeneralCategoryGroup::Punctuation.contains(general_category(character))
&& (('\u{3041}'..='\u{30ff}').contains(&character)
|| ('\u{31c0}'..='\u{31ef}').contains(&character)
|| ('\u{31f0}'..='\u{31ff}').contains(&character)
|| ScriptWithExtensions::new().has_script(character, IcuScript::Han))
}
pub(crate) fn character_is_ruby_justification_eligible(character: char) -> bool {
matches!(
EAST_ASIAN_WIDTHS
.get_or_init(CodePointMapData::<EastAsianWidth>::new)
.get(character),
EastAsianWidth::Fullwidth | EastAsianWidth::Wide
)
}
pub(crate) fn character_is_autospace_alpha(character: char) -> bool {
!character_is_autospace_ideograph(character)
&& (GeneralCategoryGroup::Letter.contains(general_category(character))
|| GeneralCategoryGroup::Mark.contains(general_category(character)))
&& !matches!(
EAST_ASIAN_WIDTHS
.get_or_init(CodePointMapData::<EastAsianWidth>::new)
.get(character),
EastAsianWidth::Wide | EastAsianWidth::Fullwidth
)
}
pub(crate) fn character_is_autospace_numeric(character: char) -> bool {
matches!(general_category(character), GeneralCategory::DecimalNumber)
&& !matches!(
EAST_ASIAN_WIDTHS
.get_or_init(CodePointMapData::<EastAsianWidth>::new)
.get(character),
EastAsianWidth::Fullwidth
)
}
pub(crate) fn character_preserves_word_boundary_context(character: char) -> bool {
matches!(
WORD_BREAK_CLASSES
.get_or_init(CodePointMapData::<IcuWordBreak>::new)
.get(character),
IcuWordBreak::MidLetter
| IcuWordBreak::MidNum
| IcuWordBreak::MidNumLet
| IcuWordBreak::SingleQuote
| IcuWordBreak::DoubleQuote
| IcuWordBreak::ExtendNumLet
)
}
pub(super) fn character_has_rtl_bidi_class(character: char) -> bool {
matches!(
BIDI_CLASSES
.get_or_init(CodePointMapData::<BidiClass>::new)
.get(character),
BidiClass::RightToLeft | BidiClass::ArabicLetter
)
}
pub(crate) fn bidi_mirroring_glyph(character: char) -> Option<char> {
BIDI_MIRRORING_GLYPHS
.get_or_init(CodePointMapData::<BidiMirroringGlyph>::new)
.get(character)
.mirroring_glyph
}
pub(crate) fn character_is_bidi_format_control(character: char) -> bool {
!character_is_join_control(character)
&& BIDI_CONTROLS
.get_or_init(CodePointSetData::new::<BidiControl>)
.contains(character)
}
pub(crate) fn character_is_unicode_control(character: char) -> bool {
matches!(general_category(character), GeneralCategory::Control)
}
pub(crate) fn character_is_default_ignorable_code_point(character: char) -> bool {
DEFAULT_IGNORABLE_CODE_POINTS
.get_or_init(CodePointSetData::new::<DefaultIgnorableCodePoint>)
.contains(character)
}
pub(crate) fn character_is_emoji(character: char) -> bool {
EMOJI_CODE_POINTS
.get_or_init(CodePointSetData::new::<Emoji>)
.contains(character)
}
pub(crate) fn character_has_emoji_presentation(character: char) -> bool {
EMOJI_PRESENTATION_CODE_POINTS
.get_or_init(CodePointSetData::new::<EmojiPresentation>)
.contains(character)
}
pub(crate) fn character_is_font_neutral_default_ignorable(character: char) -> bool {
character_is_default_ignorable_code_point(character)
&& !character_is_join_control(character)
&& !character_is_bidi_format_control(character)
&& !character_is_variation_selector(character)
}
fn character_is_variation_selector(character: char) -> bool {
matches!(character, '\u{fe00}'..='\u{fe0f}' | '\u{e0100}'..='\u{e01ef}')
}
pub(crate) fn plaintext_direction_for_text(text: &str) -> Option<Direction> {
text.chars().find_map(|character| {
match BIDI_CLASSES
.get_or_init(CodePointMapData::<BidiClass>::new)
.get(character)
{
BidiClass::LeftToRight => Some(Direction::Ltr),
BidiClass::RightToLeft | BidiClass::ArabicLetter => Some(Direction::Rtl),
_ => None,
}
})
}
fn joining_type(character: char) -> JoiningType {
JOINING_TYPES
.get_or_init(CodePointMapData::<JoiningType>::new)
.get(character)
}
fn general_category(character: char) -> GeneralCategory {
GENERAL_CATEGORIES
.get_or_init(CodePointMapData::<GeneralCategory>::new)
.get(character)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn autospace_classes_follow_css_text_character_definitions() {
assert!(character_is_autospace_ideograph('あ'));
assert!(character_is_autospace_ideograph('\u{31c0}'));
assert!(character_is_autospace_ideograph('\u{31f0}'));
assert!(character_is_autospace_ideograph('\u{20000}'));
assert!(!character_is_autospace_ideograph('。'));
assert!(character_is_autospace_alpha('A'));
assert!(character_is_autospace_alpha('\u{0301}'));
assert!(!character_is_autospace_alpha('A'));
assert!(!character_is_autospace_alpha('中'));
assert!(character_is_autospace_numeric('3'));
assert!(!character_is_autospace_numeric('3'));
assert!(!character_is_autospace_numeric('Ⅳ'));
}
#[test]
fn unicode_vertical_orientation_helper_returns_expected_classes() {
assert_eq!(
character_vertical_orientation('a'),
VerticalOrientation::Rotated
);
assert_eq!(
character_vertical_orientation('§'),
VerticalOrientation::Upright
);
assert_eq!(
character_vertical_orientation('、'),
VerticalOrientation::TransformedUpright
);
assert_eq!(
character_vertical_orientation('中'),
VerticalOrientation::Upright
);
assert_eq!(
character_vertical_orientation('!'),
VerticalOrientation::TransformedUpright
);
assert_eq!(
character_vertical_orientation('\u{2329}'),
VerticalOrientation::TransformedRotated
);
}
#[test]
fn native_vertical_script_classification_identifies_mongolian_and_phags_pa() {
assert!(character_is_native_vertical_script('\u{1828}'));
assert!(character_is_native_vertical_script('\u{a840}'));
assert!(!typographic_unit_is_upright_in_mixed_orientation(
"\u{1828}"
));
assert!(!character_is_native_vertical_script('a'));
}
#[test]
fn mixed_orientation_policy_uses_visible_base_character() {
assert_eq!(
typographic_unit_mixed_orientation("a"),
MixedTextOrientation::Sideways,
"Unicode Vertical_Orientation=R remains sideways"
);
assert_eq!(
typographic_unit_mixed_orientation("§"),
MixedTextOrientation::Upright,
"Unicode Vertical_Orientation=U is upright"
);
assert_eq!(
typographic_unit_mixed_orientation("、"),
MixedTextOrientation::Upright,
"Unicode Vertical_Orientation=Tu is upright"
);
assert_eq!(
typographic_unit_mixed_orientation("\u{2329}"),
MixedTextOrientation::Upright,
"Unicode Vertical_Orientation=Tr is upright"
);
assert!(!typographic_unit_is_upright_in_mixed_orientation(
"\u{0301}"
));
assert!(!typographic_unit_is_upright_in_mixed_orientation(
"\u{0301}a"
));
assert!(typographic_unit_is_upright_in_mixed_orientation(
"\u{200d}中"
));
assert_eq!(
typographic_unit_mixed_orientation("\u{0301}\u{2329}"),
MixedTextOrientation::Upright
);
assert_eq!(
typographic_unit_mixed_orientation("a\u{0301}"),
MixedTextOrientation::Sideways,
"a combining mark inherits the preceding base typographic unit"
);
}
#[test]
fn segment_break_keeps_currency_symbols_separate_from_cjk_text() {
assert!(east_asian_segment_break_width('₩'));
assert!(!segment_break_is_removable(SegmentBreakContext {
before: '価',
after: '₩',
before_is_currency_amount: false,
language: Some("ja"),
}));
assert!(!segment_break_is_removable(SegmentBreakContext {
before: '₩',
after: '格',
before_is_currency_amount: false,
language: Some("ja"),
}));
assert!(!segment_break_is_removable(SegmentBreakContext {
before: '0',
after: '格',
before_is_currency_amount: true,
language: Some("ja"),
}));
}
#[test]
fn segment_break_uses_declared_no_word_separator_writing_systems() {
assert!(segment_break_is_removable(SegmentBreakContext {
before: 'E',
after: '~',
before_is_currency_amount: false,
language: Some("ja"),
}));
assert!(segment_break_is_removable(SegmentBreakContext {
before: '“',
after: 'ア',
before_is_currency_amount: false,
language: Some("ain-Kana"),
}));
assert!(!segment_break_is_removable(SegmentBreakContext {
before: 'E',
after: '~',
before_is_currency_amount: false,
language: Some("ja-Hang"),
}));
assert!(segment_break_is_removable(SegmentBreakContext {
before: 'E',
after: '~',
before_is_currency_amount: false,
language: Some("ko-Hani"),
}));
assert!(!segment_break_is_removable(SegmentBreakContext {
before: 'E',
after: '~',
before_is_currency_amount: false,
language: Some("en"),
}));
}
#[test]
fn explicit_script_subtags_override_the_language_writing_system() {
assert_eq!(
content_writing_system(Some("ja")),
ContentWritingSystem::Japanese
);
assert_eq!(
content_writing_system(Some("ja-Hang")),
ContentWritingSystem::Korean
);
assert_eq!(
content_writing_system(Some("en-Hrkt")),
ContentWritingSystem::Japanese
);
assert_eq!(
content_writing_system(Some("ko-Hani")),
ContentWritingSystem::Chinese
);
assert_eq!(
content_writing_system(Some("ja-Latn")),
ContentWritingSystem::Other
);
assert_eq!(
content_writing_system(Some("ja_Hang")),
ContentWritingSystem::Korean
);
assert_eq!(
content_writing_system(Some("en_Hrkt")),
ContentWritingSystem::Japanese
);
assert_eq!(
content_writing_system(Some("ko_Hani")),
ContentWritingSystem::Chinese
);
assert_eq!(
content_writing_system(Some(" ja-Hang ")),
ContentWritingSystem::Korean
);
}
#[test]
fn no_break_space_is_a_word_separator_but_not_a_hanging_other_separator() {
assert!(!character_is_css_other_space_separator('\u{00a0}'));
assert!(character_is_css_word_separator('\u{00a0}'));
assert!(character_is_css_other_space_separator('\u{3000}'));
}
#[test]
fn atomic_boundary_protection_keeps_narrow_no_break_space_glue() {
assert_eq!(
uax14_atomic_boundary_protection('\u{00a0}'),
Uax14BoundaryProtection::None,
"CSS Text's atomic-inline compatibility override retains NBSP"
);
assert_eq!(
uax14_atomic_boundary_protection('\u{202f}'),
Uax14BoundaryProtection::Glue
);
assert_eq!(
uax14_atomic_boundary_protection('\u{2060}'),
Uax14BoundaryProtection::WordJoiner
);
assert_eq!(
uax14_atomic_boundary_protection('\u{200d}'),
Uax14BoundaryProtection::ZeroWidthJoiner
);
}
#[test]
fn text_spacing_punctuation_classes_follow_cjk_language_conventions() {
assert_eq!(
text_spacing_punctuation_class('(', Some("ja"), false),
Some(TextSpacingPunctuationClass::Opening)
);
assert_eq!(
text_spacing_punctuation_class('(', Some("ja"), false),
Some(TextSpacingPunctuationClass::NarrowOpening)
);
assert_eq!(
text_spacing_punctuation_class(')', Some("ja"), false),
Some(TextSpacingPunctuationClass::Closing)
);
assert_eq!(
text_spacing_punctuation_class(' ', Some("ja"), false),
Some(TextSpacingPunctuationClass::IdeographicSpace)
);
assert_eq!(
text_spacing_punctuation_class(':', Some("ja"), false),
Some(TextSpacingPunctuationClass::MiddleDot)
);
assert_eq!(
text_spacing_punctuation_class(':', Some("zh-hans"), false),
Some(TextSpacingPunctuationClass::Closing)
);
assert_eq!(
text_spacing_punctuation_class('。', Some("zh-hant"), true),
Some(TextSpacingPunctuationClass::MiddleDot)
);
}
#[test]
fn emoji_presentation_distinguishes_emoji_and_text_default_scalars() {
assert!(character_has_emoji_presentation('\u{1fae8}'));
assert!(!character_has_emoji_presentation('\u{2139}'));
}
#[test]
fn css_text_cursive_scripts_use_script_extensions_for_letters_only() {
for (character, expected_script) in [
('ع', CursiveScript::Arabic),
('\u{10d00}', CursiveScript::HanifiRohingya),
('\u{0840}', CursiveScript::Mandaic),
('\u{1828}', CursiveScript::Mongolian),
('\u{07de}', CursiveScript::Nko),
('\u{a840}', CursiveScript::PhagsPa),
('\u{0710}', CursiveScript::Syriac),
] {
assert_eq!(cursive_script(character), Some(expected_script));
}
for character in ['\u{1810}', '\u{1801}', '\u{a874}'] {
assert_eq!(cursive_script(character), None);
}
}
}