LCOV - code coverage report
Current view: top level - i18npool/source/textconversion - textconversion_ko.cxx (source / functions) Hit Total Coverage
Test: commit e02a6cb2c3e2b23b203b422e4e0680877f232636 Lines: 0 169 0.0 %
Date: 2014-04-14 Functions: 0 8 0.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
       2             : /*
       3             :  * This file is part of the LibreOffice project.
       4             :  *
       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/.
       8             :  *
       9             :  * This file incorporates work covered by the following license notice:
      10             :  *
      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 .
      18             :  */
      19             : 
      20             : #include <assert.h>
      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>
      29             : 
      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;
      34             : 
      35             : 
      36             : namespace com { namespace sun { namespace star { namespace i18n {
      37             : 
      38             : #define SCRIPT_OTHERS   0
      39             : #define SCRIPT_HANJA    1
      40             : #define SCRIPT_HANGUL   2
      41             : 
      42           0 : TextConversion_ko::TextConversion_ko( const Reference < XComponentContext >& xContext )
      43           0 :     : TextConversion("com.sun.star.i18n.TextConversion_ko")
      44             : {
      45           0 :     Reference < XInterface > xI;
      46             : 
      47           0 :     xI = xContext->getServiceManager()->createInstanceWithContext(
      48           0 :         OUString("com.sun.star.i18n.ConversionDictionary_ko"), xContext);
      49             : 
      50           0 :     if ( xI.is() )
      51           0 :         xCD.set( xI, UNO_QUERY );
      52             : 
      53           0 :     xCDL = ConversionDictionaryList::create(xContext);
      54             : 
      55           0 :     maxLeftLength = maxRightLength = 1;
      56             : 
      57             :     // get maximum length of word in dictionary
      58           0 :     if (xCDL.is()) {
      59             :         Locale loc(OUString("ko"),
      60             :                     OUString("KR"),
      61           0 :                     OUString());
      62           0 :         maxLeftLength = xCDL->queryMaxCharCount(loc,
      63             :                         ConversionDictionaryType::HANGUL_HANJA,
      64           0 :                         ConversionDirection_FROM_LEFT);
      65           0 :         maxRightLength = xCDL->queryMaxCharCount(loc,
      66             :                         ConversionDictionaryType::HANGUL_HANJA,
      67           0 :                         ConversionDirection_FROM_RIGHT);
      68           0 :         if (xCD.is()) {
      69           0 :             sal_Int32 tmp = xCD->getMaxCharCount(ConversionDirection_FROM_LEFT);
      70           0 :             if (tmp > maxLeftLength)
      71           0 :                 maxLeftLength = tmp;
      72           0 :             tmp = xCD->getMaxCharCount(ConversionDirection_FROM_RIGHT);
      73           0 :             if (tmp > maxRightLength)
      74           0 :                 maxRightLength = tmp;
      75           0 :         }
      76           0 :     } else if (xCD.is()) {
      77           0 :         maxLeftLength = xCD->getMaxCharCount(ConversionDirection_FROM_LEFT);
      78           0 :         maxRightLength = xCD->getMaxCharCount(ConversionDirection_FROM_RIGHT);
      79           0 :     }
      80           0 : }
      81             : 
      82           0 : sal_Int16 SAL_CALL checkScriptType(sal_Unicode c)
      83             : {
      84             :     typedef struct {
      85             :         UBlockCode from;
      86             :         UBlockCode to;
      87             :         sal_Int16 script;
      88             :     } UBlock2Script;
      89             : 
      90             :     static UBlock2Script scriptList[] = {
      91             :         {UBLOCK_HANGUL_JAMO, UBLOCK_HANGUL_JAMO, SCRIPT_HANGUL},
      92             :         {UBLOCK_CJK_RADICALS_SUPPLEMENT, UBLOCK_BOPOMOFO, SCRIPT_HANJA},
      93             :         {UBLOCK_HANGUL_COMPATIBILITY_JAMO, UBLOCK_HANGUL_COMPATIBILITY_JAMO, SCRIPT_HANGUL},
      94             :         {UBLOCK_KANBUN, UBLOCK_YI_RADICALS, SCRIPT_HANJA},
      95             :         {UBLOCK_HANGUL_SYLLABLES, UBLOCK_HANGUL_SYLLABLES, SCRIPT_HANGUL},
      96             :         {UBLOCK_CJK_COMPATIBILITY_IDEOGRAPHS, UBLOCK_CJK_COMPATIBILITY_IDEOGRAPHS, SCRIPT_HANJA},
      97             :         {UBLOCK_COMBINING_HALF_MARKS, UBLOCK_SMALL_FORM_VARIANTS, SCRIPT_HANJA},
      98             :         {UBLOCK_HALFWIDTH_AND_FULLWIDTH_FORMS, UBLOCK_HALFWIDTH_AND_FULLWIDTH_FORMS, SCRIPT_HANJA},
      99             :     };
     100             : 
     101             : #define scriptListCount sizeof (scriptList) / sizeof (UBlock2Script)
     102             : 
     103           0 :     UBlockCode block=ublock_getCode((sal_uInt32) c);
     104             :     sal_uInt16 i;
     105           0 :     for ( i = 0; i < scriptListCount; i++) {
     106           0 :         if (block <= scriptList[i].to) break;
     107             :     }
     108           0 :     return (i < scriptListCount && block >= scriptList[i].from) ? scriptList[i].script : SCRIPT_OTHERS;
     109             : }
     110             : 
     111             : #ifdef DISABLE_DYNLOADING
     112             : 
     113             : extern "C" {
     114             : 
     115             : const sal_Unicode* getHangul2HanjaData();
     116             : const Hangul_Index* getHangul2HanjaIndex();
     117             : sal_Int16 getHangul2HanjaIndexCount();
     118             : const sal_uInt16* getHanja2HangulIndex();
     119             : const sal_Unicode* getHanja2HangulData();
     120             : 
     121             : }
     122             : 
     123             : #endif
     124             : 
     125             : Sequence< OUString > SAL_CALL
     126           0 : TextConversion_ko::getCharConversions(const OUString& aText, sal_Int32 nStartPos, sal_Int32 nLength, sal_Bool toHanja)
     127             : {
     128             :     sal_Unicode ch;
     129           0 :     Sequence< OUString > output;
     130             : #ifndef DISABLE_DYNLOADING
     131           0 :     const sal_Unicode* (*getHangul2HanjaData)() = (const sal_Unicode* (*)())getFunctionBySymbol("getHangul2HanjaData");
     132           0 :     const Hangul_Index* (*getHangul2HanjaIndex)() = (const Hangul_Index* (*)()) getFunctionBySymbol("getHangul2HanjaIndex");
     133           0 :     sal_Int16 (*getHangul2HanjaIndexCount)() = (sal_Int16 (*)()) getFunctionBySymbol("getHangul2HanjaIndexCount");
     134           0 :     const sal_uInt16* (*getHanja2HangulIndex)() = (const sal_uInt16* (*)()) getFunctionBySymbol("getHanja2HangulIndex");
     135           0 :     const sal_Unicode* (*getHanja2HangulData)() = (const sal_Unicode* (*)()) getFunctionBySymbol("getHanja2HangulData");
     136             : #else
     137             : #pragma GCC diagnostic push
     138             : #ifdef __clang__
     139             : #pragma GCC diagnostic warning "-Wbool-conversions"
     140             : #else
     141             : #pragma GCC diagnostic warning "-Waddress"
     142             : #endif
     143             : #endif
     144           0 :     if (toHanja && getHangul2HanjaIndex && getHangul2HanjaIndexCount && getHangul2HanjaData) {
     145           0 :         ch = aText[nStartPos];
     146           0 :         const Hangul_Index *Hangul_ko = getHangul2HanjaIndex();
     147           0 :         sal_Int16 top =  getHangul2HanjaIndexCount();
     148           0 :         --top;
     149           0 :         sal_Int16 bottom = 0;
     150             : 
     151           0 :         while (bottom <= top) {
     152           0 :             sal_Int16 current = (top + bottom) / 2;
     153           0 :             sal_Unicode current_ch = Hangul_ko[current].code;
     154           0 :             if (ch < current_ch)
     155           0 :                 top = current - 1;
     156           0 :             else if (ch > current_ch)
     157           0 :                 bottom = current + 1;
     158             :             else {
     159           0 :                 const sal_Unicode *ptr = getHangul2HanjaData() + Hangul_ko[current].address;
     160           0 :                 sal_Int16 count = Hangul_ko[current].count;
     161           0 :                 output.realloc(count);
     162           0 :                 for (sal_Int16 i = 0; i < count; i++)
     163           0 :                     output[i] = OUString(ptr + i, 1);
     164           0 :                 break;
     165             :             }
     166           0 :         }
     167           0 :     } else if (! toHanja && getHanja2HangulIndex && getHanja2HangulData)
     168             :     {
     169           0 :         sal_Unicode *newStr = new sal_Unicode[nLength+1];
     170           0 :         sal_Int32 count = 0;
     171           0 :         while (count < nLength)
     172             :         {
     173           0 :             ch = aText[nStartPos + count];
     174           0 :             sal_Unicode address = getHanja2HangulIndex()[ch>>8];
     175           0 :             if (address != 0xFFFF)
     176           0 :                 address = getHanja2HangulData()[address + (ch & 0xFF)];
     177             : 
     178           0 :             if (address != 0xFFFF)
     179           0 :                 newStr[count++] = address;
     180             :             else
     181           0 :                 break;
     182             :         }
     183           0 :         if (count > 0)
     184             :         {
     185           0 :             output.realloc(1);
     186           0 :             output[0] = OUString(newStr, count);
     187             :         }
     188           0 :         delete[] newStr;
     189             :     }
     190             : #if defined(DISABLE_DYNLOADING)
     191             : #pragma GCC diagnostic pop
     192             : #endif
     193           0 :     return output;
     194             : }
     195             : 
     196           0 : static Sequence< OUString >& operator += (Sequence< OUString > &rSeq1, Sequence< OUString > &rSeq2 )
     197             : {
     198           0 :     if (! rSeq1.hasElements() && rSeq2.hasElements())
     199           0 :         rSeq1 = rSeq2;
     200           0 :     else if (rSeq2.hasElements()) {
     201             :         sal_Int32 i, j, k, l;
     202           0 :         k = l = rSeq1.getLength();
     203           0 :         rSeq1.realloc(l + rSeq2.getLength());
     204             : 
     205           0 :         for (i = 0; i < rSeq2.getLength(); i++) {
     206           0 :             for (j = 0; j < l; j++)
     207           0 :                 if (rSeq1[j] == rSeq2[i])
     208           0 :                     break;
     209           0 :             if (j == l)
     210           0 :                 rSeq1[k++] = rSeq2[i];
     211             :         }
     212           0 :         if (rSeq1.getLength() > k)
     213           0 :             rSeq1.realloc(k);
     214             :     }
     215           0 :     return rSeq1;
     216             : }
     217             : 
     218             : TextConversionResult SAL_CALL
     219           0 : TextConversion_ko::getConversions( const OUString& aText, sal_Int32 nStartPos, sal_Int32 nLength,
     220             :     const Locale& aLocale, sal_Int16 nConversionType, sal_Int32 nConversionOptions)
     221             :     throw(  RuntimeException, IllegalArgumentException, NoSupportException, std::exception )
     222             : {
     223           0 :     TextConversionResult result;
     224           0 :     Sequence <OUString> candidates;
     225           0 :     result.Boundary.startPos = result.Boundary.endPos = 0;
     226             : 
     227             :     // do conversion only when there are right conversion type and dictionary services.
     228           0 :     if (nConversionType == TextConversionType::TO_HANGUL ||
     229             :             nConversionType == TextConversionType::TO_HANJA) {
     230           0 :         sal_Int32 start, end, length = aText.getLength() - nStartPos;
     231             : 
     232           0 :         if (length < 0 || nStartPos < 0)
     233           0 :             length = 0;
     234           0 :         else if (length > nLength)
     235           0 :             length = nLength;
     236             : 
     237           0 :         sal_Int16 scriptType = SCRIPT_OTHERS;
     238           0 :         sal_Int32 len = 1;
     239           0 :         sal_Bool toHanja = (nConversionType == TextConversionType::TO_HANJA);
     240             :         // FROM_LEFT:  Hangul -> Hanja
     241             :         // FROM_RIGHT: Hanja  -> Hangul
     242           0 :         ConversionDirection eDirection = toHanja ? ConversionDirection_FROM_LEFT : ConversionDirection_FROM_RIGHT;
     243           0 :         sal_Int32 maxLength = toHanja ? maxLeftLength : maxRightLength;
     244           0 :         if (maxLength == 0) maxLength = 1;
     245             : 
     246             :         // search for a max length of convertible text
     247           0 :         for (start = 0, end = 0; start < length; start++) {
     248           0 :             if (end <= start) {
     249           0 :                 scriptType = checkScriptType(aText[nStartPos + start]);
     250           0 :                 if (nConversionType == TextConversionType::TO_HANJA) {
     251           0 :                     if (scriptType != SCRIPT_HANGUL) // skip non-Hangul characters
     252           0 :                         continue;
     253             :                 } else {
     254           0 :                     if (scriptType != SCRIPT_HANJA) // skip non-Hanja characters
     255           0 :                         continue;
     256             :                 }
     257           0 :                 end = start + 1;
     258             :             }
     259           0 :             if (nConversionOptions & TextConversionOption::CHARACTER_BY_CHARACTER) {
     260           0 :                 result.Candidates = getCharConversions(aText, nStartPos + start, len, toHanja); // char2char conversion
     261             :             } else {
     262           0 :                 for (; end < length && end - start < maxLength; end++)
     263           0 :                     if (checkScriptType(aText[nStartPos + end]) != scriptType)
     264           0 :                         break;
     265             : 
     266           0 :                 for (len = end - start; len > 0; len--) {
     267           0 :                     if (len > 1) {
     268             :                         try {
     269           0 :                             if (xCDL.is())
     270           0 :                                 result.Candidates = xCDL->queryConversions(aText, start + nStartPos, len,
     271           0 :                                     aLocale, ConversionDictionaryType::HANGUL_HANJA, eDirection, nConversionOptions); // user dictionary
     272             :                         }
     273           0 :                         catch ( NoSupportException & ) {
     274             :                             // clear reference (when there is no user dictionary) in order
     275             :                             // to not always have to catch this exception again
     276             :                             // in further calls. (save time)
     277           0 :                             xCDL = 0;
     278             :                         }
     279           0 :                         catch (...) {
     280             :                             // catch all other exceptions to allow
     281             :                             // querying the system dictionary in the next line
     282             :                         }
     283           0 :                         if (xCD.is() && toHanja) { // System dictionary would not do Hanja_to_Hangul conversion.
     284           0 :                             candidates = xCD->getConversions(aText, start + nStartPos, len, eDirection, nConversionOptions);
     285           0 :                             result.Candidates += candidates;
     286             :                         }
     287           0 :                     } else if (! toHanja) { // do whole word character 2 character conversion for Hanja to Hangul conversion
     288           0 :                         result.Candidates = getCharConversions(aText, nStartPos + start, length - start, toHanja);
     289           0 :                         if (result.Candidates.hasElements())
     290           0 :                             len = result.Candidates[0].getLength();
     291             :                     }
     292           0 :                     if (result.Candidates.hasElements())
     293           0 :                         break;
     294             :                 }
     295             :             }
     296             :             // found match
     297           0 :             if (result.Candidates.hasElements()) {
     298           0 :                 result.Boundary.startPos = start + nStartPos;;
     299           0 :                 result.Boundary.endPos = start + len + nStartPos;
     300           0 :                 return result;
     301             :             }
     302           0 :         }
     303             :     } else
     304           0 :         throw NoSupportException(); // Conversion type is not supported in this service.
     305           0 :     return result;
     306             : }
     307             : 
     308             : OUString SAL_CALL
     309           0 : TextConversion_ko::getConversion( const OUString& aText, sal_Int32 nStartPos, sal_Int32 nLength,
     310             :     const Locale& aLocale, sal_Int16 nConversionType, sal_Int32 nConversionOptions)
     311             :     throw(  RuntimeException, IllegalArgumentException, NoSupportException, std::exception )
     312             : {
     313           0 :     sal_Int32 length = aText.getLength() - nStartPos;
     314             : 
     315           0 :     if (length <= 0 || nStartPos < 0)
     316           0 :         return OUString();
     317           0 :     else if (length > nLength)
     318           0 :         length = nLength;
     319             : 
     320           0 :     OUStringBuffer aBuf(length + 1);
     321           0 :     TextConversionResult result;
     322           0 :     const sal_Unicode *str = aText.getStr();
     323             : 
     324           0 :     for (sal_Int32 start = nStartPos; length + nStartPos > start; start = result.Boundary.endPos) {
     325             : 
     326           0 :         result = getConversions(aText, start, length + nStartPos - start, aLocale, nConversionType, nConversionOptions);
     327             : 
     328           0 :         if (result.Boundary.endPos > 0) {
     329           0 :             if (result.Boundary.startPos > start)
     330           0 :                 aBuf.append(str + start, result.Boundary.startPos - start); // append skip portion
     331           0 :             aBuf.append(result.Candidates[0]); // append converted portion
     332             :         } else {
     333           0 :             if (length + nStartPos > start)
     334           0 :                 aBuf.append(str + start, length + nStartPos - start); // append last portion
     335           0 :             break;
     336             :         }
     337             :     }
     338             : 
     339           0 :     return aBuf.makeStringAndClear();
     340             : }
     341             : 
     342             : OUString SAL_CALL
     343           0 : TextConversion_ko::getConversionWithOffset( const OUString& aText, sal_Int32 nStartPos, sal_Int32 nLength,
     344             :     const Locale& rLocale, sal_Int16 nConversionType, sal_Int32 nConversionOptions, Sequence<sal_Int32>& offset)
     345             :     throw(  RuntimeException, IllegalArgumentException, NoSupportException, std::exception )
     346             : {
     347           0 :     offset.realloc(0);
     348           0 :     return getConversion(aText, nStartPos, nLength, rLocale, nConversionType, nConversionOptions);
     349             : }
     350             : 
     351             : sal_Bool SAL_CALL
     352           0 : TextConversion_ko::interactiveConversion( const Locale& /*rLocale*/, sal_Int16 /*nTextConversionType*/, sal_Int32 /*nTextConversionOptions*/ )
     353             :     throw(  RuntimeException, IllegalArgumentException, NoSupportException, std::exception )
     354             : {
     355           0 :     return sal_True;
     356             : }
     357             : 
     358             : } } } }
     359             : 
     360             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10