File: texfont.c

package info (click to toggle)
conquest 8.1-2
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 7,984 kB
  • ctags: 3,086
  • sloc: ansic: 39,393; sh: 8,540; yacc: 446; makefile: 296; lex: 146
file content (610 lines) | stat: -rw-r--r-- 15,353 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610

/* Copyright (c) Mark J. Kilgard, 1997. */

/* JET - I've modified it a little, but it works nicely. */
/* $Id: texfont.c,v 1.3 2004/09/04 07:38:02 jon Exp $ */
/* This program is freely distributable without licensing fees  and is
   provided without guarantee or warrantee expressed or  implied. This
   program is -not- in the public domain. */

#if defined(_WIN32)
#pragma warning (disable:4244)          /* disable bogus conversion warnings */
#endif

#include "c_defs.h"
#include "color.h"
#include "ui.h"
#include "gldisplay.h"
#include <assert.h>
#include <ctype.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#ifdef _WIN32
#include <windows.h>
#endif
#include <GL/glu.h>
#include "TexFont.h"

#if 0
/* Uncomment to debug various scenarios. */
#undef GL_VERSION_1_1
#undef GL_EXT_texture_object
#undef GL_EXT_texture
#endif /* 0 */

#ifndef GL_VERSION_1_1

#  if defined(GL_EXT_texture_object)
#    define glGenTextures glGenTexturesEXT
#    define glBindTexture glBindTextureEXT
#  else
     /* Without OpenGL 1.1 or the texture object extension, use display lists. */
#    define USE_DISPLAY_LISTS
#  endif

#  if defined(GL_EXT_texture)
#    define GL_INTENSITY4 GL_INTENSITY4_EXT
     int useLuminanceAlpha = 0;
#  else
     /* Intensity texture format not in OpenGL 1.0; added by the EXT_texture
        extension and now part of OpenGL 1.1. */
     int useLuminanceAlpha = 1;
#  endif

#else
   /* OpenGL 1.1 case. */
   int useLuminanceAlpha = 0;
#endif

/* byte swap a 32-bit value */
#define SWAPL(x, n) { \
                 n = ((char *) (x))[0];\
                 ((char *) (x))[0] = ((char *) (x))[3];\
                 ((char *) (x))[3] = n;\
                 n = ((char *) (x))[1];\
                 ((char *) (x))[1] = ((char *) (x))[2];\
                 ((char *) (x))[2] = n; }

/* byte swap a short */
#define SWAPS(x, n) { \
                 n = ((char *) (x))[0];\
                 ((char *) (x))[0] = ((char *) (x))[1];\
                 ((char *) (x))[1] = n; }

static TexGlyphVertexInfo *
getTCVI(TexFont * txf, int c)
{
  TexGlyphVertexInfo *tgvi;

  /* Automatically substitute uppercase letters with lowercase if not
     uppercase available (and vice versa). */
  if ((c >= txf->min_glyph) && (c < txf->min_glyph + txf->range)) {
    tgvi = txf->lut[c - txf->min_glyph];
    if (tgvi) {
      return tgvi;
    }
    if (islower(c)) {
      c = toupper(c);
      if ((c >= txf->min_glyph) && (c < txf->min_glyph + txf->range)) {
        return txf->lut[c - txf->min_glyph];
      }
    }
    if (isupper(c)) {
      c = tolower(c);
      if ((c >= txf->min_glyph) && (c < txf->min_glyph + txf->range)) {
        return txf->lut[c - txf->min_glyph];
      }
    }
  }

#ifdef DEBUG_GL
  clog("texfont: tried to access unavailable font character \"%c\" (%d)\n",
       isprint(c) ? c : '?', c);
  assert(0); 
#endif

  return NULL;
}

static char *lastError;

char *
txfErrorString(void)
{
  return lastError;
}

