3dphotoshoot

view libs/drawtext/font.c @ 22:d7fe157c402d

fonts
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 13 Jun 2015 05:32:07 +0300
parents c71c477521ca
children 3d082c566b53
line source
1 /*
2 libdrawtext - a simple library for fast text rendering in OpenGL
3 Copyright (C) 2011-2014 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 "assman.h"
35 #include "drawtext.h"
36 #include "drawtext_impl.h"
38 #define FTSZ_TO_PIXELS(x) ((x) / 64)
39 #define MAX_IMG_WIDTH 4096
42 #ifdef USE_FREETYPE
43 static int init_freetype(void);
44 static void cleanup(void);
46 static void calc_best_size(int total_width, int max_gwidth, int max_gheight,
47 int padding, int pow2, int *imgw, int *imgh);
48 static int next_pow2(int x);
50 static FT_Library ft;
53 static int init_done;
55 static int init_freetype(void)
56 {
57 if(!init_done) {
58 if(FT_Init_FreeType(&ft) != 0) {
59 return -1;
60 }
61 atexit(cleanup);
62 init_done = 1;
63 }
64 return 0;
65 }
67 static void cleanup(void)
68 {
69 if(init_done) {
70 FT_Done_FreeType(ft);
71 }
72 }
73 #endif /* USE_FREETYPE */
75 struct dtx_font *dtx_open_font(const char *fname, int sz)
76 {
77 struct dtx_font *fnt = 0;
79 #ifdef USE_FREETYPE
80 init_freetype();
82 if(!(fnt = calloc(1, sizeof *fnt))) {
83 fperror("failed to allocate font structure");
84 return 0;
85 }
87 if(FT_New_Face(ft, fname, 0, (FT_Face*)&fnt->face) != 0) {
88 fprintf(stderr, "failed to open font file: %s\n", fname);
89 return 0;
90 }
92 /* pre-create the extended ASCII range glyphmap */
93 if(sz) {
94 dtx_prepare_range(fnt, sz, 0, 256);
96 if(!dtx_font) {
97 dtx_use_font(fnt, sz);
98 }
99 }
100 #else
101 fprintf(stderr, "ignoring call to dtx_open_font: not compiled with freetype support!\n");
102 #endif
104 return fnt;
105 }
107 struct dtx_font *dtx_open_font_glyphmap(const char *fname)
108 {
109 struct dtx_font *fnt;
110 struct dtx_glyphmap *gmap;
112 if(!(fnt = calloc(1, sizeof *fnt))) {
113 fperror("failed to allocate font structure");
114 return 0;
115 }
117 if(fname) {
118 if(!(gmap = dtx_load_glyphmap(fname))) {
119 free(fnt);
120 return 0;
121 }
123 dtx_add_glyphmap(fnt, gmap);
125 if(!dtx_font) {
126 dtx_use_font(fnt, gmap->ptsize);
127 }
128 }
129 return fnt;
130 }
132 void dtx_close_font(struct dtx_font *fnt)
133 {
134 if(!fnt) return;
136 #ifdef USE_FREETYPE
137 FT_Done_Face(fnt->face);
138 #endif
140 /* destroy the glyphmaps */
141 while(fnt->gmaps) {
142 void *tmp = fnt->gmaps;
143 fnt->gmaps = fnt->gmaps->next;
144 dtx_free_glyphmap(tmp);
145 }
147 free(fnt);
148 }
150 void dtx_prepare(struct dtx_font *fnt, int sz)
151 {
152 if(!dtx_get_font_glyphmap_range(fnt, sz, 0, 256)) {
153 fprintf(stderr, "%s: failed (sz: %d, range: 0-255 [ascii])\n", __FUNCTION__, sz);
154 }
155 }
157 void dtx_prepare_range(struct dtx_font *fnt, int sz, int cstart, int cend)
158 {
159 if(!dtx_get_font_glyphmap_range(fnt, sz, cstart, cend)) {
160 fprintf(stderr, "%s: failed (sz: %d, range: %d-%d)\n", __FUNCTION__, sz, cstart, cend);
161 }
162 }
164 struct dtx_glyphmap *dtx_get_font_glyphmap(struct dtx_font *fnt, int sz, int code)
165 {
166 struct dtx_glyphmap *gm;
168 /* check to see if the last we've given out fits the bill */
169 if(fnt->last_gmap && code >= fnt->last_gmap->cstart && code < fnt->last_gmap->cend && fnt->last_gmap->ptsize == sz) {
170 return fnt->last_gmap;
171 }
173 /* otherwise search for the appropriate glyphmap */
174 gm = fnt->gmaps;
175 while(gm) {
176 if(code >= gm->cstart && code < gm->cend && sz == gm->ptsize) {
177 fnt->last_gmap = gm;
178 return gm;
179 }
180 gm = gm->next;
181 }
182 return 0;
183 }
185 struct dtx_glyphmap *dtx_get_font_glyphmap_range(struct dtx_font *fnt, int sz, int cstart, int cend)
186 {
187 struct dtx_glyphmap *gm;
189 /* search the available glyphmaps to see if we've got one that includes
190 * the requested range
191 */
192 gm = fnt->gmaps;
193 while(gm) {
194 if(gm->cstart <= cstart && gm->cend >= cend && gm->ptsize == sz) {
195 return gm;
196 }
197 gm = gm->next;
198 }
200 /* not found, create one and add it to the list */
201 if(!(gm = dtx_create_glyphmap_range(fnt, sz, cstart, cend))) {
202 return 0;
203 }
204 return gm;
205 }
207 struct dtx_glyphmap *dtx_create_glyphmap_range(struct dtx_font *fnt, int sz, int cstart, int cend)
208 {
209 struct dtx_glyphmap *gmap = 0;
211 #ifdef USE_FREETYPE
212 FT_Face face = fnt->face;
213 int i, j;
214 int gx, gy;
215 int padding = 4;
216 int total_width, max_width, max_height;
218 FT_Set_Char_Size(fnt->face, 0, sz * 64, 72, 72);
220 if(!(gmap = calloc(1, sizeof *gmap))) {
221 return 0;
222 }
224 gmap->ptsize = sz;
225 gmap->cstart = cstart;
226 gmap->cend = cend;
227 gmap->crange = cend - cstart;
228 gmap->line_advance = FTSZ_TO_PIXELS((float)face->size->metrics.height);
230 if(!(gmap->glyphs = malloc(gmap->crange * sizeof *gmap->glyphs))) {
231 free(gmap);
232 return 0;
233 }
235 total_width = padding;
236 max_width = max_height = 0;
238 for(i=0; i<gmap->crange; i++) {
239 int w, h;
241 FT_Load_Char(face, i + cstart, 0);
242 w = FTSZ_TO_PIXELS(face->glyph->metrics.width);
243 h = FTSZ_TO_PIXELS(face->glyph->metrics.height);
245 if(w > max_width) max_width = w;
246 if(h > max_height) max_height = h;
248 total_width += w + padding;
249 }
251 calc_best_size(total_width, max_width, max_height, padding, 1, &gmap->xsz, &gmap->ysz);
253 if(!(gmap->pixels = malloc(gmap->xsz * gmap->ysz))) {
254 free(gmap->glyphs);
255 free(gmap);
256 return 0;
257 }
258 memset(gmap->pixels, 0, gmap->xsz * gmap->ysz);
260 gx = padding;
261 gy = padding;
263 for(i=0; i<gmap->crange; i++) {
264 float gwidth, gheight;
265 unsigned char *src, *dst;
266 FT_GlyphSlot glyph;
268 FT_Load_Char(face, i + cstart, FT_LOAD_RENDER);
269 glyph = face->glyph;
270 gwidth = FTSZ_TO_PIXELS((float)glyph->metrics.width);
271 gheight = FTSZ_TO_PIXELS((float)glyph->metrics.height);
273 if(gx > gmap->xsz - gwidth - padding) {
274 gx = padding;
275 gy += max_height + padding;
276 }
278 src = glyph->bitmap.buffer;
279 dst = gmap->pixels + gy * gmap->xsz + gx;
281 for(j=0; j<glyph->bitmap.rows; j++) {
282 memcpy(dst, src, glyph->bitmap.width);
283 dst += gmap->xsz;
284 src += glyph->bitmap.pitch;
285 }
287 gmap->glyphs[i].code = i;
288 gmap->glyphs[i].x = gx - 1;
289 gmap->glyphs[i].y = gy - 1;
290 gmap->glyphs[i].width = gwidth + 2;
291 gmap->glyphs[i].height = gheight + 2;
292 gmap->glyphs[i].orig_x = -FTSZ_TO_PIXELS((float)glyph->metrics.horiBearingX) + 1;
293 gmap->glyphs[i].orig_y = FTSZ_TO_PIXELS((float)glyph->metrics.height - glyph->metrics.horiBearingY) + 1;
294 gmap->glyphs[i].advance = FTSZ_TO_PIXELS((float)glyph->metrics.horiAdvance);
295 /* also precalc normalized */
296 gmap->glyphs[i].nx = (float)gmap->glyphs[i].x / (float)gmap->xsz;
297 gmap->glyphs[i].ny = (float)gmap->glyphs[i].y / (float)gmap->ysz;
298 gmap->glyphs[i].nwidth = (float)gmap->glyphs[i].width / (float)gmap->xsz;
299 gmap->glyphs[i].nheight = (float)gmap->glyphs[i].height / (float)gmap->ysz;
301 gx += gwidth + padding;
302 }
304 /* add it to the glyphmaps list of the font */
305 dtx_add_glyphmap(fnt, gmap);
306 #endif /* USE_FREETYPE */
308 return gmap;
309 }
311 void dtx_free_glyphmap(struct dtx_glyphmap *gmap)
312 {
313 if(gmap) {
314 free(gmap->pixels);
315 free(gmap->glyphs);
316 free(gmap);
317 }
318 }
320 unsigned char *dtx_get_glyphmap_pixels(struct dtx_glyphmap *gmap)
321 {
322 return gmap->pixels;
323 }
325 int dtx_get_glyphmap_width(struct dtx_glyphmap *gmap)
326 {
327 return gmap->xsz;
328 }
330 int dtx_get_glyphmap_height(struct dtx_glyphmap *gmap)
331 {
332 return gmap->ysz;
333 }
335 struct dtx_glyphmap *dtx_load_glyphmap(const char *fname)
336 {
337 ass_file *fp;
338 struct dtx_glyphmap *gmap;
340 if(!(fp = ass_fopen(fname, "rb"))) {
341 return 0;
342 }
343 gmap = dtx_load_glyphmap_stream(fp);
344 ass_fclose(fp);
345 return gmap;
346 }
348 struct dtx_glyphmap *dtx_load_glyphmap_stream(ass_file *fp)
349 {
350 char buf[512];
351 int hdr_lines = 0;
352 struct dtx_glyphmap *gmap;
353 struct glyph *glyphs = 0;
354 struct glyph *g;
355 int min_code = INT_MAX;
356 int max_code = INT_MIN;
357 int i, max_pixval, num_pixels;
359 if(!(gmap = calloc(1, sizeof *gmap))) {
360 fperror("failed to allocate glyphmap");
361 return 0;
362 }
363 gmap->ptsize = -1;
364 gmap->line_advance = FLT_MIN;
366 while(hdr_lines < 3) {
367 char *line = buf;
368 if(!ass_fgets(buf, sizeof buf, fp)) {
369 fperror("unexpected end of file");
370 goto err;
371 }
373 while(isspace(*line)) {
374 line++;
375 }
377 if(line[0] == '#') {
378 int c, res;
379 float x, y, xsz, ysz, orig_x, orig_y, adv, line_adv;
380 int ptsize;
382 if((res = sscanf(line + 1, " size: %d\n", &ptsize)) == 1) {
383 gmap->ptsize = ptsize;
385 } else if((res = sscanf(line + 1, " advance: %f\n", &line_adv)) == 1) {
386 gmap->line_advance = line_adv;
388 } else if((res = sscanf(line + 1, " %d: %fx%f+%f+%f o:%f,%f adv:%f\n",
389 &c, &xsz, &ysz, &x, &y, &orig_x, &orig_y, &adv)) == 8) {
390 if(!(g = malloc(sizeof *g))) {
391 fperror("failed to allocate glyph");
392 goto err;
393 }
394 g->code = c;
395 g->x = x;
396 g->y = y;
397 g->width = xsz;
398 g->height = ysz;
399 g->orig_x = orig_x;
400 g->orig_y = orig_y;
401 g->advance = adv;
402 /* normalized coordinates will be precalculated after everything is loaded */
404 g->next = glyphs;
405 glyphs = g;
407 if(c < min_code) {
408 min_code = c;
409 }
410 if(c > max_code) {
411 max_code = c;
412 }
414 } else {
415 fprintf(stderr, "%s: invalid glyph info line\n", __FUNCTION__);
416 goto err;
417 }
419 } else {
420 switch(hdr_lines) {
421 case 0:
422 if(0[line] != 'P' || 1[line] != '6') {
423 fprintf(stderr, "%s: invalid file format (magic)\n", __FUNCTION__);
424 goto err;
425 }
426 break;
428 case 1:
429 if(sscanf(line, "%d %d", &gmap->xsz, &gmap->ysz) != 2) {
430 fprintf(stderr, "%s: invalid file format (dim)\n", __FUNCTION__);
431 goto err;
432 }
433 break;
435 case 2:
436 {
437 char *endp;
438 max_pixval = strtol(line, &endp, 10);
439 if(endp == line) {
440 fprintf(stderr, "%s: invalid file format (maxval)\n", __FUNCTION__);
441 goto err;
442 }
443 }
444 break;
446 default:
447 break; /* can't happen */
448 }
449 hdr_lines++;
450 }
451 }
453 if(gmap->ptsize == -1 || gmap->line_advance == FLT_MIN) {
454 fprintf(stderr, "%s: invalid glyphmap, insufficient information in ppm comments\n", __FUNCTION__);
455 goto err;
456 }
458 /* precalculate normalized glyph coordinates */
459 g = glyphs;
460 while(g) {
461 g->nx = g->x / gmap->xsz;
462 g->ny = g->y / gmap->ysz;
463 g->nwidth = g->width / gmap->xsz;
464 g->nheight = g->height / gmap->ysz;
465 g = g->next;
466 }
468 num_pixels = gmap->xsz * gmap->ysz;
469 if(!(gmap->pixels = malloc(num_pixels))) {
470 fperror("failed to allocate pixels");
471 goto err;
472 }
474 for(i=0; i<num_pixels; i++) {
475 long c = ass_fgetc(fp);
476 if(c == -1) {
477 fprintf(stderr, "unexpected end of file while reading pixels\n");
478 goto err;
479 }
480 gmap->pixels[i] = 255 * c / max_pixval;
481 ass_fseek(fp, 2, SEEK_CUR);
482 }
484 gmap->cstart = min_code;
485 gmap->cend = max_code + 1;
486 gmap->crange = gmap->cend - gmap->cstart;
488 if(!(gmap->glyphs = calloc(gmap->crange, sizeof *gmap->glyphs))) {
489 fperror("failed to allocate glyph info");
490 goto err;
491 }
493 while(glyphs) {
494 struct glyph *g = glyphs;
495 glyphs = glyphs->next;
497 gmap->glyphs[g->code - gmap->cstart] = *g;
498 free(g);
499 }
500 return gmap;
502 err:
503 dtx_free_glyphmap(gmap);
504 while(glyphs) {
505 void *tmp = glyphs;
506 glyphs = glyphs->next;
507 free(tmp);
508 }
509 return 0;
510 }
512 int dtx_save_glyphmap(const char *fname, const struct dtx_glyphmap *gmap)
513 {
514 FILE *fp;
515 int res;
517 if(!(fp = fopen(fname, "wb"))) {
518 fprintf(stderr, "%s: failed to open file: %s: %s\n", __FUNCTION__, fname, strerror(errno));
519 return -1;
520 }
521 res = dtx_save_glyphmap_stream(fp, gmap);
522 fclose(fp);
523 return res;
524 }
526 int dtx_save_glyphmap_stream(FILE *fp, const struct dtx_glyphmap *gmap)
527 {
528 int i, num_pixels;
529 struct glyph *g = gmap->glyphs;
531 fprintf(fp, "P6\n%d %d\n", gmap->xsz, gmap->ysz);
532 fprintf(fp, "# size: %d\n", gmap->ptsize);
533 fprintf(fp, "# advance: %g\n", gmap->line_advance);
534 for(i=0; i<gmap->crange; i++) {
535 fprintf(fp, "# %d: %gx%g+%g+%g o:%g,%g adv:%g\n", g->code, g->width, g->height, g->x, g->y,
536 g->orig_x, g->orig_y, g->advance);
537 g++;
538 }
539 fprintf(fp, "255\n");
541 num_pixels = gmap->xsz * gmap->ysz;
542 for(i=0; i<num_pixels; i++) {
543 int c = gmap->pixels[i];
544 fputc(c, fp);
545 fputc(c, fp);
546 fputc(c, fp);
547 }
548 return 0;
549 }
551 void dtx_add_glyphmap(struct dtx_font *fnt, struct dtx_glyphmap *gmap)
552 {
553 gmap->next = fnt->gmaps;
554 fnt->gmaps = gmap;
555 }
558 void dtx_use_font(struct dtx_font *fnt, int sz)
559 {
560 dtx_gl_init();
562 dtx_font = fnt;
563 dtx_font_sz = sz;
564 }
566 float dtx_line_height(void)
567 {
568 struct dtx_glyphmap *gmap = dtx_get_font_glyphmap(dtx_font, dtx_font_sz, '\n');
570 return gmap->line_advance;
571 }
573 void dtx_glyph_box(int code, struct dtx_box *box)
574 {
575 int cidx;
576 struct dtx_glyphmap *gmap;
577 gmap = dtx_get_font_glyphmap(dtx_font, dtx_font_sz, code);
579 cidx = code - gmap->cstart;
581 box->x = gmap->glyphs[cidx].orig_x;
582 box->y = gmap->glyphs[cidx].orig_y;
583 box->width = gmap->glyphs[cidx].width;
584 box->height = gmap->glyphs[cidx].height;
585 }
587 float dtx_glyph_width(int code)
588 {
589 struct dtx_box box;
590 dtx_glyph_box(code, &box);
591 return box.width;
592 }
594 float dtx_glyph_height(int code)
595 {
596 struct dtx_box box;
597 dtx_glyph_box(code, &box);
598 return box.height;
599 }
601 void dtx_string_box(const char *str, struct dtx_box *box)
602 {
603 int code;
604 float pos_x = 0.0f, pos_y = 0.0f;
605 struct glyph *g = 0;
606 float x0, y0, x1, y1;
608 x0 = y0 = FLT_MAX;
609 x1 = y1 = -FLT_MAX;
611 while(*str) {
612 float px, py;
613 struct dtx_glyphmap *gmap;
615 code = dtx_utf8_char_code(str);
616 str = dtx_utf8_next_char((char*)str);
618 px = pos_x;
619 py = pos_y;
621 if((gmap = dtx_proc_char(code, &pos_x, &pos_y))) {
622 g = gmap->glyphs + code - gmap->cstart;
624 if(px + g->orig_x < x0) {
625 x0 = px + g->orig_x;
626 }
627 if(py - g->orig_y < y0) {
628 y0 = py - g->orig_y;
629 }
630 if(px + g->orig_x + g->width > x1) {
631 x1 = px + g->orig_x + g->width;
632 }
633 if(py - g->orig_y + g->height > y1) {
634 y1 = py - g->orig_y + g->height;
635 }
636 }
637 }
639 box->x = x0;
640 box->y = y0;
641 box->width = x1 - x0;
642 box->height = y1 - y0;
643 }
645 float dtx_string_width(const char *str)
646 {
647 struct dtx_box box;
649 dtx_string_box(str, &box);
650 return box.width;
651 }
653 float dtx_string_height(const char *str)
654 {
655 struct dtx_box box;
657 dtx_string_box(str, &box);
658 return box.height;
659 }
661 float dtx_char_pos(const char *str, int n)
662 {
663 int i;
664 float pos = 0.0;
665 struct dtx_glyphmap *gmap;
667 for(i=0; i<n; i++) {
668 int code = dtx_utf8_char_code(str);
669 str = dtx_utf8_next_char((char*)str);
671 gmap = dtx_get_font_glyphmap(dtx_font, dtx_font_sz, code);
672 pos += gmap->glyphs[i].advance;
673 }
674 return pos;
675 }
677 int dtx_char_at_pt(const char *str, float pt)
678 {
679 int i;
680 float prev_pos = 0.0f, pos = 0.0f;
681 struct dtx_glyphmap *gmap;
683 for(i=0; *str; i++) {
684 int code = dtx_utf8_char_code(str);
685 str = dtx_utf8_next_char((char*)str);
687 gmap = dtx_get_font_glyphmap(dtx_font, dtx_font_sz, code);
688 pos += gmap->glyphs[i].advance;
690 if(fabs(pt - prev_pos) < fabs(pt - pos)) {
691 break;
692 }
693 prev_pos = pos;
694 }
695 return i;
696 }
698 struct dtx_glyphmap *dtx_proc_char(int code, float *xpos, float *ypos)
699 {
700 struct dtx_glyphmap *gmap;
701 gmap = dtx_get_font_glyphmap(dtx_font, dtx_font_sz, code);
703 switch(code) {
704 case '\n':
705 *xpos = 0.0;
706 if(gmap) {
707 *ypos -= gmap->line_advance;
708 }
709 return 0;
711 case '\t':
712 if(gmap) {
713 *xpos = (fmod(*xpos, 4.0) + 4.0) * gmap->glyphs[0].advance;
714 }
715 return 0;
717 case '\r':
718 *xpos = 0.0;
719 return 0;
721 default:
722 break;
723 }
725 if(gmap) {
726 *xpos += gmap->glyphs[code - gmap->cstart].advance;
727 }
728 return gmap;
729 }
731 #ifdef USE_FREETYPE
732 static void calc_best_size(int total_width, int max_gwidth, int max_gheight, int padding, int pow2, int *imgw, int *imgh)
733 {
734 int xsz, ysz, num_rows;
735 float aspect;
737 for(xsz=2; xsz<=MAX_IMG_WIDTH; xsz *= 2) {
738 num_rows = total_width / xsz + 1;
740 /* assume worst case, all last glyphs will float to the next line
741 * so let's add extra rows for that. */
742 num_rows += (padding + (max_gwidth + padding) * num_rows + xsz - 1) / xsz;
744 ysz = num_rows * (max_gheight + padding) + padding;
745 if(pow2) {
746 ysz = next_pow2(ysz);
747 }
748 aspect = (float)xsz / (float)ysz;
750 if(aspect >= 1.0) {
751 break;
752 }
753 }
755 if(xsz > MAX_IMG_WIDTH) {
756 xsz = MAX_IMG_WIDTH;
757 }
759 *imgw = xsz;
760 *imgh = ysz;
761 }
764 static int next_pow2(int x)
765 {
766 x--;
767 x = (x >> 1) | x;
768 x = (x >> 2) | x;
769 x = (x >> 4) | x;
770 x = (x >> 8) | x;
771 x = (x >> 16) | x;
772 return x + 1;
773 }
774 #endif