bump product version to 6.3.0.0.beta1
[LibreOffice.git] / i18npool / source / indexentry / indexentrysupplier_asian.cxx
blob975c9221872d9c1f962ad760d8fe2b9771e21e19
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 <collatorImpl.hxx>
23 #include <indexentrysupplier_asian.hxx>
24 #include "data/indexdata_alphanumeric.h"
26 using namespace ::com::sun::star::uno;
27 using namespace ::com::sun::star::lang;
29 namespace i18npool {
31 #ifndef DISABLE_DYNLOADING
33 extern "C" { static void thisModule() {} }
35 #endif
37 IndexEntrySupplier_asian::IndexEntrySupplier_asian(
38 const Reference < XComponentContext >& rxContext ) : IndexEntrySupplier_Common(rxContext)
40 implementationName = "com.sun.star.i18n.IndexEntrySupplier_asian";
41 #ifndef DISABLE_DYNLOADING
42 #ifdef SAL_DLLPREFIX
43 OUString lib(SAL_DLLPREFIX"index_data" SAL_DLLEXTENSION);
44 #else
45 OUString lib("index_data" SAL_DLLEXTENSION);
46 #endif
47 hModule = osl_loadModuleRelative(
48 &thisModule, lib.pData, SAL_LOADMODULE_DEFAULT );
49 #endif
52 IndexEntrySupplier_asian::~IndexEntrySupplier_asian()
54 #ifndef DISABLE_DYNLOADING
55 if (hModule) osl_unloadModule(hModule);
56 #endif
59 #ifdef DISABLE_DYNLOADING
61 extern "C" {
63 sal_uInt16** get_indexdata_ko_dict(sal_Int16*);
64 sal_uInt16** get_indexdata_zh_TW_radical(sal_Int16*);
65 sal_uInt16** get_indexdata_zh_TW_stroke(sal_Int16*);
66 sal_uInt16** get_indexdata_zh_pinyin(sal_Int16*);
67 sal_uInt16** get_indexdata_zh_radical(sal_Int16*);
68 sal_uInt16** get_indexdata_zh_stroke(sal_Int16*);
69 sal_uInt16** get_indexdata_zh_zhuyin(sal_Int16*);
71 sal_uInt16** get_ko_phonetic(sal_Int16*);
72 sal_uInt16** get_zh_pinyin(sal_Int16*);
73 sal_uInt16** get_zh_zhuyin(sal_Int16*);
77 #endif
79 OUString SAL_CALL
80 IndexEntrySupplier_asian::getIndexCharacter( const OUString& rIndexEntry,
81 const Locale& rLocale, const OUString& rAlgorithm )
83 sal_Int32 i=0;
84 sal_uInt32 ch = rIndexEntry.iterateCodePoints(&i, 0);
86 sal_uInt16** (*func)(sal_Int16*)=nullptr;
87 #ifndef DISABLE_DYNLOADING
88 if (hModule) {
89 OUString get("get_indexdata_");
90 if ( rLocale.Language == "zh" && OUString( "TW HK MO" ).indexOf(rLocale.Country) >= 0 )
91 func=reinterpret_cast<sal_uInt16** (*)(sal_Int16*)>(osl_getFunctionSymbol(hModule, OUString(get+rLocale.Language+"_TW_"+rAlgorithm).pData));
92 if (!func)
93 func=reinterpret_cast<sal_uInt16** (*)(sal_Int16*)>(osl_getFunctionSymbol(hModule, OUString(get+rLocale.Language+"_"+rAlgorithm).pData));
95 #else
96 if ( rLocale.Language == "zh" && OUString( "TW HK MO" ).indexOf(rLocale.Country) >= 0 ) {
97 if ( rAlgorithm == "radical" )
98 func = get_indexdata_zh_TW_radical;
99 else if ( rAlgorithm == "stroke" )
100 func = get_indexdata_zh_TW_stroke;
102 if (!func) {
103 if ( rLocale.Language == "ko" ) {
104 if ( rAlgorithm == "dict" )
105 func = get_indexdata_ko_dict;
106 } else if ( rLocale.Language == "zh" ) {
107 if ( rAlgorithm == "pinyin" )
108 func = get_indexdata_zh_pinyin;
109 else if ( rAlgorithm == "radical" )
110 func = get_indexdata_zh_radical;
111 else if ( rAlgorithm == "stroke" )
112 func = get_indexdata_zh_stroke;
113 else if ( rAlgorithm == "zhuyin" )
114 func = get_indexdata_zh_zhuyin;
117 #endif
118 if (func) {
119 sal_Int16 max_index;
120 sal_uInt16** idx=func(&max_index);
121 if (static_cast<sal_Int16>(ch >> 8) <= max_index) {
122 sal_uInt16 address=idx[0][ch >> 8];
123 if (address != 0xFFFF) {
124 address=idx[1][address+(ch & 0xFF)];
125 return idx[2]
126 ? OUString(
127 reinterpret_cast<sal_Unicode *>(&idx[2][address]))
128 : OUString(sal_Unicode(address));
133 // using alphanumeric index for non-define string
134 return OUString(&idxStr[(ch & 0xFFFFFF00) ? 0 : ch], 1);
137 OUString SAL_CALL
138 IndexEntrySupplier_asian::getIndexKey( const OUString& rIndexEntry,
139 const OUString& rPhoneticEntry, const Locale& rLocale)
141 return getIndexCharacter(getEntry(rIndexEntry, rPhoneticEntry, rLocale), rLocale, aAlgorithm);
144 sal_Int16 SAL_CALL
145 IndexEntrySupplier_asian::compareIndexEntry(
146 const OUString& rIndexEntry1, const OUString& rPhoneticEntry1, const Locale& rLocale1,
147 const OUString& rIndexEntry2, const OUString& rPhoneticEntry2, const Locale& rLocale2 )
149 sal_Int32 result = collator->compareString(getEntry(rIndexEntry1, rPhoneticEntry1, rLocale1),
150 getEntry(rIndexEntry2, rPhoneticEntry2, rLocale2));
152 // equivalent of phonetic entries does not mean equivalent of index entries.
153 // we have to continue comparing index entry here.
154 if (result == 0 && usePhonetic && !rPhoneticEntry1.isEmpty() &&
155 rLocale1.Language == rLocale2.Language && rLocale1.Country == rLocale2.Country &&
156 rLocale1.Variant == rLocale2.Variant)
157 result = collator->compareString(rIndexEntry1, rIndexEntry2);
158 return sal::static_int_cast< sal_Int16 >(result); // result in { -1, 0, 1 }
161 OUString SAL_CALL
162 IndexEntrySupplier_asian::getPhoneticCandidate( const OUString& rIndexEntry,
163 const Locale& rLocale )
165 sal_uInt16 **(*func)(sal_Int16*)=nullptr;
166 #ifndef DISABLE_DYNLOADING
167 if (hModule) {
168 const sal_Char *func_name=nullptr;
169 if ( rLocale.Language == "zh" )
170 func_name=(OUString("TW HK MO").indexOf(rLocale.Country) >= 0) ? "get_zh_zhuyin" : "get_zh_pinyin";
171 else if ( rLocale.Language == "ko" )
172 func_name="get_ko_phonetic";
173 if (func_name)
174 func=reinterpret_cast<sal_uInt16 **(*)(sal_Int16*)>(osl_getFunctionSymbol(hModule, OUString::createFromAscii(func_name).pData));
176 #else
177 if ( rLocale.Language == "zh" )
178 func = (OUString("TW HK MO").indexOf(rLocale.Country) >= 0) ? get_zh_zhuyin : get_zh_pinyin;
179 else if ( rLocale.Language == "ko" )
180 func = get_ko_phonetic;
182 #endif
183 if (func) {
184 OUStringBuffer candidate;
185 sal_Int16 max_index;
186 sal_uInt16** idx=func(&max_index);
187 for (sal_Int32 i=0,j=0; i < rIndexEntry.getLength(); i=j) {
188 sal_uInt32 ch = rIndexEntry.iterateCodePoints(&j);
189 if (static_cast<sal_Int16>(ch>>8) <= max_index) {
190 sal_uInt16 address = idx[0][ch>>8];
191 if (address != 0xFFFF) {
192 address = idx[1][address + (ch & 0xFF)];
193 if ( i > 0 && rLocale.Language == "zh" )
194 candidate.append(" ");
195 if (idx[2])
196 candidate.append(
197 reinterpret_cast<sal_Unicode *>(&idx[2][address]));
198 else
199 candidate.append(sal_Unicode(address));
200 } else
201 candidate.append(" ");
204 return candidate.makeStringAndClear();
206 return OUString();
212 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */