mirror of
https://github.com/Cockatrice/Cockatrice.git
synced 2026-11-08 02:53:25 -08:00
[Icons] Add SVG currentColor sentinel resolver
Qt's SVG renderer has no CSS cascade, so an authored `currentColor` never resolves. Resolve the sentinel to a concrete palette color as plain bytes, before the SVG reaches QSvgRenderer.
This commit is contained in:
parent
829152f388
commit
67d5cc1aab
5 changed files with 97 additions and 0 deletions
|
|
@ -12,6 +12,7 @@ set(UTILITY_SOURCES
|
|||
libcockatrice/utility/passwordhasher.cpp
|
||||
libcockatrice/utility/report_categories.cpp
|
||||
libcockatrice/utility/server_rate_limiter.cpp
|
||||
libcockatrice/utility/svg_recolor.cpp
|
||||
libcockatrice/utility/translation_loader.cpp
|
||||
libcockatrice/utility/user_data_backup.cpp
|
||||
libcockatrice/utility/warning_categories.cpp
|
||||
|
|
@ -35,6 +36,7 @@ set(UTILITY_HEADERS
|
|||
libcockatrice/utility/zone_names.h
|
||||
libcockatrice/utility/days_years_between.h
|
||||
libcockatrice/utility/server_rate_limiter.h
|
||||
libcockatrice/utility/svg_recolor.h
|
||||
libcockatrice/utility/translation_loader.h
|
||||
libcockatrice/utility/user_data_backup.h
|
||||
libcockatrice/utility/warning_categories.h
|
||||
|
|
|
|||
15
libcockatrice_utility/libcockatrice/utility/svg_recolor.cpp
Normal file
15
libcockatrice_utility/libcockatrice/utility/svg_recolor.cpp
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
#include "svg_recolor.h"
|
||||
|
||||
#include <QString>
|
||||
|
||||
QByteArray resolveSvgColorSentinel(const QByteArray &svg, const QByteArray &color)
|
||||
{
|
||||
const QString source = QString::fromUtf8(svg);
|
||||
if (!source.contains(QStringLiteral("currentColor"), Qt::CaseInsensitive)) {
|
||||
return svg;
|
||||
}
|
||||
|
||||
QString resolved = source;
|
||||
resolved.replace(QStringLiteral("currentColor"), QString::fromLatin1(color), Qt::CaseInsensitive);
|
||||
return resolved.toUtf8();
|
||||
}
|
||||
26
libcockatrice_utility/libcockatrice/utility/svg_recolor.h
Normal file
26
libcockatrice_utility/libcockatrice/utility/svg_recolor.h
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
/**
|
||||
* @file svg_recolor.h
|
||||
* @ingroup Core
|
||||
*/
|
||||
|
||||
#ifndef SVG_RECOLOR_H
|
||||
#define SVG_RECOLOR_H
|
||||
|
||||
#include <QByteArray>
|
||||
|
||||
/**
|
||||
* @brief Resolves the `currentColor` sentinel in authored SVG data.
|
||||
*
|
||||
* Qt's SVG renderer has no CSS cascade or ambient color context, so the
|
||||
* sentinel is not resolved by Qt. Callers pass the concrete color they want
|
||||
* (for example a QPalette role serialized to #RRGGBB) and receive SVG bytes
|
||||
* that are safe to hand to QSvgRenderer.
|
||||
*
|
||||
* @param svg UTF-8 SVG source. Every `currentColor` occurrence, in any case,
|
||||
* is replaced.
|
||||
* @param color Concrete SVG color (for example "#ff00ff"), as ASCII bytes.
|
||||
* @return The recolored SVG source; the input is left untouched.
|
||||
*/
|
||||
QByteArray resolveSvgColorSentinel(const QByteArray &svg, const QByteArray &color);
|
||||
|
||||
#endif // SVG_RECOLOR_H
|
||||
Loading…
Add table
Add a link
Reference in a new issue