Version 5.4.3.2, tag libreoffice-5.4.3.2
[LibreOffice.git] / i18npool / source / indexentry / indexentrysupplier_asian.cxx
bloba6594c381b6579e773840c9307a43c39991a7356
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
21 #include <rtl/ustrbuf.hxx>
22 #include <indexentrysupplier_asian.hxx>
23 #include <data/indexdata_alphanumeric.h>
25 using namespace ::com::sun::star::uno;
26 using namespace ::com::sun::star::lang;
28 namespace com { namespace sun { namespace star { namespace i18n {
30 #ifndef DISABLE_DYNLOADING
32 extern "C" { static void SAL_CALL thisModule() {} }
34 #endif
36 IndexEntrySupplier_asian::IndexEntrySupplier_asian(
37 const Reference < XComponentContext >& rxContext ) : IndexEntrySupplier_Common(rxContext)
39 implementationName = "com.sun.star.i18n.IndexEntrySupplier_asian";
40 #ifndef DISABLE_DYNLOADING
41 #ifdef SAL_DLLPREFIX
42 OUString lib(SAL_DLLPREFIX"index_data" SAL_DLLEXTENSION);
43 #else
44 OUString lib("index_data" SAL_DLLEXTENSION);
45 #endif
46 hModule = osl_loadModuleRelative(
47 &thisModule, lib.pData, SAL_LOADMODULE_DEFAULT );
48 #endif
51 IndexEntrySupplier_asian::~IndexEntrySupplier_asian()
53 #ifndef DISABLE_DYNLOADING
54 if (hModule) osl_unloadModule(hModule);
55 #endif
58 #ifdef DISABLE_DYNLOADING
60 extern "C" {
62 sal_uInt16** get_indexdata_ko_dict(sal_Int16*);
63 sal_uInt16** get_indexdata_zh_TW_radical(sal_Int16*);
64 sal_uInt16** get_indexdata_zh_TW_stroke(sal_Int16*);
65 sal_uInt16** get_indexdata_zh_pinyin(sal_Int16*);
66 sal_uInt16** get_indexdata_zh_radical(sal_Int16*);
67 sal_uInt16** get_indexdata_zh_stroke(sal_Int16*);
68 sal_uInt16** get_indexdata_zh_zhuyin(sal_Int16*);
70 sal_uInt16** get_ko_phonetic(sal_Int16*);
71 sal_uInt16** get_zh_pinyin(sal_Int16*);
72 sal_uInt16** get_zh_zhuyin(sal_Int16*);
76 #endif
78 OUString SAL_CALL
79 IndexEntrySupplier_asian::getIndexCharacter( const OUString& rIndexEntry,
80 const Locale& rLocale, const OUString& rAlgorithm )
82 sal_Int32 i=0;
83 sal_uInt32 ch = rIndexEntry.iterateCodePoints(&i, 0);
85 sal_uInt16** (*func)(sal_Int16*)=nullptr;
86 #ifndef DISABLE_DYNLOADING
87 if (hModule) {
88 OUString get("get_indexdata_");
89 if ( rLocale.Language == "zh" && OUString( "TW HK MO" ).indexOf(rLocale.Country) >= 0 )
90 func=reinterpret_cast<sal_uInt16** (*)(sal_Int16*)>(osl_getFunctionSymbol(hModule, OUString(get+rLocale.Language+"_TW_"+rAlgorithm).pData));
91 if (!func)
92 func=reinterpret_cast<sal_uInt16** (*)(sal_Int16*)>(osl_getFunctionSymbol(hModule, OUString(get+rLocale.Language+"_"+rAlgorithm).pData));
94 #else
95 if ( rLocale.Language == "zh" && OUString( "TW HK MO" ).indexOf(rLocale.Country) >= 0 ) {
96 if ( rAlgorithm == "radical" )
97 func = get_indexdata_zh_TW_radical;
98 else if ( rAlgorithm == "stroke" )
99 func = get_indexdata_zh_TW_stroke;
101 if (!func) {
102 if ( rLocale.Language == "ko" ) {
103 if ( rAlgorithm == "dict" )
104 func = get_indexdata_ko_dict;
105 } else if ( rLocale.Language == "zh" ) {
106 if ( rAlgorithm == "pinyin" )
107 func = get_indexdata_zh_pinyin;
108 else if ( rAlgorithm == "radical" )
109 func = get_indexdata_zh_radical;
110 else if ( rAlgorithm == "stroke" )
111 func = get_indexdata_zh_stroke;
112 else if ( rAlgorithm == "zhuyin" )
113 func = get_indexdata_zh_zhuyin;
116 #endif
117 if (func) {
118 sal_Int16 max_index;
119 sal_uInt16** idx=func(&max_index);
120 if (((sal_Int16)(ch >> 8)) <= max_index) {
121 sal_uInt16 address=idx[0][ch >> 8];
122 if (address != 0xFFFF) {
123 address=idx[1][address+(ch & 0xFF)];
124 return idx[2]
125 ? OUString(
126 reinterpret_cast<sal_Unicode *>(&idx[2][address]))
127 : OUString(sal_Unicode(address));
132 // using alphanumeric index for non-define string
133 return OUString(&idxStr[(ch & 0xFFFFFF00) ? 0 : ch], 1);
136 OUString SAL_CALL
137 IndexEntrySupplier_asian::getIndexKey( const OUString& rIndexEntry,
138 const OUString& rPhoneticEntry, const Locale& rLocale)
140 return getIndexCharacter(getEntry(rIndexEntry, rPhoneticEntry, rLocale), rLocale, aAlgorithm);
143 sal_Int16 SAL_CALL
144 IndexEntrySupplier_asian::compareIndexEntry(
145 const OUString& rIndexEntry1, const OUString& rPhoneticEntry1, const Locale& rLocale1,
146 const OUString& rIndexEntry2, const OUString& rPhoneticEntry2, const Locale& rLocale2 )
148 sal_Int32 result = collator->compareString(getEntry(rIndexEntry1, rPhoneticEntry1, rLocale1),
149 getEntry(rIndexEntry2, rPhoneticEntry2, rLocale2));
151 // equivalent of phonetic entries does not mean equivalent of index entries.
152 // we have to continue comparing index entry here.
153 if (result == 0 && usePhonetic && !rPhoneticEntry1.isEmpty() &&
154 rLocale1.Language == rLocale2.Language && rLocale1.Country == rLocale2.Country &&
155 rLocale1.Variant == rLocale2.Variant)
156 result = collator->compareString(rIndexEntry1, rIndexEntry2);
157 return sal::static_int_cast< sal_Int16 >(result); // result in { -1, 0, 1 }
160 OUString SAL_CALL
161 IndexEntrySupplier_asian::getPhoneticCandidate( const OUString& rIndexEntry,
162 const Locale& rLocale )
164 sal_uInt16 **(*func)(sal_Int16*)=nullptr;
165 #ifndef DISABLE_DYNLOADING
166 if (hModule) {
167 const sal_Char *func_name=nullptr;
168 if ( rLocale.Language == "zh" )
169 func_name=(OUString("TW HK MO").indexOf(rLocale.Country) >= 0) ? "get_zh_zhuyin" : "get_zh_pinyin";
170 else if ( rLocale.Language == "ko" )
171 func_name="get_ko_phonetic";
172 if (func_name)
173 func=reinterpret_cast<sal_uInt16 **(*)(sal_Int16*)>(osl_getFunctionSymbol(hModule, OUString::createFromAscii(func_name).pData));
175 #else
176 if ( rLocale.Language == "zh" )
177 func = (OUString("TW HK MO").indexOf(rLocale.Country) >= 0) ? get_zh_zhuyin : get_zh_pinyin;
178 else if ( rLocale.Language == "ko" )
179 func = get_ko_phonetic;
181 #endif
182 if (func) {
183 OUStringBuffer candidate;
184 sal_Int16 max_index;
185 sal_uInt16** idx=func(&max_index);
186 for (sal_Int32 i=0,j=0; i < rIndexEntry.getLength(); i=j) {
187 sal_uInt32 ch = rIndexEntry.iterateCodePoints(&j);
188 if (((sal_Int16)(ch>>8)) <= max_index) {
189 sal_uInt16 address = idx[0][ch>>8];
190 if (address != 0xFFFF) {
191 address = idx[1][address + (ch & 0xFF)];
192 if ( i > 0 && rLocale.Language == "zh" )
193 candidate.append(" ");
194 if (idx[2])
195 candidate.append(
196 reinterpret_cast<sal_Unicode *>(&idx[2][address]));
197 else
198 candidate.append(sal_Unicode(address));
199 } else
200 candidate.append(" ");
203 return candidate.makeStringAndClear();
205 return OUString();
207 } } } }
209 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */