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