2 ******************************************************************************
4 * Copyright (C) 2000-2012, International Business Machines
5 * Corporation and others. All Rights Reserved.
7 ******************************************************************************
8 * file name: ushape.cpp
10 * tab size: 8 (not used)
13 * created on: 2000jun29
14 * created by: Markus W. Scherer
16 * Arabic letter shaping implemented by Ayman Roshdy
19 #include "unicode/utypes.h"
20 #include "unicode/uchar.h"
21 #include "unicode/ustring.h"
22 #include "unicode/ushape.h"
26 #include "ubidi_props.h"
29 #define LENGTHOF(array) (int32_t)(sizeof(array)/sizeof((array)[0]))
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
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
49 /* definitions for Arabic letter shaping ------------------------------------ */
59 #define COMBINE (SHADDA+CSHADDA)
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
76 #define DESHAPE_MODE 1
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;
85 static const uint8_t tailFamilyIsolatedFinal
[] = {
102 static const uint8_t tashkeelMedial
[] = {
121 static const UChar yehHamzaToYeh
[] =
123 /* isolated*/ 0xFEEF,
127 static const uint8_t IrrelevantPos
[] = {
133 static const UChar convertLamAlef
[] =
145 static const UChar araLink
[178]=
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*/
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*/
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*/
226 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /*0x06C2-0x06CB*/
227 1+2+8+256 * 0xAC, /*0x06CC*/
229 1+2, 1+2, 1+2, 1+2, /*0x06CE-0x06D1*/
230 1, 1 /*0x06D2-0x06D3*/
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,
255 static const uint8_t presBLink
[]=
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
269 static const UChar convertFBto06
[] =
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
285 static const UChar convertFEto06
[] =
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
299 static const uint8_t shapeTable
[4][4][4]=
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} }
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).
314 _shapeToArabicDigitsWithContext(UChar
*s
, int32_t length
,
316 UBool isLogical
, UBool lastStrongWasAL
) {
317 const UBiDiProps
*bdp
;
321 bdp
=ubidi_getSingleton();
324 /* the iteration direction depends on the type of input */
326 for(i
=0; i
<length
; ++i
) {
328 switch(ubidi_getClass(bdp
, c
)) {
329 case U_LEFT_TO_RIGHT
: /* L */
330 case U_RIGHT_TO_LEFT
: /* R */
331 lastStrongWasAL
=FALSE
;
333 case U_RIGHT_TO_LEFT_ARABIC
: /* AL */
334 lastStrongWasAL
=TRUE
;
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 */
346 for(i
=length
; i
>0; /* pre-decrement in the body */) {
348 switch(ubidi_getClass(bdp
, c
)) {
349 case U_LEFT_TO_RIGHT
: /* L */
350 case U_RIGHT_TO_LEFT
: /* R */
351 lastStrongWasAL
=FALSE
;
353 case U_RIGHT_TO_LEFT_ARABIC
: /* AL */
354 lastStrongWasAL
=TRUE
;
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 */
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
375 invertBuffer(UChar
*buffer
, int32_t size
, uint32_t /*options*/, int32_t lowlimit
, int32_t highlimit
) {
378 for(i
=lowlimit
,j
=size
-highlimit
-1;i
<j
;i
++,j
--) {
380 buffer
[i
] = buffer
[j
];
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.
394 changeLamAlef(UChar ch
) {
410 *Function : Resolves the link between the characters as
411 * Arabic characters have four forms :
412 * Isolated, Initial, Middle and Final Form
416 if(ch
>= 0x0622 && ch
<= 0x06D3) {
417 return(araLink
[ch
-0x0622]);
418 } else if(ch
== 0x200D) {
420 } else if(ch
>= 0x206D && ch
<= 0x206F) {
422 }else if(ch
>= 0xFB50 && ch
<= 0xFC62) {
423 return(presALink
[ch
-0xFB50]);
424 } else if(ch
>= 0xFE70 && ch
<= 0xFEFC) {
425 return(presBLink
[ch
-0xFE70]);
433 *Function : Counts the number of spaces
434 * at each end of the logical buffer
437 countSpaces(UChar
*dest
, int32_t size
, uint32_t /*options*/, int32_t *spacesCountl
, int32_t *spacesCountr
) {
439 int32_t countl
= 0,countr
= 0;
440 while((dest
[i
] == SPACE_CHAR
) && (countl
< size
)) {
444 if (countl
< size
) { /* the entire buffer is not all space */
445 while(dest
[size
-1] == SPACE_CHAR
) {
450 *spacesCountl
= countl
;
451 *spacesCountr
= countr
;
455 *Name : isTashkeelChar
456 *Function : Returns 1 for Tashkeel characters in 06 range else return 0
458 static inline int32_t
459 isTashkeelChar(UChar ch
) {
460 return (int32_t)( ch
>=0x064B && ch
<= 0x0652 );
464 *Name : isTashkeelCharFE
465 *Function : Returns 1 for Tashkeel characters in FE range else return 0
467 static inline int32_t
468 isTashkeelCharFE(UChar ch
) {
469 return (int32_t)( ch
>=0xFE70 && ch
<= 0xFE7F );
474 *Function : Returns 1 for Alef characters else return 0
476 static inline int32_t
477 isAlefChar(UChar ch
) {
478 return (int32_t)( (ch
==0x0622)||(ch
==0x0623)||(ch
==0x0625)||(ch
==0x0627) );
482 *Name : isLamAlefChar
483 *Function : Returns 1 for LamAlef characters else return 0
485 static inline int32_t
486 isLamAlefChar(UChar ch
) {
487 return (int32_t)((ch
>=0xFEF5)&&(ch
<=0xFEFC) );
492 *Function : returns 1 if the character matches one of the tail characters (0xfe73 or 0x200b) otherwise returns 0
495 static inline int32_t
496 isTailChar(UChar ch
) {
497 if(ch
== OLD_TAIL_CHAR
|| ch
== NEW_TAIL_CHAR
){
505 *Name : isSeenTailFamilyChar
506 *Function : returns 1 if the character is a seen family isolated character
507 * in the FE range otherwise returns 0
510 static inline int32_t
511 isSeenTailFamilyChar(UChar ch
) {
512 if(ch
>= 0xfeb1 && ch
< 0xfebf){
513 return tailFamilyIsolatedFinal
[ch
- 0xFEB1];
519 /* Name : isSeenFamilyChar
520 * Function : returns 1 if the character is a seen family character in the Unicode
521 * 06 range otherwise returns 0
524 static inline int32_t
525 isSeenFamilyChar(UChar ch
){
526 if(ch
>= 0x633 && ch
<= 0x636){
535 *Name : isAlefMaksouraChar
536 *Function : returns 1 if the character is a Alef Maksoura Final or isolated
537 * otherwise returns 0
539 static inline int32_t
540 isAlefMaksouraChar(UChar ch
) {
541 return (int32_t)( (ch
== 0xFEEF) || ( ch
== 0xFEF0) || (ch
== 0x0649));
545 * Name : isYehHamzaChar
546 * Function : returns 1 if the character is a yehHamza isolated or yehhamza
547 * final is found otherwise returns 0
549 static inline int32_t
550 isYehHamzaChar(UChar ch
) {
551 if((ch
==0xFE89)||(ch
==0xFE8A)){
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
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
)
569 return tashkeelMedial
[ch
- 0xFE70];
570 }else if( (ch
>= 0xfcf2 && ch
<= 0xfcf4) || (ch
== SHADDA_TATWEEL_CHAR
)) {
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
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){
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.
607 calculateSize(const UChar
*source
, int32_t sourceLength
,
608 int32_t destSize
,uint32_t options
) {
611 int lamAlefOption
= 0;
612 int tashkeelOption
= 0;
614 destSize
= sourceLength
;
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
)){
621 if((options
&U_SHAPE_LETTERS_MASK
) == U_SHAPE_LETTERS_SHAPE
&&
622 ((options
&U_SHAPE_TASHKEEL_MASK
) == U_SHAPE_TASHKEEL_RESIZE
) ){
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
])) ) {
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
])) ) {
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
]))
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.
664 handleTashkeelWithTatweel(UChar
*dest
, int32_t sourceLength
,
665 int32_t /*destSize*/, uint32_t /*options*/,
666 UErrorCode
* /*pErrorCode*/) {
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
;
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
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).
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.
702 handleGeneratedSpaces(UChar
*dest
, int32_t sourceLength
,
705 UErrorCode
*pErrorCode
) {
707 int32_t i
= 0, j
= 0;
709 UChar
*tempbuffer
=NULL
;
711 int lamAlefOption
= 0;
712 int tashkeelOption
= 0;
713 int shapingMode
= SHAPE_MODE
;
715 if (shapingMode
== 0){
716 if ( (options
&U_SHAPE_LAMALEF_MASK
) == U_SHAPE_LAMALEF_RESIZE
){
719 if ( (options
&U_SHAPE_TASHKEEL_MASK
) == U_SHAPE_TASHKEEL_RESIZE
){
724 if (lamAlefOption
|| tashkeelOption
){
725 tempbuffer
= (UChar
*)uprv_malloc((sourceLength
+1)*U_SIZEOF_UCHAR
);
727 if(tempbuffer
== NULL
) {
728 *pErrorCode
= U_MEMORY_ALLOCATION_ERROR
;
732 uprv_memset(tempbuffer
, 0, (sourceLength
+1)*U_SIZEOF_UCHAR
);
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
) ){
741 tempbuffer
[j
] = dest
[i
];
748 tempbuffer
[i
] = 0x0000;
753 uprv_memcpy(dest
, tempbuffer
, sourceLength
*U_SIZEOF_UCHAR
);
754 destSize
= u_strlen(dest
);
759 if (shapingMode
== 0){
760 if ( (options
&U_SHAPE_LAMALEF_MASK
) == U_SHAPE_LAMALEF_NEAR
){
766 /* Lam+Alef is already shaped into LamAlef + FFFF */
768 while(i
< sourceLength
) {
769 if(lamAlefOption
&&dest
[i
] == LAMALEF_SPACE_SUB
){
770 dest
[i
] = SPACE_CHAR
;
774 destSize
= sourceLength
;
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)) ) {
785 if ( (options
&U_SHAPE_TASHKEEL_MASK
) == uShapeTashkeelBegin
) {
790 if(lamAlefOption
|| tashkeelOption
){
791 tempbuffer
= (UChar
*)uprv_malloc((sourceLength
+1)*U_SIZEOF_UCHAR
);
794 if(tempbuffer
== NULL
) {
795 *pErrorCode
= U_MEMORY_ALLOCATION_ERROR
;
799 uprv_memset(tempbuffer
, 0, (sourceLength
+1)*U_SIZEOF_UCHAR
);
800 i
= j
= sourceLength
; count
= 0;
802 if ( (lamAlefOption
&& dest
[i
] == LAMALEF_SPACE_SUB
) ||
803 (tashkeelOption
&& dest
[i
] == TASHKEEL_SPACE_SUB
) ){
807 tempbuffer
[j
] = dest
[i
];
813 for(i
=0 ;i
< count
; i
++){
814 tempbuffer
[i
] = SPACE_CHAR
;
817 uprv_memcpy(dest
, tempbuffer
, sourceLength
*U_SIZEOF_UCHAR
);
818 destSize
= sourceLength
;
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)) ) {
832 if ( (options
&U_SHAPE_TASHKEEL_MASK
) == uShapeTashkeelEnd
){
837 if(lamAlefOption
|| tashkeelOption
){
838 tempbuffer
= (UChar
*)uprv_malloc((sourceLength
+1)*U_SIZEOF_UCHAR
);
840 if(tempbuffer
== NULL
) {
841 *pErrorCode
= U_MEMORY_ALLOCATION_ERROR
;
845 uprv_memset(tempbuffer
, 0, (sourceLength
+1)*U_SIZEOF_UCHAR
);
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
) ){
854 tempbuffer
[j
] = dest
[i
];
861 tempbuffer
[i
] = SPACE_CHAR
;
866 uprv_memcpy(dest
,tempbuffer
, sourceLength
*U_SIZEOF_UCHAR
);
867 destSize
= sourceLength
;
872 uprv_free(tempbuffer
);
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
889 expandCompositCharAtBegin(UChar
*dest
, int32_t sourceLength
, int32_t destSize
,UErrorCode
*pErrorCode
) {
892 UChar
*tempbuffer
=NULL
;
894 tempbuffer
= (UChar
*)uprv_malloc((sourceLength
+1)*U_SIZEOF_UCHAR
);
897 if(tempbuffer
== NULL
) {
898 *pErrorCode
= U_MEMORY_ALLOCATION_ERROR
;
902 uprv_memset(tempbuffer
, 0, (sourceLength
+1)*U_SIZEOF_UCHAR
);
905 while(dest
[i
] == SPACE_CHAR
) {
910 i
= j
= sourceLength
-1;
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 ];
922 if( countl
== 0 && isLamAlefChar(dest
[i
]) ) {
923 *pErrorCode
=U_NO_SPACE_AVAILABLE
;
925 tempbuffer
[j
] = dest
[i
];
930 uprv_memcpy(dest
, tempbuffer
, sourceLength
*U_SIZEOF_UCHAR
);
932 uprv_free(tempbuffer
);
934 destSize
= sourceLength
;
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
949 expandCompositCharAtEnd(UChar
*dest
, int32_t sourceLength
, int32_t destSize
,UErrorCode
*pErrorCode
) {
953 int32_t inpsize
= sourceLength
;
955 UChar
*tempbuffer
=NULL
;
956 tempbuffer
= (UChar
*)uprv_malloc((sourceLength
+1)*U_SIZEOF_UCHAR
);
959 if(tempbuffer
== NULL
) {
960 *pErrorCode
= U_MEMORY_ALLOCATION_ERROR
;
964 uprv_memset(tempbuffer
, 0, (sourceLength
+1)*U_SIZEOF_UCHAR
);
966 while(dest
[inpsize
-1] == SPACE_CHAR
) {
971 i
= sourceLength
- countr
- 1;
972 j
= sourceLength
- 1;
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 ];
981 if ((countr
== 0) && isLamAlefChar(dest
[i
]) ) {
982 *pErrorCode
=U_NO_SPACE_AVAILABLE
;
984 tempbuffer
[j
] = dest
[i
];
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
;
998 uprv_memcpy(dest
, tempbuffer
, sourceLength
*U_SIZEOF_UCHAR
);
1000 uprv_free(tempbuffer
);
1002 destSize
= sourceLength
;
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
1016 expandCompositCharAtNear(UChar
*dest
, int32_t sourceLength
, int32_t destSize
,UErrorCode
*pErrorCode
,
1017 int yehHamzaOption
, int seenTailOption
, int lamAlefOption
) {
1021 UChar lamalefChar
, yehhamzaChar
;
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
;
1028 *pErrorCode
=U_NO_SPACE_AVAILABLE
;
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
;
1037 *pErrorCode
=U_NO_SPACE_AVAILABLE
;
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 ];
1045 *pErrorCode
=U_NO_SPACE_AVAILABLE
;
1049 destSize
= sourceLength
;
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.
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
1069 expandCompositChar(UChar
*dest
, int32_t sourceLength
,
1070 int32_t destSize
,uint32_t options
,
1071 UErrorCode
*pErrorCode
, int shapingMode
) {
1073 int32_t i
= 0,j
= 0;
1075 UChar
*tempbuffer
=NULL
;
1076 int yehHamzaOption
= 0;
1077 int seenTailOption
= 0;
1078 int lamAlefOption
= 0;
1080 if (shapingMode
== 1){
1081 if ( (options
&U_SHAPE_LAMALEF_MASK
) == U_SHAPE_LAMALEF_AUTO
){
1083 if(spacesRelativeToTextBeginEnd
== 0) {
1084 destSize
= expandCompositCharAtEnd(dest
, sourceLength
, destSize
, pErrorCode
);
1086 if(*pErrorCode
== U_NO_SPACE_AVAILABLE
) {
1087 *pErrorCode
= U_ZERO_ERROR
;
1088 destSize
= expandCompositCharAtBegin(dest
, sourceLength
, destSize
, pErrorCode
);
1091 destSize
= expandCompositCharAtBegin(dest
, sourceLength
, destSize
, pErrorCode
);
1093 if(*pErrorCode
== U_NO_SPACE_AVAILABLE
) {
1094 *pErrorCode
= U_ZERO_ERROR
;
1095 destSize
= expandCompositCharAtEnd(dest
, sourceLength
, destSize
, pErrorCode
);
1099 if(*pErrorCode
== U_NO_SPACE_AVAILABLE
) {
1100 *pErrorCode
= U_ZERO_ERROR
;
1101 destSize
= expandCompositCharAtNear(dest
, sourceLength
, destSize
, pErrorCode
, yehHamzaOption
,
1107 if (shapingMode
== 1){
1108 if ( (options
&U_SHAPE_LAMALEF_MASK
) == uShapeLamalefEnd
){
1109 destSize
= expandCompositCharAtEnd(dest
, sourceLength
, destSize
, pErrorCode
);
1113 if (shapingMode
== 1){
1114 if ( (options
&U_SHAPE_LAMALEF_MASK
) == uShapeLamalefBegin
){
1115 destSize
= expandCompositCharAtBegin(dest
, sourceLength
, destSize
, pErrorCode
);
1119 if (shapingMode
== 0){
1120 if ((options
&U_SHAPE_YEHHAMZA_MASK
) == U_SHAPE_YEHHAMZA_TWOCELL_NEAR
){
1123 if ((options
&U_SHAPE_SEEN_MASK
) == U_SHAPE_SEEN_TWOCELL_NEAR
){
1127 if (shapingMode
== 1) {
1128 if ( (options
&U_SHAPE_LAMALEF_MASK
) == U_SHAPE_LAMALEF_NEAR
) {
1134 if (yehHamzaOption
|| seenTailOption
|| lamAlefOption
){
1135 destSize
= expandCompositCharAtNear(dest
, sourceLength
, destSize
, pErrorCode
, yehHamzaOption
,
1136 seenTailOption
,lamAlefOption
);
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
);
1146 if(tempbuffer
== NULL
) {
1147 *pErrorCode
= U_MEMORY_ALLOCATION_ERROR
;
1151 uprv_memset(tempbuffer
, 0, (destSize
+1)*U_SIZEOF_UCHAR
);
1154 while(i
< destSize
&& j
< destSize
) {
1155 if(isLamAlefChar(dest
[i
]) ) {
1156 tempbuffer
[j
] = convertLamAlef
[ dest
[i
] - 0xFEF5 ];
1157 tempbuffer
[j
+1] = LAM_CHAR
;
1160 tempbuffer
[j
] = dest
[i
];
1166 uprv_memcpy(dest
, tempbuffer
, destSize
*U_SIZEOF_UCHAR
);
1171 uprv_free(tempbuffer
);
1177 *Name : shapeUnicode
1178 *Function : Converts an Arabic Unicode buffer in 06xx Range into a shaped
1179 * arabic Unicode buffer in FExx Range
1182 shapeUnicode(UChar
*dest
, int32_t sourceLength
,
1183 int32_t destSize
,uint32_t options
,
1184 UErrorCode
*pErrorCode
,
1189 int32_t lastPos
,Nx
, Nw
;
1191 int32_t lamalef_found
= 0;
1192 int32_t seenfamFound
= 0, yehhamzaFound
=0, tashkeelFound
= 0;
1193 UChar prevLink
= 0, lastLink
= 0, currLink
, nextLink
= 0;
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
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) ];
1209 } else if ( (inputChar
>= 0xFE70) && (inputChar
<= 0xFEFC)) {
1210 dest
[i
] = convertFEto06
[ (inputChar
- 0xFE70) ] ;
1212 dest
[i
] = inputChar
;
1218 /* sets the index to the end of the buffer, together with the step point to -1 */
1219 i
= sourceLength
- 1;
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
1228 currLink
= getLink(dest
[i
]);
1234 /* If high byte of currLink > 0 then more than one shape */
1235 if ((currLink
& 0xFF00) > 0 || (getLink(dest
[i
]) & IRRELEVANT
) != 0) {
1237 while (Nx
< 0) { /* we need to know about next char */
1242 nextLink
= getLink(dest
[Nw
]);
1243 if((nextLink
& IRRELEVANT
) == 0) {
1251 if ( ((currLink
& ALEFTYPE
) > 0) && ((lastLink
& LAMTYPE
) > 0) ) {
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() */
1263 if ((i
> 0) && (dest
[i
-1] == SPACE_CHAR
)){
1264 if ( isSeenFamilyChar(dest
[i
])){
1266 } else if (dest
[i
] == YEH_HAMZA_CHAR
) {
1271 if ( isSeenFamilyChar(dest
[i
])){
1273 } else if (dest
[i
] == YEH_HAMZA_CHAR
) {
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
1283 Shape
= shapeTable
[nextLink
& (LINKR
+ LINKL
)]
1284 [lastLink
& (LINKR
+ LINKL
)]
1285 [currLink
& (LINKR
+ LINKL
)];
1287 if ((currLink
& (LINKR
+LINKL
)) == 1) {
1289 } else if(isTashkeelChar(dest
[i
])) {
1290 if( (lastLink
& LINKL
) && (nextLink
& LINKR
) && (tashkeelFlag
== 1) &&
1291 dest
[i
] != 0x064C && dest
[i
] != 0x064D )
1294 if( (nextLink
&ALEFTYPE
) == ALEFTYPE
&& (lastLink
&LAMTYPE
) == LAMTYPE
) {
1302 if ((dest
[i
] ^ 0x0600) < 0x100) {
1303 if ( isTashkeelChar(dest
[i
]) ){
1304 if (tashkeelFlag
== 2){
1305 dest
[i
] = TASHKEEL_SPACE_SUB
;
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
;
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
);
1321 /* move one notch forward */
1322 if ((currLink
& IRRELEVANT
) == 0) {
1323 prevLink
= lastLink
;
1324 lastLink
= currLink
;
1330 currLink
= nextLink
;
1332 } else if(i
!= iend
) {
1333 currLink
= getLink(dest
[i
]);
1336 destSize
= sourceLength
;
1337 if ( (lamalef_found
!= 0 ) || (tashkeelFound
!= 0) ){
1338 destSize
= handleGeneratedSpaces(dest
,sourceLength
,destSize
,options
,pErrorCode
);
1341 if ( (seenfamFound
!= 0) || (yehhamzaFound
!= 0) ) {
1342 destSize
= expandCompositChar(dest
, sourceLength
,destSize
,options
,pErrorCode
, SHAPE_MODE
);
1348 *Name : deShapeUnicode
1349 *Function : Converts an Arabic Unicode buffer in FExx Range into unshaped
1350 * arabic Unicode buffer in 06xx Range
1353 deShapeUnicode(UChar
*dest
, int32_t sourceLength
,
1354 int32_t destSize
,uint32_t options
,
1355 UErrorCode
*pErrorCode
) {
1357 int32_t lamalef_found
= 0;
1358 int32_t yehHamzaComposeEnabled
= 0;
1359 int32_t seenComposeEnabled
= 0;
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;
1365 *This for loop changes the buffer from the Unicode FE range to
1366 *the Unicode 06 range
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) ];
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) ];
1385 dest
[i
] = inputChar
;
1388 if( isLamAlefChar(dest
[i
]) )
1392 destSize
= sourceLength
;
1393 if (lamalef_found
!= 0){
1394 destSize
= expandCompositChar(dest
,sourceLength
,destSize
,options
,pErrorCode
,DESHAPE_MODE
);
1400 ****************************************
1402 ****************************************
1405 U_CAPI
int32_t U_EXPORT2
1406 u_shapeArabic(const UChar
*source
, int32_t sourceLength
,
1407 UChar
*dest
, int32_t destCapacity
,
1409 UErrorCode
*pErrorCode
) {
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
;
1419 /* usual error checking */
1420 if(pErrorCode
==NULL
|| U_FAILURE(*pErrorCode
)) {
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
)
1438 *pErrorCode
=U_ILLEGAL_ARGUMENT_ERROR
;
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
)))
1449 *pErrorCode
=U_ILLEGAL_ARGUMENT_ERROR
;
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
)))
1459 *pErrorCode
=U_ILLEGAL_ARGUMENT_ERROR
;
1462 /* determine the source length */
1463 if(sourceLength
==-1) {
1464 sourceLength
=u_strlen(source
);
1466 if(sourceLength
<=0) {
1467 return u_terminateUChars(dest
, destCapacity
, 0, pErrorCode
);
1470 /* check that source and destination do not overlap */
1472 ((source
<=dest
&& dest
<source
+sourceLength
) ||
1473 (dest
<=source
&& source
<dest
+destCapacity
))) {
1474 *pErrorCode
=U_ILLEGAL_ARGUMENT_ERROR
;
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
;
1482 tailChar
= OLD_TAIL_CHAR
;
1485 if((options
&U_SHAPE_LETTERS_MASK
)!=U_SHAPE_LETTERS_NOOP
) {
1487 UChar
*tempbuffer
, *tempsource
= NULL
;
1488 int32_t outputSize
, spacesCountl
=0, spacesCountr
=0;
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;
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
;
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
]);
1517 aggregation_possible
= 1;
1518 tempsource
[j
+=step
] = source
[i
];
1523 source
= tempsource
+(logical_order
?0:j
);
1524 sourceLength
= newSourceLength
;
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
);
1533 outputSize
=sourceLength
;
1536 if(outputSize
>destCapacity
) {
1537 *pErrorCode
=U_BUFFER_OVERFLOW_ERROR
;
1538 if (tempsource
!= NULL
) uprv_free(tempsource
);
1543 * need a temporary buffer of size max(outputSize, sourceLength)
1544 * because at first we copy source->temp
1546 if(sourceLength
>outputSize
) {
1547 outputSize
=sourceLength
;
1550 /* Start of Arabic letter shaping part */
1551 if(outputSize
<=LENGTHOF(buffer
)) {
1552 outputSize
=LENGTHOF(buffer
);
1555 tempbuffer
= (UChar
*)uprv_malloc(outputSize
*U_SIZEOF_UCHAR
);
1558 if(tempbuffer
== NULL
) {
1559 *pErrorCode
= U_MEMORY_ALLOCATION_ERROR
;
1560 if (tempsource
!= NULL
) uprv_free(tempsource
);
1564 uprv_memcpy(tempbuffer
, source
, sourceLength
*U_SIZEOF_UCHAR
);
1565 if (tempsource
!= NULL
){
1566 uprv_free(tempsource
);
1569 if(sourceLength
<outputSize
) {
1570 uprv_memset(tempbuffer
+sourceLength
, 0, (outputSize
-sourceLength
)*U_SIZEOF_UCHAR
);
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
);
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
;
1584 uShapeTashkeelBegin
= U_SHAPE_TASHKEEL_END
;
1585 uShapeTashkeelEnd
= U_SHAPE_TASHKEEL_BEGIN
;
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);
1596 /* default Call the shaping function with tashkeel flag == 1 */
1597 destLength
= shapeUnicode(tempbuffer
,sourceLength
,destCapacity
,options
,pErrorCode
,1);
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
);
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);
1610 case U_SHAPE_LETTERS_UNSHAPE
:
1611 /* Call the deshaping function */
1612 destLength
= deShapeUnicode(tempbuffer
,sourceLength
,destCapacity
,options
,pErrorCode
);
1615 /* will never occur because of validity checks above */
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
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
);
1631 uprv_memcpy(dest
, tempbuffer
, uprv_min(destLength
, destCapacity
)*U_SIZEOF_UCHAR
);
1633 if(tempbuffer
!=buffer
) {
1634 uprv_free(tempbuffer
);
1637 if(destLength
>destCapacity
) {
1638 *pErrorCode
=U_BUFFER_OVERFLOW_ERROR
;
1642 /* End of Arabic letter shaping part */
1645 * No letter shaping:
1646 * just make sure the destination is large enough and copy the string.
1648 if(destCapacity
<sourceLength
) {
1649 /* this catches preflighting, too */
1650 *pErrorCode
=U_BUFFER_OVERFLOW_ERROR
;
1651 return sourceLength
;
1653 uprv_memcpy(dest
, source
, sourceLength
*U_SIZEOF_UCHAR
);
1654 destLength
=sourceLength
;
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.
1663 if((options
&U_SHAPE_DIGITS_MASK
)!=U_SHAPE_DIGITS_NOOP
) {
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" */
1672 case U_SHAPE_DIGIT_TYPE_AN_EXTENDED
:
1673 digitBase
=0x6f0; /* Unicode: "Eastern Arabic-Indic digits (Persian and Urdu)" */
1676 /* will never occur because of validity checks above */
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 */
1686 for(i
=0; i
<destLength
; ++i
) {
1687 if(((uint32_t)dest
[i
]-0x30)<10) {
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;
1700 case U_SHAPE_DIGITS_ALEN2AN_INIT_LR
:
1701 _shapeToArabicDigitsWithContext(dest
, destLength
,
1703 (UBool
)((options
&U_SHAPE_TEXT_DIRECTION_MASK
)==U_SHAPE_TEXT_DIRECTION_LOGICAL
),
1706 case U_SHAPE_DIGITS_ALEN2AN_INIT_AL
:
1707 _shapeToArabicDigitsWithContext(dest
, destLength
,
1709 (UBool
)((options
&U_SHAPE_TEXT_DIRECTION_MASK
)==U_SHAPE_TEXT_DIRECTION_LOGICAL
),
1713 /* will never occur because of validity checks above */
1718 return u_terminateUChars(dest
, destCapacity
, destLength
, pErrorCode
);