libdrawtext

annotate src/font.c @ 22:df6d52b36bd6

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