xref: /aosp_15_r20/frameworks/minikin/include/minikin/FontCollection.h (revision 834a2baab5fdfc28e9a428ee87c7ea8f6a06a53d)
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                           const VariationSettings& variationSettings);
getBestFont(U16StringPiece textBuf,const Run & run,FontStyle style)160     FakedFont getBestFont(U16StringPiece textBuf, const Run& run, FontStyle style) {
161         return getBestFont(textBuf, run, style, VariationSettings());
162     }
163 
164     // Perform the itemization until given max runs.
165     std::vector<Run> itemize(U16StringPiece text, FontStyle style, uint32_t localeListId,
166                              FamilyVariant familyVariant, uint32_t runMax) const;
167 
168     // Perform the itemization until end of the text.
itemize(U16StringPiece text,FontStyle style,uint32_t localeListId,FamilyVariant familyVariant)169     std::vector<Run> itemize(U16StringPiece text, FontStyle style, uint32_t localeListId,
170                              FamilyVariant familyVariant) const {
171         return itemize(text, style, localeListId, familyVariant, text.size());
172     }
173 
174     MinikinExtent getReferenceExtentForLocale(const MinikinPaint& paint) const;
175 
176     // Returns true if there is a glyph for the code point and variation selector pair.
177     // Returns false if no fonts have a glyph for the code point and variation
178     // selector pair, or invalid variation selector is passed.
179     bool hasVariationSelector(uint32_t baseCodepoint, uint32_t variationSelector) const;
180 
181     // Get base font with fakery information (fake bold could affect metrics)
182     FakedFont baseFontFaked(FontStyle style);
183 
184     // Creates new FontCollection based on this collection while applying font variations. Returns
185     // nullptr if none of variations apply to this collection.
186     std::shared_ptr<FontCollection> createCollectionWithVariation(
187             const VariationSettings& variations);
188     // Creates new FontCollection that uses the specified families as top families and
189     // families from this FontCollection as fallback.
190     std::shared_ptr<FontCollection> createCollectionWithFamilies(
191             std::vector<std::shared_ptr<FontFamily>>&& families) const;
192 
getSupportedAxesCount()193     size_t getSupportedAxesCount() const { return mSupportedAxesCount; }
getSupportedAxisAt(size_t index)194     AxisTag getSupportedAxisAt(size_t index) const { return mSupportedAxes[index]; }
195 
196     uint32_t getId() const;
197 
getFamilyCount()198     size_t getFamilyCount() const { return mFamilyCount; }
199 
getFamilyAt(size_t index)200     const std::shared_ptr<FontFamily>& getFamilyAt(size_t index) const {
201         if (mFamilyIndices != nullptr) {
202             index = mFamilyIndices[index];
203         }
204         return (*mMaybeSharedFamilies)[index];
205     }
206 
207 private:
208     FRIEND_TEST(FontCollectionTest, bufferTest);
209 
210     explicit FontCollection(const std::vector<std::shared_ptr<FontFamily>>& typefaces);
211     FontCollection(
212             BufferReader* reader,
213             const std::shared_ptr<std::vector<std::shared_ptr<FontFamily>>>& allFontFamilies);
214     // Write fields of the instance, using fontFamilyToIndexMap for finding
215     // indices for FontFamily.
216     void writeTo(BufferWriter* writer,
217                  const std::unordered_map<std::shared_ptr<FontFamily>, uint32_t>&
218                          fontFamilyToIndexMap) const;
219     static void collectAllFontFamilies(
220             const std::vector<std::shared_ptr<FontCollection>>& fontCollections,
221             std::vector<std::shared_ptr<FontFamily>>* outAllFontFamilies,
222             std::unordered_map<std::shared_ptr<FontFamily>, uint32_t>* outFontFamilyToIndexMap);
223 
224     static const int kLogCharsPerPage = 8;
225     static const int kPageMask = (1 << kLogCharsPerPage) - 1;
226 
227     // mFamilyVec holds the indices of the family (as in getFamilyAt()) and
228     // mRanges holds the range of indices of mFamilyVec.
229     // The maximum number of pages is 0x10FF (U+10FFFF >> 8). The maximum number of
230     // the fonts is 0xFF. Thus, technically the maximum length of mFamilyVec is 0x10EE01
231     // (0x10FF * 0xFF). However, in practice, 16-bit integers are enough since most fonts supports
232     // only limited range of code points.
233     struct Range {
234         uint16_t start;
235         uint16_t end;
236     };
237 
238     // Initialize the FontCollection.
239     void init(const std::vector<std::shared_ptr<FontFamily>>& typefaces);
240 
241     FamilyMatchResult getFamilyForChar(uint32_t ch, uint32_t vs, uint32_t localeListId,
242                                        FamilyVariant variant) const;
243 
244     uint32_t calcFamilyScore(uint32_t ch, uint32_t vs, FamilyVariant variant, uint32_t localeListId,
245                              const std::shared_ptr<FontFamily>& fontFamily) const;
246 
247     uint32_t calcCoverageScore(uint32_t ch, uint32_t vs, uint32_t localeListId,
248                                const std::shared_ptr<FontFamily>& fontFamily) const;
249 
250     bool isPrimaryFamily(const std::shared_ptr<FontFamily>& fontFamily) const;
251 
252     void filterFamilyByLocale(const LocaleList& localeList,
253                               const std::function<bool(const FontFamily& family)>& callback) const;
254 
255     static uint32_t calcLocaleMatchingScore(uint32_t userLocaleListId,
256                                             const FontFamily& fontFamily);
257 
258     static uint32_t calcVariantMatchingScore(FamilyVariant variant, const FontFamily& fontFamily);
259 
260     // unique id for this font collection (suitable for cache key)
261     uint32_t mId;
262 
263     // Highest UTF-32 code point that can be mapped
264     uint32_t mMaxChar;
265 
266     // This vector has pointers to the all font family instances in this collection.
267     // This vector can't be empty.
268     // This vector may be shared with other font collections.
269     // (1) When shared, this vector is a union of all font family instances
270     //     shared by multiple font collections.
271     //     mFamilyIndices will be non-null in this case.
272     //     The i-th family in this collection will be
273     //     mMaybeSharedFamilies[mFamilyIndices[i]].
274     // (2) When not shared, mFamilyIndices will be null and
275     //     the i-th family in this collection will be mMaybeSharedFamilies[i].
276     // Use getFamilyAt(i) to access the i-th font in this family.
277     std::shared_ptr<std::vector<std::shared_ptr<FontFamily>>> mMaybeSharedFamilies;
278     uint32_t mFamilyCount;
279     const uint32_t* mFamilyIndices;
280 
281     // Following two vectors are pre-calculated tables for resolving coverage faster.
282     // For example, to iterate over all fonts which support Unicode code point U+XXYYZZ,
283     // iterate font families index from mFamilyVec[mRanges[0xXXYY].start] to
284     // mFamilyVec[mRange[0xXXYY].end] instead of whole mFamilies.
285     // This vector contains indices into mFamilies.
286     // This vector can't be empty.
287     uint32_t mRangesCount;
288     const Range* mRanges;
289     uint32_t mFamilyVecCount;
290     const uint8_t* mFamilyVec;
291 
292     // This vector has pointers to the font family instances which have cmap 14 subtables.
293     std::vector<std::shared_ptr<FontFamily>> mVSFamilyVec;
294 
295     // Set of supported axes in this collection.
296     uint32_t mSupportedAxesCount;
297     // mSupportedAxes is sorted.
298     std::unique_ptr<AxisTag[]> mSupportedAxes;
299 
300     // Owns allocated memory if this class is created from font families, otherwise these are
301     // nullptr.
302     std::unique_ptr<Range[]> mOwnedRanges;
303     std::vector<uint8_t> mOwnedFamilyVec;
304 
305     // Pair of locale ID and text size.
306     mutable std::mutex mMutex;
307     mutable android::LruCache<LocaleExtentKey, MinikinExtent> mExtentCacheForLocale
GUARDED_BY(mMutex)308             GUARDED_BY(mMutex){8};
309 };
310 
311 }  // namespace minikin
312 
313 #endif  // MINIKIN_FONT_COLLECTION_H
314