Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(37)

Unified Diff: src/ports/SkFontMgr_fontconfig.cpp

Issue 396143004: Add a working SkFontMgr_fontconfig. (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Created 6 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: src/ports/SkFontMgr_fontconfig.cpp
diff --git a/src/ports/SkFontMgr_fontconfig.cpp b/src/ports/SkFontMgr_fontconfig.cpp
new file mode 100644
index 0000000000000000000000000000000000000000..d5801563bed9d6ca53d9a8868fe498b8595c022c
--- /dev/null
+++ b/src/ports/SkFontMgr_fontconfig.cpp
@@ -0,0 +1,783 @@
+/*
+ * Copyright 2014 Google Inc.
+ *
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+#include "SkDataTable.h"
+#include "SkFontDescriptor.h"
+#include "SkFontHost_FreeType_common.h"
+#include "SkFontMgr.h"
+#include "SkFontStyle.h"
+#include "SkMath.h"
+#include "SkString.h"
+#include "SkStream.h"
+#include "SkTDArray.h"
+#include "SkThread.h"
+#include "SkTypefaceCache.h"
+#include "SkOSFile.h"
+
+#include <fontconfig/fontconfig.h>
+
+#ifdef SK_DEBUG
+# include "SkTLS.h"
+#endif
+
+/** Since FontConfig is poorly documented, a short overview primer:
+ *
+ * FcConfig is a library state. There exists a default global state which is affected by
tomhudson 2014/07/17 21:14:46 Nit: Not sure what you mean by a "library state".
bungeman-skia 2014/07/17 22:08:27 Yeah, I wan't entirely sure what to call it. FreeT
+ * FcInit and FcFini, but this default should not normally be used.
+ * Instead, one should use FcConfigCreate and FcInit* to have a named local state.
+ *
+ * FcPatterns are {objectName -> [element]} (maps from object names to a list of elements).
+ * Each element is some custom data plus an FcValue which is a variant (a union with a type tag).
+ * Any documentation which states that a given object maps to a given type actually means that
+ * it is expected that all the values in the list mapped to by objectName will be of that type.
+ * However, there is actually no real enforcement.
tomhudson 2014/07/17 21:14:46 Nit: The previous sentence is just a bit awkward,
bungeman-skia 2014/07/17 22:08:27 Hmmm... yes. The list isn't typed at all, except b
+ *
+ * Somewhat like DirectWrite, FontConfig supports synthetics through FC_EMBOLDEN and FC_MATRIX.
+ * Like all synthetic information, such information must be passed with the font data.
+ */
+
+namespace {
+
+// Fontconfig is not threadsafe before 2.10.91. Before that, we lock with a global mutex.
+// See skia:1497 for background.
tomhudson 2014/07/17 21:14:46 Nit: please spell out the link to http://skbug.com
bungeman-skia 2014/07/17 22:08:27 Done.
+SK_DECLARE_STATIC_MUTEX(gFCMutex);
+
+#ifdef SK_DEBUG
+ void *CreateThreadFcLocked() { return SkNEW(bool); }
+ void DeleteThreadFcLocked(void* v) { SkDELETE(reinterpret_cast<bool*>(v)); }
+# define THREAD_FC_LOCKED \
+ reinterpret_cast<bool*>(SkTLS::Get(CreateThreadFcLocked, DeleteThreadFcLocked))
+#endif
+
+struct FCLocker {
+ // Assume FcGetVersion() has always been thread safe.
+
+ FCLocker() {
+ if (FcGetVersion() < 21091) {
+ gFCMutex.acquire();
+ fUnlock = true;
+ } else {
+ SkDEBUGCODE(bool* threadLocked = THREAD_FC_LOCKED);
+ SkASSERT(0 == *threadLocked);
+ SkDEBUGCODE(*threadLocked = true);
+ }
+ }
+
+ ~FCLocker() {
+ if (FcGetVersion() < 21091) {
+ if (fUnlock) {
+ gFCMutex.release();
+ }
+ } else {
+ SkDEBUGCODE(bool* threadLocked = THREAD_FC_LOCKED);
+ SkASSERT(1 == *threadLocked);
+ SkDEBUGCODE(*threadLocked = false);
+ }
+ }
+
+ static void AssertHeld() { SkDEBUGCODE(
+ if (FcGetVersion() < 21091) {
+ gFCMutex.assertHeld();
+ } else {
+ SkASSERT(true == *THREAD_FC_LOCKED);
+ }
+ ) }
+
+private:
+ bool fUnlock;
+};
+
+} // namespace
+
+template<typename T, void (*P)(T*)> void FcTDestroy(T* t) {
+ FCLocker::AssertHeld();
+ P(t);
+}
+typedef SkAutoTCallVProc<FcConfig, FcTDestroy<FcConfig, FcConfigDestroy> > SkAutoFcConfig;
+typedef SkAutoTCallVProc<FcFontSet, FcTDestroy<FcFontSet, FcFontSetDestroy> > SkAutoFcFontSet;
+typedef SkAutoTCallVProc<FcLangSet, FcTDestroy<FcLangSet, FcLangSetDestroy> > SkAutoFcLangSet;
+typedef SkAutoTCallVProc<FcObjectSet, FcTDestroy<FcObjectSet, FcObjectSetDestroy> > SkAutoFcObjectSet;
+typedef SkAutoTCallVProc<FcPattern, FcTDestroy<FcPattern, FcPatternDestroy> > SkAutoFcPattern;
+
+static int get_int(FcPattern* pattern, const char object[], int missing) {
+ int value;
+ if (FcPatternGetInteger(pattern, object, 0, &value) != FcResultMatch) {
+ return missing;
+ }
+ return value;
+}
+
+static const char* get_string(FcPattern* pattern, const char object[], const char* missing = "") {
+ FcChar8* value;
+ if (FcPatternGetString(pattern, object, 0, &value) != FcResultMatch) {
+ return missing;
+ }
+ return (const char*)value;
+}
+
+enum is_weak_return {
+ isWeak, isStrong, noId
+};
+/** Ideally there would exist a call like
+ * FcResult FcPatternIsWeak(pattern, object, id, FcBool* isWeak);
+ *
+ * However, there is no such call and as of Fc 2.11.0 even FcPatternEquals ignores the weak bit.
+ * Currently, the only reliable way of finding the weak bit is by its effect on matching.
+ * The weak bit only affects the matching of FC_FAMILY and FC_POSTSCRIPT_NAME object values.
+ * A element with the weak bit is scored after FC_LANG, without the weak bit is scored before.
+ * Note that the weak bit is stored on the element, not on the value it holds.
+ */
+static is_weak_return is_weak(FcPattern* pattern, const char object[], int id) {
+ FCLocker::AssertHeld();
+
+ FcResult result;
+
+ // Create a copy of the pattern with only the value 'pattern'['object'['id']] in it.
+ // Internally, FontConfig pattern objects are linked lists, so faster to remove from head.
+ SkAutoFcObjectSet requestedObjectOnly(FcObjectSetBuild(object, NULL));
+ SkAutoFcPattern minimal(FcPatternFilter(pattern, requestedObjectOnly));
+ FcBool hasId = true;
+ for (int i = 0; hasId && i < id; ++i) {
+ hasId = FcPatternRemove(minimal, object, 0);
+ }
+ if (!hasId) {
+ return noId;
+ }
+ FcValue value;
+ result = FcPatternGet(minimal, object, 0, &value);
+ if (result != FcResultMatch) {
+ return noId;
+ }
+ while (hasId) {
+ hasId = FcPatternRemove(minimal, object, 1);
+ }
+
+ // Create a font set with two patterns.
+ // 1. the same 'object' as minimal and a lang object with only 'nomatchlang'.
+ // 2. a different 'object' from minimal and a lang object with only 'matchlang'.
+ SkAutoFcFontSet fontSet(FcFontSetCreate());
+
+ SkAutoFcLangSet strongLangSet(FcLangSetCreate());
+ FcLangSetAdd(strongLangSet, (const FcChar8*)"nomatchlang");
+ SkAutoFcPattern strong(FcPatternDuplicate(minimal));
+ FcPatternAddLangSet(strong, FC_LANG, strongLangSet);
+
+ SkAutoFcLangSet weakLangSet(FcLangSetCreate());
+ FcLangSetAdd(weakLangSet, (const FcChar8*)"matchlang");
+ SkAutoFcPattern weak(FcPatternCreate());
+ FcPatternAddString(weak, object, (const FcChar8*)"nomatchstring");
+ FcPatternAddLangSet(weak, FC_LANG, weakLangSet);
+
+ FcFontSetAdd(fontSet, strong);
+ FcFontSetAdd(fontSet, weak);
+
+ // Add 'matchlang' to the copy of the pattern.
+ FcPatternAddLangSet(minimal, FC_LANG, weakLangSet);
+
+ // Run a match against the copy of the pattern.
+ // If the 'id' was weak, then we should match the pattern with 'matchlang'.
+ // If the 'id' was strong, then we should match the pattern with 'nomatchlang'.
+
+ // Note that this config is only used for FcFontRenderPrepare, which we don't even want.
+ // However, there appears to be no way to match/sort without it.
+ SkAutoFcConfig config(FcConfigCreate());
+ SkAutoFcPattern match(FcFontSetMatch(config, &fontSet, 1, minimal, &result));
+
+ FcLangSet* matchLangSet;
+ FcPatternGetLangSet(match, FC_LANG, 0, &matchLangSet);
+ return FcLangEqual == FcLangSetHasLang(matchLangSet, (const FcChar8*)"matchlang")
+ ? isWeak : isStrong;
+}
+
+/** Removes weak elements from either FC_FAMILY or FC_POSTSCRIPT_NAME objects in the property.
+ * This can be quite expensive, and should not be used more than once per font lookup.
+ * This removes all of the weak elements after the last strong element.
+ */
+static void remove_weak(FcPattern* pattern, const char object[]) {
+ FCLocker::AssertHeld();
+
+ SkAutoFcObjectSet requestedObjectOnly(FcObjectSetBuild(object, NULL));
+ SkAutoFcPattern minimal(FcPatternFilter(pattern, requestedObjectOnly));
+
+ int lastStrongId = -1;
+ int numIds;
+ is_weak_return result;
+ for (int id = 0; ; ++id) {
+ result = is_weak(minimal, object, 0);
+ if (noId == result) {
+ numIds = id;
+ break;
+ }
+ if (isStrong == result) {
+ lastStrongId = id;
+ }
+ SkAssertResult(FcPatternRemove(minimal, object, 0));
+ }
+
+ // If they were all weak, then leave the pattern alone.
+ if (lastStrongId < 0) {
+ return;
+ }
+
+ // Remove everything after the last strong.
+ for (int id = lastStrongId + 1; id < numIds; ++id) {
+ SkAssertResult(FcPatternRemove(pattern, object, lastStrongId + 1));
+ }
+}
+
+static int map_range(SkFixed value,
+ SkFixed old_min, SkFixed old_max,
+ SkFixed new_min, SkFixed new_max)
+{
+ SkASSERT(old_min < old_max);
+ SkASSERT(new_min <= new_max);
+ return new_min + SkMulDiv(value - old_min, new_max - new_min,
+ old_max - old_min);
+}
+
+static int ave(SkFixed a, SkFixed b) {
+ return SkFixedAve(a, b);
+}
+
+struct MapRanges {
+ SkFixed old_val;
+ SkFixed new_val;
+};
+
+static SkFixed map_ranges_fixed(SkFixed val, MapRanges const ranges[], int rangesCount) {
+ // -Inf to [0]
+ if (val < ranges[0].old_val)
+ return ranges[0].new_val;
+
+ // Linear from [i] to ave([i], [i+1]), then from ave([i], [i+1]) to [i+1]
+ for (int i = 0; i < rangesCount - 1; ++i) {
+ if (val < ave(ranges[i].old_val, ranges[i+1].old_val))
+ return map_range(val, ranges[i].old_val, ave(ranges[i].old_val, ranges[i+1].old_val),
+ ranges[i].new_val, ave(ranges[i].new_val, ranges[i+1].new_val));
+ if (val < ranges[i+1].old_val)
+ return map_range(val, ave(ranges[i].old_val, ranges[i+1].old_val), ranges[i+1].old_val,
+ ave(ranges[i].new_val, ranges[i+1].new_val), ranges[i+1].new_val);
+ }
+
+ // From [n] to +Inf
+ // if (fcweight < Inf)
+ return ranges[rangesCount-1].new_val;
+}
+
+static int map_ranges(int val, MapRanges const ranges[], int rangesCount) {
+ return SkFixedRoundToInt(map_ranges_fixed(SkIntToFixed(val), ranges, rangesCount));
+}
+
+template<int n> struct SkTIntToFixed {
+ SK_COMPILE_ASSERT(-32768 <= n && n <= 32767, SkTIntToFixed_n_not_in_range);
+ static const SkFixed value = static_cast<SkFixed>(n << 16);
+};
+
+static SkFontStyle skfontstyle_from_fcpattern(FcPattern* pattern) {
tomhudson 2014/07/17 21:14:46 Nit: line lengths?
bungeman-skia 2014/07/17 22:08:27 Done.
+ static const MapRanges weightRanges[] = {
+ { SkTIntToFixed<FC_WEIGHT_THIN>::value, SkTIntToFixed<SkFontStyle::kThin_Weight>::value },
+ { SkTIntToFixed<FC_WEIGHT_EXTRALIGHT>::value, SkTIntToFixed<SkFontStyle::kExtraLight_Weight>::value },
+ { SkTIntToFixed<FC_WEIGHT_LIGHT>::value, SkTIntToFixed<SkFontStyle::kLight_Weight>::value },
+ { SkTIntToFixed<FC_WEIGHT_REGULAR>::value, SkTIntToFixed<SkFontStyle::kNormal_Weight>::value },
+ { SkTIntToFixed<FC_WEIGHT_MEDIUM>::value, SkTIntToFixed<SkFontStyle::kMedium_Weight>::value },
+ { SkTIntToFixed<FC_WEIGHT_DEMIBOLD>::value, SkTIntToFixed<SkFontStyle::kSemiBold_Weight>::value },
+ { SkTIntToFixed<FC_WEIGHT_BOLD>::value, SkTIntToFixed<SkFontStyle::kBold_Weight>::value },
+ { SkTIntToFixed<FC_WEIGHT_EXTRABOLD>::value, SkTIntToFixed<SkFontStyle::kExtraBold_Weight>::value },
+ { SkTIntToFixed<FC_WEIGHT_BLACK>::value, SkTIntToFixed<SkFontStyle::kBlack_Weight>::value },
+ { SkTIntToFixed<FC_WEIGHT_EXTRABLACK>::value, SkTIntToFixed<1000>::value },
+ };
+ int weight = map_ranges(get_int(pattern, FC_WEIGHT, FC_WEIGHT_REGULAR),
+ weightRanges, SK_ARRAY_COUNT(weightRanges));
+
+ static const MapRanges widthRanges[] = {
+ { SkTIntToFixed<FC_WIDTH_ULTRACONDENSED>::value, SkTIntToFixed<SkFontStyle::kUltraCondensed_Width>::value },
+ { SkTIntToFixed<FC_WIDTH_EXTRACONDENSED>::value, SkTIntToFixed<SkFontStyle::kExtraCondensed_Width>::value },
+ { SkTIntToFixed<FC_WIDTH_CONDENSED>::value, SkTIntToFixed<SkFontStyle::kCondensed_Width>::value },
+ { SkTIntToFixed<FC_WIDTH_SEMICONDENSED>::value, SkTIntToFixed<SkFontStyle::kSemiCondensed_Width>::value },
+ { SkTIntToFixed<FC_WIDTH_NORMAL>::value, SkTIntToFixed<SkFontStyle::kNormal_Width>::value },
+ { SkTIntToFixed<FC_WIDTH_SEMIEXPANDED>::value, SkTIntToFixed<SkFontStyle::kSemiExpanded_Width>::value },
+ { SkTIntToFixed<FC_WIDTH_EXPANDED>::value, SkTIntToFixed<SkFontStyle::kExpanded_Width>::value },
+ { SkTIntToFixed<FC_WIDTH_EXTRAEXPANDED>::value, SkTIntToFixed<SkFontStyle::kExtraExpanded_Width>::value },
+ { SkTIntToFixed<FC_WIDTH_ULTRAEXPANDED>::value, SkTIntToFixed<SkFontStyle::kUltaExpanded_Width>::value },
+ };
+ int width = map_ranges(get_int(pattern, FC_WIDTH, FC_WIDTH_NORMAL),
+ widthRanges, SK_ARRAY_COUNT(widthRanges));
+
+ SkFontStyle::Slant slant = get_int(pattern, FC_SLANT, FC_SLANT_ROMAN) > 0
+ ? SkFontStyle::kItalic_Slant
+ : SkFontStyle::kUpright_Slant;
+
+ return SkFontStyle(weight, width, slant);
+}
+
+static void fcpattern_from_skfontstyle(SkFontStyle style, FcPattern* pattern) {
+ FCLocker::AssertHeld();
+
+ static const MapRanges weightRanges[] = {
+ { SkTIntToFixed<SkFontStyle::kThin_Weight>::value, SkTIntToFixed<FC_WEIGHT_THIN>::value },
+ { SkTIntToFixed<SkFontStyle::kExtraLight_Weight>::value, SkTIntToFixed<FC_WEIGHT_EXTRALIGHT>::value },
+ { SkTIntToFixed<SkFontStyle::kLight_Weight>::value, SkTIntToFixed<FC_WEIGHT_LIGHT>::value },
+ { SkTIntToFixed<SkFontStyle::kNormal_Weight>::value, SkTIntToFixed<FC_WEIGHT_REGULAR>::value },
+ { SkTIntToFixed<SkFontStyle::kMedium_Weight>::value, SkTIntToFixed<FC_WEIGHT_MEDIUM>::value },
+ { SkTIntToFixed<SkFontStyle::kSemiBold_Weight>::value, SkTIntToFixed<FC_WEIGHT_DEMIBOLD>::value },
+ { SkTIntToFixed<SkFontStyle::kBold_Weight>::value, SkTIntToFixed<FC_WEIGHT_BOLD>::value },
+ { SkTIntToFixed<SkFontStyle::kExtraBold_Weight>::value, SkTIntToFixed<FC_WEIGHT_EXTRABOLD>::value },
+ { SkTIntToFixed<SkFontStyle::kBlack_Weight>::value, SkTIntToFixed<FC_WEIGHT_BLACK>::value },
+ { SkTIntToFixed<1000>::value, SkTIntToFixed<FC_WEIGHT_EXTRABLACK>::value },
+ };
+ int weight = map_ranges(style.weight(), weightRanges, SK_ARRAY_COUNT(weightRanges));
+
+ static const MapRanges widthRanges[] = {
+ { SkTIntToFixed<SkFontStyle::kUltraCondensed_Width>::value, SkTIntToFixed<FC_WIDTH_ULTRACONDENSED>::value },
+ { SkTIntToFixed<SkFontStyle::kExtraCondensed_Width>::value, SkTIntToFixed<FC_WIDTH_EXTRACONDENSED>::value },
+ { SkTIntToFixed<SkFontStyle::kCondensed_Width>::value, SkTIntToFixed<FC_WIDTH_CONDENSED>::value },
+ { SkTIntToFixed<SkFontStyle::kSemiCondensed_Width>::value, SkTIntToFixed<FC_WIDTH_SEMICONDENSED>::value },
+ { SkTIntToFixed<SkFontStyle::kNormal_Width>::value, SkTIntToFixed<FC_WIDTH_NORMAL>::value },
+ { SkTIntToFixed<SkFontStyle::kSemiExpanded_Width>::value, SkTIntToFixed<FC_WIDTH_SEMIEXPANDED>::value },
+ { SkTIntToFixed<SkFontStyle::kExpanded_Width>::value, SkTIntToFixed<FC_WIDTH_EXPANDED>::value },
+ { SkTIntToFixed<SkFontStyle::kExtraExpanded_Width>::value, SkTIntToFixed<FC_WIDTH_EXTRAEXPANDED>::value },
+ { SkTIntToFixed<SkFontStyle::kUltaExpanded_Width>::value, SkTIntToFixed<FC_WIDTH_ULTRAEXPANDED>::value },
+ };
+ int width = map_ranges(style.width(), widthRanges, SK_ARRAY_COUNT(widthRanges));
+
+ FcPatternAddInteger(pattern, FC_WEIGHT, weight);
+ FcPatternAddInteger(pattern, FC_WIDTH, width);
+ FcPatternAddInteger(pattern, FC_SLANT, style.isItalic() ? FC_SLANT_ITALIC : FC_SLANT_ROMAN);
+}
+
+class SkTypeface_stream : public SkTypeface_FreeType {
+public:
+ /** @param stream does not ownership of the reference, does take ownership of the stream. */
+ SkTypeface_stream(SkTypeface::Style style, bool fixedWidth, int ttcIndex, SkStreamAsset* stream)
+ : INHERITED(style, SkTypefaceCache::NewFontID(), fixedWidth)
+ , fStream(SkRef(stream))
+ , fIndex(ttcIndex)
+ { };
+
+ virtual void onGetFontDescriptor(SkFontDescriptor* desc, bool* serialize) const SK_OVERRIDE {
+ *serialize = true;
+ }
+
+ virtual SkStream* onOpenStream(int* ttcIndex) const SK_OVERRIDE {
+ return fStream->duplicate();
+ }
+
+private:
+ SkAutoTUnref<SkStreamAsset> fStream;
+ int fIndex;
+
+ typedef SkTypeface_FreeType INHERITED;
+};
+
+class SkTypeface_fontconfig : public SkTypeface_FreeType {
+public:
+ /** @param pattern takes ownership of the reference. */
+ static SkTypeface_fontconfig* Create(FcPattern* pattern) {
+ return SkNEW_ARGS(SkTypeface_fontconfig, (pattern));
+ }
+ mutable SkAutoFcPattern fPattern;
+
+ virtual void onGetFontDescriptor(SkFontDescriptor* desc, bool* serialize) const SK_OVERRIDE {
+ FCLocker lock;
+ *serialize = false;
+ desc->setFamilyName(get_string(fPattern, FC_FAMILY));
+ desc->setFontFileName(get_string(fPattern, FC_FILE));
+ desc->setFullName(get_string(fPattern, FC_FULLNAME));
+ desc->setPostscriptName(get_string(fPattern, FC_POSTSCRIPT_NAME));
+ }
+
+ virtual SkStream* onOpenStream(int* ttcIndex) const SK_OVERRIDE {
+ FCLocker lock;
+ *ttcIndex = get_int(fPattern, FC_INDEX, 0);
+ return SkStream::NewFromFile(get_string(fPattern, FC_FILE));
+ }
+
+ virtual ~SkTypeface_fontconfig() {
+ // Hold the lock while unrefing the pattern.
+ FCLocker lock;
+ fPattern.reset();
+ }
+
+private:
+ static SkTypeface::Style TypefaceStyleFromFcPattern(FcPattern* pattern) {
+ int fcweight = get_int(pattern, FC_WEIGHT, FC_WEIGHT_REGULAR);
+ int fcslant = get_int(pattern, FC_SLANT, FC_SLANT_ROMAN);
+ return (SkTypeface::Style)((fcweight >= FC_WEIGHT_BOLD ? SkTypeface::kBold : 0) |
+ (fcslant > FC_SLANT_ROMAN ? SkTypeface::kItalic : 0));
+ }
+
+ /** @param pattern takes ownership of the reference. */
+ SkTypeface_fontconfig(FcPattern* pattern)
+ : INHERITED(TypefaceStyleFromFcPattern(pattern),
+ SkTypefaceCache::NewFontID(),
+ FC_PROPORTIONAL != get_int(pattern, FC_SPACING, FC_PROPORTIONAL))
+ , fPattern(pattern)
+ { };
+
+ typedef SkTypeface_FreeType INHERITED;
+};
+
+class SkFontMgr_fontconfig : public SkFontMgr {
+ mutable SkAutoFcConfig fFC;
+ SkAutoTUnref<SkDataTable> fFamilyNames;
+
+ class StyleSet : public SkFontStyleSet {
+ public:
+ /** @param parent does not take ownership of the reference.
+ * @param fontSet takes ownership of the reference.
+ */
+ StyleSet(const SkFontMgr_fontconfig* parent, FcFontSet* fontSet)
+ : fFontMgr(SkRef(parent)), fFontSet(fontSet)
+ { }
+
+ virtual ~StyleSet() {
+ // Hold the lock while unrefing the font set.
+ FCLocker lock;
+ fFontSet.reset();
+ }
+
+ virtual int count() SK_OVERRIDE { return fFontSet->nfont; }
+
+ virtual void getStyle(int index, SkFontStyle* style, SkString* styleName) SK_OVERRIDE {
+ if (index < 0 || fFontSet->nfont <= index) {
+ return;
+ }
+
+ FCLocker lock;
+ if (style) {
+ *style = skfontstyle_from_fcpattern(fFontSet->fonts[index]);
+ }
+ if (styleName) {
+ *styleName = get_string(fFontSet->fonts[index], FC_STYLE);
+ }
+ }
+
+ virtual SkTypeface* createTypeface(int index) SK_OVERRIDE {
+ FCLocker lock;
+
+ FcPattern* match = fFontSet->fonts[index];
+ return fFontMgr->createTypefaceFromFcPattern(match);
+ }
+
+ virtual SkTypeface* matchStyle(const SkFontStyle& style) SK_OVERRIDE {
+ FCLocker lock;
+
+ SkAutoFcPattern pattern(FcPatternCreate());
+ if (NULL == pattern) {
+ return NULL;
+ }
+
+ fcpattern_from_skfontstyle(style, pattern);
+ FcConfigSubstitute(fFontMgr->fFC, pattern, FcMatchPattern);
+ FcDefaultSubstitute(pattern);
+
+ FcResult result;
+ SkAutoFcPattern match(FcFontSetMatch(fFontMgr->fFC, &fFontSet, 1, pattern, &result));
+ if (NULL == match) {
+ return NULL;
+ }
+
+ return fFontMgr->createTypefaceFromFcPattern(match);
+ }
+
+ private:
+ SkAutoTUnref<const SkFontMgr_fontconfig> fFontMgr;
+ SkAutoFcFontSet fFontSet;
+ };
+
+ static bool find_name(const SkTDArray<const char*>& list, const char* str) {
+ int count = list.count();
+ for (int i = 0; i < count; ++i) {
+ if (!strcmp(list[i], str)) {
+ return true;
+ }
+ }
+ return false;
+ }
+
+ SkDataTable* get_family_names() {
+ FCLocker lock;
+
+ SkTDArray<const char*> names;
+ SkTDArray<size_t> sizes;
+
+ static const FcSetName fcNameSet[] = { FcSetSystem, FcSetApplication };
+ for (int setIndex = 0; setIndex < (int)SK_ARRAY_COUNT(fcNameSet); ++setIndex) {
+ // Return value of FcConfigGetFonts must not be destroyed.
+ FcFontSet* allFonts(FcConfigGetFonts(fFC, fcNameSet[setIndex]));
+ if (NULL == allFonts) {
+ continue;
+ }
+
+ for (int fontIndex = 0; fontIndex < allFonts->nfont; ++fontIndex) {
+ FcPattern* current = allFonts->fonts[fontIndex];
+ for (int id = 0; ; ++id) {
+ FcChar8* fcFamilyName;
+ FcResult result = FcPatternGetString(current, FC_FAMILY, id, &fcFamilyName);
+ if (FcResultNoId == result) {
+ break;
+ }
+ if (FcResultMatch != result) {
+ continue;
+ }
+ const char* familyName = reinterpret_cast<const char*>(fcFamilyName);
+ if (familyName && !find_name(names, familyName)) {
+ *names.append() = familyName;
+ *sizes.append() = strlen(familyName) + 1;
+ }
+ }
+ }
+ }
+
+ return SkDataTable::NewCopyArrays((void *const *const)names.begin(),
+ sizes.begin(), names.count());
+ }
+
+ static bool find_by_fc_pattern(SkTypeface* cached, SkTypeface::Style, void* ctx) {
+ SkTypeface_fontconfig* cshFace = reinterpret_cast<SkTypeface_fontconfig*>(cached);
+ FcPattern* ctxPattern = reinterpret_cast<FcPattern*>(ctx);
+ return FcTrue == FcPatternEqual(cshFace->fPattern, ctxPattern);
+ }
+
+ mutable SkMutex fTFCacheMutex;
+ mutable SkTypefaceCache fTFCache;
+ /** Creates a typeface using a typeface cache.
+ * @param pattern a complete pattern from FcFontRenderPrepare.
+ */
+ SkTypeface* createTypefaceFromFcPattern(FcPattern* pattern) const {
+ FCLocker::AssertHeld();
+ SkAutoMutexAcquire ama(fTFCacheMutex);
+ SkTypeface* face = fTFCache.findByProcAndRef(find_by_fc_pattern, pattern);
+ if (NULL == face) {
+ FcPatternReference(pattern);
+ face = SkTypeface_fontconfig::Create(pattern);
+ if (face) {
+ fTFCache.add(face, SkTypeface::kNormal, true);
+ }
+ }
+ return face;
+ }
+
+public:
+ SkFontMgr_fontconfig()
+ : fFC(FcInitLoadConfigAndFonts())
+ , fFamilyNames(get_family_names()) { }
+
+ /** Takes control of the reference to 'config'. */
+ explicit SkFontMgr_fontconfig(FcConfig* config)
+ : fFC(config)
+ , fFamilyNames(get_family_names()) { }
+
+ virtual ~SkFontMgr_fontconfig() {
+ // Hold the lock while unrefing the config.
+ FCLocker lock;
+ fFC.reset();
+ }
+
+protected:
+ virtual int onCountFamilies() const SK_OVERRIDE {
+ return fFamilyNames->count();
+ }
+
+ virtual void onGetFamilyName(int index, SkString* familyName) const SK_OVERRIDE {
+ familyName->set(fFamilyNames->atStr(index));
+ }
+
+ virtual SkFontStyleSet* onCreateStyleSet(int index) const SK_OVERRIDE {
+ return this->onMatchFamily(fFamilyNames->atStr(index));
+ }
+
+ /** True if any string object value in the font is the same
+ * as a string object value in the pattern.
+ */
+ static bool any_matching(FcPattern* font, FcPattern* pattern, const char* object) {
+ FcChar8* fontString;
+ FcChar8* patternString;
+ FcResult result;
+ // Set an arbitrary limit on the number of pattern object values to consider.
+ static const int maxId = 16;
+ for (int patternId = 0; patternId < maxId; ++patternId) {
+ result = FcPatternGetString(pattern, object, patternId, &patternString);
+ if (FcResultNoId == result) {
+ break;
+ }
+ if (FcResultMatch != result) {
+ continue;
+ }
+ for (int fontId = 0; fontId < maxId; ++fontId) {
+ result = FcPatternGetString(font, object, fontId, &fontString);
+ if (FcResultNoId == result) {
+ break;
+ }
+ if (FcResultMatch != result) {
+ continue;
+ }
+ if (0 == FcStrCmpIgnoreCase(patternString, fontString)) {
+ return true;
+ }
+ }
+ }
+ return false;
+ }
+
+ static bool valid_pattern(FcPattern* pattern) {
+ // FontConfig can return fonts which are unreadable.
+ const char* filename = get_string(pattern, FC_FILE, NULL);
+ if (NULL == filename) {
+ return false;
+ }
+ return sk_exists(filename, kRead_SkFILE_Flag);
+ }
+
+ static bool font_matches(FcPattern* font, FcPattern* pattern) {
+ return valid_pattern(font) && any_matching(font, pattern, FC_FAMILY);
+ }
+
+ virtual SkFontStyleSet* onMatchFamily(const char familyName[]) const SK_OVERRIDE {
+ FCLocker lock;
+
+ SkAutoFcPattern pattern(FcPatternCreate());
+ if (NULL == pattern) {
+ return NULL;
+ }
+
+ FcPatternAddString(pattern, FC_FAMILY, (FcChar8*)familyName);
+ FcConfigSubstitute(fFC, pattern, FcMatchPattern);
+ FcDefaultSubstitute(pattern);
+
+ FcPattern* matchPattern;
+ SkAutoFcPattern strongPattern(NULL);
+ if (familyName) {
+ strongPattern.reset(FcPatternDuplicate(pattern));
+ remove_weak(strongPattern, FC_FAMILY);
+ matchPattern = strongPattern;
+ } else {
+ matchPattern = pattern;
+ }
+
+ SkAutoFcFontSet matches(FcFontSetCreate());
+ // TODO: Some families have 'duplicates' due to symbolic links.
+ // The patterns are exactly the same except for the FC_FILE.
+ // It should be possible to collapse these patterns by normalizing.
+ static const FcSetName fcNameSet[] = { FcSetSystem, FcSetApplication };
+ for (int setIndex = 0; setIndex < (int)SK_ARRAY_COUNT(fcNameSet); ++setIndex) {
+ // Return value of FcConfigGetFonts must not be destroyed.
+ FcFontSet* allFonts(FcConfigGetFonts(fFC, fcNameSet[setIndex]));
+ if (NULL == allFonts) {
+ continue;
+ }
+
+ for (int fontIndex = 0; fontIndex < allFonts->nfont; ++fontIndex) {
+ if (font_matches(allFonts->fonts[fontIndex], matchPattern)) {
+ FcFontSetAdd(matches,
+ FcFontRenderPrepare(fFC, pattern, allFonts->fonts[fontIndex]));
+ }
+ }
+ }
+
+ return SkNEW_ARGS(StyleSet, (this, matches.detach()));
+ }
+
+ virtual SkTypeface* onMatchFamilyStyle(const char familyName[],
+ const SkFontStyle& style) const SK_OVERRIDE
+ {
+ FCLocker lock;
+
+ SkAutoFcPattern pattern(FcPatternCreate());
+ if (NULL == pattern) {
+ return NULL;
+ }
+
+ FcPatternAddString(pattern, FC_FAMILY, (FcChar8*)familyName);
+ fcpattern_from_skfontstyle(style, pattern);
+ FcConfigSubstitute(fFC, pattern, FcMatchPattern);
+ FcDefaultSubstitute(pattern);
+
+ // We really want to match strong (prefered) and same (acceptable) only here.
+ // If a family name was specified, assume that any weak matches after the last strong match
+ // are weak (default) and ignore them.
+ // The reason for is that after substitution the pattern for 'sans-serif' looks like
+ // "wwwwwwwwwwwwwwswww" where there are many weak but preferred names, followed by defaults.
+ // So it is possible to have weakly matching but preferred names.
+ // In aliases, bindings are weak by default, so this is easy and common.
+ // If no family name was specified, we'll probably only get weak matches, but that's ok.
+ FcPattern* matchPattern;
+ SkAutoFcPattern strongPattern(NULL);
+ if (familyName) {
+ strongPattern.reset(FcPatternDuplicate(pattern));
+ remove_weak(strongPattern, FC_FAMILY);
+ matchPattern = strongPattern;
+ } else {
+ matchPattern = pattern;
+ }
+
+ FcResult result;
+ SkAutoFcPattern match(FcFontMatch(fFC, pattern, &result));
+ if (NULL == match || !font_matches(match, matchPattern)) {
+ return NULL;
+ }
+
+ return createTypefaceFromFcPattern(match);
+ }
+
+ virtual SkTypeface* onMatchFaceStyle(const SkTypeface* typeface,
+ const SkFontStyle& style) const SK_OVERRIDE
+ {
+ //TODO: should the SkTypeface_fontconfig know its family?
+ const SkTypeface_fontconfig* fcTypeface =
+ reinterpret_cast<const SkTypeface_fontconfig*>(typeface);
+ return this->matchFamilyStyle(get_string(fcTypeface->fPattern, FC_FAMILY), style);
+ }
+
+ /** @param stream does not take ownership of the reference. */
+ virtual SkTypeface* onCreateFromStream(SkStream* stream, int ttcIndex) const SK_OVERRIDE {
+ const size_t length = stream->getLength();
+ if (!(0 < length && length < (1u << 30))) {
+ return NULL;
+ }
+
+ SkTypeface::Style style = SkTypeface::kNormal;
+ bool isFixedWidth = false;
+ if (!SkTypeface_FreeType::ScanFont(stream, ttcIndex, NULL, &style, &isFixedWidth)) {
+ return NULL;
+ }
+
+ return SkNEW_ARGS(SkTypeface_stream, (style, isFixedWidth, ttcIndex,
+ reinterpret_cast<SkStreamAsset*>(stream)));
+ }
+
+ virtual SkTypeface* onCreateFromData(SkData* data, int ttcIndex) const SK_OVERRIDE {
+ SkAutoTUnref<SkStreamAsset> stream(SkNEW_ARGS(SkMemoryStream, (data)));
+ return this->createFromStream(stream, ttcIndex);
+ }
+
+ virtual SkTypeface* onCreateFromFile(const char path[], int ttcIndex) const SK_OVERRIDE {
+ SkAutoTUnref<SkStreamAsset> stream(SkStream::NewFromFile(path));
+ return this->createFromStream(stream, ttcIndex);
+ }
+
+ virtual SkTypeface* onLegacyCreateTypeface(const char familyName[],
+ unsigned styleBits) const SK_OVERRIDE {
+ bool bold = styleBits & SkTypeface::kBold;
+ bool italic = styleBits & SkTypeface::kItalic;
+ SkFontStyle style = SkFontStyle(bold ? SkFontStyle::kBold_Weight
+ : SkFontStyle::kNormal_Weight,
+ SkFontStyle::kNormal_Width,
+ italic ? SkFontStyle::kItalic_Slant
+ : SkFontStyle::kUpright_Slant);
+ SkAutoTUnref<SkTypeface> typeface(this->matchFamilyStyle(familyName, style));
+ if (NULL != typeface.get()) {
+ return typeface.detach();
+ }
+
+ return this->matchFamilyStyle(NULL, style);
+ }
+};
+
+SkFontMgr* SkFontMgr::Factory() {
+ return SkNEW_ARGS(SkFontMgr_fontconfig, ());
+}

Powered by Google App Engine
This is Rietveld 408576698