SkFontMgr_indirect.h revision 78358bf624c7e7c09ffccf638c50870808d884d6
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    virtual SkTypeface* onMatchFamilyStyleCharacter(const char familyName[],
43                                                    const SkFontStyle&,
44                                                    const char bpc47[],
45                                                    uint32_t character) const SK_OVERRIDE;
46
47    virtual SkTypeface* onMatchFaceStyle(const SkTypeface* familyMember,
48                                         const SkFontStyle& fontStyle) const SK_OVERRIDE;
49
50    virtual SkTypeface* onCreateFromStream(SkStream* stream, int ttcIndex) const SK_OVERRIDE;
51    virtual SkTypeface* onCreateFromFile(const char path[], int ttcIndex) const SK_OVERRIDE;
52    virtual SkTypeface* onCreateFromData(SkData* data, int ttcIndex) const SK_OVERRIDE;
53
54    virtual SkTypeface* onLegacyCreateTypeface(const char familyName[],
55                                               unsigned styleBits) const SK_OVERRIDE;
56
57private:
58    SkTypeface* createTypefaceFromFontId(const SkFontIdentity& fontId) const;
59
60    SkAutoTUnref<SkFontMgr> fImpl;
61    SkAutoTUnref<SkRemotableFontMgr> fProxy;
62
63    struct DataEntry {
64        uint32_t fDataId;  // key1
65        uint32_t fTtcIndex;  // key2
66        SkTypeface* fTypeface;  // value: weak ref to typeface
67
68        DataEntry() { }
69
70        // This is a move!!!
71        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 = NULL;
79        }
80
81        ~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    mutable SkAutoTUnref<SkDataTable> fFamilyNames;
97    mutable bool fFamilyNamesInited;
98    mutable SkMutex fFamilyNamesMutex;
99    static void set_up_family_names(const SkFontMgr_Indirect* self);
100
101    friend class SkStyleSet_Indirect;
102};
103
104#endif
105