Flutter Engine
The Flutter Engine
asset_manager_font_provider.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/asset_manager_font_provider.h"
6
7#include <utility>
8
9#include "flutter/fml/logging.h"
15#include "txt/platform.h"
16
17namespace flutter {
18
19namespace {
20
21void MappingReleaseProc(const void* ptr, void* context) {
22 delete reinterpret_cast<fml::Mapping*>(context);
23}
24
25} // anonymous namespace
26
28 std::shared_ptr<AssetManager> asset_manager)
29 : asset_manager_(std::move(asset_manager)) {}
30
32
33// |FontAssetProvider|
35 return family_names_.size();
36}
37
38// |FontAssetProvider|
39std::string AssetManagerFontProvider::GetFamilyName(int index) const {
40 FML_DCHECK(index >= 0 && static_cast<size_t>(index) < family_names_.size());
41 return family_names_[index];
42}
43
44// |FontAssetProvider|
46 const std::string& family_name) {
47 auto found = registered_families_.find(CanonicalFamilyName(family_name));
48 if (found == registered_families_.end()) {
49 return nullptr;
50 }
51 return found->second;
52}
53
54void AssetManagerFontProvider::RegisterAsset(const std::string& family_name,
55 const std::string& asset) {
56 std::string canonical_name = CanonicalFamilyName(family_name);
57 auto family_it = registered_families_.find(canonical_name);
58
59 if (family_it == registered_families_.end()) {
60 family_names_.push_back(family_name);
61 auto value = std::make_pair(
62 canonical_name,
63 sk_make_sp<AssetManagerFontStyleSet>(asset_manager_, family_name));
64 family_it = registered_families_.emplace(value).first;
65 }
66
67 family_it->second->registerAsset(asset);
68}
69
71 std::shared_ptr<AssetManager> asset_manager,
72 std::string family_name)
73 : asset_manager_(std::move(asset_manager)),
74 family_name_(std::move(family_name)) {}
75
77
78void AssetManagerFontStyleSet::registerAsset(const std::string& asset) {
79 assets_.emplace_back(asset);
80}
81
83 return assets_.size();
84}
85
87 SkFontStyle* style,
88 SkString* name) {
89 FML_DCHECK(index < static_cast<int>(assets_.size()));
90 if (style) {
91 sk_sp<SkTypeface> typeface(createTypeface(index));
92 if (typeface) {
93 *style = typeface->fontStyle();
94 }
95 }
96 if (name) {
97 *name = family_name_.c_str();
98 }
99}
100
102 size_t index = i;
103 if (index >= assets_.size()) {
104 return nullptr;
105 }
106
107 TypefaceAsset& asset = assets_[index];
108 if (!asset.typeface) {
109 std::unique_ptr<fml::Mapping> asset_mapping =
110 asset_manager_->GetAsMapping(asset.asset);
111 if (asset_mapping == nullptr) {
112 return nullptr;
113 }
114
115 fml::Mapping* asset_mapping_ptr = asset_mapping.release();
117 asset_mapping_ptr->GetMapping(), asset_mapping_ptr->GetSize(),
118 MappingReleaseProc, asset_mapping_ptr);
119 std::unique_ptr<SkMemoryStream> stream = SkMemoryStream::Make(asset_data);
120
122 // Ownership of the stream is transferred.
123 asset.typeface = font_mgr->makeFromStream(std::move(stream));
124 if (!asset.typeface) {
125 FML_DLOG(ERROR) << "Unable to load font asset for family: "
126 << family_name_;
127 return nullptr;
128 }
129 }
130
131 return CreateTypefaceRet(SkRef(asset.typeface.get()));
132}
133
135 -> MatchStyleRet {
136 return matchStyleCSS3(pattern);
137}
138
139AssetManagerFontStyleSet::TypefaceAsset::TypefaceAsset(std::string a)
140 : asset(std::move(a)) {}
141
142AssetManagerFontStyleSet::TypefaceAsset::TypefaceAsset(
143 const AssetManagerFontStyleSet::TypefaceAsset& other) = default;
144
145AssetManagerFontStyleSet::TypefaceAsset::~TypefaceAsset() = default;
146
147} // namespace flutter
static T * SkRef(T *obj)
Definition: SkRefCnt.h:132
static sk_sp< SkData > MakeWithProc(const void *ptr, size_t length, ReleaseProc proc, void *ctx)
Definition: SkData.cpp:128
sk_sp< SkTypeface > makeFromStream(std::unique_ptr< SkStreamAsset >, int ttcIndex=0) const
Definition: SkFontMgr.cpp:127
static std::unique_ptr< SkMemoryStream > Make(sk_sp< SkData > data)
Definition: SkStream.cpp:314
SkFontStyle fontStyle() const
Definition: SkTypeface.h:55
std::string GetFamilyName(int index) const override
void RegisterAsset(const std::string &family_name, const std::string &asset)
sk_sp< SkFontStyleSet > MatchFamily(const std::string &family_name) override
AssetManagerFontProvider(std::shared_ptr< AssetManager > asset_manager)
MatchStyleRet matchStyle(const SkFontStyle &pattern) override
decltype(std::declval< SkFontStyleSet >().matchStyle(std::declval< SkFontStyle >())) MatchStyleRet
decltype(std::declval< SkFontStyleSet >().createTypeface(0)) CreateTypefaceRet
void registerAsset(const std::string &asset)
void getStyle(int index, SkFontStyle *, SkString *style) override
CreateTypefaceRet createTypeface(int index) override
AssetManagerFontStyleSet(std::shared_ptr< AssetManager > asset_manager, std::string family_name)
virtual const uint8_t * GetMapping() const =0
virtual size_t GetSize() const =0
static std::string CanonicalFamilyName(std::string family_name)
struct MyStruct a[10]
uint8_t value
#define FML_DLOG(severity)
Definition: logging.h:102
#define FML_DCHECK(condition)
Definition: logging.h:103
DEF_SWITCHES_START aot vmservice shared library name
Definition: switches.h:32
Definition: ref_ptr.h:256
sk_sp< SkFontMgr > GetDefaultFontManager(uint32_t font_initialization_data)
Definition: platform.cc:17
#define ERROR(message)
Definition: elf_loader.cc:260