michael@0: /* michael@0: * Copyright 2013 Google Inc. 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 "SkFontMgr.h" michael@0: #include "SkFontStyle.h" michael@0: #include "SkFontConfigInterface.h" michael@0: #include "SkFontConfigTypeface.h" michael@0: #include "SkMath.h" michael@0: #include "SkString.h" michael@0: #include "SkTDArray.h" michael@0: michael@0: // for now we pull these in directly. eventually we will solely rely on the michael@0: // SkFontConfigInterface instance. michael@0: #include michael@0: #include michael@0: michael@0: // Defined in SkFontHost_FreeType.cpp michael@0: bool find_name_and_attributes(SkStream* stream, SkString* name, michael@0: SkTypeface::Style* style, bool* isFixedWidth); michael@0: michael@0: // borrow this global from SkFontHost_fontconfig. eventually that file should michael@0: // go away, and be replaced with this one. michael@0: extern SkFontConfigInterface* SkFontHost_fontconfig_ref_global(); michael@0: static SkFontConfigInterface* RefFCI() { michael@0: return SkFontHost_fontconfig_ref_global(); michael@0: } michael@0: michael@0: // look for the last substring after a '/' and return that, or return null. michael@0: static const char* find_just_name(const char* str) { michael@0: const char* last = strrchr(str, '/'); michael@0: return last ? last + 1 : NULL; michael@0: } michael@0: michael@0: static bool is_lower(char c) { michael@0: return c >= 'a' && c <= 'z'; michael@0: } michael@0: michael@0: static int get_int(FcPattern* pattern, const char field[]) { michael@0: int value; michael@0: if (FcPatternGetInteger(pattern, field, 0, &value) != FcResultMatch) { michael@0: value = SK_MinS32; michael@0: } michael@0: return value; michael@0: } michael@0: michael@0: static const char* get_name(FcPattern* pattern, const char field[]) { michael@0: const char* name; michael@0: if (FcPatternGetString(pattern, field, 0, (FcChar8**)&name) != FcResultMatch) { michael@0: name = ""; michael@0: } michael@0: return name; michael@0: } michael@0: michael@0: static bool valid_pattern(FcPattern* pattern) { michael@0: FcBool is_scalable; michael@0: if (FcPatternGetBool(pattern, FC_SCALABLE, 0, &is_scalable) != FcResultMatch || !is_scalable) { michael@0: return false; michael@0: } michael@0: michael@0: // fontconfig can also return fonts which are unreadable michael@0: const char* c_filename = get_name(pattern, FC_FILE); michael@0: if (0 == *c_filename) { michael@0: return false; michael@0: } michael@0: if (access(c_filename, R_OK) != 0) { michael@0: return false; michael@0: } michael@0: return true; michael@0: } michael@0: michael@0: static bool match_name(FcPattern* pattern, const char family_name[]) { michael@0: return !strcasecmp(family_name, get_name(pattern, FC_FAMILY)); michael@0: } michael@0: michael@0: static FcPattern** MatchFont(FcFontSet* font_set, michael@0: const char post_config_family[], michael@0: int* count) { michael@0: // Older versions of fontconfig have a bug where they cannot select michael@0: // only scalable fonts so we have to manually filter the results. michael@0: michael@0: FcPattern** iter = font_set->fonts; michael@0: FcPattern** stop = iter + font_set->nfont; michael@0: // find the first good match michael@0: for (; iter < stop; ++iter) { michael@0: if (valid_pattern(*iter)) { michael@0: break; michael@0: } michael@0: } michael@0: michael@0: if (iter == stop || !match_name(*iter, post_config_family)) { michael@0: return NULL; michael@0: } michael@0: michael@0: FcPattern** firstIter = iter++; michael@0: for (; iter < stop; ++iter) { michael@0: if (!valid_pattern(*iter) || !match_name(*iter, post_config_family)) { michael@0: break; michael@0: } michael@0: } michael@0: michael@0: *count = iter - firstIter; michael@0: return firstIter; michael@0: } michael@0: michael@0: class SkFontStyleSet_FC : public SkFontStyleSet { michael@0: public: michael@0: SkFontStyleSet_FC(FcPattern** matches, int count); michael@0: virtual ~SkFontStyleSet_FC(); michael@0: michael@0: virtual int count() SK_OVERRIDE { return fRecCount; } michael@0: virtual void getStyle(int index, SkFontStyle*, SkString* style) SK_OVERRIDE; michael@0: virtual SkTypeface* createTypeface(int index) SK_OVERRIDE; michael@0: virtual SkTypeface* matchStyle(const SkFontStyle& pattern) SK_OVERRIDE; michael@0: michael@0: private: michael@0: struct Rec { michael@0: SkString fStyleName; michael@0: SkString fFileName; michael@0: SkFontStyle fStyle; michael@0: }; michael@0: Rec* fRecs; michael@0: int fRecCount; michael@0: }; michael@0: michael@0: static int map_range(int value, michael@0: int old_min, int old_max, int new_min, int new_max) { michael@0: SkASSERT(old_min < old_max); michael@0: SkASSERT(new_min < new_max); michael@0: return new_min + SkMulDiv(value - old_min, michael@0: new_max - new_min, old_max - old_min); michael@0: } michael@0: michael@0: static SkFontStyle make_fontconfig_style(FcPattern* match) { michael@0: int weight = get_int(match, FC_WEIGHT); michael@0: int width = get_int(match, FC_WIDTH); michael@0: int slant = get_int(match, FC_SLANT); michael@0: // SkDebugf("old weight %d new weight %d\n", weight, map_range(weight, 0, 80, 0, 400)); michael@0: michael@0: // fontconfig weight seems to be 0..200 or so, so we remap it here michael@0: weight = map_range(weight, 0, 80, 0, 400); michael@0: width = map_range(width, 0, 200, 0, 9); michael@0: return SkFontStyle(weight, width, slant > 0 ? SkFontStyle::kItalic_Slant michael@0: : SkFontStyle::kUpright_Slant); michael@0: } michael@0: michael@0: SkFontStyleSet_FC::SkFontStyleSet_FC(FcPattern** matches, int count) { michael@0: fRecCount = count; michael@0: fRecs = SkNEW_ARRAY(Rec, count); michael@0: for (int i = 0; i < count; ++i) { michael@0: fRecs[i].fStyleName.set(get_name(matches[i], FC_STYLE)); michael@0: fRecs[i].fFileName.set(get_name(matches[i], FC_FILE)); michael@0: fRecs[i].fStyle = make_fontconfig_style(matches[i]); michael@0: } michael@0: } michael@0: michael@0: SkFontStyleSet_FC::~SkFontStyleSet_FC() { michael@0: SkDELETE_ARRAY(fRecs); michael@0: } michael@0: michael@0: void SkFontStyleSet_FC::getStyle(int index, SkFontStyle* style, michael@0: SkString* styleName) { michael@0: SkASSERT((unsigned)index < (unsigned)fRecCount); michael@0: if (style) { michael@0: *style = fRecs[index].fStyle; michael@0: } michael@0: if (styleName) { michael@0: *styleName = fRecs[index].fStyleName; michael@0: } michael@0: } michael@0: michael@0: SkTypeface* SkFontStyleSet_FC::createTypeface(int index) { michael@0: return NULL; michael@0: } michael@0: michael@0: SkTypeface* SkFontStyleSet_FC::matchStyle(const SkFontStyle& pattern) { michael@0: return NULL; michael@0: } michael@0: michael@0: class SkFontMgr_fontconfig : public SkFontMgr { michael@0: SkAutoTUnref fFCI; michael@0: SkDataTable* fFamilyNames; michael@0: michael@0: michael@0: public: michael@0: SkFontMgr_fontconfig(SkFontConfigInterface* fci) michael@0: : fFCI(fci) michael@0: , fFamilyNames(fFCI->getFamilyNames()) {} michael@0: michael@0: virtual ~SkFontMgr_fontconfig() { michael@0: SkSafeUnref(fFamilyNames); michael@0: } michael@0: michael@0: protected: michael@0: virtual int onCountFamilies() const SK_OVERRIDE { michael@0: return fFamilyNames->count(); michael@0: } michael@0: michael@0: virtual void onGetFamilyName(int index, SkString* familyName) const SK_OVERRIDE { michael@0: familyName->set(fFamilyNames->atStr(index)); michael@0: } michael@0: michael@0: virtual SkFontStyleSet* onCreateStyleSet(int index) const SK_OVERRIDE { michael@0: return this->onMatchFamily(fFamilyNames->atStr(index)); michael@0: } michael@0: michael@0: virtual SkFontStyleSet* onMatchFamily(const char familyName[]) const SK_OVERRIDE { michael@0: FcPattern* pattern = FcPatternCreate(); michael@0: michael@0: FcPatternAddString(pattern, FC_FAMILY, (FcChar8*)familyName); michael@0: #if 0 michael@0: FcPatternAddBool(pattern, FC_SCALABLE, FcTrue); michael@0: #endif michael@0: FcConfigSubstitute(NULL, pattern, FcMatchPattern); michael@0: FcDefaultSubstitute(pattern); michael@0: michael@0: const char* post_config_family = get_name(pattern, FC_FAMILY); michael@0: michael@0: FcResult result; michael@0: FcFontSet* font_set = FcFontSort(0, pattern, 0, 0, &result); michael@0: if (!font_set) { michael@0: FcPatternDestroy(pattern); michael@0: return NULL; michael@0: } michael@0: michael@0: int count; michael@0: FcPattern** match = MatchFont(font_set, post_config_family, &count); michael@0: if (!match) { michael@0: FcPatternDestroy(pattern); michael@0: FcFontSetDestroy(font_set); michael@0: return NULL; michael@0: } michael@0: michael@0: FcPatternDestroy(pattern); michael@0: michael@0: SkTDArray trimmedMatches; michael@0: for (int i = 0; i < count; ++i) { michael@0: const char* justName = find_just_name(get_name(match[i], FC_FILE)); michael@0: if (!is_lower(*justName)) { michael@0: *trimmedMatches.append() = match[i]; michael@0: } michael@0: } michael@0: michael@0: SkFontStyleSet_FC* sset = SkNEW_ARGS(SkFontStyleSet_FC, michael@0: (trimmedMatches.begin(), michael@0: trimmedMatches.count())); michael@0: return sset; michael@0: } michael@0: michael@0: virtual SkTypeface* onMatchFamilyStyle(const char familyName[], michael@0: const SkFontStyle&) const SK_OVERRIDE { return NULL; } michael@0: virtual SkTypeface* onMatchFaceStyle(const SkTypeface*, michael@0: const SkFontStyle&) const SK_OVERRIDE { return NULL; } michael@0: michael@0: virtual SkTypeface* onCreateFromData(SkData*, int ttcIndex) const SK_OVERRIDE { return NULL; } michael@0: michael@0: virtual SkTypeface* onCreateFromStream(SkStream* stream, int ttcIndex) const SK_OVERRIDE { michael@0: const size_t length = stream->getLength(); michael@0: if (!length) { michael@0: return NULL; michael@0: } michael@0: if (length >= 1024 * 1024 * 1024) { michael@0: return NULL; // don't accept too large fonts (>= 1GB) for safety. michael@0: } michael@0: michael@0: // TODO should the caller give us the style or should we get it from freetype? michael@0: SkTypeface::Style style = SkTypeface::kNormal; michael@0: bool isFixedWidth = false; michael@0: if (!find_name_and_attributes(stream, NULL, &style, &isFixedWidth)) { michael@0: return NULL; michael@0: } michael@0: michael@0: SkTypeface* face = SkNEW_ARGS(FontConfigTypeface, (style, isFixedWidth, stream)); michael@0: return face; 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: return FontConfigTypeface::LegacyCreateTypeface(NULL, familyName, michael@0: (SkTypeface::Style)styleBits); michael@0: } michael@0: }; michael@0: michael@0: SkFontMgr* SkFontMgr::Factory() { michael@0: SkFontConfigInterface* fci = RefFCI(); michael@0: return fci ? SkNEW_ARGS(SkFontMgr_fontconfig, (fci)) : NULL; michael@0: }