VR-Forces Developer's Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
fond.h
Go to the documentation of this file.
1 
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 
16 struct stbtt_packedchar;
17 struct stbtt_fontinfo;
18 struct 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
25 enum FondError {
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 {
79  fond_font();
80  void cleanup();
81  void cleanup_fontdata();
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.
88  int m_index;
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*.
139  void free_memory();
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 )
275  , m_error_code( FOND_NO_ERROR )
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.
286  void free_buffer();
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
295  int load_buffer();
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.
346 DT_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.
351 DT_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.
362 DT_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);
371 int fond_check_glerror();
372 int fond_check_shader( GLuint shader );
373 int fond_check_program( GLuint program );
374 
376 {
379 };
380 
381 typedef std::map<std::string,FondDataWrapper*> FontDataMap;
Definition: fond.h:36
FondError m_error_code
Definition: fond.h:122
DT_DLL_VRVGRAPHICSUTILS const char * fond_error_string(FondError error)
FondError m_error_code
Definition: fond.h:314
int fond_check_glerror()
unsigned int m_height
Definition: fond.h:106
fond_font * font
Definition: fond.h:298
unsigned int myVao
Definition: fond.h:311
int m_converted_codepoints
Definition: fond.h:119
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)
Definition: fond.h:263
Dll export defines.
int fond_check_program(GLuint program)
unsigned int myVbo
Definition: fond.h:312
Definition: fond.h:43
unsigned int m_atlas
Definition: fond.h:114
Definition: fond.h:32
unsigned int myProgram
Definition: fond.h:309
const char * m_file
Definition: fond.h:85
void set_error(FondError code)
Definition: fond.h:82
unsigned int myWidth
Definition: fond.h:305
voidpf void uLong size
Definition: ioapi.h:39
stbtt_fontinfo * m_fontinfo
Definition: fond.h:118
int m_allocated_atlas
Definition: fond.h:120
Definition: fond.h:322
unsigned int GLuint
Definition: DtGlTextureBufferObject.h:18
FondError
Definition: fond.h:25
DT_DLL_VRVGRAPHICSUTILS FontDataMap the_font_data
unsigned int myEbo
Definition: fond.h:313
stbtt_packedchar * m_chardata
Definition: fond.h:117
unsigned int myFrameBuffer
Definition: fond.h:310
fond_buffer m_buffer
Definition: fond.h:378
float gap
Definition: fond.h:337
Definition: fond.h:375
unsigned int m_oversample
Definition: fond.h:112
Definition: fond.h:27
fond_font m_font
Definition: fond.h:377
void set_error(FondError error)
Definition: fond.h:281
int fond_check_shader(GLuint shader)
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)
Definition: fond.h:40
unsigned int m_width
Definition: fond.h:104
Definition: fond.h:77
const char * m_characters
Definition: fond.h:96
int32_t * m_codepoints
Definition: fond.h:102
Definition: fond.h:30
float r
Definition: fond.h:329
float l
Definition: fond.h:326
std::map< std::string, FondDataWrapper * > FontDataMap
Definition: fond.h:381
Definition: asyncJobServer.h:39
Definition: stb_truetype.h:568
float t
Definition: fond.h:332
float m_vsize
Definition: fond.h:92
Definition: fond.h:51
unsigned char * m_fontdata
Definition: fond.h:116
DT_DLL_DEBUGDRAWRENDERER void init(makVrv::DtDe &de)
Work function for the plugin initialization.
Definition: stb_truetype.h:699
unsigned int myTexture2dID
Definition: fond.h:302
unsigned int myHeight
Definition: fond.h:307
#define DT_DLL_VRVGRAPHICSUTILS
Definition: vrvGraphicsUtils.h:18
Definition: fond.h:45
int m_index
Definition: fond.h:88
unsigned int myTexture3dID
Definition: fond.h:303
Definition: fond.h:55
DT_DLL_VRVGRAPHICSUTILS int fond_decode_utf8(void *string, int32_t **decoded, size_t *size, FondError &error)
fond_buffer()
Definition: fond.h:265
float b
Definition: fond.h:335
Definition: fond.h:48

Document ID: Generated on Thu Oct 23 22:29:17 EDT 2025 from SVN revision 280951
Copyright © 2005-2024 MAK Technologies. All Rights Reserved (www.mak.com)