QGIS API Documentation 3.40.0-Bratislava (b56115d8743)
Loading...
Searching...
No Matches
Signals | Public Member Functions | Static Public Attributes | List of all members
QgsFontManager Class Reference

Manages available fonts and font installation for a QGIS instance. More...

#include <qgsfontmanager.h>

Inheritance diagram for QgsFontManager:
Inheritance graph
[legend]

Signals

void fontDownloaded (const QStringList &families, const QString &licenseDetails)
 Emitted when a font has downloaded and been locally loaded.
 
void fontDownloadErrorOccurred (const QUrl &url, const QString &identifier, const QString &error)
 Emitted when an error occurs during font downloading.
 

Public Member Functions

 QgsFontManager (QObject *parent=nullptr)
 Constructor for QgsFontManager, with the specified parent object.
 
void addFontFamilyReplacement (const QString &original, const QString &replacement)
 Adds a new font replacement from the original font family to a replacement font family.
 
void addUserFontDirectory (const QString &directory)
 Adds a directory to use for user fonts.
 
QgsFontDownloadDetails detailsForFontDownload (const QString &family, QString &matchedFamily) const
 Returns a the details for downloading the specified font family.
 
void downloadAndInstallFont (const QgsFontDownloadDetails &details, const QString &identifier=QString())
 Downloads a font and installs in the user's profile/fonts directory as an application font, where the font family is split over multiple download URLs.
 
Q_DECL_DEPRECATED void downloadAndInstallFont (const QUrl &url, const QString &identifier=QString())
 Downloads a font and installs in the user's profile/fonts directory as an application font, where the font family can be downloaded via a single url.
 
void enableFontDownloadsForSession ()
 Enables font downloads the the current QGIS session.
 
QMap< QString, QString > fontFamilyReplacements () const
 Returns the map of automatic font family replacements.
 
bool installFontsFromData (const QByteArray &data, QString &errorMessage, QStringList &families, QString &licenseDetails, const QString &filename=QString(), const QString &extension=QString())
 Installs local user fonts from the specified raw data.
 
void installUserFonts ()
 Installs user fonts from the profile/fonts directory as application fonts.
 
QString processFontFamilyName (const QString &name) const
 Processes a font family name, applying any matching fontFamilyReplacements() to the name.
 
bool removeUserFont (const QString &path)
 Removes the user font at the specified path.
 
void setFontFamilyReplacements (const QMap< QString, QString > &replacements)
 Sets the map of automatic font family replacements.
 
bool tryToDownloadFontFamily (const QString &family, QString &matchedFamily)
 Tries to download and install the specified font family.
 
Q_DECL_DEPRECATED QString urlForFontDownload (const QString &family, QString &matchedFamily) const
 Returns the URL at which the font family can be downloaded.
 
QMap< QString, QStringList > userFontToFamilyMap () const
 Returns the mapping of installed user fonts to font families.
 

Static Public Attributes

static const QgsSettingsEntryBoolsettingsDownloadMissingFonts = new QgsSettingsEntryBool( QStringLiteral( "downloadMissingFonts" ), QgsSettingsTree::sTreeFonts, true, QStringLiteral( "Automatically download missing fonts whenever possible" ) )
 Settings entry for font family replacements.
 
static const QgsSettingsEntryStringListsettingsFontFamilyReplacements = new QgsSettingsEntryStringList( QStringLiteral( "fontFamilyReplacements" ), QgsSettingsTree::sTreeFonts, QStringList(), QStringLiteral( "Automatic font family replacements" ) )
 Settings entry for font family replacements.
 

Detailed Description

Manages available fonts and font installation for a QGIS instance.

Note
QgsFontManager is not usually directly created, but rather accessed through QgsApplication::fontManager().
Since
QGIS 3.28

Definition at line 136 of file qgsfontmanager.h.

Constructor & Destructor Documentation

◆ QgsFontManager()

QgsFontManager::QgsFontManager ( QObject *  parent = nullptr)
explicit

Constructor for QgsFontManager, with the specified parent object.

Note
QgsFontManager is not usually directly created, but rather accessed through QgsApplication::fontManager().

Definition at line 64 of file qgsfontmanager.cpp.

Member Function Documentation

◆ addFontFamilyReplacement()

void QgsFontManager::addFontFamilyReplacement ( const QString &  original,
const QString &  replacement 
)

Adds a new font replacement from the original font family to a replacement font family.

This is used to transparently map an original font family to an alternative font family, e.g. to permit graceful handling of opening projects which reference fonts which are not available on the system.

