FifeGUI 0.3.0
A C++ GUI library designed for games.
imagefont.hpp
1// SPDX-License-Identifier: LGPL-2.1-or-later OR BSD-3-Clause
2// SPDX-FileCopyrightText: 2004 - 2008 Olof Naessén and Per Larsson
3// SPDX-FileCopyrightText: 2013 - 2026 Fifengine contributors
4
5#ifndef INCLUDE_FIFECHAN_IMAGEFONT_HPP_
6#define INCLUDE_FIFECHAN_IMAGEFONT_HPP_
7
8// Standard library includes
9#include <array>
10#include <string>
11
12// Platform config include
13#include "fifechan/platform.hpp"
14
15// Project headers (subdirs before local)
16#include "fifechan/color.hpp"
17#include "fifechan/font.hpp"
18#include "fifechan/rectangle.hpp"
19
20namespace fcn
21{
22 class Color;
23 class Graphics;
24 class Image;
25
41 enum class SeparatorStrategy : std::uint8_t
42 {
43 PixelAtOrigin,
44 BorderDominant,
45 ExplicitColor,
46 Auto
47 };
48
59 {
63 SeparatorStrategy strategy = SeparatorStrategy::Auto;
64
68 Color explicitSeparator = Color{255, 0, 255, 255};
69
73 int glyphPadding = 0;
74
78 bool verbose = false;
79 };
80
126 class FIFEGUI_API ImageFont : public Font
127 {
128 public:
139 ImageFont(std::string const & filename, std::string const & glyphs);
140
148 ImageFont(std::string const & filename, std::string const & glyphs, ImageFontConfig const & config);
149
161 ImageFont(Image* image, std::string const & glyphs);
162
171 ImageFont(Image* image, std::string const & glyphs, ImageFontConfig const & config);
172
187 explicit ImageFont(
188 std::string const & filename, unsigned char glyphsFrom = 32, unsigned char glyphsTo = 126);
189 // New constructor with configuration
198 ImageFont(
199 std::string const & filename,
200 unsigned char glyphsFrom,
201 unsigned char glyphsTo,
202 ImageFontConfig const & config);
203
204 ~ImageFont() override;
205
206 ImageFont(ImageFont const &) = delete;
207 ImageFont& operator=(ImageFont const &) = delete;
208 ImageFont(ImageFont&&) = delete;
209 ImageFont& operator=(ImageFont&&) = delete;
210
224 virtual int drawGlyph(Graphics* graphics, unsigned char glyph, int x, int y);
225
233 virtual void setRowSpacing(int spacing);
234
241 virtual int getRowSpacing();
242
250 virtual void setGlyphSpacing(int spacing);
251
258 virtual int getGlyphSpacing();
259
266 virtual int getWidth(unsigned char glyph) const;
267
268 // Inherited from Font
269
270 int getWidth(std::string const & text) const override;
271
272 void drawString(Graphics* graphics, std::string const & text, int x, int y) override;
273
274 int getHeight() const override;
275
276 int getStringIndexAt(std::string const & text, int x) const override;
277
278 protected:
294 Rectangle scanForGlyph(unsigned char glyph, int x, int y, Color const & separator);
295
299 std::array<Rectangle, 256> mGlyph;
300
304 int mHeight{0};
305
310
315
319 Image* mImage{nullptr};
320
324 std::string mFilename;
325 };
326} // namespace fcn
327
328#endif // INCLUDE_FIFECHAN_IMAGEFONT_HPP_
Color.
Definition color.hpp:58
Font(Font const &)=default
Copy constructor.
Abstract interface providing primitive drawing functions (lines, rectangles, etc.).
Definition graphics.hpp:58
virtual int getRowSpacing()
Gets the space between rows in pixels.
int mHeight
Holds the height of the image font.
virtual int drawGlyph(Graphics *graphics, unsigned char glyph, int x, int y)
Draws a glyph.
int getHeight() const override
Gets the height of the glyphs in the font.
int getStringIndexAt(std::string const &text, int x) const override
Gets a string index in a string providing an x coordinate.
Rectangle scanForGlyph(unsigned char glyph, int x, int y, Color const &separator)
Scans for a certain glyph.
Image * mImage
Holds the image with the font data.
std::array< Rectangle, 256 > mGlyph
Holds the glyphs areas in the image.
std::string mFilename
Holds the filename of the image with the font data.
virtual void setRowSpacing(int spacing)
Sets the space between rows in pixels.
ImageFont(std::string const &filename, std::string const &glyphs)
Constructor.
virtual int getWidth(unsigned char glyph) const
Gets a width of a glyph in pixels.
virtual int getGlyphSpacing()
Gets the spacing between letters in pixels.
virtual void setGlyphSpacing(int spacing)
Sets the spacing between glyphs in pixels.
int mGlyphSpacing
Holds the glyph spacing of the image font.
int mRowSpacing
Holds the row spacing of the image font.
void drawString(Graphics *graphics, std::string const &text, int x, int y) override
Draws a string.
Abstract holder for image data.
Definition image.hpp:34
Represents a rectangular area (X, Y, Width, Height).
Definition rectangle.hpp:22
Used replacement tokens by configure_file():
SeparatorStrategy
Defines a font implementation using an image atlas containing glyph data.
Definition imagefont.hpp:42
Configuration struct for ImageFont constructors.
Definition imagefont.hpp:59
bool verbose
If true, enable verbose debug output while scanning fonts.
Definition imagefont.hpp:78
SeparatorStrategy strategy
Strategy used to detect separator color in the image.
Definition imagefont.hpp:63
int glyphPadding
Number of pixels to pad/ignore around detected glyphs.
Definition imagefont.hpp:73
Color explicitSeparator
Explicit separator color used when ExplicitColor strategy is selected.
Definition imagefont.hpp:68