]>
Commit | Line | Data |
---|---|---|
b75a7d8f A |
1 | /* |
2 | ****************************************************************************** | |
3 | * | |
73c04bcf | 4 | * Copyright (C) 2000-2006, International Business Machines |
b75a7d8f A |
5 | * Corporation and others. All Rights Reserved. |
6 | * | |
7 | ****************************************************************************** | |
8 | * file name: ushape.c | |
9 | * encoding: US-ASCII | |
10 | * tab size: 8 (not used) | |
11 | * indentation:4 | |
12 | * | |
13 | * created on: 2000jun29 | |
14 | * created by: Markus W. Scherer | |
15 | * | |
16 | * Arabic letter shaping implemented by Ayman Roshdy | |
17 | */ | |
18 | ||
19 | #include "unicode/utypes.h" | |
20 | #include "unicode/uchar.h" | |
21 | #include "unicode/ustring.h" | |
b75a7d8f | 22 | #include "unicode/ushape.h" |
374ca955 A |
23 | #include "cmemory.h" |
24 | #include "putilimp.h" | |
b75a7d8f | 25 | #include "ustr_imp.h" |
73c04bcf | 26 | #include "ubidi_props.h" |
b75a7d8f A |
27 | |
28 | #if UTF_SIZE<16 | |
29 | /* | |
30 | * This implementation assumes that the internal encoding is UTF-16 | |
31 | * or UTF-32, not UTF-8. | |
32 | * The main assumption is that the Arabic characters and their | |
33 | * presentation forms each fit into a single UChar. | |
34 | * With UTF-8, they occupy 2 or 3 bytes, and more than the ASCII | |
35 | * characters. | |
36 | */ | |
37 | # error This implementation assumes UTF-16 or UTF-32 (check UTF_SIZE) | |
38 | #endif | |
39 | ||
40 | /* | |
41 | * ### TODO in general for letter shaping: | |
42 | * - the letter shaping code is UTF-16-unaware; needs update | |
43 | * + especially invertBuffer()?! | |
44 | * - needs to handle the "Arabic Tail" that is used in some legacy codepages | |
45 | * as a glyph fragment of wide-glyph letters | |
46 | * + IBM Unicode conversion tables map it to U+200B (ZWSP) | |
47 | * + IBM Egypt has proposed to encode the tail in Unicode among Arabic Presentation Forms | |
48 | */ | |
49 | ||
50 | /* definitions for Arabic letter shaping ------------------------------------ */ | |
51 | ||
52 | #define IRRELEVANT 4 | |
53 | #define LAMTYPE 16 | |
54 | #define ALEFTYPE 32 | |
55 | #define LINKR 1 | |
56 | #define LINKL 2 | |
57 | ||
58 | static const UChar IrrelevantPos[] = { | |
59 | 0x0, 0x2, 0x4, 0x6, | |
60 | 0x8, 0xA, 0xC, 0xE, | |
61 | }; | |
62 | ||
63 | static const UChar convertLamAlef[] = | |
64 | { | |
65 | /*FEF5*/ 0x0622, | |
66 | /*FEF6*/ 0x0622, | |
67 | /*FEF7*/ 0x0623, | |
68 | /*FEF8*/ 0x0623, | |
69 | /*FEF9*/ 0x0625, | |
70 | /*FEFA*/ 0x0625, | |
71 | /*FEFB*/ 0x0627, | |
72 | /*FEFC*/ 0x0627 | |
73 | }; | |
74 | ||
75 | static const UChar araLink[178]= | |
76 | { | |
77 | 1 + 32 + 256 * 0x11,/*0x0622*/ | |
78 | 1 + 32 + 256 * 0x13,/*0x0623*/ | |
79 | 1 + 256 * 0x15,/*0x0624*/ | |
80 | 1 + 32 + 256 * 0x17,/*0x0625*/ | |
81 | 1 + 2 + 256 * 0x19,/*0x0626*/ | |
82 | 1 + 32 + 256 * 0x1D,/*0x0627*/ | |
83 | 1 + 2 + 256 * 0x1F,/*0x0628*/ | |
84 | 1 + 256 * 0x23,/*0x0629*/ | |
85 | 1 + 2 + 256 * 0x25,/*0x062A*/ | |
86 | 1 + 2 + 256 * 0x29,/*0x062B*/ | |
87 | 1 + 2 + 256 * 0x2D,/*0x062C*/ | |
88 | 1 + 2 + 256 * 0x31,/*0x062D*/ | |
89 | 1 + 2 + 256 * 0x35,/*0x062E*/ | |
90 | 1 + 256 * 0x39,/*0x062F*/ | |
91 | 1 + 256 * 0x3B,/*0x0630*/ | |
92 | 1 + 256 * 0x3D,/*0x0631*/ | |
93 | 1 + 256 * 0x3F,/*0x0632*/ | |
94 | 1 + 2 + 256 * 0x41,/*0x0633*/ | |
95 | 1 + 2 + 256 * 0x45,/*0x0634*/ | |
96 | 1 + 2 + 256 * 0x49,/*0x0635*/ | |
97 | 1 + 2 + 256 * 0x4D,/*0x0636*/ | |
98 | 1 + 2 + 256 * 0x51,/*0x0637*/ | |
99 | 1 + 2 + 256 * 0x55,/*0x0638*/ | |
100 | 1 + 2 + 256 * 0x59,/*0x0639*/ | |
101 | 1 + 2 + 256 * 0x5D,/*0x063A*/ | |
102 | 0, 0, 0, 0, 0, /*0x063B-0x063F*/ | |
103 | 1 + 2, /*0x0640*/ | |
104 | 1 + 2 + 256 * 0x61,/*0x0641*/ | |
105 | 1 + 2 + 256 * 0x65,/*0x0642*/ | |
106 | 1 + 2 + 256 * 0x69,/*0x0643*/ | |
107 | 1 + 2 + 16 + 256 * 0x6D,/*0x0644*/ | |
108 | 1 + 2 + 256 * 0x71,/*0x0645*/ | |
109 | 1 + 2 + 256 * 0x75,/*0x0646*/ | |
110 | 1 + 2 + 256 * 0x79,/*0x0647*/ | |
111 | 1 + 256 * 0x7D,/*0x0648*/ | |
112 | 1 + 256 * 0x7F,/*0x0649*/ | |
113 | 1 + 2 + 256 * 0x81,/*0x064A*/ | |
114 | 4, 4, 4, 4, /*0x064B-0x064E*/ | |
115 | 4, 4, 4, 4, /*0x064F-0x0652*/ | |
116 | 4, 4, 4, 0, 0, /*0x0653-0x0657*/ | |
117 | 0, 0, 0, 0, /*0x0658-0x065B*/ | |
118 | 1 + 256 * 0x85,/*0x065C*/ | |
119 | 1 + 256 * 0x87,/*0x065D*/ | |
120 | 1 + 256 * 0x89,/*0x065E*/ | |
121 | 1 + 256 * 0x8B,/*0x065F*/ | |
122 | 0, 0, 0, 0, 0, /*0x0660-0x0664*/ | |
123 | 0, 0, 0, 0, 0, /*0x0665-0x0669*/ | |
124 | 0, 0, 0, 0, 0, 0, /*0x066A-0x066F*/ | |
125 | 4, /*0x0670*/ | |
126 | 0, /*0x0671*/ | |
127 | 1 + 32, /*0x0672*/ | |
128 | 1 + 32, /*0x0673*/ | |
129 | 0, /*0x0674*/ | |
130 | 1 + 32, /*0x0675*/ | |
131 | 1, 1, /*0x0676-0x0677*/ | |
132 | 1+2, 1+2, 1+2, 1+2, 1+2, 1+2, /*0x0678-0x067D*/ | |
133 | 1+2, 1+2, 1+2, 1+2, 1+2, 1+2, /*0x067E-0x0683*/ | |
134 | 1+2, 1+2, 1+2, 1+2, /*0x0684-0x0687*/ | |
135 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /*0x0688-0x0691*/ | |
136 | 1, 1, 1, 1, 1, 1, 1, 1, /*0x0692-0x0699*/ | |
137 | 1+2, 1+2, 1+2, 1+2, 1+2, 1+2, /*0x069A-0x06A3*/ | |
138 | 1+2, 1+2, 1+2, 1+2, /*0x069A-0x06A3*/ | |
139 | 1+2, 1+2, 1+2, 1+2, 1+2, 1+2, /*0x06A4-0x06AD*/ | |
140 | 1+2, 1+2, 1+2, 1+2, /*0x06A4-0x06AD*/ | |
141 | 1+2, 1+2, 1+2, 1+2, 1+2, 1+2, /*0x06AE-0x06B7*/ | |
142 | 1+2, 1+2, 1+2, 1+2, /*0x06AE-0x06B7*/ | |
143 | 1+2, 1+2, 1+2, 1+2, 1+2, 1+2, /*0x06B8-0x06BF*/ | |
144 | 1+2, 1+2, /*0x06B8-0x06BF*/ | |
145 | 1, /*0x06C0*/ | |
146 | 1+2, /*0x06C1*/ | |
147 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /*0x06C2-0x06CB*/ | |
148 | 1+2, /*0x06CC*/ | |
149 | 1, /*0x06CD*/ | |
150 | 1+2, 1+2, 1+2, 1+2, /*0x06CE-0x06D1*/ | |
151 | 1, 1 /*0x06D2-0x06D3*/ | |
152 | }; | |
153 | ||
154 | static const UChar presLink[141]= | |
155 | { | |
156 | 1 + 2, /*0xFE70*/ | |
157 | 1 + 2, /*0xFE71*/ | |
158 | 1 + 2, 0, 1+ 2, 0, 1+ 2, /*0xFE72-0xFE76*/ | |
159 | 1 + 2, /*0xFE77*/ | |
160 | 1+ 2, 1 + 2, 1+2, 1 + 2, /*0xFE78-0xFE81*/ | |
161 | 1+ 2, 1 + 2, 1+2, 1 + 2, /*0xFE82-0xFE85*/ | |
162 | 0, 0 + 32, 1 + 32, 0 + 32, /*0xFE86-0xFE89*/ | |
163 | 1 + 32, 0, 1, 0 + 32, /*0xFE8A-0xFE8D*/ | |
164 | 1 + 32, 0, 2, 1 + 2, /*0xFE8E-0xFE91*/ | |
165 | 1, 0 + 32, 1 + 32, 0, /*0xFE92-0xFE95*/ | |
166 | 2, 1 + 2, 1, 0, /*0xFE96-0xFE99*/ | |
167 | 1, 0, 2, 1 + 2, /*0xFE9A-0xFE9D*/ | |
168 | 1, 0, 2, 1 + 2, /*0xFE9E-0xFEA1*/ | |
169 | 1, 0, 2, 1 + 2, /*0xFEA2-0xFEA5*/ | |
170 | 1, 0, 2, 1 + 2, /*0xFEA6-0xFEA9*/ | |
171 | 1, 0, 2, 1 + 2, /*0xFEAA-0xFEAD*/ | |
172 | 1, 0, 1, 0, /*0xFEAE-0xFEB1*/ | |
173 | 1, 0, 1, 0, /*0xFEB2-0xFEB5*/ | |
174 | 1, 0, 2, 1+2, /*0xFEB6-0xFEB9*/ | |
175 | 1, 0, 2, 1+2, /*0xFEBA-0xFEBD*/ | |
176 | 1, 0, 2, 1+2, /*0xFEBE-0xFEC1*/ | |
177 | 1, 0, 2, 1+2, /*0xFEC2-0xFEC5*/ | |
178 | 1, 0, 2, 1+2, /*0xFEC6-0xFEC9*/ | |
179 | 1, 0, 2, 1+2, /*0xFECA-0xFECD*/ | |
180 | 1, 0, 2, 1+2, /*0xFECE-0xFED1*/ | |
181 | 1, 0, 2, 1+2, /*0xFED2-0xFED5*/ | |
182 | 1, 0, 2, 1+2, /*0xFED6-0xFED9*/ | |
183 | 1, 0, 2, 1+2, /*0xFEDA-0xFEDD*/ | |
184 | 1, 0, 2, 1+2, /*0xFEDE-0xFEE1*/ | |
185 | 1, 0 + 16, 2 + 16, 1 + 2 +16, /*0xFEE2-0xFEE5*/ | |
186 | 1 + 16, 0, 2, 1+2, /*0xFEE6-0xFEE9*/ | |
187 | 1, 0, 2, 1+2, /*0xFEEA-0xFEED*/ | |
188 | 1, 0, 2, 1+2, /*0xFEEE-0xFEF1*/ | |
189 | 1, 0, 1, 0, /*0xFEF2-0xFEF5*/ | |
190 | 1, 0, 2, 1+2, /*0xFEF6-0xFEF9*/ | |
191 | 1, 0, 1, 0, /*0xFEFA-0xFEFD*/ | |
192 | 1, 0, 1, 0, | |
193 | 1 | |
194 | }; | |
195 | ||
196 | static const UChar convertFEto06[] = | |
197 | { | |
198 | /***********0******1******2******3******4******5******6******7******8******9******A******B******C******D******E******F***/ | |
199 | /*FE7*/ 0x64B, 0x64B, 0x64C, 0x64C, 0x64D, 0x64D, 0x64E, 0x64E, 0x64F, 0x64F, 0x650, 0x650, 0x651, 0x651, 0x652, 0x652, | |
200 | /*FE8*/ 0x621, 0x622, 0x622, 0x623, 0x623, 0x624, 0x624, 0x625, 0x625, 0x626, 0x626, 0x626, 0x626, 0x627, 0x627, 0x628, | |
201 | /*FE9*/ 0x628, 0x628, 0x628, 0x629, 0x629, 0x62A, 0x62A, 0x62A, 0x62A, 0x62B, 0x62B, 0x62B, 0x62B, 0x62C, 0x62C, 0x62C, | |
202 | /*FEA*/ 0x62C, 0x62D, 0x62D, 0x62D, 0x62D, 0x62E, 0x62E, 0x62E, 0x62E, 0x62F, 0x62F, 0x630, 0x630, 0x631, 0x631, 0x632, | |
203 | /*FEB*/ 0x632, 0x633, 0x633, 0x633, 0x633, 0x634, 0x634, 0x634, 0x634, 0x635, 0x635, 0x635, 0x635, 0x636, 0x636, 0x636, | |
204 | /*FEC*/ 0x636, 0x637, 0x637, 0x637, 0x637, 0x638, 0x638, 0x638, 0x638, 0x639, 0x639, 0x639, 0x639, 0x63A, 0x63A, 0x63A, | |
205 | /*FED*/ 0x63A, 0x641, 0x641, 0x641, 0x641, 0x642, 0x642, 0x642, 0x642, 0x643, 0x643, 0x643, 0x643, 0x644, 0x644, 0x644, | |
206 | /*FEE*/ 0x644, 0x645, 0x645, 0x645, 0x645, 0x646, 0x646, 0x646, 0x646, 0x647, 0x647, 0x647, 0x647, 0x648, 0x648, 0x649, | |
207 | /*FEF*/ 0x649, 0x64A, 0x64A, 0x64A, 0x64A, 0x65C, 0x65C, 0x65D, 0x65D, 0x65E, 0x65E, 0x65F, 0x65F | |
208 | }; | |
209 | ||
210 | static const UChar shapeTable[4][4][4]= | |
211 | { | |
212 | { {0,0,0,0}, {0,0,0,0}, {0,1,0,3}, {0,1,0,1} }, | |
213 | { {0,0,2,2}, {0,0,1,2}, {0,1,1,2}, {0,1,1,3} }, | |
214 | { {0,0,0,0}, {0,0,0,0}, {0,1,0,3}, {0,1,0,3} }, | |
215 | { {0,0,1,2}, {0,0,1,2}, {0,1,1,2}, {0,1,1,3} } | |
216 | }; | |
217 | ||
218 | /* | |
219 | * This function shapes European digits to Arabic-Indic digits | |
220 | * in-place, writing over the input characters. | |
221 | * Since we know that we are only looking for BMP code points, | |
222 | * we can safely just work with code units (again, at least UTF-16). | |
223 | */ | |
224 | static void | |
225 | _shapeToArabicDigitsWithContext(UChar *s, int32_t length, | |
226 | UChar digitBase, | |
227 | UBool isLogical, UBool lastStrongWasAL) { | |
73c04bcf A |
228 | const UBiDiProps *bdp; |
229 | UErrorCode errorCode; | |
230 | ||
b75a7d8f A |
231 | int32_t i; |
232 | UChar c; | |
233 | ||
73c04bcf A |
234 | errorCode=U_ZERO_ERROR; |
235 | bdp=ubidi_getSingleton(&errorCode); | |
236 | if(U_FAILURE(errorCode)) { | |
237 | return; | |
238 | } | |
239 | ||
b75a7d8f A |
240 | digitBase-=0x30; |
241 | ||
242 | /* the iteration direction depends on the type of input */ | |
243 | if(isLogical) { | |
244 | for(i=0; i<length; ++i) { | |
245 | c=s[i]; | |
73c04bcf | 246 | switch(ubidi_getClass(bdp, c)) { |
b75a7d8f A |
247 | case U_LEFT_TO_RIGHT: /* L */ |
248 | case U_RIGHT_TO_LEFT: /* R */ | |
249 | lastStrongWasAL=FALSE; | |
250 | break; | |
251 | case U_RIGHT_TO_LEFT_ARABIC: /* AL */ | |
252 | lastStrongWasAL=TRUE; | |
253 | break; | |
254 | case U_EUROPEAN_NUMBER: /* EN */ | |
255 | if(lastStrongWasAL && (uint32_t)(c-0x30)<10) { | |
256 | s[i]=(UChar)(digitBase+c); /* digitBase+(c-0x30) - digitBase was modified above */ | |
257 | } | |
258 | break; | |
259 | default : | |
260 | break; | |
261 | } | |
262 | } | |
263 | } else { | |
264 | for(i=length; i>0; /* pre-decrement in the body */) { | |
265 | c=s[--i]; | |
73c04bcf | 266 | switch(ubidi_getClass(bdp, c)) { |
b75a7d8f A |
267 | case U_LEFT_TO_RIGHT: /* L */ |
268 | case U_RIGHT_TO_LEFT: /* R */ | |
269 | lastStrongWasAL=FALSE; | |
270 | break; | |
271 | case U_RIGHT_TO_LEFT_ARABIC: /* AL */ | |
272 | lastStrongWasAL=TRUE; | |
273 | break; | |
274 | case U_EUROPEAN_NUMBER: /* EN */ | |
275 | if(lastStrongWasAL && (uint32_t)(c-0x30)<10) { | |
276 | s[i]=(UChar)(digitBase+c); /* digitBase+(c-0x30) - digitBase was modified above */ | |
277 | } | |
278 | break; | |
279 | default : | |
280 | break; | |
281 | } | |
282 | } | |
283 | } | |
284 | } | |
285 | ||
286 | /* | |
287 | *Name : invertBuffer | |
288 | *Function : This function inverts the buffer, it's used | |
289 | * in case the user specifies the buffer to be | |
290 | * U_SHAPE_TEXT_DIRECTION_LOGICAL | |
291 | */ | |
292 | static void | |
293 | invertBuffer(UChar *buffer,int32_t size,uint32_t options,int32_t *spacesCountl,int32_t *spacesCountr) { | |
294 | ||
295 | UChar temp; | |
296 | int32_t i=0,j=0; | |
297 | int32_t lowlimit = 0, highlimit = 0; | |
298 | ||
299 | lowlimit = *spacesCountl; | |
300 | highlimit = *spacesCountr; | |
301 | ||
302 | for(i=lowlimit,j=size-highlimit-1;i<j;i++,j--) { | |
303 | temp = buffer[i]; | |
304 | buffer[i] = buffer[j]; | |
305 | buffer[j] = temp; | |
306 | } | |
307 | } | |
308 | ||
309 | /* | |
310 | *Name : changeLamAlef | |
311 | *Function : Converts the Alef characters into an equivalent | |
312 | * LamAlef location in the 0x06xx Range, this is an | |
313 | * intermediate stage in the operation of the program | |
314 | * later it'll be converted into the 0xFExx LamAlefs | |
315 | * in the shaping function. | |
316 | */ | |
73c04bcf | 317 | static U_INLINE UChar |
b75a7d8f A |
318 | changeLamAlef(UChar ch) { |
319 | ||
320 | switch(ch) { | |
321 | case 0x0622 : | |
73c04bcf | 322 | return 0x065C; |
b75a7d8f | 323 | case 0x0623 : |
73c04bcf | 324 | return 0x065D; |
b75a7d8f | 325 | case 0x0625 : |
73c04bcf | 326 | return 0x065E; |
b75a7d8f | 327 | case 0x0627 : |
73c04bcf | 328 | return 0x065F; |
b75a7d8f | 329 | } |
73c04bcf | 330 | return 0; |
b75a7d8f A |
331 | } |
332 | ||
333 | /* | |
334 | *Name : specialChar | |
335 | *Function : Special Arabic characters need special handling in the shapeUnicode | |
336 | * function, this function returns 1 or 2 for these special characters | |
337 | */ | |
338 | static int32_t | |
339 | specialChar(UChar ch) { | |
340 | ||
341 | if( (ch>0x0621 && ch<0x0626)||(ch==0x0627)||(ch>0x062e && ch<0x0633)|| | |
342 | (ch>0x0647 && ch<0x064a)||(ch==0x0629) ) { | |
343 | return (1); | |
344 | } | |
73c04bcf | 345 | else if( ch>=0x064B && ch<= 0x0652 ) |
b75a7d8f | 346 | return (2); |
73c04bcf | 347 | else if( (ch>=0x0653 && ch<= 0x0655) || ch == 0x0670 || |
b75a7d8f A |
348 | (ch>=0xFE70 && ch<= 0xFE7F) ) |
349 | return (3); | |
350 | else | |
351 | return (0); | |
352 | } | |
353 | ||
354 | /* | |
355 | *Name : getLink | |
356 | *Function : Resolves the link between the characters as | |
357 | * Arabic characters have four forms : | |
358 | * Isolated, Initial, Middle and Final Form | |
359 | */ | |
360 | static UChar | |
361 | getLink(UChar ch) { | |
362 | ||
363 | if(ch >= 0x0622 && ch <= 0x06D3) { | |
364 | return(araLink[ch-0x0622]); | |
365 | } else if(ch == 0x200D) { | |
366 | return(3); | |
367 | } else if(ch >= 0x206D && ch <= 0x206F) { | |
368 | return(4); | |
369 | } else if(ch >= 0xFE70 && ch <= 0xFEFC) { | |
370 | return(presLink[ch-0xFE70]); | |
371 | } else { | |
372 | return(0); | |
373 | } | |
374 | } | |
375 | ||
376 | /* | |
377 | *Name : countSpaces | |
378 | *Function : Counts the number of spaces | |
379 | * at each end of the logical buffer | |
380 | */ | |
381 | static void | |
382 | countSpaces(UChar *dest,int32_t size,uint32_t options,int32_t *spacesCountl,int32_t *spacesCountr) { | |
383 | ||
384 | int32_t i = 0; | |
385 | int32_t countl = 0,countr = 0; | |
386 | ||
387 | while(dest[i] == 0x0020) { | |
388 | countl++; | |
389 | i++; | |
390 | } | |
391 | while(dest[size-1] == 0x0020) { | |
392 | countr++; | |
393 | size--; | |
394 | } | |
395 | *spacesCountl = countl; | |
396 | *spacesCountr = countr; | |
397 | } | |
398 | ||
399 | /* | |
400 | *Name : isTashkeelChar | |
401 | *Function : Returns 1 for Tashkeel characters else return 0 | |
402 | */ | |
73c04bcf | 403 | static U_INLINE int32_t |
b75a7d8f | 404 | isTashkeelChar(UChar ch) { |
73c04bcf | 405 | return (int32_t)( ch>=0x064B && ch<= 0x0652 ); |
b75a7d8f A |
406 | } |
407 | ||
408 | /* | |
409 | *Name : isAlefChar | |
410 | *Function : Returns 1 for Alef characters else return 0 | |
411 | */ | |
73c04bcf | 412 | static U_INLINE int32_t |
b75a7d8f | 413 | isAlefChar(UChar ch) { |
73c04bcf | 414 | return (int32_t)( (ch==0x0622)||(ch==0x0623)||(ch==0x0625)||(ch==0x0627) ); |
b75a7d8f A |
415 | } |
416 | ||
417 | /* | |
418 | *Name : isLamAlefChar | |
419 | *Function : Returns 1 for LamAlef characters else return 0 | |
420 | */ | |
73c04bcf | 421 | static U_INLINE int32_t |
b75a7d8f | 422 | isLamAlefChar(UChar ch) { |
73c04bcf | 423 | return (int32_t)( (ch>=0xFEF5)&&(ch<=0xFEFC) ); |
b75a7d8f A |
424 | } |
425 | ||
426 | /* | |
427 | *Name : calculateSize | |
428 | *Function : This function calculates the destSize to be used in preflighting | |
429 | * when the destSize is equal to 0 | |
430 | */ | |
431 | static int32_t | |
432 | calculateSize(const UChar *source, int32_t sourceLength, | |
433 | int32_t destSize,uint32_t options) { | |
434 | ||
435 | int32_t i = 0; | |
436 | destSize = sourceLength; | |
437 | ||
438 | switch(options&U_SHAPE_LETTERS_MASK) { | |
439 | ||
440 | case U_SHAPE_LETTERS_SHAPE : | |
441 | if((options&U_SHAPE_TEXT_DIRECTION_MASK)==U_SHAPE_TEXT_DIRECTION_VISUAL_LTR) { | |
442 | for(i=0;i<sourceLength;i++) { | |
443 | if( (isAlefChar(source[i]))&&(source[i+1]==0x0644) ) { | |
444 | destSize--; | |
445 | } | |
446 | } | |
447 | } | |
448 | else | |
449 | if((options&U_SHAPE_TEXT_DIRECTION_MASK)==U_SHAPE_TEXT_DIRECTION_LOGICAL) { | |
450 | for(i=0;i<sourceLength;i++) { | |
451 | if( (isAlefChar(source[i+1]))&&(source[i]==0x0644) ) { | |
452 | destSize--; | |
453 | } | |
454 | } | |
455 | } | |
456 | break; | |
457 | ||
458 | case U_SHAPE_LETTERS_UNSHAPE : | |
459 | for(i=0;i<sourceLength;i++) { | |
460 | if( isLamAlefChar(source[i]) ) { | |
461 | destSize++; | |
462 | } | |
463 | } | |
464 | break; | |
465 | ||
466 | default : | |
467 | /* will never occur because of validity checks at the begin of u_shapeArabic */ | |
468 | break; | |
469 | } | |
470 | ||
471 | return destSize; | |
472 | } | |
473 | ||
474 | /* | |
475 | *Name : removeLamAlefSpaces | |
476 | *Function : The shapeUnicode function converts Lam + Alef into LamAlef + space, | |
477 | * this function removes the spaces behind the LamAlefs according to | |
478 | * the options the user specifies, the spaces are removed to the end | |
479 | * of the buffer, or shrink the buffer ab=nd remove spaces for good | |
480 | * or leave the buffer as it is LamAlef + space. | |
481 | */ | |
482 | static int32_t | |
483 | removeLamAlefSpaces(UChar *dest, int32_t sourceLength, | |
484 | int32_t destSize, | |
485 | uint32_t options, | |
486 | UErrorCode *pErrorCode) { | |
487 | ||
488 | int32_t i = 0, j = 0; | |
489 | int32_t count = 0; | |
490 | UChar *tempbuffer=NULL; | |
491 | ||
492 | switch(options&U_SHAPE_LENGTH_MASK) { | |
493 | case U_SHAPE_LENGTH_GROW_SHRINK : | |
494 | tempbuffer = (UChar *)uprv_malloc((sourceLength+1)*U_SIZEOF_UCHAR); | |
495 | /* Test for NULL */ | |
496 | if(tempbuffer == NULL) { | |
497 | *pErrorCode = U_MEMORY_ALLOCATION_ERROR; | |
498 | return 0; | |
499 | } | |
500 | ||
501 | uprv_memset(tempbuffer, 0, (sourceLength+1)*U_SIZEOF_UCHAR); | |
502 | ||
503 | i = j = 0; | |
504 | while(i < sourceLength) { | |
505 | if(dest[i] == 0xFFFF) { | |
506 | j--; | |
507 | count++; | |
508 | } | |
509 | else | |
510 | tempbuffer[j] = dest[i]; | |
511 | i++; | |
512 | j++; | |
513 | } | |
514 | ||
515 | while(count >= 0) { | |
516 | tempbuffer[i] = 0x0000; | |
517 | i--; | |
518 | count--; | |
519 | } | |
520 | ||
521 | uprv_memcpy(dest, tempbuffer, sourceLength*U_SIZEOF_UCHAR); | |
522 | destSize = u_strlen(dest); | |
523 | break; | |
524 | ||
525 | case U_SHAPE_LENGTH_FIXED_SPACES_NEAR : | |
526 | /* Lam+Alef is already shaped into LamAlef + FFFF */ | |
527 | i = 0; | |
528 | while(i < sourceLength) { | |
529 | if(dest[i] == 0xFFFF) | |
530 | dest[i] = 0x0020; | |
531 | i++; | |
532 | } | |
533 | destSize = sourceLength; | |
534 | break; | |
535 | ||
536 | case U_SHAPE_LENGTH_FIXED_SPACES_AT_BEGINNING : | |
537 | tempbuffer = (UChar *)uprv_malloc((sourceLength+1)*U_SIZEOF_UCHAR); | |
538 | ||
539 | /* Test for NULL */ | |
540 | if(tempbuffer == NULL) { | |
541 | *pErrorCode = U_MEMORY_ALLOCATION_ERROR; | |
542 | return 0; | |
543 | } | |
544 | ||
545 | uprv_memset(tempbuffer, 0, (sourceLength+1)*U_SIZEOF_UCHAR); | |
546 | ||
547 | i = j = sourceLength; | |
548 | while(i >= 0) { | |
549 | if(dest[i] == 0xFFFF) { | |
550 | j++; | |
551 | count++; | |
552 | } | |
553 | else | |
554 | tempbuffer[j] = dest[i]; | |
555 | i--; | |
556 | j--; | |
557 | } | |
558 | for(i=0;i<count;i++) | |
559 | tempbuffer[i] = 0x0020; | |
560 | ||
561 | uprv_memcpy(dest, tempbuffer, sourceLength*U_SIZEOF_UCHAR); | |
562 | destSize = sourceLength; | |
563 | break; | |
564 | ||
565 | case U_SHAPE_LENGTH_FIXED_SPACES_AT_END : | |
566 | tempbuffer = (UChar *)uprv_malloc((sourceLength+1)*U_SIZEOF_UCHAR); | |
567 | ||
568 | /* Test for NULL */ | |
569 | if(tempbuffer == NULL) { | |
570 | *pErrorCode = U_MEMORY_ALLOCATION_ERROR; | |
571 | return 0; | |
572 | } | |
573 | ||
574 | uprv_memset(tempbuffer, 0, (sourceLength+1)*U_SIZEOF_UCHAR); | |
575 | ||
576 | i = j = 0; | |
577 | while(i < sourceLength) { | |
578 | if(dest[i] == 0xFFFF) { | |
579 | j--; | |
580 | count++; | |
581 | } | |
582 | else | |
583 | tempbuffer[j] = dest[i]; | |
584 | i++; | |
585 | j++; | |
586 | } | |
587 | ||
588 | while(count >= 0) { | |
589 | tempbuffer[i] = 0x0020; | |
590 | i--; | |
591 | count--; | |
592 | } | |
593 | ||
594 | uprv_memcpy(dest,tempbuffer, sourceLength*U_SIZEOF_UCHAR); | |
595 | destSize = sourceLength; | |
596 | break; | |
597 | ||
598 | default : | |
599 | /* will not occur */ | |
600 | break; | |
601 | } | |
602 | ||
603 | if(tempbuffer) | |
604 | uprv_free(tempbuffer); | |
605 | ||
606 | return destSize; | |
607 | } | |
608 | ||
609 | /* | |
610 | *Name : expandLamAlef | |
611 | *Function : LamAlef needs special handling as the LamAlef is | |
612 | * one character while expanding it will give two | |
613 | * characters Lam + Alef, so we need to expand the LamAlef | |
614 | * in near or far spaces according to the options the user | |
615 | * specifies or increase the buffer size. | |
616 | * If there are no spaces to expand the LamAlef, an error | |
617 | * will be set to U_NO_SPACE_AVAILABLE as defined in utypes.h | |
618 | */ | |
619 | static int32_t | |
620 | expandLamAlef(UChar *dest, int32_t sourceLength, | |
621 | int32_t destSize,uint32_t options, | |
622 | UErrorCode *pErrorCode) { | |
623 | ||
624 | int32_t i = 0,j = 0; | |
625 | int32_t countl = 0; | |
626 | int32_t countr = 0; | |
627 | int32_t inpsize = sourceLength; | |
628 | UChar lamalefChar; | |
629 | UChar *tempbuffer=NULL; | |
630 | ||
631 | switch(options&U_SHAPE_LENGTH_MASK) { | |
632 | ||
633 | case U_SHAPE_LENGTH_GROW_SHRINK : | |
634 | destSize = calculateSize(dest,sourceLength,destSize,options); | |
635 | tempbuffer = (UChar *)uprv_malloc((destSize+1)*U_SIZEOF_UCHAR); | |
636 | ||
637 | /* Test for NULL */ | |
638 | if(tempbuffer == NULL) { | |
639 | *pErrorCode = U_MEMORY_ALLOCATION_ERROR; | |
640 | return 0; | |
641 | } | |
642 | ||
643 | uprv_memset(tempbuffer, 0, (destSize+1)*U_SIZEOF_UCHAR); | |
644 | ||
645 | i = j = 0; | |
646 | while(i < destSize && j < destSize) { | |
647 | if( isLamAlefChar(dest[i]) ) { | |
648 | tempbuffer[j] = convertLamAlef[ dest[i] - 0xFEF5 ]; | |
649 | tempbuffer[j+1] = 0x0644; | |
650 | j++; | |
651 | } | |
652 | else | |
653 | tempbuffer[j] = dest[i]; | |
654 | i++; | |
655 | j++; | |
656 | } | |
657 | ||
658 | uprv_memcpy(dest, tempbuffer, destSize*U_SIZEOF_UCHAR); | |
659 | break; | |
660 | ||
661 | case U_SHAPE_LENGTH_FIXED_SPACES_NEAR : | |
662 | for(i = 0;i<sourceLength;i++) { | |
663 | if((dest[i] == 0x0020) && isLamAlefChar(dest[i+1])) { | |
664 | lamalefChar = dest[i+1]; | |
665 | dest[i+1] = 0x0644; | |
666 | dest[i] = convertLamAlef[ lamalefChar - 0xFEF5 ]; | |
667 | } | |
668 | else | |
669 | if((dest[i] != 0x0020) && isLamAlefChar(dest[i+1])) { | |
670 | *pErrorCode=U_NO_SPACE_AVAILABLE; | |
671 | } | |
672 | } | |
673 | destSize = sourceLength; | |
674 | break; | |
675 | ||
676 | case U_SHAPE_LENGTH_FIXED_SPACES_AT_BEGINNING : | |
677 | tempbuffer = (UChar *)uprv_malloc((sourceLength+1)*U_SIZEOF_UCHAR); | |
678 | ||
679 | /* Test for NULL */ | |
680 | if(tempbuffer == NULL) { | |
681 | *pErrorCode = U_MEMORY_ALLOCATION_ERROR; | |
682 | return 0; | |
683 | } | |
684 | ||
685 | uprv_memset(tempbuffer, 0, (sourceLength+1)*U_SIZEOF_UCHAR); | |
686 | ||
687 | i = 0; | |
688 | while(dest[i] == 0x0020) { | |
689 | countl++; | |
690 | i++; | |
691 | } | |
692 | ||
693 | i = j = sourceLength-1; | |
694 | while(i >= 0 && j >= 0) { | |
695 | if( countl>0 && isLamAlefChar(dest[i]) ) { | |
696 | tempbuffer[j] = 0x0644; | |
697 | tempbuffer[j-1] = convertLamAlef[ dest[i] - 0xFEF5 ]; | |
698 | j--; | |
699 | countl--; | |
700 | } | |
701 | else { | |
702 | if( countl == 0 && isLamAlefChar(dest[i]) ) | |
703 | *pErrorCode=U_NO_SPACE_AVAILABLE; | |
704 | tempbuffer[j] = dest[i]; | |
705 | } | |
706 | i--; | |
707 | j--; | |
708 | } | |
709 | ||
710 | uprv_memcpy(dest, tempbuffer, sourceLength*U_SIZEOF_UCHAR); | |
711 | destSize = sourceLength; | |
712 | break; | |
713 | ||
714 | case U_SHAPE_LENGTH_FIXED_SPACES_AT_END : | |
715 | /* LamAlef expansion below is done from right to left to make sure that we consume | |
716 | * the spaces with the LamAlefs as they appear in the visual buffer from right to left | |
717 | */ | |
718 | tempbuffer = (UChar *)uprv_malloc((sourceLength+1)*U_SIZEOF_UCHAR); | |
719 | ||
720 | /* Test for NULL */ | |
721 | if(tempbuffer == NULL) { | |
722 | *pErrorCode = U_MEMORY_ALLOCATION_ERROR; | |
723 | return 0; | |
724 | } | |
725 | ||
726 | uprv_memset(tempbuffer, 0, (sourceLength+1)*U_SIZEOF_UCHAR); | |
727 | ||
728 | while(dest[inpsize-1] == 0x0020) { | |
729 | countr++; | |
730 | inpsize--; | |
731 | } | |
732 | ||
733 | i = sourceLength - countr - 1; | |
734 | j = sourceLength - 1; | |
735 | ||
736 | while(i >= 0 && j >= 0) { | |
737 | if( countr>0 && isLamAlefChar(dest[i]) ) { | |
738 | tempbuffer[j] = 0x0644; | |
739 | tempbuffer[j-1] = convertLamAlef[ dest[i] - 0xFEF5 ]; | |
740 | j--; | |
741 | countr--; | |
742 | } | |
743 | else { | |
744 | if( countr == 0 && isLamAlefChar(dest[i]) ) | |
745 | *pErrorCode=U_NO_SPACE_AVAILABLE; | |
746 | tempbuffer[j] = dest[i]; | |
747 | } | |
748 | i--; | |
749 | j--; | |
750 | } | |
751 | ||
752 | if(countr > 0) { | |
753 | uprv_memcpy(tempbuffer, tempbuffer+countr, sourceLength*U_SIZEOF_UCHAR); | |
754 | if(u_strlen(tempbuffer) < sourceLength) { | |
755 | for(i=sourceLength-1;i>=sourceLength-countr;i--) | |
756 | tempbuffer[i] = 0x0020; | |
757 | } | |
758 | } | |
759 | ||
760 | uprv_memcpy(dest, tempbuffer, sourceLength*U_SIZEOF_UCHAR); | |
761 | ||
762 | destSize = sourceLength; | |
763 | break; | |
764 | ||
765 | default : | |
766 | /* will never occur because of validity checks */ | |
767 | break; | |
768 | } | |
769 | ||
770 | if(tempbuffer) | |
771 | uprv_free(tempbuffer); | |
772 | ||
773 | return destSize; | |
774 | } | |
775 | ||
776 | /* | |
777 | *Name : shapeUnicode | |
778 | *Function : Converts an Arabic Unicode buffer in 06xx Range into a shaped | |
779 | * arabic Unicode buffer in FExx Range | |
780 | */ | |
781 | static int32_t | |
782 | shapeUnicode(UChar *dest, int32_t sourceLength, | |
783 | int32_t destSize,uint32_t options, | |
784 | UErrorCode *pErrorCode, | |
785 | int tashkeelFlag) { | |
786 | ||
787 | int32_t i, iend; | |
788 | int32_t step; | |
73c04bcf | 789 | int32_t lastPos,Nx, Nw; |
b75a7d8f A |
790 | unsigned int Shape; |
791 | int32_t flag; | |
792 | int32_t lamalef_found = 0; | |
793 | UChar prevLink = 0, lastLink = 0, currLink, nextLink = 0; | |
794 | UChar wLamalef; | |
795 | ||
796 | /* | |
797 | * Converts the input buffer from FExx Range into 06xx Range | |
798 | * to make sure that all characters are in the 06xx range | |
799 | * even the lamalef is converted to the special region in | |
800 | * the 06xx range | |
801 | */ | |
802 | for (i = 0; i < sourceLength; i++) { | |
803 | UChar inputChar = dest[i]; | |
804 | if ( (inputChar >= 0xFE70) && (inputChar <= 0xFEFC)) { | |
805 | dest[i] = convertFEto06 [ (inputChar - 0xFE70) ] ; | |
806 | } else { | |
807 | dest[i] = inputChar ; | |
808 | } | |
809 | } | |
810 | ||
811 | /* sets the index to the end of the buffer, together with the step point to -1 */ | |
812 | i = sourceLength - 1; | |
813 | iend = -1; | |
814 | step = -1; | |
815 | ||
816 | /* | |
817 | * This function resolves the link between the characters . | |
818 | * Arabic characters have four forms : | |
819 | * Isolated Form, Initial Form, Middle Form and Final Form | |
820 | */ | |
821 | currLink = getLink(dest[i]); | |
822 | ||
b75a7d8f A |
823 | lastPos = i; |
824 | Nx = -2, Nw = 0; | |
825 | ||
826 | while (i != iend) { | |
827 | /* If high byte of currLink > 0 then more than one shape */ | |
828 | if ((currLink & 0xFF00) > 0 || isTashkeelChar(dest[i])) { | |
829 | Nw = i + step; | |
830 | while (Nx < 0) { /* we need to know about next char */ | |
831 | if(Nw == iend) { | |
832 | nextLink = 0; | |
833 | Nx = 3000; | |
834 | } else { | |
835 | nextLink = getLink(dest[Nw]); | |
836 | if((nextLink & IRRELEVANT) == 0) { | |
837 | Nx = Nw; | |
838 | } else { | |
839 | Nw = Nw + step; | |
840 | } | |
841 | } | |
842 | } | |
843 | ||
844 | if ( ((currLink & ALEFTYPE) > 0) && ((lastLink & LAMTYPE) > 0) ) { | |
845 | lamalef_found = 1; | |
846 | wLamalef = changeLamAlef(dest[i]); /*get from 0x065C-0x065f */ | |
847 | if ( wLamalef != 0) { | |
848 | dest[i] = 0xFFFF; /* The default case is to drop the Alef and replace */ | |
849 | dest[lastPos] =wLamalef; /* it by 0xFFFF which is the last character in the */ | |
850 | i=lastPos; /* unicode private use area, this is done to make */ | |
851 | } /* sure that removeLamAlefSpaces() handles only the */ | |
852 | lastLink = prevLink; /* spaces generated during lamalef generation. */ | |
853 | currLink = getLink(wLamalef); /* 0xFFFF is added here and is replaced by spaces */ | |
854 | } /* in removeLamAlefSpaces() */ | |
855 | /* | |
856 | * get the proper shape according to link ability of neighbors | |
857 | * and of character; depends on the order of the shapes | |
858 | * (isolated, initial, middle, final) in the compatibility area | |
859 | */ | |
860 | flag = specialChar(dest[i]); | |
861 | ||
862 | Shape = shapeTable[nextLink & (LINKR + LINKL)] | |
863 | [lastLink & (LINKR + LINKL)] | |
864 | [currLink & (LINKR + LINKL)]; | |
865 | ||
866 | if (flag == 1) { | |
867 | Shape = (Shape == 1 || Shape == 3) ? 1 : 0; | |
868 | } | |
73c04bcf | 869 | else if(flag == 2) { |
b75a7d8f | 870 | if( (lastLink & LINKL) && (nextLink & LINKR) && (tashkeelFlag == 1) && |
73c04bcf A |
871 | dest[i] != 0x064C && dest[i] != 0x064D ) |
872 | { | |
b75a7d8f | 873 | Shape = 1; |
73c04bcf | 874 | if( (nextLink&ALEFTYPE) == ALEFTYPE && (lastLink&LAMTYPE) == LAMTYPE ) { |
b75a7d8f | 875 | Shape = 0; |
73c04bcf | 876 | } |
b75a7d8f A |
877 | } |
878 | else { | |
879 | Shape = 0; | |
880 | } | |
881 | } | |
882 | ||
883 | if(flag == 2) { | |
884 | dest[i] = 0xFE70 + IrrelevantPos[(dest[i] - 0x064B)] + Shape; | |
885 | } | |
886 | else | |
887 | dest[i] = (UChar)(0xFE70 + (currLink >> 8) + Shape); | |
888 | } | |
889 | ||
890 | /* move one notch forward */ | |
891 | if ((currLink & IRRELEVANT) == 0) { | |
892 | prevLink = lastLink; | |
893 | lastLink = currLink; | |
b75a7d8f A |
894 | lastPos = i; |
895 | } | |
896 | ||
897 | i = i + step; | |
898 | if (i == Nx) { | |
899 | currLink = nextLink; | |
900 | Nx = -2; | |
901 | } | |
902 | else if(i != iend) { | |
903 | currLink = getLink(dest[i]); | |
904 | } | |
905 | } | |
906 | ||
907 | /* If there is lamalef in the buffer call expandLamAlef */ | |
908 | if(lamalef_found != 0) | |
909 | destSize = removeLamAlefSpaces(dest,sourceLength,destSize,options,pErrorCode); | |
910 | else | |
911 | destSize = sourceLength; | |
912 | ||
913 | return destSize; | |
914 | } | |
915 | ||
916 | /* | |
917 | *Name : deShapeUnicode | |
918 | *Function : Converts an Arabic Unicode buffer in FExx Range into unshaped | |
919 | * arabic Unicode buffer in 06xx Range | |
920 | */ | |
921 | static int32_t | |
922 | deShapeUnicode(UChar *dest, int32_t sourceLength, | |
923 | int32_t destSize,uint32_t options, | |
924 | UErrorCode *pErrorCode) { | |
925 | int32_t i = 0; | |
926 | int32_t lamalef_found = 0; | |
927 | ||
928 | /* | |
929 | *This for loop changes the buffer from the Unicode FE range to | |
930 | *the Unicode 06 range | |
931 | */ | |
932 | for(i = 0; i < sourceLength; i++) { | |
933 | UChar inputChar = dest[i]; | |
934 | if (( inputChar >= 0xFE70) && (inputChar <= 0xFEF4 )) { /* FExx Arabic range */ | |
935 | dest[i] = convertFEto06 [ (inputChar - 0xFE70) ] ; | |
936 | } else { | |
937 | dest[i] = inputChar ; | |
938 | } | |
939 | if( isLamAlefChar(dest[i]) ) | |
940 | lamalef_found = 1; | |
941 | } | |
942 | ||
943 | /* If there is lamalef in the buffer call expandLamAlef */ | |
944 | if(lamalef_found != 0) | |
945 | destSize = expandLamAlef(dest,sourceLength,destSize,options,pErrorCode); | |
946 | else | |
947 | destSize = sourceLength; | |
948 | ||
949 | return destSize; | |
950 | } | |
951 | ||
952 | U_CAPI int32_t U_EXPORT2 | |
953 | u_shapeArabic(const UChar *source, int32_t sourceLength, | |
954 | UChar *dest, int32_t destCapacity, | |
955 | uint32_t options, | |
956 | UErrorCode *pErrorCode) { | |
957 | ||
958 | int32_t destLength; | |
959 | ||
960 | /* usual error checking */ | |
961 | if(pErrorCode==NULL || U_FAILURE(*pErrorCode)) { | |
962 | return 0; | |
963 | } | |
964 | ||
965 | /* make sure that no reserved options values are used; allow dest==NULL only for preflighting */ | |
966 | if( source==NULL || sourceLength<-1 || | |
967 | (dest==NULL && destCapacity!=0) || destCapacity<0 || | |
968 | options>=U_SHAPE_DIGIT_TYPE_RESERVED || | |
969 | (options&U_SHAPE_DIGITS_MASK)>=U_SHAPE_DIGITS_RESERVED | |
970 | ) { | |
971 | *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR; | |
972 | return 0; | |
973 | } | |
974 | ||
975 | /* determine the source length */ | |
976 | if(sourceLength==-1) { | |
977 | sourceLength=u_strlen(source); | |
978 | } | |
73c04bcf | 979 | if(sourceLength<=0) { |
b75a7d8f A |
980 | return u_terminateUChars(dest, destCapacity, 0, pErrorCode); |
981 | } | |
982 | ||
983 | /* check that source and destination do not overlap */ | |
984 | if( dest!=NULL && | |
985 | ((source<=dest && dest<source+sourceLength) || | |
986 | (dest<=source && source<dest+destCapacity)) | |
987 | ) { | |
988 | *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR; | |
989 | return 0; | |
990 | } | |
991 | ||
992 | if((options&U_SHAPE_LETTERS_MASK)!=U_SHAPE_LETTERS_NOOP) { | |
993 | UChar buffer[300]; | |
994 | UChar *tempbuffer; | |
995 | int32_t outputSize, spacesCountl=0, spacesCountr=0; | |
996 | ||
997 | /* calculate destination size */ | |
998 | /* TODO: do we ever need to do this pure preflighting? */ | |
999 | if((options&U_SHAPE_LENGTH_MASK)==U_SHAPE_LENGTH_GROW_SHRINK) { | |
1000 | outputSize=calculateSize(source,sourceLength,destCapacity,options); | |
1001 | } else { | |
1002 | outputSize=sourceLength; | |
1003 | } | |
1004 | if(outputSize>destCapacity) { | |
1005 | *pErrorCode=U_BUFFER_OVERFLOW_ERROR; | |
1006 | return outputSize; | |
1007 | } | |
1008 | ||
1009 | /* | |
1010 | * need a temporary buffer of size max(outputSize, sourceLength) | |
1011 | * because at first we copy source->temp | |
1012 | */ | |
1013 | if(sourceLength>outputSize) { | |
1014 | outputSize=sourceLength; | |
1015 | } | |
1016 | ||
1017 | /* Start of Arabic letter shaping part */ | |
1018 | if(outputSize<=sizeof(buffer)/U_SIZEOF_UCHAR) { | |
1019 | outputSize=sizeof(buffer)/U_SIZEOF_UCHAR; | |
1020 | tempbuffer=buffer; | |
1021 | } else { | |
1022 | tempbuffer = (UChar *)uprv_malloc(outputSize*U_SIZEOF_UCHAR); | |
1023 | ||
1024 | /*Test for NULL*/ | |
1025 | if(tempbuffer == NULL) { | |
1026 | *pErrorCode = U_MEMORY_ALLOCATION_ERROR; | |
1027 | return 0; | |
1028 | } | |
1029 | } | |
1030 | uprv_memcpy(tempbuffer, source, sourceLength*U_SIZEOF_UCHAR); | |
1031 | if(sourceLength<outputSize) { | |
1032 | uprv_memset(tempbuffer+sourceLength, 0, (outputSize-sourceLength)*U_SIZEOF_UCHAR); | |
1033 | } | |
1034 | ||
1035 | if((options&U_SHAPE_TEXT_DIRECTION_MASK) == U_SHAPE_TEXT_DIRECTION_LOGICAL) { | |
1036 | countSpaces(tempbuffer,sourceLength,options,&spacesCountl,&spacesCountr); | |
1037 | invertBuffer(tempbuffer,sourceLength,options,&spacesCountl,&spacesCountr); | |
1038 | } | |
1039 | ||
1040 | switch(options&U_SHAPE_LETTERS_MASK) { | |
1041 | case U_SHAPE_LETTERS_SHAPE : | |
1042 | /* Call the shaping function with tashkeel flag == 1 */ | |
1043 | destLength = shapeUnicode(tempbuffer,sourceLength,destCapacity,options,pErrorCode,1); | |
1044 | break; | |
1045 | case U_SHAPE_LETTERS_SHAPE_TASHKEEL_ISOLATED : | |
1046 | /* Call the shaping function with tashkeel flag == 0 */ | |
1047 | destLength = shapeUnicode(tempbuffer,sourceLength,destCapacity,options,pErrorCode,0); | |
1048 | break; | |
1049 | case U_SHAPE_LETTERS_UNSHAPE : | |
1050 | /* Call the deshaping function */ | |
1051 | destLength = deShapeUnicode(tempbuffer,sourceLength,destCapacity,options,pErrorCode); | |
1052 | break; | |
1053 | default : | |
1054 | /* will never occur because of validity checks above */ | |
1055 | destLength = 0; | |
1056 | break; | |
1057 | } | |
1058 | ||
1059 | /* | |
1060 | * TODO: (markus 2002aug01) | |
1061 | * For as long as we always preflight the outputSize above | |
1062 | * we should U_ASSERT(outputSize==destLength) | |
1063 | * except for the adjustment above before the tempbuffer allocation | |
1064 | */ | |
1065 | ||
1066 | if((options&U_SHAPE_TEXT_DIRECTION_MASK) == U_SHAPE_TEXT_DIRECTION_LOGICAL) { | |
1067 | countSpaces(tempbuffer,destLength,options,&spacesCountl,&spacesCountr); | |
1068 | invertBuffer(tempbuffer,destLength,options,&spacesCountl,&spacesCountr); | |
1069 | } | |
1070 | uprv_memcpy(dest, tempbuffer, uprv_min(destLength, destCapacity)*U_SIZEOF_UCHAR); | |
1071 | ||
1072 | if(tempbuffer!=buffer) { | |
1073 | uprv_free(tempbuffer); | |
1074 | } | |
1075 | ||
1076 | if(destLength>destCapacity) { | |
1077 | *pErrorCode=U_BUFFER_OVERFLOW_ERROR; | |
1078 | return destLength; | |
1079 | } | |
1080 | ||
1081 | /* End of Arabic letter shaping part */ | |
1082 | } else { | |
1083 | /* | |
1084 | * No letter shaping: | |
1085 | * just make sure the destination is large enough and copy the string. | |
1086 | */ | |
1087 | if(destCapacity<sourceLength) { | |
1088 | /* this catches preflighting, too */ | |
1089 | *pErrorCode=U_BUFFER_OVERFLOW_ERROR; | |
1090 | return sourceLength; | |
1091 | } | |
1092 | uprv_memcpy(dest, source, sourceLength*U_SIZEOF_UCHAR); | |
1093 | destLength=sourceLength; | |
1094 | } | |
1095 | ||
1096 | /* | |
1097 | * Perform number shaping. | |
1098 | * With UTF-16 or UTF-32, the length of the string is constant. | |
1099 | * The easiest way to do this is to operate on the destination and | |
1100 | * "shape" the digits in-place. | |
1101 | */ | |
1102 | if((options&U_SHAPE_DIGITS_MASK)!=U_SHAPE_DIGITS_NOOP) { | |
1103 | UChar digitBase; | |
1104 | int32_t i; | |
1105 | ||
1106 | /* select the requested digit group */ | |
1107 | switch(options&U_SHAPE_DIGIT_TYPE_MASK) { | |
1108 | case U_SHAPE_DIGIT_TYPE_AN: | |
1109 | digitBase=0x660; /* Unicode: "Arabic-Indic digits" */ | |
1110 | break; | |
1111 | case U_SHAPE_DIGIT_TYPE_AN_EXTENDED: | |
1112 | digitBase=0x6f0; /* Unicode: "Eastern Arabic-Indic digits (Persian and Urdu)" */ | |
1113 | break; | |
1114 | default: | |
1115 | /* will never occur because of validity checks above */ | |
1116 | digitBase=0; | |
1117 | break; | |
1118 | } | |
1119 | ||
1120 | /* perform the requested operation */ | |
1121 | switch(options&U_SHAPE_DIGITS_MASK) { | |
1122 | case U_SHAPE_DIGITS_EN2AN: | |
1123 | /* add (digitBase-'0') to each European (ASCII) digit code point */ | |
1124 | digitBase-=0x30; | |
1125 | for(i=0; i<destLength; ++i) { | |
1126 | if(((uint32_t)dest[i]-0x30)<10) { | |
1127 | dest[i]+=digitBase; | |
1128 | } | |
1129 | } | |
1130 | break; | |
1131 | case U_SHAPE_DIGITS_AN2EN: | |
1132 | /* subtract (digitBase-'0') from each Arabic digit code point */ | |
1133 | for(i=0; i<destLength; ++i) { | |
1134 | if(((uint32_t)dest[i]-(uint32_t)digitBase)<10) { | |
1135 | dest[i]-=digitBase-0x30; | |
1136 | } | |
1137 | } | |
1138 | break; | |
1139 | case U_SHAPE_DIGITS_ALEN2AN_INIT_LR: | |
1140 | _shapeToArabicDigitsWithContext(dest, destLength, | |
1141 | digitBase, | |
1142 | (UBool)((options&U_SHAPE_TEXT_DIRECTION_MASK)==U_SHAPE_TEXT_DIRECTION_LOGICAL), | |
1143 | FALSE); | |
1144 | break; | |
1145 | case U_SHAPE_DIGITS_ALEN2AN_INIT_AL: | |
1146 | _shapeToArabicDigitsWithContext(dest, destLength, | |
1147 | digitBase, | |
1148 | (UBool)((options&U_SHAPE_TEXT_DIRECTION_MASK)==U_SHAPE_TEXT_DIRECTION_LOGICAL), | |
1149 | TRUE); | |
1150 | break; | |
1151 | default: | |
1152 | /* will never occur because of validity checks above */ | |
1153 | break; | |
1154 | } | |
1155 | } | |
1156 | ||
1157 | return u_terminateUChars(dest, destCapacity, destLength, pErrorCode); | |
1158 | } |