FontLanguage.cpp revision 198b46f1fea3f47ef8eb6317799c0d77aaec52f6
1/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "Minikin"
18
19#include "FontLanguage.h"
20
21#include <hb.h>
22#include <unicode/uloc.h>
23
24namespace android {
25
26#define SCRIPT_TAG(c1, c2, c3, c4) \
27        (((uint32_t)(c1)) << 24 | ((uint32_t)(c2)) << 16 | ((uint32_t)(c3)) <<  8 | \
28         ((uint32_t)(c4)))
29
30// Parse BCP 47 language identifier into internal structure
31FontLanguage::FontLanguage(const char* buf, size_t length) : FontLanguage() {
32    size_t i;
33    for (i = 0; i < length; i++) {
34        char c = buf[i];
35        if (c == '-' || c == '_') break;
36    }
37    if (i == 2 || i == 3) {  // only accept two or three letter language code.
38        mLanguage = buf[0] | (buf[1] << 8) | ((i == 3) ? (buf[2] << 16) : 0);
39    } else {
40        // We don't understand anything other than two-letter or three-letter
41        // language codes, so we skip parsing the rest of the string.
42        mLanguage = 0ul;
43        return;
44    }
45
46    size_t next;
47    for (i++; i < length; i = next + 1) {
48        for (next = i; next < length; next++) {
49            char c = buf[next];
50            if (c == '-' || c == '_') break;
51        }
52        if (next - i == 4 && 'A' <= buf[i] && buf[i] <= 'Z') {
53            mScript = SCRIPT_TAG(buf[i], buf[i + 1], buf[i + 2], buf[i + 3]);
54        }
55    }
56
57    mSubScriptBits = scriptToSubScriptBits(mScript);
58}
59
60//static
61uint8_t FontLanguage::scriptToSubScriptBits(uint32_t script) {
62    uint8_t subScriptBits = 0u;
63    switch (script) {
64        case SCRIPT_TAG('H', 'a', 'n', 'g'):
65            subScriptBits = kHangulFlag;
66            break;
67        case SCRIPT_TAG('H', 'a', 'n', 'i'):
68            subScriptBits = kHanFlag;
69            break;
70        case SCRIPT_TAG('H', 'a', 'n', 's'):
71            subScriptBits = kHanFlag | kSimplifiedChineseFlag;
72            break;
73        case SCRIPT_TAG('H', 'a', 'n', 't'):
74            subScriptBits = kHanFlag | kTraditionalChineseFlag;
75            break;
76        case SCRIPT_TAG('H', 'i', 'r', 'a'):
77            subScriptBits = kHiraganaFlag;
78            break;
79        case SCRIPT_TAG('H', 'r', 'k', 't'):
80            subScriptBits = kKatakanaFlag | kHiraganaFlag;
81            break;
82        case SCRIPT_TAG('J', 'p', 'a', 'n'):
83            subScriptBits = kHanFlag | kKatakanaFlag | kHiraganaFlag;
84            break;
85        case SCRIPT_TAG('K', 'a', 'n', 'a'):
86            subScriptBits = kKatakanaFlag;
87            break;
88        case SCRIPT_TAG('K', 'o', 'r', 'e'):
89            subScriptBits = kHanFlag | kHangulFlag;
90            break;
91        case SCRIPT_TAG('Q', 'a', 'a', 'e'):
92            subScriptBits = kEmojiFlag;
93            break;
94    }
95    return subScriptBits;
96}
97
98std::string FontLanguage::getString() const {
99    if (mLanguage == 0ul) {
100        return "und";
101    }
102    char buf[16];
103    size_t i = 0;
104    buf[i++] = mLanguage & 0xFF ;
105    buf[i++] = (mLanguage >> 8) & 0xFF;
106    char third_letter = (mLanguage >> 16) & 0xFF;
107    if (third_letter != 0) buf[i++] = third_letter;
108    if (mScript != 0) {
109      buf[i++] = '-';
110      buf[i++] = (mScript >> 24) & 0xFFu;
111      buf[i++] = (mScript >> 16) & 0xFFu;
112      buf[i++] = (mScript >> 8) & 0xFFu;
113      buf[i++] = mScript & 0xFFu;
114    }
115    return std::string(buf, i);
116}
117
118bool FontLanguage::isEqualScript(const FontLanguage other) const {
119    return other.mScript == mScript;
120}
121
122bool FontLanguage::supportsHbScript(hb_script_t script) const {
123    static_assert(SCRIPT_TAG('J', 'p', 'a', 'n') == HB_TAG('J', 'p', 'a', 'n'),
124                  "The Minikin script and HarfBuzz hb_script_t have different encodings.");
125    if (script == mScript) return true;
126    uint8_t requestedBits = scriptToSubScriptBits(script);
127    return requestedBits != 0 && (mSubScriptBits & requestedBits) == requestedBits;
128}
129
130int FontLanguage::match(const FontLanguage other) const {
131    // TODO: Use script for matching.
132    return *this == other;
133}
134
135#undef SCRIPT_TAG
136}  // namespace android
137