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/base/SkMutex.h"
15 #include "src/core/SkFontDescriptor.h"
16 #include "src/core/SkResourceCache.h"
17 #include "src/core/SkTypefaceCache.h"
18 #include "src/ports/SkFontConfigTypeface.h"
19 #include <new>
20
21 using namespace skia_private;
22
onOpenStream(int * ttcIndex) const23 std::unique_ptr<SkStreamAsset> SkTypeface_FCI::onOpenStream(int* ttcIndex) const {
24 *ttcIndex = this->getIdentity().fTTCIndex;
25 return std::unique_ptr<SkStreamAsset>(fFCI->openStream(this->getIdentity()));
26 }
27
onMakeFontData() const28 std::unique_ptr<SkFontData> SkTypeface_FCI::onMakeFontData() const {
29 const SkFontConfigInterface::FontIdentity& id = this->getIdentity();
30 return std::make_unique<SkFontData>(std::unique_ptr<SkStreamAsset>(fFCI->openStream(id)),
31 id.fTTCIndex, 0, nullptr, 0, nullptr, 0);
32 }
33
onGetFontDescriptor(SkFontDescriptor * desc,bool * serialize) const34 void SkTypeface_FCI::onGetFontDescriptor(SkFontDescriptor* desc, bool* serialize) const {
35 SkString name;
36 this->getFamilyName(&name);
37 desc->setFamilyName(name.c_str());
38 desc->setStyle(this->fontStyle());
39 desc->setFactoryId(SkTypeface_FreeType::FactoryId);
40 *serialize = true;
41 }
42
43 ///////////////////////////////////////////////////////////////////////////////
44
45 class SkFontStyleSet_FCI : public SkFontStyleSet {
46 public:
SkFontStyleSet_FCI()47 SkFontStyleSet_FCI() {}
48
count()49 int count() override { return 0; }
getStyle(int index,SkFontStyle *,SkString * style)50 void getStyle(int index, SkFontStyle*, SkString* style) override { SkASSERT(false); }
createTypeface(int index)51 SkTypeface* createTypeface(int index) override { SkASSERT(false); return nullptr; }
matchStyle(const SkFontStyle & pattern)52 SkTypeface* matchStyle(const SkFontStyle& pattern) override { return nullptr; }
53 };
54
55 ///////////////////////////////////////////////////////////////////////////////
56
57 class SkFontRequestCache {
58 public:
59 struct Request : public SkResourceCache::Key {
60 private:
RequestSkFontRequestCache::Request61 Request(const char* name, size_t nameLen, const SkFontStyle& style) : fStyle(style) {
62 /** Pointer to just after the last field of this class. */
63 char* content = const_cast<char*>(SkTAfter<const char>(&this->fStyle));
64
65 // No holes.
66 SkASSERT(SkTAddOffset<char>(this, sizeof(SkResourceCache::Key) + keySize) == content);
67
68 // Has a size divisible by size of uint32_t.
69 SkASSERT((content - reinterpret_cast<char*>(this)) % sizeof(uint32_t) == 0);
70
71 size_t contentLen = SkAlign4(nameLen);
72 sk_careful_memcpy(content, name, nameLen);
73 sk_bzero(content + nameLen, contentLen - nameLen);
74 this->init(nullptr, 0, keySize + contentLen);
75 }
76 const SkFontStyle fStyle;
77 /** The sum of the sizes of the fields of this class. */
78 static const size_t keySize = sizeof(fStyle);
79
80 public:
CreateSkFontRequestCache::Request81 static Request* Create(const char* name, const SkFontStyle& style) {
82 size_t nameLen = name ? strlen(name) : 0;
83 size_t contentLen = SkAlign4(nameLen);
84 char* storage = new char[sizeof(Request) + contentLen];
85 return new (storage) Request(name, nameLen, style);
86 }
operator deleteSkFontRequestCache::Request87 void operator delete(void* storage) {
88 delete[] reinterpret_cast<char*>(storage);
89 }
90 };
91
92
93 private:
94 struct Result : public SkResourceCache::Rec {
ResultSkFontRequestCache::Result95 Result(Request* request, sk_sp<SkTypeface> typeface)
96 : fRequest(request), fFace(std::move(typeface)) {}
97 Result(Result&&) = default;
98 Result& operator=(Result&&) = default;
99
getKeySkFontRequestCache::Result100 const Key& getKey() const override { return *fRequest; }
bytesUsedSkFontRequestCache::Result101 size_t bytesUsed() const override { return fRequest->size() + sizeof(fFace); }
getCategorySkFontRequestCache::Result102 const char* getCategory() const override { return "request_cache"; }
diagnostic_only_getDiscardableSkFontRequestCache::Result103 SkDiscardableMemory* diagnostic_only_getDiscardable() const override { return nullptr; }
104
105 std::unique_ptr<Request> fRequest;
106 sk_sp<SkTypeface> fFace;
107 };
108
109 SkResourceCache fCachedResults;
110
111 public:
SkFontRequestCache(size_t maxSize)112 SkFontRequestCache(size_t maxSize) : fCachedResults(maxSize) {}
113
114 /** Takes ownership of request. It will be deleted when no longer needed. */
add(sk_sp<SkTypeface> face,Request * request)115 void add(sk_sp<SkTypeface> face, Request* request) {
116 fCachedResults.add(new Result(request, std::move(face)));
117 }
118 /** Does not take ownership of request. */
findAndRef(Request * request)119 sk_sp<SkTypeface> findAndRef(Request* request) {
120 sk_sp<SkTypeface> face;
121 fCachedResults.find(*request, [](const SkResourceCache::Rec& rec, void* context) -> bool {
122 const Result& result = static_cast<const Result&>(rec);
123 sk_sp<SkTypeface>* face = static_cast<sk_sp<SkTypeface>*>(context);
124
125 *face = result.fFace;
126 return true;
127 }, &face);
128 return face;
129 }
130 };
131
132 ///////////////////////////////////////////////////////////////////////////////
133
find_by_FontIdentity(SkTypeface * cachedTypeface,void * ctx)134 static bool find_by_FontIdentity(SkTypeface* cachedTypeface, void* ctx) {
135 typedef SkFontConfigInterface::FontIdentity FontIdentity;
136 SkTypeface_FCI* cachedFCTypeface = static_cast<SkTypeface_FCI*>(cachedTypeface);
137 FontIdentity* identity = static_cast<FontIdentity*>(ctx);
138
139 return cachedFCTypeface->getIdentity() == *identity;
140 }
141
142 ///////////////////////////////////////////////////////////////////////////////
143
144 class SkFontMgr_FCI : public SkFontMgr {
145 sk_sp<SkFontConfigInterface> fFCI;
146 SkTypeface_FreeType::Scanner fScanner;
147
148 mutable SkMutex fMutex;
149 mutable SkTypefaceCache fTFCache;
150
151 // The value of maxSize here is a compromise between cache hits and cache size.
152 // See https://crbug.com/424082#63 for reason for current size.
153 static const size_t kMaxSize = 1 << 15;
154 mutable SkFontRequestCache fCache;
155
156 public:
SkFontMgr_FCI(sk_sp<SkFontConfigInterface> fci)157 SkFontMgr_FCI(sk_sp<SkFontConfigInterface> fci)
158 : fFCI(std::move(fci))
159 , fCache(kMaxSize)
160 {}
161
162 protected:
onCountFamilies() const163 int onCountFamilies() const override {
164 SK_ABORT("Not implemented.");
165 }
166
onGetFamilyName(int index,SkString * familyName) const167 void onGetFamilyName(int index, SkString* familyName) const override {
168 SK_ABORT("Not implemented.");
169 }
170
onCreateStyleSet(int index) const171 SkFontStyleSet* onCreateStyleSet(int index) const override {
172 SK_ABORT("Not implemented.");
173 }
174
onMatchFamily(const char familyName[]) const175 SkFontStyleSet* onMatchFamily(const char familyName[]) const override {
176 SK_ABORT("Not implemented.");
177 }
178
onMatchFamilyStyle(const char requestedFamilyName[],const SkFontStyle & requestedStyle) const179 SkTypeface* onMatchFamilyStyle(const char requestedFamilyName[],
180 const SkFontStyle& requestedStyle) const override
181 {
182 SkAutoMutexExclusive ama(fMutex);
183
184 SkFontConfigInterface::FontIdentity identity;
185 SkString outFamilyName;
186 SkFontStyle outStyle;
187 if (!fFCI->matchFamilyName(requestedFamilyName, requestedStyle,
188 &identity, &outFamilyName, &outStyle))
189 {
190 return nullptr;
191 }
192
193 // Check if a typeface with this FontIdentity is already in the FontIdentity cache.
194 sk_sp<SkTypeface> face = fTFCache.findByProcAndRef(find_by_FontIdentity, &identity);
195 if (!face) {
196 face.reset(SkTypeface_FCI::Create(fFCI, identity, std::move(outFamilyName), outStyle));
197 // Add this FontIdentity to the FontIdentity cache.
198 fTFCache.add(face);
199 }
200 return face.release();
201 }
202
onMatchFamilyStyleCharacter(const char familyName[],const SkFontStyle &,const char * bcp47[],int bcp47Count,SkUnichar character) const203 SkTypeface* onMatchFamilyStyleCharacter(const char familyName[], const SkFontStyle&,
204 const char* bcp47[], int bcp47Count,
205 SkUnichar character) const override {
206 SK_ABORT("Not implemented.");
207 }
208
onMakeFromData(sk_sp<SkData> data,int ttcIndex) const209 sk_sp<SkTypeface> onMakeFromData(sk_sp<SkData> data, int ttcIndex) const override {
210 return this->onMakeFromStreamIndex(SkMemoryStream::Make(std::move(data)), ttcIndex);
211 }
212
onMakeFromStreamIndex(std::unique_ptr<SkStreamAsset> stream,int ttcIndex) const213 sk_sp<SkTypeface> onMakeFromStreamIndex(std::unique_ptr<SkStreamAsset> stream,
214 int ttcIndex) const override {
215 return this->makeFromStream(std::move(stream),
216 SkFontArguments().setCollectionIndex(ttcIndex));
217 }
218
onMakeFromStreamArgs(std::unique_ptr<SkStreamAsset> stream,const SkFontArguments & args) const219 sk_sp<SkTypeface> onMakeFromStreamArgs(std::unique_ptr<SkStreamAsset> stream,
220 const SkFontArguments& args) const override {
221 const size_t length = stream->getLength();
222 if (!length) {
223 return nullptr;
224 }
225 if (length >= 1024 * 1024 * 1024) {
226 return nullptr; // don't accept too large fonts (>= 1GB) for safety.
227 }
228
229 return SkTypeface_FreeType::MakeFromStream(std::move(stream), args);
230 }
231
onMakeFromFile(const char path[],int ttcIndex) const232 sk_sp<SkTypeface> onMakeFromFile(const char path[], int ttcIndex) const override {
233 std::unique_ptr<SkStreamAsset> stream = SkStream::MakeFromFile(path);
234 return stream ? this->makeFromStream(std::move(stream), ttcIndex) : nullptr;
235 }
236
onLegacyMakeTypeface(const char requestedFamilyName[],SkFontStyle requestedStyle) const237 sk_sp<SkTypeface> onLegacyMakeTypeface(const char requestedFamilyName[],
238 SkFontStyle requestedStyle) const override
239 {
240 SkAutoMutexExclusive ama(fMutex);
241
242 // Check if this request is already in the request cache.
243 using Request = SkFontRequestCache::Request;
244 std::unique_ptr<Request> request(Request::Create(requestedFamilyName, requestedStyle));
245 sk_sp<SkTypeface> face = fCache.findAndRef(request.get());
246 if (face) {
247 return sk_sp<SkTypeface>(face);
248 }
249
250 SkFontConfigInterface::FontIdentity identity;
251 SkString outFamilyName;
252 SkFontStyle outStyle;
253 if (!fFCI->matchFamilyName(requestedFamilyName, requestedStyle,
254 &identity, &outFamilyName, &outStyle))
255 {
256 return nullptr;
257 }
258
259 // Check if a typeface with this FontIdentity is already in the FontIdentity cache.
260 face = fTFCache.findByProcAndRef(find_by_FontIdentity, &identity);
261 if (!face) {
262 face.reset(SkTypeface_FCI::Create(fFCI, identity, std::move(outFamilyName), outStyle));
263 // Add this FontIdentity to the FontIdentity cache.
264 fTFCache.add(face);
265 }
266 // Add this request to the request cache.
267 fCache.add(face, request.release());
268
269 return face;
270 }
271 };
272
SkFontMgr_New_FCI(sk_sp<SkFontConfigInterface> fci)273 SK_API sk_sp<SkFontMgr> SkFontMgr_New_FCI(sk_sp<SkFontConfigInterface> fci) {
274 SkASSERT(fci);
275 return sk_make_sp<SkFontMgr_FCI>(std::move(fci));
276 }
277