3dphotoshoot

annotate 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
rev   line source
nuclear@10 1 /*
nuclear@10 2 libdrawtext - a simple library for fast text rendering in OpenGL
nuclear@10 3 Copyright (C) 2011-2014 John Tsiombikas <nuclear@member.fsf.org>
nuclear@10 4
nuclear@10 5 This program is free software: you can redistribute it and/or modify
nuclear@10 6 it under the terms of the GNU Lesser General Public License as published by
nuclear@10 7 the Free Software Foundation, either version 3 of the License, or
nuclear@10 8 (at your option) any later version.
nuclear@10 9
nuclear@10 10 This program is distributed in the hope that it will be useful,
nuclear@10 11 but WITHOUT ANY WARRANTY; without even the implied warranty of
nuclear@10 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
nuclear@10 13 GNU Lesser General Public License for more details.
nuclear@10 14
nuclear@10 15 You should have received a copy of the GNU Lesser General Public License
nuclear@10 16 along with this program. If not, see <http://www.gnu.org/licenses/>.
nuclear@10 17 */
nuclear@10 18 #ifndef NO_FREETYPE
nuclear@10 19 #define USE_FREETYPE
nuclear@10 20 #endif
nuclear@10 21
nuclear@10 22 #include <stdio.h>
nuclear@10 23 #include <stdlib.h>
nuclear@10 24 #include <string.h>
nuclear@10 25 #include <math.h>
nuclear@10 26 #include <limits.h>
nuclear@10 27 #include <ctype.h>
nuclear@10 28 #include <float.h>
nuclear@10 29 #include <errno.h>
nuclear@10 30 #ifdef USE_FREETYPE
nuclear@10 31 #include <ft2build.h>
nuclear@10 32 #include FT_FREETYPE_H
nuclear@10 33 #endif
nuclear@22 34 #include "assman.h"
nuclear@10 35 #include "drawtext.h"
nuclear@10 36 #include "drawtext_impl.h"
nuclear@10 37
nuclear@10 38 #define FTSZ_TO_PIXELS(x) ((x) / 64)
nuclear@10 39 #define MAX_IMG_WIDTH 4096
nuclear@10 40
nuclear@10 41
nuclear@10 42 #ifdef USE_FREETYPE
nuclear@10 43 static int init_freetype(void);
nuclear@10 44 static void cleanup(void);
nuclear@10 45
nuclear@10 46 static void calc_best_size(int total_width, int max_gwidth, int max_gheight,
nuclear@10 47 int padding, int pow2, int *imgw, int *imgh);
nuclear@10 48 static int next_pow2(int x);
nuclear@10 49
nuclear@10 50 static FT_Library ft;
nuclear@10 51
nuclear@10 52
nuclear@10 53 static int init_done;
nuclear@10 54
nuclear@10 55 static int init_freetype(void)
nuclear@10 56 {
nuclear@10 57 if(!init_done) {
nuclear@10 58 if(FT_Init_FreeType(&ft) != 0) {
nuclear@10 59 return -1;
nuclear@10 60 }
nuclear@10 61 atexit(cleanup);
nuclear@10 62 init_done = 1;
nuclear@10 63 }
nuclear@10 64 return 0;
nuclear@10 65 }
nuclear@10 66
nuclear@10 67 static void cleanup(void)
nuclear@10 68 {
nuclear@10 69 if(init_done) {
nuclear@10 70 FT_Done_FreeType(ft);
nuclear@10 71 }
nuclear@10 72 }
nuclear@10 73 #endif /* USE_FREETYPE */
nuclear@10 74
nuclear@10 75 struct dtx_font *dtx_open_font(const char *fname, int sz)
nuclear@10 76 {
nuclear@10 77 struct dtx_font *fnt = 0;
nuclear@10 78
nuclear@10 79 #ifdef USE_FREETYPE
nuclear@10 80 init_freetype();
nuclear@10 81
nuclear@10 82 if(!(fnt = calloc(1, sizeof *fnt))) {
nuclear@10 83 fperror("failed to allocate font structure");
nuclear@10 84 return 0;
nuclear@10 85 }
nuclear@10 86
nuclear@10 87 if(FT_New_Face(ft, fname, 0, (FT_Face*)&fnt->face) != 0) {
nuclear@10 88 fprintf(stderr, "failed to open font file: %s\n", fname);
nuclear@10 89 return 0;
nuclear@10 90 }
nuclear@10 91
nuclear@10 92 /* pre-create the extended ASCII range glyphmap */
nuclear@10 93 if(sz) {
nuclear@10 94 dtx_prepare_range(fnt, sz, 0, 256);
nuclear@10 95
nuclear@10 96 if(!dtx_font) {
nuclear@10 97 dtx_use_font(fnt, sz);
nuclear@10 98 }
nuclear@10 99 }
nuclear@10 100 #else
nuclear@10 101 fprintf(stderr, "ignoring call to dtx_open_font: not compiled with freetype support!\n");
nuclear@10 102 #endif
nuclear@10 103
nuclear@10 104 return fnt;
nuclear@10 105 }
nuclear@10 106
nuclear@10 107 struct dtx_font *dtx_open_font_glyphmap(const char *fname)
nuclear@10 108 {
nuclear@10 109 struct dtx_font *fnt;
nuclear@10 110 struct dtx_glyphmap *gmap;
nuclear@10 111
nuclear@10 112 if(!(fnt = calloc(1, sizeof *fnt))) {
nuclear@10 113 fperror("failed to allocate font structure");
nuclear@10 114 return 0;
nuclear@10 115 }
nuclear@10 116
nuclear@10 117 if(fname) {
nuclear@10 118 if(!(gmap = dtx_load_glyphmap(fname))) {
nuclear@10 119 free(fnt);
nuclear@10 120 return 0;
nuclear@10 121 }
nuclear@10 122
nuclear@10 123 dtx_add_glyphmap(fnt, gmap);
nuclear@10 124
nuclear@10 125 if(!dtx_font) {
nuclear@10 126 dtx_use_font(fnt, gmap->ptsize);
nuclear@10 127 }
nuclear@10 128 }
nuclear@10 129 return fnt;
nuclear@10 130 }
nuclear@10 131
nuclear@10 132 void dtx_close_font(struct dtx_font *fnt)
nuclear@10 133 {
nuclear@10 134 if(!fnt) return;
nuclear@10 135
nuclear@10 136 #ifdef USE_FREETYPE
nuclear@10 137 FT_Done_Face(fnt->face);
nuclear@10 138 #endif
nuclear@10 139
nuclear@10 140 /* destroy the glyphmaps */
nuclear@10 141 while(fnt->gmaps) {
nuclear@10 142 void *tmp = fnt->gmaps;
nuclear@10 143 fnt->gmaps = fnt->gmaps->next;
nuclear@10 144 dtx_free_glyphmap(tmp);
nuclear@10 145 }
nuclear@10 146
nuclear@10 147 free(fnt);
nuclear@10 148 }
nuclear@10 149
nuclear@10 150 void dtx_prepare(struct dtx_font *fnt, int sz)
nuclear@10 151 {
nuclear@10 152 if(!dtx_get_font_glyphmap_range(fnt, sz, 0, 256)) {
nuclear@10 153 fprintf(stderr, "%s: failed (sz: %d, range: 0-255 [ascii])\n", __FUNCTION__, sz);
nuclear@10 154 }
nuclear@10 155 }
nuclear@10 156
nuclear@10 157 void dtx_prepare_range(struct dtx_font *fnt, int sz, int cstart, int cend)
nuclear@10 158 {
nuclear@10 159 if(!dtx_get_font_glyphmap_range(fnt, sz, cstart, cend)) {
nuclear@10 160 fprintf(stderr, "%s: failed (sz: %d, range: %d-%d)\n", __FUNCTION__, sz, cstart, cend);
nuclear@10 161 }
nuclear@10 162 }
nuclear@10 163
nuclear@10 164 struct dtx_glyphmap *dtx_get_font_glyphmap(struct dtx_font *fnt, int sz, int code)
nuclear@10 165 {
nuclear@10 166 struct dtx_glyphmap *gm;
nuclear@10 167
nuclear@10 168 /* check to see if the last we've given out fits the bill */
nuclear@10 169 if(fnt->last_gmap && code >= fnt->last_gmap->cstart && code < fnt->last_gmap->cend && fnt->last_gmap->ptsize == sz) {
nuclear@10 170 return fnt->last_gmap;
nuclear@10 171 }
nuclear@10 172
nuclear@10 173 /* otherwise search for the appropriate glyphmap */
nuclear@10 174 gm = fnt->gmaps;
nuclear@10 175 while(gm) {
nuclear@10 176 if(code >= gm->cstart && code < gm->cend && sz == gm->ptsize) {
nuclear@10 177 fnt->last_gmap = gm;
nuclear@10 178 return gm;
nuclear@10 179 }
nuclear@10 180 gm = gm->next;
nuclear@10 181 }
nuclear@10 182 return 0;
nuclear@10 183 }
nuclear@10 184
nuclear@10 185 struct dtx_glyphmap *dtx_get_font_glyphmap_range(struct dtx_font *fnt, int sz, int cstart, int cend)
nuclear@10 186 {
nuclear@10 187 struct dtx_glyphmap *gm;
nuclear@10 188
nuclear@10 189 /* search the available glyphmaps to see if we've got one that includes
nuclear@10 190 * the requested range
nuclear@10 191 */
nuclear@10 192 gm = fnt->gmaps;
nuclear@10 193 while(gm) {
nuclear@10 194 if(gm->cstart <= cstart && gm->cend >= cend && gm->ptsize == sz) {
nuclear@10 195 return gm;
nuclear@10 196 }
nuclear@10 197 gm = gm->next;
nuclear@10 198 }
nuclear@10 199
nuclear@10 200 /* not found, create one and add it to the list */
nuclear@10 201 if(!(gm = dtx_create_glyphmap_range(fnt, sz, cstart, cend))) {
nuclear@10 202 return 0;
nuclear@10 203 }
nuclear@10 204 return gm;
nuclear@10 205 }
nuclear@10 206
nuclear@10 207 struct dtx_glyphmap *dtx_create_glyphmap_range(struct dtx_font *fnt, int sz, int cstart, int cend)
nuclear@10 208 {
nuclear@10 209 struct dtx_glyphmap *gmap = 0;
nuclear@10 210
nuclear@10 211 #ifdef USE_FREETYPE
nuclear@10 212 FT_Face face = fnt->face;
nuclear@10 213 int i, j;
nuclear@10 214 int gx, gy;
nuclear@10 215 int padding = 4;
nuclear@10 216 int total_width, max_width, max_height;
nuclear@10 217
nuclear@10 218 FT_Set_Char_Size(fnt->face, 0, sz * 64, 72, 72);
nuclear@10 219
nuclear@10 220 if(!(gmap = calloc(1, sizeof *gmap))) {
nuclear@10 221 return 0;
nuclear@10 222 }
nuclear@10 223
nuclear@10 224 gmap->ptsize = sz;
nuclear@10 225 gmap->cstart = cstart;
nuclear@10 226 gmap->cend = cend;
nuclear@10 227 gmap->crange = cend - cstart;
nuclear@10 228 gmap->line_advance = FTSZ_TO_PIXELS((float)face->size->metrics.height);
nuclear@10 229
nuclear@10 230 if(!(gmap->glyphs = malloc(gmap->crange * sizeof *gmap->glyphs))) {
nuclear@10 231 free(gmap);
nuclear@10 232 return 0;
nuclear@10 233 }
nuclear@10 234
nuclear@10 235 total_width = padding;
nuclear@10 236 max_width = max_height = 0;
nuclear@10 237
nuclear@10 238 for(i=0; i<gmap->crange; i++) {
nuclear@10 239 int w, h;
nuclear@10 240
nuclear@10 241 FT_Load_Char(face, i + cstart, 0);
nuclear@10 242 w = FTSZ_TO_PIXELS(face->glyph->metrics.width);
nuclear@10 243 h = FTSZ_TO_PIXELS(face->glyph->metrics.height);
nuclear@10 244
nuclear@10 245 if(w > max_width) max_width = w;
nuclear@10 246 if(h > max_height) max_height = h;
nuclear@10 247
nuclear@10 248 total_width += w + padding;
nuclear@10 249 }
nuclear@10 250
nuclear@10 251 calc_best_size(total_width, max_width, max_height, padding, 1, &gmap->xsz, &gmap->ysz);
nuclear@10 252
nuclear@10 253 if(!(gmap->pixels = malloc(gmap->xsz * gmap->ysz))) {
nuclear@10 254 free(gmap->glyphs);
nuclear@10 255 free(gmap);
nuclear@10 256 return 0;
nuclear@10 257 }
nuclear@10 258 memset(gmap->pixels, 0, gmap->xsz * gmap->ysz);
nuclear@10 259
nuclear@10 260 gx = padding;
nuclear@10 261 gy = padding;
nuclear@10 262
nuclear@10 263 for(i=0; i<gmap->crange; i++) {
nuclear@10 264 float gwidth, gheight;
nuclear@10 265 unsigned char *src, *dst;
nuclear@10 266 FT_GlyphSlot glyph;
nuclear@10 267
nuclear@10 268 FT_Load_Char(face, i + cstart, FT_LOAD_RENDER);
nuclear@10 269 glyph = face->glyph;
nuclear@10 270 gwidth = FTSZ_TO_PIXELS((float)glyph->metrics.width);
nuclear@10 271 gheight = FTSZ_TO_PIXELS((float)glyph->metrics.height);
nuclear@10 272
nuclear@10 273 if(gx > gmap->xsz - gwidth - padding) {
nuclear@10 274 gx = padding;
nuclear@10 275 gy += max_height + padding;
nuclear@10 276 }
nuclear@10 277
nuclear@10 278 src = glyph->bitmap.buffer;
nuclear@10 279 dst = gmap->pixels + gy * gmap->xsz + gx;
nuclear@10 280
nuclear@10 281 for(j=0; j<glyph->bitmap.rows; j++) {
nuclear@10 282 memcpy(dst, src, glyph->bitmap.width);
nuclear@10 283 dst += gmap->xsz;
nuclear@10 284 src += glyph->bitmap.pitch;
nuclear@10 285 }
nuclear@10 286
nuclear@10 287 gmap->glyphs[i].code = i;
nuclear@10 288 gmap->glyphs[i].x = gx - 1;
nuclear@10 289 gmap->glyphs[i].y = gy - 1;
nuclear@10 290 gmap->glyphs[i].width = gwidth + 2;
nuclear@10 291 gmap->glyphs[i].height = gheight + 2;
nuclear@10 292 gmap->glyphs[i].orig_x = -FTSZ_TO_PIXELS((float)glyph->metrics.horiBearingX) + 1;
nuclear@10 293 gmap->glyphs[i].orig_y = FTSZ_TO_PIXELS((float)glyph->metrics.height - glyph->metrics.horiBearingY) + 1;
nuclear@10 294 gmap->glyphs[i].advance = FTSZ_TO_PIXELS((float)glyph->metrics.horiAdvance);
nuclear@10 295 /* also precalc normalized */
nuclear@10 296 gmap->glyphs[i].nx = (float)gmap->glyphs[i].x / (float)gmap->xsz;
nuclear@10 297 gmap->glyphs[i].ny = (float)gmap->glyphs[i].y / (float)gmap->ysz;
nuclear@10 298 gmap->glyphs[i].nwidth = (float)gmap->glyphs[i].width / (float)gmap->xsz;
nuclear@10 299 gmap->glyphs[i].nheight = (float)gmap->glyphs[i].height / (float)gmap->ysz;
nuclear@10 300
nuclear@10 301 gx += gwidth + padding;
nuclear@10 302 }
nuclear@10 303
nuclear@10 304 /* add it to the glyphmaps list of the font */
nuclear@10 305 dtx_add_glyphmap(fnt, gmap);
nuclear@10 306 #endif /* USE_FREETYPE */
nuclear@10 307
nuclear@10 308 return gmap;
nuclear@10 309 }
nuclear@10 310
nuclear@10 311 void dtx_free_glyphmap(struct dtx_glyphmap *gmap)
nuclear@10 312 {
nuclear@10 313 if(gmap) {
nuclear@10 314 free(gmap->pixels);
nuclear@10 315 free(gmap->glyphs);
nuclear@10 316 free(gmap);
nuclear@10 317 }
nuclear@10 318 }
nuclear@10 319
nuclear@10 320 unsigned char *dtx_get_glyphmap_pixels(struct dtx_glyphmap *gmap)
nuclear@10 321 {
nuclear@10 322 return gmap->pixels;
nuclear@10 323 }
nuclear@10 324
nuclear@10 325 int dtx_get_glyphmap_width(struct dtx_glyphmap *gmap)
nuclear@10 326 {
nuclear@10 327 return gmap->xsz;
nuclear@10 328 }
nuclear@10 329
nuclear@10 330 int dtx_get_glyphmap_height(struct dtx_glyphmap *gmap)
nuclear@10 331 {
nuclear@10 332 return gmap->ysz;
nuclear@10 333 }
nuclear@10 334
nuclear@10 335 struct dtx_glyphmap *dtx_load_glyphmap(const char *fname)
nuclear@10 336 {
nuclear@22 337 ass_file *fp;
nuclear@10 338 struct dtx_glyphmap *gmap;
nuclear@10 339
nuclear@22 340 if(!(fp = ass_fopen(fname, "rb"))) {
nuclear@10 341 return 0;
nuclear@10 342 }
nuclear@10 343 gmap = dtx_load_glyphmap_stream(fp);
nuclear@22 344 ass_fclose(fp);
nuclear@10 345 return gmap;
nuclear@10 346 }
nuclear@10 347
nuclear@22 348 struct dtx_glyphmap *dtx_load_glyphmap_stream(ass_file *fp)
nuclear@10 349 {
nuclear@10 350 char buf[512];
nuclear@10 351 int hdr_lines = 0;
nuclear@10 352 struct dtx_glyphmap *gmap;
nuclear@10 353 struct glyph *glyphs = 0;
nuclear@10 354 struct glyph *g;
nuclear@10 355 int min_code = INT_MAX;
nuclear@10 356 int max_code = INT_MIN;
nuclear@10 357 int i, max_pixval, num_pixels;
nuclear@10 358
nuclear@10 359 if(!(gmap = calloc(1, sizeof *gmap))) {
nuclear@10 360 fperror("failed to allocate glyphmap");
nuclear@10 361 return 0;
nuclear@10 362 }
nuclear@10 363 gmap->ptsize = -1;
nuclear@10 364 gmap->line_advance = FLT_MIN;
nuclear@10 365
nuclear@10 366 while(hdr_lines < 3) {
nuclear@10 367 char *line = buf;
nuclear@22 368 if(!ass_fgets(buf, sizeof buf, fp)) {
nuclear@10 369 fperror("unexpected end of file");
nuclear@10 370 goto err;
nuclear@10 371 }
nuclear@10 372
nuclear@10 373 while(isspace(*line)) {
nuclear@10 374 line++;
nuclear@10 375 }
nuclear@10 376
nuclear@10 377 if(line[0] == '#') {
nuclear@10 378 int c, res;
nuclear@10 379 float x, y, xsz, ysz, orig_x, orig_y, adv, line_adv;
nuclear@10 380 int ptsize;
nuclear@10 381
nuclear@10 382 if((res = sscanf(line + 1, " size: %d\n", &ptsize)) == 1) {
nuclear@10 383 gmap->ptsize = ptsize;
nuclear@10 384
nuclear@10 385 } else if((res = sscanf(line + 1, " advance: %f\n", &line_adv)) == 1) {
nuclear@10 386 gmap->line_advance = line_adv;
nuclear@10 387
nuclear@10 388 } else if((res = sscanf(line + 1, " %d: %fx%f+%f+%f o:%f,%f adv:%f\n",
nuclear@10 389 &c, &xsz, &ysz, &x, &y, &orig_x, &orig_y, &adv)) == 8) {
nuclear@10 390 if(!(g = malloc(sizeof *g))) {
nuclear@10 391 fperror("failed to allocate glyph");
nuclear@10 392 goto err;
nuclear@10 393 }
nuclear@10 394 g->code = c;
nuclear@10 395 g->x = x;
nuclear@10 396 g->y = y;
nuclear@10 397 g->width = xsz;
nuclear@10 398 g->height = ysz;
nuclear@10 399 g->orig_x = orig_x;
nuclear@10 400 g->orig_y = orig_y;
nuclear@10 401 g->advance = adv;
nuclear@10 402 /* normalized coordinates will be precalculated after everything is loaded */
nuclear@10 403
nuclear@10 404 g->next = glyphs;
nuclear@10 405 glyphs = g;
nuclear@10 406
nuclear@10 407 if(c < min_code) {
nuclear@10 408 min_code = c;
nuclear@10 409 }
nuclear@10 410 if(c > max_code) {
nuclear@10 411 max_code = c;
nuclear@10 412 }
nuclear@10 413
nuclear@10 414 } else {
nuclear@10 415 fprintf(stderr, "%s: invalid glyph info line\n", __FUNCTION__);
nuclear@10 416 goto err;
nuclear@10 417 }
nuclear@10 418
nuclear@10 419 } else {
nuclear@10 420 switch(hdr_lines) {
nuclear@10 421 case 0:
nuclear@10 422 if(0[line] != 'P' || 1[line] != '6') {
nuclear@10 423 fprintf(stderr, "%s: invalid file format (magic)\n", __FUNCTION__);
nuclear@10 424 goto err;
nuclear@10 425 }
nuclear@10 426 break;
nuclear@10 427
nuclear@10 428 case 1:
nuclear@10 429 if(sscanf(line, "%d %d", &gmap->xsz, &gmap->ysz) != 2) {
nuclear@10 430 fprintf(stderr, "%s: invalid file format (dim)\n", __FUNCTION__);
nuclear@10 431 goto err;
nuclear@10 432 }
nuclear@10 433 break;
nuclear@10 434
nuclear@10 435 case 2:
nuclear@10 436 {
nuclear@10 437 char *endp;
nuclear@10 438 max_pixval = strtol(line, &endp, 10);
nuclear@10 439 if(endp == line) {
nuclear@10 440 fprintf(stderr, "%s: invalid file format (maxval)\n", __FUNCTION__);
nuclear@10 441 goto err;
nuclear@10 442 }
nuclear@10 443 }
nuclear@10 444 break;
nuclear@10 445
nuclear@10 446 default:
nuclear@10 447 break; /* can't happen */
nuclear@10 448 }
nuclear@10 449 hdr_lines++;
nuclear@10 450 }
nuclear@10 451 }
nuclear@10 452
nuclear@10 453 if(gmap->ptsize == -1 || gmap->line_advance == FLT_MIN) {
nuclear@10 454 fprintf(stderr, "%s: invalid glyphmap, insufficient information in ppm comments\n", __FUNCTION__);
nuclear@10 455 goto err;
nuclear@10 456 }
nuclear@10 457
nuclear@10 458 /* precalculate normalized glyph coordinates */
nuclear@10 459 g = glyphs;
nuclear@10 460 while(g) {
nuclear@10 461 g->nx = g->x / gmap->xsz;
nuclear@10 462 g->ny = g->y / gmap->ysz;
nuclear@10 463 g->nwidth = g->width / gmap->xsz;
nuclear@10 464 g->nheight = g->height / gmap->ysz;
nuclear@10 465 g = g->next;
nuclear@10 466 }
nuclear@10 467
nuclear@10 468 num_pixels = gmap->xsz * gmap->ysz;
nuclear@10 469 if(!(gmap->pixels = malloc(num_pixels))) {
nuclear@10 470 fperror("failed to allocate pixels");
nuclear@10 471 goto err;
nuclear@10 472 }
nuclear@10 473
nuclear@10 474 for(i=0; i<num_pixels; i++) {
nuclear@22 475 long c = ass_fgetc(fp);
nuclear@10 476 if(c == -1) {
nuclear@10 477 fprintf(stderr, "unexpected end of file while reading pixels\n");
nuclear@10 478 goto err;
nuclear@10 479 }
nuclear@10 480 gmap->pixels[i] = 255 * c / max_pixval;
nuclear@22 481 ass_fseek(fp, 2, SEEK_CUR);
nuclear@10 482 }
nuclear@10 483
nuclear@10 484 gmap->cstart = min_code;
nuclear@10 485 gmap->cend = max_code + 1;
nuclear@10 486 gmap->crange = gmap->cend - gmap->cstart;
nuclear@10 487
nuclear@10 488 if(!(gmap->glyphs = calloc(gmap->crange, sizeof *gmap->glyphs))) {
nuclear@10 489 fperror("failed to allocate glyph info");
nuclear@10 490 goto err;
nuclear@10 491 }
nuclear@10 492
nuclear@10 493 while(glyphs) {
nuclear@10 494 struct glyph *g = glyphs;
nuclear@10 495 glyphs = glyphs->next;
nuclear@10 496
nuclear@10 497 gmap->glyphs[g->code - gmap->cstart] = *g;
nuclear@10 498 free(g);
nuclear@10 499 }
nuclear@10 500 return gmap;
nuclear@10 501
nuclear@10 502 err:
nuclear@10 503 dtx_free_glyphmap(gmap);
nuclear@10 504 while(glyphs) {
nuclear@10 505 void *tmp = glyphs;
nuclear@10 506 glyphs = glyphs->next;
nuclear@10 507 free(tmp);
nuclear@10 508 }
nuclear@10 509 return 0;
nuclear@10 510 }
nuclear@10 511
nuclear@10 512 int dtx_save_glyphmap(const char *fname, const struct dtx_glyphmap *gmap)
nuclear@10 513 {
nuclear@10 514 FILE *fp;
nuclear@10 515 int res;
nuclear@10 516
nuclear@10 517 if(!(fp = fopen(fname, "wb"))) {
nuclear@10 518 fprintf(stderr, "%s: failed to open file: %s: %s\n", __FUNCTION__, fname, strerror(errno));
nuclear@10 519 return -1;
nuclear@10 520 }
nuclear@10 521 res = dtx_save_glyphmap_stream(fp, gmap);
nuclear@10 522 fclose(fp);
nuclear@10 523 return res;
nuclear@10 524 }
nuclear@10 525
nuclear@10 526 int dtx_save_glyphmap_stream(FILE *fp, const struct dtx_glyphmap *gmap)
nuclear@10 527 {
nuclear@10 528 int i, num_pixels;
nuclear@10 529 struct glyph *g = gmap->glyphs;
nuclear@10 530
nuclear@10 531 fprintf(fp, "P6\n%d %d\n", gmap->xsz, gmap->ysz);
nuclear@10 532 fprintf(fp, "# size: %d\n", gmap->ptsize);
nuclear@10 533 fprintf(fp, "# advance: %g\n", gmap->line_advance);
nuclear@10 534 for(i=0; i<gmap->crange; i++) {
nuclear@10 535 fprintf(fp, "# %d: %gx%g+%g+%g o:%g,%g adv:%g\n", g->code, g->width, g->height, g->x, g->y,
nuclear@10 536 g->orig_x, g->orig_y, g->advance);
nuclear@10 537 g++;
nuclear@10 538 }
nuclear@10 539 fprintf(fp, "255\n");
nuclear@10 540
nuclear@10 541 num_pixels = gmap->xsz * gmap->ysz;
nuclear@10 542 for(i=0; i<num_pixels; i++) {
nuclear@10 543 int c = gmap->pixels[i];
nuclear@10 544 fputc(c, fp);
nuclear@10 545 fputc(c, fp);
nuclear@10 546 fputc(c, fp);
nuclear@10 547 }
nuclear@10 548 return 0;
nuclear@10 549 }
nuclear@10 550
nuclear@10 551 void dtx_add_glyphmap(struct dtx_font *fnt, struct dtx_glyphmap *gmap)
nuclear@10 552 {
nuclear@10 553 gmap->next = fnt->gmaps;
nuclear@10 554 fnt->gmaps = gmap;
nuclear@10 555 }
nuclear@10 556
nuclear@10 557
nuclear@10 558 void dtx_use_font(struct dtx_font *fnt, int sz)
nuclear@10 559 {
nuclear@10 560 dtx_gl_init();
nuclear@10 561
nuclear@10 562 dtx_font = fnt;
nuclear@10 563 dtx_font_sz = sz;
nuclear@10 564 }
nuclear@10 565
nuclear@10 566 float dtx_line_height(void)
nuclear@10 567 {
nuclear@10 568 struct dtx_glyphmap *gmap = dtx_get_font_glyphmap(dtx_font, dtx_font_sz, '\n');
nuclear@10 569
nuclear@10 570 return gmap->line_advance;
nuclear@10 571 }
nuclear@10 572
nuclear@10 573 void dtx_glyph_box(int code, struct dtx_box *box)
nuclear@10 574 {
nuclear@10 575 int cidx;
nuclear@10 576 struct dtx_glyphmap *gmap;
nuclear@10 577 gmap = dtx_get_font_glyphmap(dtx_font, dtx_font_sz, code);
nuclear@10 578
nuclear@10 579 cidx = code - gmap->cstart;
nuclear@10 580
nuclear@10 581 box->x = gmap->glyphs[cidx].orig_x;
nuclear@10 582 box->y = gmap->glyphs[cidx].orig_y;
nuclear@10 583 box->width = gmap->glyphs[cidx].width;
nuclear@10 584 box->height = gmap->glyphs[cidx].height;
nuclear@10 585 }
nuclear@10 586
nuclear@10 587 float dtx_glyph_width(int code)
nuclear@10 588 {
nuclear@10 589 struct dtx_box box;
nuclear@10 590 dtx_glyph_box(code, &box);
nuclear@10 591 return box.width;
nuclear@10 592 }
nuclear@10 593
nuclear@10 594 float dtx_glyph_height(int code)
nuclear@10 595 {
nuclear@10 596 struct dtx_box box;
nuclear@10 597 dtx_glyph_box(code, &box);
nuclear@10 598 return box.height;
nuclear@10 599 }
nuclear@10 600
nuclear@10 601 void dtx_string_box(const char *str, struct dtx_box *box)
nuclear@10 602 {
nuclear@10 603 int code;
nuclear@10 604 float pos_x = 0.0f, pos_y = 0.0f;
nuclear@10 605 struct glyph *g = 0;
nuclear@10 606 float x0, y0, x1, y1;
nuclear@10 607
nuclear@10 608 x0 = y0 = FLT_MAX;
nuclear@10 609 x1 = y1 = -FLT_MAX;
nuclear@10 610
nuclear@10 611 while(*str) {
nuclear@10 612 float px, py;
nuclear@10 613 struct dtx_glyphmap *gmap;
nuclear@10 614
nuclear@10 615 code = dtx_utf8_char_code(str);
nuclear@10 616 str = dtx_utf8_next_char((char*)str);
nuclear@10 617
nuclear@10 618 px = pos_x;
nuclear@10 619 py = pos_y;
nuclear@10 620
nuclear@10 621 if((gmap = dtx_proc_char(code, &pos_x, &pos_y))) {
nuclear@10 622 g = gmap->glyphs + code - gmap->cstart;
nuclear@10 623
nuclear@10 624 if(px + g->orig_x < x0) {
nuclear@10 625 x0 = px + g->orig_x;
nuclear@10 626 }
nuclear@10 627 if(py - g->orig_y < y0) {
nuclear@10 628 y0 = py - g->orig_y;
nuclear@10 629 }
nuclear@10 630 if(px + g->orig_x + g->width > x1) {
nuclear@10 631 x1 = px + g->orig_x + g->width;
nuclear@10 632 }
nuclear@10 633 if(py - g->orig_y + g->height > y1) {
nuclear@10 634 y1 = py - g->orig_y + g->height;
nuclear@10 635 }
nuclear@10 636 }
nuclear@10 637 }
nuclear@10 638
nuclear@10 639 box->x = x0;
nuclear@10 640 box->y = y0;
nuclear@10 641 box->width = x1 - x0;
nuclear@10 642 box->height = y1 - y0;
nuclear@10 643 }
nuclear@10 644
nuclear@10 645 float dtx_string_width(const char *str)
nuclear@10 646 {
nuclear@10 647 struct dtx_box box;
nuclear@10 648
nuclear@10 649 dtx_string_box(str, &box);
nuclear@10 650 return box.width;
nuclear@10 651 }
nuclear@10 652
nuclear@10 653 float dtx_string_height(const char *str)
nuclear@10 654 {
nuclear@10 655 struct dtx_box box;
nuclear@10 656
nuclear@10 657 dtx_string_box(str, &box);
nuclear@10 658 return box.height;
nuclear@10 659 }
nuclear@10 660
nuclear@10 661 float dtx_char_pos(const char *str, int n)
nuclear@10 662 {
nuclear@10 663 int i;
nuclear@10 664 float pos = 0.0;
nuclear@10 665 struct dtx_glyphmap *gmap;
nuclear@10 666
nuclear@10 667 for(i=0; i<n; i++) {
nuclear@10 668 int code = dtx_utf8_char_code(str);
nuclear@10 669 str = dtx_utf8_next_char((char*)str);
nuclear@10 670
nuclear@10 671 gmap = dtx_get_font_glyphmap(dtx_font, dtx_font_sz, code);
nuclear@10 672 pos += gmap->glyphs[i].advance;
nuclear@10 673 }
nuclear@10 674 return pos;
nuclear@10 675 }
nuclear@10 676
nuclear@10 677 int dtx_char_at_pt(const char *str, float pt)
nuclear@10 678 {
nuclear@10 679 int i;
nuclear@10 680 float prev_pos = 0.0f, pos = 0.0f;
nuclear@10 681 struct dtx_glyphmap *gmap;
nuclear@10 682
nuclear@10 683 for(i=0; *str; i++) {
nuclear@10 684 int code = dtx_utf8_char_code(str);
nuclear@10 685 str = dtx_utf8_next_char((char*)str);
nuclear@10 686
nuclear@10 687 gmap = dtx_get_font_glyphmap(dtx_font, dtx_font_sz, code);
nuclear@10 688 pos += gmap->glyphs[i].advance;
nuclear@10 689
nuclear@10 690 if(fabs(pt - prev_pos) < fabs(pt - pos)) {
nuclear@10 691 break;
nuclear@10 692 }
nuclear@10 693 prev_pos = pos;
nuclear@10 694 }
nuclear@10 695 return i;
nuclear@10 696 }
nuclear@10 697
nuclear@10 698 struct dtx_glyphmap *dtx_proc_char(int code, float *xpos, float *ypos)
nuclear@10 699 {
nuclear@10 700 struct dtx_glyphmap *gmap;
nuclear@10 701 gmap = dtx_get_font_glyphmap(dtx_font, dtx_font_sz, code);
nuclear@10 702
nuclear@10 703 switch(code) {
nuclear@10 704 case '\n':
nuclear@10 705 *xpos = 0.0;
nuclear@10 706 if(gmap) {
nuclear@10 707 *ypos -= gmap->line_advance;
nuclear@10 708 }
nuclear@10 709 return 0;
nuclear@10 710
nuclear@10 711 case '\t':
nuclear@10 712 if(gmap) {
nuclear@10 713 *xpos = (fmod(*xpos, 4.0) + 4.0) * gmap->glyphs[0].advance;
nuclear@10 714 }
nuclear@10 715 return 0;
nuclear@10 716
nuclear@10 717 case '\r':
nuclear@10 718 *xpos = 0.0;
nuclear@10 719 return 0;
nuclear@10 720
nuclear@10 721 default:
nuclear@10 722 break;
nuclear@10 723 }
nuclear@10 724
nuclear@10 725 if(gmap) {
nuclear@10 726 *xpos += gmap->glyphs[code - gmap->cstart].advance;
nuclear@10 727 }
nuclear@10 728 return gmap;
nuclear@10 729 }
nuclear@10 730
nuclear@10 731 #ifdef USE_FREETYPE
nuclear@10 732 static void calc_best_size(int total_width, int max_gwidth, int max_gheight, int padding, int pow2, int *imgw, int *imgh)
nuclear@10 733 {
nuclear@10 734 int xsz, ysz, num_rows;
nuclear@10 735 float aspect;
nuclear@10 736
nuclear@10 737 for(xsz=2; xsz<=MAX_IMG_WIDTH; xsz *= 2) {
nuclear@10 738 num_rows = total_width / xsz + 1;
nuclear@10 739
nuclear@10 740 /* assume worst case, all last glyphs will float to the next line
nuclear@10 741 * so let's add extra rows for that. */
nuclear@10 742 num_rows += (padding + (max_gwidth + padding) * num_rows + xsz - 1) / xsz;
nuclear@10 743
nuclear@10 744 ysz = num_rows * (max_gheight + padding) + padding;
nuclear@10 745 if(pow2) {
nuclear@10 746 ysz = next_pow2(ysz);
nuclear@10 747 }
nuclear@10 748 aspect = (float)xsz / (float)ysz;
nuclear@10 749
nuclear@10 750 if(aspect >= 1.0) {
nuclear@10 751 break;
nuclear@10 752 }
nuclear@10 753 }
nuclear@10 754
nuclear@10 755 if(xsz > MAX_IMG_WIDTH) {
nuclear@10 756 xsz = MAX_IMG_WIDTH;
nuclear@10 757 }
nuclear@10 758
nuclear@10 759 *imgw = xsz;
nuclear@10 760 *imgh = ysz;
nuclear@10 761 }
nuclear@10 762
nuclear@10 763
nuclear@10 764 static int next_pow2(int x)
nuclear@10 765 {
nuclear@10 766 x--;
nuclear@10 767 x = (x >> 1) | x;
nuclear@10 768 x = (x >> 2) | x;
nuclear@10 769 x = (x >> 4) | x;
nuclear@10 770 x = (x >> 8) | x;
nuclear@10 771 x = (x >> 16) | x;
nuclear@10 772 return x + 1;
nuclear@10 773 }
nuclear@10 774 #endif