vrshoot
view libs/drawtext/font.c @ 2:334d17aed7de
visual studio project files
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Sun, 02 Feb 2014 18:36:38 +0200 |
parents | |
children |
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);
190 gmap->baseline = -FTSZ_TO_PIXELS((float)face->size->metrics.descender);
192 if(!(gmap->glyphs = malloc(gmap->crange * sizeof *gmap->glyphs))) {
193 free(gmap);
194 return 0;
195 }
197 for(i=0; i<gmap->crange; i++) {
198 int h;
200 FT_Load_Char(face, i + cstart, 0);
201 h = FTSZ_TO_PIXELS(face->glyph->metrics.height);
203 if(h > max_height) {
204 max_height = h;
205 }
206 total_width += FTSZ_TO_PIXELS(face->glyph->metrics.width) + padding;
207 }
209 calc_best_size(total_width, max_height, padding, 1, &gmap->xsz, &gmap->ysz);
211 if(!(gmap->pixels = malloc(gmap->xsz * gmap->ysz))) {
212 free(gmap->glyphs);
213 free(gmap);
214 return 0;
215 }
216 memset(gmap->pixels, 0, gmap->xsz * gmap->ysz);
218 gx = padding;
219 gy = padding;
221 for(i=0; i<gmap->crange; i++) {
222 float gwidth, gheight;
223 unsigned char *src, *dst;
224 FT_GlyphSlot glyph;
226 FT_Load_Char(face, i + cstart, FT_LOAD_RENDER);
227 glyph = face->glyph;
228 gwidth = FTSZ_TO_PIXELS((float)glyph->metrics.width);
229 gheight = FTSZ_TO_PIXELS((float)glyph->metrics.height);
231 if(gx > gmap->xsz - gwidth - padding) {
232 gx = padding;
233 gy += max_height + padding;
234 }
236 src = glyph->bitmap.buffer;
237 dst = gmap->pixels + gy * gmap->xsz + gx;
239 for(j=0; j<glyph->bitmap.rows; j++) {
240 memcpy(dst, src, glyph->bitmap.width);
241 dst += gmap->xsz;
242 src += glyph->bitmap.pitch;
243 }
245 gmap->glyphs[i].code = i;
246 gmap->glyphs[i].x = gx - 1;
247 gmap->glyphs[i].y = gy - 1;
248 gmap->glyphs[i].width = gwidth + 2;
249 gmap->glyphs[i].height = gheight + 2;
250 gmap->glyphs[i].orig_x = -FTSZ_TO_PIXELS((float)glyph->metrics.horiBearingX) + 1;
251 gmap->glyphs[i].orig_y = FTSZ_TO_PIXELS((float)glyph->metrics.height - glyph->metrics.horiBearingY) + 1;
252 gmap->glyphs[i].advance = FTSZ_TO_PIXELS((float)glyph->metrics.horiAdvance);
253 /* also precalc normalized */
254 gmap->glyphs[i].nx = (float)gmap->glyphs[i].x / (float)gmap->xsz;
255 gmap->glyphs[i].ny = (float)gmap->glyphs[i].y / (float)gmap->ysz;
256 gmap->glyphs[i].nwidth = (float)gmap->glyphs[i].width / (float)gmap->xsz;
257 gmap->glyphs[i].nheight = (float)gmap->glyphs[i].height / (float)gmap->ysz;
259 gx += gwidth + padding;
260 }
262 /* add it to the glyphmaps list of the font */
263 gmap->next = fnt->gmaps;
264 fnt->gmaps = gmap;
266 return gmap;
267 }
268 #endif /* USE_FREETYPE */
270 void dtx_free_glyphmap(struct dtx_glyphmap *gmap)
271 {
272 if(gmap) {
273 free(gmap->pixels);
274 free(gmap->glyphs);
275 free(gmap);
276 }
277 }
279 unsigned char *dtx_get_glyphmap_pixels(struct dtx_glyphmap *gmap)
280 {
281 return gmap->pixels;
282 }
284 int dtx_get_glyphmap_width(struct dtx_glyphmap *gmap)
285 {
286 return gmap->xsz;
287 }
289 int dtx_get_glyphmap_height(struct dtx_glyphmap *gmap)
290 {
291 return gmap->ysz;
292 }
294 struct dtx_glyphmap *dtx_load_glyphmap(const char *fname)
295 {
296 FILE *fp;
297 struct dtx_glyphmap *gmap;
299 if(!(fp = fopen(fname, "r"))) {
300 return 0;
301 }
302 gmap = dtx_load_glyphmap_stream(fp);
303 fclose(fp);
304 return gmap;
305 }
307 struct dtx_glyphmap *dtx_load_glyphmap_stream(FILE *fp)
308 {
309 char buf[512];
310 int hdr_lines = 0;
311 struct dtx_glyphmap *gmap;
312 struct glyph *glyphs = 0;
313 int min_code = INT_MAX;
314 int max_code = INT_MIN;
315 int i, max_pixval, num_pixels;
317 if(!(gmap = calloc(1, sizeof *gmap))) {
318 fperror("failed to allocate glyphmap");
319 return 0;
320 }
322 while(hdr_lines < 3) {
323 char *line = buf;
324 if(!fgets(buf, sizeof buf, fp)) {
325 fperror("unexpected end of file");
326 goto err;
327 }
329 while(isspace(*line)) {
330 line++;
331 }
333 if(line[0] == '#') {
334 struct glyph *g;
335 int c;
336 float x, y, xsz, ysz, res;
338 res = sscanf(line + 1, "%d: %fx%f+%f+%f\n", &c, &xsz, &ysz, &x, &y);
339 if(res != 5) {
340 fprintf(stderr, "%s: invalid glyph info line\n", __FUNCTION__);
341 goto err;
342 }
344 if(!(g = malloc(sizeof *g))) {
345 fperror("failed to allocate glyph");
346 goto err;
347 }
348 g->code = c;
349 g->x = x;
350 g->y = y;
351 g->width = xsz;
352 g->height = ysz;
353 g->next = glyphs;
354 glyphs = g;
356 if(c < min_code) {
357 min_code = c;
358 }
359 if(c > max_code) {
360 max_code = c;
361 }
362 } else {
363 switch(hdr_lines) {
364 case 0:
365 if(0[line] != 'P' || 1[line] != '6') {
366 fprintf(stderr, "%s: invalid file format (magic)\n", __FUNCTION__);
367 goto err;
368 }
369 break;
371 case 1:
372 if(sscanf(line, "%d %d", &gmap->xsz, &gmap->ysz) != 2) {
373 fprintf(stderr, "%s: invalid file format (dim)\n", __FUNCTION__);
374 goto err;
375 }
376 break;
378 case 2:
379 {
380 char *endp;
381 max_pixval = strtol(line, &endp, 10);
382 if(endp == line) {
383 fprintf(stderr, "%s: invalid file format (maxval)\n", __FUNCTION__);
384 goto err;
385 }
386 }
387 break;
389 default:
390 break; /* can't happen */
391 }
392 hdr_lines++;
393 }
394 }
396 num_pixels = gmap->xsz * gmap->ysz;
397 if(!(gmap->pixels = malloc(num_pixels))) {
398 fperror("failed to allocate pixels");
399 goto err;
400 }
402 for(i=0; i<num_pixels; i++) {
403 long c = fgetc(fp);
404 if(c == -1) {
405 fprintf(stderr, "unexpected end of file while reading pixels\n");
406 goto err;
407 }
408 gmap->pixels[i] = 255 * c / max_pixval;
409 fseek(fp, 2, SEEK_CUR);
410 }
412 gmap->cstart = min_code;
413 gmap->cend = max_code + 1;
414 gmap->crange = gmap->cend - gmap->cstart;
416 if(!(gmap->glyphs = calloc(gmap->crange, sizeof *gmap->glyphs))) {
417 fperror("failed to allocate glyph info");
418 goto err;
419 }
421 while(glyphs) {
422 struct glyph *g = glyphs;
423 glyphs = glyphs->next;
425 gmap->glyphs[g->code - gmap->cstart] = *g;
426 free(g);
427 }
428 return gmap;
430 err:
431 dtx_free_glyphmap(gmap);
432 while(glyphs) {
433 void *tmp = glyphs;
434 glyphs = glyphs->next;
435 free(tmp);
436 }
437 return 0;
438 }
440 int dtx_save_glyphmap(const char *fname, const struct dtx_glyphmap *gmap)
441 {
442 FILE *fp;
443 int res;
445 if(!(fp = fopen(fname, "wb"))) {
446 fprintf(stderr, "%s: failed to open file: %s: %s\n", __FUNCTION__, fname, strerror(errno));
447 return -1;
448 }
449 res = dtx_save_glyphmap_stream(fp, gmap);
450 fclose(fp);
451 return res;
452 }
454 int dtx_save_glyphmap_stream(FILE *fp, const struct dtx_glyphmap *gmap)
455 {
456 int i, num_pixels;
457 struct glyph *g = gmap->glyphs;
459 fprintf(fp, "P6\n%d %d\n", gmap->xsz, gmap->ysz);
460 for(i=0; i<gmap->crange; i++) {
461 fprintf(fp, "# %d: %fx%f+%f+%f\n", g->code, g->width, g->height, g->x, g->y);
462 g++;
463 }
464 fprintf(fp, "255\n");
466 num_pixels = gmap->xsz * gmap->ysz;
467 for(i=0; i<num_pixels; i++) {
468 int c = gmap->pixels[i];
469 fputc(c, fp);
470 fputc(c, fp);
471 fputc(c, fp);
472 }
473 return 0;
474 }
477 void dtx_use_font(struct dtx_font *fnt, int sz)
478 {
479 dtx_gl_init();
481 dtx_font = fnt;
482 dtx_font_sz = sz;
483 }
485 float dtx_line_height(void)
486 {
487 struct dtx_glyphmap *gmap = dtx_get_font_glyphmap(dtx_font, dtx_font_sz, '\n');
489 return gmap->line_advance;
490 }
492 float dtx_baseline(void)
493 {
494 struct dtx_glyphmap *gmap = dtx_get_font_glyphmap(dtx_font, dtx_font_sz, '\n');
496 return gmap->baseline;
497 }
499 void dtx_glyph_box(int code, struct dtx_box *box)
500 {
501 int cidx;
502 struct dtx_glyphmap *gmap;
503 gmap = dtx_get_font_glyphmap(dtx_font, dtx_font_sz, code);
505 cidx = code - gmap->cstart;
507 box->x = gmap->glyphs[cidx].orig_x;
508 box->y = gmap->glyphs[cidx].orig_y;
509 box->width = gmap->glyphs[cidx].width;
510 box->height = gmap->glyphs[cidx].height;
511 }
513 float dtx_glyph_width(int code)
514 {
515 struct dtx_box box;
516 dtx_glyph_box(code, &box);
517 return box.width;
518 }
520 float dtx_glyph_height(int code)
521 {
522 struct dtx_box box;
523 dtx_glyph_box(code, &box);
524 return box.height;
525 }
527 void dtx_string_box(const char *str, struct dtx_box *box)
528 {
529 int code;
530 float pos_x = 0.0f, pos_y = 0.0f;
531 struct glyph *g = 0;
532 float x0, y0, x1, y1;
534 x0 = y0 = FLT_MAX;
535 x1 = y1 = -FLT_MAX;
537 while(*str) {
538 float px, py;
539 struct dtx_glyphmap *gmap;
541 code = dtx_utf8_char_code(str);
542 str = dtx_utf8_next_char((char*)str);
544 px = pos_x;
545 py = pos_y;
547 if((gmap = dtx_proc_char(code, &pos_x, &pos_y))) {
548 g = gmap->glyphs + code - gmap->cstart;
550 if(px + g->orig_x < x0) {
551 x0 = px + g->orig_x;
552 }
553 if(py - g->orig_y < y0) {
554 y0 = py - g->orig_y;
555 }
556 if(px + g->orig_x + g->width > x1) {
557 x1 = px + g->orig_x + g->width;
558 }
559 if(py - g->orig_y + g->height > y1) {
560 y1 = py - g->orig_y + g->height;
561 }
562 }
563 }
565 box->x = x0;
566 box->y = y0;
567 box->width = x1 - x0;
568 box->height = y1 - y0;
569 }
571 float dtx_string_width(const char *str)
572 {
573 struct dtx_box box;
575 dtx_string_box(str, &box);
576 return box.width;
577 }
579 float dtx_string_height(const char *str)
580 {
581 struct dtx_box box;
583 dtx_string_box(str, &box);
584 return box.height;
585 }
587 float dtx_char_pos(const char *str, int n)
588 {
589 int i;
590 float pos = 0.0;
591 struct dtx_glyphmap *gmap;
593 for(i=0; i<n; i++) {
594 int code = dtx_utf8_char_code(str);
595 str = dtx_utf8_next_char((char*)str);
597 gmap = dtx_get_font_glyphmap(dtx_font, dtx_font_sz, code);
598 pos += gmap->glyphs[i].advance;
599 }
600 return pos;
601 }
603 int dtx_char_at_pt(const char *str, float pt)
604 {
605 int i;
606 float prev_pos = 0.0f, pos = 0.0f;
607 struct dtx_glyphmap *gmap;
609 for(i=0; *str; i++) {
610 int code = dtx_utf8_char_code(str);
611 str = dtx_utf8_next_char((char*)str);
613 gmap = dtx_get_font_glyphmap(dtx_font, dtx_font_sz, code);
614 pos += gmap->glyphs[i].advance;
616 if(fabs(pt - prev_pos) < fabs(pt - pos)) {
617 break;
618 }
619 prev_pos = pos;
620 }
621 return i;
622 }
624 struct dtx_glyphmap *dtx_proc_char(int code, float *xpos, float *ypos)
625 {
626 struct dtx_glyphmap *gmap;
627 gmap = dtx_get_font_glyphmap(dtx_font, dtx_font_sz, code);
629 switch(code) {
630 case '\n':
631 *xpos = 0.0;
632 if(gmap) {
633 *ypos -= gmap->line_advance;
634 }
635 return 0;
637 case '\t':
638 if(gmap) {
639 *xpos = (fmod(*xpos, 4.0) + 4.0) * gmap->glyphs[0].advance;
640 }
641 return 0;
643 case '\r':
644 *xpos = 0.0;
645 return 0;
647 default:
648 break;
649 }
651 if(gmap) {
652 *xpos += gmap->glyphs[code - gmap->cstart].advance;
653 }
654 return gmap;
655 }
657 static void calc_best_size(int total_width, int max_glyph_height, int padding, int pow2, int *imgw, int *imgh)
658 {
659 int xsz, ysz, num_rows;
660 float aspect;
662 for(xsz=2; xsz<=MAX_IMG_WIDTH; xsz *= 2) {
663 num_rows = total_width / xsz + 1;
665 /* take into account the one extra padding for each row after the first */
666 num_rows = (total_width + padding * (num_rows - 1)) / xsz + 1;
668 ysz = num_rows * (max_glyph_height + padding) + padding;
669 if(pow2) {
670 ysz = next_pow2(ysz);
671 }
672 aspect = (float)xsz / (float)ysz;
674 if(aspect >= 1.0) {
675 break;
676 }
677 }
679 if(xsz > MAX_IMG_WIDTH) {
680 xsz = MAX_IMG_WIDTH;
681 }
683 *imgw = xsz;
684 *imgh = ysz;
685 }
688 static int next_pow2(int x)
689 {
690 x--;
691 x = (x >> 1) | x;
692 x = (x >> 2) | x;
693 x = (x >> 4) | x;
694 x = (x >> 8) | x;
695 x = (x >> 16) | x;
696 return x + 1;
697 }