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 <rtl/ustrbuf.hxx>
27 #include <unicode/uchar.h>
29 using namespace com::sun::star::lang
;
30 using namespace com::sun::star::i18n
;
31 using namespace com::sun::star::linguistic2
;
32 using namespace com::sun::star::uno
;
34 using ::rtl::OUString
;
35 using ::rtl::OUStringBuffer
;
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
< XMultiServiceFactory
>& xMSF
)
45 Reference
< XInterface
> xI
;
47 xI
= xMSF
->createInstance(
48 OUString("com.sun.star.i18n.ConversionDictionary_ko"));
51 xI
->queryInterface( getCppuType((const Reference
< XConversionDictionary
>*)0) ) >>= xCD
;
53 xI
= xMSF
->createInstance(
54 OUString("com.sun.star.linguistic2.ConversionDictionaryList"));
57 xI
->queryInterface( getCppuType((const Reference
< XConversionDictionaryList
>*)0) ) >>= xCDL
;
59 maxLeftLength
= maxRightLength
= 1;
61 // get maximum length of word in dictionary
63 Locale
loc(OUString("ko"),
66 maxLeftLength
= xCDL
->queryMaxCharCount(loc
,
67 ConversionDictionaryType::HANGUL_HANJA
,
68 ConversionDirection_FROM_LEFT
);
69 maxRightLength
= xCDL
->queryMaxCharCount(loc
,
70 ConversionDictionaryType::HANGUL_HANJA
,
71 ConversionDirection_FROM_RIGHT
);
73 sal_Int32 tmp
= xCD
->getMaxCharCount(ConversionDirection_FROM_LEFT
);
74 if (tmp
> maxLeftLength
)
76 tmp
= xCD
->getMaxCharCount(ConversionDirection_FROM_RIGHT
);
77 if (tmp
> maxRightLength
)
80 } else if (xCD
.is()) {
81 maxLeftLength
= xCD
->getMaxCharCount(ConversionDirection_FROM_LEFT
);
82 maxRightLength
= xCD
->getMaxCharCount(ConversionDirection_FROM_RIGHT
);
85 implementationName
= "com.sun.star.i18n.TextConversion_ko";
88 sal_Int16 SAL_CALL
checkScriptType(sal_Unicode c
)
96 static UBlock2Script scriptList
[] = {
97 {UBLOCK_HANGUL_JAMO
, UBLOCK_HANGUL_JAMO
, SCRIPT_HANGUL
},
98 {UBLOCK_CJK_RADICALS_SUPPLEMENT
, UBLOCK_BOPOMOFO
, SCRIPT_HANJA
},
99 {UBLOCK_HANGUL_COMPATIBILITY_JAMO
, UBLOCK_HANGUL_COMPATIBILITY_JAMO
, SCRIPT_HANGUL
},
100 {UBLOCK_KANBUN
, UBLOCK_YI_RADICALS
, SCRIPT_HANJA
},
101 {UBLOCK_HANGUL_SYLLABLES
, UBLOCK_HANGUL_SYLLABLES
, SCRIPT_HANGUL
},
102 {UBLOCK_CJK_COMPATIBILITY_IDEOGRAPHS
, UBLOCK_CJK_COMPATIBILITY_IDEOGRAPHS
, SCRIPT_HANJA
},
103 {UBLOCK_COMBINING_HALF_MARKS
, UBLOCK_SMALL_FORM_VARIANTS
, SCRIPT_HANJA
},
104 {UBLOCK_HALFWIDTH_AND_FULLWIDTH_FORMS
, UBLOCK_HALFWIDTH_AND_FULLWIDTH_FORMS
, SCRIPT_HANJA
},
107 #define scriptListCount sizeof (scriptList) / sizeof (UBlock2Script)
109 UBlockCode block
=ublock_getCode((sal_uInt32
) c
);
111 for ( i
= 0; i
< scriptListCount
; i
++) {
112 if (block
<= scriptList
[i
].to
) break;
114 return (i
< scriptListCount
&& block
>= scriptList
[i
].from
) ? scriptList
[i
].script
: SCRIPT_OTHERS
;
117 #ifdef DISABLE_DYNLOADING
121 const sal_Unicode
* getHangul2HanjaData();
122 const Hangul_Index
* getHangul2HanjaIndex();
123 sal_Int16
getHangul2HanjaIndexCount();
124 const sal_uInt16
* getHanja2HangulIndex();
125 const sal_Unicode
* getHanja2HangulData();
131 Sequence
< OUString
> SAL_CALL
132 TextConversion_ko::getCharConversions(const OUString
& aText
, sal_Int32 nStartPos
, sal_Int32 nLength
, sal_Bool toHanja
)
135 Sequence
< OUString
> output
;
136 #ifndef DISABLE_DYNLOADING
137 const sal_Unicode
* (*getHangul2HanjaData
)() = (const sal_Unicode
* (*)())getFunctionBySymbol("getHangul2HanjaData");
138 const Hangul_Index
* (*getHangul2HanjaIndex
)() = (const Hangul_Index
* (*)()) getFunctionBySymbol("getHangul2HanjaIndex");
139 sal_Int16 (*getHangul2HanjaIndexCount
)() = (sal_Int16 (*)()) getFunctionBySymbol("getHangul2HanjaIndexCount");
140 const sal_uInt16
* (*getHanja2HangulIndex
)() = (const sal_uInt16
* (*)()) getFunctionBySymbol("getHanja2HangulIndex");
141 const sal_Unicode
* (*getHanja2HangulData
)() = (const sal_Unicode
* (*)()) getFunctionBySymbol("getHanja2HangulData");
144 #pragma GCC diagnostic push
146 #pragma GCC diagnostic warning "-Wbool-conversions"
148 #pragma GCC diagnostic warning "-Waddress"
152 if (toHanja
&& getHangul2HanjaIndex
&& getHangul2HanjaIndexCount
&& getHangul2HanjaData
) {
153 ch
= aText
[nStartPos
];
154 const Hangul_Index
*Hangul_ko
= getHangul2HanjaIndex();
155 sal_Int16 top
= getHangul2HanjaIndexCount();
157 sal_Int16 bottom
= 0;
159 while (bottom
<= top
) {
160 sal_Int16 current
= (top
+ bottom
) / 2;
161 sal_Unicode current_ch
= Hangul_ko
[current
].code
;
164 else if (ch
> current_ch
)
165 bottom
= current
+ 1;
167 const sal_Unicode
*ptr
= getHangul2HanjaData() + Hangul_ko
[current
].address
;
168 sal_Int16 count
= Hangul_ko
[current
].count
;
169 output
.realloc(count
);
170 for (sal_Int16 i
= 0; i
< count
; i
++)
171 output
[i
] = OUString(ptr
+ i
, 1);
175 } else if (! toHanja
&& getHanja2HangulIndex
&& getHanja2HangulData
)
177 sal_Unicode
*newStr
= new sal_Unicode
[nLength
+1];
179 while (count
< nLength
)
181 ch
= aText
[nStartPos
+ count
];
182 sal_Unicode address
= getHanja2HangulIndex()[ch
>>8];
183 if (address
!= 0xFFFF)
184 address
= getHanja2HangulData()[address
+ (ch
& 0xFF)];
186 if (address
!= 0xFFFF)
187 newStr
[count
++] = address
;
194 output
[0] = OUString(newStr
, count
);
198 #if defined(DISABLE_DYNLOADING) && defined(LIBO_WERROR)
199 #pragma GCC diagnostic pop
204 static Sequence
< OUString
>& operator += (Sequence
< OUString
> &rSeq1
, Sequence
< OUString
> &rSeq2
)
206 if (! rSeq1
.hasElements() && rSeq2
.hasElements())
208 else if (rSeq2
.hasElements()) {
209 sal_Int32 i
, j
, k
, l
;
210 k
= l
= rSeq1
.getLength();
211 rSeq1
.realloc(l
+ rSeq2
.getLength());
213 for (i
= 0; i
< rSeq2
.getLength(); i
++) {
214 for (j
= 0; j
< l
; j
++)
215 if (rSeq1
[j
] == rSeq2
[i
])
218 rSeq1
[k
++] = rSeq2
[i
];
220 if (rSeq1
.getLength() > k
)
226 TextConversionResult SAL_CALL
227 TextConversion_ko::getConversions( const OUString
& aText
, sal_Int32 nStartPos
, sal_Int32 nLength
,
228 const Locale
& aLocale
, sal_Int16 nConversionType
, sal_Int32 nConversionOptions
)
229 throw( RuntimeException
, IllegalArgumentException
, NoSupportException
)
231 TextConversionResult result
;
232 Sequence
<OUString
> candidates
;
233 result
.Boundary
.startPos
= result
.Boundary
.endPos
= 0;
235 // do conversion only when there are right conversion type and dictionary services.
236 if (nConversionType
== TextConversionType::TO_HANGUL
||
237 nConversionType
== TextConversionType::TO_HANJA
) {
238 sal_Int32 start
, end
, length
= aText
.getLength() - nStartPos
;
240 if (length
< 0 || nStartPos
< 0)
242 else if (length
> nLength
)
245 sal_Int16 scriptType
= SCRIPT_OTHERS
;
247 sal_Bool toHanja
= (nConversionType
== TextConversionType::TO_HANJA
);
248 // FROM_LEFT: Hangul -> Hanja
249 // FROM_RIGHT: Hanja -> Hangul
250 ConversionDirection eDirection
= toHanja
? ConversionDirection_FROM_LEFT
: ConversionDirection_FROM_RIGHT
;
251 sal_Int32 maxLength
= toHanja
? maxLeftLength
: maxRightLength
;
252 if (maxLength
== 0) maxLength
= 1;
254 // search for a max length of convertible text
255 for (start
= 0, end
= 0; start
< length
; start
++) {
257 scriptType
= checkScriptType(aText
[nStartPos
+ start
]);
258 if (nConversionType
== TextConversionType::TO_HANJA
) {
259 if (scriptType
!= SCRIPT_HANGUL
) // skip non-Hangul characters
262 if (scriptType
!= SCRIPT_HANJA
) // skip non-Hanja characters
267 if (nConversionOptions
& TextConversionOption::CHARACTER_BY_CHARACTER
) {
268 result
.Candidates
= getCharConversions(aText
, nStartPos
+ start
, len
, toHanja
); // char2char conversion
270 for (; end
< length
&& end
- start
< maxLength
; end
++)
271 if (checkScriptType(aText
[nStartPos
+ end
]) != scriptType
)
274 for (len
= end
- start
; len
> 0; len
--) {
278 result
.Candidates
= xCDL
->queryConversions(aText
, start
+ nStartPos
, len
,
279 aLocale
, ConversionDictionaryType::HANGUL_HANJA
, eDirection
, nConversionOptions
); // user dictionary
281 catch ( NoSupportException
& ) {
282 // clear reference (when there is no user dictionary) in order
283 // to not always have to catch this exception again
284 // in further calls. (save time)
288 // catch all other exceptions to allow
289 // querying the system dictionary in the next line
291 if (xCD
.is() && toHanja
) { // System dictionary would not do Hanja_to_Hangul conversion.
292 candidates
= xCD
->getConversions(aText
, start
+ nStartPos
, len
, eDirection
, nConversionOptions
);
293 result
.Candidates
+= candidates
;
295 } else if (! toHanja
) { // do whole word character 2 character conversion for Hanja to Hangul conversion
296 result
.Candidates
= getCharConversions(aText
, nStartPos
+ start
, length
- start
, toHanja
);
297 if (result
.Candidates
.hasElements())
298 len
= result
.Candidates
[0].getLength();
300 if (result
.Candidates
.hasElements())
305 if (result
.Candidates
.hasElements()) {
306 result
.Boundary
.startPos
= start
+ nStartPos
;;
307 result
.Boundary
.endPos
= start
+ len
+ nStartPos
;
312 throw NoSupportException(); // Conversion type is not supported in this service.
317 TextConversion_ko::getConversion( const OUString
& aText
, sal_Int32 nStartPos
, sal_Int32 nLength
,
318 const Locale
& aLocale
, sal_Int16 nConversionType
, sal_Int32 nConversionOptions
)
319 throw( RuntimeException
, IllegalArgumentException
, NoSupportException
)
321 sal_Int32 length
= aText
.getLength() - nStartPos
;
323 if (length
<= 0 || nStartPos
< 0)
325 else if (length
> nLength
)
328 OUStringBuffer
aBuf(length
+ 1);
329 TextConversionResult result
;
330 const sal_Unicode
*str
= aText
.getStr();
332 for (sal_Int32 start
= nStartPos
; length
+ nStartPos
> start
; start
= result
.Boundary
.endPos
) {
334 result
= getConversions(aText
, start
, length
+ nStartPos
- start
, aLocale
, nConversionType
, nConversionOptions
);
336 if (result
.Boundary
.endPos
> 0) {
337 if (result
.Boundary
.startPos
> start
)
338 aBuf
.append(str
+ start
, result
.Boundary
.startPos
- start
); // append skip portion
339 aBuf
.append(result
.Candidates
[0]); // append converted portion
341 if (length
+ nStartPos
> start
)
342 aBuf
.append(str
+ start
, length
+ nStartPos
- start
); // append last portion
347 return aBuf
.makeStringAndClear();
351 TextConversion_ko::getConversionWithOffset( const OUString
& aText
, sal_Int32 nStartPos
, sal_Int32 nLength
,
352 const Locale
& rLocale
, sal_Int16 nConversionType
, sal_Int32 nConversionOptions
, Sequence
<sal_Int32
>& offset
)
353 throw( RuntimeException
, IllegalArgumentException
, NoSupportException
)
356 return getConversion(aText
, nStartPos
, nLength
, rLocale
, nConversionType
, nConversionOptions
);
360 TextConversion_ko::interactiveConversion( const Locale
& /*rLocale*/, sal_Int16
/*nTextConversionType*/, sal_Int32
/*nTextConversionOptions*/ )
361 throw( RuntimeException
, IllegalArgumentException
, NoSupportException
)
368 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */