dungeon_crawler

view prototype/drawtext/font.c @ 66:6a471c87f9ca

- added visual studio project files - changed some __func__ into __FUNCTION__ to compile on visual studio
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 07 Oct 2012 00:19:48 +0200
parents 2fc004802739
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);
91 }
93 return fnt;
94 }
96 void dtx_close_font(struct dtx_font *fnt)
97 {
98 if(!fnt) return;
100 FT_Done_Face(fnt->face);
102 /* destroy the glyphmaps */
103 while(fnt->gmaps) {
104 void *tmp = fnt->gmaps;
105 fnt->gmaps = fnt->gmaps->next;
106 dtx_free_glyphmap(tmp);
107 }
109 free(fnt);
110 }
112 void dtx_prepare(struct dtx_font *fnt, int sz)
113 {
114 dtx_get_font_glyphmap_range(fnt, sz, 0, 256);
115 }
117 void dtx_prepare_range(struct dtx_font *fnt, int sz, int cstart, int cend)
118 {
119 dtx_get_font_glyphmap_range(fnt, sz, cstart, cend);
120 }
122 struct dtx_glyphmap *dtx_get_font_glyphmap(struct dtx_font *fnt, int sz, int code)
123 {
124 struct dtx_glyphmap *gm;
126 /* check to see if the last we've given out fits the bill */
127 if(fnt->last_gmap && code >= fnt->last_gmap->cstart && code < fnt->last_gmap->cend && fnt->last_gmap->ptsize == sz) {
128 return fnt->last_gmap;
129 }
131 /* otherwise search for the appropriate glyphmap */
132 gm = fnt->gmaps;
133 while(gm) {
134 if(code >= gm->cstart && code < gm->cend && sz == gm->ptsize) {
135 fnt->last_gmap = gm;
136 return gm;
137 }
138 gm = gm->next;
139 }
140 return 0;
141 }
143 struct dtx_glyphmap *dtx_get_font_glyphmap_range(struct dtx_font *fnt, int sz, int cstart, int cend)
144 {
145 struct dtx_glyphmap *gm;
147 /* search the available glyphmaps to see if we've got one that includes
148 * the requested range
149 */
150 gm = fnt->gmaps;
151 while(gm) {
152 if(gm->cstart <= cstart && gm->cend >= cend && gm->ptsize == sz) {
153 return gm;
154 }
155 gm = gm->next;
156 }
158 /* not found, create one and add it to the list */
159 if(!(gm = dtx_create_glyphmap_range(fnt, sz, cstart, cend))) {
160 return 0;
161 }
162 return gm;
163 }
165 struct dtx_glyphmap *dtx_create_glyphmap_range(struct dtx_font *fnt, int sz, int cstart, int cend)
166 {
167 FT_Face face = fnt->face;
168 struct dtx_glyphmap *gmap;
169 int i, j;
170 int gx, gy;
171 int padding = 4;
172 int total_width = padding;
173 int max_height = 0;
175 FT_Set_Char_Size(fnt->face, 0, sz * 64, 72, 72);
177 if(!(gmap = calloc(1, sizeof *gmap))) {
178 return 0;
179 }
181 gmap->ptsize = sz;
182 gmap->cstart = cstart;
183 gmap->cend = cend;
184 gmap->crange = cend - cstart;
185 gmap->line_advance = FTSZ_TO_PIXELS((float)face->size->metrics.height);
187 if(!(gmap->glyphs = malloc(gmap->crange * sizeof *gmap->glyphs))) {
188 free(gmap);
189 return 0;
190 }
192 for(i=0; i<gmap->crange; i++) {
193 int h;
195 FT_Load_Char(face, i + cstart, 0);
196 h = FTSZ_TO_PIXELS(face->glyph->metrics.height);
198 if(h > max_height) {
199 max_height = h;
200 }
201 total_width += FTSZ_TO_PIXELS(face->glyph->metrics.width) + padding;
202 }
204 calc_best_size(total_width, max_height, padding, 1, &gmap->xsz, &gmap->ysz);
206 if(!(gmap->pixels = malloc(gmap->xsz * gmap->ysz))) {
207 free(gmap->glyphs);
208 free(gmap);
209 return 0;
210 }
211 memset(gmap->pixels, 0, gmap->xsz * gmap->ysz);
213 gx = padding;
214 gy = padding;
216 for(i=0; i<gmap->crange; i++) {
217 float gwidth, gheight;
218 unsigned char *src, *dst;
219 FT_GlyphSlot glyph;
221 FT_Load_Char(face, i + cstart, FT_LOAD_RENDER);
222 glyph = face->glyph;
223 gwidth = FTSZ_TO_PIXELS((float)glyph->metrics.width);
224 gheight = FTSZ_TO_PIXELS((float)glyph->metrics.height);
226 if(gx > gmap->xsz - gwidth - padding) {
227 gx = padding;
228 gy += max_height + padding;
229 }
231 src = glyph->bitmap.buffer;
232 dst = gmap->pixels + gy * gmap->xsz + gx;
234 for(j=0; j<glyph->bitmap.rows; j++) {
235 memcpy(dst, src, glyph->bitmap.width);
236 dst += gmap->xsz;
237 src += glyph->bitmap.pitch;
238 }
240 gmap->glyphs[i].code = i;
241 gmap->glyphs[i].x = gx - 1;
242 gmap->glyphs[i].y = gy - 1;
243 gmap->glyphs[i].width = gwidth + 2;
244 gmap->glyphs[i].height = gheight + 2;
245 gmap->glyphs[i].orig_x = -FTSZ_TO_PIXELS((float)glyph->metrics.horiBearingX) + 1;
246 gmap->glyphs[i].orig_y = FTSZ_TO_PIXELS((float)glyph->metrics.height - glyph->metrics.horiBearingY) + 1;
247 gmap->glyphs[i].advance = FTSZ_TO_PIXELS((float)glyph->metrics.horiAdvance);
248 /* also precalc normalized */
249 gmap->glyphs[i].nx = (float)gmap->glyphs[i].x / (float)gmap->xsz;
250 gmap->glyphs[i].ny = (float)gmap->glyphs[i].y / (float)gmap->ysz;
251 gmap->glyphs[i].nwidth = (float)gmap->glyphs[i].width / (float)gmap->xsz;
252 gmap->glyphs[i].nheight = (float)gmap->glyphs[i].height / (float)gmap->ysz;
254 gx += gwidth + padding;
255 }
257 /* add it to the glyphmaps list of the font */
258 gmap->next = fnt->gmaps;
259 fnt->gmaps = gmap;
261 return gmap;
262 }
263 #endif /* USE_FREETYPE */
265 void dtx_free_glyphmap(struct dtx_glyphmap *gmap)
266 {
267 if(gmap) {
268 free(gmap->pixels);
269 free(gmap->glyphs);
270 free(gmap);
271 }
272 }
274 unsigned char *dtx_get_glyphmap_pixels(struct dtx_glyphmap *gmap)
275 {
276 return gmap->pixels;
277 }
279 int dtx_get_glyphmap_width(struct dtx_glyphmap *gmap)
280 {
281 return gmap->xsz;
282 }
284 int dtx_get_glyphmap_height(struct dtx_glyphmap *gmap)
285 {
286 return gmap->ysz;
287 }
289 struct dtx_glyphmap *dtx_load_glyphmap(const char *fname)
290 {
291 FILE *fp;
292 struct dtx_glyphmap *gmap;
294 if(!(fp = fopen(fname, "r"))) {
295 return 0;
296 }
297 gmap = dtx_load_glyphmap_stream(fp);
298 fclose(fp);
299 return gmap;
300 }
302 struct dtx_glyphmap *dtx_load_glyphmap_stream(FILE *fp)
303 {
304 char buf[512];
305 int hdr_lines = 0;
306 struct dtx_glyphmap *gmap;
307 struct glyph *glyphs = 0;
308 int min_code = INT_MAX;
309 int max_code = INT_MIN;
310 int i, max_pixval, num_pixels;
312 if(!(gmap = calloc(1, sizeof *gmap))) {
313 fperror("failed to allocate glyphmap");
314 return 0;
315 }
317 while(hdr_lines < 3) {
318 char *line = buf;
319 if(!fgets(buf, sizeof buf, fp)) {
320 fperror("unexpected end of file");
321 goto err;
322 }
324 while(isspace(*line)) {
325 line++;
326 }
328 if(line[0] == '#') {
329 struct glyph *g;
330 int c;
331 float x, y, xsz, ysz, res;
333 res = sscanf(line + 1, "%d: %fx%f+%f+%f\n", &c, &xsz, &ysz, &x, &y);
334 if(res != 5) {
335 fprintf(stderr, "%s: invalid glyph info line\n", __FUNCTION__);
336 goto err;
337 }
339 if(!(g = malloc(sizeof *g))) {
340 fperror("failed to allocate glyph");
341 goto err;
342 }
343 g->code = c;
344 g->x = x;
345 g->y = y;
346 g->width = xsz;
347 g->height = ysz;
348 g->next = glyphs;
349 glyphs = g;
351 if(c < min_code) {
352 min_code = c;
353 }
354 if(c > max_code) {
355 max_code = c;
356 }
357 } else {
358 switch(hdr_lines) {
359 case 0:
360 if(0[line] != 'P' || 1[line] != '6') {
361 fprintf(stderr, "%s: invalid file format (magic)\n", __FUNCTION__);
362 goto err;
363 }
364 break;
366 case 1:
367 if(sscanf(line, "%d %d", &gmap->xsz, &gmap->ysz) != 2) {
368 fprintf(stderr, "%s: invalid file format (dim)\n", __FUNCTION__);
369 goto err;
370 }
371 break;
373 case 2:
374 {
375 char *endp;
376 max_pixval = strtol(line, &endp, 10);
377 if(endp == line) {
378 fprintf(stderr, "%s: invalid file format (maxval)\n", __FUNCTION__);
379 goto err;
380 }
381 }
382 break;
384 default:
385 break; /* can't happen */
386 }
387 hdr_lines++;
388 }
389 }
391 num_pixels = gmap->xsz * gmap->ysz;
392 if(!(gmap->pixels = malloc(num_pixels))) {
393 fperror("failed to allocate pixels");
394 goto err;
395 }
397 for(i=0; i<num_pixels; i++) {
398 long c = fgetc(fp);
399 if(c == -1) {
400 fprintf(stderr, "unexpected end of file while reading pixels\n");
401 goto err;
402 }
403 gmap->pixels[i] = 255 * c / max_pixval;
404 fseek(fp, 2, SEEK_CUR);
405 }
407 gmap->cstart = min_code;
408 gmap->cend = max_code + 1;
409 gmap->crange = gmap->cend - gmap->cstart;
411 if(!(gmap->glyphs = calloc(gmap->crange, sizeof *gmap->glyphs))) {
412 fperror("failed to allocate glyph info");
413 goto err;
414 }
416 while(glyphs) {
417 struct glyph *g = glyphs;
418 glyphs = glyphs->next;
420 gmap->glyphs[g->code - gmap->cstart] = *g;
421 free(g);
422 }
423 return gmap;
425 err:
426 dtx_free_glyphmap(gmap);
427 while(glyphs) {
428 void *tmp = glyphs;
429 glyphs = glyphs->next;
430 free(tmp);
431 }
432 return 0;
433 }
435 int dtx_save_glyphmap(const char *fname, const struct dtx_glyphmap *gmap)
436 {
437 FILE *fp;
438 int res;
440 if(!(fp = fopen(fname, "wb"))) {
441 fprintf(stderr, "%s: failed to open file: %s: %s\n", __FUNCTION__, fname, strerror(errno));
442 return -1;
443 }
444 res = dtx_save_glyphmap_stream(fp, gmap);
445 fclose(fp);
446 return res;
447 }
449 int dtx_save_glyphmap_stream(FILE *fp, const struct dtx_glyphmap *gmap)
450 {
451 int i, num_pixels;
452 struct glyph *g = gmap->glyphs;
454 fprintf(fp, "P6\n%d %d\n", gmap->xsz, gmap->ysz);
455 for(i=0; i<gmap->crange; i++) {
456 fprintf(fp, "# %d: %fx%f+%f+%f\n", g->code, g->width, g->height, g->x, g->y);
457 g++;
458 }
459 fprintf(fp, "255\n");
461 num_pixels = gmap->xsz * gmap->ysz;
462 for(i=0; i<num_pixels; i++) {
463 int c = gmap->pixels[i];
464 fputc(c, fp);
465 fputc(c, fp);
466 fputc(c, fp);
467 }
468 return 0;
469 }
472 void dtx_use_font(struct dtx_font *fnt, int sz)
473 {
474 dtx_gl_init();
476 dtx_font = fnt;
477 dtx_font_sz = sz;
478 }
480 float dtx_line_height(void)
481 {
482 struct dtx_glyphmap *gmap = dtx_get_font_glyphmap(dtx_font, dtx_font_sz, '\n');
484 return gmap->line_advance;
485 }
487 void dtx_glyph_box(int code, struct dtx_box *box)
488 {
489 int cidx;
490 struct dtx_glyphmap *gmap;
491 gmap = dtx_get_font_glyphmap(dtx_font, dtx_font_sz, code);
493 cidx = code - gmap->cstart;
495 box->x = gmap->glyphs[cidx].orig_x;
496 box->y = gmap->glyphs[cidx].orig_y;
497 box->width = gmap->glyphs[cidx].width;
498 box->height = gmap->glyphs[cidx].height;
499 }
501 float dtx_glyph_width(int code)
502 {
503 struct dtx_box box;
504 dtx_glyph_box(code, &box);
505 return box.width;
506 }
508 float dtx_glyph_height(int code)
509 {
510 struct dtx_box box;
511 dtx_glyph_box(code, &box);
512 return box.height;
513 }
515 void dtx_string_box(const char *str, struct dtx_box *box)
516 {
517 int code;
518 float pos_x = 0.0f, pos_y = 0.0f;
519 struct glyph *g = 0;
520 float x0, y0, x1, y1;
522 x0 = y0 = FLT_MAX;
523 x1 = y1 = -FLT_MAX;
525 while(*str) {
526 float px, py;
527 struct dtx_glyphmap *gmap;
529 code = dtx_utf8_char_code(str);
530 str = dtx_utf8_next_char((char*)str);
532 px = pos_x;
533 py = pos_y;
535 if((gmap = dtx_proc_char(code, &pos_x, &pos_y))) {
536 g = gmap->glyphs + code - gmap->cstart;
538 if(px + g->orig_x < x0) {
539 x0 = px + g->orig_x;
540 }
541 if(py - g->orig_y < y0) {
542 y0 = py - g->orig_y;
543 }
544 if(px + g->orig_x + g->width > x1) {
545 x1 = px + g->orig_x + g->width;
546 }
547 if(py - g->orig_y + g->height > y1) {
548 y1 = py - g->orig_y + g->height;
549 }
550 }
551 }
553 box->x = x0;
554 box->y = y0;
555 box->width = x1 - x0;
556 box->height = y1 - y0;
557 }
559 float dtx_string_width(const char *str)
560 {
561 struct dtx_box box;
563 dtx_string_box(str, &box);
564 return box.width;
565 }
567 float dtx_string_height(const char *str)
568 {
569 struct dtx_box box;
571 dtx_string_box(str, &box);
572 return box.height;
573 }
575 float dtx_char_pos(const char *str, int n)
576 {
577 int i;
578 float pos = 0.0;
579 struct dtx_glyphmap *gmap;
581 for(i=0; i<n; i++) {
582 int code = dtx_utf8_char_code(str);
583 str = dtx_utf8_next_char((char*)str);
585 gmap = dtx_get_font_glyphmap(dtx_font, dtx_font_sz, code);
586 pos += gmap->glyphs[i].advance;
587 }
588 return pos;
589 }
591 int dtx_char_at_pt(const char *str, float pt)
592 {
593 int i;
594 float prev_pos = 0.0f, pos = 0.0f;
595 struct dtx_glyphmap *gmap;
597 for(i=0; *str; i++) {
598 int code = dtx_utf8_char_code(str);
599 str = dtx_utf8_next_char((char*)str);
601 gmap = dtx_get_font_glyphmap(dtx_font, dtx_font_sz, code);
602 pos += gmap->glyphs[i].advance;
604 if(fabs(pt - prev_pos) < fabs(pt - pos)) {
605 break;
606 }
607 prev_pos = pos;
608 }
609 return i;
610 }
612 struct dtx_glyphmap *dtx_proc_char(int code, float *xpos, float *ypos)
613 {
614 struct dtx_glyphmap *gmap;
615 gmap = dtx_get_font_glyphmap(dtx_font, dtx_font_sz, code);
617 switch(code) {
618 case '\n':
619 *xpos = 0.0;
620 if(gmap) {
621 *ypos -= gmap->line_advance;
622 }
623 return 0;
625 case '\t':
626 if(gmap) {
627 *xpos = (fmod(*xpos, 4.0) + 4.0) * gmap->glyphs[0].advance;
628 }
629 return 0;
631 case '\r':
632 *xpos = 0.0;
633 return 0;
635 default:
636 break;
637 }
639 if(gmap) {
640 *xpos += gmap->glyphs[code - gmap->cstart].advance;
641 }
642 return gmap;
643 }
645 static void calc_best_size(int total_width, int max_glyph_height, int padding, int pow2, int *imgw, int *imgh)
646 {
647 int xsz, ysz, num_rows;
648 float aspect;
650 for(xsz=2; xsz<=MAX_IMG_WIDTH; xsz *= 2) {
651 num_rows = total_width / xsz + 1;
653 /* take into account the one extra padding for each row after the first */
654 num_rows = (total_width + padding * (num_rows - 1)) / xsz + 1;
656 ysz = num_rows * (max_glyph_height + padding) + padding;
657 if(pow2) {
658 ysz = next_pow2(ysz);
659 }
660 aspect = (float)xsz / (float)ysz;
662 if(aspect >= 1.0) {
663 break;
664 }
665 }
667 if(xsz > MAX_IMG_WIDTH) {
668 xsz = MAX_IMG_WIDTH;
669 }
671 *imgw = xsz;
672 *imgh = ysz;
673 }
676 static int next_pow2(int x)
677 {
678 x--;
679 x = (x >> 1) | x;
680 x = (x >> 2) | x;
681 x = (x >> 4) | x;
682 x = (x >> 8) | x;
683 x = (x >> 16) | x;
684 return x + 1;
685 }