1 /*
2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3  *
4  * This code is free software; you can redistribute it and/or modify it
5  * under the terms of the GNU General Public License version 2 only, as
6  * published by the Free Software Foundation.  Oracle designates this
7  * particular file as subject to the "Classpath" exception as provided
8  * by Oracle in the LICENSE file that accompanied this code.
9  *
10  * This code is distributed in the hope that it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
13  * version 2 for more details (a copy is included in the LICENSE file that
14  * accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License version
17  * 2 along with this work; if not, write to the Free Software Foundation,
18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
19  *
20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
21  * or visit www.oracle.com if you need additional information or have any
22  * questions.
23  *
24  */
25 
26 /*
27  *
28  * (C) Copyright IBM Corp. 1998-2010 - All Rights Reserved
29  *
30  * Developed at DIT - Government of Bhutan
31  *
32  * Contact person: Pema Geyleg - <pema_geyleg@druknet.bt>
33  *
34  * This file is a modification of the ICU file KhmerReordering.cpp
35  * by Jens Herden and Javier Sola who have given all their possible rights to IBM and the Governement of Bhutan
36  * A first module for Dzongkha was developed by Karunakar under Panlocalisation funding.
37  * Assistance for this module has been received from Namgay Thinley, Christopher Fynn and Javier Sola
38  *
39  */
40 
41 //#include <stdio.h>
42 #include "LETypes.h"
43 #include "OpenTypeTables.h"
44 #include "TibetanReordering.h"
45 #include "LEGlyphStorage.h"
46 
47 
48 U_NAMESPACE_BEGIN
49 
50 // Characters that get referred to by name...
51 enum
52 {
53     C_DOTTED_CIRCLE = 0x25CC,
54     C_PRE_NUMBER_MARK = 0x0F3F
55  };
56 
57 
58 enum
59 {
60     // simple classes, they are used in the statetable (in this file) to control the length of a syllable
61     // they are also used to know where a character should be placed (location in reference to the base character)
62     // and also to know if a character, when independtly displayed, should be displayed with a dotted-circle to
63     // indicate error in syllable construction
64     _xx = TibetanClassTable::CC_RESERVED,
65     _ba = TibetanClassTable::CC_BASE,
66     _sj = TibetanClassTable::CC_SUBJOINED | TibetanClassTable::CF_DOTTED_CIRCLE | TibetanClassTable::CF_POS_BELOW,
67     _tp = TibetanClassTable::CC_TSA_PHRU  | TibetanClassTable::CF_DOTTED_CIRCLE | TibetanClassTable::CF_POS_ABOVE,
68     _ac = TibetanClassTable::CC_A_CHUNG |  TibetanClassTable::CF_DOTTED_CIRCLE | TibetanClassTable::CF_POS_BELOW,
69     _cs = TibetanClassTable::CC_COMP_SANSKRIT | TibetanClassTable::CF_DOTTED_CIRCLE | TibetanClassTable::CF_POS_BELOW,
70     _ha = TibetanClassTable::CC_HALANTA | TibetanClassTable::CF_DOTTED_CIRCLE | TibetanClassTable::CF_POS_BELOW,
71     _bv = TibetanClassTable::CC_BELOW_VOWEL | TibetanClassTable::CF_DOTTED_CIRCLE | TibetanClassTable::CF_POS_BELOW,
72     _av = TibetanClassTable::CC_ABOVE_VOWEL | TibetanClassTable::CF_DOTTED_CIRCLE | TibetanClassTable::CF_POS_ABOVE,
73     _an = TibetanClassTable::CC_ANUSVARA | TibetanClassTable::CF_DOTTED_CIRCLE | TibetanClassTable::CF_POS_ABOVE,
74     _cb = TibetanClassTable::CC_CANDRABINDU | TibetanClassTable::CF_DOTTED_CIRCLE | TibetanClassTable::CF_POS_ABOVE,
75     _vs = TibetanClassTable::CC_VISARGA | TibetanClassTable::CF_DOTTED_CIRCLE| TibetanClassTable::CF_POS_AFTER,
76     _as = TibetanClassTable::CC_ABOVE_S_MARK | TibetanClassTable::CF_DOTTED_CIRCLE | TibetanClassTable::CF_POS_ABOVE,
77     _bs = TibetanClassTable::CC_BELOW_S_MARK | TibetanClassTable::CF_DOTTED_CIRCLE | TibetanClassTable::CF_POS_BELOW,
78     _di = TibetanClassTable::CC_DIGIT | TibetanClassTable::CF_DIGIT,
79     _pd = TibetanClassTable::CC_PRE_DIGIT_MARK | TibetanClassTable::CF_DOTTED_CIRCLE | TibetanClassTable::CF_PREDIGIT | TibetanClassTable::CF_POS_BEFORE ,
80     _bd = TibetanClassTable::CC_POST_BELOW_DIGIT_M | TibetanClassTable::CF_DOTTED_CIRCLE | TibetanClassTable::CF_POS_AFTER
81 };
82 
83 
84 // Character class tables
85 //_xx Non Combining characters
86 //_ba Base Consonants
87 //_sj Subjoined consonants
88 //_tp Tsa - phru
89 //_ac A-chung, Vowel Lengthening mark
90 //_cs Precomposed Sanskrit vowel + subjoined consonants
91 //_ha Halanta/Virama
92 //_bv Below vowel
93 //_av above vowel
94 //_an Anusvara
95 //_cb Candrabindu
96 //_vs Visaraga/Post mark
97 //_as Upper Stress marks
98 //_bs Lower Stress marks
99 //_di Digit
100 //_pd Number pre combining, Needs reordering
101 //_bd Other number combining marks
102 
103 static const TibetanClassTable::CharClass tibetanCharClasses[] =
104 {
105    // 0    1    2    3    4    5    6    7    8    9   a     b   c    d     e   f
106     _xx, _ba, _xx, _xx, _ba, _ba, _xx, _xx, _xx, _xx, _xx, _xx, _xx, _xx, _xx, _xx, // 0F00 - 0F0F 0
107     _xx, _xx, _xx, _xx, _xx, _xx, _xx, _xx, _bd, _bd, _xx, _xx, _xx, _xx, _xx, _xx, // 0F10 - 0F1F 1
108     _di, _di, _di, _di, _di, _di, _di, _di, _di, _di, _xx, _xx, _xx, _xx, _xx, _xx, // 0F20 - 0F2F 2
109     _xx, _xx, _xx, _xx, _xx, _bs, _xx, _bs, _xx, _tp, _xx, _xx, _xx, _xx, _bd, _pd, // 0F30 - 0F3F 3
110     _ba, _ba, _ba, _ba, _ba, _ba, _ba, _ba, _xx, _ba, _ba, _ba, _ba, _ba, _ba, _ba, // 0F40 - 0F4F 4
111     _ba, _ba, _ba, _ba, _ba, _ba, _ba, _ba, _ba, _ba, _ba, _ba, _ba, _ba, _ba, _ba, // 0F50 - 0F5F 5
112     _ba, _ba, _ba, _ba, _ba, _ba, _ba, _ba, _ba, _ba, _ba, _xx, _xx, _xx, _xx, _xx, // 0F60 - 0F6F 6
113     _xx, _ac, _av, _cs, _bv, _bv, _cs, _cs, _cs, _cs, _av, _av, _av, _av, _an, _vs, // 0F70 - 0F7F 7
114     _av, _cs, _cb, _cb, _ha, _xx, _as, _as, _ba, _ba, _ba, _ba, _xx, _xx, _xx, _xx, // 0F80 - 0F8F 8
115     _sj, _sj, _sj, _sj, _sj, _sj, _sj, _sj, _xx, _sj, _sj, _sj, _sj, _sj, _sj, _sj, // 0F90 - 0F9F 9
116     _sj, _sj, _sj, _sj, _sj, _sj, _sj, _sj, _sj, _sj, _sj, _sj, _sj, _sj, _sj, _sj, // 0FA0 - 0FAF a
117     _sj, _sj, _sj, _sj, _sj, _sj, _sj, _sj, _sj, _sj, _sj, _sj, _sj, _xx, _sj, _sj, // 0FB0 - 0FBF b
118     _xx, _xx, _xx, _xx, _xx, _xx, _bs, _xx, _xx, _xx, _xx, _xx, _xx, _xx, _xx, _xx, // 0FC0 - 0FCF c
119     _xx, _xx, _xx, _xx, _xx, _xx, _xx, _xx, _xx, _xx, _xx, _xx, _xx, _xx, _xx, _xx,// 0FD0 - 0FDF  d
120     _xx, _xx, _xx, _xx, _xx, _xx, _xx, _xx, _xx, _xx, _xx, _xx, _xx, _xx, _xx, _xx, // 0FE0 - 0FEF e
121     _xx, _xx, _xx, _xx, _xx, _xx, _xx, _xx, _xx, _xx, _xx, _xx, _xx, _xx, _xx, _xx, // 0FF0 - 0FFF f
122 };
123 
124 
125 //
126 // Tibetan Class Tables
127 //
128 
129 //
130 // The range of characters defined in the above table is defined here. For Tibetan 0F00 to 0FFF
131 // Even if the Tibetan range is bigger, most of the characters are not combinable, and therefore treated
132 // as _xx
133 static const TibetanClassTable tibetanClassTable = {0x0F00, 0x0FFF, tibetanCharClasses};
134 
135 
136 // Below we define how a character in the input string is either in the tibetanCharClasses table
137 // (in which case we get its type back), or an unknown object in which case we get _xx (CC_RESERVED) back
getCharClass(LEUnicode ch) const138 TibetanClassTable::CharClass TibetanClassTable::getCharClass(LEUnicode ch) const
139 {
140     if (ch < firstChar || ch > lastChar) {
141         return CC_RESERVED;
142     }
143 
144     return classTable[ch - firstChar];
145 }
146 
getTibetanClassTable()147 const TibetanClassTable *TibetanClassTable::getTibetanClassTable()
148 {
149     return &tibetanClassTable;
150 }
151 
152 
153 
154 class TibetanReorderingOutput : public UMemory {
155 private:
156     le_int32 fSyllableCount;
157     le_int32 fOutIndex;
158     LEUnicode *fOutChars;
159 
160     LEGlyphStorage &fGlyphStorage;
161 
162 
163 public:
TibetanReorderingOutput(LEUnicode * outChars,LEGlyphStorage & glyphStorage)164     TibetanReorderingOutput(LEUnicode *outChars, LEGlyphStorage &glyphStorage)
165         : fSyllableCount(0), fOutIndex(0), fOutChars(outChars), fGlyphStorage(glyphStorage)
166     {
167         // nothing else to do...
168     }
169 
~TibetanReorderingOutput()170     ~TibetanReorderingOutput()
171     {
172         // nothing to do here...
173     }
174 
reset()175     void reset()
176     {
177         fSyllableCount += 1;
178     }
179 
writeChar(LEUnicode ch,le_uint32 charIndex,FeatureMask featureMask)180     void writeChar(LEUnicode ch, le_uint32 charIndex, FeatureMask featureMask)
181     {
182         LEErrorCode success = LE_NO_ERROR;
183 
184         fOutChars[fOutIndex] = ch;
185 
186         fGlyphStorage.setCharIndex(fOutIndex, charIndex, success);
187         fGlyphStorage.setAuxData(fOutIndex, featureMask, success);
188 
189         fOutIndex += 1;
190     }
191 
getOutputIndex()192     le_int32 getOutputIndex()
193     {
194         return fOutIndex;
195     }
196 };
197 
198 
199 //TODO remove unused flags
200 #define ccmpFeatureTag LE_CCMP_FEATURE_TAG
201 #define blwfFeatureTag LE_BLWF_FEATURE_TAG
202 #define pstfFeatureTag LE_PSTF_FEATURE_TAG
203 #define presFeatureTag LE_PRES_FEATURE_TAG
204 #define blwsFeatureTag LE_BLWS_FEATURE_TAG
205 #define abvsFeatureTag LE_ABVS_FEATURE_TAG
206 #define pstsFeatureTag LE_PSTS_FEATURE_TAG
207 
208 #define blwmFeatureTag LE_BLWM_FEATURE_TAG
209 #define abvmFeatureTag LE_ABVM_FEATURE_TAG
210 #define distFeatureTag LE_DIST_FEATURE_TAG
211 
212 #define prefFeatureTag LE_PREF_FEATURE_TAG
213 #define abvfFeatureTag LE_ABVF_FEATURE_TAG
214 #define cligFeatureTag LE_CLIG_FEATURE_TAG
215 #define mkmkFeatureTag LE_MKMK_FEATURE_TAG
216 
217 // Shaping features
218 #define prefFeatureMask 0x80000000UL
219 #define blwfFeatureMask 0x40000000UL
220 #define abvfFeatureMask 0x20000000UL
221 #define pstfFeatureMask 0x10000000UL
222 #define presFeatureMask 0x08000000UL
223 #define blwsFeatureMask 0x04000000UL
224 #define abvsFeatureMask 0x02000000UL
225 #define pstsFeatureMask 0x01000000UL
226 #define cligFeatureMask 0x00800000UL
227 #define ccmpFeatureMask 0x00040000UL
228 
229 // Positioning features
230 #define distFeatureMask 0x00400000UL
231 #define blwmFeatureMask 0x00200000UL
232 #define abvmFeatureMask 0x00100000UL
233 #define mkmkFeatureMask 0x00080000UL
234 
235 #define tagPref    (ccmpFeatureMask | prefFeatureMask | presFeatureMask | cligFeatureMask | distFeatureMask)
236 #define tagAbvf    (ccmpFeatureMask | abvfFeatureMask | abvsFeatureMask | cligFeatureMask | distFeatureMask | abvmFeatureMask | mkmkFeatureMask)
237 #define tagPstf    (ccmpFeatureMask | blwfFeatureMask | blwsFeatureMask | prefFeatureMask | presFeatureMask | pstfFeatureMask | pstsFeatureMask | cligFeatureMask | distFeatureMask | blwmFeatureMask)
238 #define tagBlwf    (ccmpFeatureMask | blwfFeatureMask | blwsFeatureMask | cligFeatureMask | distFeatureMask | blwmFeatureMask | mkmkFeatureMask)
239 #define tagDefault (ccmpFeatureMask | prefFeatureMask | blwfFeatureMask | presFeatureMask | blwsFeatureMask | cligFeatureMask | distFeatureMask | abvmFeatureMask | blwmFeatureMask | mkmkFeatureMask)
240 
241 
242 
243 // These are in the order in which the features need to be applied
244 // for correct processing
245 static const FeatureMap featureMap[] =
246 {
247     // Shaping features
248     {ccmpFeatureTag, ccmpFeatureMask},
249     {prefFeatureTag, prefFeatureMask},
250     {blwfFeatureTag, blwfFeatureMask},
251     {abvfFeatureTag, abvfFeatureMask},
252     {pstfFeatureTag, pstfFeatureMask},
253     {presFeatureTag, presFeatureMask},
254     {blwsFeatureTag, blwsFeatureMask},
255     {abvsFeatureTag, abvsFeatureMask},
256     {pstsFeatureTag, pstsFeatureMask},
257     {cligFeatureTag, cligFeatureMask},
258 
259     // Positioning features
260     {distFeatureTag, distFeatureMask},
261     {blwmFeatureTag, blwmFeatureMask},
262     {abvmFeatureTag, abvmFeatureMask},
263     {mkmkFeatureTag, mkmkFeatureMask},
264 };
265 
266 static const le_int32 featureMapCount = LE_ARRAY_SIZE(featureMap);
267 
268 // The stateTable is used to calculate the end (the length) of a well
269 // formed Tibetan Syllable.
270 //
271 // Each horizontal line is ordered exactly the same way as the values in TibetanClassTable
272 // CharClassValues in TibetanReordering.h This coincidence of values allows the
273 // follow up of the table.
274 //
275 // Each line corresponds to a state, which does not necessarily need to be a type
276 // of component... for example, state 2 is a base, with is always a first character
277 // in the syllable, but the state could be produced a consonant of any type when
278 // it is the first character that is analysed (in ground state).
279 //
280 static const le_int8 tibetanStateTable[][TibetanClassTable::CC_COUNT] =
281 {
282 
283 
284     //Dzongkha state table
285     //xx  ba  sj  tp  ac  cs  ha  bv  av  an  cb  vs  as  bs  di  pd  bd
286     { 1,  2,  4,  3,  8,  7,  9, 10, 14, 13, 17, 18, 19, 19, 20, 21, 21,}, //  0 - ground state
287     {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,}, //  1 - exit state (or sign to the right of the syllable)
288     {-1, -1,  4,  3,  8,  7,  9, 10, 14, 13, 17, 18, 19, 19, -1, -1, -1,}, //  2 - Base consonant
289     {-1, -1,  5, -1,  8,  7, -1, 10, 14, 13, 17, 18, 19, 19, -1, -1, -1,}, //  3 - Tsa phru after base
290     {-1, -1,  4,  6,  8,  7,  9, 10, 14, 13, 17, 18, 19, 19, -1, -1, -1,}, //  4 - Subjoined consonant after base
291     {-1, -1,  5, -1,  8,  7, -1, 10, 14, 13, 17, 18, 19, 19, -1, -1, -1,}, //  5 - Subjoined consonant after tsa phru
292     {-1, -1, -1, -1,  8,  7, -1, 10, 14, 13, 17, 18, 19, 19, -1, -1, -1,}, //  6 - Tsa phru after subjoined consonant
293     {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 19, 19, -1, -1, -1,}, //  7 - Pre Composed Sanskrit
294     {-1, -1, -1, -1, -1, -1, -1, 10, 14, 13, 17, 18, 19, 19, -1, -1, -1,}, //  8 - A-chung
295     {-1, -1, -1, -1, -1, -1, -1, -1, 14, 13, 17, -1, 19, 19, -1, -1, -1,}, //  9 - Halanta
296     {-1, -1, -1, -1, -1, -1, -1, 11, 14, 13, 17, 18, 19, 19, -1, -1, -1,}, // 10 - below vowel 1
297     {-1, -1, -1, -1, -1, -1, -1, 12, 14, 13, 17, 18, 19, 19, -1, -1, -1,}, // 11 - below vowel 2
298     {-1, -1, -1, -1, -1, -1, -1, -1, 14, 13, 17, 18, 19, 19, -1, -1, -1,}, // 12 - below vowel 3
299     {-1, -1, -1, -1, -1, -1, -1, -1, 14, 17, 17, 18, 19, 19, -1, -1, -1,}, // 13 - Anusvara before vowel
300     {-1, -1, -1, -1, -1, -1, -1, -1, 15, 17, 17, 18, 19, 19, -1, -1, -1,}, // 14 - above vowel 1
301     {-1, -1, -1, -1, -1, -1, -1, -1, 16, 17, 17, 18, 19, 19, -1, -1, -1,}, // 15 - above vowel 2
302     {-1, -1, -1, -1, -1, -1, -1, -1, -1, 17, 17, 18, 19, 19, -1, -1, -1,}, // 16 - above vowel 3
303     {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 18, 19, 19, -1, -1, -1,}, // 17 - Anusvara or Candrabindu after vowel
304     {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 19, 19, -1, -1, -1,}, // 18 - Visarga
305     {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,}, // 19 - strss mark
306     {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 21, 21,}, // 20 - digit
307     {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,}, // 21 - digit mark
308 
309 
310 };
311 
312 
getFeatureMap(le_int32 & count)313 const FeatureMap *TibetanReordering::getFeatureMap(le_int32 &count)
314 {
315     count = featureMapCount;
316 
317     return featureMap;
318 }
319 
320 
321 // Given an input string of characters and a location in which to start looking
322 // calculate, using the state table, which one is the last character of the syllable
323 // that starts in the starting position.
findSyllable(const TibetanClassTable * classTable,const LEUnicode * chars,le_int32 prev,le_int32 charCount)324 le_int32 TibetanReordering::findSyllable(const TibetanClassTable *classTable, const LEUnicode *chars, le_int32 prev, le_int32 charCount)
325 {
326     le_int32 cursor = prev;
327     le_int8 state = 0;
328 
329     while (cursor < charCount) {
330         TibetanClassTable::CharClass charClass = (classTable->getCharClass(chars[cursor]) & TibetanClassTable::CF_CLASS_MASK);
331 
332         state = tibetanStateTable[state][charClass];
333 
334         if (state < 0) {
335             break;
336         }
337 
338         cursor += 1;
339     }
340 
341     return cursor;
342 }
343 
344 
345 // This is the real reordering function as applied to the Tibetan language
346 
reorder(const LEUnicode * chars,le_int32 charCount,le_int32,LEUnicode * outChars,LEGlyphStorage & glyphStorage)347 le_int32 TibetanReordering::reorder(const LEUnicode *chars, le_int32 charCount, le_int32,
348                                   LEUnicode *outChars, LEGlyphStorage &glyphStorage)
349 {
350     const TibetanClassTable *classTable = TibetanClassTable::getTibetanClassTable();
351 
352     TibetanReorderingOutput output(outChars, glyphStorage);
353     TibetanClassTable::CharClass charClass;
354     le_int32 i, prev = 0;
355 
356     // This loop only exits when we reach the end of a run, which may contain
357     // several syllables.
358     while (prev < charCount) {
359         le_int32 syllable = findSyllable(classTable, chars, prev, charCount);
360 
361         output.reset();
362 
363         // shall we add a dotted circle?
364         // If in the position in which the base should be (first char in the string) there is
365         // a character that has the Dotted circle flag (a character that cannot be a base)
366         // then write a dotted circle
367         if (classTable->getCharClass(chars[prev]) & TibetanClassTable::CF_DOTTED_CIRCLE) {
368             output.writeChar(C_DOTTED_CIRCLE, prev, tagDefault);
369         }
370 
371         // copy the rest to output, inverting the pre-number mark if present after a digit.
372         for (i = prev; i < syllable; i += 1) {
373             charClass = classTable->getCharClass(chars[i]);
374 
375            if ((TibetanClassTable::CF_DIGIT & charClass)
376               && ( classTable->getCharClass(chars[i+1]) & TibetanClassTable::CF_PREDIGIT))
377            {
378                          output.writeChar(C_PRE_NUMBER_MARK, i, tagPref);
379                          output.writeChar(chars[i], i+1 , tagPref);
380                         i += 1;
381           } else {
382             switch (charClass & TibetanClassTable::CF_POS_MASK) {
383 
384                 // If the present character is a number, and the next character is a pre-number combining mark
385             // then the two characters are reordered
386 
387                 case TibetanClassTable::CF_POS_ABOVE :
388                     output.writeChar(chars[i], i, tagAbvf);
389                     break;
390 
391                 case TibetanClassTable::CF_POS_AFTER :
392                     output.writeChar(chars[i], i, tagPstf);
393                     break;
394 
395                 case TibetanClassTable::CF_POS_BELOW :
396                     output.writeChar(chars[i], i, tagBlwf);
397                     break;
398 
399                 default:
400                     // default - any other characters
401                    output.writeChar(chars[i], i, tagDefault);
402                     break;
403             } // switch
404           } // if
405         } // for
406 
407         prev = syllable; // move the pointer to the start of next syllable
408     }
409 
410     return output.getOutputIndex();
411 }
412 
413 
414 U_NAMESPACE_END
415