cangine
Loading...
Searching...
No Matches
text_renderer.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <iostream>
4#include <vector>
5#include <memory>
6#include <cstdint>
7#include <unordered_map>
8
9#include <ft2build.h>
10#include FT_FREETYPE_H
11
12#include "../../types/types.hpp"
13#include "../texture.hpp"
14#include "../../util/color.hpp"
15#include "../../shapes/shapes.hpp"
16
17namespace cangine {
21 struct Character {
23 int width;
24 int height;
27 unsigned int advance;
28 };
29
33 struct Font {
34 FT_Face face;
35 std::unordered_map<uint32_t, Character> glyphs;
36 int height;
37 std::string path;
38
45 Font(FT_Face f, int h, const std::string& p)
46 : face(f), height(h), path(p) {}
47 };
48
54 std::vector<uint32_t> utf8_to_codepoints(const std::string& str);
55
63 private:
65 FT_Library ft;
70 bool init_freetype();
71 std::unordered_map<std::string, std::unique_ptr<Font>> fonts;
72
73 public:
74 bool valid;
79 void clearGlyphCache(Font* font);
86 Font* loadFont(std::string path, int height = 40);
92 void setFontHeight(Font* font, int heightIn);
99 bool loadGlyph(Font* font, uint32_t cp);
108 void renderText(Font* font, const std::string &text, Vector3 pos, float scale, const Color &color);
116 Vector2 measureText(Font *font, const std::string &text, float scale);
121 static TextRenderer* getInstance();
123 TextRenderer();
126
127 };
128}
Singleton that loads fonts and renders text via FreeType.
Definition text_renderer.hpp:62
void renderText(Font *font, const std::string &text, Vector3 pos, float scale, const Color &color)
Draws a text string at a position.
Definition text_renderer.cpp:129
bool valid
Whether the renderer initialized successfully.
Definition text_renderer.hpp:74
TextRenderer()
Constructs the renderer and initializes FreeType.
Definition text_renderer.cpp:201
bool init_freetype()
Initializes the FreeType library.
Definition text_renderer.cpp:42
std::unordered_map< std::string, std::unique_ptr< Font > > fonts
Loaded fonts keyed by path.
Definition text_renderer.hpp:71
~TextRenderer()
Releases FreeType resources.
Definition text_renderer.cpp:219
Font * loadFont(std::string path, int height=40)
Loads a font from disk (or returns the cached one).
Definition text_renderer.cpp:51
bool loadGlyph(Font *font, uint32_t cp)
Rasterizes and caches a single glyph.
Definition text_renderer.cpp:93
static TextRenderer * getInstance()
Returns the singleton instance.
Definition text_renderer.cpp:197
static TextRenderer * instance
Singleton instance.
Definition text_renderer.hpp:64
void clearGlyphCache(Font *font)
Clears the cached glyphs for a font.
Definition text_renderer.cpp:37
FT_Library ft
FreeType library handle.
Definition text_renderer.hpp:65
void setFontHeight(Font *font, int heightIn)
Changes a font's pixel height and clears its glyph cache.
Definition text_renderer.cpp:83
Vector2 measureText(Font *font, const std::string &text, float scale)
Measures the rendered size of a text string.
Definition text_renderer.cpp:169
Wraps an OpenGL texture object.
Definition texture.hpp:12
Definition audiomanager.cpp:4
std::vector< uint32_t > utf8_to_codepoints(const std::string &str)
Decodes a UTF-8 string into a list of Unicode codepoints.
Definition text_renderer.cpp:9
A single cached glyph: its texture and layout metrics.
Definition text_renderer.hpp:21
unsigned int advance
Horizontal advance to the next glyph (in 1/64 px).
Definition text_renderer.hpp:27
int width
Glyph bitmap width in pixels.
Definition text_renderer.hpp:23
Texture texture
Texture holding the rasterized glyph.
Definition text_renderer.hpp:22
int bearingY
Vertical offset from the baseline to the glyph's top edge.
Definition text_renderer.hpp:26
int height
Glyph bitmap height in pixels.
Definition text_renderer.hpp:24
int bearingX
Horizontal offset from the origin to the glyph's left edge.
Definition text_renderer.hpp:25
RGBA color stored as normalized floating-point components.
Definition color.hpp:12
A loaded font face plus its cache of rasterized glyphs.
Definition text_renderer.hpp:33
std::unordered_map< uint32_t, Character > glyphs
Cache of glyphs keyed by codepoint.
Definition text_renderer.hpp:35
std::string path
Path the font was loaded from.
Definition text_renderer.hpp:37
Font(FT_Face f, int h, const std::string &p)
Constructs a font wrapper around a FreeType face.
Definition text_renderer.hpp:45
int height
Pixel height the face is set to.
Definition text_renderer.hpp:36
FT_Face face
Underlying FreeType face handle.
Definition text_renderer.hpp:34
Two-component floating-point vector.
Definition vector2.hpp:13
Three-component floating-point vector.
Definition vector3.hpp:13