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 .
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>
27 #include <comphelper/sequence.hxx>
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
= xContext
->getServiceManager()->createInstanceWithContext(
48 "com.sun.star.i18n.ConversionDictionary_ko", xContext
);
51 xCD
.set( xI
, UNO_QUERY
);
53 xCDL
= ConversionDictionaryList::create(xContext
);
55 maxLeftLength
= maxRightLength
= 1;
57 // get maximum length of word in dictionary
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
);
67 sal_Int32 tmp
= xCD
->getMaxCharCount(ConversionDirection_FROM_LEFT
);
68 if (tmp
> maxLeftLength
)
70 tmp
= xCD
->getMaxCharCount(ConversionDirection_FROM_RIGHT
);
71 if (tmp
> maxRightLength
)
74 } else if (xCD
.is()) {
75 maxLeftLength
= xCD
->getMaxCharCount(ConversionDirection_FROM_LEFT
);
76 maxRightLength
= xCD
->getMaxCharCount(ConversionDirection_FROM_RIGHT
);
80 static sal_Int16
checkScriptType(sal_Unicode c
)
82 struct UBlock2Script
{
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
},
99 UBlockCode block
=ublock_getCode(static_cast<sal_uInt32
>(c
));
101 for ( i
= 0; i
< SAL_N_ELEMENTS(scriptList
); i
++) {
102 if (block
<= scriptList
[i
].to
) break;
104 return (i
< SAL_N_ELEMENTS(scriptList
) && block
>= scriptList
[i
].from
) ? scriptList
[i
].script
: SCRIPT_OTHERS
;
107 #ifdef DISABLE_DYNLOADING
111 const sal_Unicode
* getHangul2HanjaData();
112 const Hangul_Index
* getHangul2HanjaIndex();
113 sal_Int16
getHangul2HanjaIndexCount();
114 const sal_uInt16
* getHanja2HangulIndex();
115 const sal_Unicode
* getHanja2HangulData();
122 TextConversion_ko::getCharConversions(const OUString
& aText
, sal_Int32 nStartPos
, sal_Int32 nLength
, bool toHanja
)
125 Sequence
< OUString
> output
;
126 #ifndef DISABLE_DYNLOADING
127 const sal_Unicode
* (*getHangul2HanjaData
)() = reinterpret_cast<const sal_Unicode
* (*)()>(getFunctionBySymbol("getHangul2HanjaData"));
128 const Hangul_Index
* (*getHangul2HanjaIndex
)() = reinterpret_cast<const Hangul_Index
* (*)()>(getFunctionBySymbol("getHangul2HanjaIndex"));
129 sal_Int16 (*getHangul2HanjaIndexCount
)() = reinterpret_cast<sal_Int16 (*)()>(getFunctionBySymbol("getHangul2HanjaIndexCount"));
130 const sal_uInt16
* (*getHanja2HangulIndex
)() = reinterpret_cast<const sal_uInt16
* (*)()>(getFunctionBySymbol("getHanja2HangulIndex"));
131 const sal_Unicode
* (*getHanja2HangulData
)() = reinterpret_cast<const sal_Unicode
* (*)()>(getFunctionBySymbol("getHanja2HangulData"));
133 #pragma GCC diagnostic push
135 #pragma GCC diagnostic warning "-Wbool-conversions"
137 #pragma GCC diagnostic warning "-Waddress"
140 if (toHanja
&& getHangul2HanjaIndex
&& getHangul2HanjaIndexCount
&& getHangul2HanjaData
) {
141 ch
= aText
[nStartPos
];
142 const Hangul_Index
*Hangul_ko
= getHangul2HanjaIndex();
143 sal_Int16 top
= getHangul2HanjaIndexCount();
145 sal_Int16 bottom
= 0;
147 while (bottom
<= top
) {
148 sal_Int16 current
= (top
+ bottom
) / 2;
149 sal_Unicode current_ch
= Hangul_ko
[current
].code
;
152 else if (ch
> current_ch
)
153 bottom
= current
+ 1;
155 const sal_Unicode
*ptr
= getHangul2HanjaData() + Hangul_ko
[current
].address
;
156 sal_Int16 count
= Hangul_ko
[current
].count
;
157 output
.realloc(count
);
158 for (sal_Int16 i
= 0; i
< count
; i
++)
159 output
[i
] = OUString(ptr
+ i
, 1);
163 } else if (! toHanja
&& getHanja2HangulIndex
&& getHanja2HangulData
)
165 std::unique_ptr
<sal_Unicode
[]> newStr(new sal_Unicode
[nLength
+1]);
167 while (count
< nLength
)
169 ch
= aText
[nStartPos
+ count
];
170 sal_Unicode address
= getHanja2HangulIndex()[ch
>>8];
171 if (address
!= 0xFFFF)
172 address
= getHanja2HangulData()[address
+ (ch
& 0xFF)];
174 if (address
!= 0xFFFF)
175 newStr
[count
++] = address
;
182 output
[0] = OUString(newStr
.get(), count
);
185 #if defined(DISABLE_DYNLOADING)
186 #pragma GCC diagnostic pop
191 static Sequence
< OUString
>& operator += (Sequence
< OUString
> &rSeq1
, Sequence
< OUString
> &rSeq2
)
193 if (! rSeq1
.hasElements() && rSeq2
.hasElements())
195 else if (rSeq2
.hasElements())
196 rSeq1
= comphelper::combineSequences(rSeq1
, rSeq2
);
201 TextConversionResult SAL_CALL
202 TextConversion_ko::getConversions( const OUString
& aText
, sal_Int32 nStartPos
, sal_Int32 nLength
,
203 const Locale
& aLocale
, sal_Int16 nConversionType
, sal_Int32 nConversionOptions
)
205 TextConversionResult result
;
206 Sequence
<OUString
> candidates
;
207 result
.Boundary
.startPos
= result
.Boundary
.endPos
= 0;
209 // do conversion only when there are right conversion type and dictionary services.
210 if (nConversionType
!= TextConversionType::TO_HANGUL
&&
211 nConversionType
!= TextConversionType::TO_HANJA
)
212 throw NoSupportException(); // Conversion type is not supported in this service.
213 sal_Int32 start
, end
, length
= aText
.getLength() - nStartPos
;
215 if (length
< 0 || nStartPos
< 0)
217 else if (length
> nLength
)
220 sal_Int16 scriptType
= SCRIPT_OTHERS
;
222 bool toHanja
= (nConversionType
== TextConversionType::TO_HANJA
);
223 // FROM_LEFT: Hangul -> Hanja
224 // FROM_RIGHT: Hanja -> Hangul
225 ConversionDirection eDirection
= toHanja
? ConversionDirection_FROM_LEFT
: ConversionDirection_FROM_RIGHT
;
226 sal_Int32 maxLength
= toHanja
? maxLeftLength
: maxRightLength
;
227 if (maxLength
== 0) maxLength
= 1;
229 // search for a max length of convertible text
230 for (start
= 0, end
= 0; start
< length
; start
++) {
232 scriptType
= checkScriptType(aText
[nStartPos
+ start
]);
233 if (nConversionType
== TextConversionType::TO_HANJA
) {
234 if (scriptType
!= SCRIPT_HANGUL
) // skip non-Hangul characters
237 if (scriptType
!= SCRIPT_HANJA
) // skip non-Hanja characters
242 if (nConversionOptions
& TextConversionOption::CHARACTER_BY_CHARACTER
) {
243 result
.Candidates
= getCharConversions(aText
, nStartPos
+ start
, len
, toHanja
); // char2char conversion
245 for (; end
< length
&& end
- start
< maxLength
; end
++)
246 if (checkScriptType(aText
[nStartPos
+ end
]) != scriptType
)
249 for (len
= end
- start
; len
> 0; len
--) {
253 result
.Candidates
= xCDL
->queryConversions(aText
, start
+ nStartPos
, len
,
254 aLocale
, ConversionDictionaryType::HANGUL_HANJA
, eDirection
, nConversionOptions
); // user dictionary
256 catch ( NoSupportException
& ) {
257 // clear reference (when there is no user dictionary) in order
258 // to not always have to catch this exception again
259 // in further calls. (save time)
263 // catch all other exceptions to allow
264 // querying the system dictionary in the next line
266 if (xCD
.is() && toHanja
) { // System dictionary would not do Hanja_to_Hangul conversion.
267 candidates
= xCD
->getConversions(aText
, start
+ nStartPos
, len
, eDirection
, nConversionOptions
);
268 result
.Candidates
+= candidates
;
270 } else if (! toHanja
) { // do whole word character 2 character conversion for Hanja to Hangul conversion
271 result
.Candidates
= getCharConversions(aText
, nStartPos
+ start
, length
- start
, toHanja
);
272 if (result
.Candidates
.hasElements())
273 len
= result
.Candidates
[0].getLength();
275 if (result
.Candidates
.hasElements())
280 if (result
.Candidates
.hasElements()) {
281 result
.Boundary
.startPos
= start
+ nStartPos
;
282 result
.Boundary
.endPos
= start
+ len
+ nStartPos
;
291 TextConversion_ko::getConversion( const OUString
& aText
, sal_Int32 nStartPos
, sal_Int32 nLength
,
292 const Locale
& aLocale
, sal_Int16 nConversionType
, sal_Int32 nConversionOptions
)
294 sal_Int32 length
= aText
.getLength() - nStartPos
;
296 if (length
<= 0 || nStartPos
< 0)
298 else if (length
> nLength
)
301 OUStringBuffer
aBuf(length
+ 1);
302 TextConversionResult result
;
303 const sal_Unicode
*str
= aText
.getStr();
305 for (sal_Int32 start
= nStartPos
; length
+ nStartPos
> start
; start
= result
.Boundary
.endPos
) {
307 result
= getConversions(aText
, start
, length
+ nStartPos
- start
, aLocale
, nConversionType
, nConversionOptions
);
309 if (result
.Boundary
.endPos
> 0) {
310 if (result
.Boundary
.startPos
> start
)
311 aBuf
.append(str
+ start
, result
.Boundary
.startPos
- start
); // append skip portion
312 aBuf
.append(result
.Candidates
[0]); // append converted portion
314 aBuf
.append(str
+ start
, length
+ nStartPos
- start
); // append last portion
319 return aBuf
.makeStringAndClear();
323 TextConversion_ko::getConversionWithOffset( const OUString
& aText
, sal_Int32 nStartPos
, sal_Int32 nLength
,
324 const Locale
& rLocale
, sal_Int16 nConversionType
, sal_Int32 nConversionOptions
, Sequence
<sal_Int32
>& offset
)
327 return getConversion(aText
, nStartPos
, nLength
, rLocale
, nConversionType
, nConversionOptions
);
331 TextConversion_ko::interactiveConversion( const Locale
& /*rLocale*/, sal_Int16
/*nTextConversionType*/, sal_Int32
/*nTextConversionOptions*/ )
338 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */