bungeman@google.com | 72cf4fc | 2014-03-21 22:48:32 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014 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 | |
bungeman | f20488b | 2015-07-29 11:49:40 -0700 | [diff] [blame] | 8 | #include "SkFontMgr.h" |
| 9 | #include "SkFontMgr_indirect.h" |
bungeman@google.com | 72cf4fc | 2014-03-21 22:48:32 +0000 | [diff] [blame] | 10 | #include "SkFontStyle.h" |
bungeman | f20488b | 2015-07-29 11:49:40 -0700 | [diff] [blame] | 11 | #include "SkMutex.h" |
bungeman@google.com | 72cf4fc | 2014-03-21 22:48:32 +0000 | [diff] [blame] | 12 | #include "SkOnce.h" |
bungeman | f20488b | 2015-07-29 11:49:40 -0700 | [diff] [blame] | 13 | #include "SkRefCnt.h" |
| 14 | #include "SkRemotableFontMgr.h" |
bungeman@google.com | 72cf4fc | 2014-03-21 22:48:32 +0000 | [diff] [blame] | 15 | #include "SkStream.h" |
bungeman | f20488b | 2015-07-29 11:49:40 -0700 | [diff] [blame] | 16 | #include "SkString.h" |
| 17 | #include "SkTArray.h" |
bungeman@google.com | 72cf4fc | 2014-03-21 22:48:32 +0000 | [diff] [blame] | 18 | #include "SkTypeface.h" |
bungeman | f20488b | 2015-07-29 11:49:40 -0700 | [diff] [blame] | 19 | #include "SkTypes.h" |
| 20 | #include "SkTemplates.h" |
bungeman@google.com | 72cf4fc | 2014-03-21 22:48:32 +0000 | [diff] [blame] | 21 | |
| 22 | class SkData; |
bungeman@google.com | 72cf4fc | 2014-03-21 22:48:32 +0000 | [diff] [blame] | 23 | |
| 24 | class SkStyleSet_Indirect : public SkFontStyleSet { |
| 25 | public: |
| 26 | /** Takes ownership of the SkRemotableFontIdentitySet. */ |
| 27 | SkStyleSet_Indirect(const SkFontMgr_Indirect* owner, int familyIndex, |
| 28 | SkRemotableFontIdentitySet* data) |
| 29 | : fOwner(SkRef(owner)), fFamilyIndex(familyIndex), fData(data) |
| 30 | { } |
| 31 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 32 | int count() override { return fData->count(); } |
bungeman@google.com | 72cf4fc | 2014-03-21 22:48:32 +0000 | [diff] [blame] | 33 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 34 | void getStyle(int index, SkFontStyle* fs, SkString* style) override { |
bungeman@google.com | 72cf4fc | 2014-03-21 22:48:32 +0000 | [diff] [blame] | 35 | if (fs) { |
| 36 | *fs = fData->at(index).fFontStyle; |
| 37 | } |
| 38 | if (style) { |
| 39 | // TODO: is this useful? Current locale? |
| 40 | style->reset(); |
| 41 | } |
| 42 | } |
| 43 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 44 | SkTypeface* createTypeface(int index) override { |
bungeman@google.com | 72cf4fc | 2014-03-21 22:48:32 +0000 | [diff] [blame] | 45 | return fOwner->createTypefaceFromFontId(fData->at(index)); |
| 46 | } |
| 47 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 48 | SkTypeface* matchStyle(const SkFontStyle& pattern) override { |
bungeman@google.com | 72cf4fc | 2014-03-21 22:48:32 +0000 | [diff] [blame] | 49 | if (fFamilyIndex >= 0) { |
| 50 | SkFontIdentity id = fOwner->fProxy->matchIndexStyle(fFamilyIndex, pattern); |
| 51 | return fOwner->createTypefaceFromFontId(id); |
| 52 | } |
| 53 | |
bungeman | 147ea2f | 2015-11-12 09:50:08 -0800 | [diff] [blame] | 54 | return this->matchStyleCSS3(pattern); |
bungeman@google.com | 72cf4fc | 2014-03-21 22:48:32 +0000 | [diff] [blame] | 55 | } |
| 56 | private: |
Hal Canary | 67b39de | 2016-11-07 11:47:44 -0500 | [diff] [blame] | 57 | sk_sp<const SkFontMgr_Indirect> fOwner; |
bungeman@google.com | 72cf4fc | 2014-03-21 22:48:32 +0000 | [diff] [blame] | 58 | int fFamilyIndex; |
Hal Canary | 67b39de | 2016-11-07 11:47:44 -0500 | [diff] [blame] | 59 | sk_sp<SkRemotableFontIdentitySet> fData; |
bungeman@google.com | 72cf4fc | 2014-03-21 22:48:32 +0000 | [diff] [blame] | 60 | }; |
| 61 | |
bungeman@google.com | 72cf4fc | 2014-03-21 22:48:32 +0000 | [diff] [blame] | 62 | int SkFontMgr_Indirect::onCountFamilies() const { |
Herb Derby | 9adfef8 | 2017-01-20 16:07:52 -0500 | [diff] [blame] | 63 | return 0; |
bungeman@google.com | 72cf4fc | 2014-03-21 22:48:32 +0000 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | void SkFontMgr_Indirect::onGetFamilyName(int index, SkString* familyName) const { |
Ben Wagner | b4aab9a | 2017-08-16 10:53:04 -0400 | [diff] [blame] | 67 | SK_ABORT("Not implemented"); |
bungeman@google.com | 72cf4fc | 2014-03-21 22:48:32 +0000 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | SkFontStyleSet* SkFontMgr_Indirect::onCreateStyleSet(int index) const { |
Ben Wagner | b4aab9a | 2017-08-16 10:53:04 -0400 | [diff] [blame] | 71 | SK_ABORT("Not implemented"); |
Herb Derby | 9adfef8 | 2017-01-20 16:07:52 -0500 | [diff] [blame] | 72 | return nullptr; |
bungeman@google.com | 72cf4fc | 2014-03-21 22:48:32 +0000 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | SkFontStyleSet* SkFontMgr_Indirect::onMatchFamily(const char familyName[]) const { |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 76 | return new SkStyleSet_Indirect(this, -1, fProxy->matchName(familyName)); |
bungeman@google.com | 72cf4fc | 2014-03-21 22:48:32 +0000 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | SkTypeface* SkFontMgr_Indirect::createTypefaceFromFontId(const SkFontIdentity& id) const { |
| 80 | if (id.fDataId == SkFontIdentity::kInvalidDataId) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 81 | return nullptr; |
bungeman@google.com | 72cf4fc | 2014-03-21 22:48:32 +0000 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | SkAutoMutexAcquire ama(fDataCacheMutex); |
| 85 | |
Hal Canary | 67b39de | 2016-11-07 11:47:44 -0500 | [diff] [blame] | 86 | sk_sp<SkTypeface> dataTypeface; |
bungeman@google.com | 72cf4fc | 2014-03-21 22:48:32 +0000 | [diff] [blame] | 87 | int dataTypefaceIndex = 0; |
| 88 | for (int i = 0; i < fDataCache.count(); ++i) { |
| 89 | const DataEntry& entry = fDataCache[i]; |
| 90 | if (entry.fDataId == id.fDataId) { |
| 91 | if (entry.fTtcIndex == id.fTtcIndex && |
| 92 | !entry.fTypeface->weak_expired() && entry.fTypeface->try_ref()) |
| 93 | { |
| 94 | return entry.fTypeface; |
| 95 | } |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 96 | if (dataTypeface.get() == nullptr && |
bungeman@google.com | 72cf4fc | 2014-03-21 22:48:32 +0000 | [diff] [blame] | 97 | !entry.fTypeface->weak_expired() && entry.fTypeface->try_ref()) |
| 98 | { |
| 99 | dataTypeface.reset(entry.fTypeface); |
| 100 | dataTypefaceIndex = entry.fTtcIndex; |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | if (entry.fTypeface->weak_expired()) { |
| 105 | fDataCache.removeShuffle(i); |
| 106 | --i; |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | // No exact match, but did find a data match. |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 111 | if (dataTypeface.get() != nullptr) { |
Ben Wagner | 145dbcd | 2016-11-03 14:40:50 -0400 | [diff] [blame] | 112 | std::unique_ptr<SkStreamAsset> stream(dataTypeface->openStream(nullptr)); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 113 | if (stream.get() != nullptr) { |
Mike Reed | 5922739 | 2017-09-26 09:46:08 -0400 | [diff] [blame] | 114 | return fImpl->makeFromStream(std::move(stream), dataTypefaceIndex).release(); |
bungeman@google.com | 72cf4fc | 2014-03-21 22:48:32 +0000 | [diff] [blame] | 115 | } |
| 116 | } |
| 117 | |
| 118 | // No data match, request data and add entry. |
Ben Wagner | 145dbcd | 2016-11-03 14:40:50 -0400 | [diff] [blame] | 119 | std::unique_ptr<SkStreamAsset> stream(fProxy->getData(id.fDataId)); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 120 | if (stream.get() == nullptr) { |
| 121 | return nullptr; |
bungeman@google.com | 72cf4fc | 2014-03-21 22:48:32 +0000 | [diff] [blame] | 122 | } |
| 123 | |
Mike Reed | 5922739 | 2017-09-26 09:46:08 -0400 | [diff] [blame] | 124 | sk_sp<SkTypeface> typeface(fImpl->makeFromStream(std::move(stream), id.fTtcIndex)); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 125 | if (typeface.get() == nullptr) { |
| 126 | return nullptr; |
bungeman@google.com | 72cf4fc | 2014-03-21 22:48:32 +0000 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | DataEntry& newEntry = fDataCache.push_back(); |
| 130 | typeface->weak_ref(); |
| 131 | newEntry.fDataId = id.fDataId; |
| 132 | newEntry.fTtcIndex = id.fTtcIndex; |
| 133 | newEntry.fTypeface = typeface.get(); // weak reference passed to new entry. |
| 134 | |
mtklein | 18300a3 | 2016-03-16 13:53:35 -0700 | [diff] [blame] | 135 | return typeface.release(); |
bungeman@google.com | 72cf4fc | 2014-03-21 22:48:32 +0000 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | SkTypeface* SkFontMgr_Indirect::onMatchFamilyStyle(const char familyName[], |
| 139 | const SkFontStyle& fontStyle) const { |
| 140 | SkFontIdentity id = fProxy->matchNameStyle(familyName, fontStyle); |
| 141 | return this->createTypefaceFromFontId(id); |
| 142 | } |
| 143 | |
| 144 | SkTypeface* SkFontMgr_Indirect::onMatchFamilyStyleCharacter(const char familyName[], |
| 145 | const SkFontStyle& style, |
bungeman | c20386e | 2014-10-23 07:08:05 -0700 | [diff] [blame] | 146 | const char* bcp47[], |
| 147 | int bcp47Count, |
| 148 | SkUnichar character) const { |
| 149 | SkFontIdentity id = fProxy->matchNameStyleCharacter(familyName, style, bcp47, |
| 150 | bcp47Count, character); |
bungeman@google.com | 72cf4fc | 2014-03-21 22:48:32 +0000 | [diff] [blame] | 151 | return this->createTypefaceFromFontId(id); |
| 152 | } |
bungeman@google.com | 72cf4fc | 2014-03-21 22:48:32 +0000 | [diff] [blame] | 153 | |
| 154 | SkTypeface* SkFontMgr_Indirect::onMatchFaceStyle(const SkTypeface* familyMember, |
| 155 | const SkFontStyle& fontStyle) const { |
| 156 | SkString familyName; |
| 157 | familyMember->getFamilyName(&familyName); |
| 158 | return this->matchFamilyStyle(familyName.c_str(), fontStyle); |
| 159 | } |
| 160 | |
Mike Reed | 5922739 | 2017-09-26 09:46:08 -0400 | [diff] [blame] | 161 | sk_sp<SkTypeface> SkFontMgr_Indirect::onMakeFromStreamIndex(std::unique_ptr<SkStreamAsset> stream, |
| 162 | int ttcIndex) const { |
| 163 | return fImpl->makeFromStream(std::move(stream), ttcIndex); |
bungeman@google.com | 72cf4fc | 2014-03-21 22:48:32 +0000 | [diff] [blame] | 164 | } |
| 165 | |
Mike Reed | 5922739 | 2017-09-26 09:46:08 -0400 | [diff] [blame] | 166 | sk_sp<SkTypeface> SkFontMgr_Indirect::onMakeFromFile(const char path[], int ttcIndex) const { |
| 167 | return fImpl->makeFromFile(path, ttcIndex); |
bungeman@google.com | 72cf4fc | 2014-03-21 22:48:32 +0000 | [diff] [blame] | 168 | } |
| 169 | |
Mike Reed | 5922739 | 2017-09-26 09:46:08 -0400 | [diff] [blame] | 170 | sk_sp<SkTypeface> SkFontMgr_Indirect::onMakeFromData(sk_sp<SkData> data, int ttcIndex) const { |
| 171 | return fImpl->makeFromData(std::move(data), ttcIndex); |
bungeman@google.com | 72cf4fc | 2014-03-21 22:48:32 +0000 | [diff] [blame] | 172 | } |
| 173 | |
Mike Reed | 5922739 | 2017-09-26 09:46:08 -0400 | [diff] [blame] | 174 | sk_sp<SkTypeface> SkFontMgr_Indirect::onLegacyMakeTypeface(const char familyName[], |
| 175 | SkFontStyle style) const { |
Hal Canary | 67b39de | 2016-11-07 11:47:44 -0500 | [diff] [blame] | 176 | sk_sp<SkTypeface> face(this->matchFamilyStyle(familyName, style)); |
bungeman@google.com | 72cf4fc | 2014-03-21 22:48:32 +0000 | [diff] [blame] | 177 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 178 | if (nullptr == face.get()) { |
| 179 | face.reset(this->matchFamilyStyle(nullptr, style)); |
bungeman@google.com | 72cf4fc | 2014-03-21 22:48:32 +0000 | [diff] [blame] | 180 | } |
| 181 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 182 | if (nullptr == face.get()) { |
bungeman@google.com | 72cf4fc | 2014-03-21 22:48:32 +0000 | [diff] [blame] | 183 | SkFontIdentity fontId = this->fProxy->matchIndexStyle(0, style); |
| 184 | face.reset(this->createTypefaceFromFontId(fontId)); |
| 185 | } |
| 186 | |
Mike Reed | 5922739 | 2017-09-26 09:46:08 -0400 | [diff] [blame] | 187 | return face; |
bungeman@google.com | 72cf4fc | 2014-03-21 22:48:32 +0000 | [diff] [blame] | 188 | } |