gfx/harfbuzz/src/hb-icu.cc

Fri, 16 Jan 2015 18:13:44 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Fri, 16 Jan 2015 18:13:44 +0100
branch
TOR_BUG_9701
changeset 14
925c144e1f1f
permissions
-rw-r--r--

Integrate suggestion from review to improve consistency with existing code.

michael@0 1 /*
michael@0 2 * Copyright © 2009 Red Hat, Inc.
michael@0 3 * Copyright © 2009 Keith Stribley
michael@0 4 * Copyright © 2011 Google, Inc.
michael@0 5 *
michael@0 6 * This is part of HarfBuzz, a text shaping library.
michael@0 7 *
michael@0 8 * Permission is hereby granted, without written agreement and without
michael@0 9 * license or royalty fees, to use, copy, modify, and distribute this
michael@0 10 * software and its documentation for any purpose, provided that the
michael@0 11 * above copyright notice and the following two paragraphs appear in
michael@0 12 * all copies of this software.
michael@0 13 *
michael@0 14 * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
michael@0 15 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
michael@0 16 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
michael@0 17 * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
michael@0 18 * DAMAGE.
michael@0 19 *
michael@0 20 * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
michael@0 21 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
michael@0 22 * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
michael@0 23 * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
michael@0 24 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
michael@0 25 *
michael@0 26 * Red Hat Author(s): Behdad Esfahbod
michael@0 27 * Google Author(s): Behdad Esfahbod
michael@0 28 */
michael@0 29
michael@0 30 #include "hb-private.hh"
michael@0 31
michael@0 32 #include "hb-icu.h"
michael@0 33
michael@0 34 #include "hb-unicode-private.hh"
michael@0 35
michael@0 36 #include <unicode/uchar.h>
michael@0 37 #include <unicode/unorm.h>
michael@0 38 #include <unicode/ustring.h>
michael@0 39 #include <unicode/uversion.h>
michael@0 40
michael@0 41
michael@0 42 hb_script_t
michael@0 43 hb_icu_script_to_script (UScriptCode script)
michael@0 44 {
michael@0 45 if (unlikely (script == USCRIPT_INVALID_CODE))
michael@0 46 return HB_SCRIPT_INVALID;
michael@0 47
michael@0 48 return hb_script_from_string (uscript_getShortName (script), -1);
michael@0 49 }
michael@0 50
michael@0 51 UScriptCode
michael@0 52 hb_icu_script_from_script (hb_script_t script)
michael@0 53 {
michael@0 54 if (unlikely (script == HB_SCRIPT_INVALID))
michael@0 55 return USCRIPT_INVALID_CODE;
michael@0 56
michael@0 57 for (unsigned int i = 0; i < USCRIPT_CODE_LIMIT; i++)
michael@0 58 if (unlikely (hb_icu_script_to_script ((UScriptCode) i) == script))
michael@0 59 return (UScriptCode) i;
michael@0 60
michael@0 61 return USCRIPT_UNKNOWN;
michael@0 62 }
michael@0 63
michael@0 64
michael@0 65 static hb_unicode_combining_class_t
michael@0 66 hb_icu_unicode_combining_class (hb_unicode_funcs_t *ufuncs HB_UNUSED,
michael@0 67 hb_codepoint_t unicode,
michael@0 68 void *user_data HB_UNUSED)
michael@0 69
michael@0 70 {
michael@0 71 return (hb_unicode_combining_class_t) u_getCombiningClass (unicode);
michael@0 72 }
michael@0 73
michael@0 74 static unsigned int
michael@0 75 hb_icu_unicode_eastasian_width (hb_unicode_funcs_t *ufuncs HB_UNUSED,
michael@0 76 hb_codepoint_t unicode,
michael@0 77 void *user_data HB_UNUSED)
michael@0 78 {
michael@0 79 switch (u_getIntPropertyValue(unicode, UCHAR_EAST_ASIAN_WIDTH))
michael@0 80 {
michael@0 81 case U_EA_WIDE:
michael@0 82 case U_EA_FULLWIDTH:
michael@0 83 return 2;
michael@0 84 case U_EA_NEUTRAL:
michael@0 85 case U_EA_AMBIGUOUS:
michael@0 86 case U_EA_HALFWIDTH:
michael@0 87 case U_EA_NARROW:
michael@0 88 return 1;
michael@0 89 }
michael@0 90 return 1;
michael@0 91 }
michael@0 92
michael@0 93 static hb_unicode_general_category_t
michael@0 94 hb_icu_unicode_general_category (hb_unicode_funcs_t *ufuncs HB_UNUSED,
michael@0 95 hb_codepoint_t unicode,
michael@0 96 void *user_data HB_UNUSED)
michael@0 97 {
michael@0 98 switch (u_getIntPropertyValue(unicode, UCHAR_GENERAL_CATEGORY))
michael@0 99 {
michael@0 100 case U_UNASSIGNED: return HB_UNICODE_GENERAL_CATEGORY_UNASSIGNED;
michael@0 101
michael@0 102 case U_UPPERCASE_LETTER: return HB_UNICODE_GENERAL_CATEGORY_UPPERCASE_LETTER;
michael@0 103 case U_LOWERCASE_LETTER: return HB_UNICODE_GENERAL_CATEGORY_LOWERCASE_LETTER;
michael@0 104 case U_TITLECASE_LETTER: return HB_UNICODE_GENERAL_CATEGORY_TITLECASE_LETTER;
michael@0 105 case U_MODIFIER_LETTER: return HB_UNICODE_GENERAL_CATEGORY_MODIFIER_LETTER;
michael@0 106 case U_OTHER_LETTER: return HB_UNICODE_GENERAL_CATEGORY_OTHER_LETTER;
michael@0 107
michael@0 108 case U_NON_SPACING_MARK: return HB_UNICODE_GENERAL_CATEGORY_NON_SPACING_MARK;
michael@0 109 case U_ENCLOSING_MARK: return HB_UNICODE_GENERAL_CATEGORY_ENCLOSING_MARK;
michael@0 110 case U_COMBINING_SPACING_MARK: return HB_UNICODE_GENERAL_CATEGORY_SPACING_MARK;
michael@0 111
michael@0 112 case U_DECIMAL_DIGIT_NUMBER: return HB_UNICODE_GENERAL_CATEGORY_DECIMAL_NUMBER;
michael@0 113 case U_LETTER_NUMBER: return HB_UNICODE_GENERAL_CATEGORY_LETTER_NUMBER;
michael@0 114 case U_OTHER_NUMBER: return HB_UNICODE_GENERAL_CATEGORY_OTHER_NUMBER;
michael@0 115
michael@0 116 case U_SPACE_SEPARATOR: return HB_UNICODE_GENERAL_CATEGORY_SPACE_SEPARATOR;
michael@0 117 case U_LINE_SEPARATOR: return HB_UNICODE_GENERAL_CATEGORY_LINE_SEPARATOR;
michael@0 118 case U_PARAGRAPH_SEPARATOR: return HB_UNICODE_GENERAL_CATEGORY_PARAGRAPH_SEPARATOR;
michael@0 119
michael@0 120 case U_CONTROL_CHAR: return HB_UNICODE_GENERAL_CATEGORY_CONTROL;
michael@0 121 case U_FORMAT_CHAR: return HB_UNICODE_GENERAL_CATEGORY_FORMAT;
michael@0 122 case U_PRIVATE_USE_CHAR: return HB_UNICODE_GENERAL_CATEGORY_PRIVATE_USE;
michael@0 123 case U_SURROGATE: return HB_UNICODE_GENERAL_CATEGORY_SURROGATE;
michael@0 124
michael@0 125
michael@0 126 case U_DASH_PUNCTUATION: return HB_UNICODE_GENERAL_CATEGORY_DASH_PUNCTUATION;
michael@0 127 case U_START_PUNCTUATION: return HB_UNICODE_GENERAL_CATEGORY_OPEN_PUNCTUATION;
michael@0 128 case U_END_PUNCTUATION: return HB_UNICODE_GENERAL_CATEGORY_CLOSE_PUNCTUATION;
michael@0 129 case U_CONNECTOR_PUNCTUATION: return HB_UNICODE_GENERAL_CATEGORY_CONNECT_PUNCTUATION;
michael@0 130 case U_OTHER_PUNCTUATION: return HB_UNICODE_GENERAL_CATEGORY_OTHER_PUNCTUATION;
michael@0 131
michael@0 132 case U_MATH_SYMBOL: return HB_UNICODE_GENERAL_CATEGORY_MATH_SYMBOL;
michael@0 133 case U_CURRENCY_SYMBOL: return HB_UNICODE_GENERAL_CATEGORY_CURRENCY_SYMBOL;
michael@0 134 case U_MODIFIER_SYMBOL: return HB_UNICODE_GENERAL_CATEGORY_MODIFIER_SYMBOL;
michael@0 135 case U_OTHER_SYMBOL: return HB_UNICODE_GENERAL_CATEGORY_OTHER_SYMBOL;
michael@0 136
michael@0 137 case U_INITIAL_PUNCTUATION: return HB_UNICODE_GENERAL_CATEGORY_INITIAL_PUNCTUATION;
michael@0 138 case U_FINAL_PUNCTUATION: return HB_UNICODE_GENERAL_CATEGORY_FINAL_PUNCTUATION;
michael@0 139 }
michael@0 140
michael@0 141 return HB_UNICODE_GENERAL_CATEGORY_UNASSIGNED;
michael@0 142 }
michael@0 143
michael@0 144 static hb_codepoint_t
michael@0 145 hb_icu_unicode_mirroring (hb_unicode_funcs_t *ufuncs HB_UNUSED,
michael@0 146 hb_codepoint_t unicode,
michael@0 147 void *user_data HB_UNUSED)
michael@0 148 {
michael@0 149 return u_charMirror(unicode);
michael@0 150 }
michael@0 151
michael@0 152 static hb_script_t
michael@0 153 hb_icu_unicode_script (hb_unicode_funcs_t *ufuncs HB_UNUSED,
michael@0 154 hb_codepoint_t unicode,
michael@0 155 void *user_data HB_UNUSED)
michael@0 156 {
michael@0 157 UErrorCode status = U_ZERO_ERROR;
michael@0 158 UScriptCode scriptCode = uscript_getScript(unicode, &status);
michael@0 159
michael@0 160 if (unlikely (U_FAILURE (status)))
michael@0 161 return HB_SCRIPT_UNKNOWN;
michael@0 162
michael@0 163 return hb_icu_script_to_script (scriptCode);
michael@0 164 }
michael@0 165
michael@0 166 #if U_ICU_VERSION_MAJOR_NUM >= 49
michael@0 167 static const UNormalizer2 *normalizer;
michael@0 168 #endif
michael@0 169
michael@0 170 static hb_bool_t
michael@0 171 hb_icu_unicode_compose (hb_unicode_funcs_t *ufuncs HB_UNUSED,
michael@0 172 hb_codepoint_t a,
michael@0 173 hb_codepoint_t b,
michael@0 174 hb_codepoint_t *ab,
michael@0 175 void *user_data HB_UNUSED)
michael@0 176 {
michael@0 177 #if U_ICU_VERSION_MAJOR_NUM >= 49
michael@0 178 {
michael@0 179 UChar32 ret = unorm2_composePair (normalizer, a, b);
michael@0 180 if (ret < 0) return false;
michael@0 181 *ab = ret;
michael@0 182 return true;
michael@0 183 }
michael@0 184 #endif
michael@0 185
michael@0 186 /* We don't ifdef-out the fallback code such that compiler always
michael@0 187 * sees it and makes sure it's compilable. */
michael@0 188
michael@0 189 UChar utf16[4], normalized[5];
michael@0 190 unsigned int len;
michael@0 191 hb_bool_t ret, err;
michael@0 192 UErrorCode icu_err;
michael@0 193
michael@0 194 len = 0;
michael@0 195 err = false;
michael@0 196 U16_APPEND (utf16, len, ARRAY_LENGTH (utf16), a, err);
michael@0 197 if (err) return false;
michael@0 198 U16_APPEND (utf16, len, ARRAY_LENGTH (utf16), b, err);
michael@0 199 if (err) return false;
michael@0 200
michael@0 201 icu_err = U_ZERO_ERROR;
michael@0 202 len = unorm_normalize (utf16, len, UNORM_NFC, 0, normalized, ARRAY_LENGTH (normalized), &icu_err);
michael@0 203 if (U_FAILURE (icu_err))
michael@0 204 return false;
michael@0 205 if (u_countChar32 (normalized, len) == 1) {
michael@0 206 U16_GET_UNSAFE (normalized, 0, *ab);
michael@0 207 ret = true;
michael@0 208 } else {
michael@0 209 ret = false;
michael@0 210 }
michael@0 211
michael@0 212 return ret;
michael@0 213 }
michael@0 214
michael@0 215 static hb_bool_t
michael@0 216 hb_icu_unicode_decompose (hb_unicode_funcs_t *ufuncs HB_UNUSED,
michael@0 217 hb_codepoint_t ab,
michael@0 218 hb_codepoint_t *a,
michael@0 219 hb_codepoint_t *b,
michael@0 220 void *user_data HB_UNUSED)
michael@0 221 {
michael@0 222 #if U_ICU_VERSION_MAJOR_NUM >= 49
michael@0 223 {
michael@0 224 UChar decomposed[4];
michael@0 225 int len;
michael@0 226 UErrorCode icu_err = U_ZERO_ERROR;
michael@0 227 len = unorm2_getRawDecomposition (normalizer, ab, decomposed,
michael@0 228 ARRAY_LENGTH (decomposed), &icu_err);
michael@0 229 if (U_FAILURE (icu_err) || len < 0) return false;
michael@0 230
michael@0 231 len = u_countChar32 (decomposed, len);
michael@0 232 if (len == 1) {
michael@0 233 U16_GET_UNSAFE (decomposed, 0, *a);
michael@0 234 *b = 0;
michael@0 235 return *a != ab;
michael@0 236 } else if (len == 2) {
michael@0 237 len =0;
michael@0 238 U16_NEXT_UNSAFE (decomposed, len, *a);
michael@0 239 U16_NEXT_UNSAFE (decomposed, len, *b);
michael@0 240 }
michael@0 241 return true;
michael@0 242 }
michael@0 243 #endif
michael@0 244
michael@0 245 /* We don't ifdef-out the fallback code such that compiler always
michael@0 246 * sees it and makes sure it's compilable. */
michael@0 247
michael@0 248 UChar utf16[2], normalized[2 * HB_UNICODE_MAX_DECOMPOSITION_LEN + 1];
michael@0 249 unsigned int len;
michael@0 250 hb_bool_t ret, err;
michael@0 251 UErrorCode icu_err;
michael@0 252
michael@0 253 /* This function is a monster! Maybe it wasn't a good idea adding a
michael@0 254 * pairwise decompose API... */
michael@0 255 /* Watchout for the dragons. Err, watchout for macros changing len. */
michael@0 256
michael@0 257 len = 0;
michael@0 258 err = false;
michael@0 259 U16_APPEND (utf16, len, ARRAY_LENGTH (utf16), ab, err);
michael@0 260 if (err) return false;
michael@0 261
michael@0 262 icu_err = U_ZERO_ERROR;
michael@0 263 len = unorm_normalize (utf16, len, UNORM_NFD, 0, normalized, ARRAY_LENGTH (normalized), &icu_err);
michael@0 264 if (U_FAILURE (icu_err))
michael@0 265 return false;
michael@0 266
michael@0 267 len = u_countChar32 (normalized, len);
michael@0 268
michael@0 269 if (len == 1) {
michael@0 270 U16_GET_UNSAFE (normalized, 0, *a);
michael@0 271 *b = 0;
michael@0 272 ret = *a != ab;
michael@0 273 } else if (len == 2) {
michael@0 274 len =0;
michael@0 275 U16_NEXT_UNSAFE (normalized, len, *a);
michael@0 276 U16_NEXT_UNSAFE (normalized, len, *b);
michael@0 277
michael@0 278 /* Here's the ugly part: if ab decomposes to a single character and
michael@0 279 * that character decomposes again, we have to detect that and undo
michael@0 280 * the second part :-(. */
michael@0 281 UChar recomposed[20];
michael@0 282 icu_err = U_ZERO_ERROR;
michael@0 283 unorm_normalize (normalized, len, UNORM_NFC, 0, recomposed, ARRAY_LENGTH (recomposed), &icu_err);
michael@0 284 if (U_FAILURE (icu_err))
michael@0 285 return false;
michael@0 286 hb_codepoint_t c;
michael@0 287 U16_GET_UNSAFE (recomposed, 0, c);
michael@0 288 if (c != *a && c != ab) {
michael@0 289 *a = c;
michael@0 290 *b = 0;
michael@0 291 }
michael@0 292 ret = true;
michael@0 293 } else {
michael@0 294 /* If decomposed to more than two characters, take the last one,
michael@0 295 * and recompose the rest to get the first component. */
michael@0 296 U16_PREV_UNSAFE (normalized, len, *b); /* Changes len in-place. */
michael@0 297 UChar recomposed[18 * 2];
michael@0 298 icu_err = U_ZERO_ERROR;
michael@0 299 len = unorm_normalize (normalized, len, UNORM_NFC, 0, recomposed, ARRAY_LENGTH (recomposed), &icu_err);
michael@0 300 if (U_FAILURE (icu_err))
michael@0 301 return false;
michael@0 302 /* We expect that recomposed has exactly one character now. */
michael@0 303 if (unlikely (u_countChar32 (recomposed, len) != 1))
michael@0 304 return false;
michael@0 305 U16_GET_UNSAFE (recomposed, 0, *a);
michael@0 306 ret = true;
michael@0 307 }
michael@0 308
michael@0 309 return ret;
michael@0 310 }
michael@0 311
michael@0 312 static unsigned int
michael@0 313 hb_icu_unicode_decompose_compatibility (hb_unicode_funcs_t *ufuncs HB_UNUSED,
michael@0 314 hb_codepoint_t u,
michael@0 315 hb_codepoint_t *decomposed,
michael@0 316 void *user_data HB_UNUSED)
michael@0 317 {
michael@0 318 UChar utf16[2], normalized[2 * HB_UNICODE_MAX_DECOMPOSITION_LEN + 1];
michael@0 319 unsigned int len;
michael@0 320 int32_t utf32_len;
michael@0 321 hb_bool_t err;
michael@0 322 UErrorCode icu_err;
michael@0 323
michael@0 324 /* Copy @u into a UTF-16 array to be passed to ICU. */
michael@0 325 len = 0;
michael@0 326 err = FALSE;
michael@0 327 U16_APPEND (utf16, len, ARRAY_LENGTH (utf16), u, err);
michael@0 328 if (err)
michael@0 329 return 0;
michael@0 330
michael@0 331 /* Normalise the codepoint using NFKD mode. */
michael@0 332 icu_err = U_ZERO_ERROR;
michael@0 333 len = unorm_normalize (utf16, len, UNORM_NFKD, 0, normalized, ARRAY_LENGTH (normalized), &icu_err);
michael@0 334 if (icu_err)
michael@0 335 return 0;
michael@0 336
michael@0 337 /* Convert the decomposed form from UTF-16 to UTF-32. */
michael@0 338 icu_err = U_ZERO_ERROR;
michael@0 339 u_strToUTF32 ((UChar32*) decomposed, HB_UNICODE_MAX_DECOMPOSITION_LEN, &utf32_len, normalized, len, &icu_err);
michael@0 340 if (icu_err)
michael@0 341 return 0;
michael@0 342
michael@0 343 return utf32_len;
michael@0 344 }
michael@0 345
michael@0 346
michael@0 347 hb_unicode_funcs_t *
michael@0 348 hb_icu_get_unicode_funcs (void)
michael@0 349 {
michael@0 350 static const hb_unicode_funcs_t _hb_icu_unicode_funcs = {
michael@0 351 HB_OBJECT_HEADER_STATIC,
michael@0 352
michael@0 353 NULL, /* parent */
michael@0 354 true, /* immutable */
michael@0 355 {
michael@0 356 #define HB_UNICODE_FUNC_IMPLEMENT(name) hb_icu_unicode_##name,
michael@0 357 HB_UNICODE_FUNCS_IMPLEMENT_CALLBACKS
michael@0 358 #undef HB_UNICODE_FUNC_IMPLEMENT
michael@0 359 }
michael@0 360 };
michael@0 361
michael@0 362 #if U_ICU_VERSION_MAJOR_NUM >= 49
michael@0 363 if (!hb_atomic_ptr_get (&normalizer)) {
michael@0 364 UErrorCode icu_err = U_ZERO_ERROR;
michael@0 365 /* We ignore failure in getNFCInstace(). */
michael@0 366 hb_atomic_ptr_cmpexch (&normalizer, NULL, unorm2_getNFCInstance (&icu_err));
michael@0 367 }
michael@0 368 #endif
michael@0 369 return const_cast<hb_unicode_funcs_t *> (&_hb_icu_unicode_funcs);
michael@0 370 }
michael@0 371
michael@0 372

mercurial