2 * Copyright © 2009 Red Hat, Inc.
3 * Copyright © 2011 Google, Inc.
5 * This is part of HarfBuzz, a text shaping library.
7 * Permission is hereby granted, without written agreement and without
8 * license or royalty fees, to use, copy, modify, and distribute this
9 * software and its documentation for any purpose, provided that the
10 * above copyright notice and the following two paragraphs appear in
11 * all copies of this software.
13 * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
14 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
15 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
16 * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
19 * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
20 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
21 * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
22 * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
23 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
25 * Red Hat Author(s): Behdad Esfahbod
26 * Google Author(s): Behdad Esfahbod
29 #include "hb-private.hh"
33 #include "hb-unicode-private.hh"
36 #if !GLIB_CHECK_VERSION(2,29,14)
37 static const hb_script_t
38 glib_script_to_script[] =
80 HB_SCRIPT_CANADIAN_ABORIGINAL,
87 /* Unicode-4.0 additions */
97 /* Unicode-4.1 additions */
98 HB_SCRIPT_NEW_TAI_LUE,
100 HB_SCRIPT_GLAGOLITIC,
102 HB_SCRIPT_SYLOTI_NAGRI,
103 HB_SCRIPT_OLD_PERSIAN,
104 HB_SCRIPT_KHAROSHTHI,
106 /* Unicode-5.0 additions */
110 HB_SCRIPT_PHOENICIAN,
114 /* Unicode-5.1 additions */
119 HB_SCRIPT_SAURASHTRA,
127 /* Unicode-5.2 additions */
130 HB_SCRIPT_EGYPTIAN_HIEROGLYPHS,
131 HB_SCRIPT_IMPERIAL_ARAMAIC,
132 HB_SCRIPT_INSCRIPTIONAL_PAHLAVI,
133 HB_SCRIPT_INSCRIPTIONAL_PARTHIAN,
138 HB_SCRIPT_MEETEI_MAYEK,
139 HB_SCRIPT_OLD_SOUTH_ARABIAN,
140 HB_SCRIPT_OLD_TURKIC,
144 /* Unicode-6.0 additions */
149 /* Unicode-6.1 additions */
151 HB_SCRIPT_MEROITIC_CURSIVE,
152 HB_SCRIPT_MEROITIC_HIEROGLYPHS,
155 HB_SCRIPT_SORA_SOMPENG,
161 hb_glib_script_to_script (GUnicodeScript script)
163 #if GLIB_CHECK_VERSION(2,29,14)
164 return (hb_script_t) g_unicode_script_to_iso15924 (script);
166 if (likely ((unsigned int) script < ARRAY_LENGTH (glib_script_to_script)))
167 return glib_script_to_script[script];
169 if (unlikely (script == G_UNICODE_SCRIPT_INVALID_CODE))
170 return HB_SCRIPT_INVALID;
172 return HB_SCRIPT_UNKNOWN;
177 hb_glib_script_from_script (hb_script_t script)
179 #if GLIB_CHECK_VERSION(2,29,14)
180 return g_unicode_script_from_iso15924 (script);
182 unsigned int count = ARRAY_LENGTH (glib_script_to_script);
183 for (unsigned int i = 0; i < count; i++)
184 if (glib_script_to_script[i] == script)
185 return (GUnicodeScript) i;
187 if (unlikely (script == HB_SCRIPT_INVALID))
188 return G_UNICODE_SCRIPT_INVALID_CODE;
190 return G_UNICODE_SCRIPT_UNKNOWN;
196 hb_glib_unicode_combining_class (hb_unicode_funcs_t *ufuncs HB_UNUSED,
197 hb_codepoint_t unicode,
198 void *user_data HB_UNUSED)
201 return g_unichar_combining_class (unicode);
205 hb_glib_unicode_eastasian_width (hb_unicode_funcs_t *ufuncs HB_UNUSED,
206 hb_codepoint_t unicode,
207 void *user_data HB_UNUSED)
209 return g_unichar_iswide (unicode) ? 2 : 1;
212 static hb_unicode_general_category_t
213 hb_glib_unicode_general_category (hb_unicode_funcs_t *ufuncs HB_UNUSED,
214 hb_codepoint_t unicode,
215 void *user_data HB_UNUSED)
218 /* hb_unicode_general_category_t and GUnicodeType are identical */
219 return (hb_unicode_general_category_t) g_unichar_type (unicode);
222 static hb_codepoint_t
223 hb_glib_unicode_mirroring (hb_unicode_funcs_t *ufuncs HB_UNUSED,
224 hb_codepoint_t unicode,
225 void *user_data HB_UNUSED)
227 g_unichar_get_mirror_char (unicode, &unicode);
232 hb_glib_unicode_script (hb_unicode_funcs_t *ufuncs HB_UNUSED,
233 hb_codepoint_t unicode,
234 void *user_data HB_UNUSED)
236 return hb_glib_script_to_script (g_unichar_get_script (unicode));
240 hb_glib_unicode_compose (hb_unicode_funcs_t *ufuncs HB_UNUSED,
244 void *user_data HB_UNUSED)
246 #if GLIB_CHECK_VERSION(2,29,12)
247 return g_unichar_compose (a, b, ab);
250 /* We don't ifdef-out the fallback code such that compiler always
251 * sees it and makes sure it's compilable. */
261 len = g_unichar_to_utf8 (a, utf8);
262 len += g_unichar_to_utf8 (b, utf8 + len);
263 normalized = g_utf8_normalize (utf8, len, G_NORMALIZE_NFC);
264 len = g_utf8_strlen (normalized, -1);
269 *ab = g_utf8_get_char (normalized);
280 hb_glib_unicode_decompose (hb_unicode_funcs_t *ufuncs HB_UNUSED,
284 void *user_data HB_UNUSED)
286 #if GLIB_CHECK_VERSION(2,29,12)
287 return g_unichar_decompose (ab, a, b);
290 /* We don't ifdef-out the fallback code such that compiler always
291 * sees it and makes sure it's compilable. */
298 len = g_unichar_to_utf8 (ab, utf8);
299 normalized = g_utf8_normalize (utf8, len, G_NORMALIZE_NFD);
300 len = g_utf8_strlen (normalized, -1);
305 *a = g_utf8_get_char (normalized);
308 } else if (len == 2) {
309 *a = g_utf8_get_char (normalized);
310 *b = g_utf8_get_char (g_utf8_next_char (normalized));
311 /* Here's the ugly part: if ab decomposes to a single character and
312 * that character decomposes again, we have to detect that and undo
313 * the second part :-(. */
314 gchar *recomposed = g_utf8_normalize (normalized, -1, G_NORMALIZE_NFC);
315 hb_codepoint_t c = g_utf8_get_char (recomposed);
316 if (c != ab && c != *a) {
323 /* If decomposed to more than two characters, take the last one,
324 * and recompose the rest to get the first component. */
325 gchar *end = g_utf8_offset_to_pointer (normalized, len - 1);
327 *b = g_utf8_get_char (end);
328 recomposed = g_utf8_normalize (normalized, end - normalized, G_NORMALIZE_NFC);
329 /* We expect that recomposed has exactly one character now. */
330 *a = g_utf8_get_char (recomposed);
340 extern HB_INTERNAL const hb_unicode_funcs_t _hb_glib_unicode_funcs;
341 const hb_unicode_funcs_t _hb_glib_unicode_funcs = {
342 HB_OBJECT_HEADER_STATIC,
345 true, /* immutable */
347 #define HB_UNICODE_FUNC_IMPLEMENT(name) hb_glib_unicode_##name,
348 HB_UNICODE_FUNCS_IMPLEMENT_CALLBACKS
349 #undef HB_UNICODE_FUNC_IMPLEMENT
354 hb_glib_get_unicode_funcs (void)
356 return const_cast<hb_unicode_funcs_t *> (&_hb_glib_unicode_funcs);