VR-Vantage 2.7 API Documentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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 
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.
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, 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
262 {
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  {
275  ;
276  }
277 
278  void cleanup();
279  void set_error(FondError error) { m_error_code = error; }
280 
281  // Free all the data that was allocated
282  // into the struct by fond_load_buffer. This
283  // will /not/ free the font struct.
284  void free_buffer();
285 
286  // Load the buffer struct and allocate the
287  // necessary OpenGL data.
288  // The following fields must be set in the
289  // struct:
290  // font
291  // width
292  // height
293  int load_buffer();
294 
295  // Pointer to the font that it renders.
297  // The OpenGL texture ID to which this
298  // buffer renders to.
299  // NOT OWNED
300  unsigned int myTexture2dID;
301  unsigned int myTexture3dID;
302  // The width of the texture.
303  unsigned int myWidth;
304  // The height of the texture.
305  unsigned int myHeight;
306  // Internal data.
307  unsigned int myProgram;
308  unsigned int myFrameBuffer;
309  unsigned int myVao;
310  unsigned int myVbo;
311  unsigned int myEbo;
313 };
314 
315 // This struct contains information about
316 // the extents of a text.
317 //
318 // See fond_compute_extent
319 // See fond_compute_extent_u
320 struct fond_extent {
321  // How far to the left the text extends
322  // from zero.
323  float l;
324  // How far to the right the text extends
325  // from zero.
326  float r;
327  // How far up the text extends from its
328  // baseline.
329  float t;
330  // How far down the text extends from
331  // its baseline.
332  float b;
333  // The gap between lines of the text.
334  float gap;
335 };
336 
337 // Render the given text to the buffer's
338 // texture. The text will be rendered at the
339 // given offset, with x and y being in pixels.
340 // color can be either 0 for white text, or
341 // an array of four floats, representing RGBA
342 // of the text's colour in that order.
343 DT_DLL_VRVGRAPHICSUTILS int fond_render(fond_buffer *buffer, const char *text, float x, float y, float *color, int layer_index);
344 
345 // Same as fond_render, but taking an UTF32
346 // encoded string of codepoints and its size.
347 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);
348 
349 // Decode the given UTF8 string into an UTF32
350 // string. The resulting string is put into
351 // decoded, and its size is put into size.
352 // This is used by the non _u functions to
353 // decode the string. You may want to use this
354 // internally, if you need to re-use the same
355 // string often and don't wnat to pay the
356 // conversion cost.
357 DT_DLL_VRVGRAPHICSUTILS int fond_decode_utf8(void *string, int32_t **decoded, size_t *size, FondError & error);
358 
359 
360 // Return a string for a human-readable error
361 // message of the given error code.
363 
364 // internal functions
365 //void fond_err(fond_font* font, FondError code);
366 int fond_check_glerror();
369 
371 {
374 };
375 
376 typedef std::map<std::string, FondDataWrapper*> FontDataMap;


Copyright © 2005-2021 MAK Technologies. All Rights Reserved (www.mak.com)