libdrawtext

annotate src/font.c @ 38:49bc37890c38

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