libdrawtext

view src/font.c @ 59:e0957fc47162

added dtx_char_at_pt (not tested)
author John Tsiombikas <nuclear@mutantstargoat.com>
date Sun, 18 Sep 2011 01:30:30 +0300
parents 7e0c702f1223
children 365eba4dccec
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", __func__);
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", __func__);
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", __func__);
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", __func__);
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", __func__, 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 void dtx_glyph_box(int code, struct dtx_box *box)
481 {
482 int cidx;
483 struct dtx_glyphmap *gmap;
484 gmap = dtx_get_font_glyphmap(dtx_font, dtx_font_sz, code);
486 cidx = code - gmap->cstart;
488 box->x = gmap->glyphs[cidx].orig_x;
489 box->y = gmap->glyphs[cidx].orig_y;
490 box->width = gmap->glyphs[cidx].width;
491 box->height = gmap->glyphs[cidx].height;
492 }
494 float dtx_glyph_width(int code)
495 {
496 struct dtx_box box;
497 dtx_glyph_box(code, &box);
498 return box.width;
499 }
501 float dtx_glyph_height(int code)
502 {
503 struct dtx_box box;
504 dtx_glyph_box(code, &box);
505 return box.height;
506 }
508 void dtx_string_box(const char *str, struct dtx_box *box)
509 {
510 int code;
511 float pos_x = 0.0f, pos_y = 0.0f;
512 struct glyph *g = 0;
513 float x0, y0, x1, y1;
515 x0 = y0 = FLT_MAX;
516 x1 = y1 = -FLT_MAX;
518 while(*str) {
519 float px, py;
520 struct dtx_glyphmap *gmap;
522 code = dtx_utf8_char_code(str);
523 str = dtx_utf8_next_char((char*)str);
525 px = pos_x;
526 py = pos_y;
528 if((gmap = dtx_proc_char(code, &pos_x, &pos_y))) {
529 g = gmap->glyphs + code - gmap->cstart;
531 if(px + g->orig_x < x0) {
532 x0 = px + g->orig_x;
533 }
534 if(py - g->orig_y < y0) {
535 y0 = py - g->orig_y;
536 }
537 if(px + g->orig_x + g->width > x1) {
538 x1 = px + g->orig_x + g->width;
539 }
540 if(py - g->orig_y + g->height > y1) {
541 y1 = py - g->orig_y + g->height;
542 }
543 }
544 }
546 box->x = x0;
547 box->y = y0;
548 box->width = x1 - x0;
549 box->height = y1 - y0;
550 }
552 float dtx_string_width(const char *str)
553 {
554 struct dtx_box box;
556 dtx_string_box(str, &box);
557 return box.width;
558 }
560 float dtx_string_height(const char *str)
561 {
562 struct dtx_box box;
564 dtx_string_box(str, &box);
565 return box.height;
566 }
568 float dtx_char_pos(const char *str, int n)
569 {
570 int i;
571 float pos = 0.0;
572 struct dtx_glyphmap *gmap;
574 for(i=0; i<n; i++) {
575 int code = dtx_utf8_char_code(str);
576 str = dtx_utf8_next_char((char*)str);
578 gmap = dtx_get_font_glyphmap(dtx_font, dtx_font_sz, code);
579 pos += gmap->glyphs[i].advance;
580 }
581 return pos;
582 }
584 int dtx_char_at_pt(const char *str, float pt)
585 {
586 int i;
587 float prev_pos = 0.0f, pos = 0.0f;
588 struct dtx_glyphmap *gmap;
590 for(i=0; *str; i++) {
591 int code = dtx_utf8_char_code(str);
592 str = dtx_utf8_next_char((char*)str);
594 gmap = dtx_get_font_glyphmap(dtx_font, dtx_font_sz, code);
595 pos += gmap->glyphs[i].advance;
597 if(fabs(pt - prev_pos) < fabs(pt - pos)) {
598 break;
599 }
600 prev_pos = pos;
601 }
602 return i;
603 }
605 struct dtx_glyphmap *dtx_proc_char(int code, float *xpos, float *ypos)
606 {
607 struct dtx_glyphmap *gmap;
608 gmap = dtx_get_font_glyphmap(dtx_font, dtx_font_sz, code);
610 switch(code) {
611 case '\n':
612 *xpos = 0.0;
613 if(gmap) {
614 *ypos -= gmap->line_advance;
615 }
616 return 0;
618 case '\t':
619 if(gmap) {
620 *xpos = (fmod(*xpos, 4.0) + 4.0) * gmap->glyphs[0].advance;
621 }
622 return 0;
624 case '\r':
625 *xpos = 0.0;
626 return 0;
628 default:
629 break;
630 }
632 if(gmap) {
633 *xpos += gmap->glyphs[code - gmap->cstart].advance;
634 }
635 return gmap;
636 }
638 static void calc_best_size(int total_width, int max_glyph_height, int padding, int pow2, int *imgw, int *imgh)
639 {
640 int xsz, ysz, num_rows;
641 float aspect;
643 for(xsz=2; xsz<=MAX_IMG_WIDTH; xsz *= 2) {
644 num_rows = total_width / xsz + 1;
646 /* take into account the one extra padding for each row after the first */
647 num_rows = (total_width + padding * (num_rows - 1)) / xsz + 1;
649 ysz = num_rows * (max_glyph_height + padding) + padding;
650 if(pow2) {
651 ysz = next_pow2(ysz);
652 }
653 aspect = (float)xsz / (float)ysz;
655 if(aspect >= 1.0) {
656 break;
657 }
658 }
660 if(xsz > MAX_IMG_WIDTH) {
661 xsz = MAX_IMG_WIDTH;
662 }
664 *imgw = xsz;
665 *imgh = ysz;
666 }
669 static int next_pow2(int x)
670 {
671 x--;
672 x = (x >> 1) | x;
673 x = (x >> 2) | x;
674 x = (x >> 4) | x;
675 x = (x >> 8) | x;
676 x = (x >> 16) | x;
677 return x + 1;
678 }