blob: afadeaaa9785fe55f4f7ce686878d5487aed8c0f [file] [log] [blame]
reed@google.comc452d822013-03-25 18:44:17 +00001/*
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
reed@google.comc452d822013-03-25 18:44:17 +000011#include "SkFontStyle.h"
bungemanf6c71072016-01-21 14:17:47 -080012#include "SkRefCnt.h"
13#include "SkScalar.h"
14#include "SkTypes.h"
reed@google.comc452d822013-03-25 18:44:17 +000015
16class SkData;
bungeman41868fe2015-05-20 09:21:04 -070017class SkFontData;
bungeman5f213d92015-01-27 05:39:10 -080018class SkStreamAsset;
reed@google.comc452d822013-03-25 18:44:17 +000019class SkString;
reed@google.com0b983262013-04-11 18:27:26 +000020class SkTypeface;
reed@google.comc452d822013-03-25 18:44:17 +000021
reed@google.comaae71ba2013-04-10 13:10:40 +000022class SK_API SkFontStyleSet : public SkRefCnt {
reed@google.comc452d822013-03-25 18:44:17 +000023public:
reed@google.com83787c52013-03-26 17:19:15 +000024 virtual int count() = 0;
25 virtual void getStyle(int index, SkFontStyle*, SkString* style) = 0;
26 virtual SkTypeface* createTypeface(int index) = 0;
reed@google.com964988f2013-03-29 14:57:22 +000027 virtual SkTypeface* matchStyle(const SkFontStyle& pattern) = 0;
28
29 static SkFontStyleSet* CreateEmpty();
commit-bot@chromium.orgef284a82013-07-11 22:29:29 +000030
bungeman147ea2f2015-11-12 09:50:08 -080031protected:
32 SkTypeface* matchStyleCSS3(const SkFontStyle& pattern);
33
commit-bot@chromium.orgef284a82013-07-11 22:29:29 +000034private:
35 typedef SkRefCnt INHERITED;
reed@google.comc452d822013-03-25 18:44:17 +000036};
37
reed@google.comaae71ba2013-04-10 13:10:40 +000038class SK_API SkFontMgr : public SkRefCnt {
reed@google.comc452d822013-03-25 18:44:17 +000039public:
commit-bot@chromium.org967dee32014-02-04 22:35:01 +000040 int countFamilies() const;
41 void getFamilyName(int index, SkString* familyName) const;
42 SkFontStyleSet* createStyleSet(int index) const;
reed@google.comc452d822013-03-25 18:44:17 +000043
bungeman@google.com9fc5c682013-11-12 15:25:29 +000044 /**
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.
bungeman@google.com72cf4fc2014-03-21 22:48:32 +000047 *
erikchen2b1516f2015-12-03 12:12:13 -080048 * Passing |nullptr| as the parameter will return the default system font.
49 *
bungeman@google.com72cf4fc2014-03-21 22:48:32 +000050 * It is possible that this will return a style set not accessible from
51 * createStyleSet(int) due to hidden or auto-activated fonts.
bungeman@google.com9fc5c682013-11-12 15:25:29 +000052 */
commit-bot@chromium.org967dee32014-02-04 22:35:01 +000053 SkFontStyleSet* matchFamily(const char familyName[]) const;
reed@google.com964988f2013-03-29 14:57:22 +000054
reed@google.comc452d822013-03-25 18:44:17 +000055 /**
56 * Find the closest matching typeface to the specified familyName and style
57 * and return a ref to it. The caller must call unref() on the returned
58 * object. Will never return NULL, as it will return the default font if
59 * no matching font is found.
bungeman@google.com72cf4fc2014-03-21 22:48:32 +000060 *
erikchen2b1516f2015-12-03 12:12:13 -080061 * Passing |nullptr| as the parameter for |familyName| will return the
62 * default system font.
63 *
bungeman@google.com72cf4fc2014-03-21 22:48:32 +000064 * It is possible that this will return a style set not accessible from
65 * createStyleSet(int) or matchFamily(const char[]) due to hidden or
66 * auto-activated fonts.
reed@google.comc452d822013-03-25 18:44:17 +000067 */
commit-bot@chromium.org967dee32014-02-04 22:35:01 +000068 SkTypeface* matchFamilyStyle(const char familyName[], const SkFontStyle&) const;
reed@google.comc452d822013-03-25 18:44:17 +000069
bungeman@google.com72cf4fc2014-03-21 22:48:32 +000070 /**
71 * Use the system fallback to find a typeface for the given character.
bungemanc20386e2014-10-23 07:08:05 -070072 * Note that bcp47 is a combination of ISO 639, 15924, and 3166-1 codes,
bungeman@google.com72cf4fc2014-03-21 22:48:32 +000073 * so it is fine to just pass a ISO 639 here.
74 *
75 * Will return NULL if no family can be found for the character
76 * in the system fallback.
bungemanc20386e2014-10-23 07:08:05 -070077 *
erikchen2b1516f2015-12-03 12:12:13 -080078 * Passing |nullptr| as the parameter for |familyName| will return the
79 * default system font.
80 *
bungemanc20386e2014-10-23 07:08:05 -070081 * bcp47[0] is the least significant fallback, bcp47[bcp47Count-1] is the
82 * most significant. If no specified bcp47 codes match, any font with the
83 * requested character will be matched.
bungeman@google.com72cf4fc2014-03-21 22:48:32 +000084 */
85 SkTypeface* matchFamilyStyleCharacter(const char familyName[], const SkFontStyle&,
bungemanc20386e2014-10-23 07:08:05 -070086 const char* bcp47[], int bcp47Count,
87 SkUnichar character) const;
bungeman@google.com72cf4fc2014-03-21 22:48:32 +000088
commit-bot@chromium.org967dee32014-02-04 22:35:01 +000089 SkTypeface* matchFaceStyle(const SkTypeface*, const SkFontStyle&) const;
reed@google.com95625db2013-03-25 20:44:02 +000090
reed@google.comc452d822013-03-25 18:44:17 +000091 /**
92 * Create a typeface for the specified data and TTC index (pass 0 for none)
93 * or NULL if the data is not recognized. The caller must call unref() on
94 * the returned object if it is not null.
95 */
commit-bot@chromium.org967dee32014-02-04 22:35:01 +000096 SkTypeface* createFromData(SkData*, int ttcIndex = 0) const;
reed@google.comc452d822013-03-25 18:44:17 +000097
98 /**
99 * Create a typeface for the specified stream and TTC index
100 * (pass 0 for none) or NULL if the stream is not recognized. The caller
101 * must call unref() on the returned object if it is not null.
102 */
bungeman5f213d92015-01-27 05:39:10 -0800103 SkTypeface* createFromStream(SkStreamAsset*, int ttcIndex = 0) const;
reed@google.comc452d822013-03-25 18:44:17 +0000104
bungemanf6c71072016-01-21 14:17:47 -0800105 struct FontParameters {
106 struct Axis {
107 SkFourByteTag fTag;
108 SkScalar fStyleValue;
109 };
110
111 FontParameters() : fCollectionIndex(0), fAxisCount(0), fAxes(nullptr) {}
112
113 /** Specify the index of the desired font.
114 *
115 * Font formats like ttc, dfont, cff, cid, pfr, t42, t1, and fon may actually be indexed
116 * collections of fonts.
117 */
118 FontParameters& setCollectionIndex(int collectionIndex) {
119 fCollectionIndex = collectionIndex;
120 return *this;
121 }
122
123 /** Specify the GX variation axis values.
124 *
125 * Any axes not specified will use the default value. Specified axes not present in the
126 * font will be ignored.
127 *
128 * @param axes not copied. This pointer must remain valid for life of FontParameters.
129 */
130 FontParameters& setAxes(const Axis* axes, int axisCount) {
131 fAxisCount = axisCount;
132 fAxes = axes;
133 return *this;
134 }
135
136 int getCollectionIndex() const {
137 return fCollectionIndex;
138 }
139 const Axis* getAxes(int* axisCount) const {
140 *axisCount = fAxisCount;
141 return fAxes;
142 }
143 private:
144 int fCollectionIndex;
145 int fAxisCount;
146 const Axis* fAxes;
147 };
148 /* Experimental, API subject to change. */
149 SkTypeface* createFromStream(SkStreamAsset*, const FontParameters&) const;
150
reed@google.comc452d822013-03-25 18:44:17 +0000151 /**
bungeman41868fe2015-05-20 09:21:04 -0700152 * Create a typeface from the specified font data.
bungeman41868fe2015-05-20 09:21:04 -0700153 * Will return NULL if the typeface could not be created.
154 * The caller must call unref() on the returned object if it is not null.
155 */
bungemanf93d7112016-09-16 06:24:20 -0700156 SkTypeface* createFromFontData(std::unique_ptr<SkFontData>) const;
bungeman41868fe2015-05-20 09:21:04 -0700157
158 /**
reed@google.comc452d822013-03-25 18:44:17 +0000159 * Create a typeface for the specified fileName and TTC index
160 * (pass 0 for none) or NULL if the file is not found, or its contents are
161 * not recognized. The caller must call unref() on the returned object
162 * if it is not null.
163 */
commit-bot@chromium.org967dee32014-02-04 22:35:01 +0000164 SkTypeface* createFromFile(const char path[], int ttcIndex = 0) const;
reed@google.comc452d822013-03-25 18:44:17 +0000165
bungeman11a77c62016-04-12 13:45:06 -0700166 SkTypeface* legacyCreateTypeface(const char familyName[], SkFontStyle style) const;
reed@google.com30ddd612013-07-30 17:47:39 +0000167
reed@google.com95625db2013-03-25 20:44:02 +0000168 /**
169 * Return a ref to the default fontmgr. The caller must call unref() on
170 * the returned object.
171 */
172 static SkFontMgr* RefDefault();
173
174protected:
commit-bot@chromium.org967dee32014-02-04 22:35:01 +0000175 virtual int onCountFamilies() const = 0;
176 virtual void onGetFamilyName(int index, SkString* familyName) const = 0;
177 virtual SkFontStyleSet* onCreateStyleSet(int index)const = 0;
reed@google.com95625db2013-03-25 20:44:02 +0000178
bungeman@google.com9fc5c682013-11-12 15:25:29 +0000179 /** May return NULL if the name is not found. */
commit-bot@chromium.org967dee32014-02-04 22:35:01 +0000180 virtual SkFontStyleSet* onMatchFamily(const char familyName[]) const = 0;
reed@google.com964988f2013-03-29 14:57:22 +0000181
reed@google.com95625db2013-03-25 20:44:02 +0000182 virtual SkTypeface* onMatchFamilyStyle(const char familyName[],
commit-bot@chromium.org967dee32014-02-04 22:35:01 +0000183 const SkFontStyle&) const = 0;
bungeman@google.com72cf4fc2014-03-21 22:48:32 +0000184 virtual SkTypeface* onMatchFamilyStyleCharacter(const char familyName[], const SkFontStyle&,
bungemanc20386e2014-10-23 07:08:05 -0700185 const char* bcp47[], int bcp47Count,
djsollen33068c12014-11-14 10:52:53 -0800186 SkUnichar character) const = 0;
reed@google.com95625db2013-03-25 20:44:02 +0000187 virtual SkTypeface* onMatchFaceStyle(const SkTypeface*,
commit-bot@chromium.org967dee32014-02-04 22:35:01 +0000188 const SkFontStyle&) const = 0;
reed@google.com95625db2013-03-25 20:44:02 +0000189
commit-bot@chromium.org967dee32014-02-04 22:35:01 +0000190 virtual SkTypeface* onCreateFromData(SkData*, int ttcIndex) const = 0;
bungeman5f213d92015-01-27 05:39:10 -0800191 virtual SkTypeface* onCreateFromStream(SkStreamAsset*, int ttcIndex) const = 0;
bungeman41868fe2015-05-20 09:21:04 -0700192 // TODO: make pure virtual.
bungemanf6c71072016-01-21 14:17:47 -0800193 virtual SkTypeface* onCreateFromStream(SkStreamAsset*, const FontParameters&) const;
bungemanf93d7112016-09-16 06:24:20 -0700194 virtual SkTypeface* onCreateFromFontData(std::unique_ptr<SkFontData>) const;
commit-bot@chromium.org967dee32014-02-04 22:35:01 +0000195 virtual SkTypeface* onCreateFromFile(const char path[], int ttcIndex) const = 0;
bungeman@google.comfb1663a2013-10-24 22:32:43 +0000196
bungeman11a77c62016-04-12 13:45:06 -0700197 virtual SkTypeface* onLegacyCreateTypeface(const char familyName[], SkFontStyle) const = 0;
bungeman11a77c62016-04-12 13:45:06 -0700198
reed@google.comc452d822013-03-25 18:44:17 +0000199private:
reed@google.com95625db2013-03-25 20:44:02 +0000200 static SkFontMgr* Factory(); // implemented by porting layer
skia.committer@gmail.come60ed082013-03-26 07:01:04 +0000201
reed@google.comc452d822013-03-25 18:44:17 +0000202 typedef SkRefCnt INHERITED;
203};
204
205#endif