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 : : #include <assert.h>
21 : : #include <textconversion.hxx>
22 : : #include <com/sun/star/i18n/TextConversionType.hpp>
23 : : #include <com/sun/star/i18n/TextConversionOption.hpp>
24 : : #include <com/sun/star/linguistic2/ConversionDirection.hpp>
25 : : #include <com/sun/star/linguistic2/ConversionDictionaryType.hpp>
26 : : #include <rtl/ustrbuf.hxx>
27 : : #include <unicode/uchar.h>
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 : : using ::rtl::OUStringBuffer;
36 : :
37 : : namespace com { namespace sun { namespace star { namespace i18n {
38 : :
39 : : #define SCRIPT_OTHERS 0
40 : : #define SCRIPT_HANJA 1
41 : : #define SCRIPT_HANGUL 2
42 : :
43 : 0 : TextConversion_ko::TextConversion_ko( const Reference < XMultiServiceFactory >& xMSF )
44 : : {
45 : 0 : Reference < XInterface > xI;
46 : :
47 [ # # ]: 0 : xI = xMSF->createInstance(
48 [ # # ][ # # ]: 0 : OUString("com.sun.star.i18n.ConversionDictionary_ko"));
49 : :
50 [ # # ]: 0 : if ( xI.is() )
51 [ # # ][ # # ]: 0 : xI->queryInterface( getCppuType((const Reference< XConversionDictionary>*)0) ) >>= xCD;
[ # # ][ # # ]
52 : :
53 [ # # ]: 0 : xI = xMSF->createInstance(
54 [ # # ][ # # ]: 0 : OUString("com.sun.star.linguistic2.ConversionDictionaryList"));
55 : :
56 [ # # ]: 0 : if ( xI.is() )
57 [ # # ][ # # ]: 0 : xI->queryInterface( getCppuType((const Reference< XConversionDictionaryList>*)0) ) >>= xCDL;
[ # # ][ # # ]
58 : :
59 : 0 : maxLeftLength = maxRightLength = 1;
60 : :
61 : : // get maximum length of word in dictionary
62 [ # # ]: 0 : if (xCDL.is()) {
63 : : Locale loc(OUString("ko"),
64 : : OUString("KR"),
65 : 0 : OUString());
66 [ # # ]: 0 : maxLeftLength = xCDL->queryMaxCharCount(loc,
67 : : ConversionDictionaryType::HANGUL_HANJA,
68 [ # # ]: 0 : ConversionDirection_FROM_LEFT);
69 [ # # ]: 0 : maxRightLength = xCDL->queryMaxCharCount(loc,
70 : : ConversionDictionaryType::HANGUL_HANJA,
71 [ # # ]: 0 : ConversionDirection_FROM_RIGHT);
72 [ # # ]: 0 : if (xCD.is()) {
73 [ # # ][ # # ]: 0 : sal_Int32 tmp = xCD->getMaxCharCount(ConversionDirection_FROM_LEFT);
74 [ # # ]: 0 : if (tmp > maxLeftLength)
75 : 0 : maxLeftLength = tmp;
76 [ # # ][ # # ]: 0 : tmp = xCD->getMaxCharCount(ConversionDirection_FROM_RIGHT);
77 [ # # ]: 0 : if (tmp > maxRightLength)
78 : 0 : maxRightLength = tmp;
79 : 0 : }
80 [ # # ]: 0 : } else if (xCD.is()) {
81 [ # # ][ # # ]: 0 : maxLeftLength = xCD->getMaxCharCount(ConversionDirection_FROM_LEFT);
82 [ # # ][ # # ]: 0 : maxRightLength = xCD->getMaxCharCount(ConversionDirection_FROM_RIGHT);
83 : : }
84 : :
85 : 0 : implementationName = "com.sun.star.i18n.TextConversion_ko";
86 : 0 : }
87 : :
88 : 0 : sal_Int16 SAL_CALL checkScriptType(sal_Unicode c)
89 : : {
90 : : typedef struct {
91 : : UBlockCode from;
92 : : UBlockCode to;
93 : : sal_Int16 script;
94 : : } UBlock2Script;
95 : :
96 : : static UBlock2Script scriptList[] = {
97 : : {UBLOCK_HANGUL_JAMO, UBLOCK_HANGUL_JAMO, SCRIPT_HANGUL},
98 : : {UBLOCK_CJK_RADICALS_SUPPLEMENT, UBLOCK_BOPOMOFO, SCRIPT_HANJA},
99 : : {UBLOCK_HANGUL_COMPATIBILITY_JAMO, UBLOCK_HANGUL_COMPATIBILITY_JAMO, SCRIPT_HANGUL},
100 : : {UBLOCK_KANBUN, UBLOCK_YI_RADICALS, SCRIPT_HANJA},
101 : : {UBLOCK_HANGUL_SYLLABLES, UBLOCK_HANGUL_SYLLABLES, SCRIPT_HANGUL},
102 : : {UBLOCK_CJK_COMPATIBILITY_IDEOGRAPHS, UBLOCK_CJK_COMPATIBILITY_IDEOGRAPHS, SCRIPT_HANJA},
103 : : {UBLOCK_COMBINING_HALF_MARKS, UBLOCK_SMALL_FORM_VARIANTS, SCRIPT_HANJA},
104 : : {UBLOCK_HALFWIDTH_AND_FULLWIDTH_FORMS, UBLOCK_HALFWIDTH_AND_FULLWIDTH_FORMS, SCRIPT_HANJA},
105 : : };
106 : :
107 : : #define scriptListCount sizeof (scriptList) / sizeof (UBlock2Script)
108 : :
109 : 0 : UBlockCode block=ublock_getCode((sal_uInt32) c);
110 : : sal_uInt16 i;
111 [ # # ]: 0 : for ( i = 0; i < scriptListCount; i++) {
112 [ # # ]: 0 : if (block <= scriptList[i].to) break;
113 : : }
114 [ # # ][ # # ]: 0 : return (i < scriptListCount && block >= scriptList[i].from) ? scriptList[i].script : SCRIPT_OTHERS;
115 : : }
116 : :
117 : : #ifdef DISABLE_DYNLOADING
118 : :
119 : : extern "C" {
120 : :
121 : : const sal_Unicode* getHangul2HanjaData();
122 : : const Hangul_Index* getHangul2HanjaIndex();
123 : : sal_Int16 getHangul2HanjaIndexCount();
124 : : const sal_uInt16* getHanja2HangulIndex();
125 : : const sal_Unicode* getHanja2HangulData();
126 : :
127 : : }
128 : :
129 : : #endif
130 : :
131 : : Sequence< OUString > SAL_CALL
132 : 0 : TextConversion_ko::getCharConversions(const OUString& aText, sal_Int32 nStartPos, sal_Int32 nLength, sal_Bool toHanja)
133 : : {
134 : : sal_Unicode ch;
135 : 0 : Sequence< OUString > output;
136 : : #ifndef DISABLE_DYNLOADING
137 [ # # ]: 0 : const sal_Unicode* (*getHangul2HanjaData)() = (const sal_Unicode* (*)())getFunctionBySymbol("getHangul2HanjaData");
138 [ # # ]: 0 : const Hangul_Index* (*getHangul2HanjaIndex)() = (const Hangul_Index* (*)()) getFunctionBySymbol("getHangul2HanjaIndex");
139 [ # # ]: 0 : sal_Int16 (*getHangul2HanjaIndexCount)() = (sal_Int16 (*)()) getFunctionBySymbol("getHangul2HanjaIndexCount");
140 [ # # ]: 0 : const sal_uInt16* (*getHanja2HangulIndex)() = (const sal_uInt16* (*)()) getFunctionBySymbol("getHanja2HangulIndex");
141 [ # # ]: 0 : const sal_Unicode* (*getHanja2HangulData)() = (const sal_Unicode* (*)()) getFunctionBySymbol("getHanja2HangulData");
142 : : #else
143 : : #ifdef LIBO_WERROR
144 : : #pragma GCC diagnostic push
145 : : #pragma GCC diagnostic warning "-Wbool-conversions"
146 : : #endif
147 : : #endif
148 [ # # ][ # # ]: 0 : if (toHanja && getHangul2HanjaIndex && getHangul2HanjaIndexCount && getHangul2HanjaData) {
[ # # ][ # # ]
149 : 0 : ch = aText[nStartPos];
150 [ # # ]: 0 : const Hangul_Index *Hangul_ko = getHangul2HanjaIndex();
151 [ # # ]: 0 : sal_Int16 top = getHangul2HanjaIndexCount();
152 : 0 : --top;
153 : 0 : sal_Int16 bottom = 0;
154 : :
155 [ # # ]: 0 : while (bottom <= top) {
156 : 0 : sal_Int16 current = (top + bottom) / 2;
157 : 0 : sal_Unicode current_ch = Hangul_ko[current].code;
158 [ # # ]: 0 : if (ch < current_ch)
159 : 0 : top = current - 1;
160 [ # # ]: 0 : else if (ch > current_ch)
161 : 0 : bottom = current + 1;
162 : : else {
163 [ # # ]: 0 : const sal_Unicode *ptr = getHangul2HanjaData() + Hangul_ko[current].address;
164 : 0 : sal_Int16 count = Hangul_ko[current].count;
165 [ # # ]: 0 : output.realloc(count);
166 [ # # ]: 0 : for (sal_Int16 i = 0; i < count; i++)
167 [ # # ]: 0 : output[i] = OUString(ptr + i, 1);
168 : 0 : break;
169 : : }
170 : 0 : }
171 [ # # ][ # # ]: 0 : } else if (! toHanja && getHanja2HangulIndex && getHanja2HangulData)
[ # # ]
172 : : {
173 [ # # ]: 0 : sal_Unicode *newStr = new sal_Unicode[nLength+1];
174 : 0 : sal_Int32 count = 0;
175 [ # # ]: 0 : while (count < nLength)
176 : : {
177 : 0 : ch = aText[nStartPos + count];
178 [ # # ]: 0 : sal_Unicode address = getHanja2HangulIndex()[ch>>8];
179 [ # # ]: 0 : if (address != 0xFFFF)
180 [ # # ]: 0 : address = getHanja2HangulData()[address + (ch & 0xFF)];
181 : :
182 [ # # ]: 0 : if (address != 0xFFFF)
183 : 0 : newStr[count++] = address;
184 : : else
185 : 0 : break;
186 : : }
187 [ # # ]: 0 : if (count > 0)
188 : : {
189 [ # # ]: 0 : output.realloc(1);
190 [ # # ]: 0 : output[0] = OUString(newStr, count);
191 : : }
192 [ # # ]: 0 : delete[] newStr;
193 : : }
194 : : #if defined(DISABLE_DYNLOADING) && defined(LIBO_WERROR)
195 : : #pragma GCC diagnostic pop
196 : : #endif
197 : 0 : return output;
198 : : }
199 : :
200 : 0 : static Sequence< OUString >& operator += (Sequence< OUString > &rSeq1, Sequence< OUString > &rSeq2 )
201 : : {
202 [ # # ][ # # ]: 0 : if (! rSeq1.hasElements() && rSeq2.hasElements())
[ # # ]
203 : 0 : rSeq1 = rSeq2;
204 [ # # ]: 0 : else if (rSeq2.hasElements()) {
205 : : sal_Int32 i, j, k, l;
206 : 0 : k = l = rSeq1.getLength();
207 : 0 : rSeq1.realloc(l + rSeq2.getLength());
208 : :
209 [ # # ]: 0 : for (i = 0; i < rSeq2.getLength(); i++) {
210 [ # # ]: 0 : for (j = 0; j < l; j++)
211 [ # # ]: 0 : if (rSeq1[j] == rSeq2[i])
212 : 0 : break;
213 [ # # ]: 0 : if (j == l)
214 : 0 : rSeq1[k++] = rSeq2[i];
215 : : }
216 [ # # ]: 0 : if (rSeq1.getLength() > k)
217 : 0 : rSeq1.realloc(k);
218 : : }
219 : 0 : return rSeq1;
220 : : }
221 : :
222 : : TextConversionResult SAL_CALL
223 : 0 : TextConversion_ko::getConversions( const OUString& aText, sal_Int32 nStartPos, sal_Int32 nLength,
224 : : const Locale& aLocale, sal_Int16 nConversionType, sal_Int32 nConversionOptions)
225 : : throw( RuntimeException, IllegalArgumentException, NoSupportException )
226 : : {
227 [ # # ]: 0 : TextConversionResult result;
228 [ # # ]: 0 : Sequence <OUString> candidates;
229 : 0 : result.Boundary.startPos = result.Boundary.endPos = 0;
230 : :
231 : : // do conversion only when there are right conversion type and dictionary services.
232 [ # # ][ # # ]: 0 : if (nConversionType == TextConversionType::TO_HANGUL ||
233 : : nConversionType == TextConversionType::TO_HANJA) {
234 : 0 : sal_Int32 start, end, length = aText.getLength() - nStartPos;
235 : :
236 [ # # ][ # # ]: 0 : if (length < 0 || nStartPos < 0)
237 : 0 : length = 0;
238 [ # # ]: 0 : else if (length > nLength)
239 : 0 : length = nLength;
240 : :
241 : 0 : sal_Int16 scriptType = SCRIPT_OTHERS;
242 : 0 : sal_Int32 len = 1;
243 : 0 : sal_Bool toHanja = (nConversionType == TextConversionType::TO_HANJA);
244 : : // FROM_LEFT: Hangul -> Hanja
245 : : // FROM_RIGHT: Hanja -> Hangul
246 : 0 : ConversionDirection eDirection = toHanja ? ConversionDirection_FROM_LEFT : ConversionDirection_FROM_RIGHT;
247 [ # # ]: 0 : sal_Int32 maxLength = toHanja ? maxLeftLength : maxRightLength;
248 [ # # ]: 0 : if (maxLength == 0) maxLength = 1;
249 : :
250 : : // search for a max length of convertible text
251 [ # # ]: 0 : for (start = 0, end = 0; start < length; start++) {
252 [ # # ]: 0 : if (end <= start) {
253 [ # # ]: 0 : scriptType = checkScriptType(aText[nStartPos + start]);
254 [ # # ]: 0 : if (nConversionType == TextConversionType::TO_HANJA) {
255 [ # # ]: 0 : if (scriptType != SCRIPT_HANGUL) // skip non-Hangul characters
256 : 0 : continue;
257 : : } else {
258 [ # # ]: 0 : if (scriptType != SCRIPT_HANJA) // skip non-Hanja characters
259 : 0 : continue;
260 : : }
261 : 0 : end = start + 1;
262 : : }
263 [ # # ]: 0 : if (nConversionOptions & TextConversionOption::CHARACTER_BY_CHARACTER) {
264 [ # # ][ # # ]: 0 : result.Candidates = getCharConversions(aText, nStartPos + start, len, toHanja); // char2char conversion
[ # # ]
265 : : } else {
266 [ # # ][ # # ]: 0 : for (; end < length && end - start < maxLength; end++)
[ # # ]
267 [ # # ][ # # ]: 0 : if (checkScriptType(aText[nStartPos + end]) != scriptType)
268 : 0 : break;
269 : :
270 [ # # ]: 0 : for (len = end - start; len > 0; len--) {
271 [ # # ]: 0 : if (len > 1) {
272 : : try {
273 [ # # ]: 0 : if (xCDL.is())
274 [ # # ]: 0 : result.Candidates = xCDL->queryConversions(aText, start + nStartPos, len,
275 [ # # ][ # # ]: 0 : aLocale, ConversionDictionaryType::HANGUL_HANJA, eDirection, nConversionOptions); // user dictionary
[ # # ]
276 : : }
277 [ # # # # ]: 0 : catch ( NoSupportException & ) {
278 : : // clear reference (when there is no user dictionary) in order
279 : : // to not always have to catch this exception again
280 : : // in further calls. (save time)
281 [ # # ]: 0 : xCDL = 0;
282 : : }
283 [ # # ]: 0 : catch (...) {
284 : : // catch all other exceptions to allow
285 : : // querying the system dictionary in the next line
286 : : }
287 [ # # ][ # # ]: 0 : if (xCD.is() && toHanja) { // System dictionary would not do Hanja_to_Hangul conversion.
[ # # ]
288 [ # # ][ # # ]: 0 : candidates = xCD->getConversions(aText, start + nStartPos, len, eDirection, nConversionOptions);
[ # # ][ # # ]
289 [ # # ]: 0 : result.Candidates += candidates;
290 : : }
291 [ # # ]: 0 : } else if (! toHanja) { // do whole word character 2 character conversion for Hanja to Hangul conversion
292 [ # # ][ # # ]: 0 : result.Candidates = getCharConversions(aText, nStartPos + start, length - start, toHanja);
[ # # ]
293 [ # # ]: 0 : if (result.Candidates.hasElements())
294 [ # # ]: 0 : len = result.Candidates[0].getLength();
295 : : }
296 [ # # ]: 0 : if (result.Candidates.hasElements())
297 : 0 : break;
298 : : }
299 : : }
300 : : // found match
301 [ # # ]: 0 : if (result.Candidates.hasElements()) {
302 : 0 : result.Boundary.startPos = start + nStartPos;;
303 : 0 : result.Boundary.endPos = start + len + nStartPos;
304 : : return result;
305 : : }
306 : 0 : }
307 : : } else
308 [ # # ]: 0 : throw NoSupportException(); // Conversion type is not supported in this service.
309 [ # # ]: 0 : return result;
310 : : }
311 : :
312 : : OUString SAL_CALL
313 : 0 : TextConversion_ko::getConversion( const OUString& aText, sal_Int32 nStartPos, sal_Int32 nLength,
314 : : const Locale& aLocale, sal_Int16 nConversionType, sal_Int32 nConversionOptions)
315 : : throw( RuntimeException, IllegalArgumentException, NoSupportException )
316 : : {
317 : 0 : sal_Int32 length = aText.getLength() - nStartPos;
318 : :
319 [ # # ][ # # ]: 0 : if (length <= 0 || nStartPos < 0)
320 : 0 : return OUString();
321 [ # # ]: 0 : else if (length > nLength)
322 : 0 : length = nLength;
323 : :
324 : 0 : OUStringBuffer aBuf(length + 1);
325 [ # # ]: 0 : TextConversionResult result;
326 : 0 : const sal_Unicode *str = aText.getStr();
327 : :
328 [ # # ]: 0 : for (sal_Int32 start = nStartPos; length + nStartPos > start; start = result.Boundary.endPos) {
329 : :
330 [ # # ][ # # ]: 0 : result = getConversions(aText, start, length + nStartPos - start, aLocale, nConversionType, nConversionOptions);
[ # # ]
331 : :
332 [ # # ]: 0 : if (result.Boundary.endPos > 0) {
333 [ # # ]: 0 : if (result.Boundary.startPos > start)
334 [ # # ]: 0 : aBuf.append(str + start, result.Boundary.startPos - start); // append skip portion
335 [ # # ][ # # ]: 0 : aBuf.append(result.Candidates[0]); // append converted portion
336 : : } else {
337 [ # # ]: 0 : if (length + nStartPos > start)
338 [ # # ]: 0 : aBuf.append(str + start, length + nStartPos - start); // append last portion
339 : 0 : break;
340 : : }
341 : : }
342 : :
343 [ # # ][ # # ]: 0 : return aBuf.makeStringAndClear();
344 : : }
345 : :
346 : : OUString SAL_CALL
347 : 0 : TextConversion_ko::getConversionWithOffset( const OUString& aText, sal_Int32 nStartPos, sal_Int32 nLength,
348 : : const Locale& rLocale, sal_Int16 nConversionType, sal_Int32 nConversionOptions, Sequence<sal_Int32>& offset)
349 : : throw( RuntimeException, IllegalArgumentException, NoSupportException )
350 : : {
351 : 0 : offset.realloc(0);
352 : 0 : return getConversion(aText, nStartPos, nLength, rLocale, nConversionType, nConversionOptions);
353 : : }
354 : :
355 : : sal_Bool SAL_CALL
356 : 0 : TextConversion_ko::interactiveConversion( const Locale& /*rLocale*/, sal_Int16 /*nTextConversionType*/, sal_Int32 /*nTextConversionOptions*/ )
357 : : throw( RuntimeException, IllegalArgumentException, NoSupportException )
358 : : {
359 : 0 : return sal_True;
360 : : }
361 : :
362 : : } } } }
363 : :
364 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|