• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_LOCALE_LIST_H
18 #define MINIKIN_LOCALE_LIST_H
19 
20 #include <string>
21 #include <vector>
22 
23 #include <hb.h>
24 
25 #include "StringPiece.h"
26 
27 namespace minikin {
28 
29 // Due to the limits in font fallback score calculation, we can't use anything more than 12 locales.
30 const size_t FONT_LOCALE_LIMIT = 12;
31 
32 // The language or region code is encoded to 15 bits.
33 constexpr uint16_t NO_LANGUAGE = 0x7fff;
34 constexpr uint16_t NO_REGION = 0x7fff;
35 // The script code is encoded to 20 bits.
36 constexpr uint32_t NO_SCRIPT = 0xfffff;
37 
38 class LocaleList;
39 
40 // Enum for making sub-locale from FontLangauge.
41 enum class SubtagBits : uint8_t {
42     EMPTY = 0b00000000,
43     LANGUAGE = 0b00000001,
44     SCRIPT = 0b00000010,
45     REGION = 0b00000100,
46     VARIANT = 0b00001000,
47     EMOJI = 0b00010000,
48     ALL = 0b00011111,
49 };
50 
51 inline constexpr SubtagBits operator&(SubtagBits l, SubtagBits r) {
52     return static_cast<SubtagBits>(static_cast<uint8_t>(l) & static_cast<uint8_t>(r));
53 }
54 inline constexpr SubtagBits operator|(SubtagBits l, SubtagBits r) {
55     return static_cast<SubtagBits>(static_cast<uint8_t>(l) | static_cast<uint8_t>(r));
56 }
57 
58 // Enum for emoji style.
59 enum class EmojiStyle : uint8_t {
60     EMPTY = 0,    // No emoji style is specified.
61     DEFAULT = 1,  // Default emoji style is specified.
62     EMOJI = 2,    // Emoji (color) emoji style is specified.
63     TEXT = 3,     // Text (black/white) emoji style is specified.
64 };
65 
66 // Enum for line break style.
67 enum class LineBreakStyle : uint8_t {
68     EMPTY = 0,   // No line break style is specified.
69     LOOSE = 1,   // line break style is loose.
70     NORMAL = 2,  // line break style is normal.
71     STRICT = 3,  // line break style is strict.
72 };
73 
74 // Locale is a compact representation of a BCP 47 language tag.
75 // It does not capture all possible information, only what directly affects text layout:
76 // font rendering, hyphenation, word breaking, etc.
77 struct Locale {
78 public:
79     enum class Variant : uint16_t {
80         NO_VARIANT = 0x0000,
81         GERMAN_1901_ORTHOGRAPHY = 0x0001,
82         GERMAN_1996_ORTHOGRAPHY = 0x0002,
83     };
84 
85     // Default constructor creates the unsupported locale.
LocaleLocale86     Locale()
87             : mScript(NO_SCRIPT),
88               mLanguage(NO_LANGUAGE),
89               mRegion(NO_REGION),
90               mSubScriptBits(0ul),
91               mVariant(Variant::NO_VARIANT),
92               mEmojiStyle(EmojiStyle::EMPTY),
93               mLBStyle(LineBreakStyle::EMPTY) {}
94 
95     // Parse from string
96     Locale(const StringPiece& buf);
97 
98     // Parse from identifier. See getIdentifier() for the identifier format.
LocaleLocale99     explicit Locale(uint64_t identifier)
100             : mScript(extractBits(identifier, 29, 20)),
101               mLanguage(extractBits(identifier, 49, 15)),
102               mRegion(extractBits(identifier, 14, 15)),
103               mSubScriptBits(scriptToSubScriptBits(mScript)),
104               mVariant(static_cast<Variant>(extractBits(identifier, 0, 2))),
105               mEmojiStyle(static_cast<EmojiStyle>(extractBits(identifier, 12, 2))),
106               mLBStyle(static_cast<LineBreakStyle>(extractBits(identifier, 10, 2))) {}
107 
108     bool operator==(const Locale& other) const {
109         return !isUnsupported() && isEqualScript(other) && mLanguage == other.mLanguage &&
110                mRegion == other.mRegion && mVariant == other.mVariant &&
111                mLBStyle == other.mLBStyle && mEmojiStyle == other.mEmojiStyle;
112     }
113 
114     bool operator!=(const Locale other) const { return !(*this == other); }
115 
hasLanguageLocale116     inline bool hasLanguage() const { return mLanguage != NO_LANGUAGE; }
hasScriptLocale117     inline bool hasScript() const { return mScript != NO_SCRIPT; }
hasRegionLocale118     inline bool hasRegion() const { return mRegion != NO_REGION; }
hasVariantLocale119     inline bool hasVariant() const { return mVariant != Variant::NO_VARIANT; }
hasLBStyleLocale120     inline bool hasLBStyle() const { return mLBStyle != LineBreakStyle::EMPTY; }
hasEmojiStyleLocale121     inline bool hasEmojiStyle() const { return mEmojiStyle != EmojiStyle::EMPTY; }
122 
isSupportedLocale123     inline bool isSupported() const {
124         return hasLanguage() || hasScript() || hasRegion() || hasVariant() || hasLBStyle() ||
125                hasEmojiStyle();
126     }
127 
isUnsupportedLocale128     inline bool isUnsupported() const { return !isSupported(); }
129 
getEmojiStyleLocale130     EmojiStyle getEmojiStyle() const { return mEmojiStyle; }
131 
132     bool isEqualScript(const Locale& other) const;
133 
134     // Returns true if this script supports the given script. For example, ja-Jpan supports Hira,
135     // ja-Hira doesn't support Jpan.
136     bool supportsHbScript(hb_script_t script) const;
137 
138     std::string getString() const;
139 
140     // Calculates a matching score. This score represents how well the input locales cover this
141     // locale. The maximum score in the locale list is returned.
142     // 0 = no match, 1 = script match, 2 = script and primary language match.
143     int calcScoreFor(const LocaleList& supported) const;
144 
145     // Identifier pattern:
146     // |-------|-------|-------|-------|-------|-------|-------|-------|
147     // lllllllllllllll                                                   Language Code (15 bits)
148     //                ssssssssssssssssssss                               Script Code (20 bits)
149     //                                    rrrrrrrrrrrrrrr                Region Code (15 bits)
150     //                                                   ee              Emoji Style (2 bits)
151     //                                                     bb            Line Break Style (2 bits)
152     //                                                       XXXXXXXX    Free (8 bits)
153     //                                                               vv  German Variant (2 bits)
getIdentifierLocale154     uint64_t getIdentifier() const {
155         return ((uint64_t)mLanguage << 49) | ((uint64_t)mScript << 29) | ((uint64_t)mRegion << 14) |
156                ((uint64_t)mEmojiStyle << 12) | ((uint64_t)mLBStyle << 10) | (uint64_t)mVariant;
157     }
158 
159     Locale getPartialLocale(SubtagBits bits) const;
160 
161 private:
162     friend class LocaleList;  // for LocaleList constructor
163 
164     // ISO 15924 compliant script code. The 4 chars script code are packed into a 20 bit integer.
165     // If not specified, this is kInvalidScript.
166     uint32_t mScript;
167 
168     // ISO 639-1 or ISO 639-2 compliant language code.
169     // The two- or three-letter language code is packed into a 15 bit integer.
170     // mLanguage = 0 means the Locale is unsupported.
171     uint16_t mLanguage;
172 
173     // ISO 3166-1 or UN M.49 compliant region code. The two-letter or three-digit region code is
174     // packed into a 15 bit integer.
175     uint16_t mRegion;
176 
177     // For faster comparing, use 7 bits for specific scripts.
178     static const uint8_t kBopomofoFlag = 1u;
179     static const uint8_t kHanFlag = 1u << 1;
180     static const uint8_t kHangulFlag = 1u << 2;
181     static const uint8_t kHiraganaFlag = 1u << 3;
182     static const uint8_t kKatakanaFlag = 1u << 4;
183     static const uint8_t kSimplifiedChineseFlag = 1u << 5;
184     static const uint8_t kTraditionalChineseFlag = 1u << 6;
185     uint8_t mSubScriptBits;
186 
187     Variant mVariant;
188 
189     EmojiStyle mEmojiStyle;
190     LineBreakStyle mLBStyle;
191 
192     void resolveUnicodeExtension(const char* buf, size_t length);
193 
extractBitsLocale194     inline static uint64_t extractBits(uint64_t value, uint8_t shift, uint8_t nBits) {
195         return (value >> shift) & ((1 << nBits) - 1);
196     }
197 
198     static uint8_t scriptToSubScriptBits(uint32_t rawScript);
199 
200     static LineBreakStyle resolveLineBreakStyle(const char* buf, size_t length);
201     static EmojiStyle resolveEmojiStyle(const char* buf, size_t length);
202     static EmojiStyle scriptToEmojiStyle(uint32_t script);
203 
204     // Returns true if the provide subscript bits has the requested subscript bits.
205     // Note that this function returns false if the requested subscript bits are empty.
206     static bool supportsScript(uint8_t providedBits, uint8_t requestedBits);
207 };
208 
209 // An immutable list of locale.
210 class LocaleList {
211 public:
212     explicit LocaleList(std::vector<Locale>&& locales);
LocaleList()213     LocaleList()
214             : mUnionOfSubScriptBits(0),
215               mIsAllTheSameLocale(false),
216               mEmojiStyle(EmojiStyle::EMPTY) {}
217     LocaleList(LocaleList&&) = default;
218 
size()219     size_t size() const { return mLocales.size(); }
empty()220     bool empty() const { return mLocales.empty(); }
221     const Locale& operator[](size_t n) const { return mLocales[n]; }
222 
getHbLanguage(size_t n)223     hb_language_t getHbLanguage(size_t n) const { return mHbLangs[n]; }
224 
225     // Returns an effective emoji style of this locale list.
226     // The effective means the first non empty emoji style in the list.
getEmojiStyle()227     EmojiStyle getEmojiStyle() const { return mEmojiStyle; }
228 
229 private:
230     friend struct Locale;  // for calcScoreFor
231 
232     std::vector<Locale> mLocales;
233 
234     // The languages to be passed to HarfBuzz shaper.
235     std::vector<hb_language_t> mHbLangs;
236     uint8_t mUnionOfSubScriptBits;
237     bool mIsAllTheSameLocale;
238     EmojiStyle mEmojiStyle;
239 
getUnionOfSubScriptBits()240     uint8_t getUnionOfSubScriptBits() const { return mUnionOfSubScriptBits; }
isAllTheSameLocale()241     bool isAllTheSameLocale() const { return mIsAllTheSameLocale; }
242 
243     // Do not copy and assign.
244     LocaleList(const LocaleList&) = delete;
245     void operator=(const LocaleList&) = delete;
246 };
247 
248 }  // namespace minikin
249 
250 #endif  // MINIKIN_LOCALE_LIST_H
251