blob: ed5e58dd332b2cf814aa98e69808989225c604e0 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001/*
2 * Copyright 2006 The Android Open Source Project
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
reed@android.com8a1c16f2008-12-17 15:59:43 +00006 */
7
djsollen@google.com97145162012-05-31 19:55:08 +00008#include "SkFontDescriptor.h"
bungemanf20488b2015-07-29 11:49:40 -07009#include "SkFontHost_FreeType_common.h"
bungeman@google.comb3d154d2013-11-11 15:53:29 +000010#include "SkFontMgr.h"
bungeman5cf19492015-06-15 15:17:21 -070011#include "SkFontMgr_custom.h"
bungemanf20488b2015-07-29 11:49:40 -070012#include "SkFontStyle.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000013#include "SkOSFile.h"
bungemanf20488b2015-07-29 11:49:40 -070014#include "SkRefCnt.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000015#include "SkStream.h"
bungemanf20488b2015-07-29 11:49:40 -070016#include "SkString.h"
bungeman@google.comb3d154d2013-11-11 15:53:29 +000017#include "SkTArray.h"
bungemanf20488b2015-07-29 11:49:40 -070018#include "SkTemplates.h"
19#include "SkTypeface.h"
20#include "SkTypefaceCache.h"
21#include "SkTypes.h"
bungeman@google.comb3d154d2013-11-11 15:53:29 +000022
23#include <limits>
reed@android.com8a1c16f2008-12-17 15:59:43 +000024
bungemanf20488b2015-07-29 11:49:40 -070025class SkData;
26
bungeman@google.comb3d154d2013-11-11 15:53:29 +000027/** The base SkTypeface implementation for the custom font manager. */
28class SkTypeface_Custom : public SkTypeface_FreeType {
reed@android.com8a1c16f2008-12-17 15:59:43 +000029public:
bungemana4c4a2d2014-10-20 13:33:19 -070030 SkTypeface_Custom(const SkFontStyle& style, bool isFixedPitch,
bungemand71b7572014-09-18 10:55:32 -070031 bool sysFont, const SkString familyName, int index)
bungeman@google.comb3d154d2013-11-11 15:53:29 +000032 : INHERITED(style, SkTypefaceCache::NewFontID(), isFixedPitch)
bungemand71b7572014-09-18 10:55:32 -070033 , fIsSysFont(sysFont), fFamilyName(familyName), fIndex(index)
bungeman@google.comb3d154d2013-11-11 15:53:29 +000034 { }
chudy@google.comada44802012-07-30 12:59:12 +000035
reed@android.com8a1c16f2008-12-17 15:59:43 +000036 bool isSysFont() const { return fIsSysFont; }
reed@google.com292b1d42013-03-22 17:21:59 +000037
reed@google.com5526ede2013-03-25 13:03:37 +000038protected:
mtklein36352bf2015-03-25 18:17:31 -070039 void onGetFamilyName(SkString* familyName) const override {
bungemanb374d6a2014-09-17 07:48:59 -070040 *familyName = fFamilyName;
41 }
42
mtklein36352bf2015-03-25 18:17:31 -070043 void onGetFontDescriptor(SkFontDescriptor* desc, bool* isLocal) const override {
bungeman@google.comb3d154d2013-11-11 15:53:29 +000044 desc->setFamilyName(fFamilyName.c_str());
bungeman@google.comb3d154d2013-11-11 15:53:29 +000045 *isLocal = !this->isSysFont();
46 }
reed@google.com5526ede2013-03-25 13:03:37 +000047
bungemand71b7572014-09-18 10:55:32 -070048 int getIndex() const { return fIndex; }
49
reed@android.com8a1c16f2008-12-17 15:59:43 +000050private:
bungemand71b7572014-09-18 10:55:32 -070051 const bool fIsSysFont;
52 const SkString fFamilyName;
53 const int fIndex;
chudy@google.comada44802012-07-30 12:59:12 +000054
reed@google.com032fbb82013-03-21 13:38:18 +000055 typedef SkTypeface_FreeType INHERITED;
reed@android.com8a1c16f2008-12-17 15:59:43 +000056};
57
bungeman@google.comb3d154d2013-11-11 15:53:29 +000058/** The empty SkTypeface implementation for the custom font manager.
59 * Used as the last resort fallback typeface.
reed@android.comf244f1b2010-04-16 12:40:08 +000060 */
bungeman@google.comb3d154d2013-11-11 15:53:29 +000061class SkTypeface_Empty : public SkTypeface_Custom {
reed@android.comf244f1b2010-04-16 12:40:08 +000062public:
bungemana4c4a2d2014-10-20 13:33:19 -070063 SkTypeface_Empty() : INHERITED(SkFontStyle(), false, true, SkString(), 0) {}
chudy@google.comada44802012-07-30 12:59:12 +000064
reed@google.com292b1d42013-03-22 17:21:59 +000065protected:
halcanary96fcdcc2015-08-27 07:41:13 -070066 SkStreamAsset* onOpenStream(int*) const override { return nullptr; }
reed@google.com292b1d42013-03-22 17:21:59 +000067
reed@android.comf244f1b2010-04-16 12:40:08 +000068private:
bungeman@google.comb3d154d2013-11-11 15:53:29 +000069 typedef SkTypeface_Custom INHERITED;
reed@android.comf244f1b2010-04-16 12:40:08 +000070};
71
bungeman@google.comb3d154d2013-11-11 15:53:29 +000072/** The stream SkTypeface implementation for the custom font manager. */
73class SkTypeface_Stream : public SkTypeface_Custom {
reed@android.com8a1c16f2008-12-17 15:59:43 +000074public:
bungemana4c4a2d2014-10-20 13:33:19 -070075 SkTypeface_Stream(const SkFontStyle& style, bool isFixedPitch, bool sysFont,
bungeman5f213d92015-01-27 05:39:10 -080076 const SkString familyName, SkStreamAsset* stream, int index)
bungemana4c4a2d2014-10-20 13:33:19 -070077 : INHERITED(style, isFixedPitch, sysFont, familyName, index)
scroggoe58898e2015-01-21 12:23:20 -080078 , fStream(stream)
bungeman@google.comb3d154d2013-11-11 15:53:29 +000079 { }
chudy@google.comada44802012-07-30 12:59:12 +000080
reed@google.com292b1d42013-03-22 17:21:59 +000081protected:
mtklein36352bf2015-03-25 18:17:31 -070082 SkStreamAsset* onOpenStream(int* ttcIndex) const override {
bungemand71b7572014-09-18 10:55:32 -070083 *ttcIndex = this->getIndex();
commit-bot@chromium.orga2b44dc2014-03-24 21:42:01 +000084 return fStream->duplicate();
reed@google.com292b1d42013-03-22 17:21:59 +000085 }
86
reed@android.com8a1c16f2008-12-17 15:59:43 +000087private:
bungeman5f213d92015-01-27 05:39:10 -080088 const SkAutoTDelete<const SkStreamAsset> fStream;
chudy@google.comada44802012-07-30 12:59:12 +000089
bungeman@google.comb3d154d2013-11-11 15:53:29 +000090 typedef SkTypeface_Custom INHERITED;
reed@android.com8a1c16f2008-12-17 15:59:43 +000091};
92
bungeman@google.comb3d154d2013-11-11 15:53:29 +000093/** The file SkTypeface implementation for the custom font manager. */
94class SkTypeface_File : public SkTypeface_Custom {
reed@android.com8a1c16f2008-12-17 15:59:43 +000095public:
bungemana4c4a2d2014-10-20 13:33:19 -070096 SkTypeface_File(const SkFontStyle& style, bool isFixedPitch, bool sysFont,
97 const SkString familyName, const char path[], int index)
bungemand71b7572014-09-18 10:55:32 -070098 : INHERITED(style, isFixedPitch, sysFont, familyName, index)
bungeman@google.comb3d154d2013-11-11 15:53:29 +000099 , fPath(path)
100 { }
chudy@google.comada44802012-07-30 12:59:12 +0000101
reed@google.com292b1d42013-03-22 17:21:59 +0000102protected:
mtklein36352bf2015-03-25 18:17:31 -0700103 SkStreamAsset* onOpenStream(int* ttcIndex) const override {
bungemand71b7572014-09-18 10:55:32 -0700104 *ttcIndex = this->getIndex();
bungeman5cf19492015-06-15 15:17:21 -0700105 return SkStream::NewFromFile(fPath.c_str());
reed@google.com292b1d42013-03-22 17:21:59 +0000106 }
107
reed@android.com8a1c16f2008-12-17 15:59:43 +0000108private:
109 SkString fPath;
chudy@google.comada44802012-07-30 12:59:12 +0000110
bungeman@google.comb3d154d2013-11-11 15:53:29 +0000111 typedef SkTypeface_Custom INHERITED;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000112};
113
114///////////////////////////////////////////////////////////////////////////////
reed@android.com8a1c16f2008-12-17 15:59:43 +0000115
bungeman@google.comb3d154d2013-11-11 15:53:29 +0000116/**
117 * SkFontStyleSet_Custom
118 *
119 * This class is used by SkFontMgr_Custom to hold SkTypeface_Custom families.
120 */
121class SkFontStyleSet_Custom : public SkFontStyleSet {
122public:
123 explicit SkFontStyleSet_Custom(const SkString familyName) : fFamilyName(familyName) { }
124
bungeman5c9fa282015-03-30 12:53:48 -0700125 /** Should only be called during the inital build phase. */
126 void appendTypeface(SkTypeface_Custom* typeface) {
127 fStyles.push_back().reset(typeface);
128 }
129
mtklein36352bf2015-03-25 18:17:31 -0700130 int count() override {
bungeman@google.comb3d154d2013-11-11 15:53:29 +0000131 return fStyles.count();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000132 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000133
mtklein36352bf2015-03-25 18:17:31 -0700134 void getStyle(int index, SkFontStyle* style, SkString* name) override {
bungeman@google.comb3d154d2013-11-11 15:53:29 +0000135 SkASSERT(index < fStyles.count());
bungeman77a8ab22016-04-27 12:30:25 -0700136 if (style) {
137 *style = fStyles[index]->fontStyle();
138 }
139 if (name) {
140 name->reset();
141 }
bungeman@google.comb3d154d2013-11-11 15:53:29 +0000142 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000143
mtklein36352bf2015-03-25 18:17:31 -0700144 SkTypeface* createTypeface(int index) override {
bungeman@google.comb3d154d2013-11-11 15:53:29 +0000145 SkASSERT(index < fStyles.count());
146 return SkRef(fStyles[index].get());
147 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000148
mtklein36352bf2015-03-25 18:17:31 -0700149 SkTypeface* matchStyle(const SkFontStyle& pattern) override {
bungeman77a8ab22016-04-27 12:30:25 -0700150 return this->matchStyleCSS3(pattern);
bungeman@google.comb3d154d2013-11-11 15:53:29 +0000151 }
152
bungeman5c9fa282015-03-30 12:53:48 -0700153 SkString getFamilyName() { return fFamilyName; }
154
bungeman@google.comb3d154d2013-11-11 15:53:29 +0000155private:
156 SkTArray<SkAutoTUnref<SkTypeface_Custom>, true> fStyles;
157 SkString fFamilyName;
158
bungeman@google.comb3d154d2013-11-11 15:53:29 +0000159 friend class SkFontMgr_Custom;
160};
161
162/**
163 * SkFontMgr_Custom
164 *
165 * This class is essentially a collection of SkFontStyleSet_Custom,
166 * one SkFontStyleSet_Custom for each family. This class may be modified
167 * to load fonts from any source by changing the initialization.
168 */
169class SkFontMgr_Custom : public SkFontMgr {
170public:
bungeman5c9fa282015-03-30 12:53:48 -0700171 typedef SkTArray<SkAutoTUnref<SkFontStyleSet_Custom>, true> Families;
172 class SystemFontLoader {
173 public:
174 virtual ~SystemFontLoader() { }
175 virtual void loadSystemFonts(const SkTypeface_FreeType::Scanner&, Families*) const = 0;
176 };
halcanary96fcdcc2015-08-27 07:41:13 -0700177 explicit SkFontMgr_Custom(const SystemFontLoader& loader) : fDefaultFamily(nullptr) {
bungeman5c9fa282015-03-30 12:53:48 -0700178 loader.loadSystemFonts(fScanner, &fFamilies);
179
180 // Try to pick a default font.
181 static const char* defaultNames[] = {
halcanary96fcdcc2015-08-27 07:41:13 -0700182 "Arial", "Verdana", "Times New Roman", "Droid Sans", nullptr
bungeman5c9fa282015-03-30 12:53:48 -0700183 };
184 for (size_t i = 0; i < SK_ARRAY_COUNT(defaultNames); ++i) {
185 SkFontStyleSet_Custom* set = this->onMatchFamily(defaultNames[i]);
halcanary96fcdcc2015-08-27 07:41:13 -0700186 if (nullptr == set) {
bungeman5c9fa282015-03-30 12:53:48 -0700187 continue;
188 }
189
190 SkTypeface* tf = set->matchStyle(SkFontStyle(SkFontStyle::kNormal_Weight,
191 SkFontStyle::kNormal_Width,
192 SkFontStyle::kUpright_Slant));
halcanary96fcdcc2015-08-27 07:41:13 -0700193 if (nullptr == tf) {
bungeman5c9fa282015-03-30 12:53:48 -0700194 continue;
195 }
196
197 fDefaultFamily = set;
198 break;
199 }
halcanary96fcdcc2015-08-27 07:41:13 -0700200 if (nullptr == fDefaultFamily) {
bungeman5c9fa282015-03-30 12:53:48 -0700201 fDefaultFamily = fFamilies[0];
202 }
bungeman@google.comb3d154d2013-11-11 15:53:29 +0000203 }
204
205protected:
mtklein36352bf2015-03-25 18:17:31 -0700206 int onCountFamilies() const override {
bungeman@google.comb3d154d2013-11-11 15:53:29 +0000207 return fFamilies.count();
208 }
209
mtklein36352bf2015-03-25 18:17:31 -0700210 void onGetFamilyName(int index, SkString* familyName) const override {
bungeman@google.comb3d154d2013-11-11 15:53:29 +0000211 SkASSERT(index < fFamilies.count());
bungeman5c9fa282015-03-30 12:53:48 -0700212 familyName->set(fFamilies[index]->getFamilyName());
bungeman@google.comb3d154d2013-11-11 15:53:29 +0000213 }
214
mtklein36352bf2015-03-25 18:17:31 -0700215 SkFontStyleSet_Custom* onCreateStyleSet(int index) const override {
bungeman@google.comb3d154d2013-11-11 15:53:29 +0000216 SkASSERT(index < fFamilies.count());
217 return SkRef(fFamilies[index].get());
218 }
219
mtklein36352bf2015-03-25 18:17:31 -0700220 SkFontStyleSet_Custom* onMatchFamily(const char familyName[]) const override {
bungeman@google.comb3d154d2013-11-11 15:53:29 +0000221 for (int i = 0; i < fFamilies.count(); ++i) {
bungeman5c9fa282015-03-30 12:53:48 -0700222 if (fFamilies[i]->getFamilyName().equals(familyName)) {
bungeman@google.comb3d154d2013-11-11 15:53:29 +0000223 return SkRef(fFamilies[i].get());
224 }
225 }
halcanary96fcdcc2015-08-27 07:41:13 -0700226 return nullptr;
bungeman@google.comb3d154d2013-11-11 15:53:29 +0000227 }
228
bungeman5c9fa282015-03-30 12:53:48 -0700229 SkTypeface* onMatchFamilyStyle(const char familyName[],
230 const SkFontStyle& fontStyle) const override
bungeman@google.comb3d154d2013-11-11 15:53:29 +0000231 {
232 SkAutoTUnref<SkFontStyleSet> sset(this->matchFamily(familyName));
233 return sset->matchStyle(fontStyle);
234 }
235
bungeman5c9fa282015-03-30 12:53:48 -0700236 SkTypeface* onMatchFamilyStyleCharacter(const char familyName[], const SkFontStyle&,
237 const char* bcp47[], int bcp47Count,
238 SkUnichar character) const override
djsollen33068c12014-11-14 10:52:53 -0800239 {
halcanary96fcdcc2015-08-27 07:41:13 -0700240 return nullptr;
djsollen33068c12014-11-14 10:52:53 -0800241 }
242
bungeman5c9fa282015-03-30 12:53:48 -0700243 SkTypeface* onMatchFaceStyle(const SkTypeface* familyMember,
244 const SkFontStyle& fontStyle) const override
bungeman@google.comb3d154d2013-11-11 15:53:29 +0000245 {
246 for (int i = 0; i < fFamilies.count(); ++i) {
247 for (int j = 0; j < fFamilies[i]->fStyles.count(); ++j) {
248 if (fFamilies[i]->fStyles[j] == familyMember) {
249 return fFamilies[i]->matchStyle(fontStyle);
250 }
251 }
252 }
halcanary96fcdcc2015-08-27 07:41:13 -0700253 return nullptr;
bungeman@google.comb3d154d2013-11-11 15:53:29 +0000254 }
255
mtklein36352bf2015-03-25 18:17:31 -0700256 SkTypeface* onCreateFromData(SkData* data, int ttcIndex) const override {
scroggoa1193e42015-01-21 12:09:53 -0800257 return this->createFromStream(new SkMemoryStream(data), ttcIndex);
bungeman@google.comb3d154d2013-11-11 15:53:29 +0000258 }
259
mtklein36352bf2015-03-25 18:17:31 -0700260 SkTypeface* onCreateFromStream(SkStreamAsset* bareStream, int ttcIndex) const override {
bungeman5f213d92015-01-27 05:39:10 -0800261 SkAutoTDelete<SkStreamAsset> stream(bareStream);
halcanary96fcdcc2015-08-27 07:41:13 -0700262 if (nullptr == stream || stream->getLength() <= 0) {
263 return nullptr;
bungeman@google.comb3d154d2013-11-11 15:53:29 +0000264 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000265
bungeman@google.comfe747652013-03-25 19:36:11 +0000266 bool isFixedPitch;
bungemana4c4a2d2014-10-20 13:33:19 -0700267 SkFontStyle style;
bungeman@google.comb3d154d2013-11-11 15:53:29 +0000268 SkString name;
halcanary96fcdcc2015-08-27 07:41:13 -0700269 if (fScanner.scanFont(stream, ttcIndex, &name, &style, &isFixedPitch, nullptr)) {
mtklein18300a32016-03-16 13:53:35 -0700270 return new SkTypeface_Stream(style, isFixedPitch, false, name, stream.release(),
halcanary385fe4d2015-08-26 13:07:48 -0700271 ttcIndex);
bungeman@google.comb3d154d2013-11-11 15:53:29 +0000272 } else {
halcanary96fcdcc2015-08-27 07:41:13 -0700273 return nullptr;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000274 }
reed@android.comf244f1b2010-04-16 12:40:08 +0000275 }
276
mtklein36352bf2015-03-25 18:17:31 -0700277 SkTypeface* onCreateFromFile(const char path[], int ttcIndex) const override {
bungeman5f213d92015-01-27 05:39:10 -0800278 SkAutoTDelete<SkStreamAsset> stream(SkStream::NewFromFile(path));
mtklein18300a32016-03-16 13:53:35 -0700279 return stream.get() ? this->createFromStream(stream.release(), ttcIndex) : nullptr;
bungeman@google.comb3d154d2013-11-11 15:53:29 +0000280 }
281
bungeman11a77c62016-04-12 13:45:06 -0700282 SkTypeface* onLegacyCreateTypeface(const char familyName[], SkFontStyle style) const override {
halcanary96fcdcc2015-08-27 07:41:13 -0700283 SkTypeface* tf = nullptr;
bungeman@google.comb3d154d2013-11-11 15:53:29 +0000284
bsalomon49f085d2014-09-05 13:34:00 -0700285 if (familyName) {
bungeman@google.comb3d154d2013-11-11 15:53:29 +0000286 tf = this->onMatchFamilyStyle(familyName, style);
bungeman@google.com2cf84ec2012-09-26 19:16:54 +0000287 }
bungeman@google.com2cf84ec2012-09-26 19:16:54 +0000288
halcanary96fcdcc2015-08-27 07:41:13 -0700289 if (nullptr == tf) {
bungeman5c9fa282015-03-30 12:53:48 -0700290 tf = fDefaultFamily->matchStyle(style);
bungeman@google.comb3d154d2013-11-11 15:53:29 +0000291 }
292
293 return SkSafeRef(tf);
bungeman@google.com2cf84ec2012-09-26 19:16:54 +0000294 }
295
bungeman@google.comb3d154d2013-11-11 15:53:29 +0000296private:
bungeman5c9fa282015-03-30 12:53:48 -0700297 Families fFamilies;
298 SkFontStyleSet_Custom* fDefaultFamily;
299 SkTypeface_FreeType::Scanner fScanner;
300};
bungeman@google.com2cf84ec2012-09-26 19:16:54 +0000301
bungeman5c9fa282015-03-30 12:53:48 -0700302///////////////////////////////////////////////////////////////////////////////
303
304class DirectorySystemFontLoader : public SkFontMgr_Custom::SystemFontLoader {
305public:
306 DirectorySystemFontLoader(const char* dir) : fBaseDirectory(dir) { }
307
308 void loadSystemFonts(const SkTypeface_FreeType::Scanner& scanner,
309 SkFontMgr_Custom::Families* families) const override
310 {
311 load_directory_fonts(scanner, fBaseDirectory, ".ttf", families);
312 load_directory_fonts(scanner, fBaseDirectory, ".ttc", families);
313 load_directory_fonts(scanner, fBaseDirectory, ".otf", families);
314 load_directory_fonts(scanner, fBaseDirectory, ".pfb", families);
315
316 if (families->empty()) {
317 SkFontStyleSet_Custom* family = new SkFontStyleSet_Custom(SkString());
318 families->push_back().reset(family);
halcanary385fe4d2015-08-26 13:07:48 -0700319 family->appendTypeface(new SkTypeface_Empty);
bungeman5c9fa282015-03-30 12:53:48 -0700320 }
321 }
322
323private:
324 static SkFontStyleSet_Custom* find_family(SkFontMgr_Custom::Families& families,
325 const char familyName[])
326 {
327 for (int i = 0; i < families.count(); ++i) {
328 if (families[i]->getFamilyName().equals(familyName)) {
329 return families[i].get();
330 }
331 }
halcanary96fcdcc2015-08-27 07:41:13 -0700332 return nullptr;
bungeman5c9fa282015-03-30 12:53:48 -0700333 }
334
335 static void load_directory_fonts(const SkTypeface_FreeType::Scanner& scanner,
336 const SkString& directory, const char* suffix,
337 SkFontMgr_Custom::Families* families)
338 {
bungeman14df8332014-10-28 15:07:23 -0700339 SkOSFile::Iter iter(directory.c_str(), suffix);
bungeman@google.comb3d154d2013-11-11 15:53:29 +0000340 SkString name;
341
342 while (iter.next(&name, false)) {
bungeman14df8332014-10-28 15:07:23 -0700343 SkString filename(SkOSPath::Join(directory.c_str(), name.c_str()));
scroggoa1193e42015-01-21 12:09:53 -0800344 SkAutoTDelete<SkStream> stream(SkStream::NewFromFile(filename.c_str()));
bungeman14df8332014-10-28 15:07:23 -0700345 if (!stream.get()) {
346 SkDebugf("---- failed to open <%s>\n", filename.c_str());
bungeman@google.comb3d154d2013-11-11 15:53:29 +0000347 continue;
348 }
349
bungeman14df8332014-10-28 15:07:23 -0700350 int numFaces;
bungeman5c9fa282015-03-30 12:53:48 -0700351 if (!scanner.recognizedFont(stream, &numFaces)) {
bungeman14df8332014-10-28 15:07:23 -0700352 SkDebugf("---- failed to open <%s> as a font\n", filename.c_str());
353 continue;
bungeman@google.comb3d154d2013-11-11 15:53:29 +0000354 }
bungeman14df8332014-10-28 15:07:23 -0700355
356 for (int faceIndex = 0; faceIndex < numFaces; ++faceIndex) {
357 bool isFixedPitch;
358 SkString realname;
359 SkFontStyle style = SkFontStyle(); // avoid uninitialized warning
halcanary96fcdcc2015-08-27 07:41:13 -0700360 if (!scanner.scanFont(stream, faceIndex, &realname, &style, &isFixedPitch, nullptr)) {
bungeman14df8332014-10-28 15:07:23 -0700361 SkDebugf("---- failed to open <%s> <%d> as a font\n",
362 filename.c_str(), faceIndex);
363 continue;
364 }
365
halcanary385fe4d2015-08-26 13:07:48 -0700366 SkTypeface_Custom* tf = new SkTypeface_File(style, isFixedPitch,
367 true, // system-font (cannot delete)
368 realname, filename.c_str(), faceIndex);
bungeman14df8332014-10-28 15:07:23 -0700369
bungeman5c9fa282015-03-30 12:53:48 -0700370 SkFontStyleSet_Custom* addTo = find_family(*families, realname.c_str());
halcanary96fcdcc2015-08-27 07:41:13 -0700371 if (nullptr == addTo) {
bungeman14df8332014-10-28 15:07:23 -0700372 addTo = new SkFontStyleSet_Custom(realname);
bungeman5c9fa282015-03-30 12:53:48 -0700373 families->push_back().reset(addTo);
bungeman14df8332014-10-28 15:07:23 -0700374 }
375 addTo->appendTypeface(tf);
376 }
bungeman@google.comb3d154d2013-11-11 15:53:29 +0000377 }
378
379 SkOSFile::Iter dirIter(directory.c_str());
380 while (dirIter.next(&name, true)) {
381 if (name.startsWith(".")) {
382 continue;
383 }
tfarinaa8e2e152014-07-28 19:26:58 -0700384 SkString dirname(SkOSPath::Join(directory.c_str(), name.c_str()));
bungeman5c9fa282015-03-30 12:53:48 -0700385 load_directory_fonts(scanner, dirname, suffix, families);
bungeman@google.comb3d154d2013-11-11 15:53:29 +0000386 }
387 }
388
bungeman5c9fa282015-03-30 12:53:48 -0700389 SkString fBaseDirectory;
bungeman@google.comb3d154d2013-11-11 15:53:29 +0000390};
reed@google.com070da5e2013-03-27 20:01:49 +0000391
bungeman5cf19492015-06-15 15:17:21 -0700392SK_API SkFontMgr* SkFontMgr_New_Custom_Directory(const char* dir) {
393 return new SkFontMgr_Custom(DirectorySystemFontLoader(dir));
394}
395
396///////////////////////////////////////////////////////////////////////////////
397
bungeman5c9fa282015-03-30 12:53:48 -0700398struct SkEmbeddedResource { const uint8_t* data; size_t size; };
399struct SkEmbeddedResourceHeader { const SkEmbeddedResource* entries; int count; };
400
401class EmbeddedSystemFontLoader : public SkFontMgr_Custom::SystemFontLoader {
402public:
403 EmbeddedSystemFontLoader(const SkEmbeddedResourceHeader* header) : fHeader(header) { }
404
405 void loadSystemFonts(const SkTypeface_FreeType::Scanner& scanner,
406 SkFontMgr_Custom::Families* families) const override
407 {
408 for (int i = 0; i < fHeader->count; ++i) {
409 const SkEmbeddedResource& fontEntry = fHeader->entries[i];
410 load_embedded_font(scanner, fontEntry.data, fontEntry.size, i, families);
411 }
412
413 if (families->empty()) {
414 SkFontStyleSet_Custom* family = new SkFontStyleSet_Custom(SkString());
415 families->push_back().reset(family);
halcanary385fe4d2015-08-26 13:07:48 -0700416 family->appendTypeface(new SkTypeface_Empty);
bungeman5c9fa282015-03-30 12:53:48 -0700417 }
418 }
419
420private:
421 static SkFontStyleSet_Custom* find_family(SkFontMgr_Custom::Families& families,
422 const char familyName[])
423 {
424 for (int i = 0; i < families.count(); ++i) {
425 if (families[i]->getFamilyName().equals(familyName)) {
426 return families[i].get();
427 }
428 }
halcanary96fcdcc2015-08-27 07:41:13 -0700429 return nullptr;
bungeman5c9fa282015-03-30 12:53:48 -0700430 }
431
432 static void load_embedded_font(const SkTypeface_FreeType::Scanner& scanner,
433 const uint8_t* data, size_t size, int index,
434 SkFontMgr_Custom::Families* families)
435 {
436 SkAutoTDelete<SkMemoryStream> stream(new SkMemoryStream(data, size, false));
437
438 int numFaces;
439 if (!scanner.recognizedFont(stream, &numFaces)) {
440 SkDebugf("---- failed to open <%d> as a font\n", index);
441 return;
442 }
443
444 for (int faceIndex = 0; faceIndex < numFaces; ++faceIndex) {
445 bool isFixedPitch;
446 SkString realname;
447 SkFontStyle style = SkFontStyle(); // avoid uninitialized warning
halcanary96fcdcc2015-08-27 07:41:13 -0700448 if (!scanner.scanFont(stream, faceIndex, &realname, &style, &isFixedPitch, nullptr)) {
bungeman5c9fa282015-03-30 12:53:48 -0700449 SkDebugf("---- failed to open <%d> <%d> as a font\n", index, faceIndex);
450 return;
451 }
452
halcanary385fe4d2015-08-26 13:07:48 -0700453 SkTypeface_Custom* tf =
454 new SkTypeface_Stream(style, isFixedPitch, true, // system-font (cannot delete)
mtklein18300a32016-03-16 13:53:35 -0700455 realname, stream.release(), faceIndex);
bungeman5c9fa282015-03-30 12:53:48 -0700456
457 SkFontStyleSet_Custom* addTo = find_family(*families, realname.c_str());
halcanary96fcdcc2015-08-27 07:41:13 -0700458 if (nullptr == addTo) {
bungeman5c9fa282015-03-30 12:53:48 -0700459 addTo = new SkFontStyleSet_Custom(realname);
460 families->push_back().reset(addTo);
461 }
462 addTo->appendTypeface(tf);
463 }
464 }
465
466 const SkEmbeddedResourceHeader* fHeader;
467};
468
bungeman5cf19492015-06-15 15:17:21 -0700469SkFontMgr* SkFontMgr_New_Custom_Embedded(const SkEmbeddedResourceHeader* header) {
470 return new SkFontMgr_Custom(EmbeddedSystemFontLoader(header));
reed@google.com070da5e2013-03-27 20:01:49 +0000471}
caryclarkfe7ada72016-03-21 06:55:52 -0700472
473///////////////////////////////////////////////////////////////////////////////
474
475class EmptyFontLoader : public SkFontMgr_Custom::SystemFontLoader {
476public:
477 EmptyFontLoader() { }
478
479 void loadSystemFonts(const SkTypeface_FreeType::Scanner& scanner,
480 SkFontMgr_Custom::Families* families) const override
481 {
482 SkFontStyleSet_Custom* family = new SkFontStyleSet_Custom(SkString());
483 families->push_back().reset(family);
484 family->appendTypeface(new SkTypeface_Empty);
485 }
486
487};
488
489SK_API SkFontMgr* SkFontMgr_New_Custom_Empty() {
490 return new SkFontMgr_Custom(EmptyFontLoader());
491}