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"
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.
SkFontMgr_Indirect(SkFontMgr * impl,SkRemotableFontMgr * proxy)28     SkFontMgr_Indirect(SkFontMgr* impl, SkRemotableFontMgr* proxy)
29         : fImpl(SkRef(impl)), fProxy(SkRef(proxy)), fFamilyNamesInited(false)
30     { }
31 
32 protected:
33     int onCountFamilies() const override;
34     void onGetFamilyName(int index, SkString* familyName) const override;
35     SkFontStyleSet* onCreateStyleSet(int index) const override;
36 
37     SkFontStyleSet* onMatchFamily(const char familyName[]) const override;
38 
39     virtual SkTypeface* onMatchFamilyStyle(const char familyName[],
40                                            const SkFontStyle& fontStyle) const override;
41 
42     virtual SkTypeface* onMatchFamilyStyleCharacter(const char familyName[],
43                                                     const SkFontStyle&,
44                                                     const char* bcp47[],
45                                                     int bcp47Count,
46                                                     SkUnichar character) const override;
47 
48     virtual SkTypeface* onMatchFaceStyle(const SkTypeface* familyMember,
49                                          const SkFontStyle& fontStyle) const override;
50 
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;
54 
55     virtual SkTypeface* onLegacyCreateTypeface(const char familyName[],
56                                                unsigned styleBits) const override;
57 
58 private:
59     SkTypeface* createTypefaceFromFontId(const SkFontIdentity& fontId) const;
60 
61     SkAutoTUnref<SkFontMgr> fImpl;
62     SkAutoTUnref<SkRemotableFontMgr> fProxy;
63 
64     struct DataEntry {
65         uint32_t fDataId;  // key1
66         uint32_t fTtcIndex;  // key2
67         SkTypeface* fTypeface;  // value: weak ref to typeface
68 
DataEntryDataEntry69         DataEntry() { }
70 
71         // This is a move!!!
DataEntryDataEntry72         DataEntry(DataEntry& 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 = NULL;
80         }
81 
~DataEntryDataEntry82         ~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;
98     mutable bool fFamilyNamesInited;
99     mutable SkMutex fFamilyNamesMutex;
100     static void set_up_family_names(const SkFontMgr_Indirect* self);
101 
102     friend class SkStyleSet_Indirect;
103 };
104 
105 #endif
106