From 253ce59c12d60ded2278e6d5bb8399e44bd4d302 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tor=20Arne=20Vestb=C3=B8?= Date: Thu, 3 Oct 2019 16:51:19 +0200 Subject: CoreText: Use StyleHint as fallback when family is not found Change-Id: I11fb6cafe9d41c38eac6ca0695c89f30f998f257 Reviewed-by: Eskil Abrahamsen Blomfeldt Reviewed-by: Volker Hilsheimer --- .../fontdatabases/mac/qcoretextfontdatabase.mm | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) (limited to 'src/platformsupport') diff --git a/src/platformsupport/fontdatabases/mac/qcoretextfontdatabase.mm b/src/platformsupport/fontdatabases/mac/qcoretextfontdatabase.mm index 4887a501ba..2e8e726c70 100644 --- a/src/platformsupport/fontdatabases/mac/qcoretextfontdatabase.mm +++ b/src/platformsupport/fontdatabases/mac/qcoretextfontdatabase.mm @@ -444,6 +444,14 @@ QStringList QCoreTextFontDatabase::fallbacksForFamily(const QString &family) return QStringList(); } + // If the font is not available we want to fall back to the style hint. + // By creating a matching font descriptor we can verify whether the font + // is available or not, and avoid CTFontCreateWithFontDescriptor picking + // a default font for us based on incomplete information. + fontDescriptor = CTFontDescriptorCreateMatchingFontDescriptor(fontDescriptor, 0); + if (!fontDescriptor) + return QStringList(); + QCFType font = CTFontCreateWithFontDescriptor(fontDescriptor, 12.0, 0); if (!font) { qCWarning(lcQpaFonts) << "Failed to create fallback font for" << family; @@ -472,6 +480,10 @@ QStringList QCoreTextFontDatabase::fallbacksForFamily(const QString &family, QFo { Q_UNUSED(style); + qCDebug(lcQpaFonts).nospace() << "Resolving fallbacks for" + << (!family.isEmpty() ? qPrintable(QLatin1String(" family '%1' with").arg(family)) : "") + << " style hint " << styleHint; + QMacAutoReleasePool pool; QStringList fallbackList = fallbacksForFamily(family); @@ -479,6 +491,9 @@ QStringList QCoreTextFontDatabase::fallbacksForFamily(const QString &family, QFo if (fallbackList.isEmpty()) { // We were not able to find a fallback for the specific family, // or the family was empty, so we fall back to the style hint. + if (!family.isEmpty()) + qCDebug(lcQpaFonts) << "No fallbacks found. Using style hint instead"; + QString styleFamily = [styleHint]{ switch (styleHint) { case QFont::SansSerif: return QStringLiteral("Helvetica"); @@ -541,6 +556,8 @@ QStringList QCoreTextFontDatabase::fallbacksForFamily(const QString &family, QFo extern QStringList qt_sort_families_by_writing_system(QChar::Script, const QStringList &); fallbackList = qt_sort_families_by_writing_system(script, fallbackList); + qCDebug(lcQpaFonts).nospace() << "Resolved fallbacks ordered by script " << script << ": " << fallbackList; + return fallbackList; } -- cgit v1.2.3 From b03e75670be884eb21a61c336a177a3fcb787426 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tor=20Arne=20Vestb=C3=B8?= Date: Mon, 7 Oct 2019 15:45:32 +0200 Subject: CoreText: Preserve font descriptors when resolving fallback families From macOS 10.15 and iOS 13 forward it's not possible to create font descriptors for system fonts such as .AppleSystemUIFont based on the family name. This means we have to preserve the font descriptors we get from CoreText for fallback fonts, so that we can populate them along with the family name. Task-number: QTBUG-78821 Task-number: QTBUG-77467 Change-Id: Ifce01da65f90afb7dc2bc3005c3c5870b9c116de Reviewed-by: Eskil Abrahamsen Blomfeldt --- .../fontdatabases/mac/qcoretextfontdatabase.mm | 176 ++++++++++++--------- .../fontdatabases/mac/qcoretextfontdatabase_p.h | 2 +- 2 files changed, 103 insertions(+), 75 deletions(-) (limited to 'src/platformsupport') diff --git a/src/platformsupport/fontdatabases/mac/qcoretextfontdatabase.mm b/src/platformsupport/fontdatabases/mac/qcoretextfontdatabase.mm index 2e8e726c70..daa3dc94ea 100644 --- a/src/platformsupport/fontdatabases/mac/qcoretextfontdatabase.mm +++ b/src/platformsupport/fontdatabases/mac/qcoretextfontdatabase.mm @@ -432,16 +432,46 @@ template class QCoreTextFontDatabaseEngineFactory; template class QCoreTextFontDatabaseEngineFactory; #endif -QStringList QCoreTextFontDatabase::fallbacksForFamily(const QString &family) +CTFontDescriptorRef descriptorForFamily(const QString &familyName) +{ + return CTFontDescriptorCreateWithAttributes(CFDictionaryRef(@{ + (id)kCTFontFamilyNameAttribute: familyName.toNSString() + })); +} + +CTFontDescriptorRef descriptorForFamily(const char *familyName) +{ + return descriptorForFamily(QString::fromLatin1(familyName)); +} + +CFArrayRef fallbacksForDescriptor(CTFontDescriptorRef descriptor) +{ + QCFType font = CTFontCreateWithFontDescriptor(descriptor, 0.0, nullptr); + if (!font) { + qCWarning(lcQpaFonts) << "Failed to create fallback font for" << descriptor; + return nullptr; + } + + CFArrayRef cascadeList = CFArrayRef(CTFontCopyDefaultCascadeListForLanguages(font, + (CFArrayRef)[NSUserDefaults.standardUserDefaults stringArrayForKey:@"AppleLanguages"])); + + if (!cascadeList) { + qCWarning(lcQpaFonts) << "Failed to create fallback cascade list for" << descriptor; + return nullptr; + } + + return cascadeList; +} + +CFArrayRef QCoreTextFontDatabase::fallbacksForFamily(const QString &family) { if (family.isEmpty()) - return QStringList(); + return nullptr; - auto attributes = @{ id(kCTFontFamilyNameAttribute): family.toNSString() }; - QCFType fontDescriptor = CTFontDescriptorCreateWithAttributes(CFDictionaryRef(attributes)); + QCFType fontDescriptor = descriptorForFamily(family); if (!fontDescriptor) { qCWarning(lcQpaFonts) << "Failed to create fallback font descriptor for" << family; - return QStringList(); + return nullptr; } // If the font is not available we want to fall back to the style hint. @@ -450,85 +480,94 @@ QStringList QCoreTextFontDatabase::fallbacksForFamily(const QString &family) // a default font for us based on incomplete information. fontDescriptor = CTFontDescriptorCreateMatchingFontDescriptor(fontDescriptor, 0); if (!fontDescriptor) - return QStringList(); + return nullptr; - QCFType font = CTFontCreateWithFontDescriptor(fontDescriptor, 12.0, 0); - if (!font) { - qCWarning(lcQpaFonts) << "Failed to create fallback font for" << family; - return QStringList(); - } + return fallbacksForDescriptor(fontDescriptor); +} - QCFType cascadeList = CFArrayRef(CTFontCopyDefaultCascadeListForLanguages(font, - (CFArrayRef)[NSUserDefaults.standardUserDefaults stringArrayForKey:@"AppleLanguages"])); - if (!cascadeList) { - qCWarning(lcQpaFonts) << "Failed to create fallback cascade list for" << family; - return QStringList(); - } +CTFontDescriptorRef descriptorForFontType(CTFontUIFontType uiType) +{ + static const CGFloat kDefaultSizeForRequestedUIType = 0.0; + QCFType ctFont = CTFontCreateUIFontForLanguage( + uiType, kDefaultSizeForRequestedUIType, nullptr); + return CTFontCopyFontDescriptor(ctFont); +} - QStringList fallbackList; - const int numCascades = CFArrayGetCount(cascadeList); - for (int i = 0; i < numCascades; ++i) { - CTFontDescriptorRef fontFallback = CTFontDescriptorRef(CFArrayGetValueAtIndex(cascadeList, i)); - QCFString fallbackFamilyName = CFStringRef(CTFontDescriptorCopyAttribute(fontFallback, kCTFontFamilyNameAttribute)); - fallbackList.append(QString::fromCFString(fallbackFamilyName)); +CTFontDescriptorRef descriptorForStyle(QFont::StyleHint styleHint) +{ + switch (styleHint) { + case QFont::SansSerif: return descriptorForFamily("Helvetica"); + case QFont::Serif: return descriptorForFamily("Times New Roman"); + case QFont::Monospace: return descriptorForFamily("Menlo"); +#ifdef Q_OS_MACOS + case QFont::Cursive: return descriptorForFamily("Apple Chancery"); +#endif + case QFont::Fantasy: return descriptorForFamily("Zapfino"); + case QFont::TypeWriter: return descriptorForFamily("American Typewriter"); + case QFont::AnyStyle: Q_FALLTHROUGH(); + case QFont::System: return descriptorForFontType(kCTFontUIFontSystem); + default: return nullptr; // No matching font on this platform } - - return fallbackList; } QStringList QCoreTextFontDatabase::fallbacksForFamily(const QString &family, QFont::Style style, QFont::StyleHint styleHint, QChar::Script script) const { Q_UNUSED(style); - qCDebug(lcQpaFonts).nospace() << "Resolving fallbacks for" + qCDebug(lcQpaFonts).nospace() << "Resolving fallbacks families for" << (!family.isEmpty() ? qPrintable(QLatin1String(" family '%1' with").arg(family)) : "") << " style hint " << styleHint; QMacAutoReleasePool pool; - QStringList fallbackList = fallbacksForFamily(family); + QStringList fallbackList; - if (fallbackList.isEmpty()) { + QCFType fallbackFonts = fallbacksForFamily(family); + if (!fallbackFonts || !CFArrayGetCount(fallbackFonts)) { // We were not able to find a fallback for the specific family, // or the family was empty, so we fall back to the style hint. if (!family.isEmpty()) qCDebug(lcQpaFonts) << "No fallbacks found. Using style hint instead"; - QString styleFamily = [styleHint]{ - switch (styleHint) { - case QFont::SansSerif: return QStringLiteral("Helvetica"); - case QFont::Serif: return QStringLiteral("Times New Roman"); - case QFont::Monospace: return QStringLiteral("Menlo"); -#ifdef Q_OS_MACOS - case QFont::Cursive: return QStringLiteral("Apple Chancery"); -#endif - case QFont::Fantasy: return QStringLiteral("Zapfino"); - case QFont::TypeWriter: return QStringLiteral("American Typewriter"); - case QFont::AnyStyle: Q_FALLTHROUGH(); - case QFont::System: { - QCFType font = CTFontCreateUIFontForLanguage(kCTFontUIFontSystem, 12.0, NULL); - return static_cast(QCFString(CTFontCopyFullName(font))); - } - default: return QString(); // No matching font on this platform - } - }(); - if (!styleFamily.isEmpty()) { - fallbackList = fallbacksForFamily(styleFamily); - if (!fallbackList.contains(styleFamily)) - fallbackList.prepend(styleFamily); + if (QCFType styleDescriptor = descriptorForStyle(styleHint)) { + CFMutableArrayRef tmp = CFArrayCreateMutable(kCFAllocatorDefault, 0, &kCFTypeArrayCallBacks); + CFArrayAppendValue(tmp, styleDescriptor); + QCFType styleFallbacks = fallbacksForDescriptor(styleDescriptor); + CFArrayAppendArray(tmp, styleFallbacks, CFRangeMake(0, CFArrayGetCount(styleFallbacks))); + fallbackFonts = tmp; } } - if (fallbackList.isEmpty()) + if (!fallbackFonts) return fallbackList; - // .Apple Symbols Fallback will be at the beginning of the list and we will - // detect that this has glyphs for Arabic and other writing systems. - // Since it is a symbol font, it should be the last resort, so that - // the proper fonts for these writing systems are preferred. - int symbolIndex = fallbackList.indexOf(QLatin1String(".Apple Symbols Fallback")); - if (symbolIndex >= 0) - fallbackList.move(symbolIndex, fallbackList.size() - 1); + const int numberOfFallbacks = CFArrayGetCount(fallbackFonts); + for (int i = 0; i < numberOfFallbacks; ++i) { + auto fallbackDescriptor = CTFontDescriptorRef(CFArrayGetValueAtIndex(fallbackFonts, i)); + auto fallbackFamilyName = QCFString(CTFontDescriptorCopyAttribute(fallbackDescriptor, kCTFontFamilyNameAttribute)); + + if (!isFamilyPopulated(fallbackFamilyName)) { + // We need to populate, or at least register the fallback fonts, + // otherwise the Qt font database may not know they exist. + if (isPrivateFontFamily(fallbackFamilyName)) + const_cast(this)->populateFromDescriptor(fallbackDescriptor); + else + registerFontFamily(fallbackFamilyName); + } + + fallbackList.append(fallbackFamilyName); + } + + // Some fallback fonts will have have an order in the list returned + // by Core Text that would indicate they should be preferred for e.g. + // Arabic, or Emoji, while in reality only supporting a tiny subset + // of the required glyphs, or representing them by question marks. + // Move these to the end, so that the proper fonts are preferred. + for (const char *family : { ".Apple Symbols Fallback", ".Noto Sans Universal" }) { + int index = fallbackList.indexOf(QLatin1String(family)); + if (index >= 0) + fallbackList.move(index, fallbackList.size() - 1); + } #if defined(Q_OS_MACOS) // Since we are only returning a list of default fonts for the current language, we do not @@ -544,19 +583,10 @@ QStringList QCoreTextFontDatabase::fallbacksForFamily(const QString &family, QFo fallbackList.append(QStringLiteral("Apple Symbols")); #endif - // Since iOS 13, the cascade list may contain meta-fonts which have not been - // populated to the database, such as ".AppleJapaneseFont". It is important that we - // include this in the fallback list, in order to get fallback support for all - // languages - for (const QString &fallback : fallbackList) { - if (!QPlatformFontDatabase::isFamilyPopulated(fallback)) - const_cast(this)->populateFamily(fallback); - } - extern QStringList qt_sort_families_by_writing_system(QChar::Script, const QStringList &); fallbackList = qt_sort_families_by_writing_system(script, fallbackList); - qCDebug(lcQpaFonts).nospace() << "Resolved fallbacks ordered by script " << script << ": " << fallbackList; + qCDebug(lcQpaFonts).nospace() << "Fallback families ordered by script " << script << ": " << fallbackList; return fallbackList; } @@ -717,10 +747,8 @@ static CTFontDescriptorRef fontDescriptorFromTheme(QPlatformTheme::Font f) } #endif // Q_OS_IOS, Q_OS_TVOS, Q_OS_WATCHOS - // OSX default case and iOS fallback case - CTFontUIFontType fontType = fontTypeFromTheme(f); - QCFType ctFont = CTFontCreateUIFontForLanguage(fontType, 0.0, NULL); - return CTFontCopyFontDescriptor(ctFont); + // macOS default case and iOS fallback case + return descriptorForFontType(fontTypeFromTheme(f)); } const QHash &QCoreTextFontDatabase::themeFonts() const @@ -753,8 +781,8 @@ QFont *QCoreTextFontDatabase::themeFont(QPlatformTheme::Font f) const QFont QCoreTextFontDatabase::defaultFont() const { if (defaultFontName.isEmpty()) { - QCFType font = CTFontCreateUIFontForLanguage(kCTFontUIFontSystem, 12.0, NULL); - defaultFontName = (QString) QCFString(CTFontCopyFullName(font)); + QCFType systemFont = descriptorForFontType(kCTFontUIFontSystem); + defaultFontName = QCFString(CTFontDescriptorCopyAttribute(systemFont, kCTFontFamilyNameAttribute)); } return QFont(defaultFontName); diff --git a/src/platformsupport/fontdatabases/mac/qcoretextfontdatabase_p.h b/src/platformsupport/fontdatabases/mac/qcoretextfontdatabase_p.h index 69ff454d1e..eebb3eb964 100644 --- a/src/platformsupport/fontdatabases/mac/qcoretextfontdatabase_p.h +++ b/src/platformsupport/fontdatabases/mac/qcoretextfontdatabase_p.h @@ -92,7 +92,7 @@ protected: private: void populateFromDescriptor(CTFontDescriptorRef font, const QString &familyName = QString()); - static QStringList fallbacksForFamily(const QString &family); + static CFArrayRef fallbacksForFamily(const QString &family); mutable QString defaultFontName; -- cgit v1.2.3 From 05a829f923a88e69b2ceaa372820a2dcb8c083cd Mon Sep 17 00:00:00 2001 From: Friedemann Kleint Date: Fri, 27 Sep 2019 13:36:38 +0200 Subject: Win32: Consolidate registry code Add a RAII class for registry keys and use it throughout the code base. Change-Id: I666b2fbb790f83436443101d6bc1e3c0525e78df Reviewed-by: Volker Hilsheimer --- .../fontdatabases/windows/qwindowsfontdatabase.cpp | 52 ++-------------------- .../fontdatabases/windows/qwindowsfontdatabase_p.h | 2 - .../windows/qwindowsfontenginedirectwrite.cpp | 7 +-- 3 files changed, 7 insertions(+), 54 deletions(-) (limited to 'src/platformsupport') diff --git a/src/platformsupport/fontdatabases/windows/qwindowsfontdatabase.cpp b/src/platformsupport/fontdatabases/windows/qwindowsfontdatabase.cpp index 79f7eb3d43..011476cf13 100644 --- a/src/platformsupport/fontdatabases/windows/qwindowsfontdatabase.cpp +++ b/src/platformsupport/fontdatabases/windows/qwindowsfontdatabase.cpp @@ -53,6 +53,7 @@ #include #include #include +#include #include @@ -1210,33 +1211,8 @@ static int QT_WIN_CALLBACK populateFontFamilies(const LOGFONT *logFont, const TE void QWindowsFontDatabase::addDefaultEUDCFont() { - QString path; - { - HKEY key; - if (RegOpenKeyEx(HKEY_CURRENT_USER, - L"EUDC\\1252", - 0, - KEY_READ, - &key) != ERROR_SUCCESS) { - return; - } - - WCHAR value[MAX_PATH]; - DWORD bufferSize = sizeof(value); - ZeroMemory(value, bufferSize); - - if (RegQueryValueEx(key, - L"SystemDefaultEUDCFont", - nullptr, - nullptr, - reinterpret_cast(value), - &bufferSize) == ERROR_SUCCESS) { - path = QString::fromWCharArray(value); - } - - RegCloseKey(key); - } - + const QString path = QWinRegistryKey(HKEY_CURRENT_USER, LR"(EUDC\1252)") + .stringValue(L"SystemDefaultEUDCFont"); if (!path.isEmpty()) { QFile file(path); if (!file.open(QIODevice::ReadOnly)) { @@ -2105,28 +2081,6 @@ int QWindowsFontDatabase::defaultVerticalDPI() return vDPI; } -QString QWindowsFontDatabase::readRegistryString(HKEY parentHandle, const wchar_t *keyPath, const wchar_t *keyName) -{ - QString result; - HKEY handle = 0; - if (RegOpenKeyEx(parentHandle, keyPath, 0, KEY_READ, &handle) == ERROR_SUCCESS) { - // get the size and type of the value - DWORD dataType; - DWORD dataSize; - if (RegQueryValueEx(handle, keyName, 0, &dataType, 0, &dataSize) == ERROR_SUCCESS) { - if (dataType == REG_SZ || dataType == REG_EXPAND_SZ) { - dataSize += 2; // '\0' missing? - QVarLengthArray data(dataSize); - data[dataSize - 2] = data[dataSize - 1] = '\0'; - if (RegQueryValueEx(handle, keyName, 0, 0, data.data(), &dataSize) == ERROR_SUCCESS) - result = QString::fromWCharArray(reinterpret_cast(data.data())); - } - } - RegCloseKey(handle); - } - return result; -} - bool QWindowsFontDatabase::isPrivateFontFamily(const QString &family) const { return m_eudcFonts.contains(family) || QPlatformFontDatabase::isPrivateFontFamily(family); diff --git a/src/platformsupport/fontdatabases/windows/qwindowsfontdatabase_p.h b/src/platformsupport/fontdatabases/windows/qwindowsfontdatabase_p.h index a1cab17a87..f132e69d4d 100644 --- a/src/platformsupport/fontdatabases/windows/qwindowsfontdatabase_p.h +++ b/src/platformsupport/fontdatabases/windows/qwindowsfontdatabase_p.h @@ -133,8 +133,6 @@ public: static void setFontOptions(unsigned options); static unsigned fontOptions(); - static QString readRegistryString(HKEY parentHandle, const wchar_t *keyPath, const wchar_t *keyName); - private: void removeApplicationFonts(); void addDefaultEUDCFont(); diff --git a/src/platformsupport/fontdatabases/windows/qwindowsfontenginedirectwrite.cpp b/src/platformsupport/fontdatabases/windows/qwindowsfontenginedirectwrite.cpp index a4490a6664..e796c18e79 100644 --- a/src/platformsupport/fontdatabases/windows/qwindowsfontenginedirectwrite.cpp +++ b/src/platformsupport/fontdatabases/windows/qwindowsfontenginedirectwrite.cpp @@ -47,6 +47,7 @@ #include #include #include +#include #include #include #include @@ -945,10 +946,10 @@ void QWindowsFontEngineDirectWrite::initFontInfo(const QFontDef &request, QString QWindowsFontEngineDirectWrite::fontNameSubstitute(const QString &familyName) { - const wchar_t key[] = L"Software\\Microsoft\\Windows NT\\CurrentVersion\\FontSubstitutes"; const QString substitute = - QWindowsFontDatabase::readRegistryString(HKEY_LOCAL_MACHINE, key, - reinterpret_cast(familyName.utf16())); + QWinRegistryKey(HKEY_LOCAL_MACHINE, + LR"(Software\Microsoft\Windows NT\CurrentVersion\FontSubstitutes)") + .stringValue(familyName); return substitute.isEmpty() ? familyName : substitute; } -- cgit v1.2.3