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/"
27 #ifndef SK_FONT_FILE_DIR_SEPERATOR
28 # define SK_FONT_FILE_DIR_SEPERATOR "/"
31 bool find_name_and_attributes(SkStream* stream, SkString* name,
32 SkTypeface::Style* style, bool* isFixedPitch);
34 ///////////////////////////////////////////////////////////////////////////////
36 /** The base SkTypeface implementation for the custom font manager. */
37 class SkTypeface_Custom : public SkTypeface_FreeType {
39 SkTypeface_Custom(Style style, bool sysFont, bool isFixedPitch, const SkString familyName)
40 : INHERITED(style, SkTypefaceCache::NewFontID(), isFixedPitch)
41 , fIsSysFont(sysFont), fFamilyName(familyName)
44 bool isSysFont() const { return fIsSysFont; }
46 virtual const char* getUniqueString() const = 0;
49 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();
59 typedef SkTypeface_FreeType INHERITED;
62 /** The empty SkTypeface implementation for the custom font manager.
63 * Used as the last resort fallback typeface.
65 class SkTypeface_Empty : public SkTypeface_Custom {
67 SkTypeface_Empty() : INHERITED(SkTypeface::kNormal, true, false, SkString()) {}
69 virtual const char* getUniqueString() const SK_OVERRIDE { return NULL; }
72 virtual SkStream* onOpenStream(int*) const SK_OVERRIDE { return NULL; }
75 typedef SkTypeface_Custom INHERITED;
78 /** The stream SkTypeface implementation for the custom font manager. */
79 class SkTypeface_Stream : public SkTypeface_Custom {
81 SkTypeface_Stream(Style style, bool sysFont, SkStream* stream,
82 bool isFixedPitch, const SkString familyName)
83 : INHERITED(style, sysFont, isFixedPitch, familyName)
84 , fStream(SkRef(stream))
87 virtual const char* getUniqueString() const SK_OVERRIDE { return NULL; }
90 virtual SkStream* onOpenStream(int* ttcIndex) const SK_OVERRIDE {
92 return SkRef(fStream.get());
96 SkAutoTUnref<SkStream> fStream;
98 typedef SkTypeface_Custom INHERITED;
101 /** The file SkTypeface implementation for the custom font manager. */
102 class SkTypeface_File : public SkTypeface_Custom {
104 SkTypeface_File(Style style, bool sysFont, const char path[],
105 bool isFixedPitch, const SkString familyName)
106 : INHERITED(style, sysFont, isFixedPitch, familyName)
110 virtual const char* getUniqueString() const SK_OVERRIDE {
111 const char* str = strrchr(fPath.c_str(), '/');
113 str += 1; // skip the '/'
119 virtual SkStream* onOpenStream(int* ttcIndex) const SK_OVERRIDE {
121 return SkStream::NewFromFile(fPath.c_str());
127 typedef SkTypeface_Custom INHERITED;
130 ///////////////////////////////////////////////////////////////////////////////
133 * SkFontStyleSet_Custom
135 * This class is used by SkFontMgr_Custom to hold SkTypeface_Custom families.
137 class SkFontStyleSet_Custom : public SkFontStyleSet {
139 explicit SkFontStyleSet_Custom(const SkString familyName) : fFamilyName(familyName) { }
141 virtual int count() SK_OVERRIDE {
142 return fStyles.count();
145 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);
155 virtual SkTypeface* createTypeface(int index) SK_OVERRIDE {
156 SkASSERT(index < fStyles.count());
157 return SkRef(fStyles[index].get());
160 static int match_score(const SkFontStyle& pattern, const SkFontStyle& candidate) {
162 score += (pattern.width() - candidate.width()) * 100;
163 score += (pattern.isItalic() == candidate.isItalic()) ? 0 : 1000;
164 score += pattern.weight() - candidate.weight();
168 virtual SkTypeface* matchStyle(const SkFontStyle& pattern) SK_OVERRIDE {
169 if (0 == fStyles.count()) {
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);
184 int score = match_score(pattern, style);
185 if (score < minScore) {
186 closest = fStyles[i];
190 return SkRef(closest);
194 SkTArray<SkAutoTUnref<SkTypeface_Custom>, true> fStyles;
195 SkString fFamilyName;
197 void appendTypeface(SkTypeface_Custom* typeface) {
198 fStyles.push_back().reset(typeface);
201 friend class SkFontMgr_Custom;
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.
211 class SkFontMgr_Custom : public SkFontMgr {
213 explicit SkFontMgr_Custom(const char* dir) {
214 this->load_system_fonts(dir);
218 virtual int onCountFamilies() const SK_OVERRIDE {
219 return fFamilies.count();
222 virtual void onGetFamilyName(int index, SkString* familyName) const SK_OVERRIDE {
223 SkASSERT(index < fFamilies.count());
224 familyName->set(fFamilies[index]->fFamilyName);
227 virtual SkFontStyleSet_Custom* onCreateStyleSet(int index) const SK_OVERRIDE {
228 SkASSERT(index < fFamilies.count());
229 return SkRef(fFamilies[index].get());
232 virtual SkFontStyleSet_Custom* onMatchFamily(const char familyName[]) const SK_OVERRIDE {
233 for (int i = 0; i < fFamilies.count(); ++i) {
234 if (fFamilies[i]->fFamilyName.equals(familyName)) {
235 return SkRef(fFamilies[i].get());
241 virtual SkTypeface* onMatchFamilyStyle(const char familyName[],
242 const SkFontStyle& fontStyle) const SK_OVERRIDE
244 SkAutoTUnref<SkFontStyleSet> sset(this->matchFamily(familyName));
245 return sset->matchStyle(fontStyle);
248 virtual SkTypeface* onMatchFaceStyle(const SkTypeface* familyMember,
249 const SkFontStyle& fontStyle) const SK_OVERRIDE
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);
261 virtual SkTypeface* onCreateFromData(SkData* data, int ttcIndex) const SK_OVERRIDE {
262 SkAutoTUnref<SkStream> stream(new SkMemoryStream(data));
263 return this->createFromStream(stream, ttcIndex);
266 virtual SkTypeface* onCreateFromStream(SkStream* stream, int ttcIndex) const SK_OVERRIDE {
267 if (NULL == stream || stream->getLength() <= 0) {
273 SkTypeface::Style style;
275 if (find_name_and_attributes(stream, &name, &style, &isFixedPitch)) {
276 return SkNEW_ARGS(SkTypeface_Stream, (style, false, stream, isFixedPitch, name));
282 virtual SkTypeface* onCreateFromFile(const char path[], int ttcIndex) const SK_OVERRIDE {
283 SkAutoTUnref<SkStream> stream(SkStream::NewFromFile(path));
284 return stream.get() ? this->createFromStream(stream, ttcIndex) : NULL;
287 virtual SkTypeface* onLegacyCreateTypeface(const char familyName[],
288 unsigned styleBits) const SK_OVERRIDE
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;
300 if (NULL != familyName) {
301 tf = this->onMatchFamilyStyle(familyName, style);
305 tf = gDefaultFamily->matchStyle(style);
308 return SkSafeRef(tf);
313 static bool get_name_and_style(const char path[], SkString* name,
314 SkTypeface::Style* style, bool* isFixedPitch) {
315 SkAutoTUnref<SkStream> stream(SkStream::NewFromFile(path));
317 return find_name_and_attributes(stream, name, style, isFixedPitch);
319 SkDebugf("---- failed to open <%s> as a font\n", path);
324 void load_directory_fonts(const SkString& directory) {
325 SkOSFile::Iter iter(directory.c_str(), ".ttf");
328 while (iter.next(&name, false)) {
329 SkString filename(directory);
330 filename.append(name);
334 SkTypeface::Style style = SkTypeface::kNormal; // avoid uninitialized warning
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());
341 SkTypeface_Custom* tf = SkNEW_ARGS(SkTypeface_File, (
343 true, // system-font (cannot delete)
348 SkFontStyleSet_Custom* addTo = this->onMatchFamily(realname.c_str());
350 addTo = new SkFontStyleSet_Custom(realname);
351 fFamilies.push_back().reset(addTo);
353 addTo->appendTypeface(tf);
356 SkOSFile::Iter dirIter(directory.c_str());
357 while (dirIter.next(&name, true)) {
358 if (name.startsWith(".")) {
361 SkString dirname(directory);
362 dirname.append(name);
363 dirname.append(SK_FONT_FILE_DIR_SEPERATOR);
364 load_directory_fonts(dirname);
368 void load_system_fonts(const char* dir) {
369 SkString baseDirectory(dir);
370 load_directory_fonts(baseDirectory);
372 if (fFamilies.empty()) {
373 SkFontStyleSet_Custom* family = new SkFontStyleSet_Custom(SkString());
374 fFamilies.push_back().reset(family);
375 family->appendTypeface(SkNEW(SkTypeface_Empty));
378 // Try to pick a default font.
379 static const char* gDefaultNames[] = {
380 "Arial", "Verdana", "Times New Roman", NULL
382 for (size_t i = 0; i < SK_ARRAY_COUNT(gDefaultNames); ++i) {
383 SkFontStyleSet_Custom* set = this->onMatchFamily(gDefaultNames[i]);
388 SkTypeface* tf = set->matchStyle(SkFontStyle(SkFontStyle::kNormal_Weight,
389 SkFontStyle::kNormal_Width,
390 SkFontStyle::kUpright_Slant));
395 gDefaultFamily = set;
399 if (NULL == gDefaultNormal) {
400 gDefaultFamily = fFamilies[0];
401 gDefaultNormal = gDefaultFamily->fStyles[0];
405 SkTArray<SkAutoTUnref<SkFontStyleSet_Custom>, true> fFamilies;
406 SkFontStyleSet_Custom* gDefaultFamily;
407 SkTypeface* gDefaultNormal;
410 SkFontMgr* SkFontMgr::Factory() {
411 return new SkFontMgr_Custom(SK_FONT_FILE_PREFIX);