SkFontMgr.h revision c20386e3937d3d398ac9b35f9c7d997e972ade98
1/*
2 * Copyright 2013 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_DEFINED
9#define SkFontMgr_DEFINED
10
11#include "SkRefCnt.h"
12#include "SkFontStyle.h"
13
14class SkData;
15class SkStream;
16class SkString;
17class SkTypeface;
18
19class SK_API SkFontStyleSet : public SkRefCnt {
20public:
21    SK_DECLARE_INST_COUNT(SkFontStyleSet)
22
23    virtual int count() = 0;
24    virtual void getStyle(int index, SkFontStyle*, SkString* style) = 0;
25    virtual SkTypeface* createTypeface(int index) = 0;
26    virtual SkTypeface* matchStyle(const SkFontStyle& pattern) = 0;
27
28    static SkFontStyleSet* CreateEmpty();
29
30private:
31    typedef SkRefCnt INHERITED;
32};
33
34class SkTypeface;
35
36class SK_API SkFontMgr : public SkRefCnt {
37public:
38    SK_DECLARE_INST_COUNT(SkFontMgr)
39
40    int countFamilies() const;
41    void getFamilyName(int index, SkString* familyName) const;
42    SkFontStyleSet* createStyleSet(int index) const;
43
44    /**
45     *  The caller must call unref() on the returned object.
46     *  Never returns NULL; will return an empty set if the name is not found.
47     *
48     *  It is possible that this will return a style set not accessible from
49     *  createStyleSet(int) due to hidden or auto-activated fonts.
50     */
51    SkFontStyleSet* matchFamily(const char familyName[]) const;
52
53    /**
54     *  Find the closest matching typeface to the specified familyName and style
55     *  and return a ref to it. The caller must call unref() on the returned
56     *  object. Will never return NULL, as it will return the default font if
57     *  no matching font is found.
58     *
59     *  It is possible that this will return a style set not accessible from
60     *  createStyleSet(int) or matchFamily(const char[]) due to hidden or
61     *  auto-activated fonts.
62     */
63    SkTypeface* matchFamilyStyle(const char familyName[], const SkFontStyle&) const;
64
65    /**
66     *  Use the system fallback to find a typeface for the given character.
67     *  Note that bcp47 is a combination of ISO 639, 15924, and 3166-1 codes,
68     *  so it is fine to just pass a ISO 639 here.
69     *
70     *  Will return NULL if no family can be found for the character
71     *  in the system fallback.
72     *
73     *  bcp47[0] is the least significant fallback, bcp47[bcp47Count-1] is the
74     *  most significant. If no specified bcp47 codes match, any font with the
75     *  requested character will be matched.
76     */
77#ifdef SK_FM_NEW_MATCH_FAMILY_STYLE_CHARACTER
78    SkTypeface* matchFamilyStyleCharacter(const char familyName[], const SkFontStyle&,
79                                          const char* bcp47[], int bcp47Count,
80                                          SkUnichar character) const;
81#else
82    SkTypeface* matchFamilyStyleCharacter(const char familyName[], const SkFontStyle&,
83                                          const char bcp47[], SkUnichar character) const;
84#endif
85
86    SkTypeface* matchFaceStyle(const SkTypeface*, const SkFontStyle&) const;
87
88    /**
89     *  Create a typeface for the specified data and TTC index (pass 0 for none)
90     *  or NULL if the data is not recognized. The caller must call unref() on
91     *  the returned object if it is not null.
92     */
93    SkTypeface* createFromData(SkData*, int ttcIndex = 0) const;
94
95    /**
96     *  Create a typeface for the specified stream and TTC index
97     *  (pass 0 for none) or NULL if the stream is not recognized. The caller
98     *  must call unref() on the returned object if it is not null.
99     */
100    SkTypeface* createFromStream(SkStream*, int ttcIndex = 0) const;
101
102    /**
103     *  Create a typeface for the specified fileName and TTC index
104     *  (pass 0 for none) or NULL if the file is not found, or its contents are
105     *  not recognized. The caller must call unref() on the returned object
106     *  if it is not null.
107     */
108    SkTypeface* createFromFile(const char path[], int ttcIndex = 0) const;
109
110    SkTypeface* legacyCreateTypeface(const char familyName[],
111                                     unsigned typefaceStyleBits) const;
112
113    /**
114     *  Return a ref to the default fontmgr. The caller must call unref() on
115     *  the returned object.
116     */
117    static SkFontMgr* RefDefault();
118
119protected:
120    virtual int onCountFamilies() const = 0;
121    virtual void onGetFamilyName(int index, SkString* familyName) const = 0;
122    virtual SkFontStyleSet* onCreateStyleSet(int index)const  = 0;
123
124    /** May return NULL if the name is not found. */
125    virtual SkFontStyleSet* onMatchFamily(const char familyName[]) const = 0;
126
127    virtual SkTypeface* onMatchFamilyStyle(const char familyName[],
128                                           const SkFontStyle&) const = 0;
129    // TODO: pure virtual, implement on all impls.
130#ifdef SK_FM_NEW_MATCH_FAMILY_STYLE_CHARACTER
131    virtual SkTypeface* onMatchFamilyStyleCharacter(const char familyName[], const SkFontStyle&,
132                                                    const char* bcp47[], int bcp47Count,
133                                                    SkUnichar character) const
134#else
135    virtual SkTypeface* onMatchFamilyStyleCharacter(const char familyName[], const SkFontStyle&,
136                                                    const char bcp47[], SkUnichar character) const
137#endif
138    { return NULL; }
139    virtual SkTypeface* onMatchFaceStyle(const SkTypeface*,
140                                         const SkFontStyle&) const = 0;
141
142    virtual SkTypeface* onCreateFromData(SkData*, int ttcIndex) const = 0;
143    virtual SkTypeface* onCreateFromStream(SkStream*, int ttcIndex) const = 0;
144    virtual SkTypeface* onCreateFromFile(const char path[], int ttcIndex) const = 0;
145
146    virtual SkTypeface* onLegacyCreateTypeface(const char familyName[],
147                                               unsigned styleBits) const = 0;
148private:
149    static SkFontMgr* Factory();    // implemented by porting layer
150    friend SkFontMgr* sk_fontmgr_create_default();
151
152    typedef SkRefCnt INHERITED;
153};
154
155#endif
156