Flutter Engine
flutter::FontCollection Class Reference

#include <font_collection.h>

Public Member Functions

 FontCollection ()
 
 ~FontCollection ()
 
std::shared_ptr< txt::FontCollectionGetFontCollection () const
 
void SetupDefaultFontManager (uint32_t font_initialization_data)
 
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 160 of file font_collection.cc.

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

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

◆ RegisterFonts()

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

Definition at line 76 of file font_collection.cc.

References FML_DLOG.

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

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

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

◆ SetupDefaultFontManager()

void flutter::FontCollection::SetupDefaultFontManager ( uint32_t  font_initialization_data)

Definition at line 71 of file font_collection.cc.

72  {
73  collection_->SetupDefaultFontManager(font_initialization_data);
74 }

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