libdrawtext

view src/font.c @ 21:c750059a1258

font2glyphmap tool
author John Tsiombikas <nuclear@member.fsf.org>
date Tue, 15 Apr 2014 03:33:39 +0300
parents b6ed6e9b4092
children df6d52b36bd6
line source
1 /*
2 libdrawtext - a simple library for fast text rendering in OpenGL
3 Copyright (C) 2011 John Tsiombikas <nuclear@member.fsf.org>
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18 #ifndef NO_FREETYPE
19 #define USE_FREETYPE
20 #endif
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <math.h>
26 #include <limits.h>
27 #include <ctype.h>
28 #include <float.h>
29 #include <errno.h>
30 #ifdef USE_FREETYPE
31 #include <ft2build.h>
32 #include FT_FREETYPE_H
33 #endif
34 #include "drawtext.h"
35 #include "drawtext_impl.h"
37 #define FTSZ_TO_PIXELS(x) ((x) / 64)
38 #define MAX_IMG_WIDTH 4096
41 #ifdef USE_FREETYPE
42 static int init_freetype(void);
43 static void cleanup(void);
45 static void calc_best_size(int total_width, int max_glyph_height, int padding, int pow2, int *imgw, int *imgh);
46 static int next_pow2(int x);
48 static FT_Library ft;
51 static int init_done;
53 static int init_freetype(void)
54 {
55 if(!init_done) {
56 if(FT_Init_FreeType(&ft) != 0) {
57 return -1;
58 }
59 atexit(cleanup);
60 init_done = 1;
61 }
62 return 0;
63 }
65 static void cleanup(void)
66 {
67 if(init_done) {
68 FT_Done_FreeType(ft);
69 }
70 }
72 struct dtx_font *dtx_open_font(const char *fname, int sz)
73 {
74 struct dtx_font *fnt;
76 init_freetype();
78 if(!(fnt = calloc(1, sizeof *fnt))) {
79 fperror("failed to allocate font structure");
80 return 0;
81 }
83 if(FT_New_Face(ft, fname, 0, (FT_Face*)&fnt->face) != 0) {
84 fprintf(stderr, "failed to open font file: %s\n", fname);
85 return 0;
86 }
88 /* pre-create the extended ASCII range glyphmap */
89 if(sz) {
90 dtx_prepare_range(fnt, sz, 0, 256);
92 if(!dtx_font) {
93 dtx_use_font(fnt, sz);
94 }
95 }
97 return fnt;
98 }
100 void dtx_close_font(struct dtx_font *fnt)
101 {
102 if(!fnt) return;
104 FT_Done_Face(fnt->face);
106 /* destroy the glyphmaps */
107 while(fnt->gmaps) {
108 void *tmp = fnt->gmaps;
109 fnt->gmaps = fnt->gmaps->next;
110 dtx_free_glyphmap(tmp);
111 }
113 free(fnt);
114 }
116 void dtx_prepare(struct dtx_font *fnt, int sz)
117 {
118 dtx_get_font_glyphmap_range(fnt, sz, 0, 256);
119 }
121 void dtx_prepare_range(struct dtx_font *fnt, int sz, int cstart, int cend)
122 {
123 dtx_get_font_glyphmap_range(fnt, sz, cstart, cend);
124 }
126 struct dtx_glyphmap *dtx_get_font_glyphmap(struct dtx_font *fnt, int sz, int code)
127 {
128 struct dtx_glyphmap *gm;
130 /* check to see if the last we've given out fits the bill */
131 if(fnt->last_gmap && code >= fnt->last_gmap->cstart && code < fnt->last_gmap->cend && fnt->last_gmap->ptsize == sz) {
132 return fnt->last_gmap;
133 }
135 /* otherwise search for the appropriate glyphmap */
136 gm = fnt->gmaps;
137 while(gm) {
138 if(code >= gm->cstart && code < gm->cend && sz == gm->ptsize) {
139 fnt->last_gmap = gm;
140 return gm;
141 }
142 gm = gm->next;
143 }
144 return 0;
145 }
147 struct dtx_glyphmap *dtx_get_font_glyphmap_range(struct dtx_font *fnt, int sz, int cstart, int cend)
148 {
149 struct dtx_glyphmap *gm;
151 /* search the available glyphmaps to see if we've got one that includes
152 * the requested range
153 */
154 gm = fnt->gmaps;
155 while(gm) {
156 if(gm->cstart <= cstart && gm->cend >= cend && gm->ptsize == sz) {
157 return gm;
158 }
159 gm = gm->next;
160 }
162 /* not found, create one and add it to the list */
163 if(!(gm = dtx_create_glyphmap_range(fnt, sz, cstart, cend))) {
164 return 0;
165 }
166 return gm;
167 }
169 struct dtx_glyphmap *dtx_create_glyphmap_range(struct dtx_font *fnt, int sz, int cstart, int cend)
170 {
171 FT_Face face = fnt->face;
172 struct dtx_glyphmap *gmap;
173 int i, j;
174 int gx, gy;
175 int padding = 4;
176 int total_width = padding;
177 int max_height = 0;
179 FT_Set_Char_Size(fnt->face, 0, sz * 64, 72, 72);
181 if(!(gmap = calloc(1, sizeof *gmap))) {
182 return 0;
183 }
185 gmap->ptsize = sz;
186 gmap->cstart = cstart;
187 gmap->cend = cend;
188 gmap->crange = cend - cstart;
189 gmap->line_advance = FTSZ_TO_PIXELS((float)face->size->metrics.height);
191 if(!(gmap->glyphs = malloc(gmap->crange * sizeof *gmap->glyphs))) {
192 free(gmap);
193 return 0;
194 }
196 for(i=0; i<gmap->crange; i++) {
197 int h;
199 FT_Load_Char(face, i + cstart, 0);
200 h = FTSZ_TO_PIXELS(face->glyph->metrics.height);
202 if(h > max_height) {
203 max_height = h;
204 }
205 total_width += FTSZ_TO_PIXELS(face->glyph->metrics.width) + padding;
206 }
208 calc_best_size(total_width, max_height, padding, 1, &gmap->xsz, &gmap->ysz);
210 if(!(gmap->pixels = malloc(gmap->xsz * gmap->ysz))) {
211 free(gmap->glyphs);
212 free(gmap);
213 return 0;
214 }
215 memset(gmap->pixels, 0, gmap->xsz * gmap->ysz);
217 gx = padding;
218 gy = padding;
220 for(i=0; i<gmap->crange; i++) {
221 float gwidth, gheight;
222 unsigned char *src, *dst;
223 FT_GlyphSlot glyph;
225 FT_Load_Char(face, i + cstart, FT_LOAD_RENDER);
226 glyph = face->glyph;
227 gwidth = FTSZ_TO_PIXELS((float)glyph->metrics.width);
228 gheight = FTSZ_TO_PIXELS((float)glyph->metrics.height);
230 if(gx > gmap->xsz - gwidth - padding) {
231 gx = padding;
232 gy += max_height + padding;
233 }
235 src = glyph->bitmap.buffer;
236 dst = gmap->pixels + gy * gmap->xsz + gx;
238 for(j=0; j<glyph->bitmap.rows; j++) {
239 memcpy(dst, src, glyph->bitmap.width);
240 dst += gmap->xsz;
241 src += glyph->bitmap.pitch;
242 }
244 gmap->glyphs[i].code = i;
245 gmap->glyphs[i].x = gx - 1;
246 gmap->glyphs[i].y = gy - 1;
247 gmap->glyphs[i].width = gwidth + 2;
248 gmap->glyphs[i].height = gheight + 2;
249 gmap->glyphs[i].orig_x = -FTSZ_TO_PIXELS((float)glyph->metrics.horiBearingX) + 1;
250 gmap->glyphs[i].orig_y = FTSZ_TO_PIXELS((float)glyph->metrics.height - glyph->metrics.horiBearingY) + 1;
251 gmap->glyphs[i].advance = FTSZ_TO_PIXELS((float)glyph->metrics.horiAdvance);
252 /* also precalc normalized */
253 gmap->glyphs[i].nx = (float)gmap->glyphs[i].x / (float)gmap->xsz;
254 gmap->glyphs[i].ny = (float)gmap->glyphs[i].y / (float)gmap->ysz;
255 gmap->glyphs[i].nwidth = (float)gmap->glyphs[i].width / (float)gmap->xsz;
256 gmap->glyphs[i].nheight = (float)gmap->glyphs[i].height / (float)gmap->ysz;
258 gx += gwidth + padding;
259 }
261 /* add it to the glyphmaps list of the font */
262 gmap->next = fnt->gmaps;
263 fnt->gmaps = gmap;
265 return gmap;
266 }
267 #endif /* USE_FREETYPE */
269 void dtx_free_glyphmap(struct dtx_glyphmap *gmap)
270 {
271 if(gmap) {
272 free(gmap->pixels);
273 free(gmap->glyphs);
274 free(gmap);
275 }
276 }
278 unsigned char *dtx_get_glyphmap_pixels(struct dtx_glyphmap *gmap)
279 {
280 return gmap->pixels;
281 }
283 int dtx_get_glyphmap_width(struct dtx_glyphmap *gmap)
284 {
285 return gmap->xsz;
286 }
288 int dtx_get_glyphmap_height(struct dtx_glyphmap *gmap)
289 {
290 return gmap->ysz;
291 }
293 struct dtx_glyphmap *dtx_load_glyphmap(const char *fname)
294 {
295 FILE *fp;
296 struct dtx_glyphmap *gmap;
298 if(!(fp = fopen(fname, "r"))) {
299 return 0;
300 }
301 gmap = dtx_load_glyphmap_stream(fp);
302 fclose(fp);
303 return gmap;
304 }
306 struct dtx_glyphmap *dtx_load_glyphmap_stream(FILE *fp)
307 {
308 char buf[512];
309 int hdr_lines = 0;
310 struct dtx_glyphmap *gmap;
311 struct glyph *glyphs = 0;
312 int min_code = INT_MAX;
313 int max_code = INT_MIN;
314 int i, max_pixval, num_pixels;
316 if(!(gmap = calloc(1, sizeof *gmap))) {
317 fperror("failed to allocate glyphmap");
318 return 0;
319 }
321 while(hdr_lines < 3) {
322 char *line = buf;
323 if(!fgets(buf, sizeof buf, fp)) {
324 fperror("unexpected end of file");
325 goto err;
326 }
328 while(isspace(*line)) {
329 line++;
330 }
332 if(line[0] == '#') {
333 struct glyph *g;
334 int c, res;
335 float x, y, xsz, ysz;
337 res = sscanf(line + 1, "%d: %fx%f+%f+%f\n", &c, &xsz, &ysz, &x, &y);
338 if(res != 5) {
339 fprintf(stderr, "%s: invalid glyph info line\n", __FUNCTION__);
340 goto err;
341 }
343 if(!(g = malloc(sizeof *g))) {
344 fperror("failed to allocate glyph");
345 goto err;
346 }
347 g->code = c;
348 g->x = x;
349 g->y = y;
350 g->width = xsz;
351 g->height = ysz;
352 g->next = glyphs;
353 glyphs = g;
355 if(c < min_code) {
356 min_code = c;
357 }
358 if(c > max_code) {
359 max_code = c;
360 }
361 } else {
362 switch(hdr_lines) {
363 case 0:
364 if(0[line] != 'P' || 1[line] != '6') {
365 fprintf(stderr, "%s: invalid file format (magic)\n", __FUNCTION__);
366 goto err;
367 }
368 break;
370 case 1:
371 if(sscanf(line, "%d %d", &gmap->xsz, &gmap->ysz) != 2) {
372 fprintf(stderr, "%s: invalid file format (dim)\n", __FUNCTION__);
373 goto err;
374 }
375 break;
377 case 2:
378 {
379 char *endp;
380 max_pixval = strtol(line, &endp, 10);
381 if(endp == line) {
382 fprintf(stderr, "%s: invalid file format (maxval)\n", __FUNCTION__);
383 goto err;
384 }
385 }
386 break;
388 default:
389 break; /* can't happen */
390 }
391 hdr_lines++;
392 }
393 }
395 num_pixels = gmap->xsz * gmap->ysz;
396 if(!(gmap->pixels = malloc(num_pixels))) {
397 fperror("failed to allocate pixels");
398 goto err;
399 }
401 for(i=0; i<num_pixels; i++) {
402 long c = fgetc(fp);
403 if(c == -1) {
404 fprintf(stderr, "unexpected end of file while reading pixels\n");
405 goto err;
406 }
407 gmap->pixels[i] = 255 * c / max_pixval;
408 fseek(fp, 2, SEEK_CUR);
409 }
411 gmap->cstart = min_code;
412 gmap->cend = max_code + 1;
413 gmap->crange = gmap->cend - gmap->cstart;
415 if(!(gmap->glyphs = calloc(gmap->crange, sizeof *gmap->glyphs))) {
416 fperror("failed to allocate glyph info");
417 goto err;
418 }
420 while(glyphs) {
421 struct glyph *g = glyphs;
422 glyphs = glyphs->next;
424 gmap->glyphs[g->code - gmap->cstart] = *g;
425 free(g);
426 }
427 return gmap;
429 err:
430 dtx_free_glyphmap(gmap);
431 while(glyphs) {
432 void *tmp = glyphs;
433 glyphs = glyphs->next;
434 free(tmp);
435 }
436 return 0;
437 }
439 int dtx_save_glyphmap(const char *fname, const struct dtx_glyphmap *gmap)
440 {
441 FILE *fp;
442 int res;
444 if(!(fp = fopen(fname, "wb"))) {
445 fprintf(stderr, "%s: failed to open file: %s: %s\n", __FUNCTION__, fname, strerror(errno));
446 return -1;
447 }
448 res = dtx_save_glyphmap_stream(fp, gmap);
449 fclose(fp);
450 return res;
451 }
453 int dtx_save_glyphmap_stream(FILE *fp, const struct dtx_glyphmap *gmap)
454 {
455 int i, num_pixels;
456 struct glyph *g = gmap->glyphs;
458 fprintf(fp, "P6\n%d %d\n", gmap->xsz, gmap->ysz);
459 for(i=0; i<gmap->crange; i++) {
460 fprintf(fp, "# %d: %fx%f+%f+%f\n", g->code, g->width, g->height, g->x, g->y);
461 g++;
462 }
463 fprintf(fp, "255\n");
465 num_pixels = gmap->xsz * gmap->ysz;
466 for(i=0; i<num_pixels; i++) {
467 int c = gmap->pixels[i];
468 fputc(c, fp);
469 fputc(c, fp);
470 fputc(c, fp);
471 }
472 return 0;
473 }
476 void dtx_use_font(struct dtx_font *fnt, int sz)
477 {
478 dtx_gl_init();
480 dtx_font = fnt;
481 dtx_font_sz = sz;
482 }
484 float dtx_line_height(void)
485 {
486 struct dtx_glyphmap *gmap = dtx_get_font_glyphmap(dtx_font, dtx_font_sz, '\n');
488 return gmap->line_advance;
489 }
491 void dtx_glyph_box(int code, struct dtx_box *box)
492 {
493 int cidx;
494 struct dtx_glyphmap *gmap;
495 gmap = dtx_get_font_glyphmap(dtx_font, dtx_font_sz, code);
497 cidx = code - gmap->cstart;
499 box->x = gmap->glyphs[cidx].orig_x;
500 box->y = gmap->glyphs[cidx].orig_y;
501 box->width = gmap->glyphs[cidx].width;
502 box->height = gmap->glyphs[cidx].height;
503 }
505 float dtx_glyph_width(int code)
506 {
507 struct dtx_box box;
508 dtx_glyph_box(code, &box);
509 return box.width;
510 }
512 float dtx_glyph_height(int code)
513 {
514 struct dtx_box box;
515 dtx_glyph_box(code, &box);
516 return box.height;
517 }
519 void dtx_string_box(const char *str, struct dtx_box *box)
520 {
521 int code;
522 float pos_x = 0.0f, pos_y = 0.0f;
523 struct glyph *g = 0;
524 float x0, y0, x1, y1;
526 x0 = y0 = FLT_MAX;
527 x1 = y1 = -FLT_MAX;
529 while(*str) {
530 float px, py;
531 struct dtx_glyphmap *gmap;
533 code = dtx_utf8_char_code(str);
534 str = dtx_utf8_next_char((char*)str);
536 px = pos_x;
537 py = pos_y;
539 if((gmap = dtx_proc_char(code, &pos_x, &pos_y))) {
540 g = gmap->glyphs + code - gmap->cstart;
542 if(px + g->orig_x < x0) {
543 x0 = px + g->orig_x;
544 }
545 if(py - g->orig_y < y0) {
546 y0 = py - g->orig_y;
547 }
548 if(px + g->orig_x + g->width > x1) {
549 x1 = px + g->orig_x + g->width;
550 }
551 if(py - g->orig_y + g->height > y1) {
552 y1 = py - g->orig_y + g->height;
553 }
554 }
555 }
557 box->x = x0;
558 box->y = y0;
559 box->width = x1 - x0;
560 box->height = y1 - y0;
561 }
563 float dtx_string_width(const char *str)
564 {
565 struct dtx_box box;
567 dtx_string_box(str, &box);
568 return box.width;
569 }
571 float dtx_string_height(const char *str)
572 {
573 struct dtx_box box;
575 dtx_string_box(str, &box);
576 return box.height;
577 }
579 float dtx_char_pos(const char *str, int n)
580 {
581 int i;
582 float pos = 0.0;
583 struct dtx_glyphmap *gmap;
585 for(i=0; i<n; i++) {
586 int code = dtx_utf8_char_code(str);
587 str = dtx_utf8_next_char((char*)str);
589 gmap = dtx_get_font_glyphmap(dtx_font, dtx_font_sz, code);
590 pos += gmap->glyphs[i].advance;
591 }
592 return pos;
593 }
595 int dtx_char_at_pt(const char *str, float pt)
596 {
597 int i;
598 float prev_pos = 0.0f, pos = 0.0f;
599 struct dtx_glyphmap *gmap;
601 for(i=0; *str; i++) {
602 int code = dtx_utf8_char_code(str);
603 str = dtx_utf8_next_char((char*)str);
605 gmap = dtx_get_font_glyphmap(dtx_font, dtx_font_sz, code);
606 pos += gmap->glyphs[i].advance;
608 if(fabs(pt - prev_pos) < fabs(pt - pos)) {
609 break;
610 }
611 prev_pos = pos;
612 }
613 return i;
614 }
616 struct dtx_glyphmap *dtx_proc_char(int code, float *xpos, float *ypos)
617 {
618 struct dtx_glyphmap *gmap;
619 gmap = dtx_get_font_glyphmap(dtx_font, dtx_font_sz, code);
621 switch(code) {
622 case '\n':
623 *xpos = 0.0;
624 if(gmap) {
625 *ypos -= gmap->line_advance;
626 }
627 return 0;
629 case '\t':
630 if(gmap) {
631 *xpos = (fmod(*xpos, 4.0) + 4.0) * gmap->glyphs[0].advance;
632 }
633 return 0;
635 case '\r':
636 *xpos = 0.0;
637 return 0;
639 default:
640 break;
641 }
643 if(gmap) {
644 *xpos += gmap->glyphs[code - gmap->cstart].advance;
645 }
646 return gmap;
647 }
649 static void calc_best_size(int total_width, int max_glyph_height, int padding, int pow2, int *imgw, int *imgh)
650 {
651 int xsz, ysz, num_rows;
652 float aspect;
654 for(xsz=2; xsz<=MAX_IMG_WIDTH; xsz *= 2) {
655 num_rows = total_width / xsz + 1;
657 /* take into account the one extra padding for each row after the first */
658 num_rows = (total_width + padding * (num_rows - 1)) / xsz + 1;
660 ysz = num_rows * (max_glyph_height + padding) + padding;
661 if(pow2) {
662 ysz = next_pow2(ysz);
663 }
664 aspect = (float)xsz / (float)ysz;
666 if(aspect >= 1.0) {
667 break;
668 }
669 }
671 if(xsz > MAX_IMG_WIDTH) {
672 xsz = MAX_IMG_WIDTH;
673 }
675 *imgw = xsz;
676 *imgh = ysz;
677 }
680 static int next_pow2(int x)
681 {
682 x--;
683 x = (x >> 1) | x;
684 x = (x >> 2) | x;
685 x = (x >> 4) | x;
686 x = (x >> 8) | x;
687 x = (x >> 16) | x;
688 return x + 1;
689 }