1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 .
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>
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
;
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
);
53 xCD
.set( xI
, UNO_QUERY
);
55 xCDL
= ConversionDictionaryList::create(xContext
);
57 maxLeftLength
= maxRightLength
= 1;
59 // get maximum length of word in dictionary
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
);
69 sal_Int32 tmp
= xCD
->getMaxCharCount(ConversionDirection_FROM_LEFT
);
70 if (tmp
> maxLeftLength
)
72 tmp
= xCD
->getMaxCharCount(ConversionDirection_FROM_RIGHT
);
73 if (tmp
> maxRightLength
)
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
)
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
));
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
113 const sal_Unicode
* getHangul2HanjaData();
114 const Hangul_Index
* getHangul2HanjaIndex();
115 sal_Int16
getHangul2HanjaIndexCount();
116 const sal_uInt16
* getHanja2HangulIndex();
117 const sal_Unicode
* getHanja2HangulData();
124 TextConversion_ko::getCharConversions(const OUString
& aText
, sal_Int32 nStartPos
, sal_Int32 nLength
, bool toHanja
)
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"));
135 #pragma GCC diagnostic push
137 #pragma GCC diagnostic warning "-Wbool-conversions"
139 #pragma GCC diagnostic warning "-Waddress"
142 if (toHanja
&& getHangul2HanjaIndex
&& getHangul2HanjaIndexCount
&& getHangul2HanjaData
) {
143 ch
= aText
[nStartPos
];
144 const Hangul_Index
*Hangul_ko
= getHangul2HanjaIndex();
145 sal_Int16 top
= getHangul2HanjaIndexCount();
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
;
154 else if (ch
> current_ch
)
155 bottom
= current
+ 1;
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);
165 } else if (! toHanja
&& getHanja2HangulIndex
&& getHanja2HangulData
)
167 std::unique_ptr
<sal_Unicode
[]> newStr(new sal_Unicode
[nLength
+1]);
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
;
184 output
[0] = OUString(newStr
.get(), count
);
187 #if defined(DISABLE_DYNLOADING)
188 #pragma GCC diagnostic pop
193 static Sequence
< OUString
>& operator += (Sequence
< OUString
> &rSeq1
, Sequence
< OUString
> &rSeq2
)
195 if (! rSeq1
.hasElements() && rSeq2
.hasElements())
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
])
207 rSeq1
[k
++] = rSeq2
[i
];
209 if (rSeq1
.getLength() > k
)
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)
231 else if (length
> nLength
)
234 sal_Int16 scriptType
= SCRIPT_OTHERS
;
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
++) {
246 scriptType
= checkScriptType(aText
[nStartPos
+ start
]);
247 if (nConversionType
== TextConversionType::TO_HANJA
) {
248 if (scriptType
!= SCRIPT_HANGUL
) // skip non-Hangul characters
251 if (scriptType
!= SCRIPT_HANJA
) // skip non-Hanja characters
256 if (nConversionOptions
& TextConversionOption::CHARACTER_BY_CHARACTER
) {
257 result
.Candidates
= getCharConversions(aText
, nStartPos
+ start
, len
, toHanja
); // char2char conversion
259 for (; end
< length
&& end
- start
< maxLength
; end
++)
260 if (checkScriptType(aText
[nStartPos
+ end
]) != scriptType
)
263 for (len
= end
- start
; len
> 0; len
--) {
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)
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())
294 if (result
.Candidates
.hasElements()) {
295 result
.Boundary
.startPos
= start
+ nStartPos
;
296 result
.Boundary
.endPos
= start
+ len
+ nStartPos
;
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)
312 else if (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
328 if (length
+ nStartPos
> start
)
329 aBuf
.append(str
+ start
, length
+ nStartPos
- start
); // append last portion
334 return aBuf
.makeStringAndClear();
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
)
342 return getConversion(aText
, nStartPos
, nLength
, rLocale
, nConversionType
, nConversionOptions
);
346 TextConversion_ko::interactiveConversion( const Locale
& /*rLocale*/, sal_Int16
/*nTextConversionType*/, sal_Int32
/*nTextConversionOptions*/ )
353 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */