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