LibreOffice Module i18npool (master)  1
textToPronounce_zh.cxx
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
5  * This Source Code Form is subject to the terms of the Mozilla Public
6  * License, v. 2.0. If a copy of the MPL was not distributed with this
7  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
11  * Licensed to the Apache Software Foundation (ASF) under one or more
12  * contributor license agreements. See the NOTICE file distributed
13  * with this work for additional information regarding copyright
14  * ownership. The ASF licenses this file to you under the Apache
15  * License, Version 2.0 (the "License"); you may not use this file
16  * except in compliance with the License. You may obtain a copy of
17  * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18  */
19 
20 #include <sal/config.h>
21 
22 #include <com/sun/star/i18n/MultipleCharsOutputException.hpp>
23 #include <com/sun/star/i18n/TransliterationType.hpp>
24 #include <rtl/ustring.hxx>
25 #include <rtl/ustrbuf.hxx>
26 
27 #include <textToPronounce_zh.hxx>
28 
29 using namespace com::sun::star::i18n;
30 using namespace com::sun::star::uno;
31 
32 namespace i18npool {
33 
34 sal_Int16 SAL_CALL TextToPronounce_zh::getType()
35 {
36  return TransliterationType::ONE_TO_ONE| TransliterationType::IGNORE;
37 }
38 
39 const sal_Unicode*
40 TextToPronounce_zh::getPronounce(const sal_Unicode ch)
41 {
42  static const sal_Unicode emptyString[]={0};
43  if (idx) {
44  sal_uInt16 address = idx[0][ch>>8];
45  if (address != 0xFFFF)
46  return reinterpret_cast<sal_Unicode *>(
47  &idx[2][idx[1][address + (ch & 0xFF)]]);
48  }
49  return emptyString;
50 }
51 
52 OUString
53 TextToPronounce_zh::foldingImpl(const OUString & inStr, sal_Int32 startPos,
54  sal_Int32 nCount, Sequence< sal_Int32 > & offset, bool useOffset)
55 {
56  OUStringBuffer sb;
57  const sal_Unicode * chArr = inStr.getStr() + startPos;
58 
59  if (startPos < 0)
60  throw RuntimeException();
61 
62  if (startPos + nCount > inStr.getLength())
63  nCount = inStr.getLength() - startPos;
64 
65  offset[0] = 0;
66  for (sal_Int32 i = 0; i < nCount; i++) {
67  OUString pron(getPronounce(chArr[i]));
68  sb.append(pron);
69 
70  if (useOffset)
71  offset[i + 1] = offset[i] + pron.getLength();
72  }
73  return sb.makeStringAndClear();
74 }
75 
76 OUString SAL_CALL
77 TextToPronounce_zh::transliterateChar2String( sal_Unicode inChar)
78 {
79  return OUString(getPronounce(inChar));
80 }
81 
82 sal_Unicode SAL_CALL
83 TextToPronounce_zh::transliterateChar2Char( sal_Unicode inChar)
84 {
85  const sal_Unicode* pron=getPronounce(inChar);
86  if (!pron || !pron[0])
87  return 0;
88  if (pron[1])
89  throw MultipleCharsOutputException();
90  return *pron;
91 }
92 
93 sal_Bool SAL_CALL
94 TextToPronounce_zh::equals( const OUString & str1, sal_Int32 pos1, sal_Int32 nCount1, sal_Int32 & nMatch1,
95  const OUString & str2, sal_Int32 pos2, sal_Int32 nCount2, sal_Int32 & nMatch2)
96 {
97  sal_Int32 realCount;
98  int i; // loop variable
99  const sal_Unicode * s1, * s2;
100 
101  if (nCount1 + pos1 > str1.getLength())
102  nCount1 = str1.getLength() - pos1;
103 
104  if (nCount2 + pos2 > str2.getLength())
105  nCount2 = str2.getLength() - pos2;
106 
107  realCount = std::min(nCount1, nCount2);
108 
109  s1 = str1.getStr() + pos1;
110  s2 = str2.getStr() + pos2;
111  for (i = 0; i < realCount; i++) {
112  const sal_Unicode *pron1 = getPronounce(*s1++);
113  const sal_Unicode *pron2 = getPronounce(*s2++);
114  if (pron1 != pron2) {
115  nMatch1 = nMatch2 = i;
116  return false;
117  }
118  }
119  nMatch1 = nMatch2 = realCount;
120  return (nCount1 == nCount2);
121 }
122 
123 #ifdef DISABLE_DYNLOADING
124 
125 extern "C" {
126 
127 sal_uInt16** get_zh_zhuyin();
128 sal_uInt16** get_zh_pinyin();
129 
130 }
131 
132 #endif
133 
134 TextToPinyin_zh_CN::TextToPinyin_zh_CN() :
135 #ifndef DISABLE_DYNLOADING
136  TextToPronounce_zh("get_zh_pinyin")
137 #else
138  TextToPronounce_zh(get_zh_pinyin)
139 #endif
140 {
141  transliterationName = "ChineseCharacterToPinyin";
142  implementationName = "com.sun.star.i18n.Transliteration.TextToPinyin_zh_CN";
143 }
144 
145 TextToChuyin_zh_TW::TextToChuyin_zh_TW() :
146 #ifndef DISABLE_DYNLOADING
147  TextToPronounce_zh("get_zh_zhuyin")
148 #else
149  TextToPronounce_zh(get_zh_zhuyin)
150 #endif
151 {
152  transliterationName = "ChineseCharacterToChuyin";
153  implementationName = "com.sun.star.i18n.Transliteration.TextToChuyin_zh_TW";
154 }
155 
156 #ifndef DISABLE_DYNLOADING
157 
158 extern "C" { static void thisModule() {} }
159 
161 {
162 #ifdef SAL_DLLPREFIX
163  OUString lib(SAL_DLLPREFIX"index_data" SAL_DLLEXTENSION);
164 #else
165  OUString lib("index_data" SAL_DLLEXTENSION);
166 #endif
167  hModule = osl_loadModuleRelative(
168  &thisModule, lib.pData, SAL_LOADMODULE_DEFAULT );
169  idx=nullptr;
170  if (hModule) {
171  sal_uInt16** (*function)() = reinterpret_cast<sal_uInt16** (*)()>(osl_getFunctionSymbol(hModule, OUString::createFromAscii(func_name).pData));
172  if (function)
173  idx=function();
174  }
175 }
176 
177 #else
178 
179 TextToPronounce_zh::TextToPronounce_zh(sal_uInt16 ** (*function)())
180 {
181  idx = function();
182 }
183 
184 #endif
185 
187 {
188 #ifndef DISABLE_DYNLOADING
189  if (hModule) osl_unloadModule(hModule);
190 #endif
191 }
192 }
193 
194 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
#define SAL_DLLEXTENSION
std::unique_ptr< ContentProperties > pData
#define SAL_DLLPREFIX
#define DISABLE_DYNLOADING
sal_uInt16 sal_Unicode
virtual ~TextToPronounce_zh() override
int i
unsigned char sal_Bool
TextToPronounce_zh(const char *func_name)
Constant values shared between i18npool and, for example, the number formatter.
const sal_uInt16 idx[]
static void thisModule()
Definition: xdictionary.cxx:41