2 ******************************************************************************
4 * Copyright (C) 2000-2008, 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 ------------------------------------ */
60 #define COMBINE (SHADDA+CSHADDA)
63 static const uint8_t IrrelevantPos
[] = {
68 static const UChar convertLamAlef
[] =
80 static const UChar araLink
[178]=
82 1 + 32 + 256 * 0x11,/*0x0622*/
83 1 + 32 + 256 * 0x13,/*0x0623*/
84 1 + 256 * 0x15,/*0x0624*/
85 1 + 32 + 256 * 0x17,/*0x0625*/
86 1 + 2 + 256 * 0x19,/*0x0626*/
87 1 + 32 + 256 * 0x1D,/*0x0627*/
88 1 + 2 + 256 * 0x1F,/*0x0628*/
89 1 + 256 * 0x23,/*0x0629*/
90 1 + 2 + 256 * 0x25,/*0x062A*/
91 1 + 2 + 256 * 0x29,/*0x062B*/
92 1 + 2 + 256 * 0x2D,/*0x062C*/
93 1 + 2 + 256 * 0x31,/*0x062D*/
94 1 + 2 + 256 * 0x35,/*0x062E*/
95 1 + 256 * 0x39,/*0x062F*/
96 1 + 256 * 0x3B,/*0x0630*/
97 1 + 256 * 0x3D,/*0x0631*/
98 1 + 256 * 0x3F,/*0x0632*/
99 1 + 2 + 256 * 0x41,/*0x0633*/
100 1 + 2 + 256 * 0x45,/*0x0634*/
101 1 + 2 + 256 * 0x49,/*0x0635*/
102 1 + 2 + 256 * 0x4D,/*0x0636*/
103 1 + 2 + 256 * 0x51,/*0x0637*/
104 1 + 2 + 256 * 0x55,/*0x0638*/
105 1 + 2 + 256 * 0x59,/*0x0639*/
106 1 + 2 + 256 * 0x5D,/*0x063A*/
107 0, 0, 0, 0, 0, /*0x063B-0x063F*/
109 1 + 2 + 256 * 0x61,/*0x0641*/
110 1 + 2 + 256 * 0x65,/*0x0642*/
111 1 + 2 + 256 * 0x69,/*0x0643*/
112 1 + 2 + 16 + 256 * 0x6D,/*0x0644*/
113 1 + 2 + 256 * 0x71,/*0x0645*/
114 1 + 2 + 256 * 0x75,/*0x0646*/
115 1 + 2 + 256 * 0x79,/*0x0647*/
116 1 + 256 * 0x7D,/*0x0648*/
117 1 + 256 * 0x7F,/*0x0649*/
118 1 + 2 + 256 * 0x81,/*0x064A*/
119 4 + 256 * 1, /*0x064B*/
120 4 + 128 + 256 * 1, /*0x064C*/
121 4 + 128 + 256 * 1, /*0x064D*/
122 4 + 128 + 256 * 1, /*0x064E*/
123 4 + 128 + 256 * 1, /*0x064F*/
124 4 + 128 + 256 * 1, /*0x0650*/
125 4 + 64 + 256 * 3, /*0x0651*/
126 4 + 256 * 1, /*0x0652*/
127 4 + 256 * 7, /*0x0653*/
128 4 + 256 * 8, /*0x0654*/
129 4 + 256 * 8, /*0x0655*/
130 4 + 256 * 1, /*0x0656*/
131 0, 0, 0, 0, 0, /*0x0657-0x065B*/
132 1 + 256 * 0x85,/*0x065C*/
133 1 + 256 * 0x87,/*0x065D*/
134 1 + 256 * 0x89,/*0x065E*/
135 1 + 256 * 0x8B,/*0x065F*/
136 0, 0, 0, 0, 0, /*0x0660-0x0664*/
137 0, 0, 0, 0, 0, /*0x0665-0x0669*/
138 0, 0, 0, 0, 0, 0, /*0x066A-0x066F*/
139 4 + 256 * 6, /*0x0670*/
140 1 + 8 + 256 * 0x00,/*0x0671*/
145 1, 1, /*0x0676-0x0677*/
146 1+2, 1+2, 1+2, 1+2, 1+2, 1+2, /*0x0678-0x067D*/
147 1+2+8+256 * 0x06, 1+2, 1+2, 1+2, 1+2, 1+2, /*0x067E-0x0683*/
148 1+2, 1+2, 1+2+8+256 * 0x2A, 1+2, /*0x0684-0x0687*/
149 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /*0x0688-0x0691*/
150 1, 1, 1, 1, 1, 1, 1+8+256 * 0x3A, 1, /*0x0692-0x0699*/
151 1+2, 1+2, 1+2, 1+2, 1+2, 1+2, /*0x069A-0x06A3*/
152 1+2, 1+2, 1+2, 1+2, /*0x069A-0x06A3*/
153 1+2, 1+2, 1+2, 1+2, 1+2, 1+2+8+256 * 0x3E, /*0x06A4-0x06AD*/
154 1+2, 1+2, 1+2, 1+2, /*0x06A4-0x06AD*/
155 1+2, 1+2+8+256 * 0x42, 1+2, 1+2, 1+2, 1+2, /*0x06AE-0x06B7*/
156 1+2, 1+2, 1+2, 1+2, /*0x06AE-0x06B7*/
157 1+2, 1+2, 1+2, 1+2, 1+2, 1+2, /*0x06B8-0x06BF*/
158 1+2, 1+2, /*0x06B8-0x06BF*/
161 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /*0x06C2-0x06CB*/
162 1+2+8+256 * 0xAC, /*0x06CC*/
164 1+2, 1+2, 1+2, 1+2, /*0x06CE-0x06D1*/
165 1, 1 /*0x06D2-0x06D3*/
168 static const uint8_t presALink
[] = {
169 /***********0*****1*****2*****3*****4*****5*****6*****7*****8*****9*****A*****B*****C*****D*****E*****F*/
170 /*FB5*/ 0, 1, 0, 0, 0, 0, 0, 1, 2,1 + 2, 0, 0, 0, 0, 0, 0,
171 /*FB6*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
172 /*FB7*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2,1 + 2, 0, 0,
173 /*FB8*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1,
174 /*FB9*/ 2,1 + 2, 0, 1, 2,1 + 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
175 /*FBA*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
176 /*FBB*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
177 /*FBC*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
178 /*FBD*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
179 /*FBE*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
180 /*FBF*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2,1 + 2,
181 /*FC0*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
182 /*FC1*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
183 /*FC2*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
184 /*FC3*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
185 /*FC4*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
186 /*FC5*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 4,
190 static const uint8_t presBLink
[]=
192 /***********0*****1*****2*****3*****4*****5*****6*****7*****8*****9*****A*****B*****C*****D*****E*****F*/
193 /*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,
194 /*FE8*/ 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 2,1 + 2, 0, 1, 0,
195 /*FE9*/ 1, 2,1 + 2, 0, 1, 0, 1, 2,1 + 2, 0, 1, 2,1 + 2, 0, 1, 2,
196 /*FEA*/1 + 2, 0, 1, 2,1 + 2, 0, 1, 2,1 + 2, 0, 1, 0, 1, 0, 1, 0,
197 /*FEB*/ 1, 0, 1, 2,1 + 2, 0, 1, 2,1 + 2, 0, 1, 2,1 + 2, 0, 1, 2,
198 /*FEC*/1 + 2, 0, 1, 2,1 + 2, 0, 1, 2,1 + 2, 0, 1, 2,1 + 2, 0, 1, 2,
199 /*FED*/1 + 2, 0, 1, 2,1 + 2, 0, 1, 2,1 + 2, 0, 1, 2,1 + 2, 0, 1, 2,
200 /*FEE*/1 + 2, 0, 1, 2,1 + 2, 0, 1, 2,1 + 2, 0, 1, 2,1 + 2, 0, 1, 0,
201 /*FEF*/ 1, 0, 1, 2,1 + 2, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0
204 static const UChar convertFBto06
[] =
206 /***********0******1******2******3******4******5******6******7******8******9******A******B******C******D******E******F***/
207 /*FB5*/ 0x671, 0x671, 0, 0, 0, 0, 0x07E, 0x07E, 0x07E, 0x07E, 0, 0, 0, 0, 0, 0,
208 /*FB6*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
209 /*FB7*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x686, 0x686, 0x686, 0x686, 0, 0,
210 /*FB8*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x698, 0x698, 0, 0, 0x6A9, 0x6A9,
211 /*FB9*/ 0x6A9, 0x6A9, 0x6AF, 0x6AF, 0x6AF, 0x6AF, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
212 /*FBA*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
213 /*FBB*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
214 /*FBC*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
215 /*FBD*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
216 /*FBE*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
217 /*FBF*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x6CC, 0x6CC, 0x6CC, 0x6CC
220 static const UChar convertFEto06
[] =
222 /***********0******1******2******3******4******5******6******7******8******9******A******B******C******D******E******F***/
223 /*FE7*/ 0x64B, 0x64B, 0x64C, 0x64C, 0x64D, 0x64D, 0x64E, 0x64E, 0x64F, 0x64F, 0x650, 0x650, 0x651, 0x651, 0x652, 0x652,
224 /*FE8*/ 0x621, 0x622, 0x622, 0x623, 0x623, 0x624, 0x624, 0x625, 0x625, 0x626, 0x626, 0x626, 0x626, 0x627, 0x627, 0x628,
225 /*FE9*/ 0x628, 0x628, 0x628, 0x629, 0x629, 0x62A, 0x62A, 0x62A, 0x62A, 0x62B, 0x62B, 0x62B, 0x62B, 0x62C, 0x62C, 0x62C,
226 /*FEA*/ 0x62C, 0x62D, 0x62D, 0x62D, 0x62D, 0x62E, 0x62E, 0x62E, 0x62E, 0x62F, 0x62F, 0x630, 0x630, 0x631, 0x631, 0x632,
227 /*FEB*/ 0x632, 0x633, 0x633, 0x633, 0x633, 0x634, 0x634, 0x634, 0x634, 0x635, 0x635, 0x635, 0x635, 0x636, 0x636, 0x636,
228 /*FEC*/ 0x636, 0x637, 0x637, 0x637, 0x637, 0x638, 0x638, 0x638, 0x638, 0x639, 0x639, 0x639, 0x639, 0x63A, 0x63A, 0x63A,
229 /*FED*/ 0x63A, 0x641, 0x641, 0x641, 0x641, 0x642, 0x642, 0x642, 0x642, 0x643, 0x643, 0x643, 0x643, 0x644, 0x644, 0x644,
230 /*FEE*/ 0x644, 0x645, 0x645, 0x645, 0x645, 0x646, 0x646, 0x646, 0x646, 0x647, 0x647, 0x647, 0x647, 0x648, 0x648, 0x649,
231 /*FEF*/ 0x649, 0x64A, 0x64A, 0x64A, 0x64A, 0x65C, 0x65C, 0x65D, 0x65D, 0x65E, 0x65E, 0x65F, 0x65F
234 static const uint8_t shapeTable
[4][4][4]=
236 { {0,0,0,0}, {0,0,0,0}, {0,1,0,3}, {0,1,0,1} },
237 { {0,0,2,2}, {0,0,1,2}, {0,1,1,2}, {0,1,1,3} },
238 { {0,0,0,0}, {0,0,0,0}, {0,1,0,3}, {0,1,0,3} },
239 { {0,0,1,2}, {0,0,1,2}, {0,1,1,2}, {0,1,1,3} }
243 * This function shapes European digits to Arabic-Indic digits
244 * in-place, writing over the input characters.
245 * Since we know that we are only looking for BMP code points,
246 * we can safely just work with code units (again, at least UTF-16).
249 _shapeToArabicDigitsWithContext(UChar
*s
, int32_t length
,
251 UBool isLogical
, UBool lastStrongWasAL
) {
252 const UBiDiProps
*bdp
;
253 UErrorCode errorCode
;
258 errorCode
=U_ZERO_ERROR
;
259 bdp
=ubidi_getSingleton(&errorCode
);
260 if(U_FAILURE(errorCode
)) {
266 /* the iteration direction depends on the type of input */
268 for(i
=0; i
<length
; ++i
) {
270 switch(ubidi_getClass(bdp
, c
)) {
271 case U_LEFT_TO_RIGHT
: /* L */
272 case U_RIGHT_TO_LEFT
: /* R */
273 lastStrongWasAL
=FALSE
;
275 case U_RIGHT_TO_LEFT_ARABIC
: /* AL */
276 lastStrongWasAL
=TRUE
;
278 case U_EUROPEAN_NUMBER
: /* EN */
279 if(lastStrongWasAL
&& (uint32_t)(c
-0x30)<10) {
280 s
[i
]=(UChar
)(digitBase
+c
); /* digitBase+(c-0x30) - digitBase was modified above */
288 for(i
=length
; i
>0; /* pre-decrement in the body */) {
290 switch(ubidi_getClass(bdp
, c
)) {
291 case U_LEFT_TO_RIGHT
: /* L */
292 case U_RIGHT_TO_LEFT
: /* R */
293 lastStrongWasAL
=FALSE
;
295 case U_RIGHT_TO_LEFT_ARABIC
: /* AL */
296 lastStrongWasAL
=TRUE
;
298 case U_EUROPEAN_NUMBER
: /* EN */
299 if(lastStrongWasAL
&& (uint32_t)(c
-0x30)<10) {
300 s
[i
]=(UChar
)(digitBase
+c
); /* digitBase+(c-0x30) - digitBase was modified above */
312 *Function : This function inverts the buffer, it's used
313 * in case the user specifies the buffer to be
314 * U_SHAPE_TEXT_DIRECTION_LOGICAL
317 invertBuffer(UChar
*buffer
,int32_t size
,uint32_t options
,int32_t lowlimit
,int32_t highlimit
) {
320 for(i
=lowlimit
,j
=size
-highlimit
-1;i
<j
;i
++,j
--) {
322 buffer
[i
] = buffer
[j
];
328 *Name : changeLamAlef
329 *Function : Converts the Alef characters into an equivalent
330 * LamAlef location in the 0x06xx Range, this is an
331 * intermediate stage in the operation of the program
332 * later it'll be converted into the 0xFExx LamAlefs
333 * in the shaping function.
335 static U_INLINE UChar
336 changeLamAlef(UChar ch
) {
352 *Function : Resolves the link between the characters as
353 * Arabic characters have four forms :
354 * Isolated, Initial, Middle and Final Form
358 if(ch
>= 0x0622 && ch
<= 0x06D3) {
359 return(araLink
[ch
-0x0622]);
360 } else if(ch
== 0x200D) {
362 } else if(ch
>= 0x206D && ch
<= 0x206F) {
364 } else if(ch
>= 0xFB50 && ch
<= 0xFC62) {
365 return(presALink
[ch
-0xFB50]);
366 } else if(ch
>= 0xFE70 && ch
<= 0xFEFC) {
367 return(presBLink
[ch
-0xFE70]);
375 *Function : Counts the number of spaces
376 * at each end of the logical buffer
379 countSpaces(UChar
*dest
,int32_t size
,uint32_t options
,int32_t *spacesCountl
,int32_t *spacesCountr
) {
381 int32_t countl
= 0,countr
= 0;
382 while(dest
[i
] == 0x0020) {
386 while(dest
[size
-1] == 0x0020) {
390 *spacesCountl
= countl
;
391 *spacesCountr
= countr
;
395 *Name : isTashkeelChar
396 *Function : Returns 1 for Tashkeel characters else return 0
398 static U_INLINE
int32_t
399 isTashkeelChar(UChar ch
) {
400 return (int32_t)( ch
>=0x064B && ch
<= 0x0652 );
405 *Function : Returns 1 for Alef characters else return 0
407 static U_INLINE
int32_t
408 isAlefChar(UChar ch
) {
409 return (int32_t)( (ch
==0x0622)||(ch
==0x0623)||(ch
==0x0625)||(ch
==0x0627) );
413 *Name : isLamAlefChar
414 *Function : Returns 1 for LamAlef characters else return 0
416 static U_INLINE
int32_t
417 isLamAlefChar(UChar ch
) {
418 return (int32_t)( (ch
>=0xFEF5)&&(ch
<=0xFEFC) );
422 *Name : calculateSize
423 *Function : This function calculates the destSize to be used in preflighting
424 * when the destSize is equal to 0
427 calculateSize(const UChar
*source
, int32_t sourceLength
,
428 int32_t destSize
,uint32_t options
) {
430 destSize
= sourceLength
;
431 switch(options
&U_SHAPE_LETTERS_MASK
) {
432 case U_SHAPE_LETTERS_SHAPE
:
433 case U_SHAPE_LETTERS_SHAPE_TASHKEEL_ISOLATED
:
434 if((options
&U_SHAPE_TEXT_DIRECTION_MASK
)==U_SHAPE_TEXT_DIRECTION_VISUAL_LTR
) {
435 for(i
=0;i
<sourceLength
;i
++) {
436 if( (isAlefChar(source
[i
]))&&(source
[i
+1]==0x0644) ) {
440 } else if((options
&U_SHAPE_TEXT_DIRECTION_MASK
)==U_SHAPE_TEXT_DIRECTION_LOGICAL
) {
441 for(i
=0;i
<sourceLength
;i
++) {
442 if( (isAlefChar(source
[i
+1]))&&(source
[i
]==0x0644) ) {
449 case U_SHAPE_LETTERS_UNSHAPE
:
450 for(i
=0;i
<sourceLength
;i
++) {
451 if( isLamAlefChar(source
[i
]) ) {
458 /* will never occur because of validity checks at the begin of u_shapeArabic */
466 *Name : removeLamAlefSpaces
467 *Function : The shapeUnicode function converts Lam + Alef into LamAlef + space,
468 * this function removes the spaces behind the LamAlefs according to
469 * the options the user specifies, the spaces are removed to the end
470 * of the buffer, or shrink the buffer ab=nd remove spaces for good
471 * or leave the buffer as it is LamAlef + space.
474 removeLamAlefSpaces(UChar
*dest
, int32_t sourceLength
,
477 UErrorCode
*pErrorCode
) {
479 int32_t i
= 0, j
= 0;
481 UChar
*tempbuffer
=NULL
;
483 switch(options
&U_SHAPE_LENGTH_MASK
) {
484 case U_SHAPE_LENGTH_GROW_SHRINK
:
485 tempbuffer
= (UChar
*)uprv_malloc((sourceLength
+1)*U_SIZEOF_UCHAR
);
487 if(tempbuffer
== NULL
) {
488 *pErrorCode
= U_MEMORY_ALLOCATION_ERROR
;
492 uprv_memset(tempbuffer
, 0, (sourceLength
+1)*U_SIZEOF_UCHAR
);
495 while(i
< sourceLength
) {
496 if(dest
[i
] == 0xFFFF) {
501 tempbuffer
[j
] = dest
[i
];
507 tempbuffer
[i
] = 0x0000;
512 uprv_memcpy(dest
, tempbuffer
, sourceLength
*U_SIZEOF_UCHAR
);
513 destSize
= u_strlen(dest
);
516 case U_SHAPE_LENGTH_FIXED_SPACES_NEAR
:
517 /* Lam+Alef is already shaped into LamAlef + FFFF */
519 while(i
< sourceLength
) {
520 if(dest
[i
] == 0xFFFF)
524 destSize
= sourceLength
;
527 case U_SHAPE_LENGTH_FIXED_SPACES_AT_BEGINNING
:
528 tempbuffer
= (UChar
*)uprv_malloc((sourceLength
+1)*U_SIZEOF_UCHAR
);
531 if(tempbuffer
== NULL
) {
532 *pErrorCode
= U_MEMORY_ALLOCATION_ERROR
;
536 uprv_memset(tempbuffer
, 0, (sourceLength
+1)*U_SIZEOF_UCHAR
);
538 i
= j
= sourceLength
;
540 if(dest
[i
] == 0xFFFF) {
545 tempbuffer
[j
] = dest
[i
];
550 tempbuffer
[i
] = 0x0020;
552 uprv_memcpy(dest
, tempbuffer
, sourceLength
*U_SIZEOF_UCHAR
);
553 destSize
= sourceLength
;
556 case U_SHAPE_LENGTH_FIXED_SPACES_AT_END
:
557 tempbuffer
= (UChar
*)uprv_malloc((sourceLength
+1)*U_SIZEOF_UCHAR
);
560 if(tempbuffer
== NULL
) {
561 *pErrorCode
= U_MEMORY_ALLOCATION_ERROR
;
565 uprv_memset(tempbuffer
, 0, (sourceLength
+1)*U_SIZEOF_UCHAR
);
568 while(i
< sourceLength
) {
569 if(dest
[i
] == 0xFFFF) {
574 tempbuffer
[j
] = dest
[i
];
580 tempbuffer
[i
] = 0x0020;
585 uprv_memcpy(dest
,tempbuffer
, sourceLength
*U_SIZEOF_UCHAR
);
586 destSize
= sourceLength
;
595 uprv_free(tempbuffer
);
601 *Name : expandLamAlef
602 *Function : LamAlef needs special handling as the LamAlef is
603 * one character while expanding it will give two
604 * characters Lam + Alef, so we need to expand the LamAlef
605 * in near or far spaces according to the options the user
606 * specifies or increase the buffer size.
607 * If there are no spaces to expand the LamAlef, an error
608 * will be set to U_NO_SPACE_AVAILABLE as defined in utypes.h
611 expandLamAlef(UChar
*dest
, int32_t sourceLength
,
612 int32_t destSize
,uint32_t options
,
613 UErrorCode
*pErrorCode
) {
618 int32_t inpsize
= sourceLength
;
620 UChar
*tempbuffer
=NULL
;
622 switch(options
&U_SHAPE_LENGTH_MASK
) {
624 case U_SHAPE_LENGTH_GROW_SHRINK
:
625 destSize
= calculateSize(dest
,sourceLength
,destSize
,options
);
626 tempbuffer
= (UChar
*)uprv_malloc((destSize
+1)*U_SIZEOF_UCHAR
);
629 if(tempbuffer
== NULL
) {
630 *pErrorCode
= U_MEMORY_ALLOCATION_ERROR
;
634 uprv_memset(tempbuffer
, 0, (destSize
+1)*U_SIZEOF_UCHAR
);
637 while(i
< destSize
&& j
< destSize
) {
638 if( isLamAlefChar(dest
[i
]) ) {
639 tempbuffer
[j
] = convertLamAlef
[ dest
[i
] - 0xFEF5 ];
640 tempbuffer
[j
+1] = 0x0644;
644 tempbuffer
[j
] = dest
[i
];
649 uprv_memcpy(dest
, tempbuffer
, destSize
*U_SIZEOF_UCHAR
);
652 case U_SHAPE_LENGTH_FIXED_SPACES_NEAR
:
653 for(i
= 0;i
<sourceLength
;i
++) {
654 if((dest
[i
] == 0x0020) && isLamAlefChar(dest
[i
+1])) {
655 lamalefChar
= dest
[i
+1];
657 dest
[i
] = convertLamAlef
[ lamalefChar
- 0xFEF5 ];
660 if((dest
[i
] != 0x0020) && isLamAlefChar(dest
[i
+1])) {
661 *pErrorCode
=U_NO_SPACE_AVAILABLE
;
664 destSize
= sourceLength
;
667 case U_SHAPE_LENGTH_FIXED_SPACES_AT_BEGINNING
:
668 tempbuffer
= (UChar
*)uprv_malloc((sourceLength
+1)*U_SIZEOF_UCHAR
);
671 if(tempbuffer
== NULL
) {
672 *pErrorCode
= U_MEMORY_ALLOCATION_ERROR
;
676 uprv_memset(tempbuffer
, 0, (sourceLength
+1)*U_SIZEOF_UCHAR
);
679 while(dest
[i
] == 0x0020) {
684 i
= j
= sourceLength
-1;
685 while(i
>= 0 && j
>= 0) {
686 if( countl
>0 && isLamAlefChar(dest
[i
]) ) {
687 tempbuffer
[j
] = 0x0644;
688 tempbuffer
[j
-1] = convertLamAlef
[ dest
[i
] - 0xFEF5 ];
693 if( countl
== 0 && isLamAlefChar(dest
[i
]) )
694 *pErrorCode
=U_NO_SPACE_AVAILABLE
;
695 tempbuffer
[j
] = dest
[i
];
701 uprv_memcpy(dest
, tempbuffer
, sourceLength
*U_SIZEOF_UCHAR
);
702 destSize
= sourceLength
;
705 case U_SHAPE_LENGTH_FIXED_SPACES_AT_END
:
706 /* LamAlef expansion below is done from right to left to make sure that we consume
707 * the spaces with the LamAlefs as they appear in the visual buffer from right to left
709 tempbuffer
= (UChar
*)uprv_malloc((sourceLength
+1)*U_SIZEOF_UCHAR
);
712 if(tempbuffer
== NULL
) {
713 *pErrorCode
= U_MEMORY_ALLOCATION_ERROR
;
717 uprv_memset(tempbuffer
, 0, (sourceLength
+1)*U_SIZEOF_UCHAR
);
719 while(dest
[inpsize
-1] == 0x0020) {
724 i
= sourceLength
- countr
- 1;
725 j
= sourceLength
- 1;
727 while(i
>= 0 && j
>= 0) {
728 if( countr
>0 && isLamAlefChar(dest
[i
]) ) {
729 tempbuffer
[j
] = 0x0644;
730 tempbuffer
[j
-1] = convertLamAlef
[ dest
[i
] - 0xFEF5 ];
735 if( countr
== 0 && isLamAlefChar(dest
[i
]) )
736 *pErrorCode
=U_NO_SPACE_AVAILABLE
;
737 tempbuffer
[j
] = dest
[i
];
744 uprv_memmove(tempbuffer
, tempbuffer
+countr
, sourceLength
*U_SIZEOF_UCHAR
);
745 if(u_strlen(tempbuffer
) < sourceLength
) {
746 for(i
=sourceLength
-1;i
>=sourceLength
-countr
;i
--) {
747 tempbuffer
[i
] = 0x0020;
752 uprv_memcpy(dest
, tempbuffer
, sourceLength
*U_SIZEOF_UCHAR
);
754 destSize
= sourceLength
;
758 /* will never occur because of validity checks */
763 uprv_free(tempbuffer
);
770 *Function : Converts an Arabic Unicode buffer in 06xx Range into a shaped
771 * arabic Unicode buffer in FExx Range
774 shapeUnicode(UChar
*dest
, int32_t sourceLength
,
775 int32_t destSize
,uint32_t options
,
776 UErrorCode
*pErrorCode
,
781 int32_t lastPos
,Nx
, Nw
;
783 int32_t lamalef_found
= 0;
784 UChar prevLink
= 0, lastLink
= 0, currLink
, nextLink
= 0;
788 * Converts the input buffer from FExx Range into 06xx Range
789 * to make sure that all characters are in the 06xx range
790 * even the lamalef is converted to the special region in
793 if ((options
& U_SHAPE_PRESERVE_PRESENTATION_MASK
) == U_SHAPE_PRESERVE_PRESENTATION_NOOP
) {
794 for (i
= 0; i
< sourceLength
; i
++) {
795 UChar inputChar
= dest
[i
];
796 if ( (inputChar
>= 0xFB50) && (inputChar
<= 0xFBFF)) {
797 UChar c
= convertFBto06
[ (inputChar
- 0xFB50) ];
800 } else if ( (inputChar
>= 0xFE70) && (inputChar
<= 0xFEFC)) {
801 dest
[i
] = convertFEto06
[ (inputChar
- 0xFE70) ] ;
803 dest
[i
] = inputChar
;
808 /* sets the index to the end of the buffer, together with the step point to -1 */
809 i
= sourceLength
- 1;
814 * This function resolves the link between the characters .
815 * Arabic characters have four forms :
816 * Isolated Form, Initial Form, Middle Form and Final Form
818 currLink
= getLink(dest
[i
]);
824 /* If high byte of currLink > 0 then more than one shape */
825 if ((currLink
& 0xFF00) > 0 || (getLink(dest
[i
]) & IRRELEVANT
) != 0) {
827 while (Nx
< 0) { /* we need to know about next char */
832 nextLink
= getLink(dest
[Nw
]);
833 if((nextLink
& IRRELEVANT
) == 0) {
841 if ( ((currLink
& ALEFTYPE
) > 0) && ((lastLink
& LAMTYPE
) > 0) ) {
843 wLamalef
= changeLamAlef(dest
[i
]); /*get from 0x065C-0x065f */
844 if ( wLamalef
!= 0) {
845 dest
[i
] = 0xFFFF; /* The default case is to drop the Alef and replace */
846 dest
[lastPos
] =wLamalef
; /* it by 0xFFFF which is the last character in the */
847 i
=lastPos
; /* unicode private use area, this is done to make */
848 } /* sure that removeLamAlefSpaces() handles only the */
849 lastLink
= prevLink
; /* spaces generated during lamalef generation. */
850 currLink
= getLink(wLamalef
); /* 0xFFFF is added here and is replaced by spaces */
851 } /* in removeLamAlefSpaces() */
853 * get the proper shape according to link ability of neighbors
854 * and of character; depends on the order of the shapes
855 * (isolated, initial, middle, final) in the compatibility area
857 Shape
= shapeTable
[nextLink
& (LINKR
+ LINKL
)]
858 [lastLink
& (LINKR
+ LINKL
)]
859 [currLink
& (LINKR
+ LINKL
)];
861 if ((currLink
& (LINKR
+LINKL
)) == 1) {
863 } else if(isTashkeelChar(dest
[i
])) {
864 if( (lastLink
& LINKL
) && (nextLink
& LINKR
) && (tashkeelFlag
== 1) &&
865 dest
[i
] != 0x064C && dest
[i
] != 0x064D )
868 if( (nextLink
&ALEFTYPE
) == ALEFTYPE
&& (lastLink
&LAMTYPE
) == LAMTYPE
) {
876 if ((dest
[i
] ^ 0x0600) < 0x100) {
877 if(isTashkeelChar(dest
[i
]))
878 dest
[i
] = 0xFE70 + IrrelevantPos
[(dest
[i
] - 0x064B)] + Shape
;
879 else if ((currLink
& APRESENT
) > 0)
880 dest
[i
] = (UChar
)(0xFB50 + (currLink
>> 8) + Shape
);
881 else if ((currLink
>> 8) > 0 && (currLink
& IRRELEVANT
) == 0)
882 dest
[i
] = (UChar
)(0xFE70 + (currLink
>> 8) + Shape
);
886 /* move one notch forward */
887 if ((currLink
& IRRELEVANT
) == 0) {
897 } else if(i
!= iend
) {
898 currLink
= getLink(dest
[i
]);
902 if(lamalef_found
!= 0)
903 destSize
= removeLamAlefSpaces(dest
,sourceLength
,destSize
,options
,pErrorCode
);
905 destSize
= sourceLength
;
911 *Name : deShapeUnicode
912 *Function : Converts an Arabic Unicode buffer in FExx Range into unshaped
913 * arabic Unicode buffer in 06xx Range
916 deShapeUnicode(UChar
*dest
, int32_t sourceLength
,
917 int32_t destSize
,uint32_t options
,
918 UErrorCode
*pErrorCode
) {
920 int32_t lamalef_found
= 0;
923 *This for loop changes the buffer from the Unicode FE range to
924 *the Unicode 06 range
926 for(i
= 0; i
< sourceLength
; i
++) {
927 UChar inputChar
= dest
[i
];
928 if ( (inputChar
>= 0xFB50) && (inputChar
<= 0xFBFF)) { /* FBxx Arabic range */
929 UChar c
= convertFBto06
[ (inputChar
- 0xFB50) ];
932 } else if (( inputChar
>= 0xFE70) && (inputChar
<= 0xFEF4 )) { /* FExx Arabic range */
933 dest
[i
] = convertFEto06
[ (inputChar
- 0xFE70) ] ;
935 dest
[i
] = inputChar
;
937 if( isLamAlefChar(dest
[i
]) )
941 /* If there is lamalef in the buffer call expandLamAlef */
942 if(lamalef_found
!= 0)
943 destSize
= expandLamAlef(dest
,sourceLength
,destSize
,options
,pErrorCode
);
945 destSize
= sourceLength
;
950 U_CAPI
int32_t U_EXPORT2
951 u_shapeArabic(const UChar
*source
, int32_t sourceLength
,
952 UChar
*dest
, int32_t destCapacity
,
954 UErrorCode
*pErrorCode
) {
958 /* usual error checking */
959 if(pErrorCode
==NULL
|| U_FAILURE(*pErrorCode
)) {
963 /* make sure that no reserved options values are used; allow dest==NULL only for preflighting */
964 if( source
==NULL
|| sourceLength
<-1 || (dest
==NULL
&& destCapacity
!=0) || destCapacity
<0 ||
965 (options
&U_SHAPE_DIGIT_TYPE_RESERVED
)==U_SHAPE_DIGIT_TYPE_RESERVED
||
966 (options
&U_SHAPE_DIGITS_MASK
)==U_SHAPE_DIGITS_RESERVED
||
967 ((options
&U_SHAPE_LENGTH_MASK
) != U_SHAPE_LENGTH_GROW_SHRINK
&&
968 (options
&U_SHAPE_AGGREGATE_TASHKEEL_MASK
) != 0) ||
969 ((options
&U_SHAPE_AGGREGATE_TASHKEEL_MASK
) == U_SHAPE_AGGREGATE_TASHKEEL
&&
970 (options
&U_SHAPE_LETTERS_SHAPE_TASHKEEL_ISOLATED
) != U_SHAPE_LETTERS_SHAPE_TASHKEEL_ISOLATED
)
972 *pErrorCode
=U_ILLEGAL_ARGUMENT_ERROR
;
976 /* determine the source length */
977 if(sourceLength
==-1) {
978 sourceLength
=u_strlen(source
);
980 if(sourceLength
<=0) {
981 return u_terminateUChars(dest
, destCapacity
, 0, pErrorCode
);
984 /* check that source and destination do not overlap */
986 ((source
<=dest
&& dest
<source
+sourceLength
) ||
987 (dest
<=source
&& source
<dest
+destCapacity
))) {
988 *pErrorCode
=U_ILLEGAL_ARGUMENT_ERROR
;
992 if((options
&U_SHAPE_LETTERS_MASK
)!=U_SHAPE_LETTERS_NOOP
) {
994 UChar
*tempbuffer
, *tempsource
= NULL
;
995 int32_t outputSize
, spacesCountl
=0, spacesCountr
=0;
997 if((options
&U_SHAPE_AGGREGATE_TASHKEEL_MASK
)>0) {
998 int32_t logical_order
= (options
&U_SHAPE_TEXT_DIRECTION_MASK
) == U_SHAPE_TEXT_DIRECTION_LOGICAL
;
999 int32_t aggregate_tashkeel
=
1000 (options
&(U_SHAPE_AGGREGATE_TASHKEEL_MASK
+U_SHAPE_LETTERS_SHAPE_TASHKEEL_ISOLATED
)) ==
1001 (U_SHAPE_AGGREGATE_TASHKEEL
+U_SHAPE_LETTERS_SHAPE_TASHKEEL_ISOLATED
);
1002 int step
=logical_order
?1:-1;
1003 int j
=logical_order
?-1:2*sourceLength
;
1004 int i
=logical_order
?-1:sourceLength
;
1005 int end
=logical_order
?sourceLength
:-1;
1006 int aggregation_possible
= 1;
1008 UChar prevLink
, currLink
= 0;
1009 int newSourceLength
= 0;
1010 tempsource
= (UChar
*)uprv_malloc(2*sourceLength
*U_SIZEOF_UCHAR
);
1011 if(tempsource
== NULL
) {
1012 *pErrorCode
= U_MEMORY_ALLOCATION_ERROR
;
1016 while ((i
+=step
) != end
) {
1017 prevLink
= currLink
;
1018 currLink
= getLink(source
[i
]);
1019 if (aggregate_tashkeel
&& ((prevLink
|currLink
)&COMBINE
) == COMBINE
&& aggregation_possible
) {
1020 aggregation_possible
= 0;
1021 tempsource
[j
] = (prev
<source
[i
]?prev
:source
[i
])-0x064C+0xFC5E;
1022 currLink
= getLink(tempsource
[j
]);
1024 aggregation_possible
= 1;
1025 tempsource
[j
+=step
] = source
[i
];
1030 source
= tempsource
+(logical_order
?0:j
);
1031 sourceLength
= newSourceLength
;
1034 /* calculate destination size */
1035 /* TODO: do we ever need to do this pure preflighting? */
1036 if((options
&U_SHAPE_LENGTH_MASK
)==U_SHAPE_LENGTH_GROW_SHRINK
) {
1037 outputSize
=calculateSize(source
,sourceLength
,destCapacity
,options
);
1039 outputSize
=sourceLength
;
1041 if(outputSize
>destCapacity
) {
1042 *pErrorCode
=U_BUFFER_OVERFLOW_ERROR
;
1043 if (tempsource
!= NULL
) uprv_free(tempsource
);
1048 * need a temporary buffer of size max(outputSize, sourceLength)
1049 * because at first we copy source->temp
1051 if(sourceLength
>outputSize
) {
1052 outputSize
=sourceLength
;
1055 /* Start of Arabic letter shaping part */
1056 if(outputSize
<=sizeof(buffer
)/U_SIZEOF_UCHAR
) {
1057 outputSize
=sizeof(buffer
)/U_SIZEOF_UCHAR
;
1060 tempbuffer
= (UChar
*)uprv_malloc(outputSize
*U_SIZEOF_UCHAR
);
1063 if(tempbuffer
== NULL
) {
1064 *pErrorCode
= U_MEMORY_ALLOCATION_ERROR
;
1065 if (tempsource
!= NULL
) uprv_free(tempsource
);
1069 uprv_memcpy(tempbuffer
, source
, sourceLength
*U_SIZEOF_UCHAR
);
1070 if (tempsource
!= NULL
) uprv_free(tempsource
);
1071 if(sourceLength
<outputSize
) {
1072 uprv_memset(tempbuffer
+sourceLength
, 0, (outputSize
-sourceLength
)*U_SIZEOF_UCHAR
);
1075 if((options
&U_SHAPE_TEXT_DIRECTION_MASK
) == U_SHAPE_TEXT_DIRECTION_LOGICAL
) {
1076 countSpaces(tempbuffer
,sourceLength
,options
,&spacesCountl
,&spacesCountr
);
1077 invertBuffer(tempbuffer
,sourceLength
,options
,spacesCountl
,spacesCountr
);
1080 switch(options
&U_SHAPE_LETTERS_MASK
) {
1081 case U_SHAPE_LETTERS_SHAPE
:
1082 /* Call the shaping function with tashkeel flag == 1 */
1083 destLength
= shapeUnicode(tempbuffer
,sourceLength
,destCapacity
,options
,pErrorCode
,1);
1085 case U_SHAPE_LETTERS_SHAPE_TASHKEEL_ISOLATED
:
1086 /* Call the shaping function with tashkeel flag == 0 */
1087 destLength
= shapeUnicode(tempbuffer
,sourceLength
,destCapacity
,options
,pErrorCode
,0);
1089 case U_SHAPE_LETTERS_UNSHAPE
:
1090 /* Call the deshaping function */
1091 destLength
= deShapeUnicode(tempbuffer
,sourceLength
,destCapacity
,options
,pErrorCode
);
1094 /* will never occur because of validity checks above */
1100 * TODO: (markus 2002aug01)
1101 * For as long as we always preflight the outputSize above
1102 * we should U_ASSERT(outputSize==destLength)
1103 * except for the adjustment above before the tempbuffer allocation
1106 if((options
&U_SHAPE_TEXT_DIRECTION_MASK
) == U_SHAPE_TEXT_DIRECTION_LOGICAL
) {
1107 countSpaces(tempbuffer
,destLength
,options
,&spacesCountl
,&spacesCountr
);
1108 invertBuffer(tempbuffer
,destLength
,options
,spacesCountl
,spacesCountr
);
1110 uprv_memcpy(dest
, tempbuffer
, uprv_min(destLength
, destCapacity
)*U_SIZEOF_UCHAR
);
1112 if(tempbuffer
!=buffer
) {
1113 uprv_free(tempbuffer
);
1116 if(destLength
>destCapacity
) {
1117 *pErrorCode
=U_BUFFER_OVERFLOW_ERROR
;
1121 /* End of Arabic letter shaping part */
1124 * No letter shaping:
1125 * just make sure the destination is large enough and copy the string.
1127 if(destCapacity
<sourceLength
) {
1128 /* this catches preflighting, too */
1129 *pErrorCode
=U_BUFFER_OVERFLOW_ERROR
;
1130 return sourceLength
;
1132 uprv_memcpy(dest
, source
, sourceLength
*U_SIZEOF_UCHAR
);
1133 destLength
=sourceLength
;
1137 * Perform number shaping.
1138 * With UTF-16 or UTF-32, the length of the string is constant.
1139 * The easiest way to do this is to operate on the destination and
1140 * "shape" the digits in-place.
1142 if((options
&U_SHAPE_DIGITS_MASK
)!=U_SHAPE_DIGITS_NOOP
) {
1146 /* select the requested digit group */
1147 switch(options
&U_SHAPE_DIGIT_TYPE_MASK
) {
1148 case U_SHAPE_DIGIT_TYPE_AN
:
1149 digitBase
=0x660; /* Unicode: "Arabic-Indic digits" */
1151 case U_SHAPE_DIGIT_TYPE_AN_EXTENDED
:
1152 digitBase
=0x6f0; /* Unicode: "Eastern Arabic-Indic digits (Persian and Urdu)" */
1155 /* will never occur because of validity checks above */
1160 /* perform the requested operation */
1161 switch(options
&U_SHAPE_DIGITS_MASK
) {
1162 case U_SHAPE_DIGITS_EN2AN
:
1163 /* add (digitBase-'0') to each European (ASCII) digit code point */
1165 for(i
=0; i
<destLength
; ++i
) {
1166 if(((uint32_t)dest
[i
]-0x30)<10) {
1171 case U_SHAPE_DIGITS_AN2EN
:
1172 /* subtract (digitBase-'0') from each Arabic digit code point */
1173 for(i
=0; i
<destLength
; ++i
) {
1174 if(((uint32_t)dest
[i
]-(uint32_t)digitBase
)<10) {
1175 dest
[i
]-=digitBase
-0x30;
1179 case U_SHAPE_DIGITS_ALEN2AN_INIT_LR
:
1180 _shapeToArabicDigitsWithContext(dest
, destLength
,
1182 (UBool
)((options
&U_SHAPE_TEXT_DIRECTION_MASK
)==U_SHAPE_TEXT_DIRECTION_LOGICAL
),
1185 case U_SHAPE_DIGITS_ALEN2AN_INIT_AL
:
1186 _shapeToArabicDigitsWithContext(dest
, destLength
,
1188 (UBool
)((options
&U_SHAPE_TEXT_DIRECTION_MASK
)==U_SHAPE_TEXT_DIRECTION_LOGICAL
),
1192 /* will never occur because of validity checks above */
1197 return u_terminateUChars(dest
, destCapacity
, destLength
, pErrorCode
);