diff options
Diffstat (limited to 'tests/auto/gui/text/qfontdatabase/tst_qfontdatabase.cpp')
-rw-r--r-- | tests/auto/gui/text/qfontdatabase/tst_qfontdatabase.cpp | 192 |
1 files changed, 148 insertions, 44 deletions
diff --git a/tests/auto/gui/text/qfontdatabase/tst_qfontdatabase.cpp b/tests/auto/gui/text/qfontdatabase/tst_qfontdatabase.cpp index 5a18260ffc..849e7432d1 100644 --- a/tests/auto/gui/text/qfontdatabase/tst_qfontdatabase.cpp +++ b/tests/auto/gui/text/qfontdatabase/tst_qfontdatabase.cpp @@ -1,30 +1,5 @@ -/**************************************************************************** -** -** Copyright (C) 2016 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the test suite of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:GPL-EXCEPT$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 3 as published by the Free Software -** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT -** included in the packaging of this file. Please review the following -** information to ensure the GNU General Public License requirements will -** be met: https://www.gnu.org/licenses/gpl-3.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ +// Copyright (C) 2016 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only #include <QTest> #include <QSignalSpy> @@ -37,6 +12,11 @@ #include <private/qfont_p.h> #include <private/qfontengine_p.h> #include <qpa/qplatformfontdatabase.h> +#include <qpa/qplatformintegration.h> + +#include <QtGui/private/qguiapplication_p.h> + +using namespace Qt::StringLiterals; Q_LOGGING_CATEGORY(lcTests, "qt.text.tests") @@ -84,15 +64,20 @@ private slots: void stretchRespected(); + void variableFont(); + #ifdef Q_OS_WIN void findCourier(); #endif + void addApplicationFontFallback(); + private: QString m_ledFont; QString m_testFont; QString m_testFontCondensed; QString m_testFontItalic; + QString m_testFontVariable; }; tst_QFontDatabase::tst_QFontDatabase() @@ -105,10 +90,12 @@ void tst_QFontDatabase::initTestCase() m_testFont = QFINDTESTDATA("testfont.ttf"); m_testFontCondensed = QFINDTESTDATA("testfont_condensed.ttf"); m_testFontItalic = QFINDTESTDATA("testfont_italic.ttf"); + m_testFontVariable = QFINDTESTDATA("testfont_variable.ttf"); QVERIFY(!m_ledFont.isEmpty()); QVERIFY(!m_testFont.isEmpty()); QVERIFY(!m_testFontCondensed.isEmpty()); QVERIFY(!m_testFontItalic.isEmpty()); + QVERIFY(!m_testFontVariable.isEmpty()); } void tst_QFontDatabase::styles_data() @@ -246,7 +233,7 @@ void tst_QFontDatabase::addAppFont() int id; if (useMemoryFont) { QFile fontfile(m_ledFont); - fontfile.open(QIODevice::ReadOnly); + QVERIFY(fontfile.open(QIODevice::ReadOnly)); QByteArray fontdata = fontfile.readAll(); QVERIFY(!fontdata.isEmpty()); id = QFontDatabase::addApplicationFontFromData(fontdata); @@ -259,7 +246,7 @@ void tst_QFontDatabase::addAppFont() QCOMPARE(id, -1); return; #endif - QCOMPARE(fontDbChangedSpy.count(), 1); + QCOMPARE(fontDbChangedSpy.size(), 1); if (id == -1) QSKIP("Skip the test since app fonts are not supported on this system"); @@ -268,9 +255,9 @@ void tst_QFontDatabase::addAppFont() const QStringList newFamilies = QFontDatabase::families(); QVERIFY(!newFamilies.isEmpty()); - QVERIFY(newFamilies.count() >= oldFamilies.count()); + QVERIFY(newFamilies.size() >= oldFamilies.size()); - for (int i = 0; i < addedFamilies.count(); ++i) { + for (int i = 0; i < addedFamilies.size(); ++i) { QString family = addedFamilies.at(i); QVERIFY(newFamilies.contains(family)); QFont qfont(family); @@ -279,9 +266,9 @@ void tst_QFontDatabase::addAppFont() } QVERIFY(QFontDatabase::removeApplicationFont(id)); - QCOMPARE(fontDbChangedSpy.count(), 2); + QCOMPARE(fontDbChangedSpy.size(), 2); - QVERIFY(QFontDatabase::families().count() <= oldFamilies.count()); + QVERIFY(QFontDatabase::families().size() <= oldFamilies.size()); } void tst_QFontDatabase::addTwoAppFontsFromFamily() @@ -342,8 +329,8 @@ void tst_QFontDatabase::fallbackFonts() layout.createLine(); layout.endLayout(); - QList<QGlyphRun> runs = layout.glyphRuns(0, 1); - foreach (QGlyphRun run, runs) { + const QList<QGlyphRun> runs = layout.glyphRuns(0, 1); + for (QGlyphRun run : runs) { QRawFont rawFont = run.rawFont(); QVERIFY(rawFont.isValid()); @@ -438,13 +425,12 @@ void tst_QFontDatabase::condensedFontMatching() QFont f; f.setStyleStrategy(QFont::NoFontMerging); QFontPrivate *font_d = QFontPrivate::get(f); - if (font_d->engineForScript(QChar::Script_Common)->type() != QFontEngine::Freetype) + if (font_d->engineForScript(QChar::Script_Common)->type() != QFontEngine::Freetype + && font_d->engineForScript(QChar::Script_Common)->type() != QFontEngine::DirectWrite) { QEXPECT_FAIL("","No matching of sub-family by stretch on Windows", Continue); + } #endif -#ifdef Q_OS_ANDROID - QEXPECT_FAIL("", "QTBUG-69216", Continue); -#endif QCOMPARE(QFontMetrics(tfcByStretch).horizontalAdvance(testString()), QFontMetrics(tfcByStyleName).horizontalAdvance(testString())); @@ -507,29 +493,147 @@ void tst_QFontDatabase::registerOpenTypePreferredNamesApplication() #ifdef Q_OS_WIN void tst_QFontDatabase::findCourier() { - QFont font = QFontDatabase::font(u"Courier"_qs, u""_qs, 16); + QFont font = QFontDatabase::font(u"Courier"_s, u""_s, 16); QFontInfo info(font); - QCOMPARE(info.family(), u"Courier New"_qs); + QCOMPARE(info.family(), u"Courier New"_s); QCOMPARE(info.pointSize(), 16); font = QFontDatabase::font("Courier", "", 64); info = font; - QCOMPARE(info.family(), u"Courier New"_qs); + QCOMPARE(info.family(), u"Courier New"_s); QCOMPARE(info.pointSize(), 64); // By setting "PreferBitmap" we should get Courier itself. font.setStyleStrategy(QFont::PreferBitmap); info = font; - QCOMPARE(info.family(), u"Courier"_qs); + QCOMPARE(info.family(), u"Courier"_s); // Which has an upper bound on point size QCOMPARE(info.pointSize(), 19); font.setStyleStrategy(QFont::PreferDefault); info = font; - QCOMPARE(info.family(), u"Courier New"_qs); + QCOMPARE(info.family(), u"Courier New"_s); QCOMPARE(info.pointSize(), 64); } #endif +void tst_QFontDatabase::variableFont() +{ + { + QPlatformFontDatabase *pfdb = QGuiApplicationPrivate::platformIntegration()->fontDatabase(); + if (!pfdb->supportsVariableApplicationFonts()) + QSKIP("Variable application fonts not supported on this platform"); + } + + int id = QFontDatabase::addApplicationFont(m_testFontVariable); + if (id == -1) + QSKIP("Skip the test since app fonts are not supported on this system"); + + QString family = QFontDatabase::applicationFontFamilies(id).first(); + { + QFont font(family); + QCOMPARE(QFontInfo(font).styleName(), u"Regular"_s); + QCOMPARE(QFontInfo(font).weight(), QFont::Normal); + } + + { + QFont font(family); + font.setWeight(QFont::Black); + QCOMPARE(QFontInfo(font).styleName(), u"QtExtraBold"_s); + QCOMPARE(QFontInfo(font).weight(), int(QFont::Black)); + } + + { + QFont regularFont(family); + QFont extraBoldFont(family); + extraBoldFont.setStyleName(u"QtExtraBold"_s); + + QFontMetricsF regularFm(regularFont); + QFontMetricsF extraBoldFm(extraBoldFont); + + QVERIFY(regularFm.horizontalAdvance(QLatin1Char('1')) < extraBoldFm.horizontalAdvance(QLatin1Char('1'))); + } + + QFontDatabase::removeApplicationFont(id); +} + +void tst_QFontDatabase::addApplicationFontFallback() +{ + int ledId = -1; + int id = -1; + auto cleanup = qScopeGuard([&id, &ledId] { + if (id >= 0) + QFontDatabase::removeApplicationFont(id); + if (ledId >= 0) + QFontDatabase::removeApplicationFont(ledId); + }); + + const QChar hebrewChar(0x05D0); // Hebrew 'aleph' + + ledId = QFontDatabase::addApplicationFont(m_ledFont); + if (ledId < 0) + QSKIP("Skip the test since app fonts are not supported on this system"); + + auto getHebrewFont = [&]() { + QTextLayout layout; + layout.setText(hebrewChar); + layout.setFont(QFont(u"LED Real"_s)); + layout.beginLayout(); + layout.createLine(); + layout.endLayout(); + + QList<QGlyphRun> glyphRuns = layout.glyphRuns(); + if (glyphRuns.isEmpty()) + return QString{}; + + return glyphRuns.first().rawFont().familyName(); + }; + + QString defaultHebrewFont = getHebrewFont(); + if (defaultHebrewFont.isEmpty()) + QSKIP("Skip the test since Hebrew is not supported on this system"); + + QVERIFY(QFontDatabase::applicationFallbackFontFamilies(QChar::Script_Hebrew).isEmpty()); + QFontDatabase::addApplicationFallbackFontFamily(QChar::Script_Hebrew, u"QtBidiTestFont"_s); + + QCOMPARE(QFontDatabase::applicationFallbackFontFamilies(QChar::Script_Hebrew).size(), 1); + QCOMPARE(QFontDatabase::applicationFallbackFontFamilies(QChar::Script_Hebrew).first(), u"QtBidiTestFont"_s); + + { + QString hebrewFontNow = getHebrewFont(); + QCOMPARE(hebrewFontNow, defaultHebrewFont); + } + + id = QFontDatabase::addApplicationFont(m_testFont); + QVERIFY(id >= 0); + + { + QString hebrewFontNow = getHebrewFont(); + QCOMPARE(hebrewFontNow, u"QtBidiTestFont"_s); + } + + QFontDatabase::removeApplicationFallbackFontFamily(QChar::Script_Hebrew, u"QtBidiTestFont"_s); + + { + QString hebrewFontNow = getHebrewFont(); + QCOMPARE(hebrewFontNow, defaultHebrewFont); + } + + QFontDatabase::setApplicationFallbackFontFamilies(QChar::Script_Hebrew, QStringList(u"QtBidiTestFont"_s)); + + { + QString hebrewFontNow = getHebrewFont(); + QCOMPARE(hebrewFontNow, u"QtBidiTestFont"_s); + } + + QFontDatabase::setApplicationFallbackFontFamilies(QChar::Script_Hebrew, QStringList{}); + + { + QString hebrewFontNow = getHebrewFont(); + QCOMPARE(hebrewFontNow, defaultHebrewFont); + } + +} + QTEST_MAIN(tst_QFontDatabase) #include "tst_qfontdatabase.moc" |