blob: 5d62e367f0a736580f61bf28413794da7548925d [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
bungeman@google.comb3d154d2013-11-11 15:53:29 +000024/** The base SkTypeface implementation for the custom font manager. */
25class SkTypeface_Custom : public SkTypeface_FreeType {
reed@android.com8a1c16f2008-12-17 15:59:43 +000026public:
bungemana4c4a2d2014-10-20 13:33:19 -070027 SkTypeface_Custom(const SkFontStyle& style, bool isFixedPitch,
bungemand71b7572014-09-18 10:55:32 -070028 bool sysFont, const SkString familyName, int index)
bungeman@google.comb3d154d2013-11-11 15:53:29 +000029 : INHERITED(style, SkTypefaceCache::NewFontID(), isFixedPitch)
bungemand71b7572014-09-18 10:55:32 -070030 , fIsSysFont(sysFont), fFamilyName(familyName), fIndex(index)
bungeman@google.comb3d154d2013-11-11 15:53:29 +000031 { }
chudy@google.comada44802012-07-30 12:59:12 +000032
reed@android.com8a1c16f2008-12-17 15:59:43 +000033 bool isSysFont() const { return fIsSysFont; }
reed@google.com292b1d42013-03-22 17:21:59 +000034
reed@android.com8a1c16f2008-12-17 15:59:43 +000035 virtual const char* getUniqueString() const = 0;
chudy@google.comada44802012-07-30 12:59:12 +000036
reed@google.com5526ede2013-03-25 13:03:37 +000037protected:
mtklein36352bf2015-03-25 18:17:31 -070038 void onGetFamilyName(SkString* familyName) const override {
bungemanb374d6a2014-09-17 07:48:59 -070039 *familyName = fFamilyName;
40 }
41
mtklein36352bf2015-03-25 18:17:31 -070042 void onGetFontDescriptor(SkFontDescriptor* desc, bool* isLocal) const override {
bungeman@google.comb3d154d2013-11-11 15:53:29 +000043 desc->setFamilyName(fFamilyName.c_str());
44 desc->setFontFileName(this->getUniqueString());
bungemand71b7572014-09-18 10:55:32 -070045 desc->setFontIndex(fIndex);
bungeman@google.comb3d154d2013-11-11 15:53:29 +000046 *isLocal = !this->isSysFont();
47 }
reed@google.com5526ede2013-03-25 13:03:37 +000048
bungemand71b7572014-09-18 10:55:32 -070049 int getIndex() const { return fIndex; }
50
reed@android.com8a1c16f2008-12-17 15:59:43 +000051private:
bungemand71b7572014-09-18 10:55:32 -070052 const bool fIsSysFont;
53 const SkString fFamilyName;
54 const int fIndex;
chudy@google.comada44802012-07-30 12:59:12 +000055
reed@google.com032fbb82013-03-21 13:38:18 +000056 typedef SkTypeface_FreeType INHERITED;
reed@android.com8a1c16f2008-12-17 15:59:43 +000057};
58
bungeman@google.comb3d154d2013-11-11 15:53:29 +000059/** The empty SkTypeface implementation for the custom font manager.
60 * Used as the last resort fallback typeface.
reed@android.comf244f1b2010-04-16 12:40:08 +000061 */
bungeman@google.comb3d154d2013-11-11 15:53:29 +000062class SkTypeface_Empty : public SkTypeface_Custom {
reed@android.comf244f1b2010-04-16 12:40:08 +000063public:
bungemana4c4a2d2014-10-20 13:33:19 -070064 SkTypeface_Empty() : INHERITED(SkFontStyle(), false, true, SkString(), 0) {}
chudy@google.comada44802012-07-30 12:59:12 +000065
mtklein36352bf2015-03-25 18:17:31 -070066 const char* getUniqueString() const override { return NULL; }
chudy@google.comada44802012-07-30 12:59:12 +000067
reed@google.com292b1d42013-03-22 17:21:59 +000068protected:
mtklein36352bf2015-03-25 18:17:31 -070069 SkStreamAsset* onOpenStream(int*) const override { return NULL; }
reed@google.com292b1d42013-03-22 17:21:59 +000070
reed@android.comf244f1b2010-04-16 12:40:08 +000071private:
bungeman@google.comb3d154d2013-11-11 15:53:29 +000072 typedef SkTypeface_Custom INHERITED;
reed@android.comf244f1b2010-04-16 12:40:08 +000073};
74
bungeman@google.comb3d154d2013-11-11 15:53:29 +000075/** The stream SkTypeface implementation for the custom font manager. */
76class SkTypeface_Stream : public SkTypeface_Custom {
reed@android.com8a1c16f2008-12-17 15:59:43 +000077public:
bungemana4c4a2d2014-10-20 13:33:19 -070078 SkTypeface_Stream(const SkFontStyle& style, bool isFixedPitch, bool sysFont,
bungeman5f213d92015-01-27 05:39:10 -080079 const SkString familyName, SkStreamAsset* stream, int index)
bungemana4c4a2d2014-10-20 13:33:19 -070080 : INHERITED(style, isFixedPitch, sysFont, familyName, index)
scroggoe58898e2015-01-21 12:23:20 -080081 , fStream(stream)
bungeman@google.comb3d154d2013-11-11 15:53:29 +000082 { }
chudy@google.comada44802012-07-30 12:59:12 +000083
mtklein36352bf2015-03-25 18:17:31 -070084 const char* getUniqueString() const override { return NULL; }
chudy@google.comada44802012-07-30 12:59:12 +000085
reed@google.com292b1d42013-03-22 17:21:59 +000086protected:
mtklein36352bf2015-03-25 18:17:31 -070087 SkStreamAsset* onOpenStream(int* ttcIndex) const override {
bungemand71b7572014-09-18 10:55:32 -070088 *ttcIndex = this->getIndex();
commit-bot@chromium.orga2b44dc2014-03-24 21:42:01 +000089 return fStream->duplicate();
reed@google.com292b1d42013-03-22 17:21:59 +000090 }
91
reed@android.com8a1c16f2008-12-17 15:59:43 +000092private:
bungeman5f213d92015-01-27 05:39:10 -080093 const SkAutoTDelete<const SkStreamAsset> fStream;
chudy@google.comada44802012-07-30 12:59:12 +000094
bungeman@google.comb3d154d2013-11-11 15:53:29 +000095 typedef SkTypeface_Custom INHERITED;
reed@android.com8a1c16f2008-12-17 15:59:43 +000096};
97
humperad5e9a52014-11-19 08:32:19 -080098// This configuration option is useful if we need to open and hold handles to
99// all found system font data (e.g., for skfiddle, where the application can't
100// access the filesystem to read fonts on demand)
101
102SK_CONF_DECLARE(bool, c_CustomTypefaceRetain, "fonts.customFont.retainAllData", false,
103 "Retain the open stream for each found font on the system.");
104
bungeman@google.comb3d154d2013-11-11 15:53:29 +0000105/** The file SkTypeface implementation for the custom font manager. */
106class SkTypeface_File : public SkTypeface_Custom {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000107public:
bungemana4c4a2d2014-10-20 13:33:19 -0700108 SkTypeface_File(const SkFontStyle& style, bool isFixedPitch, bool sysFont,
109 const SkString familyName, const char path[], int index)
bungemand71b7572014-09-18 10:55:32 -0700110 : INHERITED(style, isFixedPitch, sysFont, familyName, index)
bungeman@google.comb3d154d2013-11-11 15:53:29 +0000111 , fPath(path)
humperad5e9a52014-11-19 08:32:19 -0800112 , fStream(c_CustomTypefaceRetain ? SkStream::NewFromFile(fPath.c_str()) : NULL)
bungeman@google.comb3d154d2013-11-11 15:53:29 +0000113 { }
chudy@google.comada44802012-07-30 12:59:12 +0000114
mtklein36352bf2015-03-25 18:17:31 -0700115 const char* getUniqueString() const override {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000116 const char* str = strrchr(fPath.c_str(), '/');
117 if (str) {
118 str += 1; // skip the '/'
119 }
120 return str;
121 }
chudy@google.comada44802012-07-30 12:59:12 +0000122
reed@google.com292b1d42013-03-22 17:21:59 +0000123protected:
mtklein36352bf2015-03-25 18:17:31 -0700124 SkStreamAsset* onOpenStream(int* ttcIndex) const override {
bungemand71b7572014-09-18 10:55:32 -0700125 *ttcIndex = this->getIndex();
humperad5e9a52014-11-19 08:32:19 -0800126 if (fStream.get()) {
127 return fStream->duplicate();
128 } else {
129 return SkStream::NewFromFile(fPath.c_str());
130 }
reed@google.com292b1d42013-03-22 17:21:59 +0000131 }
132
reed@android.com8a1c16f2008-12-17 15:59:43 +0000133private:
134 SkString fPath;
scroggoa1193e42015-01-21 12:09:53 -0800135 const SkAutoTDelete<SkStreamAsset> fStream;
chudy@google.comada44802012-07-30 12:59:12 +0000136
bungeman@google.comb3d154d2013-11-11 15:53:29 +0000137 typedef SkTypeface_Custom INHERITED;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000138};
139
140///////////////////////////////////////////////////////////////////////////////
reed@android.com8a1c16f2008-12-17 15:59:43 +0000141
bungeman@google.comb3d154d2013-11-11 15:53:29 +0000142/**
143 * SkFontStyleSet_Custom
144 *
145 * This class is used by SkFontMgr_Custom to hold SkTypeface_Custom families.
146 */
147class SkFontStyleSet_Custom : public SkFontStyleSet {
148public:
149 explicit SkFontStyleSet_Custom(const SkString familyName) : fFamilyName(familyName) { }
150
bungeman5c9fa282015-03-30 12:53:48 -0700151 /** Should only be called during the inital build phase. */
152 void appendTypeface(SkTypeface_Custom* typeface) {
153 fStyles.push_back().reset(typeface);
154 }
155
mtklein36352bf2015-03-25 18:17:31 -0700156 int count() override {
bungeman@google.comb3d154d2013-11-11 15:53:29 +0000157 return fStyles.count();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000158 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000159
mtklein36352bf2015-03-25 18:17:31 -0700160 void getStyle(int index, SkFontStyle* style, SkString* name) override {
bungeman@google.comb3d154d2013-11-11 15:53:29 +0000161 SkASSERT(index < fStyles.count());
162 bool bold = fStyles[index]->isBold();
163 bool italic = fStyles[index]->isItalic();
164 *style = SkFontStyle(bold ? SkFontStyle::kBold_Weight : SkFontStyle::kNormal_Weight,
165 SkFontStyle::kNormal_Width,
166 italic ? SkFontStyle::kItalic_Slant : SkFontStyle::kUpright_Slant);
167 name->reset();
168 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000169
mtklein36352bf2015-03-25 18:17:31 -0700170 SkTypeface* createTypeface(int index) override {
bungeman@google.comb3d154d2013-11-11 15:53:29 +0000171 SkASSERT(index < fStyles.count());
172 return SkRef(fStyles[index].get());
173 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000174
bungeman@google.comb3d154d2013-11-11 15:53:29 +0000175 static int match_score(const SkFontStyle& pattern, const SkFontStyle& candidate) {
176 int score = 0;
177 score += (pattern.width() - candidate.width()) * 100;
178 score += (pattern.isItalic() == candidate.isItalic()) ? 0 : 1000;
179 score += pattern.weight() - candidate.weight();
180 return score;
181 }
182
mtklein36352bf2015-03-25 18:17:31 -0700183 SkTypeface* matchStyle(const SkFontStyle& pattern) override {
bungeman@google.comb3d154d2013-11-11 15:53:29 +0000184 if (0 == fStyles.count()) {
185 return NULL;
186 }
187
188 SkTypeface_Custom* closest = fStyles[0];
189 int minScore = std::numeric_limits<int>::max();
190 for (int i = 0; i < fStyles.count(); ++i) {
191 bool bold = fStyles[i]->isBold();
192 bool italic = fStyles[i]->isItalic();
193 SkFontStyle style = SkFontStyle(bold ? SkFontStyle::kBold_Weight
194 : SkFontStyle::kNormal_Weight,
195 SkFontStyle::kNormal_Width,
196 italic ? SkFontStyle::kItalic_Slant
197 : SkFontStyle::kUpright_Slant);
198
199 int score = match_score(pattern, style);
200 if (score < minScore) {
201 closest = fStyles[i];
202 minScore = score;
203 }
204 }
205 return SkRef(closest);
206 }
207
bungeman5c9fa282015-03-30 12:53:48 -0700208 SkString getFamilyName() { return fFamilyName; }
209
bungeman@google.comb3d154d2013-11-11 15:53:29 +0000210private:
211 SkTArray<SkAutoTUnref<SkTypeface_Custom>, true> fStyles;
212 SkString fFamilyName;
213
bungeman@google.comb3d154d2013-11-11 15:53:29 +0000214 friend class SkFontMgr_Custom;
215};
216
217/**
218 * SkFontMgr_Custom
219 *
220 * This class is essentially a collection of SkFontStyleSet_Custom,
221 * one SkFontStyleSet_Custom for each family. This class may be modified
222 * to load fonts from any source by changing the initialization.
223 */
224class SkFontMgr_Custom : public SkFontMgr {
225public:
bungeman5c9fa282015-03-30 12:53:48 -0700226 typedef SkTArray<SkAutoTUnref<SkFontStyleSet_Custom>, true> Families;
227 class SystemFontLoader {
228 public:
229 virtual ~SystemFontLoader() { }
230 virtual void loadSystemFonts(const SkTypeface_FreeType::Scanner&, Families*) const = 0;
231 };
232 explicit SkFontMgr_Custom(const SystemFontLoader& loader) {
233 loader.loadSystemFonts(fScanner, &fFamilies);
234
235 // Try to pick a default font.
236 static const char* defaultNames[] = {
237 "Arial", "Verdana", "Times New Roman", "Droid Sans", NULL
238 };
239 for (size_t i = 0; i < SK_ARRAY_COUNT(defaultNames); ++i) {
240 SkFontStyleSet_Custom* set = this->onMatchFamily(defaultNames[i]);
241 if (NULL == set) {
242 continue;
243 }
244
245 SkTypeface* tf = set->matchStyle(SkFontStyle(SkFontStyle::kNormal_Weight,
246 SkFontStyle::kNormal_Width,
247 SkFontStyle::kUpright_Slant));
248 if (NULL == tf) {
249 continue;
250 }
251
252 fDefaultFamily = set;
253 break;
254 }
255 if (NULL == fDefaultFamily) {
256 fDefaultFamily = fFamilies[0];
257 }
bungeman@google.comb3d154d2013-11-11 15:53:29 +0000258 }
259
260protected:
mtklein36352bf2015-03-25 18:17:31 -0700261 int onCountFamilies() const override {
bungeman@google.comb3d154d2013-11-11 15:53:29 +0000262 return fFamilies.count();
263 }
264
mtklein36352bf2015-03-25 18:17:31 -0700265 void onGetFamilyName(int index, SkString* familyName) const override {
bungeman@google.comb3d154d2013-11-11 15:53:29 +0000266 SkASSERT(index < fFamilies.count());
bungeman5c9fa282015-03-30 12:53:48 -0700267 familyName->set(fFamilies[index]->getFamilyName());
bungeman@google.comb3d154d2013-11-11 15:53:29 +0000268 }
269
mtklein36352bf2015-03-25 18:17:31 -0700270 SkFontStyleSet_Custom* onCreateStyleSet(int index) const override {
bungeman@google.comb3d154d2013-11-11 15:53:29 +0000271 SkASSERT(index < fFamilies.count());
272 return SkRef(fFamilies[index].get());
273 }
274
mtklein36352bf2015-03-25 18:17:31 -0700275 SkFontStyleSet_Custom* onMatchFamily(const char familyName[]) const override {
bungeman@google.comb3d154d2013-11-11 15:53:29 +0000276 for (int i = 0; i < fFamilies.count(); ++i) {
bungeman5c9fa282015-03-30 12:53:48 -0700277 if (fFamilies[i]->getFamilyName().equals(familyName)) {
bungeman@google.comb3d154d2013-11-11 15:53:29 +0000278 return SkRef(fFamilies[i].get());
279 }
280 }
281 return NULL;
282 }
283
bungeman5c9fa282015-03-30 12:53:48 -0700284 SkTypeface* onMatchFamilyStyle(const char familyName[],
285 const SkFontStyle& fontStyle) const override
bungeman@google.comb3d154d2013-11-11 15:53:29 +0000286 {
287 SkAutoTUnref<SkFontStyleSet> sset(this->matchFamily(familyName));
288 return sset->matchStyle(fontStyle);
289 }
290
bungeman5c9fa282015-03-30 12:53:48 -0700291 SkTypeface* onMatchFamilyStyleCharacter(const char familyName[], const SkFontStyle&,
292 const char* bcp47[], int bcp47Count,
293 SkUnichar character) const override
djsollen33068c12014-11-14 10:52:53 -0800294 {
295 return NULL;
296 }
297
bungeman5c9fa282015-03-30 12:53:48 -0700298 SkTypeface* onMatchFaceStyle(const SkTypeface* familyMember,
299 const SkFontStyle& fontStyle) const override
bungeman@google.comb3d154d2013-11-11 15:53:29 +0000300 {
301 for (int i = 0; i < fFamilies.count(); ++i) {
302 for (int j = 0; j < fFamilies[i]->fStyles.count(); ++j) {
303 if (fFamilies[i]->fStyles[j] == familyMember) {
304 return fFamilies[i]->matchStyle(fontStyle);
305 }
306 }
307 }
308 return NULL;
309 }
310
mtklein36352bf2015-03-25 18:17:31 -0700311 SkTypeface* onCreateFromData(SkData* data, int ttcIndex) const override {
scroggoa1193e42015-01-21 12:09:53 -0800312 return this->createFromStream(new SkMemoryStream(data), ttcIndex);
bungeman@google.comb3d154d2013-11-11 15:53:29 +0000313 }
314
mtklein36352bf2015-03-25 18:17:31 -0700315 SkTypeface* onCreateFromStream(SkStreamAsset* bareStream, int ttcIndex) const override {
bungeman5f213d92015-01-27 05:39:10 -0800316 SkAutoTDelete<SkStreamAsset> stream(bareStream);
bungeman@google.comb3d154d2013-11-11 15:53:29 +0000317 if (NULL == stream || stream->getLength() <= 0) {
bungeman@google.comb3d154d2013-11-11 15:53:29 +0000318 return NULL;
319 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000320
bungeman@google.comfe747652013-03-25 19:36:11 +0000321 bool isFixedPitch;
bungemana4c4a2d2014-10-20 13:33:19 -0700322 SkFontStyle style;
bungeman@google.comb3d154d2013-11-11 15:53:29 +0000323 SkString name;
bungeman14df8332014-10-28 15:07:23 -0700324 if (fScanner.scanFont(stream, ttcIndex, &name, &style, &isFixedPitch)) {
bungeman3a21d612014-07-11 08:52:26 -0700325 return SkNEW_ARGS(SkTypeface_Stream, (style, isFixedPitch, false, name,
bungeman5f213d92015-01-27 05:39:10 -0800326 stream.detach(), ttcIndex));
bungeman@google.comb3d154d2013-11-11 15:53:29 +0000327 } else {
328 return NULL;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000329 }
reed@android.comf244f1b2010-04-16 12:40:08 +0000330 }
331
mtklein36352bf2015-03-25 18:17:31 -0700332 SkTypeface* onCreateFromFile(const char path[], int ttcIndex) const override {
bungeman5f213d92015-01-27 05:39:10 -0800333 SkAutoTDelete<SkStreamAsset> stream(SkStream::NewFromFile(path));
scroggoa1193e42015-01-21 12:09:53 -0800334 return stream.get() ? this->createFromStream(stream.detach(), ttcIndex) : NULL;
bungeman@google.comb3d154d2013-11-11 15:53:29 +0000335 }
336
bungeman5c9fa282015-03-30 12:53:48 -0700337 SkTypeface* onLegacyCreateTypeface(const char familyName[], unsigned styleBits) const override {
bungeman@google.comb3d154d2013-11-11 15:53:29 +0000338 SkTypeface::Style oldStyle = (SkTypeface::Style)styleBits;
339 SkFontStyle style = SkFontStyle(oldStyle & SkTypeface::kBold
340 ? SkFontStyle::kBold_Weight
341 : SkFontStyle::kNormal_Weight,
342 SkFontStyle::kNormal_Width,
343 oldStyle & SkTypeface::kItalic
344 ? SkFontStyle::kItalic_Slant
345 : SkFontStyle::kUpright_Slant);
346 SkTypeface* tf = NULL;
347
bsalomon49f085d2014-09-05 13:34:00 -0700348 if (familyName) {
bungeman@google.comb3d154d2013-11-11 15:53:29 +0000349 tf = this->onMatchFamilyStyle(familyName, style);
bungeman@google.com2cf84ec2012-09-26 19:16:54 +0000350 }
bungeman@google.com2cf84ec2012-09-26 19:16:54 +0000351
bungeman@google.comb3d154d2013-11-11 15:53:29 +0000352 if (NULL == tf) {
bungeman5c9fa282015-03-30 12:53:48 -0700353 tf = fDefaultFamily->matchStyle(style);
bungeman@google.comb3d154d2013-11-11 15:53:29 +0000354 }
355
356 return SkSafeRef(tf);
bungeman@google.com2cf84ec2012-09-26 19:16:54 +0000357 }
358
bungeman@google.comb3d154d2013-11-11 15:53:29 +0000359private:
bungeman5c9fa282015-03-30 12:53:48 -0700360 Families fFamilies;
361 SkFontStyleSet_Custom* fDefaultFamily;
362 SkTypeface_FreeType::Scanner fScanner;
363};
bungeman@google.com2cf84ec2012-09-26 19:16:54 +0000364
bungeman5c9fa282015-03-30 12:53:48 -0700365///////////////////////////////////////////////////////////////////////////////
366
367class DirectorySystemFontLoader : public SkFontMgr_Custom::SystemFontLoader {
368public:
369 DirectorySystemFontLoader(const char* dir) : fBaseDirectory(dir) { }
370
371 void loadSystemFonts(const SkTypeface_FreeType::Scanner& scanner,
372 SkFontMgr_Custom::Families* families) const override
373 {
374 load_directory_fonts(scanner, fBaseDirectory, ".ttf", families);
375 load_directory_fonts(scanner, fBaseDirectory, ".ttc", families);
376 load_directory_fonts(scanner, fBaseDirectory, ".otf", families);
377 load_directory_fonts(scanner, fBaseDirectory, ".pfb", families);
378
379 if (families->empty()) {
380 SkFontStyleSet_Custom* family = new SkFontStyleSet_Custom(SkString());
381 families->push_back().reset(family);
382 family->appendTypeface(SkNEW(SkTypeface_Empty));
383 }
384 }
385
386private:
387 static SkFontStyleSet_Custom* find_family(SkFontMgr_Custom::Families& families,
388 const char familyName[])
389 {
390 for (int i = 0; i < families.count(); ++i) {
391 if (families[i]->getFamilyName().equals(familyName)) {
392 return families[i].get();
393 }
394 }
395 return NULL;
396 }
397
398 static void load_directory_fonts(const SkTypeface_FreeType::Scanner& scanner,
399 const SkString& directory, const char* suffix,
400 SkFontMgr_Custom::Families* families)
401 {
bungeman14df8332014-10-28 15:07:23 -0700402 SkOSFile::Iter iter(directory.c_str(), suffix);
bungeman@google.comb3d154d2013-11-11 15:53:29 +0000403 SkString name;
404
405 while (iter.next(&name, false)) {
bungeman14df8332014-10-28 15:07:23 -0700406 SkString filename(SkOSPath::Join(directory.c_str(), name.c_str()));
scroggoa1193e42015-01-21 12:09:53 -0800407 SkAutoTDelete<SkStream> stream(SkStream::NewFromFile(filename.c_str()));
bungeman14df8332014-10-28 15:07:23 -0700408 if (!stream.get()) {
409 SkDebugf("---- failed to open <%s>\n", filename.c_str());
bungeman@google.comb3d154d2013-11-11 15:53:29 +0000410 continue;
411 }
412
bungeman14df8332014-10-28 15:07:23 -0700413 int numFaces;
bungeman5c9fa282015-03-30 12:53:48 -0700414 if (!scanner.recognizedFont(stream, &numFaces)) {
bungeman14df8332014-10-28 15:07:23 -0700415 SkDebugf("---- failed to open <%s> as a font\n", filename.c_str());
416 continue;
bungeman@google.comb3d154d2013-11-11 15:53:29 +0000417 }
bungeman14df8332014-10-28 15:07:23 -0700418
419 for (int faceIndex = 0; faceIndex < numFaces; ++faceIndex) {
420 bool isFixedPitch;
421 SkString realname;
422 SkFontStyle style = SkFontStyle(); // avoid uninitialized warning
bungeman5c9fa282015-03-30 12:53:48 -0700423 if (!scanner.scanFont(stream, faceIndex, &realname, &style, &isFixedPitch)) {
bungeman14df8332014-10-28 15:07:23 -0700424 SkDebugf("---- failed to open <%s> <%d> as a font\n",
425 filename.c_str(), faceIndex);
426 continue;
427 }
428
429 SkTypeface_Custom* tf = SkNEW_ARGS(SkTypeface_File, (
430 style,
431 isFixedPitch,
432 true, // system-font (cannot delete)
433 realname,
bungeman5c9fa282015-03-30 12:53:48 -0700434 filename.c_str(),
435 faceIndex));
bungeman14df8332014-10-28 15:07:23 -0700436
bungeman5c9fa282015-03-30 12:53:48 -0700437 SkFontStyleSet_Custom* addTo = find_family(*families, realname.c_str());
bungeman14df8332014-10-28 15:07:23 -0700438 if (NULL == addTo) {
439 addTo = new SkFontStyleSet_Custom(realname);
bungeman5c9fa282015-03-30 12:53:48 -0700440 families->push_back().reset(addTo);
bungeman14df8332014-10-28 15:07:23 -0700441 }
442 addTo->appendTypeface(tf);
443 }
bungeman@google.comb3d154d2013-11-11 15:53:29 +0000444 }
445
446 SkOSFile::Iter dirIter(directory.c_str());
447 while (dirIter.next(&name, true)) {
448 if (name.startsWith(".")) {
449 continue;
450 }
tfarinaa8e2e152014-07-28 19:26:58 -0700451 SkString dirname(SkOSPath::Join(directory.c_str(), name.c_str()));
bungeman5c9fa282015-03-30 12:53:48 -0700452 load_directory_fonts(scanner, dirname, suffix, families);
bungeman@google.comb3d154d2013-11-11 15:53:29 +0000453 }
454 }
455
bungeman5c9fa282015-03-30 12:53:48 -0700456 SkString fBaseDirectory;
bungeman@google.comb3d154d2013-11-11 15:53:29 +0000457};
reed@google.com070da5e2013-03-27 20:01:49 +0000458
bungeman5c9fa282015-03-30 12:53:48 -0700459struct SkEmbeddedResource { const uint8_t* data; size_t size; };
460struct SkEmbeddedResourceHeader { const SkEmbeddedResource* entries; int count; };
461
462class EmbeddedSystemFontLoader : public SkFontMgr_Custom::SystemFontLoader {
463public:
464 EmbeddedSystemFontLoader(const SkEmbeddedResourceHeader* header) : fHeader(header) { }
465
466 void loadSystemFonts(const SkTypeface_FreeType::Scanner& scanner,
467 SkFontMgr_Custom::Families* families) const override
468 {
469 for (int i = 0; i < fHeader->count; ++i) {
470 const SkEmbeddedResource& fontEntry = fHeader->entries[i];
471 load_embedded_font(scanner, fontEntry.data, fontEntry.size, i, families);
472 }
473
474 if (families->empty()) {
475 SkFontStyleSet_Custom* family = new SkFontStyleSet_Custom(SkString());
476 families->push_back().reset(family);
477 family->appendTypeface(SkNEW(SkTypeface_Empty));
478 }
479 }
480
481private:
482 static SkFontStyleSet_Custom* find_family(SkFontMgr_Custom::Families& families,
483 const char familyName[])
484 {
485 for (int i = 0; i < families.count(); ++i) {
486 if (families[i]->getFamilyName().equals(familyName)) {
487 return families[i].get();
488 }
489 }
490 return NULL;
491 }
492
493 static void load_embedded_font(const SkTypeface_FreeType::Scanner& scanner,
494 const uint8_t* data, size_t size, int index,
495 SkFontMgr_Custom::Families* families)
496 {
497 SkAutoTDelete<SkMemoryStream> stream(new SkMemoryStream(data, size, false));
498
499 int numFaces;
500 if (!scanner.recognizedFont(stream, &numFaces)) {
501 SkDebugf("---- failed to open <%d> as a font\n", index);
502 return;
503 }
504
505 for (int faceIndex = 0; faceIndex < numFaces; ++faceIndex) {
506 bool isFixedPitch;
507 SkString realname;
508 SkFontStyle style = SkFontStyle(); // avoid uninitialized warning
509 if (!scanner.scanFont(stream, faceIndex, &realname, &style, &isFixedPitch)) {
510 SkDebugf("---- failed to open <%d> <%d> as a font\n", index, faceIndex);
511 return;
512 }
513
514 SkTypeface_Custom* tf = SkNEW_ARGS(SkTypeface_Stream, (
515 style,
516 isFixedPitch,
517 true, // system-font (cannot delete)
518 realname,
519 stream.detach(),
520 faceIndex));
521
522 SkFontStyleSet_Custom* addTo = find_family(*families, realname.c_str());
523 if (NULL == addTo) {
524 addTo = new SkFontStyleSet_Custom(realname);
525 families->push_back().reset(addTo);
526 }
527 addTo->appendTypeface(tf);
528 }
529 }
530
531 const SkEmbeddedResourceHeader* fHeader;
532};
533
534#ifdef SK_EMBEDDED_FONTS
535
536extern "C" const SkEmbeddedResourceHeader SK_EMBEDDED_FONTS;
reed@google.com070da5e2013-03-27 20:01:49 +0000537SkFontMgr* SkFontMgr::Factory() {
bungeman5c9fa282015-03-30 12:53:48 -0700538 return new SkFontMgr_Custom(EmbeddedSystemFontLoader(&SK_EMBEDDED_FONTS));
reed@google.com070da5e2013-03-27 20:01:49 +0000539}
bungeman5c9fa282015-03-30 12:53:48 -0700540
541#else
542
543#ifndef SK_FONT_FILE_PREFIX
544# define SK_FONT_FILE_PREFIX "/usr/share/fonts/"
545#endif
546SkFontMgr* SkFontMgr::Factory() {
547 return new SkFontMgr_Custom(DirectorySystemFontLoader(SK_FONT_FILE_PREFIX));
548}
549
550#endif