static AliasStrength strengthOfFirstAlias(const FcPattern& original) { // 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. FcValue value; FcResult result = FcPatternGet(&original, FC_FAMILY, 0, &value); if (result != FcResultMatch) return AliasStrength::Done; RefPtr<FcPattern> pattern = adoptRef(FcPatternDuplicate(&original)); FcBool hasMultipleFamilies = true; while (hasMultipleFamilies) hasMultipleFamilies = FcPatternRemove(pattern.get(), FC_FAMILY, 1); // Create a font set with two patterns. // 1. the same FC_FAMILY as pattern and a lang object with only 'nomatchlang'. // 2. a different FC_FAMILY from pattern and a lang object with only 'matchlang'. FcUniquePtr<FcFontSet> fontSet(FcFontSetCreate()); FcUniquePtr<FcLangSet> strongLangSet(FcLangSetCreate()); FcLangSetAdd(strongLangSet.get(), reinterpret_cast<const FcChar8*>("nomatchlang")); // Ownership of this FcPattern will be transferred with FcFontSetAdd. FcPattern* strong = FcPatternDuplicate(pattern.get()); FcPatternAddLangSet(strong, FC_LANG, strongLangSet.get()); FcUniquePtr<FcLangSet> weakLangSet(FcLangSetCreate()); FcLangSetAdd(weakLangSet.get(), reinterpret_cast<const FcChar8*>("matchlang")); // Ownership of this FcPattern will be transferred via FcFontSetAdd. FcPattern* weak = FcPatternCreate(); FcPatternAddString(weak, FC_FAMILY, reinterpret_cast<const FcChar8*>("nomatchstring")); FcPatternAddLangSet(weak, FC_LANG, weakLangSet.get()); FcFontSetAdd(fontSet.get(), strong); FcFontSetAdd(fontSet.get(), weak); // Add 'matchlang' to the copy of the pattern. FcPatternAddLangSet(pattern.get(), FC_LANG, weakLangSet.get()); // Run a match against the copy of the pattern. // If the first element was weak, then we should match the pattern with 'matchlang'. // If the first element 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. RefPtr<FcConfig> config = adoptRef(FcConfigCreate()); FcFontSet* fontSets[1] = { fontSet.get() }; RefPtr<FcPattern> match = adoptRef(FcFontSetMatch(config.get(), fontSets, 1, pattern.get(), &result)); FcLangSet* matchLangSet; FcPatternGetLangSet(match.get(), FC_LANG, 0, &matchLangSet); return FcLangEqual == FcLangSetHasLang(matchLangSet, reinterpret_cast<const FcChar8*>("matchlang")) ? AliasStrength::Weak : AliasStrength::Strong; }
Vector<String> FontCache::systemFontFamilies() { RefPtr<FcPattern> scalablesOnlyPattern = adoptRef(FcPatternCreate()); FcPatternAddBool(scalablesOnlyPattern.get(), FC_SCALABLE, FcTrue); FcUniquePtr<FcObjectSet> familiesOnly(FcObjectSetBuild(FC_FAMILY, nullptr)); FcUniquePtr<FcFontSet> fontSet(FcFontList(nullptr, scalablesOnlyPattern.get(), familiesOnly.get())); Vector<String> fontFamilies; for (int i = 0; i < fontSet->nfont; i++) { FcPattern* pattern = fontSet->fonts[i]; FcChar8* family = nullptr; FcPatternGetString(pattern, FC_FAMILY, 0, &family); if (family) fontFamilies.appendVector(patternToFamilies(*pattern)); } return fontFamilies; }