Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
font_collection.cc
Go to the documentation of this file.
1// Copyright 2013 The Flutter Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "flutter/lib/ui/text/font_collection.h"
6
7#include <mutex>
8
9#include "flutter/lib/ui/text/asset_manager_font_provider.h"
10#include "flutter/lib/ui/ui_dart_state.h"
11#include "flutter/lib/ui/window/platform_configuration.h"
12#include "flutter/runtime/test_font_data.h"
13#include "rapidjson/document.h"
14#include "rapidjson/rapidjson.h"
24#include "txt/platform.h"
26
27#if FML_OS_MACOSX || FML_OS_IOS
28#include "txt/platform_mac.h"
29#endif
30
31namespace flutter {
32
34 : collection_(std::make_shared<txt::FontCollection>()) {
35 dynamic_font_manager_ = sk_make_sp<txt::DynamicFontManager>();
36 collection_->SetDynamicFontManager(dynamic_font_manager_);
37}
38
40 collection_.reset();
42}
43
44std::shared_ptr<txt::FontCollection> FontCollection::GetFontCollection() const {
45 return collection_;
46}
47
49 uint32_t font_initialization_data) {
50 collection_->SetupDefaultFontManager(font_initialization_data);
51}
52
53// Font manifest yaml format:
54//
55// flutter:
56// fonts:
57// - family: Raleway
58// fonts:
59// - asset: fonts/Raleway-Regular.ttf
60// - asset: fonts/Raleway-Italic.ttf
61// style: italic
62// - family: RobotoMono
63// fonts:
64// - asset: fonts/RobotoMono-Regular.ttf
65// - asset: fonts/RobotoMono-Bold.ttf
66// weight: 700
67//
68// Structure described in https://docs.flutter.dev/cookbook/design/fonts
70 const std::shared_ptr<AssetManager>& asset_manager) {
71#if FML_OS_MACOSX || FML_OS_IOS
72 RegisterSystemFonts(*dynamic_font_manager_);
73#endif
74 std::unique_ptr<fml::Mapping> manifest_mapping =
75 asset_manager->GetAsMapping("FontManifest.json");
76 if (manifest_mapping == nullptr) {
77 FML_DLOG(WARNING) << "Could not find the font manifest in the asset store.";
78 return;
79 }
80
81 rapidjson::Document document;
82 static_assert(sizeof(decltype(document)::Ch) == sizeof(uint8_t), "");
83 document.Parse(reinterpret_cast<const decltype(document)::Ch*>(
84 manifest_mapping->GetMapping()),
85 manifest_mapping->GetSize());
86
87 if (document.HasParseError()) {
88 FML_DLOG(WARNING) << "Error parsing the font manifest in the asset store.";
89 return;
90 }
91
92 if (!document.IsArray()) {
93 return;
94 }
95
96 auto font_provider =
97 std::make_unique<AssetManagerFontProvider>(asset_manager);
98
99 for (const auto& family : document.GetArray()) {
100 auto family_name = family.FindMember("family");
101 if (family_name == family.MemberEnd() || !family_name->value.IsString()) {
102 continue;
103 }
104
105 auto family_fonts = family.FindMember("fonts");
106 if (family_fonts == family.MemberEnd() || !family_fonts->value.IsArray()) {
107 continue;
108 }
109
110 for (const auto& family_font : family_fonts->value.GetArray()) {
111 if (!family_font.IsObject()) {
112 continue;
113 }
114
115 auto font_asset = family_font.FindMember("asset");
116 if (font_asset == family_font.MemberEnd() ||
117 !font_asset->value.IsString()) {
118 continue;
119 }
120
121 // TODO(chinmaygarde): Handle weights and styles.
122 font_provider->RegisterAsset(family_name->value.GetString(),
123 font_asset->value.GetString());
124 }
125 }
126
127 collection_->SetAssetFontManager(
128 sk_make_sp<txt::AssetFontManager>(std::move(font_provider)));
129}
130
132 std::vector<sk_sp<SkTypeface>> test_typefaces = GetTestFontData();
133 std::unique_ptr<txt::TypefaceFontAssetProvider> font_provider =
134 std::make_unique<txt::TypefaceFontAssetProvider>();
135
136 size_t index = 0;
137 std::vector<std::string> names = GetTestFontFamilyNames();
138 for (sk_sp<SkTypeface> typeface : test_typefaces) {
139 if (typeface) {
140 font_provider->RegisterTypeface(std::move(typeface), names[index]);
141 }
142 index++;
143 }
144
145 collection_->SetTestFontManager(
146 sk_make_sp<txt::TestFontManager>(std::move(font_provider), names));
147
148 collection_->DisableFontFallback();
149}
150
153 const std::string& family_name) {
154 tonic::Uint8List font_data(font_data_handle);
156 FontCollection& font_collection = UIDartState::Current()
158 ->client()
160
161 std::unique_ptr<SkStreamAsset> font_stream = std::make_unique<SkMemoryStream>(
162 font_data.data(), font_data.num_elements(), true);
164 sk_sp<SkTypeface> typeface = font_mgr->makeFromStream(std::move(font_stream));
165 txt::TypefaceFontAssetProvider& font_provider =
166 font_collection.dynamic_font_manager_->font_provider();
167 if (family_name.empty()) {
168 font_provider.RegisterTypeface(typeface);
169 } else {
170 font_provider.RegisterTypeface(typeface, family_name);
171 }
172 font_collection.collection_->ClearFontFamilyCache();
173
174 font_data.Release();
176}
177
178} // namespace flutter
static void PurgeFontCache()
std::shared_ptr< txt::FontCollection > GetFontCollection() const
virtual void RegisterFonts(const std::shared_ptr< AssetManager > &asset_manager)
static void LoadFontFromList(Dart_Handle font_data_handle, Dart_Handle callback, const std::string &family_name)
void SetupDefaultFontManager(uint32_t font_initialization_data)
virtual FontCollection & GetFontCollection()=0
Returns the current collection of fonts available on the platform.
PlatformConfigurationClient * client() const
Access to the platform configuration client (which typically is implemented by the RuntimeController)...
PlatformConfiguration * platform_configuration() const
static UIDartState * Current()
static void ThrowIfUIOperationsProhibited()
TypefaceFontAssetProvider & font_provider() const
void RegisterTypeface(sk_sp< SkTypeface > typeface)
struct _Dart_Handle * Dart_Handle
Definition dart_api.h:258
FlKeyEvent uint64_t FlKeyResponderAsyncCallback callback
#define FML_DLOG(severity)
Definition logging.h:102
std::vector< std::string > GetTestFontFamilyNames()
std::vector< sk_sp< SkTypeface > > GetTestFontData()
Definition ref_ptr.h:256
Dart_Handle ToDart(const T &object)
Dart_Handle DartInvoke(Dart_Handle closure, std::initializer_list< Dart_Handle > args)
sk_sp< SkFontMgr > GetDefaultFontManager(uint32_t font_initialization_data)
Definition platform.cc:17