Flutter Engine
font_collection.h
Go to the documentation of this file.
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 
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/text_style.h"
33 
34 #if FLUTTER_ENABLE_SKSHAPER
35 #include "third_party/skia/modules/skparagraph/include/FontCollection.h" // nogncheck
36 #endif
37 
38 namespace txt {
39 
40 class FontCollection : public std::enable_shared_from_this<FontCollection> {
41  public:
43 
45 
46  size_t GetFontManagersCount() const;
47 
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 #if FLUTTER_ENABLE_SKSHAPER
72 
73  // Construct a Skia text layout FontCollection based on this collection.
74  sk_sp<skia::textlayout::FontCollection> CreateSktFontCollection();
75 
76 #endif // FLUTTER_ENABLE_SKSHAPER
77 
78  private:
79  struct FamilyKey {
80  FamilyKey(const std::vector<std::string>& families, const std::string& loc);
81 
82  // Concatenated string with all font families.
83  std::string font_families;
84  std::string locale;
85 
86  bool operator==(const FamilyKey& other) const;
87 
88  struct Hasher {
89  size_t operator()(const FamilyKey& key) const;
90  };
91  };
92 
93  sk_sp<SkFontMgr> default_font_manager_;
94  sk_sp<SkFontMgr> asset_font_manager_;
95  sk_sp<SkFontMgr> dynamic_font_manager_;
96  sk_sp<SkFontMgr> test_font_manager_;
97  std::unordered_map<FamilyKey,
98  std::shared_ptr<minikin::FontCollection>,
100  font_collections_cache_;
101  // Cache that stores the results of MatchFallbackFont to ensure lag-free emoji
102  // font fallback matching.
103  std::unordered_map<uint32_t, const std::shared_ptr<minikin::FontFamily>*>
104  fallback_match_cache_;
105  std::unordered_map<std::string, std::shared_ptr<minikin::FontFamily>>
106  fallback_fonts_;
107  std::unordered_map<std::string, std::vector<std::string>>
108  fallback_fonts_for_locale_;
109  bool enable_font_fallback_;
110 
111 #if FLUTTER_ENABLE_SKSHAPER
112  // An equivalent font collection usable by the Skia text shaper library.
113  sk_sp<skia::textlayout::FontCollection> skt_collection_;
114 #endif
115 
116  // Performs the actual work of MatchFallbackFont. The result is cached in
117  // fallback_match_cache_.
118  const std::shared_ptr<minikin::FontFamily>& DoMatchFallbackFont(
119  uint32_t ch,
120  std::string locale);
121 
122  std::vector<sk_sp<SkFontMgr>> GetFontManagerOrder() const;
123 
124  std::shared_ptr<minikin::FontFamily> FindFontFamilyInManagers(
125  const std::string& family_name);
126 
127  std::shared_ptr<minikin::FontFamily> CreateMinikinFontFamily(
128  const sk_sp<SkFontMgr>& manager,
129  const std::string& family_name);
130 
131  // Sorts in-place a group of SkTypeface from an SkTypefaceSet into a
132  // reasonable order for future queries.
133  FRIEND_TEST(FontCollectionTest, CheckSkTypefacesSorting);
134  static void SortSkTypefaces(std::vector<sk_sp<SkTypeface>>& sk_typefaces);
135 
136  const std::shared_ptr<minikin::FontFamily>& GetFallbackFontFamily(
137  const sk_sp<SkFontMgr>& manager,
138  const std::string& family_name);
139 
141 };
142 
143 } // namespace txt
144 
145 #endif // LIB_TXT_SRC_FONT_COLLECTION_H_
void SetAssetFontManager(sk_sp< SkFontMgr > font_manager)
void SetDynamicFontManager(sk_sp< SkFontMgr > font_manager)
size_t operator()(const FamilyKey &key) const
const std::shared_ptr< minikin::FontFamily > & MatchFallbackFont(uint32_t ch, std::string locale)
void SetTestFontManager(sk_sp< SkFontMgr > font_manager)
bool operator==(const MockTexture::PaintCall &a, const MockTexture::PaintCall &b)
Definition: mock_texture.cc:21
size_t GetFontManagersCount() const
#define FML_DISALLOW_COPY_AND_ASSIGN(TypeName)
Definition: macros.h:27
std::shared_ptr< minikin::FontCollection > GetMinikinFontCollectionForFamilies(const std::vector< std::string > &font_families, const std::string &locale)
void SetDefaultFontManager(sk_sp< SkFontMgr > font_manager)