• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
hb_icu_script_to_script(UScriptCode script)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
hb_icu_script_from_script(hb_script_t script)94 hb_icu_script_from_script (hb_script_t script)
95 {
96   UScriptCode out = USCRIPT_INVALID_CODE;
97 
98   if (unlikely (script == HB_SCRIPT_INVALID))
99     return out;
100 
101   UErrorCode icu_err = U_ZERO_ERROR;
102   const unsigned char buf[5] = {HB_UNTAG (script), 0};
103   uscript_getCode ((const char *) buf, &out, 1, &icu_err);
104 
105   return out;
106 }
107 
108 
109 static hb_unicode_combining_class_t
hb_icu_unicode_combining_class(hb_unicode_funcs_t * ufuncs HB_UNUSED,hb_codepoint_t unicode,void * user_data HB_UNUSED)110 hb_icu_unicode_combining_class (hb_unicode_funcs_t *ufuncs HB_UNUSED,
111 				hb_codepoint_t      unicode,
112 				void               *user_data HB_UNUSED)
113 
114 {
115   return (hb_unicode_combining_class_t) u_getCombiningClass (unicode);
116 }
117 
118 static hb_unicode_general_category_t
hb_icu_unicode_general_category(hb_unicode_funcs_t * ufuncs HB_UNUSED,hb_codepoint_t unicode,void * user_data HB_UNUSED)119 hb_icu_unicode_general_category (hb_unicode_funcs_t *ufuncs HB_UNUSED,
120 				 hb_codepoint_t      unicode,
121 				 void               *user_data HB_UNUSED)
122 {
123   switch (u_getIntPropertyValue(unicode, UCHAR_GENERAL_CATEGORY))
124   {
125   case U_UNASSIGNED:			return HB_UNICODE_GENERAL_CATEGORY_UNASSIGNED;
126 
127   case U_UPPERCASE_LETTER:		return HB_UNICODE_GENERAL_CATEGORY_UPPERCASE_LETTER;
128   case U_LOWERCASE_LETTER:		return HB_UNICODE_GENERAL_CATEGORY_LOWERCASE_LETTER;
129   case U_TITLECASE_LETTER:		return HB_UNICODE_GENERAL_CATEGORY_TITLECASE_LETTER;
130   case U_MODIFIER_LETTER:		return HB_UNICODE_GENERAL_CATEGORY_MODIFIER_LETTER;
131   case U_OTHER_LETTER:			return HB_UNICODE_GENERAL_CATEGORY_OTHER_LETTER;
132 
133   case U_NON_SPACING_MARK:		return HB_UNICODE_GENERAL_CATEGORY_NON_SPACING_MARK;
134   case U_ENCLOSING_MARK:		return HB_UNICODE_GENERAL_CATEGORY_ENCLOSING_MARK;
135   case U_COMBINING_SPACING_MARK:	return HB_UNICODE_GENERAL_CATEGORY_SPACING_MARK;
136 
137   case U_DECIMAL_DIGIT_NUMBER:		return HB_UNICODE_GENERAL_CATEGORY_DECIMAL_NUMBER;
138   case U_LETTER_NUMBER:			return HB_UNICODE_GENERAL_CATEGORY_LETTER_NUMBER;
139   case U_OTHER_NUMBER:			return HB_UNICODE_GENERAL_CATEGORY_OTHER_NUMBER;
140 
141   case U_SPACE_SEPARATOR:		return HB_UNICODE_GENERAL_CATEGORY_SPACE_SEPARATOR;
142   case U_LINE_SEPARATOR:		return HB_UNICODE_GENERAL_CATEGORY_LINE_SEPARATOR;
143   case U_PARAGRAPH_SEPARATOR:		return HB_UNICODE_GENERAL_CATEGORY_PARAGRAPH_SEPARATOR;
144 
145   case U_CONTROL_CHAR:			return HB_UNICODE_GENERAL_CATEGORY_CONTROL;
146   case U_FORMAT_CHAR:			return HB_UNICODE_GENERAL_CATEGORY_FORMAT;
147   case U_PRIVATE_USE_CHAR:		return HB_UNICODE_GENERAL_CATEGORY_PRIVATE_USE;
148   case U_SURROGATE:			return HB_UNICODE_GENERAL_CATEGORY_SURROGATE;
149 
150 
151   case U_DASH_PUNCTUATION:		return HB_UNICODE_GENERAL_CATEGORY_DASH_PUNCTUATION;
152   case U_START_PUNCTUATION:		return HB_UNICODE_GENERAL_CATEGORY_OPEN_PUNCTUATION;
153   case U_END_PUNCTUATION:		return HB_UNICODE_GENERAL_CATEGORY_CLOSE_PUNCTUATION;
154   case U_CONNECTOR_PUNCTUATION:		return HB_UNICODE_GENERAL_CATEGORY_CONNECT_PUNCTUATION;
155   case U_OTHER_PUNCTUATION:		return HB_UNICODE_GENERAL_CATEGORY_OTHER_PUNCTUATION;
156 
157   case U_MATH_SYMBOL:			return HB_UNICODE_GENERAL_CATEGORY_MATH_SYMBOL;
158   case U_CURRENCY_SYMBOL:		return HB_UNICODE_GENERAL_CATEGORY_CURRENCY_SYMBOL;
159   case U_MODIFIER_SYMBOL:		return HB_UNICODE_GENERAL_CATEGORY_MODIFIER_SYMBOL;
160   case U_OTHER_SYMBOL:			return HB_UNICODE_GENERAL_CATEGORY_OTHER_SYMBOL;
161 
162   case U_INITIAL_PUNCTUATION:		return HB_UNICODE_GENERAL_CATEGORY_INITIAL_PUNCTUATION;
163   case U_FINAL_PUNCTUATION:		return HB_UNICODE_GENERAL_CATEGORY_FINAL_PUNCTUATION;
164   }
165 
166   return HB_UNICODE_GENERAL_CATEGORY_UNASSIGNED;
167 }
168 
169 static hb_codepoint_t
hb_icu_unicode_mirroring(hb_unicode_funcs_t * ufuncs HB_UNUSED,hb_codepoint_t unicode,void * user_data HB_UNUSED)170 hb_icu_unicode_mirroring (hb_unicode_funcs_t *ufuncs HB_UNUSED,
171 			  hb_codepoint_t      unicode,
172 			  void               *user_data HB_UNUSED)
173 {
174   return u_charMirror(unicode);
175 }
176 
177 static hb_script_t
hb_icu_unicode_script(hb_unicode_funcs_t * ufuncs HB_UNUSED,hb_codepoint_t unicode,void * user_data HB_UNUSED)178 hb_icu_unicode_script (hb_unicode_funcs_t *ufuncs HB_UNUSED,
179 		       hb_codepoint_t      unicode,
180 		       void               *user_data HB_UNUSED)
181 {
182   UErrorCode status = U_ZERO_ERROR;
183   UScriptCode scriptCode = uscript_getScript(unicode, &status);
184 
185   if (unlikely (U_FAILURE (status)))
186     return HB_SCRIPT_UNKNOWN;
187 
188   return hb_icu_script_to_script (scriptCode);
189 }
190 
191 static hb_bool_t
hb_icu_unicode_compose(hb_unicode_funcs_t * ufuncs HB_UNUSED,hb_codepoint_t a,hb_codepoint_t b,hb_codepoint_t * ab,void * user_data)192 hb_icu_unicode_compose (hb_unicode_funcs_t *ufuncs HB_UNUSED,
193 			hb_codepoint_t      a,
194 			hb_codepoint_t      b,
195 			hb_codepoint_t     *ab,
196 			void               *user_data)
197 {
198   const UNormalizer2 *normalizer = (const UNormalizer2 *) user_data;
199   UChar32 ret = unorm2_composePair (normalizer, a, b);
200   if (ret < 0) return false;
201   *ab = ret;
202   return true;
203 }
204 
205 static hb_bool_t
hb_icu_unicode_decompose(hb_unicode_funcs_t * ufuncs HB_UNUSED,hb_codepoint_t ab,hb_codepoint_t * a,hb_codepoint_t * b,void * user_data)206 hb_icu_unicode_decompose (hb_unicode_funcs_t *ufuncs HB_UNUSED,
207 			  hb_codepoint_t      ab,
208 			  hb_codepoint_t     *a,
209 			  hb_codepoint_t     *b,
210 			  void               *user_data)
211 {
212   const UNormalizer2 *normalizer = (const UNormalizer2 *) user_data;
213   UChar decomposed[4];
214   int len;
215   UErrorCode icu_err = U_ZERO_ERROR;
216   len = unorm2_getRawDecomposition (normalizer, ab, decomposed,
217 				    ARRAY_LENGTH (decomposed), &icu_err);
218   if (U_FAILURE (icu_err) || len < 0) return false;
219 
220   len = u_countChar32 (decomposed, len);
221   if (len == 1)
222   {
223     U16_GET_UNSAFE (decomposed, 0, *a);
224     *b = 0;
225     return *a != ab;
226   }
227   else if (len == 2)
228   {
229     len = 0;
230     U16_NEXT_UNSAFE (decomposed, len, *a);
231     U16_NEXT_UNSAFE (decomposed, len, *b);
232   }
233   return true;
234 }
235 
236 
237 static inline void free_static_icu_funcs ();
238 
239 static struct hb_icu_unicode_funcs_lazy_loader_t : hb_unicode_funcs_lazy_loader_t<hb_icu_unicode_funcs_lazy_loader_t>
240 {
createhb_icu_unicode_funcs_lazy_loader_t241   static hb_unicode_funcs_t *create ()
242   {
243     void *user_data = nullptr;
244     UErrorCode icu_err = U_ZERO_ERROR;
245     user_data = (void *) unorm2_getNFCInstance (&icu_err);
246     assert (user_data);
247 
248     hb_unicode_funcs_t *funcs = hb_unicode_funcs_create (nullptr);
249 
250     hb_unicode_funcs_set_combining_class_func (funcs, hb_icu_unicode_combining_class, nullptr, nullptr);
251     hb_unicode_funcs_set_general_category_func (funcs, hb_icu_unicode_general_category, nullptr, nullptr);
252     hb_unicode_funcs_set_mirroring_func (funcs, hb_icu_unicode_mirroring, nullptr, nullptr);
253     hb_unicode_funcs_set_script_func (funcs, hb_icu_unicode_script, nullptr, nullptr);
254     hb_unicode_funcs_set_compose_func (funcs, hb_icu_unicode_compose, user_data, nullptr);
255     hb_unicode_funcs_set_decompose_func (funcs, hb_icu_unicode_decompose, user_data, nullptr);
256 
257     hb_unicode_funcs_make_immutable (funcs);
258 
259     hb_atexit (free_static_icu_funcs);
260 
261     return funcs;
262   }
263 } static_icu_funcs;
264 
265 static inline
free_static_icu_funcs()266 void free_static_icu_funcs ()
267 {
268   static_icu_funcs.free_instance ();
269 }
270 
271 /**
272  * hb_icu_get_unicode_funcs:
273  *
274  * Fetches a Unicode-functions structure that is populated
275  * with the appropriate ICU function for each method.
276  *
277  * Return value: (transfer none): a pointer to the #hb_unicode_funcs_t Unicode-functions structure
278  *
279  * Since: 0.9.38
280  **/
281 hb_unicode_funcs_t *
hb_icu_get_unicode_funcs()282 hb_icu_get_unicode_funcs ()
283 {
284   return static_icu_funcs.get_unconst ();
285 }
286 
287 #ifdef HB_ICU_EXTRA_SEMI_IGNORED
288 #pragma GCC diagnostic pop
289 #endif
290 
291 #endif
292