-
Notifications
You must be signed in to change notification settings - Fork 1
/
font.h
245 lines (203 loc) · 5.03 KB
/
font.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
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
#ifndef font_h
#define font_h
#include "mymath/mymath.h"
#include "GL/glew.h"
#include <map>
#include <list>
#include <string>
#include <vector>
/*
* Based on Shikoba
*/
struct glyph;
class font;
class font_inst;
#define FONT_LIB_VBO_SIZE 8
struct fontscalebias
{
mm::vec4 vertscalebias;
mm::vec4 texscalebias;
fontscalebias( const mm::vec2& vertscale, const mm::vec2& vertbias, const mm::vec2& texscale, const mm::vec2& texbias ) :
vertscalebias( mm::vec4( vertscale, vertbias ) ), texscalebias( mm::vec4( texscale, texbias ) ) {}
};
class library
{
friend class font;
friend class face;
friend class font_inst;
private:
void* the_library;
mm::uvec2 texture_pen;
GLint texture_row_h;
GLuint tex; //font texture
GLuint texsampler_point, texsampler_linear;
mm::uvec2 texsize;
GLuint vao; //vao
GLuint vbos[FONT_LIB_VBO_SIZE]; //vbos
std::vector<fontscalebias> font_data;
GLuint the_shader; //shader program
bool is_set_up;
std::vector<font_inst*> instances;
void delete_glyphs();
void* get_library()
{
return the_library;
}
GLuint& get_shader()
{
return the_shader; //load shader externally
}
mm::uvec2 get_texsize()
{
return texsize;
}
mm::uvec2& get_texture_pen()
{
return texture_pen;
}
GLint& get_tex_row_h()
{
return texture_row_h;
}
GLuint get_tex()
{
return tex;
}
size_t get_font_data_size()
{
return font_data.size();
}
fontscalebias& get_font_data( size_t i )
{
return font_data[i];
}
void set_up();
void destroy();
void bind_shader()
{
glUseProgram( the_shader );
}
void bind_texture()
{
glActiveTexture( GL_TEXTURE0 );
glBindTexture( GL_TEXTURE_RECTANGLE, tex );
glActiveTexture( GL_TEXTURE1 );
glBindTexture( GL_TEXTURE_RECTANGLE, tex );
glBindSampler( 0, texsampler_point );
glBindSampler( 1, texsampler_linear );
}
void bind_vao()
{
glBindVertexArray( vao );
}
template< class t >
void update_scalebiascolor( unsigned int i, const std::vector< t >& tt )
{
glBindBuffer( GL_ARRAY_BUFFER, vbos[i] );
if( tt.size() > 0 )
glBufferData( GL_ARRAY_BUFFER, sizeof( t ) * tt.size(), &tt[0], GL_DYNAMIC_DRAW );
}
bool expand_tex();
void add_font_data( const fontscalebias& fd )
{
font_data.push_back( fd );
}
protected:
library(); //singleton
library( const library& );
library( library && );
library& operator=( const library& );
~library();
public:
static library& get()
{
static library instance;
return instance;
}
};
#ifdef _WIN32
typedef unsigned int uint32_t;
#endif
//this corresponds to a font file '*.ttf'
//meaning if you'd like to switch to another font-type
//you have to switch font instances
//you can switch between sizes though
class font_inst
{
private:
protected:
public:
class face
{
friend class font;
friend class library;
private:
unsigned int size;
float asc;
float desc;
float h;
float gap;
float upos;
float uthick;
void* the_face; //FT_Face
std::map< unsigned int, std::map<uint32_t, glyph> >* glyphs;
void set_size( unsigned int val );
bool load_glyph( uint32_t val );
unsigned int get_size()
{
return size;
}
glyph& get_glyph( uint32_t i );
bool has_glyph( uint32_t i );
float advance( const uint32_t current );
float kerning( const uint32_t prev, const uint32_t next = 0 );
float height();
float linegap();
float ascender();
float descender();
float underline_position();
float underline_thickness();
protected:
public:
face();
face( const std::string& filename, unsigned int index = 0 );
~face();
}* the_face;
font_inst() : the_face( 0 ) {}
~font_inst()
{
delete the_face;
}
};
class font
{
private:
mm::uvec2 screensize;
mm::frame<float> font_frame;
void add_glyph( font_inst& f, uint32_t c, int counter = 0 );
protected:
font() {} //singleton
font( const font& );
font( font && );
font& operator=( const font& );
public:
void load_font( const std::string& filename, font_inst& font_ptr, unsigned int size );
mm::vec2 add_to_render_list( const std::wstring& text, font_inst& font_ptr, const mm::vec4& color = mm::vec4( 1 ), const mm::mat4& mat = mm::mat4::identity, const mm::vec4& highlight_color = mm::vec4( 1 ), float line_height = 1, float filter = 0 );
void render();
void set_size( font_inst& f, unsigned int s );
void resize( const mm::uvec2& ss );
void destroy()
{
library::get().destroy();
}
GLuint& get_shader()
{
return library::get().get_shader();
}
static font& get()
{
static font instance;
return instance;
}
};
#endif