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 <memory>
21 #include <unordered_set>
22 #include <vector>
23 
24 #include "minikin/FontFamily.h"
25 #include "minikin/MinikinFont.h"
26 #include "minikin/U16StringPiece.h"
27 
28 namespace minikin {
29 
30 // The maximum number of font families.
31 constexpr uint32_t MAX_FAMILY_COUNT = 254;
32 
33 class FontCollection {
34 public:
35     explicit FontCollection(const std::vector<std::shared_ptr<FontFamily>>& typefaces);
36     explicit FontCollection(std::shared_ptr<FontFamily>&& typeface);
37 
38     struct Run {
39         FakedFont fakedFont;
40         int start;
41         int end;
42     };
43 
44     // Perform the itemization until given max runs.
45     std::vector<Run> itemize(U16StringPiece text, FontStyle style, uint32_t localeListId,
46                              FamilyVariant familyVariant, uint32_t runMax) const;
47 
48     // Perform the itemization until end of the text.
itemize(U16StringPiece text,FontStyle style,uint32_t localeListId,FamilyVariant familyVariant)49     std::vector<Run> itemize(U16StringPiece text, FontStyle style, uint32_t localeListId,
50                              FamilyVariant familyVariant) const {
51         return itemize(text, style, localeListId, familyVariant, text.size());
52     }
53 
54     // Returns true if there is a glyph for the code point and variation selector pair.
55     // Returns false if no fonts have a glyph for the code point and variation
56     // selector pair, or invalid variation selector is passed.
57     bool hasVariationSelector(uint32_t baseCodepoint, uint32_t variationSelector) const;
58 
59     // Get base font with fakery information (fake bold could affect metrics)
60     FakedFont baseFontFaked(FontStyle style);
61 
62     // Creates new FontCollection based on this collection while applying font variations. Returns
63     // nullptr if none of variations apply to this collection.
64     std::shared_ptr<FontCollection> createCollectionWithVariation(
65             const std::vector<FontVariation>& variations);
66 
getSupportedTags()67     const std::unordered_set<AxisTag>& getSupportedTags() const { return mSupportedAxes; }
68 
69     uint32_t getId() const;
70 
71 private:
72     static const int kLogCharsPerPage = 8;
73     static const int kPageMask = (1 << kLogCharsPerPage) - 1;
74 
75     // mFamilyVec holds the indices of the mFamilies and mRanges holds the range of indices of
76     // mFamilyVec. The maximum number of pages is 0x10FF (U+10FFFF >> 8). The maximum number of
77     // the fonts is 0xFF. Thus, technically the maximum length of mFamilyVec is 0x10EE01
78     // (0x10FF * 0xFF). However, in practice, 16-bit integers are enough since most fonts supports
79     // only limited range of code points.
80     struct Range {
81         uint16_t start;
82         uint16_t end;
83     };
84 
85     // Initialize the FontCollection.
86     void init(const std::vector<std::shared_ptr<FontFamily>>& typefaces);
87 
88     const std::shared_ptr<FontFamily>& getFamilyForChar(uint32_t ch, uint32_t vs,
89                                                         uint32_t localeListId,
90                                                         FamilyVariant variant) const;
91 
92     uint32_t calcFamilyScore(uint32_t ch, uint32_t vs, FamilyVariant variant, uint32_t localeListId,
93                              const std::shared_ptr<FontFamily>& fontFamily) const;
94 
95     uint32_t calcCoverageScore(uint32_t ch, uint32_t vs, uint32_t localeListId,
96                                const std::shared_ptr<FontFamily>& fontFamily) const;
97 
98     static uint32_t calcLocaleMatchingScore(uint32_t userLocaleListId,
99                                             const FontFamily& fontFamily);
100 
101     static uint32_t calcVariantMatchingScore(FamilyVariant variant, const FontFamily& fontFamily);
102 
103     // unique id for this font collection (suitable for cache key)
104     uint32_t mId;
105 
106     // Highest UTF-32 code point that can be mapped
107     uint32_t mMaxChar;
108 
109     // This vector has pointers to the all font family instances in this collection.
110     // This vector can't be empty.
111     std::vector<std::shared_ptr<FontFamily>> mFamilies;
112 
113     // Following two vectors are pre-calculated tables for resolving coverage faster.
114     // For example, to iterate over all fonts which support Unicode code point U+XXYYZZ,
115     // iterate font families index from mFamilyVec[mRanges[0xXXYY].start] to
116     // mFamilyVec[mRange[0xXXYY].end] instead of whole mFamilies.
117     // This vector contains indices into mFamilies.
118     // This vector can't be empty.
119     std::vector<Range> mRanges;
120     std::vector<uint8_t> mFamilyVec;
121 
122     // This vector has pointers to the font family instances which have cmap 14 subtables.
123     std::vector<std::shared_ptr<FontFamily>> mVSFamilyVec;
124 
125     // Set of supported axes in this collection.
126     std::unordered_set<AxisTag> mSupportedAxes;
127 };
128 
129 }  // namespace minikin
130 
131 #endif  // MINIKIN_FONT_COLLECTION_H
132