TexFont *
txfLoadFont(char *filename)
{
  TexFont *txf;
  FILE *file;
  GLfloat w, h, xstep, ystep;
  char fileid[4], tmp;
  unsigned char *texbitmap;
  int min_glyph, max_glyph;
  int endianness, swap, format, stride, width, height;
  int i, j;
  unsigned long got;

  txf = NULL;
  file = fopen(filename, "rb");
  if (file == NULL) {
    lastError = "file open failed.";
    goto error;
  }
  txf = (TexFont *) malloc(sizeof(TexFont));
  if (txf == NULL) {
    lastError = "out of memory.";
    goto error;
  }
  /* For easy cleanup in error case. */
  txf->tgi = NULL;
  txf->tgvi = NULL;
  txf->lut = NULL;
  txf->teximage = NULL;

  /* JET - need to clear texobj... */
  txf->texobj = 0;

  got = fread(fileid, 1, 4, file);
  if (got != 4 || strncmp(fileid, "\377txf", 4)) {
    lastError = "not a texture font file.";
    goto error;
  }
  /*CONSTANTCONDITION*/
  assert(sizeof(int) == 4);  /* Ensure external file format size. */
  got = fread(&endianness, sizeof(int), 1, file);
  if (got == 1 && endianness == 0x12345678) {
    swap = 0;
  } else if (got == 1 && endianness == 0x78563412) {
    swap = 1;
  } else {
    lastError = "not a texture font file.";
    goto error;
  }
#define EXPECT(n) if (got != (unsigned long) n) { lastError = "premature end of file."; goto error; }
  got = fread(&format, sizeof(int), 1, file);
  EXPECT(1);
  got = fread(&txf->tex_width, sizeof(int), 1, file);
  EXPECT(1);
  got = fread(&txf->tex_height, sizeof(int), 1, file);
  EXPECT(1);
  got = fread(&txf->max_ascent, sizeof(int), 1, file);
  EXPECT(1);
  got = fread(&txf->max_descent, sizeof(int), 1, file);
  EXPECT(1);
  got = fread(&txf->num_glyphs, sizeof(int), 1, file);
  EXPECT(1);

  if (swap) {
    SWAPL(&format, tmp);
    SWAPL(&txf->tex_width, tmp);
    SWAPL(&txf->tex_height, tmp);
    SWAPL(&txf->max_ascent, tmp);
    SWAPL(&txf->max_descent, tmp);
    SWAPL(&txf->num_glyphs, tmp);
  }
  txf->tgi = (TexGlyphInfo *) malloc(txf->num_glyphs * sizeof(TexGlyphInfo));
  if (txf->tgi == NULL) {
    lastError = "out of memory.";
    goto error;
  }
  /*CONSTANTCONDITION*/
  assert(sizeof(TexGlyphInfo) == 12);  /* Ensure external file format size. */
  got = fread(txf->tgi, sizeof(TexGlyphInfo), txf->num_glyphs, file);
  EXPECT(txf->num_glyphs);

  if (swap) {
    for (i = 0; i < txf->num_glyphs; i++) {
      SWAPS(&txf->tgi[i].c, tmp);
      SWAPS(&txf->tgi[i].x, tmp);
      SWAPS(&txf->tgi[i].y, tmp);
    }
  }
  txf->tgvi = (TexGlyphVertexInfo *)
    malloc(txf->num_glyphs * sizeof(TexGlyphVertexInfo));
  if (txf->tgvi == NULL) {
    lastError = "out of memory.";
    goto error;
  }
  w = txf->tex_width;
  h = txf->tex_height;
#if 0
/* JET - this seems to clip off the glyph along the left and bottom
   edges.  Therefore, we will not do this. :) */
  xstep = 0.5 / w;
  ystep = 0.5 / h;
#else
  xstep = ystep = 0;
#endif

  for (i = 0; i < txf->num_glyphs; i++) {
    TexGlyphInfo *tgi;

    tgi = &txf->tgi[i];
    txf->tgvi[i].t0[0] = tgi->x / w + xstep;
    txf->tgvi[i].t0[1] = tgi->y / h + ystep;
    txf->tgvi[i].v0[0] = tgi->xoffset;
    txf->tgvi[i].v0[1] = tgi->yoffset;
    txf->tgvi[i].t1[0] = (tgi->x + tgi->width) / w + xstep;
    txf->tgvi[i].t1[1] = tgi->y / h + ystep;
    txf->tgvi[i].v1[0] = tgi->xoffset + tgi->width;
    txf->tgvi[i].v1[1] = tgi->yoffset;
    txf->tgvi[i].t2[0] = (tgi->x + tgi->width) / w + xstep;
    txf->tgvi[i].t2[1] = (tgi->y + tgi->height) / h + ystep;
    txf->tgvi[i].v2[0] = tgi->xoffset + tgi->width;
    txf->tgvi[i].v2[1] = tgi->yoffset + tgi->height;
    txf->tgvi[i].t3[0] = tgi->x / w + xstep;
    txf->tgvi[i].t3[1] = (tgi->y + tgi->height) / h + ystep;
    txf->tgvi[i].v3[0] = tgi->xoffset;
    txf->tgvi[i].v3[1] = tgi->yoffset + tgi->height;
    txf->tgvi[i].advance = tgi->advance;
  }

  min_glyph = txf->tgi[0].c;
  max_glyph = txf->tgi[0].c;
  for (i = 1; i < txf->num_glyphs; i++) {
    if (txf->tgi[i].c < min_glyph) {
      min_glyph = txf->tgi[i].c;
    }
    if (txf->tgi[i].c > max_glyph) {
      max_glyph = txf->tgi[i].c;
    }
  }
  txf->min_glyph = min_glyph;
  txf->range = max_glyph - min_glyph + 1;

  txf->lut = (TexGlyphVertexInfo **)
    calloc(txf->range, sizeof(TexGlyphVertexInfo *));
  if (txf->lut == NULL) {
    lastError = "out of memory.";
    goto error;
  }
  for (i = 0; i < txf->num_glyphs; i++) {
    txf->lut[txf->tgi[i].c - txf->min_glyph] = &txf->tgvi[i];
  }

  switch (format) {
  case TXF_FORMAT_BYTE:
    if (useLuminanceAlpha) {
      unsigned char *orig;

      orig = (unsigned char *) malloc(txf->tex_width * txf->tex_height);
      if (orig == NULL) {
        lastError = "out of memory.";
        goto error;
      }
      got = fread(orig, 1, txf->tex_width * txf->tex_height, file);
      EXPECT(txf->tex_width * txf->tex_height);
      txf->teximage = (unsigned char *)
        malloc(2 * txf->tex_width * txf->tex_height);
      if (txf->teximage == NULL) {
        lastError = "out of memory.";
        goto error;
      }
      for (i = 0; i < txf->tex_width * txf->tex_height; i++) {
        txf->teximage[i * 2] = orig[i];
        txf->teximage[i * 2 + 1] = orig[i];
      }
      free(orig);
    } else {
      txf->teximage = (unsigned char *)
        malloc(txf->tex_width * txf->tex_height);
      if (txf->teximage == NULL) {
        lastError = "out of memory.";
        goto error;
      }
      got = fread(txf->teximage, 1, txf->tex_width * txf->tex_height, file);
      EXPECT(txf->tex_width * txf->tex_height);
    }
    break;
  case TXF_FORMAT_BITMAP:
    width = txf->tex_width;
    height = txf->tex_height;
    stride = (width + 7) >> 3;
    texbitmap = (unsigned char *) malloc(stride * height);
    if (texbitmap == NULL) {
      lastError = "out of memory.";
      goto error;
    }
    got = fread(texbitmap, 1, stride * height, file);
    EXPECT(stride * height);

    if (useLuminanceAlpha) {
      txf->teximage = (unsigned char *) calloc(width * height * 2, 1);
      if (txf->teximage == NULL) {
        lastError = "out of memory.";
        goto error;
      }
      for (i = 0; i < height; i++) {
        for (j = 0; j < width; j++) {
          if (texbitmap[i * stride + (j >> 3)] & (1 << (j & 7))) {
            txf->teximage[(i * width + j) * 2] = 255;
            txf->teximage[(i * width + j) * 2 + 1] = 255;
          }
        }
      }
    } else {
      txf->teximage = (unsigned char *) calloc(width * height, 1);
      if (txf->teximage == NULL) {
        lastError = "out of memory.";
        goto error;
      }
      for (i = 0; i < height; i++) {
        for (j = 0; j < width; j++) {
          if (texbitmap[i * stride + (j >> 3)] & (1 << (j & 7))) {
            txf->teximage[i * width + j] = 255;
          }
        }
      }
    }
    free(texbitmap);
    break;
  }

  fclose(file);
  return txf;

error:

  if (txf) {
    if (txf->tgi)
      free(txf->tgi);
    if (txf->tgvi)
      free(txf->tgvi);
    if (txf->lut)
      free(txf->lut);
    if (txf->teximage)
      free(txf->teximage);
    free(txf);
  }
  if (file)
    fclose(file);
  return NULL;
}

GLuint
txfEstablishTexture(
  TexFont * txf,
  GLuint texobj,
  GLboolean setupMipmaps)
{
  if (txf->texobj == 0) {
    if (texobj == 0) {
#if !defined(USE_DISPLAY_LISTS)
      glGenTextures(1, &txf->texobj);
#else
      txf->texobj = glGenLists(1);
#endif
    } else {
      txf->texobj = texobj;
    }
  }
#if !defined(USE_DISPLAY_LISTS)
  glBindTexture(GL_TEXTURE_2D, txf->texobj);
#else
  glNewList(txf->texobj, GL_COMPILE);
#endif

#if 1
  /* XXX Indigo2 IMPACT in IRIX 5.3 and 6.2 does not support the GL_INTENSITY
     internal texture format. Sigh. Win32 non-GLX users should disable this
     code. */
  if (useLuminanceAlpha == 0) {
    char *vendor, *renderer, *version;

    renderer = (char *) glGetString(GL_RENDERER);
    vendor = (char *) glGetString(GL_VENDOR);
    if (!strcmp(vendor, "SGI") && !strncmp(renderer, "IMPACT", 6)) {
      version = (char *) glGetString(GL_VERSION);
      if (!strcmp(version, "1.0 Irix 6.2") ||
        !strcmp(version, "1.0 Irix 5.3")) {
        unsigned char *latex;
        int width = txf->tex_width;
        int height = txf->tex_height;
        int i;

        useLuminanceAlpha = 1;
        latex = (unsigned char *) calloc(width * height * 2, 1);
        /* XXX unprotected alloc. */
        for (i = 0; i < height * width; i++) {
          latex[i * 2] = txf->teximage[i];
          latex[i * 2 + 1] = txf->teximage[i];
        }
        free(txf->teximage);
        txf->teximage = latex;
      }
    }
  }
#endif

  if (useLuminanceAlpha) {
    if (setupMipmaps) {
      gluBuild2DMipmaps(GL_TEXTURE_2D, GL_LUMINANCE_ALPHA,
        txf->tex_width, txf->tex_height,
        GL_LUMINANCE_ALPHA, GL_UNSIGNED_BYTE, txf->teximage);
    } else {
      glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE_ALPHA,
        txf->tex_width, txf->tex_height, 0,
        GL_LUMINANCE_ALPHA, GL_UNSIGNED_BYTE, txf->teximage);
    }
  } else {
#if defined(GL_VERSION_1_1) || defined(GL_EXT_texture)
    /* Use GL_INTENSITY4 as internal texture format since we want to use as
       little texture memory as possible. */
    if (setupMipmaps) {
      gluBuild2DMipmaps(GL_TEXTURE_2D, GL_INTENSITY4,
        txf->tex_width, txf->tex_height,
        GL_LUMINANCE, GL_UNSIGNED_BYTE, txf->teximage);
    } else {
      glTexImage2D(GL_TEXTURE_2D, 0, GL_INTENSITY4,
        txf->tex_width, txf->tex_height, 0,
        GL_LUMINANCE, GL_UNSIGNED_BYTE, txf->teximage);
    }
#else
    abort();            /* Should not get here without EXT_texture or OpenGL
                           1.1. */
#endif
  }

