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: [PATCH] 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 --- .../mac/qcoretextfontdatabase.mm | 180 ++++++++++-------- .../mac/qcoretextfontdatabase_p.h | 2 +- 2 files changed, 105 insertions(+), 77 deletions(-) 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); +} + +CTFontDescriptorRef descriptorForFontType(CTFontUIFontType uiType) +{ + static const CGFloat kDefaultSizeForRequestedUIType = 0.0; + QCFType ctFont = CTFontCreateUIFontForLanguage( + uiType, kDefaultSizeForRequestedUIType, nullptr); + return CTFontCopyFontDescriptor(ctFont); +} + +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 } - - QCFType cascadeList = CFArrayRef(CTFontCopyDefaultCascadeListForLanguages(font, - (CFArrayRef)[NSUserDefaults.standardUserDefaults stringArrayForKey:@"AppleLanguages"])); - if (!cascadeList) { - qCWarning(lcQpaFonts) << "Failed to create fallback cascade list for" << family; - return QStringList(); - } - - 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)); - } - - 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;