6#include <qqmlcontext.h>
13#include <QFontDatabase>
15#include <private/qobject_p.h>
19#if QT_CONFIG(qml_network)
20#include <QNetworkRequest>
21#include <QNetworkReply>
24#include <QtCore/QCoreApplication>
25#include <QtCore/private/qduplicatetracker_p.h>
27#include <QtGui/private/qfontdatabase_p.h>
38#if QT_CONFIG(qml_network)
58 : QObject(
nullptr),
id(_id)
62#if QT_CONFIG(qml_network)
63void QQuickFontObject::download(
const QUrl &url, QNetworkAccessManager *manager)
65 QNetworkRequest req(url);
66 req.setAttribute(QNetworkRequest::HttpPipeliningAllowedAttribute,
true);
67 reply = manager->get(req);
68 QObject::connect(reply, SIGNAL(finished()),
this, SLOT(replyFinished()));
71void QQuickFontObject::replyFinished()
74 if (!reply->error()) {
75 id = QFontDatabase::addApplicationFontFromData(reply->readAll());
76 emit fontDownloaded(id);
78 qWarning(
"%s: Unable to load font '%s': %s", Q_FUNC_INFO,
79 qPrintable(reply->url().toString()), qPrintable(reply->errorString()));
80 emit fontDownloaded(-1);
90 Q_DECLARE_PUBLIC(QQuickFontLoader)
123 QDuplicateTracker<QQuickFontObject *, 256> deleted(map.size());
124 for (QQuickFontObject *fo : std::as_const(map)) {
125 if (!deleted.hasSeen(fo))
137 fontLoaderFonts()->reset();
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165QQuickFontLoader::QQuickFontLoader(QObject *parent)
166 : QObject(*(
new QQuickFontLoaderPrivate), parent)
168 connect(
this, &QQuickFontLoader::fontChanged,
this, &QQuickFontLoader::nameChanged);
172
173
174
175QUrl QQuickFontLoader::source()
const
177 Q_D(
const QQuickFontLoader);
181void QQuickFontLoader::setSource(
const QUrl &url)
183 Q_D(QQuickFontLoader);
187 emit sourceChanged();
189 auto &map = fontLoaderFonts()->map;
191 const QQmlContext *context = qmlContext(
this);
192 const QUrl &resolvedUrl = context ? context->resolvedUrl(d->url) : d->url;
193 const auto mapEntry = map.constFind(resolvedUrl);
194 const auto mapEnd = map.constEnd();
196 const QString localFile = QQmlFile::urlToLocalFileOrQrc(resolvedUrl);
197 if (!localFile.isEmpty()) {
198 if (mapEntry == mapEnd) {
199 int id = QFontDatabase::addApplicationFont(localFile);
202 QQuickFontObject *fo =
new QQuickFontObject(id);
203 map[resolvedUrl] = fo;
206 QQuickFontObject *fo = *mapEntry;
207 updateFontInfo(fo->id);
209 }
else if (mapEntry == mapEnd) {
211#if QT_CONFIG(qml_network)
212 QQuickFontObject *fo =
new QQuickFontObject;
213 map[resolvedUrl] = fo;
214 fo->download(resolvedUrl, context->engine()->networkAccessManager());
216 emit statusChanged();
217 QObject::connect(fo, SIGNAL(fontDownloaded(
int)),
218 this, SLOT(updateFontInfo(
int)));
223 QQuickFontObject *fo = *mapEntry;
225#if QT_CONFIG(qml_network)
227 emit statusChanged();
228 QObject::connect(fo, SIGNAL(fontDownloaded(
int)),
229 this, SLOT(updateFontInfo(
int)));
234 updateFontInfo(fo->id);
239void QQuickFontLoader::updateFontInfo(
int id)
241 Q_D(QQuickFontLoader);
245 QQuickFontLoader::Status status = Error;
247 QFontDatabasePrivate *p = QFontDatabasePrivate::instance();
248 if (id < p->applicationFonts.size()) {
249 const QFontDatabasePrivate::ApplicationFont &applicationFont = p->applicationFonts.at(id);
251 if (!applicationFont.properties.isEmpty()) {
252 const QFontDatabasePrivate::ApplicationFont::Properties &properties = applicationFont.properties.at(0);
253 font.setFamily(properties.familyName);
254 font.setStyleName(properties.styleName);
255 font.setWeight(QFont::Weight(properties.weight));
256 font.setStyle(properties.style);
257 font.setStretch(properties.stretch);
264 if (font != d->font) {
269 if (status != d->status) {
270 if (status == Error) {
271 const QQmlContext *context = qmlContext(
this);
272 qmlWarning(
this) <<
"Cannot load font: \""
273 << (context ? context->resolvedUrl(d->url) : d->url).toString() <<
'"';
276 emit statusChanged();
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328QFont QQuickFontLoader::font()
const
330 Q_D(
const QQuickFontLoader);
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361QString QQuickFontLoader::name()
const
363 Q_D(
const QQuickFontLoader);
364 return d->font.resolveMask() == 0 ? QString() : d->font.family();
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400QQuickFontLoader::Status QQuickFontLoader::status()
const
402 Q_D(
const QQuickFontLoader);
408#include <qquickfontloader.moc>
410#include "moc_qquickfontloader_p.cpp"
QHash< QUrl, QQuickFontObject * > map
Combined button and popup list for selecting options.
static void q_QFontLoaderFontsAddReset()
static void q_QFontLoaderFontsStaticReset()
Q_GLOBAL_STATIC(QFontLoaderFonts, fontLoaderFonts)