blob: 891e4617f3e6f6e2ec03979d330afe47ea2b1b9c [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:
bungeman@google.comb3d154d2013-11-11 15:53:29 +000043 virtual void onGetFontDescriptor(SkFontDescriptor* desc, bool* isLocal) const SK_OVERRIDE {
44 desc->setFamilyName(fFamilyName.c_str());
45 desc->setFontFileName(this->getUniqueString());
46 *isLocal = !this->isSysFont();
47 }
reed@google.com5526ede2013-03-25 13:03:37 +000048
reed@android.com8a1c16f2008-12-17 15:59:43 +000049private:
bungeman@google.comb3d154d2013-11-11 15:53:29 +000050 bool fIsSysFont;
51 SkString fFamilyName;
chudy@google.comada44802012-07-30 12:59:12 +000052
reed@google.com032fbb82013-03-21 13:38:18 +000053 typedef SkTypeface_FreeType INHERITED;
reed@android.com8a1c16f2008-12-17 15:59:43 +000054};
55
bungeman@google.comb3d154d2013-11-11 15:53:29 +000056/** The empty SkTypeface implementation for the custom font manager.
57 * Used as the last resort fallback typeface.
reed@android.comf244f1b2010-04-16 12:40:08 +000058 */
bungeman@google.comb3d154d2013-11-11 15:53:29 +000059class SkTypeface_Empty : public SkTypeface_Custom {
reed@android.comf244f1b2010-04-16 12:40:08 +000060public:
bungeman3a21d612014-07-11 08:52:26 -070061 SkTypeface_Empty() : INHERITED(SkTypeface::kNormal, false, true, SkString()) {}
chudy@google.comada44802012-07-30 12:59:12 +000062
bungeman@google.comb3d154d2013-11-11 15:53:29 +000063 virtual const char* getUniqueString() const SK_OVERRIDE { return NULL; }
chudy@google.comada44802012-07-30 12:59:12 +000064
reed@google.com292b1d42013-03-22 17:21:59 +000065protected:
66 virtual SkStream* onOpenStream(int*) const SK_OVERRIDE { return NULL; }
67
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:
bungeman3a21d612014-07-11 08:52:26 -070075 SkTypeface_Stream(Style style, bool isFixedPitch, bool sysFont, const SkString familyName,
76 SkStream* stream, int ttcIndex)
77 : INHERITED(style, isFixedPitch, sysFont, familyName)
78 , fStream(SkRef(stream)), fTtcIndex(ttcIndex)
bungeman@google.comb3d154d2013-11-11 15:53:29 +000079 { }
chudy@google.comada44802012-07-30 12:59:12 +000080
reed@google.come1575aa2013-03-18 21:08:46 +000081 virtual const char* getUniqueString() const SK_OVERRIDE { return NULL; }
chudy@google.comada44802012-07-30 12:59:12 +000082
reed@google.com292b1d42013-03-22 17:21:59 +000083protected:
84 virtual SkStream* onOpenStream(int* ttcIndex) const SK_OVERRIDE {
85 *ttcIndex = 0;
commit-bot@chromium.orga2b44dc2014-03-24 21:42:01 +000086 return fStream->duplicate();
reed@google.com292b1d42013-03-22 17:21:59 +000087 }
88
reed@android.com8a1c16f2008-12-17 15:59:43 +000089private:
bungeman@google.comb3d154d2013-11-11 15:53:29 +000090 SkAutoTUnref<SkStream> fStream;
bungeman3a21d612014-07-11 08:52:26 -070091 int fTtcIndex;
chudy@google.comada44802012-07-30 12:59:12 +000092
bungeman@google.comb3d154d2013-11-11 15:53:29 +000093 typedef SkTypeface_Custom INHERITED;
reed@android.com8a1c16f2008-12-17 15:59:43 +000094};
95
bungeman@google.comb3d154d2013-11-11 15:53:29 +000096/** The file SkTypeface implementation for the custom font manager. */
97class SkTypeface_File : public SkTypeface_Custom {
reed@android.com8a1c16f2008-12-17 15:59:43 +000098public:
bungeman3a21d612014-07-11 08:52:26 -070099 SkTypeface_File(Style style, bool isFixedPitch, bool sysFont, const SkString familyName,
100 const char path[])
101 : INHERITED(style, isFixedPitch, sysFont, familyName)
bungeman@google.comb3d154d2013-11-11 15:53:29 +0000102 , fPath(path)
103 { }
chudy@google.comada44802012-07-30 12:59:12 +0000104
reed@google.come1575aa2013-03-18 21:08:46 +0000105 virtual const char* getUniqueString() const SK_OVERRIDE {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000106 const char* str = strrchr(fPath.c_str(), '/');
107 if (str) {
108 str += 1; // skip the '/'
109 }
110 return str;
111 }
chudy@google.comada44802012-07-30 12:59:12 +0000112
reed@google.com292b1d42013-03-22 17:21:59 +0000113protected:
114 virtual SkStream* onOpenStream(int* ttcIndex) const SK_OVERRIDE {
115 *ttcIndex = 0;
116 return SkStream::NewFromFile(fPath.c_str());
117 }
118
reed@android.com8a1c16f2008-12-17 15:59:43 +0000119private:
120 SkString fPath;
chudy@google.comada44802012-07-30 12:59:12 +0000121
bungeman@google.comb3d154d2013-11-11 15:53:29 +0000122 typedef SkTypeface_Custom INHERITED;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000123};
124
125///////////////////////////////////////////////////////////////////////////////
reed@android.com8a1c16f2008-12-17 15:59:43 +0000126
bungeman@google.comb3d154d2013-11-11 15:53:29 +0000127/**
128 * SkFontStyleSet_Custom
129 *
130 * This class is used by SkFontMgr_Custom to hold SkTypeface_Custom families.
131 */
132class SkFontStyleSet_Custom : public SkFontStyleSet {
133public:
134 explicit SkFontStyleSet_Custom(const SkString familyName) : fFamilyName(familyName) { }
135
136 virtual int count() SK_OVERRIDE {
137 return fStyles.count();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000138 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000139
bungeman@google.comb3d154d2013-11-11 15:53:29 +0000140 virtual void getStyle(int index, SkFontStyle* style, SkString* name) SK_OVERRIDE {
141 SkASSERT(index < fStyles.count());
142 bool bold = fStyles[index]->isBold();
143 bool italic = fStyles[index]->isItalic();
144 *style = SkFontStyle(bold ? SkFontStyle::kBold_Weight : SkFontStyle::kNormal_Weight,
145 SkFontStyle::kNormal_Width,
146 italic ? SkFontStyle::kItalic_Slant : SkFontStyle::kUpright_Slant);
147 name->reset();
148 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000149
bungeman@google.comb3d154d2013-11-11 15:53:29 +0000150 virtual SkTypeface* createTypeface(int index) SK_OVERRIDE {
151 SkASSERT(index < fStyles.count());
152 return SkRef(fStyles[index].get());
153 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000154
bungeman@google.comb3d154d2013-11-11 15:53:29 +0000155 static int match_score(const SkFontStyle& pattern, const SkFontStyle& candidate) {
156 int score = 0;
157 score += (pattern.width() - candidate.width()) * 100;
158 score += (pattern.isItalic() == candidate.isItalic()) ? 0 : 1000;
159 score += pattern.weight() - candidate.weight();
160 return score;
161 }
162
163 virtual SkTypeface* matchStyle(const SkFontStyle& pattern) SK_OVERRIDE {
164 if (0 == fStyles.count()) {
165 return NULL;
166 }
167
168 SkTypeface_Custom* closest = fStyles[0];
169 int minScore = std::numeric_limits<int>::max();
170 for (int i = 0; i < fStyles.count(); ++i) {
171 bool bold = fStyles[i]->isBold();
172 bool italic = fStyles[i]->isItalic();
173 SkFontStyle style = SkFontStyle(bold ? SkFontStyle::kBold_Weight
174 : SkFontStyle::kNormal_Weight,
175 SkFontStyle::kNormal_Width,
176 italic ? SkFontStyle::kItalic_Slant
177 : SkFontStyle::kUpright_Slant);
178
179 int score = match_score(pattern, style);
180 if (score < minScore) {
181 closest = fStyles[i];
182 minScore = score;
183 }
184 }
185 return SkRef(closest);
186 }
187
188private:
189 SkTArray<SkAutoTUnref<SkTypeface_Custom>, true> fStyles;
190 SkString fFamilyName;
191
192 void appendTypeface(SkTypeface_Custom* typeface) {
193 fStyles.push_back().reset(typeface);
194 }
195
196 friend class SkFontMgr_Custom;
197};
198
199/**
200 * SkFontMgr_Custom
201 *
202 * This class is essentially a collection of SkFontStyleSet_Custom,
203 * one SkFontStyleSet_Custom for each family. This class may be modified
204 * to load fonts from any source by changing the initialization.
205 */
206class SkFontMgr_Custom : public SkFontMgr {
207public:
208 explicit SkFontMgr_Custom(const char* dir) {
209 this->load_system_fonts(dir);
210 }
211
212protected:
commit-bot@chromium.org967dee32014-02-04 22:35:01 +0000213 virtual int onCountFamilies() const SK_OVERRIDE {
bungeman@google.comb3d154d2013-11-11 15:53:29 +0000214 return fFamilies.count();
215 }
216
commit-bot@chromium.org967dee32014-02-04 22:35:01 +0000217 virtual void onGetFamilyName(int index, SkString* familyName) const SK_OVERRIDE {
bungeman@google.comb3d154d2013-11-11 15:53:29 +0000218 SkASSERT(index < fFamilies.count());
219 familyName->set(fFamilies[index]->fFamilyName);
220 }
221
commit-bot@chromium.org967dee32014-02-04 22:35:01 +0000222 virtual SkFontStyleSet_Custom* onCreateStyleSet(int index) const SK_OVERRIDE {
bungeman@google.comb3d154d2013-11-11 15:53:29 +0000223 SkASSERT(index < fFamilies.count());
224 return SkRef(fFamilies[index].get());
225 }
226
commit-bot@chromium.org967dee32014-02-04 22:35:01 +0000227 virtual SkFontStyleSet_Custom* onMatchFamily(const char familyName[]) const SK_OVERRIDE {
bungeman@google.comb3d154d2013-11-11 15:53:29 +0000228 for (int i = 0; i < fFamilies.count(); ++i) {
229 if (fFamilies[i]->fFamilyName.equals(familyName)) {
230 return SkRef(fFamilies[i].get());
231 }
232 }
233 return NULL;
234 }
235
236 virtual SkTypeface* onMatchFamilyStyle(const char familyName[],
commit-bot@chromium.org967dee32014-02-04 22:35:01 +0000237 const SkFontStyle& fontStyle) const SK_OVERRIDE
bungeman@google.comb3d154d2013-11-11 15:53:29 +0000238 {
239 SkAutoTUnref<SkFontStyleSet> sset(this->matchFamily(familyName));
240 return sset->matchStyle(fontStyle);
241 }
242
243 virtual SkTypeface* onMatchFaceStyle(const SkTypeface* familyMember,
commit-bot@chromium.org967dee32014-02-04 22:35:01 +0000244 const SkFontStyle& fontStyle) const SK_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 }
253 return NULL;
254 }
255
commit-bot@chromium.org967dee32014-02-04 22:35:01 +0000256 virtual SkTypeface* onCreateFromData(SkData* data, int ttcIndex) const SK_OVERRIDE {
bungeman@google.comb3d154d2013-11-11 15:53:29 +0000257 SkAutoTUnref<SkStream> stream(new SkMemoryStream(data));
258 return this->createFromStream(stream, ttcIndex);
259 }
260
commit-bot@chromium.org967dee32014-02-04 22:35:01 +0000261 virtual SkTypeface* onCreateFromStream(SkStream* stream, int ttcIndex) const SK_OVERRIDE {
bungeman@google.comb3d154d2013-11-11 15:53:29 +0000262 if (NULL == stream || stream->getLength() <= 0) {
263 SkDELETE(stream);
264 return NULL;
265 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000266
bungeman@google.comfe747652013-03-25 19:36:11 +0000267 bool isFixedPitch;
bungeman@google.comb3d154d2013-11-11 15:53:29 +0000268 SkTypeface::Style style;
269 SkString name;
bungeman3a21d612014-07-11 08:52:26 -0700270 if (SkTypeface_FreeType::ScanFont(stream, ttcIndex, &name, &style, &isFixedPitch)) {
271 return SkNEW_ARGS(SkTypeface_Stream, (style, isFixedPitch, false, name,
272 stream, ttcIndex));
bungeman@google.comb3d154d2013-11-11 15:53:29 +0000273 } else {
274 return NULL;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000275 }
reed@android.comf244f1b2010-04-16 12:40:08 +0000276 }
277
commit-bot@chromium.org967dee32014-02-04 22:35:01 +0000278 virtual SkTypeface* onCreateFromFile(const char path[], int ttcIndex) const SK_OVERRIDE {
bungeman@google.comb3d154d2013-11-11 15:53:29 +0000279 SkAutoTUnref<SkStream> stream(SkStream::NewFromFile(path));
280 return stream.get() ? this->createFromStream(stream, ttcIndex) : NULL;
281 }
282
283 virtual SkTypeface* onLegacyCreateTypeface(const char familyName[],
commit-bot@chromium.org967dee32014-02-04 22:35:01 +0000284 unsigned styleBits) const SK_OVERRIDE
bungeman@google.comb3d154d2013-11-11 15:53:29 +0000285 {
286 SkTypeface::Style oldStyle = (SkTypeface::Style)styleBits;
287 SkFontStyle style = SkFontStyle(oldStyle & SkTypeface::kBold
288 ? SkFontStyle::kBold_Weight
289 : SkFontStyle::kNormal_Weight,
290 SkFontStyle::kNormal_Width,
291 oldStyle & SkTypeface::kItalic
292 ? SkFontStyle::kItalic_Slant
293 : SkFontStyle::kUpright_Slant);
294 SkTypeface* tf = NULL;
295
296 if (NULL != familyName) {
297 tf = this->onMatchFamilyStyle(familyName, style);
bungeman@google.com2cf84ec2012-09-26 19:16:54 +0000298 }
bungeman@google.com2cf84ec2012-09-26 19:16:54 +0000299
bungeman@google.comb3d154d2013-11-11 15:53:29 +0000300 if (NULL == tf) {
301 tf = gDefaultFamily->matchStyle(style);
302 }
303
304 return SkSafeRef(tf);
bungeman@google.com2cf84ec2012-09-26 19:16:54 +0000305 }
306
bungeman@google.comb3d154d2013-11-11 15:53:29 +0000307private:
bungeman@google.com2cf84ec2012-09-26 19:16:54 +0000308
bungeman@google.comb3d154d2013-11-11 15:53:29 +0000309 static bool get_name_and_style(const char path[], SkString* name,
310 SkTypeface::Style* style, bool* isFixedPitch) {
311 SkAutoTUnref<SkStream> stream(SkStream::NewFromFile(path));
312 if (stream.get()) {
bungeman3a21d612014-07-11 08:52:26 -0700313 return SkTypeface_FreeType::ScanFont(stream, 0, name, style, isFixedPitch);
bungeman@google.comb3d154d2013-11-11 15:53:29 +0000314 } else {
315 SkDebugf("---- failed to open <%s> as a font\n", path);
316 return false;
317 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000318 }
reed@android.comf2afb672009-09-28 16:12:48 +0000319
bungeman@google.comb3d154d2013-11-11 15:53:29 +0000320 void load_directory_fonts(const SkString& directory) {
321 SkOSFile::Iter iter(directory.c_str(), ".ttf");
322 SkString name;
323
324 while (iter.next(&name, false)) {
commit-bot@chromium.orgea163cc2014-03-13 16:24:49 +0000325 SkString filename(
tfarinaa8e2e152014-07-28 19:26:58 -0700326 SkOSPath::Join(directory.c_str(), name.c_str()));
bungeman@google.comb3d154d2013-11-11 15:53:29 +0000327
328 bool isFixedPitch;
329 SkString realname;
330 SkTypeface::Style style = SkTypeface::kNormal; // avoid uninitialized warning
331
332 if (!get_name_and_style(filename.c_str(), &realname, &style, &isFixedPitch)) {
333 SkDebugf("------ can't load <%s> as a font\n", filename.c_str());
334 continue;
335 }
336
337 SkTypeface_Custom* tf = SkNEW_ARGS(SkTypeface_File, (
338 style,
bungeman@google.comb3d154d2013-11-11 15:53:29 +0000339 isFixedPitch,
bungeman3a21d612014-07-11 08:52:26 -0700340 true, // system-font (cannot delete)
341 realname,
342 filename.c_str()));
bungeman@google.comb3d154d2013-11-11 15:53:29 +0000343
344 SkFontStyleSet_Custom* addTo = this->onMatchFamily(realname.c_str());
345 if (NULL == addTo) {
346 addTo = new SkFontStyleSet_Custom(realname);
347 fFamilies.push_back().reset(addTo);
348 }
349 addTo->appendTypeface(tf);
350 }
351
352 SkOSFile::Iter dirIter(directory.c_str());
353 while (dirIter.next(&name, true)) {
354 if (name.startsWith(".")) {
355 continue;
356 }
tfarinaa8e2e152014-07-28 19:26:58 -0700357 SkString dirname(SkOSPath::Join(directory.c_str(), name.c_str()));
bungeman@google.comb3d154d2013-11-11 15:53:29 +0000358 load_directory_fonts(dirname);
359 }
360 }
361
362 void load_system_fonts(const char* dir) {
363 SkString baseDirectory(dir);
364 load_directory_fonts(baseDirectory);
365
366 if (fFamilies.empty()) {
367 SkFontStyleSet_Custom* family = new SkFontStyleSet_Custom(SkString());
368 fFamilies.push_back().reset(family);
369 family->appendTypeface(SkNEW(SkTypeface_Empty));
370 }
371
372 // Try to pick a default font.
373 static const char* gDefaultNames[] = {
commit-bot@chromium.orgea163cc2014-03-13 16:24:49 +0000374 "Arial", "Verdana", "Times New Roman", "Droid Sans", NULL
bungeman@google.comb3d154d2013-11-11 15:53:29 +0000375 };
376 for (size_t i = 0; i < SK_ARRAY_COUNT(gDefaultNames); ++i) {
377 SkFontStyleSet_Custom* set = this->onMatchFamily(gDefaultNames[i]);
378 if (NULL == set) {
379 continue;
380 }
381
382 SkTypeface* tf = set->matchStyle(SkFontStyle(SkFontStyle::kNormal_Weight,
383 SkFontStyle::kNormal_Width,
384 SkFontStyle::kUpright_Slant));
385 if (NULL == tf) {
386 continue;
387 }
388
389 gDefaultFamily = set;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000390 gDefaultNormal = tf;
391 break;
392 }
bungeman@google.comb3d154d2013-11-11 15:53:29 +0000393 if (NULL == gDefaultNormal) {
394 gDefaultFamily = fFamilies[0];
395 gDefaultNormal = gDefaultFamily->fStyles[0];
reed@android.com8a1c16f2008-12-17 15:59:43 +0000396 }
397 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000398
bungeman@google.comb3d154d2013-11-11 15:53:29 +0000399 SkTArray<SkAutoTUnref<SkFontStyleSet_Custom>, true> fFamilies;
400 SkFontStyleSet_Custom* gDefaultFamily;
401 SkTypeface* gDefaultNormal;
402};
reed@google.com070da5e2013-03-27 20:01:49 +0000403
404SkFontMgr* SkFontMgr::Factory() {
bungeman@google.comb3d154d2013-11-11 15:53:29 +0000405 return new SkFontMgr_Custom(SK_FONT_FILE_PREFIX);
reed@google.com070da5e2013-03-27 20:01:49 +0000406}