Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit f2e7441

Browse files
An API for loading fonts from a buffer provided by the application (#6508)
1 parent 05aac0f commit f2e7441

File tree

7 files changed

+96
-2
lines changed

7 files changed

+96
-2
lines changed

lib/ui/dart_ui.cc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include "flutter/lib/ui/painting/vertices.h"
2525
#include "flutter/lib/ui/semantics/semantics_update.h"
2626
#include "flutter/lib/ui/semantics/semantics_update_builder.h"
27+
#include "flutter/lib/ui/text/font_collection.h"
2728
#include "flutter/lib/ui/text/paragraph.h"
2829
#include "flutter/lib/ui/text/paragraph_builder.h"
2930
#include "flutter/lib/ui/window/window.h"
@@ -72,6 +73,7 @@ void DartUI::InitForGlobal() {
7273
Codec::RegisterNatives(g_natives);
7374
DartRuntimeHooks::RegisterNatives(g_natives);
7475
EngineLayer::RegisterNatives(g_natives);
76+
FontCollection::RegisterNatives(g_natives);
7577
FrameInfo::RegisterNatives(g_natives);
7678
ImageFilter::RegisterNatives(g_natives);
7779
ImageShader::RegisterNatives(g_natives);

lib/ui/text.dart

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1087,3 +1087,16 @@ class ParagraphBuilder extends NativeFieldWrapperClass2 {
10871087
/// cannot be used further.
10881088
Paragraph build() native 'ParagraphBuilder_build';
10891089
}
1090+
1091+
/// Loads a font from a buffer and makes it available for rendering text.
1092+
///
1093+
/// * `list`: A list of bytes containing the font file.
1094+
/// * `fontFamily`: The family name used to identify the font in text styles.
1095+
/// If this is not provided, then the family name will be extracted from the font file.
1096+
Future<void> loadFontFromList(Uint8List list, {String fontFamily}) {
1097+
return _futurize(
1098+
(_Callback<void> callback) => _loadFontFromList(list, callback, fontFamily)
1099+
);
1100+
}
1101+
1102+
String _loadFontFromList(Uint8List list, _Callback<void> callback, String fontFamily) native 'loadFontFromList';

lib/ui/text/font_collection.cc

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,29 +7,62 @@
77
#include <mutex>
88

99
#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/window.h"
1012
#include "flutter/runtime/test_font_data.h"
1113
#include "rapidjson/document.h"
1214
#include "rapidjson/rapidjson.h"
1315
#include "third_party/skia/include/core/SkFontMgr.h"
1416
#include "third_party/skia/include/core/SkGraphics.h"
1517
#include "third_party/skia/include/core/SkStream.h"
1618
#include "third_party/skia/include/core/SkTypeface.h"
19+
#include "third_party/tonic/dart_args.h"
20+
#include "third_party/tonic/dart_library_natives.h"
21+
#include "third_party/tonic/logging/dart_invoke.h"
22+
#include "third_party/tonic/typed_data/uint8_list.h"
1723
#include "txt/asset_font_manager.h"
1824
#include "txt/test_font_manager.h"
19-
#include "txt/typeface_font_asset_provider.h"
2025

2126
namespace blink {
2227

28+
namespace {
29+
30+
void LoadFontFromList(tonic::Uint8List& font_data,
31+
Dart_Handle callback,
32+
std::string family_name) {
33+
FontCollection& font_collection =
34+
UIDartState::Current()->window()->client()->GetFontCollection();
35+
font_collection.LoadFontFromList(font_data.data(), font_data.num_elements(),
36+
family_name);
37+
font_data.Release();
38+
tonic::DartInvoke(callback, {tonic::ToDart(0)});
39+
}
40+
41+
void _LoadFontFromList(Dart_NativeArguments args) {
42+
tonic::DartCallStatic(LoadFontFromList, args);
43+
}
44+
45+
} // namespace
46+
2347
FontCollection::FontCollection()
2448
: collection_(std::make_shared<txt::FontCollection>()) {
2549
collection_->SetDefaultFontManager(SkFontMgr::RefDefault());
50+
51+
dynamic_font_manager_ = sk_make_sp<txt::DynamicFontManager>();
52+
collection_->SetDynamicFontManager(dynamic_font_manager_);
2653
}
2754

2855
FontCollection::~FontCollection() {
2956
collection_.reset();
3057
SkGraphics::PurgeFontCache();
3158
}
3259

60+
void FontCollection::RegisterNatives(tonic::DartLibraryNatives* natives) {
61+
natives->Register({
62+
{"loadFontFromList", _LoadFontFromList, 3, true},
63+
});
64+
}
65+
3366
std::shared_ptr<txt::FontCollection> FontCollection::GetFontCollection() const {
3467
return collection_;
3568
}
@@ -110,4 +143,20 @@ void FontCollection::RegisterTestFonts() {
110143
collection_->DisableFontFallback();
111144
}
112145

146+
void FontCollection::LoadFontFromList(const uint8_t* font_data,
147+
int length,
148+
std::string family_name) {
149+
std::unique_ptr<SkStreamAsset> font_stream =
150+
std::make_unique<SkMemoryStream>(font_data, length, true);
151+
sk_sp<SkTypeface> typeface =
152+
SkTypeface::MakeFromStream(std::move(font_stream));
153+
txt::TypefaceFontAssetProvider& font_provider =
154+
dynamic_font_manager_->font_provider();
155+
if (family_name.empty()) {
156+
font_provider.RegisterTypeface(typeface);
157+
} else {
158+
font_provider.RegisterTypeface(typeface, family_name);
159+
}
160+
}
161+
113162
} // namespace blink

lib/ui/text/font_collection.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@
1313
#include "flutter/fml/memory/ref_ptr.h"
1414
#include "txt/font_collection.h"
1515

16+
namespace tonic {
17+
class DartLibraryNatives;
18+
} // namespace tonic
19+
1620
namespace blink {
1721

1822
class FontCollection {
@@ -21,14 +25,21 @@ class FontCollection {
2125

2226
~FontCollection();
2327

28+
static void RegisterNatives(tonic::DartLibraryNatives* natives);
29+
2430
std::shared_ptr<txt::FontCollection> GetFontCollection() const;
2531

2632
void RegisterFonts(fml::RefPtr<AssetManager> asset_manager);
2733

2834
void RegisterTestFonts();
2935

36+
void LoadFontFromList(const uint8_t* font_data,
37+
int length,
38+
std::string family_name);
39+
3040
private:
3141
std::shared_ptr<txt::FontCollection> collection_;
42+
sk_sp<txt::DynamicFontManager> dynamic_font_manager_;
3243

3344
FML_DISALLOW_COPY_AND_ASSIGN(FontCollection);
3445
};

third_party/txt/src/txt/asset_font_manager.h

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "third_party/skia/include/core/SkFontMgr.h"
2323
#include "third_party/skia/include/core/SkStream.h"
2424
#include "txt/font_asset_provider.h"
25+
#include "txt/typeface_font_asset_provider.h"
2526

2627
namespace txt {
2728

@@ -35,9 +36,9 @@ class AssetFontManager : public SkFontMgr {
3536
// |SkFontMgr|
3637
SkFontStyleSet* onMatchFamily(const char familyName[]) const override;
3738

38-
private:
3939
std::unique_ptr<FontAssetProvider> font_provider_;
4040

41+
private:
4142
// |SkFontMgr|
4243
int onCountFamilies() const override;
4344

@@ -84,6 +85,16 @@ class AssetFontManager : public SkFontMgr {
8485
FML_DISALLOW_COPY_AND_ASSIGN(AssetFontManager);
8586
};
8687

88+
class DynamicFontManager : public AssetFontManager {
89+
public:
90+
DynamicFontManager()
91+
: AssetFontManager(std::make_unique<TypefaceFontAssetProvider>()) {}
92+
93+
TypefaceFontAssetProvider& font_provider() {
94+
return static_cast<TypefaceFontAssetProvider&>(*font_provider_);
95+
}
96+
};
97+
8798
} // namespace txt
8899

89100
#endif // TXT_ASSET_FONT_MANAGER_H_

third_party/txt/src/txt/font_collection.cc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,10 @@ void FontCollection::SetAssetFontManager(sk_sp<SkFontMgr> font_manager) {
8484
asset_font_manager_ = font_manager;
8585
}
8686

87+
void FontCollection::SetDynamicFontManager(sk_sp<SkFontMgr> font_manager) {
88+
dynamic_font_manager_ = font_manager;
89+
}
90+
8791
void FontCollection::SetTestFontManager(sk_sp<SkFontMgr> font_manager) {
8892
test_font_manager_ = font_manager;
8993
}
@@ -93,6 +97,8 @@ std::vector<sk_sp<SkFontMgr>> FontCollection::GetFontManagerOrder() const {
9397
std::vector<sk_sp<SkFontMgr>> order;
9498
if (test_font_manager_)
9599
order.push_back(test_font_manager_);
100+
if (dynamic_font_manager_)
101+
order.push_back(dynamic_font_manager_);
96102
if (asset_font_manager_)
97103
order.push_back(asset_font_manager_);
98104
if (default_font_manager_)

third_party/txt/src/txt/font_collection.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ class FontCollection : public std::enable_shared_from_this<FontCollection> {
4242

4343
void SetDefaultFontManager(sk_sp<SkFontMgr> font_manager);
4444
void SetAssetFontManager(sk_sp<SkFontMgr> font_manager);
45+
void SetDynamicFontManager(sk_sp<SkFontMgr> font_manager);
4546
void SetTestFontManager(sk_sp<SkFontMgr> font_manager);
4647

4748
std::shared_ptr<minikin::FontCollection> GetMinikinFontCollectionForFamily(
@@ -73,6 +74,7 @@ class FontCollection : public std::enable_shared_from_this<FontCollection> {
7374

7475
sk_sp<SkFontMgr> default_font_manager_;
7576
sk_sp<SkFontMgr> asset_font_manager_;
77+
sk_sp<SkFontMgr> dynamic_font_manager_;
7678
sk_sp<SkFontMgr> test_font_manager_;
7779
std::unordered_map<FamilyKey,
7880
std::shared_ptr<minikin::FontCollection>,

0 commit comments

Comments
 (0)