Flutter Engine
FontCollection.h
Go to the documentation of this file.
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 <map>
21 #include <memory>
22 #include <unordered_set>
23 #include <vector>
24 
25 #include <minikin/FontFamily.h>
26 #include <minikin/MinikinFont.h>
27 
28 namespace minikin {
29 
31  public:
32  explicit FontCollection(
33  const std::vector<std::shared_ptr<FontFamily>>& typefaces);
34  explicit FontCollection(std::shared_ptr<FontFamily>&& typeface);
35 
36  // libtxt extension: an interface for looking up fallback fonts for characters
37  // that do not match this collection's font families.
39  public:
40  virtual ~FallbackFontProvider() = default;
41  virtual const std::shared_ptr<FontFamily>& matchFallbackFont(
42  uint32_t ch,
43  std::string locale) = 0;
44  };
45 
46  struct Run {
48  int start;
49  int end;
50  };
51 
52  void itemize(const uint16_t* string,
53  size_t string_length,
54  FontStyle style,
55  std::vector<Run>* result) const;
56 
57  // Returns true if there is a glyph for the code point and variation selector
58  // pair. Returns false if no fonts have a glyph for the code point and
59  // variation selector pair, or invalid variation selector is passed.
60  bool hasVariationSelector(uint32_t baseCodepoint,
61  uint32_t variationSelector) const;
62 
63  // Get base font with fakery information (fake bold could affect metrics)
65 
66  // Creates new FontCollection based on this collection while applying font
67  // variations. Returns nullptr if none of variations apply to this collection.
68  std::shared_ptr<FontCollection> createCollectionWithVariation(
69  const std::vector<FontVariation>& variations);
70 
71  const std::unordered_set<AxisTag>& getSupportedTags() const {
72  return mSupportedAxes;
73  }
74 
75  uint32_t getId() const;
76 
77  void set_fallback_font_provider(std::unique_ptr<FallbackFontProvider> ffp) {
78  mFallbackFontProvider = std::move(ffp);
79  }
80 
81  private:
82  static const int kLogCharsPerPage = 8;
83  static const int kPageMask = (1 << kLogCharsPerPage) - 1;
84 
85  // mFamilyVec holds the indices of the mFamilies and mRanges holds the range
86  // of indices of mFamilyVec. The maximum number of pages is 0x10FF (U+10FFFF
87  // >> 8). The maximum number of the fonts is 0xFF. Thus, technically the
88  // maximum length of mFamilyVec is 0x10EE01 (0x10FF * 0xFF). However, in
89  // practice, 16-bit integers are enough since most fonts supports only limited
90  // range of code points.
91  struct Range {
92  uint16_t start;
93  uint16_t end;
94  };
95 
96  // Initialize the FontCollection.
97  void init(const std::vector<std::shared_ptr<FontFamily>>& typefaces);
98 
99  const std::shared_ptr<FontFamily>& getFamilyForChar(uint32_t ch,
100  uint32_t vs,
101  uint32_t langListId,
102  int variant) const;
103 
104  const std::shared_ptr<FontFamily>&
105  findFallbackFont(uint32_t ch, uint32_t vs, uint32_t langListId) const;
106 
107  uint32_t calcFamilyScore(uint32_t ch,
108  uint32_t vs,
109  int variant,
110  uint32_t langListId,
111  const std::shared_ptr<FontFamily>& fontFamily) const;
112 
113  uint32_t calcCoverageScore(
114  uint32_t ch,
115  uint32_t vs,
116  const std::shared_ptr<FontFamily>& fontFamily) const;
117 
118  static uint32_t calcLanguageMatchingScore(uint32_t userLangListId,
119  const FontFamily& fontFamily);
120 
121  static uint32_t calcVariantMatchingScore(int variant,
122  const FontFamily& fontFamily);
123 
124  // static for allocating unique id's
125  static uint32_t sNextId;
126 
127  // unique id for this font collection (suitable for cache key)
128  uint32_t mId;
129 
130  // Highest UTF-32 code point that can be mapped
131  uint32_t mMaxChar;
132 
133  // This vector has pointers to the all font family instances in this
134  // collection. This vector can't be empty.
135  std::vector<std::shared_ptr<FontFamily>> mFamilies;
136 
137  // Following two vectors are pre-calculated tables for resolving coverage
138  // faster. For example, to iterate over all fonts which support Unicode code
139  // point U+XXYYZZ, iterate font families index from
140  // mFamilyVec[mRanges[0xXXYY].start] to mFamilyVec[mRange[0xXXYY].end] instead
141  // of whole mFamilies. This vector contains indices into mFamilies. This
142  // vector can't be empty.
143  std::vector<Range> mRanges;
144  std::vector<uint8_t> mFamilyVec;
145 
146  // This vector has pointers to the font family instances which have cmap 14
147  // subtables.
148  std::vector<std::shared_ptr<FontFamily>> mVSFamilyVec;
149 
150  // Set of supported axes in this collection.
151  std::unordered_set<AxisTag> mSupportedAxes;
152 
153  // libtxt extension: Fallback font provider.
154  std::unique_ptr<FallbackFontProvider> mFallbackFontProvider;
155 
156  // libtxt extension: Fallback fonts discovered after this font collection
157  // was constructed.
158  mutable std::map<std::string, std::vector<std::shared_ptr<FontFamily>>>
159  mCachedFallbackFamilies;
160 };
161 
162 } // namespace minikin
163 
164 #endif // MINIKIN_FONT_COLLECTION_H
const std::unordered_set< AxisTag > & getSupportedTags() const
virtual const std::shared_ptr< FontFamily > & matchFallbackFont(uint32_t ch, std::string locale)=0
void itemize(const uint16_t *string, size_t string_length, FontStyle style, std::vector< Run > *result) const
FontCollection(const std::vector< std::shared_ptr< FontFamily >> &typefaces)
uint32_t getId() const
void set_fallback_font_provider(std::unique_ptr< FallbackFontProvider > ffp)
bool hasVariationSelector(uint32_t baseCodepoint, uint32_t variationSelector) const
std::shared_ptr< FontCollection > createCollectionWithVariation(const std::vector< FontVariation > &variations)
FakedFont baseFontFaked(FontStyle style)