a29e105c72a2580966822f147e342f41490ef884
[platform/upstream/libunistring.git] / lib / uninorm / decomposition.c
1 /* Decomposition of Unicode characters.
2    Copyright (C) 2009-2010 Free Software Foundation, Inc.
3    Written by Bruno Haible <bruno@clisp.org>, 2009.
4
5    This program is free software: you can redistribute it and/or modify it
6    under the terms of the GNU Lesser General Public License as published
7    by the Free Software Foundation; either version 3 of the License, or
8    (at your option) any later version.
9
10    This program is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    Lesser General Public License for more details.
14
15    You should have received a copy of the GNU Lesser General Public License
16    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
17
18 #include <config.h>
19
20 /* Specification.  */
21 #include "uninorm.h"
22
23 #include "decomposition-table.h"
24
25 int
26 uc_decomposition (ucs4_t uc, int *decomp_tag, ucs4_t *decomposition)
27 {
28   if (uc >= 0xAC00 && uc < 0xD7A4)
29     {
30       /* Hangul syllable.  See Unicode standard, chapter 3, section
31          "Hangul Syllable Decomposition",  See also the clarification at
32          <http://www.unicode.org/versions/Unicode5.1.0/>, section
33          "Clarification of Hangul Jamo Handling".  */
34       unsigned int t;
35
36       uc -= 0xAC00;
37       t = uc % 28;
38
39       *decomp_tag = UC_DECOMP_CANONICAL;
40       if (t == 0)
41         {
42           unsigned int v, l;
43
44           uc = uc / 28;
45           v = uc % 21;
46           l = uc / 21;
47
48           decomposition[0] = 0x1100 + l;
49           decomposition[1] = 0x1161 + v;
50           return 2;
51         }
52       else
53         {
54 #if 1 /* Return the pairwise decomposition, not the full decomposition.  */
55           decomposition[0] = 0xAC00 + uc - t; /* = 0xAC00 + (l * 21 + v) * 28; */
56           decomposition[1] = 0x11A7 + t;
57           return 2;
58 #else
59           unsigned int v, l;
60
61           uc = uc / 28;
62           v = uc % 21;
63           l = uc / 21;
64
65           decomposition[0] = 0x1100 + l;
66           decomposition[1] = 0x1161 + v;
67           decomposition[2] = 0x11A7 + t;
68           return 3;
69 #endif
70         }
71     }
72   else if (uc < 0x110000)
73     {
74       unsigned short entry = decomp_index (uc);
75       if (entry != (unsigned short)(-1))
76         {
77           const unsigned char *p;
78           unsigned int element;
79           unsigned int length;
80
81           p = &gl_uninorm_decomp_chars_table[3 * (entry & 0x7FFF)];
82           element = (p[0] << 16) | (p[1] << 8) | p[2];
83           /* The first element has 5 bits for the decomposition type.  */
84           *decomp_tag = (element >> 18) & 0x1f;
85           length = 1;
86           for (;;)
87             {
88               /* Every element has an 18 bits wide Unicode code point.  */
89               *decomposition = element & 0x3ffff;
90               /* Bit 23 tells whether there are more elements,  */
91               if ((element & (1 << 23)) == 0)
92                 break;
93               p += 3;
94               element = (p[0] << 16) | (p[1] << 8) | p[2];
95               decomposition++;
96               length++;
97             }
98           return length;
99         }
100     }
101   return -1;
102 }