2 * Implementation of Indic Syllables for the Uniscribe Script Processor
4 * Copyright 2011 CodeWeavers, Aric Stewart
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
34 #include "wine/debug.h"
35 #include "usp10_internal.h"
37 WINE_DEFAULT_DEBUG_CHANNEL(uniscribe
);
39 static void debug_output_string(const WCHAR
*str
, unsigned int char_count
, lexical_function f
)
42 if (TRACE_ON(uniscribe
))
44 for (i
= 0; i
< char_count
; ++i
)
48 case lex_Consonant
: TRACE("C"); break;
49 case lex_Ra
: TRACE("Ra"); break;
50 case lex_Vowel
: TRACE("V"); break;
51 case lex_Nukta
: TRACE("N"); break;
52 case lex_Halant
: TRACE("H"); break;
53 case lex_ZWNJ
: TRACE("Zwnj"); break;
54 case lex_ZWJ
: TRACE("Zwj"); break;
55 case lex_Matra_post
: TRACE("Mp");break;
56 case lex_Matra_above
: TRACE("Ma");break;
57 case lex_Matra_below
: TRACE("Mb");break;
58 case lex_Matra_pre
: TRACE("Mm");break;
59 case lex_Modifier
: TRACE("Sm"); break;
60 case lex_Vedic
: TRACE("Vd"); break;
61 case lex_Anudatta
: TRACE("A"); break;
62 case lex_Composed_Vowel
: TRACE("t"); break;
71 static inline BOOL
is_matra( int type
)
73 return (type
== lex_Matra_above
|| type
== lex_Matra_below
||
74 type
== lex_Matra_pre
|| type
== lex_Matra_post
||
75 type
== lex_Composed_Vowel
);
78 static inline BOOL
is_joiner( int type
)
80 return (type
== lex_ZWJ
|| type
== lex_ZWNJ
);
83 static int consonant_header(const WCHAR
*input
, unsigned int cChar
,
84 unsigned int start
, unsigned int next
, lexical_function lex
)
86 if (!is_consonant( lex(input
[next
]) )) return -1;
88 if ((next
< cChar
) && lex(input
[next
]) == lex_Nukta
)
90 if ((next
< cChar
) && lex(input
[next
])==lex_Halant
)
93 if((next
< cChar
) && is_joiner( lex(input
[next
]) ))
95 if ((next
< cChar
) && is_consonant( lex(input
[next
]) ))
98 else if ((next
< cChar
) && is_joiner( lex(input
[next
]) ) && lex(input
[next
+1])==lex_Halant
)
101 if ((next
< cChar
) && is_consonant( lex(input
[next
]) ))
107 static int parse_consonant_syllable(const WCHAR
*input
, unsigned int cChar
,
108 unsigned int start
, unsigned int *main
, unsigned int next
, lexical_function lex
)
114 check
= consonant_header(input
,cChar
,start
,next
,lex
);
120 } while (check
!= -1);
121 if (headers
|| is_consonant( lex(input
[next
]) ))
128 if ((next
< cChar
) && lex(input
[next
]) == lex_Nukta
)
130 if ((next
< cChar
) && lex(input
[next
]) == lex_Anudatta
)
133 if ((next
< cChar
) && lex(input
[next
]) == lex_Halant
)
136 if((next
< cChar
) && is_joiner( lex(input
[next
]) ))
139 else if (next
< cChar
)
141 while((next
< cChar
) && is_matra( lex(input
[next
]) ))
143 if ((next
< cChar
) && lex(input
[next
]) == lex_Nukta
)
145 if ((next
< cChar
) && lex(input
[next
]) == lex_Halant
)
148 if ((next
< cChar
) && lex(input
[next
]) == lex_Modifier
)
150 if ((next
< cChar
) && lex(input
[next
]) == lex_Vedic
)
155 static int parse_vowel_syllable(const WCHAR
*input
, unsigned int cChar
,
156 unsigned int start
, unsigned int next
, lexical_function lex
)
158 if ((next
< cChar
) && lex(input
[next
]) == lex_Nukta
)
160 if ((next
< cChar
) && is_joiner( lex(input
[next
]) ) && lex(input
[next
+1])==lex_Halant
&& is_consonant( lex(input
[next
+2]) ))
162 else if ((next
< cChar
) && lex(input
[next
])==lex_Halant
&& is_consonant( lex(input
[next
+1]) ))
164 else if ((next
< cChar
) && lex(input
[next
])==lex_ZWJ
&& is_consonant( lex(input
[next
+1]) ))
167 if ((next
< cChar
) && is_matra( lex(input
[next
]) ))
169 while((next
< cChar
) && is_matra( lex(input
[next
]) ))
171 if ((next
< cChar
) && lex(input
[next
]) == lex_Nukta
)
173 if ((next
< cChar
) && lex(input
[next
]) == lex_Halant
)
177 if ((next
< cChar
) && lex(input
[next
]) == lex_Modifier
)
179 if ((next
< cChar
) && lex(input
[next
]) == lex_Vedic
)
184 static int Indic_process_next_syllable(const WCHAR
*input
, unsigned int cChar
,
185 unsigned int start
, unsigned int *main
, unsigned int next
, lexical_function lex
)
187 if (lex(input
[next
])==lex_Vowel
)
190 return parse_vowel_syllable(input
, cChar
, start
, next
+1, lex
);
192 else if ((cChar
> next
+3) && lex(input
[next
]) == lex_Ra
&& lex(input
[next
+1]) == lex_Halant
&& lex(input
[next
+2]) == lex_Vowel
)
195 return parse_vowel_syllable(input
, cChar
, start
, next
+3, lex
);
198 else if (start
== next
&& lex(input
[next
])==lex_NBSP
)
201 return parse_vowel_syllable(input
, cChar
, start
, next
+1, lex
);
203 else if (start
== next
&& (cChar
> next
+3) && lex(input
[next
]) == lex_Ra
&& lex(input
[next
+1]) == lex_Halant
&& lex(input
[next
+2]) == lex_NBSP
)
206 return parse_vowel_syllable(input
, cChar
, start
, next
+3, lex
);
209 return parse_consonant_syllable(input
, cChar
, start
, main
, next
, lex
);
212 static BOOL
Consonant_is_post_base_form(HDC hdc
, SCRIPT_ANALYSIS
*psa
, ScriptCache
*psc
,
213 const WCHAR
*pwChar
, const IndicSyllable
*s
, lexical_function lexical
, BOOL modern
)
215 if (is_consonant(lexical(pwChar
[s
->base
])) && s
->base
> s
->start
&& lexical(pwChar
[s
->base
-1]) == lex_Halant
)
218 return (SHAPE_does_GSUB_feature_apply_to_chars(hdc
, psa
, psc
, &pwChar
[s
->base
-1], 1, 2, "pstf") > 0);
222 cc
[0] = pwChar
[s
->base
];
223 cc
[1] = pwChar
[s
->base
-1];
224 return (SHAPE_does_GSUB_feature_apply_to_chars(hdc
, psa
, psc
, cc
, 1, 2, "pstf") > 0);
230 static BOOL
Consonant_is_below_base_form(HDC hdc
, SCRIPT_ANALYSIS
*psa
, ScriptCache
*psc
,
231 const WCHAR
*pwChar
, const IndicSyllable
*s
, lexical_function lexical
, BOOL modern
)
233 if (is_consonant(lexical(pwChar
[s
->base
])) && s
->base
> s
->start
&& lexical(pwChar
[s
->base
-1]) == lex_Halant
)
236 return (SHAPE_does_GSUB_feature_apply_to_chars(hdc
, psa
, psc
, &pwChar
[s
->base
-1], 1, 2, "blwf") > 0);
240 cc
[0] = pwChar
[s
->base
];
241 cc
[1] = pwChar
[s
->base
-1];
242 return (SHAPE_does_GSUB_feature_apply_to_chars(hdc
, psa
, psc
, cc
, 1, 2, "blwf") > 0);
248 static BOOL
Consonant_is_pre_base_form(HDC hdc
, SCRIPT_ANALYSIS
*psa
, ScriptCache
*psc
,
249 const WCHAR
*pwChar
, const IndicSyllable
*s
, lexical_function lexical
, BOOL modern
)
251 if (is_consonant(lexical(pwChar
[s
->base
])) && s
->base
> s
->start
&& lexical(pwChar
[s
->base
-1]) == lex_Halant
)
254 return (SHAPE_does_GSUB_feature_apply_to_chars(hdc
, psa
, psc
, &pwChar
[s
->base
-1], 1, 2, "pref") > 0);
258 cc
[0] = pwChar
[s
->base
];
259 cc
[1] = pwChar
[s
->base
-1];
260 return (SHAPE_does_GSUB_feature_apply_to_chars(hdc
, psa
, psc
, cc
, 1, 2, "pref") > 0);
266 static BOOL
Consonant_is_ralf(HDC hdc
, SCRIPT_ANALYSIS
*psa
, ScriptCache
*psc
,
267 const WCHAR
*pwChar
, const IndicSyllable
*s
, lexical_function lexical
)
269 if ((lexical(pwChar
[s
->start
])==lex_Ra
) && s
->end
> s
->start
&& lexical(pwChar
[s
->start
+1]) == lex_Halant
)
270 return (SHAPE_does_GSUB_feature_apply_to_chars(hdc
, psa
, psc
, &pwChar
[s
->start
], 1, 2, "rphf") > 0);
274 static int FindBaseConsonant(HDC hdc
, SCRIPT_ANALYSIS
*psa
, ScriptCache
*psc
,
275 const WCHAR
*input
, IndicSyllable
*s
, lexical_function lex
, BOOL modern
)
281 /* remove ralf from consideration */
282 if (Consonant_is_ralf(hdc
, psa
, psc
, input
, s
, lex
))
288 /* try to find a base consonant */
289 if (!is_consonant( lex(input
[s
->base
]) ))
291 for (i
= s
->end
; i
>= s
->start
; i
--)
292 if (is_consonant( lex(input
[i
]) ))
299 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
)))
301 if (blwf
&& s
->blwf
== -1)
302 s
->blwf
= s
->base
- 1;
303 if (pref
&& s
->pref
== -1)
304 s
->pref
= s
->base
- 1;
306 for (i
= s
->base
-1; i
>= s
->start
; i
--)
307 if (is_consonant( lex(input
[i
]) ))
317 if (s
->ralf
== s
->base
)
323 void Indic_ParseSyllables(HDC hdc
, SCRIPT_ANALYSIS
*psa
, ScriptCache
*psc
, const WCHAR
*input
, unsigned int cChar
,
324 IndicSyllable
**syllables
, int *syllable_count
, lexical_function lex
, BOOL modern
)
326 unsigned int center
= 0;
334 ERR("Failure to have required functions\n");
338 debug_output_string(input
, cChar
, lex
);
341 while((next
< cChar
) && lex(input
[next
]) == lex_Generic
)
346 next
= Indic_process_next_syllable(input
, cChar
, 0, ¢er
, index
, lex
);
350 *syllables
= HeapReAlloc(GetProcessHeap(),0,*syllables
, sizeof(IndicSyllable
)*(*syllable_count
+1));
352 *syllables
= heap_alloc(sizeof(**syllables
));
353 (*syllables
)[*syllable_count
].start
= index
;
354 (*syllables
)[*syllable_count
].base
= center
;
355 (*syllables
)[*syllable_count
].ralf
= -1;
356 (*syllables
)[*syllable_count
].blwf
= -1;
357 (*syllables
)[*syllable_count
].pref
= -1;
358 (*syllables
)[*syllable_count
].end
= next
-1;
359 FindBaseConsonant(hdc
, psa
, psc
, input
, &(*syllables
)[*syllable_count
], lex
, modern
);
361 *syllable_count
= (*syllable_count
)+1;
363 else if (index
< cChar
)
365 TRACE("Processing failed at %i\n",index
);
369 TRACE("Processed %i of %i characters into %i syllables\n",index
,cChar
,*syllable_count
);
372 void Indic_ReorderCharacters(HDC hdc
, SCRIPT_ANALYSIS
*psa
, ScriptCache
*psc
, WCHAR
*input
, unsigned int cChar
,
373 IndicSyllable
**syllables
, int *syllable_count
, lexical_function lex
, reorder_function reorder_f
, BOOL modern
)
379 ERR("Failure to have required functions\n");
383 Indic_ParseSyllables(hdc
, psa
, psc
, input
, cChar
, syllables
, syllable_count
, lex
, modern
);
384 for (i
= 0; i
< *syllable_count
; i
++)
385 reorder_f(input
, &(*syllables
)[i
], lex
);