From a890df0283b8c36998d2d8edce341de81024fea9 Mon Sep 17 00:00:00 2001 From: Eskil Abrahamsen Blomfeldt Date: Tue, 3 Oct 2023 08:51:06 +0200 Subject: [PATCH] coretext: Support variable application fonts MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Named instances of variable application fonts are exposed automatically through CTFontManagerCreateFontDescriptorsFromData() (and also for the URL equivalent). The main change here is just to call this instead of the overload which only returns the first font. Note that this also updates the test: This is because the conversion from CoreText normalized weight values to TTF values is not 100%. In the CoreText code, we map Heavy (0.56) to ExtraBold, but ExtraBold gets converted to 0.6, which is closer to the CoreText value for Black (0.62). To avoid hitting this inconsistency, the QtExtraBold has been changed to Black weight instead, which resolves to the same on all platforms. Task-number: QTBUG-108624 Change-Id: Ied6d42e9e3e1ba8b7102936c5be3d285b3d9e07f Reviewed-by: Tor Arne Vestbø Reviewed-by: Qt CI Bot --- .../text/coretext/qcoretextfontdatabase.mm | 24 +++++++++++++----- .../text/coretext/qcoretextfontdatabase_p.h | 1 + .../text/qfontdatabase/tst_qfontdatabase.cpp | 4 +-- .../shared/resources/testfont_variable.ttf | Bin 28528 -> 28528 bytes 4 files changed, 21 insertions(+), 8 deletions(-) diff --git a/src/gui/text/coretext/qcoretextfontdatabase.mm b/src/gui/text/coretext/qcoretextfontdatabase.mm index 7307595bd9b..baf2803d570 100644 --- a/src/gui/text/coretext/qcoretextfontdatabase.mm +++ b/src/gui/text/coretext/qcoretextfontdatabase.mm @@ -714,13 +714,20 @@ QStringList QCoreTextFontDatabase::addApplicationFont(const QByteArray &fontData if (!fontData.isEmpty()) { QCFType fontDataReference = fontData.toRawCFData(); - if (QCFType descriptor = CTFontManagerCreateFontDescriptorFromData(fontDataReference)) { - // There's no way to get the data back out of a font descriptor created with - // CTFontManagerCreateFontDescriptorFromData, so we attach the data manually. - NSDictionary *attributes = @{ kQtFontDataAttribute : [NSValue valueWithPointer:new QByteArray(fontData)] }; - descriptor = CTFontDescriptorCreateCopyWithAttributes(descriptor, (CFDictionaryRef)attributes); + if (QCFType descriptors = CTFontManagerCreateFontDescriptorsFromData(fontDataReference)) { CFMutableArrayRef array = CFArrayCreateMutable(kCFAllocatorDefault, 0, &kCFTypeArrayCallBacks); - CFArrayAppendValue(array, descriptor); + const int count = CFArrayGetCount(descriptors); + + for (int i = 0; i < count; ++i) { + CTFontDescriptorRef descriptor = CTFontDescriptorRef(CFArrayGetValueAtIndex(descriptors, i)); + + // There's no way to get the data back out of a font descriptor created with + // CTFontManagerCreateFontDescriptorFromData, so we attach the data manually. + NSDictionary *attributes = @{ kQtFontDataAttribute : [NSValue valueWithPointer:new QByteArray(fontData)] }; + descriptor = CTFontDescriptorCreateCopyWithAttributes(descriptor, (CFDictionaryRef)attributes); + CFArrayAppendValue(array, descriptor); + } + fonts = array; } } else { @@ -980,5 +987,10 @@ QList QCoreTextFontDatabase::standardSizes() const return ret; } +bool QCoreTextFontDatabase::supportsVariableApplicationFonts() const +{ + return true; +} + QT_END_NAMESPACE diff --git a/src/gui/text/coretext/qcoretextfontdatabase_p.h b/src/gui/text/coretext/qcoretextfontdatabase_p.h index 74c3f30b79c..eeea9ad6407 100644 --- a/src/gui/text/coretext/qcoretextfontdatabase_p.h +++ b/src/gui/text/coretext/qcoretextfontdatabase_p.h @@ -46,6 +46,7 @@ public: QFont defaultFont() const override; bool fontsAlwaysScalable() const override; QList standardSizes() const override; + bool supportsVariableApplicationFonts() const override; // For iOS and macOS platform themes QFont *themeFont(QPlatformTheme::Font) const; diff --git a/tests/auto/gui/text/qfontdatabase/tst_qfontdatabase.cpp b/tests/auto/gui/text/qfontdatabase/tst_qfontdatabase.cpp index fb61037c95d..3995f64e07b 100644 --- a/tests/auto/gui/text/qfontdatabase/tst_qfontdatabase.cpp +++ b/tests/auto/gui/text/qfontdatabase/tst_qfontdatabase.cpp @@ -534,9 +534,9 @@ void tst_QFontDatabase::variableFont() { QFont font(family); - font.setWeight(QFont::ExtraBold); + font.setWeight(QFont::Black); QCOMPARE(QFontInfo(font).styleName(), u"QtExtraBold"_s); - QCOMPARE(QFontInfo(font).weight(), QFont::ExtraBold); + QCOMPARE(QFontInfo(font).weight(), int(QFont::Black)); } { diff --git a/tests/auto/shared/resources/testfont_variable.ttf b/tests/auto/shared/resources/testfont_variable.ttf index 70f764448422294666b5765e3f7f495726499ff0..b3d7be61808a25aec791b4b6b200f79483d1650a 100644 GIT binary patch delta 98 zcmexxkMYAj#tDVY+w-L-me?|%VhNqN#g~z9G83bb%;ni%Z^ZN4d}ZM0U;u#!SA+aO t^h3w=Uz-CM_XaQ;Z{`cy%_`mk5@lpyU;>g8fJ`<