blob: e91f402453a1de62ee7189b0e217db0acc4ccede [file] [log] [blame]
Jean-Baptiste Querub13da9d2009-07-17 17:53:22 -07001/*
2 ******************************************************************************
3 *
Craig Cornelius103e9ff2012-10-09 17:03:29 -07004 * Copyright (C) 2000-2012, International Business Machines
Jean-Baptiste Querub13da9d2009-07-17 17:53:22 -07005 * Corporation and others. All Rights Reserved.
6 *
7 ******************************************************************************
Craig Cornelius103e9ff2012-10-09 17:03:29 -07008 * file name: ushape.cpp
Jean-Baptiste Querub13da9d2009-07-17 17:53:22 -07009 * 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 */
18
19#include "unicode/utypes.h"
20#include "unicode/uchar.h"
21#include "unicode/ustring.h"
22#include "unicode/ushape.h"
23#include "cmemory.h"
24#include "putilimp.h"
25#include "ustr_imp.h"
26#include "ubidi_props.h"
Craig Cornelius103e9ff2012-10-09 17:03:29 -070027#include "uassert.h"
Jean-Baptiste Querub13da9d2009-07-17 17:53:22 -070028
Craig Cornelius103e9ff2012-10-09 17:03:29 -070029#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 */
Jean-Baptiste Querub13da9d2009-07-17 17:53:22 -070038
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
56#define APRESENT 8
57#define SHADDA 64
58#define CSHADDA 128
59#define COMBINE (SHADDA+CSHADDA)
60
Jean-Baptiste Querub0ac9372009-07-20 15:09:32 -070061#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
74
75#define SHAPE_MODE 0
76#define DESHAPE_MODE 1
77
claireho50294ea2010-05-03 15:44:48 -070078static UChar tailChar = OLD_TAIL_CHAR;
79static uint32_t uShapeLamalefBegin = U_SHAPE_LAMALEF_BEGIN;
Craig Cornelius103e9ff2012-10-09 17:03:29 -070080static uint32_t uShapeLamalefEnd = U_SHAPE_LAMALEF_END;
claireho50294ea2010-05-03 15:44:48 -070081static uint32_t uShapeTashkeelBegin = U_SHAPE_TASHKEEL_BEGIN;
82static uint32_t uShapeTashkeelEnd = U_SHAPE_TASHKEEL_END;
83static int spacesRelativeToTextBeginEnd = 0;
Jean-Baptiste Querub0ac9372009-07-20 15:09:32 -070084
85static const uint8_t tailFamilyIsolatedFinal[] = {
86 /* FEB1 */ 1,
87 /* FEB2 */ 1,
88 /* FEB3 */ 0,
89 /* FEB4 */ 0,
90 /* FEB5 */ 1,
91 /* FEB6 */ 1,
92 /* FEB7 */ 0,
93 /* FEB8 */ 0,
94 /* FEB9 */ 1,
95 /* FEBA */ 1,
96 /* FEBB */ 0,
97 /* FEBC */ 0,
98 /* FEBD */ 1,
99 /* FEBE */ 1
100};
101
102static const uint8_t tashkeelMedial[] = {
103 /* FE70 */ 0,
104 /* FE71 */ 1,
105 /* FE72 */ 0,
106 /* FE73 */ 0,
107 /* FE74 */ 0,
108 /* FE75 */ 0,
109 /* FE76 */ 0,
110 /* FE77 */ 1,
111 /* FE78 */ 0,
112 /* FE79 */ 1,
113 /* FE7A */ 0,
114 /* FE7B */ 1,
115 /* FE7C */ 0,
116 /* FE7D */ 1,
117 /* FE7E */ 0,
118 /* FE7F */ 1
119};
120
121static const UChar yehHamzaToYeh[] =
122{
123/* isolated*/ 0xFEEF,
124/* final */ 0xFEF0
125};
Jean-Baptiste Querub13da9d2009-07-17 17:53:22 -0700126
Jean-Baptiste Queruc69afce2009-07-20 15:02:39 -0700127static const uint8_t IrrelevantPos[] = {
Jean-Baptiste Querub13da9d2009-07-17 17:53:22 -0700128 0x0, 0x2, 0x4, 0x6,
Jean-Baptiste Querub0ac9372009-07-20 15:09:32 -0700129 0x8, 0xA, 0xC, 0xE
Jean-Baptiste Querub13da9d2009-07-17 17:53:22 -0700130};
131
Jean-Baptiste Querub0ac9372009-07-20 15:09:32 -0700132
Jean-Baptiste Querub13da9d2009-07-17 17:53:22 -0700133static const UChar convertLamAlef[] =
134{
135/*FEF5*/ 0x0622,
136/*FEF6*/ 0x0622,
137/*FEF7*/ 0x0623,
138/*FEF8*/ 0x0623,
139/*FEF9*/ 0x0625,
140/*FEFA*/ 0x0625,
141/*FEFB*/ 0x0627,
142/*FEFC*/ 0x0627
143};
144
145static const UChar araLink[178]=
146{
147 1 + 32 + 256 * 0x11,/*0x0622*/
148 1 + 32 + 256 * 0x13,/*0x0623*/
149 1 + 256 * 0x15,/*0x0624*/
150 1 + 32 + 256 * 0x17,/*0x0625*/
151 1 + 2 + 256 * 0x19,/*0x0626*/
152 1 + 32 + 256 * 0x1D,/*0x0627*/
153 1 + 2 + 256 * 0x1F,/*0x0628*/
154 1 + 256 * 0x23,/*0x0629*/
155 1 + 2 + 256 * 0x25,/*0x062A*/
156 1 + 2 + 256 * 0x29,/*0x062B*/
157 1 + 2 + 256 * 0x2D,/*0x062C*/
158 1 + 2 + 256 * 0x31,/*0x062D*/
159 1 + 2 + 256 * 0x35,/*0x062E*/
160 1 + 256 * 0x39,/*0x062F*/
161 1 + 256 * 0x3B,/*0x0630*/
162 1 + 256 * 0x3D,/*0x0631*/
163 1 + 256 * 0x3F,/*0x0632*/
164 1 + 2 + 256 * 0x41,/*0x0633*/
165 1 + 2 + 256 * 0x45,/*0x0634*/
166 1 + 2 + 256 * 0x49,/*0x0635*/
167 1 + 2 + 256 * 0x4D,/*0x0636*/
168 1 + 2 + 256 * 0x51,/*0x0637*/
169 1 + 2 + 256 * 0x55,/*0x0638*/
170 1 + 2 + 256 * 0x59,/*0x0639*/
171 1 + 2 + 256 * 0x5D,/*0x063A*/
172 0, 0, 0, 0, 0, /*0x063B-0x063F*/
173 1 + 2, /*0x0640*/
174 1 + 2 + 256 * 0x61,/*0x0641*/
175 1 + 2 + 256 * 0x65,/*0x0642*/
176 1 + 2 + 256 * 0x69,/*0x0643*/
177 1 + 2 + 16 + 256 * 0x6D,/*0x0644*/
178 1 + 2 + 256 * 0x71,/*0x0645*/
179 1 + 2 + 256 * 0x75,/*0x0646*/
180 1 + 2 + 256 * 0x79,/*0x0647*/
181 1 + 256 * 0x7D,/*0x0648*/
182 1 + 256 * 0x7F,/*0x0649*/
183 1 + 2 + 256 * 0x81,/*0x064A*/
184 4 + 256 * 1, /*0x064B*/
185 4 + 128 + 256 * 1, /*0x064C*/
186 4 + 128 + 256 * 1, /*0x064D*/
187 4 + 128 + 256 * 1, /*0x064E*/
188 4 + 128 + 256 * 1, /*0x064F*/
189 4 + 128 + 256 * 1, /*0x0650*/
190 4 + 64 + 256 * 3, /*0x0651*/
191 4 + 256 * 1, /*0x0652*/
192 4 + 256 * 7, /*0x0653*/
193 4 + 256 * 8, /*0x0654*/
Craig Cornelius103e9ff2012-10-09 17:03:29 -0700194 4 + 256 * 8, /*0x0655*/
Jean-Baptiste Querub13da9d2009-07-17 17:53:22 -0700195 4 + 256 * 1, /*0x0656*/
196 0, 0, 0, 0, 0, /*0x0657-0x065B*/
197 1 + 256 * 0x85,/*0x065C*/
198 1 + 256 * 0x87,/*0x065D*/
199 1 + 256 * 0x89,/*0x065E*/
200 1 + 256 * 0x8B,/*0x065F*/
201 0, 0, 0, 0, 0, /*0x0660-0x0664*/
202 0, 0, 0, 0, 0, /*0x0665-0x0669*/
203 0, 0, 0, 0, 0, 0, /*0x066A-0x066F*/
204 4 + 256 * 6, /*0x0670*/
205 1 + 8 + 256 * 0x00,/*0x0671*/
206 1 + 32, /*0x0672*/
207 1 + 32, /*0x0673*/
208 0, /*0x0674*/
209 1 + 32, /*0x0675*/
210 1, 1, /*0x0676-0x0677*/
211 1+2, 1+2, 1+2, 1+2, 1+2, 1+2, /*0x0678-0x067D*/
212 1+2+8+256 * 0x06, 1+2, 1+2, 1+2, 1+2, 1+2, /*0x067E-0x0683*/
213 1+2, 1+2, 1+2+8+256 * 0x2A, 1+2, /*0x0684-0x0687*/
214 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /*0x0688-0x0691*/
215 1, 1, 1, 1, 1, 1, 1+8+256 * 0x3A, 1, /*0x0692-0x0699*/
216 1+2, 1+2, 1+2, 1+2, 1+2, 1+2, /*0x069A-0x06A3*/
217 1+2, 1+2, 1+2, 1+2, /*0x069A-0x06A3*/
218 1+2, 1+2, 1+2, 1+2, 1+2, 1+2+8+256 * 0x3E, /*0x06A4-0x06AD*/
219 1+2, 1+2, 1+2, 1+2, /*0x06A4-0x06AD*/
220 1+2, 1+2+8+256 * 0x42, 1+2, 1+2, 1+2, 1+2, /*0x06AE-0x06B7*/
221 1+2, 1+2, 1+2, 1+2, /*0x06AE-0x06B7*/
222 1+2, 1+2, 1+2, 1+2, 1+2, 1+2, /*0x06B8-0x06BF*/
223 1+2, 1+2, /*0x06B8-0x06BF*/
224 1, /*0x06C0*/
225 1+2, /*0x06C1*/
226 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /*0x06C2-0x06CB*/
227 1+2+8+256 * 0xAC, /*0x06CC*/
228 1, /*0x06CD*/
229 1+2, 1+2, 1+2, 1+2, /*0x06CE-0x06D1*/
230 1, 1 /*0x06D2-0x06D3*/
231};
232
Jean-Baptiste Queruc69afce2009-07-20 15:02:39 -0700233static const uint8_t presALink[] = {
Jean-Baptiste Querub13da9d2009-07-17 17:53:22 -0700234/***********0*****1*****2*****3*****4*****5*****6*****7*****8*****9*****A*****B*****C*****D*****E*****F*/
235/*FB5*/ 0, 1, 0, 0, 0, 0, 0, 1, 2,1 + 2, 0, 0, 0, 0, 0, 0,
236/*FB6*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
237/*FB7*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2,1 + 2, 0, 0,
238/*FB8*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1,
239/*FB9*/ 2,1 + 2, 0, 1, 2,1 + 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
240/*FBA*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
241/*FBB*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
242/*FBC*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
243/*FBD*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
244/*FBE*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
245/*FBF*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2,1 + 2,
246/*FC0*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
247/*FC1*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
248/*FC2*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
249/*FC3*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
250/*FC4*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
251/*FC5*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 4,
252/*FC6*/ 4, 4, 4
253};
254
Jean-Baptiste Queruc69afce2009-07-20 15:02:39 -0700255static const uint8_t presBLink[]=
Jean-Baptiste Querub13da9d2009-07-17 17:53:22 -0700256{
257/***********0*****1*****2*****3*****4*****5*****6*****7*****8*****9*****A*****B*****C*****D*****E*****F*/
Craig Cornelius103e9ff2012-10-09 17:03:29 -0700258/*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,
Jean-Baptiste Querub13da9d2009-07-17 17:53:22 -0700259/*FE8*/ 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 2,1 + 2, 0, 1, 0,
260/*FE9*/ 1, 2,1 + 2, 0, 1, 0, 1, 2,1 + 2, 0, 1, 2,1 + 2, 0, 1, 2,
Craig Cornelius103e9ff2012-10-09 17:03:29 -0700261/*FEA*/1 + 2, 0, 1, 2,1 + 2, 0, 1, 2,1 + 2, 0, 1, 0, 1, 0, 1, 0,
Jean-Baptiste Querub13da9d2009-07-17 17:53:22 -0700262/*FEB*/ 1, 0, 1, 2,1 + 2, 0, 1, 2,1 + 2, 0, 1, 2,1 + 2, 0, 1, 2,
263/*FEC*/1 + 2, 0, 1, 2,1 + 2, 0, 1, 2,1 + 2, 0, 1, 2,1 + 2, 0, 1, 2,
264/*FED*/1 + 2, 0, 1, 2,1 + 2, 0, 1, 2,1 + 2, 0, 1, 2,1 + 2, 0, 1, 2,
265/*FEE*/1 + 2, 0, 1, 2,1 + 2, 0, 1, 2,1 + 2, 0, 1, 2,1 + 2, 0, 1, 0,
266/*FEF*/ 1, 0, 1, 2,1 + 2, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0
267};
268
269static const UChar convertFBto06[] =
270{
271/***********0******1******2******3******4******5******6******7******8******9******A******B******C******D******E******F***/
272/*FB5*/ 0x671, 0x671, 0, 0, 0, 0, 0x07E, 0x07E, 0x07E, 0x07E, 0, 0, 0, 0, 0, 0,
273/*FB6*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
274/*FB7*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x686, 0x686, 0x686, 0x686, 0, 0,
275/*FB8*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x698, 0x698, 0, 0, 0x6A9, 0x6A9,
276/*FB9*/ 0x6A9, 0x6A9, 0x6AF, 0x6AF, 0x6AF, 0x6AF, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
277/*FBA*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
278/*FBB*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
279/*FBC*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
280/*FBD*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
281/*FBE*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
282/*FBF*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x6CC, 0x6CC, 0x6CC, 0x6CC
283};
284
285static const UChar convertFEto06[] =
286{
287/***********0******1******2******3******4******5******6******7******8******9******A******B******C******D******E******F***/
288/*FE7*/ 0x64B, 0x64B, 0x64C, 0x64C, 0x64D, 0x64D, 0x64E, 0x64E, 0x64F, 0x64F, 0x650, 0x650, 0x651, 0x651, 0x652, 0x652,
289/*FE8*/ 0x621, 0x622, 0x622, 0x623, 0x623, 0x624, 0x624, 0x625, 0x625, 0x626, 0x626, 0x626, 0x626, 0x627, 0x627, 0x628,
290/*FE9*/ 0x628, 0x628, 0x628, 0x629, 0x629, 0x62A, 0x62A, 0x62A, 0x62A, 0x62B, 0x62B, 0x62B, 0x62B, 0x62C, 0x62C, 0x62C,
291/*FEA*/ 0x62C, 0x62D, 0x62D, 0x62D, 0x62D, 0x62E, 0x62E, 0x62E, 0x62E, 0x62F, 0x62F, 0x630, 0x630, 0x631, 0x631, 0x632,
292/*FEB*/ 0x632, 0x633, 0x633, 0x633, 0x633, 0x634, 0x634, 0x634, 0x634, 0x635, 0x635, 0x635, 0x635, 0x636, 0x636, 0x636,
293/*FEC*/ 0x636, 0x637, 0x637, 0x637, 0x637, 0x638, 0x638, 0x638, 0x638, 0x639, 0x639, 0x639, 0x639, 0x63A, 0x63A, 0x63A,
294/*FED*/ 0x63A, 0x641, 0x641, 0x641, 0x641, 0x642, 0x642, 0x642, 0x642, 0x643, 0x643, 0x643, 0x643, 0x644, 0x644, 0x644,
295/*FEE*/ 0x644, 0x645, 0x645, 0x645, 0x645, 0x646, 0x646, 0x646, 0x646, 0x647, 0x647, 0x647, 0x647, 0x648, 0x648, 0x649,
296/*FEF*/ 0x649, 0x64A, 0x64A, 0x64A, 0x64A, 0x65C, 0x65C, 0x65D, 0x65D, 0x65E, 0x65E, 0x65F, 0x65F
297};
298
Jean-Baptiste Queruc69afce2009-07-20 15:02:39 -0700299static const uint8_t shapeTable[4][4][4]=
Jean-Baptiste Querub13da9d2009-07-17 17:53:22 -0700300{
301 { {0,0,0,0}, {0,0,0,0}, {0,1,0,3}, {0,1,0,1} },
302 { {0,0,2,2}, {0,0,1,2}, {0,1,1,2}, {0,1,1,3} },
303 { {0,0,0,0}, {0,0,0,0}, {0,1,0,3}, {0,1,0,3} },
304 { {0,0,1,2}, {0,0,1,2}, {0,1,1,2}, {0,1,1,3} }
305};
306
307/*
308 * This function shapes European digits to Arabic-Indic digits
309 * in-place, writing over the input characters.
310 * Since we know that we are only looking for BMP code points,
311 * we can safely just work with code units (again, at least UTF-16).
312 */
313static void
314_shapeToArabicDigitsWithContext(UChar *s, int32_t length,
315 UChar digitBase,
316 UBool isLogical, UBool lastStrongWasAL) {
317 const UBiDiProps *bdp;
Jean-Baptiste Querub13da9d2009-07-17 17:53:22 -0700318 int32_t i;
319 UChar c;
320
claireho27f65472011-06-09 11:11:49 -0700321 bdp=ubidi_getSingleton();
Jean-Baptiste Querub13da9d2009-07-17 17:53:22 -0700322 digitBase-=0x30;
323
324 /* the iteration direction depends on the type of input */
325 if(isLogical) {
326 for(i=0; i<length; ++i) {
327 c=s[i];
328 switch(ubidi_getClass(bdp, c)) {
329 case U_LEFT_TO_RIGHT: /* L */
330 case U_RIGHT_TO_LEFT: /* R */
331 lastStrongWasAL=FALSE;
332 break;
333 case U_RIGHT_TO_LEFT_ARABIC: /* AL */
334 lastStrongWasAL=TRUE;
335 break;
336 case U_EUROPEAN_NUMBER: /* EN */
337 if(lastStrongWasAL && (uint32_t)(c-0x30)<10) {
338 s[i]=(UChar)(digitBase+c); /* digitBase+(c-0x30) - digitBase was modified above */
339 }
340 break;
341 default :
342 break;
343 }
344 }
345 } else {
346 for(i=length; i>0; /* pre-decrement in the body */) {
347 c=s[--i];
348 switch(ubidi_getClass(bdp, c)) {
349 case U_LEFT_TO_RIGHT: /* L */
350 case U_RIGHT_TO_LEFT: /* R */
351 lastStrongWasAL=FALSE;
352 break;
353 case U_RIGHT_TO_LEFT_ARABIC: /* AL */
354 lastStrongWasAL=TRUE;
355 break;
356 case U_EUROPEAN_NUMBER: /* EN */
357 if(lastStrongWasAL && (uint32_t)(c-0x30)<10) {
358 s[i]=(UChar)(digitBase+c); /* digitBase+(c-0x30) - digitBase was modified above */
359 }
360 break;
361 default :
362 break;
363 }
364 }
365 }
366}
367
368/*
369 *Name : invertBuffer
370 *Function : This function inverts the buffer, it's used
371 * in case the user specifies the buffer to be
372 * U_SHAPE_TEXT_DIRECTION_LOGICAL
373 */
claireho27f65472011-06-09 11:11:49 -0700374static void
Craig Cornelius103e9ff2012-10-09 17:03:29 -0700375invertBuffer(UChar *buffer, int32_t size, uint32_t /*options*/, int32_t lowlimit, int32_t highlimit) {
Jean-Baptiste Querub13da9d2009-07-17 17:53:22 -0700376 UChar temp;
377 int32_t i=0,j=0;
378 for(i=lowlimit,j=size-highlimit-1;i<j;i++,j--) {
379 temp = buffer[i];
380 buffer[i] = buffer[j];
381 buffer[j] = temp;
382 }
383}
384
385/*
386 *Name : changeLamAlef
387 *Function : Converts the Alef characters into an equivalent
388 * LamAlef location in the 0x06xx Range, this is an
389 * intermediate stage in the operation of the program
390 * later it'll be converted into the 0xFExx LamAlefs
391 * in the shaping function.
392 */
Craig Cornelius103e9ff2012-10-09 17:03:29 -0700393static inline UChar
Jean-Baptiste Querub13da9d2009-07-17 17:53:22 -0700394changeLamAlef(UChar ch) {
395 switch(ch) {
396 case 0x0622 :
397 return 0x065C;
398 case 0x0623 :
399 return 0x065D;
400 case 0x0625 :
401 return 0x065E;
402 case 0x0627 :
403 return 0x065F;
404 }
405 return 0;
406}
407
408/*
409 *Name : getLink
410 *Function : Resolves the link between the characters as
411 * Arabic characters have four forms :
412 * Isolated, Initial, Middle and Final Form
413 */
414static UChar
415getLink(UChar ch) {
416 if(ch >= 0x0622 && ch <= 0x06D3) {
417 return(araLink[ch-0x0622]);
418 } else if(ch == 0x200D) {
419 return(3);
420 } else if(ch >= 0x206D && ch <= 0x206F) {
421 return(4);
Jean-Baptiste Querub0ac9372009-07-20 15:09:32 -0700422 }else if(ch >= 0xFB50 && ch <= 0xFC62) {
Jean-Baptiste Querub13da9d2009-07-17 17:53:22 -0700423 return(presALink[ch-0xFB50]);
424 } else if(ch >= 0xFE70 && ch <= 0xFEFC) {
425 return(presBLink[ch-0xFE70]);
Jean-Baptiste Querub0ac9372009-07-20 15:09:32 -0700426 }else {
Jean-Baptiste Querub13da9d2009-07-17 17:53:22 -0700427 return(0);
428 }
429}
430
431/*
432 *Name : countSpaces
433 *Function : Counts the number of spaces
434 * at each end of the logical buffer
435 */
436static void
Craig Cornelius103e9ff2012-10-09 17:03:29 -0700437countSpaces(UChar *dest, int32_t size, uint32_t /*options*/, int32_t *spacesCountl, int32_t *spacesCountr) {
Jean-Baptiste Querub13da9d2009-07-17 17:53:22 -0700438 int32_t i = 0;
439 int32_t countl = 0,countr = 0;
Craig Cornelius103e9ff2012-10-09 17:03:29 -0700440 while((dest[i] == SPACE_CHAR) && (countl < size)) {
Jean-Baptiste Querub13da9d2009-07-17 17:53:22 -0700441 countl++;
442 i++;
443 }
Craig Cornelius103e9ff2012-10-09 17:03:29 -0700444 if (countl < size) { /* the entire buffer is not all space */
445 while(dest[size-1] == SPACE_CHAR) {
446 countr++;
447 size--;
448 }
Jean-Baptiste Querub13da9d2009-07-17 17:53:22 -0700449 }
450 *spacesCountl = countl;
451 *spacesCountr = countr;
452}
453
454/*
455 *Name : isTashkeelChar
Jean-Baptiste Querub0ac9372009-07-20 15:09:32 -0700456 *Function : Returns 1 for Tashkeel characters in 06 range else return 0
Jean-Baptiste Querub13da9d2009-07-17 17:53:22 -0700457 */
Craig Cornelius103e9ff2012-10-09 17:03:29 -0700458static inline int32_t
Jean-Baptiste Querub13da9d2009-07-17 17:53:22 -0700459isTashkeelChar(UChar ch) {
460 return (int32_t)( ch>=0x064B && ch<= 0x0652 );
461}
462
463/*
Jean-Baptiste Querub0ac9372009-07-20 15:09:32 -0700464 *Name : isTashkeelCharFE
465 *Function : Returns 1 for Tashkeel characters in FE range else return 0
466 */
Craig Cornelius103e9ff2012-10-09 17:03:29 -0700467static inline int32_t
Jean-Baptiste Querub0ac9372009-07-20 15:09:32 -0700468isTashkeelCharFE(UChar ch) {
469 return (int32_t)( ch>=0xFE70 && ch<= 0xFE7F );
470}
471
472/*
Jean-Baptiste Querub13da9d2009-07-17 17:53:22 -0700473 *Name : isAlefChar
474 *Function : Returns 1 for Alef characters else return 0
475 */
Craig Cornelius103e9ff2012-10-09 17:03:29 -0700476static inline int32_t
Jean-Baptiste Querub13da9d2009-07-17 17:53:22 -0700477isAlefChar(UChar ch) {
478 return (int32_t)( (ch==0x0622)||(ch==0x0623)||(ch==0x0625)||(ch==0x0627) );
479}
480
481/*
482 *Name : isLamAlefChar
483 *Function : Returns 1 for LamAlef characters else return 0
484 */
Craig Cornelius103e9ff2012-10-09 17:03:29 -0700485static inline int32_t
Jean-Baptiste Querub13da9d2009-07-17 17:53:22 -0700486isLamAlefChar(UChar ch) {
Jean-Baptiste Querub0ac9372009-07-20 15:09:32 -0700487 return (int32_t)((ch>=0xFEF5)&&(ch<=0xFEFC) );
488}
489
490/*BIDI
491 *Name : isTailChar
Craig Cornelius103e9ff2012-10-09 17:03:29 -0700492 *Function : returns 1 if the character matches one of the tail characters (0xfe73 or 0x200b) otherwise returns 0
Jean-Baptiste Querub0ac9372009-07-20 15:09:32 -0700493 */
494
Craig Cornelius103e9ff2012-10-09 17:03:29 -0700495static inline int32_t
Jean-Baptiste Querub0ac9372009-07-20 15:09:32 -0700496isTailChar(UChar ch) {
497 if(ch == OLD_TAIL_CHAR || ch == NEW_TAIL_CHAR){
498 return 1;
499 }else{
500 return 0;
501 }
502}
503
504/*BIDI
505 *Name : isSeenTailFamilyChar
Craig Cornelius103e9ff2012-10-09 17:03:29 -0700506 *Function : returns 1 if the character is a seen family isolated character
Jean-Baptiste Querub0ac9372009-07-20 15:09:32 -0700507 * in the FE range otherwise returns 0
508 */
509
Craig Cornelius103e9ff2012-10-09 17:03:29 -0700510static inline int32_t
Jean-Baptiste Querub0ac9372009-07-20 15:09:32 -0700511isSeenTailFamilyChar(UChar ch) {
512 if(ch >= 0xfeb1 && ch < 0xfebf){
513 return tailFamilyIsolatedFinal [ch - 0xFEB1];
514 }else{
515 return 0;
516 }
517}
518
519 /* Name : isSeenFamilyChar
520 * Function : returns 1 if the character is a seen family character in the Unicode
521 * 06 range otherwise returns 0
522 */
523
Craig Cornelius103e9ff2012-10-09 17:03:29 -0700524static inline int32_t
Jean-Baptiste Querub0ac9372009-07-20 15:09:32 -0700525isSeenFamilyChar(UChar ch){
526 if(ch >= 0x633 && ch <= 0x636){
527 return 1;
528 }else {
529 return 0;
530 }
531}
532
533/*Start of BIDI*/
534/*
535 *Name : isAlefMaksouraChar
Craig Cornelius103e9ff2012-10-09 17:03:29 -0700536 *Function : returns 1 if the character is a Alef Maksoura Final or isolated
537 * otherwise returns 0
Jean-Baptiste Querub0ac9372009-07-20 15:09:32 -0700538 */
Craig Cornelius103e9ff2012-10-09 17:03:29 -0700539static inline int32_t
Jean-Baptiste Querub0ac9372009-07-20 15:09:32 -0700540isAlefMaksouraChar(UChar ch) {
541 return (int32_t)( (ch == 0xFEEF) || ( ch == 0xFEF0) || (ch == 0x0649));
Craig Cornelius103e9ff2012-10-09 17:03:29 -0700542}
Jean-Baptiste Querub0ac9372009-07-20 15:09:32 -0700543
544/*
545 * Name : isYehHamzaChar
546 * Function : returns 1 if the character is a yehHamza isolated or yehhamza
Craig Cornelius103e9ff2012-10-09 17:03:29 -0700547 * final is found otherwise returns 0
Jean-Baptiste Querub0ac9372009-07-20 15:09:32 -0700548 */
Craig Cornelius103e9ff2012-10-09 17:03:29 -0700549static inline int32_t
Jean-Baptiste Querub0ac9372009-07-20 15:09:32 -0700550isYehHamzaChar(UChar ch) {
551 if((ch==0xFE89)||(ch==0xFE8A)){
552 return 1;
553 }else{
554 return 0;
555 }
Craig Cornelius103e9ff2012-10-09 17:03:29 -0700556}
Jean-Baptiste Querub0ac9372009-07-20 15:09:32 -0700557
Craig Cornelius103e9ff2012-10-09 17:03:29 -0700558 /*
Jean-Baptiste Querub0ac9372009-07-20 15:09:32 -0700559 * Name: isTashkeelOnTatweelChar
Craig Cornelius103e9ff2012-10-09 17:03:29 -0700560 * Function: Checks if the Tashkeel Character is on Tatweel or not,if the
561 * Tashkeel on tatweel (FE range), it returns 1 else if the
562 * Tashkeel with shadda on tatweel (FC range)return 2 otherwise
Jean-Baptiste Querub0ac9372009-07-20 15:09:32 -0700563 * returns 0
564 */
Craig Cornelius103e9ff2012-10-09 17:03:29 -0700565static inline int32_t
Jean-Baptiste Querub0ac9372009-07-20 15:09:32 -0700566isTashkeelOnTatweelChar(UChar ch){
567 if(ch >= 0xfe70 && ch <= 0xfe7f && ch != NEW_TAIL_CHAR && ch != 0xFE75 && ch != SHADDA_TATWEEL_CHAR)
568 {
569 return tashkeelMedial [ch - 0xFE70];
570 }else if( (ch >= 0xfcf2 && ch <= 0xfcf4) || (ch == SHADDA_TATWEEL_CHAR)) {
571 return 2;
572 }else{
573 return 0;
574 }
Jean-Baptiste Querub13da9d2009-07-17 17:53:22 -0700575}
576
577/*
Jean-Baptiste Querub0ac9372009-07-20 15:09:32 -0700578 * Name: isIsolatedTashkeelChar
Craig Cornelius103e9ff2012-10-09 17:03:29 -0700579 * Function: Checks if the Tashkeel Character is in the isolated form
580 * (i.e. Unicode FE range) returns 1 else if the Tashkeel
Jean-Baptiste Querub0ac9372009-07-20 15:09:32 -0700581 * with shadda is in the isolated form (i.e. Unicode FC range)
582 * returns 2 otherwise returns 0
Jean-Baptiste Querub13da9d2009-07-17 17:53:22 -0700583 */
Craig Cornelius103e9ff2012-10-09 17:03:29 -0700584static inline int32_t
Jean-Baptiste Querub0ac9372009-07-20 15:09:32 -0700585isIsolatedTashkeelChar(UChar ch){
586 if(ch >= 0xfe70 && ch <= 0xfe7f && ch != NEW_TAIL_CHAR && ch != 0xFE75){
587 return (1 - tashkeelMedial [ch - 0xFE70]);
588 }else if(ch >= 0xfc5e && ch <= 0xfc63){
589 return 1;
590 }else{
591 return 0;
592 }
593}
594
595
596
597
598/*
599 *Name : calculateSize
Craig Cornelius103e9ff2012-10-09 17:03:29 -0700600 *Function : This function calculates the destSize to be used in preflighting
Jean-Baptiste Querub0ac9372009-07-20 15:09:32 -0700601 * when the destSize is equal to 0
Craig Cornelius103e9ff2012-10-09 17:03:29 -0700602 * It is used also to calculate the new destsize in case the
Jean-Baptiste Querub0ac9372009-07-20 15:09:32 -0700603 * destination buffer will be resized.
604 */
605
Jean-Baptiste Querub13da9d2009-07-17 17:53:22 -0700606static int32_t
607calculateSize(const UChar *source, int32_t sourceLength,
Craig Cornelius103e9ff2012-10-09 17:03:29 -0700608int32_t destSize,uint32_t options) {
Jean-Baptiste Querub13da9d2009-07-17 17:53:22 -0700609 int32_t i = 0;
Craig Cornelius103e9ff2012-10-09 17:03:29 -0700610
Jean-Baptiste Querub0ac9372009-07-20 15:09:32 -0700611 int lamAlefOption = 0;
612 int tashkeelOption = 0;
613
Jean-Baptiste Querub13da9d2009-07-17 17:53:22 -0700614 destSize = sourceLength;
Craig Cornelius103e9ff2012-10-09 17:03:29 -0700615
Jean-Baptiste Querub0ac9372009-07-20 15:09:32 -0700616 if (((options&U_SHAPE_LETTERS_MASK) == U_SHAPE_LETTERS_SHAPE ||
617 ((options&U_SHAPE_LETTERS_MASK) == U_SHAPE_LETTERS_SHAPE_TASHKEEL_ISOLATED )) &&
618 ((options&U_SHAPE_LAMALEF_MASK) == U_SHAPE_LAMALEF_RESIZE )){
619 lamAlefOption = 1;
620 }
621 if((options&U_SHAPE_LETTERS_MASK) == U_SHAPE_LETTERS_SHAPE &&
622 ((options&U_SHAPE_TASHKEEL_MASK) == U_SHAPE_TASHKEEL_RESIZE ) ){
623 tashkeelOption = 1;
624 }
Craig Cornelius103e9ff2012-10-09 17:03:29 -0700625
Jean-Baptiste Querub0ac9372009-07-20 15:09:32 -0700626 if(lamAlefOption || tashkeelOption){
Jean-Baptiste Querub13da9d2009-07-17 17:53:22 -0700627 if((options&U_SHAPE_TEXT_DIRECTION_MASK)==U_SHAPE_TEXT_DIRECTION_VISUAL_LTR) {
628 for(i=0;i<sourceLength;i++) {
Jean-Baptiste Querub0ac9372009-07-20 15:09:32 -0700629 if( ((isAlefChar(source[i]))&& (i<(sourceLength-1)) &&(source[i+1] == LAM_CHAR)) || (isTashkeelCharFE(source[i])) ) {
630 destSize--;
631 }
Jean-Baptiste Querub13da9d2009-07-17 17:53:22 -0700632 }
Jean-Baptiste Querub0ac9372009-07-20 15:09:32 -0700633 }else if((options&U_SHAPE_TEXT_DIRECTION_MASK)==U_SHAPE_TEXT_DIRECTION_LOGICAL) {
634 for(i=0;i<sourceLength;i++) {
635 if( ( (source[i] == LAM_CHAR) && (i<(sourceLength-1)) && (isAlefChar(source[i+1]))) || (isTashkeelCharFE(source[i])) ) {
636 destSize--;
637 }
Jean-Baptiste Querub13da9d2009-07-17 17:53:22 -0700638 }
639 }
640 }
Craig Cornelius103e9ff2012-10-09 17:03:29 -0700641
Jean-Baptiste Querub0ac9372009-07-20 15:09:32 -0700642 if ((options&U_SHAPE_LETTERS_MASK) == U_SHAPE_LETTERS_UNSHAPE){
643 if ( (options&U_SHAPE_LAMALEF_MASK) == U_SHAPE_LAMALEF_RESIZE){
644 for(i=0;i<sourceLength;i++) {
645 if(isLamAlefChar(source[i]))
Jean-Baptiste Querub13da9d2009-07-17 17:53:22 -0700646 destSize++;
647 }
648 }
Jean-Baptiste Querub13da9d2009-07-17 17:53:22 -0700649 }
650
651 return destSize;
652}
653
654/*
Jean-Baptiste Querub0ac9372009-07-20 15:09:32 -0700655 *Name : handleTashkeelWithTatweel
Craig Cornelius103e9ff2012-10-09 17:03:29 -0700656 *Function : Replaces Tashkeel as following:
657 * Case 1 :if the Tashkeel on tatweel, replace it with Tatweel.
658 * Case 2 :if the Tashkeel aggregated with Shadda on Tatweel, replace
Jean-Baptiste Querub0ac9372009-07-20 15:09:32 -0700659 * it with Shadda on Tatweel.
660 * Case 3: if the Tashkeel is isolated replace it with Space.
661 *
Jean-Baptiste Querub13da9d2009-07-17 17:53:22 -0700662 */
663static int32_t
Jean-Baptiste Querub0ac9372009-07-20 15:09:32 -0700664handleTashkeelWithTatweel(UChar *dest, int32_t sourceLength,
Craig Cornelius103e9ff2012-10-09 17:03:29 -0700665 int32_t /*destSize*/, uint32_t /*options*/,
666 UErrorCode * /*pErrorCode*/) {
Jean-Baptiste Querub0ac9372009-07-20 15:09:32 -0700667 int i;
668 for(i = 0; i < sourceLength; i++){
669 if((isTashkeelOnTatweelChar(dest[i]) == 1)){
670 dest[i] = TATWEEL_CHAR;
671 }else if((isTashkeelOnTatweelChar(dest[i]) == 2)){
672 dest[i] = SHADDA_TATWEEL_CHAR;
673 }else if(isIsolatedTashkeelChar(dest[i]) && dest[i] != SHADDA_CHAR){
674 dest[i] = SPACE_CHAR;
675 }
676 }
677 return sourceLength;
678}
679
680
681
682/*
683 *Name : handleGeneratedSpaces
684 *Function : The shapeUnicode function converts Lam + Alef into LamAlef + space,
Craig Cornelius103e9ff2012-10-09 17:03:29 -0700685 * and Tashkeel to space.
686 * handleGeneratedSpaces function puts these generated spaces
Jean-Baptiste Querub0ac9372009-07-20 15:09:32 -0700687 * according to the options the user specifies. LamAlef and Tashkeel
Craig Cornelius103e9ff2012-10-09 17:03:29 -0700688 * spaces can be replaced at begin, at end, at near or decrease the
Jean-Baptiste Querub0ac9372009-07-20 15:09:32 -0700689 * buffer size.
690 *
691 * There is also Auto option for LamAlef and tashkeel, which will put
Craig Cornelius103e9ff2012-10-09 17:03:29 -0700692 * the spaces at end of the buffer (or end of text if the user used
Jean-Baptiste Querub0ac9372009-07-20 15:09:32 -0700693 * the option U_SHAPE_SPACES_RELATIVE_TO_TEXT_BEGIN_END).
694 *
Craig Cornelius103e9ff2012-10-09 17:03:29 -0700695 * If the text type was visual_LTR and the option
696 * U_SHAPE_SPACES_RELATIVE_TO_TEXT_BEGIN_END was selected the END
Jean-Baptiste Querub0ac9372009-07-20 15:09:32 -0700697 * option will place the space at the beginning of the buffer and
Craig Cornelius103e9ff2012-10-09 17:03:29 -0700698 * BEGIN will place the space at the end of the buffer.
Jean-Baptiste Querub0ac9372009-07-20 15:09:32 -0700699 */
700
701static int32_t
702handleGeneratedSpaces(UChar *dest, int32_t sourceLength,
Jean-Baptiste Querub13da9d2009-07-17 17:53:22 -0700703 int32_t destSize,
Craig Cornelius103e9ff2012-10-09 17:03:29 -0700704 /* BEGIN android-changed */
Doug Felt2bae19d2010-06-07 16:28:19 -0700705 uint64_t options,
Craig Cornelius103e9ff2012-10-09 17:03:29 -0700706 /* END android-changed */
Jean-Baptiste Querub0ac9372009-07-20 15:09:32 -0700707 UErrorCode *pErrorCode ) {
Jean-Baptiste Querub13da9d2009-07-17 17:53:22 -0700708
709 int32_t i = 0, j = 0;
710 int32_t count = 0;
711 UChar *tempbuffer=NULL;
712
Jean-Baptiste Querub0ac9372009-07-20 15:09:32 -0700713 int lamAlefOption = 0;
714 int tashkeelOption = 0;
715 int shapingMode = SHAPE_MODE;
716
717 if (shapingMode == 0){
718 if ( (options&U_SHAPE_LAMALEF_MASK) == U_SHAPE_LAMALEF_RESIZE ){
719 lamAlefOption = 1;
720 }
721 if ( (options&U_SHAPE_TASHKEEL_MASK) == U_SHAPE_TASHKEEL_RESIZE ){
722 tashkeelOption = 1;
723 }
724 }
725
726 if (lamAlefOption || tashkeelOption){
Jean-Baptiste Querub13da9d2009-07-17 17:53:22 -0700727 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 }
733
734 uprv_memset(tempbuffer, 0, (sourceLength+1)*U_SIZEOF_UCHAR);
735
Jean-Baptiste Querub0ac9372009-07-20 15:09:32 -0700736 i = j = 0; count = 0;
Jean-Baptiste Querub13da9d2009-07-17 17:53:22 -0700737 while(i < sourceLength) {
Jean-Baptiste Querub0ac9372009-07-20 15:09:32 -0700738 if ( (lamAlefOption && dest[i] == LAMALEF_SPACE_SUB) ||
Craig Cornelius103e9ff2012-10-09 17:03:29 -0700739 (tashkeelOption && dest[i] == TASHKEEL_SPACE_SUB) ){
Jean-Baptiste Querub13da9d2009-07-17 17:53:22 -0700740 j--;
741 count++;
Jean-Baptiste Querub0ac9372009-07-20 15:09:32 -0700742 } else {
Jean-Baptiste Querub13da9d2009-07-17 17:53:22 -0700743 tempbuffer[j] = dest[i];
Jean-Baptiste Querub0ac9372009-07-20 15:09:32 -0700744 }
Jean-Baptiste Querub13da9d2009-07-17 17:53:22 -0700745 i++;
746 j++;
747 }
748
749 while(count >= 0) {
750 tempbuffer[i] = 0x0000;
751 i--;
752 count--;
753 }
754
755 uprv_memcpy(dest, tempbuffer, sourceLength*U_SIZEOF_UCHAR);
756 destSize = u_strlen(dest);
Jean-Baptiste Querub0ac9372009-07-20 15:09:32 -0700757 }
Jean-Baptiste Querub13da9d2009-07-17 17:53:22 -0700758
Jean-Baptiste Querub0ac9372009-07-20 15:09:32 -0700759 lamAlefOption = 0;
760
761 if (shapingMode == 0){
claireho27f65472011-06-09 11:11:49 -0700762 /* BEGIN android-changed */
Doug Felte0266fb2010-06-08 14:11:11 -0700763 if ( (options&U_SHAPE_LAMALEF_MASK) == U_SHAPE_LAMALEF_NEAR &&
claireho27f65472011-06-09 11:11:49 -0700764 (options&U_SHAPE_X_LAMALEF_SUB_ALTERNATE) == 0) { /* if set, leave LAMALEF_SPACE_SUB in the output */
765 /* END android-changed */
Jean-Baptiste Querub0ac9372009-07-20 15:09:32 -0700766 lamAlefOption = 1;
767 }
768 }
Craig Cornelius103e9ff2012-10-09 17:03:29 -0700769
Jean-Baptiste Querub0ac9372009-07-20 15:09:32 -0700770 if (lamAlefOption){
Jean-Baptiste Querub13da9d2009-07-17 17:53:22 -0700771 /* Lam+Alef is already shaped into LamAlef + FFFF */
772 i = 0;
773 while(i < sourceLength) {
Jean-Baptiste Querub0ac9372009-07-20 15:09:32 -0700774 if(lamAlefOption&&dest[i] == LAMALEF_SPACE_SUB){
775 dest[i] = SPACE_CHAR;
776 }
Jean-Baptiste Querub13da9d2009-07-17 17:53:22 -0700777 i++;
778 }
779 destSize = sourceLength;
Jean-Baptiste Querub0ac9372009-07-20 15:09:32 -0700780 }
781 lamAlefOption = 0;
782 tashkeelOption = 0;
Jean-Baptiste Querub13da9d2009-07-17 17:53:22 -0700783
Jean-Baptiste Querub0ac9372009-07-20 15:09:32 -0700784 if (shapingMode == 0) {
Craig Cornelius103e9ff2012-10-09 17:03:29 -0700785 if ( ((options&U_SHAPE_LAMALEF_MASK) == uShapeLamalefBegin) ||
Jean-Baptiste Querub0ac9372009-07-20 15:09:32 -0700786 (((options&U_SHAPE_LAMALEF_MASK) == U_SHAPE_LAMALEF_AUTO )
787 && (spacesRelativeToTextBeginEnd==1)) ) {
788 lamAlefOption = 1;
789 }
790 if ( (options&U_SHAPE_TASHKEEL_MASK) == uShapeTashkeelBegin ) {
791 tashkeelOption = 1;
792 }
793 }
794
795 if(lamAlefOption || tashkeelOption){
Jean-Baptiste Querub13da9d2009-07-17 17:53:22 -0700796 tempbuffer = (UChar *)uprv_malloc((sourceLength+1)*U_SIZEOF_UCHAR);
Craig Cornelius103e9ff2012-10-09 17:03:29 -0700797
Jean-Baptiste Querub13da9d2009-07-17 17:53:22 -0700798 /* Test for NULL */
799 if(tempbuffer == NULL) {
800 *pErrorCode = U_MEMORY_ALLOCATION_ERROR;
801 return 0;
802 }
803
804 uprv_memset(tempbuffer, 0, (sourceLength+1)*U_SIZEOF_UCHAR);
Jean-Baptiste Querub0ac9372009-07-20 15:09:32 -0700805 i = j = sourceLength; count = 0;
Jean-Baptiste Querub13da9d2009-07-17 17:53:22 -0700806 while(i >= 0) {
Jean-Baptiste Querub0ac9372009-07-20 15:09:32 -0700807 if ( (lamAlefOption && dest[i] == LAMALEF_SPACE_SUB) ||
808 (tashkeelOption && dest[i] == TASHKEEL_SPACE_SUB) ){
Jean-Baptiste Querub13da9d2009-07-17 17:53:22 -0700809 j++;
810 count++;
Jean-Baptiste Querub0ac9372009-07-20 15:09:32 -0700811 }else {
Jean-Baptiste Querub13da9d2009-07-17 17:53:22 -0700812 tempbuffer[j] = dest[i];
Jean-Baptiste Querub0ac9372009-07-20 15:09:32 -0700813 }
Jean-Baptiste Querub13da9d2009-07-17 17:53:22 -0700814 i--;
815 j--;
816 }
Craig Cornelius103e9ff2012-10-09 17:03:29 -0700817
Jean-Baptiste Querub0ac9372009-07-20 15:09:32 -0700818 for(i=0 ;i < count; i++){
819 tempbuffer[i] = SPACE_CHAR;
820 }
Craig Cornelius103e9ff2012-10-09 17:03:29 -0700821
Jean-Baptiste Querub13da9d2009-07-17 17:53:22 -0700822 uprv_memcpy(dest, tempbuffer, sourceLength*U_SIZEOF_UCHAR);
823 destSize = sourceLength;
Jean-Baptiste Querub0ac9372009-07-20 15:09:32 -0700824 }
Jean-Baptiste Querub13da9d2009-07-17 17:53:22 -0700825
claireho27f65472011-06-09 11:11:49 -0700826
827
Jean-Baptiste Querub0ac9372009-07-20 15:09:32 -0700828 lamAlefOption = 0;
829 tashkeelOption = 0;
830
831 if (shapingMode == 0) {
Craig Cornelius103e9ff2012-10-09 17:03:29 -0700832 if ( ((options&U_SHAPE_LAMALEF_MASK) == uShapeLamalefEnd) ||
Jean-Baptiste Querub0ac9372009-07-20 15:09:32 -0700833 (((options&U_SHAPE_LAMALEF_MASK) == U_SHAPE_LAMALEF_AUTO )
834 && (spacesRelativeToTextBeginEnd==0)) ) {
835 lamAlefOption = 1;
836 }
837 if ( (options&U_SHAPE_TASHKEEL_MASK) == uShapeTashkeelEnd ){
838 tashkeelOption = 1;
839 }
840 }
Craig Cornelius103e9ff2012-10-09 17:03:29 -0700841
Jean-Baptiste Querub0ac9372009-07-20 15:09:32 -0700842 if(lamAlefOption || tashkeelOption){
Jean-Baptiste Querub13da9d2009-07-17 17:53:22 -0700843 tempbuffer = (UChar *)uprv_malloc((sourceLength+1)*U_SIZEOF_UCHAR);
Jean-Baptiste Querub0ac9372009-07-20 15:09:32 -0700844 /* Test for NULL */
Jean-Baptiste Querub13da9d2009-07-17 17:53:22 -0700845 if(tempbuffer == NULL) {
846 *pErrorCode = U_MEMORY_ALLOCATION_ERROR;
847 return 0;
848 }
849
850 uprv_memset(tempbuffer, 0, (sourceLength+1)*U_SIZEOF_UCHAR);
851
Jean-Baptiste Querub0ac9372009-07-20 15:09:32 -0700852 i = j = 0; count = 0;
Jean-Baptiste Querub13da9d2009-07-17 17:53:22 -0700853 while(i < sourceLength) {
Jean-Baptiste Querub0ac9372009-07-20 15:09:32 -0700854 if ( (lamAlefOption && dest[i] == LAMALEF_SPACE_SUB) ||
855 (tashkeelOption && dest[i] == TASHKEEL_SPACE_SUB) ){
Jean-Baptiste Querub13da9d2009-07-17 17:53:22 -0700856 j--;
857 count++;
Jean-Baptiste Querub0ac9372009-07-20 15:09:32 -0700858 }else {
Jean-Baptiste Querub13da9d2009-07-17 17:53:22 -0700859 tempbuffer[j] = dest[i];
Jean-Baptiste Querub0ac9372009-07-20 15:09:32 -0700860 }
Jean-Baptiste Querub13da9d2009-07-17 17:53:22 -0700861 i++;
862 j++;
863 }
864
865 while(count >= 0) {
Jean-Baptiste Querub0ac9372009-07-20 15:09:32 -0700866 tempbuffer[i] = SPACE_CHAR;
Jean-Baptiste Querub13da9d2009-07-17 17:53:22 -0700867 i--;
868 count--;
869 }
870
871 uprv_memcpy(dest,tempbuffer, sourceLength*U_SIZEOF_UCHAR);
872 destSize = sourceLength;
Jean-Baptiste Querub13da9d2009-07-17 17:53:22 -0700873 }
874
Craig Cornelius103e9ff2012-10-09 17:03:29 -0700875
Jean-Baptiste Querub0ac9372009-07-20 15:09:32 -0700876 if(tempbuffer){
Jean-Baptiste Querub13da9d2009-07-17 17:53:22 -0700877 uprv_free(tempbuffer);
Jean-Baptiste Querub0ac9372009-07-20 15:09:32 -0700878 }
Jean-Baptiste Querub13da9d2009-07-17 17:53:22 -0700879
880 return destSize;
881}
882
883/*
Jean-Baptiste Querub0ac9372009-07-20 15:09:32 -0700884 *Name :expandCompositCharAtBegin
885 *Function :Expands the LamAlef character to Lam and Alef consuming the required
Craig Cornelius103e9ff2012-10-09 17:03:29 -0700886 * space from beginning of the buffer. If the text type was visual_LTR
Jean-Baptiste Querub0ac9372009-07-20 15:09:32 -0700887 * and the option U_SHAPE_SPACES_RELATIVE_TO_TEXT_BEGIN_END was selected
888 * the spaces will be located at end of buffer.
889 * If there are no spaces to expand the LamAlef, an error
Craig Cornelius103e9ff2012-10-09 17:03:29 -0700890 * will be set to U_NO_SPACE_AVAILABLE as defined in utypes.h
Jean-Baptiste Querub13da9d2009-07-17 17:53:22 -0700891 */
Jean-Baptiste Querub13da9d2009-07-17 17:53:22 -0700892
Jean-Baptiste Querub0ac9372009-07-20 15:09:32 -0700893static int32_t
894expandCompositCharAtBegin(UChar *dest, int32_t sourceLength, int32_t destSize,UErrorCode *pErrorCode) {
Jean-Baptiste Querub13da9d2009-07-17 17:53:22 -0700895 int32_t i = 0,j = 0;
896 int32_t countl = 0;
Jean-Baptiste Querub13da9d2009-07-17 17:53:22 -0700897 UChar *tempbuffer=NULL;
Craig Cornelius103e9ff2012-10-09 17:03:29 -0700898
Jean-Baptiste Querub0ac9372009-07-20 15:09:32 -0700899 tempbuffer = (UChar *)uprv_malloc((sourceLength+1)*U_SIZEOF_UCHAR);
Jean-Baptiste Querub13da9d2009-07-17 17:53:22 -0700900
Jean-Baptiste Querub0ac9372009-07-20 15:09:32 -0700901 /* Test for NULL */
902 if(tempbuffer == NULL) {
903 *pErrorCode = U_MEMORY_ALLOCATION_ERROR;
904 return 0;
905 }
Jean-Baptiste Querub13da9d2009-07-17 17:53:22 -0700906
907 uprv_memset(tempbuffer, 0, (sourceLength+1)*U_SIZEOF_UCHAR);
908
909 i = 0;
Jean-Baptiste Querub0ac9372009-07-20 15:09:32 -0700910 while(dest[i] == SPACE_CHAR) {
Jean-Baptiste Querub13da9d2009-07-17 17:53:22 -0700911 countl++;
912 i++;
913 }
914
915 i = j = sourceLength-1;
Craig Cornelius103e9ff2012-10-09 17:03:29 -0700916
Jean-Baptiste Querub13da9d2009-07-17 17:53:22 -0700917 while(i >= 0 && j >= 0) {
Jean-Baptiste Querub0ac9372009-07-20 15:09:32 -0700918 if( countl>0 && isLamAlefChar(dest[i])) {
919 tempbuffer[j] = LAM_CHAR;
Craig Cornelius103e9ff2012-10-09 17:03:29 -0700920 /* to ensure the array index is within the range */
921 U_ASSERT(dest[i] >= 0xFEF5u
922 && dest[i]-0xFEF5u < sizeof(convertLamAlef)/sizeof(convertLamAlef[0]));
Jean-Baptiste Querub0ac9372009-07-20 15:09:32 -0700923 tempbuffer[j-1] = convertLamAlef[ dest[i] - 0xFEF5 ];
924 j--;
925 countl--;
926 }else {
927 if( countl == 0 && isLamAlefChar(dest[i]) ) {
Jean-Baptiste Querub13da9d2009-07-17 17:53:22 -0700928 *pErrorCode=U_NO_SPACE_AVAILABLE;
Jean-Baptiste Querub0ac9372009-07-20 15:09:32 -0700929 }
Jean-Baptiste Querub13da9d2009-07-17 17:53:22 -0700930 tempbuffer[j] = dest[i];
Jean-Baptiste Querub0ac9372009-07-20 15:09:32 -0700931 }
932 i--;
933 j--;
Jean-Baptiste Querub13da9d2009-07-17 17:53:22 -0700934 }
Jean-Baptiste Querub13da9d2009-07-17 17:53:22 -0700935 uprv_memcpy(dest, tempbuffer, sourceLength*U_SIZEOF_UCHAR);
Craig Cornelius103e9ff2012-10-09 17:03:29 -0700936
Jean-Baptiste Querub0ac9372009-07-20 15:09:32 -0700937 uprv_free(tempbuffer);
Craig Cornelius103e9ff2012-10-09 17:03:29 -0700938
Jean-Baptiste Querub13da9d2009-07-17 17:53:22 -0700939 destSize = sourceLength;
Jean-Baptiste Querub0ac9372009-07-20 15:09:32 -0700940 return destSize;
941}
Jean-Baptiste Querub13da9d2009-07-17 17:53:22 -0700942
Jean-Baptiste Querub0ac9372009-07-20 15:09:32 -0700943/*
944 *Name : expandCompositCharAtEnd
Craig Cornelius103e9ff2012-10-09 17:03:29 -0700945 *Function : Expands the LamAlef character to Lam and Alef consuming the
Jean-Baptiste Querub0ac9372009-07-20 15:09:32 -0700946 * required space from end of the buffer. If the text type was
947 * Visual LTR and the option U_SHAPE_SPACES_RELATIVE_TO_TEXT_BEGIN_END
Craig Cornelius103e9ff2012-10-09 17:03:29 -0700948 * was used, the spaces will be consumed from begin of buffer. If
Jean-Baptiste Querub0ac9372009-07-20 15:09:32 -0700949 * there are no spaces to expand the LamAlef, an error
Craig Cornelius103e9ff2012-10-09 17:03:29 -0700950 * will be set to U_NO_SPACE_AVAILABLE as defined in utypes.h
Jean-Baptiste Querub0ac9372009-07-20 15:09:32 -0700951 */
Jean-Baptiste Querub13da9d2009-07-17 17:53:22 -0700952
Jean-Baptiste Querub0ac9372009-07-20 15:09:32 -0700953static int32_t
954expandCompositCharAtEnd(UChar *dest, int32_t sourceLength, int32_t destSize,UErrorCode *pErrorCode) {
955 int32_t i = 0,j = 0;
956
957 int32_t countr = 0;
958 int32_t inpsize = sourceLength;
959
960 UChar *tempbuffer=NULL;
961 tempbuffer = (UChar *)uprv_malloc((sourceLength+1)*U_SIZEOF_UCHAR);
962
963 /* Test for NULL */
964 if(tempbuffer == NULL) {
965 *pErrorCode = U_MEMORY_ALLOCATION_ERROR;
966 return 0;
967 }
Craig Cornelius103e9ff2012-10-09 17:03:29 -0700968
Jean-Baptiste Querub0ac9372009-07-20 15:09:32 -0700969 uprv_memset(tempbuffer, 0, (sourceLength+1)*U_SIZEOF_UCHAR);
Craig Cornelius103e9ff2012-10-09 17:03:29 -0700970
Jean-Baptiste Querub0ac9372009-07-20 15:09:32 -0700971 while(dest[inpsize-1] == SPACE_CHAR) {
972 countr++;
973 inpsize--;
974 }
Craig Cornelius103e9ff2012-10-09 17:03:29 -0700975
Jean-Baptiste Querub0ac9372009-07-20 15:09:32 -0700976 i = sourceLength - countr - 1;
977 j = sourceLength - 1;
Craig Cornelius103e9ff2012-10-09 17:03:29 -0700978
Jean-Baptiste Querub0ac9372009-07-20 15:09:32 -0700979 while(i >= 0 && j >= 0) {
980 if( countr>0 && isLamAlefChar(dest[i]) ) {
981 tempbuffer[j] = LAM_CHAR;
982 tempbuffer[j-1] = convertLamAlef[ dest[i] - 0xFEF5 ];
983 j--;
984 countr--;
985 }else {
986 if ((countr == 0) && isLamAlefChar(dest[i]) ) {
987 *pErrorCode=U_NO_SPACE_AVAILABLE;
988 }
989 tempbuffer[j] = dest[i];
990 }
991 i--;
992 j--;
993 }
Craig Cornelius103e9ff2012-10-09 17:03:29 -0700994
Jean-Baptiste Querub0ac9372009-07-20 15:09:32 -0700995 if(countr > 0) {
996 uprv_memmove(tempbuffer, tempbuffer+countr, sourceLength*U_SIZEOF_UCHAR);
997 if(u_strlen(tempbuffer) < sourceLength) {
998 for(i=sourceLength-1;i>=sourceLength-countr;i--) {
999 tempbuffer[i] = SPACE_CHAR;
1000 }
1001 }
1002 }
1003 uprv_memcpy(dest, tempbuffer, sourceLength*U_SIZEOF_UCHAR);
1004
1005 uprv_free(tempbuffer);
Craig Cornelius103e9ff2012-10-09 17:03:29 -07001006
Jean-Baptiste Querub0ac9372009-07-20 15:09:32 -07001007 destSize = sourceLength;
1008 return destSize;
1009}
1010
1011/*
1012 *Name : expandCompositCharAtNear
1013 *Function : Expands the LamAlef character into Lam + Alef, YehHamza character
Craig Cornelius103e9ff2012-10-09 17:03:29 -07001014 * into Yeh + Hamza, SeenFamily character into SeenFamily character
1015 * + Tail, while consuming the space next to the character.
Jean-Baptiste Querub0ac9372009-07-20 15:09:32 -07001016 * If there are no spaces next to the character, an error
Craig Cornelius103e9ff2012-10-09 17:03:29 -07001017 * will be set to U_NO_SPACE_AVAILABLE as defined in utypes.h
Jean-Baptiste Querub0ac9372009-07-20 15:09:32 -07001018 */
1019
1020static int32_t
1021expandCompositCharAtNear(UChar *dest, int32_t sourceLength, int32_t destSize,UErrorCode *pErrorCode,
1022 int yehHamzaOption, int seenTailOption, int lamAlefOption) {
1023 int32_t i = 0;
1024
1025
1026 UChar lamalefChar, yehhamzaChar;
1027
1028 for(i = 0 ;i<=sourceLength-1;i++) {
1029 if (seenTailOption && isSeenTailFamilyChar(dest[i])) {
1030 if ((i>0) && (dest[i-1] == SPACE_CHAR) ) {
1031 dest[i-1] = tailChar;
1032 }else {
1033 *pErrorCode=U_NO_SPACE_AVAILABLE;
1034 }
1035 }else if(yehHamzaOption && (isYehHamzaChar(dest[i])) ) {
1036 if ((i>0) && (dest[i-1] == SPACE_CHAR) ) {
1037 yehhamzaChar = dest[i];
1038 dest[i] = yehHamzaToYeh[yehhamzaChar - YEH_HAMZAFE_CHAR];
1039 dest[i-1] = HAMZAFE_CHAR;
1040 }else {
Craig Cornelius103e9ff2012-10-09 17:03:29 -07001041
Jean-Baptiste Querub0ac9372009-07-20 15:09:32 -07001042 *pErrorCode=U_NO_SPACE_AVAILABLE;
1043 }
1044 }else if(lamAlefOption && isLamAlefChar(dest[i+1])) {
1045 if(dest[i] == SPACE_CHAR){
1046 lamalefChar = dest[i+1];
1047 dest[i+1] = LAM_CHAR;
1048 dest[i] = convertLamAlef[ lamalefChar - 0xFEF5 ];
1049 }else {
1050 *pErrorCode=U_NO_SPACE_AVAILABLE;
1051 }
1052 }
1053 }
1054 destSize = sourceLength;
1055 return destSize;
1056}
Craig Cornelius103e9ff2012-10-09 17:03:29 -07001057 /*
Jean-Baptiste Querub0ac9372009-07-20 15:09:32 -07001058 * Name : expandCompositChar
Craig Cornelius103e9ff2012-10-09 17:03:29 -07001059 * Function : LamAlef, need special handling, since it expands from one
1060 * character into two characters while shaping or deshaping.
Jean-Baptiste Querub0ac9372009-07-20 15:09:32 -07001061 * In order to expand it, near or far spaces according to the
1062 * options user specifies. Also buffer size can be increased.
1063 *
1064 * For SeenFamily characters and YehHamza only the near option is
1065 * supported, while for LamAlef we can take spaces from begin, end,
1066 * near or even increase the buffer size.
Craig Cornelius103e9ff2012-10-09 17:03:29 -07001067 * There is also the Auto option for LamAlef only, which will first
Jean-Baptiste Querub0ac9372009-07-20 15:09:32 -07001068 * search for a space at end, begin then near, respectively.
Craig Cornelius103e9ff2012-10-09 17:03:29 -07001069 * If there are no spaces to expand these characters, an error will be set to
1070 * U_NO_SPACE_AVAILABLE as defined in utypes.h
Jean-Baptiste Querub0ac9372009-07-20 15:09:32 -07001071 */
Craig Cornelius103e9ff2012-10-09 17:03:29 -07001072
Jean-Baptiste Querub0ac9372009-07-20 15:09:32 -07001073static int32_t
1074expandCompositChar(UChar *dest, int32_t sourceLength,
Craig Cornelius103e9ff2012-10-09 17:03:29 -07001075 int32_t destSize,uint32_t options,
Jean-Baptiste Querub0ac9372009-07-20 15:09:32 -07001076 UErrorCode *pErrorCode, int shapingMode) {
1077
1078 int32_t i = 0,j = 0;
1079
1080 UChar *tempbuffer=NULL;
1081 int yehHamzaOption = 0;
1082 int seenTailOption = 0;
1083 int lamAlefOption = 0;
Craig Cornelius103e9ff2012-10-09 17:03:29 -07001084
Jean-Baptiste Querub0ac9372009-07-20 15:09:32 -07001085 if (shapingMode == 1){
1086 if ( (options&U_SHAPE_LAMALEF_MASK) == U_SHAPE_LAMALEF_AUTO){
Craig Cornelius103e9ff2012-10-09 17:03:29 -07001087
Jean-Baptiste Querub0ac9372009-07-20 15:09:32 -07001088 if(spacesRelativeToTextBeginEnd == 0) {
1089 destSize = expandCompositCharAtEnd(dest, sourceLength, destSize, pErrorCode);
Craig Cornelius103e9ff2012-10-09 17:03:29 -07001090
Jean-Baptiste Querub0ac9372009-07-20 15:09:32 -07001091 if(*pErrorCode == U_NO_SPACE_AVAILABLE) {
1092 *pErrorCode = U_ZERO_ERROR;
1093 destSize = expandCompositCharAtBegin(dest, sourceLength, destSize, pErrorCode);
1094 }
1095 }else {
1096 destSize = expandCompositCharAtBegin(dest, sourceLength, destSize, pErrorCode);
Craig Cornelius103e9ff2012-10-09 17:03:29 -07001097
Jean-Baptiste Querub0ac9372009-07-20 15:09:32 -07001098 if(*pErrorCode == U_NO_SPACE_AVAILABLE) {
1099 *pErrorCode = U_ZERO_ERROR;
1100 destSize = expandCompositCharAtEnd(dest, sourceLength, destSize, pErrorCode);
1101 }
1102 }
Craig Cornelius103e9ff2012-10-09 17:03:29 -07001103
Jean-Baptiste Querub0ac9372009-07-20 15:09:32 -07001104 if(*pErrorCode == U_NO_SPACE_AVAILABLE) {
1105 *pErrorCode = U_ZERO_ERROR;
Craig Cornelius103e9ff2012-10-09 17:03:29 -07001106 destSize = expandCompositCharAtNear(dest, sourceLength, destSize, pErrorCode, yehHamzaOption,
Jean-Baptiste Querub0ac9372009-07-20 15:09:32 -07001107 seenTailOption, 1);
1108 }
1109 }
1110 }
1111
1112 if (shapingMode == 1){
1113 if ( (options&U_SHAPE_LAMALEF_MASK) == uShapeLamalefEnd){
1114 destSize = expandCompositCharAtEnd(dest, sourceLength, destSize, pErrorCode);
1115 }
1116 }
1117
1118 if (shapingMode == 1){
1119 if ( (options&U_SHAPE_LAMALEF_MASK) == uShapeLamalefBegin){
1120 destSize = expandCompositCharAtBegin(dest, sourceLength, destSize, pErrorCode);
1121 }
1122 }
Craig Cornelius103e9ff2012-10-09 17:03:29 -07001123
Jean-Baptiste Querub0ac9372009-07-20 15:09:32 -07001124 if (shapingMode == 0){
1125 if ((options&U_SHAPE_YEHHAMZA_MASK) == U_SHAPE_YEHHAMZA_TWOCELL_NEAR){
1126 yehHamzaOption = 1;
1127 }
1128 if ((options&U_SHAPE_SEEN_MASK) == U_SHAPE_SEEN_TWOCELL_NEAR){
1129 seenTailOption = 1;
1130 }
1131 }
1132 if (shapingMode == 1) {
1133 if ( (options&U_SHAPE_LAMALEF_MASK) == U_SHAPE_LAMALEF_NEAR) {
1134 lamAlefOption = 1;
1135 }
1136 }
1137
1138
1139 if (yehHamzaOption || seenTailOption || lamAlefOption){
Craig Cornelius103e9ff2012-10-09 17:03:29 -07001140 destSize = expandCompositCharAtNear(dest, sourceLength, destSize, pErrorCode, yehHamzaOption,
1141 seenTailOption,lamAlefOption);
Jean-Baptiste Querub0ac9372009-07-20 15:09:32 -07001142 }
Craig Cornelius103e9ff2012-10-09 17:03:29 -07001143
1144
Jean-Baptiste Querub0ac9372009-07-20 15:09:32 -07001145 if (shapingMode == 1){
1146 if ( (options&U_SHAPE_LAMALEF_MASK) == U_SHAPE_LAMALEF_RESIZE){
1147 destSize = calculateSize(dest,sourceLength,destSize,options);
1148 tempbuffer = (UChar *)uprv_malloc((destSize+1)*U_SIZEOF_UCHAR);
Craig Cornelius103e9ff2012-10-09 17:03:29 -07001149
Jean-Baptiste Querub13da9d2009-07-17 17:53:22 -07001150 /* Test for NULL */
1151 if(tempbuffer == NULL) {
1152 *pErrorCode = U_MEMORY_ALLOCATION_ERROR;
1153 return 0;
1154 }
Craig Cornelius103e9ff2012-10-09 17:03:29 -07001155
Jean-Baptiste Querub0ac9372009-07-20 15:09:32 -07001156 uprv_memset(tempbuffer, 0, (destSize+1)*U_SIZEOF_UCHAR);
Craig Cornelius103e9ff2012-10-09 17:03:29 -07001157
Jean-Baptiste Querub0ac9372009-07-20 15:09:32 -07001158 i = j = 0;
1159 while(i < destSize && j < destSize) {
1160 if(isLamAlefChar(dest[i]) ) {
1161 tempbuffer[j] = convertLamAlef[ dest[i] - 0xFEF5 ];
1162 tempbuffer[j+1] = LAM_CHAR;
1163 j++;
1164 }else {
Jean-Baptiste Querub13da9d2009-07-17 17:53:22 -07001165 tempbuffer[j] = dest[i];
1166 }
Jean-Baptiste Querub0ac9372009-07-20 15:09:32 -07001167 i++;
1168 j++;
Jean-Baptiste Querub13da9d2009-07-17 17:53:22 -07001169 }
Craig Cornelius103e9ff2012-10-09 17:03:29 -07001170
Jean-Baptiste Querub0ac9372009-07-20 15:09:32 -07001171 uprv_memcpy(dest, tempbuffer, destSize*U_SIZEOF_UCHAR);
Jean-Baptiste Querub0ac9372009-07-20 15:09:32 -07001172 }
Jean-Baptiste Querub13da9d2009-07-17 17:53:22 -07001173 }
Craig Cornelius103e9ff2012-10-09 17:03:29 -07001174
Jean-Baptiste Querub0ac9372009-07-20 15:09:32 -07001175 if(tempbuffer) {
Jean-Baptiste Querub13da9d2009-07-17 17:53:22 -07001176 uprv_free(tempbuffer);
Jean-Baptiste Querub0ac9372009-07-20 15:09:32 -07001177 }
Jean-Baptiste Querub13da9d2009-07-17 17:53:22 -07001178 return destSize;
1179}
1180
1181/*
1182 *Name : shapeUnicode
1183 *Function : Converts an Arabic Unicode buffer in 06xx Range into a shaped
1184 * arabic Unicode buffer in FExx Range
1185 */
1186static int32_t
1187shapeUnicode(UChar *dest, int32_t sourceLength,
Craig Cornelius103e9ff2012-10-09 17:03:29 -07001188 int32_t destSize,uint32_t options,
Jean-Baptiste Querub13da9d2009-07-17 17:53:22 -07001189 UErrorCode *pErrorCode,
1190 int tashkeelFlag) {
1191
1192 int32_t i, iend;
1193 int32_t step;
1194 int32_t lastPos,Nx, Nw;
1195 unsigned int Shape;
1196 int32_t lamalef_found = 0;
Jean-Baptiste Querub0ac9372009-07-20 15:09:32 -07001197 int32_t seenfamFound = 0, yehhamzaFound =0, tashkeelFound = 0;
Jean-Baptiste Querub13da9d2009-07-17 17:53:22 -07001198 UChar prevLink = 0, lastLink = 0, currLink, nextLink = 0;
1199 UChar wLamalef;
1200
1201 /*
1202 * Converts the input buffer from FExx Range into 06xx Range
1203 * to make sure that all characters are in the 06xx range
1204 * even the lamalef is converted to the special region in
1205 * the 06xx range
1206 */
1207 if ((options & U_SHAPE_PRESERVE_PRESENTATION_MASK) == U_SHAPE_PRESERVE_PRESENTATION_NOOP) {
1208 for (i = 0; i < sourceLength; i++) {
1209 UChar inputChar = dest[i];
1210 if ( (inputChar >= 0xFB50) && (inputChar <= 0xFBFF)) {
1211 UChar c = convertFBto06 [ (inputChar - 0xFB50) ];
1212 if (c != 0)
1213 dest[i] = c;
1214 } else if ( (inputChar >= 0xFE70) && (inputChar <= 0xFEFC)) {
1215 dest[i] = convertFEto06 [ (inputChar - 0xFE70) ] ;
1216 } else {
1217 dest[i] = inputChar ;
1218 }
1219 }
1220 }
1221
Jean-Baptiste Querub0ac9372009-07-20 15:09:32 -07001222
Jean-Baptiste Querub13da9d2009-07-17 17:53:22 -07001223 /* sets the index to the end of the buffer, together with the step point to -1 */
1224 i = sourceLength - 1;
1225 iend = -1;
1226 step = -1;
1227
1228 /*
1229 * This function resolves the link between the characters .
1230 * Arabic characters have four forms :
1231 * Isolated Form, Initial Form, Middle Form and Final Form
1232 */
1233 currLink = getLink(dest[i]);
1234
1235 lastPos = i;
1236 Nx = -2, Nw = 0;
1237
1238 while (i != iend) {
1239 /* If high byte of currLink > 0 then more than one shape */
1240 if ((currLink & 0xFF00) > 0 || (getLink(dest[i]) & IRRELEVANT) != 0) {
1241 Nw = i + step;
1242 while (Nx < 0) { /* we need to know about next char */
1243 if(Nw == iend) {
1244 nextLink = 0;
1245 Nx = 3000;
1246 } else {
1247 nextLink = getLink(dest[Nw]);
1248 if((nextLink & IRRELEVANT) == 0) {
1249 Nx = Nw;
1250 } else {
1251 Nw = Nw + step;
1252 }
1253 }
1254 }
1255
1256 if ( ((currLink & ALEFTYPE) > 0) && ((lastLink & LAMTYPE) > 0) ) {
1257 lamalef_found = 1;
1258 wLamalef = changeLamAlef(dest[i]); /*get from 0x065C-0x065f */
1259 if ( wLamalef != 0) {
Jean-Baptiste Querub0ac9372009-07-20 15:09:32 -07001260 dest[i] = LAMALEF_SPACE_SUB; /* The default case is to drop the Alef and replace */
1261 dest[lastPos] =wLamalef; /* it by LAMALEF_SPACE_SUB which is the last character in the */
Jean-Baptiste Querub13da9d2009-07-17 17:53:22 -07001262 i=lastPos; /* unicode private use area, this is done to make */
1263 } /* sure that removeLamAlefSpaces() handles only the */
1264 lastLink = prevLink; /* spaces generated during lamalef generation. */
Jean-Baptiste Querub0ac9372009-07-20 15:09:32 -07001265 currLink = getLink(wLamalef); /* LAMALEF_SPACE_SUB is added here and is replaced by spaces */
Jean-Baptiste Querub13da9d2009-07-17 17:53:22 -07001266 } /* in removeLamAlefSpaces() */
Craig Cornelius103e9ff2012-10-09 17:03:29 -07001267
Jean-Baptiste Querub0ac9372009-07-20 15:09:32 -07001268 if ((i > 0) && (dest[i-1] == SPACE_CHAR)){
1269 if ( isSeenFamilyChar(dest[i])){
1270 seenfamFound = 1;
1271 } else if (dest[i] == YEH_HAMZA_CHAR) {
1272 yehhamzaFound = 1;
1273 }
1274 }
1275 else if(i==0){
1276 if ( isSeenFamilyChar(dest[i])){
1277 seenfamFound = 1;
1278 } else if (dest[i] == YEH_HAMZA_CHAR) {
1279 yehhamzaFound = 1;
1280 }
1281 }
1282
Craig Cornelius103e9ff2012-10-09 17:03:29 -07001283 /*
Jean-Baptiste Querub13da9d2009-07-17 17:53:22 -07001284 * get the proper shape according to link ability of neighbors
1285 * and of character; depends on the order of the shapes
1286 * (isolated, initial, middle, final) in the compatibility area
1287 */
1288 Shape = shapeTable[nextLink & (LINKR + LINKL)]
1289 [lastLink & (LINKR + LINKL)]
Craig Cornelius103e9ff2012-10-09 17:03:29 -07001290 [currLink & (LINKR + LINKL)];
Jean-Baptiste Querub13da9d2009-07-17 17:53:22 -07001291
1292 if ((currLink & (LINKR+LINKL)) == 1) {
1293 Shape &= 1;
1294 } else if(isTashkeelChar(dest[i])) {
1295 if( (lastLink & LINKL) && (nextLink & LINKR) && (tashkeelFlag == 1) &&
1296 dest[i] != 0x064C && dest[i] != 0x064D )
1297 {
Craig Cornelius103e9ff2012-10-09 17:03:29 -07001298 Shape = 1;
Jean-Baptiste Querub13da9d2009-07-17 17:53:22 -07001299 if( (nextLink&ALEFTYPE) == ALEFTYPE && (lastLink&LAMTYPE) == LAMTYPE ) {
Craig Cornelius103e9ff2012-10-09 17:03:29 -07001300 Shape = 0;
Jean-Baptiste Querub13da9d2009-07-17 17:53:22 -07001301 }
1302 }
1303 else {
1304 Shape = 0;
1305 }
1306 }
1307 if ((dest[i] ^ 0x0600) < 0x100) {
Jean-Baptiste Querub0ac9372009-07-20 15:09:32 -07001308 if ( isTashkeelChar(dest[i]) ){
1309 if (tashkeelFlag == 2){
1310 dest[i] = TASHKEEL_SPACE_SUB;
1311 tashkeelFound = 1;
Craig Cornelius103e9ff2012-10-09 17:03:29 -07001312 } else {
1313 /* to ensure the array index is within the range */
1314 U_ASSERT(dest[i] >= 0x064Bu
1315 && dest[i]-0x064Bu < sizeof(IrrelevantPos)/sizeof(IrrelevantPos[0]));
1316 dest[i] = 0xFE70 + IrrelevantPos[(dest[i] - 0x064B)] + Shape;
Jean-Baptiste Querub0ac9372009-07-20 15:09:32 -07001317 }
1318 }else if ((currLink & APRESENT) > 0) {
Jean-Baptiste Querub13da9d2009-07-17 17:53:22 -07001319 dest[i] = (UChar)(0xFB50 + (currLink >> 8) + Shape);
Jean-Baptiste Querub0ac9372009-07-20 15:09:32 -07001320 }else if ((currLink >> 8) > 0 && (currLink & IRRELEVANT) == 0) {
Jean-Baptiste Querub13da9d2009-07-17 17:53:22 -07001321 dest[i] = (UChar)(0xFE70 + (currLink >> 8) + Shape);
Jean-Baptiste Querub0ac9372009-07-20 15:09:32 -07001322 }
Jean-Baptiste Querub13da9d2009-07-17 17:53:22 -07001323 }
1324 }
1325
1326 /* move one notch forward */
1327 if ((currLink & IRRELEVANT) == 0) {
1328 prevLink = lastLink;
1329 lastLink = currLink;
1330 lastPos = i;
1331 }
1332
1333 i = i + step;
1334 if (i == Nx) {
1335 currLink = nextLink;
1336 Nx = -2;
1337 } else if(i != iend) {
1338 currLink = getLink(dest[i]);
1339 }
1340 }
Jean-Baptiste Querub0ac9372009-07-20 15:09:32 -07001341 destSize = sourceLength;
1342 if ( (lamalef_found != 0 ) || (tashkeelFound != 0) ){
Doug Felt2bae19d2010-06-07 16:28:19 -07001343 destSize = handleGeneratedSpaces(dest,sourceLength,destSize,options,pErrorCode);
Jean-Baptiste Querub0ac9372009-07-20 15:09:32 -07001344 }
Jean-Baptiste Querub13da9d2009-07-17 17:53:22 -07001345
Jean-Baptiste Querub0ac9372009-07-20 15:09:32 -07001346 if ( (seenfamFound != 0) || (yehhamzaFound != 0) ) {
1347 destSize = expandCompositChar(dest, sourceLength,destSize,options,pErrorCode, SHAPE_MODE);
1348 }
Jean-Baptiste Querub13da9d2009-07-17 17:53:22 -07001349 return destSize;
1350}
1351
1352/*
1353 *Name : deShapeUnicode
1354 *Function : Converts an Arabic Unicode buffer in FExx Range into unshaped
1355 * arabic Unicode buffer in 06xx Range
1356 */
1357static int32_t
1358deShapeUnicode(UChar *dest, int32_t sourceLength,
Craig Cornelius103e9ff2012-10-09 17:03:29 -07001359 int32_t destSize,uint32_t options,
Jean-Baptiste Querub13da9d2009-07-17 17:53:22 -07001360 UErrorCode *pErrorCode) {
1361 int32_t i = 0;
1362 int32_t lamalef_found = 0;
Jean-Baptiste Querub0ac9372009-07-20 15:09:32 -07001363 int32_t yehHamzaComposeEnabled = 0;
1364 int32_t seenComposeEnabled = 0;
1365
1366 yehHamzaComposeEnabled = ((options&U_SHAPE_YEHHAMZA_MASK) == U_SHAPE_YEHHAMZA_TWOCELL_NEAR) ? 1 : 0;
1367 seenComposeEnabled = ((options&U_SHAPE_SEEN_MASK) == U_SHAPE_SEEN_TWOCELL_NEAR)? 1 : 0;
Jean-Baptiste Querub13da9d2009-07-17 17:53:22 -07001368
1369 /*
1370 *This for loop changes the buffer from the Unicode FE range to
1371 *the Unicode 06 range
1372 */
Craig Cornelius103e9ff2012-10-09 17:03:29 -07001373
Jean-Baptiste Querub13da9d2009-07-17 17:53:22 -07001374 for(i = 0; i < sourceLength; i++) {
1375 UChar inputChar = dest[i];
1376 if ( (inputChar >= 0xFB50) && (inputChar <= 0xFBFF)) { /* FBxx Arabic range */
1377 UChar c = convertFBto06 [ (inputChar - 0xFB50) ];
1378 if (c != 0)
1379 dest[i] = c;
Craig Cornelius103e9ff2012-10-09 17:03:29 -07001380 } else if( (yehHamzaComposeEnabled == 1) && ((inputChar == HAMZA06_CHAR) || (inputChar == HAMZAFE_CHAR))
Jean-Baptiste Querub0ac9372009-07-20 15:09:32 -07001381 && (i < (sourceLength - 1)) && isAlefMaksouraChar(dest[i+1] )) {
1382 dest[i] = SPACE_CHAR;
1383 dest[i+1] = YEH_HAMZA_CHAR;
Craig Cornelius103e9ff2012-10-09 17:03:29 -07001384 } else if ( (seenComposeEnabled == 1) && (isTailChar(inputChar)) && (i< (sourceLength - 1))
Jean-Baptiste Querub0ac9372009-07-20 15:09:32 -07001385 && (isSeenTailFamilyChar(dest[i+1])) ) {
1386 dest[i] = SPACE_CHAR;
Jean-Baptiste Querub13da9d2009-07-17 17:53:22 -07001387 } else if (( inputChar >= 0xFE70) && (inputChar <= 0xFEF4 )) { /* FExx Arabic range */
Jean-Baptiste Querub0ac9372009-07-20 15:09:32 -07001388 dest[i] = convertFEto06 [ (inputChar - 0xFE70) ];
Jean-Baptiste Querub13da9d2009-07-17 17:53:22 -07001389 } else {
1390 dest[i] = inputChar ;
1391 }
Jean-Baptiste Querub0ac9372009-07-20 15:09:32 -07001392
Jean-Baptiste Querub13da9d2009-07-17 17:53:22 -07001393 if( isLamAlefChar(dest[i]) )
1394 lamalef_found = 1;
1395 }
Craig Cornelius103e9ff2012-10-09 17:03:29 -07001396
Jean-Baptiste Querub0ac9372009-07-20 15:09:32 -07001397 destSize = sourceLength;
1398 if (lamalef_found != 0){
1399 destSize = expandCompositChar(dest,sourceLength,destSize,options,pErrorCode,DESHAPE_MODE);
1400 }
1401 return destSize;
Jean-Baptiste Querub13da9d2009-07-17 17:53:22 -07001402}
1403
Craig Cornelius103e9ff2012-10-09 17:03:29 -07001404/*
Jean-Baptiste Querub0ac9372009-07-20 15:09:32 -07001405 ****************************************
1406 * u_shapeArabic
1407 ****************************************
Craig Cornelius103e9ff2012-10-09 17:03:29 -07001408 */
Jean-Baptiste Querub0ac9372009-07-20 15:09:32 -07001409
claireho27f65472011-06-09 11:11:49 -07001410/* BEGIN android-changed */
Jean-Baptiste Querub13da9d2009-07-17 17:53:22 -07001411U_CAPI int32_t U_EXPORT2
1412u_shapeArabic(const UChar *source, int32_t sourceLength,
1413 UChar *dest, int32_t destCapacity,
claireho27f65472011-06-09 11:11:49 -07001414 uint64_t options,
1415 UErrorCode *pErrorCode) {
1416/* END android-changed */
Jean-Baptiste Querub13da9d2009-07-17 17:53:22 -07001417
1418 int32_t destLength;
Craig Cornelius103e9ff2012-10-09 17:03:29 -07001419
Jean-Baptiste Querub0ac9372009-07-20 15:09:32 -07001420 spacesRelativeToTextBeginEnd = 0;
1421 uShapeLamalefBegin = U_SHAPE_LAMALEF_BEGIN;
1422 uShapeLamalefEnd = U_SHAPE_LAMALEF_END;
1423 uShapeTashkeelBegin = U_SHAPE_TASHKEEL_BEGIN;
1424 uShapeTashkeelEnd = U_SHAPE_TASHKEEL_END;
Jean-Baptiste Querub13da9d2009-07-17 17:53:22 -07001425
1426 /* usual error checking */
1427 if(pErrorCode==NULL || U_FAILURE(*pErrorCode)) {
1428 return 0;
1429 }
1430
1431 /* make sure that no reserved options values are used; allow dest==NULL only for preflighting */
1432 if( source==NULL || sourceLength<-1 || (dest==NULL && destCapacity!=0) || destCapacity<0 ||
Jean-Baptiste Querub0ac9372009-07-20 15:09:32 -07001433 (((options&U_SHAPE_TASHKEEL_MASK) > 0) &&
1434 ((options&U_SHAPE_LETTERS_SHAPE_TASHKEEL_ISOLATED) == U_SHAPE_LETTERS_SHAPE_TASHKEEL_ISOLATED) ) ||
1435 (((options&U_SHAPE_TASHKEEL_MASK) > 0) &&
1436 ((options&U_SHAPE_LETTERS_MASK) == U_SHAPE_LETTERS_UNSHAPE)) ||
Jean-Baptiste Querub13da9d2009-07-17 17:53:22 -07001437 (options&U_SHAPE_DIGIT_TYPE_RESERVED)==U_SHAPE_DIGIT_TYPE_RESERVED ||
1438 (options&U_SHAPE_DIGITS_MASK)==U_SHAPE_DIGITS_RESERVED ||
Jean-Baptiste Querub0ac9372009-07-20 15:09:32 -07001439 ((options&U_SHAPE_LAMALEF_MASK) != U_SHAPE_LAMALEF_RESIZE &&
Jean-Baptiste Querub13da9d2009-07-17 17:53:22 -07001440 (options&U_SHAPE_AGGREGATE_TASHKEEL_MASK) != 0) ||
1441 ((options&U_SHAPE_AGGREGATE_TASHKEEL_MASK) == U_SHAPE_AGGREGATE_TASHKEEL &&
1442 (options&U_SHAPE_LETTERS_SHAPE_TASHKEEL_ISOLATED) != U_SHAPE_LETTERS_SHAPE_TASHKEEL_ISOLATED)
Craig Cornelius103e9ff2012-10-09 17:03:29 -07001443 )
Jean-Baptiste Querub0ac9372009-07-20 15:09:32 -07001444 {
Jean-Baptiste Querub13da9d2009-07-17 17:53:22 -07001445 *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR;
1446 return 0;
1447 }
Jean-Baptiste Querub0ac9372009-07-20 15:09:32 -07001448 /* Validate lamalef options */
1449 if(((options&U_SHAPE_LAMALEF_MASK) > 0)&&
1450 !(((options & U_SHAPE_LAMALEF_MASK)==U_SHAPE_LAMALEF_BEGIN) ||
1451 ((options & U_SHAPE_LAMALEF_MASK)==U_SHAPE_LAMALEF_END ) ||
1452 ((options & U_SHAPE_LAMALEF_MASK)==U_SHAPE_LAMALEF_RESIZE )||
1453 ((options & U_SHAPE_LAMALEF_MASK)==U_SHAPE_LAMALEF_AUTO) ||
1454 ((options & U_SHAPE_LAMALEF_MASK)==U_SHAPE_LAMALEF_NEAR)))
1455 {
1456 *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR;
1457 return 0;
1458 }
1459 /* Validate Tashkeel options */
1460 if(((options&U_SHAPE_TASHKEEL_MASK) > 0)&&
1461 !(((options & U_SHAPE_TASHKEEL_MASK)==U_SHAPE_TASHKEEL_BEGIN) ||
1462 ((options & U_SHAPE_TASHKEEL_MASK)==U_SHAPE_TASHKEEL_END )
1463 ||((options & U_SHAPE_TASHKEEL_MASK)==U_SHAPE_TASHKEEL_RESIZE )||
1464 ((options & U_SHAPE_TASHKEEL_MASK)==U_SHAPE_TASHKEEL_REPLACE_BY_TATWEEL)))
1465 {
1466 *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR;
1467 return 0;
1468 }
Jean-Baptiste Querub13da9d2009-07-17 17:53:22 -07001469 /* determine the source length */
1470 if(sourceLength==-1) {
1471 sourceLength=u_strlen(source);
1472 }
1473 if(sourceLength<=0) {
1474 return u_terminateUChars(dest, destCapacity, 0, pErrorCode);
1475 }
1476
1477 /* check that source and destination do not overlap */
1478 if( dest!=NULL &&
1479 ((source<=dest && dest<source+sourceLength) ||
1480 (dest<=source && source<dest+destCapacity))) {
1481 *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR;
1482 return 0;
1483 }
Craig Cornelius103e9ff2012-10-09 17:03:29 -07001484
Jean-Baptiste Querub0ac9372009-07-20 15:09:32 -07001485 /* Does Options contain the new Seen Tail Unicode code point option */
clairehob26ce3a2012-01-10 17:54:41 -08001486 if ( (options&U_SHAPE_TAIL_TYPE_MASK) == U_SHAPE_TAIL_NEW_UNICODE){
Jean-Baptiste Querub0ac9372009-07-20 15:09:32 -07001487 tailChar = NEW_TAIL_CHAR;
1488 }else {
1489 tailChar = OLD_TAIL_CHAR;
1490 }
Jean-Baptiste Querub13da9d2009-07-17 17:53:22 -07001491
1492 if((options&U_SHAPE_LETTERS_MASK)!=U_SHAPE_LETTERS_NOOP) {
1493 UChar buffer[300];
1494 UChar *tempbuffer, *tempsource = NULL;
1495 int32_t outputSize, spacesCountl=0, spacesCountr=0;
1496
1497 if((options&U_SHAPE_AGGREGATE_TASHKEEL_MASK)>0) {
1498 int32_t logical_order = (options&U_SHAPE_TEXT_DIRECTION_MASK) == U_SHAPE_TEXT_DIRECTION_LOGICAL;
Craig Cornelius103e9ff2012-10-09 17:03:29 -07001499 int32_t aggregate_tashkeel =
1500 (options&(U_SHAPE_AGGREGATE_TASHKEEL_MASK+U_SHAPE_LETTERS_SHAPE_TASHKEEL_ISOLATED)) ==
Jean-Baptiste Querub13da9d2009-07-17 17:53:22 -07001501 (U_SHAPE_AGGREGATE_TASHKEEL+U_SHAPE_LETTERS_SHAPE_TASHKEEL_ISOLATED);
1502 int step=logical_order?1:-1;
1503 int j=logical_order?-1:2*sourceLength;
1504 int i=logical_order?-1:sourceLength;
1505 int end=logical_order?sourceLength:-1;
1506 int aggregation_possible = 1;
1507 UChar prev = 0;
1508 UChar prevLink, currLink = 0;
1509 int newSourceLength = 0;
1510 tempsource = (UChar *)uprv_malloc(2*sourceLength*U_SIZEOF_UCHAR);
1511 if(tempsource == NULL) {
1512 *pErrorCode = U_MEMORY_ALLOCATION_ERROR;
1513 return 0;
1514 }
1515
1516 while ((i+=step) != end) {
1517 prevLink = currLink;
1518 currLink = getLink(source[i]);
1519 if (aggregate_tashkeel && ((prevLink|currLink)&COMBINE) == COMBINE && aggregation_possible) {
Craig Cornelius103e9ff2012-10-09 17:03:29 -07001520 aggregation_possible = 0;
Jean-Baptiste Querub13da9d2009-07-17 17:53:22 -07001521 tempsource[j] = (prev<source[i]?prev:source[i])-0x064C+0xFC5E;
1522 currLink = getLink(tempsource[j]);
1523 } else {
1524 aggregation_possible = 1;
1525 tempsource[j+=step] = source[i];
1526 prev = source[i];
1527 newSourceLength++;
1528 }
1529 }
1530 source = tempsource+(logical_order?0:j);
1531 sourceLength = newSourceLength;
1532 }
1533
1534 /* calculate destination size */
1535 /* TODO: do we ever need to do this pure preflighting? */
Craig Cornelius103e9ff2012-10-09 17:03:29 -07001536 if(((options&U_SHAPE_LAMALEF_MASK)==U_SHAPE_LAMALEF_RESIZE) ||
Jean-Baptiste Querub0ac9372009-07-20 15:09:32 -07001537 ((options&U_SHAPE_TASHKEEL_MASK)==U_SHAPE_TASHKEEL_RESIZE)) {
Jean-Baptiste Querub13da9d2009-07-17 17:53:22 -07001538 outputSize=calculateSize(source,sourceLength,destCapacity,options);
1539 } else {
1540 outputSize=sourceLength;
1541 }
Jean-Baptiste Querub0ac9372009-07-20 15:09:32 -07001542
Jean-Baptiste Querub13da9d2009-07-17 17:53:22 -07001543 if(outputSize>destCapacity) {
1544 *pErrorCode=U_BUFFER_OVERFLOW_ERROR;
1545 if (tempsource != NULL) uprv_free(tempsource);
1546 return outputSize;
1547 }
1548
1549 /*
1550 * need a temporary buffer of size max(outputSize, sourceLength)
1551 * because at first we copy source->temp
1552 */
1553 if(sourceLength>outputSize) {
1554 outputSize=sourceLength;
1555 }
1556
1557 /* Start of Arabic letter shaping part */
Craig Cornelius103e9ff2012-10-09 17:03:29 -07001558 if(outputSize<=LENGTHOF(buffer)) {
1559 outputSize=LENGTHOF(buffer);
Jean-Baptiste Querub13da9d2009-07-17 17:53:22 -07001560 tempbuffer=buffer;
1561 } else {
1562 tempbuffer = (UChar *)uprv_malloc(outputSize*U_SIZEOF_UCHAR);
1563
1564 /*Test for NULL*/
1565 if(tempbuffer == NULL) {
1566 *pErrorCode = U_MEMORY_ALLOCATION_ERROR;
1567 if (tempsource != NULL) uprv_free(tempsource);
1568 return 0;
1569 }
1570 }
1571 uprv_memcpy(tempbuffer, source, sourceLength*U_SIZEOF_UCHAR);
Jean-Baptiste Querub0ac9372009-07-20 15:09:32 -07001572 if (tempsource != NULL){
1573 uprv_free(tempsource);
1574 }
1575
Jean-Baptiste Querub13da9d2009-07-17 17:53:22 -07001576 if(sourceLength<outputSize) {
1577 uprv_memset(tempbuffer+sourceLength, 0, (outputSize-sourceLength)*U_SIZEOF_UCHAR);
1578 }
1579
1580 if((options&U_SHAPE_TEXT_DIRECTION_MASK) == U_SHAPE_TEXT_DIRECTION_LOGICAL) {
1581 countSpaces(tempbuffer,sourceLength,options,&spacesCountl,&spacesCountr);
1582 invertBuffer(tempbuffer,sourceLength,options,spacesCountl,spacesCountr);
1583 }
1584
Jean-Baptiste Querub0ac9372009-07-20 15:09:32 -07001585 if((options&U_SHAPE_TEXT_DIRECTION_MASK) == U_SHAPE_TEXT_DIRECTION_VISUAL_LTR) {
1586 if((options&U_SHAPE_SPACES_RELATIVE_TO_TEXT_MASK) == U_SHAPE_SPACES_RELATIVE_TO_TEXT_BEGIN_END) {
1587 spacesRelativeToTextBeginEnd = 1;
1588 uShapeLamalefBegin = U_SHAPE_LAMALEF_END;
1589 uShapeLamalefEnd = U_SHAPE_LAMALEF_BEGIN;
1590
1591 uShapeTashkeelBegin = U_SHAPE_TASHKEEL_END;
1592 uShapeTashkeelEnd = U_SHAPE_TASHKEEL_BEGIN;
1593 }
1594 }
1595
Jean-Baptiste Querub13da9d2009-07-17 17:53:22 -07001596 switch(options&U_SHAPE_LETTERS_MASK) {
1597 case U_SHAPE_LETTERS_SHAPE :
Craig Cornelius103e9ff2012-10-09 17:03:29 -07001598 if( (options&U_SHAPE_TASHKEEL_MASK)> 0
Jean-Baptiste Querub0ac9372009-07-20 15:09:32 -07001599 && ((options&U_SHAPE_TASHKEEL_MASK) !=U_SHAPE_TASHKEEL_REPLACE_BY_TATWEEL)) {
1600 /* Call the shaping function with tashkeel flag == 2 for removal of tashkeel */
Doug Felt2bae19d2010-06-07 16:28:19 -07001601 destLength = shapeUnicode(tempbuffer,sourceLength,destCapacity,options,pErrorCode,2);
Jean-Baptiste Querub0ac9372009-07-20 15:09:32 -07001602 }else {
1603 /* default Call the shaping function with tashkeel flag == 1 */
Doug Felt2bae19d2010-06-07 16:28:19 -07001604 destLength = shapeUnicode(tempbuffer,sourceLength,destCapacity,options,pErrorCode,1);
Jean-Baptiste Querub0ac9372009-07-20 15:09:32 -07001605
1606 /*After shaping text check if user wants to remove tashkeel and replace it with tatweel*/
1607 if( (options&U_SHAPE_TASHKEEL_MASK) == U_SHAPE_TASHKEEL_REPLACE_BY_TATWEEL){
1608 destLength = handleTashkeelWithTatweel(tempbuffer,destLength,destCapacity,options,pErrorCode);
1609 }
1610 }
Jean-Baptiste Querub13da9d2009-07-17 17:53:22 -07001611 break;
1612 case U_SHAPE_LETTERS_SHAPE_TASHKEEL_ISOLATED :
1613 /* Call the shaping function with tashkeel flag == 0 */
Doug Felt2bae19d2010-06-07 16:28:19 -07001614 destLength = shapeUnicode(tempbuffer,sourceLength,destCapacity,options,pErrorCode,0);
Jean-Baptiste Querub13da9d2009-07-17 17:53:22 -07001615 break;
Jean-Baptiste Querub0ac9372009-07-20 15:09:32 -07001616
Jean-Baptiste Querub13da9d2009-07-17 17:53:22 -07001617 case U_SHAPE_LETTERS_UNSHAPE :
1618 /* Call the deshaping function */
1619 destLength = deShapeUnicode(tempbuffer,sourceLength,destCapacity,options,pErrorCode);
1620 break;
1621 default :
1622 /* will never occur because of validity checks above */
1623 destLength = 0;
1624 break;
1625 }
1626
1627 /*
1628 * TODO: (markus 2002aug01)
1629 * For as long as we always preflight the outputSize above
1630 * we should U_ASSERT(outputSize==destLength)
1631 * except for the adjustment above before the tempbuffer allocation
1632 */
1633
1634 if((options&U_SHAPE_TEXT_DIRECTION_MASK) == U_SHAPE_TEXT_DIRECTION_LOGICAL) {
1635 countSpaces(tempbuffer,destLength,options,&spacesCountl,&spacesCountr);
1636 invertBuffer(tempbuffer,destLength,options,spacesCountl,spacesCountr);
1637 }
1638 uprv_memcpy(dest, tempbuffer, uprv_min(destLength, destCapacity)*U_SIZEOF_UCHAR);
1639
1640 if(tempbuffer!=buffer) {
1641 uprv_free(tempbuffer);
1642 }
1643
1644 if(destLength>destCapacity) {
1645 *pErrorCode=U_BUFFER_OVERFLOW_ERROR;
1646 return destLength;
1647 }
1648
1649 /* End of Arabic letter shaping part */
1650 } else {
1651 /*
1652 * No letter shaping:
1653 * just make sure the destination is large enough and copy the string.
1654 */
1655 if(destCapacity<sourceLength) {
1656 /* this catches preflighting, too */
1657 *pErrorCode=U_BUFFER_OVERFLOW_ERROR;
1658 return sourceLength;
1659 }
1660 uprv_memcpy(dest, source, sourceLength*U_SIZEOF_UCHAR);
1661 destLength=sourceLength;
1662 }
1663
1664 /*
1665 * Perform number shaping.
1666 * With UTF-16 or UTF-32, the length of the string is constant.
1667 * The easiest way to do this is to operate on the destination and
1668 * "shape" the digits in-place.
1669 */
1670 if((options&U_SHAPE_DIGITS_MASK)!=U_SHAPE_DIGITS_NOOP) {
1671 UChar digitBase;
1672 int32_t i;
1673
1674 /* select the requested digit group */
1675 switch(options&U_SHAPE_DIGIT_TYPE_MASK) {
1676 case U_SHAPE_DIGIT_TYPE_AN:
1677 digitBase=0x660; /* Unicode: "Arabic-Indic digits" */
1678 break;
1679 case U_SHAPE_DIGIT_TYPE_AN_EXTENDED:
1680 digitBase=0x6f0; /* Unicode: "Eastern Arabic-Indic digits (Persian and Urdu)" */
1681 break;
1682 default:
1683 /* will never occur because of validity checks above */
1684 digitBase=0;
1685 break;
1686 }
1687
1688 /* perform the requested operation */
1689 switch(options&U_SHAPE_DIGITS_MASK) {
1690 case U_SHAPE_DIGITS_EN2AN:
1691 /* add (digitBase-'0') to each European (ASCII) digit code point */
1692 digitBase-=0x30;
1693 for(i=0; i<destLength; ++i) {
1694 if(((uint32_t)dest[i]-0x30)<10) {
1695 dest[i]+=digitBase;
1696 }
1697 }
1698 break;
1699 case U_SHAPE_DIGITS_AN2EN:
1700 /* subtract (digitBase-'0') from each Arabic digit code point */
1701 for(i=0; i<destLength; ++i) {
1702 if(((uint32_t)dest[i]-(uint32_t)digitBase)<10) {
1703 dest[i]-=digitBase-0x30;
1704 }
1705 }
1706 break;
1707 case U_SHAPE_DIGITS_ALEN2AN_INIT_LR:
1708 _shapeToArabicDigitsWithContext(dest, destLength,
1709 digitBase,
1710 (UBool)((options&U_SHAPE_TEXT_DIRECTION_MASK)==U_SHAPE_TEXT_DIRECTION_LOGICAL),
1711 FALSE);
1712 break;
1713 case U_SHAPE_DIGITS_ALEN2AN_INIT_AL:
1714 _shapeToArabicDigitsWithContext(dest, destLength,
1715 digitBase,
1716 (UBool)((options&U_SHAPE_TEXT_DIRECTION_MASK)==U_SHAPE_TEXT_DIRECTION_LOGICAL),
1717 TRUE);
1718 break;
1719 default:
1720 /* will never occur because of validity checks above */
1721 break;
1722 }
1723 }
1724
1725 return u_terminateUChars(dest, destCapacity, destLength, pErrorCode);
1726}