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/truetype/"
28 ///////////////////////////////////////////////////////////////////////////////
30 /** The base SkTypeface implementation for the custom font manager. */
31 class SkTypeface_Custom : public SkTypeface_FreeType {
33 SkTypeface_Custom(Style style, bool isFixedPitch, bool sysFont, const SkString familyName)
34 : INHERITED(style, SkTypefaceCache::NewFontID(), isFixedPitch)
35 , fIsSysFont(sysFont), fFamilyName(familyName)
38 bool isSysFont() const { return fIsSysFont; }
40 virtual const char* getUniqueString() const = 0;
43 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();
53 typedef SkTypeface_FreeType INHERITED;
56 /** The empty SkTypeface implementation for the custom font manager.
57 * Used as the last resort fallback typeface.
59 class SkTypeface_Empty : public SkTypeface_Custom {
61 SkTypeface_Empty() : INHERITED(SkTypeface::kNormal, false, true, SkString()) {}
63 virtual const char* getUniqueString() const SK_OVERRIDE { return NULL; }
66 virtual SkStream* onOpenStream(int*) const SK_OVERRIDE { return NULL; }
69 typedef SkTypeface_Custom INHERITED;
72 /** The stream SkTypeface implementation for the custom font manager. */
73 class SkTypeface_Stream : public SkTypeface_Custom {
75 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)
81 virtual const char* getUniqueString() const SK_OVERRIDE { return NULL; }
84 virtual SkStream* onOpenStream(int* ttcIndex) const SK_OVERRIDE {
86 return fStream->duplicate();
90 SkAutoTUnref<SkStream> fStream;
93 typedef SkTypeface_Custom INHERITED;
96 /** The file SkTypeface implementation for the custom font manager. */
97 class SkTypeface_File : public SkTypeface_Custom {
99 SkTypeface_File(Style style, bool isFixedPitch, bool sysFont, const SkString familyName,
101 : INHERITED(style, isFixedPitch, sysFont, familyName)
105 virtual const char* getUniqueString() const SK_OVERRIDE {
106 const char* str = strrchr(fPath.c_str(), '/');
108 str += 1; // skip the '/'
114 virtual SkStream* onOpenStream(int* ttcIndex) const SK_OVERRIDE {
116 return SkStream::NewFromFile(fPath.c_str());
122 typedef SkTypeface_Custom INHERITED;
125 ///////////////////////////////////////////////////////////////////////////////
128 * SkFontStyleSet_Custom
130 * This class is used by SkFontMgr_Custom to hold SkTypeface_Custom families.
132 class SkFontStyleSet_Custom : public SkFontStyleSet {
134 explicit SkFontStyleSet_Custom(const SkString familyName) : fFamilyName(familyName) { }
136 virtual int count() SK_OVERRIDE {
137 return fStyles.count();
140 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);
150 virtual SkTypeface* createTypeface(int index) SK_OVERRIDE {
151 SkASSERT(index < fStyles.count());
152 return SkRef(fStyles[index].get());
155 static int match_score(const SkFontStyle& pattern, const SkFontStyle& candidate) {
157 score += (pattern.width() - candidate.width()) * 100;
158 score += (pattern.isItalic() == candidate.isItalic()) ? 0 : 1000;
159 score += pattern.weight() - candidate.weight();
163 virtual SkTypeface* matchStyle(const SkFontStyle& pattern) SK_OVERRIDE {
164 if (0 == fStyles.count()) {
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);
179 int score = match_score(pattern, style);
180 if (score < minScore) {
181 closest = fStyles[i];
185 return SkRef(closest);
189 SkTArray<SkAutoTUnref<SkTypeface_Custom>, true> fStyles;
190 SkString fFamilyName;
192 void appendTypeface(SkTypeface_Custom* typeface) {
193 fStyles.push_back().reset(typeface);
196 friend class SkFontMgr_Custom;
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.
206 class SkFontMgr_Custom : public SkFontMgr {
208 explicit SkFontMgr_Custom(const char* dir) {
209 this->load_system_fonts(dir);
213 virtual int onCountFamilies() const SK_OVERRIDE {
214 return fFamilies.count();
217 virtual void onGetFamilyName(int index, SkString* familyName) const SK_OVERRIDE {
218 SkASSERT(index < fFamilies.count());
219 familyName->set(fFamilies[index]->fFamilyName);
222 virtual SkFontStyleSet_Custom* onCreateStyleSet(int index) const SK_OVERRIDE {
223 SkASSERT(index < fFamilies.count());
224 return SkRef(fFamilies[index].get());
227 virtual SkFontStyleSet_Custom* onMatchFamily(const char familyName[]) const SK_OVERRIDE {
228 for (int i = 0; i < fFamilies.count(); ++i) {
229 if (fFamilies[i]->fFamilyName.equals(familyName)) {
230 return SkRef(fFamilies[i].get());
236 virtual SkTypeface* onMatchFamilyStyle(const char familyName[],
237 const SkFontStyle& fontStyle) const SK_OVERRIDE
239 SkAutoTUnref<SkFontStyleSet> sset(this->matchFamily(familyName));
240 return sset->matchStyle(fontStyle);
243 virtual SkTypeface* onMatchFaceStyle(const SkTypeface* familyMember,
244 const SkFontStyle& fontStyle) const SK_OVERRIDE
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);
256 virtual SkTypeface* onCreateFromData(SkData* data, int ttcIndex) const SK_OVERRIDE {
257 SkAutoTUnref<SkStream> stream(new SkMemoryStream(data));
258 return this->createFromStream(stream, ttcIndex);
261 virtual SkTypeface* onCreateFromStream(SkStream* stream, int ttcIndex) const SK_OVERRIDE {
262 if (NULL == stream || stream->getLength() <= 0) {
268 SkTypeface::Style style;
270 if (SkTypeface_FreeType::ScanFont(stream, ttcIndex, &name, &style, &isFixedPitch)) {
271 return SkNEW_ARGS(SkTypeface_Stream, (style, isFixedPitch, false, name,
278 virtual SkTypeface* onCreateFromFile(const char path[], int ttcIndex) const SK_OVERRIDE {
279 SkAutoTUnref<SkStream> stream(SkStream::NewFromFile(path));
280 return stream.get() ? this->createFromStream(stream, ttcIndex) : NULL;
283 virtual SkTypeface* onLegacyCreateTypeface(const char familyName[],
284 unsigned styleBits) const SK_OVERRIDE
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;
296 if (NULL != familyName) {
297 tf = this->onMatchFamilyStyle(familyName, style);
301 tf = gDefaultFamily->matchStyle(style);
304 return SkSafeRef(tf);
309 static bool get_name_and_style(const char path[], SkString* name,
310 SkTypeface::Style* style, bool* isFixedPitch) {
311 SkAutoTUnref<SkStream> stream(SkStream::NewFromFile(path));
313 return SkTypeface_FreeType::ScanFont(stream, 0, name, style, isFixedPitch);
315 SkDebugf("---- failed to open <%s> as a font\n", path);
320 void load_directory_fonts(const SkString& directory) {
321 SkOSFile::Iter iter(directory.c_str(), ".ttf");
324 while (iter.next(&name, false)) {
326 SkOSPath::Join(directory.c_str(), name.c_str()));
330 SkTypeface::Style style = SkTypeface::kNormal; // avoid uninitialized warning
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());
337 SkTypeface_Custom* tf = SkNEW_ARGS(SkTypeface_File, (
340 true, // system-font (cannot delete)
344 SkFontStyleSet_Custom* addTo = this->onMatchFamily(realname.c_str());
346 addTo = new SkFontStyleSet_Custom(realname);
347 fFamilies.push_back().reset(addTo);
349 addTo->appendTypeface(tf);
352 SkOSFile::Iter dirIter(directory.c_str());
353 while (dirIter.next(&name, true)) {
354 if (name.startsWith(".")) {
357 SkString dirname(SkOSPath::Join(directory.c_str(), name.c_str()));
358 load_directory_fonts(dirname);
362 void load_system_fonts(const char* dir) {
363 SkString baseDirectory(dir);
364 load_directory_fonts(baseDirectory);
366 if (fFamilies.empty()) {
367 SkFontStyleSet_Custom* family = new SkFontStyleSet_Custom(SkString());
368 fFamilies.push_back().reset(family);
369 family->appendTypeface(SkNEW(SkTypeface_Empty));
372 // Try to pick a default font.
373 static const char* gDefaultNames[] = {
374 "Arial", "Verdana", "Times New Roman", "Droid Sans", NULL
376 for (size_t i = 0; i < SK_ARRAY_COUNT(gDefaultNames); ++i) {
377 SkFontStyleSet_Custom* set = this->onMatchFamily(gDefaultNames[i]);
382 SkTypeface* tf = set->matchStyle(SkFontStyle(SkFontStyle::kNormal_Weight,
383 SkFontStyle::kNormal_Width,
384 SkFontStyle::kUpright_Slant));
389 gDefaultFamily = set;
393 if (NULL == gDefaultNormal) {
394 gDefaultFamily = fFamilies[0];
395 gDefaultNormal = gDefaultFamily->fStyles[0];
399 SkTArray<SkAutoTUnref<SkFontStyleSet_Custom>, true> fFamilies;
400 SkFontStyleSet_Custom* gDefaultFamily;
401 SkTypeface* gDefaultNormal;
404 SkFontMgr* SkFontMgr::Factory() {
405 return new SkFontMgr_Custom(SK_FONT_FILE_PREFIX);