#if defined(USE_DISPLAY_LISTS)
  glEndList();
  glCallList(txf->texobj);
#endif
  return txf->texobj;
}

void
txfBindFontTexture(
  TexFont * txf)
{
#if !defined(USE_DISPLAY_LISTS)
  glBindTexture(GL_TEXTURE_2D, txf->texobj);
#else
  glCallList(txf->texobj);
#endif
}

void
txfUnloadFont(
  TexFont * txf)
{
  if (txf->teximage) {
    free(txf->teximage);
  }
  free(txf->tgi);
  free(txf->tgvi);
  free(txf->lut);
  free(txf);
}

void
txfGetStringMetrics(
  TexFont * txf,
  char *string,
  int len,
  int *width,
  int *max_ascent,
  int *max_descent)
{
  TexGlyphVertexInfo *tgvi;
  int w, i;

  w = 0;
  for (i = 0; i < len; i++) 
    {
      if (string[i] == '#')
        {                       /* a color sequence */
          i++;
          while (isdigit(string[i]) && i < len)
            i++;

          if (string[i] == '#')
            continue;
        }

      tgvi = getTCVI(txf, string[i]);
      if (tgvi)
        w += tgvi->advance;
    }

  *width = w;
  *max_ascent = txf->max_ascent;
  *max_descent = txf->max_descent;
}

