76e1dfd778e2f6c2ce3494fceb0c09c1a828b60a
[framework/uifw/harfbuzz.git] / src / hb-glib.cc
1 /*
2  * Copyright © 2009  Red Hat, Inc.
3  * Copyright © 2011  Google, Inc.
4  *
5  *  This is part of HarfBuzz, a text shaping library.
6  *
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.
12  *
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
17  * DAMAGE.
18  *
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.
24  *
25  * Red Hat Author(s): Behdad Esfahbod
26  * Google Author(s): Behdad Esfahbod
27  */
28
29 #include "hb-private.hh"
30
31 #include "hb-glib.h"
32
33 #include "hb-unicode-private.hh"
34
35 #include <glib.h>
36
37 HB_BEGIN_DECLS
38
39 #if !GLIB_CHECK_VERSION(2,29,14)
40 static const hb_script_t
41 glib_script_to_script[] =
42 {
43   HB_SCRIPT_COMMON,
44   HB_SCRIPT_INHERITED,
45   HB_SCRIPT_ARABIC,
46   HB_SCRIPT_ARMENIAN,
47   HB_SCRIPT_BENGALI,
48   HB_SCRIPT_BOPOMOFO,
49   HB_SCRIPT_CHEROKEE,
50   HB_SCRIPT_COPTIC,
51   HB_SCRIPT_CYRILLIC,
52   HB_SCRIPT_DESERET,
53   HB_SCRIPT_DEVANAGARI,
54   HB_SCRIPT_ETHIOPIC,
55   HB_SCRIPT_GEORGIAN,
56   HB_SCRIPT_GOTHIC,
57   HB_SCRIPT_GREEK,
58   HB_SCRIPT_GUJARATI,
59   HB_SCRIPT_GURMUKHI,
60   HB_SCRIPT_HAN,
61   HB_SCRIPT_HANGUL,
62   HB_SCRIPT_HEBREW,
63   HB_SCRIPT_HIRAGANA,
64   HB_SCRIPT_KANNADA,
65   HB_SCRIPT_KATAKANA,
66   HB_SCRIPT_KHMER,
67   HB_SCRIPT_LAO,
68   HB_SCRIPT_LATIN,
69   HB_SCRIPT_MALAYALAM,
70   HB_SCRIPT_MONGOLIAN,
71   HB_SCRIPT_MYANMAR,
72   HB_SCRIPT_OGHAM,
73   HB_SCRIPT_OLD_ITALIC,
74   HB_SCRIPT_ORIYA,
75   HB_SCRIPT_RUNIC,
76   HB_SCRIPT_SINHALA,
77   HB_SCRIPT_SYRIAC,
78   HB_SCRIPT_TAMIL,
79   HB_SCRIPT_TELUGU,
80   HB_SCRIPT_THAANA,
81   HB_SCRIPT_THAI,
82   HB_SCRIPT_TIBETAN,
83   HB_SCRIPT_CANADIAN_ABORIGINAL,
84   HB_SCRIPT_YI,
85   HB_SCRIPT_TAGALOG,
86   HB_SCRIPT_HANUNOO,
87   HB_SCRIPT_BUHID,
88   HB_SCRIPT_TAGBANWA,
89
90   /* Unicode-4.0 additions */
91   HB_SCRIPT_BRAILLE,
92   HB_SCRIPT_CYPRIOT,
93   HB_SCRIPT_LIMBU,
94   HB_SCRIPT_OSMANYA,
95   HB_SCRIPT_SHAVIAN,
96   HB_SCRIPT_LINEAR_B,
97   HB_SCRIPT_TAI_LE,
98   HB_SCRIPT_UGARITIC,
99
100   /* Unicode-4.1 additions */
101   HB_SCRIPT_NEW_TAI_LUE,
102   HB_SCRIPT_BUGINESE,
103   HB_SCRIPT_GLAGOLITIC,
104   HB_SCRIPT_TIFINAGH,
105   HB_SCRIPT_SYLOTI_NAGRI,
106   HB_SCRIPT_OLD_PERSIAN,
107   HB_SCRIPT_KHAROSHTHI,
108
109   /* Unicode-5.0 additions */
110   HB_SCRIPT_UNKNOWN,
111   HB_SCRIPT_BALINESE,
112   HB_SCRIPT_CUNEIFORM,
113   HB_SCRIPT_PHOENICIAN,
114   HB_SCRIPT_PHAGS_PA,
115   HB_SCRIPT_NKO,
116
117   /* Unicode-5.1 additions */
118   HB_SCRIPT_KAYAH_LI,
119   HB_SCRIPT_LEPCHA,
120   HB_SCRIPT_REJANG,
121   HB_SCRIPT_SUNDANESE,
122   HB_SCRIPT_SAURASHTRA,
123   HB_SCRIPT_CHAM,
124   HB_SCRIPT_OL_CHIKI,
125   HB_SCRIPT_VAI,
126   HB_SCRIPT_CARIAN,
127   HB_SCRIPT_LYCIAN,
128   HB_SCRIPT_LYDIAN,
129
130   /* Unicode-5.2 additions */
131   HB_SCRIPT_AVESTAN,
132   HB_SCRIPT_BAMUM,
133   HB_SCRIPT_EGYPTIAN_HIEROGLYPHS,
134   HB_SCRIPT_IMPERIAL_ARAMAIC,
135   HB_SCRIPT_INSCRIPTIONAL_PAHLAVI,
136   HB_SCRIPT_INSCRIPTIONAL_PARTHIAN,
137   HB_SCRIPT_JAVANESE,
138   HB_SCRIPT_KAITHI,
139   HB_SCRIPT_TAI_THAM,
140   HB_SCRIPT_LISU,
141   HB_SCRIPT_MEETEI_MAYEK,
142   HB_SCRIPT_OLD_SOUTH_ARABIAN,
143   HB_SCRIPT_OLD_TURKIC,
144   HB_SCRIPT_SAMARITAN,
145   HB_SCRIPT_TAI_VIET,
146
147   /* Unicode-6.0 additions */
148   HB_SCRIPT_BATAK,
149   HB_SCRIPT_BRAHMI,
150   HB_SCRIPT_MANDAIC
151 };
152 #endif
153
154 hb_script_t
155 hb_glib_script_to_script (GUnicodeScript script)
156 {
157 #if GLIB_CHECK_VERSION(2,29,14)
158   return (hb_script_t) g_unicode_script_to_iso15924 (script);
159 #else
160   if (likely ((unsigned int) script < ARRAY_LENGTH (glib_script_to_script)))
161     return glib_script_to_script[script];
162
163   if (unlikely (script == G_UNICODE_SCRIPT_INVALID_CODE))
164     return HB_SCRIPT_INVALID;
165
166   return HB_SCRIPT_UNKNOWN;
167 #endif
168 }
169
170 GUnicodeScript
171 hb_glib_script_from_script (hb_script_t script)
172 {
173 #if GLIB_CHECK_VERSION(2,29,14)
174   return g_unicode_script_from_iso15924 (script);
175 #else
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;
180
181   if (unlikely (script == HB_SCRIPT_INVALID))
182     return G_UNICODE_SCRIPT_INVALID_CODE;
183
184   return G_UNICODE_SCRIPT_UNKNOWN;
185 #endif
186 }
187
188
189 static unsigned int
190 hb_glib_unicode_combining_class (hb_unicode_funcs_t *ufuncs HB_UNUSED,
191                                  hb_codepoint_t      unicode,
192                                  void               *user_data HB_UNUSED)
193
194 {
195   return g_unichar_combining_class (unicode);
196 }
197
198 static unsigned int
199 hb_glib_unicode_eastasian_width (hb_unicode_funcs_t *ufuncs HB_UNUSED,
200                                  hb_codepoint_t      unicode,
201                                  void               *user_data HB_UNUSED)
202 {
203   return g_unichar_iswide (unicode) ? 2 : 1;
204 }
205
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)
210
211 {
212   /* hb_unicode_general_category_t and GUnicodeType are identical */
213   return (hb_unicode_general_category_t) g_unichar_type (unicode);
214 }
215
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)
220 {
221   g_unichar_get_mirror_char (unicode, &unicode);
222   return unicode;
223 }
224
225 static hb_script_t
226 hb_glib_unicode_script (hb_unicode_funcs_t *ufuncs HB_UNUSED,
227                         hb_codepoint_t      unicode,
228                         void               *user_data HB_UNUSED)
229 {
230   return hb_glib_script_to_script (g_unichar_get_script (unicode));
231 }
232
233 static hb_bool_t
234 hb_glib_unicode_compose (hb_unicode_funcs_t *ufuncs HB_UNUSED,
235                          hb_codepoint_t      a,
236                          hb_codepoint_t      b,
237                          hb_codepoint_t     *ab,
238                          void               *user_data HB_UNUSED)
239 {
240 #if GLIB_CHECK_VERSION(2,29,12)
241   return g_unichar_compose (a, b, ab);
242 #endif
243
244   /* We don't ifdef-out the fallback code such that compiler always
245    * sees it and makes sure it's compilable. */
246
247   if (!a || !b)
248     return FALSE;
249
250   gchar utf8[12];
251   gchar *normalized;
252   gint len;
253   hb_bool_t ret;
254
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);
258
259   len = g_utf8_strlen (normalized, -1);
260   if (len == 1) {
261     *ab = g_utf8_get_char (normalized);
262     ret = TRUE;
263   } else {
264     ret = FALSE;
265   }
266
267   g_free (normalized);
268   return ret;
269 }
270
271 static hb_bool_t
272 hb_glib_unicode_decompose (hb_unicode_funcs_t *ufuncs HB_UNUSED,
273                            hb_codepoint_t      ab,
274                            hb_codepoint_t     *a,
275                            hb_codepoint_t     *b,
276                            void               *user_data HB_UNUSED)
277 {
278 #if GLIB_CHECK_VERSION(2,29,12)
279   return g_unichar_decompose (ab, a, b);
280 #endif
281
282   /* We don't ifdef-out the fallback code such that compiler always
283    * sees it and makes sure it's compilable. */
284
285   gchar utf8[6];
286   gchar *normalized;
287   gint len;
288   hb_bool_t ret;
289
290   len = g_unichar_to_utf8 (ab, utf8);
291   normalized = g_utf8_normalize (utf8, len, G_NORMALIZE_NFD);
292
293   len = g_utf8_strlen (normalized, -1);
294   if (len == 1) {
295     *a = g_utf8_get_char (normalized);
296     *b = 0;
297     ret = *a != ab;
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) {
307       *a = c;
308       *b = 0;
309     }
310     g_free (recomposed);
311     ret = TRUE;
312   } else {
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);
316     gchar *recomposed;
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);
321     g_free (recomposed);
322     ret = TRUE;
323   }
324
325   g_free (normalized);
326   return ret;
327 }
328
329
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,
333
334   NULL, /* parent */
335   TRUE, /* immutable */
336   {
337 #define HB_UNICODE_FUNC_IMPLEMENT(name) hb_glib_unicode_##name,
338     HB_UNICODE_FUNCS_IMPLEMENT_CALLBACKS
339 #undef HB_UNICODE_FUNC_IMPLEMENT
340   }
341 };
342
343 hb_unicode_funcs_t *
344 hb_glib_get_unicode_funcs (void)
345 {
346   return &_hb_glib_unicode_funcs;
347 }
348
349
350 HB_END_DECLS