Create devel package not to install header and .pc file in binary
[platform/core/uifw/anthy.git] / src-diclib / xchar.c
1 /*
2  * Ê¸»ú(xchar)¤Î¥¿¥¤¥×¤Ê¤É¤ò°·¤¦
3  *
4  * Copyright (C) 2001-2006 TABATA Yusuke
5  */
6 #include <string.h>
7 #include "config.h"
8
9 #include <anthy/xstr.h>
10 #include <anthy/xchar.h>
11
12 #include "diclib_inner.h"
13
14 #define PAGE_SIZE 128
15 #define NR_PAGES 512
16 #include "e2u.h"
17 #include "u2e.h"
18
19 /* this use UCS4 */
20 static struct xchar_ent {
21   const xchar xc;
22   const int type;
23   struct xchar_ent *next;/* hash chain */
24 } xchar_tab[] =
25 {
26   {0xFF40, XCT_OPEN , 0}, /* ¡®, Äɲà*/
27   {0x2032, XCT_CLOSE, 0}, /* ¡ì, Äɲà*/
28   {0x2018, XCT_OPEN , 0}, /* ¡Æ, Äɲà*/
29   {0x2019, XCT_CLOSE, 0}, /* ¡Ç, Äɲà*/
30   {0x201C, XCT_OPEN , 0}, /* ¡È, Äɲà*/
31   {0x201D, XCT_CLOSE, 0}, /* ¡É, Ê¸»ú¥³¡¼¥É¤¬´Ö°ã¤Ã¤Æ¤¤¤ë¤È»×¤ï¤ì¤ë¤Î¤Ç½¤Àµ */
32   {0xff08, XCT_OPEN, 0}, /* ¡Ê¡¡*/
33   {0xff09, XCT_CLOSE, 0}, /* ¡Ë */
34   {0x3014, XCT_OPEN, 0},  /* ¡Ì */
35   {0x3015, XCT_CLOSE, 0}, /* ¡Í */
36   {0xff3b, XCT_OPEN, 0}, /* ¡Î */
37   {0xff3d, XCT_CLOSE, 0}, /* ] */
38   {0xff5b, XCT_OPEN, 0},  /* { */
39   {0xff5d, XCT_CLOSE, 0},  /* ¡Ñ¡¡*/
40   {0x3008, XCT_OPEN, 0},  /* ¡ã¡¡*/
41   {0x3009, XCT_CLOSE, 0},  /* ¡ä¡¡*/
42   {0x300a, XCT_OPEN, 0},  /* ¡Ô¡¡*/
43   {0x300b, XCT_CLOSE, 0},  /* ¡Õ¡¡*/
44   {0x300c, XCT_OPEN, 0},  /* ¡Ö¡¡*/
45   {0x300d, XCT_CLOSE, 0},  /* ¡×¡¡*/
46   {0x300e, XCT_OPEN, 0},  /* ¡Ø¡¡*/
47   {0x300f, XCT_CLOSE, 0},  /* ¡Ù¡¡*/
48   {0x3010, XCT_OPEN, 0},  /* ¡Ú¡¡*/
49   {0x3011, XCT_CLOSE, 0},  /* ¡Û¡¡*/
50   {0x3001, XCT_PUNCTUATION, 0},  /* ¡¢¡¡*/
51   {0x3002, XCT_PUNCTUATION, 0},  /* ¡£¡¡*/
52   {0xff0c, XCT_PUNCTUATION, 0},  /* ¡¤¡¡*/
53   {0xff0e, XCT_PUNCTUATION, 0},  /* ¡¥¡¡*/
54   {0xff1f, XCT_PUNCTUATION, 0},  /* ¡©¡¡*/
55   {0xff01, XCT_PUNCTUATION, 0},  /* ¡ª¡¡*/
56
57   {28, XCT_OPEN, 0}, /* ( */
58   {133, XCT_OPEN, 0}, /* [ */
59   {29, XCT_CLOSE, 0}, /* ) */
60   {135, XCT_CLOSE, 0}, /* ] */
61   {HK_TO, XCT_DEP, 0},/* ¤È */
62   {HK_HA, XCT_DEP, 0},/* ¤Ï */
63   {HK_NO, XCT_DEP, 0},/* ¤Î */
64   {HK_NI, XCT_DEP, 0},/* ¤Ë */
65   {HK_GA, XCT_DEP, 0},/* ¤¬ */
66   {HK_WO, XCT_DEP, 0},/* ¤ò */
67   {WIDE_0, XCT_WIDENUM, 0},
68   {WIDE_1, XCT_WIDENUM, 0},
69   {WIDE_2, XCT_WIDENUM, 0},
70   {WIDE_3, XCT_WIDENUM, 0},
71   {WIDE_4, XCT_WIDENUM, 0},
72   {WIDE_5, XCT_WIDENUM, 0},
73   {WIDE_6, XCT_WIDENUM, 0},
74   {WIDE_7, XCT_WIDENUM, 0},
75   {WIDE_8, XCT_WIDENUM, 0},
76   {WIDE_9, XCT_WIDENUM, 0},
77   {HK_DDOT, XCT_PART, 0},
78   {HK_XA, XCT_PART, 0},
79   {HK_XI, XCT_PART, 0},
80   {HK_XU, XCT_PART, 0},
81   {HK_XE, XCT_PART, 0},
82   {HK_XO, XCT_PART, 0},
83   {HK_XYA, XCT_PART, 0},
84   {HK_XYU, XCT_PART, 0},
85   {HK_XYO, XCT_PART, 0},
86   {HK_TT, XCT_PART, 0},
87   {0, 0, 0},
88 };
89
90 #define DDOT 0x8ede
91 #define CIRCLE 0x8edf
92
93 static const struct half_kana_table half_kana_tab[] = {
94   {HK_A,0x8eb1,0},
95   {HK_I,0x8eb2,0},
96   {HK_U,0x8eb3,0},
97   {HK_E,0x8eb4,0},
98   {HK_O,0x8eb5,0},
99   {HK_KA,0x8eb6,0},
100   {HK_KI,0x8eb7,0},
101   {HK_KU,0x8eb8,0},
102   {HK_KE,0x8eb9,0},
103   {HK_KO,0x8eba,0},
104   {HK_SA,0x8ebb,0},
105   {HK_SI,0x8ebc,0},
106   {HK_SU,0x8ebd,0},
107   {HK_SE,0x8ebe,0},
108   {HK_SO,0x8ebf,0},
109   {HK_TA,0x8ec0,0},
110   {HK_TI,0x8ec1,0},
111   {HK_TU,0x8ec2,0},
112   {HK_TE,0x8ec3,0},
113   {HK_TO,0x8ec4,0},
114   {HK_NA,0x8ec5,0},
115   {HK_NI,0x8ec6,0},
116   {HK_NU,0x8ec7,0},
117   {HK_NE,0x8ec8,0},
118   {HK_NO,0x8ec9,0},
119   {HK_HA,0x8eca,0},
120   {HK_HI,0x8ecb,0},
121   {HK_HU,0x8ecc,0},
122   {HK_HE,0x8ecd,0},
123   {HK_HO,0x8ece,0},
124   {HK_MA,0x8ecf,0},
125   {HK_MI,0x8ed0,0},
126   {HK_MU,0x8ed1,0},
127   {HK_ME,0x8ed2,0},
128   {HK_MO,0x8ed3,0},
129   {HK_YA,0x8ed4,0},
130   {HK_YU,0x8ed5,0},
131   {HK_YO,0x8ed6,0},
132   {HK_RA,0x8ed7,0},
133   {HK_RI,0x8ed8,0},
134   {HK_RU,0x8ed9,0},
135   {HK_RE,0x8eda,0},
136   {HK_RO,0x8edb,0},
137   {HK_WA,0x8edc,0},
138   {HK_WI,0,0},
139   {HK_WE,0,0},
140   {HK_WO,0x8ea6,0},
141   {HK_N,0x8edd,0},
142   {HK_TT,0x8eaf,0},
143   {HK_XA,0x8ea7,0},
144   {HK_XI,0x8ea8,0},
145   {HK_XU,0x8ea9,0},
146   {HK_XE,0x8eaa,0},
147   {HK_XO,0x8eab,0},
148   {HK_GA,0x8eb6,DDOT},
149   {HK_GI,0x8eb7,DDOT},
150   {HK_GU,0x8eb8,DDOT},
151   {HK_GE,0x8eb9,DDOT},
152   {HK_GO,0x8eba,DDOT},
153   {HK_ZA,0x8ebb,DDOT},
154   {HK_ZI,0x8ebc,DDOT},
155   {HK_ZU,0x8ebd,DDOT},
156   {HK_ZE,0x8ebe,DDOT},
157   {HK_ZO,0x8ebf,DDOT},
158   {HK_DA,0x8ec0,DDOT},
159   {HK_DI,0x8ec1,DDOT},
160   {HK_DU,0x8ec2,DDOT},
161   {HK_DE,0x8ec3,DDOT},
162   {HK_DO,0x8ec4,DDOT},
163   {HK_BA,0x8eca,DDOT},
164   {HK_BI,0x8ecb,DDOT},
165   {HK_BU,0x8ecc,DDOT},
166   {HK_BE,0x8ecd,DDOT},
167   {HK_BO,0x8ece,DDOT},
168   {HK_PA,0x8eca,CIRCLE},
169   {HK_PI,0x8ecb,CIRCLE},
170   {HK_PU,0x8ecc,CIRCLE},
171   {HK_PE,0x8ecd,CIRCLE},
172   {HK_PO,0x8ece,CIRCLE},
173   {HK_XYA,0x8eac,0},
174   {HK_XYU,0x8ead,0},
175   {HK_XYO,0x8eae,0},
176   {HK_XWA,0,0},
177   {HK_DDOT,DDOT,0},
178   {HK_BAR,0x8eb0,0},
179   {0,0,0}
180 };
181
182 static const struct half_wide_ent {
183   const xchar half;
184   const xchar wide;
185 } half_wide_tab[] = {
186   {'!', 0xff01},
187   {'\"', 0x201d},
188   {'#', 0xff03},
189   {'$', 0xff04},
190   {'%', 0xff05},
191   {'&', 0xff06},
192   {'\'', 0x2019},
193   {'(', 0xff08},
194   {')', 0xff09},
195   {'*', 0xff0a},
196   {'+', 0xff0b},
197   {',', 0xff0c},
198   {'-', 0xff0d},
199   {'.', 0xff0e},
200   {'/', 0xff0f},
201   {':', 0xff1a},
202   {';', 0xff1b},
203   {'<', 0xff1c},
204   {'=', 0xff1d},
205   {'>', 0xff1e},
206   {'?', 0xff1f},
207   {'@', 0xff20},
208   {'[', 0xff3b},
209   {'\\', 0xff3c},
210   {']', 0xff3d},
211   {'^', 0xff3e},
212   {'_', 0xff3f},
213   {'`', 0xff40},
214   {'{', 0xff5b},
215   {'|', 0xff5c},
216   {'}', 0xff5d},
217   {'~', 0xff5e},
218   {0, 0}
219 };
220
221 xchar
222 anthy_lookup_half_wide(xchar xc)
223 {
224   const struct half_wide_ent *hw;
225   for (hw = half_wide_tab; hw->half; hw ++) {
226     if (hw->half == xc) {
227       return hw->wide;
228     }
229     if (hw->wide == xc) {
230       return hw->half;
231     }
232   }
233   return 0;
234 }
235
236 const struct half_kana_table *
237 anthy_find_half_kana(xchar xc)
238 {
239   const struct half_kana_table *tab;
240   for (tab = half_kana_tab; tab->src; tab ++) {
241     if (tab->src == xc && tab->dst) {
242       return tab;
243     }
244   }
245   return NULL;
246 }
247
248 static int
249 find_xchar_type(xchar xc)
250 {
251   struct xchar_ent *xe = xchar_tab;
252
253   for (; xe->xc; xe++) {
254     if (xe->xc == xc) {
255       return xe->type;
256     }
257   }
258
259   return XCT_NONE;
260 }
261
262 static int
263 is_hira(xchar xc)
264 {
265   if (xc == HK_DDOT) {
266     return 1;
267   }
268   if (xc == HK_BAR) {
269     return 1;
270   }
271   xc = anthy_ucs_to_euc(xc);
272   if ((xc & 0xff00) == 0xa400) {
273     return 1;
274   }
275   return 0;
276 }
277
278 static int
279 is_kata(xchar xc)
280 {
281   if (xc == HK_BAR) {
282     return 1;
283   }
284   xc = anthy_ucs_to_euc(xc);
285   if ((xc & 0xff00) == 0xa500) {
286     return 1;
287   }
288   return 0;
289 }
290
291 static int
292 is_symbol(xchar xc)
293 {
294   if (xc == UCS_GETA) {
295     return 1;
296   }
297   xc = anthy_ucs_to_euc(xc);
298   if (xc == EUC_GETA) {
299     return 0;
300   }
301   if ((xc & 0xff00) == 0xa100) {
302     return 1;
303   }
304   if ((xc & 0xff00) == 0xa200) {
305     return 1;
306   }
307   return 0;
308 }
309
310 static int
311 is_kanji(xchar xc)
312 {
313   if (xc > 0x4e00 && xc < 0xa000) {
314     return 1;
315   }
316   return 0;
317 }
318
319 static int
320 search(const int *tab[], int v, int geta)
321 {
322   int page = v / PAGE_SIZE;
323   int off = v % PAGE_SIZE;
324   const int *t;
325   if (page >= NR_PAGES) {
326     return geta;
327   }
328   t = tab[page];
329   if (!t) {
330     return geta;
331   }
332   if (!t[off] && v) {
333     return geta;
334   }
335   return t[off];
336 }
337
338 int
339 anthy_euc_to_ucs(int ec)
340 {
341   return search(e2u_index, ec, UCS_GETA);
342 }
343
344 int
345 anthy_ucs_to_euc(int uc)
346 {
347   int r = search(u2e_index, uc, EUC_GETA);
348   if (r > 65536) {
349     return EUC_GETA;
350   }
351   return r;
352 }
353
354 int
355 anthy_get_xchar_type(const xchar xc)
356 {
357   int t = find_xchar_type(xc);
358   if (xc > 47 && xc < 58) {
359     t |= XCT_NUM;
360   }
361   if (xc < 128) {
362     t |= XCT_ASCII;
363   }
364   if (is_hira(xc)) {
365     t |= XCT_HIRA;
366   }
367   if (is_kata(xc)) {
368     t |= XCT_KATA;
369   }
370   if (is_symbol(xc)) {
371     if (!(t & XCT_OPEN) && !(t & XCT_CLOSE)) {
372       t |= XCT_SYMBOL;
373     }
374   }
375   if (is_kanji(xc)) {
376     t |= XCT_KANJI;
377   }
378   return t;
379 }
380
381 int
382 anthy_get_xstr_type(const xstr *xs)
383 {
384   int i, t = XCT_ALL;
385   for (i = 0; i < xs->len; i++) {
386     t &= anthy_get_xchar_type(xs->str[i]);
387   }
388   return t;
389 }
390
391 int
392 anthy_xchar_to_num(xchar xc)
393 {
394   switch (xc) {
395   case WIDE_0:return 0;
396   case WIDE_1:return 1;
397   case WIDE_2:return 2;
398   case WIDE_3:return 3;
399   case WIDE_4:return 4;
400   case WIDE_5:return 5;
401   case WIDE_6:return 6;
402   case WIDE_7:return 7;
403   case WIDE_8:return 8;
404   case WIDE_9:return 9;
405   }
406   if (xc >= '0' && xc <= '9') {
407     return xc - (int)'0';
408   }
409   return -1;
410 }
411
412 xchar
413 anthy_xchar_wide_num_to_num(xchar c)
414 {
415   switch (c) {
416   case WIDE_0:return '0';
417   case WIDE_1:return '1';
418   case WIDE_2:return '2';
419   case WIDE_3:return '3';
420   case WIDE_4:return '4';
421   case WIDE_5:return '5';
422   case WIDE_6:return '6';
423   case WIDE_7:return '7';
424   case WIDE_8:return '8';
425   case WIDE_9:return '9';
426   default:return c;
427   }
428 }
429
430 void
431 anthy_init_xchar_tab(void)
432 {
433 }