1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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>
27 using namespace ::com::sun::star
;
28 using namespace ::com::sun::star::uno
;
29 using namespace ::com::sun::star::i18n
;
30 using namespace ::com::sun::star::lang
;
37 BreakIterator_th::BreakIterator_th() :
40 cBreakIterator
= "com.sun.star.i18n.BreakIterator_th";
41 // to improve performance, alloc big enough memory in construct.
42 m_aNextCellIndex
.assign(512, 0);
43 m_aPreviousCellIndex
.assign(512, 0);
50 BreakIterator_th::~BreakIterator_th()
54 sal_Int32 SAL_CALL
BreakIterator_th::previousCharacters( const OUString
& Text
,
55 sal_Int32 nStartPos
, const lang::Locale
& rLocale
,
56 sal_Int16 nCharacterIteratorMode
, sal_Int32 nCount
, sal_Int32
& nDone
)
58 if (nCharacterIteratorMode
== CharacterIteratorMode::SKIPCELL
) {
60 if (nStartPos
> 0) { // for others to skip cell.
61 makeIndex(Text
, nStartPos
);
63 if (m_aNextCellIndex
[nStartPos
-1] == 0) // not a CTL character
64 return BreakIterator_Unicode::previousCharacters(Text
, nStartPos
, rLocale
,
65 nCharacterIteratorMode
, nCount
, nDone
);
66 else while (nCount
> 0 && m_aNextCellIndex
[nStartPos
- 1] > 0) {
68 nStartPos
= m_aPreviousCellIndex
[nStartPos
- 1];
72 } else { // for BS to delete one char.
73 for (nDone
= 0; nDone
< nCount
&& nStartPos
> 0; nDone
++)
74 Text
.iterateCodePoints(&nStartPos
, -1);
80 sal_Int32 SAL_CALL
BreakIterator_th::nextCharacters(const OUString
& Text
,
81 sal_Int32 nStartPos
, const lang::Locale
& rLocale
,
82 sal_Int16 nCharacterIteratorMode
, sal_Int32 nCount
, sal_Int32
& nDone
)
84 sal_Int32 len
= Text
.getLength();
85 if (nCharacterIteratorMode
== CharacterIteratorMode::SKIPCELL
) {
87 if (nStartPos
< len
) {
88 makeIndex(Text
, nStartPos
);
90 if (m_aNextCellIndex
[nStartPos
] == 0) // not a CTL character
91 return BreakIterator_Unicode::nextCharacters(Text
, nStartPos
, rLocale
,
92 nCharacterIteratorMode
, nCount
, nDone
);
93 else while (nCount
> 0 && m_aNextCellIndex
[nStartPos
] > 0) {
95 nStartPos
= m_aNextCellIndex
[nStartPos
];
100 for (nDone
= 0; nDone
< nCount
&& nStartPos
< Text
.getLength(); nDone
++)
101 Text
.iterateCodePoints(&nStartPos
);
107 // Make sure line is broken on cell boundary if we implement cell iterator.
108 LineBreakResults SAL_CALL
BreakIterator_th::getLineBreak(
109 const OUString
& Text
, sal_Int32 nStartPos
,
110 const lang::Locale
& rLocale
, sal_Int32 nMinBreakPos
,
111 const LineBreakHyphenationOptions
& hOptions
,
112 const LineBreakUserOptions
& bOptions
)
114 LineBreakResults lbr
= BreakIterator_Unicode::getLineBreak(Text
, nStartPos
,
115 rLocale
, nMinBreakPos
, hOptions
, bOptions
);
116 if (lbr
.breakIndex
< Text
.getLength()) {
117 makeIndex(Text
, lbr
.breakIndex
);
118 lbr
.breakIndex
= m_aPreviousCellIndex
[ lbr
.breakIndex
];
123 #define SARA_AM 0x0E33
126 * cell composition states
129 #define ST_COM 1 // Compose the following character with leading char and display in the same cell
130 #define ST_NXT 2 // display the following character in the next cell
131 #define ST_NDP 3 // non-display
133 static const sal_Int16 thaiCompRel
[MAX_CT
][MAX_CT
] = {
134 // C N C L F F F B B B T A A A A A A
135 // T O O V V V V V V D O D D D V V V
136 // R N N 1 2 3 1 2 N 1 2 3 1 2 3
138 // 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
139 { 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
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
}, // NON 1
141 { 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
142 { 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
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
}, // FV1 4
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
}, // FV2 5
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
}, // FV3 6
146 { 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
147 { 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
148 { 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
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
}, // TONE 10
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
}, // AD1 11
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
}, // AD2 12
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
}, // AD3 13
153 { 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
154 { 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
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_COM
, ST_NXT
, ST_NXT
, ST_NXT
, ST_NXT
} // AV3 16
159 const sal_uInt32 is_ST_COM
= (1<<CT_CTRL
)|(1<<CT_NON
)|(1<<CT_CONS
)|(1<<CT_TONE
);
161 static sal_uInt16
getCombState(const sal_Unicode
*text
, sal_Int32 pos
)
163 sal_uInt16 ch1
= getCharType(text
[pos
]);
164 sal_uInt16 ch2
= getCharType(text
[pos
+1]);
166 if (text
[pos
+1] == SARA_AM
) {
167 if ((1 << ch1
) & is_ST_COM
)
173 return thaiCompRel
[ch1
][ch2
];
177 static sal_Int32
getACell(const sal_Unicode
*text
, sal_Int32 pos
, sal_Int32 len
)
180 for (; pos
+ 1 < len
&& getCombState(text
, pos
) == ST_COM
; curr
++, pos
++) {}
184 #define is_Thai(c) (0x0e00 <= c && c <= 0x0e7f) // Unicode definition for Thai
186 void BreakIterator_th::makeIndex(const OUString
& Text
, sal_Int32
const nStartPos
)
188 if (Text
!= cachedText
) {
190 if (m_aNextCellIndex
.size() < size_t(cachedText
.getLength())) {
191 m_aNextCellIndex
.resize(cachedText
.getLength());
192 m_aPreviousCellIndex
.resize(cachedText
.getLength());
194 // reset nextCell for new Text
195 m_aNextCellIndex
.assign(cachedText
.getLength(), 0);
197 else if (nStartPos
>= Text
.getLength() || m_aNextCellIndex
[nStartPos
] > 0
198 || !is_Thai(Text
[nStartPos
]))
201 const sal_Unicode
* str
= cachedText
.getStr();
202 sal_Int32
const len
= cachedText
.getLength();
204 sal_Int32 startPos
= nStartPos
;
205 while (startPos
> 0 && is_Thai(str
[startPos
-1])) startPos
--;
206 sal_Int32 endPos
= nStartPos
;
207 while (endPos
< len
&& is_Thai(str
[endPos
])) endPos
++;
209 sal_Int32 start
, end
, pos
;
210 pos
= start
= end
= startPos
;
212 assert(size_t(endPos
) <= m_aNextCellIndex
.size());
213 while (pos
< endPos
) {
214 end
+= getACell(str
, start
, endPos
);
215 assert(size_t(end
) <= m_aNextCellIndex
.size());
217 m_aNextCellIndex
[pos
] = end
;
218 m_aPreviousCellIndex
[pos
] = start
;
227 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */