-
Notifications
You must be signed in to change notification settings - Fork 5
/
freetype-gl-cpp.h
175 lines (136 loc) · 4.93 KB
/
freetype-gl-cpp.h
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#include <stdarg.h>
#include <stdio.h>
#include <string>
#ifdef WITH_EIGEN
#include <eigen3/Eigen/Dense>
#endif
#include "freetype-gl/freetype-gl.h"
#include "freetype-gl/font-manager.h"
#include "freetype-gl/vertex-buffer.h"
#include "freetype-gl/text-buffer.h"
#include "freetype-gl/markup.h"
#include "freetype-gl/demos/mat4.h"
namespace ftgl {
constexpr char* shader_text_frag = (char*)
#include "generated/text.frag"
;
constexpr char* shader_text_vert = (char*)
#include "generated/text.vert"
;
#ifdef WITH_EIGEN
void eigen2mat4(const Eigen::Matrix4f& src, ftgl::mat4* dst);
#endif
class FreetypeGl;
class Markup {
friend class FreetypeGl;
public:
Markup();
Markup(const std::string& font_family,
float size,
const vec4 &color,
bool bold,
bool underlined,
bool italic,
bool strikethrough,
bool overline,
FreetypeGl* freetype_gl);
Markup(Markup&& other);
Markup(const Markup& other) = delete;
Markup& operator=(Markup&& other);
Markup& operator=(const Markup& other) = delete;
virtual ~Markup();
markup_t description;
private:
FreetypeGl* manager;
};
class FreetypeGlText {
public:
template <typename... markup_text>
explicit FreetypeGlText(const FreetypeGl* freetypeGL, const markup_text&... content) : manager(freetypeGL)
{
text_buffer = text_buffer_new();
vec2 pen = {{0,0}};
text_buffer_printf(text_buffer, &pen, content...);
mat4_set_identity(&pose);
}
FreetypeGlText(FreetypeGlText&& other);
FreetypeGlText(const FreetypeGlText& other) = delete;
FreetypeGlText& operator=(const FreetypeGlText& other) = delete;
virtual ~FreetypeGlText();
inline const text_buffer_t* getTextBuffer() const { return text_buffer; }
void render();
#ifdef WITH_EIGEN
void setPose(const Eigen::Matrix4f& pose);
void setPosition(const Eigen::Vector3f& position);
#endif
inline void setPose(const mat4& p){ pose = p; }
inline void setPosition(float x, float y, float z) { pose.m30 = x; pose.m31 = y; pose.m32 = z;}
ftgl::mat4 pose;
inline void setScalingFactor(float s){ scaling_factor = s; }
ftgl::mat4 getModelMatrix() const;
private:
float scaling_factor = 1;
const FreetypeGl* manager;
text_buffer_t* text_buffer;
};
class FreetypeGl {
friend class Markup;
public:
FreetypeGl(bool initialise = false);
FreetypeGl(const FreetypeGl& other) = delete;
FreetypeGl& operator=(const FreetypeGl& other) = delete;
~FreetypeGl();
void init();
Markup createMarkup(const std::string& font_family,
float size,
const vec4& color=FreetypeGl::COLOR_WHITE,
bool bold=false,
bool underlined=false,
bool italic=false,
bool strikethrough=false,
bool overline=false);
static std::string findFont(const std::string& search_pattern);
FreetypeGlText createText(const std::string& text, const Markup& markup);
FreetypeGlText createText(const std::string& text, const markup_t* markup = NULL);
/**
* @brief renderText Renders text directly (slow but easy to use)
* @param text
*/
void renderText(const std::string& text);
void renderText(const FreetypeGlText& text, bool call_pre_post=true) const;
void updateTexture();
void preRender() const;
void postRender() const;
#ifdef WITH_EIGEN
void setView(const Eigen::Matrix4f& v);
void setProjection(const Eigen::Matrix4f& p);
#endif
void setView(const ftgl::mat4& v);
void setProjection(const ftgl::mat4& p);
void setView(const float* v);
void setProjection(const float* p);
void setProjectionOrtho(float left, float right,
float bottom, float top,
float znear, float zfar);
void setProjectionPresp(float fovy, float aspect,
float znear, float zfar);
constexpr static vec4 COLOR_BLACK = {{0.0, 0.0, 0.0, 1.0}};
constexpr static vec4 COLOR_WHITE = {{1.0, 1.0, 1.0, 1.0}};
constexpr static vec4 COLOR_RED = {{1.0, 0.0, 0.0, 1.0}};
constexpr static vec4 COLOR_GREEN = {{0.0, 1.0, 0.0, 1.0}};
constexpr static vec4 COLOR_BLUE = {{0.0, 0.0, 1.0, 1.0}};
constexpr static vec4 COLOR_YELLOW = {{1.0, 1.0, 0.0, 1.0}};
constexpr static vec4 COLOR_GREY = {{0.5, 0.5, 0.5, 1.0}};
constexpr static vec4 COLOR_NONE = {{1.0, 1.0, 1.0, 0.0}};
constexpr static mat4 identity = {{1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0}};
mat4 view;
mat4 projection;
private:
GLuint compileShader(const char* source, const GLenum type);
GLuint loadShader(char* frag, char* vert);
void addLatin1Alphabet();
GLuint text_shader = 0;
font_manager_t* font_manager;
Markup default_markup;
};
}