diff options
Diffstat (limited to 'src/gui')
-rw-r--r-- | src/gui/.prev_CMakeLists.txt | 3 | ||||
-rw-r--r-- | src/gui/CMakeLists.txt | 2 | ||||
-rw-r--r-- | src/gui/text/qfontdatabase.cpp | 237 | ||||
-rw-r--r-- | src/gui/text/qfontdatabase_p.h | 279 | ||||
-rw-r--r-- | src/gui/text/qplatformfontdatabase.cpp | 14 | ||||
-rw-r--r-- | src/gui/text/qplatformfontdatabase.h | 3 | ||||
-rw-r--r-- | src/gui/text/text.pri | 1 |
7 files changed, 328 insertions, 211 deletions
diff --git a/src/gui/.prev_CMakeLists.txt b/src/gui/.prev_CMakeLists.txt index 36e53a0edc..fe6ed3d80a 100644 --- a/src/gui/.prev_CMakeLists.txt +++ b/src/gui/.prev_CMakeLists.txt @@ -115,7 +115,6 @@ qt_add_module(Gui painting/qgrayraster.c painting/qgrayraster_p.h painting/qicc.cpp painting/qicc_p.h painting/qimagescale.cpp - painting/qmatrix.cpp painting/qmatrix.h painting/qmemrotate.cpp painting/qmemrotate_p.h painting/qoutlinemapper.cpp painting/qoutlinemapper_p.h painting/qpagedpaintdevice.cpp painting/qpagedpaintdevice.h painting/qpagedpaintdevice_p.h @@ -161,7 +160,7 @@ qt_add_module(Gui text/qabstracttextdocumentlayout.cpp text/qabstracttextdocumentlayout.h text/qabstracttextdocumentlayout_p.h text/qdistancefield.cpp text/qdistancefield_p.h text/qfont.cpp text/qfont.h text/qfont_p.h - text/qfontdatabase.cpp text/qfontdatabase.h + text/qfontdatabase.cpp text/qfontdatabase.h text/qfontdatabase_p.h text/qfontengine.cpp text/qfontengine_p.h text/qfontengine_qpf2.cpp text/qfontengineglyphcache.cpp text/qfontengineglyphcache_p.h diff --git a/src/gui/CMakeLists.txt b/src/gui/CMakeLists.txt index b3108529c2..f89881210b 100644 --- a/src/gui/CMakeLists.txt +++ b/src/gui/CMakeLists.txt @@ -205,7 +205,7 @@ qt_add_module(Gui text/qabstracttextdocumentlayout.cpp text/qabstracttextdocumentlayout.h text/qabstracttextdocumentlayout_p.h text/qdistancefield.cpp text/qdistancefield_p.h text/qfont.cpp text/qfont.h text/qfont_p.h - text/qfontdatabase.cpp text/qfontdatabase.h + text/qfontdatabase.cpp text/qfontdatabase.h text/qfontdatabase_p.h text/qfontengine.cpp text/qfontengine_p.h text/qfontengine_qpf2.cpp text/qfontengineglyphcache.cpp text/qfontengineglyphcache_p.h diff --git a/src/gui/text/qfontdatabase.cpp b/src/gui/text/qfontdatabase.cpp index a43af37097..523895bc0e 100644 --- a/src/gui/text/qfontdatabase.cpp +++ b/src/gui/text/qfontdatabase.cpp @@ -38,6 +38,7 @@ ****************************************************************************/ #include "qfontdatabase.h" +#include "qfontdatabase_p.h" #include "qloggingcategory.h" #include "qalgorithms.h" #include "qguiapplication.h" @@ -173,71 +174,6 @@ static int getFontWeight(const QString &weightString) } -struct QtFontSize -{ - - void *handle; - - unsigned short pixelSize : 16; -}; - - - -struct QtFontStyle -{ - struct Key { - Key(const QString &styleString); - Key() : style(QFont::StyleNormal), - weight(QFont::Normal), stretch(0) { } - Key(const Key &o) : style(o.style), weight(o.weight), stretch(o.stretch) { } - uint style : 2; - signed int weight : 8; - signed int stretch : 12; - - bool operator==(const Key & other) { - return (style == other.style && weight == other.weight && - (stretch == 0 || other.stretch == 0 || stretch == other.stretch)); - } - bool operator!=(const Key &other) { - return !operator==(other); - } - bool operator <(const Key &o) { - int x = (style << 12) + (weight << 14) + stretch; - int y = (o.style << 12) + (o.weight << 14) + o.stretch; - return (x < y); - } - }; - - QtFontStyle(const Key &k) - : key(k), bitmapScalable(false), smoothScalable(false), - count(0), pixelSizes(nullptr) - { - } - - ~QtFontStyle() { - while (count) { - // bitfield count-- in while condition does not work correctly in mwccsym2 - count--; - QPlatformIntegration *integration = QGuiApplicationPrivate::platformIntegration(); - if (integration) { - integration->fontDatabase()->releaseHandle(pixelSizes[count].handle); - } - } - free(pixelSizes); - } - - Key key; - bool bitmapScalable : 1; - bool smoothScalable : 1; - signed int count : 30; - QtFontSize *pixelSizes; - QString styleName; - - bool antialiased; - - QtFontSize *pixelSize(unsigned short size, bool = false); -}; - QtFontStyle::Key::Key(const QString &styleString) : style(QFont::StyleNormal), weight(QFont::Normal), stretch(0) { @@ -284,22 +220,6 @@ QtFontSize *QtFontStyle::pixelSize(unsigned short size, bool add) return pixelSizes + (count++); } -struct QtFontFoundry -{ - QtFontFoundry(const QString &n) : name(n), count(0), styles(nullptr) {} - ~QtFontFoundry() { - while (count--) - delete styles[count]; - free(styles); - } - - QString name; - - int count; - QtFontStyle **styles; - QtFontStyle *style(const QtFontStyle::Key &, const QString & = QString(), bool = false); -}; - QtFontStyle *QtFontFoundry::style(const QtFontStyle::Key &key, const QString &styleName, bool create) { int pos = 0; @@ -331,46 +251,6 @@ QtFontStyle *QtFontFoundry::style(const QtFontStyle::Key &key, const QString &st return styles[pos]; } - -struct QtFontFamily -{ - enum WritingSystemStatus { - Unknown = 0, - Supported = 1, - UnsupportedFT = 2, - Unsupported = UnsupportedFT - }; - - QtFontFamily(const QString &n) - : - populated(false), - fixedPitch(false), - name(n), count(0), foundries(nullptr) - { - memset(writingSystems, 0, sizeof(writingSystems)); - } - ~QtFontFamily() { - while (count--) - delete foundries[count]; - free(foundries); - } - - bool populated : 1; - bool fixedPitch : 1; - - QString name; - QStringList aliases; - int count; - QtFontFoundry **foundries; - - unsigned char writingSystems[QFontDatabase::WritingSystemsCount]; - - bool matchesFamilyName(const QString &familyName) const; - QtFontFoundry *foundry(const QString &f, bool = false); - - void ensurePopulated(); -}; - QtFontFoundry *QtFontFamily::foundry(const QString &f, bool create) { if (f.isNull() && count == 1) @@ -414,85 +294,6 @@ void QtFontFamily::ensurePopulated() Q_ASSERT_X(populated, Q_FUNC_INFO, qPrintable(name)); } - -struct FallbacksCacheKey { - QString family; - QFont::Style style; - QFont::StyleHint styleHint; - QChar::Script script; -}; - -inline bool operator==(const FallbacksCacheKey &lhs, const FallbacksCacheKey &rhs) noexcept -{ - return lhs.script == rhs.script && - lhs.styleHint == rhs.styleHint && - lhs.style == rhs.style && - lhs.family == rhs.family; -} - -inline bool operator!=(const FallbacksCacheKey &lhs, const FallbacksCacheKey &rhs) noexcept -{ - return !operator==(lhs, rhs); -} - -inline uint qHash(const FallbacksCacheKey &key, uint seed = 0) noexcept -{ - QtPrivate::QHashCombine hash; - seed = hash(seed, key.family); - seed = hash(seed, int(key.style)); - seed = hash(seed, int(key.styleHint)); - seed = hash(seed, int(key.script)); - return seed; -} - - -class QFontDatabasePrivate -{ -public: - QFontDatabasePrivate() - : count(0), families(nullptr), - fallbacksCache(64) - { } - - ~QFontDatabasePrivate() { - free(); - } - - enum FamilyRequestFlags { - RequestFamily = 0, - EnsureCreated, - EnsurePopulated - }; - - QtFontFamily *family(const QString &f, FamilyRequestFlags flags = EnsurePopulated); - void free() { - while (count--) - delete families[count]; - ::free(families); - families = nullptr; - count = 0; - // don't clear the memory fonts! - } - - int count; - QtFontFamily **families; - - QCache<FallbacksCacheKey, QStringList> fallbacksCache; - - - struct ApplicationFont { - QString fileName; - QByteArray data; - QStringList families; - }; - QVector<ApplicationFont> applicationFonts; - int addAppFont(const QByteArray &fontData, const QString &fileName); - bool isApplicationFont(const QString &fileName); - - void invalidate(); -}; -Q_DECLARE_TYPEINFO(QFontDatabasePrivate::ApplicationFont, Q_MOVABLE_TYPE); - void QFontDatabasePrivate::invalidate() { QFontCache::instance()->clear(); @@ -748,6 +549,10 @@ QRecursiveMutex *qt_fontdatabase_mutex() return fontDatabaseMutex(); } +QFontDatabasePrivate *QFontDatabasePrivate::instance() +{ + return privateDb(); +} void qt_registerFont(const QString &familyName, const QString &stylename, const QString &foundryname, int weight, @@ -878,7 +683,7 @@ static QStringList fallbacksForFamily(const QString &family, QFont::Style style, if (!db->count) initializeDb(); - const FallbacksCacheKey cacheKey = { family, style, styleHint, script }; + const QtFontFallbacksCacheKey cacheKey = { family, style, styleHint, script }; if (const QStringList *fallbacks = db->fallbacksCache.object(cacheKey)) return *fallbacks; @@ -922,7 +727,7 @@ static void initializeDb() if (!db->count) { QGuiApplicationPrivate::platformIntegration()->fontDatabase()->populateFontDatabase(); for (int i = 0; i < db->applicationFonts.count(); i++) { - if (!db->applicationFonts.at(i).families.isEmpty()) + if (!db->applicationFonts.at(i).properties.isEmpty()) registerFont(&db->applicationFonts[i]); } } @@ -1050,9 +855,22 @@ QFontEngine *loadEngine(int script, const QFontDef &request, return engine; } +QtFontStyle::~QtFontStyle() +{ + while (count) { + // bitfield count-- in while condition does not work correctly in mwccsym2 + count--; + QPlatformIntegration *integration = QGuiApplicationPrivate::platformIntegration(); + if (integration) + integration->fontDatabase()->releaseHandle(pixelSizes[count].handle); + } + + free(pixelSizes); +} + static void registerFont(QFontDatabasePrivate::ApplicationFont *fnt) { - fnt->families = QGuiApplicationPrivate::platformIntegration()->fontDatabase()->addApplicationFont(fnt->data,fnt->fileName); + QGuiApplicationPrivate::platformIntegration()->fontDatabase()->addApplicationFont(fnt->data, fnt->fileName, fnt); } static QtFontStyle *bestStyle(QtFontFoundry *foundry, const QtFontStyle::Key &styleKey, @@ -2455,7 +2273,7 @@ int QFontDatabasePrivate::addAppFont(const QByteArray &fontData, const QString & int i; for (i = 0; i < applicationFonts.count(); ++i) - if (applicationFonts.at(i).families.isEmpty()) + if (applicationFonts.at(i).properties.isEmpty()) break; if (i >= applicationFonts.count()) { applicationFonts.append(ApplicationFont()); @@ -2467,7 +2285,7 @@ int QFontDatabasePrivate::addAppFont(const QByteArray &fontData, const QString & bool wasEmpty = privateDb()->count == 0; registerFont(&font); - if (font.families.isEmpty()) + if (font.properties.isEmpty()) return -1; applicationFonts[i] = font; @@ -2556,7 +2374,14 @@ int QFontDatabase::addApplicationFontFromData(const QByteArray &fontData) QStringList QFontDatabase::applicationFontFamilies(int id) { QMutexLocker locker(fontDatabaseMutex()); - return privateDb()->applicationFonts.value(id).families; + + QStringList ret; + ret.reserve(privateDb()->applicationFonts.value(id).properties.size()); + + for (const auto &properties : privateDb()->applicationFonts.value(id).properties) + ret.append(properties.familyName); + + return ret; } /*! diff --git a/src/gui/text/qfontdatabase_p.h b/src/gui/text/qfontdatabase_p.h new file mode 100644 index 0000000000..f13dd66aa8 --- /dev/null +++ b/src/gui/text/qfontdatabase_p.h @@ -0,0 +1,279 @@ +/**************************************************************************** +** +** Copyright (C) 2020 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:LGPL$ +** 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 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$ +** +****************************************************************************/ + +#ifndef QFONTDATABASE_P_H +#define QFONTDATABASE_P_H + +// +// W A R N I N G +// ------------- +// +// This file is not part of the Qt API. It exists for the convenience +// of internal files. This header file may change from version to version +// without notice, or even be removed. +// +// We mean it. +// + +#include <QtCore/qcache.h> + +#include <QtGui/qfontdatabase.h> + +QT_BEGIN_NAMESPACE + +struct QtFontFallbacksCacheKey +{ + QString family; + QFont::Style style; + QFont::StyleHint styleHint; + QChar::Script script; +}; + +inline bool operator==(const QtFontFallbacksCacheKey &lhs, const QtFontFallbacksCacheKey &rhs) noexcept +{ + return lhs.script == rhs.script && + lhs.styleHint == rhs.styleHint && + lhs.style == rhs.style && + lhs.family == rhs.family; +} + +inline bool operator!=(const QtFontFallbacksCacheKey &lhs, const QtFontFallbacksCacheKey &rhs) noexcept +{ + return !operator==(lhs, rhs); +} + +inline uint qHash(const QtFontFallbacksCacheKey &key, uint seed = 0) noexcept +{ + QtPrivate::QHashCombine hash; + seed = hash(seed, key.family); + seed = hash(seed, int(key.style)); + seed = hash(seed, int(key.styleHint)); + seed = hash(seed, int(key.script)); + return seed; +} + +struct Q_GUI_EXPORT QtFontSize +{ + void *handle; + unsigned short pixelSize : 16; +}; + +struct Q_GUI_EXPORT QtFontStyle +{ + struct Key + { + Key(const QString &styleString); + + Key() + : style(QFont::StyleNormal) + , weight(QFont::Normal) + , stretch(0) + {} + + Key(const Key &o) + : style(o.style) + , weight(o.weight) + , stretch(o.stretch) + {} + + uint style : 2; + signed int weight : 8; + signed int stretch : 12; + + bool operator==(const Key &other) + { + return (style == other.style && weight == other.weight && + (stretch == 0 || other.stretch == 0 || stretch == other.stretch)); + } + + bool operator!=(const Key &other) + { + return !operator==(other); + } + + bool operator<(const Key &o) + { + int x = (style << 12) + (weight << 14) + stretch; + int y = (o.style << 12) + (o.weight << 14) + o.stretch; + return (x < y); + } + }; + + QtFontStyle(const Key &k) + : key(k) + , bitmapScalable(false) + , smoothScalable(false) + , count(0) + , pixelSizes(nullptr) + { + } + + ~QtFontStyle(); + + QtFontSize *pixelSize(unsigned short size, bool = false); + + Key key; + bool bitmapScalable : 1; + bool smoothScalable : 1; + signed int count : 30; + QtFontSize *pixelSizes; + QString styleName; + bool antialiased; +}; + +struct Q_GUI_EXPORT QtFontFoundry +{ + QtFontFoundry(const QString &n) + : name(n) + , count(0) + , styles(nullptr) + {} + + ~QtFontFoundry() + { + while (count--) + delete styles[count]; + free(styles); + } + + QString name; + int count; + QtFontStyle **styles; + QtFontStyle *style(const QtFontStyle::Key &, const QString & = QString(), bool = false); +}; + +struct Q_GUI_EXPORT QtFontFamily +{ + enum WritingSystemStatus { + Unknown = 0, + Supported = 1, + UnsupportedFT = 2, + Unsupported = UnsupportedFT + }; + + QtFontFamily(const QString &n) + : + populated(false), + fixedPitch(false), + name(n), count(0), foundries(nullptr) + { + memset(writingSystems, 0, sizeof(writingSystems)); + } + ~QtFontFamily() { + while (count--) + delete foundries[count]; + free(foundries); + } + + bool populated : 1; + bool fixedPitch : 1; + + QString name; + QStringList aliases; + int count; + QtFontFoundry **foundries; + + unsigned char writingSystems[QFontDatabase::WritingSystemsCount]; + + bool matchesFamilyName(const QString &familyName) const; + QtFontFoundry *foundry(const QString &f, bool = false); + + void ensurePopulated(); +}; + +class Q_GUI_EXPORT QFontDatabasePrivate +{ +public: + QFontDatabasePrivate() + : count(0) + , families(nullptr) + , fallbacksCache(64) + { } + + ~QFontDatabasePrivate() { + free(); + } + + enum FamilyRequestFlags { + RequestFamily = 0, + EnsureCreated, + EnsurePopulated + }; + + QtFontFamily *family(const QString &f, FamilyRequestFlags flags = EnsurePopulated); + void free() { + while (count--) + delete families[count]; + ::free(families); + families = nullptr; + count = 0; + // don't clear the memory fonts! + } + + int count; + QtFontFamily **families; + + QCache<QtFontFallbacksCacheKey, QStringList> fallbacksCache; + struct ApplicationFont { + QString fileName; + QByteArray data; + + struct Properties { + QString familyName; + QString styleName; + int weight = 0; + QFont::Style style = QFont::StyleNormal; + int stretch = QFont::Unstretched; + }; + + QVector<Properties> properties; + }; + QVector<ApplicationFont> applicationFonts; + int addAppFont(const QByteArray &fontData, const QString &fileName); + bool isApplicationFont(const QString &fileName); + + static QFontDatabasePrivate *instance(); + + void invalidate(); +}; +Q_DECLARE_TYPEINFO(QFontDatabasePrivate::ApplicationFont, Q_MOVABLE_TYPE); + +QT_END_NAMESPACE + +#endif // QFONTDATABASE_P_H diff --git a/src/gui/text/qplatformfontdatabase.cpp b/src/gui/text/qplatformfontdatabase.cpp index 4ff2ad3a41..0e19f6866f 100644 --- a/src/gui/text/qplatformfontdatabase.cpp +++ b/src/gui/text/qplatformfontdatabase.cpp @@ -40,6 +40,7 @@ #include "qplatformfontdatabase.h" #include <QtGui/private/qfontengine_p.h> #include <QtGui/private/qfontengine_qpf2_p.h> +#include <QtGui/private/qfontdatabase_p.h> #include <QtGui/QGuiApplication> #include <QtGui/QScreen> #include <qpa/qplatformscreen.h> @@ -376,16 +377,27 @@ QFontEngine *QPlatformFontDatabase::fontEngine(const QByteArray &fontData, qreal or using the font contained in the file referenced by \a fileName. Returns a list of family names, or an empty list if the font could not be added. + If \a applicationFont is non-null, its \c properties vector should be filled + with information from the loaded fonts. This is exposed through FontLoader in + Qt Quick where it is needed for disambiguating fonts in the same family. When + the function exits, the \a applicationFont should contain an entry of properties + per font in the file, or it should be empty if no font was loaded. + \note The default implementation of this function does not add an application font. Subclasses should reimplement this function to perform the necessary loading and registration of fonts. */ -QStringList QPlatformFontDatabase::addApplicationFont(const QByteArray &fontData, const QString &fileName) +QStringList QPlatformFontDatabase::addApplicationFont(const QByteArray &fontData, const QString &fileName, QFontDatabasePrivate::ApplicationFont *applicationFont) { Q_UNUSED(fontData); Q_UNUSED(fileName); + Q_UNUSED(applicationFont); + + if (applicationFont != nullptr) + applicationFont->properties.clear(); qWarning("This plugin does not support application fonts"); + return QStringList(); } diff --git a/src/gui/text/qplatformfontdatabase.h b/src/gui/text/qplatformfontdatabase.h index d05bcb372b..2d25d9a036 100644 --- a/src/gui/text/qplatformfontdatabase.h +++ b/src/gui/text/qplatformfontdatabase.h @@ -59,6 +59,7 @@ #include <QtGui/QFontDatabase> #include <QtGui/private/qfontengine_p.h> #include <QtGui/private/qfont_p.h> +#include <QtGui/private/qfontdatabase_p.h> QT_BEGIN_NAMESPACE @@ -111,7 +112,7 @@ public: virtual QFontEngineMulti *fontEngineMulti(QFontEngine *fontEngine, QChar::Script script); virtual QFontEngine *fontEngine(const QFontDef &fontDef, void *handle); virtual QStringList fallbacksForFamily(const QString &family, QFont::Style style, QFont::StyleHint styleHint, QChar::Script script) const; - virtual QStringList addApplicationFont(const QByteArray &fontData, const QString &fileName); + virtual QStringList addApplicationFont(const QByteArray &fontData, const QString &fileName, QFontDatabasePrivate::ApplicationFont *font = nullptr); virtual void releaseHandle(void *handle); virtual QFontEngine *fontEngine(const QByteArray &fontData, qreal pixelSize, QFont::HintingPreference hintingPreference); diff --git a/src/gui/text/text.pri b/src/gui/text/text.pri index 464ff3953b..ab93975344 100644 --- a/src/gui/text/text.pri +++ b/src/gui/text/text.pri @@ -3,6 +3,7 @@ HEADERS += \ text/qfont.h \ text/qfontdatabase.h \ + text/qfontdatabase_p.h \ text/qfontengine_p.h \ text/qfontengineglyphcache_p.h \ text/qfontinfo.h \ |