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
18class SkData;
19class SkStream;
20class SkString;
21class SkTypeface;
22
23class SK_API SkFontMgr_Indirect : public SkFontMgr {
24public:
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)
29        : fImpl(SkRef(impl)), fProxy(SkRef(proxy)), fFamilyNamesInited(false)
30    { }
31
32protected:
33    virtual int onCountFamilies() const SK_OVERRIDE;
34    virtual void onGetFamilyName(int index, SkString* familyName) const SK_OVERRIDE;
35    virtual SkFontStyleSet* onCreateStyleSet(int index) const SK_OVERRIDE;
36
37    virtual SkFontStyleSet* onMatchFamily(const char familyName[]) const SK_OVERRIDE;
38
39    virtual SkTypeface* onMatchFamilyStyle(const char familyName[],
40                                           const SkFontStyle& fontStyle) const SK_OVERRIDE;
41
42#ifdef SK_FM_NEW_MATCH_FAMILY_STYLE_CHARACTER
43    virtual SkTypeface* onMatchFamilyStyleCharacter(const char familyName[],
44                                                    const SkFontStyle&,
45                                                    const char* bcp47[],
46                                                    int bcp47Count,
47                                                    SkUnichar character) const SK_OVERRIDE;
48#else
49    virtual SkTypeface* onMatchFamilyStyleCharacter(const char familyName[],
50                                                    const SkFontStyle&,
51                                                    const char bcp47[],
52                                                    SkUnichar character) const SK_OVERRIDE;
53#endif
54
55    virtual SkTypeface* onMatchFaceStyle(const SkTypeface* familyMember,
56                                         const SkFontStyle& fontStyle) const SK_OVERRIDE;
57
58    virtual SkTypeface* onCreateFromStream(SkStream* stream, int ttcIndex) const SK_OVERRIDE;
59    virtual SkTypeface* onCreateFromFile(const char path[], int ttcIndex) const SK_OVERRIDE;
60    virtual SkTypeface* onCreateFromData(SkData* data, int ttcIndex) const SK_OVERRIDE;
61
62    virtual SkTypeface* onLegacyCreateTypeface(const char familyName[],
63                                               unsigned styleBits) const SK_OVERRIDE;
64
65private:
66    SkTypeface* createTypefaceFromFontId(const SkFontIdentity& fontId) const;
67
68    SkAutoTUnref<SkFontMgr> fImpl;
69    SkAutoTUnref<SkRemotableFontMgr> fProxy;
70
71    struct DataEntry {
72        uint32_t fDataId;  // key1
73        uint32_t fTtcIndex;  // key2
74        SkTypeface* fTypeface;  // value: weak ref to typeface
75
76        DataEntry() { }
77
78        // This is a move!!!
79        DataEntry(DataEntry& that)
80            : fDataId(that.fDataId)
81            , fTtcIndex(that.fTtcIndex)
82            , fTypeface(that.fTypeface)
83        {
84            SkDEBUGCODE(that.fDataId = SkFontIdentity::kInvalidDataId;)
85            SkDEBUGCODE(that.fTtcIndex = 0xbbadbeef;)
86            that.fTypeface = NULL;
87        }
88
89        ~DataEntry() {
90            if (fTypeface) {
91                fTypeface->weak_unref();
92            }
93        }
94    };
95    /**
96     *  This cache is essentially { dataId: { ttcIndex: typeface } }
97     *  For data caching we want a mapping from data id to weak references to
98     *  typefaces with that data id. By storing the index next to the typeface,
99     *  this data cache also acts as a typeface cache.
100     */
101    mutable SkTArray<DataEntry> fDataCache;
102    mutable SkMutex fDataCacheMutex;
103
104    mutable SkAutoTUnref<SkDataTable> fFamilyNames;
105    mutable bool fFamilyNamesInited;
106    mutable SkMutex fFamilyNamesMutex;
107    static void set_up_family_names(const SkFontMgr_Indirect* self);
108
109    friend class SkStyleSet_Indirect;
110};
111
112#endif
113