| 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 | IndexEntrySupplier_asian::IndexEntrySupplier_asian( |
| 34 | const Reference < XComponentContext >& rxContext ) : IndexEntrySupplier_Common(rxContext) |
| 35 | { |
| 36 | implementationName = "com.sun.star.i18n.IndexEntrySupplier_asian"; |
| 37 | } |
| 38 | |
| 39 | IndexEntrySupplier_asian::~IndexEntrySupplier_asian() |
| 40 | { |
| 41 | } |
| 42 | |
| 43 | OUString SAL_CALL |
| 44 | IndexEntrySupplier_asian::getIndexCharacter( const OUString& rIndexEntry, |
| 45 | const Locale& rLocale, const OUString& rAlgorithm ) |
| 46 | { |
| 47 | sal_uInt32 ch = rIndexEntry.iterateCodePoints(&o3tl::temporary(sal_Int32(0)), 0); |
| 48 | |
| 49 | const sal_uInt16** (*func)(sal_Int16&)=nullptr; |
| 50 | if ( rLocale.Language == "zh" && u"TW HK MO"_ustr.indexOf(rLocale.Country) >= 0 ) { |
| 51 | if ( rAlgorithm == "radical" ) |
| 52 | func = get_indexdata_zh_TW_radical; |
| 53 | else if ( rAlgorithm == "stroke" ) |
| 54 | func = get_indexdata_zh_TW_stroke; |
| 55 | } |
| 56 | if (!func) { |
| 57 | if ( rLocale.Language == "ko" ) { |
| 58 | if ( rAlgorithm == "dict" ) |
| 59 | func = get_indexdata_ko_dict; |
| 60 | } else if ( rLocale.Language == "zh" ) { |
| 61 | if ( rAlgorithm == "pinyin" ) |
| 62 | func = get_indexdata_zh_pinyin; |
| 63 | else if ( rAlgorithm == "radical" ) |
| 64 | func = get_indexdata_zh_radical; |
| 65 | else if ( rAlgorithm == "stroke" ) |
| 66 | func = get_indexdata_zh_stroke; |
| 67 | else if ( rAlgorithm == "zhuyin" ) |
| 68 | func = get_indexdata_zh_zhuyin; |
| 69 | } |
| 70 | } |
| 71 | if (func) { |
| 72 | sal_Int16 max_index; |
| 73 | const sal_uInt16** idx=func(max_index); |
| 74 | if (static_cast<sal_Int16>(ch >> 8) <= max_index) { |
| 75 | sal_uInt16 address=idx[0][ch >> 8]; |
| 76 | if (address != 0xFFFF) { |
| 77 | address=idx[1][address+(ch & 0xFF)]; |
| 78 | return idx[2] |
| 79 | ? OUString( |
| 80 | reinterpret_cast<const sal_Unicode *>(&idx[2][address])) |
| 81 | : OUString(sal_Unicode(address)); |
| 82 | } |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | // using alphanumeric index for non-define string |
| 87 | return OUString(&idxStr[(ch & 0xFFFFFF00) ? 0 : ch], 1); |
| 88 | } |
| 89 | |
| 90 | OUString SAL_CALL |
| 91 | IndexEntrySupplier_asian::getIndexKey( const OUString& rIndexEntry, |
| 92 | const OUString& rPhoneticEntry, const Locale& rLocale) |
| 93 | { |
| 94 | return getIndexCharacter(getEntry(rIndexEntry, rPhoneticEntry, rLocale), rLocale, aAlgorithm); |
| 95 | } |
| 96 | |
| 97 | sal_Int16 SAL_CALL |
| 98 | IndexEntrySupplier_asian::compareIndexEntry( |
| 99 | const OUString& rIndexEntry1, const OUString& rPhoneticEntry1, const Locale& rLocale1, |
| 100 | const OUString& rIndexEntry2, const OUString& rPhoneticEntry2, const Locale& rLocale2 ) |
| 101 | { |
| 102 | sal_Int32 result = collator->compareString(getEntry(rIndexEntry1, rPhoneticEntry1, rLocale1), |
| 103 | getEntry(rIndexEntry2, rPhoneticEntry2, rLocale2)); |
| 104 | |
| 105 | // equivalent of phonetic entries does not mean equivalent of index entries. |
| 106 | // we have to continue comparing index entry here. |
| 107 | if (result == 0 && usePhonetic && !rPhoneticEntry1.isEmpty() && |
| 108 | rLocale1.Language == rLocale2.Language && rLocale1.Country == rLocale2.Country && |
| 109 | rLocale1.Variant == rLocale2.Variant) |
| 110 | result = collator->compareString(rIndexEntry1, rIndexEntry2); |
| 111 | return sal::static_int_cast< sal_Int16 >(result); // result in { -1, 0, 1 } |
| 112 | } |
| 113 | |
| 114 | OUString SAL_CALL |
| 115 | IndexEntrySupplier_asian::getPhoneticCandidate( const OUString& rIndexEntry, |
| 116 | const Locale& rLocale ) |
| 117 | { |
| 118 | sal_uInt16 const **(*func)(sal_Int16&)=nullptr; |
| 119 | if ( rLocale.Language == "zh" ) |
| 120 | func = (u"TW HK MO"_ustr.indexOf(rLocale.Country) >= 0) ? get_zh_zhuyin : get_zh_pinyin; |
| 121 | else if ( rLocale.Language == "ko" ) |
| 122 | func = get_ko_phonetic; |
| 123 | |
| 124 | if (func) { |
| 125 | OUStringBuffer candidate; |
| 126 | sal_Int16 max_index; |
| 127 | sal_uInt16 const ** idx=func(max_index); |
| 128 | for (sal_Int32 i=0,j=0; i < rIndexEntry.getLength(); i=j) { |
| 129 | sal_uInt32 ch = rIndexEntry.iterateCodePoints(&j); |
| 130 | if (static_cast<sal_Int16>(ch>>8) <= max_index) { |
| 131 | sal_uInt16 address = idx[0][ch>>8]; |
| 132 | if (address != 0xFFFF) { |
| 133 | address = idx[1][address + (ch & 0xFF)]; |
| 134 | if ( i > 0 && rLocale.Language == "zh" ) |
| 135 | candidate.append(" "); |
| 136 | if (idx[2]) |
| 137 | candidate.append( |
| 138 | reinterpret_cast<const sal_Unicode *>(&idx[2][address])); |
| 139 | else |
| 140 | candidate.append(sal_Unicode(address)); |
| 141 | } else |
| 142 | candidate.append(" "); |
| 143 | } |
| 144 | } |
| 145 | return candidate.makeStringAndClear(); |
| 146 | } |
| 147 | return OUString(); |
| 148 | } |
| 149 | |
| 150 | } |
| 151 | |
| 152 | |
| 153 | /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |