Main Page | Class Hierarchy | Alphabetical List | Data Structures | Directories | File List | Data Fields | Globals

tr_font.c File Reference

#include "tr_local.h"
#include "../qcommon/qcommon.h"

Include dependency graph for tr_font.c:

Include dependency graph

Go to the source code of this file.

Data Structures

union  poor

Defines

#define MAX_FONTS   6

Functions

void R_DoneFreeType ()
void R_InitFreeType ()
void RE_RegisterFont (const char *fontName, int pointSize, fontInfo_t *font)
float readFloat ()
int readInt ()

Variables

bytefdFile
int fdOffset
fontInfo_t registeredFont [MAX_FONTS]
int registeredFontCount = 0


Define Documentation

#define MAX_FONTS   6
 

Definition at line 90 of file tr_font.c.


Function Documentation

void R_DoneFreeType  ) 
 

Definition at line 533 of file tr_font.c.

References registeredFontCount.

Referenced by RE_Shutdown().

00533                       {
00534 #ifdef BUILD_FREETYPE
00535   if (ftLibrary) {
00536     FT_Done_FreeType( ftLibrary );
00537     ftLibrary = NULL;
00538   }
00539 #endif
00540     registeredFontCount = 0;
00541 }

void R_InitFreeType  ) 
 

Definition at line 523 of file tr_font.c.

References PRINT_ALL, registeredFontCount, and ri.

Referenced by R_Init().

00523                       {
00524 #ifdef BUILD_FREETYPE
00525   if (FT_Init_FreeType( &ftLibrary )) {
00526     ri.Printf(PRINT_ALL, "R_InitFreeType: Unable to initialize FreeType.\n");
00527   }
00528 #endif
00529   registeredFontCount = 0;
00530 }

void RE_RegisterFont const char *  fontName,
int  pointSize,
fontInfo_t font
 

Definition at line 328 of file tr_font.c.

References glyphInfo_t::bottom, Com_Memcpy(), Com_Memset(), Com_sprintf(), fdFile, fdOffset, refimport_t::FS_FreeFile, refimport_t::FS_ReadFile, refimport_t::FS_WriteFile, glyphInfo_t::glyph, fontInfo_t::glyphs, fontInfo_t::glyphScale, h(), glyphInfo_t::height, i, image_t, glyphInfo_t::imageHeight, glyphInfo_t::imageWidth, cvar_s::integer, j, k, LIGHTMAP_2D, max, MAX_QPATH, name, fontInfo_t::name, NULL, glyphInfo_t::pitch, PRINT_ALL, Q_stricmp(), Q_strncpyz(), qfalse, qhandle_t, qtrue, R_CreateImage(), r_saveFontData, R_SyncRenderThread(), RE_RegisterShaderFromImage(), RE_RegisterShaderNoMip(), readFloat(), readInt(), registeredFont, registeredFontCount, ri, glyphInfo_t::s, glyphInfo_t::s2, glyphInfo_t::shaderName, glyphInfo_t::t, glyphInfo_t::t2, glyphInfo_t::top, va(), WriteTGA(), glyphInfo_t::xSkip, Z_Free(), and Z_Malloc().

