1449ab281SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3449ab281SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4449ab281SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5449ab281SAndrew Rist  * distributed with this work for additional information
6449ab281SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7449ab281SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8449ab281SAndrew Rist  * "License"); you may not use this file except in compliance
9449ab281SAndrew Rist  * with the License.  You may obtain a copy of the License at
10449ab281SAndrew Rist  *
11449ab281SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12449ab281SAndrew Rist  *
13449ab281SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14449ab281SAndrew Rist  * software distributed under the License is distributed on an
15449ab281SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16449ab281SAndrew Rist  * KIND, either express or implied.  See the License for the
17449ab281SAndrew Rist  * specific language governing permissions and limitations
18449ab281SAndrew Rist  * under the License.
19449ab281SAndrew Rist  *
20449ab281SAndrew Rist  *************************************************************/
21449ab281SAndrew Rist 
22449ab281SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_i18npool.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir // prevent internal compiler error with MSVC6SP3
28cdf0e10cSrcweir #include <utility>
29cdf0e10cSrcweir 
30cdf0e10cSrcweir #include <rtl/string.hxx>
31cdf0e10cSrcweir #include <rtl/ustrbuf.hxx>
32cdf0e10cSrcweir #define TRANSLITERATION_ALL
33cdf0e10cSrcweir #include <textToPronounce_zh.hxx>
34cdf0e10cSrcweir 
35cdf0e10cSrcweir using namespace com::sun::star::uno;
36cdf0e10cSrcweir using namespace rtl;
37cdf0e10cSrcweir 
38cdf0e10cSrcweir namespace com { namespace sun { namespace star { namespace i18n {
39cdf0e10cSrcweir 
getType()40cdf0e10cSrcweir sal_Int16 SAL_CALL TextToPronounce_zh::getType() throw (RuntimeException)
41cdf0e10cSrcweir {
42cdf0e10cSrcweir     return TransliterationType::ONE_TO_ONE| TransliterationType::IGNORE;
43cdf0e10cSrcweir }
44cdf0e10cSrcweir 
45cdf0e10cSrcweir const sal_Unicode* SAL_CALL
getPronounce(const sal_Unicode ch)46cdf0e10cSrcweir TextToPronounce_zh::getPronounce(const sal_Unicode ch)
47cdf0e10cSrcweir {
48cdf0e10cSrcweir     static const sal_Unicode emptyString[]={0};
49cdf0e10cSrcweir     if (idx) {
50cdf0e10cSrcweir         sal_uInt16 address = idx[0][ch>>8];
51cdf0e10cSrcweir         if (address != 0xFFFF)
52cdf0e10cSrcweir             return &idx[2][idx[1][address + (ch & 0xFF)]];
53cdf0e10cSrcweir     }
54cdf0e10cSrcweir     return emptyString;
55cdf0e10cSrcweir }
56cdf0e10cSrcweir 
57cdf0e10cSrcweir OUString SAL_CALL
folding(const OUString & inStr,sal_Int32 startPos,sal_Int32 nCount,Sequence<sal_Int32> & offset)58cdf0e10cSrcweir TextToPronounce_zh::folding(const OUString & inStr, sal_Int32 startPos,
59cdf0e10cSrcweir         sal_Int32 nCount, Sequence< sal_Int32 > & offset) throw (RuntimeException)
60cdf0e10cSrcweir {
61cdf0e10cSrcweir     OUStringBuffer sb;
62cdf0e10cSrcweir     const sal_Unicode * chArr = inStr.getStr() + startPos;
63cdf0e10cSrcweir 
64cdf0e10cSrcweir     if (startPos < 0)
65cdf0e10cSrcweir         throw RuntimeException();
66cdf0e10cSrcweir 
67cdf0e10cSrcweir     if (startPos + nCount > inStr.getLength())
68cdf0e10cSrcweir         nCount = inStr.getLength() - startPos;
69cdf0e10cSrcweir 
70cdf0e10cSrcweir     offset[0] = 0;
71cdf0e10cSrcweir     for (sal_Int32 i = 0; i < nCount; i++) {
72cdf0e10cSrcweir         OUString pron(getPronounce(chArr[i]));
73cdf0e10cSrcweir         sb.append(pron);
74cdf0e10cSrcweir 
75cdf0e10cSrcweir         if (useOffset)
76cdf0e10cSrcweir             offset[i + 1] = offset[i] + pron.getLength();
77cdf0e10cSrcweir     }
78cdf0e10cSrcweir     return sb.makeStringAndClear();
79cdf0e10cSrcweir }
80cdf0e10cSrcweir 
81cdf0e10cSrcweir OUString SAL_CALL
transliterateChar2String(sal_Unicode inChar)82cdf0e10cSrcweir TextToPronounce_zh::transliterateChar2String( sal_Unicode inChar) throw(RuntimeException)
83cdf0e10cSrcweir {
84cdf0e10cSrcweir     return OUString(getPronounce(inChar));
85cdf0e10cSrcweir }
86cdf0e10cSrcweir 
87cdf0e10cSrcweir sal_Unicode SAL_CALL
transliterateChar2Char(sal_Unicode inChar)88cdf0e10cSrcweir TextToPronounce_zh::transliterateChar2Char( sal_Unicode inChar) throw(RuntimeException, MultipleCharsOutputException)
89cdf0e10cSrcweir {
90cdf0e10cSrcweir     const sal_Unicode* pron=getPronounce(inChar);
91cdf0e10cSrcweir     if (!pron || !pron[0])
92cdf0e10cSrcweir         return 0;
93cdf0e10cSrcweir     if (pron[1])
94cdf0e10cSrcweir         throw MultipleCharsOutputException();
95cdf0e10cSrcweir     return *pron;
96cdf0e10cSrcweir }
97cdf0e10cSrcweir 
98cdf0e10cSrcweir 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)99cdf0e10cSrcweir TextToPronounce_zh::equals( const OUString & str1, sal_Int32 pos1, sal_Int32 nCount1, sal_Int32 & nMatch1,
100cdf0e10cSrcweir         const OUString & str2, sal_Int32 pos2, sal_Int32 nCount2, sal_Int32 & nMatch2)
101cdf0e10cSrcweir         throw (RuntimeException)
102cdf0e10cSrcweir {
103cdf0e10cSrcweir     sal_Int32 realCount;
104cdf0e10cSrcweir     int i;  // loop variable
105cdf0e10cSrcweir     const sal_Unicode * s1, * s2;
106cdf0e10cSrcweir     const sal_Unicode *pron1, *pron2;
107cdf0e10cSrcweir 
108cdf0e10cSrcweir     if (nCount1 + pos1 > str1.getLength())
109cdf0e10cSrcweir         nCount1 = str1.getLength() - pos1;
110cdf0e10cSrcweir 
111cdf0e10cSrcweir     if (nCount2 + pos2 > str2.getLength())
112cdf0e10cSrcweir         nCount2 = str2.getLength() - pos2;
113cdf0e10cSrcweir 
114cdf0e10cSrcweir     realCount = ((nCount1 > nCount2) ? nCount2 : nCount1);
115cdf0e10cSrcweir 
116cdf0e10cSrcweir     s1 = str1.getStr() + pos1;
117cdf0e10cSrcweir     s2 = str2.getStr() + pos2;
118cdf0e10cSrcweir     for (i = 0; i < realCount; i++) {
119cdf0e10cSrcweir         pron1=getPronounce(*s1++);
120cdf0e10cSrcweir         pron2=getPronounce(*s2++);
121cdf0e10cSrcweir         if (pron1 != pron2) {
122cdf0e10cSrcweir             nMatch1 = nMatch2 = i;
123cdf0e10cSrcweir             return sal_False;
124cdf0e10cSrcweir         }
125cdf0e10cSrcweir     }
126cdf0e10cSrcweir     nMatch1 = nMatch2 = realCount;
127cdf0e10cSrcweir     return (nCount1 == nCount2);
128cdf0e10cSrcweir }
129cdf0e10cSrcweir 
TextToPinyin_zh_CN()130cdf0e10cSrcweir TextToPinyin_zh_CN::TextToPinyin_zh_CN() : TextToPronounce_zh("get_zh_pinyin")
131cdf0e10cSrcweir {
132cdf0e10cSrcweir         transliterationName = "ChineseCharacterToPinyin";
133cdf0e10cSrcweir         implementationName = "com.sun.star.i18n.Transliteration.TextToPinyin_zh_CN";
134cdf0e10cSrcweir }
135cdf0e10cSrcweir 
TextToChuyin_zh_TW()136cdf0e10cSrcweir TextToChuyin_zh_TW::TextToChuyin_zh_TW() : TextToPronounce_zh("get_zh_zhuyin")
137cdf0e10cSrcweir {
138cdf0e10cSrcweir         transliterationName = "ChineseCharacterToChuyin";
139cdf0e10cSrcweir         implementationName = "com.sun.star.i18n.Transliteration.TextToChuyin_zh_TW";
140cdf0e10cSrcweir }
141cdf0e10cSrcweir 
thisModule()142cdf0e10cSrcweir extern "C" { static void SAL_CALL thisModule() {} }
143cdf0e10cSrcweir 
TextToPronounce_zh(const sal_Char * func_name)144cdf0e10cSrcweir TextToPronounce_zh::TextToPronounce_zh(const sal_Char* func_name)
145cdf0e10cSrcweir {
146cdf0e10cSrcweir #ifdef SAL_DLLPREFIX
147*6f51c329SHerbert Dürr     OUString lib=OUString::createFromAscii( SAL_DLLPREFIX "index_data" SAL_DLLEXTENSION);
148cdf0e10cSrcweir #else
149*6f51c329SHerbert Dürr     OUString lib=OUString::createFromAscii( "index_data" SAL_DLLEXTENSION);
150cdf0e10cSrcweir #endif
151cdf0e10cSrcweir     hModule = osl_loadModuleRelative(
152cdf0e10cSrcweir         &thisModule, lib.pData, SAL_LOADMODULE_DEFAULT );
153cdf0e10cSrcweir     idx=NULL;
154cdf0e10cSrcweir     if (hModule) {
155cdf0e10cSrcweir         sal_uInt16** (*function)() = (sal_uInt16** (*)()) osl_getFunctionSymbol(hModule, OUString::createFromAscii(func_name).pData);
156cdf0e10cSrcweir         if (function)
157cdf0e10cSrcweir             idx=function();
158cdf0e10cSrcweir     }
159cdf0e10cSrcweir }
~TextToPronounce_zh()160cdf0e10cSrcweir TextToPronounce_zh::~TextToPronounce_zh()
161cdf0e10cSrcweir {
162cdf0e10cSrcweir     if (hModule) osl_unloadModule(hModule);
163cdf0e10cSrcweir }
164cdf0e10cSrcweir } } } }
165