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 .
20 #include <breakiterator_th.hxx>
23 #include <string.h> // for memset
25 using namespace ::com::sun::star::uno
;
26 using namespace ::com::sun::star::lang
;
27 using namespace ::rtl
;
29 namespace com
{ namespace sun
{ namespace star
{ namespace i18n
{
31 // ----------------------------------------------------
32 // class Breakiterator_th
33 // ----------------------------------------------------;
34 BreakIterator_th::BreakIterator_th()
36 cBreakIterator
= "com.sun.star.i18n.BreakIterator_th";
37 wordRule
=lineRule
=NULL
;
40 BreakIterator_th::~BreakIterator_th()
44 #define SARA_AM 0x0E33
47 * cell composition states
50 #define ST_COM 1 // Compose the following character with leading char and display in the same cell
51 #define ST_NXT 2 // display the following character in the next cell
52 #define ST_NDP 3 // non-display
54 static const sal_Int16 thaiCompRel
[MAX_CT
][MAX_CT
] = {
55 // C N C L F F F B B B T A A A A A A
56 // T O O V V V V V V D O D D D V V V
57 // R N N 1 2 3 1 2 N 1 2 3 1 2 3
59 // 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
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
}, // CTRL 0
61 { 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
62 { 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
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
}, // LV 3
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
}, // FV1 4
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
}, // FV2 5
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
}, // FV3 6
67 { 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
68 { 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
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
}, // BD 9
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
}, // TONE 10
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
}, // AD1 11
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
}, // AD2 12
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
}, // AD3 13
74 { 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
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_NXT
, ST_NXT
, ST_NXT
, ST_NXT
, ST_NXT
}, // AV2 15
76 { 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
80 const sal_uInt32 is_ST_COM
= (1<<CT_CTRL
)|(1<<CT_NON
)|(1<<CT_CONS
)|(1<<CT_TONE
);
82 static sal_uInt16 SAL_CALL
getCombState(const sal_Unicode
*text
, sal_Int32 pos
)
84 sal_uInt16 ch1
= getCharType(text
[pos
]);
85 sal_uInt16 ch2
= getCharType(text
[pos
+1]);
87 if (text
[pos
+1] == SARA_AM
) {
88 if ((1 << ch1
) & is_ST_COM
)
94 return thaiCompRel
[ch1
][ch2
];
98 static sal_Int32 SAL_CALL
getACell(const sal_Unicode
*text
, sal_Int32 pos
, sal_Int32 len
)
101 for (; pos
+ 1 < len
&& getCombState(text
, pos
) == ST_COM
; curr
++, pos
++) {}
105 #define is_Thai(c) (0x0e00 <= c && c <= 0x0e7f) // Unicode definition for Thai
107 void SAL_CALL
BreakIterator_th::makeIndex(const OUString
& Text
, sal_Int32 nStartPos
)
108 throw(RuntimeException
)
110 if (Text
!= cachedText
) {
112 if (cellIndexSize
< cachedText
.getLength()) {
113 cellIndexSize
= cachedText
.getLength();
115 free(previousCellIndex
);
116 nextCellIndex
= (sal_Int32
*) calloc(cellIndexSize
, sizeof(sal_Int32
));
117 previousCellIndex
= (sal_Int32
*) calloc(cellIndexSize
, sizeof(sal_Int32
));
119 // reset nextCell for new Text
120 memset(nextCellIndex
, 0, cellIndexSize
* sizeof(sal_Int32
));
122 else if (nextCellIndex
[nStartPos
] > 0 || ! is_Thai(Text
[nStartPos
]))
125 const sal_Unicode
* str
= cachedText
.getStr();
126 sal_Int32 len
= cachedText
.getLength(), startPos
, endPos
;
128 startPos
= nStartPos
;
129 while (startPos
> 0 && is_Thai(str
[startPos
-1])) startPos
--;
130 endPos
= nStartPos
+1;
131 while (endPos
< len
&& is_Thai(str
[endPos
])) endPos
++;
133 sal_Int32 start
, end
, pos
;
134 pos
= start
= end
= startPos
;
136 while (pos
< endPos
) {
137 end
+= getACell(str
, start
, endPos
);
139 nextCellIndex
[pos
] = end
;
140 previousCellIndex
[pos
] = start
;
149 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */