FifeGUI 0.3.0
A C++ GUI library designed for games.
defaultfont.cpp
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// Corresponding header include
6#include "fifechan/defaultfont.hpp"
7
8// Standard library includes
9#include <string>
10
11// Project headers (subdirs before local)
12#include "fifechan/graphics.hpp"
13
14namespace fcn
15{
17 {
18 return 8;
19 }
20
21 int DefaultFont::getWidth(std::string const & text) const
22 {
23 return 8 * text.size();
24 }
25
26 int DefaultFont::drawGlyph(Graphics* graphics, unsigned char glyph, int x, int y)
27 {
28 (void)glyph; // unused parameter
29
30 graphics->drawRectangle(x, y, 8, 8);
31
32 return 8;
33 }
34
35 void DefaultFont::drawString(Graphics* graphics, std::string const & text, int x, int y)
36 {
37 int const glyphWidth = getWidth(" ");
38 for (char const & ch : text) {
39 drawGlyph(graphics, ch, x, y);
40 x += glyphWidth;
41 }
42 }
43
44 int DefaultFont::getStringIndexAt(std::string const & text, int x) const
45 {
46 int const glyphWidth = getWidth(" ");
47 if (x > static_cast<int>(text.size()) * glyphWidth) {
48 return text.size();
49 }
50
51 return x / glyphWidth;
52 }
53} // namespace fcn
int getWidth(std::string const &text) const override
Gets the width of a string.
virtual int drawGlyph(Graphics *graphics, unsigned char glyph, int x, int y)
Draws a glyph as a rectangle.
int getStringIndexAt(std::string const &text, int x) const override
Gets a string index in a string providing an x coordinate.
int getHeight() const override
Gets the height of the glyphs in the font.
void drawString(Graphics *graphics, std::string const &text, int x, int y) override
Draws a string.
Abstract interface providing primitive drawing functions (lines, rectangles, etc.).
Definition graphics.hpp:58
virtual void drawRectangle(Rectangle const &rectangle)=0
Draws a simple, non-filled rectangle with a one pixel width.
Used replacement tokens by configure_file():