00328                                                                             {
00329 #ifdef BUILD_FREETYPE
00330   FT_Face face;
00331   int j, k, xOut, yOut, lastStart, imageNumber;
00332   int scaledSize, newSize, maxHeight, left, satLevels;
00333   unsigned char *out, *imageBuff;
00334   glyphInfo_t *glyph;
00335   image_t *image;
00336   qhandle_t h;
00337     float max;
00338 #endif
00339   void *faceData;
00340     int i, len;
00341   char name[1024];
00342     float dpi = 72;                                         //
00343     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 )
00344 
00345     if (pointSize <= 0) {
00346         pointSize = 12;
00347     }
00348     // 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
00349     glyphScale *= 48.0f / pointSize;
00350 
00351     // make sure the render thread is stopped
00352     R_SyncRenderThread();
00353 
00354   if (registeredFontCount >= MAX_FONTS) {
00355     ri.Printf(PRINT_ALL, "RE_RegisterFont: Too many fonts registered already.\n");
00356     return;
00357   }
00358 
00359     Com_sprintf(name, sizeof(name), "fonts/fontImage_%i.dat",pointSize);
00360     for (i = 0; i < registeredFontCount; i++) {
00361         if (Q_stricmp(name, registeredFont[i].name) == 0) {
00362             Com_Memcpy(font, &registeredFont[i], sizeof(fontInfo_t));
00363             return;
00364         }
00365     }
00366 
00367     len = ri.FS_ReadFile(name, NULL);
00368     if (len == sizeof(fontInfo_t)) {
00369         ri.FS_ReadFile(name, &faceData);
00370         fdOffset = 0;
00371         fdFile = faceData;
00372         for(i=0; i<GLYPHS_PER_FONT; i++) {
00373             font->glyphs[i].height      = readInt();
00374             font->glyphs[i].top         = readInt();
00375             font->glyphs[i].bottom      = readInt();
00376             font->glyphs[i].pitch       = readInt();
00377             font->glyphs[i].xSkip       = readInt();
00378             font->glyphs[i].imageWidth  = readInt();
00379             font->glyphs[i].imageHeight = readInt();
00380             font->glyphs[i].s           = readFloat();
00381             font->glyphs[i].t           = readFloat();
00382             font->glyphs[i].s2          = readFloat();
00383             font->glyphs[i].t2          = readFloat();
00384             font->glyphs[i].glyph       = readInt();
00385             Com_Memcpy(font->glyphs[i].shaderName, &fdFile[fdOffset], 32);
00386             fdOffset += 32;
00387         }
00388         font->glyphScale = readFloat();
00389         Com_Memcpy(font->name, &fdFile[fdOffset], MAX_QPATH);
00390 
00391 //      Com_Memcpy(font, faceData, sizeof(fontInfo_t));
00392         Q_strncpyz(font->name, name, sizeof(font->name));
00393         for (i = GLYPH_START; i < GLYPH_END; i++) {
00394             font->glyphs[i].glyph = RE_RegisterShaderNoMip(font->glyphs[i].shaderName);
00395         }
00396       Com_Memcpy(&registeredFont[registeredFontCount++], font, sizeof(fontInfo_t));
00397         return;
00398     }
00399 
00400 #ifndef BUILD_FREETYPE
00401     ri.Printf(PRINT_ALL, "RE_RegisterFont: FreeType code not available\n");
00402 #else
00403   if (ftLibrary == NULL) {
00404     ri.Printf(PRINT_ALL, "RE_RegisterFont: FreeType not initialized.\n");
00405     return;
00406   }
00407 
00408   len = ri.FS_ReadFile(fontName, &faceData);
00409   if (len <= 0) {
00410     ri.Printf(PRINT_ALL, "RE_RegisterFont: Unable to read font file\n");
00411     return;
00412   }
00413 
00414   // allocate on the stack first in case we fail
00415   if (FT_New_Memory_Face( ftLibrary, faceData, len, 0, &face )) {
00416     ri.Printf(PRINT_ALL, "RE_RegisterFont: FreeType2, unable to allocate new face.\n");
00417     return;
00418   }
00419 
00420 
00421   if (FT_Set_Char_Size( face, pointSize << 6, pointSize << 6, dpi, dpi)) {
00422     ri.Printf(PRINT_ALL, "RE_RegisterFont: FreeType2, Unable to set face char size.\n");
00423     return;
00424   }
00425 
00426   //*font = &registeredFonts[registeredFontCount++];
00427 
00428   // make a 256x256 image buffer, once it is full, register it, clean it and keep going 
00429   // until all glyphs are rendered
00430 
00431   out = Z_Malloc(1024*1024);
00432   if (out == NULL) {
00433     ri.Printf(PRINT_ALL, "RE_RegisterFont: Z_Malloc failure during output image creation.\n");
00434     return;
00435   }
00436   Com_Memset(out, 0, 1024*1024);
00437 
00438   maxHeight = 0;
00439 
00440   for (i = GLYPH_START; i < GLYPH_END; i++) {
00441     glyph = RE_ConstructGlyphInfo(out, &xOut, &yOut, &maxHeight, face, (unsigned char)i, qtrue);
00442   }
00443 
00444   xOut = 0;
00445   yOut = 0;
00446   i = GLYPH_START;
00447   lastStart = i;
00448   imageNumber = 0;
00449 
00450   while ( i <= GLYPH_END ) {
00451 
00452     glyph = RE_ConstructGlyphInfo(out, &xOut, &yOut, &maxHeight, face, (unsigned char)i, qfalse);
00453 
00454     if (xOut == -1 || yOut == -1 || i == GLYPH_END)  {
00455       // ran out of room
00456       // we need to create an image from the bitmap, set all the handles in the glyphs to this point
00457       // 
00458 
00459       scaledSize = 256*256;
00460       newSize = scaledSize * 4;
00461       imageBuff = Z_Malloc(newSize);
00462       left = 0;
00463       max = 0;
00464       satLevels = 255;
00465       for ( k = 0; k < (scaledSize) ; k++ ) {
00466         if (max < out[k]) {
00467           max = out[k];
00468         }
00469       }
00470 
00471             if (max > 0) {
00472                 max = 255/max;
00473             }
00474 
00475       for ( k = 0; k < (scaledSize) ; k++ ) {
00476         imageBuff[left++] = 255;
00477         imageBuff[left++] = 255;
00478         imageBuff[left++] = 255;
00479 
00480         imageBuff[left++] = ((float)out[k] * max);
00481       }
00482 
00483             Com_sprintf (name, sizeof(name), "fonts/fontImage_%i_%i.tga", imageNumber++, pointSize);
00484             if (r_saveFontData->integer) { 
00485               WriteTGA(name, imageBuff, 256, 256);
00486             }
00487 
00488         //Com_sprintf (name, sizeof(name), "fonts/fontImage_%i_%i", imageNumber++, pointSize);
00489       image = R_CreateImage(name, imageBuff, 256, 256, qfalse, qfalse, GL_CLAMP);
00490       h = RE_RegisterShaderFromImage(name, LIGHTMAP_2D, image, qfalse);
00491       for (j = lastStart; j < i; j++) {
00492         font->glyphs[j].glyph = h;
00493                 Q_strncpyz(font->glyphs[j].shaderName, name, sizeof(font->glyphs[j].shaderName));
00494       }
00495       lastStart = i;
00496           Com_Memset(out, 0, 1024*1024);
00497       xOut = 0;
00498       yOut = 0;
00499       Z_Free(imageBuff);
00500             i++;
00501     } else {
00502       Com_Memcpy(&font->glyphs[i], glyph, sizeof(glyphInfo_t));
00503       i++;
00504     }
00505   }
00506 
00507     registeredFont[registeredFontCount].glyphScale = glyphScale;
00508     font->glyphScale = glyphScale;
00509   Com_Memcpy(&registeredFont[registeredFontCount++], font, sizeof(fontInfo_t));
00510 
00511     if (r_saveFontData->integer) { 
00512         ri.FS_WriteFile(va("fonts/fontImage_%i.dat", pointSize), font, sizeof(fontInfo_t));
00513     }
00514 
00515   Z_Free(out);
00516   
00517   ri.FS_FreeFile(faceData);
00518 #endif
00519 }

