3#include "resources/config.h"
7#include <QFontDatabase>
8#include <QGuiApplication>
11#include <QPaintDevice>
22 void setIcon(
const QIcon &icon);
23 void addIconName(
const QString &iconName);
24 const QIcon &locateIcon();
27 QStringList iconNames;
31void IconOverride::setIcon(
const QIcon &icon)
37void IconOverride::addIconName(
const QString &iconName)
39 iconNames.append(iconName);
40 if (!cachedIcon.isNull()) {
45const QIcon &IconOverride::locateIcon()
47 if (!cachedIcon.isNull()) {
50 for (
const auto &iconName : std::as_const(iconNames)) {
51 cachedIcon = QIcon::fromTheme(iconName);
52 if (!cachedIcon.isNull()) {
59struct Renderer::InternalData {
60 explicit InternalData(
int id);
61 static constexpr int invalidId = -1;
65 QStringList fontFamilies;
66 QHash<QChar, IconOverride> overrides;
67 QPaintDevice *paintDevice;
70Renderer::InternalData::InternalData(
int id)
72 , fontFamilies(id != invalidId ? QFontDatabase::applicationFontFamilies(id) : QStringList())
73 , paintDevice(nullptr)
90 : m_d(std::make_unique<InternalData>(
91 QFontDatabase::addApplicationFont(fontFileName.isEmpty() ? QStringLiteral(
":/" META_FONT_FILE_NAME) : fontFileName)))
99 : m_d(std::make_unique<InternalData>(QFontDatabase::addApplicationFont(fontData)))
108 if (QCoreApplication::instance() && m_d->id != InternalData::invalidId) {
109 QFontDatabase::removeApplicationFont(m_d->id);
119 return m_d->fontFilePath;
128 const auto &path = m_d->fontFilePath;
129 if (!path.isEmpty() && !QFile::exists(path)) {
130 qWarning() <<
"ForkAwesome font file does not exist";
132 qWarning() <<
"Unable to load ForkAwesome font from " << (path.isEmpty() ? QStringLiteral(
"buffer") : path);
139Renderer::operator bool()
const
141 return !m_d->fontFamilies.empty();
145static void renderInternally(QChar character, QPainter *painter, QFont &&font,
const QRect &rect,
const QColor &color)
147 font.setPixelSize(rect.height());
149 painter->setFont(font);
150 painter->setPen(color);
151 painter->drawText(rect, QString(character), QTextOption(Qt::AlignCenter));
161 if (
auto override = m_d->overrides.find(character);
override != m_d->overrides.end()) {
162 if (
const auto &overrideIcon = override->locateIcon(); !overrideIcon.isNull()) {
163 overrideIcon.paint(painter, rect, Qt::AlignCenter, QIcon::Normal, QIcon::On);
168 renderInternally(character, painter, QFont(m_d->fontFamilies.front()), rect, color);
175QPixmap
Renderer::pixmap(QChar icon,
const QSize &size,
const QColor &color, qreal scaleFactor)
const
177 if (
auto override = m_d->overrides.find(icon);
override != m_d->overrides.end()) {
178 if (
const auto &overrideIcon = override->locateIcon(); !overrideIcon.isNull()) {
179 return overrideIcon.pixmap(size, QIcon::Normal, QIcon::On);
183 if (!
static_cast<bool>(scaleFactor)) {
185#if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
186 !QCoreApplication::testAttribute(Qt::AA_UseHighDpiPixmaps)
190 (m_d->paintDevice ? m_d->paintDevice->devicePixelRatioF() : qGuiApp->devicePixelRatio());
193 const auto scaledSize = QSize(size * scaleFactor);
194 auto pm = QPixmap(scaledSize);
195 pm.fill(QColor(Qt::transparent));
197 auto painter = QPainter(&pm);
198 renderInternally(icon, &painter, QFont(m_d->fontFamilies.front()), QRect(QPoint(), scaledSize), color);
200 pm.setDevicePixelRatio(scaleFactor);
222 return pixmap(icon, size, color, 0.0);
243 m_d->overrides[character].addIconName(iconNameInTheme);
251 m_d->overrides[character].setIcon(
override);
259 m_d->overrides.clear();
270 m_d->paintDevice = paintDevice;
278 static auto globalRenderer =
Renderer();
279 return globalRenderer;
Renderer(const QString &fontFileName=QString())
Constructs a new renderer with the given fontFileName.
static Renderer & global()
Returns the global instance (which is so far only used by the icon engine plugin).
const QString & fontFilePath() const
Returns the path of the font file.
void clearOverrides()
Clears all overrides added via addThemeOverride() or addOverride().
void addThemeOverride(QChar character, const QString &iconNameInTheme)
Uses the icon from the current icon theme obtained via QIcon::fromTheme() for character if it exists.
void warnIfInvalid() const
Prints a warning using qWarning() if no font could be loaded.
void addOverride(QChar character, const QIcon &override)
Uses the specified override icon for character if it is not null.
QPixmap pixmap(QChar icon, const QSize &size, const QColor &color, qreal scaleFactor) const
Renders the specified character as pixmap of the specified size.
void setAssociatedPaintDevice(QPaintDevice *paintDevice)
Sets the associated paintDevice.
~Renderer()
Destructs the renderer.
void render(QChar character, QPainter *painter, const QRect &rect, const QColor &color) const
Renders the specified icon using the specified painter.
Contains classes provided by the QtForkAwesome library.
Icon
The Icon enum specifies a ForkAwesome icon for calling QtForkAwesome::Renderer::render().
std::remove_reference_t< decltype(QChar().unicode())> IconBaseType