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 u
"com.sun.star.i18n.ConversionDictionary_ko"_ustr
, 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(u
"ko"_ustr
, u
"KR"_ustr
, 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 TextConversion_ko::getCharConversions(std::u16string_view aText
, sal_Int32 nStartPos
, sal_Int32 nLength
, bool toHanja
)
114 Sequence
< OUString
> output
;
117 ch
= aText
[nStartPos
];
118 const Hangul_Index
*Hangul_ko
= getHangul2HanjaIndex();
119 sal_Int16 top
= getHangul2HanjaIndexCount();
121 sal_Int16 bottom
= 0;
123 while (bottom
<= top
) {
124 sal_Int16 current
= (top
+ bottom
) / 2;
125 sal_Unicode current_ch
= Hangul_ko
[current
].code
;
128 else if (ch
> current_ch
)
129 bottom
= current
+ 1;
131 const sal_Unicode
*ptr
= getHangul2HanjaData() + Hangul_ko
[current
].address
;
132 sal_Int16 count
= Hangul_ko
[current
].count
;
133 output
.realloc(count
);
134 auto poutput
= output
.getArray();
135 for (sal_Int16 i
= 0; i
< count
; i
++)
136 poutput
[i
] = OUString(ptr
+ i
, 1);
143 std::unique_ptr
<sal_Unicode
[]> newStr(new sal_Unicode
[nLength
+1]);
145 while (count
< nLength
)
147 ch
= aText
[nStartPos
+ count
];
148 sal_Unicode address
= getHanja2HangulIndex()[ch
>>8];
149 if (address
!= 0xFFFF)
150 address
= getHanja2HangulData()[address
+ (ch
& 0xFF)];
152 if (address
!= 0xFFFF)
153 newStr
[count
++] = address
;
159 output
= { OUString(newStr
.get(), count
) };
165 static Sequence
< OUString
>& operator += (Sequence
< OUString
> &rSeq1
, const Sequence
< OUString
> &rSeq2
)
167 if (! rSeq1
.hasElements() && rSeq2
.hasElements())
169 else if (rSeq2
.hasElements())
170 rSeq1
= comphelper::combineSequences(rSeq1
, rSeq2
);
175 TextConversionResult SAL_CALL
176 TextConversion_ko::getConversions( const OUString
& aText
, sal_Int32 nStartPos
, sal_Int32 nLength
,
177 const Locale
& aLocale
, sal_Int16 nConversionType
, sal_Int32 nConversionOptions
)
179 TextConversionResult result
;
180 Sequence
<OUString
> candidates
;
181 result
.Boundary
.startPos
= result
.Boundary
.endPos
= 0;
183 // do conversion only when there are right conversion type and dictionary services.
184 if (nConversionType
!= TextConversionType::TO_HANGUL
&&
185 nConversionType
!= TextConversionType::TO_HANJA
)
186 throw NoSupportException(); // Conversion type is not supported in this service.
187 sal_Int32 start
, end
, length
= aText
.getLength() - nStartPos
;
189 if (length
< 0 || nStartPos
< 0)
191 else if (length
> nLength
)
194 sal_Int16 scriptType
= SCRIPT_OTHERS
;
196 bool toHanja
= (nConversionType
== TextConversionType::TO_HANJA
);
197 // FROM_LEFT: Hangul -> Hanja
198 // FROM_RIGHT: Hanja -> Hangul
199 ConversionDirection eDirection
= toHanja
? ConversionDirection_FROM_LEFT
: ConversionDirection_FROM_RIGHT
;
200 sal_Int32 maxLength
= toHanja
? maxLeftLength
: maxRightLength
;
201 if (maxLength
== 0) maxLength
= 1;
203 // search for a max length of convertible text
204 for (start
= 0, end
= 0; start
< length
; start
++) {
206 scriptType
= checkScriptType(aText
[nStartPos
+ start
]);
207 if (nConversionType
== TextConversionType::TO_HANJA
) {
208 if (scriptType
!= SCRIPT_HANGUL
) // skip non-Hangul characters
211 if (scriptType
!= SCRIPT_HANJA
) // skip non-Hanja characters
216 if (nConversionOptions
& TextConversionOption::CHARACTER_BY_CHARACTER
) {
217 result
.Candidates
= getCharConversions(aText
, nStartPos
+ start
, len
, toHanja
); // char2char conversion
219 for (; end
< length
&& end
- start
< maxLength
; end
++)
220 if (checkScriptType(aText
[nStartPos
+ end
]) != scriptType
)
223 for (len
= end
- start
; len
> 0; len
--) {
227 result
.Candidates
= xCDL
->queryConversions(aText
, start
+ nStartPos
, len
,
228 aLocale
, ConversionDictionaryType::HANGUL_HANJA
, eDirection
, nConversionOptions
); // user dictionary
230 catch ( NoSupportException
& ) {
231 // clear reference (when there is no user dictionary) in order
232 // to not always have to catch this exception again
233 // in further calls. (save time)
237 // catch all other exceptions to allow
238 // querying the system dictionary in the next line
240 if (xCD
.is() && toHanja
) { // System dictionary would not do Hanja_to_Hangul conversion.
241 candidates
= xCD
->getConversions(aText
, start
+ nStartPos
, len
, eDirection
, nConversionOptions
);
242 result
.Candidates
+= candidates
;
244 } else if (! toHanja
) { // do whole word character 2 character conversion for Hanja to Hangul conversion
245 result
.Candidates
= getCharConversions(aText
, nStartPos
+ start
, length
- start
, toHanja
);
246 if (result
.Candidates
.hasElements())
247 len
= result
.Candidates
[0].getLength();
249 if (result
.Candidates
.hasElements())
254 if (result
.Candidates
.hasElements()) {
255 result
.Boundary
.startPos
= start
+ nStartPos
;
256 result
.Boundary
.endPos
= start
+ len
+ nStartPos
;
265 TextConversion_ko::getConversion( const OUString
& aText
, sal_Int32 nStartPos
, sal_Int32 nLength
,
266 const Locale
& aLocale
, sal_Int16 nConversionType
, sal_Int32 nConversionOptions
)
268 sal_Int32 length
= aText
.getLength() - nStartPos
;
270 if (length
<= 0 || nStartPos
< 0)
272 else if (length
> nLength
)
275 OUStringBuffer
aBuf(length
+ 1);
276 TextConversionResult result
;
277 const sal_Unicode
*str
= aText
.getStr();
279 for (sal_Int32 start
= nStartPos
; length
+ nStartPos
> start
; start
= result
.Boundary
.endPos
) {
281 result
= getConversions(aText
, start
, length
+ nStartPos
- start
, aLocale
, nConversionType
, nConversionOptions
);
283 if (result
.Boundary
.endPos
> 0) {
284 if (result
.Boundary
.startPos
> start
)
285 aBuf
.append(str
+ start
, result
.Boundary
.startPos
- start
); // append skip portion
286 aBuf
.append(result
.Candidates
[0]); // append converted portion
288 aBuf
.append(str
+ start
, length
+ nStartPos
- start
); // append last portion
293 return aBuf
.makeStringAndClear();
297 TextConversion_ko::getConversionWithOffset( const OUString
& aText
, sal_Int32 nStartPos
, sal_Int32 nLength
,
298 const Locale
& rLocale
, sal_Int16 nConversionType
, sal_Int32 nConversionOptions
, Sequence
<sal_Int32
>& offset
)
301 return getConversion(aText
, nStartPos
, nLength
, rLocale
, nConversionType
, nConversionOptions
);
305 TextConversion_ko::interactiveConversion( const Locale
& /*rLocale*/, sal_Int16
/*nTextConversionType*/, sal_Int32
/*nTextConversionOptions*/ )
312 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */