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 | |
| 8 | #ifndef SkFontMgr_indirect_DEFINED |
| 9 | #define SkFontMgr_indirect_DEFINED |
| 10 | |
bungeman | bf521ff | 2016-02-17 13:13:44 -0800 | [diff] [blame] | 11 | #include "../private/SkMutex.h" |
mtklein | d9dd428 | 2016-04-18 08:09:11 -0700 | [diff] [blame] | 12 | #include "../private/SkOnce.h" |
bungeman | bf521ff | 2016-02-17 13:13:44 -0800 | [diff] [blame] | 13 | #include "../private/SkTArray.h" |
bungeman@google.com | 72cf4fc | 2014-03-21 22:48:32 +0000 | [diff] [blame] | 14 | #include "SkFontMgr.h" |
bungeman | f20488b | 2015-07-29 11:49:40 -0700 | [diff] [blame] | 15 | #include "SkRefCnt.h" |
bungeman@google.com | 72cf4fc | 2014-03-21 22:48:32 +0000 | [diff] [blame] | 16 | #include "SkRemotableFontMgr.h" |
bungeman@google.com | 72cf4fc | 2014-03-21 22:48:32 +0000 | [diff] [blame] | 17 | #include "SkTypeface.h" |
bungeman | f20488b | 2015-07-29 11:49:40 -0700 | [diff] [blame] | 18 | #include "SkTypes.h" |
bungeman@google.com | 72cf4fc | 2014-03-21 22:48:32 +0000 | [diff] [blame] | 19 | |
| 20 | class SkData; |
bungeman | f20488b | 2015-07-29 11:49:40 -0700 | [diff] [blame] | 21 | class SkFontStyle; |
| 22 | class SkStreamAsset; |
bungeman@google.com | 72cf4fc | 2014-03-21 22:48:32 +0000 | [diff] [blame] | 23 | class SkString; |
bungeman@google.com | 72cf4fc | 2014-03-21 22:48:32 +0000 | [diff] [blame] | 24 | |
| 25 | class SK_API SkFontMgr_Indirect : public SkFontMgr { |
| 26 | public: |
| 27 | // TODO: The SkFontMgr is only used for createFromStream/File/Data. |
| 28 | // In the future these calls should be broken out into their own interface |
| 29 | // with a name like SkFontRenderer. |
bungeman | 6bd5284 | 2016-10-27 09:30:08 -0700 | [diff] [blame] | 30 | SkFontMgr_Indirect(sk_sp<SkFontMgr> impl, sk_sp<SkRemotableFontMgr> proxy) |
| 31 | : fImpl(std::move(impl)), fProxy(std::move(proxy)) |
bungeman@google.com | 1ba6262 | 2014-03-24 18:38:25 +0000 | [diff] [blame] | 32 | { } |
bungeman@google.com | 72cf4fc | 2014-03-21 22:48:32 +0000 | [diff] [blame] | 33 | |
| 34 | protected: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 35 | int onCountFamilies() const override; |
| 36 | void onGetFamilyName(int index, SkString* familyName) const override; |
| 37 | SkFontStyleSet* onCreateStyleSet(int index) const override; |
bungeman@google.com | 72cf4fc | 2014-03-21 22:48:32 +0000 | [diff] [blame] | 38 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 39 | SkFontStyleSet* onMatchFamily(const char familyName[]) const override; |
bungeman@google.com | 72cf4fc | 2014-03-21 22:48:32 +0000 | [diff] [blame] | 40 | |
bungeman | 11a77c6 | 2016-04-12 13:45:06 -0700 | [diff] [blame] | 41 | SkTypeface* onMatchFamilyStyle(const char familyName[], |
| 42 | const SkFontStyle& fontStyle) const override; |
bungeman@google.com | 72cf4fc | 2014-03-21 22:48:32 +0000 | [diff] [blame] | 43 | |
bungeman | 11a77c6 | 2016-04-12 13:45:06 -0700 | [diff] [blame] | 44 | SkTypeface* onMatchFamilyStyleCharacter(const char familyName[], |
| 45 | const SkFontStyle&, |
| 46 | const char* bcp47[], |
| 47 | int bcp47Count, |
| 48 | SkUnichar character) const override; |
bungeman@google.com | 72cf4fc | 2014-03-21 22:48:32 +0000 | [diff] [blame] | 49 | |
bungeman | 11a77c6 | 2016-04-12 13:45:06 -0700 | [diff] [blame] | 50 | SkTypeface* onMatchFaceStyle(const SkTypeface* familyMember, |
| 51 | const SkFontStyle& fontStyle) const override; |
bungeman@google.com | 72cf4fc | 2014-03-21 22:48:32 +0000 | [diff] [blame] | 52 | |
Mike Reed | 5922739 | 2017-09-26 09:46:08 -0400 | [diff] [blame] | 53 | sk_sp<SkTypeface> onMakeFromStreamIndex(std::unique_ptr<SkStreamAsset>, int ttcIndex) const override; |
| 54 | sk_sp<SkTypeface> onMakeFromFile(const char path[], int ttcIndex) const override; |
| 55 | sk_sp<SkTypeface> onMakeFromData(sk_sp<SkData>, int ttcIndex) const override; |
| 56 | sk_sp<SkTypeface> onLegacyMakeTypeface(const char familyName[], SkFontStyle) const override; |
bungeman@google.com | 72cf4fc | 2014-03-21 22:48:32 +0000 | [diff] [blame] | 57 | |
| 58 | private: |
| 59 | SkTypeface* createTypefaceFromFontId(const SkFontIdentity& fontId) const; |
| 60 | |
bungeman | 6bd5284 | 2016-10-27 09:30:08 -0700 | [diff] [blame] | 61 | sk_sp<SkFontMgr> fImpl; |
| 62 | sk_sp<SkRemotableFontMgr> fProxy; |
bungeman@google.com | 72cf4fc | 2014-03-21 22:48:32 +0000 | [diff] [blame] | 63 | |
| 64 | struct DataEntry { |
bungeman@google.com | 3d21f21 | 2014-03-21 23:00:41 +0000 | [diff] [blame] | 65 | uint32_t fDataId; // key1 |
bungeman@google.com | f76cbb8 | 2014-03-21 23:07:24 +0000 | [diff] [blame] | 66 | uint32_t fTtcIndex; // key2 |
bungeman@google.com | 72cf4fc | 2014-03-21 22:48:32 +0000 | [diff] [blame] | 67 | SkTypeface* fTypeface; // value: weak ref to typeface |
| 68 | |
| 69 | DataEntry() { } |
| 70 | |
bungeman | 918090c | 2016-02-09 09:14:28 -0800 | [diff] [blame] | 71 | DataEntry(DataEntry&& that) |
bungeman@google.com | 72cf4fc | 2014-03-21 22:48:32 +0000 | [diff] [blame] | 72 | : fDataId(that.fDataId) |
| 73 | , fTtcIndex(that.fTtcIndex) |
| 74 | , fTypeface(that.fTypeface) |
| 75 | { |
bungeman@google.com | c8f0d60 | 2014-03-22 00:25:34 +0000 | [diff] [blame] | 76 | SkDEBUGCODE(that.fDataId = SkFontIdentity::kInvalidDataId;) |
| 77 | SkDEBUGCODE(that.fTtcIndex = 0xbbadbeef;) |
Ben Wagner | a93a14a | 2017-08-28 10:34:05 -0400 | [diff] [blame] | 78 | that.fTypeface = nullptr; |
bungeman@google.com | 72cf4fc | 2014-03-21 22:48:32 +0000 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | ~DataEntry() { |
| 82 | if (fTypeface) { |
| 83 | fTypeface->weak_unref(); |
| 84 | } |
| 85 | } |
| 86 | }; |
| 87 | /** |
| 88 | * This cache is essentially { dataId: { ttcIndex: typeface } } |
| 89 | * For data caching we want a mapping from data id to weak references to |
| 90 | * typefaces with that data id. By storing the index next to the typeface, |
| 91 | * this data cache also acts as a typeface cache. |
| 92 | */ |
| 93 | mutable SkTArray<DataEntry> fDataCache; |
| 94 | mutable SkMutex fDataCacheMutex; |
| 95 | |
bungeman@google.com | 72cf4fc | 2014-03-21 22:48:32 +0000 | [diff] [blame] | 96 | friend class SkStyleSet_Indirect; |
| 97 | }; |
| 98 | |
| 99 | #endif |