1 /**************************************************************
2 *
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
18 * under the License.
19 *
20 *************************************************************/
21
22
23
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_i18npool.hxx"
26 #include <assert.h>
27 #include <textconversion.hxx>
28 #include <com/sun/star/i18n/TextConversionType.hpp>
29 #include <com/sun/star/i18n/TextConversionOption.hpp>
30 #include <com/sun/star/linguistic2/ConversionDirection.hpp>
31 #include <com/sun/star/linguistic2/ConversionDictionaryType.hpp>
32 #include <rtl/ustrbuf.hxx>
33 #include <i18nutil/x_rtl_ustring.h>
34 #include <unicode/uchar.h>
35
36 using namespace com::sun::star::lang;
37 using namespace com::sun::star::i18n;
38 using namespace com::sun::star::linguistic2;
39 using namespace com::sun::star::uno;
40 using namespace rtl;
41
42 namespace com { namespace sun { namespace star { namespace i18n {
43
44 #define SCRIPT_OTHERS 0
45 #define SCRIPT_HANJA 1
46 #define SCRIPT_HANGUL 2
47
TextConversion_ko(const Reference<XMultiServiceFactory> & xMSF)48 TextConversion_ko::TextConversion_ko( const Reference < XMultiServiceFactory >& xMSF )
49 {
50 Reference < XInterface > xI;
51
52 xI = xMSF->createInstance(
53 OUString::createFromAscii("com.sun.star.i18n.ConversionDictionary_ko"));
54
55 if ( xI.is() )
56 xI->queryInterface( getCppuType((const Reference< XConversionDictionary>*)0) ) >>= xCD;
57
58 xI = xMSF->createInstance(
59 OUString::createFromAscii( "com.sun.star.linguistic2.ConversionDictionaryList" ));
60
61 if ( xI.is() )
62 xI->queryInterface( getCppuType((const Reference< XConversionDictionaryList>*)0) ) >>= xCDL;
63
64 maxLeftLength = maxRightLength = 1;
65
66 // get maximum length of word in dictionary
67 if (xCDL.is()) {
68 Locale loc(OUString::createFromAscii("ko"),
69 OUString::createFromAscii("KR"),
70 OUString());
71 maxLeftLength = xCDL->queryMaxCharCount(loc,
72 ConversionDictionaryType::HANGUL_HANJA,
73 ConversionDirection_FROM_LEFT);
74 maxRightLength = xCDL->queryMaxCharCount(loc,
75 ConversionDictionaryType::HANGUL_HANJA,
76 ConversionDirection_FROM_RIGHT);
77 if (xCD.is()) {
78 sal_Int32 tmp = xCD->getMaxCharCount(ConversionDirection_FROM_LEFT);
79 if (tmp > maxLeftLength)
80 maxLeftLength = tmp;
81 tmp = xCD->getMaxCharCount(ConversionDirection_FROM_RIGHT);
82 if (tmp > maxRightLength)
83 maxRightLength = tmp;
84 }
85 } else if (xCD.is()) {
86 maxLeftLength = xCD->getMaxCharCount(ConversionDirection_FROM_LEFT);
87 maxRightLength = xCD->getMaxCharCount(ConversionDirection_FROM_RIGHT);
88 }
89
90 implementationName = "com.sun.star.i18n.TextConversion_ko";
91 }
92
checkScriptType(sal_Unicode c)93 sal_Int16 SAL_CALL checkScriptType(sal_Unicode c)
94 {
95 typedef struct {
96 UBlockCode from;
97 UBlockCode to;
98 sal_Int16 script;
99 } UBlock2Script;
100
101 static UBlock2Script scriptList[] = {
102 {UBLOCK_HANGUL_JAMO, UBLOCK_HANGUL_JAMO, SCRIPT_HANGUL},
103 {UBLOCK_CJK_RADICALS_SUPPLEMENT, UBLOCK_BOPOMOFO, SCRIPT_HANJA},
104 {UBLOCK_HANGUL_COMPATIBILITY_JAMO, UBLOCK_HANGUL_COMPATIBILITY_JAMO, SCRIPT_HANGUL},
105 {UBLOCK_KANBUN, UBLOCK_YI_RADICALS, SCRIPT_HANJA},
106 {UBLOCK_HANGUL_SYLLABLES, UBLOCK_HANGUL_SYLLABLES, SCRIPT_HANGUL},
107 {UBLOCK_CJK_COMPATIBILITY_IDEOGRAPHS, UBLOCK_CJK_COMPATIBILITY_IDEOGRAPHS, SCRIPT_HANJA},
108 {UBLOCK_COMBINING_HALF_MARKS, UBLOCK_SMALL_FORM_VARIANTS, SCRIPT_HANJA},
109 {UBLOCK_HALFWIDTH_AND_FULLWIDTH_FORMS, UBLOCK_HALFWIDTH_AND_FULLWIDTH_FORMS, SCRIPT_HANJA},
110 };
111
112 #define scriptListCount sizeof (scriptList) / sizeof (UBlock2Script)
113
114 UBlockCode block=ublock_getCode((sal_uInt32) c);
115 sal_uInt16 i;
116 for ( i = 0; i < scriptListCount; i++) {
117 if (block <= scriptList[i].to) break;
118 }
119 return (i < scriptListCount && block >= scriptList[i].from) ? scriptList[i].script : SCRIPT_OTHERS;
120 }
121
122 Sequence< OUString > SAL_CALL
getCharConversions(const OUString & aText,sal_Int32 nStartPos,sal_Int32 nLength,sal_Bool toHanja)123 TextConversion_ko::getCharConversions(const OUString& aText, sal_Int32 nStartPos, sal_Int32 nLength, sal_Bool toHanja)
124 {
125 sal_Unicode ch;
126 Sequence< OUString > output;
127 const sal_Unicode* (*getHangul2HanjaData)() = (const sal_Unicode* (*)())getFunctionBySymbol("getHangul2HanjaData");
128 const Hangul_Index* (*getHangul2HanjaIndex)() = (const Hangul_Index* (*)()) getFunctionBySymbol("getHangul2HanjaIndex");
129 sal_Int16 (*getHangul2HanjaIndexCount)() = (sal_Int16 (*)()) getFunctionBySymbol("getHangul2HanjaIndexCount");
130 const sal_uInt16* (*getHanja2HangulIndex)() = (const sal_uInt16* (*)()) getFunctionBySymbol("getHanja2HangulIndex");
131 const sal_Unicode* (*getHanja2HangulData)() = (const sal_Unicode* (*)()) getFunctionBySymbol("getHanja2HangulData");
132 if (toHanja && getHangul2HanjaIndex && getHangul2HanjaIndexCount && getHangul2HanjaData) {
133 ch = aText[nStartPos];
134 const Hangul_Index *Hangul_ko = getHangul2HanjaIndex();
135 sal_Int16 top = getHangul2HanjaIndexCount();
136 --top;
137 sal_Int16 bottom = 0;
138
139 while (bottom <= top) {
140 sal_Int16 current = (top + bottom) / 2;
141 sal_Unicode current_ch = Hangul_ko[current].code;
142 if (ch < current_ch)
143 top = current - 1;
144 else if (ch > current_ch)
145 bottom = current + 1;
146 else {
147 const sal_Unicode *ptr = getHangul2HanjaData() + Hangul_ko[current].address;
148 sal_Int16 count = Hangul_ko[current].count;
149 output.realloc(count);
150 for (sal_Int16 i = 0; i < count; i++)
151 output[i] = OUString(ptr + i, 1);
152 break;
153 }
154 }
155 } else if (! toHanja && getHanja2HangulIndex && getHanja2HangulData) {
156 rtl_uString * newStr = x_rtl_uString_new_WithLength( nLength ); // defined in x_rtl_ustring.h
157 sal_Int32 count = 0;
158 while (count < nLength)
159 {
160 ch = aText[nStartPos + count];
161 sal_Unicode address = getHanja2HangulIndex()[ch>>8];
162 if (address != 0xFFFF)
163 address = getHanja2HangulData()[address + (ch & 0xFF)];
164
165 if (address != 0xFFFF)
166 newStr->buffer[count++] = address;
167 else
168 break;
169 }
170 if (count > 0)
171 {
172 output.realloc(1);
173 output[0] = OUString( newStr->buffer, count );
174 }
175 x_rtl_uString_release( newStr );
176 }
177 return output;
178 }
179
operator +=(Sequence<OUString> & rSeq1,Sequence<OUString> & rSeq2)180 static Sequence< OUString >& operator += (Sequence< OUString > &rSeq1, Sequence< OUString > &rSeq2 )
181 {
182 if (! rSeq1.hasElements() && rSeq2.hasElements())
183 rSeq1 = rSeq2;
184 else if (rSeq2.hasElements()) {
185 sal_Int32 i, j, k, l;
186 k = l = rSeq1.getLength();
187 rSeq1.realloc(l + rSeq2.getLength());
188
189 for (i = 0; i < rSeq2.getLength(); i++) {
190 for (j = 0; j < l; j++)
191 if (rSeq1[j] == rSeq2[i])
192 break;
193 if (j == l)
194 rSeq1[k++] = rSeq2[i];
195 }
196 if (rSeq1.getLength() > k)
197 rSeq1.realloc(k);
198 }
199 return rSeq1;
200 }
201
202 TextConversionResult SAL_CALL
getConversions(const OUString & aText,sal_Int32 nStartPos,sal_Int32 nLength,const Locale & aLocale,sal_Int16 nConversionType,sal_Int32 nConversionOptions)203 TextConversion_ko::getConversions( const OUString& aText, sal_Int32 nStartPos, sal_Int32 nLength,
204 const Locale& aLocale, sal_Int16 nConversionType, sal_Int32 nConversionOptions)
205 throw( RuntimeException, IllegalArgumentException, NoSupportException )
206 {
207 TextConversionResult result;
208 Sequence <OUString> candidates;
209 result.Boundary.startPos = result.Boundary.endPos = 0;
210
211 // do conversion only when there are right conversion type and dictionary services.
212 if (nConversionType == TextConversionType::TO_HANGUL ||
213 nConversionType == TextConversionType::TO_HANJA) {
214 sal_Int32 start, end, length = aText.getLength() - nStartPos;
215
216 if (length < 0 || nStartPos < 0)
217 length = 0;
218 else if (length > nLength)
219 length = nLength;
220
221 sal_Int16 scriptType = SCRIPT_OTHERS;
222 sal_Int32 len = 1;
223 sal_Bool toHanja = (nConversionType == TextConversionType::TO_HANJA);
224 // FROM_LEFT: Hangul -> Hanja
225 // FROM_RIGHT: Hanja -> Hangul
226 ConversionDirection eDirection = toHanja ? ConversionDirection_FROM_LEFT : ConversionDirection_FROM_RIGHT;
227 sal_Int32 maxLength = toHanja ? maxLeftLength : maxRightLength;
228 if (maxLength == 0) maxLength = 1;
229
230 // search for a max length of convertible text
231 for (start = 0, end = 0; start < length; start++) {
232 if (end <= start) {
233 scriptType = checkScriptType(aText[nStartPos + start]);
234 if (nConversionType == TextConversionType::TO_HANJA) {
235 if (scriptType != SCRIPT_HANGUL) // skip non-Hangul characters
236 continue;
237 } else {
238 if (scriptType != SCRIPT_HANJA) // skip non-Hanja characters
239 continue;
240 }
241 end = start + 1;
242 }
243 if (nConversionOptions & TextConversionOption::CHARACTER_BY_CHARACTER) {
244 result.Candidates = getCharConversions(aText, nStartPos + start, len, toHanja); // char2char conversion
245 } else {
246 for (; end < length && end - start < maxLength; end++)
247 if (checkScriptType(aText[nStartPos + end]) != scriptType)
248 break;
249
250 for (len = end - start; len > 0; len--) {
251 if (len > 1) {
252 try {
253 if (xCDL.is())
254 result.Candidates = xCDL->queryConversions(aText, start + nStartPos, len,
255 aLocale, ConversionDictionaryType::HANGUL_HANJA, eDirection, nConversionOptions); // user dictionary
256 }
257 catch ( NoSupportException & ) {
258 // clear reference (when there is no user dictionary) in order
259 // to not always have to catch this exception again
260 // in further calls. (save time)
261 xCDL = 0;
262 }
263 catch (...) {
264 // catch all other exceptions to allow
265 // querying the system dictionary in the next line
266 }
267 if (xCD.is() && toHanja) { // System dictionary would not do Hanja_to_Hangul conversion.
268 candidates = xCD->getConversions(aText, start + nStartPos, len, eDirection, nConversionOptions);
269 result.Candidates += candidates;
270 }
271 } else if (! toHanja) { // do whole word character 2 character conversion for Hanja to Hangul conversion
272 result.Candidates = getCharConversions(aText, nStartPos + start, length - start, toHanja);
273 if (result.Candidates.hasElements())
274 len = result.Candidates[0].getLength();
275 }
276 if (result.Candidates.hasElements())
277 break;
278 }
279 }
280 // found match
281 if (result.Candidates.hasElements()) {
282 result.Boundary.startPos = start + nStartPos;;
283 result.Boundary.endPos = start + len + nStartPos;
284 return result;
285 }
286 }
287 } else
288 throw NoSupportException(); // Conversion type is not supported in this service.
289 return result;
290 }
291
292 OUString SAL_CALL
getConversion(const OUString & aText,sal_Int32 nStartPos,sal_Int32 nLength,const Locale & aLocale,sal_Int16 nConversionType,sal_Int32 nConversionOptions)293 TextConversion_ko::getConversion( const OUString& aText, sal_Int32 nStartPos, sal_Int32 nLength,
294 const Locale& aLocale, sal_Int16 nConversionType, sal_Int32 nConversionOptions)
295 throw( RuntimeException, IllegalArgumentException, NoSupportException )
296 {
297 sal_Int32 length = aText.getLength() - nStartPos;
298
299 if (length <= 0 || nStartPos < 0)
300 return OUString();
301 else if (length > nLength)
302 length = nLength;
303
304 OUStringBuffer aBuf(length + 1);
305 TextConversionResult result;
306 const sal_Unicode *str = aText.getStr();
307
308 for (sal_Int32 start = nStartPos; length + nStartPos > start; start = result.Boundary.endPos) {
309
310 result = getConversions(aText, start, length + nStartPos - start, aLocale, nConversionType, nConversionOptions);
311
312 if (result.Boundary.endPos > 0) {
313 if (result.Boundary.startPos > start)
314 aBuf.append(str + start, result.Boundary.startPos - start); // append skip portion
315 aBuf.append(result.Candidates[0]); // append converted portion
316 } else {
317 if (length + nStartPos > start)
318 aBuf.append(str + start, length + nStartPos - start); // append last portion
319 break;
320 }
321 }
322
323 return aBuf.makeStringAndClear();
324 }
325
326 OUString SAL_CALL
getConversionWithOffset(const OUString & aText,sal_Int32 nStartPos,sal_Int32 nLength,const Locale & rLocale,sal_Int16 nConversionType,sal_Int32 nConversionOptions,Sequence<sal_Int32> & offset)327 TextConversion_ko::getConversionWithOffset( const OUString& aText, sal_Int32 nStartPos, sal_Int32 nLength,
328 const Locale& rLocale, sal_Int16 nConversionType, sal_Int32 nConversionOptions, Sequence<sal_Int32>& offset)
329 throw( RuntimeException, IllegalArgumentException, NoSupportException )
330 {
331 offset.realloc(0);
332 return getConversion(aText, nStartPos, nLength, rLocale, nConversionType, nConversionOptions);
333 }
334
335 sal_Bool SAL_CALL
interactiveConversion(const Locale &,sal_Int16,sal_Int32)336 TextConversion_ko::interactiveConversion( const Locale& /*rLocale*/, sal_Int16 /*nTextConversionType*/, sal_Int32 /*nTextConversionOptions*/ )
337 throw( RuntimeException, IllegalArgumentException, NoSupportException )
338 {
339 return sal_True;
340 }
341
342 } } } }
343