Here is the call graph for this function:

float readFloat  ) 
 

Definition at line 311 of file tr_font.c.

References fdFile, fdOffset, poor::ffred, and poor::fred.

Referenced by RE_RegisterFont().

00311                   {
00312     poor    me;
00313 #if idppc
00314     me.fred[0] = fdFile[fdOffset+3];
00315     me.fred[1] = fdFile[fdOffset+2];
00316     me.fred[2] = fdFile[fdOffset+1];
00317     me.fred[3] = fdFile[fdOffset+0];
00318 #else
00319     me.fred[0] = fdFile[fdOffset+0];
00320     me.fred[1] = fdFile[fdOffset+1];
00321     me.fred[2] = fdFile[fdOffset+2];
00322     me.fred[3] = fdFile[fdOffset+3];
00323 #endif
00324     fdOffset += 4;
00325     return me.ffred;
00326 }

int readInt  ) 
 

Definition at line 300 of file tr_font.c.

References fdFile, fdOffset, and i.

Referenced by RE_RegisterFont().

00300               {
00301     int i = fdFile[fdOffset]+(fdFile[fdOffset+1]<<8)+(fdFile[fdOffset+2]<<16)+(fdFile[fdOffset+3]<<24);
00302     fdOffset += 4;
00303     return i;
00304 }


Variable Documentation

byte* fdFile [static]
 

Definition at line 298 of file tr_font.c.

Referenced by RE_RegisterFont(), readFloat(), and readInt().

int fdOffset [static]
 

Definition at line 297 of file tr_font.c.

Referenced by RE_RegisterFont(), readFloat(), and readInt().

fontInfo_t registeredFont[MAX_FONTS] [static]
 

Definition at line 92 of file tr_font.c.

Referenced by RE_RegisterFont().

int registeredFontCount = 0 [static]
 

Definition at line 91 of file tr_font.c.

Referenced by R_DoneFreeType(), R_InitFreeType(), and RE_RegisterFont().


Generated on Thu Aug 25 15:03:32 2005 for Quake III Arena by  doxygen 1.3.9.1