reed@google.com | c452d82 | 2013-03-25 18:44:17 +0000 | [diff] [blame] | 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 | |
Ben Wagner | fc49734 | 2017-02-24 11:15:26 -0500 | [diff] [blame] | 11 | #include "SkFontArguments.h" |
reed@google.com | c452d82 | 2013-03-25 18:44:17 +0000 | [diff] [blame] | 12 | #include "SkFontStyle.h" |
bungeman | f6c7107 | 2016-01-21 14:17:47 -0800 | [diff] [blame] | 13 | #include "SkRefCnt.h" |
bungeman | f6c7107 | 2016-01-21 14:17:47 -0800 | [diff] [blame] | 14 | #include "SkTypes.h" |
reed@google.com | c452d82 | 2013-03-25 18:44:17 +0000 | [diff] [blame] | 15 | |
| 16 | class SkData; |
bungeman | 41868fe | 2015-05-20 09:21:04 -0700 | [diff] [blame] | 17 | class SkFontData; |
bungeman | 5f213d9 | 2015-01-27 05:39:10 -0800 | [diff] [blame] | 18 | class SkStreamAsset; |
reed@google.com | c452d82 | 2013-03-25 18:44:17 +0000 | [diff] [blame] | 19 | class SkString; |
reed@google.com | 0b98326 | 2013-04-11 18:27:26 +0000 | [diff] [blame] | 20 | class SkTypeface; |
reed@google.com | c452d82 | 2013-03-25 18:44:17 +0000 | [diff] [blame] | 21 | |
reed@google.com | aae71ba | 2013-04-10 13:10:40 +0000 | [diff] [blame] | 22 | class SK_API SkFontStyleSet : public SkRefCnt { |
reed@google.com | c452d82 | 2013-03-25 18:44:17 +0000 | [diff] [blame] | 23 | public: |
reed@google.com | 83787c5 | 2013-03-26 17:19:15 +0000 | [diff] [blame] | 24 | virtual int count() = 0; |
| 25 | virtual void getStyle(int index, SkFontStyle*, SkString* style) = 0; |
| 26 | virtual SkTypeface* createTypeface(int index) = 0; |
reed@google.com | 964988f | 2013-03-29 14:57:22 +0000 | [diff] [blame] | 27 | virtual SkTypeface* matchStyle(const SkFontStyle& pattern) = 0; |
| 28 | |
| 29 | static SkFontStyleSet* CreateEmpty(); |
commit-bot@chromium.org | ef284a8 | 2013-07-11 22:29:29 +0000 | [diff] [blame] | 30 | |
bungeman | 147ea2f | 2015-11-12 09:50:08 -0800 | [diff] [blame] | 31 | protected: |
| 32 | SkTypeface* matchStyleCSS3(const SkFontStyle& pattern); |
| 33 | |
commit-bot@chromium.org | ef284a8 | 2013-07-11 22:29:29 +0000 | [diff] [blame] | 34 | private: |
| 35 | typedef SkRefCnt INHERITED; |
reed@google.com | c452d82 | 2013-03-25 18:44:17 +0000 | [diff] [blame] | 36 | }; |
| 37 | |
reed@google.com | aae71ba | 2013-04-10 13:10:40 +0000 | [diff] [blame] | 38 | class SK_API SkFontMgr : public SkRefCnt { |
reed@google.com | c452d82 | 2013-03-25 18:44:17 +0000 | [diff] [blame] | 39 | public: |
commit-bot@chromium.org | 967dee3 | 2014-02-04 22:35:01 +0000 | [diff] [blame] | 40 | int countFamilies() const; |
| 41 | void getFamilyName(int index, SkString* familyName) const; |
| 42 | SkFontStyleSet* createStyleSet(int index) const; |
reed@google.com | c452d82 | 2013-03-25 18:44:17 +0000 | [diff] [blame] | 43 | |
bungeman@google.com | 9fc5c68 | 2013-11-12 15:25:29 +0000 | [diff] [blame] | 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. |
bungeman@google.com | 72cf4fc | 2014-03-21 22:48:32 +0000 | [diff] [blame] | 47 | * |
bungeman | 7575bb1 | 2017-05-01 13:02:42 -0400 | [diff] [blame] | 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. |
erikchen | 2b1516f | 2015-12-03 12:12:13 -0800 | [diff] [blame] | 51 | * |
bungeman@google.com | 72cf4fc | 2014-03-21 22:48:32 +0000 | [diff] [blame] | 52 | * 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.com | 9fc5c68 | 2013-11-12 15:25:29 +0000 | [diff] [blame] | 54 | */ |
commit-bot@chromium.org | 967dee3 | 2014-02-04 22:35:01 +0000 | [diff] [blame] | 55 | SkFontStyleSet* matchFamily(const char familyName[]) const; |
reed@google.com | 964988f | 2013-03-29 14:57:22 +0000 | [diff] [blame] | 56 | |
reed@google.com | c452d82 | 2013-03-25 18:44:17 +0000 | [diff] [blame] | 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 never return NULL, as it will return the default font if |
| 61 | * no matching font is found. |
bungeman@google.com | 72cf4fc | 2014-03-21 22:48:32 +0000 | [diff] [blame] | 62 | * |
erikchen | 2b1516f | 2015-12-03 12:12:13 -0800 | [diff] [blame] | 63 | * Passing |nullptr| as the parameter for |familyName| will return the |
| 64 | * default system font. |
| 65 | * |
bungeman@google.com | 72cf4fc | 2014-03-21 22:48:32 +0000 | [diff] [blame] | 66 | * 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.com | c452d82 | 2013-03-25 18:44:17 +0000 | [diff] [blame] | 69 | */ |
commit-bot@chromium.org | 967dee3 | 2014-02-04 22:35:01 +0000 | [diff] [blame] | 70 | SkTypeface* matchFamilyStyle(const char familyName[], const SkFontStyle&) const; |
reed@google.com | c452d82 | 2013-03-25 18:44:17 +0000 | [diff] [blame] | 71 | |
bungeman@google.com | 72cf4fc | 2014-03-21 22:48:32 +0000 | [diff] [blame] | 72 | /** |
| 73 | * Use the system fallback to find a typeface for the given character. |
bungeman | c20386e | 2014-10-23 07:08:05 -0700 | [diff] [blame] | 74 | * Note that bcp47 is a combination of ISO 639, 15924, and 3166-1 codes, |
bungeman@google.com | 72cf4fc | 2014-03-21 22:48:32 +0000 | [diff] [blame] | 75 | * 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. |
bungeman | c20386e | 2014-10-23 07:08:05 -0700 | [diff] [blame] | 79 | * |
erikchen | 2b1516f | 2015-12-03 12:12:13 -0800 | [diff] [blame] | 80 | * Passing |nullptr| as the parameter for |familyName| will return the |
| 81 | * default system font. |
| 82 | * |
bungeman | c20386e | 2014-10-23 07:08:05 -0700 | [diff] [blame] | 83 | * 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.com | 72cf4fc | 2014-03-21 22:48:32 +0000 | [diff] [blame] | 86 | */ |
| 87 | SkTypeface* matchFamilyStyleCharacter(const char familyName[], const SkFontStyle&, |
bungeman | c20386e | 2014-10-23 07:08:05 -0700 | [diff] [blame] | 88 | const char* bcp47[], int bcp47Count, |
| 89 | SkUnichar character) const; |
bungeman@google.com | 72cf4fc | 2014-03-21 22:48:32 +0000 | [diff] [blame] | 90 | |
commit-bot@chromium.org | 967dee3 | 2014-02-04 22:35:01 +0000 | [diff] [blame] | 91 | SkTypeface* matchFaceStyle(const SkTypeface*, const SkFontStyle&) const; |
reed@google.com | 95625db | 2013-03-25 20:44:02 +0000 | [diff] [blame] | 92 | |
reed@google.com | c452d82 | 2013-03-25 18:44:17 +0000 | [diff] [blame] | 93 | /** |
| 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 Reed | 5922739 | 2017-09-26 09:46:08 -0400 | [diff] [blame^] | 98 | sk_sp<SkTypeface> makeFromData(sk_sp<SkData>, int ttcIndex = 0) const; |
reed@google.com | c452d82 | 2013-03-25 18:44:17 +0000 | [diff] [blame] | 99 | |
| 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 Reed | 5922739 | 2017-09-26 09:46:08 -0400 | [diff] [blame^] | 105 | sk_sp<SkTypeface> makeFromStream(std::unique_ptr<SkStreamAsset>, int ttcIndex = 0) const; |
reed@google.com | c452d82 | 2013-03-25 18:44:17 +0000 | [diff] [blame] | 106 | |
Mike Reed | 5922739 | 2017-09-26 09:46:08 -0400 | [diff] [blame^] | 107 | #ifdef SK_SUPPORT_LEGACY_FONTMGR_API |
Ben Wagner | fc49734 | 2017-02-24 11:15:26 -0500 | [diff] [blame] | 108 | using FontParameters = SkFontArguments; |
Mike Reed | 5922739 | 2017-09-26 09:46:08 -0400 | [diff] [blame^] | 109 | #endif |
| 110 | |
bungeman | f6c7107 | 2016-01-21 14:17:47 -0800 | [diff] [blame] | 111 | /* Experimental, API subject to change. */ |
Mike Reed | 5922739 | 2017-09-26 09:46:08 -0400 | [diff] [blame^] | 112 | sk_sp<SkTypeface> makeFromStream(std::unique_ptr<SkStreamAsset>, const SkFontArguments&) const; |
bungeman | f6c7107 | 2016-01-21 14:17:47 -0800 | [diff] [blame] | 113 | |
reed@google.com | c452d82 | 2013-03-25 18:44:17 +0000 | [diff] [blame] | 114 | /** |
bungeman | 41868fe | 2015-05-20 09:21:04 -0700 | [diff] [blame] | 115 | * Create a typeface from the specified font data. |
bungeman | 41868fe | 2015-05-20 09:21:04 -0700 | [diff] [blame] | 116 | * 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 Reed | 5922739 | 2017-09-26 09:46:08 -0400 | [diff] [blame^] | 119 | sk_sp<SkTypeface> makeFromFontData(std::unique_ptr<SkFontData>) const; |
bungeman | 41868fe | 2015-05-20 09:21:04 -0700 | [diff] [blame] | 120 | |
| 121 | /** |
reed@google.com | c452d82 | 2013-03-25 18:44:17 +0000 | [diff] [blame] | 122 | * 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 Reed | 5922739 | 2017-09-26 09:46:08 -0400 | [diff] [blame^] | 127 | sk_sp<SkTypeface> makeFromFile(const char path[], int ttcIndex = 0) const; |
Mike Reed | f40ae1a | 2017-09-25 20:00:31 +0000 | [diff] [blame] | 128 | |
Mike Reed | 5922739 | 2017-09-26 09:46:08 -0400 | [diff] [blame^] | 129 | 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; |
bungeman | 11a77c6 | 2016-04-12 13:45:06 -0700 | [diff] [blame] | 137 | SkTypeface* legacyCreateTypeface(const char familyName[], SkFontStyle style) const; |
Mike Reed | 5922739 | 2017-09-26 09:46:08 -0400 | [diff] [blame^] | 138 | #endif |
reed@google.com | 30ddd61 | 2013-07-30 17:47:39 +0000 | [diff] [blame] | 139 | |
Ben Wagner | 3546ff1 | 2017-01-03 13:32:36 -0500 | [diff] [blame] | 140 | /** Return the default fontmgr. */ |
Ben Wagner | 3546ff1 | 2017-01-03 13:32:36 -0500 | [diff] [blame] | 141 | static sk_sp<SkFontMgr> RefDefault(); |
reed@google.com | 95625db | 2013-03-25 20:44:02 +0000 | [diff] [blame] | 142 | |
| 143 | protected: |
commit-bot@chromium.org | 967dee3 | 2014-02-04 22:35:01 +0000 | [diff] [blame] | 144 | 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.com | 95625db | 2013-03-25 20:44:02 +0000 | [diff] [blame] | 147 | |
bungeman@google.com | 9fc5c68 | 2013-11-12 15:25:29 +0000 | [diff] [blame] | 148 | /** May return NULL if the name is not found. */ |
commit-bot@chromium.org | 967dee3 | 2014-02-04 22:35:01 +0000 | [diff] [blame] | 149 | virtual SkFontStyleSet* onMatchFamily(const char familyName[]) const = 0; |
reed@google.com | 964988f | 2013-03-29 14:57:22 +0000 | [diff] [blame] | 150 | |
reed@google.com | 95625db | 2013-03-25 20:44:02 +0000 | [diff] [blame] | 151 | virtual SkTypeface* onMatchFamilyStyle(const char familyName[], |
commit-bot@chromium.org | 967dee3 | 2014-02-04 22:35:01 +0000 | [diff] [blame] | 152 | const SkFontStyle&) const = 0; |
bungeman@google.com | 72cf4fc | 2014-03-21 22:48:32 +0000 | [diff] [blame] | 153 | virtual SkTypeface* onMatchFamilyStyleCharacter(const char familyName[], const SkFontStyle&, |
bungeman | c20386e | 2014-10-23 07:08:05 -0700 | [diff] [blame] | 154 | const char* bcp47[], int bcp47Count, |
djsollen | 33068c1 | 2014-11-14 10:52:53 -0800 | [diff] [blame] | 155 | SkUnichar character) const = 0; |
reed@google.com | 95625db | 2013-03-25 20:44:02 +0000 | [diff] [blame] | 156 | virtual SkTypeface* onMatchFaceStyle(const SkTypeface*, |
commit-bot@chromium.org | 967dee3 | 2014-02-04 22:35:01 +0000 | [diff] [blame] | 157 | const SkFontStyle&) const = 0; |
reed@google.com | 95625db | 2013-03-25 20:44:02 +0000 | [diff] [blame] | 158 | |
Mike Reed | 5922739 | 2017-09-26 09:46:08 -0400 | [diff] [blame^] | 159 | #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 Wagner | fc49734 | 2017-02-24 11:15:26 -0500 | [diff] [blame] | 163 | virtual SkTypeface* onCreateFromStream(SkStreamAsset*, const SkFontArguments&) const; |
bungeman | f93d711 | 2016-09-16 06:24:20 -0700 | [diff] [blame] | 164 | virtual SkTypeface* onCreateFromFontData(std::unique_ptr<SkFontData>) const; |
Mike Reed | 5922739 | 2017-09-26 09:46:08 -0400 | [diff] [blame^] | 165 | virtual SkTypeface* onCreateFromFile(const char[], int) const { return nullptr; } |
| 166 | virtual SkTypeface* onLegacyCreateTypeface(const char[], SkFontStyle) const { |
| 167 | return nullptr; |
| 168 | } |
bungeman@google.com | fb1663a | 2013-10-24 22:32:43 +0000 | [diff] [blame] | 169 | |
Mike Reed | 5922739 | 2017-09-26 09:46:08 -0400 | [diff] [blame^] | 170 | // 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 |
bungeman | 11a77c6 | 2016-04-12 13:45:06 -0700 | [diff] [blame] | 191 | |
reed@google.com | c452d82 | 2013-03-25 18:44:17 +0000 | [diff] [blame] | 192 | private: |
Ben Wagner | 3546ff1 | 2017-01-03 13:32:36 -0500 | [diff] [blame] | 193 | |
| 194 | /** Implemented by porting layer to return the default factory. */ |
Ben Wagner | 3546ff1 | 2017-01-03 13:32:36 -0500 | [diff] [blame] | 195 | static sk_sp<SkFontMgr> Factory(); |
skia.committer@gmail.com | e60ed08 | 2013-03-26 07:01:04 +0000 | [diff] [blame] | 196 | |
reed@google.com | c452d82 | 2013-03-25 18:44:17 +0000 | [diff] [blame] | 197 | typedef SkRefCnt INHERITED; |
| 198 | }; |
| 199 | |
| 200 | #endif |