LCOV - code coverage report
Current view: top level - usr/local/src/libreoffice/i18npool/source/textconversion - textconversion_ko.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 0 168 0.0 %
Date: 2013-07-09 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             : {
      44           0 :     Reference < XInterface > xI;
      45             : 
      46           0 :     xI = xContext->getServiceManager()->createInstanceWithContext(
      47           0 :         OUString("com.sun.star.i18n.ConversionDictionary_ko"), xContext);
      48             : 
      49           0 :     if ( xI.is() )
      50           0 :         xCD.set( xI, UNO_QUERY );
      51             : 
      52           0 :     xCDL = ConversionDictionaryList::create(xContext);
      53             : 
      54           0 :     maxLeftLength = maxRightLength = 1;
      55             : 
      56             :     // get maximum length of word in dictionary
      57           0 :     if (xCDL.is()) {
      58             :         Locale loc(OUString("ko"),
      59             :                     OUString("KR"),
      60           0 :                     OUString());
      61           0 :         maxLeftLength = xCDL->queryMaxCharCount(loc,
      62             :                         ConversionDictionaryType::HANGUL_HANJA,
      63           0 :                         ConversionDirection_FROM_LEFT);
      64           0 :         maxRightLength = xCDL->queryMaxCharCount(loc,
      65             :                         ConversionDictionaryType::HANGUL_HANJA,
      66           0 :                         ConversionDirection_FROM_RIGHT);
      67           0 :         if (xCD.is()) {
      68           0 :             sal_Int32 tmp = xCD->getMaxCharCount(ConversionDirection_FROM_LEFT);
      69           0 :             if (tmp > maxLeftLength)
      70           0 :                 maxLeftLength = tmp;
      71           0 :             tmp = xCD->getMaxCharCount(ConversionDirection_FROM_RIGHT);
      72           0 :             if (tmp > maxRightLength)
      73           0 :                 maxRightLength = tmp;
      74           0 :         }
      75           0 :     } else if (xCD.is()) {
      76           0 :         maxLeftLength = xCD->getMaxCharCount(ConversionDirection_FROM_LEFT);
      77           0 :         maxRightLength = xCD->getMaxCharCount(ConversionDirection_FROM_RIGHT);
      78             :     }
      79             : 
      80           0 :     implementationName = "com.sun.star.i18n.TextConversion_ko";
      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, sal_Bool toHanja)
     128             : {
     129             :     sal_Unicode ch;
     130           0 :     Sequence< OUString > output;
     131             : #ifndef DISABLE_DYNLOADING
     132           0 :     const sal_Unicode* (*getHangul2HanjaData)() = (const sal_Unicode* (*)())getFunctionBySymbol("getHangul2HanjaData");
     133           0 :     const Hangul_Index* (*getHangul2HanjaIndex)() = (const Hangul_Index* (*)()) getFunctionBySymbol("getHangul2HanjaIndex");
     134           0 :     sal_Int16 (*getHangul2HanjaIndexCount)() = (sal_Int16 (*)()) getFunctionBySymbol("getHangul2HanjaIndexCount");
     135           0 :     const sal_uInt16* (*getHanja2HangulIndex)() = (const sal_uInt16* (*)()) getFunctionBySymbol("getHanja2HangulIndex");
     136           0 :     const sal_Unicode* (*getHanja2HangulData)() = (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 :         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, count);
     188             :         }
     189           0 :         delete[] newStr;
     190             :     }
     191             : #if defined(DISABLE_DYNLOADING)
     192             : #pragma GCC diagnostic pop
     193             : #endif
     194           0 :     return output;
     195             : }
     196             : 
     197           0 : static Sequence< OUString >& operator += (Sequence< OUString > &rSeq1, Sequence< OUString > &rSeq2 )
     198             : {
     199           0 :     if (! rSeq1.hasElements() && rSeq2.hasElements())
     200           0 :         rSeq1 = rSeq2;
     201           0 :     else if (rSeq2.hasElements()) {
     202             :         sal_Int32 i, j, k, l;
     203           0 :         k = l = rSeq1.getLength();
     204           0 :         rSeq1.realloc(l + rSeq2.getLength());
     205             : 
     206           0 :         for (i = 0; i < rSeq2.getLength(); i++) {
     207           0 :             for (j = 0; j < l; j++)
     208           0 :                 if (rSeq1[j] == rSeq2[i])
     209           0 :                     break;
     210           0 :             if (j == l)
     211           0 :                 rSeq1[k++] = rSeq2[i];
     212             :         }
     213           0 :         if (rSeq1.getLength() > k)
     214           0 :             rSeq1.realloc(k);
     215             :     }
     216           0 :     return rSeq1;
     217             : }
     218             : 
     219             : TextConversionResult SAL_CALL
     220           0 : TextConversion_ko::getConversions( const OUString& aText, sal_Int32 nStartPos, sal_Int32 nLength,
     221             :     const Locale& aLocale, sal_Int16 nConversionType, sal_Int32 nConversionOptions)
     222             :     throw(  RuntimeException, IllegalArgumentException, NoSupportException )
     223             : {
     224           0 :     TextConversionResult result;
     225           0 :     Sequence <OUString> candidates;
     226           0 :     result.Boundary.startPos = result.Boundary.endPos = 0;
     227             : 
     228             :     // do conversion only when there are right conversion type and dictionary services.
     229           0 :     if (nConversionType == TextConversionType::TO_HANGUL ||
     230             :             nConversionType == TextConversionType::TO_HANJA) {
     231           0 :         sal_Int32 start, end, length = aText.getLength() - nStartPos;
     232             : 
     233           0 :         if (length < 0 || nStartPos < 0)
     234           0 :             length = 0;
     235           0 :         else if (length > nLength)
     236           0 :             length = nLength;
     237             : 
     238           0 :         sal_Int16 scriptType = SCRIPT_OTHERS;
     239           0 :         sal_Int32 len = 1;
     240           0 :         sal_Bool toHanja = (nConversionType == TextConversionType::TO_HANJA);
     241             :         // FROM_LEFT:  Hangul -> Hanja
     242             :         // FROM_RIGHT: Hanja  -> Hangul
     243           0 :         ConversionDirection eDirection = toHanja ? ConversionDirection_FROM_LEFT : ConversionDirection_FROM_RIGHT;
     244           0 :         sal_Int32 maxLength = toHanja ? maxLeftLength : maxRightLength;
     245           0 :         if (maxLength == 0) maxLength = 1;
     246             : 
     247             :         // search for a max length of convertible text
     248           0 :         for (start = 0, end = 0; start < length; start++) {
     249           0 :             if (end <= start) {
     250           0 :                 scriptType = checkScriptType(aText[nStartPos + start]);
     251           0 :                 if (nConversionType == TextConversionType::TO_HANJA) {
     252           0 :                     if (scriptType != SCRIPT_HANGUL) // skip non-Hangul characters
     253           0 :                         continue;
     254             :                 } else {
     255           0 :                     if (scriptType != SCRIPT_HANJA) // skip non-Hanja characters
     256           0 :                         continue;
     257             :                 }
     258           0 :                 end = start + 1;
     259             :             }
     260           0 :             if (nConversionOptions & TextConversionOption::CHARACTER_BY_CHARACTER) {
     261           0 :                 result.Candidates = getCharConversions(aText, nStartPos + start, len, toHanja); // char2char conversion
     262             :             } else {
     263           0 :                 for (; end < length && end - start < maxLength; end++)
     264           0 :                     if (checkScriptType(aText[nStartPos + end]) != scriptType)
     265           0 :                         break;
     266             : 
     267           0 :                 for (len = end - start; len > 0; len--) {
     268           0 :                     if (len > 1) {
     269             :                         try {
     270           0 :                             if (xCDL.is())
     271           0 :                                 result.Candidates = xCDL->queryConversions(aText, start + nStartPos, len,
     272           0 :                                     aLocale, ConversionDictionaryType::HANGUL_HANJA, eDirection, nConversionOptions); // user dictionary
     273             :                         }
     274           0 :                         catch ( NoSupportException & ) {
     275             :                             // clear reference (when there is no user dictionary) in order
     276             :                             // to not always have to catch this exception again
     277             :                             // in further calls. (save time)
     278           0 :                             xCDL = 0;
     279             :                         }
     280           0 :                         catch (...) {
     281             :                             // catch all other exceptions to allow
     282             :                             // querying the system dictionary in the next line
     283             :                         }
     284           0 :                         if (xCD.is() && toHanja) { // System dictionary would not do Hanja_to_Hangul conversion.
     285           0 :                             candidates = xCD->getConversions(aText, start + nStartPos, len, eDirection, nConversionOptions);
     286           0 :                             result.Candidates += candidates;
     287             :                         }
     288           0 :                     } else if (! toHanja) { // do whole word character 2 character conversion for Hanja to Hangul conversion
     289           0 :                         result.Candidates = getCharConversions(aText, nStartPos + start, length - start, toHanja);
     290           0 :                         if (result.Candidates.hasElements())
     291           0 :                             len = result.Candidates[0].getLength();
     292             :                     }
     293           0 :                     if (result.Candidates.hasElements())
     294           0 :                         break;
     295             :                 }
     296             :             }
     297             :             // found match
     298           0 :             if (result.Candidates.hasElements()) {
     299           0 :                 result.Boundary.startPos = start + nStartPos;;
     300           0 :                 result.Boundary.endPos = start + len + nStartPos;
     301           0 :                 return result;
     302             :             }
     303           0 :         }
     304             :     } else
     305           0 :         throw NoSupportException(); // Conversion type is not supported in this service.
     306           0 :     return result;
     307             : }
     308             : 
     309             : OUString SAL_CALL
     310           0 : TextConversion_ko::getConversion( const OUString& aText, sal_Int32 nStartPos, sal_Int32 nLength,
     311             :     const Locale& aLocale, sal_Int16 nConversionType, sal_Int32 nConversionOptions)
     312             :     throw(  RuntimeException, IllegalArgumentException, NoSupportException )
     313             : {
     314           0 :     sal_Int32 length = aText.getLength() - nStartPos;
     315             : 
     316           0 :     if (length <= 0 || nStartPos < 0)
     317           0 :         return OUString();
     318           0 :     else if (length > nLength)
     319           0 :         length = nLength;
     320             : 
     321           0 :     OUStringBuffer aBuf(length + 1);
     322           0 :     TextConversionResult result;
     323           0 :     const sal_Unicode *str = aText.getStr();
     324             : 
     325           0 :     for (sal_Int32 start = nStartPos; length + nStartPos > start; start = result.Boundary.endPos) {
     326             : 
     327           0 :         result = getConversions(aText, start, length + nStartPos - start, aLocale, nConversionType, nConversionOptions);
     328             : 
     329           0 :         if (result.Boundary.endPos > 0) {
     330           0 :             if (result.Boundary.startPos > start)
     331           0 :                 aBuf.append(str + start, result.Boundary.startPos - start); // append skip portion
     332           0 :             aBuf.append(result.Candidates[0]); // append converted portion
     333             :         } else {
     334           0 :             if (length + nStartPos > start)
     335           0 :                 aBuf.append(str + start, length + nStartPos - start); // append last portion
     336           0 :             break;
     337             :         }
     338             :     }
     339             : 
     340           0 :     return aBuf.makeStringAndClear();
     341             : }
     342             : 
     343             : OUString SAL_CALL
     344           0 : TextConversion_ko::getConversionWithOffset( const OUString& aText, sal_Int32 nStartPos, sal_Int32 nLength,
     345             :     const Locale& rLocale, sal_Int16 nConversionType, sal_Int32 nConversionOptions, Sequence<sal_Int32>& offset)
     346             :     throw(  RuntimeException, IllegalArgumentException, NoSupportException )
     347             : {
     348           0 :     offset.realloc(0);
     349           0 :     return getConversion(aText, nStartPos, nLength, rLocale, nConversionType, nConversionOptions);
     350             : }
     351             : 
     352             : sal_Bool SAL_CALL
     353           0 : TextConversion_ko::interactiveConversion( const Locale& /*rLocale*/, sal_Int16 /*nTextConversionType*/, sal_Int32 /*nTextConversionOptions*/ )
     354             :     throw(  RuntimeException, IllegalArgumentException, NoSupportException )
     355             : {
     356           0 :     return sal_True;
     357             : }
     358             : 
     359             : } } } }
     360             : 
     361             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10