1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
/*
* Implementation of Indic Syllables for the Uniscribe Script Processor
*
* Copyright 2011 CodeWeavers, Aric Stewart
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
*
*/
#include "config.h"
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include "windef.h"
#include "winbase.h"
#include "winuser.h"
#include "wingdi.h"
#include "winnls.h"
#include "usp10.h"
#include "winternl.h"
#include "wine/debug.h"
#include "usp10_internal.h"
WINE_DEFAULT_DEBUG_CHANNEL(uniscribe);
static void debug_output_string(LPCWSTR str, int cChar, lexical_function f)
{
int i;
if (TRACE_ON(uniscribe))
{
for (i = 0; i < cChar; i++)
{
switch (f(str[i]))
{
case lex_Consonant: TRACE("C"); break;
case lex_Ra: TRACE("Ra"); break;
case lex_Vowel: TRACE("V"); break;
case lex_Nukta: TRACE("N"); break;
case lex_Halant: TRACE("H"); break;
case lex_ZWNJ: TRACE("Zwnj"); break;
case lex_ZWJ: TRACE("Zwj"); break;
case lex_Matra_post: TRACE("Mp");break;
case lex_Matra_above: TRACE("Ma");break;
case lex_Matra_below: TRACE("Mb");break;
case lex_Matra_pre: TRACE("Mm");break;
case lex_Modifier: TRACE("Sm"); break;
case lex_Vedic: TRACE("Vd"); break;
case lex_Anudatta: TRACE("A"); break;
case lex_Composed_Vowel: TRACE("t"); break;
default:
TRACE("X"); break;
}
}
TRACE("\n");
}
}
static inline BOOL is_matra( int type )
{
return (type == lex_Matra_above || type == lex_Matra_below ||
type == lex_Matra_pre || type == lex_Matra_post ||
type == lex_Composed_Vowel);
}
static inline BOOL is_joiner( int type )
{
return (type == lex_ZWJ || type == lex_ZWNJ);
}
static INT consonant_header(LPCWSTR input, INT cChar, INT start, INT next,
lexical_function lex)
{
if (!is_consonant( lex(input[next]) )) return -1;
next++;
if ((next < cChar) && lex(input[next]) == lex_Nukta)
next++;
if ((next < cChar) && lex(input[next])==lex_Halant)
{
next++;
if((next < cChar) && is_joiner( lex(input[next]) ))
next++;
if ((next < cChar) && is_consonant( lex(input[next]) ))
return next;
}
else if ((next < cChar) && is_joiner( lex(input[next]) ) && lex(input[next+1])==lex_Halant)
{
next+=2;
if ((next < cChar) && is_consonant( lex(input[next]) ))
return next;
}
return -1;
}
static INT parse_consonant_syllable(LPCWSTR input, INT cChar, INT start,
INT *main, INT next, lexical_function lex)
{
int check;
int headers = 0;
do
{
check = consonant_header(input,cChar,start,next,lex);
if (check != -1)
{
next = check;
headers++;
}
} while (check != -1);
if (headers || is_consonant( lex(input[next]) ))
{
*main = next;
next++;
}
else
return -1;
if ((next < cChar) && lex(input[next]) == lex_Nukta)
next++;
if ((next < cChar) && lex(input[next]) == lex_Anudatta)
next++;
if ((next < cChar) && lex(input[next]) == lex_Halant)
{
next++;
if((next < cChar) && is_joiner( lex(input[next]) ))
next++;
}
else if (next < cChar)
{
while((next < cChar) && is_matra( lex(input[next]) ))
next++;
if ((next < cChar) && lex(input[next]) == lex_Nukta)
next++;
if ((next < cChar) && lex(input[next]) == lex_Halant)
next++;
}
if ((next < cChar) && lex(input[next]) == lex_Modifier)
next++;
if ((next < cChar) && lex(input[next]) == lex_Vedic)
next++;
return next;
}
static INT parse_vowel_syllable(LPCWSTR input, INT cChar, INT start,
INT next, lexical_function lex)
{
if ((next < cChar) && lex(input[next]) == lex_Nukta)
next++;
if ((next < cChar) && is_joiner( lex(input[next]) ) && lex(input[next+1])==lex_Halant && is_consonant( lex(input[next+2]) ))
next+=3;
else if ((next < cChar) && lex(input[next])==lex_Halant && is_consonant( lex(input[next+1]) ))
next+=2;
else if ((next < cChar) && lex(input[next])==lex_ZWJ && is_consonant( lex(input[next+1]) ))
next+=2;
if ((next < cChar) && is_matra( lex(input[next]) ))
{
while((next < cChar) && is_matra( lex(input[next]) ))
next++;
if ((next < cChar) && lex(input[next]) == lex_Nukta)
next++;
if ((next < cChar) && lex(input[next]) == lex_Halant)
next++;
}
if ((next < cChar) && lex(input[next]) == lex_Modifier)
next++;
if ((next < cChar) && lex(input[next]) == lex_Vedic)
next++;
return next;
}
static INT Indic_process_next_syllable( LPCWSTR input, INT cChar, INT start, INT* main, INT next, lexical_function lex )
{
if (lex(input[next])==lex_Vowel)
{
*main = next;
return parse_vowel_syllable(input, cChar, start, next+1, lex);
}
else if ((cChar > next+3) && lex(input[next]) == lex_Ra && lex(input[next+1]) == lex_Halant && lex(input[next+2]) == lex_Vowel)
{
*main = next+2;
return parse_vowel_syllable(input, cChar, start, next+3, lex);
}
else if (start == next && lex(input[next])==lex_NBSP)
{
*main = next;
return parse_vowel_syllable(input, cChar, start, next+1, lex);
}
else if (start == next && (cChar > next+3) && lex(input[next]) == lex_Ra && lex(input[next+1]) == lex_Halant && lex(input[next+2]) == lex_NBSP)
{
*main = next+2;
return parse_vowel_syllable(input, cChar, start, next+3, lex);
}
return parse_consonant_syllable(input, cChar, start, main, next, lex);
}
static BOOL Consonant_is_post_base_form(HDC hdc, SCRIPT_ANALYSIS *psa, ScriptCache* psc, LPCWSTR pwChar, IndicSyllable *s, lexical_function lexical, BOOL modern)
{
if (is_consonant(lexical(pwChar[s->base])) && s->base > s->start && lexical(pwChar[s->base-1]) == lex_Halant)
{
if (modern)
return (SHAPE_does_GSUB_feature_apply_to_chars(hdc, psa, psc, &pwChar[s->base-1], 1, 2, "pstf") > 0);
else
{
WCHAR cc[2];
cc[0] = pwChar[s->base];
cc[1] = pwChar[s->base-1];
return (SHAPE_does_GSUB_feature_apply_to_chars(hdc, psa, psc, cc, 1, 2, "pstf") > 0);
}
}
return FALSE;
}
static BOOL Consonant_is_below_base_form(HDC hdc, SCRIPT_ANALYSIS *psa, ScriptCache* psc, LPCWSTR pwChar, IndicSyllable *s, lexical_function lexical, BOOL modern)
{
if (is_consonant(lexical(pwChar[s->base])) && s->base > s->start && lexical(pwChar[s->base-1]) == lex_Halant)
{
if (modern)
return (SHAPE_does_GSUB_feature_apply_to_chars(hdc, psa, psc, &pwChar[s->base-1], 1, 2, "blwf") > 0);
else
{
WCHAR cc[2];
cc[0] = pwChar[s->base];
cc[1] = pwChar[s->base-1];
return (SHAPE_does_GSUB_feature_apply_to_chars(hdc, psa, psc, cc, 1, 2, "blwf") > 0);
}
}
return FALSE;
}
static BOOL Consonant_is_pre_base_form(HDC hdc, SCRIPT_ANALYSIS *psa, ScriptCache* psc, LPCWSTR pwChar, IndicSyllable *s, lexical_function lexical, BOOL modern)
{
if (is_consonant(lexical(pwChar[s->base])) && s->base > s->start && lexical(pwChar[s->base-1]) == lex_Halant)
{
if (modern)
return (SHAPE_does_GSUB_feature_apply_to_chars(hdc, psa, psc, &pwChar[s->base-1], 1, 2, "pref") > 0);
else
{
WCHAR cc[2];
cc[0] = pwChar[s->base];
cc[1] = pwChar[s->base-1];
return (SHAPE_does_GSUB_feature_apply_to_chars(hdc, psa, psc, cc, 1, 2, "pref") > 0);
}
}
return FALSE;
}
static BOOL Consonant_is_ralf(HDC hdc, SCRIPT_ANALYSIS *psa, ScriptCache* psc, LPCWSTR pwChar, IndicSyllable *s, lexical_function lexical)
{
if ((lexical(pwChar[s->start])==lex_Ra) && s->end > s->start && lexical(pwChar[s->start+1]) == lex_Halant)
return (SHAPE_does_GSUB_feature_apply_to_chars(hdc, psa, psc, &pwChar[s->start], 1, 2, "rphf") > 0);
return FALSE;
}
static int FindBaseConsonant(HDC hdc, SCRIPT_ANALYSIS *psa, ScriptCache* psc, LPCWSTR input, IndicSyllable *s, lexical_function lex, BOOL modern)
{
int i;
BOOL blwf = FALSE;
BOOL pref = FALSE;
/* remove ralf from consideration */
if (Consonant_is_ralf(hdc, psa, psc, input, s, lex))
{
s->ralf = s->start;
s->start+=2;
}
/* try to find a base consonant */
if (!is_consonant( lex(input[s->base]) ))
{
for (i = s->end; i >= s->start; i--)
if (is_consonant( lex(input[i]) ))
{
s->base = i;
break;
}
}
while ((blwf = Consonant_is_below_base_form(hdc, psa, psc, input, s, lex, modern)) || Consonant_is_post_base_form(hdc, psa, psc, input, s, lex, modern) || (pref = Consonant_is_pre_base_form(hdc, psa, psc, input, s, lex, modern)))
{
if (blwf && s->blwf == -1)
s->blwf = s->base - 1;
if (pref && s->pref == -1)
s->pref = s->base - 1;
for (i = s->base-1; i >= s->start; i--)
if (is_consonant( lex(input[i]) ))
{
s->base = i;
break;
}
}
if (s->ralf >= 0)
s->start = s->ralf;
if (s->ralf == s->base)
s->ralf = -1;
return s->base;
}
void Indic_ParseSyllables( HDC hdc, SCRIPT_ANALYSIS *psa, ScriptCache* psc, LPCWSTR input, const int cChar, IndicSyllable **syllables, int *syllable_count, lexical_function lex, BOOL modern)
{
int index = 0;
int next = 0;
int center = 0;
*syllable_count = 0;
if (!lex)
{
ERR("Failure to have required functions\n");
return;
}
debug_output_string(input, cChar, lex);
while (next != -1)
{
while((next < cChar) && lex(input[next]) == lex_Generic)
next++;
index = next;
if (next >= cChar)
break;
next = Indic_process_next_syllable(input, cChar, 0, ¢er, index, lex);
if (next != -1)
{
if (*syllable_count)
*syllables = HeapReAlloc(GetProcessHeap(),0,*syllables, sizeof(IndicSyllable)*(*syllable_count+1));
else
*syllables = HeapAlloc(GetProcessHeap(),0,sizeof(IndicSyllable));
(*syllables)[*syllable_count].start = index;
(*syllables)[*syllable_count].base = center;
(*syllables)[*syllable_count].ralf = -1;
(*syllables)[*syllable_count].blwf = -1;
(*syllables)[*syllable_count].pref = -1;
(*syllables)[*syllable_count].end = next-1;
FindBaseConsonant(hdc, psa, psc, input, &(*syllables)[*syllable_count], lex, modern);
index = next;
*syllable_count = (*syllable_count)+1;
}
else if (index < cChar)
{
TRACE("Processing failed at %i\n",index);
next = ++index;
}
}
TRACE("Processed %i of %i characters into %i syllables\n",index,cChar,*syllable_count);
}
void Indic_ReorderCharacters( HDC hdc, SCRIPT_ANALYSIS *psa, ScriptCache* psc, LPWSTR input, int cChar, IndicSyllable **syllables, int *syllable_count, lexical_function lex, reorder_function reorder_f, BOOL modern)
{
int i;
if (!reorder_f)
{
ERR("Failure to have required functions\n");
return;
}
Indic_ParseSyllables(hdc, psa, psc, input, cChar, syllables, syllable_count, lex, modern);
for (i = 0; i < *syllable_count; i++)
reorder_f(input, &(*syllables)[i], lex);
}