Update tr_font.c to ioq3 latest (r2232)

This commit is contained in:
James Canete 2012-04-06 01:47:21 +00:00
parent f9551a6179
commit 63216cd936
1 changed files with 232 additions and 229 deletions

View File

@ -58,11 +58,9 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
// 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
// 5. 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
// 6. 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
@ -75,11 +73,11 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#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>
#include FT_ERRORS_H
#include FT_SYSTEM_H
#include FT_IMAGE_H
#include FT_FREETYPE_H
#include FT_OUTLINE_H
#define _FLOOR(x) ((x) & -64)
#define _CEIL(x) (((x)+63) & -64)
@ -94,7 +92,6 @@ 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);
@ -107,7 +104,6 @@ void R_GetGlyphInfo(FT_GlyphSlot glyph, int *left, int *right, int *width, int *
FT_Bitmap *R_RenderGlyph(FT_GlyphSlot glyph, glyphInfo_t* glyphOut) {
FT_Bitmap *bit2;
int left, right, width, top, bottom, height, pitch, size;
@ -116,14 +112,14 @@ FT_Bitmap *R_RenderGlyph(FT_GlyphSlot glyph, glyphInfo_t* glyphOut) {
if ( glyph->format == ft_glyph_format_outline ) {
size = pitch*height;
bit2 = Z_Malloc(sizeof(FT_Bitmap));
bit2 = ri.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->buffer = ri.Malloc(pitch*height);
bit2->num_grays = 256;
Com_Memset( bit2->buffer, 0, size );
@ -138,8 +134,7 @@ FT_Bitmap *R_RenderGlyph(FT_GlyphSlot glyph, glyphInfo_t* glyphOut) {
glyphOut->bottom = bottom;
return bit2;
}
else {
} else {
ri.Printf(PRINT_ALL, "Non-outline fonts are not supported\n");
}
return NULL;
@ -148,8 +143,11 @@ FT_Bitmap *R_RenderGlyph(FT_GlyphSlot glyph, glyphInfo_t* glyphOut) {
void WriteTGA (char *filename, byte *data, int width, int height) {
byte *buffer;
int i, c;
int row;
unsigned char *flip;
unsigned char *src, *dst;
buffer = Z_Malloc(width*height*4 + 18);
buffer = ri.Malloc(width*height*4 + 18);
Com_Memset (buffer, 0, 18);
buffer[2] = 2; // uncompressed type
buffer[12] = width&255;
@ -168,13 +166,26 @@ void WriteTGA (char *filename, byte *data, int width, int height) {
buffer[i+3] = data[i-18+3]; // alpha
}
// flip upside down
flip = (unsigned char *)ri.Malloc(width*4);
for(row = 0; row < height/2; row++)
{
src = buffer + 18 + row * 4 * width;
dst = buffer + 18 + (height - row - 1) * 4 * width;
Com_Memcpy(flip, src, width*4);
Com_Memcpy(src, dst, width*4);
Com_Memcpy(dst, flip, width*4);
}
ri.Free(flip);
ri.FS_WriteFile(filename, buffer, c);
//f = fopen (filename, "wb");
//fwrite (buffer, 1, c, f);
//fclose (f);
Z_Free (buffer);
ri.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) {
@ -200,8 +211,8 @@ static glyphInfo_t *RE_ConstructGlyphInfo(unsigned char *imageOut, int *xOut, in
}
if (calcHeight) {
Z_Free(bitmap->buffer);
Z_Free(bitmap);
ri.Free(bitmap->buffer);
ri.Free(bitmap);
return &glyph;
}
@ -219,21 +230,15 @@ static glyphInfo_t *RE_ConstructGlyphInfo(unsigned char *imageOut, int *xOut, in
// 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) {
if (*yOut + *maxHeight + 1 >= 255) {
*yOut = -1;
*xOut = -1;
Z_Free(bitmap->buffer);
Z_Free(bitmap);
ri.Free(bitmap->buffer);
ri.Free(bitmap);
return &glyph;
}
@ -265,7 +270,6 @@ static glyphInfo_t *RE_ConstructGlyphInfo(unsigned char *imageOut, int *xOut, in
src += glyph.pitch;
dst += 256;
}
} else {
for (i = 0; i < glyph.height; i++) {
@ -288,8 +292,8 @@ static glyphInfo_t *RE_ConstructGlyphInfo(unsigned char *imageOut, int *xOut, in
*xOut += scaled_width + 1;
}
Z_Free(bitmap->buffer);
Z_Free(bitmap);
ri.Free(bitmap->buffer);
ri.Free(bitmap);
return &glyph;
}
@ -330,7 +334,7 @@ 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;
int scaledSize, newSize, maxHeight, left;
unsigned char *out, *imageBuff;
glyphInfo_t *glyph;
image_t *image;
@ -359,7 +363,7 @@ void RE_RegisterFont(const char *fontName, int pointSize, fontInfo_t *font) {
R_SyncRenderThread();
if (registeredFontCount >= MAX_FONTS) {
ri.Printf(PRINT_ALL, "RE_RegisterFont: Too many fonts registered already.\n");
ri.Printf(PRINT_WARNING, "RE_RegisterFont: Too many fonts registered already.\n");
return;
}
@ -389,8 +393,8 @@ void RE_RegisterFont(const char *fontName, int pointSize, fontInfo_t *font) {
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;
Q_strncpyz(font->glyphs[i].shaderName, (const char *)&fdFile[fdOffset], sizeof(font->glyphs[i].shaderName));
fdOffset += sizeof(font->glyphs[i].shaderName);
}
font->glyphScale = readFloat();
Com_Memcpy(font->name, &fdFile[fdOffset], MAX_QPATH);
@ -405,28 +409,28 @@ void RE_RegisterFont(const char *fontName, int pointSize, fontInfo_t *font) {
}
#ifndef BUILD_FREETYPE
ri.Printf(PRINT_ALL, "RE_RegisterFont: FreeType code not available\n");
ri.Printf(PRINT_WARNING, "RE_RegisterFont: FreeType code not available\n");
#else
if (ftLibrary == NULL) {
ri.Printf(PRINT_ALL, "RE_RegisterFont: FreeType not initialized.\n");
ri.Printf(PRINT_WARNING, "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");
ri.Printf(PRINT_WARNING, "RE_RegisterFont: Unable to read font file '%s'\n", fontName);
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");
ri.Printf(PRINT_WARNING, "RE_RegisterFont: FreeType, 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");
ri.Printf(PRINT_WARNING, "RE_RegisterFont: FreeType, unable to set face char size.\n");
return;
}
@ -435,9 +439,9 @@ void RE_RegisterFont(const char *fontName, int pointSize, fontInfo_t *font) {
// 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);
out = ri.Malloc(1024*1024);
if (out == NULL) {
ri.Printf(PRINT_ALL, "RE_RegisterFont: Z_Malloc failure during output image creation.\n");
ri.Printf(PRINT_WARNING, "RE_RegisterFont: ri.Malloc failure during output image creation.\n");
return;
}
Com_Memset(out, 0, 1024*1024);
@ -465,10 +469,9 @@ void RE_RegisterFont(const char *fontName, int pointSize, fontInfo_t *font) {
scaledSize = 256*256;
newSize = scaledSize * 4;
imageBuff = Z_Malloc(newSize);
imageBuff = ri.Malloc(newSize);
left = 0;
max = 0;
satLevels = 255;
for ( k = 0; k < (scaledSize) ; k++ ) {
if (max < out[k]) {
max = out[k];
@ -503,7 +506,7 @@ void RE_RegisterFont(const char *fontName, int pointSize, fontInfo_t *font) {
Com_Memset(out, 0, 1024*1024);
xOut = 0;
yOut = 0;
Z_Free(imageBuff);
ri.Free(imageBuff);
i++;
} else {
Com_Memcpy(&font->glyphs[i], glyph, sizeof(glyphInfo_t));
@ -519,7 +522,7 @@ void RE_RegisterFont(const char *fontName, int pointSize, fontInfo_t *font) {
ri.FS_WriteFile(va("fonts/fontImage_%i.dat", pointSize), font, sizeof(fontInfo_t));
}
Z_Free(out);
ri.Free(out);
ri.FS_FreeFile(faceData);
#endif
@ -530,7 +533,7 @@ void RE_RegisterFont(const char *fontName, int pointSize, fontInfo_t *font) {
void R_InitFreeType(void) {
#ifdef BUILD_FREETYPE
if (FT_Init_FreeType( &ftLibrary )) {
ri.Printf(PRINT_ALL, "R_InitFreeType: Unable to initialize FreeType.\n");
ri.Printf(PRINT_WARNING, "R_InitFreeType: Unable to initialize FreeType.\n");
}
#endif
registeredFontCount = 0;