The replacement map is stored locally and persists across QGIS sessions.

If replacement is an empty string then any existing mapping for the original family will be removed.

Note
This method is thread safe.
See also
fontFamilyReplacements()
setFontFamilyReplacements()

Definition at line 86 of file qgsfontmanager.cpp.

◆ addUserFontDirectory()

void QgsFontManager::addUserFontDirectory ( const QString &  directory)

Adds a directory to use for user fonts.

This directory will be scanned for any TTF or OTF font files, which will automatically be added and made available for use in the QGIS session.

Additionally, if this is the first user font directory added, any fonts downloaded via downloadAndInstallFont() will be installed into this directory.

Definition at line 2045 of file qgsfontmanager.cpp.

◆ detailsForFontDownload()

QgsFontDownloadDetails QgsFontManager::detailsForFontDownload ( const QString &  family,
QString &  matchedFamily 
) const

Returns a the details for downloading the specified font family.

The returned object will contain all URLs which must be fetched to retrieve the entire font family (eg it may contain one URL per font style).

This method relies on a hardcoded list of available freely licensed fonts, and will return an invalid QgsFontDownloadDetails for any font families not present in this list.

Parameters
familyinput font family name to try to match to known fonts
matchedFamilywill be set to found font family if a match was successful
Returns
details required for downloading font, or an invalid QgsFontDownloadDetails if no URL is available
Since
QGIS 3.38

Definition at line 245 of file qgsfontmanager.cpp.

◆ downloadAndInstallFont() [1/2]

void QgsFontManager::downloadAndInstallFont ( const QgsFontDownloadDetails details,
const QString &  identifier = QString() 
)

Downloads a font and installs in the user's profile/fonts directory as an application font, where the font family is split over multiple download URLs.

The download will proceed in a background task.

The optional identifier string can be used to specify a user-friendly name for the download tasks, e.g. the font family name if known.

See also
fontDownloaded()
fontDownloadErrorOccurred()
Since
QGIS 3.38

Definition at line 1821 of file qgsfontmanager.cpp.

◆ downloadAndInstallFont() [2/2]

void QgsFontManager::downloadAndInstallFont ( const QUrl &  url,
const QString &  identifier = QString() 
)

Downloads a font and installs in the user's profile/fonts directory as an application font, where the font family can be downloaded via a single url.

The download will proceed in a background task.

The optional identifier string can be used to specify a user-friendly name for the download tasks, e.g. the font family name if known.

See also
fontDownloaded()
fontDownloadErrorOccurred()
Deprecated:
QGIS 3.38. Use the version which takes a QgsFontDownloadDetails argument instead.

Definition at line 1905 of file qgsfontmanager.cpp.

◆ enableFontDownloadsForSession()

void QgsFontManager::enableFontDownloadsForSession ( )

Enables font downloads the the current QGIS session.

Warning
Ensure that the QgsApplication is fully initialized before calling this method.

Definition at line 212 of file qgsfontmanager.cpp.

◆ fontDownloaded

void QgsFontManager::fontDownloaded ( const QStringList &  families,
const QString &  licenseDetails 
)
signal

Emitted when a font has downloaded and been locally loaded.

The families list specifies the font families contained in the downloaded font.

If found, the licenseDetails string will be populated with corresponding font license details.

See also
downloadAndInstallFont()
fontDownloadErrorOccurred()

◆ fontDownloadErrorOccurred

void QgsFontManager::fontDownloadErrorOccurred ( const QUrl &  url,
const QString &  identifier,
const QString &  error 
)
signal

Emitted when an error occurs during font downloading.

See also
downloadAndInstallFont()
fontDownloaded()

◆ fontFamilyReplacements()

QMap< QString, QString > QgsFontManager::fontFamilyReplacements ( ) const

Returns the map of automatic font family replacements.

This map is used to transparently map an original font family to an alternative font family, e.g. to permit graceful handling of opening projects which reference fonts which are not available on the system.

The map keys are the original font family names, and the values are the alternative replacement family to use for the font.

Note
This method is thread safe.
See also
addFontFamilyReplacement()
setFontFamilyReplacements()

Definition at line 80 of file qgsfontmanager.cpp.

◆ installFontsFromData()

bool QgsFontManager::installFontsFromData ( const QByteArray &  data,
QString &  errorMessage,
QStringList &  families,
QString &  licenseDetails,
const QString &  filename = QString(),
const QString &  extension = QString() 
)

Installs local user fonts from the specified raw data.

The data array may correspond to the contents of a TTF or OTF font file, or a zipped archive of font files.

