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 "../private/SkMutex.h"
12#include "../private/SkOnce.h"
13#include "../private/SkTArray.h"
14#include "SkFontMgr.h"
15#include "SkRefCnt.h"
16#include "SkRemotableFontMgr.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(sk_sp<SkFontMgr> impl, sk_sp<SkRemotableFontMgr> proxy)
31        : fImpl(std::move(impl)), fProxy(std::move(proxy))
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    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    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    SkTypeface* onLegacyCreateTypeface(const char familyName[], SkFontStyle) const override;
58
59private:
60    SkTypeface* createTypefaceFromFontId(const SkFontIdentity& fontId) const;
61
62    sk_sp<SkFontMgr> fImpl;
63    sk_sp<SkRemotableFontMgr> fProxy;
64
65    struct DataEntry {
66        uint32_t fDataId;  // key1
67        uint32_t fTtcIndex;  // key2
68        SkTypeface* fTypeface;  // value: weak ref to typeface
69
70        DataEntry() { }
71
72        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
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    friend class SkStyleSet_Indirect;
98};
99
100#endif
101