• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2017 Google Inc.
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 LIB_TXT_SRC_FONT_COLLECTION_H_
18 #define LIB_TXT_SRC_FONT_COLLECTION_H_
19 
20 #include <memory>
21 #include <mutex>
22 #include <set>
23 #include <string>
24 #include <unordered_map>
25 #include "flutter/fml/macros.h"
26 #include "minikin/FontCollection.h"
27 #include "minikin/FontFamily.h"
28 #include "third_party/googletest/googletest/include/gtest/gtest_prod.h"  // nogncheck
29 #include "third_party/skia/include/core/SkFontMgr.h"
30 #include "third_party/skia/include/core/SkRefCnt.h"
31 #include "txt/asset_font_manager.h"
32 #include "txt/platform.h"
33 #include "txt/text_style.h"
34 
35 #if FLUTTER_ENABLE_SKSHAPER
36 #include "third_party/skia/modules/skparagraph/include/FontCollection.h"
37 #endif
38 
39 namespace txt {
40 
41 class FontCollection : public std::enable_shared_from_this<FontCollection> {
42  public:
43   FontCollection();
44 
45   ~FontCollection();
46 
47   size_t GetFontManagersCount() const;
48 
49   void SetupDefaultFontManager();
50   void SetDefaultFontManager(sk_sp<SkFontMgr> font_manager);
51   void SetAssetFontManager(sk_sp<SkFontMgr> font_manager);
52   void SetDynamicFontManager(sk_sp<SkFontMgr> font_manager);
53   void SetTestFontManager(sk_sp<SkFontMgr> font_manager);
54 
55   std::shared_ptr<minikin::FontCollection> GetMinikinFontCollectionForFamilies(
56       const std::vector<std::string>& font_families,
57       const std::string& locale);
58 
59   // Provides a FontFamily that contains glyphs for ch. This caches previously
60   // matched fonts. Also see FontCollection::DoMatchFallbackFont.
61   const std::shared_ptr<minikin::FontFamily>& MatchFallbackFont(
62       uint32_t ch,
63       std::string locale);
64 
65   // Do not provide alternative fonts that can match characters which are
66   // missing from the requested font family.
67   void DisableFontFallback();
68 
69   // Remove all entries in the font family cache.
70   void ClearFontFamilyCache();
71 
72   // Vary font collection with font weight scale.
73   void VaryFontCollectionWithFontWeightScale(float font_weight_scale);
74 
75   void LoadSystemFont();
76 
77   void SetIsZawgyiMyanmar(bool is_zawgyi_myanmar);
78 
79 #if FLUTTER_ENABLE_SKSHAPER
80 
81   // Construct a Skia text layout FontCollection based on this collection.
82   sk_sp<skia::textlayout::FontCollection> CreateSktFontCollection();
83 
84 #endif  // FLUTTER_ENABLE_SKSHAPER
85 
86 #if defined(OHOS_PLATFORM) && !defined(OHOS_STANDARD_SYSTEM)
87     const std::shared_ptr<minikin::FontFamily>& MatchFallbackFontFromHwFont(
88             uint32_t ch,
89             std::string locale);
90 #endif
91 
92  private:
93   struct FamilyKey {
94     FamilyKey(const std::vector<std::string>& families, const std::string& loc);
95 
96     // Concatenated string with all font families.
97     std::string font_families;
98     std::string locale;
99 
100     bool operator==(const FamilyKey& other) const;
101 
102     struct Hasher {
103       size_t operator()(const FamilyKey& key) const;
104     };
105   };
106 
107   sk_sp<SkFontMgr> default_font_manager_;
108   sk_sp<SkFontMgr> asset_font_manager_;
109   sk_sp<SkFontMgr> dynamic_font_manager_;
110   sk_sp<SkFontMgr> test_font_manager_;
111   std::unordered_map<FamilyKey,
112                      std::shared_ptr<minikin::FontCollection>,
113                      FamilyKey::Hasher>
114       font_collections_cache_;
115   // Cache that stores the results of MatchFallbackFont to ensure lag-free emoji
116   // font fallback matching.
117   std::unordered_map<uint32_t, const std::shared_ptr<minikin::FontFamily>*>
118       fallback_match_cache_;
119   std::unordered_map<std::string, std::shared_ptr<minikin::FontFamily>>
120       fallback_fonts_;
121   std::unordered_map<std::string, std::set<std::string>>
122       fallback_fonts_for_locale_;
123   bool enable_font_fallback_;
124   bool is_zawgyi_myanmar_ = false; // whether encoding of Burmese is zawgyi, not unicode.
125   float font_weight_scale_ = 1.0f;
126   std::vector<FamilyKey> varied_fonts_;
127 
128   std::mutex mutex_;
129   mutable std::mutex fontManagerMutex_;
130 
131   // Performs the actual work of MatchFallbackFont. The result is cached in
132   // fallback_match_cache_.
133   const std::shared_ptr<minikin::FontFamily>& DoMatchFallbackFont(
134       uint32_t ch,
135       std::string locale);
136 
137   std::vector<sk_sp<SkFontMgr>> GetFontManagerOrder() const;
138 
139   std::shared_ptr<minikin::FontFamily> FindFontFamilyInManagers(
140       const std::string& family_name);
141 
142   std::shared_ptr<minikin::FontFamily> CreateMinikinFontFamily(
143       const sk_sp<SkFontMgr>& manager,
144       const std::string& family_name);
145 
146   const std::shared_ptr<minikin::FontFamily>& GetFallbackFontFamily(
147       const sk_sp<SkFontMgr>& manager,
148       const std::string& family_name);
149 
150   sk_sp<SkFontMgr> GetDefaultFontManagerSafely() const;
151 
152 #if defined(OHOS_PLATFORM) && !defined(OHOS_STANDARD_SYSTEM)
153   std::shared_ptr<minikin::FontCollection>
154   GetMinikinFontCollectionForFamiliesWithVariation(
155       const std::vector<std::string>& font_families,
156       const std::string& locale);
157 
158   std::vector<std::pair<sk_sp<SkFontMgr>, txt::FontManagerType>>
159   GetFontManagerOrderWithType() const;
160 
161   std::shared_ptr<minikin::FontFamily> FindFontFamilyInManagersWithType(
162       const std::string& family_name);
163 
164   std::shared_ptr<minikin::FontFamily> CreateMinikinFontFamilyForOHOS(
165       const sk_sp<SkFontMgr>& manager,
166       const std::string& family_name);
167 
168   std::shared_ptr<minikin::FontFamily> CreateMinikinFontFamilyExceptOHOS(
169       const sk_sp<SkFontMgr>& manager,
170       const std::string& family_name);
171 
172   void VaryTypeface(sk_sp<SkTypeface>& typeface, float wght);
173 
174   // Provides a FontFamily that contains glyphs for ch. This caches previously
175   // matched fonts. Also see FontCollection::DoMatchFallbackFontWithVariation.
176   const std::shared_ptr<minikin::FontFamily>& MatchFallbackFontWithVariation(
177       uint32_t ch,
178       std::string locale);
179 
180   // Performs the actual work of MatchFallbackFont. The result is cached in
181   // fallback_match_cache_.
182   const std::shared_ptr<minikin::FontFamily>& DoMatchFallbackFontWithVariation(
183       uint32_t ch,
184       std::string locale);
185 
186   const std::shared_ptr<minikin::FontFamily>& GetFallbackFontFamilyForOHOS(
187       const sk_sp<SkFontMgr>& manager,
188       const std::string& family_name);
189 
190   const std::shared_ptr<minikin::FontFamily>& DoMatchFallbackFontFromHwFont(
191       uint32_t ch,
192       std::string locale);
193 #endif
194 
195   FML_DISALLOW_COPY_AND_ASSIGN(FontCollection);
196 };
197 
198 }  // namespace txt
199 
200 #endif  // LIB_TXT_SRC_FONT_COLLECTION_H_
201