1 /*
2 * Copyright 2013 Google Inc.
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
8 #include "include/core/SkFontMgr.h"
9 #include "include/core/SkFontStyle.h"
10 #include "include/core/SkString.h"
11 #include "include/core/SkTypeface.h"
12 #include "include/ports/SkFontConfigInterface.h"
13 #include "include/ports/SkFontMgr_FontConfigInterface.h"
14 #include "include/private/SkMutex.h"
15 #include "src/core/SkFontDescriptor.h"
16 #include "src/core/SkMakeUnique.h"
17 #include "src/core/SkResourceCache.h"
18 #include "src/core/SkTypefaceCache.h"
19 #include "src/ports/SkFontConfigTypeface.h"
20 #include <new>
21
onOpenStream(int * ttcIndex) const22 std::unique_ptr<SkStreamAsset> SkTypeface_FCI::onOpenStream(int* ttcIndex) const {
23 *ttcIndex = this->getIdentity().fTTCIndex;
24
25 if (fFontData) {
26 SkStreamAsset* stream = fFontData->getStream();
27 if (!stream) {
28 return nullptr;
29 }
30 return stream->duplicate();
31 }
32
33 return std::unique_ptr<SkStreamAsset>(fFCI->openStream(this->getIdentity()));
34 }
35
onMakeFontData() const36 std::unique_ptr<SkFontData> SkTypeface_FCI::onMakeFontData() const {
37 if (fFontData) {
38 return skstd::make_unique<SkFontData>(*fFontData);
39 }
40
41 const SkFontConfigInterface::FontIdentity& id = this->getIdentity();
42 return skstd::make_unique<SkFontData>(std::unique_ptr<SkStreamAsset>(fFCI->openStream(id)),
43 id.fTTCIndex, nullptr, 0);
44 }
45
onGetFontDescriptor(SkFontDescriptor * desc,bool * isLocalStream) const46 void SkTypeface_FCI::onGetFontDescriptor(SkFontDescriptor* desc, bool* isLocalStream) const {
47 SkString name;
48 this->getFamilyName(&name);
49 desc->setFamilyName(name.c_str());
50 desc->setStyle(this->fontStyle());
51 *isLocalStream = SkToBool(fFontData);
52 }
53
54 ///////////////////////////////////////////////////////////////////////////////
55
56 class SkFontStyleSet_FCI : public SkFontStyleSet {
57 public:
SkFontStyleSet_FCI()58 SkFontStyleSet_FCI() {}
59
count()60 int count() override { return 0; }
getStyle(int index,SkFontStyle *,SkString * style)61 void getStyle(int index, SkFontStyle*, SkString* style) override { SkASSERT(false); }
createTypeface(int index)62 SkTypeface* createTypeface(int index) override { SkASSERT(false); return nullptr; }
matchStyle(const SkFontStyle & pattern)63 SkTypeface* matchStyle(const SkFontStyle& pattern) override { return nullptr; }
64 };
65
66 ///////////////////////////////////////////////////////////////////////////////
67
68 class SkFontRequestCache {
69 public:
70 struct Request : public SkResourceCache::Key {
71 private:
RequestSkFontRequestCache::Request72 Request(const char* name, size_t nameLen, const SkFontStyle& style) : fStyle(style) {
73 /** Pointer to just after the last field of this class. */
74 char* content = const_cast<char*>(SkTAfter<const char>(&this->fStyle));
75
76 // No holes.
77 SkASSERT(SkTAddOffset<char>(this, sizeof(SkResourceCache::Key) + keySize) == content);
78
79 // Has a size divisible by size of uint32_t.
80 SkASSERT((content - reinterpret_cast<char*>(this)) % sizeof(uint32_t) == 0);
81
82 size_t contentLen = SkAlign4(nameLen);
83 sk_careful_memcpy(content, name, nameLen);
84 sk_bzero(content + nameLen, contentLen - nameLen);
85 this->init(nullptr, 0, keySize + contentLen);
86 }
87 const SkFontStyle fStyle;
88 /** The sum of the sizes of the fields of this class. */
89 static const size_t keySize = sizeof(fStyle);
90
91 public:
CreateSkFontRequestCache::Request92 static Request* Create(const char* name, const SkFontStyle& style) {
93 size_t nameLen = name ? strlen(name) : 0;
94 size_t contentLen = SkAlign4(nameLen);
95 char* storage = new char[sizeof(Request) + contentLen];
96 return new (storage) Request(name, nameLen, style);
97 }
operator deleteSkFontRequestCache::Request98 void operator delete(void* storage) {
99 delete[] reinterpret_cast<char*>(storage);
100 }
101 };
102
103
104 private:
105 struct Result : public SkResourceCache::Rec {
ResultSkFontRequestCache::Result106 Result(Request* request, sk_sp<SkTypeface> typeface)
107 : fRequest(request), fFace(std::move(typeface)) {}
108 Result(Result&&) = default;
109 Result& operator=(Result&&) = default;
110
getKeySkFontRequestCache::Result111 const Key& getKey() const override { return *fRequest; }
bytesUsedSkFontRequestCache::Result112 size_t bytesUsed() const override { return fRequest->size() + sizeof(fFace); }
getCategorySkFontRequestCache::Result113 const char* getCategory() const override { return "request_cache"; }
diagnostic_only_getDiscardableSkFontRequestCache::Result114 SkDiscardableMemory* diagnostic_only_getDiscardable() const override { return nullptr; }
115
116 std::unique_ptr<Request> fRequest;
117 sk_sp<SkTypeface> fFace;
118 };
119
120 SkResourceCache fCachedResults;
121
122 public:
SkFontRequestCache(size_t maxSize)123 SkFontRequestCache(size_t maxSize) : fCachedResults(maxSize) {}
124
125 /** Takes ownership of request. It will be deleted when no longer needed. */
add(sk_sp<SkTypeface> face,Request * request)126 void add(sk_sp<SkTypeface> face, Request* request) {
127 fCachedResults.add(new Result(request, std::move(face)));
128 }
129 /** Does not take ownership of request. */
findAndRef(Request * request)130 sk_sp<SkTypeface> findAndRef(Request* request) {
131 sk_sp<SkTypeface> face;
132 fCachedResults.find(*request, [](const SkResourceCache::Rec& rec, void* context) -> bool {
133 const Result& result = static_cast<const Result&>(rec);
134 sk_sp<SkTypeface>* face = static_cast<sk_sp<SkTypeface>*>(context);
135
136 *face = result.fFace;
137 return true;
138 }, &face);
139 return face;
140 }
141 };
142
143 ///////////////////////////////////////////////////////////////////////////////
144
find_by_FontIdentity(SkTypeface * cachedTypeface,void * ctx)145 static bool find_by_FontIdentity(SkTypeface* cachedTypeface, void* ctx) {
146 typedef SkFontConfigInterface::FontIdentity FontIdentity;
147 SkTypeface_FCI* cachedFCTypeface = static_cast<SkTypeface_FCI*>(cachedTypeface);
148 FontIdentity* identity = static_cast<FontIdentity*>(ctx);
149
150 return cachedFCTypeface->getIdentity() == *identity;
151 }
152
153 ///////////////////////////////////////////////////////////////////////////////
154
155 class SkFontMgr_FCI : public SkFontMgr {
156 sk_sp<SkFontConfigInterface> fFCI;
157 SkTypeface_FreeType::Scanner fScanner;
158
159 mutable SkMutex fMutex;
160 mutable SkTypefaceCache fTFCache;
161
162 // The value of maxSize here is a compromise between cache hits and cache size.
163 // See https://crbug.com/424082#63 for reason for current size.
164 static const size_t kMaxSize = 1 << 15;
165 mutable SkFontRequestCache fCache;
166
167 public:
SkFontMgr_FCI(sk_sp<SkFontConfigInterface> fci)168 SkFontMgr_FCI(sk_sp<SkFontConfigInterface> fci)
169 : fFCI(std::move(fci))
170 , fCache(kMaxSize)
171 {}
172
173 protected:
onCountFamilies() const174 int onCountFamilies() const override {
175 SK_ABORT("Not implemented.");
176 }
177
onGetFamilyName(int index,SkString * familyName) const178 void onGetFamilyName(int index, SkString* familyName) const override {
179 SK_ABORT("Not implemented.");
180 }
181
onCreateStyleSet(int index) const182 SkFontStyleSet* onCreateStyleSet(int index) const override {
183 SK_ABORT("Not implemented.");
184 }
185
onMatchFamily(const char familyName[]) const186 SkFontStyleSet* onMatchFamily(const char familyName[]) const override {
187 SK_ABORT("Not implemented.");
188 }
189
onMatchFamilyStyle(const char requestedFamilyName[],const SkFontStyle & requestedStyle) const190 SkTypeface* onMatchFamilyStyle(const char requestedFamilyName[],
191 const SkFontStyle& requestedStyle) const override
192 {
193 SkAutoMutexExclusive ama(fMutex);
194
195 SkFontConfigInterface::FontIdentity identity;
196 SkString outFamilyName;
197 SkFontStyle outStyle;
198 if (!fFCI->matchFamilyName(requestedFamilyName, requestedStyle,
199 &identity, &outFamilyName, &outStyle))
200 {
201 return nullptr;
202 }
203
204 // Check if a typeface with this FontIdentity is already in the FontIdentity cache.
205 sk_sp<SkTypeface> face = fTFCache.findByProcAndRef(find_by_FontIdentity, &identity);
206 if (!face) {
207 face.reset(SkTypeface_FCI::Create(fFCI, identity, std::move(outFamilyName), outStyle));
208 // Add this FontIdentity to the FontIdentity cache.
209 fTFCache.add(face);
210 }
211 return face.release();
212 }
213
onMatchFamilyStyleCharacter(const char familyName[],const SkFontStyle &,const char * bcp47[],int bcp47Count,SkUnichar character) const214 SkTypeface* onMatchFamilyStyleCharacter(const char familyName[], const SkFontStyle&,
215 const char* bcp47[], int bcp47Count,
216 SkUnichar character) const override {
217 SK_ABORT("Not implemented.");
218 }
219
onMatchFaceStyle(const SkTypeface *,const SkFontStyle &) const220 SkTypeface* onMatchFaceStyle(const SkTypeface*, const SkFontStyle&) const override {
221 SK_ABORT("Not implemented.");
222 }
223
onMakeFromData(sk_sp<SkData> data,int ttcIndex) const224 sk_sp<SkTypeface> onMakeFromData(sk_sp<SkData> data, int ttcIndex) const override {
225 return this->onMakeFromStreamIndex(SkMemoryStream::Make(std::move(data)), ttcIndex);
226 }
227
onMakeFromStreamIndex(std::unique_ptr<SkStreamAsset> stream,int ttcIndex) const228 sk_sp<SkTypeface> onMakeFromStreamIndex(std::unique_ptr<SkStreamAsset> stream,
229 int ttcIndex) const override {
230 const size_t length = stream->getLength();
231 if (!length) {
232 return nullptr;
233 }
234 if (length >= 1024 * 1024 * 1024) {
235 return nullptr; // don't accept too large fonts (>= 1GB) for safety.
236 }
237
238 // TODO should the caller give us the style or should we get it from freetype?
239 SkString name;
240 SkFontStyle style;
241 bool isFixedPitch = false;
242 if (!fScanner.scanFont(stream.get(), 0, &name, &style, &isFixedPitch, nullptr)) {
243 return nullptr;
244 }
245
246 auto fontData = skstd::make_unique<SkFontData>(std::move(stream), ttcIndex, nullptr, 0);
247 return sk_sp<SkTypeface>(SkTypeface_FCI::Create(std::move(fontData), std::move(name),
248 style, isFixedPitch));
249 }
250
onMakeFromStreamArgs(std::unique_ptr<SkStreamAsset> stream,const SkFontArguments & args) const251 sk_sp<SkTypeface> onMakeFromStreamArgs(std::unique_ptr<SkStreamAsset> stream,
252 const SkFontArguments& args) const override {
253 using Scanner = SkTypeface_FreeType::Scanner;
254 const size_t length = stream->getLength();
255 if (!length) {
256 return nullptr;
257 }
258 if (length >= 1024 * 1024 * 1024) {
259 return nullptr; // don't accept too large fonts (>= 1GB) for safety.
260 }
261
262 bool isFixedPitch;
263 SkFontStyle style;
264 SkString name;
265 Scanner::AxisDefinitions axisDefinitions;
266 if (!fScanner.scanFont(stream.get(), args.getCollectionIndex(),
267 &name, &style, &isFixedPitch, &axisDefinitions))
268 {
269 return nullptr;
270 }
271
272 SkAutoSTMalloc<4, SkFixed> axisValues(axisDefinitions.count());
273 Scanner::computeAxisValues(axisDefinitions, args.getVariationDesignPosition(),
274 axisValues, name);
275
276 auto fontData = skstd::make_unique<SkFontData>(std::move(stream),
277 args.getCollectionIndex(),
278 axisValues.get(),
279 axisDefinitions.count());
280 return sk_sp<SkTypeface>(SkTypeface_FCI::Create(std::move(fontData), std::move(name),
281 style, isFixedPitch));
282 }
283
onMakeFromFile(const char path[],int ttcIndex) const284 sk_sp<SkTypeface> onMakeFromFile(const char path[], int ttcIndex) const override {
285 std::unique_ptr<SkStreamAsset> stream = SkStream::MakeFromFile(path);
286 return stream ? this->makeFromStream(std::move(stream), ttcIndex) : nullptr;
287 }
288
onLegacyMakeTypeface(const char requestedFamilyName[],SkFontStyle requestedStyle) const289 sk_sp<SkTypeface> onLegacyMakeTypeface(const char requestedFamilyName[],
290 SkFontStyle requestedStyle) const override
291 {
292 SkAutoMutexExclusive ama(fMutex);
293
294 // Check if this request is already in the request cache.
295 using Request = SkFontRequestCache::Request;
296 std::unique_ptr<Request> request(Request::Create(requestedFamilyName, requestedStyle));
297 sk_sp<SkTypeface> face = fCache.findAndRef(request.get());
298 if (face) {
299 return sk_sp<SkTypeface>(face);
300 }
301
302 SkFontConfigInterface::FontIdentity identity;
303 SkString outFamilyName;
304 SkFontStyle outStyle;
305 if (!fFCI->matchFamilyName(requestedFamilyName, requestedStyle,
306 &identity, &outFamilyName, &outStyle))
307 {
308 return nullptr;
309 }
310
311 // Check if a typeface with this FontIdentity is already in the FontIdentity cache.
312 face = fTFCache.findByProcAndRef(find_by_FontIdentity, &identity);
313 if (!face) {
314 face.reset(SkTypeface_FCI::Create(fFCI, identity, std::move(outFamilyName), outStyle));
315 // Add this FontIdentity to the FontIdentity cache.
316 fTFCache.add(face);
317 }
318 // Add this request to the request cache.
319 fCache.add(face, request.release());
320
321 return face;
322 }
323 };
324
SkFontMgr_New_FCI(sk_sp<SkFontConfigInterface> fci)325 SK_API sk_sp<SkFontMgr> SkFontMgr_New_FCI(sk_sp<SkFontConfigInterface> fci) {
326 SkASSERT(fci);
327 return sk_make_sp<SkFontMgr_FCI>(std::move(fci));
328 }
329