1 | /* |
2 | * Copyright (C) 2012 Grigori Goronzy <greg@kinoho.net> |
3 | * |
4 | * Permission to use, copy, modify, and/or distribute this software for any |
5 | * purpose with or without fee is hereby granted, provided that the above |
6 | * copyright notice and this permission notice appear in all copies. |
7 | * |
8 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
9 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
10 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
11 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
12 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
13 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
14 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
15 | */ |
16 | |
17 | #include "hb.hh" |
18 | |
19 | #include "hb-unicode.hh" |
20 | #include "hb-machinery.hh" |
21 | |
22 | #include "ucdn.h" |
23 | |
24 | static const hb_script_t ucdn_script_translate[] = |
25 | { |
26 | HB_SCRIPT_COMMON, |
27 | HB_SCRIPT_LATIN, |
28 | HB_SCRIPT_GREEK, |
29 | HB_SCRIPT_CYRILLIC, |
30 | HB_SCRIPT_ARMENIAN, |
31 | HB_SCRIPT_HEBREW, |
32 | HB_SCRIPT_ARABIC, |
33 | HB_SCRIPT_SYRIAC, |
34 | HB_SCRIPT_THAANA, |
35 | HB_SCRIPT_DEVANAGARI, |
36 | HB_SCRIPT_BENGALI, |
37 | HB_SCRIPT_GURMUKHI, |
38 | HB_SCRIPT_GUJARATI, |
39 | HB_SCRIPT_ORIYA, |
40 | HB_SCRIPT_TAMIL, |
41 | HB_SCRIPT_TELUGU, |
42 | HB_SCRIPT_KANNADA, |
43 | HB_SCRIPT_MALAYALAM, |
44 | HB_SCRIPT_SINHALA, |
45 | HB_SCRIPT_THAI, |
46 | HB_SCRIPT_LAO, |
47 | HB_SCRIPT_TIBETAN, |
48 | HB_SCRIPT_MYANMAR, |
49 | HB_SCRIPT_GEORGIAN, |
50 | HB_SCRIPT_HANGUL, |
51 | HB_SCRIPT_ETHIOPIC, |
52 | HB_SCRIPT_CHEROKEE, |
53 | HB_SCRIPT_CANADIAN_SYLLABICS, |
54 | HB_SCRIPT_OGHAM, |
55 | HB_SCRIPT_RUNIC, |
56 | HB_SCRIPT_KHMER, |
57 | HB_SCRIPT_MONGOLIAN, |
58 | HB_SCRIPT_HIRAGANA, |
59 | HB_SCRIPT_KATAKANA, |
60 | HB_SCRIPT_BOPOMOFO, |
61 | HB_SCRIPT_HAN, |
62 | HB_SCRIPT_YI, |
63 | HB_SCRIPT_OLD_ITALIC, |
64 | HB_SCRIPT_GOTHIC, |
65 | HB_SCRIPT_DESERET, |
66 | HB_SCRIPT_INHERITED, |
67 | HB_SCRIPT_TAGALOG, |
68 | HB_SCRIPT_HANUNOO, |
69 | HB_SCRIPT_BUHID, |
70 | HB_SCRIPT_TAGBANWA, |
71 | HB_SCRIPT_LIMBU, |
72 | HB_SCRIPT_TAI_LE, |
73 | HB_SCRIPT_LINEAR_B, |
74 | HB_SCRIPT_UGARITIC, |
75 | HB_SCRIPT_SHAVIAN, |
76 | HB_SCRIPT_OSMANYA, |
77 | HB_SCRIPT_CYPRIOT, |
78 | HB_SCRIPT_BRAILLE, |
79 | HB_SCRIPT_BUGINESE, |
80 | HB_SCRIPT_COPTIC, |
81 | HB_SCRIPT_NEW_TAI_LUE, |
82 | HB_SCRIPT_GLAGOLITIC, |
83 | HB_SCRIPT_TIFINAGH, |
84 | HB_SCRIPT_SYLOTI_NAGRI, |
85 | HB_SCRIPT_OLD_PERSIAN, |
86 | HB_SCRIPT_KHAROSHTHI, |
87 | HB_SCRIPT_BALINESE, |
88 | HB_SCRIPT_CUNEIFORM, |
89 | HB_SCRIPT_PHOENICIAN, |
90 | HB_SCRIPT_PHAGS_PA, |
91 | HB_SCRIPT_NKO, |
92 | HB_SCRIPT_SUNDANESE, |
93 | HB_SCRIPT_LEPCHA, |
94 | HB_SCRIPT_OL_CHIKI, |
95 | HB_SCRIPT_VAI, |
96 | HB_SCRIPT_SAURASHTRA, |
97 | HB_SCRIPT_KAYAH_LI, |
98 | HB_SCRIPT_REJANG, |
99 | HB_SCRIPT_LYCIAN, |
100 | HB_SCRIPT_CARIAN, |
101 | HB_SCRIPT_LYDIAN, |
102 | HB_SCRIPT_CHAM, |
103 | HB_SCRIPT_TAI_THAM, |
104 | HB_SCRIPT_TAI_VIET, |
105 | HB_SCRIPT_AVESTAN, |
106 | HB_SCRIPT_EGYPTIAN_HIEROGLYPHS, |
107 | HB_SCRIPT_SAMARITAN, |
108 | HB_SCRIPT_LISU, |
109 | HB_SCRIPT_BAMUM, |
110 | HB_SCRIPT_JAVANESE, |
111 | HB_SCRIPT_MEETEI_MAYEK, |
112 | HB_SCRIPT_IMPERIAL_ARAMAIC, |
113 | HB_SCRIPT_OLD_SOUTH_ARABIAN, |
114 | HB_SCRIPT_INSCRIPTIONAL_PARTHIAN, |
115 | HB_SCRIPT_INSCRIPTIONAL_PAHLAVI, |
116 | HB_SCRIPT_OLD_TURKIC, |
117 | HB_SCRIPT_KAITHI, |
118 | HB_SCRIPT_BATAK, |
119 | HB_SCRIPT_BRAHMI, |
120 | HB_SCRIPT_MANDAIC, |
121 | HB_SCRIPT_CHAKMA, |
122 | HB_SCRIPT_MEROITIC_CURSIVE, |
123 | HB_SCRIPT_MEROITIC_HIEROGLYPHS, |
124 | HB_SCRIPT_MIAO, |
125 | HB_SCRIPT_SHARADA, |
126 | HB_SCRIPT_SORA_SOMPENG, |
127 | HB_SCRIPT_TAKRI, |
128 | HB_SCRIPT_UNKNOWN, |
129 | HB_SCRIPT_BASSA_VAH, |
130 | HB_SCRIPT_CAUCASIAN_ALBANIAN, |
131 | HB_SCRIPT_DUPLOYAN, |
132 | HB_SCRIPT_ELBASAN, |
133 | HB_SCRIPT_GRANTHA, |
134 | HB_SCRIPT_KHOJKI, |
135 | HB_SCRIPT_KHUDAWADI, |
136 | HB_SCRIPT_LINEAR_A, |
137 | HB_SCRIPT_MAHAJANI, |
138 | HB_SCRIPT_MANICHAEAN, |
139 | HB_SCRIPT_MENDE_KIKAKUI, |
140 | HB_SCRIPT_MODI, |
141 | HB_SCRIPT_MRO, |
142 | HB_SCRIPT_NABATAEAN, |
143 | HB_SCRIPT_OLD_NORTH_ARABIAN, |
144 | HB_SCRIPT_OLD_PERMIC, |
145 | HB_SCRIPT_PAHAWH_HMONG, |
146 | HB_SCRIPT_PALMYRENE, |
147 | HB_SCRIPT_PAU_CIN_HAU, |
148 | HB_SCRIPT_PSALTER_PAHLAVI, |
149 | HB_SCRIPT_SIDDHAM, |
150 | HB_SCRIPT_TIRHUTA, |
151 | HB_SCRIPT_WARANG_CITI, |
152 | HB_SCRIPT_AHOM, |
153 | HB_SCRIPT_ANATOLIAN_HIEROGLYPHS, |
154 | HB_SCRIPT_HATRAN, |
155 | HB_SCRIPT_MULTANI, |
156 | HB_SCRIPT_OLD_HUNGARIAN, |
157 | HB_SCRIPT_SIGNWRITING, |
158 | HB_SCRIPT_ADLAM, |
159 | HB_SCRIPT_BHAIKSUKI, |
160 | HB_SCRIPT_MARCHEN, |
161 | HB_SCRIPT_NEWA, |
162 | HB_SCRIPT_OSAGE, |
163 | HB_SCRIPT_TANGUT, |
164 | HB_SCRIPT_MASARAM_GONDI, |
165 | HB_SCRIPT_NUSHU, |
166 | HB_SCRIPT_SOYOMBO, |
167 | HB_SCRIPT_ZANABAZAR_SQUARE, |
168 | HB_SCRIPT_DOGRA, |
169 | HB_SCRIPT_GUNJALA_GONDI, |
170 | HB_SCRIPT_HANIFI_ROHINGYA, |
171 | HB_SCRIPT_MAKASAR, |
172 | HB_SCRIPT_MEDEFAIDRIN, |
173 | HB_SCRIPT_OLD_SOGDIAN, |
174 | HB_SCRIPT_SOGDIAN, |
175 | }; |
176 | |
177 | static hb_unicode_combining_class_t |
178 | hb_ucdn_combining_class(hb_unicode_funcs_t *ufuncs HB_UNUSED, |
179 | hb_codepoint_t unicode, |
180 | void *user_data HB_UNUSED) |
181 | { |
182 | return (hb_unicode_combining_class_t) ucdn_get_combining_class(unicode); |
183 | } |
184 | |
185 | static unsigned int |
186 | hb_ucdn_eastasian_width(hb_unicode_funcs_t *ufuncs HB_UNUSED, |
187 | hb_codepoint_t unicode, |
188 | void *user_data HB_UNUSED) |
189 | { |
190 | int w = ucdn_get_east_asian_width(unicode); |
191 | return (w == UCDN_EAST_ASIAN_F || w == UCDN_EAST_ASIAN_W) ? 2 : 1; |
192 | } |
193 | |
194 | static hb_unicode_general_category_t |
195 | hb_ucdn_general_category(hb_unicode_funcs_t *ufuncs HB_UNUSED, |
196 | hb_codepoint_t unicode, |
197 | void *user_data HB_UNUSED) |
198 | { |
199 | return (hb_unicode_general_category_t)ucdn_get_general_category(unicode); |
200 | } |
201 | |
202 | static hb_codepoint_t |
203 | hb_ucdn_mirroring(hb_unicode_funcs_t *ufuncs HB_UNUSED, |
204 | hb_codepoint_t unicode, |
205 | void *user_data HB_UNUSED) |
206 | { |
207 | return ucdn_mirror(unicode); |
208 | } |
209 | |
210 | static hb_script_t |
211 | hb_ucdn_script(hb_unicode_funcs_t *ufuncs HB_UNUSED, |
212 | hb_codepoint_t unicode, |
213 | void *user_data HB_UNUSED) |
214 | { |
215 | return ucdn_script_translate[ucdn_get_script(unicode)]; |
216 | } |
217 | |
218 | static hb_bool_t |
219 | hb_ucdn_compose(hb_unicode_funcs_t *ufuncs HB_UNUSED, |
220 | hb_codepoint_t a, hb_codepoint_t b, hb_codepoint_t *ab, |
221 | void *user_data HB_UNUSED) |
222 | { |
223 | return ucdn_compose(ab, a, b); |
224 | } |
225 | |
226 | static hb_bool_t |
227 | hb_ucdn_decompose(hb_unicode_funcs_t *ufuncs HB_UNUSED, |
228 | hb_codepoint_t ab, hb_codepoint_t *a, hb_codepoint_t *b, |
229 | void *user_data HB_UNUSED) |
230 | { |
231 | return ucdn_decompose(ab, a, b); |
232 | } |
233 | |
234 | static unsigned int |
235 | hb_ucdn_decompose_compatibility(hb_unicode_funcs_t *ufuncs HB_UNUSED, |
236 | hb_codepoint_t u, hb_codepoint_t *decomposed, |
237 | void *user_data HB_UNUSED) |
238 | { |
239 | return ucdn_compat_decompose(u, decomposed); |
240 | } |
241 | |
242 | |
243 | static void free_static_ucdn_funcs (void); |
244 | |
245 | static struct hb_ucdn_unicode_funcs_lazy_loader_t : hb_unicode_funcs_lazy_loader_t<hb_ucdn_unicode_funcs_lazy_loader_t> |
246 | { |
247 | static inline hb_unicode_funcs_t *create (void) |
248 | { |
249 | hb_unicode_funcs_t *funcs = hb_unicode_funcs_create (nullptr); |
250 | |
251 | #define HB_UNICODE_FUNC_IMPLEMENT(name) \ |
252 | hb_unicode_funcs_set_##name##_func (funcs, hb_ucdn_##name, nullptr, nullptr); |
253 | HB_UNICODE_FUNCS_IMPLEMENT_CALLBACKS |
254 | #undef HB_UNICODE_FUNC_IMPLEMENT |
255 | |
256 | hb_unicode_funcs_make_immutable (funcs); |
257 | |
258 | #ifdef HB_USE_ATEXIT |
259 | atexit (free_static_ucdn_funcs); |
260 | #endif |
261 | |
262 | return funcs; |
263 | } |
264 | } static_ucdn_funcs; |
265 | |
266 | #ifdef HB_USE_ATEXIT |
267 | static |
268 | void free_static_ucdn_funcs (void) |
269 | { |
270 | static_ucdn_funcs.free_instance (); |
271 | } |
272 | #endif |
273 | |
274 | extern "C" HB_INTERNAL |
275 | hb_unicode_funcs_t * |
276 | hb_ucdn_get_unicode_funcs (void) |
277 | { |
278 | return static_ucdn_funcs.get_unconst (); |
279 | } |
280 | |