LCOV - code coverage report
Current view: top level - i18npool/source/textconversion - textconversion_ko.cxx (source / functions) Hit Total Coverage
Test: commit c8344322a7af75b84dd3ca8f78b05543a976dfd5 Lines: 0 169 0.0 %
Date: 2015-06-13 12:38:46 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             : #include <boost/scoped_array.hpp>
      30             : 
      31             : using namespace com::sun::star::lang;
      32             : using namespace com::sun::star::i18n;
      33             : using namespace com::sun::star::linguistic2;
      34             : using namespace com::sun::star::uno;
      35             : 
      36             : 
      37             : namespace com { namespace sun { namespace star { namespace i18n {
      38             : 
      39             : #define SCRIPT_OTHERS   0
      40             : #define SCRIPT_HANJA    1
      41             : #define SCRIPT_HANGUL   2
      42             : 
      43           0 : TextConversion_ko::TextConversion_ko( const Reference < XComponentContext >& xContext )
      44           0 :     : TextConversionService("com.sun.star.i18n.TextConversion_ko")
      45             : {
      46           0 :     Reference < XInterface > xI;
      47             : 
      48           0 :     xI = xContext->getServiceManager()->createInstanceWithContext(
      49           0 :         OUString("com.sun.star.i18n.ConversionDictionary_ko"), xContext);
      50             : 
      51           0 :     if ( xI.is() )
      52           0 :         xCD.set( xI, UNO_QUERY );
      53             : 
      54           0 :     xCDL = ConversionDictionaryList::create(xContext);
      55             : 
      56           0 :     maxLeftLength = maxRightLength = 1;
      57             : 
      58             :     // get maximum length of word in dictionary
      59           0 :     if (xCDL.is()) {
      60             :         Locale loc(OUString("ko"),
      61             :                     OUString("KR"),
      62           0 :                     OUString());
      63           0 :         maxLeftLength = xCDL->queryMaxCharCount(loc,
      64             :                         ConversionDictionaryType::HANGUL_HANJA,
      65           0 :                         ConversionDirection_FROM_LEFT);
      66           0 :         maxRightLength = xCDL->queryMaxCharCount(loc,
      67             :                         ConversionDictionaryType::HANGUL_HANJA,
      68           0 :                         ConversionDirection_FROM_RIGHT);
      69           0 :         if (xCD.is()) {
      70           0 :             sal_Int32 tmp = xCD->getMaxCharCount(ConversionDirection_FROM_LEFT);
      71           0 :             if (tmp > maxLeftLength)
      72           0 :                 maxLeftLength = tmp;
      73           0 :             tmp = xCD->getMaxCharCount(ConversionDirection_FROM_RIGHT);
      74           0 :             if (tmp > maxRightLength)
      75           0 :                 maxRightLength = tmp;
      76           0 :         }
      77           0 :     } else if (xCD.is()) {
      78           0 :         maxLeftLength = xCD->getMaxCharCount(ConversionDirection_FROM_LEFT);
      79           0 :         maxRightLength = xCD->getMaxCharCount(ConversionDirection_FROM_RIGHT);
      80           0 :     }
      81           0 : }
      82             : 
      83           0 : sal_Int16 SAL_CALL checkScriptType(sal_Unicode c)
      84             : {
      85             :     typedef struct {
      86             :         UBlockCode from;
      87             :         UBlockCode to;
      88             :         sal_Int16 script;
      89             :     } UBlock2Script;
      90             : 
      91             :     static UBlock2Script scriptList[] = {
      92             :         {UBLOCK_HANGUL_JAMO, UBLOCK_HANGUL_JAMO, SCRIPT_HANGUL},
      93             :         {UBLOCK_CJK_RADICALS_SUPPLEMENT, UBLOCK_BOPOMOFO, SCRIPT_HANJA},
      94             :         {UBLOCK_HANGUL_COMPATIBILITY_JAMO, UBLOCK_HANGUL_COMPATIBILITY_JAMO, SCRIPT_HANGUL},
      95             :         {UBLOCK_KANBUN, UBLOCK_YI_RADICALS, SCRIPT_HANJA},
      96             :         {UBLOCK_HANGUL_SYLLABLES, UBLOCK_HANGUL_SYLLABLES, SCRIPT_HANGUL},
      97             :         {UBLOCK_CJK_COMPATIBILITY_IDEOGRAPHS, UBLOCK_CJK_COMPATIBILITY_IDEOGRAPHS, SCRIPT_HANJA},
      98             :         {UBLOCK_COMBINING_HALF_MARKS, UBLOCK_SMALL_FORM_VARIANTS, SCRIPT_HANJA},
      99             :         {UBLOCK_HALFWIDTH_AND_FULLWIDTH_FORMS, UBLOCK_HALFWIDTH_AND_FULLWIDTH_FORMS, SCRIPT_HANJA},
     100             :     };
     101             : 
     102             : #define scriptListCount sizeof (scriptList) / sizeof (UBlock2Script)
     103             : 
     104           0 :     UBlockCode block=ublock_getCode((sal_uInt32) c);
     105             :     sal_uInt16 i;
     106           0 :     for ( i = 0; i < scriptListCount; i++) {
     107           0 :         if (block <= scriptList[i].to) break;
     108             :     }
     109           0 :     return (i < scriptListCount && block >= scriptList[i].from) ? scriptList[i].script : SCRIPT_OTHERS;
     110             : }
     111             : 
     112             : #ifdef DISABLE_DYNLOADING
     113             : 
     114             : extern "C" {
     115             : 
     116             : const sal_Unicode* getHangul2HanjaData();
     117             : const Hangul_Index* getHangul2HanjaIndex();
     118             : sal_Int16 getHangul2HanjaIndexCount();
     119             : const sal_uInt16* getHanja2HangulIndex();
     120             : const sal_Unicode* getHanja2HangulData();
     121             : 
     122             : }
     123             : 
     124             : #endif
     125             : 
     126             : Sequence< OUString > SAL_CALL
     127           0 : TextConversion_ko::getCharConversions(const OUString& aText, sal_Int32 nStartPos, sal_Int32 nLength, bool toHanja)
     128             : {
     129             :     sal_Unicode ch;
     130           0 :     Sequence< OUString > output;
     131             : #ifndef DISABLE_DYNLOADING
     132           0 :     const sal_Unicode* (*getHangul2HanjaData)() = reinterpret_cast<const sal_Unicode* (*)()>(getFunctionBySymbol("getHangul2HanjaData"));
     133           0 :     const Hangul_Index* (*getHangul2HanjaIndex)() = reinterpret_cast<const Hangul_Index* (*)()>(getFunctionBySymbol("getHangul2HanjaIndex"));
     134           0 :     sal_Int16 (*getHangul2HanjaIndexCount)() = reinterpret_cast<sal_Int16 (*)()>(getFunctionBySymbol("getHangul2HanjaIndexCount"));
     135           0 :     const sal_uInt16* (*getHanja2HangulIndex)() = reinterpret_cast<const sal_uInt16* (*)()>(getFunctionBySymbol("getHanja2HangulIndex"));
     136           0 :     const sal_Unicode* (*getHanja2HangulData)() = reinterpret_cast<const sal_Unicode* (*)()>(getFunctionBySymbol("getHanja2HangulData"));
     137             : #else
     138             : #pragma GCC diagnostic push
     139             : #ifdef __clang__
     140             : #pragma GCC diagnostic warning "-Wbool-conversions"
     141             : #else
     142             : #pragma GCC diagnostic warning "-Waddress"
     143             : #endif
     144             : #endif
     145           0 :     if (toHanja && getHangul2HanjaIndex && getHangul2HanjaIndexCount && getHangul2HanjaData) {
     146           0 :         ch = aText[nStartPos];
     147           0 :         const Hangul_Index *Hangul_ko = getHangul2HanjaIndex();
     148           0 :         sal_Int16 top =  getHangul2HanjaIndexCount();
     149           0 :         --top;
     150           0 :         sal_Int16 bottom = 0;
     151             : 
     152           0 :         while (bottom <= top) {
     153           0 :             sal_Int16 current = (top + bottom) / 2;
     154           0 :             sal_Unicode current_ch = Hangul_ko[current].code;
     155           0 :             if (ch < current_ch)
     156           0 :                 top = current - 1;
     157           0 :             else if (ch > current_ch)
     158           0 :                 bottom = current + 1;
     159             :             else {
     160           0 :                 const sal_Unicode *ptr = getHangul2HanjaData() + Hangul_ko[current].address;
     161           0 :                 sal_Int16 count = Hangul_ko[current].count;
     162           0 :                 output.realloc(count);
     163           0 :                 for (sal_Int16 i = 0; i < count; i++)
     164           0 :                     output[i] = OUString(ptr + i, 1);
     165           0 :                 break;
     166             :             }
     167           0 :         }
     168           0 :     } else if (! toHanja && getHanja2HangulIndex && getHanja2HangulData)
     169             :     {
     170           0 :         boost::scoped_array<sal_Unicode> newStr(new sal_Unicode[nLength+1]);
     171           0 :         sal_Int32 count = 0;
     172           0 :         while (count < nLength)
     173             :         {
     174           0 :             ch = aText[nStartPos + count];
     175           0 :             sal_Unicode address = getHanja2HangulIndex()[ch>>8];
     176           0 :             if (address != 0xFFFF)
     177           0 :                 address = getHanja2HangulData()[address + (ch & 0xFF)];
     178             : 
     179           0 :             if (address != 0xFFFF)
     180           0 :                 newStr[count++] = address;
     181             :             else
     182           0 :                 break;
     183             :         }
     184           0 :         if (count > 0)
     185             :         {
     186           0 :             output.realloc(1);
     187           0 :             output[0] = OUString(newStr.get(), count);
     188           0 :         }
     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 :         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.11