FifeGUI 0.2.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#include "fifechan/defaultfont.hpp"
6
7#include <string>
8
9#include "fifechan/graphics.hpp"
10
11namespace fcn
12{
14 {
15 return 8;
16 }
17
18 int DefaultFont::getWidth(std::string const & text) const
19 {
20 return 8 * text.size();
21 }
22
23 int DefaultFont::drawGlyph(Graphics* graphics, unsigned char glyph, int x, int y)
24 {
25 (void)glyph; // unused parameter
26
27 graphics->drawRectangle(x, y, 8, 8);
28
29 return 8;
30 }
31
32 void DefaultFont::drawString(Graphics* graphics, std::string const & text, int x, int y)
33 {
34 int const glyphWidth = getWidth(" ");
35 for (char const & ch : text) {
36 drawGlyph(graphics, ch, x, y);
37 x += glyphWidth;
38 }
39 }
40
41 int DefaultFont::getStringIndexAt(std::string const & text, int x) const
42 {
43 int const glyphWidth = getWidth(" ");
44 if (x > static_cast<int>(text.size()) * glyphWidth) {
45 return text.size();
46 }
47
48 return x / glyphWidth;
49 }
50} // 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:57
virtual void drawRectangle(Rectangle const &rectangle)=0
Draws a simple, non-filled rectangle with a one pixel width.