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_COLLECTION_H
18 #define MINIKIN_FONT_COLLECTION_H
19
20 #include <gtest/gtest_prod.h>
21 #include <utils/LruCache.h>
22
23 #include <functional>
24 #include <memory>
25 #include <mutex>
26 #include <unordered_map>
27 #include <vector>
28
29 #include "minikin/Buffer.h"
30 #include "minikin/Font.h"
31 #include "minikin/FontFamily.h"
32 #include "minikin/Hasher.h"
33 #include "minikin/MinikinExtent.h"
34 #include "minikin/MinikinFont.h"
35 #include "minikin/U16StringPiece.h"
36
37 namespace minikin {
38
39 // The maximum number of font families.
40 constexpr uint32_t MAX_FAMILY_COUNT = 254;
41
42 struct LocaleExtentKey {
43 uint32_t localeId;
44 float textSize;
45
46 bool operator==(const LocaleExtentKey& o) const {
47 return localeId == o.localeId && textSize == o.textSize;
48 }
49 };
50
hash_type(const LocaleExtentKey & key)51 inline android::hash_t hash_type(const LocaleExtentKey& key) {
52 return Hasher().update(key.localeId).update(key.textSize).hash();
53 }
54
55 class LocaleList;
56
57 class FontCollection {
58 public:
59 static std::shared_ptr<FontCollection> create(
60 const std::vector<std::shared_ptr<FontFamily>>& typefaces);
61 static std::shared_ptr<FontCollection> create(std::shared_ptr<FontFamily>&& typeface);
62
63 static std::vector<std::shared_ptr<FontCollection>> readVector(BufferReader* reader);
64 static void writeVector(BufferWriter* writer,
65 const std::vector<std::shared_ptr<FontCollection>>& fontCollections);
66
67 // Helper class for representing font family match result in packed bits.
68 struct FamilyMatchResult {
69 public:
70 struct Builder {
71 public:
BuilderFamilyMatchResult::Builder72 Builder() : mSize(0), mBits(0) {}
73
addFamilyMatchResult::Builder74 Builder& add(uint8_t x) {
75 if (mSize >= 7) [[unlikely]] {
76 return *this;
77 }
78 mBits = mBits | (static_cast<uint64_t>(x) << (8 * mSize));
79 mSize++;
80 return *this;
81 }
82
resetFamilyMatchResult::Builder83 Builder& reset() {
84 mSize = 0;
85 mBits = 0;
86 return *this;
87 }
88
sizeFamilyMatchResult::Builder89 uint8_t size() const { return mSize; }
90
emptyFamilyMatchResult::Builder91 bool empty() const { return size() == 0; }
92
buildFamilyMatchResult::Builder93 FamilyMatchResult build() {
94 return FamilyMatchResult(mBits | (static_cast<uint64_t>(mSize) << 56));
95 }
96
97 private:
98 uint8_t mSize;
99 uint64_t mBits;
100 };
101
102 // Helper class for iterating FamilyMatchResult
103 class iterator {
104 public:
105 inline bool operator==(const iterator& o) const {
106 return mOffset == o.mOffset && mResult == o.mResult;
107 }
108
109 inline bool operator!=(const iterator& o) const { return !(*this == o); }
110 inline uint8_t operator*() const { return mResult[mOffset]; }
111 inline iterator& operator++() {
112 mOffset++;
113 return *this;
114 }
115
116 private:
117 friend struct FamilyMatchResult;
iteratorFamilyMatchResult118 iterator(const FamilyMatchResult& result, uint32_t offset)
119 : mResult(result), mOffset(offset) {}
120 const FamilyMatchResult& mResult;
121 uint32_t mOffset;
122 };
123
124 // Create empty FamilyMatchResult.
FamilyMatchResultFamilyMatchResult125 FamilyMatchResult() : mBits(0) {}
126
sizeFamilyMatchResult127 inline uint8_t size() const { return static_cast<uint8_t>(mBits >> 56); }
128
129 inline uint8_t operator[](uint32_t pos) const {
130 return static_cast<uint8_t>(mBits >> (pos * 8));
131 }
132
emptyFamilyMatchResult133 inline bool empty() const { return size() == 0; }
134
135 inline bool operator==(const FamilyMatchResult& o) const { return mBits == o.mBits; }
136
137 // Returns the common family indices between l and r.
138 static FamilyMatchResult intersect(FamilyMatchResult l, FamilyMatchResult r);
139
140 // Iterator
beginFamilyMatchResult141 inline iterator begin() const { return iterator(*this, 0); }
endFamilyMatchResult142 inline iterator end() const { return iterator(*this, size()); }
143
144 FamilyMatchResult(const FamilyMatchResult& o) = default;
145 FamilyMatchResult& operator=(const FamilyMatchResult& o) = default;
146
147 private:
FamilyMatchResultFamilyMatchResult148 explicit FamilyMatchResult(uint64_t bits) : mBits(bits) {}
149 uint64_t mBits;
150 };
151
152 struct Run {
153 FamilyMatchResult familyMatch;
154 int start;
155 int end;
156 };
157
158 FakedFont getBestFont(U16StringPiece textBuf, const Run& run, FontStyle style);
159
160 // Perform the itemization until given max runs.
161 std::vector<Run> itemize(U16StringPiece text, FontStyle style, uint32_t localeListId,
162 FamilyVariant familyVariant, uint32_t runMax) const;
163
164 // Perform the itemization until end of the text.
itemize(U16StringPiece text,FontStyle style,uint32_t localeListId,FamilyVariant familyVariant)165 std::vector<Run> itemize(U16StringPiece text, FontStyle style, uint32_t localeListId,
166 FamilyVariant familyVariant) const {
167 return itemize(text, style, localeListId, familyVariant, text.size());
168 }
169
170 MinikinExtent getReferenceExtentForLocale(const MinikinPaint& paint) const;
171
172 // Returns true if there is a glyph for the code point and variation selector pair.
173 // Returns false if no fonts have a glyph for the code point and variation
174 // selector pair, or invalid variation selector is passed.
175 bool hasVariationSelector(uint32_t baseCodepoint, uint32_t variationSelector) const;
176
177 // Get base font with fakery information (fake bold could affect metrics)
178 FakedFont baseFontFaked(FontStyle style);
179
180 // Creates new FontCollection based on this collection while applying font variations. Returns
181 // nullptr if none of variations apply to this collection.
182 std::shared_ptr<FontCollection> createCollectionWithVariation(
183 const std::vector<FontVariation>& variations);
184 // Creates new FontCollection that uses the specified families as top families and
185 // families from this FontCollection as fallback.
186 std::shared_ptr<FontCollection> createCollectionWithFamilies(
187 std::vector<std::shared_ptr<FontFamily>>&& families) const;
188
getSupportedAxesCount()189 size_t getSupportedAxesCount() const { return mSupportedAxesCount; }
getSupportedAxisAt(size_t index)190 AxisTag getSupportedAxisAt(size_t index) const { return mSupportedAxes[index]; }
191
192 uint32_t getId() const;
193
getFamilyCount()194 size_t getFamilyCount() const { return mFamilyCount; }
195
getFamilyAt(size_t index)196 const std::shared_ptr<FontFamily>& getFamilyAt(size_t index) const {
197 if (mFamilyIndices != nullptr) {
198 index = mFamilyIndices[index];
199 }
200 return (*mMaybeSharedFamilies)[index];
201 }
202
203 private:
204 FRIEND_TEST(FontCollectionTest, bufferTest);
205
206 explicit FontCollection(const std::vector<std::shared_ptr<FontFamily>>& typefaces);
207 FontCollection(
208 BufferReader* reader,
209 const std::shared_ptr<std::vector<std::shared_ptr<FontFamily>>>& allFontFamilies);
210 // Write fields of the instance, using fontFamilyToIndexMap for finding
211 // indices for FontFamily.
212 void writeTo(BufferWriter* writer,
213 const std::unordered_map<std::shared_ptr<FontFamily>, uint32_t>&
214 fontFamilyToIndexMap) const;
215 static void collectAllFontFamilies(
216 const std::vector<std::shared_ptr<FontCollection>>& fontCollections,
217 std::vector<std::shared_ptr<FontFamily>>* outAllFontFamilies,
218 std::unordered_map<std::shared_ptr<FontFamily>, uint32_t>* outFontFamilyToIndexMap);
219
220 static const int kLogCharsPerPage = 8;
221 static const int kPageMask = (1 << kLogCharsPerPage) - 1;
222
223 // mFamilyVec holds the indices of the family (as in getFamilyAt()) and
224 // mRanges holds the range of indices of mFamilyVec.
225 // The maximum number of pages is 0x10FF (U+10FFFF >> 8). The maximum number of
226 // the fonts is 0xFF. Thus, technically the maximum length of mFamilyVec is 0x10EE01
227 // (0x10FF * 0xFF). However, in practice, 16-bit integers are enough since most fonts supports
228 // only limited range of code points.
229 struct Range {
230 uint16_t start;
231 uint16_t end;
232 };
233
234 // Initialize the FontCollection.
235 void init(const std::vector<std::shared_ptr<FontFamily>>& typefaces);
236
237 FamilyMatchResult getFamilyForChar(uint32_t ch, uint32_t vs, uint32_t localeListId,
238 FamilyVariant variant) const;
239
240 uint32_t calcFamilyScore(uint32_t ch, uint32_t vs, FamilyVariant variant, uint32_t localeListId,
241 const std::shared_ptr<FontFamily>& fontFamily) const;
242
243 uint32_t calcCoverageScore(uint32_t ch, uint32_t vs, uint32_t localeListId,
244 const std::shared_ptr<FontFamily>& fontFamily) const;
245
246 bool isPrimaryFamily(const std::shared_ptr<FontFamily>& fontFamily) const;
247
248 void filterFamilyByLocale(const LocaleList& localeList,
249 const std::function<void(const FontFamily& family)>& callback) const;
250
251 static uint32_t calcLocaleMatchingScore(uint32_t userLocaleListId,
252 const FontFamily& fontFamily);
253
254 static uint32_t calcVariantMatchingScore(FamilyVariant variant, const FontFamily& fontFamily);
255
256 // unique id for this font collection (suitable for cache key)
257 uint32_t mId;
258
259 // Highest UTF-32 code point that can be mapped
260 uint32_t mMaxChar;
261
262 // This vector has pointers to the all font family instances in this collection.
263 // This vector can't be empty.
264 // This vector may be shared with other font collections.
265 // (1) When shared, this vector is a union of all font family instances
266 // shared by multiple font collections.
267 // mFamilyIndices will be non-null in this case.
268 // The i-th family in this collection will be
269 // mMaybeSharedFamilies[mFamilyIndices[i]].
270 // (2) When not shared, mFamilyIndices will be null and
271 // the i-th family in this collection will be mMaybeSharedFamilies[i].
272 // Use getFamilyAt(i) to access the i-th font in this family.
273 std::shared_ptr<std::vector<std::shared_ptr<FontFamily>>> mMaybeSharedFamilies;
274 uint32_t mFamilyCount;
275 const uint32_t* mFamilyIndices;
276
277 // Following two vectors are pre-calculated tables for resolving coverage faster.
278 // For example, to iterate over all fonts which support Unicode code point U+XXYYZZ,
279 // iterate font families index from mFamilyVec[mRanges[0xXXYY].start] to
280 // mFamilyVec[mRange[0xXXYY].end] instead of whole mFamilies.
281 // This vector contains indices into mFamilies.
282 // This vector can't be empty.
283 uint32_t mRangesCount;
284 const Range* mRanges;
285 uint32_t mFamilyVecCount;
286 const uint8_t* mFamilyVec;
287
288 // This vector has pointers to the font family instances which have cmap 14 subtables.
289 std::vector<std::shared_ptr<FontFamily>> mVSFamilyVec;
290
291 // Set of supported axes in this collection.
292 uint32_t mSupportedAxesCount;
293 // mSupportedAxes is sorted.
294 std::unique_ptr<AxisTag[]> mSupportedAxes;
295
296 // Owns allocated memory if this class is created from font families, otherwise these are
297 // nullptr.
298 std::unique_ptr<Range[]> mOwnedRanges;
299 std::vector<uint8_t> mOwnedFamilyVec;
300
301 // Pair of locale ID and text size.
302 mutable std::mutex mMutex;
303 mutable android::LruCache<LocaleExtentKey, MinikinExtent> mExtentCacheForLocale
GUARDED_BY(mMutex)304 GUARDED_BY(mMutex){8};
305 };
306
307 } // namespace minikin
308
309 #endif // MINIKIN_FONT_COLLECTION_H
310