blob: d96365b381cd3e5c0493dea516eea4ec54e6d4da [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
reed@google.com0fc17c32013-03-21 13:33:49 +00008#include "SkFontHost_FreeType_common.h"
djsollen@google.com97145162012-05-31 19:55:08 +00009#include "SkFontDescriptor.h"
bungeman@google.comb3d154d2013-11-11 15:53:29 +000010#include "SkFontMgr.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000011#include "SkDescriptor.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000012#include "SkOSFile.h"
13#include "SkPaint.h"
humperad5e9a52014-11-19 08:32:19 -080014#include "SkRTConf.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000015#include "SkString.h"
16#include "SkStream.h"
17#include "SkThread.h"
18#include "SkTSearch.h"
bungeman@google.comb3d154d2013-11-11 15:53:29 +000019#include "SkTypefaceCache.h"
20#include "SkTArray.h"
21
22#include <limits>
reed@android.com8a1c16f2008-12-17 15:59:43 +000023
reed@android.com8a1c16f2008-12-17 15:59:43 +000024#ifndef SK_FONT_FILE_PREFIX
bungeman14df8332014-10-28 15:07:23 -070025# define SK_FONT_FILE_PREFIX "/usr/share/fonts/"
bungeman@google.com2cf84ec2012-09-26 19:16:54 +000026#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +000027
reed@android.com8a1c16f2008-12-17 15:59:43 +000028///////////////////////////////////////////////////////////////////////////////
29
bungeman@google.comb3d154d2013-11-11 15:53:29 +000030/** The base SkTypeface implementation for the custom font manager. */
31class SkTypeface_Custom : public SkTypeface_FreeType {
reed@android.com8a1c16f2008-12-17 15:59:43 +000032public:
bungemana4c4a2d2014-10-20 13:33:19 -070033 SkTypeface_Custom(const SkFontStyle& style, bool isFixedPitch,
bungemand71b7572014-09-18 10:55:32 -070034 bool sysFont, const SkString familyName, int index)
bungeman@google.comb3d154d2013-11-11 15:53:29 +000035 : INHERITED(style, SkTypefaceCache::NewFontID(), isFixedPitch)
bungemand71b7572014-09-18 10:55:32 -070036 , fIsSysFont(sysFont), fFamilyName(familyName), fIndex(index)
bungeman@google.comb3d154d2013-11-11 15:53:29 +000037 { }
chudy@google.comada44802012-07-30 12:59:12 +000038
reed@android.com8a1c16f2008-12-17 15:59:43 +000039 bool isSysFont() const { return fIsSysFont; }
reed@google.com292b1d42013-03-22 17:21:59 +000040
reed@android.com8a1c16f2008-12-17 15:59:43 +000041 virtual const char* getUniqueString() const = 0;
chudy@google.comada44802012-07-30 12:59:12 +000042
reed@google.com5526ede2013-03-25 13:03:37 +000043protected:
mtklein36352bf2015-03-25 18:17:31 -070044 void onGetFamilyName(SkString* familyName) const override {
bungemanb374d6a2014-09-17 07:48:59 -070045 *familyName = fFamilyName;
46 }
47
mtklein36352bf2015-03-25 18:17:31 -070048 void onGetFontDescriptor(SkFontDescriptor* desc, bool* isLocal) const override {
bungeman@google.comb3d154d2013-11-11 15:53:29 +000049 desc->setFamilyName(fFamilyName.c_str());
50 desc->setFontFileName(this->getUniqueString());
bungemand71b7572014-09-18 10:55:32 -070051 desc->setFontIndex(fIndex);
bungeman@google.comb3d154d2013-11-11 15:53:29 +000052 *isLocal = !this->isSysFont();
53 }
reed@google.com5526ede2013-03-25 13:03:37 +000054
bungemand71b7572014-09-18 10:55:32 -070055 int getIndex() const { return fIndex; }
56
reed@android.com8a1c16f2008-12-17 15:59:43 +000057private:
bungemand71b7572014-09-18 10:55:32 -070058 const bool fIsSysFont;
59 const SkString fFamilyName;
60 const int fIndex;
chudy@google.comada44802012-07-30 12:59:12 +000061
reed@google.com032fbb82013-03-21 13:38:18 +000062 typedef SkTypeface_FreeType INHERITED;
reed@android.com8a1c16f2008-12-17 15:59:43 +000063};
64
bungeman@google.comb3d154d2013-11-11 15:53:29 +000065/** The empty SkTypeface implementation for the custom font manager.
66 * Used as the last resort fallback typeface.
reed@android.comf244f1b2010-04-16 12:40:08 +000067 */
bungeman@google.comb3d154d2013-11-11 15:53:29 +000068class SkTypeface_Empty : public SkTypeface_Custom {
reed@android.comf244f1b2010-04-16 12:40:08 +000069public:
bungemana4c4a2d2014-10-20 13:33:19 -070070 SkTypeface_Empty() : INHERITED(SkFontStyle(), false, true, SkString(), 0) {}
chudy@google.comada44802012-07-30 12:59:12 +000071
mtklein36352bf2015-03-25 18:17:31 -070072 const char* getUniqueString() const override { return NULL; }
chudy@google.comada44802012-07-30 12:59:12 +000073
reed@google.com292b1d42013-03-22 17:21:59 +000074protected:
mtklein36352bf2015-03-25 18:17:31 -070075 SkStreamAsset* onOpenStream(int*) const override { return NULL; }
reed@google.com292b1d42013-03-22 17:21:59 +000076
reed@android.comf244f1b2010-04-16 12:40:08 +000077private:
bungeman@google.comb3d154d2013-11-11 15:53:29 +000078 typedef SkTypeface_Custom INHERITED;
reed@android.comf244f1b2010-04-16 12:40:08 +000079};
80
bungeman@google.comb3d154d2013-11-11 15:53:29 +000081/** The stream SkTypeface implementation for the custom font manager. */
82class SkTypeface_Stream : public SkTypeface_Custom {
reed@android.com8a1c16f2008-12-17 15:59:43 +000083public:
bungemana4c4a2d2014-10-20 13:33:19 -070084 SkTypeface_Stream(const SkFontStyle& style, bool isFixedPitch, bool sysFont,
bungeman5f213d92015-01-27 05:39:10 -080085 const SkString familyName, SkStreamAsset* stream, int index)
bungemana4c4a2d2014-10-20 13:33:19 -070086 : INHERITED(style, isFixedPitch, sysFont, familyName, index)
scroggoe58898e2015-01-21 12:23:20 -080087 , fStream(stream)
bungeman@google.comb3d154d2013-11-11 15:53:29 +000088 { }
chudy@google.comada44802012-07-30 12:59:12 +000089
mtklein36352bf2015-03-25 18:17:31 -070090 const char* getUniqueString() const override { return NULL; }
chudy@google.comada44802012-07-30 12:59:12 +000091
reed@google.com292b1d42013-03-22 17:21:59 +000092protected:
mtklein36352bf2015-03-25 18:17:31 -070093 SkStreamAsset* onOpenStream(int* ttcIndex) const override {
bungemand71b7572014-09-18 10:55:32 -070094 *ttcIndex = this->getIndex();
commit-bot@chromium.orga2b44dc2014-03-24 21:42:01 +000095 return fStream->duplicate();
reed@google.com292b1d42013-03-22 17:21:59 +000096 }
97
reed@android.com8a1c16f2008-12-17 15:59:43 +000098private:
bungeman5f213d92015-01-27 05:39:10 -080099 const SkAutoTDelete<const SkStreamAsset> fStream;
chudy@google.comada44802012-07-30 12:59:12 +0000100
bungeman@google.comb3d154d2013-11-11 15:53:29 +0000101 typedef SkTypeface_Custom INHERITED;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000102};
103
humperad5e9a52014-11-19 08:32:19 -0800104// This configuration option is useful if we need to open and hold handles to
105// all found system font data (e.g., for skfiddle, where the application can't
106// access the filesystem to read fonts on demand)
107
108SK_CONF_DECLARE(bool, c_CustomTypefaceRetain, "fonts.customFont.retainAllData", false,
109 "Retain the open stream for each found font on the system.");
110
bungeman@google.comb3d154d2013-11-11 15:53:29 +0000111/** The file SkTypeface implementation for the custom font manager. */
112class SkTypeface_File : public SkTypeface_Custom {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000113public:
bungemana4c4a2d2014-10-20 13:33:19 -0700114 SkTypeface_File(const SkFontStyle& style, bool isFixedPitch, bool sysFont,
115 const SkString familyName, const char path[], int index)
bungemand71b7572014-09-18 10:55:32 -0700116 : INHERITED(style, isFixedPitch, sysFont, familyName, index)
bungeman@google.comb3d154d2013-11-11 15:53:29 +0000117 , fPath(path)
humperad5e9a52014-11-19 08:32:19 -0800118 , fStream(c_CustomTypefaceRetain ? SkStream::NewFromFile(fPath.c_str()) : NULL)
bungeman@google.comb3d154d2013-11-11 15:53:29 +0000119 { }
chudy@google.comada44802012-07-30 12:59:12 +0000120
mtklein36352bf2015-03-25 18:17:31 -0700121 const char* getUniqueString() const override {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000122 const char* str = strrchr(fPath.c_str(), '/');
123 if (str) {
124 str += 1; // skip the '/'
125 }
126 return str;
127 }
chudy@google.comada44802012-07-30 12:59:12 +0000128
reed@google.com292b1d42013-03-22 17:21:59 +0000129protected:
mtklein36352bf2015-03-25 18:17:31 -0700130 SkStreamAsset* onOpenStream(int* ttcIndex) const override {
bungemand71b7572014-09-18 10:55:32 -0700131 *ttcIndex = this->getIndex();
humperad5e9a52014-11-19 08:32:19 -0800132 if (fStream.get()) {
133 return fStream->duplicate();
134 } else {
135 return SkStream::NewFromFile(fPath.c_str());
136 }
reed@google.com292b1d42013-03-22 17:21:59 +0000137 }
138
reed@android.com8a1c16f2008-12-17 15:59:43 +0000139private:
140 SkString fPath;
scroggoa1193e42015-01-21 12:09:53 -0800141 const SkAutoTDelete<SkStreamAsset> fStream;
chudy@google.comada44802012-07-30 12:59:12 +0000142
bungeman@google.comb3d154d2013-11-11 15:53:29 +0000143 typedef SkTypeface_Custom INHERITED;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000144};
145
146///////////////////////////////////////////////////////////////////////////////
reed@android.com8a1c16f2008-12-17 15:59:43 +0000147
bungeman@google.comb3d154d2013-11-11 15:53:29 +0000148/**
149 * SkFontStyleSet_Custom
150 *
151 * This class is used by SkFontMgr_Custom to hold SkTypeface_Custom families.
152 */
153class SkFontStyleSet_Custom : public SkFontStyleSet {
154public:
155 explicit SkFontStyleSet_Custom(const SkString familyName) : fFamilyName(familyName) { }
156
mtklein36352bf2015-03-25 18:17:31 -0700157 int count() override {
bungeman@google.comb3d154d2013-11-11 15:53:29 +0000158 return fStyles.count();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000159 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000160
mtklein36352bf2015-03-25 18:17:31 -0700161 void getStyle(int index, SkFontStyle* style, SkString* name) override {
bungeman@google.comb3d154d2013-11-11 15:53:29 +0000162 SkASSERT(index < fStyles.count());
163 bool bold = fStyles[index]->isBold();
164 bool italic = fStyles[index]->isItalic();
165 *style = SkFontStyle(bold ? SkFontStyle::kBold_Weight : SkFontStyle::kNormal_Weight,
166 SkFontStyle::kNormal_Width,
167 italic ? SkFontStyle::kItalic_Slant : SkFontStyle::kUpright_Slant);
168 name->reset();
169 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000170
mtklein36352bf2015-03-25 18:17:31 -0700171 SkTypeface* createTypeface(int index) override {
bungeman@google.comb3d154d2013-11-11 15:53:29 +0000172 SkASSERT(index < fStyles.count());
173 return SkRef(fStyles[index].get());
174 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000175
bungeman@google.comb3d154d2013-11-11 15:53:29 +0000176 static int match_score(const SkFontStyle& pattern, const SkFontStyle& candidate) {
177 int score = 0;
178 score += (pattern.width() - candidate.width()) * 100;
179 score += (pattern.isItalic() == candidate.isItalic()) ? 0 : 1000;
180 score += pattern.weight() - candidate.weight();
181 return score;
182 }
183
mtklein36352bf2015-03-25 18:17:31 -0700184 SkTypeface* matchStyle(const SkFontStyle& pattern) override {
bungeman@google.comb3d154d2013-11-11 15:53:29 +0000185 if (0 == fStyles.count()) {
186 return NULL;
187 }
188
189 SkTypeface_Custom* closest = fStyles[0];
190 int minScore = std::numeric_limits<int>::max();
191 for (int i = 0; i < fStyles.count(); ++i) {
192 bool bold = fStyles[i]->isBold();
193 bool italic = fStyles[i]->isItalic();
194 SkFontStyle style = SkFontStyle(bold ? SkFontStyle::kBold_Weight
195 : SkFontStyle::kNormal_Weight,
196 SkFontStyle::kNormal_Width,
197 italic ? SkFontStyle::kItalic_Slant
198 : SkFontStyle::kUpright_Slant);
199
200 int score = match_score(pattern, style);
201 if (score < minScore) {
202 closest = fStyles[i];
203 minScore = score;
204 }
205 }
206 return SkRef(closest);
207 }
208
209private:
210 SkTArray<SkAutoTUnref<SkTypeface_Custom>, true> fStyles;
211 SkString fFamilyName;
212
213 void appendTypeface(SkTypeface_Custom* typeface) {
214 fStyles.push_back().reset(typeface);
215 }
216
217 friend class SkFontMgr_Custom;
218};
219
220/**
221 * SkFontMgr_Custom
222 *
223 * This class is essentially a collection of SkFontStyleSet_Custom,
224 * one SkFontStyleSet_Custom for each family. This class may be modified
225 * to load fonts from any source by changing the initialization.
226 */
227class SkFontMgr_Custom : public SkFontMgr {
228public:
229 explicit SkFontMgr_Custom(const char* dir) {
230 this->load_system_fonts(dir);
231 }
232
233protected:
mtklein36352bf2015-03-25 18:17:31 -0700234 int onCountFamilies() const override {
bungeman@google.comb3d154d2013-11-11 15:53:29 +0000235 return fFamilies.count();
236 }
237
mtklein36352bf2015-03-25 18:17:31 -0700238 void onGetFamilyName(int index, SkString* familyName) const override {
bungeman@google.comb3d154d2013-11-11 15:53:29 +0000239 SkASSERT(index < fFamilies.count());
240 familyName->set(fFamilies[index]->fFamilyName);
241 }
242
mtklein36352bf2015-03-25 18:17:31 -0700243 SkFontStyleSet_Custom* onCreateStyleSet(int index) const override {
bungeman@google.comb3d154d2013-11-11 15:53:29 +0000244 SkASSERT(index < fFamilies.count());
245 return SkRef(fFamilies[index].get());
246 }
247
mtklein36352bf2015-03-25 18:17:31 -0700248 SkFontStyleSet_Custom* onMatchFamily(const char familyName[]) const override {
bungeman@google.comb3d154d2013-11-11 15:53:29 +0000249 for (int i = 0; i < fFamilies.count(); ++i) {
250 if (fFamilies[i]->fFamilyName.equals(familyName)) {
251 return SkRef(fFamilies[i].get());
252 }
253 }
254 return NULL;
255 }
256
257 virtual SkTypeface* onMatchFamilyStyle(const char familyName[],
mtklein36352bf2015-03-25 18:17:31 -0700258 const SkFontStyle& fontStyle) const override
bungeman@google.comb3d154d2013-11-11 15:53:29 +0000259 {
260 SkAutoTUnref<SkFontStyleSet> sset(this->matchFamily(familyName));
261 return sset->matchStyle(fontStyle);
262 }
263
djsollen33068c12014-11-14 10:52:53 -0800264 virtual SkTypeface* onMatchFamilyStyleCharacter(const char familyName[], const SkFontStyle&,
265 const char* bcp47[], int bcp47Count,
mtklein36352bf2015-03-25 18:17:31 -0700266 SkUnichar character) const override
djsollen33068c12014-11-14 10:52:53 -0800267 {
268 return NULL;
269 }
270
bungeman@google.comb3d154d2013-11-11 15:53:29 +0000271 virtual SkTypeface* onMatchFaceStyle(const SkTypeface* familyMember,
mtklein36352bf2015-03-25 18:17:31 -0700272 const SkFontStyle& fontStyle) const override
bungeman@google.comb3d154d2013-11-11 15:53:29 +0000273 {
274 for (int i = 0; i < fFamilies.count(); ++i) {
275 for (int j = 0; j < fFamilies[i]->fStyles.count(); ++j) {
276 if (fFamilies[i]->fStyles[j] == familyMember) {
277 return fFamilies[i]->matchStyle(fontStyle);
278 }
279 }
280 }
281 return NULL;
282 }
283
mtklein36352bf2015-03-25 18:17:31 -0700284 SkTypeface* onCreateFromData(SkData* data, int ttcIndex) const override {
scroggoa1193e42015-01-21 12:09:53 -0800285 return this->createFromStream(new SkMemoryStream(data), ttcIndex);
bungeman@google.comb3d154d2013-11-11 15:53:29 +0000286 }
287
mtklein36352bf2015-03-25 18:17:31 -0700288 SkTypeface* onCreateFromStream(SkStreamAsset* bareStream, int ttcIndex) const override {
bungeman5f213d92015-01-27 05:39:10 -0800289 SkAutoTDelete<SkStreamAsset> stream(bareStream);
bungeman@google.comb3d154d2013-11-11 15:53:29 +0000290 if (NULL == stream || stream->getLength() <= 0) {
bungeman@google.comb3d154d2013-11-11 15:53:29 +0000291 return NULL;
292 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000293
bungeman@google.comfe747652013-03-25 19:36:11 +0000294 bool isFixedPitch;
bungemana4c4a2d2014-10-20 13:33:19 -0700295 SkFontStyle style;
bungeman@google.comb3d154d2013-11-11 15:53:29 +0000296 SkString name;
bungeman14df8332014-10-28 15:07:23 -0700297 if (fScanner.scanFont(stream, ttcIndex, &name, &style, &isFixedPitch)) {
bungeman3a21d612014-07-11 08:52:26 -0700298 return SkNEW_ARGS(SkTypeface_Stream, (style, isFixedPitch, false, name,
bungeman5f213d92015-01-27 05:39:10 -0800299 stream.detach(), ttcIndex));
bungeman@google.comb3d154d2013-11-11 15:53:29 +0000300 } else {
301 return NULL;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000302 }
reed@android.comf244f1b2010-04-16 12:40:08 +0000303 }
304
mtklein36352bf2015-03-25 18:17:31 -0700305 SkTypeface* onCreateFromFile(const char path[], int ttcIndex) const override {
bungeman5f213d92015-01-27 05:39:10 -0800306 SkAutoTDelete<SkStreamAsset> stream(SkStream::NewFromFile(path));
scroggoa1193e42015-01-21 12:09:53 -0800307 return stream.get() ? this->createFromStream(stream.detach(), ttcIndex) : NULL;
bungeman@google.comb3d154d2013-11-11 15:53:29 +0000308 }
309
310 virtual SkTypeface* onLegacyCreateTypeface(const char familyName[],
mtklein36352bf2015-03-25 18:17:31 -0700311 unsigned styleBits) const override
bungeman@google.comb3d154d2013-11-11 15:53:29 +0000312 {
313 SkTypeface::Style oldStyle = (SkTypeface::Style)styleBits;
314 SkFontStyle style = SkFontStyle(oldStyle & SkTypeface::kBold
315 ? SkFontStyle::kBold_Weight
316 : SkFontStyle::kNormal_Weight,
317 SkFontStyle::kNormal_Width,
318 oldStyle & SkTypeface::kItalic
319 ? SkFontStyle::kItalic_Slant
320 : SkFontStyle::kUpright_Slant);
321 SkTypeface* tf = NULL;
322
bsalomon49f085d2014-09-05 13:34:00 -0700323 if (familyName) {
bungeman@google.comb3d154d2013-11-11 15:53:29 +0000324 tf = this->onMatchFamilyStyle(familyName, style);
bungeman@google.com2cf84ec2012-09-26 19:16:54 +0000325 }
bungeman@google.com2cf84ec2012-09-26 19:16:54 +0000326
bungeman@google.comb3d154d2013-11-11 15:53:29 +0000327 if (NULL == tf) {
328 tf = gDefaultFamily->matchStyle(style);
329 }
330
331 return SkSafeRef(tf);
bungeman@google.com2cf84ec2012-09-26 19:16:54 +0000332 }
333
bungeman@google.comb3d154d2013-11-11 15:53:29 +0000334private:
bungeman@google.com2cf84ec2012-09-26 19:16:54 +0000335
bungeman14df8332014-10-28 15:07:23 -0700336 void load_directory_fonts(const SkString& directory, const char* suffix) {
337 SkOSFile::Iter iter(directory.c_str(), suffix);
bungeman@google.comb3d154d2013-11-11 15:53:29 +0000338 SkString name;
339
340 while (iter.next(&name, false)) {
bungeman14df8332014-10-28 15:07:23 -0700341 SkString filename(SkOSPath::Join(directory.c_str(), name.c_str()));
scroggoa1193e42015-01-21 12:09:53 -0800342 SkAutoTDelete<SkStream> stream(SkStream::NewFromFile(filename.c_str()));
bungeman14df8332014-10-28 15:07:23 -0700343 if (!stream.get()) {
344 SkDebugf("---- failed to open <%s>\n", filename.c_str());
bungeman@google.comb3d154d2013-11-11 15:53:29 +0000345 continue;
346 }
347
bungeman14df8332014-10-28 15:07:23 -0700348 int numFaces;
349 if (!fScanner.recognizedFont(stream, &numFaces)) {
350 SkDebugf("---- failed to open <%s> as a font\n", filename.c_str());
351 continue;
bungeman@google.comb3d154d2013-11-11 15:53:29 +0000352 }
bungeman14df8332014-10-28 15:07:23 -0700353
354 for (int faceIndex = 0; faceIndex < numFaces; ++faceIndex) {
355 bool isFixedPitch;
356 SkString realname;
357 SkFontStyle style = SkFontStyle(); // avoid uninitialized warning
358 if (!fScanner.scanFont(stream, faceIndex, &realname, &style, &isFixedPitch)) {
359 SkDebugf("---- failed to open <%s> <%d> as a font\n",
360 filename.c_str(), faceIndex);
361 continue;
362 }
363
364 SkTypeface_Custom* tf = SkNEW_ARGS(SkTypeface_File, (
365 style,
366 isFixedPitch,
367 true, // system-font (cannot delete)
368 realname,
369 filename.c_str(), 0));
370
371 SkFontStyleSet_Custom* addTo = this->onMatchFamily(realname.c_str());
372 if (NULL == addTo) {
373 addTo = new SkFontStyleSet_Custom(realname);
374 fFamilies.push_back().reset(addTo);
375 }
376 addTo->appendTypeface(tf);
377 }
bungeman@google.comb3d154d2013-11-11 15:53:29 +0000378 }
379
380 SkOSFile::Iter dirIter(directory.c_str());
381 while (dirIter.next(&name, true)) {
382 if (name.startsWith(".")) {
383 continue;
384 }
tfarinaa8e2e152014-07-28 19:26:58 -0700385 SkString dirname(SkOSPath::Join(directory.c_str(), name.c_str()));
bungeman14df8332014-10-28 15:07:23 -0700386 load_directory_fonts(dirname, suffix);
bungeman@google.comb3d154d2013-11-11 15:53:29 +0000387 }
388 }
389
390 void load_system_fonts(const char* dir) {
391 SkString baseDirectory(dir);
bungeman14df8332014-10-28 15:07:23 -0700392 load_directory_fonts(baseDirectory, ".ttf");
393 load_directory_fonts(baseDirectory, ".ttc");
394 load_directory_fonts(baseDirectory, ".otf");
395 load_directory_fonts(baseDirectory, ".pfb");
bungeman@google.comb3d154d2013-11-11 15:53:29 +0000396
397 if (fFamilies.empty()) {
398 SkFontStyleSet_Custom* family = new SkFontStyleSet_Custom(SkString());
399 fFamilies.push_back().reset(family);
400 family->appendTypeface(SkNEW(SkTypeface_Empty));
401 }
402
403 // Try to pick a default font.
404 static const char* gDefaultNames[] = {
commit-bot@chromium.orgea163cc2014-03-13 16:24:49 +0000405 "Arial", "Verdana", "Times New Roman", "Droid Sans", NULL
bungeman@google.comb3d154d2013-11-11 15:53:29 +0000406 };
407 for (size_t i = 0; i < SK_ARRAY_COUNT(gDefaultNames); ++i) {
408 SkFontStyleSet_Custom* set = this->onMatchFamily(gDefaultNames[i]);
409 if (NULL == set) {
410 continue;
411 }
412
413 SkTypeface* tf = set->matchStyle(SkFontStyle(SkFontStyle::kNormal_Weight,
414 SkFontStyle::kNormal_Width,
415 SkFontStyle::kUpright_Slant));
416 if (NULL == tf) {
417 continue;
418 }
419
420 gDefaultFamily = set;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000421 gDefaultNormal = tf;
422 break;
423 }
bungeman@google.comb3d154d2013-11-11 15:53:29 +0000424 if (NULL == gDefaultNormal) {
425 gDefaultFamily = fFamilies[0];
426 gDefaultNormal = gDefaultFamily->fStyles[0];
reed@android.com8a1c16f2008-12-17 15:59:43 +0000427 }
428 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000429
bungeman@google.comb3d154d2013-11-11 15:53:29 +0000430 SkTArray<SkAutoTUnref<SkFontStyleSet_Custom>, true> fFamilies;
431 SkFontStyleSet_Custom* gDefaultFamily;
432 SkTypeface* gDefaultNormal;
bungeman14df8332014-10-28 15:07:23 -0700433 SkTypeface_FreeType::Scanner fScanner;
bungeman@google.comb3d154d2013-11-11 15:53:29 +0000434};
reed@google.com070da5e2013-03-27 20:01:49 +0000435
436SkFontMgr* SkFontMgr::Factory() {
bungeman@google.comb3d154d2013-11-11 15:53:29 +0000437 return new SkFontMgr_Custom(SK_FONT_FILE_PREFIX);
reed@google.com070da5e2013-03-27 20:01:49 +0000438}