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 : #ifdef __clang__
146 : #pragma GCC diagnostic warning "-Wbool-conversions"
147 : #else
148 : #pragma GCC diagnostic warning "-Waddress"
149 : #endif
150 : #endif
151 : #endif
152 0 : if (toHanja && getHangul2HanjaIndex && getHangul2HanjaIndexCount && getHangul2HanjaData) {
153 0 : ch = aText[nStartPos];
154 0 : const Hangul_Index *Hangul_ko = getHangul2HanjaIndex();
155 0 : sal_Int16 top = getHangul2HanjaIndexCount();
156 0 : --top;
157 0 : sal_Int16 bottom = 0;
158 :
159 0 : while (bottom <= top) {
160 0 : sal_Int16 current = (top + bottom) / 2;
161 0 : sal_Unicode current_ch = Hangul_ko[current].code;
162 0 : if (ch < current_ch)
163 0 : top = current - 1;
164 0 : else if (ch > current_ch)
165 0 : bottom = current + 1;
166 : else {
167 0 : const sal_Unicode *ptr = getHangul2HanjaData() + Hangul_ko[current].address;
168 0 : sal_Int16 count = Hangul_ko[current].count;
169 0 : output.realloc(count);
170 0 : for (sal_Int16 i = 0; i < count; i++)
171 0 : output[i] = OUString(ptr + i, 1);
172 0 : break;
173 : }
174 0 : }
175 0 : } else if (! toHanja && getHanja2HangulIndex && getHanja2HangulData)
176 : {
177 0 : sal_Unicode *newStr = new sal_Unicode[nLength+1];
178 0 : sal_Int32 count = 0;
179 0 : while (count < nLength)
180 : {
181 0 : ch = aText[nStartPos + count];
182 0 : sal_Unicode address = getHanja2HangulIndex()[ch>>8];
183 0 : if (address != 0xFFFF)
184 0 : address = getHanja2HangulData()[address + (ch & 0xFF)];
185 :
186 0 : if (address != 0xFFFF)
187 0 : newStr[count++] = address;
188 : else
189 0 : break;
190 : }
191 0 : if (count > 0)
192 : {
193 0 : output.realloc(1);
194 0 : output[0] = OUString(newStr, count);
195 : }
196 0 : delete[] newStr;
197 : }
198 : #if defined(DISABLE_DYNLOADING) && defined(LIBO_WERROR)
199 : #pragma GCC diagnostic pop
200 : #endif
201 0 : return output;
202 : }
203 :
204 0 : static Sequence< OUString >& operator += (Sequence< OUString > &rSeq1, Sequence< OUString > &rSeq2 )
205 : {
206 0 : if (! rSeq1.hasElements() && rSeq2.hasElements())
207 0 : rSeq1 = rSeq2;
208 0 : else if (rSeq2.hasElements()) {
209 : sal_Int32 i, j, k, l;
210 0 : k = l = rSeq1.getLength();
211 0 : rSeq1.realloc(l + rSeq2.getLength());
212 :
213 0 : for (i = 0; i < rSeq2.getLength(); i++) {
214 0 : for (j = 0; j < l; j++)
215 0 : if (rSeq1[j] == rSeq2[i])
216 0 : break;
217 0 : if (j == l)
218 0 : rSeq1[k++] = rSeq2[i];
219 : }
220 0 : if (rSeq1.getLength() > k)
221 0 : rSeq1.realloc(k);
222 : }
223 0 : return rSeq1;
224 : }
225 :
226 : TextConversionResult SAL_CALL
227 0 : TextConversion_ko::getConversions( const OUString& aText, sal_Int32 nStartPos, sal_Int32 nLength,
228 : const Locale& aLocale, sal_Int16 nConversionType, sal_Int32 nConversionOptions)
229 : throw( RuntimeException, IllegalArgumentException, NoSupportException )
230 : {
231 0 : TextConversionResult result;
232 0 : Sequence <OUString> candidates;
233 0 : result.Boundary.startPos = result.Boundary.endPos = 0;
234 :
235 : // do conversion only when there are right conversion type and dictionary services.
236 0 : if (nConversionType == TextConversionType::TO_HANGUL ||
237 : nConversionType == TextConversionType::TO_HANJA) {
238 0 : sal_Int32 start, end, length = aText.getLength() - nStartPos;
239 :
240 0 : if (length < 0 || nStartPos < 0)
241 0 : length = 0;
242 0 : else if (length > nLength)
243 0 : length = nLength;
244 :
245 0 : sal_Int16 scriptType = SCRIPT_OTHERS;
246 0 : sal_Int32 len = 1;
247 0 : sal_Bool toHanja = (nConversionType == TextConversionType::TO_HANJA);
248 : // FROM_LEFT: Hangul -> Hanja
249 : // FROM_RIGHT: Hanja -> Hangul
250 0 : ConversionDirection eDirection = toHanja ? ConversionDirection_FROM_LEFT : ConversionDirection_FROM_RIGHT;
251 0 : sal_Int32 maxLength = toHanja ? maxLeftLength : maxRightLength;
252 0 : if (maxLength == 0) maxLength = 1;
253 :
254 : // search for a max length of convertible text
255 0 : for (start = 0, end = 0; start < length; start++) {
256 0 : if (end <= start) {
257 0 : scriptType = checkScriptType(aText[nStartPos + start]);
258 0 : if (nConversionType == TextConversionType::TO_HANJA) {
259 0 : if (scriptType != SCRIPT_HANGUL) // skip non-Hangul characters
260 0 : continue;
261 : } else {
262 0 : if (scriptType != SCRIPT_HANJA) // skip non-Hanja characters
263 0 : continue;
264 : }
265 0 : end = start + 1;
266 : }
267 0 : if (nConversionOptions & TextConversionOption::CHARACTER_BY_CHARACTER) {
268 0 : result.Candidates = getCharConversions(aText, nStartPos + start, len, toHanja); // char2char conversion
269 : } else {
270 0 : for (; end < length && end - start < maxLength; end++)
271 0 : if (checkScriptType(aText[nStartPos + end]) != scriptType)
272 0 : break;
273 :
274 0 : for (len = end - start; len > 0; len--) {
275 0 : if (len > 1) {
276 : try {
277 0 : if (xCDL.is())
278 0 : result.Candidates = xCDL->queryConversions(aText, start + nStartPos, len,
279 0 : aLocale, ConversionDictionaryType::HANGUL_HANJA, eDirection, nConversionOptions); // user dictionary
280 : }
281 0 : catch ( NoSupportException & ) {
282 : // clear reference (when there is no user dictionary) in order
283 : // to not always have to catch this exception again
284 : // in further calls. (save time)
285 0 : xCDL = 0;
286 : }
287 0 : catch (...) {
288 : // catch all other exceptions to allow
289 : // querying the system dictionary in the next line
290 : }
291 0 : if (xCD.is() && toHanja) { // System dictionary would not do Hanja_to_Hangul conversion.
292 0 : candidates = xCD->getConversions(aText, start + nStartPos, len, eDirection, nConversionOptions);
293 0 : result.Candidates += candidates;
294 : }
295 0 : } else if (! toHanja) { // do whole word character 2 character conversion for Hanja to Hangul conversion
296 0 : result.Candidates = getCharConversions(aText, nStartPos + start, length - start, toHanja);
297 0 : if (result.Candidates.hasElements())
298 0 : len = result.Candidates[0].getLength();
299 : }
300 0 : if (result.Candidates.hasElements())
301 0 : break;
302 : }
303 : }
304 : // found match
305 0 : if (result.Candidates.hasElements()) {
306 0 : result.Boundary.startPos = start + nStartPos;;
307 0 : result.Boundary.endPos = start + len + nStartPos;
308 : return result;
309 : }
310 0 : }
311 : } else
312 0 : throw NoSupportException(); // Conversion type is not supported in this service.
313 0 : return result;
314 : }
315 :
316 : OUString SAL_CALL
317 0 : TextConversion_ko::getConversion( const OUString& aText, sal_Int32 nStartPos, sal_Int32 nLength,
318 : const Locale& aLocale, sal_Int16 nConversionType, sal_Int32 nConversionOptions)
319 : throw( RuntimeException, IllegalArgumentException, NoSupportException )
320 : {
321 0 : sal_Int32 length = aText.getLength() - nStartPos;
322 :
323 0 : if (length <= 0 || nStartPos < 0)
324 0 : return OUString();
325 0 : else if (length > nLength)
326 0 : length = nLength;
327 :
328 0 : OUStringBuffer aBuf(length + 1);
329 0 : TextConversionResult result;
330 0 : const sal_Unicode *str = aText.getStr();
331 :
332 0 : for (sal_Int32 start = nStartPos; length + nStartPos > start; start = result.Boundary.endPos) {
333 :
334 0 : result = getConversions(aText, start, length + nStartPos - start, aLocale, nConversionType, nConversionOptions);
335 :
336 0 : if (result.Boundary.endPos > 0) {
337 0 : if (result.Boundary.startPos > start)
338 0 : aBuf.append(str + start, result.Boundary.startPos - start); // append skip portion
339 0 : aBuf.append(result.Candidates[0]); // append converted portion
340 : } else {
341 0 : if (length + nStartPos > start)
342 0 : aBuf.append(str + start, length + nStartPos - start); // append last portion
343 0 : break;
344 : }
345 : }
346 :
347 0 : return aBuf.makeStringAndClear();
348 : }
349 :
350 : OUString SAL_CALL
351 0 : TextConversion_ko::getConversionWithOffset( const OUString& aText, sal_Int32 nStartPos, sal_Int32 nLength,
352 : const Locale& rLocale, sal_Int16 nConversionType, sal_Int32 nConversionOptions, Sequence<sal_Int32>& offset)
353 : throw( RuntimeException, IllegalArgumentException, NoSupportException )
354 : {
355 0 : offset.realloc(0);
356 0 : return getConversion(aText, nStartPos, nLength, rLocale, nConversionType, nConversionOptions);
357 : }
358 :
359 : sal_Bool SAL_CALL
360 0 : TextConversion_ko::interactiveConversion( const Locale& /*rLocale*/, sal_Int16 /*nTextConversionType*/, sal_Int32 /*nTextConversionOptions*/ )
361 : throw( RuntimeException, IllegalArgumentException, NoSupportException )
362 : {
363 0 : return sal_True;
364 : }
365 :
366 : } } } }
367 :
368 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|