2 * Copyright © 2009 Red Hat, Inc.
3 * Copyright © 2009 Keith Stribley
4 * Copyright © 2011 Google, Inc.
6 * This is part of HarfBuzz, a text shaping library.
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.
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
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.
26 * Red Hat Author(s): Behdad Esfahbod
27 * Google Author(s): Behdad Esfahbod
30 #include "hb-private.hh"
34 #include "hb-unicode-private.hh"
36 #include <unicode/uchar.h>
37 #include <unicode/unorm.h>
38 #include <unicode/ustring.h>
39 #include <unicode/uversion.h>
43 hb_icu_script_to_script (UScriptCode script)
45 if (unlikely (script == USCRIPT_INVALID_CODE))
46 return HB_SCRIPT_INVALID;
48 return hb_script_from_string (uscript_getShortName (script), -1);
52 hb_icu_script_from_script (hb_script_t script)
54 if (unlikely (script == HB_SCRIPT_INVALID))
55 return USCRIPT_INVALID_CODE;
57 for (unsigned int i = 0; i < USCRIPT_CODE_LIMIT; i++)
58 if (unlikely (hb_icu_script_to_script ((UScriptCode) i) == script))
59 return (UScriptCode) i;
61 return USCRIPT_UNKNOWN;
65 static hb_unicode_combining_class_t
66 hb_icu_unicode_combining_class (hb_unicode_funcs_t *ufuncs HB_UNUSED,
67 hb_codepoint_t unicode,
68 void *user_data HB_UNUSED)
71 return (hb_unicode_combining_class_t) u_getCombiningClass (unicode);
75 hb_icu_unicode_eastasian_width (hb_unicode_funcs_t *ufuncs HB_UNUSED,
76 hb_codepoint_t unicode,
77 void *user_data HB_UNUSED)
79 switch (u_getIntPropertyValue(unicode, UCHAR_EAST_ASIAN_WIDTH))
93 static hb_unicode_general_category_t
94 hb_icu_unicode_general_category (hb_unicode_funcs_t *ufuncs HB_UNUSED,
95 hb_codepoint_t unicode,
96 void *user_data HB_UNUSED)
98 switch (u_getIntPropertyValue(unicode, UCHAR_GENERAL_CATEGORY))
100 case U_UNASSIGNED: return HB_UNICODE_GENERAL_CATEGORY_UNASSIGNED;
102 case U_UPPERCASE_LETTER: return HB_UNICODE_GENERAL_CATEGORY_UPPERCASE_LETTER;
103 case U_LOWERCASE_LETTER: return HB_UNICODE_GENERAL_CATEGORY_LOWERCASE_LETTER;
104 case U_TITLECASE_LETTER: return HB_UNICODE_GENERAL_CATEGORY_TITLECASE_LETTER;
105 case U_MODIFIER_LETTER: return HB_UNICODE_GENERAL_CATEGORY_MODIFIER_LETTER;
106 case U_OTHER_LETTER: return HB_UNICODE_GENERAL_CATEGORY_OTHER_LETTER;
108 case U_NON_SPACING_MARK: return HB_UNICODE_GENERAL_CATEGORY_NON_SPACING_MARK;
109 case U_ENCLOSING_MARK: return HB_UNICODE_GENERAL_CATEGORY_ENCLOSING_MARK;
110 case U_COMBINING_SPACING_MARK: return HB_UNICODE_GENERAL_CATEGORY_SPACING_MARK;
112 case U_DECIMAL_DIGIT_NUMBER: return HB_UNICODE_GENERAL_CATEGORY_DECIMAL_NUMBER;
113 case U_LETTER_NUMBER: return HB_UNICODE_GENERAL_CATEGORY_LETTER_NUMBER;
114 case U_OTHER_NUMBER: return HB_UNICODE_GENERAL_CATEGORY_OTHER_NUMBER;
116 case U_SPACE_SEPARATOR: return HB_UNICODE_GENERAL_CATEGORY_SPACE_SEPARATOR;
117 case U_LINE_SEPARATOR: return HB_UNICODE_GENERAL_CATEGORY_LINE_SEPARATOR;
118 case U_PARAGRAPH_SEPARATOR: return HB_UNICODE_GENERAL_CATEGORY_PARAGRAPH_SEPARATOR;
120 case U_CONTROL_CHAR: return HB_UNICODE_GENERAL_CATEGORY_CONTROL;
121 case U_FORMAT_CHAR: return HB_UNICODE_GENERAL_CATEGORY_FORMAT;
122 case U_PRIVATE_USE_CHAR: return HB_UNICODE_GENERAL_CATEGORY_PRIVATE_USE;
123 case U_SURROGATE: return HB_UNICODE_GENERAL_CATEGORY_SURROGATE;
126 case U_DASH_PUNCTUATION: return HB_UNICODE_GENERAL_CATEGORY_DASH_PUNCTUATION;
127 case U_START_PUNCTUATION: return HB_UNICODE_GENERAL_CATEGORY_OPEN_PUNCTUATION;
128 case U_END_PUNCTUATION: return HB_UNICODE_GENERAL_CATEGORY_CLOSE_PUNCTUATION;
129 case U_CONNECTOR_PUNCTUATION: return HB_UNICODE_GENERAL_CATEGORY_CONNECT_PUNCTUATION;
130 case U_OTHER_PUNCTUATION: return HB_UNICODE_GENERAL_CATEGORY_OTHER_PUNCTUATION;
132 case U_MATH_SYMBOL: return HB_UNICODE_GENERAL_CATEGORY_MATH_SYMBOL;
133 case U_CURRENCY_SYMBOL: return HB_UNICODE_GENERAL_CATEGORY_CURRENCY_SYMBOL;
134 case U_MODIFIER_SYMBOL: return HB_UNICODE_GENERAL_CATEGORY_MODIFIER_SYMBOL;
135 case U_OTHER_SYMBOL: return HB_UNICODE_GENERAL_CATEGORY_OTHER_SYMBOL;
137 case U_INITIAL_PUNCTUATION: return HB_UNICODE_GENERAL_CATEGORY_INITIAL_PUNCTUATION;
138 case U_FINAL_PUNCTUATION: return HB_UNICODE_GENERAL_CATEGORY_FINAL_PUNCTUATION;
141 return HB_UNICODE_GENERAL_CATEGORY_UNASSIGNED;
144 static hb_codepoint_t
145 hb_icu_unicode_mirroring (hb_unicode_funcs_t *ufuncs HB_UNUSED,
146 hb_codepoint_t unicode,
147 void *user_data HB_UNUSED)
149 return u_charMirror(unicode);
153 hb_icu_unicode_script (hb_unicode_funcs_t *ufuncs HB_UNUSED,
154 hb_codepoint_t unicode,
155 void *user_data HB_UNUSED)
157 UErrorCode status = U_ZERO_ERROR;
158 UScriptCode scriptCode = uscript_getScript(unicode, &status);
160 if (unlikely (U_FAILURE (status)))
161 return HB_SCRIPT_UNKNOWN;
163 return hb_icu_script_to_script (scriptCode);
166 #if U_ICU_VERSION_MAJOR_NUM >= 49
167 static const UNormalizer2 *normalizer;
171 hb_icu_unicode_compose (hb_unicode_funcs_t *ufuncs HB_UNUSED,
175 void *user_data HB_UNUSED)
177 #if U_ICU_VERSION_MAJOR_NUM >= 49
179 UChar32 ret = unorm2_composePair (normalizer, a, b);
180 if (ret < 0) return false;
186 /* We don't ifdef-out the fallback code such that compiler always
187 * sees it and makes sure it's compilable. */
189 UChar utf16[4], normalized[5];
196 U16_APPEND (utf16, len, ARRAY_LENGTH (utf16), a, err);
197 if (err) return false;
198 U16_APPEND (utf16, len, ARRAY_LENGTH (utf16), b, err);
199 if (err) return false;
201 icu_err = U_ZERO_ERROR;
202 len = unorm_normalize (utf16, len, UNORM_NFC, 0, normalized, ARRAY_LENGTH (normalized), &icu_err);
203 if (U_FAILURE (icu_err))
205 if (u_countChar32 (normalized, len) == 1) {
206 U16_GET_UNSAFE (normalized, 0, *ab);
216 hb_icu_unicode_decompose (hb_unicode_funcs_t *ufuncs HB_UNUSED,
220 void *user_data HB_UNUSED)
222 #if U_ICU_VERSION_MAJOR_NUM >= 49
226 UErrorCode icu_err = U_ZERO_ERROR;
227 len = unorm2_getRawDecomposition (normalizer, ab, decomposed,
228 ARRAY_LENGTH (decomposed), &icu_err);
229 if (U_FAILURE (icu_err) || len < 0) return false;
231 len = u_countChar32 (decomposed, len);
233 U16_GET_UNSAFE (decomposed, 0, *a);
236 } else if (len == 2) {
238 U16_NEXT_UNSAFE (decomposed, len, *a);
239 U16_NEXT_UNSAFE (decomposed, len, *b);
245 /* We don't ifdef-out the fallback code such that compiler always
246 * sees it and makes sure it's compilable. */
248 UChar utf16[2], normalized[2 * HB_UNICODE_MAX_DECOMPOSITION_LEN + 1];
253 /* This function is a monster! Maybe it wasn't a good idea adding a
254 * pairwise decompose API... */
255 /* Watchout for the dragons. Err, watchout for macros changing len. */
259 U16_APPEND (utf16, len, ARRAY_LENGTH (utf16), ab, err);
260 if (err) return false;
262 icu_err = U_ZERO_ERROR;
263 len = unorm_normalize (utf16, len, UNORM_NFD, 0, normalized, ARRAY_LENGTH (normalized), &icu_err);
264 if (U_FAILURE (icu_err))
267 len = u_countChar32 (normalized, len);
270 U16_GET_UNSAFE (normalized, 0, *a);
273 } else if (len == 2) {
275 U16_NEXT_UNSAFE (normalized, len, *a);
276 U16_NEXT_UNSAFE (normalized, len, *b);
278 /* Here's the ugly part: if ab decomposes to a single character and
279 * that character decomposes again, we have to detect that and undo
280 * the second part :-(. */
281 UChar recomposed[20];
282 icu_err = U_ZERO_ERROR;
283 unorm_normalize (normalized, len, UNORM_NFC, 0, recomposed, ARRAY_LENGTH (recomposed), &icu_err);
284 if (U_FAILURE (icu_err))
287 U16_GET_UNSAFE (recomposed, 0, c);
288 if (c != *a && c != ab) {
294 /* If decomposed to more than two characters, take the last one,
295 * and recompose the rest to get the first component. */
296 U16_PREV_UNSAFE (normalized, len, *b); /* Changes len in-place. */
297 UChar recomposed[18 * 2];
298 icu_err = U_ZERO_ERROR;
299 len = unorm_normalize (normalized, len, UNORM_NFC, 0, recomposed, ARRAY_LENGTH (recomposed), &icu_err);
300 if (U_FAILURE (icu_err))
302 /* We expect that recomposed has exactly one character now. */
303 if (unlikely (u_countChar32 (recomposed, len) != 1))
305 U16_GET_UNSAFE (recomposed, 0, *a);
313 hb_icu_unicode_decompose_compatibility (hb_unicode_funcs_t *ufuncs HB_UNUSED,
315 hb_codepoint_t *decomposed,
316 void *user_data HB_UNUSED)
318 UChar utf16[2], normalized[2 * HB_UNICODE_MAX_DECOMPOSITION_LEN + 1];
324 /* Copy @u into a UTF-16 array to be passed to ICU. */
327 U16_APPEND (utf16, len, ARRAY_LENGTH (utf16), u, err);
331 /* Normalise the codepoint using NFKD mode. */
332 icu_err = U_ZERO_ERROR;
333 len = unorm_normalize (utf16, len, UNORM_NFKD, 0, normalized, ARRAY_LENGTH (normalized), &icu_err);
337 /* Convert the decomposed form from UTF-16 to UTF-32. */
338 icu_err = U_ZERO_ERROR;
339 u_strToUTF32 ((UChar32*) decomposed, HB_UNICODE_MAX_DECOMPOSITION_LEN, &utf32_len, normalized, len, &icu_err);
348 hb_icu_get_unicode_funcs (void)
350 static const hb_unicode_funcs_t _hb_icu_unicode_funcs = {
351 HB_OBJECT_HEADER_STATIC,
354 true, /* immutable */
356 #define HB_UNICODE_FUNC_IMPLEMENT(name) hb_icu_unicode_##name,
357 HB_UNICODE_FUNCS_IMPLEMENT_CALLBACKS
358 #undef HB_UNICODE_FUNC_IMPLEMENT
362 #if U_ICU_VERSION_MAJOR_NUM >= 49
363 if (!hb_atomic_ptr_get (&normalizer)) {
364 UErrorCode icu_err = U_ZERO_ERROR;
365 /* We ignore failure in getNFCInstace(). */
366 (void) hb_atomic_ptr_cmpexch (&normalizer, NULL, unorm2_getNFCInstance (&icu_err));
369 return const_cast<hb_unicode_funcs_t *> (&_hb_icu_unicode_funcs);