SDOM - Simple SDL Document Object Model
A lightweight, extensible Document Object Model for SDL-based applications.
Loading...
Searching...
No Matches
SDOM_IFontObject.hpp
Go to the documentation of this file.
1// SDOM_IFontObject.hpp
2#pragma once
3
4#include <SDL3/SDL.h>
5#include <unordered_map>
7
8namespace SDOM
9{
10 constexpr int maxOutlineThickness = 3; // up to 3, as needed
11
12 // Text alignment options
13 enum class LabelAlign : uint8_t
14 {
15 DEFAULT = 0b0000, // default to top-left
16 LEFT = 0b0001, // default to top-left
17 CENTER = 0b0010, // default to top-center
18 RIGHT = 0b0011, // default to top-right
19 TOP = 0b0100, // default to top-left
20 TOP_LEFT = 0b0101, // top-left
21 TOP_CENTER = 0b0110, // top-center
22 TOP_RIGHT = 0b0111, // top-right
23 MIDDLE = 0b1000, // default to middle-left
24 MIDDLE_LEFT = 0b1001, // middle-left
25 MIDDLE_CENTER = 0b1010, // middle-center
26 MIDDLE_RIGHT = 0b1011, // middle-right
27 BOTTOM = 0b1100, // default to bottom-left
28 BOTTOM_LEFT = 0b1101, // bottom-left
29 BOTTOM_CENTER = 0b1110, // bottom-center
30 BOTTOM_RIGHT = 0b1111 // bottom-right
31 };
32
33 // Font style structure for rendering options
34 struct FontStyle
35 {
36 bool bold = false;
37 bool italic = false;
38 bool underline = false;
39 bool strikethrough = false;
40 bool border = false;
41 bool background = false;
42 bool outline = false;
43 bool dropshadow = false;
44
45 int fontSize = 8; // Uniform scaling for both font types
46 // Optional non-uniform per-axis size (bitmap fonts only).
47 // When negative/unset the BitmapFont implementation will fall back
48 // to the underlying sprite metrics and scale uniformly using
49 // `fontSize`. TrueType fonts ignore these fields.
50 int fontWidth = -1; // Optional: non-uniform width (bitmap only)
51 int fontHeight = -1; // Optional: non-uniform height (bitmap only)
52
53 bool wordwrap = false; // enable word wrap
54 bool auto_resize = true; // enable auto resizing to fit text
55 int maxWidth = 0; // maximum allowable width for auto resizing (0 disables auto width)
56 int maxHeight = 0; // maximum allowable height for auto resizing (0 disables auto height)
57
58 int borderThickness = 1; // Border thickness (default: 0)
59 int outlineThickness = 1; // Outline thickness (default: 0)
60 int padding_horiz = 0; // Horizontal padding (left/right)
61 int padding_vert = 0; // Vertical padding (top/bottom)
62 int dropshadowOffsetX = 1; // Drop shadow offset (horizontal)
63 int dropshadowOffsetY = 1; // Drop shadow offset (vertical)
64
65 LabelAlign alignment = LabelAlign::TOP_LEFT; // Text alignment (default: top-left)
66
67 SDL_Color foregroundColor = {255, 255, 255, 255}; // Text color (default: white)
68 SDL_Color backgroundColor = {0, 0, 0, 0}; // Background color (default: transparent)
69 SDL_Color borderColor = {0, 0, 0, 0}; // Border color (default: transparent)
70 SDL_Color outlineColor = {0, 0, 0, 255}; // Default: black
71 SDL_Color dropshadowColor = {0, 0, 0, 128}; // Default: semi-transparent black
72 };
73
74 // FontStyle inequality operator
75 inline bool operator!=(const SDOM::FontStyle& a, const SDOM::FontStyle& b) {
76 return
77 a.bold != b.bold ||
78 a.italic != b.italic ||
79 a.underline != b.underline ||
81 a.border != b.border ||
82 a.background != b.background ||
83 a.outline != b.outline ||
84 a.dropshadow != b.dropshadow ||
85 a.fontSize != b.fontSize ||
86 a.fontWidth != b.fontWidth ||
87 a.fontHeight != b.fontHeight ||
88 a.wordwrap != b.wordwrap ||
89 a.auto_resize != b.auto_resize ||
90 a.maxWidth != b.maxWidth ||
91 a.maxHeight != b.maxHeight ||
98 a.alignment != b.alignment ||
100 a.foregroundColor.g != b.foregroundColor.g ||
101 a.foregroundColor.b != b.foregroundColor.b ||
102 a.foregroundColor.a != b.foregroundColor.a ||
103 a.backgroundColor.r != b.backgroundColor.r ||
104 a.backgroundColor.g != b.backgroundColor.g ||
105 a.backgroundColor.b != b.backgroundColor.b ||
106 a.backgroundColor.a != b.backgroundColor.a ||
107 a.borderColor.r != b.borderColor.r ||
108 a.borderColor.g != b.borderColor.g ||
109 a.borderColor.b != b.borderColor.b ||
110 a.borderColor.a != b.borderColor.a ||
111 a.outlineColor.r != b.outlineColor.r ||
112 a.outlineColor.g != b.outlineColor.g ||
113 a.outlineColor.b != b.outlineColor.b ||
114 a.outlineColor.a != b.outlineColor.a ||
115 a.dropshadowColor.r != b.dropshadowColor.r ||
116 a.dropshadowColor.g != b.dropshadowColor.g ||
117 a.dropshadowColor.b != b.dropshadowColor.b ||
119 }
120 // FontStyle equality operator
121 inline bool operator==(const SDOM::FontStyle& a, const SDOM::FontStyle& b) {
122 return
123 a.bold == b.bold &&
124 a.italic == b.italic &&
125 a.underline == b.underline &&
127 a.border == b.border &&
128 a.background == b.background &&
129 a.outline == b.outline &&
130 a.dropshadow == b.dropshadow &&
131 a.fontSize == b.fontSize &&
132 a.wordwrap == b.wordwrap &&
133 a.auto_resize == b.auto_resize &&
134 a.maxWidth == b.maxWidth &&
135 a.maxHeight == b.maxHeight &&
139 a.padding_vert == b.padding_vert &&
142 a.alignment == b.alignment &&
143 a.foregroundColor.r == b.foregroundColor.r &&
144 a.foregroundColor.g == b.foregroundColor.g &&
145 a.foregroundColor.b == b.foregroundColor.b &&
146 a.foregroundColor.a == b.foregroundColor.a &&
147 a.backgroundColor.r == b.backgroundColor.r &&
148 a.backgroundColor.g == b.backgroundColor.g &&
149 a.backgroundColor.b == b.backgroundColor.b &&
150 a.backgroundColor.a == b.backgroundColor.a &&
151 a.borderColor.r == b.borderColor.r &&
152 a.borderColor.g == b.borderColor.g &&
153 a.borderColor.b == b.borderColor.b &&
154 a.borderColor.a == b.borderColor.a &&
155 a.outlineColor.r == b.outlineColor.r &&
156 a.outlineColor.g == b.outlineColor.g &&
157 a.outlineColor.b == b.outlineColor.b &&
158 a.outlineColor.a == b.outlineColor.a &&
159 a.dropshadowColor.r == b.dropshadowColor.r &&
160 a.dropshadowColor.g == b.dropshadowColor.g &&
161 a.dropshadowColor.b == b.dropshadowColor.b &&
163 }
164
165
166 class Factory;
167 class Core;
168
170 {
171 using SUPER = IAssetObject;
172
173 public:
174 // --- Type Info --- //
175 static constexpr const char* TypeName = "IFontObject";
176
177 enum class FontType { Bitmap, Truetype };
178 inline static std::unordered_map<FontType, std::string> FontTypeToString = {
179 { FontType::Bitmap, "bitmap" },
180 { FontType::Truetype, "truetype" }
181 };
182 inline static std::unordered_map<std::string, FontType> StringToFontType = {
183 { "bitmap", FontType::Bitmap },
184 { "truetype", FontType::Truetype },
185 // Common aliases used by config/type names
186 { "bitmapfont", FontType::Bitmap },
187 { "truetypefont", FontType::Truetype }
188 };
189
190 // --- Construction & Initialization --- //
192 {
194 {
195 name = TypeName;
196 type = TypeName;
197 filename = TypeName; // Default filename, can be overridden
198 }
199 int fontSize_ = 8; // Font size property for TrueType fonts (and BitmapFont scaling)
200 };
201
202
203 protected:
204 // --- Constructors --- //
205 IFontObject(const InitStruct& init);
206 IFontObject(const sol::table& config);
207
208 // // The filename is immutable after initialization
209 // explicit IFontObject(const std::string& assetName, int fontSize = 10);
210
211 public:
212 // // --- Static Factory Methods --- //
213 // static std::unique_ptr<IFontObject> CreateFromLua(const sol::table& config) {
214 // return std::unique_ptr<IFontObject>(new IFontObject(config));
215 // }
216 // static std::unique_ptr<IFontObject> CreateFromInitStruct(const IAssetObject::InitStruct& baseInit) {
217 // const auto& fontInit = static_cast<const IFontObject::InitStruct&>(baseInit);
218 // return std::unique_ptr<IFontObject>(new IFontObject(fontInit));
219 // }
220
221 virtual ~IFontObject() override;
222
223 // methods to be overridden from IDataObject
224 virtual bool onInit() = 0;
225 virtual void onQuit() = 0;
226
227 // ResourceObject virtual methods
228 virtual void onLoad() = 0;
229 virtual void onUnload() = 0;
230 virtual void create(const sol::table& config) = 0;
231
232 virtual void drawGlyph(Uint32 ch, int x, int y, const FontStyle& style) = 0;
233 virtual void drawPhrase(const std::string& str, int x, int y, const FontStyle& style) = 0;
234 virtual void drawPhraseOutline(const std::string& str, int x, int y, const FontStyle& style) = 0;
235 virtual void drawPhraseDropshadow(const std::string& str, int x, int y, const FontStyle& style) = 0;
236
237 virtual bool getGlyphMetrics(Uint32 ch, int *minx, int *maxx, int *miny, int *maxy, int *advance) const = 0;
238 virtual int getGlyphHeight(Uint32 ch) const = 0;
239 virtual int getGlyphWidth(Uint32 ch) const = 0;
240
241 virtual int getFontAscent() = 0; // int TTF_GetFontAscent(const TTF_Font *font)
242 virtual int getFontSize() = 0; // TTF_GetFontSize(TTF_Font *font);
243 virtual void setFontSize(int p_size) = 0;
244 virtual void setFontStyle(const FontStyle& style) = 0;
245 virtual FontStyle getFontStyle() = 0;
246
247 int getWordWidth(const std::string& word) const;
248 int getWordHeight(const std::string& word) const;
249 int getFontSize() const { return fontSize_; }
250 FontType getFontType() const { return fontType_; }
251
252 // Helper: If a display object has a font resource name pointing to a
253 // BitmapFont asset but did not provide explicit per-axis or size
254 // metrics, this utility fills the provided references with the
255 // BitmapFont's native metrics. This centralizes the fallback logic
256 // so multiple display objects (Button, Label creation helpers, etc.)
257 // can consistently obtain defaults.
258 static void applyBitmapFontDefaults(class Factory& factory,
259 const std::string& fontResourceName,
260 int &outFontSize,
261 int &outFontWidth,
262 int &outFontHeight);
263
264 protected:
265 friend Factory;
266 friend Core;
267
268 int fontSize_ = 8; // Font size property for TrueType fonts (and BitmapFont scaling)
270
271 // --- Lua Registration --- //
272 virtual void _registerLuaBindings(const std::string& typeName, sol::state_view lua);
273
274 }; // END class IFontObject
275
276} // END namespace SDOM
277
Definition SDOM_Factory.hpp:43
Definition SDOM_IAssetObject.hpp:10
IAssetObject()
Definition SDOM_IAssetObject.cpp:29
Definition SDOM_IFontObject.hpp:170
virtual void onQuit()=0
virtual int getFontSize()=0
virtual void drawPhraseOutline(const std::string &str, int x, int y, const FontStyle &style)=0
int getWordWidth(const std::string &word) const
Definition SDOM_IFontObject.cpp:78
friend Core
Definition SDOM_IFontObject.hpp:266
int getWordHeight(const std::string &word) const
Definition SDOM_IFontObject.cpp:89
virtual void drawPhrase(const std::string &str, int x, int y, const FontStyle &style)=0
virtual ~IFontObject() override
Definition SDOM_IFontObject.cpp:72
virtual void onLoad()=0
FontType
Definition SDOM_IFontObject.hpp:177
static std::unordered_map< FontType, std::string > FontTypeToString
Definition SDOM_IFontObject.hpp:178
virtual void setFontStyle(const FontStyle &style)=0
static std::unordered_map< std::string, FontType > StringToFontType
Definition SDOM_IFontObject.hpp:182
virtual int getFontAscent()=0
static constexpr const char * TypeName
Definition SDOM_IFontObject.hpp:175
virtual void drawGlyph(Uint32 ch, int x, int y, const FontStyle &style)=0
FontType fontType_
Definition SDOM_IFontObject.hpp:269
virtual int getGlyphHeight(Uint32 ch) const =0
virtual bool getGlyphMetrics(Uint32 ch, int *minx, int *maxx, int *miny, int *maxy, int *advance) const =0
friend Factory
Definition SDOM_IFontObject.hpp:265
virtual void setFontSize(int p_size)=0
int fontSize_
Definition SDOM_IFontObject.hpp:268
static void applyBitmapFontDefaults(class Factory &factory, const std::string &fontResourceName, int &outFontSize, int &outFontWidth, int &outFontHeight)
Definition SDOM_IFontObject.cpp:143
virtual FontStyle getFontStyle()=0
int getFontSize() const
Definition SDOM_IFontObject.hpp:249
virtual void onUnload()=0
virtual bool onInit()=0
virtual void create(const sol::table &config)=0
virtual int getGlyphWidth(Uint32 ch) const =0
FontType getFontType() const
Definition SDOM_IFontObject.hpp:250
virtual void drawPhraseDropshadow(const std::string &str, int x, int y, const FontStyle &style)=0
virtual void _registerLuaBindings(const std::string &typeName, sol::state_view lua)
Definition SDOM_IFontObject.cpp:99
Contains all core classes and utilities for the SDOM library.
Definition lua_BindHelpers.hpp:7
LabelAlign
Definition SDOM_IFontObject.hpp:14
bool operator!=(const SDOM::FontStyle &a, const SDOM::FontStyle &b)
Definition SDOM_IFontObject.hpp:75
constexpr int maxOutlineThickness
Definition SDOM_IFontObject.hpp:10
@ MIDDLE_RIGHT
Middle-right edge of the parent.
@ BOTTOM
Alias for BOTTOM_CENTER.
@ RIGHT
Alias for MIDDLE_RIGHT.
@ BOTTOM_RIGHT
Bottom-right corner of the parent.
@ BOTTOM_CENTER
Bottom-center edge of the parent.
@ MIDDLE
Alias for MIDDLE_CENTER.
@ DEFAULT
Default anchor point (same as TOP_LEFT)
@ TOP
Alias for TOP_CENTER.
@ LEFT
Alias for MIDDLE_LEFT.
@ TOP_LEFT
Top-left corner of the parent.
@ MIDDLE_CENTER
Center of the parent.
@ BOTTOM_LEFT
Bottom-left corner of the parent.
@ TOP_RIGHT
Top-right corner of the parent.
@ MIDDLE_LEFT
Middle-left edge of the parent.
@ CENTER
Alias for MIDDLE_CENTER.
@ TOP_CENTER
Top-center edge of the parent.
bool operator==(const IAssetObject &a, const IAssetObject &b)
Definition SDOM_IAssetObject.hpp:66
Definition SDOM_IFontObject.hpp:35
bool bold
Definition SDOM_IFontObject.hpp:36
int maxWidth
Definition SDOM_IFontObject.hpp:55
bool strikethrough
Definition SDOM_IFontObject.hpp:39
bool wordwrap
Definition SDOM_IFontObject.hpp:53
bool border
Definition SDOM_IFontObject.hpp:40
int padding_horiz
Definition SDOM_IFontObject.hpp:60
SDL_Color outlineColor
Definition SDOM_IFontObject.hpp:70
int padding_vert
Definition SDOM_IFontObject.hpp:61
bool dropshadow
Definition SDOM_IFontObject.hpp:43
int borderThickness
Definition SDOM_IFontObject.hpp:58
SDL_Color borderColor
Definition SDOM_IFontObject.hpp:69
SDL_Color foregroundColor
Definition SDOM_IFontObject.hpp:67
SDL_Color backgroundColor
Definition SDOM_IFontObject.hpp:68
int fontSize
Definition SDOM_IFontObject.hpp:45
int fontHeight
Definition SDOM_IFontObject.hpp:51
SDL_Color dropshadowColor
Definition SDOM_IFontObject.hpp:71
int dropshadowOffsetY
Definition SDOM_IFontObject.hpp:63
int outlineThickness
Definition SDOM_IFontObject.hpp:59
bool auto_resize
Definition SDOM_IFontObject.hpp:54
bool underline
Definition SDOM_IFontObject.hpp:38
bool italic
Definition SDOM_IFontObject.hpp:37
LabelAlign alignment
Definition SDOM_IFontObject.hpp:65
bool outline
Definition SDOM_IFontObject.hpp:42
int dropshadowOffsetX
Definition SDOM_IFontObject.hpp:62
bool background
Definition SDOM_IFontObject.hpp:41
int maxHeight
Definition SDOM_IFontObject.hpp:56
int fontWidth
Definition SDOM_IFontObject.hpp:50
Definition SDOM_IAssetObject.hpp:18
std::string name
Definition SDOM_IAssetObject.hpp:19
std::string type
Definition SDOM_IAssetObject.hpp:20
std::string filename
Definition SDOM_IAssetObject.hpp:21
Definition SDOM_IFontObject.hpp:192
InitStruct()
Definition SDOM_IFontObject.hpp:193
int fontSize_
Definition SDOM_IFontObject.hpp:199