-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathfont.c
482 lines (396 loc) · 11.3 KB
/
font.c
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
/*
* A very simple font cache and rasterizer that uses stb_truetype
* to draw fonts from a single OpenGL texture. The code uses
* a linear-probe hashtable, and writes new glyphs into
* the texture using glTexSubImage2D. When the texture fills
* up, or the hash table gets too crowded, the cache is emptied.
*
* This is designed to be used for horizontal text only,
* and draws unhinted text with subpixel accurate metrics
* and kerning. As such, you should always call the drawing
* function with an orthogonal transform that maps units
* to pixels accurately.
*/
#include "mio.h"
static void text_flush(void);
static void clear_glyph_cache(void);
static struct cache *font_cache = NULL;
/*
* TrueType font
*/
#define STB_TRUETYPE_IMPLEMENTATION
#define STBTT_malloc(x,u) malloc(x)
#define STBTT_free(x,u) free(x)
#include "stb_truetype.h"
struct font
{
int tag;
unsigned char *data;
stbtt_fontinfo info;
};
struct font *load_font_from_memory(const char *filename, unsigned char *data, int len)
{
struct font *font;
int ok;
font = malloc(sizeof(struct font));
font->tag = TAG_FONT;
font->data = malloc(len);
memcpy(font->data, data, len);
ok = stbtt_InitFont(&font->info, font->data, 0);
if (!ok) {
warn("error: cannot init font file: '%s'", filename);
return NULL;
}
return font;
}
struct font *load_font(const char *filename)
{
struct font *font;
int ok;
font = lookup(font_cache, filename);
if (font)
return font;
font = malloc(sizeof(struct font));
font->tag = TAG_FONT;
font->data = load_file(filename, NULL);
if (!font->data) {
warn("error: cannot load font file: '%s'", filename);
return NULL;
}
ok = stbtt_InitFont(&font->info, font->data, 0);
if (!ok) {
warn("error: cannot init font file: '%s'", filename);
return NULL;
}
if (font)
font_cache = insert(font_cache, filename, font);
return font;
}
void free_font(struct font *font)
{
clear_glyph_cache();
free(font->data);
free(font);
}
/*
* Glyph cache
*/
#define PADDING 1 /* set to 0 to save some space but disallow arbitrary transforms */
#define MAXGLYPHS 4093 /* prime number for hash table goodness */
#define CACHESIZE 512
#define XPRECISION 4
#define YPRECISION 1
struct key
{
struct font *font;
int size;
int gid;
short subx;
short suby;
};
struct glyph
{
short x, y, w, h;
short s, t;
float advance;
};
struct table
{
struct key key;
struct glyph glyph;
};
static struct table table[MAXGLYPHS];
static int table_load = 0;
static unsigned char cache_zero[CACHESIZE * CACHESIZE];
static unsigned int cache_tex = 0;
static int cache_row_y = PADDING;
static int cache_row_x = PADDING;
static int cache_row_h = 0;
static void clear_glyph_cache(void)
{
text_flush();
memset(table, 0, sizeof table);
table_load = 0;
glBindTexture(GL_TEXTURE_2D, cache_tex);
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, CACHESIZE, CACHESIZE,
GL_RED, GL_UNSIGNED_BYTE, cache_zero);
cache_row_y = PADDING;
cache_row_x = PADDING;
cache_row_h = 0;
}
static unsigned int hashfunc(struct key *key)
{
unsigned char *buf = (unsigned char *)key;
unsigned int len = sizeof(struct key);
unsigned int h = 0;
while (len--)
h = *buf++ + (h << 6) + (h << 16) - h;
return h;
}
static unsigned int lookup_table(struct key *key)
{
unsigned int pos = hashfunc(key) % MAXGLYPHS;
while (1) {
if (!table[pos].key.font) /* empty slot */
return pos;
if (!memcmp(key, &table[pos].key, sizeof(struct key))) /* matching slot */
return pos;
pos = (pos + 1) % MAXGLYPHS;
}
}
static struct glyph *lookup_glyph(struct font *font, float scale, int gid, int subx, int suby)
{
struct key key;
unsigned int pos;
unsigned char *data;
float shift_x, shift_y;
int w, h, x, y;
int advance, lsb;
/* Look it up in the table */
memset(&key, 0, sizeof key);
key.font = font;
key.size = scale * 10000;
key.gid = gid;
key.subx = subx;
key.suby = suby;
pos = lookup_table(&key);
if (table[pos].key.font)
return &table[pos].glyph;
/* Render the bitmap */
shift_x = (float)subx / XPRECISION;
shift_y = (float)suby / YPRECISION;
stbtt_GetGlyphHMetrics(&font->info, gid, &advance, &lsb);
data = stbtt_GetGlyphBitmapSubpixel(&font->info, scale, scale, shift_x, shift_y, gid, &w, &h, &x, &y);
/* Find an empty slot in the texture */
if (table_load == (MAXGLYPHS * 3) / 4) {
puts("glyph cache table full, clearing cache");
clear_glyph_cache();
pos = lookup_table(&key);
}
if (h + PADDING > CACHESIZE || w + PADDING > CACHESIZE) {
warn("error: rendered glyph exceeds cache dimensions");
exit(1);
}
if (cache_row_x + w + PADDING > CACHESIZE) {
cache_row_y += cache_row_h + PADDING;
cache_row_x = PADDING;
cache_row_h = 0;
}
if (cache_row_y + h + PADDING > CACHESIZE) {
puts("glyph cache texture full, clearing cache");
clear_glyph_cache();
pos = lookup_table(&key);
}
/* Copy the bitmap into our texture */
memcpy(&table[pos].key, &key, sizeof(struct key));
table[pos].glyph.w = w;
table[pos].glyph.h = h;
table[pos].glyph.x = x;
table[pos].glyph.y = y;
table[pos].glyph.s = cache_row_x;
table[pos].glyph.t = cache_row_y;
table[pos].glyph.advance = advance * scale;
table_load ++;
if (data && w && h) {
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glPixelStorei(GL_UNPACK_ROW_LENGTH, w);
glTexSubImage2D(GL_TEXTURE_2D, 0,
cache_row_x, cache_row_y, w, h,
GL_RED, GL_UNSIGNED_BYTE, data);
glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
}
free(data);
cache_row_x += w + PADDING;
if (cache_row_h < h + PADDING)
cache_row_h = h + PADDING;
return &table[pos].glyph;
}
/*
* Text buffer and drawing.
*/
static const char *text_vert_src =
"uniform mat4 clip_from_view;\n"
"uniform mat4 view_from_world;\n"
"in vec4 att_position;\n"
"in vec2 att_texcoord;\n"
"in vec4 att_color;\n"
"out vec2 var_texcoord;\n"
"out vec4 var_color;\n"
"void main() {\n"
" gl_Position = clip_from_view * view_from_world * att_position;\n"
" var_texcoord = att_texcoord;\n"
" var_color = att_color;\n"
"}\n"
;
static const char *text_frag_src =
"uniform sampler2D map_color;\n"
"in vec2 var_texcoord;\n"
"in vec4 var_color;\n"
"out vec4 frag_color;\n"
"void main() {\n"
" float coverage = texture(map_color, var_texcoord).r;\n"
" frag_color = var_color * coverage;\n"
"}\n"
;
#define MAXBUFFER (256*6) /* size of our triangle buffer */
static unsigned int text_vao = 0;
static unsigned int text_vbo = 0;
static int text_buf_len = 0;
static struct {
float position[2];
float texcoord[2];
float color[4];
} text_buf[MAXBUFFER];
static float text_color[4] = { 1, 1, 1, 1 };
static struct font *text_font = NULL;
static float text_scale = 1;
void text_set_color(float r, float g, float b, float a)
{
text_color[0] = r;
text_color[1] = g;
text_color[2] = b;
text_color[3] = a;
}
void text_set_font(struct font *font, float size)
{
text_font = font;
text_scale = stbtt_ScaleForPixelHeight(&font->info, size);
}
void text_begin(mat4 clip_from_view, mat4 view_from_world)
{
static int text_prog = 0;
static int text_uni_clip_from_view = -1;
static int text_uni_view_from_world = -1;
if (!cache_tex) {
memset(table, 0, sizeof table);
memset(cache_zero, 0, sizeof cache_zero);
glGenTextures(1, &cache_tex);
glBindTexture(GL_TEXTURE_2D, cache_tex);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RED, CACHESIZE, CACHESIZE, 0,
GL_RED, GL_UNSIGNED_BYTE, cache_zero);
}
if (!text_prog) {
text_prog = compile_shader(text_vert_src, text_frag_src);
text_uni_clip_from_view = glGetUniformLocation(text_prog, "clip_from_view");
text_uni_view_from_world = glGetUniformLocation(text_prog, "view_from_world");
}
if (!text_vao) {
glGenVertexArrays(1, &text_vao);
glBindVertexArray(text_vao);
glGenBuffers(1, &text_vbo);
glBindBuffer(GL_ARRAY_BUFFER, text_vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof text_buf, NULL, GL_STREAM_DRAW);
glEnableVertexAttribArray(ATT_POSITION);
glEnableVertexAttribArray(ATT_TEXCOORD);
glEnableVertexAttribArray(ATT_COLOR);
glVertexAttribPointer(ATT_POSITION, 2, GL_FLOAT, 0, sizeof text_buf[0], (void*)0);
glVertexAttribPointer(ATT_TEXCOORD, 2, GL_FLOAT, 0, sizeof text_buf[0], (void*)8);
glVertexAttribPointer(ATT_COLOR, 4, GL_FLOAT, 0, sizeof text_buf[0], (void*)16);
}
glEnable(GL_BLEND);
glUseProgram(text_prog);
glUniformMatrix4fv(text_uni_clip_from_view, 1, 0, clip_from_view);
glUniformMatrix4fv(text_uni_view_from_world, 1, 0, view_from_world);
glBindVertexArray(text_vao);
glActiveTexture(MAP_COLOR);
glBindTexture(GL_TEXTURE_2D, cache_tex);
}
static void text_flush(void)
{
if (text_buf_len > 0) {
glBindBuffer(GL_ARRAY_BUFFER, text_vbo);
glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof text_buf[0] * text_buf_len, text_buf);
glDrawArrays(GL_TRIANGLES, 0, text_buf_len);
text_buf_len = 0;
}
}
void text_end(void)
{
text_flush();
glDisable(GL_BLEND);
}
static void add_vertex(float x, float y, float s, float t, float *color)
{
text_buf[text_buf_len].position[0] = x;
text_buf[text_buf_len].position[1] = y;
text_buf[text_buf_len].texcoord[0] = s;
text_buf[text_buf_len].texcoord[1] = t;
text_buf[text_buf_len].color[0] = color[0];
text_buf[text_buf_len].color[1] = color[1];
text_buf[text_buf_len].color[2] = color[2];
text_buf[text_buf_len].color[3] = color[3];
text_buf_len++;
}
static void add_rect(float x0, float y0, float x1, float y1,
float s0, float t0, float s1, float t1)
{
if (text_buf_len + 6 >= MAXBUFFER)
text_flush();
add_vertex(x0, y0, s0, t0, text_color);
add_vertex(x0, y1, s0, t1, text_color);
add_vertex(x1, y1, s1, t1, text_color);
add_vertex(x0, y0, s0, t0, text_color);
add_vertex(x1, y1, s1, t1, text_color);
add_vertex(x1, y0, s1, t0, text_color);
}
static float add_glyph(int gid, float x, float y)
{
struct glyph *glyph;
int subx = (x - floor(x)) * XPRECISION;
int suby = (y - floor(y)) * YPRECISION;
glyph = lookup_glyph(text_font, text_scale, gid, subx, suby);
if (!glyph)
return 0;
float s0 = (float) glyph->s / CACHESIZE;
float t0 = (float) glyph->t / CACHESIZE;
float s1 = (float) (glyph->s + glyph->w) / CACHESIZE;
float t1 = (float) (glyph->t + glyph->h) / CACHESIZE;
float xc = floor(x) + glyph->x;
float yc = floor(y) + glyph->y;
add_rect(xc, yc, xc + glyph->w, yc + glyph->h, s0, t0, s1, t1);
return glyph->advance;
}
float text_show(float x, float y, char *str)
{
int ucs, gid;
int kern;
int left = 0;
while (*str) {
str += chartorune(&ucs, str);
gid = stbtt_FindGlyphIndex(&text_font->info, ucs);
kern = stbtt_GetGlyphKernAdvance(&text_font->info, left, gid);
x += add_glyph(gid, x, y);
x += kern * text_scale;
left = gid;
}
return x;
}
static float font_width_imp(struct font *font, float scale, char *str)
{
int ucs, gid;
int advance, kern;
float w = 0.0;
int left = 0;
while (*str) {
str += chartorune(&ucs, str);
gid = stbtt_FindGlyphIndex(&font->info, ucs);
stbtt_GetGlyphHMetrics(&font->info, gid, &advance, NULL);
kern = stbtt_GetGlyphKernAdvance(&font->info, left, gid);
w += advance * scale;
w += kern * scale;
left = gid;
}
return w;
}
float font_width(struct font *font, float size, char *str)
{
return font_width_imp(font, stbtt_ScaleForPixelHeight(&font->info, size), str);
}
float text_width(char *str)
{
return font_width_imp(text_font, text_scale, str);
}