xref: /aosp_15_r20/frameworks/minikin/libs/minikin/HyphenatorMap.cpp (revision 834a2baab5fdfc28e9a428ee87c7ea8f6a06a53d)
1*834a2baaSAndroid Build Coastguard Worker /*
2*834a2baaSAndroid Build Coastguard Worker  * Copyright (C) 2017 The Android Open Source Project
3*834a2baaSAndroid Build Coastguard Worker  *
4*834a2baaSAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*834a2baaSAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*834a2baaSAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*834a2baaSAndroid Build Coastguard Worker  *
8*834a2baaSAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*834a2baaSAndroid Build Coastguard Worker  *
10*834a2baaSAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*834a2baaSAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*834a2baaSAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*834a2baaSAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*834a2baaSAndroid Build Coastguard Worker  * limitations under the License.
15*834a2baaSAndroid Build Coastguard Worker  */
16*834a2baaSAndroid Build Coastguard Worker 
17*834a2baaSAndroid Build Coastguard Worker #include "HyphenatorMap.h"
18*834a2baaSAndroid Build Coastguard Worker 
19*834a2baaSAndroid Build Coastguard Worker #include "LocaleListCache.h"
20*834a2baaSAndroid Build Coastguard Worker #include "MinikinInternal.h"
21*834a2baaSAndroid Build Coastguard Worker 
22*834a2baaSAndroid Build Coastguard Worker namespace minikin {
23*834a2baaSAndroid Build Coastguard Worker 
24*834a2baaSAndroid Build Coastguard Worker namespace {
25*834a2baaSAndroid Build Coastguard Worker constexpr SubtagBits LANGUAGE = SubtagBits::LANGUAGE;
26*834a2baaSAndroid Build Coastguard Worker constexpr SubtagBits SCRIPT = SubtagBits::SCRIPT;
27*834a2baaSAndroid Build Coastguard Worker constexpr SubtagBits REGION = SubtagBits::REGION;
28*834a2baaSAndroid Build Coastguard Worker constexpr SubtagBits VARIANT = SubtagBits::VARIANT;
29*834a2baaSAndroid Build Coastguard Worker 
30*834a2baaSAndroid Build Coastguard Worker constexpr int DEFAULT_MIN_PREFIX = 2;
31*834a2baaSAndroid Build Coastguard Worker constexpr int DEFAULT_MAX_PREFIX = 2;
32*834a2baaSAndroid Build Coastguard Worker }  // namespace
33*834a2baaSAndroid Build Coastguard Worker 
34*834a2baaSAndroid Build Coastguard Worker // Following two function's implementations are here since Hyphenator.cpp can't include
35*834a2baaSAndroid Build Coastguard Worker // HyphenatorMap.h due to harfbuzz dependency on the host binary.
addHyphenator(const std::string & localeStr,const Hyphenator * hyphenator)36*834a2baaSAndroid Build Coastguard Worker void addHyphenator(const std::string& localeStr, const Hyphenator* hyphenator) {
37*834a2baaSAndroid Build Coastguard Worker     HyphenatorMap::add(localeStr, hyphenator);
38*834a2baaSAndroid Build Coastguard Worker }
39*834a2baaSAndroid Build Coastguard Worker 
addHyphenatorAlias(const std::string & fromLocaleStr,const std::string & toLocaleStr)40*834a2baaSAndroid Build Coastguard Worker void addHyphenatorAlias(const std::string& fromLocaleStr, const std::string& toLocaleStr) {
41*834a2baaSAndroid Build Coastguard Worker     HyphenatorMap::addAlias(fromLocaleStr, toLocaleStr);
42*834a2baaSAndroid Build Coastguard Worker }
43*834a2baaSAndroid Build Coastguard Worker 
HyphenatorMap()44*834a2baaSAndroid Build Coastguard Worker HyphenatorMap::HyphenatorMap()
45*834a2baaSAndroid Build Coastguard Worker         : mSoftHyphenOnlyHyphenator(
46*834a2baaSAndroid Build Coastguard Worker                   Hyphenator::loadBinary(nullptr, 0, DEFAULT_MIN_PREFIX, DEFAULT_MAX_PREFIX, "")) {}
47*834a2baaSAndroid Build Coastguard Worker 
addInternal(const std::string & localeStr,const Hyphenator * hyphenator)48*834a2baaSAndroid Build Coastguard Worker void HyphenatorMap::addInternal(const std::string& localeStr, const Hyphenator* hyphenator) {
49*834a2baaSAndroid Build Coastguard Worker     const Locale locale(localeStr);
50*834a2baaSAndroid Build Coastguard Worker     std::lock_guard<std::mutex> lock(mMutex);
51*834a2baaSAndroid Build Coastguard Worker     // Overwrite even if there is already a fallback entry.
52*834a2baaSAndroid Build Coastguard Worker     mMap[locale.getIdentifier()] = hyphenator;
53*834a2baaSAndroid Build Coastguard Worker }
54*834a2baaSAndroid Build Coastguard Worker 
clearInternal()55*834a2baaSAndroid Build Coastguard Worker void HyphenatorMap::clearInternal() {
56*834a2baaSAndroid Build Coastguard Worker     std::lock_guard<std::mutex> lock(mMutex);
57*834a2baaSAndroid Build Coastguard Worker     mMap.clear();
58*834a2baaSAndroid Build Coastguard Worker }
addAliasInternal(const std::string & fromLocaleStr,const std::string & toLocaleStr)59*834a2baaSAndroid Build Coastguard Worker void HyphenatorMap::addAliasInternal(const std::string& fromLocaleStr,
60*834a2baaSAndroid Build Coastguard Worker                                      const std::string& toLocaleStr) {
61*834a2baaSAndroid Build Coastguard Worker     const Locale fromLocale(fromLocaleStr);
62*834a2baaSAndroid Build Coastguard Worker     const Locale toLocale(toLocaleStr);
63*834a2baaSAndroid Build Coastguard Worker     std::lock_guard<std::mutex> lock(mMutex);
64*834a2baaSAndroid Build Coastguard Worker     auto it = mMap.find(toLocale.getIdentifier());
65*834a2baaSAndroid Build Coastguard Worker     if (it == mMap.end()) {
66*834a2baaSAndroid Build Coastguard Worker         ALOGE("Target Hyphenator not found.");
67*834a2baaSAndroid Build Coastguard Worker         return;
68*834a2baaSAndroid Build Coastguard Worker     }
69*834a2baaSAndroid Build Coastguard Worker     // Overwrite even if there is already a fallback entry.
70*834a2baaSAndroid Build Coastguard Worker     mMap[fromLocale.getIdentifier()] = it->second;
71*834a2baaSAndroid Build Coastguard Worker }
72*834a2baaSAndroid Build Coastguard Worker 
lookupInternal(const Locale & locale)73*834a2baaSAndroid Build Coastguard Worker const Hyphenator* HyphenatorMap::lookupInternal(const Locale& locale) {
74*834a2baaSAndroid Build Coastguard Worker     const uint64_t id = locale.getIdentifier();
75*834a2baaSAndroid Build Coastguard Worker     std::lock_guard<std::mutex> lock(mMutex);
76*834a2baaSAndroid Build Coastguard Worker     const Hyphenator* result = lookupByIdentifier(id);
77*834a2baaSAndroid Build Coastguard Worker     if (result != nullptr) {
78*834a2baaSAndroid Build Coastguard Worker         return result;  // Found with exact match.
79*834a2baaSAndroid Build Coastguard Worker     }
80*834a2baaSAndroid Build Coastguard Worker 
81*834a2baaSAndroid Build Coastguard Worker     // First, try with dropping emoji extensions.
82*834a2baaSAndroid Build Coastguard Worker     result = lookupBySubtag(locale, LANGUAGE | REGION | SCRIPT | VARIANT);
83*834a2baaSAndroid Build Coastguard Worker     if (result != nullptr) {
84*834a2baaSAndroid Build Coastguard Worker         goto insert_result_and_return;
85*834a2baaSAndroid Build Coastguard Worker     }
86*834a2baaSAndroid Build Coastguard Worker     // If not found, try with dropping script.
87*834a2baaSAndroid Build Coastguard Worker     result = lookupBySubtag(locale, LANGUAGE | REGION | VARIANT);
88*834a2baaSAndroid Build Coastguard Worker     if (result != nullptr) {
89*834a2baaSAndroid Build Coastguard Worker         goto insert_result_and_return;
90*834a2baaSAndroid Build Coastguard Worker     }
91*834a2baaSAndroid Build Coastguard Worker     // If not found, try with dropping script and region code.
92*834a2baaSAndroid Build Coastguard Worker     result = lookupBySubtag(locale, LANGUAGE | VARIANT);
93*834a2baaSAndroid Build Coastguard Worker     if (result != nullptr) {
94*834a2baaSAndroid Build Coastguard Worker         goto insert_result_and_return;
95*834a2baaSAndroid Build Coastguard Worker     }
96*834a2baaSAndroid Build Coastguard Worker     // If not found, try only with language code.
97*834a2baaSAndroid Build Coastguard Worker     result = lookupBySubtag(locale, LANGUAGE);
98*834a2baaSAndroid Build Coastguard Worker     if (result != nullptr) {
99*834a2baaSAndroid Build Coastguard Worker         goto insert_result_and_return;
100*834a2baaSAndroid Build Coastguard Worker     }
101*834a2baaSAndroid Build Coastguard Worker     // Still not found, try only with script.
102*834a2baaSAndroid Build Coastguard Worker     result = lookupBySubtag(locale, SCRIPT);
103*834a2baaSAndroid Build Coastguard Worker     if (result != nullptr) {
104*834a2baaSAndroid Build Coastguard Worker         goto insert_result_and_return;
105*834a2baaSAndroid Build Coastguard Worker     }
106*834a2baaSAndroid Build Coastguard Worker 
107*834a2baaSAndroid Build Coastguard Worker     // If not found, use soft hyphen only hyphenator.
108*834a2baaSAndroid Build Coastguard Worker     result = mSoftHyphenOnlyHyphenator;
109*834a2baaSAndroid Build Coastguard Worker 
110*834a2baaSAndroid Build Coastguard Worker insert_result_and_return:
111*834a2baaSAndroid Build Coastguard Worker     mMap.insert(std::make_pair(id, result));
112*834a2baaSAndroid Build Coastguard Worker     return result;
113*834a2baaSAndroid Build Coastguard Worker }
114*834a2baaSAndroid Build Coastguard Worker 
lookupByIdentifier(uint64_t id) const115*834a2baaSAndroid Build Coastguard Worker const Hyphenator* HyphenatorMap::lookupByIdentifier(uint64_t id) const {
116*834a2baaSAndroid Build Coastguard Worker     auto it = mMap.find(id);
117*834a2baaSAndroid Build Coastguard Worker     return it == mMap.end() ? nullptr : it->second;
118*834a2baaSAndroid Build Coastguard Worker }
119*834a2baaSAndroid Build Coastguard Worker 
lookupBySubtag(const Locale & locale,SubtagBits bits) const120*834a2baaSAndroid Build Coastguard Worker const Hyphenator* HyphenatorMap::lookupBySubtag(const Locale& locale, SubtagBits bits) const {
121*834a2baaSAndroid Build Coastguard Worker     const Locale partialLocale = locale.getPartialLocale(bits);
122*834a2baaSAndroid Build Coastguard Worker     if (!partialLocale.isSupported() || partialLocale == locale) {
123*834a2baaSAndroid Build Coastguard Worker         return nullptr;  // Skip the partial locale result in the same locale or not supported.
124*834a2baaSAndroid Build Coastguard Worker     }
125*834a2baaSAndroid Build Coastguard Worker     return lookupByIdentifier(partialLocale.getIdentifier());
126*834a2baaSAndroid Build Coastguard Worker }
127*834a2baaSAndroid Build Coastguard Worker 
128*834a2baaSAndroid Build Coastguard Worker }  // namespace minikin
129