summaryrefslogtreecommitdiffstats
path: root/tests/auto/gui/text/qfontdatabase/tst_qfontdatabase.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'tests/auto/gui/text/qfontdatabase/tst_qfontdatabase.cpp')
-rw-r--r--tests/auto/gui/text/qfontdatabase/tst_qfontdatabase.cpp306
1 files changed, 236 insertions, 70 deletions
diff --git a/tests/auto/gui/text/qfontdatabase/tst_qfontdatabase.cpp b/tests/auto/gui/text/qfontdatabase/tst_qfontdatabase.cpp
index c2b82059ec..849e7432d1 100644
--- a/tests/auto/gui/text/qfontdatabase/tst_qfontdatabase.cpp
+++ b/tests/auto/gui/text/qfontdatabase/tst_qfontdatabase.cpp
@@ -1,39 +1,22 @@
-/****************************************************************************
-**
-** 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$
-**
-****************************************************************************/
-
-#include <QtTest/QtTest>
+// Copyright (C) 2016 The Qt Company Ltd.
+// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
+
+#include <QTest>
+#include <QSignalSpy>
#include <qfontdatabase.h>
#include <qfontinfo.h>
#include <qfontmetrics.h>
#include <qtextlayout.h>
#include <private/qrawfont_p.h>
+#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")
@@ -79,11 +62,22 @@ private slots:
void registerOpenTypePreferredNamesSystem();
void registerOpenTypePreferredNamesApplication();
+ 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()
@@ -96,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()
@@ -113,8 +109,7 @@ void tst_QFontDatabase::styles()
{
QFETCH( QString, font );
- QFontDatabase fdb;
- QStringList styles = fdb.styles( font );
+ QStringList styles = QFontDatabase::styles( font );
QStringList::Iterator it = styles.begin();
while ( it != styles.end() ) {
QString style = *it;
@@ -151,11 +146,10 @@ void tst_QFontDatabase::fixedPitch()
QFETCH(QString, font);
QFETCH(bool, fixedPitch);
- QFontDatabase fdb;
- if (!fdb.families().contains(font))
+ if (!QFontDatabase::families().contains(font))
QSKIP("Font not installed");
- QCOMPARE(fdb.isFixedPitch(font), fixedPitch);
+ QCOMPARE(QFontDatabase::isFixedPitch(font), fixedPitch);
QFont qfont(font);
QFontInfo fi(qfont);
@@ -184,8 +178,7 @@ void tst_QFontDatabase::trickyFonts()
{
QFETCH(QString, font);
- QFontDatabase fdb;
- if (!fdb.families().contains(font))
+ if (!QFontDatabase::families().contains(font))
QSKIP( "Font not installed");
QFont qfont(font);
@@ -232,9 +225,7 @@ void tst_QFontDatabase::addAppFont()
QFETCH(bool, useMemoryFont);
QSignalSpy fontDbChangedSpy(QGuiApplication::instance(), SIGNAL(fontDatabaseChanged()));
- QFontDatabase db;
-
- const QStringList oldFamilies = db.families();
+ const QStringList oldFamilies = QFontDatabase::families();
QVERIFY(!oldFamilies.isEmpty());
fontDbChangedSpy.clear();
@@ -242,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);
@@ -255,18 +246,18 @@ 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");
const QStringList addedFamilies = QFontDatabase::applicationFontFamilies(id);
QVERIFY(!addedFamilies.isEmpty());
- const QStringList newFamilies = db.families();
+ 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);
@@ -275,9 +266,9 @@ void tst_QFontDatabase::addAppFont()
}
QVERIFY(QFontDatabase::removeApplicationFont(id));
- QCOMPARE(fontDbChangedSpy.count(), 2);
+ QCOMPARE(fontDbChangedSpy.size(), 2);
- QCOMPARE(db.families(), oldFamilies);
+ QVERIFY(QFontDatabase::families().size() <= oldFamilies.size());
}
void tst_QFontDatabase::addTwoAppFontsFromFamily()
@@ -306,15 +297,25 @@ void tst_QFontDatabase::addTwoAppFontsFromFamily()
void tst_QFontDatabase::aliases()
{
- QFontDatabase db;
- const QStringList families = db.families();
+ const QStringList families = QFontDatabase::families();
QVERIFY(!families.isEmpty());
- const QString firstFont = families.front();
- QVERIFY(db.hasFamily(firstFont));
+
+ QString firstFont;
+ for (int i = 0; i < families.size(); ++i) {
+ if (!families.at(i).contains('[')) {
+ firstFont = families.at(i);
+ break;
+ }
+ }
+
+ if (firstFont.isEmpty())
+ QSKIP("Skipped because there are no unambiguous font families on the system.");
+
+ QVERIFY(QFontDatabase::hasFamily(firstFont));
const QString alias = QStringLiteral("AliasToFirstFont") + firstFont;
- QVERIFY(!db.hasFamily(alias));
+ QVERIFY(!QFontDatabase::hasFamily(alias));
QPlatformFontDatabase::registerAliasToFontFamily(firstFont, alias);
- QVERIFY(db.hasFamily(alias));
+ QVERIFY(QFontDatabase::hasFamily(alias));
}
void tst_QFontDatabase::fallbackFonts()
@@ -328,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());
@@ -343,6 +344,28 @@ static QString testString()
return QStringLiteral("foo bar");
}
+void tst_QFontDatabase::stretchRespected()
+{
+ int italicId = QFontDatabase::addApplicationFont(m_testFontItalic);
+ QVERIFY(italicId != -1);
+
+ QVERIFY(!QFontDatabase::applicationFontFamilies(italicId).isEmpty());
+
+ QString italicFontName = QFontDatabase::applicationFontFamilies(italicId).first();
+
+ QFont italicFont = QFontDatabase::font(italicFontName,
+ QString::fromLatin1("Italic"), 14);
+ QVERIFY(italicFont.italic());
+
+ QFont italicStretchedFont = italicFont;
+ italicStretchedFont.setStretch( 400 );
+
+ QVERIFY(QFontMetricsF(italicFont).horizontalAdvance(QStringLiteral("foobar")) <
+ QFontMetricsF(italicStretchedFont).horizontalAdvance(QStringLiteral("foobar")));
+
+ QFontDatabase::removeApplicationFont(italicId);
+}
+
void tst_QFontDatabase::condensedFontWidthNoFontMerging()
{
int regularFontId = QFontDatabase::addApplicationFont(m_testFont);
@@ -368,12 +391,11 @@ void tst_QFontDatabase::condensedFontWidthNoFontMerging()
void tst_QFontDatabase::condensedFontWidth()
{
- QFontDatabase db;
QFontDatabase::addApplicationFont(m_testFont);
QFontDatabase::addApplicationFont(m_testFontCondensed);
- QVERIFY(db.hasFamily("QtBidiTestFont"));
- if (!db.hasFamily("QtBidiTestFontCondensed"))
+ QVERIFY(QFontDatabase::hasFamily("QtBidiTestFont"));
+ if (!QFontDatabase::hasFamily("QtBidiTestFontCondensed"))
QSKIP("This platform doesn't support font sub-family names (QTBUG-55625)");
// Test we really get a condensed font, and a not renormalized one (QTBUG-48043):
@@ -387,10 +409,9 @@ void tst_QFontDatabase::condensedFontWidth()
void tst_QFontDatabase::condensedFontMatching()
{
- QFontDatabase db;
QFontDatabase::removeAllApplicationFonts();
QFontDatabase::addApplicationFont(m_testFontCondensed);
- if (!db.hasFamily("QtBidiTestFont"))
+ if (!QFontDatabase::hasFamily("QtBidiTestFont"))
QSKIP("This platform doesn't support preferred font family names (QTBUG-53478)");
QFontDatabase::addApplicationFont(m_testFont);
@@ -401,16 +422,19 @@ void tst_QFontDatabase::condensedFontMatching()
tfcByStyleName.setStyleName("Condensed");
#ifdef Q_OS_WIN
- QEXPECT_FAIL("","No matching of sub-family by stretch on Windows", Continue);
+ QFont f;
+ f.setStyleStrategy(QFont::NoFontMerging);
+ QFontPrivate *font_d = QFontPrivate::get(f);
+ 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()));
- if (!db.hasFamily("QtBidiTestFontCondensed"))
+ if (!QFontDatabase::hasFamily("QtBidiTestFontCondensed"))
QSKIP("This platform doesn't support font sub-family names (QTBUG-55625)");
QFont tfcBySubfamilyName("QtBidiTestFontCondensed");
@@ -445,29 +469,171 @@ void tst_QFontDatabase::smoothFonts()
void tst_QFontDatabase::registerOpenTypePreferredNamesSystem()
{
- QFontDatabase db;
// This font family was picked because it was the only one I had installed which showcased the
// problem
- if (!db.hasFamily(QString::fromLatin1("Source Code Pro ExtraLight")))
+ if (!QFontDatabase::hasFamily(QString::fromLatin1("Source Code Pro ExtraLight")))
QSKIP("Source Code Pro ExtraLight is not installed");
- QStringList styles = db.styles(QString::fromLatin1("Source Code Pro"));
+ QStringList styles = QFontDatabase::styles(QString::fromLatin1("Source Code Pro"));
QVERIFY(styles.contains(QLatin1String("ExtraLight")));
}
void tst_QFontDatabase::registerOpenTypePreferredNamesApplication()
{
- QFontDatabase db;
-
int id = QFontDatabase::addApplicationFont(QString::fromLatin1(":/testfont_open.otf"));
if (id == -1)
QSKIP("Skip the test since app fonts are not supported on this system");
- QStringList styles = db.styles(QString::fromLatin1("QtBidiTestFont"));
+ QStringList styles = QFontDatabase::styles(QString::fromLatin1("QtBidiTestFont"));
QVERIFY(styles.contains(QLatin1String("Open")));
QFontDatabase::removeApplicationFont(id);
}
+#ifdef Q_OS_WIN
+void tst_QFontDatabase::findCourier()
+{
+ QFont font = QFontDatabase::font(u"Courier"_s, u""_s, 16);
+ QFontInfo info(font);
+ QCOMPARE(info.family(), u"Courier New"_s);
+ QCOMPARE(info.pointSize(), 16);
+
+ font = QFontDatabase::font("Courier", "", 64);
+ info = font;
+ 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"_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"_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"