Consistency
[llpp.git] / glfont.c
blob01617fb5c89adbb47e4601f09d767ef27952e7c6
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;
64 static int g_use_kern = 0;
66 static void init_font_cache(void)
68 int code;
70 code = FT_Init_FreeType(&g_freetype_lib);
71 if (code)
72 errx(1, "cannot initialize freetype");
74 glGenTextures(1, &g_cache_tex);
75 glBindTexture(GL_TEXTURE_2D, g_cache_tex);
76 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
77 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
78 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
79 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
80 glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, g_cache_w, g_cache_h, 0, GL_ALPHA, GL_UNSIGNED_BYTE, NULL);
83 static void clear_font_cache(void)
85 #if PADDING > 0
86 unsigned char *zero = calloc(g_cache_w, g_cache_h);
87 if (!zero)
88 err(1, "malloc zero (%u bytes failed)", g_cache_w * g_cache_h);
89 glBindTexture(GL_TEXTURE_2D, g_cache_tex);
90 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, g_cache_w, g_cache_h, GL_ALPHA, GL_UNSIGNED_BYTE, zero);
91 free(zero);
92 #endif
94 memset(g_table, 0, sizeof(g_table));
95 g_table_load = 0;
97 g_cache_row_y = PADDING;
98 g_cache_row_x = PADDING;
99 g_cache_row_h = 0;
102 static FT_Face load_font(char *fontname)
104 FT_Face face;
105 int code;
107 if (g_freetype_lib == NULL)
109 init_font_cache();
110 clear_font_cache();
113 code = FT_New_Face (g_freetype_lib, fontname, 0, &face);
114 if (code) {
115 fprintf (stderr, "failed to load font `%s'\n", fontname);
116 return NULL;
119 FT_Select_Charmap(face, ft_encoding_unicode);
120 return face;
123 static FT_Face UNUSED_ATTR load_builtin_font(void *base, int len)
125 FT_Face face;
126 int code;
128 if (g_freetype_lib == NULL)
130 init_font_cache();
131 clear_font_cache();
134 code = FT_New_Memory_Face(g_freetype_lib, base, len, 0, &face);
135 if (code) {
136 fprintf (stderr, "failed to load builtin font\n");
137 return NULL;
140 FT_Select_Charmap(face, ft_encoding_unicode);
141 return face;
144 static void UNUSED_ATTR free_font(FT_Face face)
146 clear_font_cache();
147 FT_Done_Face(face);
150 static unsigned int hashfunc(struct key *key)
152 unsigned char *buf = (unsigned char *)key;
153 unsigned int len = sizeof(struct key);
154 unsigned int h = 0;
155 while (len--)
156 h = *buf++ + (h << 6) + (h << 16) - h;
157 return h;
160 static unsigned int lookup_table(struct key *key)
162 unsigned int pos = hashfunc(key) % MAXGLYPHS;
163 while (1)
165 if (!g_table[pos].key.face) /* empty slot */
166 return pos;
167 if (!memcmp(key, &g_table[pos].key, sizeof(struct key))) /* matching slot */
168 return pos;
169 pos = (pos + 1) % MAXGLYPHS;
173 static struct glyph * lookup_glyph(FT_Face face, int size, int gid, int subx, int suby)
175 FT_Vector subv;
176 struct key key;
177 unsigned int pos;
178 int code;
179 int w, h;
182 * Look it up in the table
185 key.face = face;
186 key.size = size;
187 key.gid = gid;
188 key.subx = subx;
189 key.suby = suby;
191 pos = lookup_table(&key);
192 if (g_table[pos].key.face)
193 return &g_table[pos].glyph;
196 * Render the bitmap
198 #ifdef FFP
199 glEnd();
200 #endif
202 subv.x = subx;
203 subv.y = suby;
205 FT_Set_Transform(face, NULL, &subv);
207 code = FT_Load_Glyph(face, gid, FT_LOAD_NO_BITMAP | FT_LOAD_NO_HINTING);
208 if (code < 0)
209 return NULL;
211 code = FT_Render_Glyph(face->glyph, FT_RENDER_MODE_LIGHT);
212 if (code < 0)
213 return NULL;
215 w = face->glyph->bitmap.width;
216 h = face->glyph->bitmap.rows;
219 * Find an empty slot in the texture
222 if (g_table_load == (MAXGLYPHS * 3) / 4)
224 lprintf("font cache table full, clearing cache");
225 clear_font_cache();
226 pos = lookup_table(&key);
229 if (h + PADDING > g_cache_h || w + PADDING > g_cache_w)
230 errx(1, "rendered glyph exceeds cache dimensions");
232 if (g_cache_row_x + w + PADDING > g_cache_w)
234 g_cache_row_y += g_cache_row_h + PADDING;
235 g_cache_row_x = PADDING;
236 g_cache_row_h = 0;
238 if (g_cache_row_y + h + PADDING > g_cache_h)
240 lprintf("font cache texture full, clearing cache");
241 clear_font_cache();
242 pos = lookup_table(&key);
246 * Copy bitmap into texture
249 memcpy(&g_table[pos].key, &key, sizeof(struct key));
250 g_table[pos].glyph.w = face->glyph->bitmap.width;
251 g_table[pos].glyph.h = face->glyph->bitmap.rows;
252 g_table[pos].glyph.lsb = face->glyph->bitmap_left;
253 g_table[pos].glyph.top = face->glyph->bitmap_top;
254 g_table[pos].glyph.s = g_cache_row_x;
255 g_table[pos].glyph.t = g_cache_row_y;
256 g_table[pos].glyph.advance = face->glyph->advance.x / 64.0;
257 g_table_load ++;
259 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
260 glPixelStorei(GL_UNPACK_ROW_LENGTH, face->glyph->bitmap.pitch);
261 glTexSubImage2D(GL_TEXTURE_2D, 0, g_cache_row_x, g_cache_row_y, w, h,
262 GL_ALPHA, GL_UNSIGNED_BYTE, face->glyph->bitmap.buffer);
263 glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
265 #ifdef FFP
266 glBegin(GL_QUADS);
267 #endif
268 g_cache_row_x += w + PADDING;
269 if (g_cache_row_h < h + PADDING)
270 g_cache_row_h = h + PADDING;
272 return &g_table[pos].glyph;
275 static float draw_glyph(FT_Face face, int size, int gid, float x, float y)
277 struct glyph *glyph;
278 int subx = (x - floor(x)) * XPRECISION;
279 int suby = (y - floor(y)) * YPRECISION;
280 #ifndef FFP
281 GLfloat *t = state.texcoords;
282 GLfloat *v = state.vertices;
283 #endif
284 float s0, t0, s1, t1, xc, yc;
286 subx = (subx * 64) / XPRECISION;
287 suby = (suby * 64) / YPRECISION;
289 glyph = lookup_glyph(face, size, gid, subx, suby);
290 if (!glyph)
291 return 0.0;
293 s0 = (float) glyph->s / g_cache_w;
294 t0 = (float) glyph->t / g_cache_h;
295 s1 = (float) (glyph->s + glyph->w) / g_cache_w;
296 t1 = (float) (glyph->t + glyph->h) / g_cache_h;
297 xc = floor(x) + glyph->lsb;
298 yc = floor(y) - glyph->top + glyph->h;
300 #ifndef FFP
301 t[0] = s0; t[1] = t0; v[0] = xc; v[1] = yc - glyph->h;
302 t[2] = s1; t[3] = t0; v[2] = xc + glyph->w; v[3] = yc - glyph->h;
303 t[4] = s0; t[5] = t1; v[4] = xc; v[5] = yc;
304 t[6] = s1; t[7] = t1; v[6] = xc + glyph->w; v[7] = yc;
306 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
307 #else
308 glTexCoord2f(s0, t0); glVertex2f(xc, yc - glyph->h);
309 glTexCoord2f(s1, t0); glVertex2f(xc + glyph->w, yc - glyph->h);
310 glTexCoord2f(s1, t1); glVertex2f(xc + glyph->w, yc);
311 glTexCoord2f(s0, t1); glVertex2f(xc, yc);
312 #endif
314 return glyph->advance;
317 static float measure_string(FT_Face face, float fsize, char *str)
319 int size = fsize * 64;
320 FT_Fixed advance;
321 Rune ucs, gid;
322 float w = 0.0;
323 int left = 0;
325 FT_Set_Char_Size(face, size, size, 72, 72);
327 while (*str)
329 str += fz_chartorune(&ucs, str);
330 gid = FT_Get_Char_Index(face, ucs);
331 FT_Get_Advance(face, gid, FT_LOAD_NO_BITMAP | FT_LOAD_NO_HINTING, &advance);
332 w += advance / 65536.0;
333 if (g_use_kern) {
334 FT_Vector kern;
336 FT_Get_Kerning(face, left, gid, FT_KERNING_UNFITTED, &kern);
337 w += kern.x / 64.0;
339 left = gid;
342 return w;
345 static float draw_string(FT_Face face, float fsize, float x, float y, char *str)
347 int size = fsize * 64;
348 Rune ucs, gid;
349 int left = 0;
351 FT_Set_Char_Size(face, size, size, 72, 72);
353 glBindTexture(GL_TEXTURE_2D, g_cache_tex);
354 #ifdef FFP
355 glBegin(GL_QUADS);
356 #else
357 glVertexPointer(2, GL_FLOAT, 0, state.vertices);
358 glTexCoordPointer(2, GL_FLOAT, 0, state.texcoords);
359 #endif
360 while (*str)
362 str += fz_chartorune(&ucs, str);
363 gid = FT_Get_Char_Index(face, ucs);
364 x += draw_glyph(face, size, gid, x, y);
365 if (g_use_kern) {
366 FT_Vector kern;
368 FT_Get_Kerning(face, left, gid, FT_KERNING_UNFITTED, &kern);
369 x += kern.x / 64.0;
371 left = gid;
374 #ifdef FFP
375 glEnd();
376 #endif
378 return x;