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/linguistic2/ConversionDirection.hpp>
25 #include <com/sun/star/linguistic2/ConversionDictionaryType.hpp>
26 #include <com/sun/star/linguistic2/ConversionDictionaryList.hpp>
27 #include <rtl/ustrbuf.hxx>
28 #include <unicode/uchar.h>
31 using namespace com::sun::star::lang
;
32 using namespace com::sun::star::i18n
;
33 using namespace com::sun::star::linguistic2
;
34 using namespace com::sun::star::uno
;
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
< XComponentContext
>& xContext
)
44 : TextConversionService("com.sun.star.i18n.TextConversion_ko")
46 Reference
< XInterface
> xI
;
48 xI
= xContext
->getServiceManager()->createInstanceWithContext(
49 "com.sun.star.i18n.ConversionDictionary_ko", xContext
);
52 xCD
.set( xI
, UNO_QUERY
);
54 xCDL
= ConversionDictionaryList::create(xContext
);
56 maxLeftLength
= maxRightLength
= 1;
58 // get maximum length of word in dictionary
60 Locale
loc(OUString("ko"),
63 maxLeftLength
= xCDL
->queryMaxCharCount(loc
,
64 ConversionDictionaryType::HANGUL_HANJA
,
65 ConversionDirection_FROM_LEFT
);
66 maxRightLength
= xCDL
->queryMaxCharCount(loc
,
67 ConversionDictionaryType::HANGUL_HANJA
,
68 ConversionDirection_FROM_RIGHT
);
70 sal_Int32 tmp
= xCD
->getMaxCharCount(ConversionDirection_FROM_LEFT
);
71 if (tmp
> maxLeftLength
)
73 tmp
= xCD
->getMaxCharCount(ConversionDirection_FROM_RIGHT
);
74 if (tmp
> maxRightLength
)
77 } else if (xCD
.is()) {
78 maxLeftLength
= xCD
->getMaxCharCount(ConversionDirection_FROM_LEFT
);
79 maxRightLength
= xCD
->getMaxCharCount(ConversionDirection_FROM_RIGHT
);
83 sal_Int16 SAL_CALL
checkScriptType(sal_Unicode c
)
91 static UBlock2Script scriptList
[] = {
92 {UBLOCK_HANGUL_JAMO
, UBLOCK_HANGUL_JAMO
, SCRIPT_HANGUL
},
93 {UBLOCK_CJK_RADICALS_SUPPLEMENT
, UBLOCK_BOPOMOFO
, SCRIPT_HANJA
},
94 {UBLOCK_HANGUL_COMPATIBILITY_JAMO
, UBLOCK_HANGUL_COMPATIBILITY_JAMO
, SCRIPT_HANGUL
},
95 {UBLOCK_KANBUN
, UBLOCK_YI_RADICALS
, SCRIPT_HANJA
},
96 {UBLOCK_HANGUL_SYLLABLES
, UBLOCK_HANGUL_SYLLABLES
, SCRIPT_HANGUL
},
97 {UBLOCK_CJK_COMPATIBILITY_IDEOGRAPHS
, UBLOCK_CJK_COMPATIBILITY_IDEOGRAPHS
, SCRIPT_HANJA
},
98 {UBLOCK_COMBINING_HALF_MARKS
, UBLOCK_SMALL_FORM_VARIANTS
, SCRIPT_HANJA
},
99 {UBLOCK_HALFWIDTH_AND_FULLWIDTH_FORMS
, UBLOCK_HALFWIDTH_AND_FULLWIDTH_FORMS
, SCRIPT_HANJA
},
102 #define scriptListCount sizeof (scriptList) / sizeof (UBlock2Script)
104 UBlockCode block
=ublock_getCode((sal_uInt32
) c
);
106 for ( i
= 0; i
< scriptListCount
; i
++) {
107 if (block
<= scriptList
[i
].to
) break;
109 return (i
< scriptListCount
&& block
>= scriptList
[i
].from
) ? scriptList
[i
].script
: SCRIPT_OTHERS
;
112 #ifdef DISABLE_DYNLOADING
116 const sal_Unicode
* getHangul2HanjaData();
117 const Hangul_Index
* getHangul2HanjaIndex();
118 sal_Int16
getHangul2HanjaIndexCount();
119 const sal_uInt16
* getHanja2HangulIndex();
120 const sal_Unicode
* getHanja2HangulData();
126 Sequence
< OUString
> SAL_CALL
127 TextConversion_ko::getCharConversions(const OUString
& aText
, sal_Int32 nStartPos
, sal_Int32 nLength
, bool toHanja
)
130 Sequence
< OUString
> output
;
131 #ifndef DISABLE_DYNLOADING
132 const sal_Unicode
* (*getHangul2HanjaData
)() = reinterpret_cast<const sal_Unicode
* (*)()>(getFunctionBySymbol("getHangul2HanjaData"));
133 const Hangul_Index
* (*getHangul2HanjaIndex
)() = reinterpret_cast<const Hangul_Index
* (*)()>(getFunctionBySymbol("getHangul2HanjaIndex"));
134 sal_Int16 (*getHangul2HanjaIndexCount
)() = reinterpret_cast<sal_Int16 (*)()>(getFunctionBySymbol("getHangul2HanjaIndexCount"));
135 const sal_uInt16
* (*getHanja2HangulIndex
)() = reinterpret_cast<const sal_uInt16
* (*)()>(getFunctionBySymbol("getHanja2HangulIndex"));
136 const sal_Unicode
* (*getHanja2HangulData
)() = reinterpret_cast<const sal_Unicode
* (*)()>(getFunctionBySymbol("getHanja2HangulData"));
138 #pragma GCC diagnostic push
140 #pragma GCC diagnostic warning "-Wbool-conversions"
142 #pragma GCC diagnostic warning "-Waddress"
145 if (toHanja
&& getHangul2HanjaIndex
&& getHangul2HanjaIndexCount
&& getHangul2HanjaData
) {
146 ch
= aText
[nStartPos
];
147 const Hangul_Index
*Hangul_ko
= getHangul2HanjaIndex();
148 sal_Int16 top
= getHangul2HanjaIndexCount();
150 sal_Int16 bottom
= 0;
152 while (bottom
<= top
) {
153 sal_Int16 current
= (top
+ bottom
) / 2;
154 sal_Unicode current_ch
= Hangul_ko
[current
].code
;
157 else if (ch
> current_ch
)
158 bottom
= current
+ 1;
160 const sal_Unicode
*ptr
= getHangul2HanjaData() + Hangul_ko
[current
].address
;
161 sal_Int16 count
= Hangul_ko
[current
].count
;
162 output
.realloc(count
);
163 for (sal_Int16 i
= 0; i
< count
; i
++)
164 output
[i
] = OUString(ptr
+ i
, 1);
168 } else if (! toHanja
&& getHanja2HangulIndex
&& getHanja2HangulData
)
170 std::unique_ptr
<sal_Unicode
[]> newStr(new sal_Unicode
[nLength
+1]);
172 while (count
< nLength
)
174 ch
= aText
[nStartPos
+ count
];
175 sal_Unicode address
= getHanja2HangulIndex()[ch
>>8];
176 if (address
!= 0xFFFF)
177 address
= getHanja2HangulData()[address
+ (ch
& 0xFF)];
179 if (address
!= 0xFFFF)
180 newStr
[count
++] = address
;
187 output
[0] = OUString(newStr
.get(), count
);
190 #if defined(DISABLE_DYNLOADING)
191 #pragma GCC diagnostic pop
196 static Sequence
< OUString
>& operator += (Sequence
< OUString
> &rSeq1
, Sequence
< OUString
> &rSeq2
)
198 if (! rSeq1
.hasElements() && rSeq2
.hasElements())
200 else if (rSeq2
.hasElements()) {
201 sal_Int32 i
, j
, k
, l
;
202 k
= l
= rSeq1
.getLength();
203 rSeq1
.realloc(l
+ rSeq2
.getLength());
205 for (i
= 0; i
< rSeq2
.getLength(); i
++) {
206 for (j
= 0; j
< l
; j
++)
207 if (rSeq1
[j
] == rSeq2
[i
])
210 rSeq1
[k
++] = rSeq2
[i
];
212 if (rSeq1
.getLength() > k
)
218 TextConversionResult SAL_CALL
219 TextConversion_ko::getConversions( const OUString
& aText
, sal_Int32 nStartPos
, sal_Int32 nLength
,
220 const Locale
& aLocale
, sal_Int16 nConversionType
, sal_Int32 nConversionOptions
)
221 throw( RuntimeException
, IllegalArgumentException
, NoSupportException
, std::exception
)
223 TextConversionResult result
;
224 Sequence
<OUString
> candidates
;
225 result
.Boundary
.startPos
= result
.Boundary
.endPos
= 0;
227 // do conversion only when there are right conversion type and dictionary services.
228 if (nConversionType
== TextConversionType::TO_HANGUL
||
229 nConversionType
== TextConversionType::TO_HANJA
) {
230 sal_Int32 start
, end
, length
= aText
.getLength() - nStartPos
;
232 if (length
< 0 || nStartPos
< 0)
234 else if (length
> nLength
)
237 sal_Int16 scriptType
= SCRIPT_OTHERS
;
239 bool toHanja
= (nConversionType
== TextConversionType::TO_HANJA
);
240 // FROM_LEFT: Hangul -> Hanja
241 // FROM_RIGHT: Hanja -> Hangul
242 ConversionDirection eDirection
= toHanja
? ConversionDirection_FROM_LEFT
: ConversionDirection_FROM_RIGHT
;
243 sal_Int32 maxLength
= toHanja
? maxLeftLength
: maxRightLength
;
244 if (maxLength
== 0) maxLength
= 1;
246 // search for a max length of convertible text
247 for (start
= 0, end
= 0; start
< length
; start
++) {
249 scriptType
= checkScriptType(aText
[nStartPos
+ start
]);
250 if (nConversionType
== TextConversionType::TO_HANJA
) {
251 if (scriptType
!= SCRIPT_HANGUL
) // skip non-Hangul characters
254 if (scriptType
!= SCRIPT_HANJA
) // skip non-Hanja characters
259 if (nConversionOptions
& TextConversionOption::CHARACTER_BY_CHARACTER
) {
260 result
.Candidates
= getCharConversions(aText
, nStartPos
+ start
, len
, toHanja
); // char2char conversion
262 for (; end
< length
&& end
- start
< maxLength
; end
++)
263 if (checkScriptType(aText
[nStartPos
+ end
]) != scriptType
)
266 for (len
= end
- start
; len
> 0; len
--) {
270 result
.Candidates
= xCDL
->queryConversions(aText
, start
+ nStartPos
, len
,
271 aLocale
, ConversionDictionaryType::HANGUL_HANJA
, eDirection
, nConversionOptions
); // user dictionary
273 catch ( NoSupportException
& ) {
274 // clear reference (when there is no user dictionary) in order
275 // to not always have to catch this exception again
276 // in further calls. (save time)
280 // catch all other exceptions to allow
281 // querying the system dictionary in the next line
283 if (xCD
.is() && toHanja
) { // System dictionary would not do Hanja_to_Hangul conversion.
284 candidates
= xCD
->getConversions(aText
, start
+ nStartPos
, len
, eDirection
, nConversionOptions
);
285 result
.Candidates
+= candidates
;
287 } else if (! toHanja
) { // do whole word character 2 character conversion for Hanja to Hangul conversion
288 result
.Candidates
= getCharConversions(aText
, nStartPos
+ start
, length
- start
, toHanja
);
289 if (result
.Candidates
.hasElements())
290 len
= result
.Candidates
[0].getLength();
292 if (result
.Candidates
.hasElements())
297 if (result
.Candidates
.hasElements()) {
298 result
.Boundary
.startPos
= start
+ nStartPos
;
299 result
.Boundary
.endPos
= start
+ len
+ nStartPos
;
304 throw NoSupportException(); // Conversion type is not supported in this service.
309 TextConversion_ko::getConversion( const OUString
& aText
, sal_Int32 nStartPos
, sal_Int32 nLength
,
310 const Locale
& aLocale
, sal_Int16 nConversionType
, sal_Int32 nConversionOptions
)
311 throw( RuntimeException
, IllegalArgumentException
, NoSupportException
, std::exception
)
313 sal_Int32 length
= aText
.getLength() - nStartPos
;
315 if (length
<= 0 || nStartPos
< 0)
317 else if (length
> nLength
)
320 OUStringBuffer
aBuf(length
+ 1);
321 TextConversionResult result
;
322 const sal_Unicode
*str
= aText
.getStr();
324 for (sal_Int32 start
= nStartPos
; length
+ nStartPos
> start
; start
= result
.Boundary
.endPos
) {
326 result
= getConversions(aText
, start
, length
+ nStartPos
- start
, aLocale
, nConversionType
, nConversionOptions
);
328 if (result
.Boundary
.endPos
> 0) {
329 if (result
.Boundary
.startPos
> start
)
330 aBuf
.append(str
+ start
, result
.Boundary
.startPos
- start
); // append skip portion
331 aBuf
.append(result
.Candidates
[0]); // append converted portion
333 if (length
+ nStartPos
> start
)
334 aBuf
.append(str
+ start
, length
+ nStartPos
- start
); // append last portion
339 return aBuf
.makeStringAndClear();
343 TextConversion_ko::getConversionWithOffset( const OUString
& aText
, sal_Int32 nStartPos
, sal_Int32 nLength
,
344 const Locale
& rLocale
, sal_Int16 nConversionType
, sal_Int32 nConversionOptions
, Sequence
<sal_Int32
>& offset
)
345 throw( RuntimeException
, IllegalArgumentException
, NoSupportException
, std::exception
)
348 return getConversion(aText
, nStartPos
, nLength
, rLocale
, nConversionType
, nConversionOptions
);
352 TextConversion_ko::interactiveConversion( const Locale
& /*rLocale*/, sal_Int16
/*nTextConversionType*/, sal_Int32
/*nTextConversionOptions*/ )
353 throw( RuntimeException
, IllegalArgumentException
, NoSupportException
, std::exception
)
360 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */