Debian Codegraph @

code/renderercommon/tr_font.c

raw symbols SCIP
1 /*
2 ===========================================================================
3 Copyright (C) 1999-2005 Id Software, Inc.
4
5 This file is part of Quake III Arena source code.
6
7 Quake III Arena source code is free software; you can redistribute it
8 and/or modify it under the terms of the GNU General Public License as
9 published by the Free Software Foundation; either version 2 of the License,
10 or (at your option) any later version.
11
12 Quake III Arena source code is distributed in the hope that it will be
13 useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with Quake III Arena source code; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 ===========================================================================
21 */
22 // tr_font.c
23 //
24 //
25 // The font system uses FreeType 2.x to render TrueType fonts for use within the game.
26 // As of this writing ( Nov, 2000 ) Team Arena uses these fonts for all of the ui and
27 // about 90% of the cgame presentation. A few areas of the CGAME were left uses the old
28 // fonts since the code is shared with standard Q3A.
29 //
30 // If you include this font rendering code in a commercial product you MUST include the
31 // following somewhere with your product, see www.freetype.org for specifics or changes.
32 // The Freetype code also uses some hinting techniques that MIGHT infringe on patents
33 // held by apple so be aware of that also.
34 //
35 // As of Q3A 1.25+ and Team Arena, we are shipping the game with the font rendering code
36 // disabled. This removes any potential patent issues and it keeps us from having to
37 // distribute an actual TrueTrype font which is 1. expensive to do and 2. seems to require
38 // an act of god to accomplish.
39 //
40 // What we did was pre-render the fonts using FreeType ( which is why we leave the FreeType
41 // credit in the credits ) and then saved off the glyph data and then hand touched up the
42 // font bitmaps so they scale a bit better in GL.
43 //
44 // There are limitations in the way fonts are saved and reloaded in that it is based on
45 // point size and not name. So if you pre-render Helvetica in 18 point and Impact in 18 point
46 // you will end up with a single 18 point data file and image set. Typically you will want to
47 // choose 3 sizes to best approximate the scaling you will be doing in the ui scripting system
48 //
49 // In the UI Scripting code, a scale of 1.0 is equal to a 48 point font. In Team Arena, we
50 // use three or four scales, most of them exactly equaling the specific rendered size. We
51 // rendered three sizes in Team Arena, 12, 16, and 20.
52 //
53 // To generate new font data you need to go through the following steps.
54 // 1. delete the fontImage_x_xx.tga files and fontImage_xx.dat files from the fonts path.
55 // 2. in a ui script, specificy a font, smallFont, and bigFont keyword with font name and
56 // point size. the original TrueType fonts must exist in fonts at this point.
57 // 3. run the game, you should see things normally.
58 // 4. Exit the game and there will be three dat files and at least three tga files. The
59 // tga's are in 256x256 pages so if it takes three images to render a 24 point font you
60 // will end up with fontImage_0_24.tga through fontImage_2_24.tga
61 // 5. In future runs of the game, the system looks for these images and data files when a s
62 // specific point sized font is rendered and loads them for use.
63 // 6. Because of the original beta nature of the FreeType code you will probably want to hand
64 // touch the font bitmaps.
65 //
66 // Currently a define in the project turns on or off the FreeType code which is currently
67 // defined out. To pre-render new fonts you need enable the define ( BUILD_FREETYPE ) and
68 // uncheck the exclude from build check box in the FreeType2 area of the Renderer project.
69
70
71 #include "tr_common.h"
72 #include "../qcommon/qcommon.h"
73
74 #ifdef BUILD_FREETYPE
75 #include <ft2build.h>
76 #include FT_FREETYPE_H
77 #include FT_ERRORS_H
78 #include FT_SYSTEM_H
79 #include FT_IMAGE_H
80 #include FT_OUTLINE_H
81
82 #define _FLOOR(x) ((x) & -64)
83 #define _CEIL(x) (((x)+63) & -64)
84 #define _TRUNC(x) ((x) >> 6)
85
86 FT_Library ftLibrary = NULL;
87 #endif
88
89 #define MAX_FONTS 6
90 static int registeredFontCount = 0;
91 static fontInfo_t registeredFont[MAX_FONTS];
92
93 #ifdef BUILD_FREETYPE
94 void R_GetGlyphInfo(FT_GlyphSlot glyph, int *left, int *right, int *width, int *top, int *bottom, int *height, int *pitch) {
95 *left = _FLOOR( glyph->metrics.horiBearingX );
96 *right = _CEIL( glyph->metrics.horiBearingX + glyph->metrics.width );
97 *width = _TRUNC(*right - *left);
98
99 *top = _CEIL( glyph->metrics.horiBearingY );
100 *bottom = _FLOOR( glyph->metrics.horiBearingY - glyph->metrics.height );
101 *height = _TRUNC( *top - *bottom );
102 *pitch = ( qtrue ? (*width+3) & -4 : (*width+7) >> 3 );
103 }
104
105
106 FT_Bitmap *R_RenderGlyph(FT_GlyphSlot glyph, glyphInfo_t* glyphOut) {
107 FT_Bitmap *bit2;
108 int left, right, width, top, bottom, height, pitch, size;
109
110 R_GetGlyphInfo(glyph, &left, &right, &width, &top, &bottom, &height, &pitch);
111
112 if ( glyph->format == ft_glyph_format_outline ) {
113 size = pitch*height;
114
115 bit2 = ri.Malloc(sizeof(FT_Bitmap));
116
117 bit2->width = width;
118 bit2->rows = height;
119 bit2->pitch = pitch;
120 bit2->pixel_mode = ft_pixel_mode_grays;
121 //bit2->pixel_mode = ft_pixel_mode_mono;
122 bit2->buffer = ri.Malloc(pitch*height);
123 bit2->num_grays = 256;
124
125 Com_Memset( bit2->buffer, 0, size );
126
127 FT_Outline_Translate( &glyph->outline, -left, -bottom );
128
129 FT_Outline_Get_Bitmap( ftLibrary, &glyph->outline, bit2 );
130
131 glyphOut->height = height;
132 glyphOut->pitch = pitch;
133 glyphOut->top = (glyph->metrics.horiBearingY >> 6) + 1;
134 glyphOut->bottom = bottom;
135
136 return bit2;
137 } else {
138 ri.Printf(PRINT_ALL, "Non-outline fonts are not supported\n");
139 }
140 return NULL;
141 }
142
143 void WriteTGA (char *filename, byte *data, int width, int height) {
144 byte *buffer;
145 int i, c;
146 int row;
147 unsigned char *flip;
148 unsigned char *src, *dst;
149
150 buffer = ri.Malloc(width*height*4 + 18);
151 Com_Memset (buffer, 0, 18);
152 buffer[2] = 2; // uncompressed type
153 buffer[12] = width&255;
154 buffer[13] = width>>8;
155 buffer[14] = height&255;
156 buffer[15] = height>>8;
157 buffer[16] = 32; // pixel size
158
159 // swap rgb to bgr
160 c = 18 + width * height * 4;
161 for (i=18 ; i<c ; i+=4)
162 {
163 buffer[i] = data[i-18+2]; // blue
164 buffer[i+1] = data[i-18+1]; // green
165 buffer[i+2] = data[i-18+0]; // red
166 buffer[i+3] = data[i-18+3]; // alpha
167 }
168
169 // flip upside down
170 flip = (unsigned char *)ri.Malloc(width*4);
171 for(row = 0; row < height/2; row++)
172 {
173 src = buffer + 18 + row * 4 * width;
174 dst = buffer + 18 + (height - row - 1) * 4 * width;
175
176 Com_Memcpy(flip, src, width*4);
177 Com_Memcpy(src, dst, width*4);
178 Com_Memcpy(dst, flip, width*4);
179 }
180 ri.Free(flip);
181
182 ri.FS_WriteFile(filename, buffer, c);
183
184 //f = fopen (filename, "wb");
185 //fwrite (buffer, 1, c, f);
186 //fclose (f);
187
188 ri.Free (buffer);
189 }
190
191 static glyphInfo_t *RE_ConstructGlyphInfo(unsigned char *imageOut, int *xOut, int *yOut, int *maxHeight, FT_Face face, const unsigned char c, qboolean calcHeight) {
192 int i;
193 static glyphInfo_t glyph;
194 unsigned char *src, *dst;
195 float scaled_width, scaled_height;
196 FT_Bitmap *bitmap = NULL;
197
198 Com_Memset(&glyph, 0, sizeof(glyphInfo_t));
199 // make sure everything is here
200 if (face != NULL) {
201 FT_Load_Glyph(face, FT_Get_Char_Index( face, c), FT_LOAD_DEFAULT );
202 bitmap = R_RenderGlyph(face->glyph, &glyph);
203 if (bitmap) {
204 glyph.xSkip = (face->glyph->metrics.horiAdvance >> 6) + 1;
205 } else {
206 return &glyph;
207 }
208
209 if (glyph.height > *maxHeight) {
210 *maxHeight = glyph.height;
211 }
212
213 if (calcHeight) {
214 ri.Free(bitmap->buffer);
215 ri.Free(bitmap);
216 return &glyph;
217 }
218
219 /*
220 // need to convert to power of 2 sizes so we do not get
221 // any scaling from the gl upload
222 for (scaled_width = 1 ; scaled_width < glyph.pitch ; scaled_width<<=1)
223 ;
224 for (scaled_height = 1 ; scaled_height < glyph.height ; scaled_height<<=1)
225 ;
226 */
227
228 scaled_width = glyph.pitch;
229 scaled_height = glyph.height;
230
231 // we need to make sure we fit
232 if (*xOut + scaled_width + 1 >= 255) {
233 *xOut = 0;
234 *yOut += *maxHeight + 1;
235 }
236
237 if (*yOut + *maxHeight + 1 >= 255) {
238 *yOut = -1;
239 *xOut = -1;
240 ri.Free(bitmap->buffer);
241 ri.Free(bitmap);
242 return &glyph;
243 }
244
245
246 src = bitmap->buffer;
247 dst = imageOut + (*yOut * 256) + *xOut;
248
249 if (bitmap->pixel_mode == ft_pixel_mode_mono) {
250 for (i = 0; i < glyph.height; i++) {
251 int j;
252 unsigned char *_src = src;
253 unsigned char *_dst = dst;
254 unsigned char mask = 0x80;
255 unsigned char val = *_src;
256 for (j = 0; j < glyph.pitch; j++) {
257 if (mask == 0x80) {
258 val = *_src++;
259 }
260 if (val & mask) {
261 *_dst = 0xff;
262 }
263 mask >>= 1;
264
265 if ( mask == 0 ) {
266 mask = 0x80;
267 }
268 _dst++;
269 }
270
271 src += glyph.pitch;
272 dst += 256;
273 }
274 } else {
275 for (i = 0; i < glyph.height; i++) {
276 Com_Memcpy(dst, src, glyph.pitch);
277 src += glyph.pitch;
278 dst += 256;
279 }
280 }
281
282 // we now have an 8 bit per pixel grey scale bitmap
283 // that is width wide and pf->ftSize->metrics.y_ppem tall
284
285 glyph.imageHeight = scaled_height;
286 glyph.imageWidth = scaled_width;
287 glyph.s = (float)*xOut / 256;
288 glyph.t = (float)*yOut / 256;
289 glyph.s2 = glyph.s + (float)scaled_width / 256;
290 glyph.t2 = glyph.t + (float)scaled_height / 256;
291
292 *xOut += scaled_width + 1;
293
294 ri.Free(bitmap->buffer);
295 ri.Free(bitmap);
296 }
297
298 return &glyph;
299 }
300 #endif
301
302 static int fdOffset;
303 static byte *fdFile;
304
305 int readInt( void ) {
306 int i = ((unsigned int)fdFile[fdOffset] | ((unsigned int)fdFile[fdOffset+1]<<8) | ((unsigned int)fdFile[fdOffset+2]<<16) | ((unsigned int)fdFile[fdOffset+3]<<24));
307 fdOffset += 4;
308 return i;
309 }
310
311 typedef union {
312 byte fred[4];
313 float ffred;
314 } poor;
315
316 float readFloat( void ) {
317 poor me;
318 #if defined Q3_BIG_ENDIAN
319 me.fred[0] = fdFile[fdOffset+3];
320 me.fred[1] = fdFile[fdOffset+2];
321 me.fred[2] = fdFile[fdOffset+1];
322 me.fred[3] = fdFile[fdOffset+0];
323 #elif defined Q3_LITTLE_ENDIAN
324 me.fred[0] = fdFile[fdOffset+0];
325 me.fred[1] = fdFile[fdOffset+1];
326 me.fred[2] = fdFile[fdOffset+2];
327 me.fred[3] = fdFile[fdOffset+3];
328 #endif
329 fdOffset += 4;
330 return me.ffred;
331 }
332
333 void RE_RegisterFont(const char *fontName, int pointSize, fontInfo_t *font) {
334 #ifdef BUILD_FREETYPE
335 FT_Face face;
336 int j, k, xOut, yOut, lastStart, imageNumber;
337 int scaledSize, newSize, maxHeight, left;
338 unsigned char *out, *imageBuff;
339 glyphInfo_t *glyph;
340 image_t *image;
341 qhandle_t h;
342 float max;
343 float dpi = 72;
344 float glyphScale;
345 #endif
346 void *faceData;
347 int i, len;
348 char name[1024];
349
350 if (!fontName) {
351 ri.Printf(PRINT_ALL, "RE_RegisterFont: called with empty name\n");
352 return;
353 }
354
355 if (pointSize <= 0) {
356 pointSize = 12;
357 }
358
359 R_IssuePendingRenderCommands();
360
361 if (registeredFontCount >= MAX_FONTS) {
362 ri.Printf(PRINT_WARNING, "RE_RegisterFont: Too many fonts registered already.\n");
363 return;
364 }
365
366 Com_sprintf(name, sizeof(name), "fonts/fontImage_%i.dat",pointSize);
367 for (i = 0; i < registeredFontCount; i++) {
368 if (Q_stricmp(name, registeredFont[i].name) == 0) {
369 Com_Memcpy(font, &registeredFont[i], sizeof(fontInfo_t));
370 return;
371 }
372 }
373
374 len = ri.FS_ReadFile(name, NULL);
375 if (len == sizeof(fontInfo_t)) {
376 ri.FS_ReadFile(name, &faceData);
377 fdOffset = 0;
378 fdFile = faceData;
379 for(i=0; i<GLYPHS_PER_FONT; i++) {
380 font->glyphs[i].height = readInt();
381 font->glyphs[i].top = readInt();
382 font->glyphs[i].bottom = readInt();
383 font->glyphs[i].pitch = readInt();
384 font->glyphs[i].xSkip = readInt();
385 font->glyphs[i].imageWidth = readInt();
386 font->glyphs[i].imageHeight = readInt();
387 font->glyphs[i].s = readFloat();
388 font->glyphs[i].t = readFloat();
389 font->glyphs[i].s2 = readFloat();
390 font->glyphs[i].t2 = readFloat();
391 font->glyphs[i].glyph = readInt();
392 Q_strncpyz(font->glyphs[i].shaderName, (const char *)&fdFile[fdOffset], sizeof(font->glyphs[i].shaderName));
393 fdOffset += sizeof(font->glyphs[i].shaderName);
394 }
395 font->glyphScale = readFloat();
396 Com_Memcpy(font->name, &fdFile[fdOffset], MAX_QPATH);
397
398 // Com_Memcpy(font, faceData, sizeof(fontInfo_t));
399 Q_strncpyz(font->name, name, sizeof(font->name));
400 for (i = GLYPH_START; i <= GLYPH_END; i++) {
401 font->glyphs[i].glyph = RE_RegisterShaderNoMip(font->glyphs[i].shaderName);
402 }
403 Com_Memcpy(&registeredFont[registeredFontCount++], font, sizeof(fontInfo_t));
404 ri.FS_FreeFile(faceData);
405 return;
406 }
407
408 #ifndef BUILD_FREETYPE
409 ri.Printf(PRINT_WARNING, "RE_RegisterFont: FreeType code not available\n");
410 #else
411 if (ftLibrary == NULL) {
412 ri.Printf(PRINT_WARNING, "RE_RegisterFont: FreeType not initialized.\n");
413 return;
414 }
415
416 len = ri.FS_ReadFile(fontName, &faceData);
417 if (len <= 0) {
418 ri.Printf(PRINT_WARNING, "RE_RegisterFont: Unable to read font file '%s'\n", fontName);
419 return;
420 }
421
422 // allocate on the stack first in case we fail
423 if (FT_New_Memory_Face( ftLibrary, faceData, len, 0, &face )) {
424 ri.Printf(PRINT_WARNING, "RE_RegisterFont: FreeType, unable to allocate new face.\n");
425 return;
426 }
427
428
429 if (FT_Set_Char_Size( face, pointSize << 6, pointSize << 6, dpi, dpi)) {
430 ri.Printf(PRINT_WARNING, "RE_RegisterFont: FreeType, unable to set face char size.\n");
431 return;
432 }
433
434 //*font = &registeredFonts[registeredFontCount++];
435
436 // make a 256x256 image buffer, once it is full, register it, clean it and keep going
437 // until all glyphs are rendered
438
439 out = ri.Malloc(256*256);
440 if (out == NULL) {
441 ri.Printf(PRINT_WARNING, "RE_RegisterFont: ri.Malloc failure during output image creation.\n");
442 return;
443 }
444 Com_Memset(out, 0, 256*256);
445
446 maxHeight = 0;
447
448 for (i = GLYPH_START; i <= GLYPH_END; i++) {
449 RE_ConstructGlyphInfo(out, &xOut, &yOut, &maxHeight, face, (unsigned char)i, qtrue);
450 }
451
452 xOut = 0;
453 yOut = 0;
454 i = GLYPH_START;
455 lastStart = i;
456 imageNumber = 0;
457
458 while ( i <= GLYPH_END + 1 ) {
459
460 if ( i == GLYPH_END + 1 ) {
461 // upload/save current image buffer
462 xOut = yOut = -1;
463 } else {
464 glyph = RE_ConstructGlyphInfo(out, &xOut, &yOut, &maxHeight, face, (unsigned char)i, qfalse);
465 }
466
467 if (xOut == -1 || yOut == -1) {
468 // ran out of room
469 // we need to create an image from the bitmap, set all the handles in the glyphs to this point
470 //
471
472 scaledSize = 256*256;
473 newSize = scaledSize * 4;
474 imageBuff = ri.Malloc(newSize);
475 left = 0;
476 max = 0;
477 for ( k = 0; k < (scaledSize) ; k++ ) {
478 if (max < out[k]) {
479 max = out[k];
480 }
481 }
482
483 if (max > 0) {
484 max = 255/max;
485 }
486
487 for ( k = 0; k < (scaledSize) ; k++ ) {
488 imageBuff[left++] = 255;
489 imageBuff[left++] = 255;
490 imageBuff[left++] = 255;
491
492 imageBuff[left++] = ((float)out[k] * max);
493 }
494
495 Com_sprintf (name, sizeof(name), "fonts/fontImage_%i_%i.tga", imageNumber++, pointSize);
496 if (r_saveFontData->integer) {
497 WriteTGA(name, imageBuff, 256, 256);
498 }
499
500 //Com_sprintf (name, sizeof(name), "fonts/fontImage_%i_%i", imageNumber++, pointSize);
501 image = R_CreateImage(name, imageBuff, 256, 256, IMGTYPE_COLORALPHA, IMGFLAG_CLAMPTOEDGE, 0 );
502 h = RE_RegisterShaderFromImage(name, LIGHTMAP_2D, image, qfalse);
503 for (j = lastStart; j < i; j++) {
504 font->glyphs[j].glyph = h;
505 Q_strncpyz(font->glyphs[j].shaderName, name, sizeof(font->glyphs[j].shaderName));
506 }
507 lastStart = i;
508 Com_Memset(out, 0, 256*256);
509 xOut = 0;
510 yOut = 0;
511 ri.Free(imageBuff);
512 if ( i == GLYPH_END + 1 )
513 i++;
514 } else {
515 Com_Memcpy(&font->glyphs[i], glyph, sizeof(glyphInfo_t));
516 i++;
517 }
518 }
519
520 // change the scale to be relative to 1 based on 72 dpi ( so dpi of 144 means a scale of .5 )
521 glyphScale = 72.0f / dpi;
522
523 // 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
524 glyphScale *= 48.0f / pointSize;
525
526 registeredFont[registeredFontCount].glyphScale = glyphScale;
527 font->glyphScale = glyphScale;
528 Com_Memcpy(&registeredFont[registeredFontCount++], font, sizeof(fontInfo_t));
529
530 if (r_saveFontData->integer) {
531 ri.FS_WriteFile(va("fonts/fontImage_%i.dat", pointSize), font, sizeof(fontInfo_t));
532 }
533
534 ri.Free(out);
535
536 ri.FS_FreeFile(faceData);
537 #endif
538 }
539
540
541
542 void R_InitFreeType(void) {
543 #ifdef BUILD_FREETYPE
544 if (FT_Init_FreeType( &ftLibrary )) {
545 ri.Printf(PRINT_WARNING, "R_InitFreeType: Unable to initialize FreeType.\n");
546 }
547 #endif
548 registeredFontCount = 0;
549 }
550
551
552 void R_DoneFreeType(void) {
553 #ifdef BUILD_FREETYPE
554 if (ftLibrary) {
555 FT_Done_FreeType( ftLibrary );
556 ftLibrary = NULL;
557 }
558 #endif
559 registeredFontCount = 0;
560 }
561