blob: 9eabac31c6a06c3beb4de898905abcd6bf11abe9 [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
27#ifndef SK_FONT_FILE_DIR_SEPERATOR
bungeman@google.comb3d154d2013-11-11 15:53:29 +000028# define SK_FONT_FILE_DIR_SEPERATOR "/"
reed@android.com8a1c16f2008-12-17 15:59:43 +000029#endif
30
djsollen@google.com4dc686d2012-02-15 21:03:45 +000031bool find_name_and_attributes(SkStream* stream, SkString* name,
bungeman@google.comfe747652013-03-25 19:36:11 +000032 SkTypeface::Style* style, bool* isFixedPitch);
reed@android.com8a1c16f2008-12-17 15:59:43 +000033
reed@android.com8a1c16f2008-12-17 15:59:43 +000034///////////////////////////////////////////////////////////////////////////////
35
bungeman@google.comb3d154d2013-11-11 15:53:29 +000036/** The base SkTypeface implementation for the custom font manager. */
37class SkTypeface_Custom : public SkTypeface_FreeType {
reed@android.com8a1c16f2008-12-17 15:59:43 +000038public:
bungeman@google.comb3d154d2013-11-11 15:53:29 +000039 SkTypeface_Custom(Style style, bool sysFont, bool isFixedPitch, const SkString familyName)
40 : INHERITED(style, SkTypefaceCache::NewFontID(), isFixedPitch)
41 , fIsSysFont(sysFont), fFamilyName(familyName)
42 { }
chudy@google.comada44802012-07-30 12:59:12 +000043
reed@android.com8a1c16f2008-12-17 15:59:43 +000044 bool isSysFont() const { return fIsSysFont; }
reed@google.com292b1d42013-03-22 17:21:59 +000045
reed@android.com8a1c16f2008-12-17 15:59:43 +000046 virtual const char* getUniqueString() const = 0;
chudy@google.comada44802012-07-30 12:59:12 +000047
reed@google.com5526ede2013-03-25 13:03:37 +000048protected:
bungeman@google.comb3d154d2013-11-11 15:53:29 +000049 virtual void onGetFontDescriptor(SkFontDescriptor* desc, bool* isLocal) const SK_OVERRIDE {
50 desc->setFamilyName(fFamilyName.c_str());
51 desc->setFontFileName(this->getUniqueString());
52 *isLocal = !this->isSysFont();
53 }
reed@google.com5526ede2013-03-25 13:03:37 +000054
reed@android.com8a1c16f2008-12-17 15:59:43 +000055private:
bungeman@google.comb3d154d2013-11-11 15:53:29 +000056 bool fIsSysFont;
57 SkString fFamilyName;
chudy@google.comada44802012-07-30 12:59:12 +000058
reed@google.com032fbb82013-03-21 13:38:18 +000059 typedef SkTypeface_FreeType INHERITED;
reed@android.com8a1c16f2008-12-17 15:59:43 +000060};
61
bungeman@google.comb3d154d2013-11-11 15:53:29 +000062/** The empty SkTypeface implementation for the custom font manager.
63 * Used as the last resort fallback typeface.
reed@android.comf244f1b2010-04-16 12:40:08 +000064 */
bungeman@google.comb3d154d2013-11-11 15:53:29 +000065class SkTypeface_Empty : public SkTypeface_Custom {
reed@android.comf244f1b2010-04-16 12:40:08 +000066public:
bungeman@google.comb3d154d2013-11-11 15:53:29 +000067 SkTypeface_Empty() : INHERITED(SkTypeface::kNormal, true, false, SkString()) {}
chudy@google.comada44802012-07-30 12:59:12 +000068
bungeman@google.comb3d154d2013-11-11 15:53:29 +000069 virtual const char* getUniqueString() const SK_OVERRIDE { return NULL; }
chudy@google.comada44802012-07-30 12:59:12 +000070
reed@google.com292b1d42013-03-22 17:21:59 +000071protected:
72 virtual SkStream* onOpenStream(int*) const SK_OVERRIDE { return NULL; }
73
reed@android.comf244f1b2010-04-16 12:40:08 +000074private:
bungeman@google.comb3d154d2013-11-11 15:53:29 +000075 typedef SkTypeface_Custom INHERITED;
reed@android.comf244f1b2010-04-16 12:40:08 +000076};
77
bungeman@google.comb3d154d2013-11-11 15:53:29 +000078/** The stream SkTypeface implementation for the custom font manager. */
79class SkTypeface_Stream : public SkTypeface_Custom {
reed@android.com8a1c16f2008-12-17 15:59:43 +000080public:
bungeman@google.comb3d154d2013-11-11 15:53:29 +000081 SkTypeface_Stream(Style style, bool sysFont, SkStream* stream,
82 bool isFixedPitch, const SkString familyName)
83 : INHERITED(style, sysFont, isFixedPitch, familyName)
84 , fStream(SkRef(stream))
85 { }
chudy@google.comada44802012-07-30 12:59:12 +000086
reed@google.come1575aa2013-03-18 21:08:46 +000087 virtual const char* getUniqueString() const SK_OVERRIDE { return NULL; }
chudy@google.comada44802012-07-30 12:59:12 +000088
reed@google.com292b1d42013-03-22 17:21:59 +000089protected:
90 virtual SkStream* onOpenStream(int* ttcIndex) const SK_OVERRIDE {
91 *ttcIndex = 0;
bungeman@google.comb3d154d2013-11-11 15:53:29 +000092 return SkRef(fStream.get());
reed@google.com292b1d42013-03-22 17:21:59 +000093 }
94
reed@android.com8a1c16f2008-12-17 15:59:43 +000095private:
bungeman@google.comb3d154d2013-11-11 15:53:29 +000096 SkAutoTUnref<SkStream> fStream;
chudy@google.comada44802012-07-30 12:59:12 +000097
bungeman@google.comb3d154d2013-11-11 15:53:29 +000098 typedef SkTypeface_Custom INHERITED;
reed@android.com8a1c16f2008-12-17 15:59:43 +000099};
100
bungeman@google.comb3d154d2013-11-11 15:53:29 +0000101/** The file SkTypeface implementation for the custom font manager. */
102class SkTypeface_File : public SkTypeface_Custom {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000103public:
bungeman@google.comb3d154d2013-11-11 15:53:29 +0000104 SkTypeface_File(Style style, bool sysFont, const char path[],
105 bool isFixedPitch, const SkString familyName)
106 : INHERITED(style, sysFont, isFixedPitch, familyName)
107 , fPath(path)
108 { }
chudy@google.comada44802012-07-30 12:59:12 +0000109
reed@google.come1575aa2013-03-18 21:08:46 +0000110 virtual const char* getUniqueString() const SK_OVERRIDE {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000111 const char* str = strrchr(fPath.c_str(), '/');
112 if (str) {
113 str += 1; // skip the '/'
114 }
115 return str;
116 }
chudy@google.comada44802012-07-30 12:59:12 +0000117
reed@google.com292b1d42013-03-22 17:21:59 +0000118protected:
119 virtual SkStream* onOpenStream(int* ttcIndex) const SK_OVERRIDE {
120 *ttcIndex = 0;
121 return SkStream::NewFromFile(fPath.c_str());
122 }
123
reed@android.com8a1c16f2008-12-17 15:59:43 +0000124private:
125 SkString fPath;
chudy@google.comada44802012-07-30 12:59:12 +0000126
bungeman@google.comb3d154d2013-11-11 15:53:29 +0000127 typedef SkTypeface_Custom INHERITED;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000128};
129
130///////////////////////////////////////////////////////////////////////////////
reed@android.com8a1c16f2008-12-17 15:59:43 +0000131
bungeman@google.comb3d154d2013-11-11 15:53:29 +0000132/**
133 * SkFontStyleSet_Custom
134 *
135 * This class is used by SkFontMgr_Custom to hold SkTypeface_Custom families.
136 */
137class SkFontStyleSet_Custom : public SkFontStyleSet {
138public:
139 explicit SkFontStyleSet_Custom(const SkString familyName) : fFamilyName(familyName) { }
140
141 virtual int count() SK_OVERRIDE {
142 return fStyles.count();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000143 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000144
bungeman@google.comb3d154d2013-11-11 15:53:29 +0000145 virtual void getStyle(int index, SkFontStyle* style, SkString* name) SK_OVERRIDE {
146 SkASSERT(index < fStyles.count());
147 bool bold = fStyles[index]->isBold();
148 bool italic = fStyles[index]->isItalic();
149 *style = SkFontStyle(bold ? SkFontStyle::kBold_Weight : SkFontStyle::kNormal_Weight,
150 SkFontStyle::kNormal_Width,
151 italic ? SkFontStyle::kItalic_Slant : SkFontStyle::kUpright_Slant);
152 name->reset();
153 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000154
bungeman@google.comb3d154d2013-11-11 15:53:29 +0000155 virtual SkTypeface* createTypeface(int index) SK_OVERRIDE {
156 SkASSERT(index < fStyles.count());
157 return SkRef(fStyles[index].get());
158 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000159
bungeman@google.comb3d154d2013-11-11 15:53:29 +0000160 static int match_score(const SkFontStyle& pattern, const SkFontStyle& candidate) {
161 int score = 0;
162 score += (pattern.width() - candidate.width()) * 100;
163 score += (pattern.isItalic() == candidate.isItalic()) ? 0 : 1000;
164 score += pattern.weight() - candidate.weight();
165 return score;
166 }
167
168 virtual SkTypeface* matchStyle(const SkFontStyle& pattern) SK_OVERRIDE {
169 if (0 == fStyles.count()) {
170 return NULL;
171 }
172
173 SkTypeface_Custom* closest = fStyles[0];
174 int minScore = std::numeric_limits<int>::max();
175 for (int i = 0; i < fStyles.count(); ++i) {
176 bool bold = fStyles[i]->isBold();
177 bool italic = fStyles[i]->isItalic();
178 SkFontStyle style = SkFontStyle(bold ? SkFontStyle::kBold_Weight
179 : SkFontStyle::kNormal_Weight,
180 SkFontStyle::kNormal_Width,
181 italic ? SkFontStyle::kItalic_Slant
182 : SkFontStyle::kUpright_Slant);
183
184 int score = match_score(pattern, style);
185 if (score < minScore) {
186 closest = fStyles[i];
187 minScore = score;
188 }
189 }
190 return SkRef(closest);
191 }
192
193private:
194 SkTArray<SkAutoTUnref<SkTypeface_Custom>, true> fStyles;
195 SkString fFamilyName;
196
197 void appendTypeface(SkTypeface_Custom* typeface) {
198 fStyles.push_back().reset(typeface);
199 }
200
201 friend class SkFontMgr_Custom;
202};
203
204/**
205 * SkFontMgr_Custom
206 *
207 * This class is essentially a collection of SkFontStyleSet_Custom,
208 * one SkFontStyleSet_Custom for each family. This class may be modified
209 * to load fonts from any source by changing the initialization.
210 */
211class SkFontMgr_Custom : public SkFontMgr {
212public:
213 explicit SkFontMgr_Custom(const char* dir) {
214 this->load_system_fonts(dir);
215 }
216
217protected:
218 virtual int onCountFamilies() SK_OVERRIDE {
219 return fFamilies.count();
220 }
221
222 virtual void onGetFamilyName(int index, SkString* familyName) SK_OVERRIDE {
223 SkASSERT(index < fFamilies.count());
224 familyName->set(fFamilies[index]->fFamilyName);
225 }
226
227 virtual SkFontStyleSet_Custom* onCreateStyleSet(int index) SK_OVERRIDE {
228 SkASSERT(index < fFamilies.count());
229 return SkRef(fFamilies[index].get());
230 }
231
232 virtual SkFontStyleSet_Custom* onMatchFamily(const char familyName[]) SK_OVERRIDE {
233 for (int i = 0; i < fFamilies.count(); ++i) {
234 if (fFamilies[i]->fFamilyName.equals(familyName)) {
235 return SkRef(fFamilies[i].get());
236 }
237 }
238 return NULL;
239 }
240
241 virtual SkTypeface* onMatchFamilyStyle(const char familyName[],
242 const SkFontStyle& fontStyle) SK_OVERRIDE
243 {
244 SkAutoTUnref<SkFontStyleSet> sset(this->matchFamily(familyName));
245 return sset->matchStyle(fontStyle);
246 }
247
248 virtual SkTypeface* onMatchFaceStyle(const SkTypeface* familyMember,
249 const SkFontStyle& fontStyle) SK_OVERRIDE
250 {
251 for (int i = 0; i < fFamilies.count(); ++i) {
252 for (int j = 0; j < fFamilies[i]->fStyles.count(); ++j) {
253 if (fFamilies[i]->fStyles[j] == familyMember) {
254 return fFamilies[i]->matchStyle(fontStyle);
255 }
256 }
257 }
258 return NULL;
259 }
260
261 virtual SkTypeface* onCreateFromData(SkData* data, int ttcIndex) SK_OVERRIDE {
262 SkAutoTUnref<SkStream> stream(new SkMemoryStream(data));
263 return this->createFromStream(stream, ttcIndex);
264 }
265
266 virtual SkTypeface* onCreateFromStream(SkStream* stream, int ttcIndex) SK_OVERRIDE {
267 if (NULL == stream || stream->getLength() <= 0) {
268 SkDELETE(stream);
269 return NULL;
270 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000271
bungeman@google.comfe747652013-03-25 19:36:11 +0000272 bool isFixedPitch;
bungeman@google.comb3d154d2013-11-11 15:53:29 +0000273 SkTypeface::Style style;
274 SkString name;
275 if (find_name_and_attributes(stream, &name, &style, &isFixedPitch)) {
276 return SkNEW_ARGS(SkTypeface_Stream, (style, false, stream, isFixedPitch, name));
277 } else {
278 return NULL;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000279 }
reed@android.comf244f1b2010-04-16 12:40:08 +0000280 }
281
bungeman@google.comb3d154d2013-11-11 15:53:29 +0000282 virtual SkTypeface* onCreateFromFile(const char path[], int ttcIndex) SK_OVERRIDE {
283 SkAutoTUnref<SkStream> stream(SkStream::NewFromFile(path));
284 return stream.get() ? this->createFromStream(stream, ttcIndex) : NULL;
285 }
286
287 virtual SkTypeface* onLegacyCreateTypeface(const char familyName[],
288 unsigned styleBits) SK_OVERRIDE
289 {
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
300 if (NULL != familyName) {
301 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()) {
317 return find_name_and_attributes(stream, name, style, isFixedPitch);
318 } 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)) {
329 SkString filename(directory);
330 filename.append(name);
331
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,
343 true, // system-font (cannot delete)
344 filename.c_str(),
345 isFixedPitch,
346 realname));
347
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 }
361 SkString dirname(directory);
362 dirname.append(name);
363 dirname.append(SK_FONT_FILE_DIR_SEPERATOR);
364 load_directory_fonts(dirname);
365 }
366 }
367
368 void load_system_fonts(const char* dir) {
369 SkString baseDirectory(dir);
370 load_directory_fonts(baseDirectory);
371
372 if (fFamilies.empty()) {
373 SkFontStyleSet_Custom* family = new SkFontStyleSet_Custom(SkString());
374 fFamilies.push_back().reset(family);
375 family->appendTypeface(SkNEW(SkTypeface_Empty));
376 }
377
378 // Try to pick a default font.
379 static const char* gDefaultNames[] = {
380 "Arial", "Verdana", "Times New Roman", NULL
381 };
382 for (size_t i = 0; i < SK_ARRAY_COUNT(gDefaultNames); ++i) {
383 SkFontStyleSet_Custom* set = this->onMatchFamily(gDefaultNames[i]);
384 if (NULL == set) {
385 continue;
386 }
387
388 SkTypeface* tf = set->matchStyle(SkFontStyle(SkFontStyle::kNormal_Weight,
389 SkFontStyle::kNormal_Width,
390 SkFontStyle::kUpright_Slant));
391 if (NULL == tf) {
392 continue;
393 }
394
395 gDefaultFamily = set;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000396 gDefaultNormal = tf;
397 break;
398 }
bungeman@google.comb3d154d2013-11-11 15:53:29 +0000399 if (NULL == gDefaultNormal) {
400 gDefaultFamily = fFamilies[0];
401 gDefaultNormal = gDefaultFamily->fStyles[0];
reed@android.com8a1c16f2008-12-17 15:59:43 +0000402 }
403 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000404
bungeman@google.comb3d154d2013-11-11 15:53:29 +0000405 SkTArray<SkAutoTUnref<SkFontStyleSet_Custom>, true> fFamilies;
406 SkFontStyleSet_Custom* gDefaultFamily;
407 SkTypeface* gDefaultNormal;
408};
reed@google.com070da5e2013-03-27 20:01:49 +0000409
410SkFontMgr* SkFontMgr::Factory() {
bungeman@google.comb3d154d2013-11-11 15:53:29 +0000411 return new SkFontMgr_Custom(SK_FONT_FILE_PREFIX);
reed@google.com070da5e2013-03-27 20:01:49 +0000412}