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