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"
39 #if !GLIB_CHECK_VERSION(2,29,14)
40 static const hb_script_t
41 glib_script_to_script[] =
83 HB_SCRIPT_CANADIAN_ABORIGINAL,
90 /* Unicode-4.0 additions */
100 /* Unicode-4.1 additions */
101 HB_SCRIPT_NEW_TAI_LUE,
103 HB_SCRIPT_GLAGOLITIC,
105 HB_SCRIPT_SYLOTI_NAGRI,
106 HB_SCRIPT_OLD_PERSIAN,
107 HB_SCRIPT_KHAROSHTHI,
109 /* Unicode-5.0 additions */
113 HB_SCRIPT_PHOENICIAN,
117 /* Unicode-5.1 additions */
122 HB_SCRIPT_SAURASHTRA,
130 /* Unicode-5.2 additions */
133 HB_SCRIPT_EGYPTIAN_HIEROGLYPHS,
134 HB_SCRIPT_IMPERIAL_ARAMAIC,
135 HB_SCRIPT_INSCRIPTIONAL_PAHLAVI,
136 HB_SCRIPT_INSCRIPTIONAL_PARTHIAN,
141 HB_SCRIPT_MEETEI_MAYEK,
142 HB_SCRIPT_OLD_SOUTH_ARABIAN,
143 HB_SCRIPT_OLD_TURKIC,
147 /* Unicode-6.0 additions */
155 hb_glib_script_to_script (GUnicodeScript script)
157 #if GLIB_CHECK_VERSION(2,29,14)
158 return (hb_script_t) g_unicode_script_to_iso15924 (script);
160 if (likely ((unsigned int) script < ARRAY_LENGTH (glib_script_to_script)))
161 return glib_script_to_script[script];
163 if (unlikely (script == G_UNICODE_SCRIPT_INVALID_CODE))
164 return HB_SCRIPT_INVALID;
166 return HB_SCRIPT_UNKNOWN;
171 hb_glib_script_from_script (hb_script_t script)
173 #if GLIB_CHECK_VERSION(2,29,14)
174 return g_unicode_script_from_iso15924 (script);
176 unsigned int count = ARRAY_LENGTH (glib_script_to_script);
177 for (unsigned int i = 0; i < count; i++)
178 if (glib_script_to_script[i] == script)
179 return (GUnicodeScript) i;
181 if (unlikely (script == HB_SCRIPT_INVALID))
182 return G_UNICODE_SCRIPT_INVALID_CODE;
184 return G_UNICODE_SCRIPT_UNKNOWN;
190 hb_glib_unicode_combining_class (hb_unicode_funcs_t *ufuncs HB_UNUSED,
191 hb_codepoint_t unicode,
192 void *user_data HB_UNUSED)
195 return g_unichar_combining_class (unicode);
199 hb_glib_unicode_eastasian_width (hb_unicode_funcs_t *ufuncs HB_UNUSED,
200 hb_codepoint_t unicode,
201 void *user_data HB_UNUSED)
203 return g_unichar_iswide (unicode) ? 2 : 1;
206 static hb_unicode_general_category_t
207 hb_glib_unicode_general_category (hb_unicode_funcs_t *ufuncs HB_UNUSED,
208 hb_codepoint_t unicode,
209 void *user_data HB_UNUSED)
212 /* hb_unicode_general_category_t and GUnicodeType are identical */
213 return (hb_unicode_general_category_t) g_unichar_type (unicode);
216 static hb_codepoint_t
217 hb_glib_unicode_mirroring (hb_unicode_funcs_t *ufuncs HB_UNUSED,
218 hb_codepoint_t unicode,
219 void *user_data HB_UNUSED)
221 g_unichar_get_mirror_char (unicode, &unicode);
226 hb_glib_unicode_script (hb_unicode_funcs_t *ufuncs HB_UNUSED,
227 hb_codepoint_t unicode,
228 void *user_data HB_UNUSED)
230 return hb_glib_script_to_script (g_unichar_get_script (unicode));
234 hb_glib_unicode_compose (hb_unicode_funcs_t *ufuncs HB_UNUSED,
238 void *user_data HB_UNUSED)
240 #if GLIB_CHECK_VERSION(2,29,12)
241 return g_unichar_compose (a, b, ab);
244 /* We don't ifdef-out the fallback code such that compiler always
245 * sees it and makes sure it's compilable. */
255 len = g_unichar_to_utf8 (a, utf8);
256 len += g_unichar_to_utf8 (b, utf8 + len);
257 normalized = g_utf8_normalize (utf8, len, G_NORMALIZE_NFC);
259 len = g_utf8_strlen (normalized, -1);
261 *ab = g_utf8_get_char (normalized);
272 hb_glib_unicode_decompose (hb_unicode_funcs_t *ufuncs HB_UNUSED,
276 void *user_data HB_UNUSED)
278 #if GLIB_CHECK_VERSION(2,29,12)
279 return g_unichar_decompose (ab, a, b);
282 /* We don't ifdef-out the fallback code such that compiler always
283 * sees it and makes sure it's compilable. */
290 len = g_unichar_to_utf8 (ab, utf8);
291 normalized = g_utf8_normalize (utf8, len, G_NORMALIZE_NFD);
293 len = g_utf8_strlen (normalized, -1);
295 *a = g_utf8_get_char (normalized);
298 } else if (len == 2) {
299 *a = g_utf8_get_char (normalized);
300 *b = g_utf8_get_char (g_utf8_next_char (normalized));
301 /* Here's the ugly part: if ab decomposes to a single character and
302 * that character decomposes again, we have to detect that and undo
303 * the second part :-(. */
304 gchar *recomposed = g_utf8_normalize (normalized, -1, G_NORMALIZE_NFC);
305 hb_codepoint_t c = g_utf8_get_char (recomposed);
306 if (c != ab && c != *a) {
313 /* If decomposed to more than two characters, take the last one,
314 * and recompose the rest to get the first component. */
315 gchar *end = g_utf8_offset_to_pointer (normalized, len - 1);
317 *b = g_utf8_get_char (end);
318 recomposed = g_utf8_normalize (normalized, end - normalized, G_NORMALIZE_NFC);
319 /* We expect that recomposed has exactly one character now. */
320 *a = g_utf8_get_char (recomposed);
330 extern HB_INTERNAL hb_unicode_funcs_t _hb_unicode_funcs_glib;
331 hb_unicode_funcs_t _hb_glib_unicode_funcs = {
332 HB_OBJECT_HEADER_STATIC,
335 TRUE, /* immutable */
337 #define HB_UNICODE_FUNC_IMPLEMENT(name) hb_glib_unicode_##name,
338 HB_UNICODE_FUNCS_IMPLEMENT_CALLBACKS
339 #undef HB_UNICODE_FUNC_IMPLEMENT
344 hb_glib_get_unicode_funcs (void)
346 return &_hb_glib_unicode_funcs;