libdrawtext

view src/font.c @ 55:59e5858de836

fixed a bug in utf-8 decoding
author John Tsiombikas <nuclear@mutantstargoat.com>
date Thu, 15 Sep 2011 23:32:39 +0300
parents bfe431dd1d80
children 095ff7ca4e74
line source
1 #ifndef NO_FREETYPE
2 #define USE_FREETYPE
3 #endif
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <string.h>
8 #include <limits.h>
9 #include <ctype.h>
10 #include <errno.h>
11 #ifdef USE_FREETYPE
12 #include <ft2build.h>
13 #include FT_FREETYPE_H
14 #endif
15 #include "drawtext.h"
16 #include "drawtext_impl.h"
18 #define FTSZ_TO_PIXELS(x) ((x) / 64)
19 #define MAX_IMG_WIDTH 4096
22 #ifdef USE_FREETYPE
23 static int init_freetype(void);
24 static void cleanup(void);
26 static void calc_best_size(int total_width, int max_glyph_height, int padding, int pow2, int *imgw, int *imgh);
27 static int next_pow2(int x);
29 static FT_Library ft;
32 static int init_done;
34 static int init_freetype(void)
35 {
36 if(!init_done) {
37 if(FT_Init_FreeType(&ft) != 0) {
38 return -1;
39 }
40 atexit(cleanup);
41 init_done = 1;
42 }
43 return 0;
44 }
46 static void cleanup(void)
47 {
48 if(init_done) {
49 FT_Done_FreeType(ft);
50 }
51 }
53 struct dtx_font *dtx_open_font(const char *fname, int sz)
54 {
55 struct dtx_font *fnt;
57 init_freetype();
59 if(!(fnt = calloc(1, sizeof *fnt))) {
60 fperror("failed to allocate font structure");
61 return 0;
62 }
64 if(FT_New_Face(ft, fname, 0, (FT_Face*)&fnt->face) != 0) {
65 fprintf(stderr, "failed to open font file: %s\n", fname);
66 return 0;
67 }
69 /* pre-create the extended ASCII range glyphmap */
70 if(sz) {
71 dtx_prepare_range(fnt, sz, 0, 256);
72 }
74 return fnt;
75 }
77 void dtx_close_font(struct dtx_font *fnt)
78 {
79 if(!fnt) return;
81 FT_Done_Face(fnt->face);
83 /* destroy the glyphmaps */
84 while(fnt->gmaps) {
85 void *tmp = fnt->gmaps;
86 fnt->gmaps = fnt->gmaps->next;
87 dtx_free_glyphmap(tmp);
88 }
90 free(fnt);
91 }
93 void dtx_prepare(struct dtx_font *fnt, int sz)
94 {
95 dtx_get_font_glyphmap_range(fnt, sz, 0, 256);
96 }
98 void dtx_prepare_range(struct dtx_font *fnt, int sz, int cstart, int cend)
99 {
100 dtx_get_font_glyphmap_range(fnt, sz, cstart, cend);
101 }
103 struct dtx_glyphmap *dtx_get_font_glyphmap(struct dtx_font *fnt, int sz, int code)
104 {
105 struct dtx_glyphmap *gm;
107 /* check to see if the last we've given out fits the bill */
108 if(fnt->last_gmap && code >= fnt->last_gmap->cstart && code < fnt->last_gmap->cend && fnt->last_gmap->ptsize == sz) {
109 return fnt->last_gmap;
110 }
112 /* otherwise search for the appropriate glyphmap */
113 gm = fnt->gmaps;
114 while(gm) {
115 if(code >= gm->cstart && code < gm->cend && sz == gm->ptsize) {
116 fnt->last_gmap = gm;
117 return gm;
118 }
119 gm = gm->next;
120 }
121 return 0;
122 }
124 struct dtx_glyphmap *dtx_get_font_glyphmap_range(struct dtx_font *fnt, int sz, int cstart, int cend)
125 {
126 struct dtx_glyphmap *gm;
128 /* search the available glyphmaps to see if we've got one that includes
129 * the requested range
130 */
131 gm = fnt->gmaps;
132 while(gm) {
133 if(gm->cstart <= cstart && gm->cend >= cend && gm->ptsize == sz) {
134 return gm;
135 }
136 gm = gm->next;
137 }
139 /* not found, create one and add it to the list */
140 if(!(gm = dtx_create_glyphmap_range(fnt, sz, cstart, cend))) {
141 return 0;
142 }
143 return gm;
144 }
146 struct dtx_glyphmap *dtx_create_glyphmap_range(struct dtx_font *fnt, int sz, int cstart, int cend)
147 {
148 FT_Face face = fnt->face;
149 struct dtx_glyphmap *gmap;
150 int i, j;
151 int gx, gy;
152 int padding = 4;
153 int total_width = padding;
154 int max_height = 0;
156 FT_Set_Char_Size(fnt->face, 0, sz * 64, 72, 72);
158 if(!(gmap = calloc(1, sizeof *gmap))) {
159 return 0;
160 }
162 gmap->ptsize = sz;
163 gmap->cstart = cstart;
164 gmap->cend = cend;
165 gmap->crange = cend - cstart;
166 gmap->line_advance = FTSZ_TO_PIXELS((float)face->size->metrics.height);
168 if(!(gmap->glyphs = malloc(gmap->crange * sizeof *gmap->glyphs))) {
169 free(gmap);
170 return 0;
171 }
173 for(i=0; i<gmap->crange; i++) {
174 int h;
176 FT_Load_Char(face, i + cstart, 0);
177 h = FTSZ_TO_PIXELS(face->glyph->metrics.height);
179 if(h > max_height) {
180 max_height = h;
181 }
182 total_width += FTSZ_TO_PIXELS(face->glyph->metrics.width) + padding;
183 }
185 calc_best_size(total_width, max_height, padding, 1, &gmap->xsz, &gmap->ysz);
187 if(!(gmap->pixels = malloc(gmap->xsz * gmap->ysz))) {
188 free(gmap->glyphs);
189 free(gmap);
190 return 0;
191 }
192 memset(gmap->pixels, 0, gmap->xsz * gmap->ysz);
194 gx = padding;
195 gy = padding;
197 for(i=0; i<gmap->crange; i++) {
198 float gwidth, gheight;
199 unsigned char *src, *dst;
200 FT_GlyphSlot glyph;
202 FT_Load_Char(face, i + cstart, FT_LOAD_RENDER);
203 glyph = face->glyph;
204 gwidth = FTSZ_TO_PIXELS((float)glyph->metrics.width);
205 gheight = FTSZ_TO_PIXELS((float)glyph->metrics.height);
207 if(gx > gmap->xsz - gwidth - padding) {
208 gx = padding;
209 gy += max_height + padding;
210 }
212 src = glyph->bitmap.buffer;
213 dst = gmap->pixels + gy * gmap->xsz + gx;
215 for(j=0; j<glyph->bitmap.rows; j++) {
216 memcpy(dst, src, glyph->bitmap.width);
217 dst += gmap->xsz;
218 src += glyph->bitmap.pitch;
219 }
221 gmap->glyphs[i].code = i;
222 gmap->glyphs[i].x = gx - 1;
223 gmap->glyphs[i].y = gy - 1;
224 gmap->glyphs[i].width = gwidth + 2;
225 gmap->glyphs[i].height = gheight + 2;
226 gmap->glyphs[i].orig_x = -FTSZ_TO_PIXELS((float)glyph->metrics.horiBearingX) + 1;
227 gmap->glyphs[i].orig_y = FTSZ_TO_PIXELS((float)glyph->metrics.height - glyph->metrics.horiBearingY) + 1;
228 gmap->glyphs[i].advance = FTSZ_TO_PIXELS((float)glyph->metrics.horiAdvance);
229 /* also precalc normalized */
230 gmap->glyphs[i].nx = (float)gmap->glyphs[i].x / (float)gmap->xsz;
231 gmap->glyphs[i].ny = (float)gmap->glyphs[i].y / (float)gmap->ysz;
232 gmap->glyphs[i].nwidth = (float)gmap->glyphs[i].width / (float)gmap->xsz;
233 gmap->glyphs[i].nheight = (float)gmap->glyphs[i].height / (float)gmap->ysz;
235 gx += gwidth + padding;
236 }
238 /* add it to the glyphmaps list of the font */
239 gmap->next = fnt->gmaps;
240 fnt->gmaps = gmap;
242 return gmap;
243 }
244 #endif /* USE_FREETYPE */
246 void dtx_free_glyphmap(struct dtx_glyphmap *gmap)
247 {
248 if(gmap) {
249 free(gmap->pixels);
250 free(gmap->glyphs);
251 free(gmap);
252 }
253 }
255 unsigned char *dtx_get_glyphmap_pixels(struct dtx_glyphmap *gmap)
256 {
257 return gmap->pixels;
258 }
260 int dtx_get_glyphmap_width(struct dtx_glyphmap *gmap)
261 {
262 return gmap->xsz;
263 }
265 int dtx_get_glyphmap_height(struct dtx_glyphmap *gmap)
266 {
267 return gmap->ysz;
268 }
270 struct dtx_glyphmap *dtx_load_glyphmap(const char *fname)
271 {
272 FILE *fp;
273 struct dtx_glyphmap *gmap;
275 if(!(fp = fopen(fname, "r"))) {
276 return 0;
277 }
278 gmap = dtx_load_glyphmap_stream(fp);
279 fclose(fp);
280 return gmap;
281 }
283 struct dtx_glyphmap *dtx_load_glyphmap_stream(FILE *fp)
284 {
285 char buf[512];
286 int hdr_lines = 0;
287 struct dtx_glyphmap *gmap;
288 struct glyph *glyphs = 0;
289 int min_code = INT_MAX;
290 int max_code = INT_MIN;
291 int i, max_pixval, num_pixels;
293 if(!(gmap = calloc(1, sizeof *gmap))) {
294 fperror("failed to allocate glyphmap");
295 return 0;
296 }
298 while(hdr_lines < 3) {
299 char *line = buf;
300 if(!fgets(buf, sizeof buf, fp)) {
301 fperror("unexpected end of file");
302 goto err;
303 }
305 while(isspace(*line)) {
306 line++;
307 }
309 if(line[0] == '#') {
310 struct glyph *g;
311 int c;
312 float x, y, xsz, ysz, res;
314 res = sscanf(line + 1, "%d: %fx%f+%f+%f\n", &c, &xsz, &ysz, &x, &y);
315 if(res != 5) {
316 fprintf(stderr, "%s: invalid glyph info line\n", __func__);
317 goto err;
318 }
320 if(!(g = malloc(sizeof *g))) {
321 fperror("failed to allocate glyph");
322 goto err;
323 }
324 g->code = c;
325 g->x = x;
326 g->y = y;
327 g->width = xsz;
328 g->height = ysz;
329 g->next = glyphs;
330 glyphs = g;
332 if(c < min_code) {
333 min_code = c;
334 }
335 if(c > max_code) {
336 max_code = c;
337 }
338 } else {
339 switch(hdr_lines) {
340 case 0:
341 if(0[line] != 'P' || 1[line] != '6') {
342 fprintf(stderr, "%s: invalid file format (magic)\n", __func__);
343 goto err;
344 }
345 break;
347 case 1:
348 if(sscanf(line, "%d %d", &gmap->xsz, &gmap->ysz) != 2) {
349 fprintf(stderr, "%s: invalid file format (dim)\n", __func__);
350 goto err;
351 }
352 break;
354 case 2:
355 {
356 char *endp;
357 max_pixval = strtol(line, &endp, 10);
358 if(endp == line) {
359 fprintf(stderr, "%s: invalid file format (maxval)\n", __func__);
360 goto err;
361 }
362 }
363 break;
365 default:
366 break; /* can't happen */
367 }
368 hdr_lines++;
369 }
370 }
372 num_pixels = gmap->xsz * gmap->ysz;
373 if(!(gmap->pixels = malloc(num_pixels))) {
374 fperror("failed to allocate pixels");
375 goto err;
376 }
378 for(i=0; i<num_pixels; i++) {
379 long c = fgetc(fp);
380 if(c == -1) {
381 fprintf(stderr, "unexpected end of file while reading pixels\n");
382 goto err;
383 }
384 gmap->pixels[i] = 255 * c / max_pixval;
385 fseek(fp, 2, SEEK_CUR);
386 }
388 gmap->cstart = min_code;
389 gmap->cend = max_code + 1;
390 gmap->crange = gmap->cend - gmap->cstart;
392 if(!(gmap->glyphs = calloc(gmap->crange, sizeof *gmap->glyphs))) {
393 fperror("failed to allocate glyph info");
394 goto err;
395 }
397 while(glyphs) {
398 struct glyph *g = glyphs;
399 glyphs = glyphs->next;
401 gmap->glyphs[g->code - gmap->cstart] = *g;
402 free(g);
403 }
404 return gmap;
406 err:
407 dtx_free_glyphmap(gmap);
408 while(glyphs) {
409 void *tmp = glyphs;
410 glyphs = glyphs->next;
411 free(tmp);
412 }
413 return 0;
414 }
416 int dtx_save_glyphmap(const char *fname, const struct dtx_glyphmap *gmap)
417 {
418 FILE *fp;
419 int res;
421 if(!(fp = fopen(fname, "wb"))) {
422 fprintf(stderr, "%s: failed to open file: %s: %s\n", __func__, fname, strerror(errno));
423 return -1;
424 }
425 res = dtx_save_glyphmap_stream(fp, gmap);
426 fclose(fp);
427 return res;
428 }
430 int dtx_save_glyphmap_stream(FILE *fp, const struct dtx_glyphmap *gmap)
431 {
432 int i, num_pixels;
433 struct glyph *g = gmap->glyphs;
435 fprintf(fp, "P6\n%d %d\n", gmap->xsz, gmap->ysz);
436 for(i=0; i<gmap->crange; i++) {
437 fprintf(fp, "# %d: %fx%f+%f+%f\n", g->code, g->width, g->height, g->x, g->y);
438 g++;
439 }
440 fprintf(fp, "255\n");
442 num_pixels = gmap->xsz * gmap->ysz;
443 for(i=0; i<num_pixels; i++) {
444 int c = gmap->pixels[i];
445 fputc(c, fp);
446 fputc(c, fp);
447 fputc(c, fp);
448 }
449 return 0;
450 }
453 static void calc_best_size(int total_width, int max_glyph_height, int padding, int pow2, int *imgw, int *imgh)
454 {
455 int xsz, ysz, num_rows;
456 float aspect;
458 for(xsz=2; xsz<=MAX_IMG_WIDTH; xsz *= 2) {
459 num_rows = total_width / xsz + 1;
461 /* take into account the one extra padding for each row after the first */
462 num_rows = (total_width + padding * (num_rows - 1)) / xsz + 1;
464 ysz = num_rows * (max_glyph_height + padding) + padding;
465 if(pow2) {
466 ysz = next_pow2(ysz);
467 }
468 aspect = (float)xsz / (float)ysz;
470 if(aspect >= 1.0) {
471 break;
472 }
473 }
475 if(xsz > MAX_IMG_WIDTH) {
476 xsz = MAX_IMG_WIDTH;
477 }
479 *imgw = xsz;
480 *imgh = ysz;
481 }
484 static int next_pow2(int x)
485 {
486 x--;
487 x = (x >> 1) | x;
488 x = (x >> 2) | x;
489 x = (x >> 4) | x;
490 x = (x >> 8) | x;
491 x = (x >> 16) | x;
492 return x + 1;
493 }