VR-Vantage 2.8 API Documentation
Home
Modules
Namespaces
Classes
Files
Libraries
Examples
File List
File Members
All
Classes
Namespaces
Files
Functions
Variables
Typedefs
Enumerations
Enumerator
Friends
Macros
Groups
Pages
include
vrvGraphicsUtils
fond.h
Go to the documentation of this file.
1
2
3
#pragma once
4
5
#include <
vrvGraphicsUtils/vrvGraphicsUtils.h
>
6
7
8
#include <
stdint.h
>
9
#include <stdlib.h>
10
11
#include <map>
12
13
#include <GL/glew.h>
14
15
struct
stbtt_packedchar
;
16
struct
stbtt_fontinfo
;
17
struct
fond_extent
;
18
19
// This enum contains all possible error codes
20
// that libfond can produce.
21
//
22
// See fond_error
23
// See fond_error_string
24
enum
FondError
{
25
// Everything is OK.
26
FOND_NO_ERROR
,
27
// The font file could not be read. Most likely
28
// it doesn't exist or the permission is denied.
29
FOND_FILE_LOAD_FAILED
,
30
// An allocation failed, probably due to OOM.
31
FOND_OUT_OF_MEMORY
,
32
// Failed to pack the font into an atlas.
33
// This most likely means that the atlas' size
34
// is too small.
35
FOND_FONT_PACK_FAILED
,
36
// Failed to parse the font file's information.
37
// It's likely that the file you gave was not
38
// a TTF.
39
FOND_FONT_INIT_FAILED
,
40
// OpenGL signalled an error. Check stderr for
41
// more information.
42
FOND_OPENGL_ERROR
,
43
// The maximum size on fond_load_fit was reached.
44
FOND_SIZE_EXCEEDED
,
45
// A render or compute function was called when
46
// the font was not properly loaded yet.
47
FOND_NOT_LOADED
,
48
// The UTF8 string could not be converted as it
49
// is malformatted.
50
FOND_UTF8_CONVERSION_ERROR
,
51
// An attempt was made to render/compute a glyph
52
// that was not included in the list of glyphs
53
// when the font was loaded.
54
FOND_UNLOADED_GLYPH
,
55
// The fond_font struct did contain neither a
56
// list of characters nor a list of codepoints.
57
FOND_NO_CHARACTERS_OR_CODEPOINTS
58
};
59
60
// This is the primary struct that contains all
61
// relevant information about a font. You must
62
// allocate this struct yourself and make sure
63
// that it is zeroed out before you do anything
64
// with it.
65
// Either stack allocate it with ={0}, or use
66
// calloc. Not zeroing out will land you in a
67
// world of pain.
68
//
69
// See fond_free
70
// See fond_load
71
// See fond_load_fit
72
// See fond_compute
73
// See fond_compute_u
74
// See fond_compute_extent
75
// See fond_compute_extent_u
76
struct
DT_DLL_VRVGRAPHICSUTILS
fond_font
77
{
78
fond_font
();
79
void
cleanup();
80
void
cleanup_fontdata();
81
void
set_error
(
FondError
code) { m_error_code = code; }
82
83
// Path to the TTF file.
84
const
char
*
m_file
;
85
// The index of the font within the TTF file.
86
// You probably don't need to set this.
87
int
m_index
;
88
// The vertical font size in pixels. If you
89
// render it above this resolution, you'll
90
// get a blurry mess.
91
float
m_vsize
;
92
// A UTF8 encoded string of characters that
93
// this font instance will be able to render.
94
// Must be null-terminated.
95
const
char
*
m_characters
;
96
// An array of Unicode codepoints that this
97
// font instance will be able to render. Must
98
// be null-terminated. This is automatically
99
// filled in for you, if it is NULL and the
100
// characters field is provided instead.
101
int32_t
*
m_codepoints
;
102
// The width of the glyph texture atlas.
103
unsigned
int
m_width
;
104
// The height of the glyph texture atlas.
105
unsigned
int
m_height
;
106
// How much oversampling should be done.
107
// Higher oversampling might improve the
108
// quality of the rendering, but will need
109
// a much bigger atlas size:
110
// width*oversampling,height*oversampling
111
unsigned
int
m_oversample
;
112
// The OpenGL texture ID for the atlas.
113
unsigned
int
m_atlas
;
114
// Internal data.
115
unsigned
char
*
m_fontdata
;
116
stbtt_packedchar
*
m_chardata
;
117
stbtt_fontinfo
*
m_fontinfo
;
118
int
m_converted_codepoints
;
119
int
m_allocated_atlas
;
120
121
FondError
m_error_code
;
122
123
124
// Most functions in this API return an int,
125
// which can be ither 1 or 0, with the former
126
// representing success and the latter
127
// failure. On failure, you should check
128
// fond_error to see what went wrong.
129
130
// Free all the data that was allocated
131
// into the struct by fond_load*. This
132
// will /not/ free the characters array,
133
// the file array, or the codepoints array
134
// if the codepoints array was not computed
135
// by fond_load*. It will also not free the
136
// texture atlas if it was not alloced by
137
// fond_load*.
138
void
free_memory();
139
140
// Load the font struct and allocate the
141
// necessary OpenGL data. The texture atlas
142
// is not allocated if the font's atlas field
143
// is already set. It is however always filled
144
// via glSubImage2D. The bit depth of the pixel
145
// values is only 255, and only the red channel
146
// is filled, so a texture with internal format
147
// of GL_R8 will be created for you if you
148
// don't specify your own.
149
// The following fields must be set in the
150
// struct:
151
// file
152
// size
153
// width
154
// height
155
// characters or codepoints
156
int
init
();
157
158
// Load the font struct, attempting to fit
159
// an atlas automatically. This may not
160
// result in the most compact atlas possible.
161
// max_size is the maximum size of the width
162
// or height that can be reached before it
163
// gives up. The following fields must be set
164
// in the struct:
165
// file
166
// size
167
// characters or codepoints
168
int
load_fit(
unsigned
int
max_size);
169
170
// Compute the Vertex Array Object to render
171
// the given text. Here, n and vao are output
172
// arguments, containing the number of elements
173
// and the OpenGL VAO ID respectively.
174
// The text must be UTF8 encoded and null-
175
// terminated. The VAO packs two arrays, one
176
// at location 0 and one at 1, with both being
177
// vec2s. The first being the vertex coordinates
178
// and the second being the texture coordinates.
179
// The vertex coordinates start at 0 and increase
180
// in x and y as per the font's size. You are
181
// responsible for scaling it as appropriate
182
// for your display.
183
// The texture coordinates are for the font's
184
// atlas texture.
185
// The triangles are defined with their vertices
186
// in counter-clockwise (CCW) order, meaning
187
// that if you apply cull-face, you need to have
188
// front-faces be set to CCW, which should be
189
// the default.
190
// If the text contains a Linefeed character
191
// (U+000A) a new line is started automatically
192
// by resetting X to 0 and decreasing Y by the
193
// necessary height for a new line.
194
//
195
// The VAO and necessary VBOs are allocated
196
// automatically for you and then filled in as
197
// per fond_update.
198
int
compute
(
char
*text,
size_t
*
n
,
unsigned
int
*vao);
199
200
// Same as fond_compute, but taking an UTF32
201
// encoded string of codepoints and its size.
202
int
compute_utf(
int32_t
*text,
size_t
size
,
size_t
*
n
,
unsigned
int
*vao);
203
204
205
int
codepoint_index(
unsigned
int
glyph);
206
207
// Update the given vertex buffer and element
208
// buffers to contain the necessary data to draw
209
// the given text.
210
// The EBO must be bound to a VAO as a
211
// GL_ELEMENT_ARRAY_BUFFER and the VBO as a
212
// GL_ARRAY_BUFFER. The VBO contains GLfloats and
213
// the EBO contains GLuints. The VBO packs two
214
// distinct values as described in fond_compute.
215
// They are stored alternatingly in the buffer,
216
// meaning that you should bind the buffer to a
217
// VAO like this:
218
//
219
// glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(float)*4, (GLvoid*)0);
220
// glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(float)*4, (GLvoid*)(2*sizeof(float)));
221
//
222
// The data is stored in the buffers using
223
// DYNAMIC_DRAW, meaning they should be able to
224
// be updated frequently enough.
225
//
226
// Note that the GL buffer and vertex array
227
// bindings are modified by this function. You
228
// should thus not expect previously bound values
229
// to still be the same after this function
230
// returns.
231
int
update(
char
*text,
size_t
*
n
,
unsigned
int
vbo,
unsigned
int
ebo);
232
233
// Same as fond_update, but taking an UTF32
234
// encoded string of codepoints and its size.
235
int
update_utf(
int32_t
*text,
size_t
num_chars,
size_t
*
n
,
unsigned
int
vbo,
unsigned
int
ebo,
int
*
width
);
236
237
// Compute the extent of the given text.
238
// You must allocate the extent struct yourself
239
// and make sure it is zeroed out.
240
int
compute_extent(
char
*text,
fond_extent
*
extent
);
241
242
// Same as fond_compute_extent, but taking an
243
// UTF32 encoded string of codepoints and its
244
// size.
245
int
compute_extent_utf(
int32_t
*text,
size_t
size
,
fond_extent
*
extent
);
246
247
int
load_file(
const
char
*file,
unsigned
char
**content);
248
249
};
250
251
// This struct allows for convenience in
252
// rendering, as it will render text for you
253
// into a texture, which you can then render
254
// like any other. Thus you won't need to
255
// handle the actual rendering logic yourself.
256
//
257
// See fond_free_buffer
258
// See fond_load_buffer
259
// See fond_render
260
// See fond_render_u
261
struct
DT_DLL_VRVGRAPHICSUTILS
fond_buffer
262
{
263
fond_buffer
() :
264
myTexture2dID(0),
265
myTexture3dID(0),
266
myWidth(0),
267
myHeight(0),
268
myProgram(0),
269
myFrameBuffer(0),
270
myVao(0),
271
myVbo(0),
272
myEbo(0),
273
m_error_code(
FOND_NO_ERROR
),
274
font(nullptr)
275
{
276
;
277
}
278
279
void
cleanup();
280
void
set_error
(
FondError
error) { m_error_code = error; }
281
282
// Free all the data that was allocated
283
// into the struct by fond_load_buffer. This
284
// will /not/ free the font struct.
285
void
free_buffer();
286
287
// Load the buffer struct and allocate the
288
// necessary OpenGL data.
289
// The following fields must be set in the
290
// struct:
291
// font
292
// width
293
// height
294
int
load_buffer();
295
296
// Pointer to the font that it renders.
297
fond_font
*
font
;
298
// The OpenGL texture ID to which this
299
// buffer renders to.
300
// NOT OWNED
301
unsigned
int
myTexture2dID
;
302
unsigned
int
myTexture3dID
;
303
// The width of the texture.
304
unsigned
int
myWidth
;
305
// The height of the texture.
306
unsigned
int
myHeight
;
307
// Internal data.
308
unsigned
int
myProgram
;
309
unsigned
int
myFrameBuffer
;
310
unsigned
int
myVao
;
311
unsigned
int
myVbo
;
312
unsigned
int
myEbo
;
313
FondError
m_error_code
;
314
};
315
316
// This struct contains information about
317
// the extents of a text.
318
//
319
// See fond_compute_extent
320
// See fond_compute_extent_u
321
struct
fond_extent
{
322
// How far to the left the text extends
323
// from zero.
324
float
l
;
325
// How far to the right the text extends
326
// from zero.
327
float
r
;
328
// How far up the text extends from its
329
// baseline.
330
float
t
;
331
// How far down the text extends from
332
// its baseline.
333
float
b
;
334
// The gap between lines of the text.
335
float
gap
;
336
};
337
338
// Render the given text to the buffer's
339
// texture. The text will be rendered at the
340
// given offset, with x and y being in pixels.
341
// color can be either 0 for white text, or
342
// an array of four floats, representing RGBA
343
// of the text's colour in that order.
344
DT_DLL_VRVGRAPHICSUTILS
int
fond_render
(
fond_buffer
*
buffer
,
const
char
*text,
float
x
,
float
y
,
float
*
color
,
int
layer_index);
345
346
// Same as fond_render, but taking an UTF32
347
// encoded string of codepoints and its size.
348
DT_DLL_VRVGRAPHICSUTILS
int
fond_render_utf
(
fond_buffer
*
buffer
,
int32_t
*text,
size_t
num_chars,
float
x
,
float
y
,
float
*
color
,
int
layer_index);
349
350
// Decode the given UTF8 string into an UTF32
351
// string. The resulting string is put into
352
// decoded, and its size is put into size.
353
// This is used by the non _u functions to
354
// decode the string. You may want to use this
355
// internally, if you need to re-use the same
356
// string often and don't wnat to pay the
357
// conversion cost.
358
DT_DLL_VRVGRAPHICSUTILS
int
fond_decode_utf8
(
void
*
string
,
int32_t
**decoded,
size_t
*
size
,
FondError
& error);
359
360
361
// Return a string for a human-readable error
362
// message of the given error code.
363
DT_DLL_VRVGRAPHICSUTILS
const
char
*
fond_error_string
(
FondError
error);
364
365
// internal functions
366
//void fond_err(fond_font* font, FondError code);
367
int
fond_check_glerror
();
368
int
fond_check_shader
(
GLuint
shader
);
369
int
fond_check_program
(
GLuint
program
);
370
371
struct
FondDataWrapper
372
{
373
fond_font
m_font
;
374
fond_buffer
m_buffer
;
375
};
376
377
typedef
std::map<std::string, FondDataWrapper*>
FontDataMap
;
378
DT_DLL_VRVGRAPHICSUTILS
extern
FontDataMap
the_font_data
;
Copyright © 2005-2021 MAK Technologies. All Rights Reserved (
www.mak.com
)