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