2 * Copyright 2006 The Android Open Source Project
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
8 #include "SkFontHost.h"
9 #include "SkFontHost_FreeType_common.h"
10 #include "SkFontDescriptor.h"
11 #include "SkFontMgr.h"
12 #include "SkDescriptor.h"
18 #include "SkTSearch.h"
19 #include "SkTypefaceCache.h"
24 #ifndef SK_FONT_FILE_PREFIX
25 # define SK_FONT_FILE_PREFIX "/usr/share/fonts/"
28 ///////////////////////////////////////////////////////////////////////////////
30 /** The base SkTypeface implementation for the custom font manager. */
31 class SkTypeface_Custom : public SkTypeface_FreeType {
33 SkTypeface_Custom(const SkFontStyle& style, bool isFixedPitch,
34 bool sysFont, const SkString familyName, int index)
35 : INHERITED(style, SkTypefaceCache::NewFontID(), isFixedPitch)
36 , fIsSysFont(sysFont), fFamilyName(familyName), fIndex(index)
39 bool isSysFont() const { return fIsSysFont; }
41 virtual const char* getUniqueString() const = 0;
44 virtual void onGetFamilyName(SkString* familyName) const SK_OVERRIDE {
45 *familyName = fFamilyName;
48 virtual void onGetFontDescriptor(SkFontDescriptor* desc, bool* isLocal) const SK_OVERRIDE {
49 desc->setFamilyName(fFamilyName.c_str());
50 desc->setFontFileName(this->getUniqueString());
51 desc->setFontIndex(fIndex);
52 *isLocal = !this->isSysFont();
55 int getIndex() const { return fIndex; }
58 const bool fIsSysFont;
59 const SkString fFamilyName;
62 typedef SkTypeface_FreeType INHERITED;
65 /** The empty SkTypeface implementation for the custom font manager.
66 * Used as the last resort fallback typeface.
68 class SkTypeface_Empty : public SkTypeface_Custom {
70 SkTypeface_Empty() : INHERITED(SkFontStyle(), false, true, SkString(), 0) {}
72 virtual const char* getUniqueString() const SK_OVERRIDE { return NULL; }
75 virtual SkStream* onOpenStream(int*) const SK_OVERRIDE { return NULL; }
78 typedef SkTypeface_Custom INHERITED;
81 /** The stream SkTypeface implementation for the custom font manager. */
82 class SkTypeface_Stream : public SkTypeface_Custom {
84 SkTypeface_Stream(const SkFontStyle& style, bool isFixedPitch, bool sysFont,
85 const SkString familyName, SkStream* stream, int index)
86 : INHERITED(style, isFixedPitch, sysFont, familyName, index)
87 , fStream(SkRef(stream))
90 virtual const char* getUniqueString() const SK_OVERRIDE { return NULL; }
93 virtual SkStream* onOpenStream(int* ttcIndex) const SK_OVERRIDE {
94 *ttcIndex = this->getIndex();
95 return fStream->duplicate();
99 const SkAutoTUnref<const SkStream> fStream;
101 typedef SkTypeface_Custom INHERITED;
104 /** The file SkTypeface implementation for the custom font manager. */
105 class SkTypeface_File : public SkTypeface_Custom {
107 SkTypeface_File(const SkFontStyle& style, bool isFixedPitch, bool sysFont,
108 const SkString familyName, const char path[], int index)
109 : INHERITED(style, isFixedPitch, sysFont, familyName, index)
113 virtual const char* getUniqueString() const SK_OVERRIDE {
114 const char* str = strrchr(fPath.c_str(), '/');
116 str += 1; // skip the '/'
122 virtual SkStream* onOpenStream(int* ttcIndex) const SK_OVERRIDE {
123 *ttcIndex = this->getIndex();
124 return SkStream::NewFromFile(fPath.c_str());
130 typedef SkTypeface_Custom INHERITED;
133 ///////////////////////////////////////////////////////////////////////////////
136 * SkFontStyleSet_Custom
138 * This class is used by SkFontMgr_Custom to hold SkTypeface_Custom families.
140 class SkFontStyleSet_Custom : public SkFontStyleSet {
142 explicit SkFontStyleSet_Custom(const SkString familyName) : fFamilyName(familyName) { }
144 virtual int count() SK_OVERRIDE {
145 return fStyles.count();
148 virtual void getStyle(int index, SkFontStyle* style, SkString* name) SK_OVERRIDE {
149 SkASSERT(index < fStyles.count());
150 bool bold = fStyles[index]->isBold();
151 bool italic = fStyles[index]->isItalic();
152 *style = SkFontStyle(bold ? SkFontStyle::kBold_Weight : SkFontStyle::kNormal_Weight,
153 SkFontStyle::kNormal_Width,
154 italic ? SkFontStyle::kItalic_Slant : SkFontStyle::kUpright_Slant);
158 virtual SkTypeface* createTypeface(int index) SK_OVERRIDE {
159 SkASSERT(index < fStyles.count());
160 return SkRef(fStyles[index].get());
163 static int match_score(const SkFontStyle& pattern, const SkFontStyle& candidate) {
165 score += (pattern.width() - candidate.width()) * 100;
166 score += (pattern.isItalic() == candidate.isItalic()) ? 0 : 1000;
167 score += pattern.weight() - candidate.weight();
171 virtual SkTypeface* matchStyle(const SkFontStyle& pattern) SK_OVERRIDE {
172 if (0 == fStyles.count()) {
176 SkTypeface_Custom* closest = fStyles[0];
177 int minScore = std::numeric_limits<int>::max();
178 for (int i = 0; i < fStyles.count(); ++i) {
179 bool bold = fStyles[i]->isBold();
180 bool italic = fStyles[i]->isItalic();
181 SkFontStyle style = SkFontStyle(bold ? SkFontStyle::kBold_Weight
182 : SkFontStyle::kNormal_Weight,
183 SkFontStyle::kNormal_Width,
184 italic ? SkFontStyle::kItalic_Slant
185 : SkFontStyle::kUpright_Slant);
187 int score = match_score(pattern, style);
188 if (score < minScore) {
189 closest = fStyles[i];
193 return SkRef(closest);
197 SkTArray<SkAutoTUnref<SkTypeface_Custom>, true> fStyles;
198 SkString fFamilyName;
200 void appendTypeface(SkTypeface_Custom* typeface) {
201 fStyles.push_back().reset(typeface);
204 friend class SkFontMgr_Custom;
210 * This class is essentially a collection of SkFontStyleSet_Custom,
211 * one SkFontStyleSet_Custom for each family. This class may be modified
212 * to load fonts from any source by changing the initialization.
214 class SkFontMgr_Custom : public SkFontMgr {
216 explicit SkFontMgr_Custom(const char* dir) {
217 this->load_system_fonts(dir);
221 virtual int onCountFamilies() const SK_OVERRIDE {
222 return fFamilies.count();
225 virtual void onGetFamilyName(int index, SkString* familyName) const SK_OVERRIDE {
226 SkASSERT(index < fFamilies.count());
227 familyName->set(fFamilies[index]->fFamilyName);
230 virtual SkFontStyleSet_Custom* onCreateStyleSet(int index) const SK_OVERRIDE {
231 SkASSERT(index < fFamilies.count());
232 return SkRef(fFamilies[index].get());
235 virtual SkFontStyleSet_Custom* onMatchFamily(const char familyName[]) const SK_OVERRIDE {
236 for (int i = 0; i < fFamilies.count(); ++i) {
237 if (fFamilies[i]->fFamilyName.equals(familyName)) {
238 return SkRef(fFamilies[i].get());
244 virtual SkTypeface* onMatchFamilyStyle(const char familyName[],
245 const SkFontStyle& fontStyle) const SK_OVERRIDE
247 SkAutoTUnref<SkFontStyleSet> sset(this->matchFamily(familyName));
248 return sset->matchStyle(fontStyle);
251 virtual SkTypeface* onMatchFaceStyle(const SkTypeface* familyMember,
252 const SkFontStyle& fontStyle) const SK_OVERRIDE
254 for (int i = 0; i < fFamilies.count(); ++i) {
255 for (int j = 0; j < fFamilies[i]->fStyles.count(); ++j) {
256 if (fFamilies[i]->fStyles[j] == familyMember) {
257 return fFamilies[i]->matchStyle(fontStyle);
264 virtual SkTypeface* onCreateFromData(SkData* data, int ttcIndex) const SK_OVERRIDE {
265 SkAutoTUnref<SkStream> stream(new SkMemoryStream(data));
266 return this->createFromStream(stream, ttcIndex);
269 virtual SkTypeface* onCreateFromStream(SkStream* stream, int ttcIndex) const SK_OVERRIDE {
270 if (NULL == stream || stream->getLength() <= 0) {
278 if (fScanner.scanFont(stream, ttcIndex, &name, &style, &isFixedPitch)) {
279 return SkNEW_ARGS(SkTypeface_Stream, (style, isFixedPitch, false, name,
286 virtual SkTypeface* onCreateFromFile(const char path[], int ttcIndex) const SK_OVERRIDE {
287 SkAutoTUnref<SkStream> stream(SkStream::NewFromFile(path));
288 return stream.get() ? this->createFromStream(stream, ttcIndex) : NULL;
291 virtual SkTypeface* onLegacyCreateTypeface(const char familyName[],
292 unsigned styleBits) const SK_OVERRIDE
294 SkTypeface::Style oldStyle = (SkTypeface::Style)styleBits;
295 SkFontStyle style = SkFontStyle(oldStyle & SkTypeface::kBold
296 ? SkFontStyle::kBold_Weight
297 : SkFontStyle::kNormal_Weight,
298 SkFontStyle::kNormal_Width,
299 oldStyle & SkTypeface::kItalic
300 ? SkFontStyle::kItalic_Slant
301 : SkFontStyle::kUpright_Slant);
302 SkTypeface* tf = NULL;
305 tf = this->onMatchFamilyStyle(familyName, style);
309 tf = gDefaultFamily->matchStyle(style);
312 return SkSafeRef(tf);
317 void load_directory_fonts(const SkString& directory, const char* suffix) {
318 SkOSFile::Iter iter(directory.c_str(), suffix);
321 while (iter.next(&name, false)) {
322 SkString filename(SkOSPath::Join(directory.c_str(), name.c_str()));
323 SkAutoTUnref<SkStream> stream(SkStream::NewFromFile(filename.c_str()));
325 SkDebugf("---- failed to open <%s>\n", filename.c_str());
330 if (!fScanner.recognizedFont(stream, &numFaces)) {
331 SkDebugf("---- failed to open <%s> as a font\n", filename.c_str());
335 for (int faceIndex = 0; faceIndex < numFaces; ++faceIndex) {
338 SkFontStyle style = SkFontStyle(); // avoid uninitialized warning
339 if (!fScanner.scanFont(stream, faceIndex, &realname, &style, &isFixedPitch)) {
340 SkDebugf("---- failed to open <%s> <%d> as a font\n",
341 filename.c_str(), faceIndex);
345 SkTypeface_Custom* tf = SkNEW_ARGS(SkTypeface_File, (
348 true, // system-font (cannot delete)
350 filename.c_str(), 0));
352 SkFontStyleSet_Custom* addTo = this->onMatchFamily(realname.c_str());
354 addTo = new SkFontStyleSet_Custom(realname);
355 fFamilies.push_back().reset(addTo);
357 addTo->appendTypeface(tf);
361 SkOSFile::Iter dirIter(directory.c_str());
362 while (dirIter.next(&name, true)) {
363 if (name.startsWith(".")) {
366 SkString dirname(SkOSPath::Join(directory.c_str(), name.c_str()));
367 load_directory_fonts(dirname, suffix);
371 void load_system_fonts(const char* dir) {
372 SkString baseDirectory(dir);
373 load_directory_fonts(baseDirectory, ".ttf");
374 load_directory_fonts(baseDirectory, ".ttc");
375 load_directory_fonts(baseDirectory, ".otf");
376 load_directory_fonts(baseDirectory, ".pfb");
378 if (fFamilies.empty()) {
379 SkFontStyleSet_Custom* family = new SkFontStyleSet_Custom(SkString());
380 fFamilies.push_back().reset(family);
381 family->appendTypeface(SkNEW(SkTypeface_Empty));
384 // Try to pick a default font.
385 static const char* gDefaultNames[] = {
386 "Arial", "Verdana", "Times New Roman", "Droid Sans", NULL
388 for (size_t i = 0; i < SK_ARRAY_COUNT(gDefaultNames); ++i) {
389 SkFontStyleSet_Custom* set = this->onMatchFamily(gDefaultNames[i]);
394 SkTypeface* tf = set->matchStyle(SkFontStyle(SkFontStyle::kNormal_Weight,
395 SkFontStyle::kNormal_Width,
396 SkFontStyle::kUpright_Slant));
401 gDefaultFamily = set;
405 if (NULL == gDefaultNormal) {
406 gDefaultFamily = fFamilies[0];
407 gDefaultNormal = gDefaultFamily->fStyles[0];
411 SkTArray<SkAutoTUnref<SkFontStyleSet_Custom>, true> fFamilies;
412 SkFontStyleSet_Custom* gDefaultFamily;
413 SkTypeface* gDefaultNormal;
414 SkTypeface_FreeType::Scanner fScanner;
417 SkFontMgr* SkFontMgr::Factory() {
418 return new SkFontMgr_Custom(SK_FONT_FILE_PREFIX);