Flutter Engine
flutter::FontCollection Class Reference

#include <font_collection.h>

Public Member Functions

 FontCollection ()
 
 ~FontCollection ()
 
std::shared_ptr< txt::FontCollectionGetFontCollection () const
 
void SetupDefaultFontManager ()
 
void RegisterFonts (std::shared_ptr< AssetManager > asset_manager)
 
void RegisterTestFonts ()
 
void LoadFontFromList (const uint8_t *font_data, int length, std::string family_name)
 

Static Public Member Functions

static void RegisterNatives (tonic::DartLibraryNatives *natives)
 

Detailed Description

Definition at line 22 of file font_collection.h.

Constructor & Destructor Documentation

◆ FontCollection()

flutter::FontCollection::FontCollection ( )

Definition at line 50 of file font_collection.cc.

51  : collection_(std::make_shared<txt::FontCollection>()) {
52  dynamic_font_manager_ = sk_make_sp<txt::DynamicFontManager>();
53  collection_->SetDynamicFontManager(dynamic_font_manager_);
54 }

◆ ~FontCollection()

flutter::FontCollection::~FontCollection ( )

Definition at line 56 of file font_collection.cc.

56  {
57  collection_.reset();
58  SkGraphics::PurgeFontCache();
59 }

Member Function Documentation

◆ GetFontCollection()

std::shared_ptr< txt::FontCollection > flutter::FontCollection::GetFontCollection ( ) const

Definition at line 67 of file font_collection.cc.

Referenced by flutter::decodeStrut().

67  {
68  return collection_;
69 }

◆ LoadFontFromList()

void flutter::FontCollection::LoadFontFromList ( const uint8_t *  font_data,
int  length,
std::string  family_name 
)

Definition at line 159 of file font_collection.cc.

References length, and txt::TypefaceFontAssetProvider::RegisterTypeface().

161  {
162  std::unique_ptr<SkStreamAsset> font_stream =
163  std::make_unique<SkMemoryStream>(font_data, length, true);
164  sk_sp<SkTypeface> typeface =
165  SkTypeface::MakeFromStream(std::move(font_stream));
166  txt::TypefaceFontAssetProvider& font_provider =
167  dynamic_font_manager_->font_provider();
168  if (family_name.empty()) {
169  font_provider.RegisterTypeface(typeface);
170  } else {
171  font_provider.RegisterTypeface(typeface, family_name);
172  }
173  collection_->ClearFontFamilyCache();
174 }
size_t length
void RegisterTypeface(sk_sp< SkTypeface > typeface)

◆ RegisterFonts()

void flutter::FontCollection::RegisterFonts ( std::shared_ptr< AssetManager asset_manager)

Definition at line 75 of file font_collection.cc.

References FML_DLOG.

Referenced by flutter::Engine::UpdateAssetManager().

76  {
77  std::unique_ptr<fml::Mapping> manifest_mapping =
78  asset_manager->GetAsMapping("FontManifest.json");
79  if (manifest_mapping == nullptr) {
80  FML_DLOG(WARNING) << "Could not find the font manifest in the asset store.";
81  return;
82  }
83 
84  rapidjson::Document document;
85  static_assert(sizeof(decltype(document)::Ch) == sizeof(uint8_t), "");
86  document.Parse(reinterpret_cast<const decltype(document)::Ch*>(
87  manifest_mapping->GetMapping()),
88  manifest_mapping->GetSize());
89 
90  if (document.HasParseError()) {
91  FML_DLOG(WARNING) << "Error parsing the font manifest in the asset store.";
92  return;
93  }
94 
95  // Structure described in https://flutter.io/custom-fonts/
96 
97  if (!document.IsArray()) {
98  return;
99  }
100 
101  auto font_provider =
102  std::make_unique<AssetManagerFontProvider>(asset_manager);
103 
104  for (const auto& family : document.GetArray()) {
105  auto family_name = family.FindMember("family");
106  if (family_name == family.MemberEnd() || !family_name->value.IsString()) {
107  continue;
108  }
109 
110  auto family_fonts = family.FindMember("fonts");
111  if (family_fonts == family.MemberEnd() || !family_fonts->value.IsArray()) {
112  continue;
113  }
114 
115  for (const auto& family_font : family_fonts->value.GetArray()) {
116  if (!family_font.IsObject()) {
117  continue;
118  }
119 
120  auto font_asset = family_font.FindMember("asset");
121  if (font_asset == family_font.MemberEnd() ||
122  !font_asset->value.IsString()) {
123  continue;
124  }
125 
126  // TODO(chinmaygarde): Handle weights and styles.
127  font_provider->RegisterAsset(family_name->value.GetString(),
128  font_asset->value.GetString());
129  }
130  }
131 
132  collection_->SetAssetFontManager(
133  sk_make_sp<txt::AssetFontManager>(std::move(font_provider)));
134 }
#define FML_DLOG(severity)
Definition: logging.h:85

◆ RegisterNatives()

void flutter::FontCollection::RegisterNatives ( tonic::DartLibraryNatives natives)
static

Definition at line 61 of file font_collection.cc.

References tonic::DartLibraryNatives::Register().

Referenced by flutter::DartUI::InitForGlobal().

61  {
62  natives->Register({
63  {"loadFontFromList", _LoadFontFromList, 3, true},
64  });
65 }
void Register(std::initializer_list< Entry > entries)

◆ RegisterTestFonts()

void flutter::FontCollection::RegisterTestFonts ( )

Definition at line 136 of file font_collection.cc.

References flutter::GetTestFontData(), and flutter::GetTestFontFamilyNames().

Referenced by flutter::Engine::UpdateAssetManager().

136  {
137  std::vector<sk_sp<SkTypeface>> test_typefaces;
138  std::vector<std::unique_ptr<SkStreamAsset>> font_data = GetTestFontData();
139  for (auto& font : font_data) {
140  test_typefaces.push_back(SkTypeface::MakeFromStream(std::move(font)));
141  }
142 
143  std::unique_ptr<txt::TypefaceFontAssetProvider> font_provider =
144  std::make_unique<txt::TypefaceFontAssetProvider>();
145 
146  size_t index = 0;
147  std::vector<std::string> names = GetTestFontFamilyNames();
148  for (sk_sp<SkTypeface> typeface : test_typefaces) {
149  font_provider->RegisterTypeface(std::move(typeface), names[index]);
150  index++;
151  }
152 
153  collection_->SetTestFontManager(
154  sk_make_sp<txt::TestFontManager>(std::move(font_provider), names));
155 
156  collection_->DisableFontFallback();
157 }
std::vector< std::unique_ptr< SkStreamAsset > > GetTestFontData()
std::vector< std::string > GetTestFontFamilyNames()

◆ SetupDefaultFontManager()

void flutter::FontCollection::SetupDefaultFontManager ( )

Definition at line 71 of file font_collection.cc.

Referenced by flutter::Engine::SetupDefaultFontManager().

71  {
72  collection_->SetupDefaultFontManager();
73 }

The documentation for this class was generated from the following files: