@ -90,11 +90,9 @@ static SpriteFont defaultFont; // Default font provided by raylib
/ / - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
/ / Module specific Functions Declaration
/ / - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
static int GetCharIndex ( SpriteFont font , int letter ) ;
static SpriteFont LoadImageFont ( Image image , Color key , int firstChar ) ; / / Load a Image font file ( XNA style )
# if defined(SUPPORT_FILEFORMAT_FNT)
static SpriteFont LoadBMFont ( const char * fileName ) ; / / Load a BMFont file ( AngelCode font file )
static SpriteFont LoadBMFont ( const char * fileName ) ; / / Load a BMFont file ( AngelCode font file )
# endif
# if defined(SUPPORT_FILEFORMAT_TTF)
static SpriteFont LoadTTF ( const char * fileName , int fontSize , int charsCount , int * fontChars ) ; / / Load spritefont from TTF data
@ -345,13 +343,13 @@ SpriteFont LoadSpriteFontEx(const char *fileName, int fontSize, int charsCount,
}
/ / Unload SpriteFont from GPU memory ( VRAM )
void UnloadSpriteFont ( SpriteFont spriteF ont)
void UnloadSpriteFont ( SpriteFont f ont)
{
/ / NOTE : Make sure spriteFont is not default font ( fallback )
if ( spriteF ont. texture . id ! = GetDefaultFont ( ) . texture . id )
if ( f ont. texture . id ! = GetDefaultFont ( ) . texture . id )
{
UnloadTexture ( spriteF ont. texture ) ;
free ( spriteF ont. chars ) ;
UnloadTexture ( f ont. texture ) ;
free ( f ont. chars ) ;
TraceLog ( LOG_DEBUG , " Unloaded sprite font data " ) ;
}
@ -377,7 +375,7 @@ void DrawText(const char *text, int posX, int posY, int fontSize, Color color)
/ / Draw text using SpriteFont
/ / NOTE : chars spacing is NOT proportional to fontSize
void DrawTextEx ( SpriteFont spriteF ont, const char * text , Vector2 position , float fontSize , int spacing , Color tint )
void DrawTextEx ( SpriteFont f ont, const char * text , Vector2 position , float fontSize , int spacing , Color tint )
{
int length = strlen ( text ) ;
int textOffsetX = 0 ; / / Offset between characters
@ -387,7 +385,7 @@ void DrawTextEx(SpriteFont spriteFont, const char *text, Vector2 position, float
unsigned char letter ; / / Current character
int index ; / / Index position in sprite font
scaleFactor = fontSize / spriteF ont. baseSize ;
scaleFactor = fontSize / f ont. baseSize ;
/ / NOTE : Some ugly hacks are made to support Latin - 1 Extended characters directly
/ / written in C code files ( codified by default as UTF - 8 )
@ -397,7 +395,7 @@ void DrawTextEx(SpriteFont spriteFont, const char *text, Vector2 position, float
if ( ( unsigned char ) text [ i ] = = ' \n ' )
{
/ / NOTE : Fixed line spacing of 1.5 lines
textOffsetY + = ( int ) ( ( spriteF ont. baseSize + spriteF ont. baseSize / 2 ) * scaleFactor ) ;
textOffsetY + = ( int ) ( ( f ont. baseSize + f ont. baseSize / 2 ) * scaleFactor ) ;
textOffsetX = 0 ;
}
else
@ -406,29 +404,29 @@ void DrawTextEx(SpriteFont spriteFont, const char *text, Vector2 position, float
{
/ / Support UTF - 8 encoded values from [ 0xc2 0x80 ] - > [ 0xc2 0xbf ] ( ¿ )
letter = ( unsigned char ) text [ i + 1 ] ;
index = GetCharIndex ( spriteF ont , ( int ) letter ) ;
index = GetGlyphIndex ( f ont , ( int ) letter ) ;
i + + ;
}
else if ( ( unsigned char ) text [ i ] = = 0xc3 ) / / UTF - 8 encoding identification HACK !
{
/ / Support UTF - 8 encoded values from [ 0xc3 0x80 ] ( À ) - > [ 0xc3 0xbf ] ( ÿ )
letter = ( unsigned char ) text [ i + 1 ] ;
index = GetCharIndex ( spriteF ont , ( int ) letter + 64 ) ;
index = GetGlyphIndex ( f ont , ( int ) letter + 64 ) ;
i + + ;
}
else index = GetCharIndex ( spriteF ont , ( unsigned char ) text [ i ] ) ;
else index = GetGlyphIndex ( f ont , ( unsigned char ) text [ i ] ) ;
if ( ( unsigned char ) text [ i ] ! = ' ' )
{
DrawTexturePro ( spriteF ont. texture , spriteF ont. chars [ index ] . rec ,
( Rectangle ) { position . x + textOffsetX + spriteF ont. chars [ index ] . offsetX * scaleFactor ,
position . y + textOffsetY + spriteF ont. chars [ index ] . offsetY * scaleFactor ,
spriteF ont. chars [ index ] . rec . width * scaleFactor ,
spriteF ont. chars [ index ] . rec . height * scaleFactor } , ( Vector2 ) { 0 , 0 } , 0.0f , tint ) ;
DrawTexturePro ( f ont. texture , f ont. chars [ index ] . rec ,
( Rectangle ) { position . x + textOffsetX + f ont. chars [ index ] . offsetX * scaleFactor ,
position . y + textOffsetY + f ont. chars [ index ] . offsetY * scaleFactor ,
f ont. chars [ index ] . rec . width * scaleFactor ,
f ont. chars [ index ] . rec . height * scaleFactor } , ( Vector2 ) { 0 , 0 } , 0.0f , tint ) ;
}
if ( spriteF ont. chars [ index ] . advanceX = = 0 ) textOffsetX + = ( int ) ( spriteF ont. chars [ index ] . rec . width * scaleFactor + spacing ) ;
else textOffsetX + = ( int ) ( spriteF ont. chars [ index ] . advanceX * scaleFactor + spacing ) ;
if ( f ont. chars [ index ] . advanceX = = 0 ) textOffsetX + = ( int ) ( f ont. chars [ index ] . rec . width * scaleFactor + spacing ) ;
else textOffsetX + = ( int ) ( f ont. chars [ index ] . advanceX * scaleFactor + spacing ) ;
}
}
}
@ -490,7 +488,7 @@ int MeasureText(const char *text, int fontSize)
}
/ / Measure string size for SpriteFont
Vector2 MeasureTextEx ( SpriteFont spriteF ont, const char * text , float fontSize , int spacing )
Vector2 MeasureTextEx ( SpriteFont f ont, const char * text , float fontSize , int spacing )
{
int len = strlen ( text ) ;
int tempLen = 0 ; / / Used to count longer text line num chars
@ -499,8 +497,8 @@ Vector2 MeasureTextEx(SpriteFont spriteFont, const char *text, float fontSize, i
float textWidth = 0 ;
float tempTextWidth = 0 ; / / Used to count longer text line width
float textHeight = ( float ) spriteF ont. baseSize ;
float scaleFactor = fontSize / ( float ) spriteF ont. baseSize ;
float textHeight = ( float ) f ont. baseSize ;
float scaleFactor = fontSize / ( float ) f ont. baseSize ;
for ( int i = 0 ; i < len ; i + + )
{
@ -508,17 +506,17 @@ Vector2 MeasureTextEx(SpriteFont spriteFont, const char *text, float fontSize, i
if ( text [ i ] ! = ' \n ' )
{
int index = GetCharIndex ( spriteF ont , ( int ) text [ i ] ) ;
int index = GetGlyphIndex ( f ont , ( int ) text [ i ] ) ;
if ( spriteF ont. chars [ index ] . advanceX ! = 0 ) textWidth + = spriteF ont. chars [ index ] . advanceX ;
else textWidth + = ( spriteF ont. chars [ index ] . rec . width + spriteF ont. chars [ index ] . offsetX ) ;
if ( f ont. chars [ index ] . advanceX ! = 0 ) textWidth + = f ont. chars [ index ] . advanceX ;
else textWidth + = ( f ont. chars [ index ] . rec . width + f ont. chars [ index ] . offsetX ) ;
}
else
{
if ( tempTextWidth < textWidth ) tempTextWidth = textWidth ;
lenCounter = 0 ;
textWidth = 0 ;
textHeight + = ( ( float ) spriteF ont. baseSize * 1.5f ) ; / / NOTE : Fixed line spacing of 1.5 lines
textHeight + = ( ( float ) f ont. baseSize * 1.5f ) ; / / NOTE : Fixed line spacing of 1.5 lines
}
if ( tempLen < lenCounter ) tempLen = lenCounter ;
@ -533,6 +531,28 @@ Vector2 MeasureTextEx(SpriteFont spriteFont, const char *text, float fontSize, i
return vec ;
}
/ / Returns index position for a unicode character on spritefont
int GetGlyphIndex ( SpriteFont font , int character )
{
# define UNORDERED_CHARSET
# if defined(UNORDERED_CHARSET)
int index = 0 ;
for ( int i = 0 ; i < font . charsCount ; i + + )
{
if ( font . chars [ i ] . value = = character )
{
index = i ;
break ;
}
}
return index ;
# else
return ( character - 32 ) ;
# endif
}
/ / Shows current FPS on top - left corner
/ / NOTE : Uses default font
void DrawFPS ( int posX , int posY )
@ -559,27 +579,6 @@ void DrawFPS(int posX, int posY)
/ / Module specific Functions Definition
/ / - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
static int GetCharIndex ( SpriteFont font , int letter )
{
# define UNORDERED_CHARSET
# if defined(UNORDERED_CHARSET)
int index = 0 ;
for ( int i = 0 ; i < font . charsCount ; i + + )
{
if ( font . chars [ i ] . value = = letter )
{
index = i ;
break ;
}
}
return index ;
# else
return ( letter - 32 ) ;
# endif
}
/ / Load an Image font file ( XNA style )
static SpriteFont LoadImageFont ( Image image , Color key , int firstChar )
{