Line data Source code
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 : #include <breakiterator_th.hxx>
21 : #include <wtt.h>
22 :
23 : #include <string.h>
24 :
25 : using namespace ::com::sun::star::uno;
26 : using namespace ::com::sun::star::lang;
27 :
28 : namespace com { namespace sun { namespace star { namespace i18n {
29 :
30 :
31 : // class Breakiterator_th
32 : // ----------------------------------------------------;
33 2 : BreakIterator_th::BreakIterator_th()
34 : {
35 2 : cBreakIterator = "com.sun.star.i18n.BreakIterator_th";
36 2 : lineRule=NULL;
37 2 : }
38 :
39 4 : BreakIterator_th::~BreakIterator_th()
40 : {
41 4 : }
42 :
43 : #define SARA_AM 0x0E33
44 :
45 : /*
46 : * cell composition states
47 : */
48 :
49 : #define ST_COM 1 // Compose the following character with leading char and display in the same cell
50 : #define ST_NXT 2 // display the following character in the next cell
51 : #define ST_NDP 3 // non-display
52 :
53 : static const sal_Int16 thaiCompRel[MAX_CT][MAX_CT] = {
54 : // C N C L F F F B B B T A A A A A A
55 : // T O O V V V V V V D O D D D V V V
56 : // R N N 1 2 3 1 2 N 1 2 3 1 2 3
57 : // L S E
58 : // 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
59 : { 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
60 : { 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
61 : { 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
62 : { 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
63 : { 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
64 : { 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
65 : { 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
66 : { 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
67 : { 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
68 : { 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
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 }, // TONE 10
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 }, // AD1 11
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 }, // AD2 12
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 }, // AD3 13
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 }, // AV1 14
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 }, // AV2 15
75 : { 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
76 :
77 : };
78 :
79 : const sal_uInt32 is_ST_COM = (1<<CT_CTRL)|(1<<CT_NON)|(1<<CT_CONS)|(1<<CT_TONE);
80 :
81 0 : static sal_uInt16 SAL_CALL getCombState(const sal_Unicode *text, sal_Int32 pos)
82 : {
83 0 : sal_uInt16 ch1 = getCharType(text[pos]);
84 0 : sal_uInt16 ch2 = getCharType(text[pos+1]);
85 :
86 0 : if (text[pos+1] == SARA_AM) {
87 0 : if ((1 << ch1) & is_ST_COM)
88 0 : return ST_COM;
89 : else
90 0 : ch2 = CT_AD1;
91 : }
92 :
93 0 : return thaiCompRel[ch1][ch2];
94 : }
95 :
96 :
97 0 : static sal_Int32 SAL_CALL getACell(const sal_Unicode *text, sal_Int32 pos, sal_Int32 len)
98 : {
99 0 : sal_uInt32 curr = 1;
100 0 : for (; pos + 1 < len && getCombState(text, pos) == ST_COM; curr++, pos++) {}
101 0 : return curr;
102 : }
103 :
104 : #define is_Thai(c) (0x0e00 <= c && c <= 0x0e7f) // Unicode definition for Thai
105 :
106 0 : void SAL_CALL BreakIterator_th::makeIndex(const OUString& Text, sal_Int32 nStartPos)
107 : throw(RuntimeException)
108 : {
109 0 : if (Text != cachedText) {
110 0 : cachedText = Text;
111 0 : if (cellIndexSize < cachedText.getLength()) {
112 0 : cellIndexSize = cachedText.getLength();
113 0 : free(nextCellIndex);
114 0 : free(previousCellIndex);
115 0 : nextCellIndex = (sal_Int32*) calloc(cellIndexSize, sizeof(sal_Int32));
116 0 : previousCellIndex = (sal_Int32*) calloc(cellIndexSize, sizeof(sal_Int32));
117 : }
118 : // reset nextCell for new Text
119 0 : memset(nextCellIndex, 0, cellIndexSize * sizeof(sal_Int32));
120 : }
121 0 : else if (nStartPos >= Text.getLength() || nextCellIndex[nStartPos] > 0
122 0 : || !is_Thai(Text[nStartPos]))
123 0 : return;
124 :
125 0 : const sal_Unicode* str = cachedText.getStr();
126 0 : sal_Int32 len = cachedText.getLength(), startPos, endPos;
127 :
128 0 : startPos = nStartPos;
129 0 : while (startPos > 0 && is_Thai(str[startPos-1])) startPos--;
130 0 : endPos = nStartPos+1;
131 0 : while (endPos < len && is_Thai(str[endPos])) endPos++;
132 :
133 : sal_Int32 start, end, pos;
134 0 : pos = start = end = startPos;
135 :
136 0 : while (pos < endPos) {
137 0 : end += getACell(str, start, endPos);
138 0 : while (pos < end) {
139 0 : nextCellIndex[pos] = end;
140 0 : previousCellIndex[pos] = start;
141 0 : pos++;
142 : }
143 0 : start = end;
144 : }
145 : }
146 :
147 : } } } }
148 :
149 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|