FontFamily.h revision 71ec97055357b6ccb13a2697a56254bb19f43ae9
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
23#include <utils/TypeHelpers.h>
24
25#include <minikin/MinikinRefCounted.h>
26#include <minikin/SparseBitSet.h>
27
28namespace android {
29
30class MinikinFont;
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.
35class FontLanguage {
36    friend class FontStyle;
37public:
38    FontLanguage() : mBits(0) { }
39
40    // Parse from string
41    FontLanguage(const char* buf, size_t size);
42
43    bool operator==(const FontLanguage other) const {
44        return mBits != kUnsupportedLanguage && mBits == other.mBits;
45    }
46    operator bool() const { return mBits != 0; }
47
48    std::string getString() const;
49
50    // 0 = no match, 1 = language matches, 2 = language and script match
51    int match(const FontLanguage other) const;
52
53private:
54    explicit FontLanguage(uint32_t bits) : mBits(bits) { }
55
56    uint32_t bits() const { return mBits; }
57
58    static const uint32_t kUnsupportedLanguage = 0xFFFFFFFFu;
59    static const uint32_t kBaseLangMask = 0xFFFFFFu;
60    static const uint32_t kScriptMask = (1u << 26) - (1u << 24);
61    static const uint32_t kHansFlag = 1u << 24;
62    static const uint32_t kHantFlag = 1u << 25;
63    uint32_t mBits;
64};
65
66// FontStyle represents all style information needed to select an actual font
67// from a collection. The implementation is packed into a single 32-bit word
68// so it can be efficiently copied, embedded in other objects, etc.
69class FontStyle {
70public:
71    FontStyle(int weight = 4, bool italic = false) {
72        bits = (weight & kWeightMask) | (italic ? kItalicMask : 0);
73    }
74    FontStyle(FontLanguage lang, int variant = 0, int weight = 4, bool italic = false) {
75        bits = (weight & kWeightMask) | (italic ? kItalicMask : 0)
76                | (variant << kVariantShift) | (lang.bits() << kLangShift);
77    }
78    int getWeight() const { return bits & kWeightMask; }
79    bool getItalic() const { return (bits & kItalicMask) != 0; }
80    int getVariant() const { return (bits >> kVariantShift) & kVariantMask; }
81    FontLanguage getLanguage() const { return FontLanguage(bits >> kLangShift); }
82
83    bool operator==(const FontStyle other) const { return bits == other.bits; }
84
85    hash_t hash() const { return bits; }
86private:
87    static const uint32_t kWeightMask = (1 << 4) - 1;
88    static const uint32_t kItalicMask = 1 << 4;
89    static const int kVariantShift = 5;
90    static const uint32_t kVariantMask = (1 << 2) - 1;
91    static const int kLangShift = 7;
92    uint32_t bits;
93};
94
95enum FontVariant {
96    VARIANT_DEFAULT = 0,
97    VARIANT_COMPACT = 1,
98    VARIANT_ELEGANT = 2,
99};
100
101inline hash_t hash_type(const FontStyle &style) {
102    return style.hash();
103}
104
105// attributes representing transforms (fake bold, fake italic) to match styles
106class FontFakery {
107public:
108    FontFakery() : mFakeBold(false), mFakeItalic(false) { }
109    FontFakery(bool fakeBold, bool fakeItalic) : mFakeBold(fakeBold), mFakeItalic(fakeItalic) { }
110    // TODO: want to support graded fake bolding
111    bool isFakeBold() { return mFakeBold; }
112    bool isFakeItalic() { return mFakeItalic; }
113private:
114    bool mFakeBold;
115    bool mFakeItalic;
116};
117
118struct FakedFont {
119    // ownership is the enclosing FontCollection
120    MinikinFont* font;
121    FontFakery fakery;
122};
123
124class FontFamily : public MinikinRefCounted {
125public:
126    FontFamily() { }
127
128    FontFamily(FontLanguage lang, int variant) : mLang(lang), mVariant(variant) {
129    }
130
131    ~FontFamily();
132
133    // Add font to family, extracting style information from the font
134    bool addFont(MinikinFont* typeface);
135
136    void addFont(MinikinFont* typeface, FontStyle style);
137    FakedFont getClosestMatch(FontStyle style) const;
138
139    FontLanguage lang() const { return mLang; }
140    int variant() const { return mVariant; }
141
142    // API's for enumerating the fonts in a family. These don't guarantee any particular order
143    size_t getNumFonts() const;
144    MinikinFont* getFont(size_t index) const;
145    FontStyle getStyle(size_t index) const;
146
147    // Get Unicode coverage. Lifetime of returned bitset is same as receiver. May return nullptr on
148    // error.
149    const SparseBitSet* getCoverage();
150private:
151    void addFontLocked(MinikinFont* typeface, FontStyle style);
152
153    class Font {
154    public:
155        Font(MinikinFont* typeface, FontStyle style) :
156            typeface(typeface), style(style) { }
157        MinikinFont* typeface;
158        FontStyle style;
159    };
160    FontLanguage mLang;
161    int mVariant;
162    std::vector<Font> mFonts;
163
164    SparseBitSet mCoverage;
165    bool mCoverageValid;
166};
167
168}  // namespace android
169
170#endif  // MINIKIN_FONT_FAMILY_H
171