QString QFont::defaultFamily() const
{
- QString familyName;
- switch(d->request.styleHint) {
- case QFont::SansSerif:
- familyName = QString::fromLatin1("sans-serif");
- break;
- case QFont::Serif:
- familyName = QString::fromLatin1("serif");
- break;
- case QFont::TypeWriter:
- case QFont::Monospace:
- familyName = QString::fromLatin1("monospace");
- break;
- case QFont::Cursive:
- familyName = QString::fromLatin1("cursive");
- break;
- case QFont::Fantasy:
- familyName = QString::fromLatin1("fantasy");
- break;
- case QFont::Decorative:
- familyName = QString::fromLatin1("decorative");
- break;
- case QFont::System:
- default:
- familyName = QString();
- break;
- }
-
- return QGuiApplicationPrivate::platformIntegration()->fontDatabase()->resolveFontFamilyAlias(familyName);
-
+ QPlatformFontDatabase *fontDB = QGuiApplicationPrivate::platformIntegration()->fontDatabase();
+ const QStringList fallbacks = fontDB->fallbacksForFamily(QString(), QFont::StyleNormal
+ , QFont::StyleHint(d->request.styleHint), QUnicodeTables::Common);
+ if (!fallbacks.isEmpty())
+ return fallbacks.first();
+ return QString();
}
QString QFont::lastResortFamily() const
QStringList result = QPlatformFontDatabase::fallbacksForFamily(family, style, styleHint, script);
if (!result.isEmpty())
return result;
+
+ switch (styleHint) {
+ case QFont::Times:
+ result << QString::fromLatin1("Times New Roman");
+ break;
+ case QFont::Courier:
+ result << QString::fromLatin1("Courier New");
+ break;
+ case QFont::Monospace:
+ result << QString::fromLatin1("Courier New");
+ break;
+ case QFont::Cursive:
+ result << QString::fromLatin1("Comic Sans MS");
+ break;
+ case QFont::Fantasy:
+ result << QString::fromLatin1("Impact");
+ break;
+ case QFont::Decorative:
+ result << QString::fromLatin1("Old English");
+ break;
+ case QFont::Helvetica:
+ case QFont::System:
+ default:
+ result << QString::fromLatin1("Arial");
+ }
+
if (QWindowsContext::verboseFonts)
qDebug() << __FUNCTION__ << family << style << styleHint
<< script << result << m_families.size();
QStringList result = QPlatformFontDatabase::fallbacksForFamily(family, style, styleHint, script);
if (!result.isEmpty())
return result;
+
+ switch (styleHint) {
+ case QFont::Times:
+ result << QString::fromLatin1("Times New Roman");
+ break;
+ case QFont::Courier:
+ result << QString::fromLatin1("Courier New");
+ break;
+ case QFont::Monospace:
+ result << QString::fromLatin1("Courier New");
+ break;
+ case QFont::Cursive:
+ result << QString::fromLatin1("Comic Sans MS");
+ break;
+ case QFont::Fantasy:
+ result << QString::fromLatin1("Impact");
+ break;
+ case QFont::Decorative:
+ result << QString::fromLatin1("Old English");
+ break;
+ case QFont::Helvetica:
+ case QFont::System:
+ default:
+ result << QString::fromLatin1("Arial");
+ }
if (QWindowsContext::verboseFonts)
qDebug() << __FUNCTION__ << family << style << styleHint
<< script << result << m_families;
void serializeSpacing();
void lastResortFont();
void styleName();
-#ifdef QT_BUILD_INTERNAL
void defaultFamily_data();
void defaultFamily();
-#endif
};
// Testing get/set functions
#endif
}
-#ifdef QT_BUILD_INTERNAL
+QString getPlatformGenericFont(const char* genericName)
+{
+#if defined(Q_OS_UNIX) && !defined(QT_NO_FONTCONFIG)
+ QProcess p;
+ p.start(QLatin1String("fc-match"), (QStringList() << "-f%{family}" << genericName));
+ if (!p.waitForStarted())
+ qWarning("fc-match cannot be started: %s", qPrintable(p.errorString()));
+ if (p.waitForFinished())
+ return QString::fromLatin1(p.readAllStandardOutput());
+#endif
+ return QLatin1String(genericName);
+}
+
+static inline QByteArray msgNotAcceptableFont(const QString &defaultFamily, const QStringList &acceptableFamilies)
+{
+ QString res = QString::fromLatin1("Font family '%1' is not one of the following accaptable results: ").arg(defaultFamily);
+ Q_FOREACH (const QString &family, acceptableFamilies)
+ res += QString::fromLatin1("\n %1").arg(family);
+ return res.toLocal8Bit();
+}
+
Q_DECLARE_METATYPE(QFont::StyleHint)
void tst_QFont::defaultFamily_data()
{
QTest::addColumn<QFont::StyleHint>("styleHint");
- QTest::addColumn<QString>("defaultFamily");
-
- QTest::newRow("serif") << QFont::Times << "serif";
- QTest::newRow("monospace") << QFont::Monospace << "monospace";
- QTest::newRow("sans-serif") << QFont::SansSerif << "sans-serif";
- QTest::newRow("cursive") << QFont::Cursive << "cursive";
- QTest::newRow("fantasy") << QFont::Fantasy << "fantasy";
- QTest::newRow("old english") << QFont::OldEnglish << "Old English";
+ QTest::addColumn<QStringList>("acceptableFamilies");
+
+ QTest::newRow("serif") << QFont::Serif << (QStringList() << "Times New Roman" << "Times" << getPlatformGenericFont("serif"));
+ QTest::newRow("monospace") << QFont::Monospace << (QStringList() << "Courier New" << "Monaco" << getPlatformGenericFont("monospace"));
+ QTest::newRow("cursive") << QFont::Cursive << (QStringList() << "Comic Sans MS" << "Apple Chancery" << getPlatformGenericFont("cursive"));
+ QTest::newRow("fantasy") << QFont::Fantasy << (QStringList() << "Impact" << "Zapfino" << getPlatformGenericFont("fantasy"));
+ QTest::newRow("sans-serif") << QFont::SansSerif << (QStringList() << "Arial" << "Lucida Grande" << getPlatformGenericFont("sans-serif"));
}
void tst_QFont::defaultFamily()
{
QFETCH(QFont::StyleHint, styleHint);
- QFETCH(QString, defaultFamily);
-
- QFontDatabase db;
- if (!db.hasFamily(defaultFamily))
- QSKIP("Font family is not available on the system");
+ QFETCH(QStringList, acceptableFamilies);
QFont f;
+ QFontDatabase db;
f.setStyleHint(styleHint);
- QCOMPARE(QFontDatabase::resolveFontFamilyAlias(f.defaultFamily()), QFontDatabase::resolveFontFamilyAlias(defaultFamily));
+ const QString familyForHint(f.defaultFamily());
+ // it should at least return a family that is available.
+ QVERIFY(db.hasFamily(familyForHint));
+
+ bool isAcceptable = false;
+ Q_FOREACH (const QString& family, acceptableFamilies) {
+ if (!familyForHint.compare(family, Qt::CaseInsensitive)) {
+ isAcceptable = true;
+ break;
+ }
+ }
+ QVERIFY2(isAcceptable, msgNotAcceptableFont(familyForHint, acceptableFamilies));
}
-#endif // QT_BUILD_INTERNAL
QTEST_MAIN(tst_QFont)
#include "tst_qfont.moc"