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