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 
14 class SkData;
15 class SkStreamAsset;
16 class SkString;
17 class SkTypeface;
18 
19 class SK_API SkFontStyleSet : public SkRefCnt {
20 public:
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 
30 private:
31     typedef SkRefCnt INHERITED;
32 };
33 
34 class SkTypeface;
35 
36 class SK_API SkFontMgr : public SkRefCnt {
37 public:
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     SkTypeface* matchFamilyStyleCharacter(const char familyName[], const SkFontStyle&,
78                                           const char* bcp47[], int bcp47Count,
79                                           SkUnichar character) const;
80 
81     SkTypeface* matchFaceStyle(const SkTypeface*, const SkFontStyle&) const;
82 
83     /**
84      *  Create a typeface for the specified data and TTC index (pass 0 for none)
85      *  or NULL if the data is not recognized. The caller must call unref() on
86      *  the returned object if it is not null.
87      */
88     SkTypeface* createFromData(SkData*, int ttcIndex = 0) const;
89 
90     /**
91      *  Create a typeface for the specified stream and TTC index
92      *  (pass 0 for none) or NULL if the stream is not recognized. The caller
93      *  must call unref() on the returned object if it is not null.
94      */
95     SkTypeface* createFromStream(SkStreamAsset*, int ttcIndex = 0) const;
96 
97     /**
98      *  Create a typeface for the specified fileName and TTC index
99      *  (pass 0 for none) or NULL if the file is not found, or its contents are
100      *  not recognized. The caller must call unref() on the returned object
101      *  if it is not null.
102      */
103     SkTypeface* createFromFile(const char path[], int ttcIndex = 0) const;
104 
105     SkTypeface* legacyCreateTypeface(const char familyName[],
106                                      unsigned typefaceStyleBits) const;
107 
108     /**
109      *  Return a ref to the default fontmgr. The caller must call unref() on
110      *  the returned object.
111      */
112     static SkFontMgr* RefDefault();
113 
114 protected:
115     virtual int onCountFamilies() const = 0;
116     virtual void onGetFamilyName(int index, SkString* familyName) const = 0;
117     virtual SkFontStyleSet* onCreateStyleSet(int index)const  = 0;
118 
119     /** May return NULL if the name is not found. */
120     virtual SkFontStyleSet* onMatchFamily(const char familyName[]) const = 0;
121 
122     virtual SkTypeface* onMatchFamilyStyle(const char familyName[],
123                                            const SkFontStyle&) const = 0;
124     virtual SkTypeface* onMatchFamilyStyleCharacter(const char familyName[], const SkFontStyle&,
125                                                     const char* bcp47[], int bcp47Count,
126                                                     SkUnichar character) const = 0;
127     virtual SkTypeface* onMatchFaceStyle(const SkTypeface*,
128                                          const SkFontStyle&) const = 0;
129 
130     virtual SkTypeface* onCreateFromData(SkData*, int ttcIndex) const = 0;
131     virtual SkTypeface* onCreateFromStream(SkStreamAsset*, int ttcIndex) const = 0;
132     virtual SkTypeface* onCreateFromFile(const char path[], int ttcIndex) const = 0;
133 
134     virtual SkTypeface* onLegacyCreateTypeface(const char familyName[],
135                                                unsigned styleBits) const = 0;
136 private:
137     static SkFontMgr* Factory();    // implemented by porting layer
138     friend SkFontMgr* sk_fontmgr_create_default();
139 
140     typedef SkRefCnt INHERITED;
141 };
142 
143 #endif
144