blob: 08e406a97d696c109487d91aa72e560fb6aa42b7 [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
Ben Wagnerfc497342017-02-24 11:15:26 -050011#include "SkFontArguments.h"
reed@google.comc452d822013-03-25 18:44:17 +000012#include "SkFontStyle.h"
bungemanf6c71072016-01-21 14:17:47 -080013#include "SkRefCnt.h"
bungemanf6c71072016-01-21 14:17:47 -080014#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 *
bungeman7575bb12017-05-01 13:02:42 -040048 * 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.
erikchen2b1516f2015-12-03 12:12:13 -080051 *
bungeman@google.com72cf4fc2014-03-21 22:48:32 +000052 * It is possible that this will return a style set not accessible from
53 * createStyleSet(int) due to hidden or auto-activated fonts.
bungeman@google.com9fc5c682013-11-12 15:25:29 +000054 */
commit-bot@chromium.org967dee32014-02-04 22:35:01 +000055 SkFontStyleSet* matchFamily(const char familyName[]) const;
reed@google.com964988f2013-03-29 14:57:22 +000056
reed@google.comc452d822013-03-25 18:44:17 +000057 /**
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 never return NULL, as it will return the default font if
61 * no matching font is found.
bungeman@google.com72cf4fc2014-03-21 22:48:32 +000062 *
erikchen2b1516f2015-12-03 12:12:13 -080063 * Passing |nullptr| as the parameter for |familyName| will return the
64 * default system font.
65 *
bungeman@google.com72cf4fc2014-03-21 22:48:32 +000066 * It is possible that this will return a style set not accessible from
67 * createStyleSet(int) or matchFamily(const char[]) due to hidden or
68 * auto-activated fonts.
reed@google.comc452d822013-03-25 18:44:17 +000069 */
commit-bot@chromium.org967dee32014-02-04 22:35:01 +000070 SkTypeface* matchFamilyStyle(const char familyName[], const SkFontStyle&) const;
reed@google.comc452d822013-03-25 18:44:17 +000071
bungeman@google.com72cf4fc2014-03-21 22:48:32 +000072 /**
73 * Use the system fallback to find a typeface for the given character.
bungemanc20386e2014-10-23 07:08:05 -070074 * Note that bcp47 is a combination of ISO 639, 15924, and 3166-1 codes,
bungeman@google.com72cf4fc2014-03-21 22:48:32 +000075 * so it is fine to just pass a ISO 639 here.
76 *
77 * Will return NULL if no family can be found for the character
78 * in the system fallback.
bungemanc20386e2014-10-23 07:08:05 -070079 *
erikchen2b1516f2015-12-03 12:12:13 -080080 * Passing |nullptr| as the parameter for |familyName| will return the
81 * default system font.
82 *
bungemanc20386e2014-10-23 07:08:05 -070083 * bcp47[0] is the least significant fallback, bcp47[bcp47Count-1] is the
84 * most significant. If no specified bcp47 codes match, any font with the
85 * requested character will be matched.
bungeman@google.com72cf4fc2014-03-21 22:48:32 +000086 */
87 SkTypeface* matchFamilyStyleCharacter(const char familyName[], const SkFontStyle&,
bungemanc20386e2014-10-23 07:08:05 -070088 const char* bcp47[], int bcp47Count,
89 SkUnichar character) const;
bungeman@google.com72cf4fc2014-03-21 22:48:32 +000090
commit-bot@chromium.org967dee32014-02-04 22:35:01 +000091 SkTypeface* matchFaceStyle(const SkTypeface*, const SkFontStyle&) const;
reed@google.com95625db2013-03-25 20:44:02 +000092
reed@google.comc452d822013-03-25 18:44:17 +000093 /**
94 * Create a typeface for the specified data and TTC index (pass 0 for none)
95 * or NULL if the data is not recognized. The caller must call unref() on
96 * the returned object if it is not null.
97 */
Mike Reed59227392017-09-26 09:46:08 -040098 sk_sp<SkTypeface> makeFromData(sk_sp<SkData>, int ttcIndex = 0) const;
reed@google.comc452d822013-03-25 18:44:17 +000099
100 /**
101 * Create a typeface for the specified stream and TTC index
102 * (pass 0 for none) or NULL if the stream is not recognized. The caller
103 * must call unref() on the returned object if it is not null.
104 */
Mike Reed59227392017-09-26 09:46:08 -0400105 sk_sp<SkTypeface> makeFromStream(std::unique_ptr<SkStreamAsset>, int ttcIndex = 0) const;
reed@google.comc452d822013-03-25 18:44:17 +0000106
Mike Reed59227392017-09-26 09:46:08 -0400107#ifdef SK_SUPPORT_LEGACY_FONTMGR_API
Ben Wagnerfc497342017-02-24 11:15:26 -0500108 using FontParameters = SkFontArguments;
Mike Reed59227392017-09-26 09:46:08 -0400109#endif
110
bungemanf6c71072016-01-21 14:17:47 -0800111 /* Experimental, API subject to change. */
Mike Reed59227392017-09-26 09:46:08 -0400112 sk_sp<SkTypeface> makeFromStream(std::unique_ptr<SkStreamAsset>, const SkFontArguments&) const;
bungemanf6c71072016-01-21 14:17:47 -0800113
reed@google.comc452d822013-03-25 18:44:17 +0000114 /**
bungeman41868fe2015-05-20 09:21:04 -0700115 * Create a typeface from the specified font data.
bungeman41868fe2015-05-20 09:21:04 -0700116 * Will return NULL if the typeface could not be created.
117 * The caller must call unref() on the returned object if it is not null.
118 */
Mike Reed59227392017-09-26 09:46:08 -0400119 sk_sp<SkTypeface> makeFromFontData(std::unique_ptr<SkFontData>) const;
bungeman41868fe2015-05-20 09:21:04 -0700120
121 /**
reed@google.comc452d822013-03-25 18:44:17 +0000122 * Create a typeface for the specified fileName and TTC index
123 * (pass 0 for none) or NULL if the file is not found, or its contents are
124 * not recognized. The caller must call unref() on the returned object
125 * if it is not null.
126 */
Mike Reed59227392017-09-26 09:46:08 -0400127 sk_sp<SkTypeface> makeFromFile(const char path[], int ttcIndex = 0) const;
Mike Reedf40ae1a2017-09-25 20:00:31 +0000128
Mike Reed59227392017-09-26 09:46:08 -0400129 sk_sp<SkTypeface> legacyMakeTypeface(const char familyName[], SkFontStyle style) const;
130
131#ifdef SK_SUPPORT_LEGACY_FONTMGR_API
132 SkTypeface* createFromData(SkData* data, int ttcIndex = 0) const;
133 SkTypeface* createFromStream(SkStreamAsset* strm, int ttcIndex = 0) const;
134 SkTypeface* createFromStream(SkStreamAsset* strm, const SkFontArguments& args) const;
135 SkTypeface* createFromFontData(std::unique_ptr<SkFontData> fd) const;
136 SkTypeface* createFromFile(const char path[], int ttcIndex = 0) const;
bungeman11a77c62016-04-12 13:45:06 -0700137 SkTypeface* legacyCreateTypeface(const char familyName[], SkFontStyle style) const;
Mike Reed59227392017-09-26 09:46:08 -0400138#endif
reed@google.com30ddd612013-07-30 17:47:39 +0000139
Ben Wagner3546ff12017-01-03 13:32:36 -0500140 /** Return the default fontmgr. */
Ben Wagner3546ff12017-01-03 13:32:36 -0500141 static sk_sp<SkFontMgr> RefDefault();
reed@google.com95625db2013-03-25 20:44:02 +0000142
143protected:
commit-bot@chromium.org967dee32014-02-04 22:35:01 +0000144 virtual int onCountFamilies() const = 0;
145 virtual void onGetFamilyName(int index, SkString* familyName) const = 0;
146 virtual SkFontStyleSet* onCreateStyleSet(int index)const = 0;
reed@google.com95625db2013-03-25 20:44:02 +0000147
bungeman@google.com9fc5c682013-11-12 15:25:29 +0000148 /** May return NULL if the name is not found. */
commit-bot@chromium.org967dee32014-02-04 22:35:01 +0000149 virtual SkFontStyleSet* onMatchFamily(const char familyName[]) const = 0;
reed@google.com964988f2013-03-29 14:57:22 +0000150
reed@google.com95625db2013-03-25 20:44:02 +0000151 virtual SkTypeface* onMatchFamilyStyle(const char familyName[],
commit-bot@chromium.org967dee32014-02-04 22:35:01 +0000152 const SkFontStyle&) const = 0;
bungeman@google.com72cf4fc2014-03-21 22:48:32 +0000153 virtual SkTypeface* onMatchFamilyStyleCharacter(const char familyName[], const SkFontStyle&,
bungemanc20386e2014-10-23 07:08:05 -0700154 const char* bcp47[], int bcp47Count,
djsollen33068c12014-11-14 10:52:53 -0800155 SkUnichar character) const = 0;
reed@google.com95625db2013-03-25 20:44:02 +0000156 virtual SkTypeface* onMatchFaceStyle(const SkTypeface*,
commit-bot@chromium.org967dee32014-02-04 22:35:01 +0000157 const SkFontStyle&) const = 0;
reed@google.com95625db2013-03-25 20:44:02 +0000158
Mike Reed59227392017-09-26 09:46:08 -0400159#ifdef SK_SUPPORT_LEGACY_FONTMGR_API
160 // legacy virtuals
161 virtual SkTypeface* onCreateFromData(SkData*, int) const { return nullptr; }
162 virtual SkTypeface* onCreateFromStream(SkStreamAsset*, int) const { return nullptr; }
Ben Wagnerfc497342017-02-24 11:15:26 -0500163 virtual SkTypeface* onCreateFromStream(SkStreamAsset*, const SkFontArguments&) const;
bungemanf93d7112016-09-16 06:24:20 -0700164 virtual SkTypeface* onCreateFromFontData(std::unique_ptr<SkFontData>) const;
Mike Reed59227392017-09-26 09:46:08 -0400165 virtual SkTypeface* onCreateFromFile(const char[], int) const { return nullptr; }
166 virtual SkTypeface* onLegacyCreateTypeface(const char[], SkFontStyle) const {
167 return nullptr;
168 }
bungeman@google.comfb1663a2013-10-24 22:32:43 +0000169
Mike Reed59227392017-09-26 09:46:08 -0400170 // new virtuals express as calling legacy versions
171
172 virtual sk_sp<SkTypeface> onMakeFromData(sk_sp<SkData> data, int ttcIndex) const;
173 virtual sk_sp<SkTypeface> onMakeFromStreamIndex(std::unique_ptr<SkStreamAsset> strm,
174 int ttcIndex) const;
175 virtual sk_sp<SkTypeface> onMakeFromStreamArgs(std::unique_ptr<SkStreamAsset> strm,
176 const SkFontArguments& args) const;
177 virtual sk_sp<SkTypeface> onMakeFromFontData(std::unique_ptr<SkFontData> fd) const;
178 virtual sk_sp<SkTypeface> onMakeFromFile(const char path[], int ttcIndex) const;
179 virtual sk_sp<SkTypeface> onLegacyMakeTypeface(const char familyName[], SkFontStyle style) const;
180#else
181 virtual sk_sp<SkTypeface> onMakeFromData(sk_sp<SkData>, int ttcIndex) const = 0;
182 virtual sk_sp<SkTypeface> onMakeFromStreamIndex(std::unique_ptr<SkStreamAsset>,
183 int ttcIndex) const = 0;
184 virtual sk_sp<SkTypeface> onMakeFromStreamArgs(std::unique_ptr<SkStreamAsset>,
185 const SkFontArguments&) const;
186 virtual sk_sp<SkTypeface> onMakeFromFontData(std::unique_ptr<SkFontData>) const;
187 virtual sk_sp<SkTypeface> onMakeFromFile(const char path[], int ttcIndex) const = 0;
188
189 virtual sk_sp<SkTypeface> onLegacyMakeTypeface(const char familyName[], SkFontStyle) const = 0;
190#endif
bungeman11a77c62016-04-12 13:45:06 -0700191
reed@google.comc452d822013-03-25 18:44:17 +0000192private:
Ben Wagner3546ff12017-01-03 13:32:36 -0500193
194 /** Implemented by porting layer to return the default factory. */
Ben Wagner3546ff12017-01-03 13:32:36 -0500195 static sk_sp<SkFontMgr> Factory();
skia.committer@gmail.come60ed082013-03-26 07:01:04 +0000196
reed@google.comc452d822013-03-25 18:44:17 +0000197 typedef SkRefCnt INHERITED;
198};
199
200#endif