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()
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)
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)
83 {
84 return OUString(getPronounce(inChar));
85 }
86
87 sal_Unicode SAL_CALL
transliterateChar2Char(sal_Unicode inChar)88 TextToPronounce_zh::transliterateChar2Char( sal_Unicode inChar)
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 {
102 sal_Int32 realCount;
103 int i; // loop variable
104 const sal_Unicode * s1, * s2;
105 const sal_Unicode *pron1, *pron2;
106
107 if (nCount1 + pos1 > str1.getLength())
108 nCount1 = str1.getLength() - pos1;
109
110 if (nCount2 + pos2 > str2.getLength())
111 nCount2 = str2.getLength() - pos2;
112
113 realCount = ((nCount1 > nCount2) ? nCount2 : nCount1);
114
115 s1 = str1.getStr() + pos1;
116 s2 = str2.getStr() + pos2;
117 for (i = 0; i < realCount; i++) {
118 pron1=getPronounce(*s1++);
119 pron2=getPronounce(*s2++);
120 if (pron1 != pron2) {
121 nMatch1 = nMatch2 = i;
122 return sal_False;
123 }
124 }
125 nMatch1 = nMatch2 = realCount;
126 return (nCount1 == nCount2);
127 }
128
TextToPinyin_zh_CN()129 TextToPinyin_zh_CN::TextToPinyin_zh_CN() : TextToPronounce_zh("get_zh_pinyin")
130 {
131 transliterationName = "ChineseCharacterToPinyin";
132 implementationName = "com.sun.star.i18n.Transliteration.TextToPinyin_zh_CN";
133 }
134
TextToChuyin_zh_TW()135 TextToChuyin_zh_TW::TextToChuyin_zh_TW() : TextToPronounce_zh("get_zh_zhuyin")
136 {
137 transliterationName = "ChineseCharacterToChuyin";
138 implementationName = "com.sun.star.i18n.Transliteration.TextToChuyin_zh_TW";
139 }
140
thisModule()141 extern "C" { static void SAL_CALL thisModule() {} }
142
TextToPronounce_zh(const sal_Char * func_name)143 TextToPronounce_zh::TextToPronounce_zh(const sal_Char* func_name)
144 {
145 #ifdef SAL_DLLPREFIX
146 OUString lib=OUString::createFromAscii( SAL_DLLPREFIX "index_data" SAL_DLLEXTENSION);
147 #else
148 OUString lib=OUString::createFromAscii( "index_data" SAL_DLLEXTENSION);
149 #endif
150 hModule = osl_loadModuleRelative(
151 &thisModule, lib.pData, SAL_LOADMODULE_DEFAULT );
152 idx=NULL;
153 if (hModule) {
154 sal_uInt16** (*function)() = (sal_uInt16** (*)()) osl_getFunctionSymbol(hModule, OUString::createFromAscii(func_name).pData);
155 if (function)
156 idx=function();
157 }
158 }
~TextToPronounce_zh()159 TextToPronounce_zh::~TextToPronounce_zh()
160 {
161 if (hModule) osl_unloadModule(hModule);
162 }
163 } } } }
164