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