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#ifndef MINIKIN_FONT_LANGUAGE_H
18#define MINIKIN_FONT_LANGUAGE_H
19
20#include <string>
21#include <vector>
22
23#include <hb.h>
24
25namespace android {
26
27// Due to the limits in font fallback score calculation, we can't use anything more than 17
28// languages.
29const size_t FONT_LANGUAGES_LIMIT = 17;
30class FontLanguages;
31
32// FontLanguage is a compact representation of a BCP 47 language tag. It
33// does not capture all possible information, only what directly affects
34// font rendering.
35struct FontLanguage {
36public:
37    // Default constructor creates the unsupported language.
38    FontLanguage() : mScript(0ul), mLanguage(0ul), mSubScriptBits(0ul) {}
39
40    // Parse from string
41    FontLanguage(const char* buf, size_t length);
42
43    bool operator==(const FontLanguage other) const {
44        return !isUnsupported() && isEqualScript(other) && mLanguage == other.mLanguage;
45    }
46
47    bool operator!=(const FontLanguage other) const {
48        return !(*this == other);
49    }
50
51    bool isUnsupported() const { return mLanguage == 0ul; }
52    bool hasEmojiFlag() const { return mSubScriptBits & kEmojiFlag; }
53
54    bool isEqualScript(const FontLanguage& other) const;
55
56    // Returns true if this script supports the given script. For example, ja-Jpan supports Hira,
57    // ja-Hira doesn't support Jpan.
58    bool supportsHbScript(hb_script_t script) const;
59
60    std::string getString() const;
61
62    // Calculates a matching score. This score represents how well the input languages cover this
63    // language. The maximum score in the language list is returned.
64    // 0 = no match, 1 = script match, 2 = script and primary language match.
65    int calcScoreFor(const FontLanguages& supported) const;
66
67    uint64_t getIdentifier() const { return (uint64_t)mScript << 32 | (uint64_t)mLanguage; }
68
69private:
70    friend class FontLanguages;  // for FontLanguages constructor
71
72    // ISO 15924 compliant script code. The 4 chars script code are packed into a 32 bit integer.
73    uint32_t mScript;
74
75    // ISO 639-1 or ISO 639-2 compliant language code.
76    // The two or three letter language code is packed into 32 bit integer.
77    // mLanguage = 0 means the FontLanguage is unsupported.
78    uint32_t mLanguage;
79
80    // For faster comparing, use 8 bits for specific scripts.
81    static const uint8_t kBopomofoFlag = 1u;
82    static const uint8_t kEmojiFlag = 1u << 1;
83    static const uint8_t kHanFlag = 1u << 2;
84    static const uint8_t kHangulFlag = 1u << 3;
85    static const uint8_t kHiraganaFlag = 1u << 4;
86    static const uint8_t kKatakanaFlag = 1u << 5;
87    static const uint8_t kSimplifiedChineseFlag = 1u << 6;
88    static const uint8_t kTraditionalChineseFlag = 1u << 7;
89    uint8_t mSubScriptBits;
90
91    static uint8_t scriptToSubScriptBits(uint32_t script);
92
93    // Returns true if the provide subscript bits has the requested subscript bits.
94    // Note that this function returns false if the requested subscript bits are empty.
95    static bool supportsScript(uint8_t providedBits, uint8_t requestedBits);
96};
97
98// An immutable list of languages.
99class FontLanguages {
100public:
101    FontLanguages(std::vector<FontLanguage>&& languages);
102    FontLanguages() : mUnionOfSubScriptBits(0), mIsAllTheSameLanguage(false) {}
103    FontLanguages(FontLanguages&&) = default;
104
105    size_t size() const { return mLanguages.size(); }
106    bool empty() const { return mLanguages.empty(); }
107    const FontLanguage& operator[] (size_t n) const { return mLanguages[n]; }
108
109private:
110    friend struct FontLanguage;  // for calcScoreFor
111
112    std::vector<FontLanguage> mLanguages;
113    uint8_t mUnionOfSubScriptBits;
114    bool mIsAllTheSameLanguage;
115
116    uint8_t getUnionOfSubScriptBits() const { return mUnionOfSubScriptBits; }
117    bool isAllTheSameLanguage() const { return mIsAllTheSameLanguage; }
118
119    // Do not copy and assign.
120    FontLanguages(const FontLanguages&) = delete;
121    void operator=(const FontLanguages&) = delete;
122};
123
124}  // namespace android
125
126#endif  // MINIKIN_FONT_LANGUAGE_H
127