1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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
;
31 #ifndef DISABLE_DYNLOADING
33 extern "C" { static void thisModule() {} }
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
43 OUString
lib(SAL_DLLPREFIX
"index_data" SAL_DLLEXTENSION
);
45 OUString
lib("index_data" SAL_DLLEXTENSION
);
47 hModule
= osl_loadModuleRelative(
48 &thisModule
, lib
.pData
, SAL_LOADMODULE_DEFAULT
);
52 IndexEntrySupplier_asian::~IndexEntrySupplier_asian()
54 #ifndef DISABLE_DYNLOADING
55 if (hModule
) osl_unloadModule(hModule
);
59 #ifdef DISABLE_DYNLOADING
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
*);
80 IndexEntrySupplier_asian::getIndexCharacter( const OUString
& rIndexEntry
,
81 const Locale
& rLocale
, const OUString
& rAlgorithm
)
84 sal_uInt32 ch
= rIndexEntry
.iterateCodePoints(&i
, 0);
86 sal_uInt16
** (*func
)(sal_Int16
*)=nullptr;
87 #ifndef DISABLE_DYNLOADING
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
));
93 func
=reinterpret_cast<sal_uInt16
** (*)(sal_Int16
*)>(osl_getFunctionSymbol(hModule
, OUString(get
+rLocale
.Language
+"_"+rAlgorithm
).pData
));
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
;
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
;
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)];
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);
138 IndexEntrySupplier_asian::getIndexKey( const OUString
& rIndexEntry
,
139 const OUString
& rPhoneticEntry
, const Locale
& rLocale
)
141 return getIndexCharacter(getEntry(rIndexEntry
, rPhoneticEntry
, rLocale
), rLocale
, aAlgorithm
);
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 }
162 IndexEntrySupplier_asian::getPhoneticCandidate( const OUString
& rIndexEntry
,
163 const Locale
& rLocale
)
165 sal_uInt16
**(*func
)(sal_Int16
*)=nullptr;
166 #ifndef DISABLE_DYNLOADING
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";
174 func
=reinterpret_cast<sal_uInt16
**(*)(sal_Int16
*)>(osl_getFunctionSymbol(hModule
, OUString::createFromAscii(func_name
).pData
));
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
;
184 OUStringBuffer candidate
;
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(" ");
197 reinterpret_cast<sal_Unicode
*>(&idx
[2][address
]));
199 candidate
.append(sal_Unicode(address
));
201 candidate
.append(" ");
204 return candidate
.makeStringAndClear();
212 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */