FontFamily.h revision 369d2d44cb09ac4346cdd9e3b131bb173d764f88
1/*
2 * Copyright (C) 2013 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_FAMILY_H
18#define MINIKIN_FONT_FAMILY_H
19
20#include <vector>
21#include <string>
22#include <hb.h>
23
24#include <utils/TypeHelpers.h>
25
26#include <minikin/MinikinRefCounted.h>
27#include <minikin/SparseBitSet.h>
28
29namespace android {
30
31class MinikinFont;
32
33// FontLanguage is a compact representation of a bcp-47 language tag. It
34// does not capture all possible information, only what directly affects
35// font rendering.
36class FontLanguage {
37    friend class FontStyle;
38    friend class FontLanguages;
39public:
40    FontLanguage() : mBits(0) { }
41
42    // Parse from string
43    FontLanguage(const char* buf, size_t size);
44
45    bool operator==(const FontLanguage other) const {
46        return mBits != kUnsupportedLanguage && mBits == other.mBits;
47    }
48    operator bool() const { return mBits != 0; }
49
50    bool isUnsupported() const { return mBits == kUnsupportedLanguage; }
51    bool hasEmojiFlag() const { return isUnsupported() ? false : (mBits & kEmojiFlag); }
52
53    std::string getString() const;
54
55    // 0 = no match, 1 = language matches, 2 = language and script match
56    int match(const FontLanguage other) const;
57
58private:
59    explicit FontLanguage(uint32_t bits) : mBits(bits) { }
60
61    uint32_t bits() const { return mBits; }
62
63    static const uint32_t kUnsupportedLanguage = 0xFFFFFFFFu;
64    static const uint32_t kBaseLangMask = 0xFFFFFFu;
65    static const uint32_t kHansFlag = 1u << 24;
66    static const uint32_t kHantFlag = 1u << 25;
67    static const uint32_t kEmojiFlag = 1u << 26;
68    static const uint32_t kScriptMask = kHansFlag | kHantFlag | kEmojiFlag;
69    uint32_t mBits;
70};
71
72// A list of zero or more instances of FontLanguage, in the order of
73// preference. Used for further resolution of rendering results.
74class FontLanguages {
75public:
76    FontLanguages() { mLangs.clear(); }
77
78    // Parse from string, which is a comma-separated list of languages
79    FontLanguages(const char* buf, size_t size);
80
81    const FontLanguage& operator[](size_t index) const { return mLangs.at(index); }
82
83    size_t size() const { return mLangs.size(); }
84
85private:
86    std::vector<FontLanguage> mLangs;
87};
88
89// FontStyle represents all style information needed to select an actual font
90// from a collection. The implementation is packed into a single 32-bit word
91// so it can be efficiently copied, embedded in other objects, etc.
92class FontStyle {
93public:
94    FontStyle(int weight = 4, bool italic = false) {
95        bits = (weight & kWeightMask) | (italic ? kItalicMask : 0);
96    }
97    FontStyle(FontLanguage lang, int variant = 0, int weight = 4, bool italic = false) {
98        bits = (weight & kWeightMask) | (italic ? kItalicMask : 0)
99                | (variant << kVariantShift) | (lang.bits() << kLangShift);
100    }
101    FontStyle(FontLanguages langs, int variant = 0, int weight = 4, bool italic = false) :
102        // TODO: Use all the languages in langs
103        FontStyle(langs[0], variant, weight, italic) { }
104    int getWeight() const { return bits & kWeightMask; }
105    bool getItalic() const { return (bits & kItalicMask) != 0; }
106    int getVariant() const { return (bits >> kVariantShift) & kVariantMask; }
107    FontLanguage getLanguage() const { return FontLanguage(bits >> kLangShift); }
108
109    bool operator==(const FontStyle other) const { return bits == other.bits; }
110
111    hash_t hash() const { return bits; }
112private:
113    static const uint32_t kWeightMask = (1 << 4) - 1;
114    static const uint32_t kItalicMask = 1 << 4;
115    static const int kVariantShift = 5;
116    static const uint32_t kVariantMask = (1 << 2) - 1;
117    static const int kLangShift = 7;
118    uint32_t bits;
119};
120
121enum FontVariant {
122    VARIANT_DEFAULT = 0,
123    VARIANT_COMPACT = 1,
124    VARIANT_ELEGANT = 2,
125};
126
127inline hash_t hash_type(const FontStyle &style) {
128    return style.hash();
129}
130
131// attributes representing transforms (fake bold, fake italic) to match styles
132class FontFakery {
133public:
134    FontFakery() : mFakeBold(false), mFakeItalic(false) { }
135    FontFakery(bool fakeBold, bool fakeItalic) : mFakeBold(fakeBold), mFakeItalic(fakeItalic) { }
136    // TODO: want to support graded fake bolding
137    bool isFakeBold() { return mFakeBold; }
138    bool isFakeItalic() { return mFakeItalic; }
139private:
140    bool mFakeBold;
141    bool mFakeItalic;
142};
143
144struct FakedFont {
145    // ownership is the enclosing FontCollection
146    MinikinFont* font;
147    FontFakery fakery;
148};
149
150class FontFamily : public MinikinRefCounted {
151public:
152    FontFamily() : mHbFont(nullptr) { }
153
154    FontFamily(FontLanguage lang, int variant) : mLang(lang), mVariant(variant), mHbFont(nullptr) {
155    }
156
157    ~FontFamily();
158
159    // Add font to family, extracting style information from the font
160    bool addFont(MinikinFont* typeface);
161
162    void addFont(MinikinFont* typeface, FontStyle style);
163    FakedFont getClosestMatch(FontStyle style) const;
164
165    FontLanguage lang() const { return mLang; }
166    int variant() const { return mVariant; }
167
168    // API's for enumerating the fonts in a family. These don't guarantee any particular order
169    size_t getNumFonts() const;
170    MinikinFont* getFont(size_t index) const;
171    FontStyle getStyle(size_t index) const;
172
173    // Get Unicode coverage. Lifetime of returned bitset is same as receiver. May return nullptr on
174    // error.
175    const SparseBitSet* getCoverage();
176
177    // Returns true if the font has a glyph for the code point and variation selector pair.
178    // Caller should acquire a lock before calling the method.
179    bool hasVariationSelector(uint32_t codepoint, uint32_t variationSelector);
180
181    // Purges cached mHbFont.
182    // hb_font_t keeps a reference to hb_face_t which is managed by HbFaceCache. Thus,
183    // it is good to purge hb_font_t once it is no longer necessary.
184    // Caller should acquire a lock before calling the method.
185    void purgeHbFontCache();
186private:
187    void addFontLocked(MinikinFont* typeface, FontStyle style);
188
189    class Font {
190    public:
191        Font(MinikinFont* typeface, FontStyle style) :
192            typeface(typeface), style(style) { }
193        MinikinFont* typeface;
194        FontStyle style;
195    };
196    FontLanguage mLang;
197    int mVariant;
198    std::vector<Font> mFonts;
199
200    SparseBitSet mCoverage;
201    bool mCoverageValid;
202
203    hb_font_t* mHbFont;
204};
205
206}  // namespace android
207
208#endif  // MINIKIN_FONT_FAMILY_H
209