| 1 | /* | 
|---|
| 2 | * Copyright © 2009  Red Hat, Inc. | 
|---|
| 3 | * Copyright © 2009  Keith Stribley | 
|---|
| 4 | * Copyright © 2011  Google, Inc. | 
|---|
| 5 | * | 
|---|
| 6 | *  This is part of HarfBuzz, a text shaping library. | 
|---|
| 7 | * | 
|---|
| 8 | * Permission is hereby granted, without written agreement and without | 
|---|
| 9 | * license or royalty fees, to use, copy, modify, and distribute this | 
|---|
| 10 | * software and its documentation for any purpose, provided that the | 
|---|
| 11 | * above copyright notice and the following two paragraphs appear in | 
|---|
| 12 | * all copies of this software. | 
|---|
| 13 | * | 
|---|
| 14 | * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR | 
|---|
| 15 | * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES | 
|---|
| 16 | * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN | 
|---|
| 17 | * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH | 
|---|
| 18 | * DAMAGE. | 
|---|
| 19 | * | 
|---|
| 20 | * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, | 
|---|
| 21 | * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND | 
|---|
| 22 | * FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS | 
|---|
| 23 | * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO | 
|---|
| 24 | * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. | 
|---|
| 25 | * | 
|---|
| 26 | * Red Hat Author(s): Behdad Esfahbod | 
|---|
| 27 | * Google Author(s): Behdad Esfahbod | 
|---|
| 28 | */ | 
|---|
| 29 |  | 
|---|
| 30 | #include "hb.hh" | 
|---|
| 31 |  | 
|---|
| 32 | #ifdef HAVE_ICU | 
|---|
| 33 |  | 
|---|
| 34 | #include "hb-icu.h" | 
|---|
| 35 |  | 
|---|
| 36 | #include "hb-machinery.hh" | 
|---|
| 37 |  | 
|---|
| 38 | #include <unicode/uchar.h> | 
|---|
| 39 | #include <unicode/unorm2.h> | 
|---|
| 40 | #include <unicode/ustring.h> | 
|---|
| 41 | #include <unicode/utf16.h> | 
|---|
| 42 | #include <unicode/uversion.h> | 
|---|
| 43 |  | 
|---|
| 44 | /* ICU extra semicolon, fixed since 65, https://github.com/unicode-org/icu/commit/480bec3 */ | 
|---|
| 45 | #if U_ICU_VERSION_MAJOR_NUM < 65 && (defined(__GNUC__) || defined(__clang__)) | 
|---|
| 46 | #define HB_ICU_EXTRA_SEMI_IGNORED | 
|---|
| 47 | #pragma GCC diagnostic push | 
|---|
| 48 | #pragma GCC diagnostic ignored "-Wextra-semi-stmt" | 
|---|
| 49 | #endif | 
|---|
| 50 |  | 
|---|
| 51 | /** | 
|---|
| 52 | * SECTION:hb-icu | 
|---|
| 53 | * @title: hb-icu | 
|---|
| 54 | * @short_description: ICU integration | 
|---|
| 55 | * @include: hb-icu.h | 
|---|
| 56 | * | 
|---|
| 57 | * Functions for using HarfBuzz with the International Components for Unicode | 
|---|
| 58 | * (ICU) library. HarfBuzz supports using ICU to provide Unicode data, by attaching | 
|---|
| 59 | * ICU functions to the virtual methods in a #hb_unicode_funcs_t function | 
|---|
| 60 | * structure. | 
|---|
| 61 | **/ | 
|---|
| 62 |  | 
|---|
| 63 | /** | 
|---|
| 64 | * hb_icu_script_to_script: | 
|---|
| 65 | * @script: The UScriptCode identifier to query | 
|---|
| 66 | * | 
|---|
| 67 | * Fetches the #hb_script_t script that corresponds to the | 
|---|
| 68 | * specified UScriptCode identifier. | 
|---|
| 69 | * | 
|---|
| 70 | * Return value: the #hb_script_t script found | 
|---|
| 71 | * | 
|---|
| 72 | **/ | 
|---|
| 73 |  | 
|---|
| 74 | hb_script_t | 
|---|
| 75 | hb_icu_script_to_script (UScriptCode script) | 
|---|
| 76 | { | 
|---|
| 77 | if (unlikely (script == USCRIPT_INVALID_CODE)) | 
|---|
| 78 | return HB_SCRIPT_INVALID; | 
|---|
| 79 |  | 
|---|
| 80 | return hb_script_from_string (uscript_getShortName (script), -1); | 
|---|
| 81 | } | 
|---|
| 82 |  | 
|---|
| 83 | /** | 
|---|
| 84 | * hb_icu_script_from_script: | 
|---|
| 85 | * @script: The #hb_script_t script to query | 
|---|
| 86 | * | 
|---|
| 87 | * Fetches the UScriptCode identifier that corresponds to the | 
|---|
| 88 | * specified #hb_script_t script. | 
|---|
| 89 | * | 
|---|
| 90 | * Return value: the UScriptCode identifier found | 
|---|
| 91 | * | 
|---|
| 92 | **/ | 
|---|
| 93 | UScriptCode | 
|---|
| 94 | hb_icu_script_from_script (hb_script_t script) | 
|---|
| 95 | { | 
|---|
| 96 | if (unlikely (script == HB_SCRIPT_INVALID)) | 
|---|
| 97 | return USCRIPT_INVALID_CODE; | 
|---|
| 98 |  | 
|---|
| 99 | unsigned int numScriptCode = 1 + u_getIntPropertyMaxValue (UCHAR_SCRIPT); | 
|---|
| 100 | for (unsigned int i = 0; i < numScriptCode; i++) | 
|---|
| 101 | if (unlikely (hb_icu_script_to_script ((UScriptCode) i) == script)) | 
|---|
| 102 | return (UScriptCode) i; | 
|---|
| 103 |  | 
|---|
| 104 | return USCRIPT_UNKNOWN; | 
|---|
| 105 | } | 
|---|
| 106 |  | 
|---|
| 107 |  | 
|---|
| 108 | static hb_unicode_combining_class_t | 
|---|
| 109 | hb_icu_unicode_combining_class (hb_unicode_funcs_t *ufuncs HB_UNUSED, | 
|---|
| 110 | hb_codepoint_t      unicode, | 
|---|
| 111 | void               *user_data HB_UNUSED) | 
|---|
| 112 |  | 
|---|
| 113 | { | 
|---|
| 114 | return (hb_unicode_combining_class_t) u_getCombiningClass (unicode); | 
|---|
| 115 | } | 
|---|
| 116 |  | 
|---|
| 117 | static hb_unicode_general_category_t | 
|---|
| 118 | hb_icu_unicode_general_category (hb_unicode_funcs_t *ufuncs HB_UNUSED, | 
|---|
| 119 | hb_codepoint_t      unicode, | 
|---|
| 120 | void               *user_data HB_UNUSED) | 
|---|
| 121 | { | 
|---|
| 122 | switch (u_getIntPropertyValue(unicode, UCHAR_GENERAL_CATEGORY)) | 
|---|
| 123 | { | 
|---|
| 124 | case U_UNASSIGNED:			return HB_UNICODE_GENERAL_CATEGORY_UNASSIGNED; | 
|---|
| 125 |  | 
|---|
| 126 | case U_UPPERCASE_LETTER:		return HB_UNICODE_GENERAL_CATEGORY_UPPERCASE_LETTER; | 
|---|
| 127 | case U_LOWERCASE_LETTER:		return HB_UNICODE_GENERAL_CATEGORY_LOWERCASE_LETTER; | 
|---|
| 128 | case U_TITLECASE_LETTER:		return HB_UNICODE_GENERAL_CATEGORY_TITLECASE_LETTER; | 
|---|
| 129 | case U_MODIFIER_LETTER:		return HB_UNICODE_GENERAL_CATEGORY_MODIFIER_LETTER; | 
|---|
| 130 | case U_OTHER_LETTER:			return HB_UNICODE_GENERAL_CATEGORY_OTHER_LETTER; | 
|---|
| 131 |  | 
|---|
| 132 | case U_NON_SPACING_MARK:		return HB_UNICODE_GENERAL_CATEGORY_NON_SPACING_MARK; | 
|---|
| 133 | case U_ENCLOSING_MARK:		return HB_UNICODE_GENERAL_CATEGORY_ENCLOSING_MARK; | 
|---|
| 134 | case U_COMBINING_SPACING_MARK:	return HB_UNICODE_GENERAL_CATEGORY_SPACING_MARK; | 
|---|
| 135 |  | 
|---|
| 136 | case U_DECIMAL_DIGIT_NUMBER:		return HB_UNICODE_GENERAL_CATEGORY_DECIMAL_NUMBER; | 
|---|
| 137 | case U_LETTER_NUMBER:			return HB_UNICODE_GENERAL_CATEGORY_LETTER_NUMBER; | 
|---|
| 138 | case U_OTHER_NUMBER:			return HB_UNICODE_GENERAL_CATEGORY_OTHER_NUMBER; | 
|---|
| 139 |  | 
|---|
| 140 | case U_SPACE_SEPARATOR:		return HB_UNICODE_GENERAL_CATEGORY_SPACE_SEPARATOR; | 
|---|
| 141 | case U_LINE_SEPARATOR:		return HB_UNICODE_GENERAL_CATEGORY_LINE_SEPARATOR; | 
|---|
| 142 | case U_PARAGRAPH_SEPARATOR:		return HB_UNICODE_GENERAL_CATEGORY_PARAGRAPH_SEPARATOR; | 
|---|
| 143 |  | 
|---|
| 144 | case U_CONTROL_CHAR:			return HB_UNICODE_GENERAL_CATEGORY_CONTROL; | 
|---|
| 145 | case U_FORMAT_CHAR:			return HB_UNICODE_GENERAL_CATEGORY_FORMAT; | 
|---|
| 146 | case U_PRIVATE_USE_CHAR:		return HB_UNICODE_GENERAL_CATEGORY_PRIVATE_USE; | 
|---|
| 147 | case U_SURROGATE:			return HB_UNICODE_GENERAL_CATEGORY_SURROGATE; | 
|---|
| 148 |  | 
|---|
| 149 |  | 
|---|
| 150 | case U_DASH_PUNCTUATION:		return HB_UNICODE_GENERAL_CATEGORY_DASH_PUNCTUATION; | 
|---|
| 151 | case U_START_PUNCTUATION:		return HB_UNICODE_GENERAL_CATEGORY_OPEN_PUNCTUATION; | 
|---|
| 152 | case U_END_PUNCTUATION:		return HB_UNICODE_GENERAL_CATEGORY_CLOSE_PUNCTUATION; | 
|---|
| 153 | case U_CONNECTOR_PUNCTUATION:		return HB_UNICODE_GENERAL_CATEGORY_CONNECT_PUNCTUATION; | 
|---|
| 154 | case U_OTHER_PUNCTUATION:		return HB_UNICODE_GENERAL_CATEGORY_OTHER_PUNCTUATION; | 
|---|
| 155 |  | 
|---|
| 156 | case U_MATH_SYMBOL:			return HB_UNICODE_GENERAL_CATEGORY_MATH_SYMBOL; | 
|---|
| 157 | case U_CURRENCY_SYMBOL:		return HB_UNICODE_GENERAL_CATEGORY_CURRENCY_SYMBOL; | 
|---|
| 158 | case U_MODIFIER_SYMBOL:		return HB_UNICODE_GENERAL_CATEGORY_MODIFIER_SYMBOL; | 
|---|
| 159 | case U_OTHER_SYMBOL:			return HB_UNICODE_GENERAL_CATEGORY_OTHER_SYMBOL; | 
|---|
| 160 |  | 
|---|
| 161 | case U_INITIAL_PUNCTUATION:		return HB_UNICODE_GENERAL_CATEGORY_INITIAL_PUNCTUATION; | 
|---|
| 162 | case U_FINAL_PUNCTUATION:		return HB_UNICODE_GENERAL_CATEGORY_FINAL_PUNCTUATION; | 
|---|
| 163 | } | 
|---|
| 164 |  | 
|---|
| 165 | return HB_UNICODE_GENERAL_CATEGORY_UNASSIGNED; | 
|---|
| 166 | } | 
|---|
| 167 |  | 
|---|
| 168 | static hb_codepoint_t | 
|---|
| 169 | hb_icu_unicode_mirroring (hb_unicode_funcs_t *ufuncs HB_UNUSED, | 
|---|
| 170 | hb_codepoint_t      unicode, | 
|---|
| 171 | void               *user_data HB_UNUSED) | 
|---|
| 172 | { | 
|---|
| 173 | return u_charMirror(unicode); | 
|---|
| 174 | } | 
|---|
| 175 |  | 
|---|
| 176 | static hb_script_t | 
|---|
| 177 | hb_icu_unicode_script (hb_unicode_funcs_t *ufuncs HB_UNUSED, | 
|---|
| 178 | hb_codepoint_t      unicode, | 
|---|
| 179 | void               *user_data HB_UNUSED) | 
|---|
| 180 | { | 
|---|
| 181 | UErrorCode status = U_ZERO_ERROR; | 
|---|
| 182 | UScriptCode scriptCode = uscript_getScript(unicode, &status); | 
|---|
| 183 |  | 
|---|
| 184 | if (unlikely (U_FAILURE (status))) | 
|---|
| 185 | return HB_SCRIPT_UNKNOWN; | 
|---|
| 186 |  | 
|---|
| 187 | return hb_icu_script_to_script (scriptCode); | 
|---|
| 188 | } | 
|---|
| 189 |  | 
|---|
| 190 | static hb_bool_t | 
|---|
| 191 | hb_icu_unicode_compose (hb_unicode_funcs_t *ufuncs HB_UNUSED, | 
|---|
| 192 | hb_codepoint_t      a, | 
|---|
| 193 | hb_codepoint_t      b, | 
|---|
| 194 | hb_codepoint_t     *ab, | 
|---|
| 195 | void               *user_data) | 
|---|
| 196 | { | 
|---|
| 197 | const UNormalizer2 *normalizer = (const UNormalizer2 *) user_data; | 
|---|
| 198 | UChar32 ret = unorm2_composePair (normalizer, a, b); | 
|---|
| 199 | if (ret < 0) return false; | 
|---|
| 200 | *ab = ret; | 
|---|
| 201 | return true; | 
|---|
| 202 | } | 
|---|
| 203 |  | 
|---|
| 204 | static hb_bool_t | 
|---|
| 205 | hb_icu_unicode_decompose (hb_unicode_funcs_t *ufuncs HB_UNUSED, | 
|---|
| 206 | hb_codepoint_t      ab, | 
|---|
| 207 | hb_codepoint_t     *a, | 
|---|
| 208 | hb_codepoint_t     *b, | 
|---|
| 209 | void               *user_data) | 
|---|
| 210 | { | 
|---|
| 211 | const UNormalizer2 *normalizer = (const UNormalizer2 *) user_data; | 
|---|
| 212 | UChar decomposed[4]; | 
|---|
| 213 | int len; | 
|---|
| 214 | UErrorCode icu_err = U_ZERO_ERROR; | 
|---|
| 215 | len = unorm2_getRawDecomposition (normalizer, ab, decomposed, | 
|---|
| 216 | ARRAY_LENGTH (decomposed), &icu_err); | 
|---|
| 217 | if (U_FAILURE (icu_err) || len < 0) return false; | 
|---|
| 218 |  | 
|---|
| 219 | len = u_countChar32 (decomposed, len); | 
|---|
| 220 | if (len == 1) | 
|---|
| 221 | { | 
|---|
| 222 | U16_GET_UNSAFE (decomposed, 0, *a); | 
|---|
| 223 | *b = 0; | 
|---|
| 224 | return *a != ab; | 
|---|
| 225 | } | 
|---|
| 226 | else if (len == 2) | 
|---|
| 227 | { | 
|---|
| 228 | len = 0; | 
|---|
| 229 | U16_NEXT_UNSAFE (decomposed, len, *a); | 
|---|
| 230 | U16_NEXT_UNSAFE (decomposed, len, *b); | 
|---|
| 231 | } | 
|---|
| 232 | return true; | 
|---|
| 233 | } | 
|---|
| 234 |  | 
|---|
| 235 |  | 
|---|
| 236 | static inline void free_static_icu_funcs (); | 
|---|
| 237 |  | 
|---|
| 238 | static struct hb_icu_unicode_funcs_lazy_loader_t : hb_unicode_funcs_lazy_loader_t<hb_icu_unicode_funcs_lazy_loader_t> | 
|---|
| 239 | { | 
|---|
| 240 | static hb_unicode_funcs_t *create () | 
|---|
| 241 | { | 
|---|
| 242 | void *user_data = nullptr; | 
|---|
| 243 | UErrorCode icu_err = U_ZERO_ERROR; | 
|---|
| 244 | user_data = (void *) unorm2_getNFCInstance (&icu_err); | 
|---|
| 245 | assert (user_data); | 
|---|
| 246 |  | 
|---|
| 247 | hb_unicode_funcs_t *funcs = hb_unicode_funcs_create (nullptr); | 
|---|
| 248 |  | 
|---|
| 249 | hb_unicode_funcs_set_combining_class_func (funcs, hb_icu_unicode_combining_class, nullptr, nullptr); | 
|---|
| 250 | hb_unicode_funcs_set_general_category_func (funcs, hb_icu_unicode_general_category, nullptr, nullptr); | 
|---|
| 251 | hb_unicode_funcs_set_mirroring_func (funcs, hb_icu_unicode_mirroring, nullptr, nullptr); | 
|---|
| 252 | hb_unicode_funcs_set_script_func (funcs, hb_icu_unicode_script, nullptr, nullptr); | 
|---|
| 253 | hb_unicode_funcs_set_compose_func (funcs, hb_icu_unicode_compose, user_data, nullptr); | 
|---|
| 254 | hb_unicode_funcs_set_decompose_func (funcs, hb_icu_unicode_decompose, user_data, nullptr); | 
|---|
| 255 |  | 
|---|
| 256 | hb_unicode_funcs_make_immutable (funcs); | 
|---|
| 257 |  | 
|---|
| 258 | hb_atexit (free_static_icu_funcs); | 
|---|
| 259 |  | 
|---|
| 260 | return funcs; | 
|---|
| 261 | } | 
|---|
| 262 | } static_icu_funcs; | 
|---|
| 263 |  | 
|---|
| 264 | static inline | 
|---|
| 265 | void free_static_icu_funcs () | 
|---|
| 266 | { | 
|---|
| 267 | static_icu_funcs.free_instance (); | 
|---|
| 268 | } | 
|---|
| 269 |  | 
|---|
| 270 | /** | 
|---|
| 271 | * hb_icu_get_unicode_funcs: | 
|---|
| 272 | * | 
|---|
| 273 | * Fetches a Unicode-functions structure that is populated | 
|---|
| 274 | * with the appropriate ICU function for each method. | 
|---|
| 275 | * | 
|---|
| 276 | * Return value: (transfer none): a pointer to the #hb_unicode_funcs_t Unicode-functions structure | 
|---|
| 277 | * | 
|---|
| 278 | * Since: 0.9.38 | 
|---|
| 279 | **/ | 
|---|
| 280 | hb_unicode_funcs_t * | 
|---|
| 281 | hb_icu_get_unicode_funcs () | 
|---|
| 282 | { | 
|---|
| 283 | return static_icu_funcs.get_unconst (); | 
|---|
| 284 | } | 
|---|
| 285 |  | 
|---|
| 286 | #ifdef HB_ICU_EXTRA_SEMI_IGNORED | 
|---|
| 287 | #pragma GCC diagnostic pop | 
|---|
| 288 | #endif | 
|---|
| 289 |  | 
|---|
| 290 | #endif | 
|---|
| 291 |  | 
|---|