| 1 |
// This file is part of ICU4X. For terms of use, please see the file |
| 2 |
// called LICENSE at the top level of the ICU4X source tree |
| 3 |
// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ). |
| 4 |
|
| 5 |
use crate::props::{BidiMirroringGlyph, GeneralCategory, Script}; |
| 6 |
use crate::provider::{PropertyEnumScriptV1, PropertyNameShortScriptV1}; |
| 7 |
use crate::{ |
| 8 |
CodePointMapData, CodePointMapDataBorrowed, PropertyNamesShort, PropertyNamesShortBorrowed, |
| 9 |
}; |
| 10 |
use icu_provider::prelude::*; |
| 11 |
|
| 12 |
use harfbuzz_traits::{GeneralCategoryFunc, MirroringFunc, ScriptFunc}; |
| 13 |
|
| 14 |
/// ✨ *Enabled with the `harfbuzz_traits` Cargo feature.* |
| 15 |
impl GeneralCategoryFunc for CodePointMapDataBorrowed<'_, GeneralCategory> { |
| 16 |
fn general_category(&self, ch: char) -> harfbuzz_traits::GeneralCategory { |
| 17 |
self.get(ch).into() |
| 18 |
} |
| 19 |
} |
| 20 |
|
| 21 |
/// ✨ *Enabled with the `harfbuzz_traits` Cargo feature.* |
| 22 |
impl GeneralCategoryFunc for CodePointMapData<GeneralCategory> { |
| 23 |
fn general_category(&self, ch: char) -> harfbuzz_traits::GeneralCategory { |
| 24 |
GeneralCategoryFunc::general_category(&self.as_borrowed(), ch) |
| 25 |
} |
| 26 |
} |
| 27 |
|
| 28 |
/// ✨ *Enabled with the `harfbuzz_traits` Cargo feature.* |
| 29 |
impl GeneralCategoryFunc for &'_ CodePointMapData<GeneralCategory> { |
| 30 |
fn general_category(&self, ch: char) -> harfbuzz_traits::GeneralCategory { |
| 31 |
GeneralCategoryFunc::general_category(&self.as_borrowed(), ch) |
| 32 |
} |
| 33 |
} |
| 34 |
|
| 35 |
/// ✨ *Enabled with the `harfbuzz_traits` Cargo feature.* |
| 36 |
impl MirroringFunc for CodePointMapDataBorrowed<'_, BidiMirroringGlyph> { |
| 37 |
fn mirroring(&self, ch: char) -> char { |
| 38 |
self.get(ch).mirroring_glyph.unwrap_or(ch) |
| 39 |
} |
| 40 |
} |
| 41 |
|
| 42 |
/// ✨ *Enabled with the `harfbuzz_traits` Cargo feature.* |
| 43 |
impl MirroringFunc for CodePointMapData<BidiMirroringGlyph> { |
| 44 |
fn mirroring(&self, ch: char) -> char { |
| 45 |
MirroringFunc::mirroring(&self.as_borrowed(), ch) |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
/// ✨ *Enabled with the `harfbuzz_traits` Cargo feature.* |
| 50 |
impl MirroringFunc for &'_ CodePointMapData<BidiMirroringGlyph> { |
| 51 |
fn mirroring(&self, ch: char) -> char { |
| 52 |
MirroringFunc::mirroring(&self.as_borrowed(), ch) |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
/// ✨ *Enabled with the `harfbuzz_traits` Cargo feature.* |
| 57 |
impl ScriptFunc for HarfbuzzScriptDataBorrowed<'_> { |
| 58 |
fn script(&self, ch: char) -> [u8; 4] { |
| 59 |
let script = self.script.get(ch); |
| 60 |
self.script_names |
| 61 |
.get_locale_script(script) |
| 62 |
.unwrap_or(icu_locale_core::subtags::script!("Zzzz")) |
| 63 |
.into_raw() |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
/// ✨ *Enabled with the `harfbuzz_traits` Cargo feature.* |
| 68 |
impl ScriptFunc for HarfbuzzScriptData { |
| 69 |
fn script(&self, ch: char) -> [u8; 4] { |
| 70 |
ScriptFunc::script(&self.as_borrowed(), ch) |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
/// ✨ *Enabled with the `harfbuzz_traits` Cargo feature.* |
| 75 |
impl ScriptFunc for &'_ HarfbuzzScriptData { |
| 76 |
fn script(&self, ch: char) -> [u8; 4] { |
| 77 |
ScriptFunc::script(&self.as_borrowed(), ch) |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
/// ✨ *Enabled with the `harfbuzz_traits` Cargo feature.* |
| 82 |
impl From<GeneralCategory> for harfbuzz_traits::GeneralCategory { |
| 83 |
fn from(val: GeneralCategory) -> Self { |
| 84 |
use GeneralCategory::*; |
| 85 |
match val { |
| 86 |
Unassigned => harfbuzz_traits::GeneralCategory::Unassigned, |
| 87 |
UppercaseLetter => harfbuzz_traits::GeneralCategory::UppercaseLetter, |
| 88 |
LowercaseLetter => harfbuzz_traits::GeneralCategory::LowercaseLetter, |
| 89 |
TitlecaseLetter => harfbuzz_traits::GeneralCategory::TitlecaseLetter, |
| 90 |
ModifierLetter => harfbuzz_traits::GeneralCategory::ModifierLetter, |
| 91 |
OtherLetter => harfbuzz_traits::GeneralCategory::OtherLetter, |
| 92 |
NonspacingMark => harfbuzz_traits::GeneralCategory::NonSpacingMark, |
| 93 |
SpacingMark => harfbuzz_traits::GeneralCategory::SpacingMark, |
| 94 |
EnclosingMark => harfbuzz_traits::GeneralCategory::EnclosingMark, |
| 95 |
DecimalNumber => harfbuzz_traits::GeneralCategory::DecimalNumber, |
| 96 |
LetterNumber => harfbuzz_traits::GeneralCategory::LetterNumber, |
| 97 |
OtherNumber => harfbuzz_traits::GeneralCategory::OtherNumber, |
| 98 |
SpaceSeparator => harfbuzz_traits::GeneralCategory::SpaceSeparator, |
| 99 |
LineSeparator => harfbuzz_traits::GeneralCategory::LineSeparator, |
| 100 |
ParagraphSeparator => harfbuzz_traits::GeneralCategory::ParagraphSeparator, |
| 101 |
Control => harfbuzz_traits::GeneralCategory::Control, |
| 102 |
Format => harfbuzz_traits::GeneralCategory::Format, |
| 103 |
PrivateUse => harfbuzz_traits::GeneralCategory::PrivateUse, |
| 104 |
Surrogate => harfbuzz_traits::GeneralCategory::Surrogate, |
| 105 |
DashPunctuation => harfbuzz_traits::GeneralCategory::DashPunctuation, |
| 106 |
OpenPunctuation => harfbuzz_traits::GeneralCategory::OpenPunctuation, |
| 107 |
ClosePunctuation => harfbuzz_traits::GeneralCategory::ClosePunctuation, |
| 108 |
ConnectorPunctuation => harfbuzz_traits::GeneralCategory::ConnectPunctuation, |
| 109 |
InitialPunctuation => harfbuzz_traits::GeneralCategory::InitialPunctuation, |
| 110 |
FinalPunctuation => harfbuzz_traits::GeneralCategory::FinalPunctuation, |
| 111 |
OtherPunctuation => harfbuzz_traits::GeneralCategory::OtherPunctuation, |
| 112 |
MathSymbol => harfbuzz_traits::GeneralCategory::MathSymbol, |
| 113 |
CurrencySymbol => harfbuzz_traits::GeneralCategory::CurrencySymbol, |
| 114 |
ModifierSymbol => harfbuzz_traits::GeneralCategory::ModifierSymbol, |
| 115 |
OtherSymbol => harfbuzz_traits::GeneralCategory::OtherSymbol, |
| 116 |
} |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
/// Harfbuzz data for the [`ScriptFunc`] implementation |
| 121 |
/// |
| 122 |
/// ✨ *Enabled with the `harfbuzz_traits` Cargo feature.* |
| 123 |
#[derive(Debug)] |
| 124 |
pub struct HarfbuzzScriptDataBorrowed<'a> { |
| 125 |
script: CodePointMapDataBorrowed<'a, Script>, |
| 126 |
script_names: PropertyNamesShortBorrowed<'a, Script>, |
| 127 |
} |
| 128 |
|
| 129 |
/// Harfbuzz data for the [`ScriptFunc`] implementation |
| 130 |
/// |
| 131 |
/// ✨ *Enabled with the `harfbuzz_traits` Cargo feature.* |
| 132 |
#[derive(Debug)] |
| 133 |
pub struct HarfbuzzScriptData { |
| 134 |
script: CodePointMapData<Script>, |
| 135 |
script_names: PropertyNamesShort<Script>, |
| 136 |
} |
| 137 |
|
| 138 |
impl HarfbuzzScriptData { |
| 139 |
/// Construct a new [`HarfbuzzScriptData`] using compiled data. |
| 140 |
/// |
| 141 |
/// ✨ *Enabled with the `compiled_data` Cargo feature.* |
| 142 |
#[cfg(feature = "compiled_data")] |
| 143 |
#[expect(clippy::new_ret_no_self)] |
| 144 |
pub fn new() -> HarfbuzzScriptDataBorrowed<'static> { |
| 145 |
HarfbuzzScriptDataBorrowed { |
| 146 |
script: CodePointMapData::<Script>::new(), |
| 147 |
script_names: PropertyNamesShort::<Script>::new(), |
| 148 |
} |
| 149 |
} |
| 150 |
|
| 151 |
/// Construct a new [`HarfbuzzScriptData`] from a data provider. |
| 152 |
pub fn try_new_unstable<D>(provider: &D) -> Result<Self, DataError> |
| 153 |
where |
| 154 |
D: DataProvider<PropertyEnumScriptV1> + DataProvider<PropertyNameShortScriptV1> + ?Sized, |
| 155 |
{ |
| 156 |
let script_set = CodePointMapData::<Script>::try_new_unstable(provider)?; |
| 157 |
let script_names = PropertyNamesShort::try_new_unstable(provider)?; |
| 158 |
Ok(Self { |
| 159 |
script: script_set, |
| 160 |
script_names, |
| 161 |
}) |
| 162 |
} |
| 163 |
|
| 164 |
#[cfg(feature = "serde")] |
| 165 |
#[doc = icu_provider::gen_buffer_unstable_docs!(BUFFER,Self::try_new_unstable)] |
| 166 |
pub fn try_new_with_buffer_provider( |
| 167 |
provider: &(impl BufferProvider + ?Sized), |
| 168 |
) -> Result<Self, DataError> { |
| 169 |
Self::try_new_unstable(&provider.as_deserializing()) |
| 170 |
} |
| 171 |
|
| 172 |
fn as_borrowed(&self) -> HarfbuzzScriptDataBorrowed<'_> { |
| 173 |
HarfbuzzScriptDataBorrowed { |
| 174 |
script: self.script.as_borrowed(), |
| 175 |
script_names: self.script_names.as_borrowed(), |
| 176 |
} |
| 177 |
} |
| 178 |
} |