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