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