LibreOffice Module i18npool (master)  1
inputsequencechecker_th.cxx
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
5  * This Source Code Form is subject to the terms of the Mozilla Public
6  * License, v. 2.0. If a copy of the MPL was not distributed with this
7  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
11  * Licensed to the Apache Software Foundation (ASF) under one or more
12  * contributor license agreements. See the NOTICE file distributed
13  * with this work for additional information regarding copyright
14  * ownership. The ASF licenses this file to you under the Apache
15  * License, Version 2.0 (the "License"); you may not use this file
16  * except in compliance with the License. You may obtain a copy of
17  * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18  */
19 
20 
22 #include <wtt.h>
23 
24 
25 namespace i18npool {
26 
28  : InputSequenceCheckerImpl("com.sun.star.i18n.InputSequenceChecker_th")
29 {
30 }
31 
33 {
34 }
35 
36 /* Table for Thai Cell Manipulation */
37 char const TAC_celltype_inputcheck[17][17] = {
38 /* Cn */ /* 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F */
39 /* Cn-1 00 */{ 'X', 'A', 'A', 'A', 'A', 'A', 'A', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R' },
40  /* 10 */{ 'X', 'A', 'A', 'A', 'S', 'S', 'A', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R' },
41  /* 20 */{ 'X', 'A', 'A', 'A', 'A', 'S', 'A', 'C', 'C', 'C', 'C', 'C', 'C', 'C', 'C', 'C', 'C' },
42  /* 30 */{ 'X', 'S', 'A', 'S', 'S', 'S', 'S', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R' },
43  /* 40 */{ 'X', 'A', 'A', 'A', 'A', 'S', 'A', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R' },
44  /* 50 */{ 'X', 'A', 'A', 'A', 'A', 'S', 'A', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R' },
45  /* 60 */{ 'X', 'A', 'A', 'A', 'S', 'A', 'S', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R' },
46  /* 70 */{ 'X', 'A', 'A', 'A', 'A', 'S', 'A', 'R', 'R', 'R', 'C', 'C', 'R', 'R', 'R', 'R', 'R' },
47  /* 80 */{ 'X', 'A', 'A', 'A', 'S', 'S', 'A', 'R', 'R', 'R', 'C', 'R', 'R', 'R', 'R', 'R', 'R' },
48  /* 90 */{ 'X', 'A', 'A', 'A', 'S', 'S', 'A', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R' },
49  /* A0 */{ 'X', 'A', 'A', 'A', 'A', 'A', 'A', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R' },
50  /* B0 */{ 'X', 'A', 'A', 'A', 'S', 'S', 'A', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R' },
51  /* C0 */{ 'X', 'A', 'A', 'A', 'S', 'S', 'A', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R' },
52  /* D0 */{ 'X', 'A', 'A', 'A', 'S', 'S', 'A', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R' },
53  /* E0 */{ 'X', 'A', 'A', 'A', 'S', 'S', 'A', 'R', 'R', 'R', 'C', 'C', 'R', 'R', 'R', 'R', 'R' },
54  /* F0 */{ 'X', 'A', 'A', 'A', 'S', 'S', 'A', 'R', 'R', 'R', 'C', 'R', 'R', 'R', 'R', 'R', 'R' },
55  { 'X', 'A', 'A', 'A', 'S', 'S', 'A', 'R', 'R', 'R', 'C', 'R', 'C', 'R', 'R', 'R', 'R' }
56 };
57 
58 bool const TAC_Composible[3][5] = {
59  /* 'A', 'C', 'S', 'R', 'X' */
60 /* Mode 0 */ {true, true, true, true, true}, // PASSTHROUGH = 0
61 /* Mode 1 */ {true, true, true, false, true}, // BASIC = 1
62 /* Mode 2 */ {true, true, false, false, true} // STRICT = 2
63 };
64 
65 static bool check(sal_Unicode ch1, sal_Unicode ch2, sal_Int16 inputCheckMode)
66 {
67  sal_Int16 composible_class;
68  switch (TAC_celltype_inputcheck[getCharType(ch1)][getCharType(ch2)]) {
69  case 'A': composible_class = 0; break;
70  case 'C': composible_class = 1; break;
71  case 'S': composible_class = 2; break;
72  case 'R': composible_class = 3; break;
73  case 'X': composible_class = 4; break;
74  default: composible_class = 0;
75  }
76  return (TAC_Composible[inputCheckMode][composible_class]);
77 }
78 
79 sal_Bool SAL_CALL
80 InputSequenceChecker_th::checkInputSequence(const OUString& Text, sal_Int32 nStartPos,
81  sal_Unicode inputChar, sal_Int16 inputCheckMode)
82 {
83  return check(Text[nStartPos], inputChar, inputCheckMode);
84 }
85 
86 sal_Int32 SAL_CALL
88  sal_Int32 nStartPos,
89  sal_Unicode inputChar,
90  sal_Int16 inputCheckMode)
91 {
92 /* 9 rules for input sequence correction, see issue i42661 for detail,
93 
94 https://bz.apache.org/ooo/show_bug.cgi?id=42661
95 
96 <abv> = <av1>|<av2>|<av3>|<bv1>|<bv2>
97 <abv1> = <av1>|<bv1>
98 <thanthakhat> = 0E4C (karan)
99 
100 1. <cons> <abv>_x + <abv>_y => <cons> <abv>_y (replace)
101 2. <cons> <tone>_x + <tone>_y => <cons> <tone>_y (replace)
102 3. <cons> <abv> <tone>_x + <tone>_y => <cons> <abv> <tone>_y (replace)
103 4. <cons> <abv>_x <tone> + <abv>_y => <cons> <abv>_y <tone> (replace, reorder)
104 5. <cons> <tone> + <abv> => <cons> <abv> <tone> (reorder)
105 6. <cons> <fv1> + <tone> => <cons> <tone> <fv1> (reorder)
106 7. <cons> <tone>_x <fv1> + <tone>_y => <cons> <tone>_y <fv1> (replace, reorder)
107 8. <cons> <thanthakhat> + <abv1> => <cons> <abv1> <thanthakhat> (reorder)
108 9. <cons> <abv1>_x <thanthakhat> + <abv1>_y => <cons> <abv1>_y <thanthakhat>(reorder, replace)
109 */
110 #define CT_ABV(t) ( (t>=CT_AV1 && t<=CT_AV3) || t==CT_BV1 || t==CT_BV2)
111 #define CT_ABV1(t) (t==CT_AV1 || t==CT_BV1)
112 
113  if (check(Text[nStartPos], inputChar, inputCheckMode))
114  Text = Text.replaceAt(++nStartPos, 0, OUString(inputChar));
115  else if (nStartPos > 0 && getCharType(Text[nStartPos-1]) == CT_CONS) {
116  sal_uInt16 t1=getCharType(Text[nStartPos]), t2=getCharType(inputChar);
117  if ( (CT_ABV(t1) && CT_ABV(t2)) || // 1.
118  (t1==CT_TONE && t2==CT_TONE) )// 2.
119  Text = Text.replaceAt(nStartPos, 1, OUString(inputChar));
120  else if ( (t1==CT_TONE && CT_ABV(t2)) || // 5.
121  (t1==CT_FV1 && t2==CT_TONE) || // 6.
122  (Text[nStartPos]==0x0E4C && CT_ABV1(t2)) ) // 8.
123  Text = Text.replaceAt(nStartPos++, 0, OUString(inputChar));
124  else
125  nStartPos=Text.getLength();
126  } else if (nStartPos > 1 && getCharType(Text[nStartPos-2]) == CT_CONS) {
127  sal_uInt16 t1=getCharType(Text[nStartPos-1]), t2=getCharType(Text[nStartPos]), t3=getCharType(inputChar);
128  if (CT_ABV(t1) && t2==CT_TONE && t3==CT_TONE) // 3.
129  Text = Text.replaceAt(nStartPos, 1, OUString(inputChar));
130  else if ( (CT_ABV(t1) && t2==CT_TONE && CT_ABV(t3)) || // 4.
131  (t1==CT_TONE && t2==CT_FV1 && t3==CT_TONE) || // 7.
132  (CT_ABV1(t1) && Text[nStartPos]==0x0E4C && CT_ABV1(t3)) ) // 9.
133  Text = Text.replaceAt(nStartPos-1, 1, OUString(inputChar));
134  else
135  nStartPos=Text.getLength();
136  } else
137  nStartPos=Text.getLength();
138 
139  return nStartPos;
140 }
141 
142 }
143 
144 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
sal_Bool SAL_CALL checkInputSequence(const OUString &Text, sal_Int32 nStartPos, sal_Unicode inputChar, sal_Int16 inputCheckMode) override
#define CT_TONE
Definition: wtt.h:38
sal_uInt16 sal_Unicode
#define CT_ABV(t)
static constexpr sal_uInt16 getCharType(sal_Unicode x)
unsigned char sal_Bool
Constant values shared between i18npool and, for example, the number formatter.
#define CT_CONS
Definition: wtt.h:30
bool const TAC_Composible[3][5]
static bool check(sal_Unicode ch1, sal_Unicode ch2, sal_Int16 inputCheckMode)
#define CT_FV1
Definition: wtt.h:32
char const TAC_celltype_inputcheck[17][17]
sal_Int32 SAL_CALL correctInputSequence(OUString &Text, sal_Int32 nStartPos, sal_Unicode inputChar, sal_Int16 inputCheckMode) override
#define CT_ABV1(t)