Cosmetics
[llpp.git] / glfont.c
blob3eb3bd0db6f5a0487b0363b75d7c985402c5bf14
1 /* This is a slightly modified
2 https://github.com/ccxvii/snippets/blob/master/glfont.c by Tor Andersson
3 */
4 /*
5 * A very simple font cache and rasterizer that uses freetype
6 * to draw fonts from a single OpenGL texture. The code uses
7 * a linear-probe hashtable, and writes new glyphs into
8 * the texture using glTexSubImage2D. When the texture fills
9 * up, or the hash table gets too crowded, everything is wiped.
11 * This is designed to be used for horizontal text only,
12 * and draws unhinted text with subpixel accurate metrics
13 * and kerning. As such, you should always call the drawing
14 * function with an identity transform that maps units
15 * to pixels accurately.
17 * If you wish to use it to draw arbitrarily transformed
18 * text, change the min and mag filters to GL_LINEAR and
19 * add a pixel of padding between glyphs and rows, and
20 * make sure to clear the texture when wiping the cache.
23 #include FT_ADVANCES_H
24 typedef int Rune; /* 32 bits */
26 #define PADDING 1 /* set to 0 to save some space but disallow arbitrary transforms */
28 #define MAXGLYPHS 4093 /* prime number for hash table goodness */
29 #define CACHESIZE 256
30 #define XPRECISION 4
31 #define YPRECISION 1
33 struct key
35 FT_Face face;
36 short size;
37 short gid;
38 short subx;
39 short suby;
42 struct glyph
44 signed char lsb, top, w, h;
45 short s, t;
46 float advance;
49 struct table
51 struct key key;
52 struct glyph glyph;
55 static FT_Library g_freetype_lib = NULL;
56 static struct table g_table[MAXGLYPHS];
57 static int g_table_load = 0;
58 static unsigned int g_cache_tex = 0;
59 static int g_cache_w = CACHESIZE;
60 static int g_cache_h = CACHESIZE;
61 static int g_cache_row_y = 0;
62 static int g_cache_row_x = 0;
63 static int g_cache_row_h = 0;
65 static void init_font_cache(void)
67 int code;
69 code = FT_Init_FreeType(&g_freetype_lib);
70 if (code)
71 errx(1, "cannot initialize freetype");
73 glGenTextures(1, &g_cache_tex);
74 glBindTexture(GL_TEXTURE_2D, g_cache_tex);
75 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
76 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
77 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
78 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
79 glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, g_cache_w, g_cache_h, 0, GL_ALPHA, GL_UNSIGNED_BYTE, NULL);
82 static void clear_font_cache(void)
84 #if PADDING > 0
85 unsigned char *zero = malloc(g_cache_w * g_cache_h);
86 memset(zero, 0, g_cache_w * g_cache_h);
87 glBindTexture(GL_TEXTURE_2D, g_cache_tex);
88 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, g_cache_w, g_cache_h, GL_ALPHA, GL_UNSIGNED_BYTE, zero);
89 free(zero);
90 #endif
92 memset(g_table, 0, sizeof(g_table));
93 g_table_load = 0;
95 g_cache_row_y = PADDING;
96 g_cache_row_x = PADDING;
97 g_cache_row_h = 0;
100 static FT_Face load_font(char *fontname)
102 FT_Face face;
103 int code;
105 if (g_freetype_lib == NULL)
107 init_font_cache();
108 clear_font_cache();
111 code = FT_New_Face (g_freetype_lib, fontname, 0, &face);
112 if (code) {
113 fprintf (stderr, "failed to load font `%s'\n", fontname);
114 return NULL;
117 FT_Select_Charmap(face, ft_encoding_unicode);
118 return face;
121 static void UNUSED free_font(FT_Face face)
123 clear_font_cache();
124 FT_Done_Face(face);
127 static unsigned int hashfunc(struct key *key)
129 unsigned char *buf = (unsigned char *)key;
130 unsigned int len = sizeof(struct key);
131 unsigned int h = 0;
132 while (len--)
133 h = *buf++ + (h << 6) + (h << 16) - h;
134 return h;
137 static unsigned int lookup_table(struct key *key)
139 unsigned int pos = hashfunc(key) % MAXGLYPHS;
140 while (1)
142 if (!g_table[pos].key.face) /* empty slot */
143 return pos;
144 if (!memcmp(key, &g_table[pos].key, sizeof(struct key))) /* matching slot */
145 return pos;
146 pos = (pos + 1) % MAXGLYPHS;
150 static struct glyph * lookup_glyph(FT_Face face, int size, int gid, int subx, int suby)
152 FT_Vector subv;
153 struct key key;
154 unsigned int pos;
155 int code;
156 int w, h;
159 * Look it up in the table
162 key.face = face;
163 key.size = size;
164 key.gid = gid;
165 key.subx = subx;
166 key.suby = suby;
168 pos = lookup_table(&key);
169 if (g_table[pos].key.face)
170 return &g_table[pos].glyph;
173 * Render the bitmap
176 glEnd();
178 subv.x = subx;
179 subv.y = suby;
181 FT_Set_Transform(face, NULL, &subv);
183 code = FT_Load_Glyph(face, gid, FT_LOAD_NO_BITMAP | FT_LOAD_NO_HINTING);
184 if (code < 0)
185 return NULL;
187 code = FT_Render_Glyph(face->glyph, FT_RENDER_MODE_LIGHT);
188 if (code < 0)
189 return NULL;
191 w = face->glyph->bitmap.width;
192 h = face->glyph->bitmap.rows;
195 * Find an empty slot in the texture
198 if (g_table_load == (MAXGLYPHS * 3) / 4)
200 lprintf("font cache table full, clearing cache");
201 clear_font_cache();
202 pos = lookup_table(&key);
205 if (h + PADDING > g_cache_h || w + PADDING > g_cache_w)
206 errx(1, "rendered glyph exceeds cache dimensions");
208 if (g_cache_row_x + w + PADDING > g_cache_w)
210 g_cache_row_y += g_cache_row_h + PADDING;
211 g_cache_row_x = PADDING;
213 if (g_cache_row_y + h + PADDING > g_cache_h)
215 lprintf("font cache texture full, clearing cache");
216 clear_font_cache();
217 pos = lookup_table(&key);
221 * Copy bitmap into texture
224 memcpy(&g_table[pos].key, &key, sizeof(struct key));
225 g_table[pos].glyph.w = face->glyph->bitmap.width;
226 g_table[pos].glyph.h = face->glyph->bitmap.rows;
227 g_table[pos].glyph.lsb = face->glyph->bitmap_left;
228 g_table[pos].glyph.top = face->glyph->bitmap_top;
229 g_table[pos].glyph.s = g_cache_row_x;
230 g_table[pos].glyph.t = g_cache_row_y;
231 g_table[pos].glyph.advance = face->glyph->advance.x / 64.0;
232 g_table_load ++;
234 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
235 glPixelStorei(GL_UNPACK_ROW_LENGTH, face->glyph->bitmap.pitch);
236 glTexSubImage2D(GL_TEXTURE_2D, 0, g_cache_row_x, g_cache_row_y, w, h,
237 GL_ALPHA, GL_UNSIGNED_BYTE, face->glyph->bitmap.buffer);
238 glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
240 glBegin(GL_QUADS);
242 g_cache_row_x += w + PADDING;
243 if (g_cache_row_h < h + PADDING)
244 g_cache_row_h = h + PADDING;
246 return &g_table[pos].glyph;
249 static float draw_glyph(FT_Face face, int size, int gid, float x, float y)
251 struct glyph *glyph;
252 int subx = (x - floor(x)) * XPRECISION;
253 int suby = (y - floor(y)) * YPRECISION;
254 subx = (subx * 64) / XPRECISION;
255 suby = (suby * 64) / YPRECISION;
257 glyph = lookup_glyph(face, size, gid, subx, suby);
258 if (!glyph)
259 return 0.0;
261 float s0 = (float) glyph->s / g_cache_w;
262 float t0 = (float) glyph->t / g_cache_h;
263 float s1 = (float) (glyph->s + glyph->w) / g_cache_w;
264 float t1 = (float) (glyph->t + glyph->h) / g_cache_h;
265 float xc = floor(x) + glyph->lsb;
266 float yc = floor(y) - glyph->top + glyph->h;
268 glTexCoord2f(s0, t0); glVertex2f(xc, yc - glyph->h);
269 glTexCoord2f(s1, t0); glVertex2f(xc + glyph->w, yc - glyph->h);
270 glTexCoord2f(s1, t1); glVertex2f(xc + glyph->w, yc);
271 glTexCoord2f(s0, t1); glVertex2f(xc, yc);
273 return glyph->advance;
276 float measure_string(FT_Face face, float fsize, char *str)
278 int size = fsize * 64;
279 FT_Fixed advance;
280 FT_Vector kern;
281 Rune ucs, gid;
282 float w = 0.0;
283 int left = 0;
285 FT_Set_Char_Size(face, size, size, 72, 72);
287 while (*str)
289 str += chartorune(&ucs, str);
290 gid = FT_Get_Char_Index(face, ucs);
291 FT_Get_Advance(face, gid, FT_LOAD_NO_BITMAP | FT_LOAD_NO_HINTING, &advance);
292 w += advance / 65536.0;
293 FT_Get_Kerning(face, left, gid, FT_KERNING_UNFITTED, &kern);
294 w += kern.x / 64.0;
295 left = gid;
298 return w;
301 float draw_string(FT_Face face, float fsize, float x, float y, char *str)
303 int size = fsize * 64;
304 FT_Vector kern;
305 Rune ucs, gid;
306 int left = 0;
308 FT_Set_Char_Size(face, size, size, 72, 72);
310 glBindTexture(GL_TEXTURE_2D, g_cache_tex);
311 glBegin(GL_QUADS);
313 while (*str)
315 str += chartorune(&ucs, str);
316 gid = FT_Get_Char_Index(face, ucs);
317 x += draw_glyph(face, size, gid, x, y);
318 FT_Get_Kerning(face, left, gid, FT_KERNING_UNFITTED, &kern);
319 x += kern.x / 64.0;
320 left = gid;
323 glEnd();
325 return x;