Zappy GUI
C++ graphic client: renderer, camera, network
Loading...
Searching...
No Matches
TextRenderer.cpp
Go to the documentation of this file.
1#include "TextRenderer.hpp"
2
3#include <vector>
4
5Font TextRenderer::_font = {};
6bool TextRenderer::_loaded = false;
7
9{
10 return 0.0f; // monospace advance is baked into glyphs; no extra gap
11}
12
14{
15 static const Font fallback = GetFontDefault();
16 return _loaded ? _font : fallback;
17}
18
19// Codepoints to bake: ASCII (32–126) + Latin-1 Supplement (160–255).
20// Covers é, è, ê, à, ù, ç, ô, î, û and all FR characters used in I18n strings.
21static std::vector<int> _buildCodepoints()
22{
23 std::vector<int> cp;
24 cp.reserve(95 + 96);
25 for (int i = 32; i <= 126; ++i) cp.push_back(i);
26 for (int i = 160; i <= 255; ++i) cp.push_back(i);
27 return cp;
28}
29
30bool TextRenderer::loadFont(const std::string& path)
31{
32 if (_loaded) unloadFont();
33
34 static auto codepoints = _buildCodepoints();
35 _font =
36 LoadFontEx(path.c_str(), BASE_SIZE, codepoints.data(), static_cast<int>(codepoints.size()));
37 if (_font.texture.id == 0) return false;
38
39 SetTextureFilter(_font.texture, TEXTURE_FILTER_BILINEAR); // smooth when scaled
40 _loaded = true;
41 return true;
42}
43
45{
46 if (!_loaded) return;
47 UnloadFont(_font);
48 _font = {};
49 _loaded = false;
50}
51
52void TextRenderer::draw(const std::string& text, int x, int y, int fontSize, Color color)
53{
54 Vector2 pos = {static_cast<float>(x), static_cast<float>(y)};
55 DrawTextEx(_active(), text.c_str(), pos, static_cast<float>(fontSize), _spacing(fontSize),
56 color);
57}
58
59int TextRenderer::measure(const std::string& text, int fontSize)
60{
61 return static_cast<int>(
62 MeasureTextEx(_active(), text.c_str(), static_cast<float>(fontSize), _spacing(fontSize)).x);
63}
static std::vector< int > _buildCodepoints()
static constexpr int BASE_SIZE
static Font _font
static float _spacing(int fontSize)
static void draw(const std::string &text, int x, int y, int fontSize, Color color)
static bool _loaded
static const Font & _active()
static bool loadFont(const std::string &path)
static int measure(const std::string &text, int fontSize)
static void unloadFont()