Skip to content
Permalink
Browse files

Clean up some font registration code and fix a couple error handling …

…bugs
  • Loading branch information...
slipher committed Mar 31, 2019
1 parent d57fcb0 commit 97ea04de5968b295e25532667a27c1c91d2023f5
@@ -716,7 +716,7 @@ void CL_ShutdownAll()
CL_ShutdownCGame();

// Clear Faces
if ( re.UnregisterFont && cls.consoleFont )
if ( cls.consoleFont )
{
re.UnregisterFont( cls.consoleFont );
cls.consoleFont = nullptr;
@@ -3031,18 +3031,23 @@ bool CL_InitRenderer()
{
if ( cl_consoleFontScaling->value == 0 )
{
re.RegisterFont( cl_consoleFont->string, nullptr, cl_consoleFontSize->integer, &cls.consoleFont );
cls.consoleFont = re.RegisterFont( cl_consoleFont->string, nullptr, cl_consoleFontSize->integer );
}
else
{
// This gets 12px on 1920×1080 screen, which is libRocket default for 1em
int fontScale = std::min(cls.glconfig.vidWidth, cls.glconfig.vidHeight) / 90;

// fontScale / 12px gets 1px on 1920×1080 screen
re.RegisterFont( cl_consoleFont->string, nullptr, cl_consoleFontSize->integer * fontScale / 12, &cls.consoleFont );
cls.consoleFont = re.RegisterFont( cl_consoleFont->string, nullptr, cl_consoleFontSize->integer * fontScale / 12 );
}

cls.useLegacyConsoleFont = false;
if ( cls.consoleFont != nullptr )
cls.useLegacyConsoleFont = false;
}
else
{
Log::Warn("Font file '%s' not found", cl_consoleFont->string);
}

FS_FCloseFile( f );
@@ -50,14 +50,9 @@ qhandle_t RE_RegisterShader( const char *, RegisterShaderFlags_t )
{
return 1;
}
void RE_RegisterFont( const char *, const char *, int pointSize, fontInfo_t **font )
fontInfo_t* RE_RegisterFont( const char *, const char *, int pointSize )
{
if (!*font)
return;
(*font)->pointSize = pointSize;
(*font)->height = 1;
(*font)->glyphScale = 1.0f;
(*font)->name[0] = '\0';
return nullptr;
}
void RE_Glyph( fontInfo_t *, const char *, glyphInfo_t *glyph )
{
@@ -2131,6 +2131,7 @@ struct glyphInfo_t
char shaderName[ 32 ];
};

// Unlike with many other handle types, 0 is valid, not an error or default return value.
using fontHandle_t = int;

using glyphBlock_t = glyphInfo_t[256];
@@ -604,7 +604,7 @@ static void RE_FreeFontFile( void *data )
}
}

static fontHandle_t RE_RegisterFont_Internal( const char *fontName, const char *fallbackName, int pointSize, fontInfo_t **font )
fontInfo_t* RE_RegisterFont( const char *fontName, const char *fallbackName, int pointSize )
{
FT_Face face, fallback;
void *faceData, *fallbackData;
@@ -656,21 +656,20 @@ static fontHandle_t RE_RegisterFont_Internal( const char *fontName, const char *
}
else if ( pointSize == registeredFont[ i ].pointSize && Q_stricmp( strippedName, registeredFont[ i ].name ) == 0 )
{
*font = &registeredFont[ i ];
++fontUsage[ i ];
return i;
return &registeredFont[ i ];
}
}

*font = &registeredFont[ fontNo ];
memset( *font, 0, sizeof( fontInfo_t ) );

if ( fontNo < 0 )
{
Log::Warn("RE_RegisterFont: Too many fonts registered already." );
return -1;
return nullptr;
}

fontInfo_t* font = &registeredFont[ fontNo ];
memset( font, 0, sizeof( fontInfo_t ) );

len = ri.FS_ReadFile( fileName, nullptr );

if ( len > 0x5004 && len <= 0x5004 + MAX_QPATH ) // 256 glyphs, scale info, and the bitmap name
@@ -682,7 +681,7 @@ static fontHandle_t RE_RegisterFont_Internal( const char *fontName, const char *
fdOffset = 0;
fdFile = (byte*) faceData;

glyphs = (*font)->glyphBlock[0] = (glyphInfo_t*) ri.Z_Malloc( sizeof( glyphBlock_t ) );
glyphs = font->glyphBlock[0] = (glyphInfo_t*) ri.Z_Malloc( sizeof( glyphBlock_t ) );
memset( glyphs, 0, sizeof( glyphBlock_t ) );

for ( i = 0; i < GLYPHS_PER_FONT; i++ )
@@ -709,10 +708,10 @@ static fontHandle_t RE_RegisterFont_Internal( const char *fontName, const char *
fdOffset += sizeof( glyphs[ i ].shaderName );
}

(*font)->pointSize = pointSize;
(*font)->height = height;
(*font)->glyphScale = readFloat();
Q_strncpyz( (*font)->name, registeredName, sizeof( (*font)->name ) );
font->pointSize = pointSize;
font->height = height;
font->glyphScale = readFloat();
Q_strncpyz( font->name, registeredName, sizeof( font->name ) );

ri.FS_FreeFile( faceData );

@@ -722,17 +721,17 @@ static fontHandle_t RE_RegisterFont_Internal( const char *fontName, const char *
}

++fontUsage[ fontNo ];
return fontNo;
return font;
}

