Debian Codegraph @

intl/icu/source/common/ushape.cpp

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