Branch data 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 : :
21 : : #include <assert.h>
22 : : #include <textconversion.hxx>
23 : : #include <com/sun/star/i18n/TextConversionType.hpp>
24 : : #include <com/sun/star/i18n/TextConversionOption.hpp>
25 : : #include <com/sun/star/linguistic2/ConversionDirection.hpp>
26 : : #include <com/sun/star/linguistic2/ConversionDictionaryType.hpp>
27 : : #include <comphelper/string.hxx>
28 : :
29 : : using namespace com::sun::star::lang;
30 : : using namespace com::sun::star::i18n;
31 : : using namespace com::sun::star::linguistic2;
32 : : using namespace com::sun::star::uno;
33 : :
34 : : using ::rtl::OUString;
35 : :
36 : : namespace com { namespace sun { namespace star { namespace i18n {
37 : :
38 : 0 : TextConversion_zh::TextConversion_zh( const Reference < XMultiServiceFactory >& xMSF )
39 : : {
40 : 0 : Reference < XInterface > xI;
41 [ # # ]: 0 : xI = xMSF->createInstance(
42 [ # # ][ # # ]: 0 : OUString("com.sun.star.linguistic2.ConversionDictionaryList"));
43 [ # # ]: 0 : if ( xI.is() )
44 [ # # ][ # # ]: 0 : xI->queryInterface( getCppuType((const Reference< XConversionDictionaryList>*)0) ) >>= xCDL;
[ # # ][ # # ]
45 : :
46 : 0 : implementationName = "com.sun.star.i18n.TextConversion_zh";
47 : 0 : }
48 : :
49 : 0 : sal_Unicode SAL_CALL getOneCharConversion(sal_Unicode ch, const sal_Unicode* Data, const sal_uInt16* Index)
50 : : {
51 [ # # ][ # # ]: 0 : if (Data && Index) {
52 : 0 : sal_Unicode address = Index[ch>>8];
53 [ # # ]: 0 : if (address != 0xFFFF)
54 : 0 : address = Data[address + (ch & 0xFF)];
55 [ # # ]: 0 : return (address != 0xFFFF) ? address : ch;
56 : : } else {
57 : 0 : return ch;
58 : : }
59 : : }
60 : :
61 : : #ifdef DISABLE_DYNLOADING
62 : :
63 : : extern "C" {
64 : :
65 : : const sal_Unicode* getSTC_CharData_T2S();
66 : : const sal_uInt16* getSTC_CharIndex_T2S();
67 : : const sal_Unicode* getSTC_CharData_S2V();
68 : : const sal_uInt16* getSTC_CharIndex_S2V();
69 : : const sal_Unicode* getSTC_CharData_S2T();
70 : : const sal_uInt16* getSTC_CharIndex_S2T();
71 : :
72 : : const sal_Unicode *getSTC_WordData(sal_Int32&);
73 : :
74 : : const sal_uInt16 *getSTC_WordIndex_T2S(sal_Int32&);
75 : : const sal_uInt16 *getSTC_WordEntry_T2S();
76 : : const sal_uInt16 *getSTC_WordIndex_S2T(sal_Int32&);
77 : : const sal_uInt16 *getSTC_WordEntry_S2T();
78 : :
79 : : }
80 : :
81 : : #endif
82 : :
83 : : OUString SAL_CALL
84 : 0 : TextConversion_zh::getCharConversion(const OUString& aText, sal_Int32 nStartPos, sal_Int32 nLength, sal_Bool toSChinese, sal_Int32 nConversionOptions)
85 : : {
86 : : const sal_Unicode *Data;
87 : : const sal_uInt16 *Index;
88 : :
89 : : #ifndef DISABLE_DYNLOADING
90 [ # # ]: 0 : if (toSChinese) {
91 : 0 : Data = ((const sal_Unicode* (*)())getFunctionBySymbol("getSTC_CharData_T2S"))();
92 : 0 : Index = ((const sal_uInt16* (*)())getFunctionBySymbol("getSTC_CharIndex_T2S"))();
93 [ # # ]: 0 : } else if (nConversionOptions & TextConversionOption::USE_CHARACTER_VARIANTS) {
94 : 0 : Data = ((const sal_Unicode* (*)())getFunctionBySymbol("getSTC_CharData_S2V"))();
95 : 0 : Index = ((const sal_uInt16* (*)())getFunctionBySymbol("getSTC_CharIndex_S2V"))();
96 : : } else {
97 : 0 : Data = ((const sal_Unicode* (*)())getFunctionBySymbol("getSTC_CharData_S2T"))();
98 : 0 : Index = ((const sal_uInt16* (*)())getFunctionBySymbol("getSTC_CharIndex_S2T"))();
99 : : }
100 : : #else
101 : : if (toSChinese) {
102 : : Data = getSTC_CharData_T2S();
103 : : Index = getSTC_CharIndex_T2S();
104 : : } else if (nConversionOptions & TextConversionOption::USE_CHARACTER_VARIANTS) {
105 : : Data = getSTC_CharData_S2V();
106 : : Index = getSTC_CharIndex_S2V();
107 : : } else {
108 : : Data = getSTC_CharData_S2T();
109 : : Index = getSTC_CharIndex_S2T();
110 : : }
111 : : #endif
112 : :
113 : 0 : rtl_uString * newStr = comphelper::string::rtl_uString_alloc(nLength);
114 [ # # ]: 0 : for (sal_Int32 i = 0; i < nLength; i++)
115 : : newStr->buffer[i] =
116 : 0 : getOneCharConversion(aText[nStartPos+i], Data, Index);
117 : 0 : return OUString(newStr, SAL_NO_ACQUIRE); //take ownership
118 : : }
119 : :
120 : : OUString SAL_CALL
121 : 0 : TextConversion_zh::getWordConversion(const OUString& aText, sal_Int32 nStartPos, sal_Int32 nLength, sal_Bool toSChinese, sal_Int32 nConversionOptions, Sequence<sal_Int32>& offset)
122 : : {
123 : 0 : sal_Int32 dictLen = 0;
124 : 0 : sal_Int32 maxLen = 0;
125 : : const sal_uInt16 *index;
126 : : const sal_uInt16 *entry;
127 : : const sal_Unicode *charData;
128 : : const sal_uInt16 *charIndex;
129 : 0 : sal_Bool one2one=sal_True;
130 : :
131 : : #ifndef DISABLE_DYNLOADING
132 [ # # ][ # # ]: 0 : const sal_Unicode *wordData = ((const sal_Unicode* (*)(sal_Int32&)) getFunctionBySymbol("getSTC_WordData"))(dictLen);
133 [ # # ]: 0 : if (toSChinese) {
134 [ # # ][ # # ]: 0 : index = ((const sal_uInt16* (*)(sal_Int32&)) getFunctionBySymbol("getSTC_WordIndex_T2S"))(maxLen);
135 [ # # ][ # # ]: 0 : entry = ((const sal_uInt16* (*)()) getFunctionBySymbol("getSTC_WordEntry_T2S"))();
136 [ # # ][ # # ]: 0 : charData = ((const sal_Unicode* (*)()) getFunctionBySymbol("getSTC_CharData_T2S"))();
137 [ # # ][ # # ]: 0 : charIndex = ((const sal_uInt16* (*)()) getFunctionBySymbol("getSTC_CharIndex_T2S"))();
138 : : } else {
139 [ # # ][ # # ]: 0 : index = ((const sal_uInt16* (*)(sal_Int32&)) getFunctionBySymbol("getSTC_WordIndex_S2T"))(maxLen);
140 [ # # ][ # # ]: 0 : entry = ((const sal_uInt16* (*)()) getFunctionBySymbol("getSTC_WordEntry_S2T"))();
141 [ # # ]: 0 : if (nConversionOptions & TextConversionOption::USE_CHARACTER_VARIANTS) {
142 [ # # ][ # # ]: 0 : charData = ((const sal_Unicode* (*)()) getFunctionBySymbol("getSTC_CharData_S2V"))();
143 [ # # ][ # # ]: 0 : charIndex = ((const sal_uInt16* (*)()) getFunctionBySymbol("getSTC_CharIndex_S2V"))();
144 : : } else {
145 [ # # ][ # # ]: 0 : charData = ((const sal_Unicode* (*)()) getFunctionBySymbol("getSTC_CharData_S2T"))();
146 [ # # ][ # # ]: 0 : charIndex = ((const sal_uInt16* (*)()) getFunctionBySymbol("getSTC_CharIndex_S2T"))();
147 : : }
148 : : }
149 : : #else
150 : : const sal_Unicode *wordData = getSTC_WordData(dictLen);
151 : : if (toSChinese) {
152 : : index = getSTC_WordIndex_T2S(maxLen);
153 : : entry = getSTC_WordEntry_T2S();
154 : : charData = getSTC_CharData_T2S();
155 : : charIndex = getSTC_CharIndex_T2S();
156 : : } else {
157 : : index = getSTC_WordIndex_S2T(maxLen);
158 : : entry = getSTC_WordEntry_S2T();
159 : : if (nConversionOptions & TextConversionOption::USE_CHARACTER_VARIANTS) {
160 : : charData = getSTC_CharData_S2V();
161 : : charIndex = getSTC_CharIndex_S2V();
162 : : } else {
163 : : charData = getSTC_CharData_S2T();
164 : : charIndex = getSTC_CharIndex_S2T();
165 : : }
166 : : }
167 : : #endif
168 : :
169 [ # # ][ # # ]: 0 : if ((!wordData || !index || !entry) && !xCDL.is()) // no word mapping defined, do char2char conversion.
[ # # ][ # # ]
[ # # ]
170 [ # # ]: 0 : return getCharConversion(aText, nStartPos, nLength, toSChinese, nConversionOptions);
171 : :
172 [ # # ]: 0 : sal_Unicode *newStr = new sal_Unicode[nLength * 2 + 1];
173 : 0 : sal_Int32 currPos = 0, count = 0;
174 [ # # ]: 0 : while (currPos < nLength) {
175 : 0 : sal_Int32 len = nLength - currPos;
176 : 0 : sal_Bool found = sal_False;
177 [ # # ]: 0 : if (len > maxLen)
178 : 0 : len = maxLen;
179 [ # # ][ # # ]: 0 : for (; len > 0 && ! found; len--) {
[ # # ]
180 : 0 : OUString word = aText.copy(nStartPos + currPos, len);
181 : 0 : sal_Int32 current = 0;
182 : : // user dictionary
183 [ # # ]: 0 : if (xCDL.is()) {
184 [ # # ]: 0 : Sequence < OUString > conversions;
185 : : try {
186 [ # # ]: 0 : conversions = xCDL->queryConversions(word, 0, len,
187 : : aLocale, ConversionDictionaryType::SCHINESE_TCHINESE,
188 : : /*toSChinese ?*/ ConversionDirection_FROM_LEFT /*: ConversionDirection_FROM_RIGHT*/,
189 [ # # ][ # # ]: 0 : nConversionOptions);
[ # # ]
190 : : }
191 [ # # # # ]: 0 : catch ( NoSupportException & ) {
192 : : // clear reference (when there is no user dictionary) in order
193 : : // to not always have to catch this exception again
194 : : // in further calls. (save time)
195 [ # # ]: 0 : xCDL = 0;
196 : : }
197 [ # # ]: 0 : catch (...) {
198 : : // catch all other exceptions to allow
199 : : // querying the system dictionary in the next line
200 : : }
201 [ # # ]: 0 : if (conversions.getLength() > 0) {
202 [ # # ]: 0 : if (offset.getLength() > 0) {
203 [ # # ][ # # ]: 0 : if (word.getLength() != conversions[0].getLength())
204 : 0 : one2one=sal_False;
205 [ # # ][ # # ]: 0 : while (current < conversions[0].getLength()) {
206 [ # # ]: 0 : offset[count] = nStartPos + currPos + (current *
207 [ # # ]: 0 : word.getLength() / conversions[0].getLength());
208 [ # # ]: 0 : newStr[count++] = conversions[0][current++];
209 : : }
210 : : // offset[count-1] = nStartPos + currPos + word.getLength() - 1;
211 : : } else {
212 [ # # ][ # # ]: 0 : while (current < conversions[0].getLength())
213 [ # # ]: 0 : newStr[count++] = conversions[0][current++];
214 : : }
215 : 0 : currPos += word.getLength();
216 : 0 : found = sal_True;
217 [ # # ]: 0 : }
218 : : }
219 : :
220 [ # # ][ # # ]: 0 : if (!found && index[len+1] - index[len] > 0) {
221 : 0 : sal_Int32 bottom = (sal_Int32) index[len];
222 : 0 : sal_Int32 top = (sal_Int32) index[len+1] - 1;
223 : :
224 [ # # ][ # # ]: 0 : while (bottom <= top && !found) {
[ # # ]
225 : 0 : current = (top + bottom) / 2;
226 : 0 : const sal_Int32 result = word.compareTo(wordData + entry[current]);
227 [ # # ]: 0 : if (result < 0)
228 : 0 : top = current - 1;
229 [ # # ]: 0 : else if (result > 0)
230 : 0 : bottom = current + 1;
231 : : else {
232 [ # # ]: 0 : if (toSChinese) // Traditionary/Simplified conversion,
233 [ # # ][ # # ]: 0 : for (current = entry[current]-1; current > 0 && wordData[current-1]; current--) ;
[ # # ]
234 : : else // Simplified/Traditionary conversion, forwards search for next word
235 : 0 : current = entry[current] + word.getLength() + 1;
236 : 0 : sal_Int32 start=current;
237 [ # # ]: 0 : if (offset.getLength() > 0) {
238 [ # # ]: 0 : if (word.getLength() != OUString(&wordData[current]).getLength())
239 : 0 : one2one=sal_False;
240 : 0 : sal_Int32 convertedLength=OUString(&wordData[current]).getLength();
241 [ # # ]: 0 : while (wordData[current]) {
242 [ # # ]: 0 : offset[count]=nStartPos + currPos + ((current-start) *
243 : 0 : word.getLength() / convertedLength);
244 : 0 : newStr[count++] = wordData[current++];
245 : : }
246 : : // offset[count-1]=nStartPos + currPos + word.getLength() - 1;
247 : : } else {
248 [ # # ]: 0 : while (wordData[current])
249 : 0 : newStr[count++] = wordData[current++];
250 : : }
251 : 0 : currPos += word.getLength();
252 : 0 : found = sal_True;
253 : : }
254 : : }
255 : : }
256 : 0 : }
257 [ # # ]: 0 : if (!found) {
258 [ # # ]: 0 : if (offset.getLength() > 0)
259 [ # # ]: 0 : offset[count]=nStartPos+currPos;
260 : 0 : newStr[count++] =
261 : 0 : getOneCharConversion(aText[nStartPos+currPos], charData, charIndex);
262 : 0 : currPos++;
263 : : }
264 : : }
265 [ # # ]: 0 : if (offset.getLength() > 0)
266 [ # # ][ # # ]: 0 : offset.realloc(one2one ? 0 : count);
267 : 0 : OUString aRet(newStr, count);
268 [ # # ]: 0 : delete[] newStr;
269 : 0 : return aRet;
270 : : }
271 : :
272 : : TextConversionResult SAL_CALL
273 : 0 : TextConversion_zh::getConversions( const OUString& aText, sal_Int32 nStartPos, sal_Int32 nLength,
274 : : const Locale& rLocale, sal_Int16 nConversionType, sal_Int32 nConversionOptions)
275 : : throw( RuntimeException, IllegalArgumentException, NoSupportException )
276 : : {
277 : 0 : TextConversionResult result;
278 : :
279 [ # # ]: 0 : result.Candidates.realloc(1);
280 [ # # ][ # # ]: 0 : result.Candidates[0] = getConversion( aText, nStartPos, nLength, rLocale, nConversionType, nConversionOptions);
281 : 0 : result.Boundary.startPos = nStartPos;
282 : 0 : result.Boundary.endPos = nStartPos + nLength;
283 : :
284 : 0 : return result;
285 : : }
286 : :
287 : : OUString SAL_CALL
288 : 0 : TextConversion_zh::getConversion( const OUString& aText, sal_Int32 nStartPos, sal_Int32 nLength,
289 : : const Locale& rLocale, sal_Int16 nConversionType, sal_Int32 nConversionOptions)
290 : : throw( RuntimeException, IllegalArgumentException, NoSupportException )
291 : : {
292 [ # # ][ # # ]: 0 : if (rLocale.Language == "zh" && ( nConversionType == TextConversionType::TO_SCHINESE || nConversionType == TextConversionType::TO_TCHINESE) ) {
[ # # ][ # # ]
293 : :
294 : 0 : aLocale=rLocale;
295 : 0 : sal_Bool toSChinese = nConversionType == TextConversionType::TO_SCHINESE;
296 : :
297 [ # # ]: 0 : if (nConversionOptions & TextConversionOption::CHARACTER_BY_CHARACTER)
298 : : // char to char dictionary
299 : 0 : return getCharConversion(aText, nStartPos, nLength, toSChinese, nConversionOptions);
300 : : else {
301 [ # # ]: 0 : Sequence <sal_Int32> offset;
302 : : // word to word dictionary
303 [ # # ][ # # ]: 0 : return getWordConversion(aText, nStartPos, nLength, toSChinese, nConversionOptions, offset);
304 : : }
305 : : } else
306 [ # # ]: 0 : throw NoSupportException(); // Conversion type is not supported in this service.
307 : : }
308 : :
309 : : OUString SAL_CALL
310 : 0 : TextConversion_zh::getConversionWithOffset( const OUString& aText, sal_Int32 nStartPos, sal_Int32 nLength,
311 : : const Locale& rLocale, sal_Int16 nConversionType, sal_Int32 nConversionOptions, Sequence<sal_Int32>& offset)
312 : : throw( RuntimeException, IllegalArgumentException, NoSupportException )
313 : : {
314 [ # # ][ # # ]: 0 : if (rLocale.Language == "zh" && ( nConversionType == TextConversionType::TO_SCHINESE || nConversionType == TextConversionType::TO_TCHINESE) ) {
[ # # ][ # # ]
315 : :
316 : 0 : aLocale=rLocale;
317 : 0 : sal_Bool toSChinese = nConversionType == TextConversionType::TO_SCHINESE;
318 : :
319 [ # # ]: 0 : if (nConversionOptions & TextConversionOption::CHARACTER_BY_CHARACTER) {
320 : 0 : offset.realloc(0);
321 : : // char to char dictionary
322 : 0 : return getCharConversion(aText, nStartPos, nLength, toSChinese, nConversionOptions);
323 : : } else {
324 [ # # ]: 0 : if (offset.getLength() < 2*nLength)
325 : 0 : offset.realloc(2*nLength);
326 : : // word to word dictionary
327 : 0 : return getWordConversion(aText, nStartPos, nLength, toSChinese, nConversionOptions, offset);
328 : : }
329 : : } else
330 [ # # ]: 0 : throw NoSupportException(); // Conversion type is not supported in this service.
331 : : }
332 : :
333 : : sal_Bool SAL_CALL
334 : 0 : TextConversion_zh::interactiveConversion( const Locale& /*rLocale*/, sal_Int16 /*nTextConversionType*/, sal_Int32 /*nTextConversionOptions*/ )
335 : : throw( RuntimeException, IllegalArgumentException, NoSupportException )
336 : : {
337 : 0 : return sal_False;
338 : : }
339 : :
340 : : } } } }
341 : :
342 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|