2 ******************************************************************************
4 * Copyright (C) 2000-2006, International Business Machines
5 * Corporation and others. All Rights Reserved.
7 ******************************************************************************
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"
30 * This implementation assumes that the internal encoding is UTF-16
31 * or UTF-32, not UTF-8.
32 * The main assumption is that the Arabic characters and their
33 * presentation forms each fit into a single UChar.
34 * With UTF-8, they occupy 2 or 3 bytes, and more than the ASCII
37 # error This implementation assumes UTF-16 or UTF-32 (check UTF_SIZE)
41 * ### TODO in general for letter shaping:
42 * - the letter shaping code is UTF-16-unaware; needs update
43 * + especially invertBuffer()?!
44 * - needs to handle the "Arabic Tail" that is used in some legacy codepages
45 * as a glyph fragment of wide-glyph letters
46 * + IBM Unicode conversion tables map it to U+200B (ZWSP)
47 * + IBM Egypt has proposed to encode the tail in Unicode among Arabic Presentation Forms
50 /* definitions for Arabic letter shaping ------------------------------------ */
58 static const UChar IrrelevantPos
[] = {
63 static const UChar convertLamAlef
[] =
75 static const UChar araLink
[178]=
77 1 + 32 + 256 * 0x11,/*0x0622*/
78 1 + 32 + 256 * 0x13,/*0x0623*/
79 1 + 256 * 0x15,/*0x0624*/
80 1 + 32 + 256 * 0x17,/*0x0625*/
81 1 + 2 + 256 * 0x19,/*0x0626*/
82 1 + 32 + 256 * 0x1D,/*0x0627*/
83 1 + 2 + 256 * 0x1F,/*0x0628*/
84 1 + 256 * 0x23,/*0x0629*/
85 1 + 2 + 256 * 0x25,/*0x062A*/
86 1 + 2 + 256 * 0x29,/*0x062B*/
87 1 + 2 + 256 * 0x2D,/*0x062C*/
88 1 + 2 + 256 * 0x31,/*0x062D*/
89 1 + 2 + 256 * 0x35,/*0x062E*/
90 1 + 256 * 0x39,/*0x062F*/
91 1 + 256 * 0x3B,/*0x0630*/
92 1 + 256 * 0x3D,/*0x0631*/
93 1 + 256 * 0x3F,/*0x0632*/
94 1 + 2 + 256 * 0x41,/*0x0633*/
95 1 + 2 + 256 * 0x45,/*0x0634*/
96 1 + 2 + 256 * 0x49,/*0x0635*/
97 1 + 2 + 256 * 0x4D,/*0x0636*/
98 1 + 2 + 256 * 0x51,/*0x0637*/
99 1 + 2 + 256 * 0x55,/*0x0638*/
100 1 + 2 + 256 * 0x59,/*0x0639*/
101 1 + 2 + 256 * 0x5D,/*0x063A*/
102 0, 0, 0, 0, 0, /*0x063B-0x063F*/
104 1 + 2 + 256 * 0x61,/*0x0641*/
105 1 + 2 + 256 * 0x65,/*0x0642*/
106 1 + 2 + 256 * 0x69,/*0x0643*/
107 1 + 2 + 16 + 256 * 0x6D,/*0x0644*/
108 1 + 2 + 256 * 0x71,/*0x0645*/
109 1 + 2 + 256 * 0x75,/*0x0646*/
110 1 + 2 + 256 * 0x79,/*0x0647*/
111 1 + 256 * 0x7D,/*0x0648*/
112 1 + 256 * 0x7F,/*0x0649*/
113 1 + 2 + 256 * 0x81,/*0x064A*/
114 4, 4, 4, 4, /*0x064B-0x064E*/
115 4, 4, 4, 4, /*0x064F-0x0652*/
116 4, 4, 4, 0, 0, /*0x0653-0x0657*/
117 0, 0, 0, 0, /*0x0658-0x065B*/
118 1 + 256 * 0x85,/*0x065C*/
119 1 + 256 * 0x87,/*0x065D*/
120 1 + 256 * 0x89,/*0x065E*/
121 1 + 256 * 0x8B,/*0x065F*/
122 0, 0, 0, 0, 0, /*0x0660-0x0664*/
123 0, 0, 0, 0, 0, /*0x0665-0x0669*/
124 0, 0, 0, 0, 0, 0, /*0x066A-0x066F*/
131 1, 1, /*0x0676-0x0677*/
132 1+2, 1+2, 1+2, 1+2, 1+2, 1+2, /*0x0678-0x067D*/
133 1+2, 1+2, 1+2, 1+2, 1+2, 1+2, /*0x067E-0x0683*/
134 1+2, 1+2, 1+2, 1+2, /*0x0684-0x0687*/
135 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /*0x0688-0x0691*/
136 1, 1, 1, 1, 1, 1, 1, 1, /*0x0692-0x0699*/
137 1+2, 1+2, 1+2, 1+2, 1+2, 1+2, /*0x069A-0x06A3*/
138 1+2, 1+2, 1+2, 1+2, /*0x069A-0x06A3*/
139 1+2, 1+2, 1+2, 1+2, 1+2, 1+2, /*0x06A4-0x06AD*/
140 1+2, 1+2, 1+2, 1+2, /*0x06A4-0x06AD*/
141 1+2, 1+2, 1+2, 1+2, 1+2, 1+2, /*0x06AE-0x06B7*/
142 1+2, 1+2, 1+2, 1+2, /*0x06AE-0x06B7*/
143 1+2, 1+2, 1+2, 1+2, 1+2, 1+2, /*0x06B8-0x06BF*/
144 1+2, 1+2, /*0x06B8-0x06BF*/
147 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /*0x06C2-0x06CB*/
150 1+2, 1+2, 1+2, 1+2, /*0x06CE-0x06D1*/
151 1, 1 /*0x06D2-0x06D3*/
154 static const UChar presLink
[141]=
158 1 + 2, 0, 1+ 2, 0, 1+ 2, /*0xFE72-0xFE76*/
160 1+ 2, 1 + 2, 1+2, 1 + 2, /*0xFE78-0xFE81*/
161 1+ 2, 1 + 2, 1+2, 1 + 2, /*0xFE82-0xFE85*/
162 0, 0 + 32, 1 + 32, 0 + 32, /*0xFE86-0xFE89*/
163 1 + 32, 0, 1, 0 + 32, /*0xFE8A-0xFE8D*/
164 1 + 32, 0, 2, 1 + 2, /*0xFE8E-0xFE91*/
165 1, 0 + 32, 1 + 32, 0, /*0xFE92-0xFE95*/
166 2, 1 + 2, 1, 0, /*0xFE96-0xFE99*/
167 1, 0, 2, 1 + 2, /*0xFE9A-0xFE9D*/
168 1, 0, 2, 1 + 2, /*0xFE9E-0xFEA1*/
169 1, 0, 2, 1 + 2, /*0xFEA2-0xFEA5*/
170 1, 0, 2, 1 + 2, /*0xFEA6-0xFEA9*/
171 1, 0, 2, 1 + 2, /*0xFEAA-0xFEAD*/
172 1, 0, 1, 0, /*0xFEAE-0xFEB1*/
173 1, 0, 1, 0, /*0xFEB2-0xFEB5*/
174 1, 0, 2, 1+2, /*0xFEB6-0xFEB9*/
175 1, 0, 2, 1+2, /*0xFEBA-0xFEBD*/
176 1, 0, 2, 1+2, /*0xFEBE-0xFEC1*/
177 1, 0, 2, 1+2, /*0xFEC2-0xFEC5*/
178 1, 0, 2, 1+2, /*0xFEC6-0xFEC9*/
179 1, 0, 2, 1+2, /*0xFECA-0xFECD*/
180 1, 0, 2, 1+2, /*0xFECE-0xFED1*/
181 1, 0, 2, 1+2, /*0xFED2-0xFED5*/
182 1, 0, 2, 1+2, /*0xFED6-0xFED9*/
183 1, 0, 2, 1+2, /*0xFEDA-0xFEDD*/
184 1, 0, 2, 1+2, /*0xFEDE-0xFEE1*/
185 1, 0 + 16, 2 + 16, 1 + 2 +16, /*0xFEE2-0xFEE5*/
186 1 + 16, 0, 2, 1+2, /*0xFEE6-0xFEE9*/
187 1, 0, 2, 1+2, /*0xFEEA-0xFEED*/
188 1, 0, 2, 1+2, /*0xFEEE-0xFEF1*/
189 1, 0, 1, 0, /*0xFEF2-0xFEF5*/
190 1, 0, 2, 1+2, /*0xFEF6-0xFEF9*/
191 1, 0, 1, 0, /*0xFEFA-0xFEFD*/
196 static const UChar convertFEto06
[] =
198 /***********0******1******2******3******4******5******6******7******8******9******A******B******C******D******E******F***/
199 /*FE7*/ 0x64B, 0x64B, 0x64C, 0x64C, 0x64D, 0x64D, 0x64E, 0x64E, 0x64F, 0x64F, 0x650, 0x650, 0x651, 0x651, 0x652, 0x652,
200 /*FE8*/ 0x621, 0x622, 0x622, 0x623, 0x623, 0x624, 0x624, 0x625, 0x625, 0x626, 0x626, 0x626, 0x626, 0x627, 0x627, 0x628,
201 /*FE9*/ 0x628, 0x628, 0x628, 0x629, 0x629, 0x62A, 0x62A, 0x62A, 0x62A, 0x62B, 0x62B, 0x62B, 0x62B, 0x62C, 0x62C, 0x62C,
202 /*FEA*/ 0x62C, 0x62D, 0x62D, 0x62D, 0x62D, 0x62E, 0x62E, 0x62E, 0x62E, 0x62F, 0x62F, 0x630, 0x630, 0x631, 0x631, 0x632,
203 /*FEB*/ 0x632, 0x633, 0x633, 0x633, 0x633, 0x634, 0x634, 0x634, 0x634, 0x635, 0x635, 0x635, 0x635, 0x636, 0x636, 0x636,
204 /*FEC*/ 0x636, 0x637, 0x637, 0x637, 0x637, 0x638, 0x638, 0x638, 0x638, 0x639, 0x639, 0x639, 0x639, 0x63A, 0x63A, 0x63A,
205 /*FED*/ 0x63A, 0x641, 0x641, 0x641, 0x641, 0x642, 0x642, 0x642, 0x642, 0x643, 0x643, 0x643, 0x643, 0x644, 0x644, 0x644,
206 /*FEE*/ 0x644, 0x645, 0x645, 0x645, 0x645, 0x646, 0x646, 0x646, 0x646, 0x647, 0x647, 0x647, 0x647, 0x648, 0x648, 0x649,
207 /*FEF*/ 0x649, 0x64A, 0x64A, 0x64A, 0x64A, 0x65C, 0x65C, 0x65D, 0x65D, 0x65E, 0x65E, 0x65F, 0x65F
210 static const UChar shapeTable
[4][4][4]=
212 { {0,0,0,0}, {0,0,0,0}, {0,1,0,3}, {0,1,0,1} },
213 { {0,0,2,2}, {0,0,1,2}, {0,1,1,2}, {0,1,1,3} },
214 { {0,0,0,0}, {0,0,0,0}, {0,1,0,3}, {0,1,0,3} },
215 { {0,0,1,2}, {0,0,1,2}, {0,1,1,2}, {0,1,1,3} }
219 * This function shapes European digits to Arabic-Indic digits
220 * in-place, writing over the input characters.
221 * Since we know that we are only looking for BMP code points,
222 * we can safely just work with code units (again, at least UTF-16).
225 _shapeToArabicDigitsWithContext(UChar
*s
, int32_t length
,
227 UBool isLogical
, UBool lastStrongWasAL
) {
228 const UBiDiProps
*bdp
;
229 UErrorCode errorCode
;
234 errorCode
=U_ZERO_ERROR
;
235 bdp
=ubidi_getSingleton(&errorCode
);
236 if(U_FAILURE(errorCode
)) {
242 /* the iteration direction depends on the type of input */
244 for(i
=0; i
<length
; ++i
) {
246 switch(ubidi_getClass(bdp
, c
)) {
247 case U_LEFT_TO_RIGHT
: /* L */
248 case U_RIGHT_TO_LEFT
: /* R */
249 lastStrongWasAL
=FALSE
;
251 case U_RIGHT_TO_LEFT_ARABIC
: /* AL */
252 lastStrongWasAL
=TRUE
;
254 case U_EUROPEAN_NUMBER
: /* EN */
255 if(lastStrongWasAL
&& (uint32_t)(c
-0x30)<10) {
256 s
[i
]=(UChar
)(digitBase
+c
); /* digitBase+(c-0x30) - digitBase was modified above */
264 for(i
=length
; i
>0; /* pre-decrement in the body */) {
266 switch(ubidi_getClass(bdp
, c
)) {
267 case U_LEFT_TO_RIGHT
: /* L */
268 case U_RIGHT_TO_LEFT
: /* R */
269 lastStrongWasAL
=FALSE
;
271 case U_RIGHT_TO_LEFT_ARABIC
: /* AL */
272 lastStrongWasAL
=TRUE
;
274 case U_EUROPEAN_NUMBER
: /* EN */
275 if(lastStrongWasAL
&& (uint32_t)(c
-0x30)<10) {
276 s
[i
]=(UChar
)(digitBase
+c
); /* digitBase+(c-0x30) - digitBase was modified above */
288 *Function : This function inverts the buffer, it's used
289 * in case the user specifies the buffer to be
290 * U_SHAPE_TEXT_DIRECTION_LOGICAL
293 invertBuffer(UChar
*buffer
,int32_t size
,uint32_t options
,int32_t *spacesCountl
,int32_t *spacesCountr
) {
297 int32_t lowlimit
= 0, highlimit
= 0;
299 lowlimit
= *spacesCountl
;
300 highlimit
= *spacesCountr
;
302 for(i
=lowlimit
,j
=size
-highlimit
-1;i
<j
;i
++,j
--) {
304 buffer
[i
] = buffer
[j
];
310 *Name : changeLamAlef
311 *Function : Converts the Alef characters into an equivalent
312 * LamAlef location in the 0x06xx Range, this is an
313 * intermediate stage in the operation of the program
314 * later it'll be converted into the 0xFExx LamAlefs
315 * in the shaping function.
317 static U_INLINE UChar
318 changeLamAlef(UChar ch
) {
335 *Function : Special Arabic characters need special handling in the shapeUnicode
336 * function, this function returns 1 or 2 for these special characters
339 specialChar(UChar ch
) {
341 if( (ch
>0x0621 && ch
<0x0626)||(ch
==0x0627)||(ch
>0x062e && ch
<0x0633)||
342 (ch
>0x0647 && ch
<0x064a)||(ch
==0x0629) ) {
345 else if( ch
>=0x064B && ch
<= 0x0652 )
347 else if( (ch
>=0x0653 && ch
<= 0x0655) || ch
== 0x0670 ||
348 (ch
>=0xFE70 && ch
<= 0xFE7F) )
356 *Function : Resolves the link between the characters as
357 * Arabic characters have four forms :
358 * Isolated, Initial, Middle and Final Form
363 if(ch
>= 0x0622 && ch
<= 0x06D3) {
364 return(araLink
[ch
-0x0622]);
365 } else if(ch
== 0x200D) {
367 } else if(ch
>= 0x206D && ch
<= 0x206F) {
369 } else if(ch
>= 0xFE70 && ch
<= 0xFEFC) {
370 return(presLink
[ch
-0xFE70]);
378 *Function : Counts the number of spaces
379 * at each end of the logical buffer
382 countSpaces(UChar
*dest
,int32_t size
,uint32_t options
,int32_t *spacesCountl
,int32_t *spacesCountr
) {
385 int32_t countl
= 0,countr
= 0;
387 while(dest
[i
] == 0x0020) {
391 while(dest
[size
-1] == 0x0020) {
395 *spacesCountl
= countl
;
396 *spacesCountr
= countr
;
400 *Name : isTashkeelChar
401 *Function : Returns 1 for Tashkeel characters else return 0
403 static U_INLINE
int32_t
404 isTashkeelChar(UChar ch
) {
405 return (int32_t)( ch
>=0x064B && ch
<= 0x0652 );
410 *Function : Returns 1 for Alef characters else return 0
412 static U_INLINE
int32_t
413 isAlefChar(UChar ch
) {
414 return (int32_t)( (ch
==0x0622)||(ch
==0x0623)||(ch
==0x0625)||(ch
==0x0627) );
418 *Name : isLamAlefChar
419 *Function : Returns 1 for LamAlef characters else return 0
421 static U_INLINE
int32_t
422 isLamAlefChar(UChar ch
) {
423 return (int32_t)( (ch
>=0xFEF5)&&(ch
<=0xFEFC) );
427 *Name : calculateSize
428 *Function : This function calculates the destSize to be used in preflighting
429 * when the destSize is equal to 0
432 calculateSize(const UChar
*source
, int32_t sourceLength
,
433 int32_t destSize
,uint32_t options
) {
436 destSize
= sourceLength
;
438 switch(options
&U_SHAPE_LETTERS_MASK
) {
440 case U_SHAPE_LETTERS_SHAPE
:
441 if((options
&U_SHAPE_TEXT_DIRECTION_MASK
)==U_SHAPE_TEXT_DIRECTION_VISUAL_LTR
) {
442 for(i
=0;i
<sourceLength
;i
++) {
443 if( (isAlefChar(source
[i
]))&&(source
[i
+1]==0x0644) ) {
449 if((options
&U_SHAPE_TEXT_DIRECTION_MASK
)==U_SHAPE_TEXT_DIRECTION_LOGICAL
) {
450 for(i
=0;i
<sourceLength
;i
++) {
451 if( (isAlefChar(source
[i
+1]))&&(source
[i
]==0x0644) ) {
458 case U_SHAPE_LETTERS_UNSHAPE
:
459 for(i
=0;i
<sourceLength
;i
++) {
460 if( isLamAlefChar(source
[i
]) ) {
467 /* will never occur because of validity checks at the begin of u_shapeArabic */
475 *Name : removeLamAlefSpaces
476 *Function : The shapeUnicode function converts Lam + Alef into LamAlef + space,
477 * this function removes the spaces behind the LamAlefs according to
478 * the options the user specifies, the spaces are removed to the end
479 * of the buffer, or shrink the buffer ab=nd remove spaces for good
480 * or leave the buffer as it is LamAlef + space.
483 removeLamAlefSpaces(UChar
*dest
, int32_t sourceLength
,
486 UErrorCode
*pErrorCode
) {
488 int32_t i
= 0, j
= 0;
490 UChar
*tempbuffer
=NULL
;
492 switch(options
&U_SHAPE_LENGTH_MASK
) {
493 case U_SHAPE_LENGTH_GROW_SHRINK
:
494 tempbuffer
= (UChar
*)uprv_malloc((sourceLength
+1)*U_SIZEOF_UCHAR
);
496 if(tempbuffer
== NULL
) {
497 *pErrorCode
= U_MEMORY_ALLOCATION_ERROR
;
501 uprv_memset(tempbuffer
, 0, (sourceLength
+1)*U_SIZEOF_UCHAR
);
504 while(i
< sourceLength
) {
505 if(dest
[i
] == 0xFFFF) {
510 tempbuffer
[j
] = dest
[i
];
516 tempbuffer
[i
] = 0x0000;
521 uprv_memcpy(dest
, tempbuffer
, sourceLength
*U_SIZEOF_UCHAR
);
522 destSize
= u_strlen(dest
);
525 case U_SHAPE_LENGTH_FIXED_SPACES_NEAR
:
526 /* Lam+Alef is already shaped into LamAlef + FFFF */
528 while(i
< sourceLength
) {
529 if(dest
[i
] == 0xFFFF)
533 destSize
= sourceLength
;
536 case U_SHAPE_LENGTH_FIXED_SPACES_AT_BEGINNING
:
537 tempbuffer
= (UChar
*)uprv_malloc((sourceLength
+1)*U_SIZEOF_UCHAR
);
540 if(tempbuffer
== NULL
) {
541 *pErrorCode
= U_MEMORY_ALLOCATION_ERROR
;
545 uprv_memset(tempbuffer
, 0, (sourceLength
+1)*U_SIZEOF_UCHAR
);
547 i
= j
= sourceLength
;
549 if(dest
[i
] == 0xFFFF) {
554 tempbuffer
[j
] = dest
[i
];
559 tempbuffer
[i
] = 0x0020;
561 uprv_memcpy(dest
, tempbuffer
, sourceLength
*U_SIZEOF_UCHAR
);
562 destSize
= sourceLength
;
565 case U_SHAPE_LENGTH_FIXED_SPACES_AT_END
:
566 tempbuffer
= (UChar
*)uprv_malloc((sourceLength
+1)*U_SIZEOF_UCHAR
);
569 if(tempbuffer
== NULL
) {
570 *pErrorCode
= U_MEMORY_ALLOCATION_ERROR
;
574 uprv_memset(tempbuffer
, 0, (sourceLength
+1)*U_SIZEOF_UCHAR
);
577 while(i
< sourceLength
) {
578 if(dest
[i
] == 0xFFFF) {
583 tempbuffer
[j
] = dest
[i
];
589 tempbuffer
[i
] = 0x0020;
594 uprv_memcpy(dest
,tempbuffer
, sourceLength
*U_SIZEOF_UCHAR
);
595 destSize
= sourceLength
;
604 uprv_free(tempbuffer
);
610 *Name : expandLamAlef
611 *Function : LamAlef needs special handling as the LamAlef is
612 * one character while expanding it will give two
613 * characters Lam + Alef, so we need to expand the LamAlef
614 * in near or far spaces according to the options the user
615 * specifies or increase the buffer size.
616 * If there are no spaces to expand the LamAlef, an error
617 * will be set to U_NO_SPACE_AVAILABLE as defined in utypes.h
620 expandLamAlef(UChar
*dest
, int32_t sourceLength
,
621 int32_t destSize
,uint32_t options
,
622 UErrorCode
*pErrorCode
) {
627 int32_t inpsize
= sourceLength
;
629 UChar
*tempbuffer
=NULL
;
631 switch(options
&U_SHAPE_LENGTH_MASK
) {
633 case U_SHAPE_LENGTH_GROW_SHRINK
:
634 destSize
= calculateSize(dest
,sourceLength
,destSize
,options
);
635 tempbuffer
= (UChar
*)uprv_malloc((destSize
+1)*U_SIZEOF_UCHAR
);
638 if(tempbuffer
== NULL
) {
639 *pErrorCode
= U_MEMORY_ALLOCATION_ERROR
;
643 uprv_memset(tempbuffer
, 0, (destSize
+1)*U_SIZEOF_UCHAR
);
646 while(i
< destSize
&& j
< destSize
) {
647 if( isLamAlefChar(dest
[i
]) ) {
648 tempbuffer
[j
] = convertLamAlef
[ dest
[i
] - 0xFEF5 ];
649 tempbuffer
[j
+1] = 0x0644;
653 tempbuffer
[j
] = dest
[i
];
658 uprv_memcpy(dest
, tempbuffer
, destSize
*U_SIZEOF_UCHAR
);
661 case U_SHAPE_LENGTH_FIXED_SPACES_NEAR
:
662 for(i
= 0;i
<sourceLength
;i
++) {
663 if((dest
[i
] == 0x0020) && isLamAlefChar(dest
[i
+1])) {
664 lamalefChar
= dest
[i
+1];
666 dest
[i
] = convertLamAlef
[ lamalefChar
- 0xFEF5 ];
669 if((dest
[i
] != 0x0020) && isLamAlefChar(dest
[i
+1])) {
670 *pErrorCode
=U_NO_SPACE_AVAILABLE
;
673 destSize
= sourceLength
;
676 case U_SHAPE_LENGTH_FIXED_SPACES_AT_BEGINNING
:
677 tempbuffer
= (UChar
*)uprv_malloc((sourceLength
+1)*U_SIZEOF_UCHAR
);
680 if(tempbuffer
== NULL
) {
681 *pErrorCode
= U_MEMORY_ALLOCATION_ERROR
;
685 uprv_memset(tempbuffer
, 0, (sourceLength
+1)*U_SIZEOF_UCHAR
);
688 while(dest
[i
] == 0x0020) {
693 i
= j
= sourceLength
-1;
694 while(i
>= 0 && j
>= 0) {
695 if( countl
>0 && isLamAlefChar(dest
[i
]) ) {
696 tempbuffer
[j
] = 0x0644;
697 tempbuffer
[j
-1] = convertLamAlef
[ dest
[i
] - 0xFEF5 ];
702 if( countl
== 0 && isLamAlefChar(dest
[i
]) )
703 *pErrorCode
=U_NO_SPACE_AVAILABLE
;
704 tempbuffer
[j
] = dest
[i
];
710 uprv_memcpy(dest
, tempbuffer
, sourceLength
*U_SIZEOF_UCHAR
);
711 destSize
= sourceLength
;
714 case U_SHAPE_LENGTH_FIXED_SPACES_AT_END
:
715 /* LamAlef expansion below is done from right to left to make sure that we consume
716 * the spaces with the LamAlefs as they appear in the visual buffer from right to left
718 tempbuffer
= (UChar
*)uprv_malloc((sourceLength
+1)*U_SIZEOF_UCHAR
);
721 if(tempbuffer
== NULL
) {
722 *pErrorCode
= U_MEMORY_ALLOCATION_ERROR
;
726 uprv_memset(tempbuffer
, 0, (sourceLength
+1)*U_SIZEOF_UCHAR
);
728 while(dest
[inpsize
-1] == 0x0020) {
733 i
= sourceLength
- countr
- 1;
734 j
= sourceLength
- 1;
736 while(i
>= 0 && j
>= 0) {
737 if( countr
>0 && isLamAlefChar(dest
[i
]) ) {
738 tempbuffer
[j
] = 0x0644;
739 tempbuffer
[j
-1] = convertLamAlef
[ dest
[i
] - 0xFEF5 ];
744 if( countr
== 0 && isLamAlefChar(dest
[i
]) )
745 *pErrorCode
=U_NO_SPACE_AVAILABLE
;
746 tempbuffer
[j
] = dest
[i
];
753 uprv_memcpy(tempbuffer
, tempbuffer
+countr
, sourceLength
*U_SIZEOF_UCHAR
);
754 if(u_strlen(tempbuffer
) < sourceLength
) {
755 for(i
=sourceLength
-1;i
>=sourceLength
-countr
;i
--)
756 tempbuffer
[i
] = 0x0020;
760 uprv_memcpy(dest
, tempbuffer
, sourceLength
*U_SIZEOF_UCHAR
);
762 destSize
= sourceLength
;
766 /* will never occur because of validity checks */
771 uprv_free(tempbuffer
);
778 *Function : Converts an Arabic Unicode buffer in 06xx Range into a shaped
779 * arabic Unicode buffer in FExx Range
782 shapeUnicode(UChar
*dest
, int32_t sourceLength
,
783 int32_t destSize
,uint32_t options
,
784 UErrorCode
*pErrorCode
,
789 int32_t lastPos
,Nx
, Nw
;
792 int32_t lamalef_found
= 0;
793 UChar prevLink
= 0, lastLink
= 0, currLink
, nextLink
= 0;
797 * Converts the input buffer from FExx Range into 06xx Range
798 * to make sure that all characters are in the 06xx range
799 * even the lamalef is converted to the special region in
802 for (i
= 0; i
< sourceLength
; i
++) {
803 UChar inputChar
= dest
[i
];
804 if ( (inputChar
>= 0xFE70) && (inputChar
<= 0xFEFC)) {
805 dest
[i
] = convertFEto06
[ (inputChar
- 0xFE70) ] ;
807 dest
[i
] = inputChar
;
811 /* sets the index to the end of the buffer, together with the step point to -1 */
812 i
= sourceLength
- 1;
817 * This function resolves the link between the characters .
818 * Arabic characters have four forms :
819 * Isolated Form, Initial Form, Middle Form and Final Form
821 currLink
= getLink(dest
[i
]);
827 /* If high byte of currLink > 0 then more than one shape */
828 if ((currLink
& 0xFF00) > 0 || isTashkeelChar(dest
[i
])) {
830 while (Nx
< 0) { /* we need to know about next char */
835 nextLink
= getLink(dest
[Nw
]);
836 if((nextLink
& IRRELEVANT
) == 0) {
844 if ( ((currLink
& ALEFTYPE
) > 0) && ((lastLink
& LAMTYPE
) > 0) ) {
846 wLamalef
= changeLamAlef(dest
[i
]); /*get from 0x065C-0x065f */
847 if ( wLamalef
!= 0) {
848 dest
[i
] = 0xFFFF; /* The default case is to drop the Alef and replace */
849 dest
[lastPos
] =wLamalef
; /* it by 0xFFFF which is the last character in the */
850 i
=lastPos
; /* unicode private use area, this is done to make */
851 } /* sure that removeLamAlefSpaces() handles only the */
852 lastLink
= prevLink
; /* spaces generated during lamalef generation. */
853 currLink
= getLink(wLamalef
); /* 0xFFFF is added here and is replaced by spaces */
854 } /* in removeLamAlefSpaces() */
856 * get the proper shape according to link ability of neighbors
857 * and of character; depends on the order of the shapes
858 * (isolated, initial, middle, final) in the compatibility area
860 flag
= specialChar(dest
[i
]);
862 Shape
= shapeTable
[nextLink
& (LINKR
+ LINKL
)]
863 [lastLink
& (LINKR
+ LINKL
)]
864 [currLink
& (LINKR
+ LINKL
)];
867 Shape
= (Shape
== 1 || Shape
== 3) ? 1 : 0;
870 if( (lastLink
& LINKL
) && (nextLink
& LINKR
) && (tashkeelFlag
== 1) &&
871 dest
[i
] != 0x064C && dest
[i
] != 0x064D )
874 if( (nextLink
&ALEFTYPE
) == ALEFTYPE
&& (lastLink
&LAMTYPE
) == LAMTYPE
) {
884 dest
[i
] = 0xFE70 + IrrelevantPos
[(dest
[i
] - 0x064B)] + Shape
;
887 dest
[i
] = (UChar
)(0xFE70 + (currLink
>> 8) + Shape
);
890 /* move one notch forward */
891 if ((currLink
& IRRELEVANT
) == 0) {
903 currLink
= getLink(dest
[i
]);
907 /* If there is lamalef in the buffer call expandLamAlef */
908 if(lamalef_found
!= 0)
909 destSize
= removeLamAlefSpaces(dest
,sourceLength
,destSize
,options
,pErrorCode
);
911 destSize
= sourceLength
;
917 *Name : deShapeUnicode
918 *Function : Converts an Arabic Unicode buffer in FExx Range into unshaped
919 * arabic Unicode buffer in 06xx Range
922 deShapeUnicode(UChar
*dest
, int32_t sourceLength
,
923 int32_t destSize
,uint32_t options
,
924 UErrorCode
*pErrorCode
) {
926 int32_t lamalef_found
= 0;
929 *This for loop changes the buffer from the Unicode FE range to
930 *the Unicode 06 range
932 for(i
= 0; i
< sourceLength
; i
++) {
933 UChar inputChar
= dest
[i
];
934 if (( inputChar
>= 0xFE70) && (inputChar
<= 0xFEF4 )) { /* FExx Arabic range */
935 dest
[i
] = convertFEto06
[ (inputChar
- 0xFE70) ] ;
937 dest
[i
] = inputChar
;
939 if( isLamAlefChar(dest
[i
]) )
943 /* If there is lamalef in the buffer call expandLamAlef */
944 if(lamalef_found
!= 0)
945 destSize
= expandLamAlef(dest
,sourceLength
,destSize
,options
,pErrorCode
);
947 destSize
= sourceLength
;
952 U_CAPI
int32_t U_EXPORT2
953 u_shapeArabic(const UChar
*source
, int32_t sourceLength
,
954 UChar
*dest
, int32_t destCapacity
,
956 UErrorCode
*pErrorCode
) {
960 /* usual error checking */
961 if(pErrorCode
==NULL
|| U_FAILURE(*pErrorCode
)) {
965 /* make sure that no reserved options values are used; allow dest==NULL only for preflighting */
966 if( source
==NULL
|| sourceLength
<-1 ||
967 (dest
==NULL
&& destCapacity
!=0) || destCapacity
<0 ||
968 options
>=U_SHAPE_DIGIT_TYPE_RESERVED
||
969 (options
&U_SHAPE_DIGITS_MASK
)>=U_SHAPE_DIGITS_RESERVED
971 *pErrorCode
=U_ILLEGAL_ARGUMENT_ERROR
;
975 /* determine the source length */
976 if(sourceLength
==-1) {
977 sourceLength
=u_strlen(source
);
979 if(sourceLength
<=0) {
980 return u_terminateUChars(dest
, destCapacity
, 0, pErrorCode
);
983 /* check that source and destination do not overlap */
985 ((source
<=dest
&& dest
<source
+sourceLength
) ||
986 (dest
<=source
&& source
<dest
+destCapacity
))
988 *pErrorCode
=U_ILLEGAL_ARGUMENT_ERROR
;
992 if((options
&U_SHAPE_LETTERS_MASK
)!=U_SHAPE_LETTERS_NOOP
) {
995 int32_t outputSize
, spacesCountl
=0, spacesCountr
=0;
997 /* calculate destination size */
998 /* TODO: do we ever need to do this pure preflighting? */
999 if((options
&U_SHAPE_LENGTH_MASK
)==U_SHAPE_LENGTH_GROW_SHRINK
) {
1000 outputSize
=calculateSize(source
,sourceLength
,destCapacity
,options
);
1002 outputSize
=sourceLength
;
1004 if(outputSize
>destCapacity
) {
1005 *pErrorCode
=U_BUFFER_OVERFLOW_ERROR
;
1010 * need a temporary buffer of size max(outputSize, sourceLength)
1011 * because at first we copy source->temp
1013 if(sourceLength
>outputSize
) {
1014 outputSize
=sourceLength
;
1017 /* Start of Arabic letter shaping part */
1018 if(outputSize
<=sizeof(buffer
)/U_SIZEOF_UCHAR
) {
1019 outputSize
=sizeof(buffer
)/U_SIZEOF_UCHAR
;
1022 tempbuffer
= (UChar
*)uprv_malloc(outputSize
*U_SIZEOF_UCHAR
);
1025 if(tempbuffer
== NULL
) {
1026 *pErrorCode
= U_MEMORY_ALLOCATION_ERROR
;
1030 uprv_memcpy(tempbuffer
, source
, sourceLength
*U_SIZEOF_UCHAR
);
1031 if(sourceLength
<outputSize
) {
1032 uprv_memset(tempbuffer
+sourceLength
, 0, (outputSize
-sourceLength
)*U_SIZEOF_UCHAR
);
1035 if((options
&U_SHAPE_TEXT_DIRECTION_MASK
) == U_SHAPE_TEXT_DIRECTION_LOGICAL
) {
1036 countSpaces(tempbuffer
,sourceLength
,options
,&spacesCountl
,&spacesCountr
);
1037 invertBuffer(tempbuffer
,sourceLength
,options
,&spacesCountl
,&spacesCountr
);
1040 switch(options
&U_SHAPE_LETTERS_MASK
) {
1041 case U_SHAPE_LETTERS_SHAPE
:
1042 /* Call the shaping function with tashkeel flag == 1 */
1043 destLength
= shapeUnicode(tempbuffer
,sourceLength
,destCapacity
,options
,pErrorCode
,1);
1045 case U_SHAPE_LETTERS_SHAPE_TASHKEEL_ISOLATED
:
1046 /* Call the shaping function with tashkeel flag == 0 */
1047 destLength
= shapeUnicode(tempbuffer
,sourceLength
,destCapacity
,options
,pErrorCode
,0);
1049 case U_SHAPE_LETTERS_UNSHAPE
:
1050 /* Call the deshaping function */
1051 destLength
= deShapeUnicode(tempbuffer
,sourceLength
,destCapacity
,options
,pErrorCode
);
1054 /* will never occur because of validity checks above */
1060 * TODO: (markus 2002aug01)
1061 * For as long as we always preflight the outputSize above
1062 * we should U_ASSERT(outputSize==destLength)
1063 * except for the adjustment above before the tempbuffer allocation
1066 if((options
&U_SHAPE_TEXT_DIRECTION_MASK
) == U_SHAPE_TEXT_DIRECTION_LOGICAL
) {
1067 countSpaces(tempbuffer
,destLength
,options
,&spacesCountl
,&spacesCountr
);
1068 invertBuffer(tempbuffer
,destLength
,options
,&spacesCountl
,&spacesCountr
);
1070 uprv_memcpy(dest
, tempbuffer
, uprv_min(destLength
, destCapacity
)*U_SIZEOF_UCHAR
);
1072 if(tempbuffer
!=buffer
) {
1073 uprv_free(tempbuffer
);
1076 if(destLength
>destCapacity
) {
1077 *pErrorCode
=U_BUFFER_OVERFLOW_ERROR
;
1081 /* End of Arabic letter shaping part */
1084 * No letter shaping:
1085 * just make sure the destination is large enough and copy the string.
1087 if(destCapacity
<sourceLength
) {
1088 /* this catches preflighting, too */
1089 *pErrorCode
=U_BUFFER_OVERFLOW_ERROR
;
1090 return sourceLength
;
1092 uprv_memcpy(dest
, source
, sourceLength
*U_SIZEOF_UCHAR
);
1093 destLength
=sourceLength
;
1097 * Perform number shaping.
1098 * With UTF-16 or UTF-32, the length of the string is constant.
1099 * The easiest way to do this is to operate on the destination and
1100 * "shape" the digits in-place.
1102 if((options
&U_SHAPE_DIGITS_MASK
)!=U_SHAPE_DIGITS_NOOP
) {
1106 /* select the requested digit group */
1107 switch(options
&U_SHAPE_DIGIT_TYPE_MASK
) {
1108 case U_SHAPE_DIGIT_TYPE_AN
:
1109 digitBase
=0x660; /* Unicode: "Arabic-Indic digits" */
1111 case U_SHAPE_DIGIT_TYPE_AN_EXTENDED
:
1112 digitBase
=0x6f0; /* Unicode: "Eastern Arabic-Indic digits (Persian and Urdu)" */
1115 /* will never occur because of validity checks above */
1120 /* perform the requested operation */
1121 switch(options
&U_SHAPE_DIGITS_MASK
) {
1122 case U_SHAPE_DIGITS_EN2AN
:
1123 /* add (digitBase-'0') to each European (ASCII) digit code point */
1125 for(i
=0; i
<destLength
; ++i
) {
1126 if(((uint32_t)dest
[i
]-0x30)<10) {
1131 case U_SHAPE_DIGITS_AN2EN
:
1132 /* subtract (digitBase-'0') from each Arabic digit code point */
1133 for(i
=0; i
<destLength
; ++i
) {
1134 if(((uint32_t)dest
[i
]-(uint32_t)digitBase
)<10) {
1135 dest
[i
]-=digitBase
-0x30;
1139 case U_SHAPE_DIGITS_ALEN2AN_INIT_LR
:
1140 _shapeToArabicDigitsWithContext(dest
, destLength
,
1142 (UBool
)((options
&U_SHAPE_TEXT_DIRECTION_MASK
)==U_SHAPE_TEXT_DIRECTION_LOGICAL
),
1145 case U_SHAPE_DIGITS_ALEN2AN_INIT_AL
:
1146 _shapeToArabicDigitsWithContext(dest
, destLength
,
1148 (UBool
)((options
&U_SHAPE_TEXT_DIRECTION_MASK
)==U_SHAPE_TEXT_DIRECTION_LOGICAL
),
1152 /* will never occur because of validity checks above */
1157 return u_terminateUChars(dest
, destCapacity
, destLength
, pErrorCode
);