summaryrefslogtreecommitdiff
path: root/ftgl.h (plain)
blob: 1a6a0baf4be730071c84a918440440cf1a0fa0e1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107

#ifndef _FTGL_H
#define _FTGL_H

#include <ft2build.h>
#include FT_FREETYPE_H

struct _FTGL_Glyph;

typedef struct _FTGL_Font {
  unsigned int texture;         // OpenGL texture ID
  int texture_size;             // current texture size
  long height;                  // font height (distance between baselines)
  long ascent;                  // max dist. between baseline and glyph top
  long descent;                 // max dist. between baseline and glyph bottom
  FT_Face face;                 // the Freetype face object
  int use_kerning;              // whether the font uses kerning
  
  // private part starts here
  struct _FTGL_Glyph *glyph;    // rendered glyph data
  int num_glyphs;               // n.o. rendered glyphs
  int num_allocated_glyphs;     // size of the glyph array
  
  int pen_x, pen_y;             // glyph renderer state (current pen position)
  unsigned char* texture_data;  // glyph renderer state (font texture image)
} FTGL_Font;

/**
 * creates a new FTGL_Font object.
 *
 * FTGL_Font keeps an OpenGL texture that contains glyphs from a font face,
 * and can use this texture to render a string in OpenGL.
 *
 * 'face' is the Freetype face which will be rendered. It should already have
 * a size (use something like FT_Set_Char_Size(face, 0, size*64, 0, 96)).
 *
 * 'initial_texture_size' is the initial size of the texture. (It must be a
 * power of two.) If the texture isn't big enough for all required glyphs,
 * it'll automatically grow. (This is an expensive operation, and textures can
 * be used more effectively when already created with the correct size.)
 * You can query the current texture size with font->texture_size.
 *
 * Kerning is used if 'use_kerning' is true and the face supports it.
 *
 * The only things FTGL does with textures is glBindTexture and glTexImage2D.
 * Creating and deleting textures must be done by you.
 *
 * As a side effect, this function changes current bound texture to
 * 'texture_id'.
 *
 * This function renders all ASCII characters. If the printed strings contain
 * Unicode, you must FTGL_Prepare each string to render them.
 *
 * This function returns NULL in case of errors.
 */
FTGL_Font* FTGL_Create(FT_Face face, int initial_texture_size, unsigned int texture_id, int use_kerning);

/**
 * destroys a FTGL_Font object.
 *
 * This doesn't destroy the OpenGL texture nor the FT_Face object.
 */
void FTGL_Destroy(FTGL_Font* font);

/**
 * prints a string.
 *
 * It prints the string using OpenGL at the current position (0,0,0).
 *
 * This doesn't use any Freetype functions. All glyphs in the string must
 * already be rendered and inside the font->glyph array. Use FTGL_Prepare to
 * make sure all characters in the string are already rendered.
 *
 * 'string' should be in UTF-8. Non-ASCII characters will be converted to UCS
 * automatically.
 *
 * If 'underline' is 0, the string isn't underlined. If 'underline' is 1, the
 * whole string is underlined. If 'underline' is an ASCII code (other than 0
 * and 1), it's a special character that, when found in 'string', underlines
 * the following glyph.
 *
 * As a side effect, this function changes current bound texture to the font
 * texture, and translates the current GL position to the end of the string.
 *
 * This function might not work correctly if the following is not enabled:
 * glEnable(GL_TEXTURE_2D); glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA,
 * GL_ONE_MINUS_SRC_ALPHA);
 */
void FTGL_Print(FTGL_Font* font, const char* string, int underline);

/**
 * prepares a string for printing.
 *
 * If the string contains Unicode glyphs that weren't rendered yet, this
 * renders them and stores them in the font texture (expanding it if it isn't
 * big enough).
 *
 * Every string should be FTGL_Prepare'd at least once before printing. It
 * isn't needed to call FTGL_Prepare every frame, however.
 *
 * On success, FTGL_Prepare returns the string's width. (Thus, it can be used
 * to measure string sizes before printing them.) If it returns -1, a memory
 * error occured and the font is invalid (it can't be used anymore).
 */
long FTGL_Prepare(FTGL_Font* font, const char* string);

#endif