Version 6.1.4.1, tag libreoffice-6.1.4.1
[LibreOffice.git] / i18npool / source / textconversion / textconversion_ko.cxx
blobb1dfe5d28cb7e42b136abdcd01a8203dbede7e10
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/lang/NoSupportException.hpp>
25 #include <com/sun/star/linguistic2/ConversionDirection.hpp>
26 #include <com/sun/star/linguistic2/ConversionDictionaryType.hpp>
27 #include <com/sun/star/linguistic2/ConversionDictionaryList.hpp>
28 #include <rtl/ustrbuf.hxx>
29 #include <unicode/uchar.h>
30 #include <memory>
32 using namespace com::sun::star::lang;
33 using namespace com::sun::star::i18n;
34 using namespace com::sun::star::linguistic2;
35 using namespace com::sun::star::uno;
38 namespace i18npool {
40 #define SCRIPT_OTHERS 0
41 #define SCRIPT_HANJA 1
42 #define SCRIPT_HANGUL 2
44 TextConversion_ko::TextConversion_ko( const Reference < XComponentContext >& xContext )
45 : TextConversionService("com.sun.star.i18n.TextConversion_ko")
47 Reference < XInterface > xI;
49 xI = xContext->getServiceManager()->createInstanceWithContext(
50 "com.sun.star.i18n.ConversionDictionary_ko", xContext);
52 if ( xI.is() )
53 xCD.set( xI, UNO_QUERY );
55 xCDL = ConversionDictionaryList::create(xContext);
57 maxLeftLength = maxRightLength = 1;
59 // get maximum length of word in dictionary
60 if (xCDL.is()) {
61 Locale loc("ko", "KR", OUString());
62 maxLeftLength = xCDL->queryMaxCharCount(loc,
63 ConversionDictionaryType::HANGUL_HANJA,
64 ConversionDirection_FROM_LEFT);
65 maxRightLength = xCDL->queryMaxCharCount(loc,
66 ConversionDictionaryType::HANGUL_HANJA,
67 ConversionDirection_FROM_RIGHT);
68 if (xCD.is()) {
69 sal_Int32 tmp = xCD->getMaxCharCount(ConversionDirection_FROM_LEFT);
70 if (tmp > maxLeftLength)
71 maxLeftLength = tmp;
72 tmp = xCD->getMaxCharCount(ConversionDirection_FROM_RIGHT);
73 if (tmp > maxRightLength)
74 maxRightLength = tmp;
76 } else if (xCD.is()) {
77 maxLeftLength = xCD->getMaxCharCount(ConversionDirection_FROM_LEFT);
78 maxRightLength = xCD->getMaxCharCount(ConversionDirection_FROM_RIGHT);
82 sal_Int16 checkScriptType(sal_Unicode c)
84 typedef struct {
85 UBlockCode from;
86 UBlockCode to;
87 sal_Int16 script;
88 } UBlock2Script;
90 static const UBlock2Script scriptList[] = {
91 {UBLOCK_HANGUL_JAMO, UBLOCK_HANGUL_JAMO, SCRIPT_HANGUL},
92 {UBLOCK_CJK_RADICALS_SUPPLEMENT, UBLOCK_BOPOMOFO, SCRIPT_HANJA},
93 {UBLOCK_HANGUL_COMPATIBILITY_JAMO, UBLOCK_HANGUL_COMPATIBILITY_JAMO, SCRIPT_HANGUL},
94 {UBLOCK_KANBUN, UBLOCK_YI_RADICALS, SCRIPT_HANJA},
95 {UBLOCK_HANGUL_SYLLABLES, UBLOCK_HANGUL_SYLLABLES, SCRIPT_HANGUL},
96 {UBLOCK_CJK_COMPATIBILITY_IDEOGRAPHS, UBLOCK_CJK_COMPATIBILITY_IDEOGRAPHS, SCRIPT_HANJA},
97 {UBLOCK_COMBINING_HALF_MARKS, UBLOCK_SMALL_FORM_VARIANTS, SCRIPT_HANJA},
98 {UBLOCK_HALFWIDTH_AND_FULLWIDTH_FORMS, UBLOCK_HALFWIDTH_AND_FULLWIDTH_FORMS, SCRIPT_HANJA},
101 UBlockCode block=ublock_getCode(static_cast<sal_uInt32>(c));
102 sal_uInt16 i;
103 for ( i = 0; i < SAL_N_ELEMENTS(scriptList); i++) {
104 if (block <= scriptList[i].to) break;
106 return (i < SAL_N_ELEMENTS(scriptList) && block >= scriptList[i].from) ? scriptList[i].script : SCRIPT_OTHERS;
109 #ifdef DISABLE_DYNLOADING
111 extern "C" {
113 const sal_Unicode* getHangul2HanjaData();
114 const Hangul_Index* getHangul2HanjaIndex();
115 sal_Int16 getHangul2HanjaIndexCount();
116 const sal_uInt16* getHanja2HangulIndex();
117 const sal_Unicode* getHanja2HangulData();
121 #endif
123 Sequence< OUString >
124 TextConversion_ko::getCharConversions(const OUString& aText, sal_Int32 nStartPos, sal_Int32 nLength, bool toHanja)
126 sal_Unicode ch;
127 Sequence< OUString > output;
128 #ifndef DISABLE_DYNLOADING
129 const sal_Unicode* (*getHangul2HanjaData)() = reinterpret_cast<const sal_Unicode* (*)()>(getFunctionBySymbol("getHangul2HanjaData"));
130 const Hangul_Index* (*getHangul2HanjaIndex)() = reinterpret_cast<const Hangul_Index* (*)()>(getFunctionBySymbol("getHangul2HanjaIndex"));
131 sal_Int16 (*getHangul2HanjaIndexCount)() = reinterpret_cast<sal_Int16 (*)()>(getFunctionBySymbol("getHangul2HanjaIndexCount"));
132 const sal_uInt16* (*getHanja2HangulIndex)() = reinterpret_cast<const sal_uInt16* (*)()>(getFunctionBySymbol("getHanja2HangulIndex"));
133 const sal_Unicode* (*getHanja2HangulData)() = reinterpret_cast<const sal_Unicode* (*)()>(getFunctionBySymbol("getHanja2HangulData"));
134 #else
135 #pragma GCC diagnostic push
136 #ifdef __clang__
137 #pragma GCC diagnostic warning "-Wbool-conversions"
138 #else
139 #pragma GCC diagnostic warning "-Waddress"
140 #endif
141 #endif
142 if (toHanja && getHangul2HanjaIndex && getHangul2HanjaIndexCount && getHangul2HanjaData) {
143 ch = aText[nStartPos];
144 const Hangul_Index *Hangul_ko = getHangul2HanjaIndex();
145 sal_Int16 top = getHangul2HanjaIndexCount();
146 --top;
147 sal_Int16 bottom = 0;
149 while (bottom <= top) {
150 sal_Int16 current = (top + bottom) / 2;
151 sal_Unicode current_ch = Hangul_ko[current].code;
152 if (ch < current_ch)
153 top = current - 1;
154 else if (ch > current_ch)
155 bottom = current + 1;
156 else {
157 const sal_Unicode *ptr = getHangul2HanjaData() + Hangul_ko[current].address;
158 sal_Int16 count = Hangul_ko[current].count;
159 output.realloc(count);
160 for (sal_Int16 i = 0; i < count; i++)
161 output[i] = OUString(ptr + i, 1);
162 break;
165 } else if (! toHanja && getHanja2HangulIndex && getHanja2HangulData)
167 std::unique_ptr<sal_Unicode[]> newStr(new sal_Unicode[nLength+1]);
168 sal_Int32 count = 0;
169 while (count < nLength)
171 ch = aText[nStartPos + count];
172 sal_Unicode address = getHanja2HangulIndex()[ch>>8];
173 if (address != 0xFFFF)
174 address = getHanja2HangulData()[address + (ch & 0xFF)];
176 if (address != 0xFFFF)
177 newStr[count++] = address;
178 else
179 break;
181 if (count > 0)
183 output.realloc(1);
184 output[0] = OUString(newStr.get(), count);
187 #if defined(DISABLE_DYNLOADING)
188 #pragma GCC diagnostic pop
189 #endif
190 return output;
193 static Sequence< OUString >& operator += (Sequence< OUString > &rSeq1, Sequence< OUString > &rSeq2 )
195 if (! rSeq1.hasElements() && rSeq2.hasElements())
196 rSeq1 = rSeq2;
197 else if (rSeq2.hasElements()) {
198 sal_Int32 i, j, k, l;
199 k = l = rSeq1.getLength();
200 rSeq1.realloc(l + rSeq2.getLength());
202 for (i = 0; i < rSeq2.getLength(); i++) {
203 for (j = 0; j < l; j++)
204 if (rSeq1[j] == rSeq2[i])
205 break;
206 if (j == l)
207 rSeq1[k++] = rSeq2[i];
209 if (rSeq1.getLength() > k)
210 rSeq1.realloc(k);
212 return rSeq1;
215 TextConversionResult SAL_CALL
216 TextConversion_ko::getConversions( const OUString& aText, sal_Int32 nStartPos, sal_Int32 nLength,
217 const Locale& aLocale, sal_Int16 nConversionType, sal_Int32 nConversionOptions)
219 TextConversionResult result;
220 Sequence <OUString> candidates;
221 result.Boundary.startPos = result.Boundary.endPos = 0;
223 // do conversion only when there are right conversion type and dictionary services.
224 if (nConversionType != TextConversionType::TO_HANGUL &&
225 nConversionType != TextConversionType::TO_HANJA)
226 throw NoSupportException(); // Conversion type is not supported in this service.
227 sal_Int32 start, end, length = aText.getLength() - nStartPos;
229 if (length < 0 || nStartPos < 0)
230 length = 0;
231 else if (length > nLength)
232 length = nLength;
234 sal_Int16 scriptType = SCRIPT_OTHERS;
235 sal_Int32 len = 1;
236 bool toHanja = (nConversionType == TextConversionType::TO_HANJA);
237 // FROM_LEFT: Hangul -> Hanja
238 // FROM_RIGHT: Hanja -> Hangul
239 ConversionDirection eDirection = toHanja ? ConversionDirection_FROM_LEFT : ConversionDirection_FROM_RIGHT;
240 sal_Int32 maxLength = toHanja ? maxLeftLength : maxRightLength;
241 if (maxLength == 0) maxLength = 1;
243 // search for a max length of convertible text
244 for (start = 0, end = 0; start < length; start++) {
245 if (end <= start) {
246 scriptType = checkScriptType(aText[nStartPos + start]);
247 if (nConversionType == TextConversionType::TO_HANJA) {
248 if (scriptType != SCRIPT_HANGUL) // skip non-Hangul characters
249 continue;
250 } else {
251 if (scriptType != SCRIPT_HANJA) // skip non-Hanja characters
252 continue;
254 end = start + 1;
256 if (nConversionOptions & TextConversionOption::CHARACTER_BY_CHARACTER) {
257 result.Candidates = getCharConversions(aText, nStartPos + start, len, toHanja); // char2char conversion
258 } else {
259 for (; end < length && end - start < maxLength; end++)
260 if (checkScriptType(aText[nStartPos + end]) != scriptType)
261 break;
263 for (len = end - start; len > 0; len--) {
264 if (len > 1) {
265 try {
266 if (xCDL.is())
267 result.Candidates = xCDL->queryConversions(aText, start + nStartPos, len,
268 aLocale, ConversionDictionaryType::HANGUL_HANJA, eDirection, nConversionOptions); // user dictionary
270 catch ( NoSupportException & ) {
271 // clear reference (when there is no user dictionary) in order
272 // to not always have to catch this exception again
273 // in further calls. (save time)
274 xCDL = nullptr;
276 catch (...) {
277 // catch all other exceptions to allow
278 // querying the system dictionary in the next line
280 if (xCD.is() && toHanja) { // System dictionary would not do Hanja_to_Hangul conversion.
281 candidates = xCD->getConversions(aText, start + nStartPos, len, eDirection, nConversionOptions);
282 result.Candidates += candidates;
284 } else if (! toHanja) { // do whole word character 2 character conversion for Hanja to Hangul conversion
285 result.Candidates = getCharConversions(aText, nStartPos + start, length - start, toHanja);
286 if (result.Candidates.hasElements())
287 len = result.Candidates[0].getLength();
289 if (result.Candidates.hasElements())
290 break;
293 // found match
294 if (result.Candidates.hasElements()) {
295 result.Boundary.startPos = start + nStartPos;
296 result.Boundary.endPos = start + len + nStartPos;
297 return result;
301 return result;
304 OUString SAL_CALL
305 TextConversion_ko::getConversion( const OUString& aText, sal_Int32 nStartPos, sal_Int32 nLength,
306 const Locale& aLocale, sal_Int16 nConversionType, sal_Int32 nConversionOptions)
308 sal_Int32 length = aText.getLength() - nStartPos;
310 if (length <= 0 || nStartPos < 0)
311 return OUString();
312 else if (length > nLength)
313 length = nLength;
315 OUStringBuffer aBuf(length + 1);
316 TextConversionResult result;
317 const sal_Unicode *str = aText.getStr();
319 for (sal_Int32 start = nStartPos; length + nStartPos > start; start = result.Boundary.endPos) {
321 result = getConversions(aText, start, length + nStartPos - start, aLocale, nConversionType, nConversionOptions);
323 if (result.Boundary.endPos > 0) {
324 if (result.Boundary.startPos > start)
325 aBuf.append(str + start, result.Boundary.startPos - start); // append skip portion
326 aBuf.append(result.Candidates[0]); // append converted portion
327 } else {
328 if (length + nStartPos > start)
329 aBuf.append(str + start, length + nStartPos - start); // append last portion
330 break;
334 return aBuf.makeStringAndClear();
337 OUString SAL_CALL
338 TextConversion_ko::getConversionWithOffset( const OUString& aText, sal_Int32 nStartPos, sal_Int32 nLength,
339 const Locale& rLocale, sal_Int16 nConversionType, sal_Int32 nConversionOptions, Sequence<sal_Int32>& offset)
341 offset.realloc(0);
342 return getConversion(aText, nStartPos, nLength, rLocale, nConversionType, nConversionOptions);
345 sal_Bool SAL_CALL
346 TextConversion_ko::interactiveConversion( const Locale& /*rLocale*/, sal_Int16 /*nTextConversionType*/, sal_Int32 /*nTextConversionOptions*/ )
348 return true;
353 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */