libdrawtext
view src/font.c @ 57:7b01e04c9c8b
added copyright notices
author | John Tsiombikas <nuclear@mutantstargoat.com> |
---|---|
date | Fri, 16 Sep 2011 08:42:07 +0300 |
parents | fe0c54e574ae |
children | 7e0c702f1223 |
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 <limits.h>
26 #include <ctype.h>
27 #include <errno.h>
28 #ifdef USE_FREETYPE
29 #include <ft2build.h>
30 #include FT_FREETYPE_H
31 #endif
32 #include "drawtext.h"
33 #include "drawtext_impl.h"
35 #define FTSZ_TO_PIXELS(x) ((x) / 64)
36 #define MAX_IMG_WIDTH 4096
39 #ifdef USE_FREETYPE
40 static int init_freetype(void);
41 static void cleanup(void);
43 static void calc_best_size(int total_width, int max_glyph_height, int padding, int pow2, int *imgw, int *imgh);
44 static int next_pow2(int x);
46 static FT_Library ft;
49 static int init_done;
51 static int init_freetype(void)
52 {
53 if(!init_done) {
54 if(FT_Init_FreeType(&ft) != 0) {
55 return -1;
56 }
57 atexit(cleanup);
58 init_done = 1;
59 }
60 return 0;
61 }
63 static void cleanup(void)
64 {
65 if(init_done) {
66 FT_Done_FreeType(ft);
67 }
68 }
70 struct dtx_font *dtx_open_font(const char *fname, int sz)
71 {
72 struct dtx_font *fnt;
74 init_freetype();
76 if(!(fnt = calloc(1, sizeof *fnt))) {
77 fperror("failed to allocate font structure");
78 return 0;
79 }
81 if(FT_New_Face(ft, fname, 0, (FT_Face*)&fnt->face) != 0) {
82 fprintf(stderr, "failed to open font file: %s\n", fname);
83 return 0;
84 }
86 /* pre-create the extended ASCII range glyphmap */
87 if(sz) {
88 dtx_prepare_range(fnt, sz, 0, 256);
89 }
91 return fnt;
92 }
94 void dtx_close_font(struct dtx_font *fnt)
95 {
96 if(!fnt) return;
98 FT_Done_Face(fnt->face);
100 /* destroy the glyphmaps */
101 while(fnt->gmaps) {
102 void *tmp = fnt->gmaps;
103 fnt->gmaps = fnt->gmaps->next;
104 dtx_free_glyphmap(tmp);
105 }
107 free(fnt);
108 }
110 void dtx_prepare(struct dtx_font *fnt, int sz)
111 {
112 dtx_get_font_glyphmap_range(fnt, sz, 0, 256);
113 }
115 void dtx_prepare_range(struct dtx_font *fnt, int sz, int cstart, int cend)
116 {
117 dtx_get_font_glyphmap_range(fnt, sz, cstart, cend);
118 }
120 struct dtx_glyphmap *dtx_get_font_glyphmap(struct dtx_font *fnt, int sz, int code)
121 {
122 struct dtx_glyphmap *gm;
124 /* check to see if the last we've given out fits the bill */
125 if(fnt->last_gmap && code >= fnt->last_gmap->cstart && code < fnt->last_gmap->cend && fnt->last_gmap->ptsize == sz) {
126 return fnt->last_gmap;
127 }
129 /* otherwise search for the appropriate glyphmap */
130 gm = fnt->gmaps;
131 while(gm) {
132 if(code >= gm->cstart && code < gm->cend && sz == gm->ptsize) {
133 fnt->last_gmap = gm;
134 return gm;
135 }
136 gm = gm->next;
137 }
138 return 0;
139 }
141 struct dtx_glyphmap *dtx_get_font_glyphmap_range(struct dtx_font *fnt, int sz, int cstart, int cend)
142 {
143 struct dtx_glyphmap *gm;
145 /* search the available glyphmaps to see if we've got one that includes
146 * the requested range
147 */
148 gm = fnt->gmaps;
149 while(gm) {
150 if(gm->cstart <= cstart && gm->cend >= cend && gm->ptsize == sz) {
151 return gm;
152 }
153 gm = gm->next;
154 }
156 /* not found, create one and add it to the list */
157 if(!(gm = dtx_create_glyphmap_range(fnt, sz, cstart, cend))) {
158 return 0;
159 }
160 return gm;
161 }
163 struct dtx_glyphmap *dtx_create_glyphmap_range(struct dtx_font *fnt, int sz, int cstart, int cend)
164 {
165 FT_Face face = fnt->face;
166 struct dtx_glyphmap *gmap;
167 int i, j;
168 int gx, gy;
169 int padding = 4;
170 int total_width = padding;
171 int max_height = 0;
173 FT_Set_Char_Size(fnt->face, 0, sz * 64, 72, 72);
175 if(!(gmap = calloc(1, sizeof *gmap))) {
176 return 0;
177 }
179 gmap->ptsize = sz;
180 gmap->cstart = cstart;
181 gmap->cend = cend;
182 gmap->crange = cend - cstart;
183 gmap->line_advance = FTSZ_TO_PIXELS((float)face->size->metrics.height);
185 if(!(gmap->glyphs = malloc(gmap->crange * sizeof *gmap->glyphs))) {
186 free(gmap);
187 return 0;
188 }
190 for(i=0; i<gmap->crange; i++) {
191 int h;
193 FT_Load_Char(face, i + cstart, 0);
194 h = FTSZ_TO_PIXELS(face->glyph->metrics.height);
196 if(h > max_height) {
197 max_height = h;
198 }
199 total_width += FTSZ_TO_PIXELS(face->glyph->metrics.width) + padding;
200 }
202 calc_best_size(total_width, max_height, padding, 1, &gmap->xsz, &gmap->ysz);
204 if(!(gmap->pixels = malloc(gmap->xsz * gmap->ysz))) {
205 free(gmap->glyphs);
206 free(gmap);
207 return 0;
208 }
209 memset(gmap->pixels, 0, gmap->xsz * gmap->ysz);
211 gx = padding;
212 gy = padding;
214 for(i=0; i<gmap->crange; i++) {
215 float gwidth, gheight;
216 unsigned char *src, *dst;
217 FT_GlyphSlot glyph;
219 FT_Load_Char(face, i + cstart, FT_LOAD_RENDER);
220 glyph = face->glyph;
221 gwidth = FTSZ_TO_PIXELS((float)glyph->metrics.width);
222 gheight = FTSZ_TO_PIXELS((float)glyph->metrics.height);
224 if(gx > gmap->xsz - gwidth - padding) {
225 gx = padding;
226 gy += max_height + padding;
227 }
229 src = glyph->bitmap.buffer;
230 dst = gmap->pixels + gy * gmap->xsz + gx;
232 for(j=0; j<glyph->bitmap.rows; j++) {
233 memcpy(dst, src, glyph->bitmap.width);
234 dst += gmap->xsz;
235 src += glyph->bitmap.pitch;
236 }
238 gmap->glyphs[i].code = i;
239 gmap->glyphs[i].x = gx - 1;
240 gmap->glyphs[i].y = gy - 1;
241 gmap->glyphs[i].width = gwidth + 2;
242 gmap->glyphs[i].height = gheight + 2;
243 gmap->glyphs[i].orig_x = -FTSZ_TO_PIXELS((float)glyph->metrics.horiBearingX) + 1;
244 gmap->glyphs[i].orig_y = FTSZ_TO_PIXELS((float)glyph->metrics.height - glyph->metrics.horiBearingY) + 1;
245 gmap->glyphs[i].advance = FTSZ_TO_PIXELS((float)glyph->metrics.horiAdvance);
246 /* also precalc normalized */
247 gmap->glyphs[i].nx = (float)gmap->glyphs[i].x / (float)gmap->xsz;
248 gmap->glyphs[i].ny = (float)gmap->glyphs[i].y / (float)gmap->ysz;
249 gmap->glyphs[i].nwidth = (float)gmap->glyphs[i].width / (float)gmap->xsz;
250 gmap->glyphs[i].nheight = (float)gmap->glyphs[i].height / (float)gmap->ysz;
252 gx += gwidth + padding;
253 }
255 /* add it to the glyphmaps list of the font */
256 gmap->next = fnt->gmaps;
257 fnt->gmaps = gmap;
259 return gmap;
260 }
261 #endif /* USE_FREETYPE */
263 void dtx_free_glyphmap(struct dtx_glyphmap *gmap)
264 {
265 if(gmap) {
266 free(gmap->pixels);
267 free(gmap->glyphs);
268 free(gmap);
269 }
270 }
272 unsigned char *dtx_get_glyphmap_pixels(struct dtx_glyphmap *gmap)
273 {
274 return gmap->pixels;
275 }
277 int dtx_get_glyphmap_width(struct dtx_glyphmap *gmap)
278 {
279 return gmap->xsz;
280 }
282 int dtx_get_glyphmap_height(struct dtx_glyphmap *gmap)
283 {
284 return gmap->ysz;
285 }
287 struct dtx_glyphmap *dtx_load_glyphmap(const char *fname)
288 {
289 FILE *fp;
290 struct dtx_glyphmap *gmap;
292 if(!(fp = fopen(fname, "r"))) {
293 return 0;
294 }
295 gmap = dtx_load_glyphmap_stream(fp);
296 fclose(fp);
297 return gmap;
298 }
300 struct dtx_glyphmap *dtx_load_glyphmap_stream(FILE *fp)
301 {
302 char buf[512];
303 int hdr_lines = 0;
304 struct dtx_glyphmap *gmap;
305 struct glyph *glyphs = 0;
306 int min_code = INT_MAX;
307 int max_code = INT_MIN;
308 int i, max_pixval, num_pixels;
310 if(!(gmap = calloc(1, sizeof *gmap))) {
311 fperror("failed to allocate glyphmap");
312 return 0;
313 }
315 while(hdr_lines < 3) {
316 char *line = buf;
317 if(!fgets(buf, sizeof buf, fp)) {
318 fperror("unexpected end of file");
319 goto err;
320 }
322 while(isspace(*line)) {
323 line++;
324 }
326 if(line[0] == '#') {
327 struct glyph *g;
328 int c;
329 float x, y, xsz, ysz, res;
331 res = sscanf(line + 1, "%d: %fx%f+%f+%f\n", &c, &xsz, &ysz, &x, &y);
332 if(res != 5) {
333 fprintf(stderr, "%s: invalid glyph info line\n", __func__);
334 goto err;
335 }
337 if(!(g = malloc(sizeof *g))) {
338 fperror("failed to allocate glyph");
339 goto err;
340 }
341 g->code = c;
342 g->x = x;
343 g->y = y;
344 g->width = xsz;
345 g->height = ysz;
346 g->next = glyphs;
347 glyphs = g;
349 if(c < min_code) {
350 min_code = c;
351 }
352 if(c > max_code) {
353 max_code = c;
354 }
355 } else {
356 switch(hdr_lines) {
357 case 0:
358 if(0[line] != 'P' || 1[line] != '6') {
359 fprintf(stderr, "%s: invalid file format (magic)\n", __func__);
360 goto err;
361 }
362 break;
364 case 1:
365 if(sscanf(line, "%d %d", &gmap->xsz, &gmap->ysz) != 2) {
366 fprintf(stderr, "%s: invalid file format (dim)\n", __func__);
367 goto err;
368 }
369 break;
371 case 2:
372 {
373 char *endp;
374 max_pixval = strtol(line, &endp, 10);
375 if(endp == line) {
376 fprintf(stderr, "%s: invalid file format (maxval)\n", __func__);
377 goto err;
378 }
379 }
380 break;
382 default:
383 break; /* can't happen */
384 }
385 hdr_lines++;
386 }
387 }
389 num_pixels = gmap->xsz * gmap->ysz;
390 if(!(gmap->pixels = malloc(num_pixels))) {
391 fperror("failed to allocate pixels");
392 goto err;
393 }
395 for(i=0; i<num_pixels; i++) {
396 long c = fgetc(fp);
397 if(c == -1) {
398 fprintf(stderr, "unexpected end of file while reading pixels\n");
399 goto err;
400 }
401 gmap->pixels[i] = 255 * c / max_pixval;
402 fseek(fp, 2, SEEK_CUR);
403 }
405 gmap->cstart = min_code;
406 gmap->cend = max_code + 1;
407 gmap->crange = gmap->cend - gmap->cstart;
409 if(!(gmap->glyphs = calloc(gmap->crange, sizeof *gmap->glyphs))) {
410 fperror("failed to allocate glyph info");
411 goto err;
412 }
414 while(glyphs) {
415 struct glyph *g = glyphs;
416 glyphs = glyphs->next;
418 gmap->glyphs[g->code - gmap->cstart] = *g;
419 free(g);
420 }
421 return gmap;
423 err:
424 dtx_free_glyphmap(gmap);
425 while(glyphs) {
426 void *tmp = glyphs;
427 glyphs = glyphs->next;
428 free(tmp);
429 }
430 return 0;
431 }
433 int dtx_save_glyphmap(const char *fname, const struct dtx_glyphmap *gmap)
434 {
435 FILE *fp;
436 int res;
438 if(!(fp = fopen(fname, "wb"))) {
439 fprintf(stderr, "%s: failed to open file: %s: %s\n", __func__, fname, strerror(errno));
440 return -1;
441 }
442 res = dtx_save_glyphmap_stream(fp, gmap);
443 fclose(fp);
444 return res;
445 }
447 int dtx_save_glyphmap_stream(FILE *fp, const struct dtx_glyphmap *gmap)
448 {
449 int i, num_pixels;
450 struct glyph *g = gmap->glyphs;
452 fprintf(fp, "P6\n%d %d\n", gmap->xsz, gmap->ysz);
453 for(i=0; i<gmap->crange; i++) {
454 fprintf(fp, "# %d: %fx%f+%f+%f\n", g->code, g->width, g->height, g->x, g->y);
455 g++;
456 }
457 fprintf(fp, "255\n");
459 num_pixels = gmap->xsz * gmap->ysz;
460 for(i=0; i<num_pixels; i++) {
461 int c = gmap->pixels[i];
462 fputc(c, fp);
463 fputc(c, fp);
464 fputc(c, fp);
465 }
466 return 0;
467 }
470 static void calc_best_size(int total_width, int max_glyph_height, int padding, int pow2, int *imgw, int *imgh)
471 {
472 int xsz, ysz, num_rows;
473 float aspect;
475 for(xsz=2; xsz<=MAX_IMG_WIDTH; xsz *= 2) {
476 num_rows = total_width / xsz + 1;
478 /* take into account the one extra padding for each row after the first */
479 num_rows = (total_width + padding * (num_rows - 1)) / xsz + 1;
481 ysz = num_rows * (max_glyph_height + padding) + padding;
482 if(pow2) {
483 ysz = next_pow2(ysz);
484 }
485 aspect = (float)xsz / (float)ysz;
487 if(aspect >= 1.0) {
488 break;
489 }
490 }
492 if(xsz > MAX_IMG_WIDTH) {
493 xsz = MAX_IMG_WIDTH;
494 }
496 *imgw = xsz;
497 *imgh = ysz;
498 }
501 static int next_pow2(int x)
502 {
503 x--;
504 x = (x >> 1) | x;
505 x = (x >> 2) | x;
506 x = (x >> 4) | x;
507 x = (x >> 8) | x;
508 x = (x >> 16) | x;
509 return x + 1;
510 }