Parameters
dataraw font data or zipped font data
errorMessagewill be set to a descriptive error message if the installation fails
familieswill be populated with a list of font families installed from the data
licenseDetailswill be populated with font license details, if found
filenamefilename hint for destination file. Will be ignored for archived content (e.g. zip file data)
extensionknown file extension (eg "ttf", "otf") for font. Should be specified to avoid issues with font installation on Windows if filename is not specified (since QGIS 3.38)
Returns
true if installation was successful.

Definition at line 1910 of file qgsfontmanager.cpp.

◆ installUserFonts()

void QgsFontManager::installUserFonts ( )

Installs user fonts from the profile/fonts directory as application fonts.

Note
Not available in Python bindings

Definition at line 131 of file qgsfontmanager.cpp.

◆ processFontFamilyName()

QString QgsFontManager::processFontFamilyName ( const QString &  name) const

Processes a font family name, applying any matching fontFamilyReplacements() to the name.

Note
This method is thread safe.

Definition at line 113 of file qgsfontmanager.cpp.

◆ removeUserFont()

bool QgsFontManager::removeUserFont ( const QString &  path)

Removes the user font at the specified path.

Definition at line 2070 of file qgsfontmanager.cpp.

◆ setFontFamilyReplacements()

void QgsFontManager::setFontFamilyReplacements ( const QMap< QString, QString > &  replacements)

Sets the map of automatic font family replacements.

This map is used to transparently map an original font family to an alternative font family, e.g. to permit graceful handling of opening projects which reference fonts which are not available on the system.

The map keys are the original font family names, and the values are the alternative replacement family to use for the font.

The replacement map is stored locally and persists across QGIS sessions.

Note
This method is thread safe.
See also
fontFamilyReplacements()
addFontFamilyReplacement()

Definition at line 102 of file qgsfontmanager.cpp.

◆ tryToDownloadFontFamily()

bool QgsFontManager::tryToDownloadFontFamily ( const QString &  family,
QString &  matchedFamily 
)

Tries to download and install the specified font family.

This method will attempt to download missing fonts, if the font download URL is known and the font is freely licensed.

Returns true if a download link for the family is known and the download has commenced, or false if the family is not known and cannot be automatically downloaded.

The actual download operation occurs in a background task, and this method returns immediately. Connect to fontDownloaded() in order to respond when the font is installed and available for use.

Warning
Before calling this method a QgsApplication must be fully initialized and a call to enableFontDownloadsForSession() made.
Parameters
familyinput font family name to try to match to known fonts
matchedFamilywill be set to found font family if a match was successful
Returns
true if match was successful and the download will occur

Definition at line 172 of file qgsfontmanager.cpp.

◆ urlForFontDownload()

QString QgsFontManager::urlForFontDownload ( const QString &  family,
QString &  matchedFamily 
) const

Returns the URL at which the font family can be downloaded.

This method relies on a hardcoded list of available freely licensed fonts, and will return an empty string for any font families not present in this list.

Parameters
familyinput font family name to try to match to known fonts
matchedFamilywill be set to found font family if a match was successful
Returns
URL to download font, or an empty string if no URL is available
Deprecated:
QGIS 3.38. Use detailsForFontDownload() instead.

Definition at line 1815 of file qgsfontmanager.cpp.

◆ userFontToFamilyMap()

QMap< QString, QStringList > QgsFontManager::userFontToFamilyMap ( ) const

Returns the mapping of installed user fonts to font families.

The map keys are the file names, the values are a list of families provided by the file.

Definition at line 2064 of file qgsfontmanager.cpp.

Member Data Documentation

◆ settingsDownloadMissingFonts

const QgsSettingsEntryBool * QgsFontManager::settingsDownloadMissingFonts = new QgsSettingsEntryBool( QStringLiteral( "downloadMissingFonts" ), QgsSettingsTree::sTreeFonts, true, QStringLiteral( "Automatically download missing fonts whenever possible" ) )
static

Settings entry for font family replacements.

Definition at line 147 of file qgsfontmanager.h.

◆ settingsFontFamilyReplacements

const QgsSettingsEntryStringList * QgsFontManager::settingsFontFamilyReplacements = new QgsSettingsEntryStringList( QStringLiteral( "fontFamilyReplacements" ), QgsSettingsTree::sTreeFonts, QStringList(), QStringLiteral( "Automatic font family replacements" ) )
static

Settings entry for font family replacements.

Definition at line 144 of file qgsfontmanager.h.


The documentation for this class was generated from the following files: