blob: 9378d5d3f5962acd2fed9333f58ee1865dc27889 [file] [log] [blame]
bungeman7d0e3bc2016-08-02 07:07:33 -07001/*
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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#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"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050016#include "src/core/SkResourceCache.h"
17#include "src/core/SkTypefaceCache.h"
18#include "src/ports/SkFontConfigTypeface.h"
Mike Klein79aea6a2018-06-11 10:45:26 -040019#include <new>
bungeman7d0e3bc2016-08-02 07:07:33 -070020
Ben Wagner4212a7d2019-02-25 14:27:46 -050021std::unique_ptr<SkStreamAsset> SkTypeface_FCI::onOpenStream(int* ttcIndex) const {
bungeman7d0e3bc2016-08-02 07:07:33 -070022 *ttcIndex = this->getIdentity().fTTCIndex;
23
bungeman4bb029c2016-09-01 08:52:29 -070024 if (fFontData) {
25 SkStreamAsset* stream = fFontData->getStream();
26 if (!stream) {
27 return nullptr;
28 }
Ben Wagner4212a7d2019-02-25 14:27:46 -050029 return stream->duplicate();
bungeman7d0e3bc2016-08-02 07:07:33 -070030 }
31
Ben Wagner4212a7d2019-02-25 14:27:46 -050032 return std::unique_ptr<SkStreamAsset>(fFCI->openStream(this->getIdentity()));
bungeman7d0e3bc2016-08-02 07:07:33 -070033}
34
bungemanf93d7112016-09-16 06:24:20 -070035std::unique_ptr<SkFontData> SkTypeface_FCI::onMakeFontData() const {
bungeman4bb029c2016-09-01 08:52:29 -070036 if (fFontData) {
Mike Kleinf46d5ca2019-12-11 10:45:01 -050037 return std::make_unique<SkFontData>(*fFontData);
bungeman4bb029c2016-09-01 08:52:29 -070038 }
39
40 const SkFontConfigInterface::FontIdentity& id = this->getIdentity();
Mike Kleinf46d5ca2019-12-11 10:45:01 -050041 return std::make_unique<SkFontData>(std::unique_ptr<SkStreamAsset>(fFCI->openStream(id)),
bungemanf93d7112016-09-16 06:24:20 -070042 id.fTTCIndex, nullptr, 0);
bungeman4bb029c2016-09-01 08:52:29 -070043}
44
bungeman7d0e3bc2016-08-02 07:07:33 -070045void SkTypeface_FCI::onGetFontDescriptor(SkFontDescriptor* desc, bool* isLocalStream) const {
46 SkString name;
47 this->getFamilyName(&name);
48 desc->setFamilyName(name.c_str());
49 desc->setStyle(this->fontStyle());
bungeman4bb029c2016-09-01 08:52:29 -070050 *isLocalStream = SkToBool(fFontData);
bungeman7d0e3bc2016-08-02 07:07:33 -070051}
52
53///////////////////////////////////////////////////////////////////////////////
54
55class SkFontStyleSet_FCI : public SkFontStyleSet {
56public:
57 SkFontStyleSet_FCI() {}
58
59 int count() override { return 0; }
60 void getStyle(int index, SkFontStyle*, SkString* style) override { SkASSERT(false); }
61 SkTypeface* createTypeface(int index) override { SkASSERT(false); return nullptr; }
62 SkTypeface* matchStyle(const SkFontStyle& pattern) override { return nullptr; }
63};
64
65///////////////////////////////////////////////////////////////////////////////
66
67class SkFontRequestCache {
68public:
69 struct Request : public SkResourceCache::Key {
70 private:
71 Request(const char* name, size_t nameLen, const SkFontStyle& style) : fStyle(style) {
72 /** Pointer to just after the last field of this class. */
73 char* content = const_cast<char*>(SkTAfter<const char>(&this->fStyle));
74
75 // No holes.
76 SkASSERT(SkTAddOffset<char>(this, sizeof(SkResourceCache::Key) + keySize) == content);
77
78 // Has a size divisible by size of uint32_t.
79 SkASSERT((content - reinterpret_cast<char*>(this)) % sizeof(uint32_t) == 0);
80
81 size_t contentLen = SkAlign4(nameLen);
82 sk_careful_memcpy(content, name, nameLen);
83 sk_bzero(content + nameLen, contentLen - nameLen);
84 this->init(nullptr, 0, keySize + contentLen);
85 }
86 const SkFontStyle fStyle;
87 /** The sum of the sizes of the fields of this class. */
88 static const size_t keySize = sizeof(fStyle);
89
90 public:
91 static Request* Create(const char* name, const SkFontStyle& style) {
92 size_t nameLen = name ? strlen(name) : 0;
93 size_t contentLen = SkAlign4(nameLen);
94 char* storage = new char[sizeof(Request) + contentLen];
95 return new (storage) Request(name, nameLen, style);
96 }
97 void operator delete(void* storage) {
98 delete[] reinterpret_cast<char*>(storage);
99 }
100 };
101
102
103private:
104 struct Result : public SkResourceCache::Rec {
Ben Wagner68efb212019-03-04 16:15:40 -0500105 Result(Request* request, sk_sp<SkTypeface> typeface)
106 : fRequest(request), fFace(std::move(typeface)) {}
bungeman7d0e3bc2016-08-02 07:07:33 -0700107 Result(Result&&) = default;
108 Result& operator=(Result&&) = default;
109
110 const Key& getKey() const override { return *fRequest; }
111 size_t bytesUsed() const override { return fRequest->size() + sizeof(fFace); }
112 const char* getCategory() const override { return "request_cache"; }
113 SkDiscardableMemory* diagnostic_only_getDiscardable() const override { return nullptr; }
114
Ben Wagner145dbcd2016-11-03 14:40:50 -0400115 std::unique_ptr<Request> fRequest;
Hal Canary67b39de2016-11-07 11:47:44 -0500116 sk_sp<SkTypeface> fFace;
bungeman7d0e3bc2016-08-02 07:07:33 -0700117 };
118
119 SkResourceCache fCachedResults;
120
121public:
122 SkFontRequestCache(size_t maxSize) : fCachedResults(maxSize) {}
123
124 /** Takes ownership of request. It will be deleted when no longer needed. */
Ben Wagner68efb212019-03-04 16:15:40 -0500125 void add(sk_sp<SkTypeface> face, Request* request) {
126 fCachedResults.add(new Result(request, std::move(face)));
bungeman7d0e3bc2016-08-02 07:07:33 -0700127 }
128 /** Does not take ownership of request. */
Ben Wagner68efb212019-03-04 16:15:40 -0500129 sk_sp<SkTypeface> findAndRef(Request* request) {
130 sk_sp<SkTypeface> face;
bungeman7d0e3bc2016-08-02 07:07:33 -0700131 fCachedResults.find(*request, [](const SkResourceCache::Rec& rec, void* context) -> bool {
132 const Result& result = static_cast<const Result&>(rec);
Ben Wagner68efb212019-03-04 16:15:40 -0500133 sk_sp<SkTypeface>* face = static_cast<sk_sp<SkTypeface>*>(context);
bungeman7d0e3bc2016-08-02 07:07:33 -0700134
Ben Wagner68efb212019-03-04 16:15:40 -0500135 *face = result.fFace;
bungeman7d0e3bc2016-08-02 07:07:33 -0700136 return true;
137 }, &face);
Ben Wagner68efb212019-03-04 16:15:40 -0500138 return face;
bungeman7d0e3bc2016-08-02 07:07:33 -0700139 }
140};
141
142///////////////////////////////////////////////////////////////////////////////
143
144static bool find_by_FontIdentity(SkTypeface* cachedTypeface, void* ctx) {
145 typedef SkFontConfigInterface::FontIdentity FontIdentity;
146 SkTypeface_FCI* cachedFCTypeface = static_cast<SkTypeface_FCI*>(cachedTypeface);
147 FontIdentity* identity = static_cast<FontIdentity*>(ctx);
148
149 return cachedFCTypeface->getIdentity() == *identity;
150}
151
152///////////////////////////////////////////////////////////////////////////////
153
154class SkFontMgr_FCI : public SkFontMgr {
bungeman1ae0e012016-09-19 12:13:16 -0700155 sk_sp<SkFontConfigInterface> fFCI;
bungeman7d0e3bc2016-08-02 07:07:33 -0700156 SkTypeface_FreeType::Scanner fScanner;
157
158 mutable SkMutex fMutex;
159 mutable SkTypefaceCache fTFCache;
160
161 // The value of maxSize here is a compromise between cache hits and cache size.
162 // See https://crbug.com/424082#63 for reason for current size.
163 static const size_t kMaxSize = 1 << 15;
164 mutable SkFontRequestCache fCache;
165
166public:
bungeman1ae0e012016-09-19 12:13:16 -0700167 SkFontMgr_FCI(sk_sp<SkFontConfigInterface> fci)
168 : fFCI(std::move(fci))
bungeman7d0e3bc2016-08-02 07:07:33 -0700169 , fCache(kMaxSize)
170 {}
171
172protected:
173 int onCountFamilies() const override {
Ben Wagner44a9d572018-05-15 11:06:08 -0400174 SK_ABORT("Not implemented.");
bungeman7d0e3bc2016-08-02 07:07:33 -0700175 }
176
177 void onGetFamilyName(int index, SkString* familyName) const override {
Ben Wagnerb4aab9a2017-08-16 10:53:04 -0400178 SK_ABORT("Not implemented.");
bungeman7d0e3bc2016-08-02 07:07:33 -0700179 }
180
181 SkFontStyleSet* onCreateStyleSet(int index) const override {
Ben Wagnerb4aab9a2017-08-16 10:53:04 -0400182 SK_ABORT("Not implemented.");
bungeman7d0e3bc2016-08-02 07:07:33 -0700183 }
184
185 SkFontStyleSet* onMatchFamily(const char familyName[]) const override {
Ben Wagner44a9d572018-05-15 11:06:08 -0400186 SK_ABORT("Not implemented.");
bungeman7d0e3bc2016-08-02 07:07:33 -0700187 }
188
Ben Wagnera1ac8412018-06-27 11:01:40 -0400189 SkTypeface* onMatchFamilyStyle(const char requestedFamilyName[],
190 const SkFontStyle& requestedStyle) const override
191 {
Herb Derby9b869552019-05-10 12:16:17 -0400192 SkAutoMutexExclusive ama(fMutex);
Ben Wagnera1ac8412018-06-27 11:01:40 -0400193
194 SkFontConfigInterface::FontIdentity identity;
195 SkString outFamilyName;
196 SkFontStyle outStyle;
197 if (!fFCI->matchFamilyName(requestedFamilyName, requestedStyle,
198 &identity, &outFamilyName, &outStyle))
199 {
200 return nullptr;
201 }
202
203 // Check if a typeface with this FontIdentity is already in the FontIdentity cache.
Ben Wagner68efb212019-03-04 16:15:40 -0500204 sk_sp<SkTypeface> face = fTFCache.findByProcAndRef(find_by_FontIdentity, &identity);
Ben Wagnera1ac8412018-06-27 11:01:40 -0400205 if (!face) {
Ben Wagner68efb212019-03-04 16:15:40 -0500206 face.reset(SkTypeface_FCI::Create(fFCI, identity, std::move(outFamilyName), outStyle));
Ben Wagnera1ac8412018-06-27 11:01:40 -0400207 // Add this FontIdentity to the FontIdentity cache.
208 fTFCache.add(face);
209 }
Ben Wagner68efb212019-03-04 16:15:40 -0500210 return face.release();
Ben Wagner44a9d572018-05-15 11:06:08 -0400211 }
212
bungeman7d0e3bc2016-08-02 07:07:33 -0700213 SkTypeface* onMatchFamilyStyleCharacter(const char familyName[], const SkFontStyle&,
214 const char* bcp47[], int bcp47Count,
215 SkUnichar character) const override {
Ben Wagner44a9d572018-05-15 11:06:08 -0400216 SK_ABORT("Not implemented.");
bungeman7d0e3bc2016-08-02 07:07:33 -0700217 }
bungeman7d0e3bc2016-08-02 07:07:33 -0700218
Ben Wagner44a9d572018-05-15 11:06:08 -0400219 SkTypeface* onMatchFaceStyle(const SkTypeface*, const SkFontStyle&) const override {
220 SK_ABORT("Not implemented.");
Ben Wagner44a9d572018-05-15 11:06:08 -0400221 }
222
Mike Reedc81ebd22018-09-10 11:47:21 -0400223 sk_sp<SkTypeface> onMakeFromData(sk_sp<SkData> data, int ttcIndex) const override {
224 return this->onMakeFromStreamIndex(SkMemoryStream::Make(std::move(data)), ttcIndex);
Ben Wagner44a9d572018-05-15 11:06:08 -0400225 }
bungeman7d0e3bc2016-08-02 07:07:33 -0700226
Mike Reed59227392017-09-26 09:46:08 -0400227 sk_sp<SkTypeface> onMakeFromStreamIndex(std::unique_ptr<SkStreamAsset> stream,
228 int ttcIndex) const override {
bungeman7d0e3bc2016-08-02 07:07:33 -0700229 const size_t length = stream->getLength();
230 if (!length) {
231 return nullptr;
232 }
233 if (length >= 1024 * 1024 * 1024) {
234 return nullptr; // don't accept too large fonts (>= 1GB) for safety.
235 }
236
237 // TODO should the caller give us the style or should we get it from freetype?
Ben Wagner25272302017-01-25 11:15:50 -0500238 SkString name;
bungeman7d0e3bc2016-08-02 07:07:33 -0700239 SkFontStyle style;
bungeman4bb029c2016-09-01 08:52:29 -0700240 bool isFixedPitch = false;
Ben Wagner25272302017-01-25 11:15:50 -0500241 if (!fScanner.scanFont(stream.get(), 0, &name, &style, &isFixedPitch, nullptr)) {
bungeman7d0e3bc2016-08-02 07:07:33 -0700242 return nullptr;
243 }
244
Mike Kleinf46d5ca2019-12-11 10:45:01 -0500245 auto fontData = std::make_unique<SkFontData>(std::move(stream), ttcIndex, nullptr, 0);
Mike Reed59227392017-09-26 09:46:08 -0400246 return sk_sp<SkTypeface>(SkTypeface_FCI::Create(std::move(fontData), std::move(name),
247 style, isFixedPitch));
bungeman4bb029c2016-09-01 08:52:29 -0700248 }
249
Mike Reed59227392017-09-26 09:46:08 -0400250 sk_sp<SkTypeface> onMakeFromStreamArgs(std::unique_ptr<SkStreamAsset> stream,
251 const SkFontArguments& args) const override {
bungeman4bb029c2016-09-01 08:52:29 -0700252 using Scanner = SkTypeface_FreeType::Scanner;
bungeman4bb029c2016-09-01 08:52:29 -0700253 const size_t length = stream->getLength();
254 if (!length) {
255 return nullptr;
256 }
257 if (length >= 1024 * 1024 * 1024) {
258 return nullptr; // don't accept too large fonts (>= 1GB) for safety.
259 }
260
bungeman4bb029c2016-09-01 08:52:29 -0700261 SkString name;
Ben Wagnere80a5952020-07-14 17:57:13 -0400262 SkFontStyle style;
263 bool isFixedPitch = false;
bungeman4bb029c2016-09-01 08:52:29 -0700264 Scanner::AxisDefinitions axisDefinitions;
Ben Wagnerfc497342017-02-24 11:15:26 -0500265 if (!fScanner.scanFont(stream.get(), args.getCollectionIndex(),
bungemanf93d7112016-09-16 06:24:20 -0700266 &name, &style, &isFixedPitch, &axisDefinitions))
bungeman4bb029c2016-09-01 08:52:29 -0700267 {
268 return nullptr;
269 }
270
bungeman4bb029c2016-09-01 08:52:29 -0700271 SkAutoSTMalloc<4, SkFixed> axisValues(axisDefinitions.count());
Ben Wagnerfc497342017-02-24 11:15:26 -0500272 Scanner::computeAxisValues(axisDefinitions, args.getVariationDesignPosition(),
273 axisValues, name);
bungeman4bb029c2016-09-01 08:52:29 -0700274
Mike Kleinf46d5ca2019-12-11 10:45:01 -0500275 auto fontData = std::make_unique<SkFontData>(std::move(stream),
Ben Wagnere80a5952020-07-14 17:57:13 -0400276 args.getCollectionIndex(),
277 axisValues.get(),
278 axisDefinitions.count());
Mike Reed59227392017-09-26 09:46:08 -0400279 return sk_sp<SkTypeface>(SkTypeface_FCI::Create(std::move(fontData), std::move(name),
280 style, isFixedPitch));
bungeman7d0e3bc2016-08-02 07:07:33 -0700281 }
282
Mike Reed59227392017-09-26 09:46:08 -0400283 sk_sp<SkTypeface> onMakeFromFile(const char path[], int ttcIndex) const override {
bungemanf93d7112016-09-16 06:24:20 -0700284 std::unique_ptr<SkStreamAsset> stream = SkStream::MakeFromFile(path);
Mike Reed59227392017-09-26 09:46:08 -0400285 return stream ? this->makeFromStream(std::move(stream), ttcIndex) : nullptr;
bungeman7d0e3bc2016-08-02 07:07:33 -0700286 }
287
Ben Wagnere80a5952020-07-14 17:57:13 -0400288 sk_sp<SkTypeface> onMakeFromFontData(std::unique_ptr<SkFontData> fontData) const override {
289 SkStreamAsset* stream(fontData->getStream());
290 const size_t length = stream->getLength();
291 if (!length) {
292 return nullptr;
293 }
294 if (length >= 1024 * 1024 * 1024) {
295 return nullptr; // don't accept too large fonts (>= 1GB) for safety.
296 }
297
298 const int ttcIndex = fontData->getIndex();
299 SkString name;
300 SkFontStyle style;
301 bool isFixedPitch = false;
302 if (!fScanner.scanFont(stream, ttcIndex, &name, &style, &isFixedPitch, nullptr)) {
303 return nullptr;
304 }
305
306 return sk_sp<SkTypeface>(SkTypeface_FCI::Create(std::move(fontData), std::move(name),
307 style, isFixedPitch));
308 }
309
Mike Reed59227392017-09-26 09:46:08 -0400310 sk_sp<SkTypeface> onLegacyMakeTypeface(const char requestedFamilyName[],
311 SkFontStyle requestedStyle) const override
bungeman7d0e3bc2016-08-02 07:07:33 -0700312 {
Herb Derby9b869552019-05-10 12:16:17 -0400313 SkAutoMutexExclusive ama(fMutex);
bungeman7d0e3bc2016-08-02 07:07:33 -0700314
315 // Check if this request is already in the request cache.
316 using Request = SkFontRequestCache::Request;
Ben Wagner145dbcd2016-11-03 14:40:50 -0400317 std::unique_ptr<Request> request(Request::Create(requestedFamilyName, requestedStyle));
Ben Wagner68efb212019-03-04 16:15:40 -0500318 sk_sp<SkTypeface> face = fCache.findAndRef(request.get());
bungeman7d0e3bc2016-08-02 07:07:33 -0700319 if (face) {
Mike Reed59227392017-09-26 09:46:08 -0400320 return sk_sp<SkTypeface>(face);
bungeman7d0e3bc2016-08-02 07:07:33 -0700321 }
322
323 SkFontConfigInterface::FontIdentity identity;
324 SkString outFamilyName;
325 SkFontStyle outStyle;
326 if (!fFCI->matchFamilyName(requestedFamilyName, requestedStyle,
327 &identity, &outFamilyName, &outStyle))
328 {
329 return nullptr;
330 }
331
332 // Check if a typeface with this FontIdentity is already in the FontIdentity cache.
333 face = fTFCache.findByProcAndRef(find_by_FontIdentity, &identity);
334 if (!face) {
Ben Wagner68efb212019-03-04 16:15:40 -0500335 face.reset(SkTypeface_FCI::Create(fFCI, identity, std::move(outFamilyName), outStyle));
bungeman7d0e3bc2016-08-02 07:07:33 -0700336 // Add this FontIdentity to the FontIdentity cache.
337 fTFCache.add(face);
338 }
339 // Add this request to the request cache.
340 fCache.add(face, request.release());
341
Ben Wagner68efb212019-03-04 16:15:40 -0500342 return face;
bungeman7d0e3bc2016-08-02 07:07:33 -0700343 }
344};
345
Ben Wagner3546ff12017-01-03 13:32:36 -0500346SK_API sk_sp<SkFontMgr> SkFontMgr_New_FCI(sk_sp<SkFontConfigInterface> fci) {
347 SkASSERT(fci);
348 return sk_make_sp<SkFontMgr_FCI>(std::move(fci));
349}