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 <o3tl/temporary.hxx>
25#include <rtl/ustring.hxx>
26#include <rtl/ustrbuf.hxx>
27
29
30using namespace com::sun::star::i18n;
31using namespace com::sun::star::uno;
32
33namespace i18npool {
34
35sal_Int16 SAL_CALL TextToPronounce_zh::getType()
36{
37 return TransliterationType::ONE_TO_ONE| TransliterationType::IGNORE;
38}
39
40const sal_Unicode*
42{
43 static const sal_Unicode emptyString[]={0};
44 if (idx) {
45 sal_uInt16 address = idx[0][ch>>8];
46 if (address != 0xFFFF)
47 return reinterpret_cast<sal_Unicode const *>(
48 &idx[2][idx[1][address + (ch & 0xFF)]]);
49 }
50 return emptyString;
51}
52
53OUString
54TextToPronounce_zh::foldingImpl(const OUString & inStr, sal_Int32 startPos,
55 sal_Int32 nCount, Sequence< sal_Int32 >* pOffset)
56{
57 OUStringBuffer sb;
58 const sal_Unicode * chArr = inStr.getStr() + startPos;
59
60 if (startPos < 0)
61 throw RuntimeException();
62
63 if (startPos + nCount > inStr.getLength())
64 nCount = inStr.getLength() - startPos;
65
66 auto ppOffset = pOffset ? pOffset->getArray() : nullptr;
67 if (ppOffset)
68 ppOffset[0] = 0;
69 for (sal_Int32 i = 0; i < nCount; i++) {
70 OUString pron(getPronounce(chArr[i]));
71 sb.append(pron);
72
73 if (ppOffset)
74 ppOffset[i + 1] = (*pOffset)[i] + pron.getLength();
75 }
76 return sb.makeStringAndClear();
77}
78
79OUString SAL_CALL
81{
82 return OUString(getPronounce(inChar));
83}
84
85sal_Unicode SAL_CALL
87{
88 const sal_Unicode* pron=getPronounce(inChar);
89 if (!pron || !pron[0])
90 return 0;
91 if (pron[1])
92 throw MultipleCharsOutputException();
93 return *pron;
94}
95
96sal_Bool SAL_CALL
97TextToPronounce_zh::equals( const OUString & str1, sal_Int32 pos1, sal_Int32 nCount1, sal_Int32 & nMatch1,
98 const OUString & str2, sal_Int32 pos2, sal_Int32 nCount2, sal_Int32 & nMatch2)
99{
100 sal_Int32 realCount;
101 int i; // loop variable
102 const sal_Unicode * s1, * s2;
103
104 if (nCount1 + pos1 > str1.getLength())
105 nCount1 = str1.getLength() - pos1;
106
107 if (nCount2 + pos2 > str2.getLength())
108 nCount2 = str2.getLength() - pos2;
109
110 realCount = std::min(nCount1, nCount2);
111
112 s1 = str1.getStr() + pos1;
113 s2 = str2.getStr() + pos2;
114 for (i = 0; i < realCount; i++) {
115 const sal_Unicode *pron1 = getPronounce(*s1++);
116 const sal_Unicode *pron2 = getPronounce(*s2++);
117 if (pron1 != pron2) {
118 nMatch1 = nMatch2 = i;
119 return false;
120 }
121 }
122 nMatch1 = nMatch2 = realCount;
123 return (nCount1 == nCount2);
124}
125
126#ifdef DISABLE_DYNLOADING
127
128extern "C" {
129
130sal_uInt16 const ** get_zh_zhuyin(sal_Int16 & max_index);
131sal_uInt16 const ** get_zh_pinyin(sal_Int16 & max_index);
132
133}
134
135#endif
136
137TextToPinyin_zh_CN::TextToPinyin_zh_CN() :
138#ifndef DISABLE_DYNLOADING
139 TextToPronounce_zh("get_zh_pinyin")
140#else
141 TextToPronounce_zh(get_zh_pinyin)
142#endif
143{
144 transliterationName = "ChineseCharacterToPinyin";
145 implementationName = "com.sun.star.i18n.Transliteration.TextToPinyin_zh_CN";
146}
147
148TextToChuyin_zh_TW::TextToChuyin_zh_TW() :
149#ifndef DISABLE_DYNLOADING
150 TextToPronounce_zh("get_zh_zhuyin")
151#else
152 TextToPronounce_zh(get_zh_zhuyin)
153#endif
154{
155 transliterationName = "ChineseCharacterToChuyin";
156 implementationName = "com.sun.star.i18n.Transliteration.TextToChuyin_zh_TW";
157}
158
159#ifndef DISABLE_DYNLOADING
160
161extern "C" { static void thisModule() {} }
162
164{
165#ifdef SAL_DLLPREFIX
166 OUString lib(SAL_DLLPREFIX"index_data" SAL_DLLEXTENSION);
167#else
168 OUString lib("index_data" SAL_DLLEXTENSION);
169#endif
170 hModule = osl_loadModuleRelative(
171 &thisModule, lib.pData, SAL_LOADMODULE_DEFAULT );
172 idx=nullptr;
173 if (hModule) {
174 sal_uInt16 const ** (*function)(sal_Int16 &) = reinterpret_cast<sal_uInt16 const ** (*)(sal_Int16 &)>(osl_getFunctionSymbol(hModule, OUString::createFromAscii(func_name).pData));
175 if (function)
176 idx=function(o3tl::temporary(sal_Int16()));
177 }
178}
179
180#else
181
182TextToPronounce_zh::TextToPronounce_zh(sal_uInt16 const ** (*function)(sal_Int16 &))
183{
184 idx = function(o3tl::temporary(sal_Int16()));
185}
186
187#endif
188
190{
191#ifndef DISABLE_DYNLOADING
192 if (hModule) osl_unloadModule(hModule);
193#endif
194}
195}
196
197/* 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
constexpr T & temporary(T &&x)
unsigned char sal_Bool
sal_uInt16 sal_Unicode