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
36 #include "hb-machinery.hh"
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>
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"
54 * @short_description: ICU integration
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
64 * hb_icu_script_to_script:
65 * @script: The UScriptCode identifier to query
67 * Fetches the #hb_script_t script that corresponds to the
68 * specified UScriptCode identifier.
70 * Return value: the #hb_script_t script found
75 hb_icu_script_to_script (UScriptCode script)
77 if (unlikely (script == USCRIPT_INVALID_CODE))
78 return HB_SCRIPT_INVALID;
80 return hb_script_from_string (uscript_getShortName (script), -1);
84 * hb_icu_script_from_script:
85 * @script: The #hb_script_t script to query
87 * Fetches the UScriptCode identifier that corresponds to the
88 * specified #hb_script_t script.
90 * Return value: the UScriptCode identifier found
94 hb_icu_script_from_script (hb_script_t script)
96 if (unlikely (script == HB_SCRIPT_INVALID))
97 return USCRIPT_INVALID_CODE;
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;
104 return USCRIPT_UNKNOWN;
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)
114 return (hb_unicode_combining_class_t) u_getCombiningClass (unicode);
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)
122 switch (u_getIntPropertyValue(unicode, UCHAR_GENERAL_CATEGORY))
124 case U_UNASSIGNED: return HB_UNICODE_GENERAL_CATEGORY_UNASSIGNED;
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;
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;
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;
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;
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;
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;
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;
161 case U_INITIAL_PUNCTUATION: return HB_UNICODE_GENERAL_CATEGORY_INITIAL_PUNCTUATION;
162 case U_FINAL_PUNCTUATION: return HB_UNICODE_GENERAL_CATEGORY_FINAL_PUNCTUATION;
165 return HB_UNICODE_GENERAL_CATEGORY_UNASSIGNED;
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)
173 return u_charMirror(unicode);
177 hb_icu_unicode_script (hb_unicode_funcs_t *ufuncs HB_UNUSED,
178 hb_codepoint_t unicode,
179 void *user_data HB_UNUSED)
181 UErrorCode status = U_ZERO_ERROR;
182 UScriptCode scriptCode = uscript_getScript(unicode, &status);
184 if (unlikely (U_FAILURE (status)))
185 return HB_SCRIPT_UNKNOWN;
187 return hb_icu_script_to_script (scriptCode);
191 hb_icu_unicode_compose (hb_unicode_funcs_t *ufuncs HB_UNUSED,
195 void *user_data HB_UNUSED)
197 #if U_ICU_VERSION_MAJOR_NUM >= 49
199 const UNormalizer2 *normalizer = (const UNormalizer2 *) user_data;
200 UChar32 ret = unorm2_composePair (normalizer, a, b);
201 if (ret < 0) return false;
207 /* We don't ifdef-out the fallback code such that compiler always
208 * sees it and makes sure it's compilable. */
210 UChar utf16[4], normalized[5];
217 U16_APPEND (utf16, len, ARRAY_LENGTH (utf16), a, err);
218 if (err) return false;
219 U16_APPEND (utf16, len, ARRAY_LENGTH (utf16), b, err);
220 if (err) return false;
222 icu_err = U_ZERO_ERROR;
223 len = unorm2_normalize (unorm2_getNFCInstance (&icu_err), utf16, len, normalized, ARRAY_LENGTH (normalized), &icu_err);
224 if (U_FAILURE (icu_err))
226 if (u_countChar32 (normalized, len) == 1) {
227 U16_GET_UNSAFE (normalized, 0, *ab);
237 hb_icu_unicode_decompose (hb_unicode_funcs_t *ufuncs HB_UNUSED,
241 void *user_data HB_UNUSED)
243 #if U_ICU_VERSION_MAJOR_NUM >= 49
245 const UNormalizer2 *normalizer = (const UNormalizer2 *) user_data;
248 UErrorCode icu_err = U_ZERO_ERROR;
249 len = unorm2_getRawDecomposition (normalizer, ab, decomposed,
250 ARRAY_LENGTH (decomposed), &icu_err);
251 if (U_FAILURE (icu_err) || len < 0) return false;
253 len = u_countChar32 (decomposed, len);
255 U16_GET_UNSAFE (decomposed, 0, *a);
258 } else if (len == 2) {
260 U16_NEXT_UNSAFE (decomposed, len, *a);
261 U16_NEXT_UNSAFE (decomposed, len, *b);
267 /* We don't ifdef-out the fallback code such that compiler always
268 * sees it and makes sure it's compilable. */
270 UChar utf16[2], normalized[2 * 19/*HB_UNICODE_MAX_DECOMPOSITION_LEN*/ + 1];
275 /* This function is a monster! Maybe it wasn't a good idea adding a
276 * pairwise decompose API... */
277 /* Watchout for the dragons. Err, watchout for macros changing len. */
281 U16_APPEND (utf16, len, ARRAY_LENGTH (utf16), ab, err);
282 if (err) return false;
284 icu_err = U_ZERO_ERROR;
285 len = unorm2_normalize (unorm2_getNFDInstance (&icu_err), utf16, len, normalized, ARRAY_LENGTH (normalized), &icu_err);
286 if (U_FAILURE (icu_err))
289 len = u_countChar32 (normalized, len);
292 U16_GET_UNSAFE (normalized, 0, *a);
295 } else if (len == 2) {
297 U16_NEXT_UNSAFE (normalized, len, *a);
298 U16_NEXT_UNSAFE (normalized, len, *b);
300 /* Here's the ugly part: if ab decomposes to a single character and
301 * that character decomposes again, we have to detect that and undo
302 * the second part :-(. */
303 UChar recomposed[20];
304 icu_err = U_ZERO_ERROR;
305 unorm2_normalize (unorm2_getNFCInstance (&icu_err), normalized, len, recomposed, ARRAY_LENGTH (recomposed), &icu_err);
306 if (U_FAILURE (icu_err))
309 U16_GET_UNSAFE (recomposed, 0, c);
310 if (c != *a && c != ab) {
316 /* If decomposed to more than two characters, take the last one,
317 * and recompose the rest to get the first component. */
318 U16_PREV_UNSAFE (normalized, len, *b); /* Changes len in-place. */
319 UChar recomposed[18 * 2];
320 icu_err = U_ZERO_ERROR;
321 len = unorm2_normalize (unorm2_getNFCInstance (&icu_err), normalized, len, recomposed, ARRAY_LENGTH (recomposed), &icu_err);
322 if (U_FAILURE (icu_err))
324 /* We expect that recomposed has exactly one character now. */
325 if (unlikely (u_countChar32 (recomposed, len) != 1))
327 U16_GET_UNSAFE (recomposed, 0, *a);
336 static void free_static_icu_funcs ();
339 static struct hb_icu_unicode_funcs_lazy_loader_t : hb_unicode_funcs_lazy_loader_t<hb_icu_unicode_funcs_lazy_loader_t>
341 static hb_unicode_funcs_t *create ()
343 void *user_data = nullptr;
344 #if U_ICU_VERSION_MAJOR_NUM >= 49
345 UErrorCode icu_err = U_ZERO_ERROR;
346 user_data = (void *) unorm2_getNFCInstance (&icu_err);
350 hb_unicode_funcs_t *funcs = hb_unicode_funcs_create (nullptr);
352 hb_unicode_funcs_set_combining_class_func (funcs, hb_icu_unicode_combining_class, nullptr, nullptr);
353 hb_unicode_funcs_set_general_category_func (funcs, hb_icu_unicode_general_category, nullptr, nullptr);
354 hb_unicode_funcs_set_mirroring_func (funcs, hb_icu_unicode_mirroring, nullptr, nullptr);
355 hb_unicode_funcs_set_script_func (funcs, hb_icu_unicode_script, nullptr, nullptr);
356 hb_unicode_funcs_set_compose_func (funcs, hb_icu_unicode_compose, user_data, nullptr);
357 hb_unicode_funcs_set_decompose_func (funcs, hb_icu_unicode_decompose, user_data, nullptr);
359 hb_unicode_funcs_make_immutable (funcs);
362 atexit (free_static_icu_funcs);
371 void free_static_icu_funcs ()
373 static_icu_funcs.free_instance ();
378 * hb_icu_get_unicode_funcs:
380 * Fetches a Unicode-functions structure that is populated
381 * with the appropriate ICU function for each method.
383 * Return value: (transfer none): a pointer to the #hb_unicode_funcs_t Unicode-functions structure
388 hb_icu_get_unicode_funcs ()
390 return static_icu_funcs.get_unconst ();
393 #ifdef HB_ICU_EXTRA_SEMI_IGNORED
394 #pragma GCC diagnostic pop