Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qquickfontloader.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
5
6#include <qqmlcontext.h>
7#include <qqmlengine.h>
8
9#include <QStringList>
10#include <QUrl>
11#include <QDebug>
12
13#include <QFontDatabase>
14
15#include <private/qobject_p.h>
16#include <qqmlinfo.h>
17#include <qqmlfile.h>
18
19#if QT_CONFIG(qml_network)
20#include <QNetworkRequest>
21#include <QNetworkReply>
22#endif
23
24#include <QtCore/QCoreApplication>
25#include <QtCore/private/qduplicatetracker_p.h>
26
27#include <QtGui/private/qfontdatabase_p.h>
28
30
32{
34
35public:
36 explicit QQuickFontObject(int _id = -1);
37
38#if QT_CONFIG(qml_network)
39 void download(const QUrl &url, QNetworkAccessManager *manager);
40
42 void fontDownloaded(int id);
43
44private:
45 QNetworkReply *reply = nullptr;
46
47private Q_SLOTS:
48 void replyFinished();
49#endif // qml_network
50
51public:
52 int id;
53
54 Q_DISABLE_COPY(QQuickFontObject)
55};
56
61
62#if QT_CONFIG(qml_network)
63void QQuickFontObject::download(const QUrl &url, QNetworkAccessManager *manager)
64{
67 reply = manager->get(req);
68 QObject::connect(reply, SIGNAL(finished()), this, SLOT(replyFinished()));
69}
70
71void QQuickFontObject::replyFinished()
72{
73 if (reply) {
74 if (!reply->error()) {
76 emit fontDownloaded(id);
77 } else {
78 qWarning("%s: Unable to load font '%s': %s", Q_FUNC_INFO,
80 emit fontDownloaded(-1);
81 }
83 reply = nullptr;
84 }
85}
86#endif // qml_network
87
99
106{
107public:
113
119
120
121 void reset()
122 {
123 QDuplicateTracker<QQuickFontObject *, 256> deleted(map.size());
124 for (QQuickFontObject *fo : std::as_const(map)) {
125 if (!deleted.hasSeen(fo))
126 delete fo;
127 }
128 map.clear();
129 }
130
131 QHash<QUrl, QQuickFontObject *> map;
132};
134
136{
137 fontLoaderFonts()->reset();
138}
139
170
176{
177 Q_D(const QQuickFontLoader);
178 return d->url;
179}
180
182{
183 Q_D(QQuickFontLoader);
184 if (url == d->url)
185 return;
186 d->url = url;
188
189 const QQmlContext *context = qmlContext(this);
190 const QUrl &resolvedUrl = context ? context->resolvedUrl(d->url) : d->url;
192 if (!localFile.isEmpty()) {
193 if (!fontLoaderFonts()->map.contains(resolvedUrl)) {
194 int id = QFontDatabase::addApplicationFont(localFile);
195 updateFontInfo(id);
196 if (id != -1) {
198 fontLoaderFonts()->map[resolvedUrl] = fo;
199 }
200 } else {
201 updateFontInfo(fontLoaderFonts()->map.value(resolvedUrl)->id);
202 }
203 } else {
204 if (!fontLoaderFonts()->map.contains(resolvedUrl)) {
206#if QT_CONFIG(qml_network)
208 fontLoaderFonts()->map[resolvedUrl] = fo;
209 fo->download(resolvedUrl, context->engine()->networkAccessManager());
210 d->status = Loading;
212 QObject::connect(fo, SIGNAL(fontDownloaded(int)),
213 this, SLOT(updateFontInfo(int)));
214#else
215// Silently fail if compiled with no_network
216#endif
217 } else {
218 QQuickFontObject *fo = fontLoaderFonts()->map.value(resolvedUrl);
219 if (fo->id == -1) {
220#if QT_CONFIG(qml_network)
221 d->status = Loading;
223 QObject::connect(fo, SIGNAL(fontDownloaded(int)),
224 this, SLOT(updateFontInfo(int)));
225#else
226// Silently fail if compiled with no_network
227#endif
228 }
229 else
230 updateFontInfo(fo->id);
231 }
232 }
233}
234
235void QQuickFontLoader::updateFontInfo(int id)
236{
237 Q_D(QQuickFontLoader);
238
239 QFont font;
240
242 if (id >= 0) {
244 if (id < p->applicationFonts.size()) {
245 const QFontDatabasePrivate::ApplicationFont &applicationFont = p->applicationFonts.at(id);
246
247 if (!applicationFont.properties.isEmpty()) {
248 const QFontDatabasePrivate::ApplicationFont::Properties &properties = applicationFont.properties.at(0);
249 font.setFamily(properties.familyName);
250 font.setStyleName(properties.styleName);
252 font.setStyle(properties.style);
253 font.setStretch(properties.stretch);
254 }
255 }
256
257 status = Ready;
258 }
259
260 if (font != d->font) {
261 d->font = font;
263 }
264
265 if (status != d->status) {
266 if (status == Error) {
267 const QQmlContext *context = qmlContext(this);
268 qmlWarning(this) << "Cannot load font: \""
269 << (context ? context->resolvedUrl(d->url) : d->url).toString() << '"';
270 }
271 d->status = status;
273 }
274}
275
325{
326 Q_D(const QQuickFontLoader);
327 return d->font;
328}
329
358{
359 Q_D(const QQuickFontLoader);
360 return d->font.resolveMask() == 0 ? QString() : d->font.family();
361}
362
397{
398 Q_D(const QQuickFontLoader);
399 return d->status;
400}
401
403
404#include <qquickfontloader.moc>
405
406#include "moc_qquickfontloader_p.cpp"
static QFontDatabasePrivate * instance()
static int addApplicationFontFromData(const QByteArray &fontData)
static int addApplicationFont(const QString &fileName)
QHash< QUrl, QQuickFontObject * > map
\reentrant
Definition qfont.h:22
void setStyle(Style style)
Sets the style of the font to style.
Definition qfont.cpp:1116
void setFamily(const QString &)
Sets the family name of the font.
Definition qfont.cpp:835
void setStyleName(const QString &)
Definition qfont.cpp:867
void setStretch(int)
Sets the stretch factor for the font.
Definition qfont.cpp:1588
Weight
Qt uses a weighting scale from 1 to 1000 compatible with OpenType.
Definition qfont.h:63
void setWeight(Weight weight)
Sets the weight of the font to weight, using the scale defined by \l QFont::Weight enumeration.
Definition qfont.cpp:1205
qsizetype size() const noexcept
Returns the number of items in the hash.
Definition qhash.h:927
void clear() noexcept(std::is_nothrow_destructible< Node >::value)
Removes all items from the hash and frees up all memory used by it.
Definition qhash.h:951
QByteArray readAll()
Reads all remaining data from the device, and returns it as a byte array.
QString errorString() const
Returns a human-readable description of the last device error that occurred.
T value(const Key &key, const T &defaultValue=T()) const
Definition qmap.h:357
bool contains(const Key &key) const
Definition qmap.h:341
The QNetworkAccessManager class allows the application to send network requests and receive replies.
QNetworkReply * get(const QNetworkRequest &request)
Posts a request to obtain the contents of the target request and returns a new QNetworkReply object o...
The QNetworkReply class contains the data and headers for a request sent with QNetworkAccessManager.
NetworkError error() const
Returns the error that was found during the processing of this request.
QUrl url() const
Returns the URL of the content downloaded or uploaded.
The QNetworkRequest class holds a request to be sent with QNetworkAccessManager.
\inmodule QtCore
Definition qobject.h:103
static QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
\threadsafe
Definition qobject.cpp:2960
void deleteLater()
\threadsafe
Definition qobject.cpp:2435
The QQmlContext class defines a context within a QML engine.
Definition qqmlcontext.h:25
static QString urlToLocalFileOrQrc(const QString &)
If url is a local file returns a path suitable for passing to \l{QFile}.
Definition qqmlfile.cpp:742
QQuickFontLoader::Status status
QQuickFontLoader(QObject *parent=nullptr)
\qmltype FontLoader \instantiates QQuickFontLoader \inqmlmodule QtQuick
void setSource(const QUrl &url)
QQuickFontObject(int _id=-1)
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
\inmodule QtCore
Definition qurl.h:94
QString url(FormattingOptions options=FormattingOptions(PrettyDecoded)) const
Returns a string representation of the URL.
Definition qurl.cpp:2817
QString toString(FormattingOptions options=FormattingOptions(PrettyDecoded)) const
Returns a string representation of the URL.
Definition qurl.cpp:2831
QMap< QString, QString > map
[6]
Combined button and popup list for selecting options.
static void * context
#define Q_FUNC_INFO
void qAddPreRoutine(QtStartUpFunction p)
void qAddPostRoutine(QtCleanUpFunction p)
void qRemovePostRoutine(QtCleanUpFunction p)
static const QCssKnownValue properties[NumProperties - 1]
#define Q_GLOBAL_STATIC(TYPE, NAME,...)
#define qWarning
Definition qlogging.h:166
#define SLOT(a)
Definition qobjectdefs.h:52
#define SIGNAL(a)
Definition qobjectdefs.h:53
GLenum GLuint id
[7]
GLfloat GLfloat p
[1]
QQmlContext * qmlContext(const QObject *obj)
Definition qqml.cpp:75
Q_QML_EXPORT QQmlInfo qmlWarning(const QObject *me)
static void q_QFontLoaderFontsAddReset()
static void q_QFontLoaderFontsStaticReset()
static QUrl resolvedUrl(const QUrl &url, const QQmlRefPointer< QQmlContextData > &context)
#define Q_ASSERT(cond)
Definition qrandom.cpp:47
#define qPrintable(string)
Definition qstring.h:1531
#define Q_OBJECT
#define Q_SLOTS
#define Q_SIGNALS
#define emit
QUrl url("example.com")
[constructor-url-reference]
QObject::connect nullptr
QNetworkAccessManager manager
QNetworkReply * reply
void replyFinished(QNetworkReply *reply)
[1]
char * toString(const MyType &t)
[31]