aboutsummaryrefslogtreecommitdiffstats
path: root/src/quick/util
diff options
context:
space:
mode:
authorEskil Abrahamsen Blomfeldt <eskil.abrahamsen-blomfeldt@qt.io>2020-01-31 12:25:46 +0100
committerEskil Abrahamsen Blomfeldt <eskil.abrahamsen-blomfeldt@qt.io>2020-04-23 09:02:03 +0200
commit4601cd23f6c90372edbd4145833041bd7cf227cb (patch)
tree948ef69b9575b2bfcdcbce58ac39ad121e88ec30 /src/quick/util
parent7e5a691006c1051be1a163fa6a7d40effb6ef0bb (diff)
Add additional properties to FontLoader to help disambiguate
The FontLoader only returns the family name, and there is no way to disambiguate fonts within the same family. This change adds the option of getting other properties from the loaded font to help with this. [ChangeLog][QtQuick] Added FontLoader.font property which is filled with a font query that matches the loaded font. This can be used to disambiguate when multiple fonts in the same family are loaded in the same application. Fixes: QTBUG-68829 Change-Id: If05e85a1ca8d93b93100345cb2d05cd1ad6c7549 Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org> Reviewed-by: Simon Hausmann <simon.hausmann@qt.io>
Diffstat (limited to 'src/quick/util')
-rw-r--r--src/quick/util/qquickfontloader.cpp122
-rw-r--r--src/quick/util/qquickfontloader_p.h6
2 files changed, 104 insertions, 24 deletions
diff --git a/src/quick/util/qquickfontloader.cpp b/src/quick/util/qquickfontloader.cpp
index 63aba04c34..e672fb8510 100644
--- a/src/quick/util/qquickfontloader.cpp
+++ b/src/quick/util/qquickfontloader.cpp
@@ -59,6 +59,8 @@
#include <QtCore/QCoreApplication>
+#include <QtGui/private/qfontdatabase_p.h>
+
QT_BEGIN_NAMESPACE
#define FONTLOADER_MAXIMUM_REDIRECT_RECURSION 16
@@ -74,7 +76,7 @@ public:
void download(const QUrl &url, QNetworkAccessManager *manager);
Q_SIGNALS:
- void fontDownloaded(const QString&, QQuickFontLoader::Status);
+ void fontDownloaded(int id);
private:
int redirectCount = 0;
@@ -123,14 +125,11 @@ void QQuickFontObject::replyFinished()
if (!reply->error()) {
id = QFontDatabase::addApplicationFontFromData(reply->readAll());
- if (id != -1)
- emit fontDownloaded(QFontDatabase::applicationFontFamilies(id).at(0), QQuickFontLoader::Ready);
- else
- emit fontDownloaded(QString(), QQuickFontLoader::Error);
+ emit fontDownloaded(id);
} else {
qWarning("%s: Unable to load font '%s': %s", Q_FUNC_INFO,
qPrintable(reply->url().toString()), qPrintable(reply->errorString()));
- emit fontDownloaded(QString(), QQuickFontLoader::Error);
+ emit fontDownloaded(-1);
}
reply->deleteLater();
reply = nullptr;
@@ -146,7 +145,7 @@ public:
QQuickFontLoaderPrivate() {}
QUrl url;
- QString name;
+ QFont font;
QQuickFontLoader::Status status = QQuickFontLoader::Null;
};
@@ -213,8 +212,8 @@ static void q_QFontLoaderFontsStaticReset()
FontLoader { id: fixedFont; name: "Courier" }
FontLoader { id: webFont; source: "http://www.mysite.com/myfont.ttf" }
- Text { text: "Fixed-size font"; font.family: fixedFont.name }
- Text { text: "Fancy font"; font.family: webFont.name }
+ Text { text: "Fixed-size font"; font: fixedFont.font }
+ Text { text: "Fancy font"; font: webFont.font }
}
\endqml
@@ -223,6 +222,7 @@ static void q_QFontLoaderFontsStaticReset()
QQuickFontLoader::QQuickFontLoader(QObject *parent)
: QObject(*(new QQuickFontLoaderPrivate), parent)
{
+ connect(this, &QQuickFontLoader::fontChanged, this, &QQuickFontLoader::nameChanged);
}
QQuickFontLoader::~QQuickFontLoader()
@@ -251,15 +251,13 @@ void QQuickFontLoader::setSource(const QUrl &url)
if (!localFile.isEmpty()) {
if (!fontLoaderFonts()->map.contains(d->url)) {
int id = QFontDatabase::addApplicationFont(localFile);
+ updateFontInfo(id);
if (id != -1) {
- updateFontInfo(QFontDatabase::applicationFontFamilies(id).at(0), Ready);
QQuickFontObject *fo = new QQuickFontObject(id);
fontLoaderFonts()->map[d->url] = fo;
- } else {
- updateFontInfo(QString(), Error);
}
} else {
- updateFontInfo(QFontDatabase::applicationFontFamilies(fontLoaderFonts()->map.value(d->url)->id).at(0), Ready);
+ updateFontInfo(fontLoaderFonts()->map.value(d->url)->id);
}
} else {
if (!fontLoaderFonts()->map.contains(d->url)) {
@@ -269,8 +267,8 @@ void QQuickFontLoader::setSource(const QUrl &url)
fo->download(d->url, qmlEngine(this)->networkAccessManager());
d->status = Loading;
emit statusChanged();
- QObject::connect(fo, SIGNAL(fontDownloaded(QString,QQuickFontLoader::Status)),
- this, SLOT(updateFontInfo(QString,QQuickFontLoader::Status)));
+ QObject::connect(fo, SIGNAL(fontDownloaded(int)),
+ this, SLOT(updateFontInfo(int)));
#else
// Silently fail if compiled with no_network
#endif
@@ -280,26 +278,48 @@ void QQuickFontLoader::setSource(const QUrl &url)
#if QT_CONFIG(qml_network)
d->status = Loading;
emit statusChanged();
- QObject::connect(fo, SIGNAL(fontDownloaded(QString,QQuickFontLoader::Status)),
- this, SLOT(updateFontInfo(QString,QQuickFontLoader::Status)));
+ QObject::connect(fo, SIGNAL(fontDownloaded(int)),
+ this, SLOT(updateFontInfo(int)));
#else
// Silently fail if compiled with no_network
#endif
}
else
- updateFontInfo(QFontDatabase::applicationFontFamilies(fo->id).at(0), Ready);
+ updateFontInfo(fo->id);
}
}
}
-void QQuickFontLoader::updateFontInfo(const QString& name, QQuickFontLoader::Status status)
+void QQuickFontLoader::updateFontInfo(int id)
{
Q_D(QQuickFontLoader);
- if (name != d->name) {
- d->name = name;
- emit nameChanged();
+ QFont font;
+
+ QQuickFontLoader::Status status = Error;
+ if (id >= 0) {
+ QFontDatabasePrivate *p = QFontDatabasePrivate::instance();
+ if (id < p->applicationFonts.size()) {
+ const QFontDatabasePrivate::ApplicationFont &applicationFont = p->applicationFonts.at(id);
+
+ if (!applicationFont.properties.isEmpty()) {
+ const QFontDatabasePrivate::ApplicationFont::Properties &properties = applicationFont.properties.at(0);
+ font.setFamily(properties.familyName);
+ font.setStyleName(properties.styleName);
+ font.setWeight(properties.weight);
+ font.setStyle(properties.style);
+ font.setStretch(properties.stretch);
+ }
+ }
+
+ status = Ready;
+ }
+
+ if (font != d->font) {
+ d->font = font;
+ emit fontChanged();
}
+
if (status != d->status) {
if (status == Error)
qmlWarning(this) << "Cannot load font: \"" << d->url.toString() << '"';
@@ -309,12 +329,68 @@ void QQuickFontLoader::updateFontInfo(const QString& name, QQuickFontLoader::Sta
}
/*!
+ \qmlproperty font QtQuick::FontLoader::font
+ \since 6.0
+
+ This property holds a default query for the loaded font.
+
+ You can use this to select the font if other properties than just the
+ family name are needed to disambiguate. You can either specify the
+ font using individual properties:
+
+ \qml
+ Item {
+ width: 200; height: 50
+
+ FontLoader {
+ id: webFont
+ source: "http://www.mysite.com/myfont.ttf"
+ }
+ Text {
+ text: "Fancy font"
+ font.family: webFont.font.family
+ font.weight: webFont.font.weight
+ font.style: webFont.font.style
+ font.pixelSize: 24
+ }
+ }
+ \endqml
+
+ Or you can set the full font query directly:
+
+ \qml
+ Item {
+ width: 200; height: 50
+
+ FontLoader {
+ id: webFont
+ source: "http://www.mysite.com/myfont.ttf"
+ }
+ Text {
+ text: "Fancy font"
+ font: webFont.font
+ }
+ }
+ \endqml
+
+ In this case, the default font query will be used with no modifications
+ (so font size, for instance, will be the system default).
+*/
+QFont QQuickFontLoader::font() const
+{
+ Q_D(const QQuickFontLoader);
+ return d->font;
+}
+
+/*!
\qmlproperty string QtQuick::FontLoader::name
\readonly
This property holds the name of the font family.
It is set automatically when a font is loaded using the \l source property.
+ This is equivalent to the family property of the FontLoader's \l font property.
+
Use this to set the \c font.family property of a \c Text item.
Example:
@@ -336,7 +412,7 @@ void QQuickFontLoader::updateFontInfo(const QString& name, QQuickFontLoader::Sta
QString QQuickFontLoader::name() const
{
Q_D(const QQuickFontLoader);
- return d->name;
+ return d->font.resolve() == 0 ? QString() : d->font.family();
}
/*!
diff --git a/src/quick/util/qquickfontloader_p.h b/src/quick/util/qquickfontloader_p.h
index 8d831dac4d..bc7046e808 100644
--- a/src/quick/util/qquickfontloader_p.h
+++ b/src/quick/util/qquickfontloader_p.h
@@ -67,6 +67,7 @@ class Q_AUTOTEST_EXPORT QQuickFontLoader : public QObject
Q_PROPERTY(QUrl source READ source WRITE setSource NOTIFY sourceChanged)
Q_PROPERTY(QString name READ name NOTIFY nameChanged)
Q_PROPERTY(Status status READ status NOTIFY statusChanged)
+ Q_PROPERTY(QFont font READ font NOTIFY fontChanged)
QML_NAMED_ELEMENT(FontLoader)
QML_ADDED_IN_VERSION(2, 0)
@@ -82,14 +83,17 @@ public:
QString name() const;
+ QFont font() const;
+
Status status() const;
private Q_SLOTS:
- void updateFontInfo(const QString&, QQuickFontLoader::Status);
+ void updateFontInfo(int);
Q_SIGNALS:
void sourceChanged();
void nameChanged();
+ void fontChanged();
void statusChanged();
};