libdrawtext

annotate src/font.c @ 47:00f98842608f

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