bungeman | 7d0e3bc | 2016-08-02 07:07:33 -0700 | [diff] [blame] | 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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 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/SkMutex.h" |
| 15 | #include "src/core/SkFontDescriptor.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 16 | #include "src/core/SkResourceCache.h" |
| 17 | #include "src/core/SkTypefaceCache.h" |
| 18 | #include "src/ports/SkFontConfigTypeface.h" |
Mike Klein | 79aea6a | 2018-06-11 10:45:26 -0400 | [diff] [blame] | 19 | #include <new> |
bungeman | 7d0e3bc | 2016-08-02 07:07:33 -0700 | [diff] [blame] | 20 | |
Ben Wagner | 4212a7d | 2019-02-25 14:27:46 -0500 | [diff] [blame] | 21 | std::unique_ptr<SkStreamAsset> SkTypeface_FCI::onOpenStream(int* ttcIndex) const { |
bungeman | 7d0e3bc | 2016-08-02 07:07:33 -0700 | [diff] [blame] | 22 | *ttcIndex = this->getIdentity().fTTCIndex; |
| 23 | |
bungeman | 4bb029c | 2016-09-01 08:52:29 -0700 | [diff] [blame] | 24 | if (fFontData) { |
| 25 | SkStreamAsset* stream = fFontData->getStream(); |
| 26 | if (!stream) { |
| 27 | return nullptr; |
| 28 | } |
Ben Wagner | 4212a7d | 2019-02-25 14:27:46 -0500 | [diff] [blame] | 29 | return stream->duplicate(); |
bungeman | 7d0e3bc | 2016-08-02 07:07:33 -0700 | [diff] [blame] | 30 | } |
| 31 | |
Ben Wagner | 4212a7d | 2019-02-25 14:27:46 -0500 | [diff] [blame] | 32 | return std::unique_ptr<SkStreamAsset>(fFCI->openStream(this->getIdentity())); |
bungeman | 7d0e3bc | 2016-08-02 07:07:33 -0700 | [diff] [blame] | 33 | } |
| 34 | |
bungeman | f93d711 | 2016-09-16 06:24:20 -0700 | [diff] [blame] | 35 | std::unique_ptr<SkFontData> SkTypeface_FCI::onMakeFontData() const { |
bungeman | 4bb029c | 2016-09-01 08:52:29 -0700 | [diff] [blame] | 36 | if (fFontData) { |
Mike Klein | f46d5ca | 2019-12-11 10:45:01 -0500 | [diff] [blame] | 37 | return std::make_unique<SkFontData>(*fFontData); |
bungeman | 4bb029c | 2016-09-01 08:52:29 -0700 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | const SkFontConfigInterface::FontIdentity& id = this->getIdentity(); |
Mike Klein | f46d5ca | 2019-12-11 10:45:01 -0500 | [diff] [blame] | 41 | return std::make_unique<SkFontData>(std::unique_ptr<SkStreamAsset>(fFCI->openStream(id)), |
bungeman | f93d711 | 2016-09-16 06:24:20 -0700 | [diff] [blame] | 42 | id.fTTCIndex, nullptr, 0); |
bungeman | 4bb029c | 2016-09-01 08:52:29 -0700 | [diff] [blame] | 43 | } |
| 44 | |
bungeman | 7d0e3bc | 2016-08-02 07:07:33 -0700 | [diff] [blame] | 45 | void 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()); |
bungeman | 4bb029c | 2016-09-01 08:52:29 -0700 | [diff] [blame] | 50 | *isLocalStream = SkToBool(fFontData); |
bungeman | 7d0e3bc | 2016-08-02 07:07:33 -0700 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | /////////////////////////////////////////////////////////////////////////////// |
| 54 | |
| 55 | class SkFontStyleSet_FCI : public SkFontStyleSet { |
| 56 | public: |
| 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 | |
| 67 | class SkFontRequestCache { |
| 68 | public: |
| 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 | |
| 103 | private: |
| 104 | struct Result : public SkResourceCache::Rec { |
Ben Wagner | 68efb21 | 2019-03-04 16:15:40 -0500 | [diff] [blame] | 105 | Result(Request* request, sk_sp<SkTypeface> typeface) |
| 106 | : fRequest(request), fFace(std::move(typeface)) {} |
bungeman | 7d0e3bc | 2016-08-02 07:07:33 -0700 | [diff] [blame] | 107 | 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 Wagner | 145dbcd | 2016-11-03 14:40:50 -0400 | [diff] [blame] | 115 | std::unique_ptr<Request> fRequest; |
Hal Canary | 67b39de | 2016-11-07 11:47:44 -0500 | [diff] [blame] | 116 | sk_sp<SkTypeface> fFace; |
bungeman | 7d0e3bc | 2016-08-02 07:07:33 -0700 | [diff] [blame] | 117 | }; |
| 118 | |
| 119 | SkResourceCache fCachedResults; |
| 120 | |
| 121 | public: |
| 122 | SkFontRequestCache(size_t maxSize) : fCachedResults(maxSize) {} |
| 123 | |
| 124 | /** Takes ownership of request. It will be deleted when no longer needed. */ |
Ben Wagner | 68efb21 | 2019-03-04 16:15:40 -0500 | [diff] [blame] | 125 | void add(sk_sp<SkTypeface> face, Request* request) { |
| 126 | fCachedResults.add(new Result(request, std::move(face))); |
bungeman | 7d0e3bc | 2016-08-02 07:07:33 -0700 | [diff] [blame] | 127 | } |
| 128 | /** Does not take ownership of request. */ |
Ben Wagner | 68efb21 | 2019-03-04 16:15:40 -0500 | [diff] [blame] | 129 | sk_sp<SkTypeface> findAndRef(Request* request) { |
| 130 | sk_sp<SkTypeface> face; |
bungeman | 7d0e3bc | 2016-08-02 07:07:33 -0700 | [diff] [blame] | 131 | fCachedResults.find(*request, [](const SkResourceCache::Rec& rec, void* context) -> bool { |
| 132 | const Result& result = static_cast<const Result&>(rec); |
Ben Wagner | 68efb21 | 2019-03-04 16:15:40 -0500 | [diff] [blame] | 133 | sk_sp<SkTypeface>* face = static_cast<sk_sp<SkTypeface>*>(context); |
bungeman | 7d0e3bc | 2016-08-02 07:07:33 -0700 | [diff] [blame] | 134 | |
Ben Wagner | 68efb21 | 2019-03-04 16:15:40 -0500 | [diff] [blame] | 135 | *face = result.fFace; |
bungeman | 7d0e3bc | 2016-08-02 07:07:33 -0700 | [diff] [blame] | 136 | return true; |
| 137 | }, &face); |
Ben Wagner | 68efb21 | 2019-03-04 16:15:40 -0500 | [diff] [blame] | 138 | return face; |
bungeman | 7d0e3bc | 2016-08-02 07:07:33 -0700 | [diff] [blame] | 139 | } |
| 140 | }; |
| 141 | |
| 142 | /////////////////////////////////////////////////////////////////////////////// |
| 143 | |
| 144 | static 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 | |
| 154 | class SkFontMgr_FCI : public SkFontMgr { |
bungeman | 1ae0e01 | 2016-09-19 12:13:16 -0700 | [diff] [blame] | 155 | sk_sp<SkFontConfigInterface> fFCI; |
bungeman | 7d0e3bc | 2016-08-02 07:07:33 -0700 | [diff] [blame] | 156 | 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 | |
| 166 | public: |
bungeman | 1ae0e01 | 2016-09-19 12:13:16 -0700 | [diff] [blame] | 167 | SkFontMgr_FCI(sk_sp<SkFontConfigInterface> fci) |
| 168 | : fFCI(std::move(fci)) |
bungeman | 7d0e3bc | 2016-08-02 07:07:33 -0700 | [diff] [blame] | 169 | , fCache(kMaxSize) |
| 170 | {} |
| 171 | |
| 172 | protected: |
| 173 | int onCountFamilies() const override { |
Ben Wagner | 44a9d57 | 2018-05-15 11:06:08 -0400 | [diff] [blame] | 174 | SK_ABORT("Not implemented."); |
bungeman | 7d0e3bc | 2016-08-02 07:07:33 -0700 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | void onGetFamilyName(int index, SkString* familyName) const override { |
Ben Wagner | b4aab9a | 2017-08-16 10:53:04 -0400 | [diff] [blame] | 178 | SK_ABORT("Not implemented."); |
bungeman | 7d0e3bc | 2016-08-02 07:07:33 -0700 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | SkFontStyleSet* onCreateStyleSet(int index) const override { |
Ben Wagner | b4aab9a | 2017-08-16 10:53:04 -0400 | [diff] [blame] | 182 | SK_ABORT("Not implemented."); |
bungeman | 7d0e3bc | 2016-08-02 07:07:33 -0700 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | SkFontStyleSet* onMatchFamily(const char familyName[]) const override { |
Ben Wagner | 44a9d57 | 2018-05-15 11:06:08 -0400 | [diff] [blame] | 186 | SK_ABORT("Not implemented."); |
bungeman | 7d0e3bc | 2016-08-02 07:07:33 -0700 | [diff] [blame] | 187 | } |
| 188 | |
Ben Wagner | a1ac841 | 2018-06-27 11:01:40 -0400 | [diff] [blame] | 189 | SkTypeface* onMatchFamilyStyle(const char requestedFamilyName[], |
| 190 | const SkFontStyle& requestedStyle) const override |
| 191 | { |
Herb Derby | 9b86955 | 2019-05-10 12:16:17 -0400 | [diff] [blame] | 192 | SkAutoMutexExclusive ama(fMutex); |
Ben Wagner | a1ac841 | 2018-06-27 11:01:40 -0400 | [diff] [blame] | 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. |
Ben Wagner | 68efb21 | 2019-03-04 16:15:40 -0500 | [diff] [blame] | 204 | sk_sp<SkTypeface> face = fTFCache.findByProcAndRef(find_by_FontIdentity, &identity); |
Ben Wagner | a1ac841 | 2018-06-27 11:01:40 -0400 | [diff] [blame] | 205 | if (!face) { |
Ben Wagner | 68efb21 | 2019-03-04 16:15:40 -0500 | [diff] [blame] | 206 | face.reset(SkTypeface_FCI::Create(fFCI, identity, std::move(outFamilyName), outStyle)); |
Ben Wagner | a1ac841 | 2018-06-27 11:01:40 -0400 | [diff] [blame] | 207 | // Add this FontIdentity to the FontIdentity cache. |
| 208 | fTFCache.add(face); |
| 209 | } |
Ben Wagner | 68efb21 | 2019-03-04 16:15:40 -0500 | [diff] [blame] | 210 | return face.release(); |
Ben Wagner | 44a9d57 | 2018-05-15 11:06:08 -0400 | [diff] [blame] | 211 | } |
| 212 | |
bungeman | 7d0e3bc | 2016-08-02 07:07:33 -0700 | [diff] [blame] | 213 | SkTypeface* onMatchFamilyStyleCharacter(const char familyName[], const SkFontStyle&, |
| 214 | const char* bcp47[], int bcp47Count, |
| 215 | SkUnichar character) const override { |
Ben Wagner | 44a9d57 | 2018-05-15 11:06:08 -0400 | [diff] [blame] | 216 | SK_ABORT("Not implemented."); |
bungeman | 7d0e3bc | 2016-08-02 07:07:33 -0700 | [diff] [blame] | 217 | } |
bungeman | 7d0e3bc | 2016-08-02 07:07:33 -0700 | [diff] [blame] | 218 | |
Ben Wagner | 44a9d57 | 2018-05-15 11:06:08 -0400 | [diff] [blame] | 219 | SkTypeface* onMatchFaceStyle(const SkTypeface*, const SkFontStyle&) const override { |
| 220 | SK_ABORT("Not implemented."); |
Ben Wagner | 44a9d57 | 2018-05-15 11:06:08 -0400 | [diff] [blame] | 221 | } |
| 222 | |
Mike Reed | c81ebd2 | 2018-09-10 11:47:21 -0400 | [diff] [blame] | 223 | sk_sp<SkTypeface> onMakeFromData(sk_sp<SkData> data, int ttcIndex) const override { |
| 224 | return this->onMakeFromStreamIndex(SkMemoryStream::Make(std::move(data)), ttcIndex); |
Ben Wagner | 44a9d57 | 2018-05-15 11:06:08 -0400 | [diff] [blame] | 225 | } |
bungeman | 7d0e3bc | 2016-08-02 07:07:33 -0700 | [diff] [blame] | 226 | |
Mike Reed | 5922739 | 2017-09-26 09:46:08 -0400 | [diff] [blame] | 227 | sk_sp<SkTypeface> onMakeFromStreamIndex(std::unique_ptr<SkStreamAsset> stream, |
| 228 | int ttcIndex) const override { |
bungeman | 7d0e3bc | 2016-08-02 07:07:33 -0700 | [diff] [blame] | 229 | 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 Wagner | 2527230 | 2017-01-25 11:15:50 -0500 | [diff] [blame] | 238 | SkString name; |
bungeman | 7d0e3bc | 2016-08-02 07:07:33 -0700 | [diff] [blame] | 239 | SkFontStyle style; |
bungeman | 4bb029c | 2016-09-01 08:52:29 -0700 | [diff] [blame] | 240 | bool isFixedPitch = false; |
Ben Wagner | 2527230 | 2017-01-25 11:15:50 -0500 | [diff] [blame] | 241 | if (!fScanner.scanFont(stream.get(), 0, &name, &style, &isFixedPitch, nullptr)) { |
bungeman | 7d0e3bc | 2016-08-02 07:07:33 -0700 | [diff] [blame] | 242 | return nullptr; |
| 243 | } |
| 244 | |
Mike Klein | f46d5ca | 2019-12-11 10:45:01 -0500 | [diff] [blame] | 245 | auto fontData = std::make_unique<SkFontData>(std::move(stream), ttcIndex, nullptr, 0); |
Mike Reed | 5922739 | 2017-09-26 09:46:08 -0400 | [diff] [blame] | 246 | return sk_sp<SkTypeface>(SkTypeface_FCI::Create(std::move(fontData), std::move(name), |
| 247 | style, isFixedPitch)); |
bungeman | 4bb029c | 2016-09-01 08:52:29 -0700 | [diff] [blame] | 248 | } |
| 249 | |
Mike Reed | 5922739 | 2017-09-26 09:46:08 -0400 | [diff] [blame] | 250 | sk_sp<SkTypeface> onMakeFromStreamArgs(std::unique_ptr<SkStreamAsset> stream, |
| 251 | const SkFontArguments& args) const override { |
bungeman | 4bb029c | 2016-09-01 08:52:29 -0700 | [diff] [blame] | 252 | using Scanner = SkTypeface_FreeType::Scanner; |
bungeman | 4bb029c | 2016-09-01 08:52:29 -0700 | [diff] [blame] | 253 | 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 | |
bungeman | 4bb029c | 2016-09-01 08:52:29 -0700 | [diff] [blame] | 261 | SkString name; |
Ben Wagner | e80a595 | 2020-07-14 17:57:13 -0400 | [diff] [blame] | 262 | SkFontStyle style; |
| 263 | bool isFixedPitch = false; |
bungeman | 4bb029c | 2016-09-01 08:52:29 -0700 | [diff] [blame] | 264 | Scanner::AxisDefinitions axisDefinitions; |
Ben Wagner | fc49734 | 2017-02-24 11:15:26 -0500 | [diff] [blame] | 265 | if (!fScanner.scanFont(stream.get(), args.getCollectionIndex(), |
bungeman | f93d711 | 2016-09-16 06:24:20 -0700 | [diff] [blame] | 266 | &name, &style, &isFixedPitch, &axisDefinitions)) |
bungeman | 4bb029c | 2016-09-01 08:52:29 -0700 | [diff] [blame] | 267 | { |
| 268 | return nullptr; |
| 269 | } |
| 270 | |
bungeman | 4bb029c | 2016-09-01 08:52:29 -0700 | [diff] [blame] | 271 | SkAutoSTMalloc<4, SkFixed> axisValues(axisDefinitions.count()); |
Ben Wagner | fc49734 | 2017-02-24 11:15:26 -0500 | [diff] [blame] | 272 | Scanner::computeAxisValues(axisDefinitions, args.getVariationDesignPosition(), |
| 273 | axisValues, name); |
bungeman | 4bb029c | 2016-09-01 08:52:29 -0700 | [diff] [blame] | 274 | |
Mike Klein | f46d5ca | 2019-12-11 10:45:01 -0500 | [diff] [blame] | 275 | auto fontData = std::make_unique<SkFontData>(std::move(stream), |
Ben Wagner | e80a595 | 2020-07-14 17:57:13 -0400 | [diff] [blame] | 276 | args.getCollectionIndex(), |
| 277 | axisValues.get(), |
| 278 | axisDefinitions.count()); |
Mike Reed | 5922739 | 2017-09-26 09:46:08 -0400 | [diff] [blame] | 279 | return sk_sp<SkTypeface>(SkTypeface_FCI::Create(std::move(fontData), std::move(name), |
| 280 | style, isFixedPitch)); |
bungeman | 7d0e3bc | 2016-08-02 07:07:33 -0700 | [diff] [blame] | 281 | } |
| 282 | |
Mike Reed | 5922739 | 2017-09-26 09:46:08 -0400 | [diff] [blame] | 283 | sk_sp<SkTypeface> onMakeFromFile(const char path[], int ttcIndex) const override { |
bungeman | f93d711 | 2016-09-16 06:24:20 -0700 | [diff] [blame] | 284 | std::unique_ptr<SkStreamAsset> stream = SkStream::MakeFromFile(path); |
Mike Reed | 5922739 | 2017-09-26 09:46:08 -0400 | [diff] [blame] | 285 | return stream ? this->makeFromStream(std::move(stream), ttcIndex) : nullptr; |
bungeman | 7d0e3bc | 2016-08-02 07:07:33 -0700 | [diff] [blame] | 286 | } |
| 287 | |
Ben Wagner | e80a595 | 2020-07-14 17:57:13 -0400 | [diff] [blame] | 288 | 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 Reed | 5922739 | 2017-09-26 09:46:08 -0400 | [diff] [blame] | 310 | sk_sp<SkTypeface> onLegacyMakeTypeface(const char requestedFamilyName[], |
| 311 | SkFontStyle requestedStyle) const override |
bungeman | 7d0e3bc | 2016-08-02 07:07:33 -0700 | [diff] [blame] | 312 | { |
Herb Derby | 9b86955 | 2019-05-10 12:16:17 -0400 | [diff] [blame] | 313 | SkAutoMutexExclusive ama(fMutex); |
bungeman | 7d0e3bc | 2016-08-02 07:07:33 -0700 | [diff] [blame] | 314 | |
| 315 | // Check if this request is already in the request cache. |
| 316 | using Request = SkFontRequestCache::Request; |
Ben Wagner | 145dbcd | 2016-11-03 14:40:50 -0400 | [diff] [blame] | 317 | std::unique_ptr<Request> request(Request::Create(requestedFamilyName, requestedStyle)); |
Ben Wagner | 68efb21 | 2019-03-04 16:15:40 -0500 | [diff] [blame] | 318 | sk_sp<SkTypeface> face = fCache.findAndRef(request.get()); |
bungeman | 7d0e3bc | 2016-08-02 07:07:33 -0700 | [diff] [blame] | 319 | if (face) { |
Mike Reed | 5922739 | 2017-09-26 09:46:08 -0400 | [diff] [blame] | 320 | return sk_sp<SkTypeface>(face); |
bungeman | 7d0e3bc | 2016-08-02 07:07:33 -0700 | [diff] [blame] | 321 | } |
| 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 Wagner | 68efb21 | 2019-03-04 16:15:40 -0500 | [diff] [blame] | 335 | face.reset(SkTypeface_FCI::Create(fFCI, identity, std::move(outFamilyName), outStyle)); |
bungeman | 7d0e3bc | 2016-08-02 07:07:33 -0700 | [diff] [blame] | 336 | // 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 Wagner | 68efb21 | 2019-03-04 16:15:40 -0500 | [diff] [blame] | 342 | return face; |
bungeman | 7d0e3bc | 2016-08-02 07:07:33 -0700 | [diff] [blame] | 343 | } |
| 344 | }; |
| 345 | |
Ben Wagner | 3546ff1 | 2017-01-03 13:32:36 -0500 | [diff] [blame] | 346 | SK_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 | } |