| 1 |
/* This Source Code Form is subject to the terms of the Mozilla Public |
| 2 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 |
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 4 |
#ifndef intl_components_UnicodeProperties_h_ |
| 5 |
#define intl_components_UnicodeProperties_h_ |
| 6 |
|
| 7 |
#include "mozilla/intl/BidiClass.h" |
| 8 |
#include "mozilla/intl/GeneralCategory.h" |
| 9 |
#include "mozilla/intl/ICU4CGlue.h" |
| 10 |
#include "mozilla/intl/UnicodeScriptCodes.h" |
| 11 |
#include "mozilla/Vector.h" |
| 12 |
|
| 13 |
#include "unicode/uchar.h" |
| 14 |
#include "unicode/uscript.h" |
| 15 |
|
| 16 |
namespace mozilla::intl { |
| 17 |
|
| 18 |
/** |
| 19 |
* This component is a Mozilla-focused API for working with text properties. |
| 20 |
*/ |
| 21 |
class UnicodeProperties final { |
| 22 |
public: |
| 23 |
/** |
| 24 |
* Return the BidiClass for the character. |
| 25 |
*/ |
| 26 |
static inline BidiClass GetBidiClass(uint32_t aCh) { |
| 27 |
return BidiClass(u_charDirection(aCh)); |
| 28 |
} |
| 29 |
|
| 30 |
/** |
| 31 |
* Maps the specified character to a "mirror-image" character. |
| 32 |
*/ |
| 33 |
static inline uint32_t CharMirror(uint32_t aCh) { return u_charMirror(aCh); } |
| 34 |
|
| 35 |
/** |
| 36 |
* Return the general category value for the code point. |
| 37 |
*/ |
| 38 |
static inline GeneralCategory CharType(uint32_t aCh) { |
| 39 |
return GeneralCategory(u_charType(aCh)); |
| 40 |
} |
| 41 |
|
| 42 |
/** |
| 43 |
* Determine whether the code point has the Bidi_Mirrored property. |
| 44 |
*/ |
| 45 |
static inline bool IsMirrored(uint32_t aCh) { return u_isMirrored(aCh); } |
| 46 |
|
| 47 |
/** |
| 48 |
* Returns the combining class of the code point as specified in |
| 49 |
* UnicodeData.txt. |
| 50 |
*/ |
| 51 |
static inline uint8_t GetCombiningClass(uint32_t aCh) { |
| 52 |
return u_getCombiningClass(aCh); |
| 53 |
} |
| 54 |
|
| 55 |
enum class IntProperty { |
| 56 |
BidiPairedBracketType, |
| 57 |
EastAsianWidth, |
| 58 |
HangulSyllableType, |
| 59 |
LineBreak, |
| 60 |
NumericType, |
| 61 |
VerticalOrientation, |
| 62 |
}; |
| 63 |
|
| 64 |
/** |
| 65 |
* Get the property value for an enumerated or integer Unicode property for a |
| 66 |
* code point. |
| 67 |
*/ |
| 68 |
static inline int32_t GetIntPropertyValue(uint32_t aCh, IntProperty aProp) { |
| 69 |
UProperty prop; |
| 70 |
switch (aProp) { |
| 71 |
case IntProperty::BidiPairedBracketType: |
| 72 |
prop = UCHAR_BIDI_PAIRED_BRACKET_TYPE; |
| 73 |
break; |
| 74 |
case IntProperty::EastAsianWidth: |
| 75 |
prop = UCHAR_EAST_ASIAN_WIDTH; |
| 76 |
break; |
| 77 |
case IntProperty::HangulSyllableType: |
| 78 |
prop = UCHAR_HANGUL_SYLLABLE_TYPE; |
| 79 |
break; |
| 80 |
case IntProperty::LineBreak: |
| 81 |
prop = UCHAR_LINE_BREAK; |
| 82 |
break; |
| 83 |
case IntProperty::NumericType: |
| 84 |
prop = UCHAR_NUMERIC_TYPE; |
| 85 |
break; |
| 86 |
case IntProperty::VerticalOrientation: |
| 87 |
prop = UCHAR_VERTICAL_ORIENTATION; |
| 88 |
break; |
| 89 |
} |
| 90 |
return u_getIntPropertyValue(aCh, prop); |
| 91 |
} |
| 92 |
|
| 93 |
/** |
| 94 |
* Get the numeric value for a Unicode code point as defined in the |
| 95 |
* Unicode Character Database if the input is decimal or a digit, |
| 96 |
* otherwise, returns -1. |
| 97 |
*/ |
| 98 |
static inline int8_t GetNumericValue(uint32_t aCh) { |
| 99 |
UNumericType type = |
| 100 |
UNumericType(GetIntPropertyValue(aCh, IntProperty::NumericType)); |
| 101 |
return type == U_NT_DECIMAL || type == U_NT_DIGIT |
| 102 |
? int8_t(u_getNumericValue(aCh)) |
| 103 |
: -1; |
| 104 |
} |
| 105 |
|
| 106 |
/** |
| 107 |
* Maps the specified character to its paired bracket character. |
| 108 |
*/ |
| 109 |
static inline uint32_t GetBidiPairedBracket(uint32_t aCh) { |
| 110 |
return u_getBidiPairedBracket(aCh); |
| 111 |
} |
| 112 |
|
| 113 |
/** |
| 114 |
* The given character is mapped to its uppercase equivalent according to |
| 115 |
* UnicodeData.txt; if the character has no uppercase equivalent, the |
| 116 |
* character itself is returned. |
| 117 |
*/ |
| 118 |
static inline uint32_t ToUpper(uint32_t aCh) { return u_toupper(aCh); } |
| 119 |
|
| 120 |
/** |
| 121 |
* The given character is mapped to its lowercase equivalent according to |
| 122 |
* UnicodeData.txt; if the character has no lowercase equivalent, the |
| 123 |
* character itself is returned. |
| 124 |
*/ |
| 125 |
static inline uint32_t ToLower(uint32_t aCh) { return u_tolower(aCh); } |
| 126 |
|
| 127 |
/** |
| 128 |
* Check if a code point has the Lowercase Unicode property. |
| 129 |
*/ |
| 130 |
static inline bool IsLowercase(uint32_t aCh) { return u_isULowercase(aCh); } |
| 131 |
|
| 132 |
/** |
| 133 |
* The given character is mapped to its titlecase equivalent according to |
| 134 |
* UnicodeData.txt; if the character has no titlecase equivalent, the |
| 135 |
* character itself is returned. |
| 136 |
*/ |
| 137 |
static inline uint32_t ToTitle(uint32_t aCh) { return u_totitle(aCh); } |
| 138 |
|
| 139 |
/** |
| 140 |
* The given character is mapped to its case folding equivalent according to |
| 141 |
* UnicodeData.txt and CaseFolding.txt; |
| 142 |
* if the character has no case folding equivalent, the character |
| 143 |
* itself is returned. |
| 144 |
*/ |
| 145 |
static inline uint32_t FoldCase(uint32_t aCh) { |
| 146 |
return u_foldCase(aCh, U_FOLD_CASE_DEFAULT); |
| 147 |
} |
| 148 |
|
| 149 |
enum class BinaryProperty { |
| 150 |
DefaultIgnorableCodePoint, |
| 151 |
Emoji, |
| 152 |
EmojiPresentation, |
| 153 |
}; |
| 154 |
|
| 155 |
/** |
| 156 |
* Check a binary Unicode property for a code point. |
| 157 |
*/ |
| 158 |
static inline bool HasBinaryProperty(uint32_t aCh, BinaryProperty aProp) { |
| 159 |
UProperty prop; |
| 160 |
switch (aProp) { |
| 161 |
case BinaryProperty::DefaultIgnorableCodePoint: |
| 162 |
prop = UCHAR_DEFAULT_IGNORABLE_CODE_POINT; |
| 163 |
break; |
| 164 |
case BinaryProperty::Emoji: |
| 165 |
prop = UCHAR_EMOJI; |
| 166 |
break; |
| 167 |
case BinaryProperty::EmojiPresentation: |
| 168 |
prop = UCHAR_EMOJI_PRESENTATION; |
| 169 |
break; |
| 170 |
} |
| 171 |
return u_hasBinaryProperty(aCh, prop); |
| 172 |
} |
| 173 |
|
| 174 |
/** |
| 175 |
* Check if the width of aCh is full width, half width or wide. |
| 176 |
*/ |
| 177 |
static inline bool IsEastAsianWidthFHW(uint32_t aCh) { |
| 178 |
switch (GetIntPropertyValue(aCh, IntProperty::EastAsianWidth)) { |
| 179 |
case U_EA_FULLWIDTH: |
| 180 |
case U_EA_HALFWIDTH: |
| 181 |
case U_EA_WIDE: |
| 182 |
return true; |
| 183 |
case U_EA_AMBIGUOUS: |
| 184 |
case U_EA_NARROW: |
| 185 |
case U_EA_NEUTRAL: |
| 186 |
return false; |
| 187 |
} |
| 188 |
return false; |
| 189 |
} |
| 190 |
|
| 191 |
/** |
| 192 |
* Check if the width of aCh is full width, half width or wide |
| 193 |
* excluding emoji. |
| 194 |
*/ |
| 195 |
static inline bool IsEastAsianWidthFHWexcludingEmoji(uint32_t aCh) { |
| 196 |
switch (GetIntPropertyValue(aCh, IntProperty::EastAsianWidth)) { |
| 197 |
case U_EA_FULLWIDTH: |
| 198 |
case U_EA_HALFWIDTH: |
| 199 |
return true; |
| 200 |
case U_EA_WIDE: |
| 201 |
return HasBinaryProperty(aCh, BinaryProperty::Emoji) ? false : true; |
| 202 |
case U_EA_AMBIGUOUS: |
| 203 |
case U_EA_NARROW: |
| 204 |
case U_EA_NEUTRAL: |
| 205 |
return false; |
| 206 |
} |
| 207 |
return false; |
| 208 |
} |
| 209 |
|
| 210 |
/** |
| 211 |
* Check if the width of aCh is ambiguous, full width, or wide. |
| 212 |
*/ |
| 213 |
static inline bool IsEastAsianWidthAFW(uint32_t aCh) { |
| 214 |
switch (GetIntPropertyValue(aCh, IntProperty::EastAsianWidth)) { |
| 215 |
case U_EA_AMBIGUOUS: |
| 216 |
case U_EA_FULLWIDTH: |
| 217 |
case U_EA_WIDE: |
| 218 |
return true; |
| 219 |
case U_EA_HALFWIDTH: |
| 220 |
case U_EA_NARROW: |
| 221 |
case U_EA_NEUTRAL: |
| 222 |
return false; |
| 223 |
} |
| 224 |
return false; |
| 225 |
} |
| 226 |
|
| 227 |
/** |
| 228 |
* Check if the width of aCh is full width, or wide. |
| 229 |
*/ |
| 230 |
static inline bool IsEastAsianWidthFW(uint32_t aCh) { |
| 231 |
switch (GetIntPropertyValue(aCh, IntProperty::EastAsianWidth)) { |
| 232 |
case U_EA_FULLWIDTH: |
| 233 |
case U_EA_WIDE: |
| 234 |
return true; |
| 235 |
case U_EA_AMBIGUOUS: |
| 236 |
case U_EA_HALFWIDTH: |
| 237 |
case U_EA_NARROW: |
| 238 |
case U_EA_NEUTRAL: |
| 239 |
return false; |
| 240 |
} |
| 241 |
return false; |
| 242 |
} |
| 243 |
|
| 244 |
/** |
| 245 |
* Check if the CharType of aCh is a punctuation type. |
| 246 |
*/ |
| 247 |
static inline bool IsPunctuation(uint32_t aCh) { |
| 248 |
switch (CharType(aCh)) { |
| 249 |
case GeneralCategory::Dash_Punctuation: |
| 250 |
case GeneralCategory::Open_Punctuation: |
| 251 |
case GeneralCategory::Close_Punctuation: |
| 252 |
case GeneralCategory::Connector_Punctuation: |
| 253 |
case GeneralCategory::Other_Punctuation: |
| 254 |
case GeneralCategory::Initial_Punctuation: |
| 255 |
case GeneralCategory::Final_Punctuation: |
| 256 |
return true; |
| 257 |
default: |
| 258 |
return false; |
| 259 |
} |
| 260 |
} |
| 261 |
|
| 262 |
/** |
| 263 |
* Check if the CharType of aCh is math or other symbol. |
| 264 |
*/ |
| 265 |
static inline bool IsMathOrMusicSymbol(uint32_t aCh) { |
| 266 |
// Keep this function in sync with is_math_symbol in base_chars.py. |
| 267 |
return CharType(aCh) == GeneralCategory::Math_Symbol || |
| 268 |
CharType(aCh) == GeneralCategory::Other_Symbol; |
| 269 |
} |
| 270 |
|
| 271 |
static inline Script GetScriptCode(uint32_t aCh) { |
| 272 |
// We can safely ignore the error code here because uscript_getScript |
| 273 |
// returns USCRIPT_INVALID_CODE in the event of an error. |
| 274 |
UErrorCode err = U_ZERO_ERROR; |
| 275 |
return Script(uscript_getScript(aCh, &err)); |
| 276 |
} |
| 277 |
|
| 278 |
static inline bool HasScript(uint32_t aCh, Script aScript) { |
| 279 |
return uscript_hasScript(aCh, UScriptCode(aScript)); |
| 280 |
} |
| 281 |
|
| 282 |
static inline const char* GetScriptShortName(Script aScript) { |
| 283 |
return uscript_getShortName(UScriptCode(aScript)); |
| 284 |
} |
| 285 |
|
| 286 |
static inline int32_t GetMaxNumberOfScripts() { |
| 287 |
return u_getIntPropertyMaxValue(UCHAR_SCRIPT); |
| 288 |
} |
| 289 |
|
| 290 |
// Return true if aChar belongs to a SEAsian script that is written without |
| 291 |
// word spaces, so we need to use the "complex breaker" to find possible word |
| 292 |
// boundaries. (https://en.wikipedia.org/wiki/Scriptio_continua) |
| 293 |
static bool IsScriptioContinua(char16_t aChar) { |
| 294 |
Script sc = GetScriptCode(aChar); |
| 295 |
return sc == Script::THAI || sc == Script::MYANMAR || sc == Script::KHMER || |
| 296 |
sc == Script::JAVANESE || sc == Script::BALINESE || |
| 297 |
sc == Script::SUNDANESE || sc == Script::LAO; |
| 298 |
} |
| 299 |
|
| 300 |
// The code point which has the most script extensions is 0x0965, which has 21 |
| 301 |
// script extensions, so choose the vector size as 32 to prevent heap |
| 302 |
// allocation. |
| 303 |
static constexpr size_t kMaxScripts = 32; |
| 304 |
|
| 305 |
using ScriptExtensionVector = Vector<Script, kMaxScripts>; |
| 306 |
|
| 307 |
/** |
| 308 |
* Get the script extensions for the given code point, and write the script |
| 309 |
* extensions to aExtensions vector. If the code point has script extensions, |
| 310 |
* the script code (Script::COMMON or Script::INHERITED) will be excluded. |
| 311 |
* |
| 312 |
* If the code point doesn't have any script extension, then its script code |
| 313 |
* will be written to aExtensions vector. |
| 314 |
* |
| 315 |
* If the code point is invalid, Script::UNKNOWN will be written to |
| 316 |
* aExtensions vector. |
| 317 |
* |
| 318 |
* Note: aExtensions will be cleared after calling this method regardless of |
| 319 |
* failure. |
| 320 |
* |
| 321 |
* See [1] for the script code of the code point, [2] for the script |
| 322 |
* extensions. |
| 323 |
* |
| 324 |
* https://www.unicode.org/Public/UNIDATA/Scripts.txt |
| 325 |
* https://www.unicode.org/Public/UNIDATA/ScriptExtensions.txt |
| 326 |
*/ |
| 327 |
static ICUResult GetExtensions(char32_t aCodePoint, |
| 328 |
ScriptExtensionVector& aExtensions) { |
| 329 |
// Clear the vector first. |
| 330 |
aExtensions.clear(); |
| 331 |
|
| 332 |
// We cannot pass aExtensions to uscript_getScriptExtension as USCriptCode |
| 333 |
// takes 4 bytes, so create a local UScriptCode array to get the extensions. |
| 334 |
UScriptCode ext[kMaxScripts]; |
| 335 |
UErrorCode status = U_ZERO_ERROR; |
| 336 |
int32_t len = uscript_getScriptExtensions(static_cast<UChar32>(aCodePoint), |
| 337 |
ext, kMaxScripts, &status); |
| 338 |
if (U_FAILURE(status)) { |
| 339 |
// kMaxScripts should be large enough to hold the maximun number of script |
| 340 |
// extensions. |
| 341 |
MOZ_DIAGNOSTIC_ASSERT(status != U_BUFFER_OVERFLOW_ERROR); |
| 342 |
return Err(ToICUError(status)); |
| 343 |
} |
| 344 |
|
| 345 |
if (!aExtensions.reserve(len)) { |
| 346 |
return Err(ICUError::OutOfMemory); |
| 347 |
} |
| 348 |
|
| 349 |
for (int32_t i = 0; i < len; i++) { |
| 350 |
aExtensions.infallibleAppend(Script(ext[i])); |
| 351 |
} |
| 352 |
|
| 353 |
return Ok(); |
| 354 |
} |
| 355 |
}; |
| 356 |
|
| 357 |
} // namespace mozilla::intl |
| 358 |
|
| 359 |
#endif |