merged tag ooo/OOO330_m14
[LibreOffice.git] / i18npool / source / breakiterator / breakiterator_th.cxx
blobcbbcd510379b1a3619464066d8f022942e368c14
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
11 * OpenOffice.org is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License version 3
13 * only, as published by the Free Software Foundation.
15 * OpenOffice.org is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License version 3 for more details
19 * (a copy is included in the LICENSE file that accompanied this code).
21 * You should have received a copy of the GNU Lesser General Public License
22 * version 3 along with OpenOffice.org. If not, see
23 * <http://www.openoffice.org/license.html>
24 * for a copy of the LGPLv3 License.
26 ************************************************************************/
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_i18npool.hxx"
30 #include <breakiterator_th.hxx>
31 #include <wtt.h>
33 #include <string.h> // for memset
35 using namespace ::com::sun::star::uno;
36 using namespace ::com::sun::star::lang;
37 using namespace ::rtl;
39 namespace com { namespace sun { namespace star { namespace i18n {
41 // ----------------------------------------------------
42 // class Breakiterator_th
43 // ----------------------------------------------------;
44 BreakIterator_th::BreakIterator_th()
46 cBreakIterator = "com.sun.star.i18n.BreakIterator_th";
47 wordRule=lineRule=NULL;
50 BreakIterator_th::~BreakIterator_th()
54 #define SARA_AM 0x0E33
57 * cell composition states
60 #define ST_COM 1 // Compose the following character with leading char and display in the same cell
61 #define ST_NXT 2 // display the following character in the next cell
62 #define ST_NDP 3 // non-display
64 static const sal_Int16 thaiCompRel[MAX_CT][MAX_CT] = {
65 // C N C L F F F B B B T A A A A A A
66 // T O O V V V V V V D O D D D V V V
67 // R N N 1 2 3 1 2 N 1 2 3 1 2 3
68 // L S E
69 // 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
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 }, // CTRL 0
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 }, // NON 1
72 { 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
73 { 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
74 { 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
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 }, // FV2 5
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 }, // FV3 6
77 { 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
78 { 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
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 }, // BD 9
80 { 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
81 { 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
82 { 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
83 { 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
84 { 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
85 { 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
86 { 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
90 const sal_uInt32 is_ST_COM = (1<<CT_CTRL)|(1<<CT_NON)|(1<<CT_CONS)|(1<<CT_TONE);
92 static sal_uInt16 SAL_CALL getCombState(const sal_Unicode *text, sal_Int32 pos)
94 sal_uInt16 ch1 = getCharType(text[pos]);
95 sal_uInt16 ch2 = getCharType(text[pos+1]);
97 if (text[pos+1] == SARA_AM) {
98 if ((1 << ch1) & is_ST_COM)
99 return ST_COM;
100 else
101 ch2 = CT_AD1;
104 return thaiCompRel[ch1][ch2];
108 static sal_Int32 SAL_CALL getACell(const sal_Unicode *text, sal_Int32 pos, sal_Int32 len)
110 sal_uInt32 curr = 1;
111 for (; pos + 1 < len && getCombState(text, pos) == ST_COM; curr++, pos++) {}
112 return curr;
115 #define is_Thai(c) (0x0e00 <= c && c <= 0x0e7f) // Unicode definition for Thai
117 void SAL_CALL BreakIterator_th::makeIndex(const OUString& Text, sal_Int32 nStartPos)
118 throw(RuntimeException)
120 if (Text != cachedText) {
121 cachedText = Text;
122 if (cellIndexSize < cachedText.getLength()) {
123 cellIndexSize = cachedText.getLength();
124 free(nextCellIndex);
125 free(previousCellIndex);
126 nextCellIndex = (sal_Int32*) calloc(cellIndexSize, sizeof(sal_Int32));
127 previousCellIndex = (sal_Int32*) calloc(cellIndexSize, sizeof(sal_Int32));
129 // reset nextCell for new Text
130 memset(nextCellIndex, 0, cellIndexSize * sizeof(sal_Int32));
132 else if (nextCellIndex[nStartPos] > 0 || ! is_Thai(Text[nStartPos]))
133 return;
135 const sal_Unicode* str = cachedText.getStr();
136 sal_Int32 len = cachedText.getLength(), startPos, endPos;
138 startPos = nStartPos;
139 while (startPos > 0 && is_Thai(str[startPos-1])) startPos--;
140 endPos = nStartPos+1;
141 while (endPos < len && is_Thai(str[endPos])) endPos++;
143 sal_Int32 start, end, pos;
144 pos = start = end = startPos;
146 while (pos < endPos) {
147 end += getACell(str, start, endPos);
148 while (pos < end) {
149 nextCellIndex[pos] = end;
150 previousCellIndex[pos] = start;
151 pos++;
153 start = end;
157 } } } }