libdrawtext

annotate src/font.c @ 2:d8c3ce3fa588

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