-
Notifications
You must be signed in to change notification settings - Fork 4
/
freetype-gl-cpp.cpp
383 lines (318 loc) · 12.2 KB
/
freetype-gl-cpp.cpp
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
#include "freetype-gl-cpp.h"
#include <cstring>
#include <cassert>
#include <vector>
#include <iostream>
#include <stdexcept>
#ifdef WITH_FONTCONFIG
#include <fontconfig/fontconfig.h>
#endif
namespace ftgl {
constexpr vec4 FreetypeGl::COLOR_BLACK;
constexpr vec4 FreetypeGl::COLOR_WHITE;
constexpr vec4 FreetypeGl::COLOR_RED;
constexpr vec4 FreetypeGl::COLOR_GREEN;
constexpr vec4 FreetypeGl::COLOR_BLUE;
constexpr vec4 FreetypeGl::COLOR_YELLOW;
constexpr vec4 FreetypeGl::COLOR_GREY;
constexpr vec4 FreetypeGl::COLOR_NONE;
constexpr mat4 FreetypeGl::identity;
Markup::Markup(){
description.font = NULL;
description.family = NULL;
manager = NULL;
}
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){
manager = freetype_gl;
description.family = strdup((char*)FreetypeGl::findFont(font_family).c_str());
description.size = size;
description.bold = bold;
description.italic = italic;
description.spacing= 0.0;
description.gamma = 2.;
description.foreground_color = color;
description.background_color = FreetypeGl::COLOR_NONE;
description.outline = 0;
description.outline_color = FreetypeGl::COLOR_NONE;
description.underline = underlined;
description.underline_color = color;
description.overline = overline;
description.overline_color = color;
description.strikethrough = strikethrough;
description.strikethrough_color= color;
description.font = font_manager_get_from_markup(manager->font_manager, &description);
}
Markup::Markup(Markup &&other){
description = other.description;
other.description.font = NULL;
other.description.family = NULL;
other.manager = NULL;
}
Markup& Markup::operator =(Markup &&other){
description = other.description;
other.description.font = NULL;
other.description.family = NULL;
other.manager = NULL;
}
Markup::~Markup(){
if(description.family != NULL)
free(description.family);
//if(manager != NULL && description.font != NULL)
// font_manager_delete_font(manager->font_manager, description.font);
//FIXME why is this crashing?
//FIXME Remove glyphs as well!
}
FreetypeGlText::FreetypeGlText(FreetypeGlText&& other){
manager = other.manager;
text_buffer = other.text_buffer;
pose = other.pose;
scaling_factor = other.scaling_factor;
other.manager = NULL;
other.text_buffer = NULL;
}
FreetypeGlText::~FreetypeGlText(){
if(text_buffer)
text_buffer_delete(text_buffer);
}
void FreetypeGlText::render(){
manager->renderText(*this);
}
#ifdef WITH_EIGEN
void FreetypeGlText::setPose(const Eigen::Matrix4f &pose){
eigen2mat4(pose, &this->pose);
}
void FreetypeGlText::setPosition(const Eigen::Vector3f &position){
setPosition(position(0),position(1),position(2));
}
#endif
mat4 FreetypeGlText::getModelMatrix() const{
mat4 m = pose;
m.m00 *= scaling_factor;
m.m01 *= scaling_factor;
m.m02 *= scaling_factor;
m.m10 *= scaling_factor;
m.m11 *= scaling_factor;
m.m12 *= scaling_factor;
m.m20 *= scaling_factor;
m.m21 *= scaling_factor;
m.m22 *= scaling_factor;
return m;
}
FreetypeGl::FreetypeGl(bool initialise){
if(initialise) init();
}
FreetypeGl::~FreetypeGl(){
glDeleteTextures(1, &font_manager->atlas->id);
font_manager_delete(font_manager);
glDeleteProgram(text_shader);
}
void FreetypeGl::init(){
assert(!text_shader && "FreetypeGl was already initialised");
font_manager = font_manager_new(1024, 1024, LCD_FILTERING_ON);
#ifdef WITH_FONTCONFIG
default_markup = std::move(createMarkup(findFont("DejaVu Sans"), 32));
#else
default_markup = createMarkup("/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf", 32);
#endif
text_shader = loadShader(shader_text_frag, shader_text_vert);
addLatin1Alphabet();
updateTexture();
mat4_set_identity(&view);
mat4_set_orthographic(&projection, -500, 500, -500, 500, -std::numeric_limits<float>::min(), std::numeric_limits<float>::max()); // If user forgets to provide matrix, this should be easier to debug than identity (it is more likely that something is visible)
}
Markup FreetypeGl::createMarkup(const std::string& font_family,
float size,
const vec4 &color,
bool bold,
bool underlined,
bool italic,
bool strikethrough,
bool overline) {
return Markup(font_family, size, color, bold, underlined, italic, strikethrough, overline, this);
}
std::string FreetypeGl::findFont(const std::string &search_pattern) {
// If the search pattern is a path already, return this path
#if (defined(_WIN32) || defined(_WIN64))
if(search_pattern.find('\\') != std::string::npos) return search_pattern;
#else
if(search_pattern.find('/') != std::string::npos) return search_pattern;
#endif
// Otherwise (if the pattern is not a path), use fontconfig to search for font based on pattern
#ifndef WITH_FONTCONFIG
throw std::runtime_error("Finding font " + search_pattern + " failed: Pattern was not a path and fontconfig could not be used to find font.");
#else
auto throwError = [&search_pattern]() {
throw std::runtime_error("FreetypeGL error -- could not find font: " + search_pattern);
};
std::string filename;
FcInit();
FcPattern *pattern = FcNameParse((const FcChar8*)search_pattern.c_str());
FcConfigSubstitute( 0, pattern, FcMatchPattern );
FcDefaultSubstitute( pattern );
FcResult font_result;
FcPattern *match = FcFontMatch(0, pattern, &font_result);
FcPatternDestroy(pattern);
if (!match) throwError();
FcValue value;
FcResult result = FcPatternGet( match, FC_FILE, 0, &value );
if (result) throwError();
filename = (char *)(value.u.s);
FcPatternDestroy( match );
return filename;
#endif
}
FreetypeGlText FreetypeGl::createText(const std::string& text, const markup_t* markup){
assert(text_shader && "FreetypeGl needs to be initialised first");
if(markup == NULL) return FreetypeGlText(this, &this->default_markup.description, text.c_str(), NULL);
return FreetypeGlText(this, markup, text.c_str(), NULL);
}
FreetypeGlText FreetypeGl::createText(const std::string &text, const Markup& markup){
return createText(text, &markup.description);
}
void FreetypeGl::FreetypeGl::updateTexture(){
assert(text_shader && "FreetypeGl needs to be initialised first");
glDeleteTextures(1, &font_manager->atlas->id);
glGenTextures(1, &font_manager->atlas->id);
glBindTexture( GL_TEXTURE_2D, font_manager->atlas->id );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
glTexImage2D( GL_TEXTURE_2D, 0, GL_RGB, font_manager->atlas->width,
font_manager->atlas->height, 0, GL_RGB, GL_UNSIGNED_BYTE,
font_manager->atlas->data );
}
void FreetypeGl::preRender() const{
glColor4f(1.00,1.00,1.00,1.00);
glUseProgram(text_shader);
glUniformMatrix4fv( glGetUniformLocation( text_shader, "view" ), 1, 0, view.data);
glUniformMatrix4fv( glGetUniformLocation( text_shader, "projection" ), 1, 0, projection.data);
glUniform1i( glGetUniformLocation( text_shader, "tex" ), 0 );
glUniform3f( glGetUniformLocation( text_shader, "pixel" ),
1.0f/font_manager->atlas->width,
1.0f/font_manager->atlas->height,
(float)font_manager->atlas->depth );
glEnable( GL_BLEND );
glActiveTexture( GL_TEXTURE0 );
glBindTexture( GL_TEXTURE_2D, font_manager->atlas->id );
glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
glBlendColor( 1, 1, 1, 1 );
}
void FreetypeGl::postRender() const{
glBindTexture( GL_TEXTURE_2D, 0 );
glDisable( GL_BLEND );
glBlendColor( 0, 0, 0, 0 );
glUseProgram( 0 );
}
void FreetypeGl::renderText(const std::string &text){
assert(text_shader && "FreetypeGl needs to be initialised first");
text_buffer_t* buffer = text_buffer_new( );
vec2 pen = {{0,0}};
text_buffer_printf(buffer, &pen, &default_markup.description, text.c_str(), NULL);
//updateTexture();
preRender();
glUniformMatrix4fv( glGetUniformLocation( text_shader, "model" ),1, 0, identity.data);
vertex_buffer_render( buffer->buffer, GL_TRIANGLES );
postRender();
text_buffer_delete( buffer );
}
void FreetypeGl::renderText(const FreetypeGlText& text, bool call_pre_post) const {
assert(text_shader && "FreetypeGl needs to be initialised first");
if(call_pre_post) preRender();
glUniformMatrix4fv( glGetUniformLocation( text_shader, "model" ), 1, 0, text.getModelMatrix().data);
vertex_buffer_render( text.getTextBuffer()->buffer, GL_TRIANGLES );
if(call_pre_post) postRender();
}
GLuint FreetypeGl::compileShader(const char* source, const GLenum type){
GLint gl_compile_status;
GLuint handle = glCreateShader(type);
glShaderSource(handle, 1, &source, 0);
glCompileShader(handle);
glGetShaderiv( handle, GL_COMPILE_STATUS, &gl_compile_status);
if(gl_compile_status == GL_FALSE){
GLint log_length = 0;
glGetShaderiv(handle, GL_INFO_LOG_LENGTH, &log_length);
std::vector<GLchar> log(log_length);
glGetShaderInfoLog(handle, log_length, &log_length, &log[0]);
glDeleteShader(handle);
throw std::runtime_error((char*)&log[0]);
}
return handle;
}
GLuint FreetypeGl::loadShader(char *frag, char *vert){
GLuint handle = glCreateProgram( );
GLint link_status;
if(strlen(vert)){
GLuint vert_shader = compileShader(vert, GL_VERTEX_SHADER);
glAttachShader(handle, vert_shader);
glDeleteShader(vert_shader);
}
if(strlen(frag)){
GLuint frag_shader = compileShader(frag, GL_FRAGMENT_SHADER);
glAttachShader(handle, frag_shader);
glDeleteShader(frag_shader);
}
glLinkProgram( handle );
glGetProgramiv( handle, GL_LINK_STATUS, &link_status );
if (link_status == GL_FALSE){
GLint log_length = 0;
glGetProgramiv(handle, GL_INFO_LOG_LENGTH, &log_length);
std::vector<GLchar> log(log_length);
glGetProgramInfoLog(handle, log_length, &log_length, &log[0]);
glDeleteProgram(handle);
throw std::runtime_error((char*)&log[0]);
}
return handle;
}
void FreetypeGl::addLatin1Alphabet(){
const char a[] = "qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM";
for(int i=0; i<strlen(a); i++)
texture_font_load_glyph(default_markup.description.font, a+i);
}
void FreetypeGl::setView(const ftgl::mat4& v){
view = v;
}
void FreetypeGl::setProjection(const ftgl::mat4& p){
projection = p;
}
void FreetypeGl::setView(const float* v){
std::memcpy(&view, v, 16 * sizeof(float));
}
void FreetypeGl::setProjection(const float* p){
std::memcpy(&projection, p, 16 * sizeof(float));
}
void FreetypeGl::setProjectionOrtho(float left, float right,
float bottom, float top,
float znear, float zfar){
mat4_set_orthographic(&projection, left, right, bottom, top, znear, zfar);
}
void FreetypeGl::setProjectionPresp(float fovy, float aspect,
float znear, float zfar){
mat4_set_perspective(&projection, fovy, aspect, znear, zfar);
}
#ifdef WITH_EIGEN
void FreetypeGl::setView(const Eigen::Matrix4f& v){
eigen2mat4(v, &view);
}
void FreetypeGl::setProjection(const Eigen::Matrix4f& p){
eigen2mat4(p, &projection);
}
void eigen2mat4(const Eigen::Matrix4f& src, mat4 *dst){
assert(src.innerStride() == 1 && src.outerStride() == 4);
if(src.IsRowMajor){
Eigen::Matrix4f tmp = src.transpose();
std::memcpy(dst, tmp.data(), 16 * sizeof(float));
} else {
std::memcpy(dst, src.data(), 16 * sizeof(float));
}
}
#endif
}