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
28
29using namespace com::sun::star::i18n;
30using namespace com::sun::star::uno;
31
32namespace i18npool {
33
34sal_Int16 SAL_CALL TextToPronounce_zh::getType()
35{
36 return TransliterationType::ONE_TO_ONE| TransliterationType::IGNORE;
37}
38
39const sal_Unicode*
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
52OUString
53TextToPronounce_zh::foldingImpl(const OUString & inStr, sal_Int32 startPos,
54 sal_Int32 nCount, Sequence< sal_Int32 >* pOffset)
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 auto ppOffset = pOffset ? pOffset->getArray() : nullptr;
66 if (ppOffset)
67 ppOffset[0] = 0;
68 for (sal_Int32 i = 0; i < nCount; i++) {
69 OUString pron(getPronounce(chArr[i]));
70 sb.append(pron);
71
72 if (ppOffset)
73 ppOffset[i + 1] = (*pOffset)[i] + pron.getLength();
74 }
75 return sb.makeStringAndClear();
76}
77
78OUString SAL_CALL
80{
81 return OUString(getPronounce(inChar));
82}
83
84sal_Unicode SAL_CALL
86{
87 const sal_Unicode* pron=getPronounce(inChar);
88 if (!pron || !pron[0])
89 return 0;
90 if (pron[1])
91 throw MultipleCharsOutputException();
92 return *pron;
93}
94
95sal_Bool SAL_CALL
96TextToPronounce_zh::equals( const OUString & str1, sal_Int32 pos1, sal_Int32 nCount1, sal_Int32 & nMatch1,
97 const OUString & str2, sal_Int32 pos2, sal_Int32 nCount2, sal_Int32 & nMatch2)
98{
99 sal_Int32 realCount;
100 int i; // loop variable
101 const sal_Unicode * s1, * s2;
102
103 if (nCount1 + pos1 > str1.getLength())
104 nCount1 = str1.getLength() - pos1;
105
106 if (nCount2 + pos2 > str2.getLength())
107 nCount2 = str2.getLength() - pos2;
108
109 realCount = std::min(nCount1, nCount2);
110
111 s1 = str1.getStr() + pos1;
112 s2 = str2.getStr() + pos2;
113 for (i = 0; i < realCount; i++) {
114 const sal_Unicode *pron1 = getPronounce(*s1++);
115 const sal_Unicode *pron2 = getPronounce(*s2++);
116 if (pron1 != pron2) {
117 nMatch1 = nMatch2 = i;
118 return false;
119 }
120 }
121 nMatch1 = nMatch2 = realCount;
122 return (nCount1 == nCount2);
123}
124
125#ifdef DISABLE_DYNLOADING
126
127extern "C" {
128
129sal_uInt16** get_collator_data_zh_zhuyin();
130sal_uInt16** get_collator_data_zh_pinyin();
131
132}
133
134#endif
135
136TextToPinyin_zh_CN::TextToPinyin_zh_CN() :
137#ifndef DISABLE_DYNLOADING
138 TextToPronounce_zh("get_zh_pinyin")
139#else
140 TextToPronounce_zh(get_collator_data_zh_pinyin)
141#endif
142{
143 transliterationName = "ChineseCharacterToPinyin";
144 implementationName = "com.sun.star.i18n.Transliteration.TextToPinyin_zh_CN";
145}
146
147TextToChuyin_zh_TW::TextToChuyin_zh_TW() :
148#ifndef DISABLE_DYNLOADING
149 TextToPronounce_zh("get_zh_zhuyin")
150#else
151 TextToPronounce_zh(get_collator_data_zh_zhuyin)
152#endif
153{
154 transliterationName = "ChineseCharacterToChuyin";
155 implementationName = "com.sun.star.i18n.Transliteration.TextToChuyin_zh_TW";
156}
157
158#ifndef DISABLE_DYNLOADING
159
160extern "C" { static void thisModule() {} }
161
163{
164#ifdef SAL_DLLPREFIX
165 OUString lib(SAL_DLLPREFIX"index_data" SAL_DLLEXTENSION);
166#else
167 OUString lib("index_data" SAL_DLLEXTENSION);
168#endif
169 hModule = osl_loadModuleRelative(
170 &thisModule, lib.pData, SAL_LOADMODULE_DEFAULT );
171 idx=nullptr;
172 if (hModule) {
173 sal_uInt16** (*function)() = reinterpret_cast<sal_uInt16** (*)()>(osl_getFunctionSymbol(hModule, OUString::createFromAscii(func_name).pData));
174 if (function)
175 idx=function();
176 }
177}
178
179#else
180
181TextToPronounce_zh::TextToPronounce_zh(sal_uInt16 ** (*function)())
182{
183 idx = function();
184}
185
186#endif
187
189{
190#ifndef DISABLE_DYNLOADING
191 if (hModule) osl_unloadModule(hModule);
192#endif
193}
194}
195
196/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
#define SAL_DLLPREFIX
OUString foldingImpl(const OUString &inStr, sal_Int32 startPos, sal_Int32 nCount, css::uno::Sequence< sal_Int32 > *pOffset) override
sal_Unicode SAL_CALL transliterateChar2Char(sal_Unicode inChar) override
sal_Int16 SAL_CALL getType() override
const sal_Unicode * getPronounce(const sal_Unicode ch)
virtual ~TextToPronounce_zh() override
TextToPronounce_zh(const char *func_name)
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) override
OUString SAL_CALL transliterateChar2String(sal_Unicode inChar) override
#define SAL_DLLEXTENSION
int nCount
std::unique_ptr< sal_Int32[]> pData
int i
Constant values shared between i18npool and, for example, the number formatter.
static void thisModule()
Definition: xdictionary.cxx:41
unsigned char sal_Bool
sal_uInt16 sal_Unicode