Version 4.0.0.1, tag libreoffice-4.0.0.1
[LibreOffice.git] / i18npool / source / textconversion / textconversion_ko.cxx
blobdfb21e0bb6bb709ebb67c88d39eedc5e60795f6c
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 <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>
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;
34 using ::rtl::OUString;
35 using ::rtl::OUStringBuffer;
37 namespace com { namespace sun { namespace star { namespace i18n {
39 #define SCRIPT_OTHERS 0
40 #define SCRIPT_HANJA 1
41 #define SCRIPT_HANGUL 2
43 TextConversion_ko::TextConversion_ko( const Reference < XMultiServiceFactory >& xMSF )
45 Reference < XInterface > xI;
47 xI = xMSF->createInstance(
48 OUString("com.sun.star.i18n.ConversionDictionary_ko"));
50 if ( xI.is() )
51 xI->queryInterface( getCppuType((const Reference< XConversionDictionary>*)0) ) >>= xCD;
53 xI = xMSF->createInstance(
54 OUString("com.sun.star.linguistic2.ConversionDictionaryList"));
56 if ( xI.is() )
57 xI->queryInterface( getCppuType((const Reference< XConversionDictionaryList>*)0) ) >>= xCDL;
59 maxLeftLength = maxRightLength = 1;
61 // get maximum length of word in dictionary
62 if (xCDL.is()) {
63 Locale loc(OUString("ko"),
64 OUString("KR"),
65 OUString());
66 maxLeftLength = xCDL->queryMaxCharCount(loc,
67 ConversionDictionaryType::HANGUL_HANJA,
68 ConversionDirection_FROM_LEFT);
69 maxRightLength = xCDL->queryMaxCharCount(loc,
70 ConversionDictionaryType::HANGUL_HANJA,
71 ConversionDirection_FROM_RIGHT);
72 if (xCD.is()) {
73 sal_Int32 tmp = xCD->getMaxCharCount(ConversionDirection_FROM_LEFT);
74 if (tmp > maxLeftLength)
75 maxLeftLength = tmp;
76 tmp = xCD->getMaxCharCount(ConversionDirection_FROM_RIGHT);
77 if (tmp > maxRightLength)
78 maxRightLength = tmp;
80 } else if (xCD.is()) {
81 maxLeftLength = xCD->getMaxCharCount(ConversionDirection_FROM_LEFT);
82 maxRightLength = xCD->getMaxCharCount(ConversionDirection_FROM_RIGHT);
85 implementationName = "com.sun.star.i18n.TextConversion_ko";
88 sal_Int16 SAL_CALL checkScriptType(sal_Unicode c)
90 typedef struct {
91 UBlockCode from;
92 UBlockCode to;
93 sal_Int16 script;
94 } UBlock2Script;
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},
107 #define scriptListCount sizeof (scriptList) / sizeof (UBlock2Script)
109 UBlockCode block=ublock_getCode((sal_uInt32) c);
110 sal_uInt16 i;
111 for ( i = 0; i < scriptListCount; i++) {
112 if (block <= scriptList[i].to) break;
114 return (i < scriptListCount && block >= scriptList[i].from) ? scriptList[i].script : SCRIPT_OTHERS;
117 #ifdef DISABLE_DYNLOADING
119 extern "C" {
121 const sal_Unicode* getHangul2HanjaData();
122 const Hangul_Index* getHangul2HanjaIndex();
123 sal_Int16 getHangul2HanjaIndexCount();
124 const sal_uInt16* getHanja2HangulIndex();
125 const sal_Unicode* getHanja2HangulData();
129 #endif
131 Sequence< OUString > SAL_CALL
132 TextConversion_ko::getCharConversions(const OUString& aText, sal_Int32 nStartPos, sal_Int32 nLength, sal_Bool toHanja)
134 sal_Unicode ch;
135 Sequence< OUString > output;
136 #ifndef DISABLE_DYNLOADING
137 const sal_Unicode* (*getHangul2HanjaData)() = (const sal_Unicode* (*)())getFunctionBySymbol("getHangul2HanjaData");
138 const Hangul_Index* (*getHangul2HanjaIndex)() = (const Hangul_Index* (*)()) getFunctionBySymbol("getHangul2HanjaIndex");
139 sal_Int16 (*getHangul2HanjaIndexCount)() = (sal_Int16 (*)()) getFunctionBySymbol("getHangul2HanjaIndexCount");
140 const sal_uInt16* (*getHanja2HangulIndex)() = (const sal_uInt16* (*)()) getFunctionBySymbol("getHanja2HangulIndex");
141 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 if (toHanja && getHangul2HanjaIndex && getHangul2HanjaIndexCount && getHangul2HanjaData) {
153 ch = aText[nStartPos];
154 const Hangul_Index *Hangul_ko = getHangul2HanjaIndex();
155 sal_Int16 top = getHangul2HanjaIndexCount();
156 --top;
157 sal_Int16 bottom = 0;
159 while (bottom <= top) {
160 sal_Int16 current = (top + bottom) / 2;
161 sal_Unicode current_ch = Hangul_ko[current].code;
162 if (ch < current_ch)
163 top = current - 1;
164 else if (ch > current_ch)
165 bottom = current + 1;
166 else {
167 const sal_Unicode *ptr = getHangul2HanjaData() + Hangul_ko[current].address;
168 sal_Int16 count = Hangul_ko[current].count;
169 output.realloc(count);
170 for (sal_Int16 i = 0; i < count; i++)
171 output[i] = OUString(ptr + i, 1);
172 break;
175 } else if (! toHanja && getHanja2HangulIndex && getHanja2HangulData)
177 sal_Unicode *newStr = new sal_Unicode[nLength+1];
178 sal_Int32 count = 0;
179 while (count < nLength)
181 ch = aText[nStartPos + count];
182 sal_Unicode address = getHanja2HangulIndex()[ch>>8];
183 if (address != 0xFFFF)
184 address = getHanja2HangulData()[address + (ch & 0xFF)];
186 if (address != 0xFFFF)
187 newStr[count++] = address;
188 else
189 break;
191 if (count > 0)
193 output.realloc(1);
194 output[0] = OUString(newStr, count);
196 delete[] newStr;
198 #if defined(DISABLE_DYNLOADING) && defined(LIBO_WERROR)
199 #pragma GCC diagnostic pop
200 #endif
201 return output;
204 static Sequence< OUString >& operator += (Sequence< OUString > &rSeq1, Sequence< OUString > &rSeq2 )
206 if (! rSeq1.hasElements() && rSeq2.hasElements())
207 rSeq1 = rSeq2;
208 else if (rSeq2.hasElements()) {
209 sal_Int32 i, j, k, l;
210 k = l = rSeq1.getLength();
211 rSeq1.realloc(l + rSeq2.getLength());
213 for (i = 0; i < rSeq2.getLength(); i++) {
214 for (j = 0; j < l; j++)
215 if (rSeq1[j] == rSeq2[i])
216 break;
217 if (j == l)
218 rSeq1[k++] = rSeq2[i];
220 if (rSeq1.getLength() > k)
221 rSeq1.realloc(k);
223 return rSeq1;
226 TextConversionResult SAL_CALL
227 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 )
231 TextConversionResult result;
232 Sequence <OUString> candidates;
233 result.Boundary.startPos = result.Boundary.endPos = 0;
235 // do conversion only when there are right conversion type and dictionary services.
236 if (nConversionType == TextConversionType::TO_HANGUL ||
237 nConversionType == TextConversionType::TO_HANJA) {
238 sal_Int32 start, end, length = aText.getLength() - nStartPos;
240 if (length < 0 || nStartPos < 0)
241 length = 0;
242 else if (length > nLength)
243 length = nLength;
245 sal_Int16 scriptType = SCRIPT_OTHERS;
246 sal_Int32 len = 1;
247 sal_Bool toHanja = (nConversionType == TextConversionType::TO_HANJA);
248 // FROM_LEFT: Hangul -> Hanja
249 // FROM_RIGHT: Hanja -> Hangul
250 ConversionDirection eDirection = toHanja ? ConversionDirection_FROM_LEFT : ConversionDirection_FROM_RIGHT;
251 sal_Int32 maxLength = toHanja ? maxLeftLength : maxRightLength;
252 if (maxLength == 0) maxLength = 1;
254 // search for a max length of convertible text
255 for (start = 0, end = 0; start < length; start++) {
256 if (end <= start) {
257 scriptType = checkScriptType(aText[nStartPos + start]);
258 if (nConversionType == TextConversionType::TO_HANJA) {
259 if (scriptType != SCRIPT_HANGUL) // skip non-Hangul characters
260 continue;
261 } else {
262 if (scriptType != SCRIPT_HANJA) // skip non-Hanja characters
263 continue;
265 end = start + 1;
267 if (nConversionOptions & TextConversionOption::CHARACTER_BY_CHARACTER) {
268 result.Candidates = getCharConversions(aText, nStartPos + start, len, toHanja); // char2char conversion
269 } else {
270 for (; end < length && end - start < maxLength; end++)
271 if (checkScriptType(aText[nStartPos + end]) != scriptType)
272 break;
274 for (len = end - start; len > 0; len--) {
275 if (len > 1) {
276 try {
277 if (xCDL.is())
278 result.Candidates = xCDL->queryConversions(aText, start + nStartPos, len,
279 aLocale, ConversionDictionaryType::HANGUL_HANJA, eDirection, nConversionOptions); // user dictionary
281 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 xCDL = 0;
287 catch (...) {
288 // catch all other exceptions to allow
289 // querying the system dictionary in the next line
291 if (xCD.is() && toHanja) { // System dictionary would not do Hanja_to_Hangul conversion.
292 candidates = xCD->getConversions(aText, start + nStartPos, len, eDirection, nConversionOptions);
293 result.Candidates += candidates;
295 } else if (! toHanja) { // do whole word character 2 character conversion for Hanja to Hangul conversion
296 result.Candidates = getCharConversions(aText, nStartPos + start, length - start, toHanja);
297 if (result.Candidates.hasElements())
298 len = result.Candidates[0].getLength();
300 if (result.Candidates.hasElements())
301 break;
304 // found match
305 if (result.Candidates.hasElements()) {
306 result.Boundary.startPos = start + nStartPos;;
307 result.Boundary.endPos = start + len + nStartPos;
308 return result;
311 } else
312 throw NoSupportException(); // Conversion type is not supported in this service.
313 return result;
316 OUString SAL_CALL
317 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 )
321 sal_Int32 length = aText.getLength() - nStartPos;
323 if (length <= 0 || nStartPos < 0)
324 return OUString();
325 else if (length > nLength)
326 length = nLength;
328 OUStringBuffer aBuf(length + 1);
329 TextConversionResult result;
330 const sal_Unicode *str = aText.getStr();
332 for (sal_Int32 start = nStartPos; length + nStartPos > start; start = result.Boundary.endPos) {
334 result = getConversions(aText, start, length + nStartPos - start, aLocale, nConversionType, nConversionOptions);
336 if (result.Boundary.endPos > 0) {
337 if (result.Boundary.startPos > start)
338 aBuf.append(str + start, result.Boundary.startPos - start); // append skip portion
339 aBuf.append(result.Candidates[0]); // append converted portion
340 } else {
341 if (length + nStartPos > start)
342 aBuf.append(str + start, length + nStartPos - start); // append last portion
343 break;
347 return aBuf.makeStringAndClear();
350 OUString SAL_CALL
351 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 )
355 offset.realloc(0);
356 return getConversion(aText, nStartPos, nLength, rLocale, nConversionType, nConversionOptions);
359 sal_Bool SAL_CALL
360 TextConversion_ko::interactiveConversion( const Locale& /*rLocale*/, sal_Int16 /*nTextConversionType*/, sal_Int32 /*nTextConversionOptions*/ )
361 throw( RuntimeException, IllegalArgumentException, NoSupportException )
363 return sal_True;
366 } } } }
368 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */