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/text_style.h" |
32 | |
33 | #if FLUTTER_ENABLE_SKSHAPER |
34 | #include "third_party/skia/modules/skparagraph/include/FontCollection.h" // nogncheck |
35 | #endif |
36 | |
37 | namespace txt { |
38 | |
39 | class FontCollection : public std::enable_shared_from_this<FontCollection> { |
40 | public: |
41 | FontCollection(); |
42 | |
43 | ~FontCollection(); |
44 | |
45 | size_t GetFontManagersCount() const; |
46 | |
47 | void SetupDefaultFontManager(); |
48 | void SetDefaultFontManager(sk_sp<SkFontMgr> font_manager); |
49 | void SetAssetFontManager(sk_sp<SkFontMgr> font_manager); |
50 | void SetDynamicFontManager(sk_sp<SkFontMgr> font_manager); |
51 | void SetTestFontManager(sk_sp<SkFontMgr> font_manager); |
52 | |
53 | std::shared_ptr<minikin::FontCollection> GetMinikinFontCollectionForFamilies( |
54 | const std::vector<std::string>& font_families, |
55 | const std::string& locale); |
56 | |
57 | // Provides a FontFamily that contains glyphs for ch. This caches previously |
58 | // matched fonts. Also see FontCollection::DoMatchFallbackFont. |
59 | const std::shared_ptr<minikin::FontFamily>& MatchFallbackFont( |
60 | uint32_t ch, |
61 | std::string locale); |
62 | |
63 | // Do not provide alternative fonts that can match characters which are |
64 | // missing from the requested font family. |
65 | void DisableFontFallback(); |
66 | |
67 | // Remove all entries in the font family cache. |
68 | void ClearFontFamilyCache(); |
69 | |
70 | #if FLUTTER_ENABLE_SKSHAPER |
71 | |
72 | // Construct a Skia text layout FontCollection based on this collection. |
73 | sk_sp<skia::textlayout::FontCollection> CreateSktFontCollection(); |
74 | |
75 | #endif // FLUTTER_ENABLE_SKSHAPER |
76 | |
77 | private: |
78 | struct FamilyKey { |
79 | FamilyKey(const std::vector<std::string>& families, const std::string& loc); |
80 | |
81 | // Concatenated string with all font families. |
82 | std::string font_families; |
83 | std::string locale; |
84 | |
85 | bool operator==(const FamilyKey& other) const; |
86 | |
87 | struct Hasher { |
88 | size_t operator()(const FamilyKey& key) const; |
89 | }; |
90 | }; |
91 | |
92 | sk_sp<SkFontMgr> default_font_manager_; |
93 | sk_sp<SkFontMgr> asset_font_manager_; |
94 | sk_sp<SkFontMgr> dynamic_font_manager_; |
95 | sk_sp<SkFontMgr> test_font_manager_; |
96 | std::unordered_map<FamilyKey, |
97 | std::shared_ptr<minikin::FontCollection>, |
98 | FamilyKey::Hasher> |
99 | font_collections_cache_; |
100 | // Cache that stores the results of MatchFallbackFont to ensure lag-free emoji |
101 | // font fallback matching. |
102 | std::unordered_map<uint32_t, const std::shared_ptr<minikin::FontFamily>*> |
103 | fallback_match_cache_; |
104 | std::unordered_map<std::string, std::shared_ptr<minikin::FontFamily>> |
105 | fallback_fonts_; |
106 | std::unordered_map<std::string, std::vector<std::string>> |
107 | fallback_fonts_for_locale_; |
108 | bool enable_font_fallback_; |
109 | |
110 | #if FLUTTER_ENABLE_SKSHAPER |
111 | // An equivalent font collection usable by the Skia text shaper library. |
112 | sk_sp<skia::textlayout::FontCollection> skt_collection_; |
113 | #endif |
114 | |
115 | // Performs the actual work of MatchFallbackFont. The result is cached in |
116 | // fallback_match_cache_. |
117 | const std::shared_ptr<minikin::FontFamily>& DoMatchFallbackFont( |
118 | uint32_t ch, |
119 | std::string locale); |
120 | |
121 | std::vector<sk_sp<SkFontMgr>> GetFontManagerOrder() const; |
122 | |
123 | std::shared_ptr<minikin::FontFamily> FindFontFamilyInManagers( |
124 | const std::string& family_name); |
125 | |
126 | std::shared_ptr<minikin::FontFamily> CreateMinikinFontFamily( |
127 | const sk_sp<SkFontMgr>& manager, |
128 | const std::string& family_name); |
129 | |
130 | const std::shared_ptr<minikin::FontFamily>& GetFallbackFontFamily( |
131 | const sk_sp<SkFontMgr>& manager, |
132 | const std::string& family_name); |
133 | |
134 | FML_DISALLOW_COPY_AND_ASSIGN(FontCollection); |
135 | }; |
136 | |
137 | } // namespace txt |
138 | |
139 | #endif // LIB_TXT_SRC_FONT_COLLECTION_H_ |
140 | |