• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "include/core/SkFontArguments.h"
12 #include "include/core/SkFontStyle.h"
13 #include "include/core/SkRefCnt.h"
14 #include "include/core/SkTypes.h"
15 
16 #include <memory>
17 
18 class SkData;
19 class SkFontData;
20 class SkStreamAsset;
21 class SkString;
22 class SkTypeface;
23 
24 class SK_API SkFontStyleSet : public SkRefCnt {
25 public:
26     virtual int count() = 0;
27     virtual void getStyle(int index, SkFontStyle*, SkString* style) = 0;
28     virtual SkTypeface* createTypeface(int index) = 0;
29     virtual SkTypeface* matchStyle(const SkFontStyle& pattern) = 0;
30 
31     static SkFontStyleSet* CreateEmpty();
32 
33 protected:
34     SkTypeface* matchStyleCSS3(const SkFontStyle& pattern);
35 
36 private:
37     using INHERITED = SkRefCnt;
38 };
39 
40 class SK_API SkFontMgr : public SkRefCnt {
41 public:
42     int countFamilies() const;
43     void getFamilyName(int index, SkString* familyName) const;
44     SkFontStyleSet* createStyleSet(int index) const;
45 
46     /**
47      *  The caller must call unref() on the returned object.
48      *  Never returns NULL; will return an empty set if the name is not found.
49      *
50      *  Passing nullptr as the parameter will return the default system family.
51      *  Note that most systems don't have a default system family, so passing nullptr will often
52      *  result in the empty set.
53      *
54      *  It is possible that this will return a style set not accessible from
55      *  createStyleSet(int) due to hidden or auto-activated fonts.
56      */
57     SkFontStyleSet* matchFamily(const char familyName[]) const;
58 
59     /**
60      *  Find the closest matching typeface to the specified familyName and style
61      *  and return a ref to it. The caller must call unref() on the returned
62      *  object. Will return nullptr if no 'good' match is found.
63      *
64      *  Passing |nullptr| as the parameter for |familyName| will return the
65      *  default system font.
66      *
67      *  It is possible that this will return a style set not accessible from
68      *  createStyleSet(int) or matchFamily(const char[]) due to hidden or
69      *  auto-activated fonts.
70      */
71     SkTypeface* matchFamilyStyle(const char familyName[], const SkFontStyle&) const;
72 
73     /**
74      *  Use the system fallback to find a typeface for the given character.
75      *  Note that bcp47 is a combination of ISO 639, 15924, and 3166-1 codes,
76      *  so it is fine to just pass a ISO 639 here.
77      *
78      *  Will return NULL if no family can be found for the character
79      *  in the system fallback.
80      *
81      *  Passing |nullptr| as the parameter for |familyName| will return the
82      *  default system font.
83      *
84      *  bcp47[0] is the least significant fallback, bcp47[bcp47Count-1] is the
85      *  most significant. If no specified bcp47 codes match, any font with the
86      *  requested character will be matched.
87      */
88     SkTypeface* matchFamilyStyleCharacter(const char familyName[], const SkFontStyle&,
89                                           const char* bcp47[], int bcp47Count,
90                                           SkUnichar character) const;
91 
92     /**
93      *  Create a typeface for the specified data and TTC index (pass 0 for none)
94      *  or NULL if the data is not recognized. The caller must call unref() on
95      *  the returned object if it is not null.
96      */
97     sk_sp<SkTypeface> makeFromData(sk_sp<SkData>, int ttcIndex = 0) const;
98 
99     /**
100      *  Create a typeface for the specified stream and TTC index
101      *  (pass 0 for none) or NULL if the stream is not recognized. The caller
102      *  must call unref() on the returned object if it is not null.
103      */
104     sk_sp<SkTypeface> makeFromStream(std::unique_ptr<SkStreamAsset>, int ttcIndex = 0) const;
105 
106     /* Experimental, API subject to change. */
107     sk_sp<SkTypeface> makeFromStream(std::unique_ptr<SkStreamAsset>, const SkFontArguments&) const;
108 
109     /**
110      *  Create a typeface for the specified fileName and TTC index
111      *  (pass 0 for none) or NULL if the file is not found, or its contents are
112      *  not recognized. The caller must call unref() on the returned object
113      *  if it is not null.
114      */
115     sk_sp<SkTypeface> makeFromFile(const char path[], int ttcIndex = 0) const;
116 
117     sk_sp<SkTypeface> legacyMakeTypeface(const char familyName[], SkFontStyle style) const;
118 
119     /** Return the default fontmgr. */
120     static sk_sp<SkFontMgr> RefDefault();
121 
122     /* Returns an empty font manager without any typeface dependencies */
123     static sk_sp<SkFontMgr> RefEmpty();
124 
125 protected:
126     virtual int onCountFamilies() const = 0;
127     virtual void onGetFamilyName(int index, SkString* familyName) const = 0;
128     virtual SkFontStyleSet* onCreateStyleSet(int index)const  = 0;
129 
130     /** May return NULL if the name is not found. */
131     virtual SkFontStyleSet* onMatchFamily(const char familyName[]) const = 0;
132 
133     virtual SkTypeface* onMatchFamilyStyle(const char familyName[],
134                                            const SkFontStyle&) const = 0;
135     virtual SkTypeface* onMatchFamilyStyleCharacter(const char familyName[], const SkFontStyle&,
136                                                     const char* bcp47[], int bcp47Count,
137                                                     SkUnichar character) const = 0;
138 
139     virtual sk_sp<SkTypeface> onMakeFromData(sk_sp<SkData>, int ttcIndex) const = 0;
140     virtual sk_sp<SkTypeface> onMakeFromStreamIndex(std::unique_ptr<SkStreamAsset>,
141                                                     int ttcIndex) const = 0;
142     virtual sk_sp<SkTypeface> onMakeFromStreamArgs(std::unique_ptr<SkStreamAsset>,
143                                                    const SkFontArguments&) const = 0;
144     virtual sk_sp<SkTypeface> onMakeFromFile(const char path[], int ttcIndex) const = 0;
145 
146     virtual sk_sp<SkTypeface> onLegacyMakeTypeface(const char familyName[], SkFontStyle) const = 0;
147 
148     // this method is never called -- will be removed
onMatchFaceStyle(const SkTypeface *,const SkFontStyle &)149     virtual SkTypeface* onMatchFaceStyle(const SkTypeface*,
150                                          const SkFontStyle&) const {
151         return nullptr;
152     }
153 
154 private:
155 
156     /** Implemented by porting layer to return the default factory. */
157     static sk_sp<SkFontMgr> Factory();
158 
159     using INHERITED = SkRefCnt;
160 };
161 
162 #endif
163