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 // prevent internal compiler error with MSVC6SP3
28 #include <utility>
29 
30 #include <rtl/string.hxx>
31 #include <rtl/ustrbuf.hxx>
32 #define TRANSLITERATION_ALL
33 #include <textToPronounce_zh.hxx>
34 
35 using namespace com::sun::star::uno;
36 using namespace rtl;
37 
38 namespace com { namespace sun { namespace star { namespace i18n {
39 
getType()40 sal_Int16 SAL_CALL TextToPronounce_zh::getType() throw (RuntimeException)
41 {
42     return TransliterationType::ONE_TO_ONE| TransliterationType::IGNORE;
43 }
44 
45 const sal_Unicode* SAL_CALL
getPronounce(const sal_Unicode ch)46 TextToPronounce_zh::getPronounce(const sal_Unicode ch)
47 {
48     static const sal_Unicode emptyString[]={0};
49     if (idx) {
50         sal_uInt16 address = idx[0][ch>>8];
51         if (address != 0xFFFF)
52             return &idx[2][idx[1][address + (ch & 0xFF)]];
53     }
54     return emptyString;
55 }
56 
57 OUString SAL_CALL
folding(const OUString & inStr,sal_Int32 startPos,sal_Int32 nCount,Sequence<sal_Int32> & offset)58 TextToPronounce_zh::folding(const OUString & inStr, sal_Int32 startPos,
59         sal_Int32 nCount, Sequence< sal_Int32 > & offset) throw (RuntimeException)
60 {
61     OUStringBuffer sb;
62     const sal_Unicode * chArr = inStr.getStr() + startPos;
63 
64     if (startPos < 0)
65         throw RuntimeException();
66 
67     if (startPos + nCount > inStr.getLength())
68         nCount = inStr.getLength() - startPos;
69 
70     offset[0] = 0;
71     for (sal_Int32 i = 0; i < nCount; i++) {
72         OUString pron(getPronounce(chArr[i]));
73         sb.append(pron);
74 
75         if (useOffset)
76             offset[i + 1] = offset[i] + pron.getLength();
77     }
78     return sb.makeStringAndClear();
79 }
80 
81 OUString SAL_CALL
transliterateChar2String(sal_Unicode inChar)82 TextToPronounce_zh::transliterateChar2String( sal_Unicode inChar) throw(RuntimeException)
83 {
84     return OUString(getPronounce(inChar));
85 }
86 
87 sal_Unicode SAL_CALL
transliterateChar2Char(sal_Unicode inChar)88 TextToPronounce_zh::transliterateChar2Char( sal_Unicode inChar) throw(RuntimeException, MultipleCharsOutputException)
89 {
90     const sal_Unicode* pron=getPronounce(inChar);
91     if (!pron || !pron[0])
92         return 0;
93     if (pron[1])
94         throw MultipleCharsOutputException();
95     return *pron;
96 }
97 
98 sal_Bool SAL_CALL
equals(const OUString & str1,sal_Int32 pos1,sal_Int32 nCount1,sal_Int32 & nMatch1,const OUString & str2,sal_Int32 pos2,sal_Int32 nCount2,sal_Int32 & nMatch2)99 TextToPronounce_zh::equals( const OUString & str1, sal_Int32 pos1, sal_Int32 nCount1, sal_Int32 & nMatch1,
100         const OUString & str2, sal_Int32 pos2, sal_Int32 nCount2, sal_Int32 & nMatch2)
101         throw (RuntimeException)
102 {
103     sal_Int32 realCount;
104     int i;  // loop variable
105     const sal_Unicode * s1, * s2;
106     const sal_Unicode *pron1, *pron2;
107 
108     if (nCount1 + pos1 > str1.getLength())
109         nCount1 = str1.getLength() - pos1;
110 
111     if (nCount2 + pos2 > str2.getLength())
112         nCount2 = str2.getLength() - pos2;
113 
114     realCount = ((nCount1 > nCount2) ? nCount2 : nCount1);
115 
116     s1 = str1.getStr() + pos1;
117     s2 = str2.getStr() + pos2;
118     for (i = 0; i < realCount; i++) {
119         pron1=getPronounce(*s1++);
120         pron2=getPronounce(*s2++);
121         if (pron1 != pron2) {
122             nMatch1 = nMatch2 = i;
123             return sal_False;
124         }
125     }
126     nMatch1 = nMatch2 = realCount;
127     return (nCount1 == nCount2);
128 }
129 
TextToPinyin_zh_CN()130 TextToPinyin_zh_CN::TextToPinyin_zh_CN() : TextToPronounce_zh("get_zh_pinyin")
131 {
132         transliterationName = "ChineseCharacterToPinyin";
133         implementationName = "com.sun.star.i18n.Transliteration.TextToPinyin_zh_CN";
134 }
135 
TextToChuyin_zh_TW()136 TextToChuyin_zh_TW::TextToChuyin_zh_TW() : TextToPronounce_zh("get_zh_zhuyin")
137 {
138         transliterationName = "ChineseCharacterToChuyin";
139         implementationName = "com.sun.star.i18n.Transliteration.TextToChuyin_zh_TW";
140 }
141 
thisModule()142 extern "C" { static void SAL_CALL thisModule() {} }
143 
TextToPronounce_zh(const sal_Char * func_name)144 TextToPronounce_zh::TextToPronounce_zh(const sal_Char* func_name)
145 {
146 #ifdef SAL_DLLPREFIX
147     OUString lib=OUString::createFromAscii( SAL_DLLPREFIX "index_data" SAL_DLLEXTENSION);
148 #else
149     OUString lib=OUString::createFromAscii( "index_data" SAL_DLLEXTENSION);
150 #endif
151     hModule = osl_loadModuleRelative(
152         &thisModule, lib.pData, SAL_LOADMODULE_DEFAULT );
153     idx=NULL;
154     if (hModule) {
155         sal_uInt16** (*function)() = (sal_uInt16** (*)()) osl_getFunctionSymbol(hModule, OUString::createFromAscii(func_name).pData);
156         if (function)
157             idx=function();
158     }
159 }
~TextToPronounce_zh()160 TextToPronounce_zh::~TextToPronounce_zh()
161 {
162     if (hModule) osl_unloadModule(hModule);
163 }
164 } } } }
165