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 
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.
27  // The font file could not be read. Most likely
28  // it doesn't exist or the permission is denied.
30  // An allocation failed, probably due to OOM.
32  // Failed to pack the font into an atlas.
33  // This most likely means that the atlas' size
34  // is too small.
36  // Failed to parse the font file's information.
37  // It's likely that the file you gave was not
38  // a TTF.
40  // OpenGL signalled an error. Check stderr for
41  // more information.
43  // The maximum size on fond_load_fit was reached.
45  // A render or compute function was called when
46  // the font was not properly loaded yet.
48  // The UTF8 string could not be converted as it
49  // is malformatted.
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.
55  // The fond_font struct did contain neither a
56  // list of characters nor a list of 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
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;
120 
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,
236  int* width = NULL, int* height = NULL );
237 
238  // Compute the extent of the given text.
239  // You must allocate the extent struct yourself
240  // and make sure it is zeroed out.
241  int compute_extent(char *text, fond_extent *extent);
242 
243  // Same as fond_compute_extent, but taking an
244  // UTF32 encoded string of codepoints and its
245  // size.
246  int compute_extent_utf(int32_t *text, size_t size, fond_extent *extent);
247 
248  int load_file(const char *file, unsigned char **content);
249 
250 };
251 
252 // This struct allows for convenience in
253 // rendering, as it will render text for you
254 // into a texture, which you can then render
255 // like any other. Thus you won't need to
256 // handle the actual rendering logic yourself.
257 //
258 // See fond_free_buffer
259 // See fond_load_buffer
260 // See fond_render
261 // See fond_render_u
263 {
265  : myTexture2dID( 0 )
266  , myTexture3dID( 0 )
267  , myWidth( 0 )
268  , myHeight( 0 )
269  , myProgram( 0 )
270  , myFrameBuffer( 0 )
271  , myVao( 0 )
272  , myVbo( 0 )
273  , myEbo( 0 )
274  , m_error_code( FOND_NO_ERROR )
275  , font( nullptr )
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.
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;
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
322 {
323  // How far to the left the text extends
324  // from zero.
325  float l;
326  // How far to the right the text extends
327  // from zero.
328  float r;
329  // How far up the text extends from its
330  // baseline.
331  float t;
332  // How far down the text extends from
333  // its baseline.
334  float b;
335  // The gap between lines of the text.
336  float gap;
337 };
338 
339 // Render the given text to the buffer's
340 // texture. The text will be rendered at the
341 // given offset, with x and y being in pixels.
342 // color can be either 0 for white text, or
343 // an array of four floats, representing RGBA
344 // of the text's colour in that order.
345 DT_DLL_VRVGRAPHICSUTILS int fond_render( fond_buffer* buffer, const char* text, float x, float y,
346  float* color, int layer_index, float aspect_ratio = 1.0f );
347 
348 // Same as fond_render, but taking an UTF32
349 // encoded string of codepoints and its size.
350 DT_DLL_VRVGRAPHICSUTILS int fond_render_utf( fond_buffer* buffer, int32_t* text, size_t num_chars,
351  float x, float y, float* color, int layer_index, float aspect_ratio = 1.0f );
352 
353 // Decode the given UTF8 string into an UTF32
354 // string. The resulting string is put into
355 // decoded, and its size is put into size.
356 // This is used by the non _u functions to
357 // decode the string. You may want to use this
358 // internally, if you need to re-use the same
359 // string often and don't want to pay the
360 // conversion cost.
361 DT_DLL_VRVGRAPHICSUTILS int fond_decode_utf8( void* string, int32_t** decoded, size_t* size, FondError& error );
362 
363 
364 // Return a string for a human-readable error
365 // message of the given error code.
367 
368 // internal functions
369 //void fond_err(fond_font* font, FondError code);
370 int fond_check_glerror();
371 int fond_check_shader( GLuint shader );
372 int fond_check_program( GLuint program );
373 
375 {
378 };
379 
380 typedef std::map<std::string,FondDataWrapper*> FontDataMap;
Definition: fond.h:35
FondError m_error_code
Definition: fond.h:121
DT_DLL_VRVGRAPHICSUTILS const char * fond_error_string(FondError error)
FondError m_error_code
Definition: fond.h:313
int fond_check_glerror()
unsigned int m_height
Definition: fond.h:105
fond_font * font
Definition: fond.h:297
unsigned int myVao
Definition: fond.h:310
int m_converted_codepoints
Definition: fond.h:118
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:262
Dll export defines.
int fond_check_program(GLuint program)
unsigned int myVbo
Definition: fond.h:311
Definition: fond.h:42
unsigned int m_atlas
Definition: fond.h:113
Definition: fond.h:31
unsigned int myProgram
Definition: fond.h:308
const char * m_file
Definition: fond.h:84
void set_error(FondError code)
Definition: fond.h:81
unsigned int myWidth
Definition: fond.h:304
voidpf void uLong size
Definition: ioapi.h:39
stbtt_fontinfo * m_fontinfo
Definition: fond.h:117
int m_allocated_atlas
Definition: fond.h:119
Definition: fond.h:321
unsigned int GLuint
Definition: DtGlTextureBufferObject.h:18
FondError
Definition: fond.h:24
DT_DLL_VRVGRAPHICSUTILS FontDataMap the_font_data
unsigned int myEbo
Definition: fond.h:312
stbtt_packedchar * m_chardata
Definition: fond.h:116
unsigned int myFrameBuffer
Definition: fond.h:309
fond_buffer m_buffer
Definition: fond.h:377
float gap
Definition: fond.h:336
Definition: fond.h:374
unsigned int m_oversample
Definition: fond.h:111
Definition: fond.h:26
fond_font m_font
Definition: fond.h:376
void set_error(FondError error)
Definition: fond.h:280
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:39
unsigned int m_width
Definition: fond.h:103
Definition: fond.h:76
const char * m_characters
Definition: fond.h:95
int32_t * m_codepoints
Definition: fond.h:101
Definition: fond.h:29
float r
Definition: fond.h:328
float l
Definition: fond.h:325
std::map< std::string, FondDataWrapper * > FontDataMap
Definition: fond.h:380
Definition: asyncJobServer.h:39
Definition: stb_truetype.h:568
float t
Definition: fond.h:331
float m_vsize
Definition: fond.h:91
Definition: fond.h:50
unsigned char * m_fontdata
Definition: fond.h:115
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:301
unsigned int myHeight
Definition: fond.h:306
#define DT_DLL_VRVGRAPHICSUTILS
Definition: vrvGraphicsUtils.h:18
Definition: fond.h:44
int m_index
Definition: fond.h:87
unsigned int myTexture3dID
Definition: fond.h:302
Definition: fond.h:54
DT_DLL_VRVGRAPHICSUTILS int fond_decode_utf8(void *string, int32_t **decoded, size_t *size, FondError &error)
fond_buffer()
Definition: fond.h:264
float b
Definition: fond.h:334
Definition: fond.h:47

Document ID: Generated on Tue Sep 24 19:28:17 EDT 2024 from SVN revision 269799
Copyright © 2005-2024 MAK Technologies. All Rights Reserved (www.mak.com)