summaryrefslogtreecommitdiff
path: root/libs/minikin/LocaleListCache.cpp
blob: 625b4d285879554897c1900a5ce95df5b8e70ef5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/*
 * Copyright (C) 2015 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#define LOG_TAG "Minikin"

#include "LocaleListCache.h"

#include <unordered_set>

#include <log/log.h>
#include <unicode/uloc.h>

#include "Locale.h"
#include "MinikinInternal.h"

namespace minikin {

const uint32_t LocaleListCache::kEmptyListId;

// Returns the text length of output.
static size_t toLanguageTag(char* output, size_t outSize, const StringPiece& locale) {
    output[0] = '\0';
    if (locale.empty()) {
        return 0;
    }

    std::string localeString = locale.toString();  // ICU only understands C-style string.

    size_t outLength = 0;
    UErrorCode uErr = U_ZERO_ERROR;
    outLength = uloc_canonicalize(localeString.c_str(), output, outSize, &uErr);
    if (U_FAILURE(uErr)) {
        // unable to build a proper locale identifier
        ALOGD("uloc_canonicalize(\"%s\") failed: %s", localeString.c_str(), u_errorName(uErr));
        output[0] = '\0';
        return 0;
    }

    // Preserve "und" and "und-****" since uloc_addLikelySubtags changes "und" to "en-Latn-US".
    if (strncmp(output, "und", 3) == 0 &&
        (outLength == 3 || (outLength == 8 && output[3] == '_'))) {
        output[3] = '-';  // to be language tag.
        return outLength;
    }

    char likelyChars[ULOC_FULLNAME_CAPACITY];
    uErr = U_ZERO_ERROR;
    uloc_addLikelySubtags(output, likelyChars, ULOC_FULLNAME_CAPACITY, &uErr);
    if (U_FAILURE(uErr)) {
        // unable to build a proper locale identifier
        ALOGD("uloc_addLikelySubtags(\"%s\") failed: %s", output, u_errorName(uErr));
        output[0] = '\0';
        return 0;
    }

    uErr = U_ZERO_ERROR;
    outLength = uloc_toLanguageTag(likelyChars, output, outSize, FALSE, &uErr);
    if (U_FAILURE(uErr)) {
        // unable to build a proper locale identifier
        ALOGD("uloc_toLanguageTag(\"%s\") failed: %s", likelyChars, u_errorName(uErr));
        output[0] = '\0';
        return 0;
    }
    return outLength;
}

static std::vector<Locale> parseLocaleList(const std::string& input) {
    std::vector<Locale> result;
    char langTag[ULOC_FULLNAME_CAPACITY];
    std::unordered_set<uint64_t> seen;

    SplitIterator it(input, ',');
    while (it.hasNext()) {
        StringPiece localeStr = it.next();
        size_t length = toLanguageTag(langTag, ULOC_FULLNAME_CAPACITY, localeStr);
        Locale locale(StringPiece(langTag, length));
        if (locale.isUnsupported()) {
            continue;
        }
        const bool isNewLocale = seen.insert(locale.getIdentifier()).second;
        if (!isNewLocale) {
            continue;
        }

        result.push_back(locale);
        if (result.size() >= FONT_LOCALE_LIMIT) {
            break;
        }
    }
    return result;
}

// static
uint32_t LocaleListCache::getId(const std::string& locales) {
    LocaleListCache* inst = LocaleListCache::getInstance();
    std::unordered_map<std::string, uint32_t>::const_iterator it =
            inst->mLocaleListLookupTable.find(locales);
    if (it != inst->mLocaleListLookupTable.end()) {
        return it->second;
    }

    // Given locale list is not in cache. Insert it and return newly assigned ID.
    const uint32_t nextId = inst->mLocaleLists.size();
    LocaleList fontLocales(parseLocaleList(locales));
    if (fontLocales.empty()) {
        return kEmptyListId;
    }
    inst->mLocaleLists.push_back(std::move(fontLocales));
    inst->mLocaleListLookupTable.insert(std::make_pair(locales, nextId));
    return nextId;
}

// static
const LocaleList& LocaleListCache::getById(uint32_t id) {
    LocaleListCache* inst = LocaleListCache::getInstance();
    MINIKIN_ASSERT(id < inst->mLocaleLists.size(), "Lookup by unknown locale list ID.");
    return inst->mLocaleLists[id];
}

// static
LocaleListCache* LocaleListCache::getInstance() {
    assertMinikinLocked();
    static LocaleListCache* instance = nullptr;
    if (instance == nullptr) {
        instance = new LocaleListCache();

        // Insert an empty locale list for mapping default locale list to kEmptyListId.
        // The default locale list has only one Locale and it is the unsupported locale.
        instance->mLocaleLists.push_back(LocaleList());
        instance->mLocaleListLookupTable.insert(std::make_pair("", kEmptyListId));
    }
    return instance;
}

}  // namespace minikin