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 
27 #include <assert.h>
28 #include <textconversion.hxx>
29 #include <com/sun/star/i18n/TextConversionType.hpp>
30 #include <com/sun/star/i18n/TextConversionOption.hpp>
31 #include <com/sun/star/linguistic2/ConversionDirection.hpp>
32 #include <com/sun/star/linguistic2/ConversionDictionaryType.hpp>
33 #include <i18nutil/x_rtl_ustring.h>
34 
35 using namespace com::sun::star::lang;
36 using namespace com::sun::star::i18n;
37 using namespace com::sun::star::linguistic2;
38 using namespace com::sun::star::uno;
39 using namespace rtl;
40 
41 namespace com { namespace sun { namespace star { namespace i18n {
42 
43 TextConversion_zh::TextConversion_zh( const Reference < XMultiServiceFactory >& xMSF )
44 {
45     Reference < XInterface > xI;
46     xI = xMSF->createInstance(
47         OUString::createFromAscii( "com.sun.star.linguistic2.ConversionDictionaryList" ));
48     if ( xI.is() )
49         xI->queryInterface( getCppuType((const Reference< XConversionDictionaryList>*)0) ) >>= xCDL;
50 
51     implementationName = "com.sun.star.i18n.TextConversion_zh";
52 }
53 
54 sal_Unicode SAL_CALL getOneCharConversion(sal_Unicode ch, const sal_Unicode* Data, const sal_uInt16* Index)
55 {
56     if (Data && Index) {
57         sal_Unicode address = Index[ch>>8];
58         if (address != 0xFFFF)
59             address = Data[address + (ch & 0xFF)];
60         return (address != 0xFFFF) ? address : ch;
61     } else {
62         return ch;
63     }
64 }
65 
66 OUString SAL_CALL
67 TextConversion_zh::getCharConversion(const OUString& aText, sal_Int32 nStartPos, sal_Int32 nLength, sal_Bool toSChinese, sal_Int32 nConversionOptions)
68 {
69     const sal_Unicode *Data;
70     const sal_uInt16 *Index;
71 
72     if (toSChinese) {
73         Data = ((const sal_Unicode* (*)())getFunctionBySymbol("getSTC_CharData_T2S"))();
74         Index = ((const sal_uInt16* (*)())getFunctionBySymbol("getSTC_CharIndex_T2S"))();
75     } else if (nConversionOptions & TextConversionOption::USE_CHARACTER_VARIANTS) {
76         Data = ((const sal_Unicode* (*)())getFunctionBySymbol("getSTC_CharData_S2V"))();
77         Index = ((const sal_uInt16* (*)())getFunctionBySymbol("getSTC_CharIndex_S2V"))();
78     } else {
79         Data = ((const sal_Unicode* (*)())getFunctionBySymbol("getSTC_CharData_S2T"))();
80         Index = ((const sal_uInt16* (*)())getFunctionBySymbol("getSTC_CharIndex_S2T"))();
81     }
82 
83     rtl_uString * newStr = x_rtl_uString_new_WithLength( nLength ); // defined in x_rtl_ustring.h
84     for (sal_Int32 i = 0; i < nLength; i++)
85         newStr->buffer[i] =
86             getOneCharConversion(aText[nStartPos+i], Data, Index);
87     return OUString( newStr->buffer, nLength);
88 }
89 
90 OUString SAL_CALL
91 TextConversion_zh::getWordConversion(const OUString& aText, sal_Int32 nStartPos, sal_Int32 nLength, sal_Bool toSChinese, sal_Int32 nConversionOptions, Sequence<sal_Int32>& offset)
92 {
93     sal_Int32 dictLen = 0;
94     sal_Int32 maxLen = 0;
95     const sal_uInt16 *index;
96     const sal_uInt16 *entry;
97     const sal_Unicode *charData;
98     const sal_uInt16 *charIndex;
99     sal_Bool one2one=sal_True;
100 
101     const sal_Unicode *wordData = ((const sal_Unicode* (*)(sal_Int32&)) getFunctionBySymbol("getSTC_WordData"))(dictLen);
102     if (toSChinese) {
103         index = ((const sal_uInt16* (*)(sal_Int32&)) getFunctionBySymbol("getSTC_WordIndex_T2S"))(maxLen);
104         entry = ((const sal_uInt16* (*)()) getFunctionBySymbol("getSTC_WordEntry_T2S"))();
105         charData = ((const sal_Unicode* (*)()) getFunctionBySymbol("getSTC_CharData_T2S"))();
106         charIndex = ((const sal_uInt16* (*)()) getFunctionBySymbol("getSTC_CharIndex_T2S"))();
107     } else {
108         index = ((const sal_uInt16* (*)(sal_Int32&)) getFunctionBySymbol("getSTC_WordIndex_S2T"))(maxLen);
109         entry = ((const sal_uInt16* (*)()) getFunctionBySymbol("getSTC_WordEntry_S2T"))();
110         if (nConversionOptions & TextConversionOption::USE_CHARACTER_VARIANTS) {
111             charData = ((const sal_Unicode* (*)()) getFunctionBySymbol("getSTC_CharData_S2V"))();
112             charIndex = ((const sal_uInt16* (*)()) getFunctionBySymbol("getSTC_CharIndex_S2V"))();
113         } else {
114             charData = ((const sal_Unicode* (*)()) getFunctionBySymbol("getSTC_CharData_S2T"))();
115             charIndex = ((const sal_uInt16* (*)()) getFunctionBySymbol("getSTC_CharIndex_S2T"))();
116         }
117     }
118 
119     if ((!wordData || !index || !entry) && !xCDL.is()) // no word mapping defined, do char2char conversion.
120         return getCharConversion(aText, nStartPos, nLength, toSChinese, nConversionOptions);
121 
122     rtl_uString * newStr = x_rtl_uString_new_WithLength( nLength * 2 ); // defined in x_rtl_ustring.h
123     sal_Int32 currPos = 0, count = 0;
124     while (currPos < nLength) {
125         sal_Int32 len = nLength - currPos;
126         sal_Bool found = sal_False;
127         if (len > maxLen)
128             len = maxLen;
129         for (; len > 0 && ! found; len--) {
130             OUString word = aText.copy(nStartPos + currPos, len);
131             sal_Int32 current = 0;
132             // user dictionary
133             if (xCDL.is()) {
134                 Sequence < OUString > conversions;
135                 try {
136                     conversions = xCDL->queryConversions(word, 0, len,
137                             aLocale, ConversionDictionaryType::SCHINESE_TCHINESE,
138                             /*toSChinese ?*/ ConversionDirection_FROM_LEFT /*: ConversionDirection_FROM_RIGHT*/,
139                             nConversionOptions);
140                 }
141                 catch ( NoSupportException & ) {
142                     // clear reference (when there is no user dictionary) in order
143                     // to not always have to catch this exception again
144                     // in further calls. (save time)
145                     xCDL = 0;
146                 }
147                 catch (...) {
148                     // catch all other exceptions to allow
149                     // querying the system dictionary in the next line
150                 }
151                 if (conversions.getLength() > 0) {
152                     if (offset.getLength() > 0) {
153                         if (word.getLength() != conversions[0].getLength())
154                             one2one=sal_False;
155                         while (current < conversions[0].getLength()) {
156                             offset[count] = nStartPos + currPos + (current *
157                                     word.getLength() / conversions[0].getLength());
158                             newStr->buffer[count++] = conversions[0][current++];
159                         }
160                         // offset[count-1] = nStartPos + currPos + word.getLength() - 1;
161                     } else {
162                         while (current < conversions[0].getLength())
163                             newStr->buffer[count++] = conversions[0][current++];
164                     }
165                     currPos += word.getLength();
166                     found = sal_True;
167                 }
168             }
169 
170             if (!found && index[len+1] - index[len] > 0) {
171                 sal_Int32 bottom = (sal_Int32) index[len];
172                 sal_Int32 top = (sal_Int32) index[len+1] - 1;
173 
174                 while (bottom <= top && !found) {
175                     current = (top + bottom) / 2;
176                     const sal_Int32 result = word.compareTo(wordData + entry[current]);
177                     if (result < 0)
178                         top = current - 1;
179                     else if (result > 0)
180                         bottom = current + 1;
181                     else {
182                         if (toSChinese)   // Traditionary/Simplified conversion,
183                             for (current = entry[current]-1; current > 0 && wordData[current-1]; current--) ;
184                         else  // Simplified/Traditionary conversion, forwards search for next word
185                             current = entry[current] + word.getLength() + 1;
186                         sal_Int32 start=current;
187                         if (offset.getLength() > 0) {
188                             if (word.getLength() != OUString(&wordData[current]).getLength())
189                                 one2one=sal_False;
190                             sal_Int32 convertedLength=OUString(&wordData[current]).getLength();
191                             while (wordData[current]) {
192                                 offset[count]=nStartPos + currPos + ((current-start) *
193                                     word.getLength() / convertedLength);
194                                 newStr->buffer[count++] = wordData[current++];
195                             }
196                             // offset[count-1]=nStartPos + currPos + word.getLength() - 1;
197                         } else {
198                             while (wordData[current])
199                                 newStr->buffer[count++] = wordData[current++];
200                         }
201                         currPos += word.getLength();
202                         found = sal_True;
203                     }
204                 }
205             }
206         }
207         if (!found) {
208             if (offset.getLength() > 0)
209                 offset[count]=nStartPos+currPos;
210             newStr->buffer[count++] =
211                 getOneCharConversion(aText[nStartPos+currPos], charData, charIndex);
212             currPos++;
213         }
214     }
215     if (offset.getLength() > 0)
216         offset.realloc(one2one ? 0 : count);
217     return OUString( newStr->buffer, count);
218 }
219 
220 TextConversionResult SAL_CALL
221 TextConversion_zh::getConversions( const OUString& aText, sal_Int32 nStartPos, sal_Int32 nLength,
222     const Locale& rLocale, sal_Int16 nConversionType, sal_Int32 nConversionOptions)
223     throw(  RuntimeException, IllegalArgumentException, NoSupportException )
224 {
225     TextConversionResult result;
226 
227     result.Candidates.realloc(1);
228     result.Candidates[0] = getConversion( aText, nStartPos, nLength, rLocale, nConversionType, nConversionOptions);
229     result.Boundary.startPos = nStartPos;
230     result.Boundary.endPos = nStartPos + nLength;
231 
232     return result;
233 }
234 
235 OUString SAL_CALL
236 TextConversion_zh::getConversion( const OUString& aText, sal_Int32 nStartPos, sal_Int32 nLength,
237     const Locale& rLocale, sal_Int16 nConversionType, sal_Int32 nConversionOptions)
238     throw(  RuntimeException, IllegalArgumentException, NoSupportException )
239 {
240     if (rLocale.Language.equalsAscii("zh") &&
241             ( nConversionType == TextConversionType::TO_SCHINESE ||
242             nConversionType == TextConversionType::TO_TCHINESE) ) {
243 
244         aLocale=rLocale;
245         sal_Bool toSChinese = nConversionType == TextConversionType::TO_SCHINESE;
246 
247         if (nConversionOptions & TextConversionOption::CHARACTER_BY_CHARACTER)
248             // char to char dictionary
249             return getCharConversion(aText, nStartPos, nLength, toSChinese, nConversionOptions);
250         else {
251             Sequence <sal_Int32> offset;
252             // word to word dictionary
253             return  getWordConversion(aText, nStartPos, nLength, toSChinese, nConversionOptions, offset);
254         }
255     } else
256         throw NoSupportException(); // Conversion type is not supported in this service.
257 }
258 
259 OUString SAL_CALL
260 TextConversion_zh::getConversionWithOffset( const OUString& aText, sal_Int32 nStartPos, sal_Int32 nLength,
261     const Locale& rLocale, sal_Int16 nConversionType, sal_Int32 nConversionOptions, Sequence<sal_Int32>& offset)
262     throw(  RuntimeException, IllegalArgumentException, NoSupportException )
263 {
264     if (rLocale.Language.equalsAscii("zh") &&
265             ( nConversionType == TextConversionType::TO_SCHINESE ||
266             nConversionType == TextConversionType::TO_TCHINESE) ) {
267 
268         aLocale=rLocale;
269         sal_Bool toSChinese = nConversionType == TextConversionType::TO_SCHINESE;
270 
271         if (nConversionOptions & TextConversionOption::CHARACTER_BY_CHARACTER) {
272             offset.realloc(0);
273             // char to char dictionary
274             return getCharConversion(aText, nStartPos, nLength, toSChinese, nConversionOptions);
275         } else {
276             if (offset.getLength() < 2*nLength)
277                 offset.realloc(2*nLength);
278             // word to word dictionary
279             return  getWordConversion(aText, nStartPos, nLength, toSChinese, nConversionOptions, offset);
280         }
281     } else
282         throw NoSupportException(); // Conversion type is not supported in this service.
283 }
284 
285 sal_Bool SAL_CALL
286 TextConversion_zh::interactiveConversion( const Locale& /*rLocale*/, sal_Int16 /*nTextConversionType*/, sal_Int32 /*nTextConversionOptions*/ )
287     throw(  RuntimeException, IllegalArgumentException, NoSupportException )
288 {
289     return sal_False;
290 }
291 
292 } } } }
293