1 | /*
|
---|
2 | * String folding
|
---|
3 | *
|
---|
4 | * Copyright 2003 Jon Griffiths
|
---|
5 | *
|
---|
6 | * This library is free software; you can redistribute it and/or
|
---|
7 | * modify it under the terms of the GNU Lesser General Public
|
---|
8 | * License as published by the Free Software Foundation; either
|
---|
9 | * version 2.1 of the License, or (at your option) any later version.
|
---|
10 | *
|
---|
11 | * This library is distributed in the hope that it will be useful,
|
---|
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
14 | * Lesser General Public License for more details.
|
---|
15 | *
|
---|
16 | * You should have received a copy of the GNU Lesser General Public
|
---|
17 | * License along with this library; if not, write to the Free Software
|
---|
18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
|
---|
19 | */
|
---|
20 |
|
---|
21 | /*
|
---|
22 | * Sun LGPL Disclaimer: For the avoidance of doubt, except that if any license choice
|
---|
23 | * other than GPL or LGPL is available it will apply instead, Sun elects to use only
|
---|
24 | * the Lesser General Public License version 2.1 (LGPLv2) at this time for any software where
|
---|
25 | * a choice of LGPL license versions is made available with the language indicating
|
---|
26 | * that LGPLv2 or any later version may be used, or where a choice of which version
|
---|
27 | * of the LGPL is applied is otherwise unspecified.
|
---|
28 | */
|
---|
29 |
|
---|
30 | #include "wine/unicode.h"
|
---|
31 |
|
---|
32 | static inline WCHAR to_unicode_digit( WCHAR ch )
|
---|
33 | {
|
---|
34 | extern const WCHAR wine_digitmap[];
|
---|
35 | return ch + wine_digitmap[wine_digitmap[ch >> 8] + (ch & 0xff)];
|
---|
36 | }
|
---|
37 |
|
---|
38 | static inline WCHAR to_unicode_native( WCHAR ch )
|
---|
39 | {
|
---|
40 | extern const WCHAR wine_compatmap[];
|
---|
41 | return ch + wine_compatmap[wine_compatmap[ch >> 8] + (ch & 0xff)];
|
---|
42 | }
|
---|
43 |
|
---|
44 | static const WCHAR wine_ligatures[] =
|
---|
45 | {
|
---|
46 | 0x00c6, 0x00de, 0x00df, 0x00e6, 0x00fe, 0x0132, 0x0133, 0x0152,
|
---|
47 | 0x0153, 0x01c4, 0x01c5, 0x01c6, 0x01c7, 0x01c8, 0x01c9, 0x01ca,
|
---|
48 | 0x01cb, 0x01cc, 0x01e2, 0x01e3, 0x01f1, 0x01f2, 0x01f3, 0x01fc,
|
---|
49 | 0x01fd, 0x05f0, 0x05f1, 0x05f2, 0xfb00, 0xfb01, 0xfb02, 0xfb03,
|
---|
50 | 0xfb04, 0xfb05, 0xfb06
|
---|
51 | };
|
---|
52 |
|
---|
53 | /* Unicode expanded ligatures */
|
---|
54 | static const WCHAR wine_expanded_ligatures[][4] =
|
---|
55 | {
|
---|
56 | { 'A','E','\0',1 },
|
---|
57 | { 'T','H','\0',1 },
|
---|
58 | { 's','s','\0',1 },
|
---|
59 | { 'a','e','\0',1 },
|
---|
60 | { 't','h','\0',1 },
|
---|
61 | { 'I','J','\0',1 },
|
---|
62 | { 'i','j','\0',1 },
|
---|
63 | { 'O','E','\0',1 },
|
---|
64 | { 'o','e','\0',1 },
|
---|
65 | { 'D',0x017d,'\0',1 },
|
---|
66 | { 'D',0x017e,'\0',1 },
|
---|
67 | { 'd',0x017e,'\0',1 },
|
---|
68 | { 'L','J','\0',1 },
|
---|
69 | { 'L','j','\0',1 },
|
---|
70 | { 'l','j','\0',1 },
|
---|
71 | { 'N','J','\0',1 },
|
---|
72 | { 'N','j','\0',1 },
|
---|
73 | { 'n','j','\0',1 },
|
---|
74 | { 0x0100,0x0112,'\0',1 },
|
---|
75 | { 0x0101,0x0113,'\0',1 },
|
---|
76 | { 'D','Z','\0',1 },
|
---|
77 | { 'D','z','\0',1 },
|
---|
78 | { 'd','z','\0',1 },
|
---|
79 | { 0x00c1,0x00c9,'\0',1 },
|
---|
80 | { 0x00e1,0x00e9,'\0',1 },
|
---|
81 | { 0x05d5,0x05d5,'\0',1 },
|
---|
82 | { 0x05d5,0x05d9,'\0',1 },
|
---|
83 | { 0x05d9,0x05d9,'\0',1 },
|
---|
84 | { 'f','f','\0',1 },
|
---|
85 | { 'f','i','\0',1 },
|
---|
86 | { 'f','l','\0',1 },
|
---|
87 | { 'f','f','i',2 },
|
---|
88 | { 'f','f','l',2 },
|
---|
89 | { 0x017f,'t','\0',1 },
|
---|
90 | { 's','t','\0',1 }
|
---|
91 | };
|
---|
92 |
|
---|
93 | static inline int get_ligature_len( WCHAR wc )
|
---|
94 | {
|
---|
95 | int low = 0, high = sizeof(wine_ligatures)/sizeof(WCHAR) -1;
|
---|
96 | while (low <= high)
|
---|
97 | {
|
---|
98 | int pos = (low + high) / 2;
|
---|
99 | if (wine_ligatures[pos] < wc)
|
---|
100 | low = pos + 1;
|
---|
101 | else if (wine_ligatures[pos] > wc)
|
---|
102 | high = pos - 1;
|
---|
103 | else
|
---|
104 | return wine_expanded_ligatures[pos][3];
|
---|
105 | }
|
---|
106 | return 0;
|
---|
107 | }
|
---|
108 |
|
---|
109 | static inline const WCHAR* get_ligature( WCHAR wc )
|
---|
110 | {
|
---|
111 | static const WCHAR empty_ligature[] = { '\0','\0','\0', 0 };
|
---|
112 | int low = 0, high = sizeof(wine_ligatures)/sizeof(WCHAR) -1;
|
---|
113 | while (low <= high)
|
---|
114 | {
|
---|
115 | int pos = (low + high) / 2;
|
---|
116 | if (wine_ligatures[pos] < wc)
|
---|
117 | low = pos + 1;
|
---|
118 | else if (wine_ligatures[pos] > wc)
|
---|
119 | high = pos - 1;
|
---|
120 | else
|
---|
121 | return wine_expanded_ligatures[pos];
|
---|
122 | }
|
---|
123 | return empty_ligature;
|
---|
124 | }
|
---|
125 |
|
---|
126 | /* fold a unicode string */
|
---|
127 | int wine_fold_string( int flags, const WCHAR *src, int srclen, WCHAR *dst, int dstlen )
|
---|
128 | {
|
---|
129 | WCHAR *dstbase = dst;
|
---|
130 | const WCHAR *expand;
|
---|
131 | int i;
|
---|
132 |
|
---|
133 | if (srclen == -1)
|
---|
134 | srclen = strlenW(src) + 1; /* Include terminating NUL in count */
|
---|
135 |
|
---|
136 | if (!dstlen)
|
---|
137 | {
|
---|
138 | /* Calculate the required size for dst */
|
---|
139 | dstlen = srclen;
|
---|
140 |
|
---|
141 | if (flags & MAP_EXPAND_LIGATURES)
|
---|
142 | {
|
---|
143 | while (srclen--)
|
---|
144 | {
|
---|
145 | dstlen += get_ligature_len(*src);
|
---|
146 | src++;
|
---|
147 | }
|
---|
148 | }
|
---|
149 | else if (flags & MAP_COMPOSITE)
|
---|
150 | {
|
---|
151 | /* FIXME */
|
---|
152 | }
|
---|
153 | else if (flags & MAP_PRECOMPOSED)
|
---|
154 | {
|
---|
155 | /* FIXME */
|
---|
156 | }
|
---|
157 | return dstlen;
|
---|
158 | }
|
---|
159 |
|
---|
160 | if (srclen > dstlen)
|
---|
161 | return 0;
|
---|
162 |
|
---|
163 | dstlen -= srclen;
|
---|
164 |
|
---|
165 | /* Actually perform the mapping(s) specified */
|
---|
166 | for (i = 0; i < srclen; i++)
|
---|
167 | {
|
---|
168 | WCHAR ch = *src;
|
---|
169 |
|
---|
170 | if (flags & MAP_EXPAND_LIGATURES)
|
---|
171 | {
|
---|
172 | expand = get_ligature(ch);
|
---|
173 | if (expand[0])
|
---|
174 | {
|
---|
175 | if (!dstlen--)
|
---|
176 | return 0;
|
---|
177 | dst[0] = expand[0];
|
---|
178 | if (expand[2])
|
---|
179 | {
|
---|
180 | if (!dstlen--)
|
---|
181 | return 0;
|
---|
182 | *++dst = expand[1];
|
---|
183 | ch = expand[2];
|
---|
184 | }
|
---|
185 | else
|
---|
186 | ch = expand[1];
|
---|
187 | dst++;
|
---|
188 | }
|
---|
189 | }
|
---|
190 | else if (flags & MAP_COMPOSITE)
|
---|
191 | {
|
---|
192 | /* FIXME */
|
---|
193 | }
|
---|
194 | else if (flags & MAP_PRECOMPOSED)
|
---|
195 | {
|
---|
196 | /* FIXME */
|
---|
197 | }
|
---|
198 | if (flags & MAP_FOLDDIGITS)
|
---|
199 | ch = to_unicode_digit(ch);
|
---|
200 | if (flags & MAP_FOLDCZONE)
|
---|
201 | ch = to_unicode_native(ch);
|
---|
202 |
|
---|
203 | *dst++ = ch;
|
---|
204 | src++;
|
---|
205 | }
|
---|
206 | return dst - dstbase;
|
---|
207 | }
|
---|