From 87330d4bd7e08adff3cebb156ee7fa8f6539866c Mon Sep 17 00:00:00 2001 From: Eskil Abrahamsen Blomfeldt Date: Wed, 15 Jan 2025 15:39:16 +0100 Subject: [PATCH] FontConfig: Fix detection of color fonts There were two mistakes in the code that intended to detect if a specific font was a color font in the FontConfig database. 1. The "int n" parameter in FcPatternGet*() is not an array size, but an index, so it should be 0 and not 1. 2. We need to add FC_COLOR to the list of properties in our pattern when populating the database, otherwise we will just fail to match it and none of the system fonts will be listed as color. Fixes: QTBUG-132377 Change-Id: Ib3c112e8a354abacd05679c62283a1f1abfb40ee Reviewed-by: Eirik Aavitsland (cherry picked from commit 2e1db6541777ee076195ff9e793e4e83afd81539) Reviewed-by: Qt Cherry-pick Bot --- src/gui/text/coretext/qcoretextfontdatabase.mm | 5 +++++ src/gui/text/coretext/qcoretextfontdatabase_p.h | 1 + src/gui/text/freetype/qfreetypefontdatabase.cpp | 9 +++++++++ src/gui/text/freetype/qfreetypefontdatabase_p.h | 1 + src/gui/text/qplatformfontdatabase.cpp | 9 +++++++++ src/gui/text/qplatformfontdatabase.h | 1 + src/gui/text/unix/qfontconfigdatabase.cpp | 5 ++++- src/gui/text/windows/qwindowsfontdatabase.cpp | 5 +++++ src/gui/text/windows/qwindowsfontdatabase_p.h | 1 + tests/auto/gui/text/qfontdatabase/tst_qfontdatabase.cpp | 6 ++++++ 10 files changed, 42 insertions(+), 1 deletion(-) diff --git a/src/gui/text/coretext/qcoretextfontdatabase.mm b/src/gui/text/coretext/qcoretextfontdatabase.mm index fc551cdba76..1877a5b5509 100644 --- a/src/gui/text/coretext/qcoretextfontdatabase.mm +++ b/src/gui/text/coretext/qcoretextfontdatabase.mm @@ -1032,6 +1032,11 @@ QList QCoreTextFontDatabase::standardSizes() const return ret; } +bool QCoreTextFontDatabase::supportsColrv0Fonts() const +{ + return true; +} + bool QCoreTextFontDatabase::supportsVariableApplicationFonts() const { return true; diff --git a/src/gui/text/coretext/qcoretextfontdatabase_p.h b/src/gui/text/coretext/qcoretextfontdatabase_p.h index 5c8a6ad3b74..3cc3486acdb 100644 --- a/src/gui/text/coretext/qcoretextfontdatabase_p.h +++ b/src/gui/text/coretext/qcoretextfontdatabase_p.h @@ -47,6 +47,7 @@ public: bool fontsAlwaysScalable() const override; QList standardSizes() const override; bool supportsVariableApplicationFonts() const override; + bool supportsColrv0Fonts() const override; // For iOS and macOS platform themes QFont *themeFont(QPlatformTheme::Font) const; diff --git a/src/gui/text/freetype/qfreetypefontdatabase.cpp b/src/gui/text/freetype/qfreetypefontdatabase.cpp index 66c6a510e5d..084b44692f6 100644 --- a/src/gui/text/freetype/qfreetypefontdatabase.cpp +++ b/src/gui/text/freetype/qfreetypefontdatabase.cpp @@ -355,6 +355,15 @@ QStringList QFreeTypeFontDatabase::addTTFile(const QByteArray &fontData, const Q return families; } +bool QFreeTypeFontDatabase::supportsColrv0Fonts() const +{ +#if (FREETYPE_MAJOR*10000 + FREETYPE_MINOR*100 + FREETYPE_PATCH) >= 21000 + return true; +#else + return false; +#endif +} + bool QFreeTypeFontDatabase::supportsVariableApplicationFonts() const { #if (FREETYPE_MAJOR*10000 + FREETYPE_MINOR*100 + FREETYPE_PATCH) >= 20900 diff --git a/src/gui/text/freetype/qfreetypefontdatabase_p.h b/src/gui/text/freetype/qfreetypefontdatabase_p.h index bc5cdbffa9f..176ab4d97cb 100644 --- a/src/gui/text/freetype/qfreetypefontdatabase_p.h +++ b/src/gui/text/freetype/qfreetypefontdatabase_p.h @@ -43,6 +43,7 @@ public: QStringList addApplicationFont(const QByteArray &fontData, const QString &fileName, QFontDatabasePrivate::ApplicationFont *applicationFont = nullptr) override; void releaseHandle(void *handle) override; bool supportsVariableApplicationFonts() const override; + bool supportsColrv0Fonts() const override; static void addNamedInstancesForFace(void *face, int faceIndex, const QString &family, const QString &styleName, diff --git a/src/gui/text/qplatformfontdatabase.cpp b/src/gui/text/qplatformfontdatabase.cpp index 381431ac4e9..314df8152c6 100644 --- a/src/gui/text/qplatformfontdatabase.cpp +++ b/src/gui/text/qplatformfontdatabase.cpp @@ -657,6 +657,15 @@ bool QPlatformFontDatabase::supportsVariableApplicationFonts() const return false; } +/*! + Returns true if this font database supports loading color fonts in the COLRv0 format. + + \since 6.9 +*/ +bool QPlatformFontDatabase::supportsColrv0Fonts() const +{ + return false; +} /*! \class QPlatformFontDatabase diff --git a/src/gui/text/qplatformfontdatabase.h b/src/gui/text/qplatformfontdatabase.h index 407ecbc27c1..3d871a44687 100644 --- a/src/gui/text/qplatformfontdatabase.h +++ b/src/gui/text/qplatformfontdatabase.h @@ -89,6 +89,7 @@ public: virtual QList standardSizes() const; virtual bool supportsVariableApplicationFonts() const; + virtual bool supportsColrv0Fonts() const; // helper static QSupportedWritingSystems writingSystemsFromTrueTypeBits(quint32 unicodeRange[4], quint32 codePageRange[2]); diff --git a/src/gui/text/unix/qfontconfigdatabase.cpp b/src/gui/text/unix/qfontconfigdatabase.cpp index de6618fc3f5..20794ed1453 100644 --- a/src/gui/text/unix/qfontconfigdatabase.cpp +++ b/src/gui/text/unix/qfontconfigdatabase.cpp @@ -478,7 +478,7 @@ static void populateFromPattern(FcPattern *pattern, FcBool colorFont = false; #ifdef FC_COLOR - FcPatternGetBool(pattern, FC_COLOR, 1, &colorFont); + FcPatternGetBool(pattern, FC_COLOR, 0, &colorFont); #endif // Note: stretch should really be an int but registerFont incorrectly uses an enum @@ -576,6 +576,9 @@ void QFontconfigDatabase::populateFontDatabase() FC_WIDTH, FC_FAMILYLANG, #if FC_VERSION >= 20297 FC_CAPABILITY, +#endif +#if defined(FC_COLOR) + FC_COLOR, #endif (const char *)nullptr }; diff --git a/src/gui/text/windows/qwindowsfontdatabase.cpp b/src/gui/text/windows/qwindowsfontdatabase.cpp index 73814c6a634..f6c36e1cb42 100644 --- a/src/gui/text/windows/qwindowsfontdatabase.cpp +++ b/src/gui/text/windows/qwindowsfontdatabase.cpp @@ -1292,4 +1292,9 @@ bool QWindowsFontDatabase::isPrivateFontFamily(const QString &family) const return m_eudcFonts.contains(family) || QPlatformFontDatabase::isPrivateFontFamily(family); } +bool QWindowsFontDatabase::supportsColrv0Fonts() const +{ + return true; +} + QT_END_NAMESPACE diff --git a/src/gui/text/windows/qwindowsfontdatabase_p.h b/src/gui/text/windows/qwindowsfontdatabase_p.h index 926287264f6..92e3f04f968 100644 --- a/src/gui/text/windows/qwindowsfontdatabase_p.h +++ b/src/gui/text/windows/qwindowsfontdatabase_p.h @@ -46,6 +46,7 @@ public: void populateFontDatabase() override; void invalidate() override; void removeApplicationFonts(); + bool supportsColrv0Fonts() const override; void populateFamily(const QString &familyName) override; bool populateFamilyAliases(const QString &missingFamily) override; diff --git a/tests/auto/gui/text/qfontdatabase/tst_qfontdatabase.cpp b/tests/auto/gui/text/qfontdatabase/tst_qfontdatabase.cpp index d24b37732c5..56ecccc20f9 100644 --- a/tests/auto/gui/text/qfontdatabase/tst_qfontdatabase.cpp +++ b/tests/auto/gui/text/qfontdatabase/tst_qfontdatabase.cpp @@ -769,6 +769,12 @@ void tst_QFontDatabase::addApplicationFontFallback() void tst_QFontDatabase::addApplicationEmojiFontFamily() { + { + QPlatformFontDatabase *pfdb = QGuiApplicationPrivate::platformIntegration()->fontDatabase(); + if (!pfdb->supportsColrv0Fonts()) + QSKIP("This test depends on COLRv0 support."); + } + int id = -1; auto cleanup = qScopeGuard([&id] { if (id >= 0)