1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: breakiterator_th.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_i18npool.hxx"
33 #include <breakiterator_th.hxx>
36 #include <string.h> // for memset
38 using namespace ::com::sun::star::uno
;
39 using namespace ::com::sun::star::lang
;
40 using namespace ::rtl
;
42 namespace com
{ namespace sun
{ namespace star
{ namespace i18n
{
44 // ----------------------------------------------------
45 // class Breakiterator_th
46 // ----------------------------------------------------;
47 BreakIterator_th::BreakIterator_th()
49 cBreakIterator
= "com.sun.star.i18n.BreakIterator_th";
50 wordRule
=lineRule
=NULL
;
53 BreakIterator_th::~BreakIterator_th()
57 #define SARA_AM 0x0E33
60 * cell composition states
63 #define ST_COM 1 // Compose the following character with leading char and display in the same cell
64 #define ST_NXT 2 // display the following character in the next cell
65 #define ST_NDP 3 // non-display
67 static const sal_Int16 thaiCompRel
[MAX_CT
][MAX_CT
] = {
68 // C N C L F F F B B B T A A A A A A
69 // T O O V V V V V V D O D D D V V V
70 // R N N 1 2 3 1 2 N 1 2 3 1 2 3
72 // 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
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
}, // CTRL 0
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
}, // NON 1
75 { 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
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
}, // LV 3
77 { 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
78 { 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
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
}, // FV3 6
80 { 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
81 { 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
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
}, // BD 9
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
}, // TONE 10
84 { 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
85 { 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
86 { 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
87 { 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
88 { 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
89 { 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
93 const sal_uInt32 is_ST_COM
= (1<<CT_CTRL
)|(1<<CT_NON
)|(1<<CT_CONS
)|(1<<CT_TONE
);
95 static sal_uInt16 SAL_CALL
getCombState(const sal_Unicode
*text
, sal_Int32 pos
)
97 sal_uInt16 ch1
= getCharType(text
[pos
]);
98 sal_uInt16 ch2
= getCharType(text
[pos
+1]);
100 if (text
[pos
+1] == SARA_AM
) {
101 if ((1 << ch1
) & is_ST_COM
)
107 return thaiCompRel
[ch1
][ch2
];
111 static sal_Int32 SAL_CALL
getACell(const sal_Unicode
*text
, sal_Int32 pos
, sal_Int32 len
)
114 for (; pos
+ 1 < len
&& getCombState(text
, pos
) == ST_COM
; curr
++, pos
++) {}
118 #define is_Thai(c) (0x0e00 <= c && c <= 0x0e7f) // Unicode definition for Thai
120 void SAL_CALL
BreakIterator_th::makeIndex(const OUString
& Text
, sal_Int32 nStartPos
)
121 throw(RuntimeException
)
123 if (Text
!= cachedText
) {
125 if (cellIndexSize
< cachedText
.getLength()) {
126 cellIndexSize
= cachedText
.getLength();
128 free(previousCellIndex
);
129 nextCellIndex
= (sal_Int32
*) calloc(cellIndexSize
, sizeof(sal_Int32
));
130 previousCellIndex
= (sal_Int32
*) calloc(cellIndexSize
, sizeof(sal_Int32
));
132 // reset nextCell for new Text
133 memset(nextCellIndex
, 0, cellIndexSize
* sizeof(sal_Int32
));
135 else if (nextCellIndex
[nStartPos
] > 0 || ! is_Thai(Text
[nStartPos
]))
138 const sal_Unicode
* str
= cachedText
.getStr();
139 sal_Int32 len
= cachedText
.getLength(), startPos
, endPos
;
141 startPos
= nStartPos
;
142 while (startPos
> 0 && is_Thai(str
[startPos
-1])) startPos
--;
143 endPos
= nStartPos
+1;
144 while (endPos
< len
&& is_Thai(str
[endPos
])) endPos
++;
146 sal_Int32 start
, end
, pos
;
147 pos
= start
= end
= startPos
;
149 while (pos
< endPos
) {
150 end
+= getACell(str
, start
, endPos
);
152 nextCellIndex
[pos
] = end
;
153 previousCellIndex
[pos
] = start
;