|
7 | 7 | #include <mutex> |
8 | 8 |
|
9 | 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/window.h" |
10 | 12 | #include "flutter/runtime/test_font_data.h" |
11 | 13 | #include "rapidjson/document.h" |
12 | 14 | #include "rapidjson/rapidjson.h" |
13 | 15 | #include "third_party/skia/include/core/SkFontMgr.h" |
14 | 16 | #include "third_party/skia/include/core/SkGraphics.h" |
15 | 17 | #include "third_party/skia/include/core/SkStream.h" |
16 | 18 | #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" |
17 | 23 | #include "txt/asset_font_manager.h" |
18 | 24 | #include "txt/test_font_manager.h" |
19 | | -#include "txt/typeface_font_asset_provider.h" |
20 | 25 |
|
21 | 26 | namespace blink { |
22 | 27 |
|
| 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 | + |
23 | 47 | FontCollection::FontCollection() |
24 | 48 | : collection_(std::make_shared<txt::FontCollection>()) { |
25 | 49 | collection_->SetDefaultFontManager(SkFontMgr::RefDefault()); |
| 50 | + |
| 51 | + dynamic_font_manager_ = sk_make_sp<txt::DynamicFontManager>(); |
| 52 | + collection_->SetDynamicFontManager(dynamic_font_manager_); |
26 | 53 | } |
27 | 54 |
|
28 | 55 | FontCollection::~FontCollection() { |
29 | 56 | collection_.reset(); |
30 | 57 | SkGraphics::PurgeFontCache(); |
31 | 58 | } |
32 | 59 |
|
| 60 | +void FontCollection::RegisterNatives(tonic::DartLibraryNatives* natives) { |
| 61 | + natives->Register({ |
| 62 | + {"loadFontFromList", _LoadFontFromList, 3, true}, |
| 63 | + }); |
| 64 | +} |
| 65 | + |
33 | 66 | std::shared_ptr<txt::FontCollection> FontCollection::GetFontCollection() const { |
34 | 67 | return collection_; |
35 | 68 | } |
@@ -110,4 +143,20 @@ void FontCollection::RegisterTestFonts() { |
110 | 143 | collection_->DisableFontFallback(); |
111 | 144 | } |
112 | 145 |
|
| 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 | + |
113 | 162 | } // namespace blink |
0 commit comments