SkFontMgr_indirect.h revision 918090c819109585f8fd2295039385a60eb0b572
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 "../private/SkMutex.h"
14#include "SkRefCnt.h"
15#include "SkRemotableFontMgr.h"
16#include "SkTArray.h"
17#include "SkTypeface.h"
18#include "SkTypes.h"
19
20class SkData;
21class SkFontStyle;
22class SkStreamAsset;
23class SkString;
24
25class SK_API SkFontMgr_Indirect : public SkFontMgr {
26public:
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.
30    SkFontMgr_Indirect(SkFontMgr* impl, SkRemotableFontMgr* proxy)
31        : fImpl(SkRef(impl)), fProxy(SkRef(proxy)), fFamilyNamesInited(false)
32    { }
33
34protected:
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    virtual SkTypeface* onMatchFamilyStyle(const char familyName[],
42                                           const SkFontStyle& fontStyle) const override;
43
44    virtual SkTypeface* onMatchFamilyStyleCharacter(const char familyName[],
45                                                    const SkFontStyle&,
46                                                    const char* bcp47[],
47                                                    int bcp47Count,
48                                                    SkUnichar character) const override;
49
50    virtual SkTypeface* onMatchFaceStyle(const SkTypeface* familyMember,
51                                         const SkFontStyle& fontStyle) const override;
52
53    SkTypeface* onCreateFromStream(SkStreamAsset* stream, int ttcIndex) const override;
54    SkTypeface* onCreateFromFile(const char path[], int ttcIndex) const override;
55    SkTypeface* onCreateFromData(SkData* data, int ttcIndex) const override;
56
57    virtual SkTypeface* onLegacyCreateTypeface(const char familyName[],
58                                               unsigned styleBits) const override;
59
60private:
61    SkTypeface* createTypefaceFromFontId(const SkFontIdentity& fontId) const;
62
63    SkAutoTUnref<SkFontMgr> fImpl;
64    SkAutoTUnref<SkRemotableFontMgr> fProxy;
65
66    struct DataEntry {
67        uint32_t fDataId;  // key1
68        uint32_t fTtcIndex;  // key2
69        SkTypeface* fTypeface;  // value: weak ref to typeface
70
71        DataEntry() { }
72
73        DataEntry(DataEntry&& that)
74            : fDataId(that.fDataId)
75            , fTtcIndex(that.fTtcIndex)
76            , fTypeface(that.fTypeface)
77        {
78            SkDEBUGCODE(that.fDataId = SkFontIdentity::kInvalidDataId;)
79            SkDEBUGCODE(that.fTtcIndex = 0xbbadbeef;)
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;
99    mutable bool fFamilyNamesInited;
100    mutable SkMutex fFamilyNamesMutex;
101    static void set_up_family_names(const SkFontMgr_Indirect* self);
102
103    friend class SkStyleSet_Indirect;
104};
105
106#endif
107