diff options
Diffstat (limited to 'src/gui/text/qfontdatabase.cpp')
-rw-r--r-- | src/gui/text/qfontdatabase.cpp | 118 |
1 files changed, 75 insertions, 43 deletions
diff --git a/src/gui/text/qfontdatabase.cpp b/src/gui/text/qfontdatabase.cpp index 91037fbee4..2011f935a9 100644 --- a/src/gui/text/qfontdatabase.cpp +++ b/src/gui/text/qfontdatabase.cpp @@ -1,12 +1,11 @@ /**************************************************************************** ** -** Copyright (C) 2021 The Qt Company Ltd. +** Copyright (C) 2016 The Qt Company Ltd. ** Contact: https://www.qt.io/licensing/ ** ** This file is part of the QtGui module of the Qt Toolkit. ** -** $QT_BEGIN_LICENSE:COMM$ -** +** $QT_BEGIN_LICENSE:LGPL$ ** Commercial License Usage ** Licensees holding valid commercial Qt licenses may use this file in ** accordance with the commercial license agreement provided with the @@ -15,25 +14,26 @@ ** and conditions see https://www.qt.io/terms-conditions. For further ** information use the contact form at https://www.qt.io/contact-us. ** -** $QT_END_LICENSE$ -** -** -** -** -** -** -** -** -** -** -** -** -** -** -** -** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. ** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or (at your option) the GNU General +** Public license version 3 or any later version approved by the KDE Free +** Qt Foundation. The licenses are as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 +** 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-2.0.html and +** https://www.gnu.org/licenses/gpl-3.0.html. ** +** $QT_END_LICENSE$ ** ****************************************************************************/ @@ -414,6 +414,25 @@ void QtFontFamily::ensurePopulated() Q_ASSERT_X(populated, Q_FUNC_INFO, qPrintable(name)); } +/*! + \internal + + Tests if the given family \a family supports writing system \a writingSystem, + including the special case for Han script mapping to several subsequent writing systems +*/ +static bool familySupportsWritingSystem(QtFontFamily *family, size_t writingSystem) +{ + Q_ASSERT(family != nullptr); + Q_ASSERT(writingSystem != QFontDatabase::Any && writingSystem < QFontDatabase::WritingSystemsCount); + + size_t ws = writingSystem; + do { + if ((family->writingSystems[ws] & QtFontFamily::Supported) != 0) + return true; + } while (writingSystem >= QFontDatabase::SimplifiedChinese && writingSystem <= QFontDatabase::Japanese && ++ws <= QFontDatabase::Japanese); + + return false; +} struct FallbacksCacheKey { QString family; @@ -503,12 +522,10 @@ void QFontDatabasePrivate::invalidate() emit static_cast<QGuiApplication *>(QCoreApplication::instance())->fontDatabaseChanged(); } -QtFontFamily *QFontDatabasePrivate::family(const QString &family, FamilyRequestFlags flags) +QtFontFamily *QFontDatabasePrivate::family(const QString &f, FamilyRequestFlags flags) { QtFontFamily *fam = nullptr; - const QString f = family.trimmed(); - int low = 0; int high = count; int pos = count / 2; @@ -828,7 +845,7 @@ QStringList QPlatformFontDatabase::fallbacksForFamily(const QString &family, QFo f->ensurePopulated(); - if (writingSystem > QFontDatabase::Any && f->writingSystems[writingSystem] != QtFontFamily::Supported) + if (writingSystem > QFontDatabase::Any && !familySupportsWritingSystem(f, writingSystem)) continue; for (int j = 0; j < f->count; ++j) { @@ -1221,9 +1238,13 @@ static bool matchFamilyName(const QString &familyName, QtFontFamily *f) Tries to find the best match for a given request and family/foundry */ -static int match(int script, const QFontDef &request, - const QString &family_name, const QString &foundry_name, - QtFontDesc *desc, const QList<int> &blacklistedFamilies) +static int match(int script, + const QFontDef &request, + const QString &family_name, + const QString &foundry_name, + QtFontDesc *desc, + const QList<int> &blacklistedFamilies, + unsigned int *resultingScore = nullptr) { int result = -1; @@ -1273,8 +1294,10 @@ static int match(int script, const QFontDef &request, test.family->ensurePopulated(); // Check if family is supported in the script we want - if (writingSystem != QFontDatabase::Any && !(test.family->writingSystems[writingSystem] & QtFontFamily::Supported)) + if (writingSystem != QFontDatabase::Any + && !familySupportsWritingSystem(test.family, writingSystem)) { continue; + } // as we know the script is supported, we can be sure // to find a matching font here. @@ -1298,6 +1321,10 @@ static int match(int script, const QFontDef &request, if (newscore < 10) // xlfd instead of FT... just accept it break; } + + if (resultingScore != nullptr) + *resultingScore = score; + return result; } @@ -2482,13 +2509,12 @@ bool QFontDatabasePrivate::isApplicationFont(const QString &fileName) with removeApplicationFont() or to retrieve the list of family names contained in the font. +//! [add-application-font-doc] The function returns -1 if the font could not be loaded. Currently only TrueType fonts, TrueType font collections, and OpenType fonts are supported. - - \note Adding application fonts on Unix/X11 platforms without fontconfig is - currently not supported. +//! [add-application-font-doc] \sa addApplicationFontFromData(), applicationFontFamilies(), removeApplicationFont() */ @@ -2516,12 +2542,7 @@ int QFontDatabase::addApplicationFont(const QString &fileName) with removeApplicationFont() or to retrieve the list of family names contained in the font. - The function returns -1 if the font could not be loaded. - - Currently only TrueType fonts and TrueType font collections are supported. - - \b{Note:} Adding application fonts on Unix/X11 platforms without fontconfig is - currently not supported. + \include qfontdatabase.cpp add-application-font-doc \sa addApplicationFont(), applicationFontFamilies(), removeApplicationFont() */ @@ -2656,7 +2677,9 @@ bool QFontDatabase::supportsThreadedFontRendering() /*! \internal */ -QFontEngine *QFontDatabase::findFont(const QFontDef &request, int script) +QFontEngine *QFontDatabase::findFont(const QFontDef &request, + int script, + bool preferScriptOverFamily) { QMutexLocker locker(fontDatabaseMutex()); @@ -2698,11 +2721,20 @@ QFontEngine *QFontDatabase::findFont(const QFontDef &request, int script) parseFontName(requestFamily, foundry_name, family_name); QtFontDesc desc; QList<int> blackListed; - int index = match(multi ? QChar::Script_Common : script, request, family_name, foundry_name, &desc, blackListed); - if (index < 0 && QGuiApplicationPrivate::platformIntegration()->fontDatabase()->populateFamilyAliases(family_name)) { + unsigned int score = UINT_MAX; + int index = match(multi ? QChar::Script_Common : script, request, family_name, foundry_name, &desc, blackListed, &score); + if (score > 0 && QGuiApplicationPrivate::platformIntegration()->fontDatabase()->populateFamilyAliases(family_name)) { // We populated familiy aliases (e.g. localized families), so try again index = match(multi ? QChar::Script_Common : script, request, family_name, foundry_name, &desc, blackListed); } + + // If we do not find a match and NoFontMerging is set, use the requested font even if it does + // not support the script. + // + // (we do this at the end to prefer foundries that support the script if they exist) + if (index < 0 && !multi && !preferScriptOverFamily) + index = match(QChar::Script_Common, request, family_name, foundry_name, &desc, blackListed); + if (index >= 0) { QFontDef fontDef = request; @@ -2895,10 +2927,8 @@ Q_GUI_EXPORT QStringList qt_sort_families_by_writing_system(QChar::Script script } uint order = i; - if (testFamily == nullptr - || (testFamily->writingSystems[writingSystem] & QtFontFamily::Supported) == 0) { + if (testFamily == nullptr || !familySupportsWritingSystem(testFamily, writingSystem)) order |= 1u << 31; - } supported.insert(order, family); } @@ -2908,3 +2938,5 @@ Q_GUI_EXPORT QStringList qt_sort_families_by_writing_system(QChar::Script script QT_END_NAMESPACE +#include "moc_qfontdatabase.cpp" + |