Default implementation for QPlatformFontDatabase::fallbacksForFamily()

...mainly for platforms that do not provide a native/unified way
to obtain system-defined font fallbacks list
(ie !CoreText && !FontConfig).

Change-Id: I23c5589d79ddecb6311ccc52ec8b29977f06d408
Reviewed-by: Eskil Abrahamsen Blomfeldt <eskil.abrahamsen-blomfeldt@theqtcompany.com>
This commit is contained in:
Konstantin Ritt 2015-05-18 09:27:10 +04:00
parent 735cd0ccf3
commit 5e3e34731b
8 changed files with 60 additions and 48 deletions

View File

@ -756,6 +756,46 @@ QString qt_resolveFontFamilyAlias(const QString &alias)
return alias;
}
QStringList QPlatformFontDatabase::fallbacksForFamily(const QString &family, QFont::Style style, QFont::StyleHint styleHint, QChar::Script script) const
{
Q_UNUSED(family);
Q_UNUSED(styleHint);
QStringList retList;
size_t writingSystem = std::find(scriptForWritingSystem,
scriptForWritingSystem + QFontDatabase::WritingSystemsCount,
script) - scriptForWritingSystem;
if (writingSystem >= QFontDatabase::WritingSystemsCount)
writingSystem = QFontDatabase::Any;
QFontDatabasePrivate *db = privateDb();
for (int i = 0; i < db->count; ++i) {
QtFontFamily *f = db->families[i];
f->ensurePopulated();
if (writingSystem > QFontDatabase::Any && f->writingSystems[writingSystem] != QtFontFamily::Supported)
continue;
for (int j = 0; j < f->count; ++j) {
QtFontFoundry *foundry = f->foundries[j];
for (int k = 0; k < foundry->count; ++k) {
if (style == foundry->styles[k]->key.style) {
if (foundry->name.isEmpty())
retList.append(f->name);
else
retList.append(f->name + QLatin1String(" [") + foundry->name + QLatin1Char(']'));
break;
}
}
}
}
return retList;
}
QStringList qt_fallbacksForFamily(const QString &family, QFont::Style style, QFont::StyleHint styleHint, QChar::Script script)
{
// make sure that the db has all fallback families

View File

@ -343,17 +343,15 @@ QFontEngine *QPlatformFontDatabase::fontEngine(const QByteArray &fontData, qreal
}
/*!
\fn QStringList QPlatformFontDatabase::fallbacksForFamily(const QString &family, QFont::Style style, QFont::StyleHint styleHint, QChar::Script script) const
Returns a list of alternative fonts for the specified \a family and
\a style and \a script using the \a styleHint given.
Default implementation returns a list of fonts for which \a style and \a script support
has been reported during the font database population.
*/
QStringList QPlatformFontDatabase::fallbacksForFamily(const QString &family, QFont::Style style, QFont::StyleHint styleHint, QChar::Script script) const
{
Q_UNUSED(family);
Q_UNUSED(style);
Q_UNUSED(styleHint);
Q_UNUSED(script);
return QStringList();
}
// implemented in qfontdatabase.cpp
/*!
Adds an application font described by the font contained supplied \a fontData

View File

@ -176,15 +176,6 @@ QFontEngine *QBasicFontDatabase::fontEngine(const QByteArray &fontData, qreal pi
return fe;
}
QStringList QBasicFontDatabase::fallbacksForFamily(const QString &family, QFont::Style style, QFont::StyleHint styleHint, QChar::Script script) const
{
Q_UNUSED(family);
Q_UNUSED(style);
Q_UNUSED(script);
Q_UNUSED(styleHint);
return QStringList();
}
QStringList QBasicFontDatabase::addApplicationFont(const QByteArray &fontData, const QString &fileName)
{
return addTTFile(fontData,fileName.toLocal8Bit());

View File

@ -63,7 +63,6 @@ public:
void populateFontDatabase() Q_DECL_OVERRIDE;
QFontEngine *fontEngine(const QFontDef &fontDef, void *handle) Q_DECL_OVERRIDE;
QFontEngine *fontEngine(const QByteArray &fontData, qreal pixelSize, QFont::HintingPreference hintingPreference) Q_DECL_OVERRIDE;
QStringList fallbacksForFamily(const QString &family, QFont::Style style, QFont::StyleHint styleHint, QChar::Script script) const Q_DECL_OVERRIDE;
QStringList addApplicationFont(const QByteArray &fontData, const QString &fileName) Q_DECL_OVERRIDE;
void releaseHandle(void *handle) Q_DECL_OVERRIDE;

View File

@ -55,16 +55,7 @@ void QAndroidPlatformFontDatabase::populateFontDatabase()
QList<QFileInfo> entries = dir.entryInfoList(QStringList() << QStringLiteral("*.ttf") << QStringLiteral("*.otf"), QDir::Files);
for (int i = 0; i < int(entries.count()); ++i) {
const QByteArray file = QFile::encodeName(entries.at(i).absoluteFilePath());
QSupportedWritingSystems supportedWritingSystems;
QStringList families = addTTFile(QByteArray(), file, &supportedWritingSystems);
extern int qt_script_for_writing_system(QFontDatabase::WritingSystem writingSystem);
for (int i = 0; i < QFontDatabase::WritingSystemsCount; ++i) {
if (i == QFontDatabase::Any || supportedWritingSystems.supported(QFontDatabase::WritingSystem(i))) {
QChar::Script script = QChar::Script(qt_script_for_writing_system(QFontDatabase::WritingSystem(i)));
m_fallbacks[script] += families;
}
}
QBasicFontDatabase::addTTFile(QByteArray(), file);
}
}
@ -73,15 +64,16 @@ QStringList QAndroidPlatformFontDatabase::fallbacksForFamily(const QString &fami
QFont::StyleHint styleHint,
QChar::Script script) const
{
Q_UNUSED(family);
Q_UNUSED(style);
QStringList result;
if (styleHint == QFont::Monospace || styleHint == QFont::Courier)
return QString(qgetenv("QT_ANDROID_FONTS_MONOSPACE")).split(";") + m_fallbacks[script];
result.append(QString(qgetenv("QT_ANDROID_FONTS_MONOSPACE")).split(";"));
else if (styleHint == QFont::Serif)
return QString(qgetenv("QT_ANDROID_FONTS_SERIF")).split(";") + m_fallbacks[script];
result.append(QString(qgetenv("QT_ANDROID_FONTS_SERIF")).split(";"));
else
result.append(QString(qgetenv("QT_ANDROID_FONTS")).split(";"));
result.append(QPlatformFontDatabase::fallbacksForFamily(family, style, styleHint, script));
return QString(qgetenv("QT_ANDROID_FONTS")).split(";") + m_fallbacks[script];
return result;
}
QT_END_NAMESPACE

View File

@ -47,9 +47,6 @@ public:
QFont::Style style,
QFont::StyleHint styleHint,
QChar::Script script) const;
private:
QHash<QChar::Script, QStringList> m_fallbacks;
};
QT_END_NAMESPACE

View File

@ -1080,11 +1080,7 @@ QWindowsFontDatabase::~QWindowsFontDatabase()
QFontEngineMulti *QWindowsFontDatabase::fontEngineMulti(QFontEngine *fontEngine, QChar::Script script)
{
if (script == QChar::Script_Common)
return new QWindowsMultiFontEngine(fontEngine, script);
// ### as long as fallbacksForFamily() does not take script parameter into account,
// prefer QFontEngineMulti's loadEngine() implementation for complex scripts
return QPlatformFontDatabase::fontEngineMulti(fontEngine, script);
}
QFontEngine * QWindowsFontDatabase::fontEngine(const QFontDef &fontDef, void *handle)
@ -1666,11 +1662,10 @@ QString QWindowsFontDatabase::familyForStyleHint(QFont::StyleHint styleHint)
QStringList QWindowsFontDatabase::fallbacksForFamily(const QString &family, QFont::Style style, QFont::StyleHint styleHint, QChar::Script script) const
{
QStringList result = QPlatformFontDatabase::fallbacksForFamily(family, style, styleHint, script);
if (!result.isEmpty())
return result;
QStringList result;
result.append(QWindowsFontDatabase::familyForStyleHint(styleHint));
result.append(QWindowsFontDatabase::extraTryFontsForFamily(family));
result.append(QPlatformFontDatabase::fallbacksForFamily(family, style, styleHint, script));
qCDebug(lcQpaFonts) << __FUNCTION__ << family << style << styleHint
<< script << result;

View File

@ -661,9 +661,7 @@ QFontEngine *QWindowsFontDatabaseFT::fontEngine(const QByteArray &fontData, qrea
QStringList QWindowsFontDatabaseFT::fallbacksForFamily(const QString &family, QFont::Style style, QFont::StyleHint styleHint, QChar::Script script) const
{
QStringList result = QPlatformFontDatabase::fallbacksForFamily(family, style, styleHint, script);
if (!result.isEmpty())
return result;
QStringList result;
result.append(QWindowsFontDatabase::familyForStyleHint(styleHint));
@ -679,6 +677,8 @@ QStringList QWindowsFontDatabaseFT::fallbacksForFamily(const QString &family, QF
result.append(QWindowsFontDatabase::extraTryFontsForFamily(family));
result.append(QPlatformFontDatabase::fallbacksForFamily(family, style, styleHint, script));
qCDebug(lcQpaFonts) << __FUNCTION__ << family << style << styleHint
<< script << result;