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 "src/ports/SkTypeface_FreeType.h"
20 #include <new>
21
22 using namespace skia_private;
23
onOpenStream(int * ttcIndex) const24 std::unique_ptr<SkStreamAsset> SkTypeface_FCI::onOpenStream(int* ttcIndex) const {
25 *ttcIndex = this->getIdentity().fTTCIndex;
26 return std::unique_ptr<SkStreamAsset>(fFCI->openStream(this->getIdentity()));
27 }
28
onMakeFontData() const29 std::unique_ptr<SkFontData> SkTypeface_FCI::onMakeFontData() const {
30 const SkFontConfigInterface::FontIdentity& id = this->getIdentity();
31 return std::make_unique<SkFontData>(std::unique_ptr<SkStreamAsset>(fFCI->openStream(id)),
32 id.fTTCIndex, 0, nullptr, 0, nullptr, 0);
33 }
34
onGetFontDescriptor(SkFontDescriptor * desc,bool * serialize) const35 void SkTypeface_FCI::onGetFontDescriptor(SkFontDescriptor* desc, bool* serialize) const {
36 SkString name;
37 this->getFamilyName(&name);
38 desc->setFamilyName(name.c_str());
39 desc->setStyle(this->fontStyle());
40 desc->setFactoryId(SkTypeface_FreeType::FactoryId);
41 *serialize = true;
42 }
43
44 ///////////////////////////////////////////////////////////////////////////////
45
46 class SkFontStyleSet_FCI : public SkFontStyleSet {
47 public:
SkFontStyleSet_FCI()48 SkFontStyleSet_FCI() {}
49
count()50 int count() override { return 0; }
getStyle(int index,SkFontStyle *,SkString * style)51 void getStyle(int index, SkFontStyle*, SkString* style) override { SkASSERT(false); }
createTypeface(int index)52 sk_sp<SkTypeface> createTypeface(int index) override { SkASSERT(false); return nullptr; }
matchStyle(const SkFontStyle & pattern)53 sk_sp<SkTypeface> matchStyle(const SkFontStyle& pattern) override { return nullptr; }
54 };
55
56 ///////////////////////////////////////////////////////////////////////////////
57
58 class SkFontRequestCache {
59 public:
60 struct Request : public SkResourceCache::Key {
61 private:
RequestSkFontRequestCache::Request62 Request(const char* name, size_t nameLen, const SkFontStyle& style) : fStyle(style) {
63 /** Pointer to just after the last field of this class. */
64 char* content = const_cast<char*>(SkTAfter<const char>(&this->fStyle));
65
66 // No holes.
67 SkASSERT(SkTAddOffset<char>(this, sizeof(SkResourceCache::Key) + keySize) == content);
68
69 // Has a size divisible by size of uint32_t.
70 SkASSERT((content - reinterpret_cast<char*>(this)) % sizeof(uint32_t) == 0);
71
72 size_t contentLen = SkAlign4(nameLen);
73 sk_careful_memcpy(content, name, nameLen);
74 sk_bzero(content + nameLen, contentLen - nameLen);
75 this->init(nullptr, 0, keySize + contentLen);
76 }
77 const SkFontStyle fStyle;
78 /** The sum of the sizes of the fields of this class. */
79 static const size_t keySize = sizeof(fStyle);
80
81 public:
CreateSkFontRequestCache::Request82 static Request* Create(const char* name, const SkFontStyle& style) {
83 size_t nameLen = name ? strlen(name) : 0;
84 size_t contentLen = SkAlign4(nameLen);
85 char* storage = new char[sizeof(Request) + contentLen];
86 return new (storage) Request(name, nameLen, style);
87 }
operator deleteSkFontRequestCache::Request88 void operator delete(void* storage) {
89 delete[] reinterpret_cast<char*>(storage);
90 }
91 };
92
93
94 private:
95 struct Result : public SkResourceCache::Rec {
ResultSkFontRequestCache::Result96 Result(Request* request, sk_sp<SkTypeface> typeface)
97 : fRequest(request), fFace(std::move(typeface)) {}
98 Result(Result&&) = default;
99 Result& operator=(Result&&) = default;
100
getKeySkFontRequestCache::Result101 const Key& getKey() const override { return *fRequest; }
bytesUsedSkFontRequestCache::Result102 size_t bytesUsed() const override { return fRequest->size() + sizeof(fFace); }
getCategorySkFontRequestCache::Result103 const char* getCategory() const override { return "request_cache"; }
diagnostic_only_getDiscardableSkFontRequestCache::Result104 SkDiscardableMemory* diagnostic_only_getDiscardable() const override { return nullptr; }
105
106 std::unique_ptr<Request> fRequest;
107 sk_sp<SkTypeface> fFace;
108 };
109
110 SkResourceCache fCachedResults;
111
112 public:
SkFontRequestCache(size_t maxSize)113 SkFontRequestCache(size_t maxSize) : fCachedResults(maxSize) {}
114
115 /** Takes ownership of request. It will be deleted when no longer needed. */
add(sk_sp<SkTypeface> face,Request * request)116 void add(sk_sp<SkTypeface> face, Request* request) {
117 fCachedResults.add(new Result(request, std::move(face)));
118 }
119 /** Does not take ownership of request. */
findAndRef(Request * request)120 sk_sp<SkTypeface> findAndRef(Request* request) {
121 sk_sp<SkTypeface> face;
122 fCachedResults.find(*request, [](const SkResourceCache::Rec& rec, void* context) -> bool {
123 const Result& result = static_cast<const Result&>(rec);
124 sk_sp<SkTypeface>* face = static_cast<sk_sp<SkTypeface>*>(context);
125
126 *face = result.fFace;
127 return true;
128 }, &face);
129 return face;
130 }
131 };
132
133 ///////////////////////////////////////////////////////////////////////////////
134
find_by_FontIdentity(SkTypeface * cachedTypeface,void * ctx)135 static bool find_by_FontIdentity(SkTypeface* cachedTypeface, void* ctx) {
136 typedef SkFontConfigInterface::FontIdentity FontIdentity;
137 SkTypeface_FCI* cachedFCTypeface = static_cast<SkTypeface_FCI*>(cachedTypeface);
138 FontIdentity* identity = static_cast<FontIdentity*>(ctx);
139
140 return cachedFCTypeface->getIdentity() == *identity;
141 }
142
143 ///////////////////////////////////////////////////////////////////////////////
144
145 class SkFontMgr_FCI : public SkFontMgr {
146 sk_sp<SkFontConfigInterface> fFCI;
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 SkASSERT_RELEASE(fFCI);
162 }
163
164 protected:
onCountFamilies() const165 int onCountFamilies() const override {
166 SK_ABORT("Not implemented.");
167 }
168
onGetFamilyName(int index,SkString * familyName) const169 void onGetFamilyName(int index, SkString* familyName) const override {
170 SK_ABORT("Not implemented.");
171 }
172
onCreateStyleSet(int index) const173 sk_sp<SkFontStyleSet> onCreateStyleSet(int index) const override {
174 SK_ABORT("Not implemented.");
175 }
176
onMatchFamily(const char familyName[]) const177 sk_sp<SkFontStyleSet> onMatchFamily(const char familyName[]) const override {
178 SK_ABORT("Not implemented.");
179 }
180
onMatchFamilyStyle(const char requestedFamilyName[],const SkFontStyle & requestedStyle) const181 sk_sp<SkTypeface> onMatchFamilyStyle(const char requestedFamilyName[],
182 const SkFontStyle& requestedStyle) const override
183 {
184 SkAutoMutexExclusive ama(fMutex);
185
186 // Check if this request is already in the request cache.
187 using Request = SkFontRequestCache::Request;
188 std::unique_ptr<Request> request(Request::Create(requestedFamilyName, requestedStyle));
189 sk_sp<SkTypeface> face = fCache.findAndRef(request.get());
190 if (face) {
191 return sk_sp<SkTypeface>(face);
192 }
193
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.
204 face = fTFCache.findByProcAndRef(find_by_FontIdentity, &identity);
205 if (!face) {
206 face.reset(SkTypeface_FCI::Create(fFCI, identity, std::move(outFamilyName), outStyle));
207 // Add this FontIdentity to the FontIdentity cache.
208 fTFCache.add(face);
209 }
210 // Add this request to the request cache.
211 fCache.add(face, request.release());
212
213 return face;
214 }
215
onMatchFamilyStyleCharacter(const char familyName[],const SkFontStyle &,const char * bcp47[],int bcp47Count,SkUnichar character) const216 sk_sp<SkTypeface> onMatchFamilyStyleCharacter(const char familyName[], const SkFontStyle&,
217 const char* bcp47[], int bcp47Count,
218 SkUnichar character) const override {
219 SK_ABORT("Not implemented.");
220 }
221
onMakeFromData(sk_sp<SkData> data,int ttcIndex) const222 sk_sp<SkTypeface> onMakeFromData(sk_sp<SkData> data, int ttcIndex) const override {
223 return this->onMakeFromStreamIndex(SkMemoryStream::Make(std::move(data)), ttcIndex);
224 }
225
onMakeFromStreamIndex(std::unique_ptr<SkStreamAsset> stream,int ttcIndex) const226 sk_sp<SkTypeface> onMakeFromStreamIndex(std::unique_ptr<SkStreamAsset> stream,
227 int ttcIndex) const override {
228 return this->makeFromStream(std::move(stream),
229 SkFontArguments().setCollectionIndex(ttcIndex));
230 }
231
onMakeFromStreamArgs(std::unique_ptr<SkStreamAsset> stream,const SkFontArguments & args) const232 sk_sp<SkTypeface> onMakeFromStreamArgs(std::unique_ptr<SkStreamAsset> stream,
233 const SkFontArguments& args) const override {
234 const size_t length = stream->getLength();
235 if (!length) {
236 return nullptr;
237 }
238 if (length >= 1024 * 1024 * 1024) {
239 return nullptr; // don't accept too large fonts (>= 1GB) for safety.
240 }
241
242 return SkTypeface_FreeType::MakeFromStream(std::move(stream), args);
243 }
244
onMakeFromFile(const char path[],int ttcIndex) const245 sk_sp<SkTypeface> onMakeFromFile(const char path[], int ttcIndex) const override {
246 std::unique_ptr<SkStreamAsset> stream = SkStream::MakeFromFile(path);
247 return stream ? this->makeFromStream(std::move(stream), ttcIndex) : nullptr;
248 }
249
onLegacyMakeTypeface(const char requestedFamilyName[],SkFontStyle requestedStyle) const250 sk_sp<SkTypeface> onLegacyMakeTypeface(const char requestedFamilyName[],
251 SkFontStyle requestedStyle) const override {
252 return this->onMatchFamilyStyle(requestedFamilyName, requestedStyle);
253 }
254 };
255
SkFontMgr_New_FCI(sk_sp<SkFontConfigInterface> fci)256 SK_API sk_sp<SkFontMgr> SkFontMgr_New_FCI(sk_sp<SkFontConfigInterface> fci) {
257 SkASSERT(fci);
258 return sk_make_sp<SkFontMgr_FCI>(std::move(fci));
259 }
260