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