libdrawtext

annotate src/font.c @ 13:365eba4dccec

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