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 11 #include "include/core/SkFontMgr.h" 12 #include "include/core/SkRefCnt.h" 13 #include "include/core/SkTypeface.h" 14 #include "include/core/SkTypes.h" 15 #include "include/ports/SkRemotableFontMgr.h" 16 #include "include/private/SkMutex.h" 17 #include "include/private/SkOnce.h" 18 #include "include/private/SkTArray.h" 19 20 class SkData; 21 class SkFontStyle; 22 class SkStreamAsset; 23 class SkString; 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. SkFontMgr_Indirect(sk_sp<SkFontMgr> impl,sk_sp<SkRemotableFontMgr> proxy)30 SkFontMgr_Indirect(sk_sp<SkFontMgr> impl, sk_sp<SkRemotableFontMgr> proxy) 31 : fImpl(std::move(impl)), fProxy(std::move(proxy)) 32 { } 33 34 protected: 35 int onCountFamilies() const override; 36 void onGetFamilyName(int index, SkString* familyName) const override; 37 SkFontStyleSet* onCreateStyleSet(int index) const override; 38 39 SkFontStyleSet* onMatchFamily(const char familyName[]) const override; 40 41 SkTypeface* onMatchFamilyStyle(const char familyName[], 42 const SkFontStyle& fontStyle) const override; 43 44 SkTypeface* onMatchFamilyStyleCharacter(const char familyName[], 45 const SkFontStyle&, 46 const char* bcp47[], 47 int bcp47Count, 48 SkUnichar character) const override; 49 50 sk_sp<SkTypeface> onMakeFromStreamIndex(std::unique_ptr<SkStreamAsset>, int ttcIndex) const override; 51 sk_sp<SkTypeface> onMakeFromStreamArgs(std::unique_ptr<SkStreamAsset> stream, 52 const SkFontArguments& args) const override; 53 sk_sp<SkTypeface> onMakeFromFile(const char path[], int ttcIndex) const override; 54 sk_sp<SkTypeface> onMakeFromData(sk_sp<SkData>, int ttcIndex) const override; 55 sk_sp<SkTypeface> onLegacyMakeTypeface(const char familyName[], SkFontStyle) const override; 56 57 private: 58 SkTypeface* createTypefaceFromFontId(const SkFontIdentity& fontId) const; 59 60 sk_sp<SkFontMgr> fImpl; 61 sk_sp<SkRemotableFontMgr> fProxy; 62 63 struct DataEntry { 64 uint32_t fDataId; // key1 65 uint32_t fTtcIndex; // key2 66 SkTypeface* fTypeface; // value: weak ref to typeface 67 68 DataEntry() = default; 69 DataEntryDataEntry70 DataEntry(DataEntry&& that) { *this = std::move(that); } 71 DataEntry& operator=(DataEntry&& that) { 72 if (this != &that) { 73 fDataId = that.fDataId; 74 fTtcIndex = that.fTtcIndex; 75 fTypeface = that.fTypeface; 76 77 SkDEBUGCODE(that.fDataId = SkFontIdentity::kInvalidDataId;) 78 SkDEBUGCODE(that.fTtcIndex = 0xbbadbeef;) 79 that.fTypeface = nullptr; 80 } 81 return *this; 82 } 83 ~DataEntryDataEntry84 ~DataEntry() { 85 if (fTypeface) { 86 fTypeface->weak_unref(); 87 } 88 } 89 }; 90 /** 91 * This cache is essentially { dataId: { ttcIndex: typeface } } 92 * For data caching we want a mapping from data id to weak references to 93 * typefaces with that data id. By storing the index next to the typeface, 94 * this data cache also acts as a typeface cache. 95 */ 96 mutable SkTArray<DataEntry> fDataCache; 97 mutable SkMutex fDataCacheMutex; 98 99 friend class SkStyleSet_Indirect; 100 }; 101 102 #endif 103