From 66b5b0aca1ce5f7f775ae192c8c05948ba2bc380 Mon Sep 17 00:00:00 2001 From: RickyRister Date: Sun, 2 Feb 2025 01:33:49 -0800 Subject: [PATCH] scale up svg --- .../src/client/ui/pixel_map_generator.cpp | 23 +++++++++++++------ .../src/client/ui/pixel_map_generator.h | 2 +- 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/cockatrice/src/client/ui/pixel_map_generator.cpp b/cockatrice/src/client/ui/pixel_map_generator.cpp index 501fa3c95..670a18873 100644 --- a/cockatrice/src/client/ui/pixel_map_generator.cpp +++ b/cockatrice/src/client/ui/pixel_map_generator.cpp @@ -129,9 +129,18 @@ void setAttrRecur(QDomElement &elem, } /** - * Returns an icon of the svg that has its color filled in + * Loads the usericon svg and fills in its colors. + * The image is kept as a QIcon to preserve the image quality. + * + * Call icon.pixmap(w, h) in order to convert this icon into a pixmap with the given dimensions. + * Avoid scaling the pixmap in other ways, as that destroys image quality. + * + * @param minSize If the dimensions of the source svg is smaller than this, then it will be scaled up to this size */ -QIcon changeSVGColor(const QString &iconPath, const QString &colorLeft, const std::optional &colorRight) +static QIcon loadAndColorSvg(const QString &iconPath, + const QString &colorLeft, + const std::optional &colorRight, + const int minSize) { QFile file(iconPath); if (!file.open(QIODevice::ReadOnly)) { @@ -152,7 +161,7 @@ QIcon changeSVGColor(const QString &iconPath, const QString &colorLeft, const st QSvgRenderer svgRenderer(doc.toByteArray()); - QPixmap pix(svgRenderer.defaultSize()); + QPixmap pix(svgRenderer.defaultSize().expandedTo(QSize(minSize, minSize))); pix.fill(Qt::transparent); QPainter pixPainter(&pix); @@ -171,7 +180,7 @@ QPixmap UserLevelPixmapGenerator::generatePixmap(int height, return generateIcon(height, userLevel, pawnColorsOverride, isBuddy, privLevel).pixmap(height, height); } -QIcon UserLevelPixmapGenerator::generateIcon(int height, +QIcon UserLevelPixmapGenerator::generateIcon(int minHeight, UserLevelFlags userLevel, ServerInfo_User::PawnColorsOverride pawnColorsOverride, bool isBuddy, @@ -189,11 +198,11 @@ QIcon UserLevelPixmapGenerator::generateIcon(int height, // Has Color Override if (colorLeft.has_value()) { - return generateIconWithColorOverride(height, isBuddy, privLevel, colorLeft, colorRight); + return generateIconWithColorOverride(minHeight, isBuddy, privLevel, colorLeft, colorRight); } // Has No Color Override - return generateIconDefault(height, userLevel, isBuddy, privLevel); + return generateIconDefault(minHeight, userLevel, isBuddy, privLevel); } QIcon UserLevelPixmapGenerator::generateIconDefault(int height, @@ -263,7 +272,7 @@ QIcon UserLevelPixmapGenerator::generateIconWithColorOverride(int height, QString iconPath = QString("theme:usericons/%1_%2.svg").arg(iconType).arg(arity); - QIcon icon(changeSVGColor(iconPath, colorLeft.value(), colorRight)); + QIcon icon = loadAndColorSvg(iconPath, colorLeft.value(), colorRight, height); iconCache.insert(key, icon); return icon; } diff --git a/cockatrice/src/client/ui/pixel_map_generator.h b/cockatrice/src/client/ui/pixel_map_generator.h index b206ee0ea..fc18f107d 100644 --- a/cockatrice/src/client/ui/pixel_map_generator.h +++ b/cockatrice/src/client/ui/pixel_map_generator.h @@ -81,7 +81,7 @@ public: bool isBuddy, const QString &privLevel); - static QIcon generateIcon(int height, + static QIcon generateIcon(int minHeight, UserLevelFlags userLevel, ServerInfo_User::PawnColorsOverride pawnColors, bool isBuddy,