FontCacheFreeType.cpp [plain text]
#include "config.h"
#include "FontCache.h"
#include "CairoUniquePtr.h"
#include "CairoUtilities.h"
#include "CharacterProperties.h"
#include "FcUniquePtr.h"
#include "FloatConversion.h"
#include "Font.h"
#include "FontDescription.h"
#include "FontCacheFreeType.h"
#include "RefPtrCairo.h"
#include "RefPtrFontconfig.h"
#include "UTF16UChar32Iterator.h"
#include <cairo-ft.h>
#include <cairo.h>
#include <fontconfig/fcfreetype.h>
#include <wtf/Assertions.h>
#include <wtf/HashFunctions.h>
#include <wtf/HashMap.h>
#include <wtf/text/CString.h>
#if PLATFORM(GTK)
#include "GtkUtilities.h"
#endif
#if ENABLE(VARIATION_FONTS)
#include FT_MULTIPLE_MASTERS_H
#endif
namespace WebCore {
void FontCache::platformInit()
{
if (!FcInit())
ASSERT_NOT_REACHED();
}
static int fontWeightToFontconfigWeight(FontSelectionValue weight)
{
if (weight < FontSelectionValue(150))
return FC_WEIGHT_THIN;
if (weight < FontSelectionValue(250))
return FC_WEIGHT_ULTRALIGHT;
if (weight < FontSelectionValue(350))
return FC_WEIGHT_LIGHT;
if (weight < FontSelectionValue(450))
return FC_WEIGHT_REGULAR;
if (weight < FontSelectionValue(550))
return FC_WEIGHT_MEDIUM;
if (weight < FontSelectionValue(650))
return FC_WEIGHT_SEMIBOLD;
if (weight < FontSelectionValue(750))
return FC_WEIGHT_BOLD;
if (weight < FontSelectionValue(850))
return FC_WEIGHT_EXTRABOLD;
return FC_WEIGHT_ULTRABLACK;
}
static bool configurePatternForFontDescription(FcPattern* pattern, const FontDescription& fontDescription)
{
if (!FcPatternAddInteger(pattern, FC_SLANT, fontDescription.italic() ? FC_SLANT_ITALIC : FC_SLANT_ROMAN))
return false;
if (!FcPatternAddInteger(pattern, FC_WEIGHT, fontWeightToFontconfigWeight(fontDescription.weight())))
return false;
if (!FcPatternAddDouble(pattern, FC_PIXEL_SIZE, fontDescription.computedPixelSize()))
return false;
return true;
}
static void getFontPropertiesFromPattern(FcPattern* pattern, const FontDescription& fontDescription, bool& fixedWidth, bool& syntheticBold, bool& syntheticOblique)
{
fixedWidth = false;
int spacing;
if (FcPatternGetInteger(pattern, FC_SPACING, 0, &spacing) == FcResultMatch && spacing == FC_MONO)
fixedWidth = true;
syntheticBold = false;
bool descriptionAllowsSyntheticBold = fontDescription.fontSynthesis() & FontSynthesisWeight;
if (descriptionAllowsSyntheticBold && isFontWeightBold(fontDescription.weight())) {
FcBool fontConfigEmbolden = FcFalse;
if (FcPatternGetBool(pattern, FC_EMBOLDEN, 0, &fontConfigEmbolden) == FcResultMatch)
syntheticBold = fontConfigEmbolden;
int weight = 0;
if (!syntheticBold && FcPatternGetInteger(pattern, FC_WEIGHT, 0, &weight) == FcResultMatch)
syntheticBold = syntheticBold || weight < FC_WEIGHT_DEMIBOLD;
}
syntheticOblique = false;
int actualFontSlant;
bool descriptionAllowsSyntheticOblique = fontDescription.fontSynthesis() & FontSynthesisStyle;
if (descriptionAllowsSyntheticOblique && fontDescription.italic()
&& FcPatternGetInteger(pattern, FC_SLANT, 0, &actualFontSlant) == FcResultMatch) {
syntheticOblique = actualFontSlant == FC_SLANT_ROMAN;
}
}
struct CachedPattern {
FcPattern* pattern { nullptr };
FcCharSet* charSet { nullptr };
};
class CachedFontSet {
WTF_MAKE_NONCOPYABLE(CachedFontSet); WTF_MAKE_FAST_ALLOCATED;
public:
explicit CachedFontSet(RefPtr<FcPattern>&& pattern)
: m_pattern(WTFMove(pattern))
{
FcResult result;
m_fontSet.reset(FcFontSort(nullptr, m_pattern.get(), FcTrue, nullptr, &result));
for (int i = 0; i < m_fontSet->nfont; ++i) {
FcPattern* pattern = m_fontSet->fonts[i];
FcCharSet* charSet;
if (FcPatternGetCharSet(pattern, FC_CHARSET, 0, &charSet) == FcResultMatch)
m_patterns.append({ pattern, charSet });
}
}
RefPtr<FcPattern> bestForCharacters(const UChar* characters, unsigned length)
{
if (m_patterns.isEmpty()) {
FcResult result;
return adoptRef(FcFontMatch(nullptr, m_pattern.get(), &result));
}
FcUniquePtr<FcCharSet> fontConfigCharSet(FcCharSetCreate());
UTF16UChar32Iterator iterator(characters, length);
UChar32 character = iterator.next();
bool hasNonIgnorableCharacters = false;
while (character != iterator.end()) {
if (!isDefaultIgnorableCodePoint(character)) {
FcCharSetAddChar(fontConfigCharSet.get(), character);
hasNonIgnorableCharacters = true;
}
character = iterator.next();
}
FcPattern* bestPattern = nullptr;
int minScore = std::numeric_limits<int>::max();
if (hasNonIgnorableCharacters) {
for (const auto& cachedPattern : m_patterns) {
if (!cachedPattern.charSet)
continue;
int score = FcCharSetSubtractCount(fontConfigCharSet.get(), cachedPattern.charSet);
if (!score)
return adoptRef(FcFontRenderPrepare(nullptr, m_pattern.get(), cachedPattern.pattern));
if (score < minScore) {
bestPattern = cachedPattern.pattern;
minScore = score;
}
}
}
if (bestPattern)
return adoptRef(FcFontRenderPrepare(nullptr, m_pattern.get(), bestPattern));
return adoptRef(FcFontRenderPrepare(nullptr, m_pattern.get(), m_patterns[0].pattern));
}
private:
RefPtr<FcPattern> m_pattern;
FcUniquePtr<FcFontSet> m_fontSet;
Vector<CachedPattern> m_patterns;
};
struct FallbackFontDescriptionKey {
FallbackFontDescriptionKey() = default;
FallbackFontDescriptionKey(const FontDescription& description, FontCache::PreferColoredFont preferColoredFont)
: descriptionKey(description)
, coloredFont(preferColoredFont == FontCache::PreferColoredFont::Yes)
{
}
explicit FallbackFontDescriptionKey(WTF::HashTableDeletedValueType deletedValue)
: descriptionKey(deletedValue)
{
}
bool operator==(const FallbackFontDescriptionKey& other) const
{
return descriptionKey == other.descriptionKey && coloredFont == other.coloredFont;
}
bool operator!=(const FallbackFontDescriptionKey& other) const
{
return !(*this == other);
}
bool isHashTableDeletedValue() const { return descriptionKey.isHashTableDeletedValue(); }
unsigned computeHash() const
{
return WTF::pairIntHash(descriptionKey.computeHash(), WTF::DefaultHash<bool>::Hash::hash(coloredFont));
}
FontDescriptionKey descriptionKey;
bool coloredFont { false };
};
struct FallbackFontDescriptionKeyHash {
static unsigned hash(const FallbackFontDescriptionKey& key) { return key.computeHash(); }
static bool equal(const FallbackFontDescriptionKey& a, const FallbackFontDescriptionKey& b) { return a == b; }
static const bool safeToCompareToEmptyOrDeleted = true;
};
using SystemFallbackCache = HashMap<FallbackFontDescriptionKey, std::unique_ptr<CachedFontSet>, FallbackFontDescriptionKeyHash, SimpleClassHashTraits<FallbackFontDescriptionKey>>;
static SystemFallbackCache& systemFallbackCache()
{
static NeverDestroyed<SystemFallbackCache> cache;
return cache.get();
}
RefPtr<Font> FontCache::systemFallbackForCharacters(const FontDescription& description, const Font*, IsForPlatformFont, PreferColoredFont preferColoredFont, const UChar* characters, unsigned length)
{
auto addResult = systemFallbackCache().ensure(FallbackFontDescriptionKey(description, preferColoredFont), [&description, preferColoredFont]() -> std::unique_ptr<CachedFontSet> {
RefPtr<FcPattern> pattern = adoptRef(FcPatternCreate());
FcPatternAddBool(pattern.get(), FC_SCALABLE, FcTrue);
#ifdef FC_COLOR
if (preferColoredFont == PreferColoredFont::Yes)
FcPatternAddBool(pattern.get(), FC_COLOR, FcTrue);
#endif
if (!configurePatternForFontDescription(pattern.get(), description))
return nullptr;
FcConfigSubstitute(nullptr, pattern.get(), FcMatchPattern);
cairo_ft_font_options_substitute(getDefaultCairoFontOptions(), pattern.get());
FcDefaultSubstitute(pattern.get());
return makeUnique<CachedFontSet>(WTFMove(pattern));
});
if (!addResult.iterator->value)
return nullptr;
RefPtr<FcPattern> resultPattern = addResult.iterator->value->bestForCharacters(characters, length);
if (!resultPattern)
return nullptr;
bool fixedWidth, syntheticBold, syntheticOblique;
getFontPropertiesFromPattern(resultPattern.get(), description, fixedWidth, syntheticBold, syntheticOblique);
RefPtr<cairo_font_face_t> fontFace = adoptRef(cairo_ft_font_face_create_for_pattern(resultPattern.get()));
FontPlatformData alternateFontData(fontFace.get(), resultPattern.get(), description.computedPixelSize(), fixedWidth, syntheticBold, syntheticOblique, description.orientation());
return fontForPlatformData(alternateFontData);
}
void FontCache::platformPurgeInactiveFontData()
{
systemFallbackCache().clear();
}
static Vector<String> patternToFamilies(FcPattern& pattern)
{
char* patternChars = reinterpret_cast<char*>(FcPatternFormat(&pattern, reinterpret_cast<const FcChar8*>("%{family}")));
String patternString = String::fromUTF8(patternChars);
free(patternChars);
return patternString.split(',');
}
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;
}
bool FontCache::isSystemFontForbiddenForEditing(const String&)
{
return false;
}
Ref<Font> FontCache::lastResortFallbackFont(const FontDescription& fontDescription)
{
static AtomString timesStr("serif");
if (RefPtr<Font> font = fontForFamily(fontDescription, timesStr))
return *font;
RELEASE_ASSERT_NOT_REACHED();
}
Vector<FontSelectionCapabilities> FontCache::getFontSelectionCapabilitiesInFamily(const AtomString&, AllowUserInstalledFonts)
{
return { };
}
static String getFamilyNameStringFromFamily(const AtomString& family)
{
if (family.length() && !family.startsWith("-webkit-"))
return family.string();
if (family == standardFamily || family == serifFamily)
return "serif";
if (family == sansSerifFamily)
return "sans-serif";
if (family == monospaceFamily)
return "monospace";
if (family == cursiveFamily)
return "cursive";
if (family == fantasyFamily)
return "fantasy";
#if PLATFORM(GTK)
if (family == systemUiFamily || family == "-webkit-system-font")
return defaultGtkSystemFont();
#endif
return "";
}
enum class AliasStrength {
Weak,
Strong,
Done
};
static AliasStrength strengthOfFirstAlias(const FcPattern& original)
{
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);
FcUniquePtr<FcFontSet> fontSet(FcFontSetCreate());
FcUniquePtr<FcLangSet> strongLangSet(FcLangSetCreate());
FcLangSetAdd(strongLangSet.get(), reinterpret_cast<const FcChar8*>("nomatchlang"));
FcPattern* strong = FcPatternDuplicate(pattern.get());
FcPatternAddLangSet(strong, FC_LANG, strongLangSet.get());
FcUniquePtr<FcLangSet> weakLangSet(FcLangSetCreate());
FcLangSetAdd(weakLangSet.get(), reinterpret_cast<const FcChar8*>("matchlang"));
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);
FcPatternAddLangSet(pattern.get(), FC_LANG, weakLangSet.get());
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;
}
static Vector<String> strongAliasesForFamily(const String& family)
{
RefPtr<FcPattern> pattern = adoptRef(FcPatternCreate());
if (!FcPatternAddString(pattern.get(), FC_FAMILY, reinterpret_cast<const FcChar8*>(family.utf8().data())))
return Vector<String>();
FcConfigSubstitute(nullptr, pattern.get(), FcMatchPattern);
cairo_ft_font_options_substitute(getDefaultCairoFontOptions(), pattern.get());
FcDefaultSubstitute(pattern.get());
FcUniquePtr<FcObjectSet> familiesOnly(FcObjectSetBuild(FC_FAMILY, nullptr));
RefPtr<FcPattern> minimal = adoptRef(FcPatternFilter(pattern.get(), familiesOnly.get()));
int lastStrongId = -1;
int numIds = 0;
for (int id = 0; ; ++id) {
AliasStrength result = strengthOfFirstAlias(*minimal);
if (result == AliasStrength::Done) {
numIds = id;
break;
}
if (result == AliasStrength::Strong)
lastStrongId = id;
if (!FcPatternRemove(minimal.get(), FC_FAMILY, 0))
return Vector<String>();
}
if (lastStrongId < 0)
return Vector<String>();
for (int id = lastStrongId + 1; id < numIds; ++id) {
if (!FcPatternRemove(pattern.get(), FC_FAMILY, lastStrongId + 1)) {
ASSERT_NOT_REACHED();
return Vector<String>();
}
}
return patternToFamilies(*pattern);
}
static bool areStronglyAliased(const String& familyA, const String& familyB)
{
for (auto& family : strongAliasesForFamily(familyA)) {
if (family == familyB)
return true;
}
return false;
}
static inline bool isCommonlyUsedGenericFamily(const String& familyNameString)
{
return equalLettersIgnoringASCIICase(familyNameString, "sans")
|| equalLettersIgnoringASCIICase(familyNameString, "sans-serif")
|| equalLettersIgnoringASCIICase(familyNameString, "serif")
|| equalLettersIgnoringASCIICase(familyNameString, "monospace")
|| equalLettersIgnoringASCIICase(familyNameString, "fantasy")
#if PLATFORM(GTK)
|| equalLettersIgnoringASCIICase(familyNameString, "-webkit-system-font")
|| equalLettersIgnoringASCIICase(familyNameString, "-webkit-system-ui")
#endif
|| equalLettersIgnoringASCIICase(familyNameString, "cursive");
}
std::unique_ptr<FontPlatformData> FontCache::createFontPlatformData(const FontDescription& fontDescription, const AtomString& family, const FontFeatureSettings*, FontSelectionSpecifiedCapabilities)
{
RefPtr<FcPattern> pattern = adoptRef(FcPatternCreate());
FcPatternAddBool(pattern.get(), FC_SCALABLE, FcTrue);
#if ENABLE(VARIATION_FONTS)
FcPatternAddBool(pattern.get(), FC_VARIABLE, FcDontCare);
#endif
String familyNameString(getFamilyNameStringFromFamily(family));
if (!FcPatternAddString(pattern.get(), FC_FAMILY, reinterpret_cast<const FcChar8*>(familyNameString.utf8().data())))
return nullptr;
if (!configurePatternForFontDescription(pattern.get(), fontDescription))
return nullptr;
FcConfigSubstitute(nullptr, pattern.get(), FcMatchPattern);
cairo_ft_font_options_substitute(getDefaultCairoFontOptions(), pattern.get());
FcDefaultSubstitute(pattern.get());
FcChar8* fontConfigFamilyNameAfterConfiguration;
FcPatternGetString(pattern.get(), FC_FAMILY, 0, &fontConfigFamilyNameAfterConfiguration);
String familyNameAfterConfiguration = String::fromUTF8(reinterpret_cast<char*>(fontConfigFamilyNameAfterConfiguration));
FcResult fontConfigResult;
RefPtr<FcPattern> resultPattern = adoptRef(FcFontMatch(nullptr, pattern.get(), &fontConfigResult));
if (!resultPattern) return nullptr;
bool matchedFontFamily = false;
FcChar8* fontConfigFamilyNameAfterMatching;
for (int i = 0; FcPatternGetString(resultPattern.get(), FC_FAMILY, i, &fontConfigFamilyNameAfterMatching) == FcResultMatch; ++i) {
String familyNameAfterMatching = String::fromUTF8(reinterpret_cast<char*>(fontConfigFamilyNameAfterMatching));
if (equalIgnoringASCIICase(familyNameAfterConfiguration, familyNameAfterMatching) || isCommonlyUsedGenericFamily(familyNameString) || areStronglyAliased(familyNameAfterConfiguration, familyNameAfterMatching)) {
matchedFontFamily = true;
break;
}
}
if (!matchedFontFamily)
return nullptr;
bool fixedWidth, syntheticBold, syntheticOblique;
getFontPropertiesFromPattern(resultPattern.get(), fontDescription, fixedWidth, syntheticBold, syntheticOblique);
RefPtr<cairo_font_face_t> fontFace = adoptRef(cairo_ft_font_face_create_for_pattern(resultPattern.get()));
#if ENABLE(VARIATION_FONTS)
CairoUniquePtr<cairo_font_options_t> options(cairo_font_options_copy(getDefaultCairoFontOptions()));
cairo_matrix_t matrix;
cairo_matrix_init_identity(&matrix);
RefPtr<cairo_scaled_font_t> scaledFont = adoptRef(cairo_scaled_font_create(fontFace.get(), &matrix, &matrix, options.get()));
CairoFtFaceLocker cairoFtFaceLocker(scaledFont.get());
if (FT_Face freeTypeFace = cairoFtFaceLocker.ftFace()) {
auto variants = buildVariationSettings(freeTypeFace, fontDescription);
if (!variants.isEmpty())
FcPatternAddString(resultPattern.get(), FC_FONT_VARIATIONS, reinterpret_cast<const FcChar8*>(variants.utf8().data()));
}
#endif
auto platformData = makeUnique<FontPlatformData>(fontFace.get(), resultPattern.get(), fontDescription.computedPixelSize(), fixedWidth, syntheticBold, syntheticOblique, fontDescription.orientation());
if (!platformData->hasCompatibleCharmap())
return nullptr;
return platformData;
}
const AtomString& FontCache::platformAlternateFamilyName(const AtomString&)
{
return nullAtom();
}
#if ENABLE(VARIATION_FONTS)
struct VariationDefaults {
float defaultValue;
float minimumValue;
float maximumValue;
};
typedef HashMap<FontTag, VariationDefaults, FourCharacterTagHash, FourCharacterTagHashTraits> VariationDefaultsMap;
typedef HashMap<FontTag, float, FourCharacterTagHash, FourCharacterTagHashTraits> VariationsMap;
static VariationDefaultsMap defaultVariationValues(FT_Face face)
{
VariationDefaultsMap result;
FT_MM_Var* ftMMVar;
if (FT_Get_MM_Var(face, &ftMMVar))
return result;
for (unsigned i = 0; i < ftMMVar->num_axis; ++i) {
auto tag = ftMMVar->axis[i].tag;
auto b1 = 0xFF & (tag >> 24);
auto b2 = 0xFF & (tag >> 16);
auto b3 = 0xFF & (tag >> 8);
auto b4 = 0xFF & (tag >> 0);
FontTag resultKey = {{ static_cast<char>(b1), static_cast<char>(b2), static_cast<char>(b3), static_cast<char>(b4) }};
VariationDefaults resultValues = { narrowPrecisionToFloat(ftMMVar->axis[i].def / 65536.), narrowPrecisionToFloat(ftMMVar->axis[i].minimum / 65536.), narrowPrecisionToFloat(ftMMVar->axis[i].maximum / 65536.) };
result.set(resultKey, resultValues);
}
FT_Done_MM_Var(face->glyph->library, ftMMVar);
return result;
}
String buildVariationSettings(FT_Face face, const FontDescription& fontDescription)
{
auto defaultValues = defaultVariationValues(face);
const auto& variations = fontDescription.variationSettings();
VariationsMap variationsToBeApplied;
auto applyVariation = [&](const FontTag& tag, float value) {
auto iterator = defaultValues.find(tag);
if (iterator == defaultValues.end())
return;
float valueToApply = clampTo(value, iterator->value.minimumValue, iterator->value.maximumValue);
variationsToBeApplied.set(tag, valueToApply);
};
for (auto& variation : variations)
applyVariation(variation.tag(), variation.value());
StringBuilder builder;
for (auto& variation : variationsToBeApplied) {
if (!builder.isEmpty())
builder.append(',');
builder.append(variation.key[0]);
builder.append(variation.key[1]);
builder.append(variation.key[2]);
builder.append(variation.key[3]);
builder.append('=');
builder.append(FormattedNumber::fixedPrecision(variation.value));
}
return builder.toString();
}
#endif // ENABLE(VARIATION_FONTS)
}