game_launcher 1.1.0
Modern Linux game launcher with Steam / Proton / Wine integration
Loading...
Searching...
No Matches
icon_font.h
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2// ==============================================================================
3// qt/icon_font.h
4// ==============================================================================
5// FontAwesome 6 icon utility — loads bundled FA fonts and provides helpers
6// for rendering icons as QIcon or inline text throughout the UI.
7// ==============================================================================
8
9#pragma once
10
11#include <QColor>
12#include <QFont>
13#include <QFontDatabase>
14#include <QGuiApplication>
15#include <QIcon>
16#include <QPainter>
17#include <QPixmap>
18#include <QString>
19
20// ==============================================================================
21// FontAwesome 6 codepoint constants
22// ==============================================================================
23
24namespace FA
25{
26
27// Library sidebar
28constexpr char32_t TableCellsLarge = 0xf009; // grid / "All"
29constexpr char32_t Star = 0xf005; // Favorites
30constexpr char32_t ClockRotateLeft = 0xf1da; // Recently Played
31constexpr char32_t HardDrive = 0xf0a0; // Installed
32constexpr char32_t Cloud = 0xf0c2; // Not Installed
33constexpr char32_t CircleUp = 0xf35b; // Updates Available
34constexpr char32_t CirclePlay = 0xf144; // Running
35
36// Sources
37constexpr char32_t Steam = 0xf1b6; // Steam (Brands)
38constexpr char32_t WineGlass = 0xf4e3; // Lutris / Wine
39constexpr char32_t CompactDisc = 0xf51f; // GOG
40constexpr char32_t Gamepad = 0xf11b; // Epic / generic game
41constexpr char32_t Folder = 0xf07b; // Local
42constexpr char32_t ItchIo = 0xf83a; // Itch.io (Brands)
43constexpr char32_t BoxOpen = 0xf49e; // Flatpak
44
45// Runners
46constexpr char32_t LayerGroup = 0xf5fd; // All runners
47constexpr char32_t Linux = 0xf17c; // Native Linux (Brands)
48constexpr char32_t Atom = 0xf5d2; // Proton
49
50// Collections
51constexpr char32_t FolderOpen = 0xf07c;
52constexpr char32_t Bookmark = 0xf02e;
53
54// Chevrons
55constexpr char32_t ChevronRight = 0xf054;
56constexpr char32_t ChevronDown = 0xf078;
57
58// Toolbar / UI
59constexpr char32_t Bars = 0xf0c9; // Hamburger menu
60constexpr char32_t TableColumns = 0xf0db; // Sidebar toggle
61constexpr char32_t TableList = 0xf00b; // Games list toggle
62constexpr char32_t BookOpen = 0xf518; // Details toggle
63constexpr char32_t Terminal = 0xf120; // Monitor
64constexpr char32_t House = 0xf015; // Home Assistant
65
66// General
67constexpr char32_t Play = 0xf04b;
68constexpr char32_t Stop = 0xf04d;
69constexpr char32_t Download = 0xf019;
70constexpr char32_t ArrowUp = 0xf062;
71constexpr char32_t ChartBar = 0xf080;
72constexpr char32_t Gear = 0xf013;
73constexpr char32_t CircleCheck = 0xf058;
74constexpr char32_t Xmark = 0xf00d;
75constexpr char32_t Refresh = 0xf021;
76constexpr char32_t CloudArrowDown = 0xf0ed;
77constexpr char32_t Plus = 0x2b;
78
79} // namespace FA
80
81// ==============================================================================
82// IconFont — lightweight utility for rendering FontAwesome icons
83// ==============================================================================
84
86{
87 public:
88 /// Load FontAwesome fonts from Qt resources. Call once at startup.
89 static bool init()
90 {
91 int solidId = QFontDatabase::addApplicationFont(":/fonts/FontAwesome6-Solid.ttf");
92 int brandsId = QFontDatabase::addApplicationFont(":/fonts/FontAwesome6-Brands.ttf");
93
94 if (solidId >= 0)
95 {
96 auto families = QFontDatabase::applicationFontFamilies(solidId);
97 if (!families.isEmpty())
98 solidFamily_ = families.first();
99 }
100
101 if (brandsId >= 0)
102 {
103 auto families = QFontDatabase::applicationFontFamilies(brandsId);
104 if (!families.isEmpty())
105 brandsFamily_ = families.first();
106 }
107
108 initialized_ = !solidFamily_.isEmpty();
109 return initialized_;
110 }
111
112 /// Convert a codepoint to a QString glyph
113 static QString text(char32_t codepoint)
114 {
115 return QString::fromUcs4(&codepoint, 1);
116 }
117
118 /// Render a FontAwesome glyph as a QIcon at the given size and color
119 static QIcon icon(char32_t codepoint, int size, const QColor& color)
120 {
121 QPixmap pm = renderPixmap(codepoint, size, color);
122 return QIcon(pm);
123 }
124
125 /// Render a FontAwesome glyph as QIcon with normal + selected colors
126 static QIcon icon(char32_t codepoint, int size, const QColor& normalColor,
127 const QColor& selectedColor)
128 {
129 QIcon ic;
130 ic.addPixmap(renderPixmap(codepoint, size, normalColor), QIcon::Normal);
131 ic.addPixmap(renderPixmap(codepoint, size, selectedColor), QIcon::Selected);
132 return ic;
133 }
134
135 /// Get the loaded Solid font family name
136 static const QString& solidFamily()
137 {
138 return solidFamily_;
139 }
140
141 /// Get the loaded Brands font family name
142 static const QString& brandsFamily()
143 {
144 return brandsFamily_;
145 }
146
147 /// Check if a codepoint is from the Brands font
148 static bool isBrandIcon(char32_t cp)
149 {
150 return cp == FA::Steam || cp == FA::Linux || cp == FA::ItchIo;
151 }
152
153 /// Check if fonts were loaded successfully
154 static bool isInitialized()
155 {
156 return initialized_;
157 }
158
159 private:
160 static QPixmap renderPixmap(char32_t codepoint, int size, const QColor& color)
161 {
162 auto* app = qobject_cast<QGuiApplication*>(QCoreApplication::instance());
163 qreal dpr = app ? app->devicePixelRatio() : 1.0;
164 int pixelSize = static_cast<int>(size * dpr);
165
166 QPixmap pm(pixelSize, pixelSize);
167 pm.setDevicePixelRatio(dpr);
168 pm.fill(Qt::transparent);
169
170 QPainter p(&pm);
171 p.setRenderHint(QPainter::Antialiasing);
172 p.setRenderHint(QPainter::TextAntialiasing);
173
174 QFont font(isBrandIcon(codepoint) ? brandsFamily_ : solidFamily_);
175 font.setPixelSize(static_cast<int>(size * 0.85)); // Slight reduction for visual centering
176 p.setFont(font);
177 p.setPen(color);
178 p.drawText(QRect(0, 0, size, size), Qt::AlignCenter, text(codepoint));
179 p.end();
180
181 return pm;
182 }
183
184 static inline QString solidFamily_;
185 static inline QString brandsFamily_;
186 static inline bool initialized_ = false;
187};
static QIcon icon(char32_t codepoint, int size, const QColor &normalColor, const QColor &selectedColor)
Render a FontAwesome glyph as QIcon with normal + selected colors.
Definition icon_font.h:126
static const QString & brandsFamily()
Get the loaded Brands font family name.
Definition icon_font.h:142
static bool isBrandIcon(char32_t cp)
Check if a codepoint is from the Brands font.
Definition icon_font.h:148
static QIcon icon(char32_t codepoint, int size, const QColor &color)
Render a FontAwesome glyph as a QIcon at the given size and color.
Definition icon_font.h:119
static bool init()
Load FontAwesome fonts from Qt resources. Call once at startup.
Definition icon_font.h:89
static const QString & solidFamily()
Get the loaded Solid font family name.
Definition icon_font.h:136
static QString text(char32_t codepoint)
Convert a codepoint to a QString glyph.
Definition icon_font.h:113
static bool isInitialized()
Check if fonts were loaded successfully.
Definition icon_font.h:154
Definition icon_font.h:25
constexpr char32_t ClockRotateLeft
Definition icon_font.h:30
constexpr char32_t HardDrive
Definition icon_font.h:31
constexpr char32_t Play
Definition icon_font.h:67
constexpr char32_t ArrowUp
Definition icon_font.h:70
constexpr char32_t CloudArrowDown
Definition icon_font.h:76
constexpr char32_t FolderOpen
Definition icon_font.h:51
constexpr char32_t House
Definition icon_font.h:64
constexpr char32_t CompactDisc
Definition icon_font.h:39
constexpr char32_t ChevronDown
Definition icon_font.h:56
constexpr char32_t CircleUp
Definition icon_font.h:33
constexpr char32_t CircleCheck
Definition icon_font.h:73
constexpr char32_t Bars
Definition icon_font.h:59
constexpr char32_t CirclePlay
Definition icon_font.h:34
constexpr char32_t Atom
Definition icon_font.h:48
constexpr char32_t BoxOpen
Definition icon_font.h:43
constexpr char32_t ChevronRight
Definition icon_font.h:55
constexpr char32_t Terminal
Definition icon_font.h:63
constexpr char32_t Cloud
Definition icon_font.h:32
constexpr char32_t Gamepad
Definition icon_font.h:40
constexpr char32_t Star
Definition icon_font.h:29
constexpr char32_t ChartBar
Definition icon_font.h:71
constexpr char32_t BookOpen
Definition icon_font.h:62
constexpr char32_t TableColumns
Definition icon_font.h:60
constexpr char32_t WineGlass
Definition icon_font.h:38
constexpr char32_t LayerGroup
Definition icon_font.h:46
constexpr char32_t ItchIo
Definition icon_font.h:42
constexpr char32_t Bookmark
Definition icon_font.h:52
constexpr char32_t Linux
Definition icon_font.h:47
constexpr char32_t Download
Definition icon_font.h:69
constexpr char32_t TableCellsLarge
Definition icon_font.h:28
constexpr char32_t Plus
Definition icon_font.h:77
constexpr char32_t Steam
Definition icon_font.h:37
constexpr char32_t Folder
Definition icon_font.h:41
constexpr char32_t Xmark
Definition icon_font.h:74
constexpr char32_t TableList
Definition icon_font.h:61
constexpr char32_t Stop
Definition icon_font.h:68
constexpr char32_t Gear
Definition icon_font.h:72
constexpr char32_t Refresh
Definition icon_font.h:75