Allow rendering any `QChar` so `Renderer` could also be used for other fonts

This commit is contained in:
Martchus 2021-09-13 18:24:08 +02:00
parent 02a56eebfd
commit 56fdef53cd
2 changed files with 24 additions and 5 deletions

View File

@ -30,7 +30,7 @@ Renderer::InternalData::InternalData(int id)
/*!
*
* \class QtForkAwesome::Renderer
* \brief Allows rendering a QtForkAwesome::Icon.
* \brief Allows rendering a QtForkAwesome::Icon (or an arbitrary QChar using an arbitrary font file).
*/
/*!
@ -72,7 +72,7 @@ Renderer::operator bool() const
/*!
* \brief Renders the specified \a icon using the specified \a painter.
*/
void Renderer::render(Icon icon, QPainter *painter, const QRect &rect, const QColor &color)
void QtForkAwesome::Renderer::render(QChar character, QPainter *painter, const QRect &rect, const QColor &color)
{
if (!*this) {
return;
@ -82,14 +82,14 @@ void Renderer::render(Icon icon, QPainter *painter, const QRect &rect, const QCo
painter->save();
painter->setFont(font);
painter->setPen(color);
painter->drawText(rect, QString(QChar(static_cast<IconBaseType>(icon))), QTextOption(Qt::AlignCenter));
painter->drawText(rect, QString(character), QTextOption(Qt::AlignCenter));
painter->restore();
}
/*!
* \brief Renders the specified \a icon as pixmap of the specified \a size.
* \brief Renders the specified \a character as pixmap of the specified \a size.
*/
QPixmap QtForkAwesome::Renderer::pixmap(Icon icon, const QSize &size, const QColor &color)
QPixmap QtForkAwesome::Renderer::pixmap(QChar icon, const QSize &size, const QColor &color)
{
const auto scaleFactor =
#if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
@ -105,4 +105,12 @@ QPixmap QtForkAwesome::Renderer::pixmap(Icon icon, const QSize &size, const QCol
return pm;
}
/*!
* \brief Renders the specified \a icon as pixmap of the specified \a size.
*/
QPixmap Renderer::pixmap(Icon icon, const QSize &size, const QColor &color)
{
return pixmap(QChar(static_cast<IconBaseType>(icon)), size, color);
}
} // namespace QtForkAwesome

View File

@ -27,13 +27,24 @@ public:
~Renderer();
operator bool() const;
void render(QChar character, QPainter *painter, const QRect &rect, const QColor &color);
void render(Icon icon, QPainter *painter, const QRect &rect, const QColor &color);
QPixmap pixmap(QChar icon, const QSize &size, const QColor &color);
QPixmap pixmap(Icon icon, const QSize &size, const QColor &color);
private:
std::unique_ptr<Renderer::InternalData> m_d;
};
/*!
* \brief Renders the specified \a icon using the specified \a painter.
*/
inline void Renderer::render(Icon icon, QPainter *painter, const QRect &rect, const QColor &color)
{
render(QChar(static_cast<IconBaseType>(icon)), painter, rect, color);
}
} // namespace QtForkAwesome
#endif // QT_FORK_AWESOME_RENDERER