1 /**************************************************************
2 *
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
18 * under the License.
19 *
20 *************************************************************/
21
22
23
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_i18npool.hxx"
26 #include <breakiterator_th.hxx>
27 #include <wtt.h>
28
29 #include <string.h> // for memset
30
31 using namespace ::com::sun::star::uno;
32 using namespace ::com::sun::star::lang;
33 using namespace ::rtl;
34
35 namespace com { namespace sun { namespace star { namespace i18n {
36
37 // ----------------------------------------------------
38 // class Breakiterator_th
39 // ----------------------------------------------------;
BreakIterator_th()40 BreakIterator_th::BreakIterator_th()
41 {
42 cBreakIterator = "com.sun.star.i18n.BreakIterator_th";
43 wordRule=lineRule=NULL;
44 }
45
~BreakIterator_th()46 BreakIterator_th::~BreakIterator_th()
47 {
48 }
49
50 #define SARA_AM 0x0E33
51
52 /*
53 * cell composition states
54 */
55
56 #define ST_COM 1 // Compose the following character with leading char and display in the same cell
57 #define ST_NXT 2 // display the following character in the next cell
58 #define ST_NDP 3 // non-display
59
60 static const sal_Int16 thaiCompRel[MAX_CT][MAX_CT] = {
61 // C N C L F F F B B B T A A A A A A
62 // T O O V V V V V V D O D D D V V V
63 // R N N 1 2 3 1 2 N 1 2 3 1 2 3
64 // L S E
65 // 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
66 { ST_NDP, ST_NXT, ST_NXT, ST_NXT, ST_NXT, ST_NXT, ST_NXT, ST_NXT, ST_NXT, ST_NXT, ST_NXT, ST_NXT, ST_NXT, ST_NXT, ST_NXT, ST_NXT, ST_NXT }, // CTRL 0
67 { ST_NDP, ST_NXT, ST_NXT, ST_NXT, ST_NXT, ST_NXT, ST_NXT, ST_NXT, ST_NXT, ST_NXT, ST_NXT, ST_NXT, ST_NXT, ST_NXT, ST_NXT, ST_NXT, ST_NXT }, // NON 1
68 { ST_NDP, ST_NXT, ST_NXT, ST_NXT, ST_NXT, ST_NXT, ST_NXT, ST_COM, ST_COM, ST_COM, ST_COM, ST_COM, ST_COM, ST_COM, ST_COM, ST_COM, ST_COM }, // CONS 2
69 { ST_NDP, ST_NXT, ST_NXT, ST_NXT, ST_NXT, ST_NXT, ST_NXT, ST_NXT, ST_NXT, ST_NXT, ST_NXT, ST_NXT, ST_NXT, ST_NXT, ST_NXT, ST_NXT, ST_NXT }, // LV 3
70 { ST_NDP, ST_NXT, ST_NXT, ST_NXT, ST_NXT, ST_NXT, ST_NXT, ST_NXT, ST_NXT, ST_NXT, ST_NXT, ST_NXT, ST_NXT, ST_NXT, ST_NXT, ST_NXT, ST_NXT }, // FV1 4
71 { ST_NDP, ST_NXT, ST_NXT, ST_NXT, ST_NXT, ST_NXT, ST_NXT, ST_NXT, ST_NXT, ST_NXT, ST_NXT, ST_NXT, ST_NXT, ST_NXT, ST_NXT, ST_NXT, ST_NXT }, // FV2 5
72 { ST_NDP, ST_NXT, ST_NXT, ST_NXT, ST_NXT, ST_NXT, ST_NXT, ST_NXT, ST_NXT, ST_NXT, ST_NXT, ST_NXT, ST_NXT, ST_NXT, ST_NXT, ST_NXT, ST_NXT }, // FV3 6
73 { ST_NDP, ST_NXT, ST_NXT, ST_NXT, ST_NXT, ST_NXT, ST_NXT, ST_NXT, ST_NXT, ST_NXT, ST_COM, ST_COM, ST_NXT, ST_NXT, ST_NXT, ST_NXT, ST_NXT }, // BV1 7
74 { ST_NDP, ST_NXT, ST_NXT, ST_NXT, ST_NXT, ST_NXT, ST_NXT, ST_NXT, ST_NXT, ST_NXT, ST_COM, ST_NXT, ST_NXT, ST_NXT, ST_NXT, ST_NXT, ST_NXT }, // BV2 8
75 { ST_NDP, ST_NXT, ST_NXT, ST_NXT, ST_NXT, ST_NXT, ST_NXT, ST_NXT, ST_NXT, ST_NXT, ST_NXT, ST_NXT, ST_NXT, ST_NXT, ST_NXT, ST_NXT, ST_NXT }, // BD 9
76 { ST_NDP, ST_NXT, ST_NXT, ST_NXT, ST_NXT, ST_NXT, ST_NXT, ST_NXT, ST_NXT, ST_NXT, ST_NXT, ST_NXT, ST_NXT, ST_NXT, ST_NXT, ST_NXT, ST_NXT }, // TONE 10
77 { ST_NDP, ST_NXT, ST_NXT, ST_NXT, ST_NXT, ST_NXT, ST_NXT, ST_NXT, ST_NXT, ST_NXT, ST_NXT, ST_NXT, ST_NXT, ST_NXT, ST_NXT, ST_NXT, ST_NXT }, // AD1 11
78 { ST_NDP, ST_NXT, ST_NXT, ST_NXT, ST_NXT, ST_NXT, ST_NXT, ST_NXT, ST_NXT, ST_NXT, ST_NXT, ST_NXT, ST_NXT, ST_NXT, ST_NXT, ST_NXT, ST_NXT }, // AD2 12
79 { ST_NDP, ST_NXT, ST_NXT, ST_NXT, ST_NXT, ST_NXT, ST_NXT, ST_NXT, ST_NXT, ST_NXT, ST_NXT, ST_NXT, ST_NXT, ST_NXT, ST_NXT, ST_NXT, ST_NXT }, // AD3 13
80 { ST_NDP, ST_NXT, ST_NXT, ST_NXT, ST_NXT, ST_NXT, ST_NXT, ST_NXT, ST_NXT, ST_NXT, ST_COM, ST_COM, ST_NXT, ST_NXT, ST_NXT, ST_NXT, ST_NXT }, // AV1 14
81 { ST_NDP, ST_NXT, ST_NXT, ST_NXT, ST_NXT, ST_NXT, ST_NXT, ST_NXT, ST_NXT, ST_NXT, ST_COM, ST_NXT, ST_NXT, ST_NXT, ST_NXT, ST_NXT, ST_NXT }, // AV2 15
82 { ST_NDP, ST_NXT, ST_NXT, ST_NXT, ST_NXT, ST_NXT, ST_NXT, ST_NXT, ST_NXT, ST_NXT, ST_COM, ST_NXT, ST_COM, ST_NXT, ST_NXT, ST_NXT, ST_NXT } // AV3 16
83
84 };
85
86 const sal_uInt32 is_ST_COM = (1<<CT_CTRL)|(1<<CT_NON)|(1<<CT_CONS)|(1<<CT_TONE);
87
getCombState(const sal_Unicode * text,sal_Int32 pos)88 static sal_uInt16 SAL_CALL getCombState(const sal_Unicode *text, sal_Int32 pos)
89 {
90 sal_uInt16 ch1 = getCharType(text[pos]);
91 sal_uInt16 ch2 = getCharType(text[pos+1]);
92
93 if (text[pos+1] == SARA_AM) {
94 if ((1 << ch1) & is_ST_COM)
95 return ST_COM;
96 else
97 ch2 = CT_AD1;
98 }
99
100 return thaiCompRel[ch1][ch2];
101 }
102
103
getACell(const sal_Unicode * text,sal_Int32 pos,sal_Int32 len)104 static sal_Int32 SAL_CALL getACell(const sal_Unicode *text, sal_Int32 pos, sal_Int32 len)
105 {
106 sal_uInt32 curr = 1;
107 for (; pos + 1 < len && getCombState(text, pos) == ST_COM; curr++, pos++) {}
108 return curr;
109 }
110
111 #define is_Thai(c) (0x0e00 <= c && c <= 0x0e7f) // Unicode definition for Thai
112
makeIndex(const OUString & Text,sal_Int32 nStartPos)113 void SAL_CALL BreakIterator_th::makeIndex(const OUString& Text, sal_Int32 nStartPos)
114 throw(RuntimeException)
115 {
116 if (Text != cachedText) {
117 cachedText = Text;
118 if (cellIndexSize < cachedText.getLength()) {
119 cellIndexSize = cachedText.getLength();
120 free(nextCellIndex);
121 free(previousCellIndex);
122 nextCellIndex = (sal_Int32*) calloc(cellIndexSize, sizeof(sal_Int32));
123 previousCellIndex = (sal_Int32*) calloc(cellIndexSize, sizeof(sal_Int32));
124 }
125 // reset nextCell for new Text
126 memset(nextCellIndex, 0, cellIndexSize * sizeof(sal_Int32));
127 }
128 else if (nextCellIndex[nStartPos] > 0 || ! is_Thai(Text[nStartPos]))
129 return;
130
131 const sal_Unicode* str = cachedText.getStr();
132 sal_Int32 len = cachedText.getLength(), startPos, endPos;
133
134 startPos = nStartPos;
135 while (startPos > 0 && is_Thai(str[startPos-1])) startPos--;
136 endPos = nStartPos+1;
137 while (endPos < len && is_Thai(str[endPos])) endPos++;
138
139 sal_Int32 start, end, pos;
140 pos = start = end = startPos;
141
142 while (pos < endPos) {
143 end += getACell(str, start, endPos);
144 while (pos < end) {
145 nextCellIndex[pos] = end;
146 previousCellIndex[pos] = start;
147 pos++;
148 }
149 start = end;
150 }
151 }
152
153 } } } }
154