Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
SkFontMgr_custom.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2006 The Android Open Source Project
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
20
21#include <limits>
22#include <memory>
23
24using namespace skia_private;
25
26class SkData;
27
28SkTypeface_Custom::SkTypeface_Custom(const SkFontStyle& style, bool isFixedPitch,
29 bool sysFont, SkString familyName, int index)
30 : INHERITED(style, isFixedPitch)
31 , fIsSysFont(sysFont), fFamilyName(std::move(familyName)), fIndex(index)
32{ }
33
34bool SkTypeface_Custom::isSysFont() const { return fIsSysFont; }
35
37 *familyName = fFamilyName;
38}
39
41 desc->setFamilyName(fFamilyName.c_str());
42 desc->setStyle(this->fontStyle());
44 *isLocal = !this->isSysFont();
45}
46
47int SkTypeface_Custom::getIndex() const { return fIndex; }
48
49
51
52std::unique_ptr<SkStreamAsset> SkTypeface_Empty::onOpenStream(int*) const { return nullptr; }
53
57
58std::unique_ptr<SkFontData> SkTypeface_Empty::onMakeFontData() const { return nullptr; }
59
60SkTypeface_File::SkTypeface_File(const SkFontStyle& style, bool isFixedPitch, bool sysFont,
61 SkString familyName, const char path[], int index)
62 : INHERITED(style, isFixedPitch, sysFont, std::move(familyName), index)
63 , fPath(path)
64{ }
65
66std::unique_ptr<SkStreamAsset> SkTypeface_File::onOpenStream(int* ttcIndex) const {
67 *ttcIndex = this->getIndex();
68 return SkStream::MakeFromFile(fPath.c_str());
69}
70
72 SkFontStyle style = this->fontStyle();
73 std::unique_ptr<SkFontData> data = this->cloneFontData(args, &style);
74 if (!data) {
75 return nullptr;
76 }
77
78 SkString familyName;
79 this->getFamilyName(&familyName);
80
81 return sk_make_sp<SkTypeface_FreeTypeStream>(
82 std::move(data), familyName, style, this->isFixedPitch());
83}
84
85std::unique_ptr<SkFontData> SkTypeface_File::onMakeFontData() const {
86 int index;
87 std::unique_ptr<SkStreamAsset> stream(this->onOpenStream(&index));
88 if (!stream) {
89 return nullptr;
90 }
91 return std::make_unique<SkFontData>(std::move(stream), index, 0, nullptr, 0, nullptr, 0);
92}
93
94///////////////////////////////////////////////////////////////////////////////
95
97 : fFamilyName(std::move(familyName)) {}
98
100 fStyles.emplace_back(std::move(typeface));
101}
102
104 return fStyles.size();
105}
106
108 SkASSERT(index < fStyles.size());
109 if (style) {
110 *style = fStyles[index]->fontStyle();
111 }
112 if (name) {
113 name->reset();
114 }
115}
116
118 SkASSERT(index < fStyles.size());
119 return fStyles[index];
120}
121
123 return this->matchStyleCSS3(pattern);
124}
125
127
129 : fDefaultFamily(nullptr)
130 , fScanner(std::make_unique<SkFontScanner_FreeType>()) {
131
132 loader.loadSystemFonts(fScanner.get(), &fFamilies);
133
134 // Try to pick a default font.
135 static const char* defaultNames[] = {
136 "Arial", "Verdana", "Times New Roman", "Droid Sans", "DejaVu Serif", nullptr
137 };
138 for (size_t i = 0; i < std::size(defaultNames); ++i) {
139 sk_sp<SkFontStyleSet> set(this->onMatchFamily(defaultNames[i]));
140 if (nullptr == set) {
141 continue;
142 }
143
147 if (nullptr == tf) {
148 continue;
149 }
150
151 fDefaultFamily = set;
152 break;
153 }
154 if (nullptr == fDefaultFamily) {
155 fDefaultFamily = fFamilies[0];
156 }
157}
158
160 return fFamilies.size();
161}
162
163void SkFontMgr_Custom::onGetFamilyName(int index, SkString* familyName) const {
164 SkASSERT(index < fFamilies.size());
165 familyName->set(fFamilies[index]->getFamilyName());
166}
167
169 SkASSERT(index < fFamilies.size());
170 return fFamilies[index];
171}
172
174 for (int i = 0; i < fFamilies.size(); ++i) {
175 if (fFamilies[i]->getFamilyName().equals(familyName)) {
176 return fFamilies[i];
177 }
178 }
179 return nullptr;
180}
181
183 const SkFontStyle& fontStyle) const
184{
185 sk_sp<SkFontStyleSet> sset(this->matchFamily(familyName));
186 return sset->matchStyle(fontStyle);
187}
188
190 const char familyName[], const SkFontStyle&,
191 const char* bcp47[], int bcp47Count,
192 SkUnichar) const
193{
194 return nullptr;
195}
196
198 return this->makeFromStream(std::make_unique<SkMemoryStream>(std::move(data)), ttcIndex);
199}
200
201sk_sp<SkTypeface> SkFontMgr_Custom::onMakeFromStreamIndex(std::unique_ptr<SkStreamAsset> stream,
202 int ttcIndex) const {
203 return this->makeFromStream(std::move(stream), SkFontArguments().setCollectionIndex(ttcIndex));
204}
205
206sk_sp<SkTypeface> SkFontMgr_Custom::onMakeFromStreamArgs(std::unique_ptr<SkStreamAsset> stream,
207 const SkFontArguments& args) const {
208 return SkTypeface_FreeType::MakeFromStream(std::move(stream), args);
209}
210
211sk_sp<SkTypeface> SkFontMgr_Custom::onMakeFromFile(const char path[], int ttcIndex) const {
212 std::unique_ptr<SkStreamAsset> stream = SkStream::MakeFromFile(path);
213 return stream ? this->makeFromStream(std::move(stream), ttcIndex) : nullptr;
214}
215
217 SkFontStyle style) const {
219
220 if (familyName) {
221 tf = this->onMatchFamilyStyle(familyName, style);
222 }
223
224 if (!tf) {
225 tf = fDefaultFamily->matchStyle(style);
226 }
227
228 return tf;
229}
SkPath fPath
#define SkASSERT(cond)
Definition SkAssert.h:116
static bool equals(T *a, T *b)
sk_sp< T > sk_ref_sp(T *obj)
Definition SkRefCnt.h:381
int32_t SkUnichar
Definition SkTypes.h:175
const void * data() const
Definition SkData.h:37
void setFactoryId(SkTypeface::FactoryId factoryId)
virtual void loadSystemFonts(const SkFontScanner *, Families *) const =0
sk_sp< SkTypeface > onMakeFromStreamIndex(std::unique_ptr< SkStreamAsset >, int ttcIndex) const override
int onCountFamilies() const override
sk_sp< SkFontStyleSet > onCreateStyleSet(int index) const override
SkFontMgr_Custom(const SystemFontLoader &loader)
sk_sp< SkTypeface > onMatchFamilyStyleCharacter(const char familyName[], const SkFontStyle &, const char *bcp47[], int bcp47Count, SkUnichar character) const override
sk_sp< SkTypeface > onMatchFamilyStyle(const char familyName[], const SkFontStyle &fontStyle) const override
sk_sp< SkTypeface > onLegacyMakeTypeface(const char familyName[], SkFontStyle style) const override
sk_sp< SkTypeface > onMakeFromData(sk_sp< SkData > data, int ttcIndex) const override
sk_sp< SkTypeface > onMakeFromFile(const char path[], int ttcIndex) const override
sk_sp< SkTypeface > onMakeFromStreamArgs(std::unique_ptr< SkStreamAsset >, const SkFontArguments &) const override
void onGetFamilyName(int index, SkString *familyName) const override
sk_sp< SkFontStyleSet > onMatchFamily(const char familyName[]) const override
sk_sp< SkTypeface > makeFromStream(std::unique_ptr< SkStreamAsset >, int ttcIndex=0) const
void getFamilyName(int index, SkString *familyName) const
Definition SkFontMgr.cpp:97
sk_sp< SkFontStyleSet > matchFamily(const char familyName[]) const
void appendTypeface(sk_sp< SkTypeface > typeface)
void getStyle(int index, SkFontStyle *style, SkString *name) override
SkFontStyleSet_Custom(SkString familyName)
sk_sp< SkTypeface > matchStyle(const SkFontStyle &pattern) override
sk_sp< SkTypeface > createTypeface(int index) override
sk_sp< SkTypeface > matchStyleCSS3(const SkFontStyle &pattern)
virtual sk_sp< SkTypeface > matchStyle(const SkFontStyle &pattern)=0
static std::unique_ptr< SkStreamAsset > MakeFromFile(const char path[])
Definition SkStream.cpp:922
void set(const SkString &src)
Definition SkString.h:186
const char * c_str() const
Definition SkString.h:133
void onGetFamilyName(SkString *familyName) const override
void onGetFontDescriptor(SkFontDescriptor *desc, bool *isLocal) const override
SkTypeface_Custom(const SkFontStyle &style, bool isFixedPitch, bool sysFont, SkString familyName, int index)
sk_sp< SkTypeface > onMakeClone(const SkFontArguments &args) const override
std::unique_ptr< SkStreamAsset > onOpenStream(int *) const override
std::unique_ptr< SkFontData > onMakeFontData() const override
SkTypeface_File(const SkFontStyle &style, bool isFixedPitch, bool sysFont, SkString familyName, const char path[], int index)
sk_sp< SkTypeface > onMakeClone(const SkFontArguments &args) const override
std::unique_ptr< SkStreamAsset > onOpenStream(int *ttcIndex) const override
std::unique_ptr< SkFontData > onMakeFontData() const override
std::unique_ptr< SkFontData > cloneFontData(const SkFontArguments &, SkFontStyle *style) const
static sk_sp< SkTypeface > MakeFromStream(std::unique_ptr< SkStreamAsset >, const SkFontArguments &)
void getFamilyName(SkString *name) const
SkFontStyle fontStyle() const
Definition SkTypeface.h:55
bool isFixedPitch() const
Definition SkTypeface.h:68
SkFourByteTag FactoryId
Definition SkTypeface.h:335
void reset(int n)
Definition SkTArray.h:139
int size() const
Definition SkTArray.h:416
T & emplace_back(Args &&... args)
Definition SkTArray.h:243
G_BEGIN_DECLS G_MODULE_EXPORT FlValue * args
const char * name
Definition fuchsia.cc:50
Definition ref_ptr.h:256