QGIS API Documentation 3.41.0-Master (45a0abf3bec)
Loading...
Searching...
No Matches
qgsfontmanager.h
Go to the documentation of this file.
1/***************************************************************************
2 qgsfontmanager.h
3 ------------------
4 Date : June 2022
5 Copyright : (C) 2022 Nyall Dawson
6 Email : nyall dot dawson at gmail dot com
7 ***************************************************************************
8 * *
9 * This program is free software; you can redistribute it and/or modify *
10 * it under the terms of the GNU General Public License as published by *
11 * the Free Software Foundation; either version 2 of the License, or *
12 * (at your option) any later version. *
13 * *
14 ***************************************************************************/
15
16#ifndef QGSFONTMANAGER_H
17#define QGSFONTMANAGER_H
18
19#include "qgis_core.h"
20#include "qgis_sip.h"
22#include "qgstaskmanager.h"
23#include <QObject>
24#include <QMap>
25#include <QReadWriteLock>
26#include <QSet>
27
36class CORE_EXPORT QgsFontDownloadDetails
37{
38 public:
39
44
52 QgsFontDownloadDetails( const QString &family, const QStringList &fontUrls, const QString &licenseUrl = QString() );
53
57 static QString standardizeFamily( const QString &family );
58
62 bool isValid() const { return !mFontUrls.empty(); }
63
69 QString family() const { return mFamily; }
70
74 QString standardizedFamily() const { return mStandardizedFamily; }
75
79 QStringList fontUrls() const { return mFontUrls; }
80
84 QString licenseUrl() const { return mLicenseUrl; }
85
86 private:
87
88 QString mFamily;
89 QString mStandardizedFamily;
90 QStringList mFontUrls;
91 QString mLicenseUrl;
92};
93
94#ifndef SIP_RUN
96class CORE_EXPORT QgsFontDownloadTask : public QgsTask
97{
98 Q_OBJECT
99
100 public:
101
102 QgsFontDownloadTask( const QString &description, const QgsFontDownloadDetails &details );
103
104 bool run() override;
105 void cancel() override;
106 QString errorMessage() const { return mErrorMessage; }
107 QString failedUrl() const { return mFailedUrl; }
108 QList< QByteArray > fontData() const { return mFontData; }
109 QByteArray licenseData() const { return mLicenseData; }
110 QStringList contentDispositionFilenames() const { return mContentDispositionFilenames; }
111 private:
112
113 QgsFontDownloadDetails mDetails;
114 std::unique_ptr< QgsFeedback > mFeedback;
115 bool mResult = false;
116 QString mErrorMessage;
117 QString mFailedUrl;
118 QList< QByteArray > mFontData;
119 QStringList mContentDispositionFilenames;
120 QByteArray mLicenseData;
121
122};
124#endif
125
136class CORE_EXPORT QgsFontManager : public QObject
137{
138 Q_OBJECT
139
140 public:
141
142#ifndef SIP_RUN
145
148#endif
149
155 explicit QgsFontManager( QObject *parent SIP_TRANSFERTHIS = nullptr );
156
172 QMap< QString, QString > fontFamilyReplacements() const;
173
191 void addFontFamilyReplacement( const QString &original, const QString &replacement );
192
210 void setFontFamilyReplacements( const QMap< QString, QString> &replacements );
211
218 QString processFontFamilyName( const QString &name ) const;
219
225 void installUserFonts() SIP_SKIP;
226
248 bool tryToDownloadFontFamily( const QString &family, QString &matchedFamily SIP_OUT );
249
255 void enableFontDownloadsForSession();
256
269 Q_DECL_DEPRECATED QString urlForFontDownload( const QString &family, QString &matchedFamily SIP_OUT ) const SIP_DEPRECATED;
270
286 QgsFontDownloadDetails detailsForFontDownload( const QString &family, QString &matchedFamily SIP_OUT ) const;
287
302 Q_DECL_DEPRECATED void downloadAndInstallFont( const QUrl &url, const QString &identifier = QString() ) SIP_DEPRECATED;
303
318 void downloadAndInstallFont( const QgsFontDownloadDetails &details, const QString &identifier = QString() );
319
335 bool installFontsFromData( const QByteArray &data, QString &errorMessage SIP_OUT, QStringList &families SIP_OUT, QString &licenseDetails SIP_OUT, const QString &filename = QString(), const QString &extension = QString() );
336
346 void addUserFontDirectory( const QString &directory );
347
353 QMap< QString, QStringList > userFontToFamilyMap() const;
354
358 bool removeUserFont( const QString &path );
359
360 signals:
361
372 void fontDownloaded( const QStringList &families, const QString &licenseDetails );
373
380 void fontDownloadErrorOccurred( const QUrl &url, const QString &identifier, const QString &error );
381
382 private:
383
384 QMap< QString, QString > mFamilyReplacements;
385 QMap< QString, QString > mLowerCaseFamilyReplacements;
386 QMap< QString, QStringList > mUserFontToFamilyMap;
387 QMap< QString, int > mUserFontToIdMap;
388 mutable QReadWriteLock mReplacementLock;
389 QStringList mUserFontDirectories;
390
391 bool mEnableFontDownloads = false;
392 QMap< QString, QString > mPendingFontDownloads;
393 QMap< QString, QgsFontDownloadDetails > mDeferredFontDownloads;
394
395 void storeFamilyReplacements();
396 void installFontsFromDirectory( const QString &dir );
397};
398
399#endif // QGSFONTMANAGER_H
Encapsulates details required for downloading a font.
QString licenseUrl() const
Returns the optional URL for downloading the font license details.
QStringList fontUrls() const
Returns a list of download URLs for all files associated with the font family.
QgsFontDownloadDetails()
Constructor for an invalid QgsFontDownloadDetails.
QString standardizedFamily() const
Returns the cleaned, standardized font family name.
bool isValid() const
Returns true if the details represent a valid downloadable font.
QString family() const
Returns the font family.
Manages available fonts and font installation for a QGIS instance.
static const QgsSettingsEntryStringList * settingsFontFamilyReplacements
Settings entry for font family replacements.
static const QgsSettingsEntryBool * settingsDownloadMissingFonts
Settings entry for font family replacements.
A boolean settings entry.
A string list settings entry.
Abstract base class for long running background tasks.
#define SIP_DEPRECATED
Definition qgis_sip.h:106
#define SIP_TRANSFERTHIS
Definition qgis_sip.h:53
#define SIP_SKIP
Definition qgis_sip.h:126
#define SIP_OUT
Definition qgis_sip.h:58