-
Notifications
You must be signed in to change notification settings - Fork 1
/
Font.hpp
75 lines (65 loc) · 1.7 KB
/
Font.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#pragma once
#include <map>
#include <glm/glm.hpp>
#include "incfreetype.h"
#include "Debug.hpp"
#include "Logger.hpp"
#include "Texture.hpp"
namespace ui {
struct Font {
static FT_Library ft;
static bool initialized;
FT_Face face;
struct Character {
gl::Texture tex;
glm::ivec2 size;
glm::ivec2 bearing;
GLuint advance;
Character(gl::Texture tex, glm::ivec2 size, glm::ivec2 bearing, GLuint advance):
tex(tex), size(size), bearing(bearing), advance(advance)
{}
};
std::map<GLchar, Character> alphabet;
std::string filename;
Font(const std::string &filename):
filename(filename)
{}
static void setup() {
int rc = FT_Init_FreeType(&ft);
ASSERT(!rc);
initialized = true;
}
void init() {
ASSERT(initialized);
int rc = FT_New_Face(ft, filename.c_str(), 0, &face);
ASSERT(!rc);
FT_Set_Pixel_Sizes(face, 0, 48);
for(GLubyte c = 0; c < 128; ++c) {
int rc = FT_Load_Char(face, c, FT_LOAD_RENDER);
ASSERT(!rc);
alphabet.insert({c, Character(
gl::Texture("font_texture"),
glm::ivec2(face->glyph->bitmap.width, face->glyph->bitmap.rows),
glm::ivec2(face->glyph->bitmap_left, face->glyph->bitmap_top),
GLuint(face->glyph->advance.x)
)});
ASSERT(alphabet.find(c) != std::end(alphabet));
alphabet.at(c).tex.init(&face->glyph);
}
Logger::Info("Initialized font from file %s\n", filename.c_str());
}
void clear() {
ASSERT(initialized);
FT_Done_Face(face);
for(auto &it : alphabet) {
it.second.tex.clear();
}
}
static void cleanup() {
FT_Done_FreeType(ft);
initialized = false;
}
};
FT_Library Font::ft;
bool Font::initialized = false;
}