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