blob: 2904cac9bcecd34c19c5364bf709f3d881b6d343 [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
8#include "SkFontHost.h"
reed@google.com0fc17c32013-03-21 13:33:49 +00009#include "SkFontHost_FreeType_common.h"
djsollen@google.com97145162012-05-31 19:55:08 +000010#include "SkFontDescriptor.h"
bungeman@google.comb3d154d2013-11-11 15:53:29 +000011#include "SkFontMgr.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000012#include "SkDescriptor.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000013#include "SkOSFile.h"
14#include "SkPaint.h"
15#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
bungeman@google.comb3d154d2013-11-11 15:53:29 +000025# define SK_FONT_FILE_PREFIX "/usr/share/fonts/truetype/"
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:
bungeman3a21d612014-07-11 08:52:26 -070033 SkTypeface_Custom(Style style, bool isFixedPitch, bool sysFont, const SkString familyName)
bungeman@google.comb3d154d2013-11-11 15:53:29 +000034 : INHERITED(style, SkTypefaceCache::NewFontID(), isFixedPitch)
35 , fIsSysFont(sysFont), fFamilyName(familyName)
36 { }
chudy@google.comada44802012-07-30 12:59:12 +000037
reed@android.com8a1c16f2008-12-17 15:59:43 +000038 bool isSysFont() const { return fIsSysFont; }
reed@google.com292b1d42013-03-22 17:21:59 +000039
reed@android.com8a1c16f2008-12-17 15:59:43 +000040 virtual const char* getUniqueString() const = 0;
chudy@google.comada44802012-07-30 12:59:12 +000041
reed@google.com5526ede2013-03-25 13:03:37 +000042protected:
bungemanb374d6a2014-09-17 07:48:59 -070043 virtual void onGetFamilyName(SkString* familyName) const SK_OVERRIDE {
44 *familyName = fFamilyName;
45 }
46
bungeman@google.comb3d154d2013-11-11 15:53:29 +000047 virtual void onGetFontDescriptor(SkFontDescriptor* desc, bool* isLocal) const SK_OVERRIDE {
48 desc->setFamilyName(fFamilyName.c_str());
49 desc->setFontFileName(this->getUniqueString());
50 *isLocal = !this->isSysFont();
51 }
reed@google.com5526ede2013-03-25 13:03:37 +000052
reed@android.com8a1c16f2008-12-17 15:59:43 +000053private:
bungeman@google.comb3d154d2013-11-11 15:53:29 +000054 bool fIsSysFont;
55 SkString fFamilyName;
chudy@google.comada44802012-07-30 12:59:12 +000056
reed@google.com032fbb82013-03-21 13:38:18 +000057 typedef SkTypeface_FreeType INHERITED;
reed@android.com8a1c16f2008-12-17 15:59:43 +000058};
59
bungeman@google.comb3d154d2013-11-11 15:53:29 +000060/** The empty SkTypeface implementation for the custom font manager.
61 * Used as the last resort fallback typeface.
reed@android.comf244f1b2010-04-16 12:40:08 +000062 */
bungeman@google.comb3d154d2013-11-11 15:53:29 +000063class SkTypeface_Empty : public SkTypeface_Custom {
reed@android.comf244f1b2010-04-16 12:40:08 +000064public:
bungeman3a21d612014-07-11 08:52:26 -070065 SkTypeface_Empty() : INHERITED(SkTypeface::kNormal, false, true, SkString()) {}
chudy@google.comada44802012-07-30 12:59:12 +000066
bungeman@google.comb3d154d2013-11-11 15:53:29 +000067 virtual const char* getUniqueString() const SK_OVERRIDE { return NULL; }
chudy@google.comada44802012-07-30 12:59:12 +000068
reed@google.com292b1d42013-03-22 17:21:59 +000069protected:
70 virtual SkStream* onOpenStream(int*) const SK_OVERRIDE { return NULL; }
71
reed@android.comf244f1b2010-04-16 12:40:08 +000072private:
bungeman@google.comb3d154d2013-11-11 15:53:29 +000073 typedef SkTypeface_Custom INHERITED;
reed@android.comf244f1b2010-04-16 12:40:08 +000074};
75
bungeman@google.comb3d154d2013-11-11 15:53:29 +000076/** The stream SkTypeface implementation for the custom font manager. */
77class SkTypeface_Stream : public SkTypeface_Custom {
reed@android.com8a1c16f2008-12-17 15:59:43 +000078public:
bungeman3a21d612014-07-11 08:52:26 -070079 SkTypeface_Stream(Style style, bool isFixedPitch, bool sysFont, const SkString familyName,
80 SkStream* stream, int ttcIndex)
81 : INHERITED(style, isFixedPitch, sysFont, familyName)
82 , fStream(SkRef(stream)), fTtcIndex(ttcIndex)
bungeman@google.comb3d154d2013-11-11 15:53:29 +000083 { }
chudy@google.comada44802012-07-30 12:59:12 +000084
reed@google.come1575aa2013-03-18 21:08:46 +000085 virtual const char* getUniqueString() const SK_OVERRIDE { return NULL; }
chudy@google.comada44802012-07-30 12:59:12 +000086
reed@google.com292b1d42013-03-22 17:21:59 +000087protected:
88 virtual SkStream* onOpenStream(int* ttcIndex) const SK_OVERRIDE {
89 *ttcIndex = 0;
commit-bot@chromium.orga2b44dc2014-03-24 21:42:01 +000090 return fStream->duplicate();
reed@google.com292b1d42013-03-22 17:21:59 +000091 }
92
reed@android.com8a1c16f2008-12-17 15:59:43 +000093private:
bungeman@google.comb3d154d2013-11-11 15:53:29 +000094 SkAutoTUnref<SkStream> fStream;
bungeman3a21d612014-07-11 08:52:26 -070095 int fTtcIndex;
chudy@google.comada44802012-07-30 12:59:12 +000096
bungeman@google.comb3d154d2013-11-11 15:53:29 +000097 typedef SkTypeface_Custom INHERITED;
reed@android.com8a1c16f2008-12-17 15:59:43 +000098};
99
bungeman@google.comb3d154d2013-11-11 15:53:29 +0000100/** The file SkTypeface implementation for the custom font manager. */
101class SkTypeface_File : public SkTypeface_Custom {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000102public:
bungeman3a21d612014-07-11 08:52:26 -0700103 SkTypeface_File(Style style, bool isFixedPitch, bool sysFont, const SkString familyName,
104 const char path[])
105 : INHERITED(style, isFixedPitch, sysFont, familyName)
bungeman@google.comb3d154d2013-11-11 15:53:29 +0000106 , fPath(path)
107 { }
chudy@google.comada44802012-07-30 12:59:12 +0000108
reed@google.come1575aa2013-03-18 21:08:46 +0000109 virtual const char* getUniqueString() const SK_OVERRIDE {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000110 const char* str = strrchr(fPath.c_str(), '/');
111 if (str) {
112 str += 1; // skip the '/'
113 }
114 return str;
115 }
chudy@google.comada44802012-07-30 12:59:12 +0000116
reed@google.com292b1d42013-03-22 17:21:59 +0000117protected:
118 virtual SkStream* onOpenStream(int* ttcIndex) const SK_OVERRIDE {
119 *ttcIndex = 0;
120 return SkStream::NewFromFile(fPath.c_str());
121 }
122
reed@android.com8a1c16f2008-12-17 15:59:43 +0000123private:
124 SkString fPath;
chudy@google.comada44802012-07-30 12:59:12 +0000125
bungeman@google.comb3d154d2013-11-11 15:53:29 +0000126 typedef SkTypeface_Custom INHERITED;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000127};
128
129///////////////////////////////////////////////////////////////////////////////
reed@android.com8a1c16f2008-12-17 15:59:43 +0000130
bungeman@google.comb3d154d2013-11-11 15:53:29 +0000131/**
132 * SkFontStyleSet_Custom
133 *
134 * This class is used by SkFontMgr_Custom to hold SkTypeface_Custom families.
135 */
136class SkFontStyleSet_Custom : public SkFontStyleSet {
137public:
138 explicit SkFontStyleSet_Custom(const SkString familyName) : fFamilyName(familyName) { }
139
140 virtual int count() SK_OVERRIDE {
141 return fStyles.count();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000142 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000143
bungeman@google.comb3d154d2013-11-11 15:53:29 +0000144 virtual void getStyle(int index, SkFontStyle* style, SkString* name) SK_OVERRIDE {
145 SkASSERT(index < fStyles.count());
146 bool bold = fStyles[index]->isBold();
147 bool italic = fStyles[index]->isItalic();
148 *style = SkFontStyle(bold ? SkFontStyle::kBold_Weight : SkFontStyle::kNormal_Weight,
149 SkFontStyle::kNormal_Width,
150 italic ? SkFontStyle::kItalic_Slant : SkFontStyle::kUpright_Slant);
151 name->reset();
152 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000153
bungeman@google.comb3d154d2013-11-11 15:53:29 +0000154 virtual SkTypeface* createTypeface(int index) SK_OVERRIDE {
155 SkASSERT(index < fStyles.count());
156 return SkRef(fStyles[index].get());
157 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000158
bungeman@google.comb3d154d2013-11-11 15:53:29 +0000159 static int match_score(const SkFontStyle& pattern, const SkFontStyle& candidate) {
160 int score = 0;
161 score += (pattern.width() - candidate.width()) * 100;
162 score += (pattern.isItalic() == candidate.isItalic()) ? 0 : 1000;
163 score += pattern.weight() - candidate.weight();
164 return score;
165 }
166
167 virtual SkTypeface* matchStyle(const SkFontStyle& pattern) SK_OVERRIDE {
168 if (0 == fStyles.count()) {
169 return NULL;
170 }
171
172 SkTypeface_Custom* closest = fStyles[0];
173 int minScore = std::numeric_limits<int>::max();
174 for (int i = 0; i < fStyles.count(); ++i) {
175 bool bold = fStyles[i]->isBold();
176 bool italic = fStyles[i]->isItalic();
177 SkFontStyle style = SkFontStyle(bold ? SkFontStyle::kBold_Weight
178 : SkFontStyle::kNormal_Weight,
179 SkFontStyle::kNormal_Width,
180 italic ? SkFontStyle::kItalic_Slant
181 : SkFontStyle::kUpright_Slant);
182
183 int score = match_score(pattern, style);
184 if (score < minScore) {
185 closest = fStyles[i];
186 minScore = score;
187 }
188 }
189 return SkRef(closest);
190 }
191
192private:
193 SkTArray<SkAutoTUnref<SkTypeface_Custom>, true> fStyles;
194 SkString fFamilyName;
195
196 void appendTypeface(SkTypeface_Custom* typeface) {
197 fStyles.push_back().reset(typeface);
198 }
199
200 friend class SkFontMgr_Custom;
201};
202
203/**
204 * SkFontMgr_Custom
205 *
206 * This class is essentially a collection of SkFontStyleSet_Custom,
207 * one SkFontStyleSet_Custom for each family. This class may be modified
208 * to load fonts from any source by changing the initialization.
209 */
210class SkFontMgr_Custom : public SkFontMgr {
211public:
212 explicit SkFontMgr_Custom(const char* dir) {
213 this->load_system_fonts(dir);
214 }
215
216protected:
commit-bot@chromium.org967dee32014-02-04 22:35:01 +0000217 virtual int onCountFamilies() const SK_OVERRIDE {
bungeman@google.comb3d154d2013-11-11 15:53:29 +0000218 return fFamilies.count();
219 }
220
commit-bot@chromium.org967dee32014-02-04 22:35:01 +0000221 virtual void onGetFamilyName(int index, SkString* familyName) const SK_OVERRIDE {
bungeman@google.comb3d154d2013-11-11 15:53:29 +0000222 SkASSERT(index < fFamilies.count());
223 familyName->set(fFamilies[index]->fFamilyName);
224 }
225
commit-bot@chromium.org967dee32014-02-04 22:35:01 +0000226 virtual SkFontStyleSet_Custom* onCreateStyleSet(int index) const SK_OVERRIDE {
bungeman@google.comb3d154d2013-11-11 15:53:29 +0000227 SkASSERT(index < fFamilies.count());
228 return SkRef(fFamilies[index].get());
229 }
230
commit-bot@chromium.org967dee32014-02-04 22:35:01 +0000231 virtual SkFontStyleSet_Custom* onMatchFamily(const char familyName[]) const SK_OVERRIDE {
bungeman@google.comb3d154d2013-11-11 15:53:29 +0000232 for (int i = 0; i < fFamilies.count(); ++i) {
233 if (fFamilies[i]->fFamilyName.equals(familyName)) {
234 return SkRef(fFamilies[i].get());
235 }
236 }
237 return NULL;
238 }
239
240 virtual SkTypeface* onMatchFamilyStyle(const char familyName[],
commit-bot@chromium.org967dee32014-02-04 22:35:01 +0000241 const SkFontStyle& fontStyle) const SK_OVERRIDE
bungeman@google.comb3d154d2013-11-11 15:53:29 +0000242 {
243 SkAutoTUnref<SkFontStyleSet> sset(this->matchFamily(familyName));
244 return sset->matchStyle(fontStyle);
245 }
246
247 virtual SkTypeface* onMatchFaceStyle(const SkTypeface* familyMember,
commit-bot@chromium.org967dee32014-02-04 22:35:01 +0000248 const SkFontStyle& fontStyle) const SK_OVERRIDE
bungeman@google.comb3d154d2013-11-11 15:53:29 +0000249 {
250 for (int i = 0; i < fFamilies.count(); ++i) {
251 for (int j = 0; j < fFamilies[i]->fStyles.count(); ++j) {
252 if (fFamilies[i]->fStyles[j] == familyMember) {
253 return fFamilies[i]->matchStyle(fontStyle);
254 }
255 }
256 }
257 return NULL;
258 }
259
commit-bot@chromium.org967dee32014-02-04 22:35:01 +0000260 virtual SkTypeface* onCreateFromData(SkData* data, int ttcIndex) const SK_OVERRIDE {
bungeman@google.comb3d154d2013-11-11 15:53:29 +0000261 SkAutoTUnref<SkStream> stream(new SkMemoryStream(data));
262 return this->createFromStream(stream, ttcIndex);
263 }
264
commit-bot@chromium.org967dee32014-02-04 22:35:01 +0000265 virtual SkTypeface* onCreateFromStream(SkStream* stream, int ttcIndex) const SK_OVERRIDE {
bungeman@google.comb3d154d2013-11-11 15:53:29 +0000266 if (NULL == stream || stream->getLength() <= 0) {
267 SkDELETE(stream);
268 return NULL;
269 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000270
bungeman@google.comfe747652013-03-25 19:36:11 +0000271 bool isFixedPitch;
bungeman@google.comb3d154d2013-11-11 15:53:29 +0000272 SkTypeface::Style style;
273 SkString name;
bungeman3a21d612014-07-11 08:52:26 -0700274 if (SkTypeface_FreeType::ScanFont(stream, ttcIndex, &name, &style, &isFixedPitch)) {
275 return SkNEW_ARGS(SkTypeface_Stream, (style, isFixedPitch, false, name,
276 stream, ttcIndex));
bungeman@google.comb3d154d2013-11-11 15:53:29 +0000277 } else {
278 return NULL;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000279 }
reed@android.comf244f1b2010-04-16 12:40:08 +0000280 }
281
commit-bot@chromium.org967dee32014-02-04 22:35:01 +0000282 virtual SkTypeface* onCreateFromFile(const char path[], int ttcIndex) const SK_OVERRIDE {
bungeman@google.comb3d154d2013-11-11 15:53:29 +0000283 SkAutoTUnref<SkStream> stream(SkStream::NewFromFile(path));
284 return stream.get() ? this->createFromStream(stream, ttcIndex) : NULL;
285 }
286
287 virtual SkTypeface* onLegacyCreateTypeface(const char familyName[],
commit-bot@chromium.org967dee32014-02-04 22:35:01 +0000288 unsigned styleBits) const SK_OVERRIDE
bungeman@google.comb3d154d2013-11-11 15:53:29 +0000289 {
290 SkTypeface::Style oldStyle = (SkTypeface::Style)styleBits;
291 SkFontStyle style = SkFontStyle(oldStyle & SkTypeface::kBold
292 ? SkFontStyle::kBold_Weight
293 : SkFontStyle::kNormal_Weight,
294 SkFontStyle::kNormal_Width,
295 oldStyle & SkTypeface::kItalic
296 ? SkFontStyle::kItalic_Slant
297 : SkFontStyle::kUpright_Slant);
298 SkTypeface* tf = NULL;
299
bsalomon49f085d2014-09-05 13:34:00 -0700300 if (familyName) {
bungeman@google.comb3d154d2013-11-11 15:53:29 +0000301 tf = this->onMatchFamilyStyle(familyName, style);
bungeman@google.com2cf84ec2012-09-26 19:16:54 +0000302 }
bungeman@google.com2cf84ec2012-09-26 19:16:54 +0000303
bungeman@google.comb3d154d2013-11-11 15:53:29 +0000304 if (NULL == tf) {
305 tf = gDefaultFamily->matchStyle(style);
306 }
307
308 return SkSafeRef(tf);
bungeman@google.com2cf84ec2012-09-26 19:16:54 +0000309 }
310
bungeman@google.comb3d154d2013-11-11 15:53:29 +0000311private:
bungeman@google.com2cf84ec2012-09-26 19:16:54 +0000312
bungeman@google.comb3d154d2013-11-11 15:53:29 +0000313 static bool get_name_and_style(const char path[], SkString* name,
314 SkTypeface::Style* style, bool* isFixedPitch) {
315 SkAutoTUnref<SkStream> stream(SkStream::NewFromFile(path));
316 if (stream.get()) {
bungeman3a21d612014-07-11 08:52:26 -0700317 return SkTypeface_FreeType::ScanFont(stream, 0, name, style, isFixedPitch);
bungeman@google.comb3d154d2013-11-11 15:53:29 +0000318 } else {
319 SkDebugf("---- failed to open <%s> as a font\n", path);
320 return false;
321 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000322 }
reed@android.comf2afb672009-09-28 16:12:48 +0000323
bungeman@google.comb3d154d2013-11-11 15:53:29 +0000324 void load_directory_fonts(const SkString& directory) {
325 SkOSFile::Iter iter(directory.c_str(), ".ttf");
326 SkString name;
327
328 while (iter.next(&name, false)) {
commit-bot@chromium.orgea163cc2014-03-13 16:24:49 +0000329 SkString filename(
tfarinaa8e2e152014-07-28 19:26:58 -0700330 SkOSPath::Join(directory.c_str(), name.c_str()));
bungeman@google.comb3d154d2013-11-11 15:53:29 +0000331
332 bool isFixedPitch;
333 SkString realname;
334 SkTypeface::Style style = SkTypeface::kNormal; // avoid uninitialized warning
335
336 if (!get_name_and_style(filename.c_str(), &realname, &style, &isFixedPitch)) {
337 SkDebugf("------ can't load <%s> as a font\n", filename.c_str());
338 continue;
339 }
340
341 SkTypeface_Custom* tf = SkNEW_ARGS(SkTypeface_File, (
342 style,
bungeman@google.comb3d154d2013-11-11 15:53:29 +0000343 isFixedPitch,
bungeman3a21d612014-07-11 08:52:26 -0700344 true, // system-font (cannot delete)
345 realname,
346 filename.c_str()));
bungeman@google.comb3d154d2013-11-11 15:53:29 +0000347
348 SkFontStyleSet_Custom* addTo = this->onMatchFamily(realname.c_str());
349 if (NULL == addTo) {
350 addTo = new SkFontStyleSet_Custom(realname);
351 fFamilies.push_back().reset(addTo);
352 }
353 addTo->appendTypeface(tf);
354 }
355
356 SkOSFile::Iter dirIter(directory.c_str());
357 while (dirIter.next(&name, true)) {
358 if (name.startsWith(".")) {
359 continue;
360 }
tfarinaa8e2e152014-07-28 19:26:58 -0700361 SkString dirname(SkOSPath::Join(directory.c_str(), name.c_str()));
bungeman@google.comb3d154d2013-11-11 15:53:29 +0000362 load_directory_fonts(dirname);
363 }
364 }
365
366 void load_system_fonts(const char* dir) {
367 SkString baseDirectory(dir);
368 load_directory_fonts(baseDirectory);
369
370 if (fFamilies.empty()) {
371 SkFontStyleSet_Custom* family = new SkFontStyleSet_Custom(SkString());
372 fFamilies.push_back().reset(family);
373 family->appendTypeface(SkNEW(SkTypeface_Empty));
374 }
375
376 // Try to pick a default font.
377 static const char* gDefaultNames[] = {
commit-bot@chromium.orgea163cc2014-03-13 16:24:49 +0000378 "Arial", "Verdana", "Times New Roman", "Droid Sans", NULL
bungeman@google.comb3d154d2013-11-11 15:53:29 +0000379 };
380 for (size_t i = 0; i < SK_ARRAY_COUNT(gDefaultNames); ++i) {
381 SkFontStyleSet_Custom* set = this->onMatchFamily(gDefaultNames[i]);
382 if (NULL == set) {
383 continue;
384 }
385
386 SkTypeface* tf = set->matchStyle(SkFontStyle(SkFontStyle::kNormal_Weight,
387 SkFontStyle::kNormal_Width,
388 SkFontStyle::kUpright_Slant));
389 if (NULL == tf) {
390 continue;
391 }
392
393 gDefaultFamily = set;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000394 gDefaultNormal = tf;
395 break;
396 }
bungeman@google.comb3d154d2013-11-11 15:53:29 +0000397 if (NULL == gDefaultNormal) {
398 gDefaultFamily = fFamilies[0];
399 gDefaultNormal = gDefaultFamily->fStyles[0];
reed@android.com8a1c16f2008-12-17 15:59:43 +0000400 }
401 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000402
bungeman@google.comb3d154d2013-11-11 15:53:29 +0000403 SkTArray<SkAutoTUnref<SkFontStyleSet_Custom>, true> fFamilies;
404 SkFontStyleSet_Custom* gDefaultFamily;
405 SkTypeface* gDefaultNormal;
406};
reed@google.com070da5e2013-03-27 20:01:49 +0000407
408SkFontMgr* SkFontMgr::Factory() {
bungeman@google.comb3d154d2013-11-11 15:53:29 +0000409 return new SkFontMgr_Custom(SK_FONT_FILE_PREFIX);
reed@google.com070da5e2013-03-27 20:01:49 +0000410}