if ( ftLibrary == nullptr )
{
Log::Warn("RE_RegisterFont: FreeType not initialized." );
return -1;
return nullptr;
}

// FIXME: fallback name OR index no.
Q_strncpyz( (*font)->name, strippedName, sizeof( (*font)->name ) );
Q_strncpyz( font->name, strippedName, sizeof( font->name ) );

Q_strncpyz( fileName, fontName, sizeof( fileName ) );

@@ -742,23 +741,23 @@ static fontHandle_t RE_RegisterFont_Internal( const char *fontName, const char *
{
Log::Warn("RE_RegisterFont: Unable to read font file %s", fileName );
RE_FreeFontFile( faceData );
return -1;
return nullptr;
}

// allocate on the stack first in case we fail
if ( FT_New_Memory_Face( ftLibrary, (FT_Byte*) faceData, len, 0, &face ) )
{
Log::Warn("RE_RegisterFont: FreeType2, unable to allocate new face." );
RE_FreeFontFile( faceData );
return -1;
return nullptr;
}

if ( FT_Set_Char_Size( face, pointSize << 6, pointSize << 6, 72, 72 ) )
{
Log::Warn("RE_RegisterFont: FreeType2, Unable to set face char size." );
FT_Done_Face( face );
RE_FreeFontFile( faceData );
return -1;
return nullptr;
}

fallback = nullptr;
@@ -776,7 +775,7 @@ static fontHandle_t RE_RegisterFont_Internal( const char *fontName, const char *
RE_FreeFontFile( fallbackData );
FT_Done_Face( face );
RE_FreeFontFile( faceData );
return -1;
return nullptr;
}

// allocate on the stack first in case we fail
@@ -786,7 +785,7 @@ static fontHandle_t RE_RegisterFont_Internal( const char *fontName, const char *
RE_FreeFontFile( fallbackData );
FT_Done_Face( face );
RE_FreeFontFile( faceData );
return -1;
return nullptr;
}

if ( FT_Set_Char_Size( fallback, pointSize << 6, pointSize << 6, 72, 72 ) )
@@ -796,27 +795,22 @@ static fontHandle_t RE_RegisterFont_Internal( const char *fontName, const char *
RE_FreeFontFile( fallbackData );
FT_Done_Face( face );
RE_FreeFontFile( faceData );
return -1;
return nullptr;
}
}

(*font)->face = face;
(*font)->faceData = faceData;
(*font)->fallback = fallback;
(*font)->fallbackData = fallbackData;
(*font)->pointSize = pointSize;
(*font)->glyphScale = Com_Clamp( 24.0f, 64.0f, r_fontScale->value ) / pointSize;
(*font)->height = ceil( ( face->height / 64.0 ) * ( face->size->metrics.y_scale / 65536.0 ) * (*font)->glyphScale );
font->face = face;
font->faceData = faceData;
font->fallback = fallback;
font->fallbackData = fallbackData;
font->pointSize = pointSize;
font->glyphScale = Com_Clamp( 24.0f, 64.0f, r_fontScale->value ) / pointSize;
font->height = ceil( ( face->height / 64.0 ) * ( face->size->metrics.y_scale / 65536.0 ) * font->glyphScale );

RE_RenderChunk( *font, 0 );
RE_RenderChunk( font, 0 );

++fontUsage[ fontNo ];
return fontNo;
}

void RE_RegisterFont( const char *fontName, const char *fallbackName, int pointSize, fontInfo_t **font )
{
RE_RegisterFont_Internal( fontName, fallbackName, pointSize, font );
return font;
}

void R_InitFreeType()
@@ -3902,7 +3902,7 @@ static inline void halfToFloat( const f16vec4_t in, vec4_t out )
// font stuff
void R_InitFreeType();
void R_DoneFreeType();
void RE_RegisterFont( const char *fontName, const char *fallbackName, int pointSize, fontInfo_t **font );
fontInfo_t* RE_RegisterFont( const char *fontName, const char *fallbackName, int pointSize );
void RE_UnregisterFont( fontInfo_t *font );
void RE_Glyph(fontInfo_t *font, const char *str, glyphInfo_t *glyph);
void RE_GlyphChar(fontInfo_t *font, int ch, glyphInfo_t *glyph);
@@ -69,7 +69,7 @@ struct refexport_t
qhandle_t ( *RegisterSkin )( const char *name );
qhandle_t ( *RegisterShader )( const char *name,
RegisterShaderFlags_t flags );
void ( *RegisterFont )( const char *fontName, const char *fallbackName, int pointSize, fontInfo_t **font );
fontInfo_t* ( *RegisterFont )( const char *fontName, const char *fallbackName, int pointSize );
void ( *UnregisterFont )( fontInfo_t *font );
void ( *Glyph )( fontInfo_t *font, const char *str, glyphInfo_t *glyph );
void ( *GlyphChar )( fontInfo_t *font, int ch, glyphInfo_t *glyph );

0 comments on commit 97ea04d

Please sign in to comment.
You can’t perform that action at this time.