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     SkTypeface* onMatchFaceStyle(const SkTypeface* familyMember,
51                                  const SkFontStyle& fontStyle) const override;
52 
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;
57 
58 private:
59     SkTypeface* createTypefaceFromFontId(const SkFontIdentity& fontId) const;
60 
61     sk_sp<SkFontMgr> fImpl;
62     sk_sp<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 
DataEntryDataEntry71         DataEntry(DataEntry&& that)
72             : fDataId(that.fDataId)
73             , fTtcIndex(that.fTtcIndex)
74             , fTypeface(that.fTypeface)
75         {
76             SkDEBUGCODE(that.fDataId = SkFontIdentity::kInvalidDataId;)
77             SkDEBUGCODE(that.fTtcIndex = 0xbbadbeef;)
78             that.fTypeface = nullptr;
79         }
80 
~DataEntryDataEntry81         ~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 
96     friend class SkStyleSet_Indirect;
97 };
98 
99 #endif
100