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