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 | |
| 11 | #include "SkDataTable.h" |
| 12 | #include "SkFontMgr.h" |
| 13 | #include "SkFontStyle.h" |
mtklein | 1b24933 | 2015-07-07 12:21:21 -0700 | [diff] [blame^] | 14 | #include "SkMutex.h" |
bungeman@google.com | 72cf4fc | 2014-03-21 22:48:32 +0000 | [diff] [blame] | 15 | #include "SkRemotableFontMgr.h" |
| 16 | #include "SkTArray.h" |
| 17 | #include "SkTypeface.h" |
| 18 | |
| 19 | class SkData; |
| 20 | class SkStream; |
| 21 | class SkString; |
| 22 | class SkTypeface; |
| 23 | |
| 24 | class SK_API SkFontMgr_Indirect : public SkFontMgr { |
| 25 | public: |
| 26 | // TODO: The SkFontMgr is only used for createFromStream/File/Data. |
| 27 | // In the future these calls should be broken out into their own interface |
| 28 | // with a name like SkFontRenderer. |
| 29 | SkFontMgr_Indirect(SkFontMgr* impl, SkRemotableFontMgr* proxy) |
bungeman@google.com | 1ba6262 | 2014-03-24 18:38:25 +0000 | [diff] [blame] | 30 | : fImpl(SkRef(impl)), fProxy(SkRef(proxy)), fFamilyNamesInited(false) |
| 31 | { } |
bungeman@google.com | 72cf4fc | 2014-03-21 22:48:32 +0000 | [diff] [blame] | 32 | |
| 33 | protected: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 34 | int onCountFamilies() const override; |
| 35 | void onGetFamilyName(int index, SkString* familyName) const override; |
| 36 | SkFontStyleSet* onCreateStyleSet(int index) const override; |
bungeman@google.com | 72cf4fc | 2014-03-21 22:48:32 +0000 | [diff] [blame] | 37 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 38 | SkFontStyleSet* onMatchFamily(const char familyName[]) const override; |
bungeman@google.com | 72cf4fc | 2014-03-21 22:48:32 +0000 | [diff] [blame] | 39 | |
| 40 | virtual SkTypeface* onMatchFamilyStyle(const char familyName[], |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 41 | const SkFontStyle& fontStyle) const override; |
bungeman@google.com | 72cf4fc | 2014-03-21 22:48:32 +0000 | [diff] [blame] | 42 | |
| 43 | virtual SkTypeface* onMatchFamilyStyleCharacter(const char familyName[], |
| 44 | const SkFontStyle&, |
bungeman | c20386e | 2014-10-23 07:08:05 -0700 | [diff] [blame] | 45 | const char* bcp47[], |
| 46 | int bcp47Count, |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 47 | SkUnichar character) const override; |
bungeman@google.com | 72cf4fc | 2014-03-21 22:48:32 +0000 | [diff] [blame] | 48 | |
| 49 | virtual SkTypeface* onMatchFaceStyle(const SkTypeface* familyMember, |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 50 | const SkFontStyle& fontStyle) const override; |
bungeman@google.com | 72cf4fc | 2014-03-21 22:48:32 +0000 | [diff] [blame] | 51 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 52 | SkTypeface* onCreateFromStream(SkStreamAsset* stream, int ttcIndex) const override; |
| 53 | SkTypeface* onCreateFromFile(const char path[], int ttcIndex) const override; |
| 54 | SkTypeface* onCreateFromData(SkData* data, int ttcIndex) const override; |
bungeman@google.com | 72cf4fc | 2014-03-21 22:48:32 +0000 | [diff] [blame] | 55 | |
| 56 | virtual SkTypeface* onLegacyCreateTypeface(const char familyName[], |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 57 | unsigned styleBits) const override; |
bungeman@google.com | 72cf4fc | 2014-03-21 22:48:32 +0000 | [diff] [blame] | 58 | |
| 59 | private: |
| 60 | SkTypeface* createTypefaceFromFontId(const SkFontIdentity& fontId) const; |
| 61 | |
| 62 | SkAutoTUnref<SkFontMgr> fImpl; |
| 63 | SkAutoTUnref<SkRemotableFontMgr> fProxy; |
| 64 | |
| 65 | struct DataEntry { |
bungeman@google.com | 3d21f21 | 2014-03-21 23:00:41 +0000 | [diff] [blame] | 66 | uint32_t fDataId; // key1 |
bungeman@google.com | f76cbb8 | 2014-03-21 23:07:24 +0000 | [diff] [blame] | 67 | uint32_t fTtcIndex; // key2 |
bungeman@google.com | 72cf4fc | 2014-03-21 22:48:32 +0000 | [diff] [blame] | 68 | SkTypeface* fTypeface; // value: weak ref to typeface |
| 69 | |
| 70 | DataEntry() { } |
| 71 | |
| 72 | // This is a move!!! |
| 73 | DataEntry(DataEntry& that) |
| 74 | : fDataId(that.fDataId) |
| 75 | , fTtcIndex(that.fTtcIndex) |
| 76 | , fTypeface(that.fTypeface) |
| 77 | { |
bungeman@google.com | c8f0d60 | 2014-03-22 00:25:34 +0000 | [diff] [blame] | 78 | SkDEBUGCODE(that.fDataId = SkFontIdentity::kInvalidDataId;) |
| 79 | SkDEBUGCODE(that.fTtcIndex = 0xbbadbeef;) |
bungeman@google.com | 72cf4fc | 2014-03-21 22:48:32 +0000 | [diff] [blame] | 80 | that.fTypeface = NULL; |
| 81 | } |
| 82 | |
| 83 | ~DataEntry() { |
| 84 | if (fTypeface) { |
| 85 | fTypeface->weak_unref(); |
| 86 | } |
| 87 | } |
| 88 | }; |
| 89 | /** |
| 90 | * This cache is essentially { dataId: { ttcIndex: typeface } } |
| 91 | * For data caching we want a mapping from data id to weak references to |
| 92 | * typefaces with that data id. By storing the index next to the typeface, |
| 93 | * this data cache also acts as a typeface cache. |
| 94 | */ |
| 95 | mutable SkTArray<DataEntry> fDataCache; |
| 96 | mutable SkMutex fDataCacheMutex; |
| 97 | |
| 98 | mutable SkAutoTUnref<SkDataTable> fFamilyNames; |
bungeman@google.com | 1ba6262 | 2014-03-24 18:38:25 +0000 | [diff] [blame] | 99 | mutable bool fFamilyNamesInited; |
| 100 | mutable SkMutex fFamilyNamesMutex; |
bungeman@google.com | 72cf4fc | 2014-03-21 22:48:32 +0000 | [diff] [blame] | 101 | static void set_up_family_names(const SkFontMgr_Indirect* self); |
| 102 | |
| 103 | friend class SkStyleSet_Indirect; |
| 104 | }; |
| 105 | |
| 106 | #endif |