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             ch = aText[nStartPos + count];
160             sal_Unicode address = getHanja2HangulIndex()[ch>>8];
161             if (address != 0xFFFF)
162                 address = getHanja2HangulData()[address + (ch & 0xFF)];
163 
164             if (address != 0xFFFF)
165                 newStr->buffer[count++] = address;
166             else
167                 break;
168         }
169         if (count > 0) {
170             output.realloc(1);
171             output[0] = OUString( newStr->buffer, count);
172         }
173     }
174     return output;
175 }
176 
177 static Sequence< OUString >& operator += (Sequence< OUString > &rSeq1, Sequence< OUString > &rSeq2 )
178 {
179     if (! rSeq1.hasElements() && rSeq2.hasElements())
180         rSeq1 = rSeq2;
181     else if (rSeq2.hasElements()) {
182         sal_Int32 i, j, k, l;
183         k = l = rSeq1.getLength();
184         rSeq1.realloc(l + rSeq2.getLength());
185 
186         for (i = 0; i < rSeq2.getLength(); i++) {
187             for (j = 0; j < l; j++)
188                 if (rSeq1[j] == rSeq2[i])
189                     break;
190             if (j == l)
191                 rSeq1[k++] = rSeq2[i];
192         }
193         if (rSeq1.getLength() > k)
194             rSeq1.realloc(k);
195     }
196     return rSeq1;
197 }
198 
199 TextConversionResult SAL_CALL
200 TextConversion_ko::getConversions( const OUString& aText, sal_Int32 nStartPos, sal_Int32 nLength,
201     const Locale& aLocale, sal_Int16 nConversionType, sal_Int32 nConversionOptions)
202     throw(  RuntimeException, IllegalArgumentException, NoSupportException )
203 {
204     TextConversionResult result;
205     Sequence <OUString> candidates;
206     result.Boundary.startPos = result.Boundary.endPos = 0;
207 
208     // do conversion only when there are right conversion type and dictionary services.
209     if (nConversionType == TextConversionType::TO_HANGUL ||
210             nConversionType == TextConversionType::TO_HANJA) {
211         sal_Int32 start, end, length = aText.getLength() - nStartPos;
212 
213         if (length < 0 || nStartPos < 0)
214             length = 0;
215         else if (length > nLength)
216             length = nLength;
217 
218         sal_Int16 scriptType = SCRIPT_OTHERS;
219         sal_Int32 len = 1;
220         sal_Bool toHanja = (nConversionType == TextConversionType::TO_HANJA);
221         // FROM_LEFT:  Hangul -> Hanja
222         // FROM_RIGHT: Hanja  -> Hangul
223         ConversionDirection eDirection = toHanja ? ConversionDirection_FROM_LEFT : ConversionDirection_FROM_RIGHT;
224         sal_Int32 maxLength = toHanja ? maxLeftLength : maxRightLength;
225         if (maxLength == 0) maxLength = 1;
226 
227         // search for a max length of convertible text
228         for (start = 0, end = 0; start < length; start++) {
229             if (end <= start) {
230                 scriptType = checkScriptType(aText[nStartPos + start]);
231                 if (nConversionType == TextConversionType::TO_HANJA) {
232                     if (scriptType != SCRIPT_HANGUL) // skip non-Hangul characters
233                         continue;
234                 } else {
235                     if (scriptType != SCRIPT_HANJA) // skip non-Hanja characters
236                         continue;
237                 }
238                 end = start + 1;
239             }
240             if (nConversionOptions & TextConversionOption::CHARACTER_BY_CHARACTER) {
241                 result.Candidates = getCharConversions(aText, nStartPos + start, len, toHanja); // char2char conversion
242             } else {
243                 for (; end < length && end - start < maxLength; end++)
244                     if (checkScriptType(aText[nStartPos + end]) != scriptType)
245                         break;
246 
247                 for (len = end - start; len > 0; len--) {
248                     if (len > 1) {
249                         try {
250                             if (xCDL.is())
251                                 result.Candidates = xCDL->queryConversions(aText, start + nStartPos, len,
252                                     aLocale, ConversionDictionaryType::HANGUL_HANJA, eDirection, nConversionOptions); // user dictionary
253                         }
254                         catch ( NoSupportException & ) {
255                             // clear reference (when there is no user dictionary) in order
256                             // to not always have to catch this exception again
257                             // in further calls. (save time)
258                             xCDL = 0;
259                         }
260                         catch (...) {
261                             // catch all other exceptions to allow
262                             // querying the system dictionary in the next line
263                         }
264                         if (xCD.is() && toHanja) { // System dictionary would not do Hanja_to_Hangul conversion.
265                             candidates = xCD->getConversions(aText, start + nStartPos, len, eDirection, nConversionOptions);
266                             result.Candidates += candidates;
267                         }
268                     } else if (! toHanja) { // do whole word character 2 character conversion for Hanja to Hangul conversion
269                         result.Candidates = getCharConversions(aText, nStartPos + start, length - start, toHanja);
270                         if (result.Candidates.hasElements())
271                             len = result.Candidates[0].getLength();
272                     }
273                     if (result.Candidates.hasElements())
274                         break;
275                 }
276             }
277             // found match
278             if (result.Candidates.hasElements()) {
279                 result.Boundary.startPos = start + nStartPos;;
280                 result.Boundary.endPos = start + len + nStartPos;
281                 return result;
282             }
283         }
284     } else
285         throw NoSupportException(); // Conversion type is not supported in this service.
286     return result;
287 }
288 
289 OUString SAL_CALL
290 TextConversion_ko::getConversion( const OUString& aText, sal_Int32 nStartPos, sal_Int32 nLength,
291     const Locale& aLocale, sal_Int16 nConversionType, sal_Int32 nConversionOptions)
292     throw(  RuntimeException, IllegalArgumentException, NoSupportException )
293 {
294     sal_Int32 length = aText.getLength() - nStartPos;
295 
296     if (length <= 0 || nStartPos < 0)
297         return OUString();
298     else if (length > nLength)
299         length = nLength;
300 
301     OUStringBuffer aBuf(length + 1);
302     TextConversionResult result;
303     const sal_Unicode *str = aText.getStr();
304 
305     for (sal_Int32 start = nStartPos; length + nStartPos > start; start = result.Boundary.endPos) {
306 
307         result = getConversions(aText, start, length + nStartPos - start, aLocale, nConversionType, nConversionOptions);
308 
309         if (result.Boundary.endPos > 0) {
310             if (result.Boundary.startPos > start)
311                 aBuf.append(str + start, result.Boundary.startPos - start); // append skip portion
312             aBuf.append(result.Candidates[0]); // append converted portion
313         } else {
314             if (length + nStartPos > start)
315                 aBuf.append(str + start, length + nStartPos - start); // append last portion
316             break;
317         }
318     }
319 
320     return aBuf.makeStringAndClear();
321 }
322 
323 OUString SAL_CALL
324 TextConversion_ko::getConversionWithOffset( const OUString& aText, sal_Int32 nStartPos, sal_Int32 nLength,
325     const Locale& rLocale, sal_Int16 nConversionType, sal_Int32 nConversionOptions, Sequence<sal_Int32>& offset)
326     throw(  RuntimeException, IllegalArgumentException, NoSupportException )
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     throw(  RuntimeException, IllegalArgumentException, NoSupportException )
335 {
336     return sal_True;
337 }
338 
339 } } } }
340