[network/ruqola] src/widgets/misc: Make sure that emoji is drawing correctly in dpr

Laurent Montel <[email protected]>
Newsgroups gmane.comp.kde.cvs
Message-ID <[email protected]>
Git commit edcbb896dcd253a9596e089a1314dcdd08043799 by Laurent Montel.
Committed on 13/08/2026 at 20:41.
Pushed by mlaurent into branch 'master'.

Make sure that emoji is drawing correctly in dpr

M  +26   -16   src/widgets/misc/avatarcachemanager.cpp

https://invent.kde.org/network/ruqola/-/commit/edcbb896dcd253a9596e089a1314dcdd08043799

diff --git a/src/widgets/misc/avatarcachemanager.cpp b/src/widgets/misc/avatarcachemanager.cpp
index e07e903b80..455258b90f 100644
--- a/src/widgets/misc/avatarcachemanager.cpp
+++ b/src/widgets/misc/avatarcachemanager.cpp
@@ -68,24 +68,34 @@ QPixmap AvatarCacheManager::makeAvatarEmojiPixmap(const QString &emojiStr, const
     if (downScaled.isNull()) {
         auto *emojiManager = mRocketChatAccount->emojiManager();
         const TextEmoticonsCore::UnicodeEmoticon emoticon = emojiManager->unicodeEmoticonForEmoji(emojiStr);
-        if (emoticon.isValid()) {
-            const QFontMetrics fm(mEmojiFont);
-            const QSize size = fm.boundingRect(emoticon.unicode()).size();
-
-            // qDebug() << " size " << size << "emojiStr "<< emojiStr << " emoticon.unicode() " <<emoticon.unicode() <<
-            // fm.horizontalAdvance(emoticon.unicode()); boundingRect can return a width == 0 for existing character as :warning: emoji.
-            QPixmap fullScale(fm.horizontalAdvance(emoticon.unicode()), size.height());
-
-            fullScale.fill(Qt::white);
-            QPainter painter(&fullScale);
-            painter.setFont(mEmojiFont);
-            painter.drawText(fullScale.rect(), Qt::AlignCenter, emoticon.unicode());
-            downScaled = fullScale.scaledToHeight(maxHeight * dpr, Qt::SmoothTransformation);
-            downScaled.setDevicePixelRatio(dpr);
-            cache.insertCachedPixmap(emojiStr, downScaled);
-        } else {
+        if (!emoticon.isValid()) {
             return makeAvatarPixmap(widget, info, maxHeight);
         }
+        const int targetHeight = qMax(1, qRound(maxHeight * dpr));
+        // Draw the glyph at (roughly) its final device size: rendering at the font's natural size and
+        // upscaling afterwards made emoji avatars blurry on hidpi screens.
+        QFont emojiFont(mEmojiFont);
+        emojiFont.setPixelSize(targetHeight);
+        const QFontMetrics fm(emojiFont);
+        const QSize size = fm.boundingRect(emoticon.unicode()).size();
+
+        // qDebug() << " size " << size << "emojiStr "<< emojiStr << " emoticon.unicode() " <<emoticon.unicode() <<
+        // fm.horizontalAdvance(emoticon.unicode()); boundingRect can return a width == 0 for existing character as :warning: emoji.
+        QPixmap fullScale(fm.horizontalAdvance(emoticon.unicode()), size.height());
+
+        // The avatar is drawn over the view background, so the emoji must not carry an opaque
+        // rectangle of its own.
+        fullScale.fill(Qt::transparent);
+        QPainter painter(&fullScale);
+        painter.setFont(emojiFont);
+        // Emojis without a color glyph fall back to the pen color, which defaults to black: follow the
+        // palette so that they stay readable with a dark color scheme.
+        painter.setPen(widget->palette().color(QPalette::WindowText));
+        painter.drawText(fullScale.rect(), Qt::AlignCenter, emoticon.unicode());
+        painter.end();
+        downScaled = fullScale.scaledToHeight(targetHeight, Qt::SmoothTransformation);
+        downScaled.setDevicePixelRatio(dpr);
+        cache.insertCachedPixmap(emojiStr, downScaled);
     }
     return downScaled;
 }
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.