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
|
/*
===========================================================================
Copyright (C) 1999-2005 Id Software, Inc.
This file is part of Quake III Arena source code.
Quake III Arena source code is free software; you can redistribute it
and/or modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of the License,
or (at your option) any later version.
Quake III Arena source code is distributed in the hope that it will be
useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Quake III Arena source code; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
===========================================================================
*/
// tr_font.c
//
//
// The font system uses FreeType 2.x to render TrueType fonts for use within the game.
// As of this writing ( Nov, 2000 ) Team Arena uses these fonts for all of the ui and
// about 90% of the cgame presentation. A few areas of the CGAME were left uses the old
// fonts since the code is shared with standard Q3A.
//
// If you include this font rendering code in a commercial product you MUST include the
// following somewhere with your product, see www.freetype.org for specifics or changes.
// The Freetype code also uses some hinting techniques that MIGHT infringe on patents
// held by apple so be aware of that also.
//
// As of Q3A 1.25+ and Team Arena, we are shipping the game with the font rendering code
// disabled. This removes any potential patent issues and it keeps us from having to
// distribute an actual TrueTrype font which is 1. expensive to do and 2. seems to require
// an act of god to accomplish.
//
// What we did was pre-render the fonts using FreeType ( which is why we leave the FreeType
// credit in the credits ) and then saved off the glyph data and then hand touched up the
// font bitmaps so they scale a bit better in GL.
//
// There are limitations in the way fonts are saved and reloaded in that it is based on
// point size and not name. So if you pre-render Helvetica in 18 point and Impact in 18 point
// you will end up with a single 18 point data file and image set. Typically you will want to
// choose 3 sizes to best approximate the scaling you will be doing in the ui scripting system
//
// In the UI Scripting code, a scale of 1.0 is equal to a 48 point font. In Team Arena, we
// use three or four scales, most of them exactly equaling the specific rendered size. We
// rendered three sizes in Team Arena, 12, 16, and 20.
//
// To generate new font data you need to go through the following steps.
// 1. delete the fontImage_x_xx.tga files and fontImage_xx.dat files from the fonts path.
// 2. in a ui script, specificy a font, smallFont, and bigFont keyword with font name and
// point size. the original TrueType fonts must exist in fonts at this point.
// 3. run the game, you should see things normally.
// 4. Exit the game and there will be three dat files and at least three tga files. The
// tga's are in 256x256 pages so if it takes three images to render a 24 point font you
// will end up with fontImage_0_24.tga through fontImage_2_24.tga
// 5. You will need to flip the tga's in Photoshop as the tga output code writes them upside
// down.
// 6. In future runs of the game, the system looks for these images and data files when a s
// specific point sized font is rendered and loads them for use.
// 7. Because of the original beta nature of the FreeType code you will probably want to hand
// touch the font bitmaps.
//
// Currently a define in the project turns on or off the FreeType code which is currently
// defined out. To pre-render new fonts you need enable the define ( BUILD_FREETYPE ) and
// uncheck the exclude from build check box in the FreeType2 area of the Renderer project.
#include "tr_local.h"
#include "../qcommon/qcommon.h"
#ifdef BUILD_FREETYPE
#include <ft2build.h>
#include <freetype/fterrors.h>
#include <freetype/ftsystem.h>
#include <freetype/ftimage.h>
#include <freetype/freetype.h>
#include <freetype/ftoutln.h>
#define _FLOOR(x) ((x) & -64)
#define _CEIL(x) (((x)+63) & -64)
#define _TRUNC(x) ((x) >> 6)
FT_Library ftLibrary = NULL;
#endif
#define MAX_FONTS 6
static int registeredFontCount = 0;
static fontInfo_t registeredFont[MAX_FONTS];
#ifdef BUILD_FREETYPE
void R_GetGlyphInfo(FT_GlyphSlot glyph, int *left, int *right, int *width, int *top, int *bottom, int *height, int *pitch) {
*left = _FLOOR( glyph->metrics.horiBearingX );
*right = _CEIL( glyph->metrics.horiBearingX + glyph->metrics.width );
*width = _TRUNC(*right - *left);
*top = _CEIL( glyph->metrics.horiBearingY );
*bottom = _FLOOR( glyph->metrics.horiBearingY - glyph->metrics.height );
*height = _TRUNC( *top - *bottom );
*pitch = ( qtrue ? (*width+3) & -4 : (*width+7) >> 3 );
}
FT_Bitmap *R_RenderGlyph(FT_GlyphSlot glyph, glyphInfo_t* glyphOut) {
FT_Bitmap *bit2;
int left, right, width, top, bottom, height, pitch, size;
R_GetGlyphInfo(glyph, &left, &right, &width, &top, &bottom, &height, &pitch);
if ( glyph->format == ft_glyph_format_outline ) {
size = pitch*height;
bit2 = Z_Malloc(sizeof(FT_Bitmap));
bit2->width = width;
bit2->rows = height;
bit2->pitch = pitch;
bit2->pixel_mode = ft_pixel_mode_grays;
//bit2->pixel_mode = ft_pixel_mode_mono;
bit2->buffer = Z_Malloc(pitch*height);
bit2->num_grays = 256;
Com_Memset( bit2->buffer, 0, size );
FT_Outline_Translate( &glyph->outline, -left, -bottom );
FT_Outline_Get_Bitmap( ftLibrary, &glyph->outline, bit2 );
glyphOut->height = height;
glyphOut->pitch = pitch;
glyphOut->top = (glyph->metrics.horiBearingY >> 6) + 1;
glyphOut->bottom = bottom;
return bit2;
}
else {
ri.Printf(PRINT_ALL, "Non-outline fonts are not supported\n");
}
return NULL;
}
void WriteTGA (char *filename, byte *data, int width, int height) {
byte *buffer;
int i, c;
buffer = Z_Malloc(width*height*4 + 18);
Com_Memset (buffer, 0, 18);
buffer[2] = 2; // uncompressed type
buffer[12] = width&255;
buffer[13] = width>>8;
buffer[14] = height&255;
buffer[15] = height>>8;
buffer[16] = 32; // pixel size
// swap rgb to bgr
c = 18 + width * height * 4;
for (i=18 ; i<c ; i+=4)
{
buffer[i] = data[i-18+2]; // blue
buffer[i+1] = data[i-18+1]; // green
buffer[i+2] = data[i-18+0]; // red
buffer[i+3] = data[i-18+3]; // alpha
}
ri.FS_WriteFile(filename, buffer, c);
//f = fopen (filename, "wb");
//fwrite (buffer, 1, c, f);
//fclose (f);
Z_Free (buffer);
}
static glyphInfo_t *RE_ConstructGlyphInfo(unsigned char *imageOut, int *xOut, int *yOut, int *maxHeight, FT_Face face, const unsigned char c, qboolean calcHeight) {
int i;
static glyphInfo_t glyph;
unsigned char *src, *dst;
float scaled_width, scaled_height;
FT_Bitmap *bitmap = NULL;
Com_Memset(&glyph, 0, sizeof(glyphInfo_t));
// make sure everything is here
if (face != NULL) {
FT_Load_Glyph(face, FT_Get_Char_Index( face, c), FT_LOAD_DEFAULT );
bitmap = R_RenderGlyph(face->glyph, &glyph);
if (bitmap) {
glyph.xSkip = (face->glyph->metrics.horiAdvance >> 6) + 1;
} else {
return &glyph;
}
if (glyph.height > *maxHeight) {
*maxHeight = glyph.height;
}
if (calcHeight) {
Z_Free(bitmap->buffer);
Z_Free(bitmap);
return &glyph;
}
/*
// need to convert to power of 2 sizes so we do not get
// any scaling from the gl upload
for (scaled_width = 1 ; scaled_width < glyph.pitch ; scaled_width<<=1)
;
for (scaled_height = 1 ; scaled_height < glyph.height ; scaled_height<<=1)
;
*/
scaled_width = glyph.pitch;
scaled_height = glyph.height;
// we need to make sure we fit
if (*xOut + scaled_width + 1 >= 255) {
if (*yOut + *maxHeight + 1 >= 255) {
*yOut = -1;
*xOut = -1;
Z_Free(bitmap->buffer);
Z_Free(bitmap);
return &glyph;
} else {
*xOut = 0;
*yOut += *maxHeight + 1;
}
} else if (*yOut + *maxHeight + 1 >= 255) {
*yOut = -1;
*xOut = -1;
Z_Free(bitmap->buffer);
Z_Free(bitmap);
return &glyph;
}
src = bitmap->buffer;
dst = imageOut + (*yOut * 256) + *xOut;
if (bitmap->pixel_mode == ft_pixel_mode_mono) {
for (i = 0; i < glyph.height; i++) {
int j;
unsigned char *_src = src;
unsigned char *_dst = dst;
unsigned char mask = 0x80;
unsigned char val = *_src;
for (j = 0; j < glyph.pitch; j++) {
if (mask == 0x80) {
val = *_src++;
}
if (val & mask) {
*_dst = 0xff;
}
mask >>= 1;
if ( mask == 0 ) {
mask = 0x80;
}
_dst++;
}
src += glyph.pitch;
dst += 256;
}
} else {
for (i = 0; i < glyph.height; i++) {
Com_Memcpy(dst, src, glyph.pitch);
src += glyph.pitch;
dst += 256;
}
}
// we now have an 8 bit per pixel grey scale bitmap
// that is width wide and pf->ftSize->metrics.y_ppem tall
glyph.imageHeight = scaled_height;
glyph.imageWidth = scaled_width;
glyph.s = (float)*xOut / 256;
glyph.t = (float)*yOut / 256;
glyph.s2 = glyph.s + (float)scaled_width / 256;
glyph.t2 = glyph.t + (float)scaled_height / 256;
*xOut += scaled_width + 1;
}
Z_Free(bitmap->buffer);
Z_Free(bitmap);
return &glyph;
}
#endif
static int fdOffset;
static byte *fdFile;
int readInt( void ) {
int i = fdFile[fdOffset]+(fdFile[fdOffset+1]<<8)+(fdFile[fdOffset+2]<<16)+(fdFile[fdOffset+3]<<24);
fdOffset += 4;
return i;
}
typedef union {
byte fred[4];
float ffred;
} poor;
float readFloat( void ) {
poor me;
#if defined Q3_BIG_ENDIAN
me.fred[0] = fdFile[fdOffset+3];
me.fred[1] = fdFile[fdOffset+2];
me.fred[2] = fdFile[fdOffset+1];
me.fred[3] = fdFile[fdOffset+0];
#elif defined Q3_LITTLE_ENDIAN
me.fred[0] = fdFile[fdOffset+0];
me.fred[1] = fdFile[fdOffset+1];
me.fred[2] = fdFile[fdOffset+2];
me.fred[3] = fdFile[fdOffset+3];
#endif
fdOffset += 4;
return me.ffred;
}
void RE_RegisterFont(const char *fontName, int pointSize, fontInfo_t *font) {
#ifdef BUILD_FREETYPE
FT_Face face;
int j, k, xOut, yOut, lastStart, imageNumber;
int scaledSize, newSize, maxHeight, left, satLevels;
unsigned char *out, *imageBuff;
glyphInfo_t *glyph;
image_t *image;
qhandle_t h;
float max;
#endif
void *faceData;
int i, len;
char name[1024];
float dpi = 72; //
float glyphScale = 72.0f / dpi; // change the scale to be relative to 1 based on 72 dpi ( so dpi of 144 means a scale of .5 )
if (!fontName) {
ri.Printf(PRINT_ALL, "RE_RegisterFont: called with empty name\n");
return;
}
if (pointSize <= 0) {
pointSize = 12;
}
// we also need to adjust the scale based on point size relative to 48 points as the ui scaling is based on a 48 point font
glyphScale *= 48.0f / pointSize;
// make sure the render thread is stopped
R_SyncRenderThread();
if (registeredFontCount >= MAX_FONTS) {
ri.Printf(PRINT_ALL, "RE_RegisterFont: Too many fonts registered already.\n");
return;
}
Com_sprintf(name, sizeof(name), "fonts/fontImage_%i.dat",pointSize);
for (i = 0; i < registeredFontCount; i++) {
if (Q_stricmp(name, registeredFont[i].name) == 0) {
Com_Memcpy(font, ®isteredFont[i], sizeof(fontInfo_t));
return;
}
}
len = ri.FS_ReadFile(name, NULL);
if (len == sizeof(fontInfo_t)) {
ri.FS_ReadFile(name, &faceData);
fdOffset = 0;
fdFile = faceData;
for(i=0; i<GLYPHS_PER_FONT; i++) {
font->glyphs[i].height = readInt();
font->glyphs[i].top = readInt();
font->glyphs[i].bottom = readInt();
font->glyphs[i].pitch = readInt();
font->glyphs[i].xSkip = readInt();
font->glyphs[i].imageWidth = readInt();
font->glyphs[i].imageHeight = readInt();
font->glyphs[i].s = readFloat();
font->glyphs[i].t = readFloat();
font->glyphs[i].s2 = readFloat();
font->glyphs[i].t2 = readFloat();
font->glyphs[i].glyph = readInt();
Com_Memcpy(font->glyphs[i].shaderName, &fdFile[fdOffset], 32);
fdOffset += 32;
}
font->glyphScale = readFloat();
Com_Memcpy(font->name, &fdFile[fdOffset], MAX_QPATH);
// Com_Memcpy(font, faceData, sizeof(fontInfo_t));
Q_strncpyz(font->name, name, sizeof(font->name));
for (i = GLYPH_START; i < GLYPH_END; i++) {
font->glyphs[i].glyph = RE_RegisterShaderNoMip(font->glyphs[i].shaderName);
}
Com_Memcpy(®isteredFont[registeredFontCount++], font, sizeof(fontInfo_t));
return;
}
#ifndef BUILD_FREETYPE
ri.Printf(PRINT_ALL, "RE_RegisterFont: FreeType code not available\n");
#else
if (ftLibrary == NULL) {
ri.Printf(PRINT_ALL, "RE_RegisterFont: FreeType not initialized.\n");
return;
}
len = ri.FS_ReadFile(fontName, &faceData);
if (len <= 0) {
ri.Printf(PRINT_ALL, "RE_RegisterFont: Unable to read font file\n");
return;
}
// allocate on the stack first in case we fail
if (FT_New_Memory_Face( ftLibrary, faceData, len, 0, &face )) {
ri.Printf(PRINT_ALL, "RE_RegisterFont: FreeType2, unable to allocate new face.\n");
return;
}
if (FT_Set_Char_Size( face, pointSize << 6, pointSize << 6, dpi, dpi)) {
ri.Printf(PRINT_ALL, "RE_RegisterFont: FreeType2, Unable to set face char size.\n");
return;
}
//*font = ®isteredFonts[registeredFontCount++];
// make a 256x256 image buffer, once it is full, register it, clean it and keep going
// until all glyphs are rendered
out = Z_Malloc(1024*1024);
if (out == NULL) {
ri.Printf(PRINT_ALL, "RE_RegisterFont: Z_Malloc failure during output image creation.\n");
return;
}
Com_Memset(out, 0, 1024*1024);
maxHeight = 0;
for (i = GLYPH_START; i < GLYPH_END; i++) {
glyph = RE_ConstructGlyphInfo(out, &xOut, &yOut, &maxHeight, face, (unsigned char)i, qtrue);
}
xOut = 0;
yOut = 0;
i = GLYPH_START;
lastStart = i;
imageNumber = 0;
while ( i <= GLYPH_END ) {
glyph = RE_ConstructGlyphInfo(out, &xOut, &yOut, &maxHeight, face, (unsigned char)i, qfalse);
if (xOut == -1 || yOut == -1 || i == GLYPH_END) {
// ran out of room
// we need to create an image from the bitmap, set all the handles in the glyphs to this point
//
scaledSize = 256*256;
newSize = scaledSize * 4;
imageBuff = Z_Malloc(newSize);
left = 0;
max = 0;
satLevels = 255;
for ( k = 0; k < (scaledSize) ; k++ ) {
if (max < out[k]) {
max = out[k];
}
}
if (max > 0) {
max = 255/max;
}
for ( k = 0; k < (scaledSize) ; k++ ) {
imageBuff[left++] = 255;
imageBuff[left++] = 255;
imageBuff[left++] = 255;
imageBuff[left++] = ((float)out[k] * max);
}
Com_sprintf (name, sizeof(name), "fonts/fontImage_%i_%i.tga", imageNumber++, pointSize);
if (r_saveFontData->integer) {
WriteTGA(name, imageBuff, 256, 256);
}
//Com_sprintf (name, sizeof(name), "fonts/fontImage_%i_%i", imageNumber++, pointSize);
image = R_CreateImage(name, imageBuff, 256, 256, qfalse, qfalse, GL_CLAMP_TO_EDGE);
h = RE_RegisterShaderFromImage(name, LIGHTMAP_2D, image, qfalse);
for (j = lastStart; j < i; j++) {
font->glyphs[j].glyph = h;
Q_strncpyz(font->glyphs[j].shaderName, name, sizeof(font->glyphs[j].shaderName));
}
lastStart = i;
Com_Memset(out, 0, 1024*1024);
xOut = 0;
yOut = 0;
Z_Free(imageBuff);
i++;
} else {
Com_Memcpy(&font->glyphs[i], glyph, sizeof(glyphInfo_t));
i++;
}
}
registeredFont[registeredFontCount].glyphScale = glyphScale;
font->glyphScale = glyphScale;
Com_Memcpy(®isteredFont[registeredFontCount++], font, sizeof(fontInfo_t));
if (r_saveFontData->integer) {
ri.FS_WriteFile(va("fonts/fontImage_%i.dat", pointSize), font, sizeof(fontInfo_t));
}
Z_Free(out);
ri.FS_FreeFile(faceData);
#endif
}
void R_InitFreeType(void) {
#ifdef BUILD_FREETYPE
if (FT_Init_FreeType( &ftLibrary )) {
ri.Printf(PRINT_ALL, "R_InitFreeType: Unable to initialize FreeType.\n");
}
#endif
registeredFontCount = 0;
}
void R_DoneFreeType(void) {
#ifdef BUILD_FREETYPE
if (ftLibrary) {
FT_Done_FreeType( ftLibrary );
ftLibrary = NULL;
}
#endif
registeredFontCount = 0;
}
|