aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/quick/util/qquickfontloader.cpp122
-rw-r--r--src/quick/util/qquickfontloader_p.h6
-rw-r--r--tests/auto/quick/qquickfontloader/tst_qquickfontloader.cpp10
3 files changed, 109 insertions, 29 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();
};
diff --git a/tests/auto/quick/qquickfontloader/tst_qquickfontloader.cpp b/tests/auto/quick/qquickfontloader/tst_qquickfontloader.cpp
index 8f6910bee4..4d63803e78 100644
--- a/tests/auto/quick/qquickfontloader/tst_qquickfontloader.cpp
+++ b/tests/auto/quick/qquickfontloader/tst_qquickfontloader.cpp
@@ -158,9 +158,9 @@ void tst_qquickfontloader::failWebFont()
void tst_qquickfontloader::changeFont()
{
- QString componentStr = "import QtQuick 2.0\nFontLoader { source: font }";
+ QString componentStr = "import QtQuick 2.0\nFontLoader { source: fnt }";
QQmlContext *ctxt = engine.rootContext();
- ctxt->setContextProperty("font", testFileUrl("tarzeau_ocr_a.ttf"));
+ ctxt->setContextProperty("fnt", testFileUrl("tarzeau_ocr_a.ttf"));
QQmlComponent component(&engine);
component.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
QQuickFontLoader *fontObject = qobject_cast<QQuickFontLoader*>(component.create());
@@ -175,20 +175,20 @@ void tst_qquickfontloader::changeFont()
QCOMPARE(statusSpy.count(), 0);
QTRY_COMPARE(fontObject->name(), QString("OCRA"));
- ctxt->setContextProperty("font", server.urlString("/daniel.ttf"));
+ ctxt->setContextProperty("fnt", server.urlString("/daniel.ttf"));
QTRY_COMPARE(fontObject->status(), QQuickFontLoader::Loading);
QTRY_COMPARE(fontObject->status(), QQuickFontLoader::Ready);
QCOMPARE(nameSpy.count(), 1);
QCOMPARE(statusSpy.count(), 2);
QTRY_COMPARE(fontObject->name(), QString("Daniel"));
- ctxt->setContextProperty("font", testFileUrl("tarzeau_ocr_a.ttf"));
+ ctxt->setContextProperty("fnt", testFileUrl("tarzeau_ocr_a.ttf"));
QTRY_COMPARE(fontObject->status(), QQuickFontLoader::Ready);
QCOMPARE(nameSpy.count(), 2);
QCOMPARE(statusSpy.count(), 2);
QTRY_COMPARE(fontObject->name(), QString("OCRA"));
- ctxt->setContextProperty("font", server.urlString("/daniel.ttf"));
+ ctxt->setContextProperty("fnt", server.urlString("/daniel.ttf"));
QTRY_COMPARE(fontObject->status(), QQuickFontLoader::Ready);
QCOMPARE(nameSpy.count(), 3);
QCOMPARE(statusSpy.count(), 2);