FifeGUI 0.2.0
A C++ GUI library designed for games.
truetypefont.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/backends/sdl2/truetypefont.hpp"
6
7#include <SDL2/SDL_render.h>
8
9#include <string>
10
11#include "fifechan/backends/sdl2/graphics.hpp"
12#include "fifechan/exception.hpp"
13#include "fifechan/graphics.hpp"
14#include "fifechan/image.hpp"
15
16namespace fcn::sdl2
17{
18 TrueTypeFont::TrueTypeFont(std::string const & filename, int size) :
19 mRowSpacing(0),
21 mAntiAlias(true),
22 mFilename(filename),
23 mFont(TTF_OpenFont(filename.c_str(), size))
24 {
25
26 if (mFont == nullptr) {
27 throwException("TrueTypeFont::TrueTypeFont. " + std::string(TTF_GetError()));
28 }
29 }
30
31 TrueTypeFont::~TrueTypeFont()
32 {
33 TTF_CloseFont(mFont);
34 }
35
36 int TrueTypeFont::getWidth(std::string const & text) const
37 {
38 int w = 0;
39 int h = 0;
40 TTF_SizeText(mFont, text.c_str(), &w, &h);
41
42 return w;
43 }
44
46 {
47 return TTF_FontHeight(mFont) + mRowSpacing;
48 }
49
50 void TrueTypeFont::drawString(fcn::Graphics* graphics, std::string const & text, int x, int y)
51 {
52 if (text.empty()) {
53 return;
54 }
55
56 auto* sdlGraphics = dynamic_cast<fcn::sdl2::Graphics*>(graphics);
57
58 if (sdlGraphics == nullptr) {
59 throwException("TrueTypeFont::drawString. Graphics object must be fcn::sdl2::Graphics!");
60 return;
61 }
62
63 int const yoffset = getRowSpacing() / 2;
64
65 Color const col = graphics->getColor();
66
67 SDL_Color sdlCol;
68 sdlCol.b = col.b;
69 sdlCol.r = col.r;
70 sdlCol.g = col.g;
71 sdlCol.a = col.a;
72
73 SDL_Surface* textSurface = nullptr;
74 if (mAntiAlias) {
75 textSurface = TTF_RenderText_Blended(mFont, text.c_str(), sdlCol);
76 } else {
77 textSurface = TTF_RenderText_Solid(mFont, text.c_str(), sdlCol);
78 }
79
80 if (textSurface == nullptr) {
81 throwException("TrueTypeFont::drawString. " + std::string(TTF_GetError()));
82 return;
83 }
84
85 SDL_Renderer* renderer = sdlGraphics->getRenderTarget();
86 SDL_Texture* texture = SDL_CreateTextureFromSurface(renderer, textSurface);
87 if (texture == nullptr) {
88 SDL_FreeSurface(textSurface);
89 throwException("TrueTypeFont::drawString. Failed to create texture: " + std::string(SDL_GetError()));
90 return;
91 }
92
93 SDL_SetTextureBlendMode(texture, SDL_BLENDMODE_BLEND);
94#if SDL_VERSION_ATLEAST(2, 0, 12)
95 SDL_SetTextureScaleMode(texture, SDL_ScaleModeNearest);
96#endif
97
98 SDL_Rect dst;
99 SDL_Rect src;
100 dst.x = x;
101 dst.y = y + yoffset;
102 src.w = textSurface->w;
103 src.h = textSurface->h;
104 src.x = 0;
105 src.y = 0;
106
107 sdlGraphics->drawSDLTexture(texture, src, dst);
108
109 SDL_DestroyTexture(texture);
110 SDL_FreeSurface(textSurface);
111 }
112
114 {
115 mRowSpacing = spacing;
116 }
117
119 {
120 return mRowSpacing;
121 }
122
124 {
125 mGlyphSpacing = spacing;
126 }
127
129 {
130 return mGlyphSpacing;
131 }
132
133 void TrueTypeFont::setAntiAlias(bool antiAlias)
134 {
135 mAntiAlias = antiAlias;
136 }
137
139 {
140 return mAntiAlias;
141 }
142} // namespace fcn::sdl2
Color.
Definition color.hpp:56
uint8_t a
Alpha color component (0-255).
Definition color.hpp:322
uint8_t b
Blue color component (0-255).
Definition color.hpp:319
uint8_t g
Green color component (0-255).
Definition color.hpp:316
uint8_t r
Red color component (0-255).
Definition color.hpp:313
Abstract interface providing primitive drawing functions (lines, rectangles, etc.).
Definition graphics.hpp:57
virtual Color const & getColor() const =0
Gets the color to use when drawing.
SDL2 renderer-specific implementation of the Graphics interface.
TrueTypeFont(std::string const &filename, int size)
Constructor.
int mRowSpacing
Additional spacing between rows in pixels.
void drawString(fcn::Graphics *graphics, std::string const &text, int x, int y) override
Draws a string.
TTF_Font * mFont
Underlying TTF_Font pointer from SDL_ttf.
int mGlyphSpacing
Additional spacing between glyphs in pixels.
std::string mFilename
Filename of the font used to create mFont.
virtual void setGlyphSpacing(int spacing)
Sets the spacing between letters in pixels.
int getWidth(std::string const &text) const override
Gets the width of a string.
virtual void setRowSpacing(int spacing)
Sets the spacing between rows in pixels.
int getHeight() const override
Gets the height of the glyphs in the font.
virtual bool isAntiAlias()
Checks if anti aliasing is used.
virtual int getGlyphSpacing()
Gets the spacing between letters in pixels.
virtual int getRowSpacing()
Gets the spacing between rows in pixels.
bool mAntiAlias
Whether anti-aliasing is enabled for rendering.
virtual void setAntiAlias(bool antiAlias)
Enable or disable anti-aliasing for rendered glyphs.
Unified header for the SDL backend.