1*449ab281SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*449ab281SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*449ab281SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*449ab281SAndrew Rist  * distributed with this work for additional information
6*449ab281SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*449ab281SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*449ab281SAndrew Rist  * "License"); you may not use this file except in compliance
9*449ab281SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*449ab281SAndrew Rist  *
11*449ab281SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*449ab281SAndrew Rist  *
13*449ab281SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*449ab281SAndrew Rist  * software distributed under the License is distributed on an
15*449ab281SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*449ab281SAndrew Rist  * KIND, either express or implied.  See the License for the
17*449ab281SAndrew Rist  * specific language governing permissions and limitations
18*449ab281SAndrew Rist  * under the License.
19*449ab281SAndrew Rist  *
20*449ab281SAndrew Rist  *************************************************************/
21*449ab281SAndrew Rist 
22*449ab281SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_i18npool.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include <inputsequencechecker_th.hxx>
28cdf0e10cSrcweir #include <wtt.h>
29cdf0e10cSrcweir 
30cdf0e10cSrcweir using namespace rtl;
31cdf0e10cSrcweir 
32cdf0e10cSrcweir namespace com { namespace sun { namespace star { namespace i18n {
33cdf0e10cSrcweir 
InputSequenceChecker_th()34cdf0e10cSrcweir InputSequenceChecker_th::InputSequenceChecker_th()
35cdf0e10cSrcweir {
36cdf0e10cSrcweir 	serviceName = "com.sun.star.i18n.InputSequenceChecker_th";
37cdf0e10cSrcweir }
38cdf0e10cSrcweir 
~InputSequenceChecker_th()39cdf0e10cSrcweir InputSequenceChecker_th::~InputSequenceChecker_th()
40cdf0e10cSrcweir {
41cdf0e10cSrcweir }
42cdf0e10cSrcweir 
43cdf0e10cSrcweir /* Table for Thai Cell Manipulation */
44cdf0e10cSrcweir sal_Char _TAC_celltype_inputcheck[17][17] = {
45cdf0e10cSrcweir /* Cn */ /*	 0,   1,   2,   3,   4,   5,   6,   7,   8,   9,   A,   B,   C,   D,   E,   F	    */
46cdf0e10cSrcweir /* Cn-1 00 */{	'X', 'A', 'A', 'A', 'A', 'A', 'A', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R' },
47cdf0e10cSrcweir      /* 10 */{	'X', 'A', 'A', 'A', 'S', 'S', 'A', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R' },
48cdf0e10cSrcweir      /* 20 */{	'X', 'A', 'A', 'A', 'A', 'S', 'A', 'C', 'C', 'C', 'C', 'C', 'C', 'C', 'C', 'C', 'C' },
49cdf0e10cSrcweir      /*	30 */{	'X', 'S', 'A', 'S', 'S', 'S', 'S', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R' },
50cdf0e10cSrcweir      /* 40 */{	'X', 'A', 'A', 'A', 'A', 'S', 'A', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R' },
51cdf0e10cSrcweir      /* 50 */{	'X', 'A', 'A', 'A', 'A', 'S', 'A', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R' },
52cdf0e10cSrcweir      /* 60 */{	'X', 'A', 'A', 'A', 'S', 'A', 'S', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R' },
53cdf0e10cSrcweir      /* 70 */{	'X', 'A', 'A', 'A', 'A', 'S', 'A', 'R', 'R', 'R', 'C', 'C', 'R', 'R', 'R', 'R', 'R' },
54cdf0e10cSrcweir      /* 80 */{	'X', 'A', 'A', 'A', 'S', 'S', 'A', 'R', 'R', 'R', 'C', 'R', 'R', 'R', 'R', 'R', 'R' },
55cdf0e10cSrcweir      /* 90 */{	'X', 'A', 'A', 'A', 'S', 'S', 'A', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R' },
56cdf0e10cSrcweir      /* A0 */{	'X', 'A', 'A', 'A', 'A', 'A', 'A', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R' },
57cdf0e10cSrcweir      /* B0 */{	'X', 'A', 'A', 'A', 'S', 'S', 'A', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R' },
58cdf0e10cSrcweir      /* C0 */{	'X', 'A', 'A', 'A', 'S', 'S', 'A', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R' },
59cdf0e10cSrcweir      /* D0 */{	'X', 'A', 'A', 'A', 'S', 'S', 'A', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R' },
60cdf0e10cSrcweir      /* E0 */{	'X', 'A', 'A', 'A', 'S', 'S', 'A', 'R', 'R', 'R', 'C', 'C', 'R', 'R', 'R', 'R', 'R' },
61cdf0e10cSrcweir      /* F0 */{	'X', 'A', 'A', 'A', 'S', 'S', 'A', 'R', 'R', 'R', 'C', 'R', 'R', 'R', 'R', 'R', 'R' },
62cdf0e10cSrcweir 		 { 'X', 'A', 'A', 'A', 'S', 'S', 'A', 'R', 'R', 'R', 'C', 'R', 'C', 'R', 'R', 'R', 'R' }
63cdf0e10cSrcweir };
64cdf0e10cSrcweir 
65cdf0e10cSrcweir sal_Bool _TAC_Composible[3][5] = {
66cdf0e10cSrcweir 		/*  'A',	'C',		'S',		'R',		'X'   */
67cdf0e10cSrcweir /* Mode 0 */    {sal_True,	sal_True,	sal_True,	sal_True,	sal_True}, // PASSTHROUGH = 0
68cdf0e10cSrcweir /* Mode 1 */    {sal_True,	sal_True,	sal_True,	sal_False,  	sal_True}, // BASIC = 1
69cdf0e10cSrcweir /* Mode 2 */    {sal_True,	sal_True,	sal_False,  	sal_False,  	sal_True}  // STRICT = 2
70cdf0e10cSrcweir };
71cdf0e10cSrcweir 
check(sal_Unicode ch1,sal_Unicode ch2,sal_Int16 inputCheckMode)72cdf0e10cSrcweir static sal_Bool SAL_CALL check(sal_Unicode ch1, sal_Unicode ch2, sal_Int16 inputCheckMode)
73cdf0e10cSrcweir {
74cdf0e10cSrcweir 	sal_Int16  composible_class;
75cdf0e10cSrcweir 	switch (_TAC_celltype_inputcheck[getCharType(ch1)][getCharType(ch2)]) {
76cdf0e10cSrcweir 	    case 'A': composible_class = 0; break;
77cdf0e10cSrcweir 	    case 'C': composible_class = 1; break;
78cdf0e10cSrcweir 	    case 'S': composible_class = 2; break;
79cdf0e10cSrcweir 	    case 'R': composible_class = 3; break;
80cdf0e10cSrcweir 	    case 'X': composible_class = 4; break;
81cdf0e10cSrcweir 	    default:  composible_class = 0;
82cdf0e10cSrcweir 	}
83cdf0e10cSrcweir 	return (_TAC_Composible[inputCheckMode][composible_class]);
84cdf0e10cSrcweir }
85cdf0e10cSrcweir 
86cdf0e10cSrcweir sal_Bool SAL_CALL
checkInputSequence(const OUString & Text,sal_Int32 nStartPos,sal_Unicode inputChar,sal_Int16 inputCheckMode)87cdf0e10cSrcweir InputSequenceChecker_th::checkInputSequence(const OUString& Text, sal_Int32 nStartPos,
88cdf0e10cSrcweir 	sal_Unicode inputChar, sal_Int16 inputCheckMode) throw(com::sun::star::uno::RuntimeException)
89cdf0e10cSrcweir {
90cdf0e10cSrcweir     return check(Text[nStartPos], inputChar, inputCheckMode);
91cdf0e10cSrcweir }
92cdf0e10cSrcweir 
93cdf0e10cSrcweir sal_Int32 SAL_CALL
correctInputSequence(OUString & Text,sal_Int32 nStartPos,sal_Unicode inputChar,sal_Int16 inputCheckMode)94cdf0e10cSrcweir InputSequenceChecker_th::correctInputSequence(OUString& Text,
95cdf0e10cSrcweir                                             sal_Int32       nStartPos,
96cdf0e10cSrcweir                                             sal_Unicode     inputChar,
97cdf0e10cSrcweir                                             sal_Int16       inputCheckMode)
98cdf0e10cSrcweir   throw(com::sun::star::uno::RuntimeException)
99cdf0e10cSrcweir {
100cdf0e10cSrcweir /* 9 rules for input sequence correction, see issue i42661 for detail,
101cdf0e10cSrcweir 
102cdf0e10cSrcweir http://www.openoffice.org/issues/show_bug.cgi?id=42661
103cdf0e10cSrcweir 
104cdf0e10cSrcweir <abv> = <av1>|<av2>|<av3>|<bv1>|<bv2>
105cdf0e10cSrcweir <abv1> = <av1>|<bv1>
106cdf0e10cSrcweir <thanthakhat> = 0E4C (karan)
107cdf0e10cSrcweir 
108cdf0e10cSrcweir 1. <cons> <abv>_x + <abv>_y => <cons> <abv>_y (replace)
109cdf0e10cSrcweir 2. <cons> <tone>_x + <tone>_y => <cons> <tone>_y (replace)
110cdf0e10cSrcweir 3. <cons> <abv> <tone>_x + <tone>_y => <cons> <abv> <tone>_y (replace)
111cdf0e10cSrcweir 4. <cons> <abv>_x <tone> + <abv>_y => <cons> <abv>_y <tone> (replace, reorder)
112cdf0e10cSrcweir 5. <cons> <tone> + <abv> => <cons> <abv> <tone> (reorder)
113cdf0e10cSrcweir 6. <cons> <fv1> + <tone> => <cons> <tone> <fv1> (reorder)
114cdf0e10cSrcweir 7. <cons> <tone>_x <fv1> + <tone>_y => <cons> <tone>_y <fv1> (replace, reorder)
115cdf0e10cSrcweir 8. <cons> <thanthakhat> + <abv1> => <cons> <abv1> <thanthakhat> (reorder)
116cdf0e10cSrcweir 9. <cons> <abv1>_x <thanthakhat> + <abv1>_y => <cons> <abv1>_y <thanthakhat>(reorder, replace)
117cdf0e10cSrcweir */
118cdf0e10cSrcweir #define CT_ABV(t)  ( (t>=CT_AV1 && t<=CT_AV3) || t==CT_BV1 || t==CT_BV2)
119cdf0e10cSrcweir #define CT_ABV1(t) (t==CT_AV1 || t==CT_BV1)
120cdf0e10cSrcweir 
121cdf0e10cSrcweir     if (check(Text[nStartPos], inputChar, inputCheckMode))
122cdf0e10cSrcweir         Text = Text.replaceAt(++nStartPos, 0, OUString(inputChar));
123cdf0e10cSrcweir     else if (nStartPos > 0 && getCharType(Text[nStartPos-1]) == CT_CONS) {
124cdf0e10cSrcweir         sal_uInt16 t1=getCharType(Text[nStartPos]), t2=getCharType(inputChar);
125cdf0e10cSrcweir         if ( (CT_ABV(t1) && CT_ABV(t2)) || // 1.
126cdf0e10cSrcweir                 (t1==CT_TONE && t2==CT_TONE) )// 2.
127cdf0e10cSrcweir             Text = Text.replaceAt(nStartPos, 1, OUString(inputChar));
128cdf0e10cSrcweir         else if ( (t1==CT_TONE && CT_ABV(t2)) ||  // 5.
129cdf0e10cSrcweir                 (t1==CT_FV1 && t2==CT_TONE) ||  // 6.
130cdf0e10cSrcweir                 (Text[nStartPos]==0x0E4C && CT_ABV1(t2)) ) // 8.
131cdf0e10cSrcweir             Text = Text.replaceAt(nStartPos++, 0, OUString(inputChar));
132cdf0e10cSrcweir         else
133cdf0e10cSrcweir             nStartPos=Text.getLength();
134cdf0e10cSrcweir     } else if (nStartPos > 1 && getCharType(Text[nStartPos-2]) == CT_CONS) {
135cdf0e10cSrcweir         sal_uInt16 t1=getCharType(Text[nStartPos-1]), t2=getCharType(Text[nStartPos]), t3=getCharType(inputChar);
136cdf0e10cSrcweir         if (CT_ABV(t1) && t2==CT_TONE && t3==CT_TONE) // 3.
137cdf0e10cSrcweir             Text = Text.replaceAt(nStartPos, 1, OUString(inputChar));
138cdf0e10cSrcweir         else if ( (CT_ABV(t1) && t2==CT_TONE && CT_ABV(t3)) || // 4.
139cdf0e10cSrcweir                 (t1==CT_TONE && t2==CT_FV1 && t3==CT_TONE) || // 7.
140cdf0e10cSrcweir                 (CT_ABV1(t1) && Text[nStartPos]==0x0E4C && CT_ABV1(t3)) ) // 9.
141cdf0e10cSrcweir             Text = Text.replaceAt(nStartPos-1, 1, OUString(inputChar));
142cdf0e10cSrcweir         else
143cdf0e10cSrcweir             nStartPos=Text.getLength();
144cdf0e10cSrcweir     } else
145cdf0e10cSrcweir         nStartPos=Text.getLength();
146cdf0e10cSrcweir 
147cdf0e10cSrcweir     return nStartPos;
148cdf0e10cSrcweir }
149cdf0e10cSrcweir 
150cdf0e10cSrcweir } } } }
151