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 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 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 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 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 203 TextConversion_ko::getConversions( const OUString& aText, sal_Int32 nStartPos, sal_Int32 nLength, 204 const Locale& aLocale, sal_Int16 nConversionType, sal_Int32 nConversionOptions) 205 { 206 TextConversionResult result; 207 Sequence <OUString> candidates; 208 result.Boundary.startPos = result.Boundary.endPos = 0; 209 210 // do conversion only when there are right conversion type and dictionary services. 211 if (nConversionType == TextConversionType::TO_HANGUL || 212 nConversionType == TextConversionType::TO_HANJA) { 213 sal_Int32 start, end, length = aText.getLength() - nStartPos; 214 215 if (length < 0 || nStartPos < 0) 216 length = 0; 217 else if (length > nLength) 218 length = nLength; 219 220 sal_Int16 scriptType = SCRIPT_OTHERS; 221 sal_Int32 len = 1; 222 sal_Bool toHanja = (nConversionType == TextConversionType::TO_HANJA); 223 // FROM_LEFT: Hangul -> Hanja 224 // FROM_RIGHT: Hanja -> Hangul 225 ConversionDirection eDirection = toHanja ? ConversionDirection_FROM_LEFT : ConversionDirection_FROM_RIGHT; 226 sal_Int32 maxLength = toHanja ? maxLeftLength : maxRightLength; 227 if (maxLength == 0) maxLength = 1; 228 229 // search for a max length of convertible text 230 for (start = 0, end = 0; start < length; start++) { 231 if (end <= start) { 232 scriptType = checkScriptType(aText[nStartPos + start]); 233 if (nConversionType == TextConversionType::TO_HANJA) { 234 if (scriptType != SCRIPT_HANGUL) // skip non-Hangul characters 235 continue; 236 } else { 237 if (scriptType != SCRIPT_HANJA) // skip non-Hanja characters 238 continue; 239 } 240 end = start + 1; 241 } 242 if (nConversionOptions & TextConversionOption::CHARACTER_BY_CHARACTER) { 243 result.Candidates = getCharConversions(aText, nStartPos + start, len, toHanja); // char2char conversion 244 } else { 245 for (; end < length && end - start < maxLength; end++) 246 if (checkScriptType(aText[nStartPos + end]) != scriptType) 247 break; 248 249 for (len = end - start; len > 0; len--) { 250 if (len > 1) { 251 try { 252 if (xCDL.is()) 253 result.Candidates = xCDL->queryConversions(aText, start + nStartPos, len, 254 aLocale, ConversionDictionaryType::HANGUL_HANJA, eDirection, nConversionOptions); // user dictionary 255 } 256 catch ( NoSupportException & ) { 257 // clear reference (when there is no user dictionary) in order 258 // to not always have to catch this exception again 259 // in further calls. (save time) 260 xCDL = 0; 261 } 262 catch (...) { 263 // catch all other exceptions to allow 264 // querying the system dictionary in the next line 265 } 266 if (xCD.is() && toHanja) { // System dictionary would not do Hanja_to_Hangul conversion. 267 candidates = xCD->getConversions(aText, start + nStartPos, len, eDirection, nConversionOptions); 268 result.Candidates += candidates; 269 } 270 } else if (! toHanja) { // do whole word character 2 character conversion for Hanja to Hangul conversion 271 result.Candidates = getCharConversions(aText, nStartPos + start, length - start, toHanja); 272 if (result.Candidates.hasElements()) 273 len = result.Candidates[0].getLength(); 274 } 275 if (result.Candidates.hasElements()) 276 break; 277 } 278 } 279 // found match 280 if (result.Candidates.hasElements()) { 281 result.Boundary.startPos = start + nStartPos; 282 result.Boundary.endPos = start + len + nStartPos; 283 return result; 284 } 285 } 286 } else 287 throw NoSupportException(); // Conversion type is not supported in this service. 288 return result; 289 } 290 291 OUString SAL_CALL 292 TextConversion_ko::getConversion( const OUString& aText, sal_Int32 nStartPos, sal_Int32 nLength, 293 const Locale& aLocale, sal_Int16 nConversionType, sal_Int32 nConversionOptions) 294 { 295 sal_Int32 length = aText.getLength() - nStartPos; 296 297 if (length <= 0 || nStartPos < 0) 298 return OUString(); 299 else if (length > nLength) 300 length = nLength; 301 302 OUStringBuffer aBuf(length + 1); 303 TextConversionResult result; 304 const sal_Unicode *str = aText.getStr(); 305 306 for (sal_Int32 start = nStartPos; length + nStartPos > start; start = result.Boundary.endPos) { 307 308 result = getConversions(aText, start, length + nStartPos - start, aLocale, nConversionType, nConversionOptions); 309 310 if (result.Boundary.endPos > 0) { 311 if (result.Boundary.startPos > start) 312 aBuf.append(str + start, result.Boundary.startPos - start); // append skip portion 313 aBuf.append(result.Candidates[0]); // append converted portion 314 } else { 315 if (length + nStartPos > start) 316 aBuf.append(str + start, length + nStartPos - start); // append last portion 317 break; 318 } 319 } 320 321 return aBuf.makeStringAndClear(); 322 } 323 324 OUString SAL_CALL 325 TextConversion_ko::getConversionWithOffset( const OUString& aText, sal_Int32 nStartPos, sal_Int32 nLength, 326 const Locale& rLocale, sal_Int16 nConversionType, sal_Int32 nConversionOptions, Sequence<sal_Int32>& offset) 327 { 328 offset.realloc(0); 329 return getConversion(aText, nStartPos, nLength, rLocale, nConversionType, nConversionOptions); 330 } 331 332 sal_Bool SAL_CALL 333 TextConversion_ko::interactiveConversion( const Locale& /*rLocale*/, sal_Int16 /*nTextConversionType*/, sal_Int32 /*nTextConversionOptions*/ ) 334 { 335 return sal_True; 336 } 337 338 } } } } 339