void
txfRenderGlyph(TexFont * txf, int c)
{
  TexGlyphVertexInfo *tgvi;

  tgvi = getTCVI(txf, c);
  if (!tgvi)
    return;

  glBegin(GL_QUADS);
  glTexCoord2fv(tgvi->t0);
  glVertex2sv(tgvi->v0);
  glTexCoord2fv(tgvi->t1);
  glVertex2sv(tgvi->v1);
  glTexCoord2fv(tgvi->t2);
  glVertex2sv(tgvi->v2);
  glTexCoord2fv(tgvi->t3);
  glVertex2sv(tgvi->v3);
  glEnd();
  glTranslatef(tgvi->advance, 0.0, 0.0);
}

void
txfRenderString(
  TexFont * txf,
  char *string,
  int len)
{
  int i;

  for (i = 0; i < len; i++) {
    txfRenderGlyph(txf, string[i]);
  }
}

enum {
  MONO, TOP_BOTTOM, LEFT_RIGHT, FOUR
};

/* JET - for this one, all we want to be able to do is handle embedded
   cqColor info, represented as '#<int color>#' */
void
txfRenderFancyString(
  TexFont * txf,
  char *string,
  int len)
{
  int i;
  char *ptr, buf256[256];
  int color;

  for (i = 0; i < len; i++) 
    {
      if (string[i] == '#')
        {                       /* a color sequence */
          i++;
          ptr = buf256;
          while (isdigit(string[i]) && i < len)
            {
              *ptr = string[i];
              i++;
              ptr++;
            }

          if (string[i] == '#')
            {                   /* we have complete sequence, emit a
                                   color */
              *ptr = 0;
              color = atoi(buf256);
              
              uiPutColor(color);
              continue;
            }
        }
      else
        txfRenderGlyph(txf, string[i]);
    }

  return;
}


int
txfInFont(TexFont * txf, int c)
{
  /* NOTE: No uppercase/lowercase substitution. */
  if ((c >= txf->min_glyph) && (c < txf->min_glyph + txf->range)) {
    if (txf->lut[c - txf->min_glyph]) {
      return 1;
    }
  }
  return 0;
}