| 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/private/base/SkMutex.h" |
| 16 |
#include "src/core/SkFontDescriptor.h" |
| 17 |
#include "src/core/SkResourceCache.h" |
| 18 |
#include "src/core/SkTypefaceCache.h" |
| 19 |
#include "src/ports/SkFontConfigTypeface.h" |
| 20 |
|
| 21 |
#include <new> |
| 22 |
|
| 23 |
using namespace skia_private; |
| 24 |
|
| 25 |
std::unique_ptr<SkStreamAsset> SkTypeface_FCI::onOpenStream(int* ttcIndex) const { |
| 26 |
*ttcIndex = this->getIdentity().fTTCIndex; |
| 27 |
return std::unique_ptr<SkStreamAsset>(fFCI->openStream(this->getIdentity())); |
| 28 |
} |
| 29 |
|
| 30 |
void SkTypeface_FCI::onGetFontDescriptor(SkFontDescriptor* desc, bool* serialize) const { |
| 31 |
SkTypeface_proxy::onGetFontDescriptor(desc, serialize); |
| 32 |
SkString name; |
| 33 |
this->getFamilyName(&name); |
| 34 |
desc->setFamilyName(name.c_str()); |
| 35 |
desc->setStyle(this->fontStyle()); |
| 36 |
*serialize = true; |
| 37 |
} |
| 38 |
|
| 39 |
/////////////////////////////////////////////////////////////////////////////// |
| 40 |
|
| 41 |
class SkFontStyleSet_FCI : public SkFontStyleSet { |
| 42 |
public: |
| 43 |
SkFontStyleSet_FCI() {} |
| 44 |
|
| 45 |
int count() override { return 0; } |
| 46 |
void getStyle(int index, SkFontStyle*, SkString* style) override { SkASSERT(false); } |
| 47 |
sk_sp<SkTypeface> createTypeface(int index) override { SkASSERT(false); return nullptr; } |
| 48 |
sk_sp<SkTypeface> matchStyle(const SkFontStyle& pattern) override { return nullptr; } |
| 49 |
}; |
| 50 |
|
| 51 |
/////////////////////////////////////////////////////////////////////////////// |
| 52 |
|
| 53 |
class SkFontRequestCache { |
| 54 |
public: |
| 55 |
struct Request : public SkResourceCache::Key { |
| 56 |
private: |
| 57 |
Request(const char* name, size_t nameLen, const SkFontStyle& style) : fStyle(style) { |
| 58 |
/** Pointer to just after the last field of this class. */ |
| 59 |
char* content = const_cast<char*>(SkTAfter<const char>(&this->fStyle)); |
| 60 |
|
| 61 |
// No holes. |
| 62 |
SkASSERT(SkTAddOffset<char>(this, sizeof(SkResourceCache::Key) + keySize) == content); |
| 63 |
|
| 64 |
// Has a size divisible by size of uint32_t. |
| 65 |
SkASSERT((content - reinterpret_cast<char*>(this)) % sizeof(uint32_t) == 0); |
| 66 |
|
| 67 |
size_t contentLen = SkAlign4(nameLen); |
| 68 |
sk_careful_memcpy(content, name, nameLen); |
| 69 |
sk_bzero(content + nameLen, contentLen - nameLen); |
| 70 |
this->init(nullptr, 0, keySize + contentLen); |
| 71 |
} |
| 72 |
const SkFontStyle fStyle; |
| 73 |
/** The sum of the sizes of the fields of this class. */ |
| 74 |
static const size_t keySize = sizeof(fStyle); |
| 75 |
|
| 76 |
public: |
| 77 |
static Request* Create(const char* name, const SkFontStyle& style) { |
| 78 |
size_t nameLen = name ? strlen(name) : 0; |
| 79 |
size_t contentLen = SkAlign4(nameLen); |
| 80 |
char* storage = new char[sizeof(Request) + contentLen]; |
| 81 |
return new (storage) Request(name, nameLen, style); |
| 82 |
} |
| 83 |
void operator delete(void* storage) { |
| 84 |
delete[] reinterpret_cast<char*>(storage); |
| 85 |
} |
| 86 |
}; |
| 87 |
|
| 88 |
|
| 89 |
private: |
| 90 |
struct Result : public SkResourceCache::Rec { |
| 91 |
Result(Request* request, sk_sp<SkTypeface> typeface) |
| 92 |
: fRequest(request), fFace(std::move(typeface)) {} |
| 93 |
Result(Result&&) = default; |
| 94 |
Result& operator=(Result&&) = default; |
| 95 |
|
| 96 |
const Key& getKey() const override { return *fRequest; } |
| 97 |
size_t bytesUsed() const override { return fRequest->size() + sizeof(fFace); } |
| 98 |
const char* getCategory() const override { return "request_cache"; } |
| 99 |
SkDiscardableMemory* diagnostic_only_getDiscardable() const override { return nullptr; } |
| 100 |
|
| 101 |
std::unique_ptr<Request> fRequest; |
| 102 |
sk_sp<SkTypeface> fFace; |
| 103 |
}; |
| 104 |
|
| 105 |
SkResourceCache fCachedResults; |
| 106 |
|
| 107 |
public: |
| 108 |
SkFontRequestCache(size_t maxSize) : fCachedResults(maxSize) {} |
| 109 |
|
| 110 |
/** Takes ownership of request. It will be deleted when no longer needed. */ |
| 111 |
void add(sk_sp<SkTypeface> face, Request* request) { |
| 112 |
fCachedResults.add(new Result(request, std::move(face))); |
| 113 |
} |
| 114 |
/** Does not take ownership of request. */ |
| 115 |
sk_sp<SkTypeface> findAndRef(Request* request) { |
| 116 |
sk_sp<SkTypeface> face; |
| 117 |
fCachedResults.find(*request, [](const SkResourceCache::Rec& rec, void* context) -> bool { |
| 118 |
const Result& result = static_cast<const Result&>(rec); |
| 119 |
sk_sp<SkTypeface>* face = static_cast<sk_sp<SkTypeface>*>(context); |
| 120 |
|
| 121 |
*face = result.fFace; |
| 122 |
return true; |
| 123 |
}, &face); |
| 124 |
return face; |
| 125 |
} |
| 126 |
}; |
| 127 |
|
| 128 |
/////////////////////////////////////////////////////////////////////////////// |
| 129 |
|
| 130 |
static bool find_by_FontIdentity(SkTypeface* cachedTypeface, void* ctx) { |
| 131 |
typedef SkFontConfigInterface::FontIdentity FontIdentity; |
| 132 |
SkTypeface_FCI* cachedFCTypeface = static_cast<SkTypeface_FCI*>(cachedTypeface); |
| 133 |
FontIdentity* identity = static_cast<FontIdentity*>(ctx); |
| 134 |
|
| 135 |
return cachedFCTypeface->getIdentity() == *identity; |
| 136 |
} |
| 137 |
|
| 138 |
/////////////////////////////////////////////////////////////////////////////// |
| 139 |
|
| 140 |
class SkFontMgr_FCI : public SkFontMgr { |
| 141 |
sk_sp<SkFontConfigInterface> fFCI; |
| 142 |
std::unique_ptr<SkFontScanner> fScanner; |
| 143 |
|
| 144 |
mutable SkMutex fMutex; |
| 145 |
mutable SkTypefaceCache fTFCache; |
| 146 |
|
| 147 |
// The value of maxSize here is a compromise between cache hits and cache size. |
| 148 |
// See https://crbug.com/424082#63 for reason for current size. |
| 149 |
static const size_t kMaxSize = 1 << 15; |
| 150 |
mutable SkFontRequestCache fCache; |
| 151 |
|
| 152 |
public: |
| 153 |
SkFontMgr_FCI(sk_sp<SkFontConfigInterface> fci, std::unique_ptr<SkFontScanner> scanner) |
| 154 |
: fFCI(std::move(fci)) |
| 155 |
, fScanner(std::move(scanner)) |
| 156 |
, fCache(kMaxSize) |
| 157 |
{ |
| 158 |
SkASSERT_RELEASE(fFCI); |
| 159 |
} |
| 160 |
|
| 161 |
protected: |
| 162 |
int onCountFamilies() const override { |
| 163 |
SK_ABORT("Not implemented."); |
| 164 |
} |
| 165 |
|
| 166 |
void onGetFamilyName(int index, SkString* familyName) const override { |
| 167 |
SK_ABORT("Not implemented."); |
| 168 |
} |
| 169 |
|
| 170 |
sk_sp<SkFontStyleSet> onCreateStyleSet(int index) const override { |
| 171 |
SK_ABORT("Not implemented."); |
| 172 |
} |
| 173 |
|
| 174 |
sk_sp<SkFontStyleSet> onMatchFamily(const char familyName[]) const override { |
| 175 |
SK_ABORT("Not implemented."); |
| 176 |
} |
| 177 |
|
| 178 |
sk_sp<SkTypeface> onMatchFamilyStyle(const char requestedFamilyName[], |
| 179 |
const SkFontStyle& requestedStyle) const override |
| 180 |
{ |
| 181 |
SkAutoMutexExclusive ama(fMutex); |
| 182 |
|
| 183 |
// Check if this request is already in the request cache. |
| 184 |
using Request = SkFontRequestCache::Request; |
| 185 |
std::unique_ptr<Request> request(Request::Create(requestedFamilyName, requestedStyle)); |
| 186 |
sk_sp<SkTypeface> face = fCache.findAndRef(request.get()); |
| 187 |
if (face) { |
| 188 |
return sk_sp<SkTypeface>(face); |
| 189 |
} |
| 190 |
|
| 191 |
SkFontConfigInterface::FontIdentity identity; |
| 192 |
SkString outFamilyName; |
| 193 |
SkFontStyle outStyle; |
| 194 |
if (!fFCI->matchFamilyName(requestedFamilyName, requestedStyle, |
| 195 |
&identity, &outFamilyName, &outStyle)) |
| 196 |
{ |
| 197 |
return nullptr; |
| 198 |
} |
| 199 |
|
| 200 |
// Check if a typeface with this FontIdentity is already in the typeface cache. |
| 201 |
face = fTFCache.findByProcAndRef(find_by_FontIdentity, &identity); |
| 202 |
if (!face) { |
| 203 |
sk_sp<SkTypeface> realTypeface = fScanner->MakeFromStream( |
| 204 |
std::unique_ptr<SkStreamAsset>(fFCI->openStream(identity)), |
| 205 |
SkFontArguments().setCollectionIndex(identity.fTTCIndex)); |
| 206 |
face.reset(SkTypeface_FCI::Create(std::move(realTypeface), fFCI, identity, |
| 207 |
std::move(outFamilyName), outStyle, false)); |
| 208 |
if (face) { |
| 209 |
// Add this typeface to the typeface cache. |
| 210 |
fTFCache.add(face); |
| 211 |
} |
| 212 |
} |
| 213 |
// Add this request to the request cache. |
| 214 |
fCache.add(face, request.release()); |
| 215 |
|
| 216 |
return face; |
| 217 |
} |
| 218 |
|
| 219 |
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 |
|
| 225 |
sk_sp<SkTypeface> onMakeFromData(sk_sp<SkData> data, int ttcIndex) const override { |
| 226 |
return this->onMakeFromStreamIndex(SkMemoryStream::Make(std::move(data)), ttcIndex); |
| 227 |
} |
| 228 |
|
| 229 |
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 |
|
| 235 |
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 fScanner->MakeFromStream(std::move(stream), args); |
| 246 |
} |
| 247 |
|
| 248 |
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 |
|
| 253 |
sk_sp<SkTypeface> onLegacyMakeTypeface(const char requestedFamilyName[], |
| 254 |
SkFontStyle requestedStyle) const override { |
| 255 |
return this->onMatchFamilyStyle(requestedFamilyName, requestedStyle); |
| 256 |
} |
| 257 |
}; |
| 258 |
|
| 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 |
} |