Version 6.1.4.1, tag libreoffice-6.1.4.1
[LibreOffice.git] / i18npool / source / breakiterator / breakiterator_th.cxx
bloba5ab63e9a63f08c94e1bfa9dd85025a4cadf36fb
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
21 #include <com/sun/star/i18n/CharacterIteratorMode.hpp>
22 #include <breakiterator_th.hxx>
23 #include <wtt.h>
25 #include <string.h>
26 #include <algorithm>
28 using namespace ::com::sun::star;
29 using namespace ::com::sun::star::uno;
30 using namespace ::com::sun::star::i18n;
31 using namespace ::com::sun::star::lang;
33 namespace i18npool {
35 /**
36 * Constructor.
38 BreakIterator_th::BreakIterator_th() :
39 cachedText()
41 cBreakIterator = "com.sun.star.i18n.BreakIterator_th";
42 // to improve performance, alloc big enough memory in construct.
43 m_aNextCellIndex.assign(512, 0);
44 m_aPreviousCellIndex.assign(512, 0);
45 lineRule=nullptr;
48 /**
49 * Deconstructor.
51 BreakIterator_th::~BreakIterator_th()
55 sal_Int32 SAL_CALL BreakIterator_th::previousCharacters( const OUString& Text,
56 sal_Int32 nStartPos, const lang::Locale& rLocale,
57 sal_Int16 nCharacterIteratorMode, sal_Int32 nCount, sal_Int32& nDone )
59 if (nCharacterIteratorMode == CharacterIteratorMode::SKIPCELL ) {
60 nDone = 0;
61 if (nStartPos > 0) { // for others to skip cell.
62 makeIndex(Text, nStartPos);
64 if (m_aNextCellIndex[nStartPos-1] == 0) // not a CTL character
65 return BreakIterator_Unicode::previousCharacters(Text, nStartPos, rLocale,
66 nCharacterIteratorMode, nCount, nDone);
67 else while (nCount > 0 && m_aNextCellIndex[nStartPos - 1] > 0) {
68 nCount--; nDone++;
69 nStartPos = m_aPreviousCellIndex[nStartPos - 1];
71 } else
72 nStartPos = 0;
73 } else { // for BS to delete one char.
74 for (nDone = 0; nDone < nCount && nStartPos > 0; nDone++)
75 Text.iterateCodePoints(&nStartPos, -1);
78 return nStartPos;
81 sal_Int32 SAL_CALL BreakIterator_th::nextCharacters(const OUString& Text,
82 sal_Int32 nStartPos, const lang::Locale& rLocale,
83 sal_Int16 nCharacterIteratorMode, sal_Int32 nCount, sal_Int32& nDone)
85 sal_Int32 len = Text.getLength();
86 if (nCharacterIteratorMode == CharacterIteratorMode::SKIPCELL ) {
87 nDone = 0;
88 if (nStartPos < len) {
89 makeIndex(Text, nStartPos);
91 if (m_aNextCellIndex[nStartPos] == 0) // not a CTL character
92 return BreakIterator_Unicode::nextCharacters(Text, nStartPos, rLocale,
93 nCharacterIteratorMode, nCount, nDone);
94 else while (nCount > 0 && m_aNextCellIndex[nStartPos] > 0) {
95 nCount--; nDone++;
96 nStartPos = m_aNextCellIndex[nStartPos];
98 } else
99 nStartPos = len;
100 } else {
101 for (nDone = 0; nDone < nCount && nStartPos < Text.getLength(); nDone++)
102 Text.iterateCodePoints(&nStartPos);
105 return nStartPos;
108 // Make sure line is broken on cell boundary if we implement cell iterator.
109 LineBreakResults SAL_CALL BreakIterator_th::getLineBreak(
110 const OUString& Text, sal_Int32 nStartPos,
111 const lang::Locale& rLocale, sal_Int32 nMinBreakPos,
112 const LineBreakHyphenationOptions& hOptions,
113 const LineBreakUserOptions& bOptions )
115 LineBreakResults lbr = BreakIterator_Unicode::getLineBreak(Text, nStartPos,
116 rLocale, nMinBreakPos, hOptions, bOptions );
117 if (lbr.breakIndex < Text.getLength()) {
118 makeIndex(Text, lbr.breakIndex);
119 lbr.breakIndex = m_aPreviousCellIndex[ lbr.breakIndex ];
121 return lbr;
124 #define SARA_AM 0x0E33
127 * cell composition states
130 #define ST_COM 1 // Compose the following character with leading char and display in the same cell
131 #define ST_NXT 2 // display the following character in the next cell
132 #define ST_NDP 3 // non-display
134 static const sal_Int16 thaiCompRel[MAX_CT][MAX_CT] = {
135 // C N C L F F F B B B T A A A A A A
136 // T O O V V V V V V D O D D D V V V
137 // R N N 1 2 3 1 2 N 1 2 3 1 2 3
138 // L S E
139 // 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
140 { 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
141 { 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
142 { 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
143 { 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
144 { 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
145 { 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
146 { 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
147 { 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
148 { 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
149 { 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
150 { 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
151 { 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
152 { 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
153 { 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
154 { 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
155 { 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
156 { 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
160 const sal_uInt32 is_ST_COM = (1<<CT_CTRL)|(1<<CT_NON)|(1<<CT_CONS)|(1<<CT_TONE);
162 static sal_uInt16 getCombState(const sal_Unicode *text, sal_Int32 pos)
164 sal_uInt16 ch1 = getCharType(text[pos]);
165 sal_uInt16 ch2 = getCharType(text[pos+1]);
167 if (text[pos+1] == SARA_AM) {
168 if ((1 << ch1) & is_ST_COM)
169 return ST_COM;
170 else
171 ch2 = CT_AD1;
174 return thaiCompRel[ch1][ch2];
178 static sal_Int32 getACell(const sal_Unicode *text, sal_Int32 pos, sal_Int32 len)
180 sal_uInt32 curr = 1;
181 for (; pos + 1 < len && getCombState(text, pos) == ST_COM; curr++, pos++) {}
182 return curr;
185 #define is_Thai(c) (0x0e00 <= c && c <= 0x0e7f) // Unicode definition for Thai
187 void BreakIterator_th::makeIndex(const OUString& Text, sal_Int32 const nStartPos)
189 if (Text != cachedText) {
190 cachedText = Text;
191 if (m_aNextCellIndex.size() < size_t(cachedText.getLength())) {
192 m_aNextCellIndex.resize(cachedText.getLength());
193 m_aPreviousCellIndex.resize(cachedText.getLength());
195 // reset nextCell for new Text
196 m_aNextCellIndex.assign(cachedText.getLength(), 0);
198 else if (nStartPos >= Text.getLength() || m_aNextCellIndex[nStartPos] > 0
199 || !is_Thai(Text[nStartPos]))
200 return;
202 const sal_Unicode* str = cachedText.getStr();
203 sal_Int32 const len = cachedText.getLength();
205 sal_Int32 startPos = nStartPos;
206 while (startPos > 0 && is_Thai(str[startPos-1])) startPos--;
207 sal_Int32 endPos = nStartPos;
208 while (endPos < len && is_Thai(str[endPos])) endPos++;
210 sal_Int32 start, end, pos;
211 pos = start = end = startPos;
213 assert(size_t(endPos) <= m_aNextCellIndex.size());
214 while (pos < endPos) {
215 end += getACell(str, start, endPos);
216 assert(size_t(end) <= m_aNextCellIndex.size());
217 while (pos < end) {
218 m_aNextCellIndex[pos] = end;
219 m_aPreviousCellIndex[pos] = start;
220 pos++;
222 start = end;
228 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */