michael@0: /* michael@0: * Copyright 2006 The Android Open Source Project michael@0: * michael@0: * Use of this source code is governed by a BSD-style license that can be michael@0: * found in the LICENSE file. michael@0: */ michael@0: michael@0: #include "SkFontHost.h" michael@0: #include "SkFontHost_FreeType_common.h" michael@0: #include "SkFontDescriptor.h" michael@0: #include "SkFontMgr.h" michael@0: #include "SkDescriptor.h" michael@0: #include "SkOSFile.h" michael@0: #include "SkPaint.h" michael@0: #include "SkString.h" michael@0: #include "SkStream.h" michael@0: #include "SkThread.h" michael@0: #include "SkTSearch.h" michael@0: #include "SkTypefaceCache.h" michael@0: #include "SkTArray.h" michael@0: michael@0: #include michael@0: michael@0: #ifndef SK_FONT_FILE_PREFIX michael@0: # define SK_FONT_FILE_PREFIX "/usr/share/fonts/truetype/" michael@0: #endif michael@0: michael@0: bool find_name_and_attributes(SkStream* stream, SkString* name, michael@0: SkTypeface::Style* style, bool* isFixedPitch); michael@0: michael@0: /////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: /** The base SkTypeface implementation for the custom font manager. */ michael@0: class SkTypeface_Custom : public SkTypeface_FreeType { michael@0: public: michael@0: SkTypeface_Custom(Style style, bool sysFont, bool isFixedPitch, const SkString familyName) michael@0: : INHERITED(style, SkTypefaceCache::NewFontID(), isFixedPitch) michael@0: , fIsSysFont(sysFont), fFamilyName(familyName) michael@0: { } michael@0: michael@0: bool isSysFont() const { return fIsSysFont; } michael@0: michael@0: virtual const char* getUniqueString() const = 0; michael@0: michael@0: protected: michael@0: virtual void onGetFontDescriptor(SkFontDescriptor* desc, bool* isLocal) const SK_OVERRIDE { michael@0: desc->setFamilyName(fFamilyName.c_str()); michael@0: desc->setFontFileName(this->getUniqueString()); michael@0: *isLocal = !this->isSysFont(); michael@0: } michael@0: michael@0: private: michael@0: bool fIsSysFont; michael@0: SkString fFamilyName; michael@0: michael@0: typedef SkTypeface_FreeType INHERITED; michael@0: }; michael@0: michael@0: /** The empty SkTypeface implementation for the custom font manager. michael@0: * Used as the last resort fallback typeface. michael@0: */ michael@0: class SkTypeface_Empty : public SkTypeface_Custom { michael@0: public: michael@0: SkTypeface_Empty() : INHERITED(SkTypeface::kNormal, true, false, SkString()) {} michael@0: michael@0: virtual const char* getUniqueString() const SK_OVERRIDE { return NULL; } michael@0: michael@0: protected: michael@0: virtual SkStream* onOpenStream(int*) const SK_OVERRIDE { return NULL; } michael@0: michael@0: private: michael@0: typedef SkTypeface_Custom INHERITED; michael@0: }; michael@0: michael@0: /** The stream SkTypeface implementation for the custom font manager. */ michael@0: class SkTypeface_Stream : public SkTypeface_Custom { michael@0: public: michael@0: SkTypeface_Stream(Style style, bool sysFont, SkStream* stream, michael@0: bool isFixedPitch, const SkString familyName) michael@0: : INHERITED(style, sysFont, isFixedPitch, familyName) michael@0: , fStream(SkRef(stream)) michael@0: { } michael@0: michael@0: virtual const char* getUniqueString() const SK_OVERRIDE { return NULL; } michael@0: michael@0: protected: michael@0: virtual SkStream* onOpenStream(int* ttcIndex) const SK_OVERRIDE { michael@0: *ttcIndex = 0; michael@0: return SkRef(fStream.get()); michael@0: } michael@0: michael@0: private: michael@0: SkAutoTUnref fStream; michael@0: michael@0: typedef SkTypeface_Custom INHERITED; michael@0: }; michael@0: michael@0: /** The file SkTypeface implementation for the custom font manager. */ michael@0: class SkTypeface_File : public SkTypeface_Custom { michael@0: public: michael@0: SkTypeface_File(Style style, bool sysFont, const char path[], michael@0: bool isFixedPitch, const SkString familyName) michael@0: : INHERITED(style, sysFont, isFixedPitch, familyName) michael@0: , fPath(path) michael@0: { } michael@0: michael@0: virtual const char* getUniqueString() const SK_OVERRIDE { michael@0: const char* str = strrchr(fPath.c_str(), '/'); michael@0: if (str) { michael@0: str += 1; // skip the '/' michael@0: } michael@0: return str; michael@0: } michael@0: michael@0: protected: michael@0: virtual SkStream* onOpenStream(int* ttcIndex) const SK_OVERRIDE { michael@0: *ttcIndex = 0; michael@0: return SkStream::NewFromFile(fPath.c_str()); michael@0: } michael@0: michael@0: private: michael@0: SkString fPath; michael@0: michael@0: typedef SkTypeface_Custom INHERITED; michael@0: }; michael@0: michael@0: /////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: /** michael@0: * SkFontStyleSet_Custom michael@0: * michael@0: * This class is used by SkFontMgr_Custom to hold SkTypeface_Custom families. michael@0: */ michael@0: class SkFontStyleSet_Custom : public SkFontStyleSet { michael@0: public: michael@0: explicit SkFontStyleSet_Custom(const SkString familyName) : fFamilyName(familyName) { } michael@0: michael@0: virtual int count() SK_OVERRIDE { michael@0: return fStyles.count(); michael@0: } michael@0: michael@0: virtual void getStyle(int index, SkFontStyle* style, SkString* name) SK_OVERRIDE { michael@0: SkASSERT(index < fStyles.count()); michael@0: bool bold = fStyles[index]->isBold(); michael@0: bool italic = fStyles[index]->isItalic(); michael@0: *style = SkFontStyle(bold ? SkFontStyle::kBold_Weight : SkFontStyle::kNormal_Weight, michael@0: SkFontStyle::kNormal_Width, michael@0: italic ? SkFontStyle::kItalic_Slant : SkFontStyle::kUpright_Slant); michael@0: name->reset(); michael@0: } michael@0: michael@0: virtual SkTypeface* createTypeface(int index) SK_OVERRIDE { michael@0: SkASSERT(index < fStyles.count()); michael@0: return SkRef(fStyles[index].get()); michael@0: } michael@0: michael@0: static int match_score(const SkFontStyle& pattern, const SkFontStyle& candidate) { michael@0: int score = 0; michael@0: score += (pattern.width() - candidate.width()) * 100; michael@0: score += (pattern.isItalic() == candidate.isItalic()) ? 0 : 1000; michael@0: score += pattern.weight() - candidate.weight(); michael@0: return score; michael@0: } michael@0: michael@0: virtual SkTypeface* matchStyle(const SkFontStyle& pattern) SK_OVERRIDE { michael@0: if (0 == fStyles.count()) { michael@0: return NULL; michael@0: } michael@0: michael@0: SkTypeface_Custom* closest = fStyles[0]; michael@0: int minScore = std::numeric_limits::max(); michael@0: for (int i = 0; i < fStyles.count(); ++i) { michael@0: bool bold = fStyles[i]->isBold(); michael@0: bool italic = fStyles[i]->isItalic(); michael@0: SkFontStyle style = SkFontStyle(bold ? SkFontStyle::kBold_Weight michael@0: : SkFontStyle::kNormal_Weight, michael@0: SkFontStyle::kNormal_Width, michael@0: italic ? SkFontStyle::kItalic_Slant michael@0: : SkFontStyle::kUpright_Slant); michael@0: michael@0: int score = match_score(pattern, style); michael@0: if (score < minScore) { michael@0: closest = fStyles[i]; michael@0: minScore = score; michael@0: } michael@0: } michael@0: return SkRef(closest); michael@0: } michael@0: michael@0: private: michael@0: SkTArray, true> fStyles; michael@0: SkString fFamilyName; michael@0: michael@0: void appendTypeface(SkTypeface_Custom* typeface) { michael@0: fStyles.push_back().reset(typeface); michael@0: } michael@0: michael@0: friend class SkFontMgr_Custom; michael@0: }; michael@0: michael@0: /** michael@0: * SkFontMgr_Custom michael@0: * michael@0: * This class is essentially a collection of SkFontStyleSet_Custom, michael@0: * one SkFontStyleSet_Custom for each family. This class may be modified michael@0: * to load fonts from any source by changing the initialization. michael@0: */ michael@0: class SkFontMgr_Custom : public SkFontMgr { michael@0: public: michael@0: explicit SkFontMgr_Custom(const char* dir) { michael@0: this->load_system_fonts(dir); michael@0: } michael@0: michael@0: protected: michael@0: virtual int onCountFamilies() const SK_OVERRIDE { michael@0: return fFamilies.count(); michael@0: } michael@0: michael@0: virtual void onGetFamilyName(int index, SkString* familyName) const SK_OVERRIDE { michael@0: SkASSERT(index < fFamilies.count()); michael@0: familyName->set(fFamilies[index]->fFamilyName); michael@0: } michael@0: michael@0: virtual SkFontStyleSet_Custom* onCreateStyleSet(int index) const SK_OVERRIDE { michael@0: SkASSERT(index < fFamilies.count()); michael@0: return SkRef(fFamilies[index].get()); michael@0: } michael@0: michael@0: virtual SkFontStyleSet_Custom* onMatchFamily(const char familyName[]) const SK_OVERRIDE { michael@0: for (int i = 0; i < fFamilies.count(); ++i) { michael@0: if (fFamilies[i]->fFamilyName.equals(familyName)) { michael@0: return SkRef(fFamilies[i].get()); michael@0: } michael@0: } michael@0: return NULL; michael@0: } michael@0: michael@0: virtual SkTypeface* onMatchFamilyStyle(const char familyName[], michael@0: const SkFontStyle& fontStyle) const SK_OVERRIDE michael@0: { michael@0: SkAutoTUnref sset(this->matchFamily(familyName)); michael@0: return sset->matchStyle(fontStyle); michael@0: } michael@0: michael@0: virtual SkTypeface* onMatchFaceStyle(const SkTypeface* familyMember, michael@0: const SkFontStyle& fontStyle) const SK_OVERRIDE michael@0: { michael@0: for (int i = 0; i < fFamilies.count(); ++i) { michael@0: for (int j = 0; j < fFamilies[i]->fStyles.count(); ++j) { michael@0: if (fFamilies[i]->fStyles[j] == familyMember) { michael@0: return fFamilies[i]->matchStyle(fontStyle); michael@0: } michael@0: } michael@0: } michael@0: return NULL; michael@0: } michael@0: michael@0: virtual SkTypeface* onCreateFromData(SkData* data, int ttcIndex) const SK_OVERRIDE { michael@0: SkAutoTUnref stream(new SkMemoryStream(data)); michael@0: return this->createFromStream(stream, ttcIndex); michael@0: } michael@0: michael@0: virtual SkTypeface* onCreateFromStream(SkStream* stream, int ttcIndex) const SK_OVERRIDE { michael@0: if (NULL == stream || stream->getLength() <= 0) { michael@0: SkDELETE(stream); michael@0: return NULL; michael@0: } michael@0: michael@0: bool isFixedPitch; michael@0: SkTypeface::Style style; michael@0: SkString name; michael@0: if (find_name_and_attributes(stream, &name, &style, &isFixedPitch)) { michael@0: return SkNEW_ARGS(SkTypeface_Stream, (style, false, stream, isFixedPitch, name)); michael@0: } else { michael@0: return NULL; michael@0: } michael@0: } michael@0: michael@0: virtual SkTypeface* onCreateFromFile(const char path[], int ttcIndex) const SK_OVERRIDE { michael@0: SkAutoTUnref stream(SkStream::NewFromFile(path)); michael@0: return stream.get() ? this->createFromStream(stream, ttcIndex) : NULL; michael@0: } michael@0: michael@0: virtual SkTypeface* onLegacyCreateTypeface(const char familyName[], michael@0: unsigned styleBits) const SK_OVERRIDE michael@0: { michael@0: SkTypeface::Style oldStyle = (SkTypeface::Style)styleBits; michael@0: SkFontStyle style = SkFontStyle(oldStyle & SkTypeface::kBold michael@0: ? SkFontStyle::kBold_Weight michael@0: : SkFontStyle::kNormal_Weight, michael@0: SkFontStyle::kNormal_Width, michael@0: oldStyle & SkTypeface::kItalic michael@0: ? SkFontStyle::kItalic_Slant michael@0: : SkFontStyle::kUpright_Slant); michael@0: SkTypeface* tf = NULL; michael@0: michael@0: if (NULL != familyName) { michael@0: tf = this->onMatchFamilyStyle(familyName, style); michael@0: } michael@0: michael@0: if (NULL == tf) { michael@0: tf = gDefaultFamily->matchStyle(style); michael@0: } michael@0: michael@0: return SkSafeRef(tf); michael@0: } michael@0: michael@0: private: michael@0: michael@0: static bool get_name_and_style(const char path[], SkString* name, michael@0: SkTypeface::Style* style, bool* isFixedPitch) { michael@0: SkAutoTUnref stream(SkStream::NewFromFile(path)); michael@0: if (stream.get()) { michael@0: return find_name_and_attributes(stream, name, style, isFixedPitch); michael@0: } else { michael@0: SkDebugf("---- failed to open <%s> as a font\n", path); michael@0: return false; michael@0: } michael@0: } michael@0: michael@0: void load_directory_fonts(const SkString& directory) { michael@0: SkOSFile::Iter iter(directory.c_str(), ".ttf"); michael@0: SkString name; michael@0: michael@0: while (iter.next(&name, false)) { michael@0: SkString filename( michael@0: SkOSPath::SkPathJoin(directory.c_str(), name.c_str())); michael@0: michael@0: bool isFixedPitch; michael@0: SkString realname; michael@0: SkTypeface::Style style = SkTypeface::kNormal; // avoid uninitialized warning michael@0: michael@0: if (!get_name_and_style(filename.c_str(), &realname, &style, &isFixedPitch)) { michael@0: SkDebugf("------ can't load <%s> as a font\n", filename.c_str()); michael@0: continue; michael@0: } michael@0: michael@0: SkTypeface_Custom* tf = SkNEW_ARGS(SkTypeface_File, ( michael@0: style, michael@0: true, // system-font (cannot delete) michael@0: filename.c_str(), michael@0: isFixedPitch, michael@0: realname)); michael@0: michael@0: SkFontStyleSet_Custom* addTo = this->onMatchFamily(realname.c_str()); michael@0: if (NULL == addTo) { michael@0: addTo = new SkFontStyleSet_Custom(realname); michael@0: fFamilies.push_back().reset(addTo); michael@0: } michael@0: addTo->appendTypeface(tf); michael@0: } michael@0: michael@0: SkOSFile::Iter dirIter(directory.c_str()); michael@0: while (dirIter.next(&name, true)) { michael@0: if (name.startsWith(".")) { michael@0: continue; michael@0: } michael@0: SkString dirname( michael@0: SkOSPath::SkPathJoin(directory.c_str(), name.c_str())); michael@0: load_directory_fonts(dirname); michael@0: } michael@0: } michael@0: michael@0: void load_system_fonts(const char* dir) { michael@0: SkString baseDirectory(dir); michael@0: load_directory_fonts(baseDirectory); michael@0: michael@0: if (fFamilies.empty()) { michael@0: SkFontStyleSet_Custom* family = new SkFontStyleSet_Custom(SkString()); michael@0: fFamilies.push_back().reset(family); michael@0: family->appendTypeface(SkNEW(SkTypeface_Empty)); michael@0: } michael@0: michael@0: // Try to pick a default font. michael@0: static const char* gDefaultNames[] = { michael@0: "Arial", "Verdana", "Times New Roman", "Droid Sans", NULL michael@0: }; michael@0: for (size_t i = 0; i < SK_ARRAY_COUNT(gDefaultNames); ++i) { michael@0: SkFontStyleSet_Custom* set = this->onMatchFamily(gDefaultNames[i]); michael@0: if (NULL == set) { michael@0: continue; michael@0: } michael@0: michael@0: SkTypeface* tf = set->matchStyle(SkFontStyle(SkFontStyle::kNormal_Weight, michael@0: SkFontStyle::kNormal_Width, michael@0: SkFontStyle::kUpright_Slant)); michael@0: if (NULL == tf) { michael@0: continue; michael@0: } michael@0: michael@0: gDefaultFamily = set; michael@0: gDefaultNormal = tf; michael@0: break; michael@0: } michael@0: if (NULL == gDefaultNormal) { michael@0: gDefaultFamily = fFamilies[0]; michael@0: gDefaultNormal = gDefaultFamily->fStyles[0]; michael@0: } michael@0: } michael@0: michael@0: SkTArray, true> fFamilies; michael@0: SkFontStyleSet_Custom* gDefaultFamily; michael@0: SkTypeface* gDefaultNormal; michael@0: }; michael@0: michael@0: SkFontMgr* SkFontMgr::Factory() { michael@0: return new SkFontMgr_Custom(SK_FONT_FILE_PREFIX); michael@0: }