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 ICU library to provide Unicode data.
58 **/
59
60hb_script_t
61hb_icu_script_to_script (UScriptCode script)
62{
63 if (unlikely (script == USCRIPT_INVALID_CODE))
64 return HB_SCRIPT_INVALID;
65
66 return hb_script_from_string (uscript_getShortName (script), -1);
67}
68
69UScriptCode
70hb_icu_script_from_script (hb_script_t script)
71{
72 if (unlikely (script == HB_SCRIPT_INVALID))
73 return USCRIPT_INVALID_CODE;
74
75 unsigned int numScriptCode = 1 + u_getIntPropertyMaxValue (UCHAR_SCRIPT);
76 for (unsigned int i = 0; i < numScriptCode; i++)
77 if (unlikely (hb_icu_script_to_script ((UScriptCode) i) == script))
78 return (UScriptCode) i;
79
80 return USCRIPT_UNKNOWN;
81}
82
83
84static hb_unicode_combining_class_t
85hb_icu_unicode_combining_class (hb_unicode_funcs_t *ufuncs HB_UNUSED,
86 hb_codepoint_t unicode,
87 void *user_data HB_UNUSED)
88
89{
90 return (hb_unicode_combining_class_t) u_getCombiningClass (unicode);
91}
92
93static hb_unicode_general_category_t
94hb_icu_unicode_general_category (hb_unicode_funcs_t *ufuncs HB_UNUSED,
95 hb_codepoint_t unicode,
96 void *user_data HB_UNUSED)
97{
98 switch (u_getIntPropertyValue(unicode, UCHAR_GENERAL_CATEGORY))
99 {
100 case U_UNASSIGNED: return HB_UNICODE_GENERAL_CATEGORY_UNASSIGNED;
101
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;
107
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;
111
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;
115
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;
119
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;
124
125
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;
131
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;
136
137 case U_INITIAL_PUNCTUATION: return HB_UNICODE_GENERAL_CATEGORY_INITIAL_PUNCTUATION;
138 case U_FINAL_PUNCTUATION: return HB_UNICODE_GENERAL_CATEGORY_FINAL_PUNCTUATION;
139 }
140
141 return HB_UNICODE_GENERAL_CATEGORY_UNASSIGNED;
142}
143
144static hb_codepoint_t
145hb_icu_unicode_mirroring (hb_unicode_funcs_t *ufuncs HB_UNUSED,
146 hb_codepoint_t unicode,
147 void *user_data HB_UNUSED)
148{
149 return u_charMirror(unicode);
150}
151
152static hb_script_t
153hb_icu_unicode_script (hb_unicode_funcs_t *ufuncs HB_UNUSED,
154 hb_codepoint_t unicode,
155 void *user_data HB_UNUSED)
156{
157 UErrorCode status = U_ZERO_ERROR;
158 UScriptCode scriptCode = uscript_getScript(unicode, &status);
159
160 if (unlikely (U_FAILURE (status)))
161 return HB_SCRIPT_UNKNOWN;
162
163 return hb_icu_script_to_script (scriptCode);
164}
165
166static hb_bool_t
167hb_icu_unicode_compose (hb_unicode_funcs_t *ufuncs HB_UNUSED,
168 hb_codepoint_t a,
169 hb_codepoint_t b,
170 hb_codepoint_t *ab,
171 void *user_data HB_UNUSED)
172{
173#if U_ICU_VERSION_MAJOR_NUM >= 49
174 {
175 const UNormalizer2 *normalizer = (const UNormalizer2 *) user_data;
176 UChar32 ret = unorm2_composePair (normalizer, a, b);
177 if (ret < 0) return false;
178 *ab = ret;
179 return true;
180 }
181#endif
182
183 /* We don't ifdef-out the fallback code such that compiler always
184 * sees it and makes sure it's compilable. */
185
186 UChar utf16[4], normalized[5];
187 unsigned int len;
188 hb_bool_t ret, err;
189 UErrorCode icu_err;
190
191 len = 0;
192 err = false;
193 U16_APPEND (utf16, len, ARRAY_LENGTH (utf16), a, err);
194 if (err) return false;
195 U16_APPEND (utf16, len, ARRAY_LENGTH (utf16), b, err);
196 if (err) return false;
197
198 icu_err = U_ZERO_ERROR;
199 len = unorm2_normalize (unorm2_getNFCInstance (&icu_err), utf16, len, normalized, ARRAY_LENGTH (normalized), &icu_err);
200 if (U_FAILURE (icu_err))
201 return false;
202 if (u_countChar32 (normalized, len) == 1) {
203 U16_GET_UNSAFE (normalized, 0, *ab);
204 ret = true;
205 } else {
206 ret = false;
207 }
208
209 return ret;
210}
211
212static hb_bool_t
213hb_icu_unicode_decompose (hb_unicode_funcs_t *ufuncs HB_UNUSED,
214 hb_codepoint_t ab,
215 hb_codepoint_t *a,
216 hb_codepoint_t *b,
217 void *user_data HB_UNUSED)
218{
219#if U_ICU_VERSION_MAJOR_NUM >= 49
220 {
221 const UNormalizer2 *normalizer = (const UNormalizer2 *) user_data;
222 UChar decomposed[4];
223 int len;
224 UErrorCode icu_err = U_ZERO_ERROR;
225 len = unorm2_getRawDecomposition (normalizer, ab, decomposed,
226 ARRAY_LENGTH (decomposed), &icu_err);
227 if (U_FAILURE (icu_err) || len < 0) return false;
228
229 len = u_countChar32 (decomposed, len);
230 if (len == 1) {
231 U16_GET_UNSAFE (decomposed, 0, *a);
232 *b = 0;
233 return *a != ab;
234 } else if (len == 2) {
235 len = 0;
236 U16_NEXT_UNSAFE (decomposed, len, *a);
237 U16_NEXT_UNSAFE (decomposed, len, *b);
238 }
239 return true;
240 }
241#endif
242
243 /* We don't ifdef-out the fallback code such that compiler always
244 * sees it and makes sure it's compilable. */
245
246 UChar utf16[2], normalized[2 * 19/*HB_UNICODE_MAX_DECOMPOSITION_LEN*/ + 1];
247 unsigned int len;
248 hb_bool_t ret, err;
249 UErrorCode icu_err;
250
251 /* This function is a monster! Maybe it wasn't a good idea adding a
252 * pairwise decompose API... */
253 /* Watchout for the dragons. Err, watchout for macros changing len. */
254
255 len = 0;
256 err = false;
257 U16_APPEND (utf16, len, ARRAY_LENGTH (utf16), ab, err);
258 if (err) return false;
259
260 icu_err = U_ZERO_ERROR;
261 len = unorm2_normalize (unorm2_getNFDInstance (&icu_err), utf16, len, normalized, ARRAY_LENGTH (normalized), &icu_err);
262 if (U_FAILURE (icu_err))
263 return false;
264
265 len = u_countChar32 (normalized, len);
266
267 if (len == 1) {
268 U16_GET_UNSAFE (normalized, 0, *a);
269 *b = 0;
270 ret = *a != ab;
271 } else if (len == 2) {
272 len = 0;
273 U16_NEXT_UNSAFE (normalized, len, *a);
274 U16_NEXT_UNSAFE (normalized, len, *b);
275
276 /* Here's the ugly part: if ab decomposes to a single character and
277 * that character decomposes again, we have to detect that and undo
278 * the second part :-(. */
279 UChar recomposed[20];
280 icu_err = U_ZERO_ERROR;
281 unorm2_normalize (unorm2_getNFCInstance (&icu_err), normalized, len, recomposed, ARRAY_LENGTH (recomposed), &icu_err);
282 if (U_FAILURE (icu_err))
283 return false;
284 hb_codepoint_t c;
285 U16_GET_UNSAFE (recomposed, 0, c);
286 if (c != *a && c != ab) {
287 *a = c;
288 *b = 0;
289 }
290 ret = true;
291 } else {
292 /* If decomposed to more than two characters, take the last one,
293 * and recompose the rest to get the first component. */
294 U16_PREV_UNSAFE (normalized, len, *b); /* Changes len in-place. */
295 UChar recomposed[18 * 2];
296 icu_err = U_ZERO_ERROR;
297 len = unorm2_normalize (unorm2_getNFCInstance (&icu_err), normalized, len, recomposed, ARRAY_LENGTH (recomposed), &icu_err);
298 if (U_FAILURE (icu_err))
299 return false;
300 /* We expect that recomposed has exactly one character now. */
301 if (unlikely (u_countChar32 (recomposed, len) != 1))
302 return false;
303 U16_GET_UNSAFE (recomposed, 0, *a);
304 ret = true;
305 }
306
307 return ret;
308}
309
310
311#if HB_USE_ATEXIT
312static void free_static_icu_funcs ();
313#endif
314
315static struct hb_icu_unicode_funcs_lazy_loader_t : hb_unicode_funcs_lazy_loader_t<hb_icu_unicode_funcs_lazy_loader_t>
316{
317 static hb_unicode_funcs_t *create ()
318 {
319 void *user_data = nullptr;
320#if U_ICU_VERSION_MAJOR_NUM >= 49
321 UErrorCode icu_err = U_ZERO_ERROR;
322 user_data = (void *) unorm2_getNFCInstance (&icu_err);
323 assert (user_data);
324#endif
325
326 hb_unicode_funcs_t *funcs = hb_unicode_funcs_create (nullptr);
327
328 hb_unicode_funcs_set_combining_class_func (funcs, hb_icu_unicode_combining_class, nullptr, nullptr);
329 hb_unicode_funcs_set_general_category_func (funcs, hb_icu_unicode_general_category, nullptr, nullptr);
330 hb_unicode_funcs_set_mirroring_func (funcs, hb_icu_unicode_mirroring, nullptr, nullptr);
331 hb_unicode_funcs_set_script_func (funcs, hb_icu_unicode_script, nullptr, nullptr);
332 hb_unicode_funcs_set_compose_func (funcs, hb_icu_unicode_compose, user_data, nullptr);
333 hb_unicode_funcs_set_decompose_func (funcs, hb_icu_unicode_decompose, user_data, nullptr);
334
335 hb_unicode_funcs_make_immutable (funcs);
336
337#if HB_USE_ATEXIT
338 atexit (free_static_icu_funcs);
339#endif
340
341 return funcs;
342 }
343} static_icu_funcs;
344
345#if HB_USE_ATEXIT
346static
347void free_static_icu_funcs ()
348{
349 static_icu_funcs.free_instance ();
350}
351#endif
352
353hb_unicode_funcs_t *
354hb_icu_get_unicode_funcs ()
355{
356 return static_icu_funcs.get_unconst ();
357}
358
359#ifdef HB_ICU_EXTRA_SEMI_IGNORED
360#pragma GCC diagnostic pop
361#endif
362
363#endif
364