]> git.saurik.com Git - apple/icu.git/blob - icuSources/common/ushape.cpp
ICU-491.11.2.tar.gz
[apple/icu.git] / icuSources / common / ushape.cpp
1 /*
2 ******************************************************************************
3 *
4 * Copyright (C) 2000-2012, International Business Machines
5 * Corporation and others. All Rights Reserved.
6 *
7 ******************************************************************************
8 * file name: ushape.cpp
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"
22 #include "unicode/ushape.h"
23 #include "cmemory.h"
24 #include "putilimp.h"
25 #include "ustr_imp.h"
26 #include "ubidi_props.h"
27 #include "uassert.h"
28
29 #define LENGTHOF(array) (int32_t)(sizeof(array)/sizeof((array)[0]))
30
31 /*
32 * This implementation is designed for 16-bit Unicode strings.
33 * The main assumption is that the Arabic characters and their
34 * presentation forms each fit into a single UChar.
35 * With UTF-8, they occupy 2 or 3 bytes, and more than the ASCII
36 * characters.
37 */
38
39 /*
40 * ### TODO in general for letter shaping:
41 * - the letter shaping code is UTF-16-unaware; needs update
42 * + especially invertBuffer()?!
43 * - needs to handle the "Arabic Tail" that is used in some legacy codepages
44 * as a glyph fragment of wide-glyph letters
45 * + IBM Unicode conversion tables map it to U+200B (ZWSP)
46 * + IBM Egypt has proposed to encode the tail in Unicode among Arabic Presentation Forms
47 */
48
49 /* definitions for Arabic letter shaping ------------------------------------ */
50
51 #define IRRELEVANT 4
52 #define LAMTYPE 16
53 #define ALEFTYPE 32
54 #define LINKR 1
55 #define LINKL 2
56 #define APRESENT 8
57 #define SHADDA 64
58 #define CSHADDA 128
59 #define COMBINE (SHADDA+CSHADDA)
60
61 #define HAMZAFE_CHAR 0xfe80
62 #define HAMZA06_CHAR 0x0621
63 #define YEH_HAMZA_CHAR 0x0626
64 #define YEH_HAMZAFE_CHAR 0xFE89
65 #define LAMALEF_SPACE_SUB 0xFFFF
66 #define TASHKEEL_SPACE_SUB 0xFFFE
67 #define NEW_TAIL_CHAR 0xFE73
68 #define OLD_TAIL_CHAR 0x200B
69 #define LAM_CHAR 0x0644
70 #define SPACE_CHAR 0x0020
71 #define SHADDA_CHAR 0xFE7C
72 #define TATWEEL_CHAR 0x0640
73 #define SHADDA_TATWEEL_CHAR 0xFE7D
74
75 #define SHAPE_MODE 0
76 #define DESHAPE_MODE 1
77
78 static UChar tailChar = OLD_TAIL_CHAR;
79 static uint32_t uShapeLamalefBegin = U_SHAPE_LAMALEF_BEGIN;
80 static uint32_t uShapeLamalefEnd = U_SHAPE_LAMALEF_END;
81 static uint32_t uShapeTashkeelBegin = U_SHAPE_TASHKEEL_BEGIN;
82 static uint32_t uShapeTashkeelEnd = U_SHAPE_TASHKEEL_END;
83 static int spacesRelativeToTextBeginEnd = 0;
84
85 static const uint8_t tailFamilyIsolatedFinal[] = {
86 /* FEB1 */ 1,
87 /* FEB2 */ 1,
88 /* FEB3 */ 0,
89 /* FEB4 */ 0,
90 /* FEB5 */ 1,
91 /* FEB6 */ 1,
92 /* FEB7 */ 0,
93 /* FEB8 */ 0,
94 /* FEB9 */ 1,
95 /* FEBA */ 1,
96 /* FEBB */ 0,
97 /* FEBC */ 0,
98 /* FEBD */ 1,
99 /* FEBE */ 1
100 };
101
102 static const uint8_t tashkeelMedial[] = {
103 /* FE70 */ 0,
104 /* FE71 */ 1,
105 /* FE72 */ 0,
106 /* FE73 */ 0,
107 /* FE74 */ 0,
108 /* FE75 */ 0,
109 /* FE76 */ 0,
110 /* FE77 */ 1,
111 /* FE78 */ 0,
112 /* FE79 */ 1,
113 /* FE7A */ 0,
114 /* FE7B */ 1,
115 /* FE7C */ 0,
116 /* FE7D */ 1,
117 /* FE7E */ 0,
118 /* FE7F */ 1
119 };
120
121 static const UChar yehHamzaToYeh[] =
122 {
123 /* isolated*/ 0xFEEF,
124 /* final */ 0xFEF0
125 };
126
127 static const uint8_t IrrelevantPos[] = {
128 0x0, 0x2, 0x4, 0x6,
129 0x8, 0xA, 0xC, 0xE
130 };
131
132
133 static const UChar convertLamAlef[] =
134 {
135 /*FEF5*/ 0x0622,
136 /*FEF6*/ 0x0622,
137 /*FEF7*/ 0x0623,
138 /*FEF8*/ 0x0623,
139 /*FEF9*/ 0x0625,
140 /*FEFA*/ 0x0625,
141 /*FEFB*/ 0x0627,
142 /*FEFC*/ 0x0627
143 };
144
145 static const UChar araLink[178]=
146 {
147 1 + 32 + 256 * 0x11,/*0x0622*/
148 1 + 32 + 256 * 0x13,/*0x0623*/
149 1 + 256 * 0x15,/*0x0624*/
150 1 + 32 + 256 * 0x17,/*0x0625*/
151 1 + 2 + 256 * 0x19,/*0x0626*/
152 1 + 32 + 256 * 0x1D,/*0x0627*/
153 1 + 2 + 256 * 0x1F,/*0x0628*/
154 1 + 256 * 0x23,/*0x0629*/
155 1 + 2 + 256 * 0x25,/*0x062A*/
156 1 + 2 + 256 * 0x29,/*0x062B*/
157 1 + 2 + 256 * 0x2D,/*0x062C*/
158 1 + 2 + 256 * 0x31,/*0x062D*/
159 1 + 2 + 256 * 0x35,/*0x062E*/
160 1 + 256 * 0x39,/*0x062F*/
161 1 + 256 * 0x3B,/*0x0630*/
162 1 + 256 * 0x3D,/*0x0631*/
163 1 + 256 * 0x3F,/*0x0632*/
164 1 + 2 + 256 * 0x41,/*0x0633*/
165 1 + 2 + 256 * 0x45,/*0x0634*/
166 1 + 2 + 256 * 0x49,/*0x0635*/
167 1 + 2 + 256 * 0x4D,/*0x0636*/
168 1 + 2 + 256 * 0x51,/*0x0637*/
169 1 + 2 + 256 * 0x55,/*0x0638*/
170 1 + 2 + 256 * 0x59,/*0x0639*/
171 1 + 2 + 256 * 0x5D,/*0x063A*/
172 0, 0, 0, 0, 0, /*0x063B-0x063F*/
173 1 + 2, /*0x0640*/
174 1 + 2 + 256 * 0x61,/*0x0641*/
175 1 + 2 + 256 * 0x65,/*0x0642*/
176 1 + 2 + 256 * 0x69,/*0x0643*/
177 1 + 2 + 16 + 256 * 0x6D,/*0x0644*/
178 1 + 2 + 256 * 0x71,/*0x0645*/
179 1 + 2 + 256 * 0x75,/*0x0646*/
180 1 + 2 + 256 * 0x79,/*0x0647*/
181 1 + 256 * 0x7D,/*0x0648*/
182 1 + 256 * 0x7F,/*0x0649*/
183 1 + 2 + 256 * 0x81,/*0x064A*/
184 4 + 256 * 1, /*0x064B*/
185 4 + 128 + 256 * 1, /*0x064C*/
186 4 + 128 + 256 * 1, /*0x064D*/
187 4 + 128 + 256 * 1, /*0x064E*/
188 4 + 128 + 256 * 1, /*0x064F*/
189 4 + 128 + 256 * 1, /*0x0650*/
190 4 + 64 + 256 * 3, /*0x0651*/
191 4 + 256 * 1, /*0x0652*/
192 4 + 256 * 7, /*0x0653*/
193 4 + 256 * 8, /*0x0654*/
194 4 + 256 * 8, /*0x0655*/
195 4 + 256 * 1, /*0x0656*/
196 0, 0, 0, 0, 0, /*0x0657-0x065B*/
197 1 + 256 * 0x85,/*0x065C*/
198 1 + 256 * 0x87,/*0x065D*/
199 1 + 256 * 0x89,/*0x065E*/
200 1 + 256 * 0x8B,/*0x065F*/
201 0, 0, 0, 0, 0, /*0x0660-0x0664*/
202 0, 0, 0, 0, 0, /*0x0665-0x0669*/
203 0, 0, 0, 0, 0, 0, /*0x066A-0x066F*/
204 4 + 256 * 6, /*0x0670*/
205 1 + 8 + 256 * 0x00,/*0x0671*/
206 1 + 32, /*0x0672*/
207 1 + 32, /*0x0673*/
208 0, /*0x0674*/
209 1 + 32, /*0x0675*/
210 1, 1, /*0x0676-0x0677*/
211 1+2, 1+2, 1+2, 1+2, 1+2, 1+2, /*0x0678-0x067D*/
212 1+2+8+256 * 0x06, 1+2, 1+2, 1+2, 1+2, 1+2, /*0x067E-0x0683*/
213 1+2, 1+2, 1+2+8+256 * 0x2A, 1+2, /*0x0684-0x0687*/
214 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /*0x0688-0x0691*/
215 1, 1, 1, 1, 1, 1, 1+8+256 * 0x3A, 1, /*0x0692-0x0699*/
216 1+2, 1+2, 1+2, 1+2, 1+2, 1+2, /*0x069A-0x06A3*/
217 1+2, 1+2, 1+2, 1+2, /*0x069A-0x06A3*/
218 1+2, 1+2, 1+2, 1+2, 1+2, 1+2+8+256 * 0x3E, /*0x06A4-0x06AD*/
219 1+2, 1+2, 1+2, 1+2, /*0x06A4-0x06AD*/
220 1+2, 1+2+8+256 * 0x42, 1+2, 1+2, 1+2, 1+2, /*0x06AE-0x06B7*/
221 1+2, 1+2, 1+2, 1+2, /*0x06AE-0x06B7*/
222 1+2, 1+2, 1+2, 1+2, 1+2, 1+2, /*0x06B8-0x06BF*/
223 1+2, 1+2, /*0x06B8-0x06BF*/
224 1, /*0x06C0*/
225 1+2, /*0x06C1*/
226 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /*0x06C2-0x06CB*/
227 1+2+8+256 * 0xAC, /*0x06CC*/
228 1, /*0x06CD*/
229 1+2, 1+2, 1+2, 1+2, /*0x06CE-0x06D1*/
230 1, 1 /*0x06D2-0x06D3*/
231 };
232
233 static const uint8_t presALink[] = {
234 /***********0*****1*****2*****3*****4*****5*****6*****7*****8*****9*****A*****B*****C*****D*****E*****F*/
235 /*FB5*/ 0, 1, 0, 0, 0, 0, 0, 1, 2,1 + 2, 0, 0, 0, 0, 0, 0,
236 /*FB6*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
237 /*FB7*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2,1 + 2, 0, 0,
238 /*FB8*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1,
239 /*FB9*/ 2,1 + 2, 0, 1, 2,1 + 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
240 /*FBA*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
241 /*FBB*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
242 /*FBC*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
243 /*FBD*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
244 /*FBE*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
245 /*FBF*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2,1 + 2,
246 /*FC0*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
247 /*FC1*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
248 /*FC2*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
249 /*FC3*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
250 /*FC4*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
251 /*FC5*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 4,
252 /*FC6*/ 4, 4, 4
253 };
254
255 static const uint8_t presBLink[]=
256 {
257 /***********0*****1*****2*****3*****4*****5*****6*****7*****8*****9*****A*****B*****C*****D*****E*****F*/
258 /*FE7*/1 + 2,1 + 2,1 + 2, 0,1 + 2, 0,1 + 2,1 + 2,1 + 2,1 + 2,1 + 2,1 + 2,1 + 2,1 + 2,1 + 2,1 + 2,
259 /*FE8*/ 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 2,1 + 2, 0, 1, 0,
260 /*FE9*/ 1, 2,1 + 2, 0, 1, 0, 1, 2,1 + 2, 0, 1, 2,1 + 2, 0, 1, 2,
261 /*FEA*/1 + 2, 0, 1, 2,1 + 2, 0, 1, 2,1 + 2, 0, 1, 0, 1, 0, 1, 0,
262 /*FEB*/ 1, 0, 1, 2,1 + 2, 0, 1, 2,1 + 2, 0, 1, 2,1 + 2, 0, 1, 2,
263 /*FEC*/1 + 2, 0, 1, 2,1 + 2, 0, 1, 2,1 + 2, 0, 1, 2,1 + 2, 0, 1, 2,
264 /*FED*/1 + 2, 0, 1, 2,1 + 2, 0, 1, 2,1 + 2, 0, 1, 2,1 + 2, 0, 1, 2,
265 /*FEE*/1 + 2, 0, 1, 2,1 + 2, 0, 1, 2,1 + 2, 0, 1, 2,1 + 2, 0, 1, 0,
266 /*FEF*/ 1, 0, 1, 2,1 + 2, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0
267 };
268
269 static const UChar convertFBto06[] =
270 {
271 /***********0******1******2******3******4******5******6******7******8******9******A******B******C******D******E******F***/
272 /*FB5*/ 0x671, 0x671, 0, 0, 0, 0, 0x07E, 0x07E, 0x07E, 0x07E, 0, 0, 0, 0, 0, 0,
273 /*FB6*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
274 /*FB7*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x686, 0x686, 0x686, 0x686, 0, 0,
275 /*FB8*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x698, 0x698, 0, 0, 0x6A9, 0x6A9,
276 /*FB9*/ 0x6A9, 0x6A9, 0x6AF, 0x6AF, 0x6AF, 0x6AF, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
277 /*FBA*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
278 /*FBB*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
279 /*FBC*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
280 /*FBD*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
281 /*FBE*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
282 /*FBF*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x6CC, 0x6CC, 0x6CC, 0x6CC
283 };
284
285 static const UChar convertFEto06[] =
286 {
287 /***********0******1******2******3******4******5******6******7******8******9******A******B******C******D******E******F***/
288 /*FE7*/ 0x64B, 0x64B, 0x64C, 0x64C, 0x64D, 0x64D, 0x64E, 0x64E, 0x64F, 0x64F, 0x650, 0x650, 0x651, 0x651, 0x652, 0x652,
289 /*FE8*/ 0x621, 0x622, 0x622, 0x623, 0x623, 0x624, 0x624, 0x625, 0x625, 0x626, 0x626, 0x626, 0x626, 0x627, 0x627, 0x628,
290 /*FE9*/ 0x628, 0x628, 0x628, 0x629, 0x629, 0x62A, 0x62A, 0x62A, 0x62A, 0x62B, 0x62B, 0x62B, 0x62B, 0x62C, 0x62C, 0x62C,
291 /*FEA*/ 0x62C, 0x62D, 0x62D, 0x62D, 0x62D, 0x62E, 0x62E, 0x62E, 0x62E, 0x62F, 0x62F, 0x630, 0x630, 0x631, 0x631, 0x632,
292 /*FEB*/ 0x632, 0x633, 0x633, 0x633, 0x633, 0x634, 0x634, 0x634, 0x634, 0x635, 0x635, 0x635, 0x635, 0x636, 0x636, 0x636,
293 /*FEC*/ 0x636, 0x637, 0x637, 0x637, 0x637, 0x638, 0x638, 0x638, 0x638, 0x639, 0x639, 0x639, 0x639, 0x63A, 0x63A, 0x63A,
294 /*FED*/ 0x63A, 0x641, 0x641, 0x641, 0x641, 0x642, 0x642, 0x642, 0x642, 0x643, 0x643, 0x643, 0x643, 0x644, 0x644, 0x644,
295 /*FEE*/ 0x644, 0x645, 0x645, 0x645, 0x645, 0x646, 0x646, 0x646, 0x646, 0x647, 0x647, 0x647, 0x647, 0x648, 0x648, 0x649,
296 /*FEF*/ 0x649, 0x64A, 0x64A, 0x64A, 0x64A, 0x65C, 0x65C, 0x65D, 0x65D, 0x65E, 0x65E, 0x65F, 0x65F
297 };
298
299 static const uint8_t shapeTable[4][4][4]=
300 {
301 { {0,0,0,0}, {0,0,0,0}, {0,1,0,3}, {0,1,0,1} },
302 { {0,0,2,2}, {0,0,1,2}, {0,1,1,2}, {0,1,1,3} },
303 { {0,0,0,0}, {0,0,0,0}, {0,1,0,3}, {0,1,0,3} },
304 { {0,0,1,2}, {0,0,1,2}, {0,1,1,2}, {0,1,1,3} }
305 };
306
307 /*
308 * This function shapes European digits to Arabic-Indic digits
309 * in-place, writing over the input characters.
310 * Since we know that we are only looking for BMP code points,
311 * we can safely just work with code units (again, at least UTF-16).
312 */
313 static void
314 _shapeToArabicDigitsWithContext(UChar *s, int32_t length,
315 UChar digitBase,
316 UBool isLogical, UBool lastStrongWasAL) {
317 const UBiDiProps *bdp;
318 int32_t i;
319 UChar c;
320
321 bdp=ubidi_getSingleton();
322 digitBase-=0x30;
323
324 /* the iteration direction depends on the type of input */
325 if(isLogical) {
326 for(i=0; i<length; ++i) {
327 c=s[i];
328 switch(ubidi_getClass(bdp, c)) {
329 case U_LEFT_TO_RIGHT: /* L */
330 case U_RIGHT_TO_LEFT: /* R */
331 lastStrongWasAL=FALSE;
332 break;
333 case U_RIGHT_TO_LEFT_ARABIC: /* AL */
334 lastStrongWasAL=TRUE;
335 break;
336 case U_EUROPEAN_NUMBER: /* EN */
337 if(lastStrongWasAL && (uint32_t)(c-0x30)<10) {
338 s[i]=(UChar)(digitBase+c); /* digitBase+(c-0x30) - digitBase was modified above */
339 }
340 break;
341 default :
342 break;
343 }
344 }
345 } else {
346 for(i=length; i>0; /* pre-decrement in the body */) {
347 c=s[--i];
348 switch(ubidi_getClass(bdp, c)) {
349 case U_LEFT_TO_RIGHT: /* L */
350 case U_RIGHT_TO_LEFT: /* R */
351 lastStrongWasAL=FALSE;
352 break;
353 case U_RIGHT_TO_LEFT_ARABIC: /* AL */
354 lastStrongWasAL=TRUE;
355 break;
356 case U_EUROPEAN_NUMBER: /* EN */
357 if(lastStrongWasAL && (uint32_t)(c-0x30)<10) {
358 s[i]=(UChar)(digitBase+c); /* digitBase+(c-0x30) - digitBase was modified above */
359 }
360 break;
361 default :
362 break;
363 }
364 }
365 }
366 }
367
368 /*
369 *Name : invertBuffer
370 *Function : This function inverts the buffer, it's used
371 * in case the user specifies the buffer to be
372 * U_SHAPE_TEXT_DIRECTION_LOGICAL
373 */
374 static void
375 invertBuffer(UChar *buffer, int32_t size, uint32_t /*options*/, int32_t lowlimit, int32_t highlimit) {
376 UChar temp;
377 int32_t i=0,j=0;
378 for(i=lowlimit,j=size-highlimit-1;i<j;i++,j--) {
379 temp = buffer[i];
380 buffer[i] = buffer[j];
381 buffer[j] = temp;
382 }
383 }
384
385 /*
386 *Name : changeLamAlef
387 *Function : Converts the Alef characters into an equivalent
388 * LamAlef location in the 0x06xx Range, this is an
389 * intermediate stage in the operation of the program
390 * later it'll be converted into the 0xFExx LamAlefs
391 * in the shaping function.
392 */
393 static inline UChar
394 changeLamAlef(UChar ch) {
395 switch(ch) {
396 case 0x0622 :
397 return 0x065C;
398 case 0x0623 :
399 return 0x065D;
400 case 0x0625 :
401 return 0x065E;
402 case 0x0627 :
403 return 0x065F;
404 }
405 return 0;
406 }
407
408 /*
409 *Name : getLink
410 *Function : Resolves the link between the characters as
411 * Arabic characters have four forms :
412 * Isolated, Initial, Middle and Final Form
413 */
414 static UChar
415 getLink(UChar ch) {
416 if(ch >= 0x0622 && ch <= 0x06D3) {
417 return(araLink[ch-0x0622]);
418 } else if(ch == 0x200D) {
419 return(3);
420 } else if(ch >= 0x206D && ch <= 0x206F) {
421 return(4);
422 }else if(ch >= 0xFB50 && ch <= 0xFC62) {
423 return(presALink[ch-0xFB50]);
424 } else if(ch >= 0xFE70 && ch <= 0xFEFC) {
425 return(presBLink[ch-0xFE70]);
426 }else {
427 return(0);
428 }
429 }
430
431 /*
432 *Name : countSpaces
433 *Function : Counts the number of spaces
434 * at each end of the logical buffer
435 */
436 static void
437 countSpaces(UChar *dest, int32_t size, uint32_t /*options*/, int32_t *spacesCountl, int32_t *spacesCountr) {
438 int32_t i = 0;
439 int32_t countl = 0,countr = 0;
440 while((dest[i] == SPACE_CHAR) && (countl < size)) {
441 countl++;
442 i++;
443 }
444 if (countl < size) { /* the entire buffer is not all space */
445 while(dest[size-1] == SPACE_CHAR) {
446 countr++;
447 size--;
448 }
449 }
450 *spacesCountl = countl;
451 *spacesCountr = countr;
452 }
453
454 /*
455 *Name : isTashkeelChar
456 *Function : Returns 1 for Tashkeel characters in 06 range else return 0
457 */
458 static inline int32_t
459 isTashkeelChar(UChar ch) {
460 return (int32_t)( ch>=0x064B && ch<= 0x0652 );
461 }
462
463 /*
464 *Name : isTashkeelCharFE
465 *Function : Returns 1 for Tashkeel characters in FE range else return 0
466 */
467 static inline int32_t
468 isTashkeelCharFE(UChar ch) {
469 return (int32_t)( ch>=0xFE70 && ch<= 0xFE7F );
470 }
471
472 /*
473 *Name : isAlefChar
474 *Function : Returns 1 for Alef characters else return 0
475 */
476 static inline int32_t
477 isAlefChar(UChar ch) {
478 return (int32_t)( (ch==0x0622)||(ch==0x0623)||(ch==0x0625)||(ch==0x0627) );
479 }
480
481 /*
482 *Name : isLamAlefChar
483 *Function : Returns 1 for LamAlef characters else return 0
484 */
485 static inline int32_t
486 isLamAlefChar(UChar ch) {
487 return (int32_t)((ch>=0xFEF5)&&(ch<=0xFEFC) );
488 }
489
490 /*BIDI
491 *Name : isTailChar
492 *Function : returns 1 if the character matches one of the tail characters (0xfe73 or 0x200b) otherwise returns 0
493 */
494
495 static inline int32_t
496 isTailChar(UChar ch) {
497 if(ch == OLD_TAIL_CHAR || ch == NEW_TAIL_CHAR){
498 return 1;
499 }else{
500 return 0;
501 }
502 }
503
504 /*BIDI
505 *Name : isSeenTailFamilyChar
506 *Function : returns 1 if the character is a seen family isolated character
507 * in the FE range otherwise returns 0
508 */
509
510 static inline int32_t
511 isSeenTailFamilyChar(UChar ch) {
512 if(ch >= 0xfeb1 && ch < 0xfebf){
513 return tailFamilyIsolatedFinal [ch - 0xFEB1];
514 }else{
515 return 0;
516 }
517 }
518
519 /* Name : isSeenFamilyChar
520 * Function : returns 1 if the character is a seen family character in the Unicode
521 * 06 range otherwise returns 0
522 */
523
524 static inline int32_t
525 isSeenFamilyChar(UChar ch){
526 if(ch >= 0x633 && ch <= 0x636){
527 return 1;
528 }else {
529 return 0;
530 }
531 }
532
533 /*Start of BIDI*/
534 /*
535 *Name : isAlefMaksouraChar
536 *Function : returns 1 if the character is a Alef Maksoura Final or isolated
537 * otherwise returns 0
538 */
539 static inline int32_t
540 isAlefMaksouraChar(UChar ch) {
541 return (int32_t)( (ch == 0xFEEF) || ( ch == 0xFEF0) || (ch == 0x0649));
542 }
543
544 /*
545 * Name : isYehHamzaChar
546 * Function : returns 1 if the character is a yehHamza isolated or yehhamza
547 * final is found otherwise returns 0
548 */
549 static inline int32_t
550 isYehHamzaChar(UChar ch) {
551 if((ch==0xFE89)||(ch==0xFE8A)){
552 return 1;
553 }else{
554 return 0;
555 }
556 }
557
558 /*
559 * Name: isTashkeelOnTatweelChar
560 * Function: Checks if the Tashkeel Character is on Tatweel or not,if the
561 * Tashkeel on tatweel (FE range), it returns 1 else if the
562 * Tashkeel with shadda on tatweel (FC range)return 2 otherwise
563 * returns 0
564 */
565 static inline int32_t
566 isTashkeelOnTatweelChar(UChar ch){
567 if(ch >= 0xfe70 && ch <= 0xfe7f && ch != NEW_TAIL_CHAR && ch != 0xFE75 && ch != SHADDA_TATWEEL_CHAR)
568 {
569 return tashkeelMedial [ch - 0xFE70];
570 }else if( (ch >= 0xfcf2 && ch <= 0xfcf4) || (ch == SHADDA_TATWEEL_CHAR)) {
571 return 2;
572 }else{
573 return 0;
574 }
575 }
576
577 /*
578 * Name: isIsolatedTashkeelChar
579 * Function: Checks if the Tashkeel Character is in the isolated form
580 * (i.e. Unicode FE range) returns 1 else if the Tashkeel
581 * with shadda is in the isolated form (i.e. Unicode FC range)
582 * returns 2 otherwise returns 0
583 */
584 static inline int32_t
585 isIsolatedTashkeelChar(UChar ch){
586 if(ch >= 0xfe70 && ch <= 0xfe7f && ch != NEW_TAIL_CHAR && ch != 0xFE75){
587 return (1 - tashkeelMedial [ch - 0xFE70]);
588 }else if(ch >= 0xfc5e && ch <= 0xfc63){
589 return 1;
590 }else{
591 return 0;
592 }
593 }
594
595
596
597
598 /*
599 *Name : calculateSize
600 *Function : This function calculates the destSize to be used in preflighting
601 * when the destSize is equal to 0
602 * It is used also to calculate the new destsize in case the
603 * destination buffer will be resized.
604 */
605
606 static int32_t
607 calculateSize(const UChar *source, int32_t sourceLength,
608 int32_t destSize,uint32_t options) {
609 int32_t i = 0;
610
611 int lamAlefOption = 0;
612 int tashkeelOption = 0;
613
614 destSize = sourceLength;
615
616 if (((options&U_SHAPE_LETTERS_MASK) == U_SHAPE_LETTERS_SHAPE ||
617 ((options&U_SHAPE_LETTERS_MASK) == U_SHAPE_LETTERS_SHAPE_TASHKEEL_ISOLATED )) &&
618 ((options&U_SHAPE_LAMALEF_MASK) == U_SHAPE_LAMALEF_RESIZE )){
619 lamAlefOption = 1;
620 }
621 if((options&U_SHAPE_LETTERS_MASK) == U_SHAPE_LETTERS_SHAPE &&
622 ((options&U_SHAPE_TASHKEEL_MASK) == U_SHAPE_TASHKEEL_RESIZE ) ){
623 tashkeelOption = 1;
624 }
625
626 if(lamAlefOption || tashkeelOption){
627 if((options&U_SHAPE_TEXT_DIRECTION_MASK)==U_SHAPE_TEXT_DIRECTION_VISUAL_LTR) {
628 for(i=0;i<sourceLength;i++) {
629 if( ((isAlefChar(source[i]))&& (i<(sourceLength-1)) &&(source[i+1] == LAM_CHAR)) || (isTashkeelCharFE(source[i])) ) {
630 destSize--;
631 }
632 }
633 }else if((options&U_SHAPE_TEXT_DIRECTION_MASK)==U_SHAPE_TEXT_DIRECTION_LOGICAL) {
634 for(i=0;i<sourceLength;i++) {
635 if( ( (source[i] == LAM_CHAR) && (i<(sourceLength-1)) && (isAlefChar(source[i+1]))) || (isTashkeelCharFE(source[i])) ) {
636 destSize--;
637 }
638 }
639 }
640 }
641
642 if ((options&U_SHAPE_LETTERS_MASK) == U_SHAPE_LETTERS_UNSHAPE){
643 if ( (options&U_SHAPE_LAMALEF_MASK) == U_SHAPE_LAMALEF_RESIZE){
644 for(i=0;i<sourceLength;i++) {
645 if(isLamAlefChar(source[i]))
646 destSize++;
647 }
648 }
649 }
650
651 return destSize;
652 }
653
654 /*
655 *Name : handleTashkeelWithTatweel
656 *Function : Replaces Tashkeel as following:
657 * Case 1 :if the Tashkeel on tatweel, replace it with Tatweel.
658 * Case 2 :if the Tashkeel aggregated with Shadda on Tatweel, replace
659 * it with Shadda on Tatweel.
660 * Case 3: if the Tashkeel is isolated replace it with Space.
661 *
662 */
663 static int32_t
664 handleTashkeelWithTatweel(UChar *dest, int32_t sourceLength,
665 int32_t /*destSize*/, uint32_t /*options*/,
666 UErrorCode * /*pErrorCode*/) {
667 int i;
668 for(i = 0; i < sourceLength; i++){
669 if((isTashkeelOnTatweelChar(dest[i]) == 1)){
670 dest[i] = TATWEEL_CHAR;
671 }else if((isTashkeelOnTatweelChar(dest[i]) == 2)){
672 dest[i] = SHADDA_TATWEEL_CHAR;
673 }else if(isIsolatedTashkeelChar(dest[i]) && dest[i] != SHADDA_CHAR){
674 dest[i] = SPACE_CHAR;
675 }
676 }
677 return sourceLength;
678 }
679
680
681
682 /*
683 *Name : handleGeneratedSpaces
684 *Function : The shapeUnicode function converts Lam + Alef into LamAlef + space,
685 * and Tashkeel to space.
686 * handleGeneratedSpaces function puts these generated spaces
687 * according to the options the user specifies. LamAlef and Tashkeel
688 * spaces can be replaced at begin, at end, at near or decrease the
689 * buffer size.
690 *
691 * There is also Auto option for LamAlef and tashkeel, which will put
692 * the spaces at end of the buffer (or end of text if the user used
693 * the option U_SHAPE_SPACES_RELATIVE_TO_TEXT_BEGIN_END).
694 *
695 * If the text type was visual_LTR and the option
696 * U_SHAPE_SPACES_RELATIVE_TO_TEXT_BEGIN_END was selected the END
697 * option will place the space at the beginning of the buffer and
698 * BEGIN will place the space at the end of the buffer.
699 */
700
701 static int32_t
702 handleGeneratedSpaces(UChar *dest, int32_t sourceLength,
703 int32_t destSize,
704 uint32_t options,
705 UErrorCode *pErrorCode ) {
706
707 int32_t i = 0, j = 0;
708 int32_t count = 0;
709 UChar *tempbuffer=NULL;
710
711 int lamAlefOption = 0;
712 int tashkeelOption = 0;
713 int shapingMode = SHAPE_MODE;
714
715 if (shapingMode == 0){
716 if ( (options&U_SHAPE_LAMALEF_MASK) == U_SHAPE_LAMALEF_RESIZE ){
717 lamAlefOption = 1;
718 }
719 if ( (options&U_SHAPE_TASHKEEL_MASK) == U_SHAPE_TASHKEEL_RESIZE ){
720 tashkeelOption = 1;
721 }
722 }
723
724 if (lamAlefOption || tashkeelOption){
725 tempbuffer = (UChar *)uprv_malloc((sourceLength+1)*U_SIZEOF_UCHAR);
726 /* Test for NULL */
727 if(tempbuffer == NULL) {
728 *pErrorCode = U_MEMORY_ALLOCATION_ERROR;
729 return 0;
730 }
731
732 uprv_memset(tempbuffer, 0, (sourceLength+1)*U_SIZEOF_UCHAR);
733
734 i = j = 0; count = 0;
735 while(i < sourceLength) {
736 if ( (lamAlefOption && dest[i] == LAMALEF_SPACE_SUB) ||
737 (tashkeelOption && dest[i] == TASHKEEL_SPACE_SUB) ){
738 j--;
739 count++;
740 } else {
741 tempbuffer[j] = dest[i];
742 }
743 i++;
744 j++;
745 }
746
747 while(count >= 0) {
748 tempbuffer[i] = 0x0000;
749 i--;
750 count--;
751 }
752
753 uprv_memcpy(dest, tempbuffer, sourceLength*U_SIZEOF_UCHAR);
754 destSize = u_strlen(dest);
755 }
756
757 lamAlefOption = 0;
758
759 if (shapingMode == 0){
760 if ( (options&U_SHAPE_LAMALEF_MASK) == U_SHAPE_LAMALEF_NEAR ){
761 lamAlefOption = 1;
762 }
763 }
764
765 if (lamAlefOption){
766 /* Lam+Alef is already shaped into LamAlef + FFFF */
767 i = 0;
768 while(i < sourceLength) {
769 if(lamAlefOption&&dest[i] == LAMALEF_SPACE_SUB){
770 dest[i] = SPACE_CHAR;
771 }
772 i++;
773 }
774 destSize = sourceLength;
775 }
776 lamAlefOption = 0;
777 tashkeelOption = 0;
778
779 if (shapingMode == 0) {
780 if ( ((options&U_SHAPE_LAMALEF_MASK) == uShapeLamalefBegin) ||
781 (((options&U_SHAPE_LAMALEF_MASK) == U_SHAPE_LAMALEF_AUTO )
782 && (spacesRelativeToTextBeginEnd==1)) ) {
783 lamAlefOption = 1;
784 }
785 if ( (options&U_SHAPE_TASHKEEL_MASK) == uShapeTashkeelBegin ) {
786 tashkeelOption = 1;
787 }
788 }
789
790 if(lamAlefOption || tashkeelOption){
791 tempbuffer = (UChar *)uprv_malloc((sourceLength+1)*U_SIZEOF_UCHAR);
792
793 /* Test for NULL */
794 if(tempbuffer == NULL) {
795 *pErrorCode = U_MEMORY_ALLOCATION_ERROR;
796 return 0;
797 }
798
799 uprv_memset(tempbuffer, 0, (sourceLength+1)*U_SIZEOF_UCHAR);
800 i = j = sourceLength; count = 0;
801 while(i >= 0) {
802 if ( (lamAlefOption && dest[i] == LAMALEF_SPACE_SUB) ||
803 (tashkeelOption && dest[i] == TASHKEEL_SPACE_SUB) ){
804 j++;
805 count++;
806 }else {
807 tempbuffer[j] = dest[i];
808 }
809 i--;
810 j--;
811 }
812
813 for(i=0 ;i < count; i++){
814 tempbuffer[i] = SPACE_CHAR;
815 }
816
817 uprv_memcpy(dest, tempbuffer, sourceLength*U_SIZEOF_UCHAR);
818 destSize = sourceLength;
819 }
820
821
822
823 lamAlefOption = 0;
824 tashkeelOption = 0;
825
826 if (shapingMode == 0) {
827 if ( ((options&U_SHAPE_LAMALEF_MASK) == uShapeLamalefEnd) ||
828 (((options&U_SHAPE_LAMALEF_MASK) == U_SHAPE_LAMALEF_AUTO )
829 && (spacesRelativeToTextBeginEnd==0)) ) {
830 lamAlefOption = 1;
831 }
832 if ( (options&U_SHAPE_TASHKEEL_MASK) == uShapeTashkeelEnd ){
833 tashkeelOption = 1;
834 }
835 }
836
837 if(lamAlefOption || tashkeelOption){
838 tempbuffer = (UChar *)uprv_malloc((sourceLength+1)*U_SIZEOF_UCHAR);
839 /* Test for NULL */
840 if(tempbuffer == NULL) {
841 *pErrorCode = U_MEMORY_ALLOCATION_ERROR;
842 return 0;
843 }
844
845 uprv_memset(tempbuffer, 0, (sourceLength+1)*U_SIZEOF_UCHAR);
846
847 i = j = 0; count = 0;
848 while(i < sourceLength) {
849 if ( (lamAlefOption && dest[i] == LAMALEF_SPACE_SUB) ||
850 (tashkeelOption && dest[i] == TASHKEEL_SPACE_SUB) ){
851 j--;
852 count++;
853 }else {
854 tempbuffer[j] = dest[i];
855 }
856 i++;
857 j++;
858 }
859
860 while(count >= 0) {
861 tempbuffer[i] = SPACE_CHAR;
862 i--;
863 count--;
864 }
865
866 uprv_memcpy(dest,tempbuffer, sourceLength*U_SIZEOF_UCHAR);
867 destSize = sourceLength;
868 }
869
870
871 if(tempbuffer){
872 uprv_free(tempbuffer);
873 }
874
875 return destSize;
876 }
877
878 /*
879 *Name :expandCompositCharAtBegin
880 *Function :Expands the LamAlef character to Lam and Alef consuming the required
881 * space from beginning of the buffer. If the text type was visual_LTR
882 * and the option U_SHAPE_SPACES_RELATIVE_TO_TEXT_BEGIN_END was selected
883 * the spaces will be located at end of buffer.
884 * If there are no spaces to expand the LamAlef, an error
885 * will be set to U_NO_SPACE_AVAILABLE as defined in utypes.h
886 */
887
888 static int32_t
889 expandCompositCharAtBegin(UChar *dest, int32_t sourceLength, int32_t destSize,UErrorCode *pErrorCode) {
890 int32_t i = 0,j = 0;
891 int32_t countl = 0;
892 UChar *tempbuffer=NULL;
893
894 tempbuffer = (UChar *)uprv_malloc((sourceLength+1)*U_SIZEOF_UCHAR);
895
896 /* Test for NULL */
897 if(tempbuffer == NULL) {
898 *pErrorCode = U_MEMORY_ALLOCATION_ERROR;
899 return 0;
900 }
901
902 uprv_memset(tempbuffer, 0, (sourceLength+1)*U_SIZEOF_UCHAR);
903
904 i = 0;
905 while(dest[i] == SPACE_CHAR) {
906 countl++;
907 i++;
908 }
909
910 i = j = sourceLength-1;
911
912 while(i >= 0 && j >= 0) {
913 if( countl>0 && isLamAlefChar(dest[i])) {
914 tempbuffer[j] = LAM_CHAR;
915 /* to ensure the array index is within the range */
916 U_ASSERT(dest[i] >= 0xFEF5u
917 && dest[i]-0xFEF5u < sizeof(convertLamAlef)/sizeof(convertLamAlef[0]));
918 tempbuffer[j-1] = convertLamAlef[ dest[i] - 0xFEF5 ];
919 j--;
920 countl--;
921 }else {
922 if( countl == 0 && isLamAlefChar(dest[i]) ) {
923 *pErrorCode=U_NO_SPACE_AVAILABLE;
924 }
925 tempbuffer[j] = dest[i];
926 }
927 i--;
928 j--;
929 }
930 uprv_memcpy(dest, tempbuffer, sourceLength*U_SIZEOF_UCHAR);
931
932 uprv_free(tempbuffer);
933
934 destSize = sourceLength;
935 return destSize;
936 }
937
938 /*
939 *Name : expandCompositCharAtEnd
940 *Function : Expands the LamAlef character to Lam and Alef consuming the
941 * required space from end of the buffer. If the text type was
942 * Visual LTR and the option U_SHAPE_SPACES_RELATIVE_TO_TEXT_BEGIN_END
943 * was used, the spaces will be consumed from begin of buffer. If
944 * there are no spaces to expand the LamAlef, an error
945 * will be set to U_NO_SPACE_AVAILABLE as defined in utypes.h
946 */
947
948 static int32_t
949 expandCompositCharAtEnd(UChar *dest, int32_t sourceLength, int32_t destSize,UErrorCode *pErrorCode) {
950 int32_t i = 0,j = 0;
951
952 int32_t countr = 0;
953 int32_t inpsize = sourceLength;
954
955 UChar *tempbuffer=NULL;
956 tempbuffer = (UChar *)uprv_malloc((sourceLength+1)*U_SIZEOF_UCHAR);
957
958 /* Test for NULL */
959 if(tempbuffer == NULL) {
960 *pErrorCode = U_MEMORY_ALLOCATION_ERROR;
961 return 0;
962 }
963
964 uprv_memset(tempbuffer, 0, (sourceLength+1)*U_SIZEOF_UCHAR);
965
966 while(dest[inpsize-1] == SPACE_CHAR) {
967 countr++;
968 inpsize--;
969 }
970
971 i = sourceLength - countr - 1;
972 j = sourceLength - 1;
973
974 while(i >= 0 && j >= 0) {
975 if( countr>0 && isLamAlefChar(dest[i]) ) {
976 tempbuffer[j] = LAM_CHAR;
977 tempbuffer[j-1] = convertLamAlef[ dest[i] - 0xFEF5 ];
978 j--;
979 countr--;
980 }else {
981 if ((countr == 0) && isLamAlefChar(dest[i]) ) {
982 *pErrorCode=U_NO_SPACE_AVAILABLE;
983 }
984 tempbuffer[j] = dest[i];
985 }
986 i--;
987 j--;
988 }
989
990 if(countr > 0) {
991 uprv_memmove(tempbuffer, tempbuffer+countr, sourceLength*U_SIZEOF_UCHAR);
992 if(u_strlen(tempbuffer) < sourceLength) {
993 for(i=sourceLength-1;i>=sourceLength-countr;i--) {
994 tempbuffer[i] = SPACE_CHAR;
995 }
996 }
997 }
998 uprv_memcpy(dest, tempbuffer, sourceLength*U_SIZEOF_UCHAR);
999
1000 uprv_free(tempbuffer);
1001
1002 destSize = sourceLength;
1003 return destSize;
1004 }
1005
1006 /*
1007 *Name : expandCompositCharAtNear
1008 *Function : Expands the LamAlef character into Lam + Alef, YehHamza character
1009 * into Yeh + Hamza, SeenFamily character into SeenFamily character
1010 * + Tail, while consuming the space next to the character.
1011 * If there are no spaces next to the character, an error
1012 * will be set to U_NO_SPACE_AVAILABLE as defined in utypes.h
1013 */
1014
1015 static int32_t
1016 expandCompositCharAtNear(UChar *dest, int32_t sourceLength, int32_t destSize,UErrorCode *pErrorCode,
1017 int yehHamzaOption, int seenTailOption, int lamAlefOption) {
1018 int32_t i = 0;
1019
1020
1021 UChar lamalefChar, yehhamzaChar;
1022
1023 for(i = 0 ;i<=sourceLength-1;i++) {
1024 if (seenTailOption && isSeenTailFamilyChar(dest[i])) {
1025 if ((i>0) && (dest[i-1] == SPACE_CHAR) ) {
1026 dest[i-1] = tailChar;
1027 }else {
1028 *pErrorCode=U_NO_SPACE_AVAILABLE;
1029 }
1030 }else if(yehHamzaOption && (isYehHamzaChar(dest[i])) ) {
1031 if ((i>0) && (dest[i-1] == SPACE_CHAR) ) {
1032 yehhamzaChar = dest[i];
1033 dest[i] = yehHamzaToYeh[yehhamzaChar - YEH_HAMZAFE_CHAR];
1034 dest[i-1] = HAMZAFE_CHAR;
1035 }else {
1036
1037 *pErrorCode=U_NO_SPACE_AVAILABLE;
1038 }
1039 }else if(lamAlefOption && isLamAlefChar(dest[i+1])) {
1040 if(dest[i] == SPACE_CHAR){
1041 lamalefChar = dest[i+1];
1042 dest[i+1] = LAM_CHAR;
1043 dest[i] = convertLamAlef[ lamalefChar - 0xFEF5 ];
1044 }else {
1045 *pErrorCode=U_NO_SPACE_AVAILABLE;
1046 }
1047 }
1048 }
1049 destSize = sourceLength;
1050 return destSize;
1051 }
1052 /*
1053 * Name : expandCompositChar
1054 * Function : LamAlef, need special handling, since it expands from one
1055 * character into two characters while shaping or deshaping.
1056 * In order to expand it, near or far spaces according to the
1057 * options user specifies. Also buffer size can be increased.
1058 *
1059 * For SeenFamily characters and YehHamza only the near option is
1060 * supported, while for LamAlef we can take spaces from begin, end,
1061 * near or even increase the buffer size.
1062 * There is also the Auto option for LamAlef only, which will first
1063 * search for a space at end, begin then near, respectively.
1064 * If there are no spaces to expand these characters, an error will be set to
1065 * U_NO_SPACE_AVAILABLE as defined in utypes.h
1066 */
1067
1068 static int32_t
1069 expandCompositChar(UChar *dest, int32_t sourceLength,
1070 int32_t destSize,uint32_t options,
1071 UErrorCode *pErrorCode, int shapingMode) {
1072
1073 int32_t i = 0,j = 0;
1074
1075 UChar *tempbuffer=NULL;
1076 int yehHamzaOption = 0;
1077 int seenTailOption = 0;
1078 int lamAlefOption = 0;
1079
1080 if (shapingMode == 1){
1081 if ( (options&U_SHAPE_LAMALEF_MASK) == U_SHAPE_LAMALEF_AUTO){
1082
1083 if(spacesRelativeToTextBeginEnd == 0) {
1084 destSize = expandCompositCharAtEnd(dest, sourceLength, destSize, pErrorCode);
1085
1086 if(*pErrorCode == U_NO_SPACE_AVAILABLE) {
1087 *pErrorCode = U_ZERO_ERROR;
1088 destSize = expandCompositCharAtBegin(dest, sourceLength, destSize, pErrorCode);
1089 }
1090 }else {
1091 destSize = expandCompositCharAtBegin(dest, sourceLength, destSize, pErrorCode);
1092
1093 if(*pErrorCode == U_NO_SPACE_AVAILABLE) {
1094 *pErrorCode = U_ZERO_ERROR;
1095 destSize = expandCompositCharAtEnd(dest, sourceLength, destSize, pErrorCode);
1096 }
1097 }
1098
1099 if(*pErrorCode == U_NO_SPACE_AVAILABLE) {
1100 *pErrorCode = U_ZERO_ERROR;
1101 destSize = expandCompositCharAtNear(dest, sourceLength, destSize, pErrorCode, yehHamzaOption,
1102 seenTailOption, 1);
1103 }
1104 }
1105 }
1106
1107 if (shapingMode == 1){
1108 if ( (options&U_SHAPE_LAMALEF_MASK) == uShapeLamalefEnd){
1109 destSize = expandCompositCharAtEnd(dest, sourceLength, destSize, pErrorCode);
1110 }
1111 }
1112
1113 if (shapingMode == 1){
1114 if ( (options&U_SHAPE_LAMALEF_MASK) == uShapeLamalefBegin){
1115 destSize = expandCompositCharAtBegin(dest, sourceLength, destSize, pErrorCode);
1116 }
1117 }
1118
1119 if (shapingMode == 0){
1120 if ((options&U_SHAPE_YEHHAMZA_MASK) == U_SHAPE_YEHHAMZA_TWOCELL_NEAR){
1121 yehHamzaOption = 1;
1122 }
1123 if ((options&U_SHAPE_SEEN_MASK) == U_SHAPE_SEEN_TWOCELL_NEAR){
1124 seenTailOption = 1;
1125 }
1126 }
1127 if (shapingMode == 1) {
1128 if ( (options&U_SHAPE_LAMALEF_MASK) == U_SHAPE_LAMALEF_NEAR) {
1129 lamAlefOption = 1;
1130 }
1131 }
1132
1133
1134 if (yehHamzaOption || seenTailOption || lamAlefOption){
1135 destSize = expandCompositCharAtNear(dest, sourceLength, destSize, pErrorCode, yehHamzaOption,
1136 seenTailOption,lamAlefOption);
1137 }
1138
1139
1140 if (shapingMode == 1){
1141 if ( (options&U_SHAPE_LAMALEF_MASK) == U_SHAPE_LAMALEF_RESIZE){
1142 destSize = calculateSize(dest,sourceLength,destSize,options);
1143 tempbuffer = (UChar *)uprv_malloc((destSize+1)*U_SIZEOF_UCHAR);
1144
1145 /* Test for NULL */
1146 if(tempbuffer == NULL) {
1147 *pErrorCode = U_MEMORY_ALLOCATION_ERROR;
1148 return 0;
1149 }
1150
1151 uprv_memset(tempbuffer, 0, (destSize+1)*U_SIZEOF_UCHAR);
1152
1153 i = j = 0;
1154 while(i < destSize && j < destSize) {
1155 if(isLamAlefChar(dest[i]) ) {
1156 tempbuffer[j] = convertLamAlef[ dest[i] - 0xFEF5 ];
1157 tempbuffer[j+1] = LAM_CHAR;
1158 j++;
1159 }else {
1160 tempbuffer[j] = dest[i];
1161 }
1162 i++;
1163 j++;
1164 }
1165
1166 uprv_memcpy(dest, tempbuffer, destSize*U_SIZEOF_UCHAR);
1167 }
1168 }
1169
1170 if(tempbuffer) {
1171 uprv_free(tempbuffer);
1172 }
1173 return destSize;
1174 }
1175
1176 /*
1177 *Name : shapeUnicode
1178 *Function : Converts an Arabic Unicode buffer in 06xx Range into a shaped
1179 * arabic Unicode buffer in FExx Range
1180 */
1181 static int32_t
1182 shapeUnicode(UChar *dest, int32_t sourceLength,
1183 int32_t destSize,uint32_t options,
1184 UErrorCode *pErrorCode,
1185 int tashkeelFlag) {
1186
1187 int32_t i, iend;
1188 int32_t step;
1189 int32_t lastPos,Nx, Nw;
1190 unsigned int Shape;
1191 int32_t lamalef_found = 0;
1192 int32_t seenfamFound = 0, yehhamzaFound =0, tashkeelFound = 0;
1193 UChar prevLink = 0, lastLink = 0, currLink, nextLink = 0;
1194 UChar wLamalef;
1195
1196 /*
1197 * Converts the input buffer from FExx Range into 06xx Range
1198 * to make sure that all characters are in the 06xx range
1199 * even the lamalef is converted to the special region in
1200 * the 06xx range
1201 */
1202 if ((options & U_SHAPE_PRESERVE_PRESENTATION_MASK) == U_SHAPE_PRESERVE_PRESENTATION_NOOP) {
1203 for (i = 0; i < sourceLength; i++) {
1204 UChar inputChar = dest[i];
1205 if ( (inputChar >= 0xFB50) && (inputChar <= 0xFBFF)) {
1206 UChar c = convertFBto06 [ (inputChar - 0xFB50) ];
1207 if (c != 0)
1208 dest[i] = c;
1209 } else if ( (inputChar >= 0xFE70) && (inputChar <= 0xFEFC)) {
1210 dest[i] = convertFEto06 [ (inputChar - 0xFE70) ] ;
1211 } else {
1212 dest[i] = inputChar ;
1213 }
1214 }
1215 }
1216
1217
1218 /* sets the index to the end of the buffer, together with the step point to -1 */
1219 i = sourceLength - 1;
1220 iend = -1;
1221 step = -1;
1222
1223 /*
1224 * This function resolves the link between the characters .
1225 * Arabic characters have four forms :
1226 * Isolated Form, Initial Form, Middle Form and Final Form
1227 */
1228 currLink = getLink(dest[i]);
1229
1230 lastPos = i;
1231 Nx = -2, Nw = 0;
1232
1233 while (i != iend) {
1234 /* If high byte of currLink > 0 then more than one shape */
1235 if ((currLink & 0xFF00) > 0 || (getLink(dest[i]) & IRRELEVANT) != 0) {
1236 Nw = i + step;
1237 while (Nx < 0) { /* we need to know about next char */
1238 if(Nw == iend) {
1239 nextLink = 0;
1240 Nx = 3000;
1241 } else {
1242 nextLink = getLink(dest[Nw]);
1243 if((nextLink & IRRELEVANT) == 0) {
1244 Nx = Nw;
1245 } else {
1246 Nw = Nw + step;
1247 }
1248 }
1249 }
1250
1251 if ( ((currLink & ALEFTYPE) > 0) && ((lastLink & LAMTYPE) > 0) ) {
1252 lamalef_found = 1;
1253 wLamalef = changeLamAlef(dest[i]); /*get from 0x065C-0x065f */
1254 if ( wLamalef != 0) {
1255 dest[i] = LAMALEF_SPACE_SUB; /* The default case is to drop the Alef and replace */
1256 dest[lastPos] =wLamalef; /* it by LAMALEF_SPACE_SUB which is the last character in the */
1257 i=lastPos; /* unicode private use area, this is done to make */
1258 } /* sure that removeLamAlefSpaces() handles only the */
1259 lastLink = prevLink; /* spaces generated during lamalef generation. */
1260 currLink = getLink(wLamalef); /* LAMALEF_SPACE_SUB is added here and is replaced by spaces */
1261 } /* in removeLamAlefSpaces() */
1262
1263 if ((i > 0) && (dest[i-1] == SPACE_CHAR)){
1264 if ( isSeenFamilyChar(dest[i])){
1265 seenfamFound = 1;
1266 } else if (dest[i] == YEH_HAMZA_CHAR) {
1267 yehhamzaFound = 1;
1268 }
1269 }
1270 else if(i==0){
1271 if ( isSeenFamilyChar(dest[i])){
1272 seenfamFound = 1;
1273 } else if (dest[i] == YEH_HAMZA_CHAR) {
1274 yehhamzaFound = 1;
1275 }
1276 }
1277
1278 /*
1279 * get the proper shape according to link ability of neighbors
1280 * and of character; depends on the order of the shapes
1281 * (isolated, initial, middle, final) in the compatibility area
1282 */
1283 Shape = shapeTable[nextLink & (LINKR + LINKL)]
1284 [lastLink & (LINKR + LINKL)]
1285 [currLink & (LINKR + LINKL)];
1286
1287 if ((currLink & (LINKR+LINKL)) == 1) {
1288 Shape &= 1;
1289 } else if(isTashkeelChar(dest[i])) {
1290 if( (lastLink & LINKL) && (nextLink & LINKR) && (tashkeelFlag == 1) &&
1291 dest[i] != 0x064C && dest[i] != 0x064D )
1292 {
1293 Shape = 1;
1294 if( (nextLink&ALEFTYPE) == ALEFTYPE && (lastLink&LAMTYPE) == LAMTYPE ) {
1295 Shape = 0;
1296 }
1297 }
1298 else {
1299 Shape = 0;
1300 }
1301 }
1302 if ((dest[i] ^ 0x0600) < 0x100) {
1303 if ( isTashkeelChar(dest[i]) ){
1304 if (tashkeelFlag == 2){
1305 dest[i] = TASHKEEL_SPACE_SUB;
1306 tashkeelFound = 1;
1307 } else {
1308 /* to ensure the array index is within the range */
1309 U_ASSERT(dest[i] >= 0x064Bu
1310 && dest[i]-0x064Bu < sizeof(IrrelevantPos)/sizeof(IrrelevantPos[0]));
1311 dest[i] = 0xFE70 + IrrelevantPos[(dest[i] - 0x064B)] + Shape;
1312 }
1313 }else if ((currLink & APRESENT) > 0) {
1314 dest[i] = (UChar)(0xFB50 + (currLink >> 8) + Shape);
1315 }else if ((currLink >> 8) > 0 && (currLink & IRRELEVANT) == 0) {
1316 dest[i] = (UChar)(0xFE70 + (currLink >> 8) + Shape);
1317 }
1318 }
1319 }
1320
1321 /* move one notch forward */
1322 if ((currLink & IRRELEVANT) == 0) {
1323 prevLink = lastLink;
1324 lastLink = currLink;
1325 lastPos = i;
1326 }
1327
1328 i = i + step;
1329 if (i == Nx) {
1330 currLink = nextLink;
1331 Nx = -2;
1332 } else if(i != iend) {
1333 currLink = getLink(dest[i]);
1334 }
1335 }
1336 destSize = sourceLength;
1337 if ( (lamalef_found != 0 ) || (tashkeelFound != 0) ){
1338 destSize = handleGeneratedSpaces(dest,sourceLength,destSize,options,pErrorCode);
1339 }
1340
1341 if ( (seenfamFound != 0) || (yehhamzaFound != 0) ) {
1342 destSize = expandCompositChar(dest, sourceLength,destSize,options,pErrorCode, SHAPE_MODE);
1343 }
1344 return destSize;
1345 }
1346
1347 /*
1348 *Name : deShapeUnicode
1349 *Function : Converts an Arabic Unicode buffer in FExx Range into unshaped
1350 * arabic Unicode buffer in 06xx Range
1351 */
1352 static int32_t
1353 deShapeUnicode(UChar *dest, int32_t sourceLength,
1354 int32_t destSize,uint32_t options,
1355 UErrorCode *pErrorCode) {
1356 int32_t i = 0;
1357 int32_t lamalef_found = 0;
1358 int32_t yehHamzaComposeEnabled = 0;
1359 int32_t seenComposeEnabled = 0;
1360
1361 yehHamzaComposeEnabled = ((options&U_SHAPE_YEHHAMZA_MASK) == U_SHAPE_YEHHAMZA_TWOCELL_NEAR) ? 1 : 0;
1362 seenComposeEnabled = ((options&U_SHAPE_SEEN_MASK) == U_SHAPE_SEEN_TWOCELL_NEAR)? 1 : 0;
1363
1364 /*
1365 *This for loop changes the buffer from the Unicode FE range to
1366 *the Unicode 06 range
1367 */
1368
1369 for(i = 0; i < sourceLength; i++) {
1370 UChar inputChar = dest[i];
1371 if ( (inputChar >= 0xFB50) && (inputChar <= 0xFBFF)) { /* FBxx Arabic range */
1372 UChar c = convertFBto06 [ (inputChar - 0xFB50) ];
1373 if (c != 0)
1374 dest[i] = c;
1375 } else if( (yehHamzaComposeEnabled == 1) && ((inputChar == HAMZA06_CHAR) || (inputChar == HAMZAFE_CHAR))
1376 && (i < (sourceLength - 1)) && isAlefMaksouraChar(dest[i+1] )) {
1377 dest[i] = SPACE_CHAR;
1378 dest[i+1] = YEH_HAMZA_CHAR;
1379 } else if ( (seenComposeEnabled == 1) && (isTailChar(inputChar)) && (i< (sourceLength - 1))
1380 && (isSeenTailFamilyChar(dest[i+1])) ) {
1381 dest[i] = SPACE_CHAR;
1382 } else if (( inputChar >= 0xFE70) && (inputChar <= 0xFEF4 )) { /* FExx Arabic range */
1383 dest[i] = convertFEto06 [ (inputChar - 0xFE70) ];
1384 } else {
1385 dest[i] = inputChar ;
1386 }
1387
1388 if( isLamAlefChar(dest[i]) )
1389 lamalef_found = 1;
1390 }
1391
1392 destSize = sourceLength;
1393 if (lamalef_found != 0){
1394 destSize = expandCompositChar(dest,sourceLength,destSize,options,pErrorCode,DESHAPE_MODE);
1395 }
1396 return destSize;
1397 }
1398
1399 /*
1400 ****************************************
1401 * u_shapeArabic
1402 ****************************************
1403 */
1404
1405 U_CAPI int32_t U_EXPORT2
1406 u_shapeArabic(const UChar *source, int32_t sourceLength,
1407 UChar *dest, int32_t destCapacity,
1408 uint32_t options,
1409 UErrorCode *pErrorCode) {
1410
1411 int32_t destLength;
1412
1413 spacesRelativeToTextBeginEnd = 0;
1414 uShapeLamalefBegin = U_SHAPE_LAMALEF_BEGIN;
1415 uShapeLamalefEnd = U_SHAPE_LAMALEF_END;
1416 uShapeTashkeelBegin = U_SHAPE_TASHKEEL_BEGIN;
1417 uShapeTashkeelEnd = U_SHAPE_TASHKEEL_END;
1418
1419 /* usual error checking */
1420 if(pErrorCode==NULL || U_FAILURE(*pErrorCode)) {
1421 return 0;
1422 }
1423
1424 /* make sure that no reserved options values are used; allow dest==NULL only for preflighting */
1425 if( source==NULL || sourceLength<-1 || (dest==NULL && destCapacity!=0) || destCapacity<0 ||
1426 (((options&U_SHAPE_TASHKEEL_MASK) > 0) &&
1427 ((options&U_SHAPE_LETTERS_SHAPE_TASHKEEL_ISOLATED) == U_SHAPE_LETTERS_SHAPE_TASHKEEL_ISOLATED) ) ||
1428 (((options&U_SHAPE_TASHKEEL_MASK) > 0) &&
1429 ((options&U_SHAPE_LETTERS_MASK) == U_SHAPE_LETTERS_UNSHAPE)) ||
1430 (options&U_SHAPE_DIGIT_TYPE_RESERVED)==U_SHAPE_DIGIT_TYPE_RESERVED ||
1431 (options&U_SHAPE_DIGITS_MASK)==U_SHAPE_DIGITS_RESERVED ||
1432 ((options&U_SHAPE_LAMALEF_MASK) != U_SHAPE_LAMALEF_RESIZE &&
1433 (options&U_SHAPE_AGGREGATE_TASHKEEL_MASK) != 0) ||
1434 ((options&U_SHAPE_AGGREGATE_TASHKEEL_MASK) == U_SHAPE_AGGREGATE_TASHKEEL &&
1435 (options&U_SHAPE_LETTERS_SHAPE_TASHKEEL_ISOLATED) != U_SHAPE_LETTERS_SHAPE_TASHKEEL_ISOLATED)
1436 )
1437 {
1438 *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR;
1439 return 0;
1440 }
1441 /* Validate lamalef options */
1442 if(((options&U_SHAPE_LAMALEF_MASK) > 0)&&
1443 !(((options & U_SHAPE_LAMALEF_MASK)==U_SHAPE_LAMALEF_BEGIN) ||
1444 ((options & U_SHAPE_LAMALEF_MASK)==U_SHAPE_LAMALEF_END ) ||
1445 ((options & U_SHAPE_LAMALEF_MASK)==U_SHAPE_LAMALEF_RESIZE )||
1446 ((options & U_SHAPE_LAMALEF_MASK)==U_SHAPE_LAMALEF_AUTO) ||
1447 ((options & U_SHAPE_LAMALEF_MASK)==U_SHAPE_LAMALEF_NEAR)))
1448 {
1449 *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR;
1450 return 0;
1451 }
1452 /* Validate Tashkeel options */
1453 if(((options&U_SHAPE_TASHKEEL_MASK) > 0)&&
1454 !(((options & U_SHAPE_TASHKEEL_MASK)==U_SHAPE_TASHKEEL_BEGIN) ||
1455 ((options & U_SHAPE_TASHKEEL_MASK)==U_SHAPE_TASHKEEL_END )
1456 ||((options & U_SHAPE_TASHKEEL_MASK)==U_SHAPE_TASHKEEL_RESIZE )||
1457 ((options & U_SHAPE_TASHKEEL_MASK)==U_SHAPE_TASHKEEL_REPLACE_BY_TATWEEL)))
1458 {
1459 *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR;
1460 return 0;
1461 }
1462 /* determine the source length */
1463 if(sourceLength==-1) {
1464 sourceLength=u_strlen(source);
1465 }
1466 if(sourceLength<=0) {
1467 return u_terminateUChars(dest, destCapacity, 0, pErrorCode);
1468 }
1469
1470 /* check that source and destination do not overlap */
1471 if( dest!=NULL &&
1472 ((source<=dest && dest<source+sourceLength) ||
1473 (dest<=source && source<dest+destCapacity))) {
1474 *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR;
1475 return 0;
1476 }
1477
1478 /* Does Options contain the new Seen Tail Unicode code point option */
1479 if ( (options&U_SHAPE_TAIL_TYPE_MASK) == U_SHAPE_TAIL_NEW_UNICODE){
1480 tailChar = NEW_TAIL_CHAR;
1481 }else {
1482 tailChar = OLD_TAIL_CHAR;
1483 }
1484
1485 if((options&U_SHAPE_LETTERS_MASK)!=U_SHAPE_LETTERS_NOOP) {
1486 UChar buffer[300];
1487 UChar *tempbuffer, *tempsource = NULL;
1488 int32_t outputSize, spacesCountl=0, spacesCountr=0;
1489
1490 if((options&U_SHAPE_AGGREGATE_TASHKEEL_MASK)>0) {
1491 int32_t logical_order = (options&U_SHAPE_TEXT_DIRECTION_MASK) == U_SHAPE_TEXT_DIRECTION_LOGICAL;
1492 int32_t aggregate_tashkeel =
1493 (options&(U_SHAPE_AGGREGATE_TASHKEEL_MASK+U_SHAPE_LETTERS_SHAPE_TASHKEEL_ISOLATED)) ==
1494 (U_SHAPE_AGGREGATE_TASHKEEL+U_SHAPE_LETTERS_SHAPE_TASHKEEL_ISOLATED);
1495 int step=logical_order?1:-1;
1496 int j=logical_order?-1:2*sourceLength;
1497 int i=logical_order?-1:sourceLength;
1498 int end=logical_order?sourceLength:-1;
1499 int aggregation_possible = 1;
1500 UChar prev = 0;
1501 UChar prevLink, currLink = 0;
1502 int newSourceLength = 0;
1503 tempsource = (UChar *)uprv_malloc(2*sourceLength*U_SIZEOF_UCHAR);
1504 if(tempsource == NULL) {
1505 *pErrorCode = U_MEMORY_ALLOCATION_ERROR;
1506 return 0;
1507 }
1508
1509 while ((i+=step) != end) {
1510 prevLink = currLink;
1511 currLink = getLink(source[i]);
1512 if (aggregate_tashkeel && ((prevLink|currLink)&COMBINE) == COMBINE && aggregation_possible) {
1513 aggregation_possible = 0;
1514 tempsource[j] = (prev<source[i]?prev:source[i])-0x064C+0xFC5E;
1515 currLink = getLink(tempsource[j]);
1516 } else {
1517 aggregation_possible = 1;
1518 tempsource[j+=step] = source[i];
1519 prev = source[i];
1520 newSourceLength++;
1521 }
1522 }
1523 source = tempsource+(logical_order?0:j);
1524 sourceLength = newSourceLength;
1525 }
1526
1527 /* calculate destination size */
1528 /* TODO: do we ever need to do this pure preflighting? */
1529 if(((options&U_SHAPE_LAMALEF_MASK)==U_SHAPE_LAMALEF_RESIZE) ||
1530 ((options&U_SHAPE_TASHKEEL_MASK)==U_SHAPE_TASHKEEL_RESIZE)) {
1531 outputSize=calculateSize(source,sourceLength,destCapacity,options);
1532 } else {
1533 outputSize=sourceLength;
1534 }
1535
1536 if(outputSize>destCapacity) {
1537 *pErrorCode=U_BUFFER_OVERFLOW_ERROR;
1538 if (tempsource != NULL) uprv_free(tempsource);
1539 return outputSize;
1540 }
1541
1542 /*
1543 * need a temporary buffer of size max(outputSize, sourceLength)
1544 * because at first we copy source->temp
1545 */
1546 if(sourceLength>outputSize) {
1547 outputSize=sourceLength;
1548 }
1549
1550 /* Start of Arabic letter shaping part */
1551 if(outputSize<=LENGTHOF(buffer)) {
1552 outputSize=LENGTHOF(buffer);
1553 tempbuffer=buffer;
1554 } else {
1555 tempbuffer = (UChar *)uprv_malloc(outputSize*U_SIZEOF_UCHAR);
1556
1557 /*Test for NULL*/
1558 if(tempbuffer == NULL) {
1559 *pErrorCode = U_MEMORY_ALLOCATION_ERROR;
1560 if (tempsource != NULL) uprv_free(tempsource);
1561 return 0;
1562 }
1563 }
1564 uprv_memcpy(tempbuffer, source, sourceLength*U_SIZEOF_UCHAR);
1565 if (tempsource != NULL){
1566 uprv_free(tempsource);
1567 }
1568
1569 if(sourceLength<outputSize) {
1570 uprv_memset(tempbuffer+sourceLength, 0, (outputSize-sourceLength)*U_SIZEOF_UCHAR);
1571 }
1572
1573 if((options&U_SHAPE_TEXT_DIRECTION_MASK) == U_SHAPE_TEXT_DIRECTION_LOGICAL) {
1574 countSpaces(tempbuffer,sourceLength,options,&spacesCountl,&spacesCountr);
1575 invertBuffer(tempbuffer,sourceLength,options,spacesCountl,spacesCountr);
1576 }
1577
1578 if((options&U_SHAPE_TEXT_DIRECTION_MASK) == U_SHAPE_TEXT_DIRECTION_VISUAL_LTR) {
1579 if((options&U_SHAPE_SPACES_RELATIVE_TO_TEXT_MASK) == U_SHAPE_SPACES_RELATIVE_TO_TEXT_BEGIN_END) {
1580 spacesRelativeToTextBeginEnd = 1;
1581 uShapeLamalefBegin = U_SHAPE_LAMALEF_END;
1582 uShapeLamalefEnd = U_SHAPE_LAMALEF_BEGIN;
1583
1584 uShapeTashkeelBegin = U_SHAPE_TASHKEEL_END;
1585 uShapeTashkeelEnd = U_SHAPE_TASHKEEL_BEGIN;
1586 }
1587 }
1588
1589 switch(options&U_SHAPE_LETTERS_MASK) {
1590 case U_SHAPE_LETTERS_SHAPE :
1591 if( (options&U_SHAPE_TASHKEEL_MASK)> 0
1592 && ((options&U_SHAPE_TASHKEEL_MASK) !=U_SHAPE_TASHKEEL_REPLACE_BY_TATWEEL)) {
1593 /* Call the shaping function with tashkeel flag == 2 for removal of tashkeel */
1594 destLength = shapeUnicode(tempbuffer,sourceLength,destCapacity,options,pErrorCode,2);
1595 }else {
1596 /* default Call the shaping function with tashkeel flag == 1 */
1597 destLength = shapeUnicode(tempbuffer,sourceLength,destCapacity,options,pErrorCode,1);
1598
1599 /*After shaping text check if user wants to remove tashkeel and replace it with tatweel*/
1600 if( (options&U_SHAPE_TASHKEEL_MASK) == U_SHAPE_TASHKEEL_REPLACE_BY_TATWEEL){
1601 destLength = handleTashkeelWithTatweel(tempbuffer,destLength,destCapacity,options,pErrorCode);
1602 }
1603 }
1604 break;
1605 case U_SHAPE_LETTERS_SHAPE_TASHKEEL_ISOLATED :
1606 /* Call the shaping function with tashkeel flag == 0 */
1607 destLength = shapeUnicode(tempbuffer,sourceLength,destCapacity,options,pErrorCode,0);
1608 break;
1609
1610 case U_SHAPE_LETTERS_UNSHAPE :
1611 /* Call the deshaping function */
1612 destLength = deShapeUnicode(tempbuffer,sourceLength,destCapacity,options,pErrorCode);
1613 break;
1614 default :
1615 /* will never occur because of validity checks above */
1616 destLength = 0;
1617 break;
1618 }
1619
1620 /*
1621 * TODO: (markus 2002aug01)
1622 * For as long as we always preflight the outputSize above
1623 * we should U_ASSERT(outputSize==destLength)
1624 * except for the adjustment above before the tempbuffer allocation
1625 */
1626
1627 if((options&U_SHAPE_TEXT_DIRECTION_MASK) == U_SHAPE_TEXT_DIRECTION_LOGICAL) {
1628 countSpaces(tempbuffer,destLength,options,&spacesCountl,&spacesCountr);
1629 invertBuffer(tempbuffer,destLength,options,spacesCountl,spacesCountr);
1630 }
1631 uprv_memcpy(dest, tempbuffer, uprv_min(destLength, destCapacity)*U_SIZEOF_UCHAR);
1632
1633 if(tempbuffer!=buffer) {
1634 uprv_free(tempbuffer);
1635 }
1636
1637 if(destLength>destCapacity) {
1638 *pErrorCode=U_BUFFER_OVERFLOW_ERROR;
1639 return destLength;
1640 }
1641
1642 /* End of Arabic letter shaping part */
1643 } else {
1644 /*
1645 * No letter shaping:
1646 * just make sure the destination is large enough and copy the string.
1647 */
1648 if(destCapacity<sourceLength) {
1649 /* this catches preflighting, too */
1650 *pErrorCode=U_BUFFER_OVERFLOW_ERROR;
1651 return sourceLength;
1652 }
1653 uprv_memcpy(dest, source, sourceLength*U_SIZEOF_UCHAR);
1654 destLength=sourceLength;
1655 }
1656
1657 /*
1658 * Perform number shaping.
1659 * With UTF-16 or UTF-32, the length of the string is constant.
1660 * The easiest way to do this is to operate on the destination and
1661 * "shape" the digits in-place.
1662 */
1663 if((options&U_SHAPE_DIGITS_MASK)!=U_SHAPE_DIGITS_NOOP) {
1664 UChar digitBase;
1665 int32_t i;
1666
1667 /* select the requested digit group */
1668 switch(options&U_SHAPE_DIGIT_TYPE_MASK) {
1669 case U_SHAPE_DIGIT_TYPE_AN:
1670 digitBase=0x660; /* Unicode: "Arabic-Indic digits" */
1671 break;
1672 case U_SHAPE_DIGIT_TYPE_AN_EXTENDED:
1673 digitBase=0x6f0; /* Unicode: "Eastern Arabic-Indic digits (Persian and Urdu)" */
1674 break;
1675 default:
1676 /* will never occur because of validity checks above */
1677 digitBase=0;
1678 break;
1679 }
1680
1681 /* perform the requested operation */
1682 switch(options&U_SHAPE_DIGITS_MASK) {
1683 case U_SHAPE_DIGITS_EN2AN:
1684 /* add (digitBase-'0') to each European (ASCII) digit code point */
1685 digitBase-=0x30;
1686 for(i=0; i<destLength; ++i) {
1687 if(((uint32_t)dest[i]-0x30)<10) {
1688 dest[i]+=digitBase;
1689 }
1690 }
1691 break;
1692 case U_SHAPE_DIGITS_AN2EN:
1693 /* subtract (digitBase-'0') from each Arabic digit code point */
1694 for(i=0; i<destLength; ++i) {
1695 if(((uint32_t)dest[i]-(uint32_t)digitBase)<10) {
1696 dest[i]-=digitBase-0x30;
1697 }
1698 }
1699 break;
1700 case U_SHAPE_DIGITS_ALEN2AN_INIT_LR:
1701 _shapeToArabicDigitsWithContext(dest, destLength,
1702 digitBase,
1703 (UBool)((options&U_SHAPE_TEXT_DIRECTION_MASK)==U_SHAPE_TEXT_DIRECTION_LOGICAL),
1704 FALSE);
1705 break;
1706 case U_SHAPE_DIGITS_ALEN2AN_INIT_AL:
1707 _shapeToArabicDigitsWithContext(dest, destLength,
1708 digitBase,
1709 (UBool)((options&U_SHAPE_TEXT_DIRECTION_MASK)==U_SHAPE_TEXT_DIRECTION_LOGICAL),
1710 TRUE);
1711 break;
1712 default:
1713 /* will never occur because of validity checks above */
1714 break;
1715 }
1716 }
1717
1718 return u_terminateUChars(dest, destCapacity, destLength, pErrorCode);
1719 }