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 <memory>
21 #include <string>
22 #include <unordered_set>
23 #include <vector>
24
25 #include <hb.h>
26
27 #include <utils/TypeHelpers.h>
28
29 #include <minikin/SparseBitSet.h>
30
31 namespace minikin {
32
33 class MinikinFont;
34
35 // FontStyle represents all style information needed to select an actual font
36 // from a collection. The implementation is packed into two 32-bit words
37 // so it can be efficiently copied, embedded in other objects, etc.
38 class FontStyle {
39 public:
FontStyle()40 FontStyle()
41 : FontStyle(0 /* variant */, 4 /* weight */, false /* italic */) {}
FontStyle(int weight,bool italic)42 FontStyle(int weight, bool italic)
43 : FontStyle(0 /* variant */, weight, italic) {}
FontStyle(uint32_t langListId)44 FontStyle(uint32_t langListId) // NOLINT(implicit)
45 : FontStyle(langListId,
46 0 /* variant */,
47 4 /* weight */,
48 false /* italic */) {}
49
50 FontStyle(int variant, int weight, bool italic);
51 FontStyle(uint32_t langListId, int variant, int weight, bool italic);
52
getWeight()53 int getWeight() const { return bits & kWeightMask; }
getItalic()54 bool getItalic() const { return (bits & kItalicMask) != 0; }
getVariant()55 int getVariant() const { return (bits >> kVariantShift) & kVariantMask; }
getLanguageListId()56 uint32_t getLanguageListId() const { return mLanguageListId; }
57
58 bool operator==(const FontStyle other) const {
59 return bits == other.bits && mLanguageListId == other.mLanguageListId;
60 }
61
62 android::hash_t hash() const;
63
64 // Looks up a language list from an internal cache and returns its ID.
65 // If the passed language list is not in the cache, registers it and returns
66 // newly assigned ID.
67 static uint32_t registerLanguageList(const std::string& languages);
68
69 private:
70 static const uint32_t kWeightMask = (1 << 4) - 1;
71 static const uint32_t kItalicMask = 1 << 4;
72 static const int kVariantShift = 5;
73 static const uint32_t kVariantMask = (1 << 2) - 1;
74
75 static uint32_t pack(int variant, int weight, bool italic);
76
77 uint32_t bits;
78 uint32_t mLanguageListId;
79 };
80
81 enum FontVariant {
82 VARIANT_DEFAULT = 0,
83 VARIANT_COMPACT = 1,
84 VARIANT_ELEGANT = 2,
85 };
86
hash_type(const FontStyle & style)87 inline android::hash_t hash_type(const FontStyle& style) {
88 return style.hash();
89 }
90
91 // attributes representing transforms (fake bold, fake italic) to match styles
92 class FontFakery {
93 public:
FontFakery()94 FontFakery() : mFakeBold(false), mFakeItalic(false) {}
FontFakery(bool fakeBold,bool fakeItalic)95 FontFakery(bool fakeBold, bool fakeItalic)
96 : mFakeBold(fakeBold), mFakeItalic(fakeItalic) {}
97 // TODO: want to support graded fake bolding
isFakeBold()98 bool isFakeBold() { return mFakeBold; }
isFakeItalic()99 bool isFakeItalic() { return mFakeItalic; }
100
101 private:
102 bool mFakeBold;
103 bool mFakeItalic;
104 };
105
106 struct FakedFont {
107 // ownership is the enclosing FontCollection
108 MinikinFont* font;
109 FontFakery fakery;
110 };
111
112 typedef uint32_t AxisTag;
113
114 struct Font {
115 Font(const std::shared_ptr<MinikinFont>& typeface, FontStyle style);
116 Font(std::shared_ptr<MinikinFont>&& typeface, FontStyle style);
117 Font(Font&& o);
118 Font(const Font& o);
119
120 std::shared_ptr<MinikinFont> typeface;
121 FontStyle style;
122
123 std::unordered_set<AxisTag> getSupportedAxesLocked() const;
124 };
125
126 struct FontVariation {
FontVariationFontVariation127 FontVariation(AxisTag axisTag, float value)
128 : axisTag(axisTag), value(value) {}
129 AxisTag axisTag;
130 float value;
131 };
132
133 class FontFamily {
134 public:
135 explicit FontFamily(std::vector<Font>&& fonts);
136 FontFamily(int variant, std::vector<Font>&& fonts);
137 FontFamily(uint32_t langId, int variant, std::vector<Font>&& fonts);
138
139 // TODO: Good to expose FontUtil.h.
140 static bool analyzeStyle(const std::shared_ptr<MinikinFont>& typeface,
141 int* weight,
142 bool* italic);
143 FakedFont getClosestMatch(FontStyle style) const;
144
langId()145 uint32_t langId() const { return mLangId; }
variant()146 int variant() const { return mVariant; }
147
148 // API's for enumerating the fonts in a family. These don't guarantee any
149 // particular order
getNumFonts()150 size_t getNumFonts() const { return mFonts.size(); }
getFont(size_t index)151 const std::shared_ptr<MinikinFont>& getFont(size_t index) const {
152 return mFonts[index].typeface;
153 }
getStyle(size_t index)154 FontStyle getStyle(size_t index) const { return mFonts[index].style; }
155 bool isColorEmojiFamily() const;
supportedAxes()156 const std::unordered_set<AxisTag>& supportedAxes() const {
157 return mSupportedAxes;
158 }
159
160 // Get Unicode coverage.
getCoverage()161 const SparseBitSet& getCoverage() const { return mCoverage; }
162
163 // Returns true if the font has a glyph for the code point and variation
164 // selector pair. Caller should acquire a lock before calling the method.
165 bool hasGlyph(uint32_t codepoint, uint32_t variationSelector) const;
166
167 // Returns true if this font family has a variaion sequence table (cmap format
168 // 14 subtable).
hasVSTable()169 bool hasVSTable() const { return mHasVSTable; }
170
171 // Creates new FontFamily based on this family while applying font variations.
172 // Returns nullptr if none of variations apply to this family.
173 std::shared_ptr<FontFamily> createFamilyWithVariation(
174 const std::vector<FontVariation>& variations) const;
175
176 int getHwFontFamilyType();
177 void setHwFontFamilyType(int type);
178
179 private:
180 void computeCoverage();
181
182 uint32_t mLangId;
183 int mVariant;
184 std::vector<Font> mFonts;
185 std::unordered_set<AxisTag> mSupportedAxes;
186
187 SparseBitSet mCoverage;
188 bool mHasVSTable;
189 int mHwFontFamilyType = 0;
190
191 // Forbid copying and assignment.
192 FontFamily(const FontFamily&) = delete;
193 void operator=(const FontFamily&) = delete;
194 };
195
196 } // namespace minikin
197
198 #endif // MINIKIN_FONT_FAMILY_H
199