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>
30 using namespace com::sun::star::lang
;
31 using namespace com::sun::star::i18n
;
32 using namespace com::sun::star::linguistic2
;
33 using namespace com::sun::star::uno
;
36 namespace com
{ namespace sun
{ namespace star
{ namespace i18n
{
38 #define SCRIPT_OTHERS 0
39 #define SCRIPT_HANJA 1
40 #define SCRIPT_HANGUL 2
42 TextConversion_ko::TextConversion_ko( const Reference
< XComponentContext
>& xContext
)
44 Reference
< XInterface
> xI
;
46 xI
= xContext
->getServiceManager()->createInstanceWithContext(
47 OUString("com.sun.star.i18n.ConversionDictionary_ko"), xContext
);
50 xCD
.set( xI
, UNO_QUERY
);
52 xCDL
= ConversionDictionaryList::create(xContext
);
54 maxLeftLength
= maxRightLength
= 1;
56 // get maximum length of word in dictionary
58 Locale
loc(OUString("ko"),
61 maxLeftLength
= xCDL
->queryMaxCharCount(loc
,
62 ConversionDictionaryType::HANGUL_HANJA
,
63 ConversionDirection_FROM_LEFT
);
64 maxRightLength
= xCDL
->queryMaxCharCount(loc
,
65 ConversionDictionaryType::HANGUL_HANJA
,
66 ConversionDirection_FROM_RIGHT
);
68 sal_Int32 tmp
= xCD
->getMaxCharCount(ConversionDirection_FROM_LEFT
);
69 if (tmp
> maxLeftLength
)
71 tmp
= xCD
->getMaxCharCount(ConversionDirection_FROM_RIGHT
);
72 if (tmp
> maxRightLength
)
75 } else if (xCD
.is()) {
76 maxLeftLength
= xCD
->getMaxCharCount(ConversionDirection_FROM_LEFT
);
77 maxRightLength
= xCD
->getMaxCharCount(ConversionDirection_FROM_RIGHT
);
80 implementationName
= "com.sun.star.i18n.TextConversion_ko";
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
, sal_Bool toHanja
)
130 Sequence
< OUString
> output
;
131 #ifndef DISABLE_DYNLOADING
132 const sal_Unicode
* (*getHangul2HanjaData
)() = (const sal_Unicode
* (*)())getFunctionBySymbol("getHangul2HanjaData");
133 const Hangul_Index
* (*getHangul2HanjaIndex
)() = (const Hangul_Index
* (*)()) getFunctionBySymbol("getHangul2HanjaIndex");
134 sal_Int16 (*getHangul2HanjaIndexCount
)() = (sal_Int16 (*)()) getFunctionBySymbol("getHangul2HanjaIndexCount");
135 const sal_uInt16
* (*getHanja2HangulIndex
)() = (const sal_uInt16
* (*)()) getFunctionBySymbol("getHanja2HangulIndex");
136 const sal_Unicode
* (*getHanja2HangulData
)() = (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 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
, count
);
191 #if defined(DISABLE_DYNLOADING)
192 #pragma GCC diagnostic pop
197 static Sequence
< OUString
>& operator += (Sequence
< OUString
> &rSeq1
, Sequence
< OUString
> &rSeq2
)
199 if (! rSeq1
.hasElements() && rSeq2
.hasElements())
201 else if (rSeq2
.hasElements()) {
202 sal_Int32 i
, j
, k
, l
;
203 k
= l
= rSeq1
.getLength();
204 rSeq1
.realloc(l
+ rSeq2
.getLength());
206 for (i
= 0; i
< rSeq2
.getLength(); i
++) {
207 for (j
= 0; j
< l
; j
++)
208 if (rSeq1
[j
] == rSeq2
[i
])
211 rSeq1
[k
++] = rSeq2
[i
];
213 if (rSeq1
.getLength() > k
)
219 TextConversionResult SAL_CALL
220 TextConversion_ko::getConversions( const OUString
& aText
, sal_Int32 nStartPos
, sal_Int32 nLength
,
221 const Locale
& aLocale
, sal_Int16 nConversionType
, sal_Int32 nConversionOptions
)
222 throw( RuntimeException
, IllegalArgumentException
, NoSupportException
)
224 TextConversionResult result
;
225 Sequence
<OUString
> candidates
;
226 result
.Boundary
.startPos
= result
.Boundary
.endPos
= 0;
228 // do conversion only when there are right conversion type and dictionary services.
229 if (nConversionType
== TextConversionType::TO_HANGUL
||
230 nConversionType
== TextConversionType::TO_HANJA
) {
231 sal_Int32 start
, end
, length
= aText
.getLength() - nStartPos
;
233 if (length
< 0 || nStartPos
< 0)
235 else if (length
> nLength
)
238 sal_Int16 scriptType
= SCRIPT_OTHERS
;
240 sal_Bool toHanja
= (nConversionType
== TextConversionType::TO_HANJA
);
241 // FROM_LEFT: Hangul -> Hanja
242 // FROM_RIGHT: Hanja -> Hangul
243 ConversionDirection eDirection
= toHanja
? ConversionDirection_FROM_LEFT
: ConversionDirection_FROM_RIGHT
;
244 sal_Int32 maxLength
= toHanja
? maxLeftLength
: maxRightLength
;
245 if (maxLength
== 0) maxLength
= 1;
247 // search for a max length of convertible text
248 for (start
= 0, end
= 0; start
< length
; start
++) {
250 scriptType
= checkScriptType(aText
[nStartPos
+ start
]);
251 if (nConversionType
== TextConversionType::TO_HANJA
) {
252 if (scriptType
!= SCRIPT_HANGUL
) // skip non-Hangul characters
255 if (scriptType
!= SCRIPT_HANJA
) // skip non-Hanja characters
260 if (nConversionOptions
& TextConversionOption::CHARACTER_BY_CHARACTER
) {
261 result
.Candidates
= getCharConversions(aText
, nStartPos
+ start
, len
, toHanja
); // char2char conversion
263 for (; end
< length
&& end
- start
< maxLength
; end
++)
264 if (checkScriptType(aText
[nStartPos
+ end
]) != scriptType
)
267 for (len
= end
- start
; len
> 0; len
--) {
271 result
.Candidates
= xCDL
->queryConversions(aText
, start
+ nStartPos
, len
,
272 aLocale
, ConversionDictionaryType::HANGUL_HANJA
, eDirection
, nConversionOptions
); // user dictionary
274 catch ( NoSupportException
& ) {
275 // clear reference (when there is no user dictionary) in order
276 // to not always have to catch this exception again
277 // in further calls. (save time)
281 // catch all other exceptions to allow
282 // querying the system dictionary in the next line
284 if (xCD
.is() && toHanja
) { // System dictionary would not do Hanja_to_Hangul conversion.
285 candidates
= xCD
->getConversions(aText
, start
+ nStartPos
, len
, eDirection
, nConversionOptions
);
286 result
.Candidates
+= candidates
;
288 } else if (! toHanja
) { // do whole word character 2 character conversion for Hanja to Hangul conversion
289 result
.Candidates
= getCharConversions(aText
, nStartPos
+ start
, length
- start
, toHanja
);
290 if (result
.Candidates
.hasElements())
291 len
= result
.Candidates
[0].getLength();
293 if (result
.Candidates
.hasElements())
298 if (result
.Candidates
.hasElements()) {
299 result
.Boundary
.startPos
= start
+ nStartPos
;;
300 result
.Boundary
.endPos
= start
+ len
+ nStartPos
;
305 throw NoSupportException(); // Conversion type is not supported in this service.
310 TextConversion_ko::getConversion( const OUString
& aText
, sal_Int32 nStartPos
, sal_Int32 nLength
,
311 const Locale
& aLocale
, sal_Int16 nConversionType
, sal_Int32 nConversionOptions
)
312 throw( RuntimeException
, IllegalArgumentException
, NoSupportException
)
314 sal_Int32 length
= aText
.getLength() - nStartPos
;
316 if (length
<= 0 || nStartPos
< 0)
318 else if (length
> nLength
)
321 OUStringBuffer
aBuf(length
+ 1);
322 TextConversionResult result
;
323 const sal_Unicode
*str
= aText
.getStr();
325 for (sal_Int32 start
= nStartPos
; length
+ nStartPos
> start
; start
= result
.Boundary
.endPos
) {
327 result
= getConversions(aText
, start
, length
+ nStartPos
- start
, aLocale
, nConversionType
, nConversionOptions
);
329 if (result
.Boundary
.endPos
> 0) {
330 if (result
.Boundary
.startPos
> start
)
331 aBuf
.append(str
+ start
, result
.Boundary
.startPos
- start
); // append skip portion
332 aBuf
.append(result
.Candidates
[0]); // append converted portion
334 if (length
+ nStartPos
> start
)
335 aBuf
.append(str
+ start
, length
+ nStartPos
- start
); // append last portion
340 return aBuf
.makeStringAndClear();
344 TextConversion_ko::getConversionWithOffset( const OUString
& aText
, sal_Int32 nStartPos
, sal_Int32 nLength
,
345 const Locale
& rLocale
, sal_Int16 nConversionType
, sal_Int32 nConversionOptions
, Sequence
<sal_Int32
>& offset
)
346 throw( RuntimeException
, IllegalArgumentException
, NoSupportException
)
349 return getConversion(aText
, nStartPos
, nLength
, rLocale
, nConversionType
, nConversionOptions
);
353 TextConversion_ko::interactiveConversion( const Locale
& /*rLocale*/, sal_Int16
/*nTextConversionType*/, sal_Int32
/*nTextConversionOptions*/ )
354 throw( RuntimeException
, IllegalArgumentException
, NoSupportException
)
361 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */