From 9af10686b28cb48de7cdd37625ae119e913d36b2 Mon Sep 17 00:00:00 2001 From: raysan5 Date: Tue, 6 Oct 2015 17:17:53 +0200 Subject: Added comments and more... Corrected bug on defaultFont.size Renamed funcs: ImageConvertFormat() -> ImageFormat() ImageConvertToPOT() -> ImageToPOT() --- src/text.c | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) (limited to 'src/text.c') diff --git a/src/text.c b/src/text.c index 35a5f049..ebd5d44e 100644 --- a/src/text.c +++ b/src/text.c @@ -172,7 +172,7 @@ extern void LoadDefaultFont(void) //fclose(myimage); Image image = LoadImageEx(imagePixels, imWidth, imHeight); - ImageConvertFormat(&image, UNCOMPRESSED_GRAY_ALPHA); + ImageFormat(&image, UNCOMPRESSED_GRAY_ALPHA); free(imagePixels); @@ -211,7 +211,7 @@ extern void LoadDefaultFont(void) else currentPosX = testPosX; } - defaultFont.size = defaultFont.charRecs[0].y; + defaultFont.size = defaultFont.charRecs[0].height; TraceLog(INFO, "[TEX ID %i] Default font loaded successfully", defaultFont.texture.id); } @@ -242,10 +242,10 @@ SpriteFont LoadSpriteFont(const char *fileName) Image image = LoadImage(fileName); #if defined(PLATFORM_WEB) - ImageConvertToPOT(&image, MAGENTA); + ImageToPOT(&image, MAGENTA); #endif // Process bitmap font pixel data to get characters measures - // spriteFont.charSet data is filled inside the function and memory is allocated! + // spriteFont chars data is filled inside the function and memory is allocated! int numChars = ParseImageData(image, &spriteFont.charValues, &spriteFont.charRecs); TraceLog(DEBUG, "[%s] SpriteFont data parsed correctly", fileName); @@ -288,7 +288,6 @@ void DrawText(const char *text, int posX, int posY, int fontSize, Color color) } // Draw text using SpriteFont -// NOTE: If font size is lower than base size, base size is used // NOTE: chars spacing is NOT proportional to fontSize void DrawTextEx(SpriteFont spriteFont, const char *text, Vector2 position, int fontSize, int spacing, Color tint) { @@ -299,21 +298,27 @@ void DrawTextEx(SpriteFont spriteFont, const char *text, Vector2 position, int f Rectangle rec; - //if (fontSize <= spriteFont.charRecs[0].height) scaleFactor = 1.0f; - //else scaleFactor = (float)fontSize / spriteFont.charRecs[0].height; - scaleFactor = (float)fontSize/spriteFont.charRecs[0].height; + // NOTE: Some ugly hacks are made to support Latin-1 Extended characters directly + // written in C code files (codified by default as UTF-8) + for(int i = 0; i < length; i++) { - if ((unsigned char)text[i] == 0xc2) + // TODO: Right now we are supposing characters follow a continous order and start at FONT_FIRST_CHAR, + // this sytem can be improved to support any characters order and init value... + // An intermediate table could be created to link char values with predefined char position index in chars rectangle array + + if ((unsigned char)text[i] == 0xc2) // UTF-8 encoding identification HACK! { + // Support UTF-8 encoded values from [0xc2 0x80] -> [0xc2 0xbf](¿) letter = (unsigned char)text[i + 1]; rec = spriteFont.charRecs[letter - FONT_FIRST_CHAR]; i++; } - else if ((unsigned char)text[i] == 0xc3) + 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]; rec = spriteFont.charRecs[letter - FONT_FIRST_CHAR + 64]; i++; @@ -569,7 +574,7 @@ static SpriteFont LoadRBMF(const char *fileName) } Image image = LoadImageEx(imagePixels, rbmfHeader.imgWidth, rbmfHeader.imgHeight); - ImageConvertFormat(&image, UNCOMPRESSED_GRAY_ALPHA); + ImageFormat(&image, UNCOMPRESSED_GRAY_ALPHA); free(imagePixels); -- cgit v1.2.3 From e9792ef7713d9b0066cabdc66227cb5431462797 Mon Sep 17 00:00:00 2001 From: raysan5 Date: Tue, 6 Oct 2015 17:32:00 +0200 Subject: Convert to UTF-8 --- src/text.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/text.c') diff --git a/src/text.c b/src/text.c index ebd5d44e..65650956 100644 --- a/src/text.c +++ b/src/text.c @@ -311,14 +311,14 @@ void DrawTextEx(SpriteFont spriteFont, const char *text, Vector2 position, int f if ((unsigned char)text[i] == 0xc2) // UTF-8 encoding identification HACK! { - // Support UTF-8 encoded values from [0xc2 0x80] -> [0xc2 0xbf](¿) + // Support UTF-8 encoded values from [0xc2 0x80] -> [0xc2 0xbf](¿) letter = (unsigned char)text[i + 1]; rec = spriteFont.charRecs[letter - FONT_FIRST_CHAR]; i++; } else if ((unsigned char)text[i] == 0xc3) // UTF-8 encoding identification HACK! { - // Support UTF-8 encoded values from [0xc3 0x80](À) -> [0xc3 0xbf](ÿ) + // Support UTF-8 encoded values from [0xc3 0x80](À) -> [0xc3 0xbf](ÿ) letter = (unsigned char)text[i + 1]; rec = spriteFont.charRecs[letter - FONT_FIRST_CHAR + 64]; i++; -- cgit v1.2.3 From f13b30d6060ce7541a7ef7f3074966c623128090 Mon Sep 17 00:00:00 2001 From: Ray San Date: Wed, 21 Oct 2015 18:24:44 +0200 Subject: Feature: Line-break support for text --- src/text.c | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) (limited to 'src/text.c') diff --git a/src/text.c b/src/text.c index 65650956..381aa052 100644 --- a/src/text.c +++ b/src/text.c @@ -293,6 +293,7 @@ void DrawTextEx(SpriteFont spriteFont, const char *text, Vector2 position, int f { int length = strlen(text); int offsetX = 0; + int offsetY = 0; // Line break! float scaleFactor; unsigned char letter; @@ -323,11 +324,23 @@ void DrawTextEx(SpriteFont spriteFont, const char *text, Vector2 position, int f rec = spriteFont.charRecs[letter - FONT_FIRST_CHAR + 64]; i++; } - else rec = spriteFont.charRecs[(int)text[i] - FONT_FIRST_CHAR]; + else + { + if ((unsigned char)text[i] == '\n') + { + offsetY += ((spriteFont.size + spriteFont.size/2)*scaleFactor); + offsetX = 0; + rec.x = -1; + } + else rec = spriteFont.charRecs[(int)text[i] - FONT_FIRST_CHAR]; + } - DrawTexturePro(spriteFont.texture, rec, (Rectangle){ position.x + offsetX, position.y, rec.width*scaleFactor, rec.height*scaleFactor} , (Vector2){ 0, 0 }, 0.0f, tint); + if (rec.x > 0) + { + DrawTexturePro(spriteFont.texture, rec, (Rectangle){ position.x + offsetX, position.y + offsetY, rec.width*scaleFactor, rec.height*scaleFactor} , (Vector2){ 0, 0 }, 0.0f, tint); - offsetX += (rec.width*scaleFactor + spacing); + offsetX += (rec.width*scaleFactor + spacing); + } } } -- cgit v1.2.3 From afdf357fbee7f01535bddefb4e62cb1308ed55b5 Mon Sep 17 00:00:00 2001 From: Ray Date: Sun, 25 Oct 2015 01:50:15 +0200 Subject: Added some image manipulation functions Added (or completed functions): Image ImageText(const char *text, int fontSize, Color color); Image ImageTextEx(SpriteFont font, const char *text, int fontSize, int spacing, Color tint); void ImageFlipVertical(Image *image); void ImageFlipHorizontal(Image *image); void ImageColorTint(Image *image, Color color); void ImageColorInvert(Image *image); void ImageColorGrayscale(Image *image); void ImageColorContrast(Image *image, float contrast); void ImageColorBrightness(Image *image, int brightness); --- src/text.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) (limited to 'src/text.c') diff --git a/src/text.c b/src/text.c index 381aa052..5e947187 100644 --- a/src/text.c +++ b/src/text.c @@ -279,9 +279,7 @@ void DrawText(const char *text, int posX, int posY, int fontSize, Color color) Vector2 position = { (float)posX, (float)posY }; int defaultFontSize = 10; // Default Font chars height in pixel - if (fontSize < defaultFontSize) fontSize = defaultFontSize; - int spacing = fontSize / defaultFontSize; DrawTextEx(defaultFont, text, position, fontSize, spacing, color); @@ -380,7 +378,7 @@ Vector2 MeasureTextEx(SpriteFont spriteFont, const char *text, int fontSize, int for (int i = 0; i < len; i++) { - textWidth += spriteFont.charRecs[(int)text[i] - FONT_FIRST_CHAR].width; + if (text[i] != '\n') textWidth += spriteFont.charRecs[(int)text[i] - FONT_FIRST_CHAR].width; } if (fontSize <= spriteFont.charRecs[0].height) scaleFactor = 1.0f; -- cgit v1.2.3 From 2fa7e00f169c87585a46b178e6282ed63889189c Mon Sep 17 00:00:00 2001 From: Ray Date: Thu, 5 Nov 2015 12:28:45 +0100 Subject: Variables initialization --- src/text.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/text.c') diff --git a/src/text.c b/src/text.c index 5e947187..7b9660dd 100644 --- a/src/text.c +++ b/src/text.c @@ -649,7 +649,7 @@ static SpriteFont LoadTTF(const char *fileName, int fontSize) // 1) Generate sprite sheet image with characters from TTF // 2) Load image as SpriteFont - SpriteFont font; + SpriteFont font = { 0 }; Image image; image.width = 512; -- cgit v1.2.3 From 85908befeabecf34f04b983173d73a4f80fbf4b7 Mon Sep 17 00:00:00 2001 From: raysan5 Date: Thu, 3 Dec 2015 13:44:45 +0100 Subject: Corrected bug on spritefont loading --- src/text.c | 37 ++++++++++++++++++++++--------------- 1 file changed, 22 insertions(+), 15 deletions(-) (limited to 'src/text.c') diff --git a/src/text.c b/src/text.c index 7b9660dd..c62704aa 100644 --- a/src/text.c +++ b/src/text.c @@ -240,20 +240,25 @@ SpriteFont LoadSpriteFont(const char *fileName) else { Image image = LoadImage(fileName); - -#if defined(PLATFORM_WEB) - ImageToPOT(&image, MAGENTA); -#endif - // Process bitmap font pixel data to get characters measures - // spriteFont chars data is filled inside the function and memory is allocated! - int numChars = ParseImageData(image, &spriteFont.charValues, &spriteFont.charRecs); - - TraceLog(DEBUG, "[%s] SpriteFont data parsed correctly", fileName); - TraceLog(DEBUG, "[%s] SpriteFont num chars detected: %i", fileName, numChars); - - spriteFont.numChars = numChars; - spriteFont.texture = LoadTextureFromImage(image); // Convert loaded image to OpenGL texture - spriteFont.size = spriteFont.charRecs[0].height; + + if (image.data != NULL) + { + // Process bitmap font pixel data to get characters measures + // spriteFont chars data is filled inside the function and memory is allocated! + int numChars = ParseImageData(image, &spriteFont.charValues, &spriteFont.charRecs); + + TraceLog(DEBUG, "[%s] SpriteFont data parsed correctly", fileName); + TraceLog(DEBUG, "[%s] SpriteFont num chars detected: %i", fileName, numChars); + + spriteFont.numChars = numChars; + spriteFont.texture = LoadTextureFromImage(image); // Convert loaded image to OpenGL texture + spriteFont.size = spriteFont.charRecs[0].height; + } + else + { + TraceLog(WARNING, "[%s] SpriteFont could not be loaded, using default font", fileName, numChars); + spriteFont = GetDefaultFont(); + } UnloadImage(image); } @@ -545,7 +550,9 @@ static SpriteFont LoadRBMF(const char *fileName) if (rbmfFile == NULL) { - TraceLog(WARNING, "[%s] rBMF font file could not be opened", fileName); + TraceLog(WARNING, "[%s] rBMF font file could not be opened, using default font", fileName); + + spriteFont = GetDefaultFont(); } else { -- cgit v1.2.3 From b62f7c3057cdfefacb9dc46e3096ea377a7f05a6 Mon Sep 17 00:00:00 2001 From: raysan5 Date: Wed, 9 Dec 2015 20:21:58 +0100 Subject: Corrected bug --- src/text.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/text.c') diff --git a/src/text.c b/src/text.c index c62704aa..081a8491 100644 --- a/src/text.c +++ b/src/text.c @@ -256,7 +256,7 @@ SpriteFont LoadSpriteFont(const char *fileName) } else { - TraceLog(WARNING, "[%s] SpriteFont could not be loaded, using default font", fileName, numChars); + TraceLog(WARNING, "[%s] SpriteFont could not be loaded, using default font", fileName); spriteFont = GetDefaultFont(); } -- cgit v1.2.3 From f144b6bae43465a62a45f8e45ab2099f215664dc Mon Sep 17 00:00:00 2001 From: raysan5 Date: Wed, 9 Dec 2015 20:57:50 +0100 Subject: MeasureTextEx() - Added support for multi-line size measure --- src/text.c | 32 +++++++++++++++++++++++++++----- 1 file changed, 27 insertions(+), 5 deletions(-) (limited to 'src/text.c') diff --git a/src/text.c b/src/text.c index 081a8491..d24ff972 100644 --- a/src/text.c +++ b/src/text.c @@ -378,20 +378,42 @@ int MeasureText(const char *text, int fontSize) Vector2 MeasureTextEx(SpriteFont spriteFont, const char *text, int fontSize, int spacing) { int len = strlen(text); + int tempLen = 0; // Used to count longer text line num chars + int lenCounter = 0; + int textWidth = 0; + int tempTextWidth = 0; // Used to count longer text line width + + int textHeight = spriteFont.size; float scaleFactor; for (int i = 0; i < len; i++) { - if (text[i] != '\n') textWidth += spriteFont.charRecs[(int)text[i] - FONT_FIRST_CHAR].width; + lenCounter++; + + if (text[i] != '\n') + { + textWidth += spriteFont.charRecs[(int)text[i] - FONT_FIRST_CHAR].width; + } + else + { + if (tempTextWidth < textWidth) tempTextWidth = textWidth; + lenCounter = 0; + textWidth = 0; + textHeight += (spriteFont.size + spriteFont.size/2); + } + + if (tempLen < lenCounter) tempLen = lenCounter; } + + if (tempTextWidth < textWidth) tempTextWidth = textWidth; - if (fontSize <= spriteFont.charRecs[0].height) scaleFactor = 1.0f; - else scaleFactor = (float)fontSize / spriteFont.charRecs[0].height; + if (fontSize <= spriteFont.size) scaleFactor = 1.0f; + else scaleFactor = (float)fontSize/spriteFont.size; Vector2 vec; - vec.x = (float)textWidth * scaleFactor + (len - 1) * spacing; // Adds chars spacing to measure - vec.y = (float)spriteFont.charRecs[0].height * scaleFactor; + vec.x = (float)tempTextWidth*scaleFactor + (tempLen - 1)*spacing; // Adds chars spacing to measure + vec.y = (float)textHeight*scaleFactor; return vec; } -- cgit v1.2.3 From da28cff0f62b5f14e1f557e17587e964ad0f79fd Mon Sep 17 00:00:00 2001 From: raysan5 Date: Wed, 30 Dec 2015 13:33:26 +0100 Subject: Added function: SubText() Useful to get a piece of text, could be used for text typing animations --- src/text.c | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) (limited to 'src/text.c') diff --git a/src/text.c b/src/text.c index d24ff972..0930b779 100644 --- a/src/text.c +++ b/src/text.c @@ -44,6 +44,7 @@ #define FONT_FIRST_CHAR 32 #define MAX_FONTCHARS 128 #define MAX_FORMATTEXT_LENGTH 64 +#define MAX_SUBTEXT_LENGTH 64 #define BIT_CHECK(a,b) ((a) & (1<<(b))) @@ -360,6 +361,31 @@ const char *FormatText(const char *text, ...) return buffer; } +// Get a piece of a text string +const char *SubText(const char *text, int position, int length) +{ + static char buffer[MAX_SUBTEXT_LENGTH]; + int textLength = strlen(text); + + if (position >= textLength) + { + position = textLength - 1; + length = 0; + } + + if (length >= textLength) length = textLength; + + for (int c = 0 ; c < length ; c++) + { + *(buffer+c) = *(text+position); + text++; + } + + *(buffer+length) = '\0'; + + return buffer; +} + // Measure string width for default font int MeasureText(const char *text, int fontSize) { -- cgit v1.2.3 From fa057f512f6bcfb84a7f736d10c45da50793c8d9 Mon Sep 17 00:00:00 2001 From: raysan5 Date: Sat, 2 Jan 2016 10:45:51 +0100 Subject: Improved fonts support Added LoadBMFont() to load AngelCode fonts (.fnt) Implemented LoadTTF() to load .ttf fonts (crappy packaging) --- src/text.c | 306 +++++++++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 207 insertions(+), 99 deletions(-) (limited to 'src/text.c') diff --git a/src/text.c b/src/text.c index 0930b779..1a75b9e4 100644 --- a/src/text.c +++ b/src/text.c @@ -34,10 +34,12 @@ // Following libs are used on LoadTTF() #define STB_TRUETYPE_IMPLEMENTATION -#define STB_RECT_PACK_IMPLEMENTATION -#include "stb_rect_pack.h" #include "stb_truetype.h" +// Rectangle packing functions (not used at the moment) +//#define STB_RECT_PACK_IMPLEMENTATION +//#include "stb_rect_pack.h" + //---------------------------------------------------------------------------------- // Defines and Macros //---------------------------------------------------------------------------------- @@ -70,6 +72,7 @@ static SpriteFont defaultFont; // Default font provided by raylib static bool PixelIsMagenta(Color p); // Check if a pixel is magenta static int ParseImageData(Image image, int **charValues, Rectangle **charSet); // Parse image pixel data to obtain characters data static SpriteFont LoadRBMF(const char *fileName); // Load a rBMF font file (raylib BitMap Font) +static SpriteFont LoadBMFont(const char *fileName); // Load a BMFont file (AngelCode font file) static SpriteFont LoadTTF(const char *fileName, int fontSize); // Generate a sprite font image from TTF data (font size required) extern void LoadDefaultFont(void); @@ -185,6 +188,10 @@ extern void LoadDefaultFont(void) defaultFont.charValues = (int *)malloc(defaultFont.numChars*sizeof(int)); defaultFont.charRecs = (Rectangle *)malloc(defaultFont.numChars*sizeof(Rectangle)); // Allocate space for our character rectangle data // This memory should be freed at end! --> Done on CloseWindow() + + defaultFont.charOffsets = (Vector2 *)malloc(defaultFont.numChars*sizeof(Vector2)); + defaultFont.charAdvanceX = (int *)malloc(defaultFont.numChars*sizeof(int)); + int currentLine = 0; int currentPosX = charsDivisor; int testPosX = charsDivisor; @@ -210,6 +217,10 @@ extern void LoadDefaultFont(void) defaultFont.charRecs[i].y = charsDivisor + currentLine*(charsHeight + charsDivisor); } else currentPosX = testPosX; + + // NOTE: On default font character offsets and xAdvance are not required + defaultFont.charOffsets[i] = (Vector2){ 0.0f, 0.0f }; + defaultFont.charAdvanceX[i] = 0; } defaultFont.size = defaultFont.charRecs[0].height; @@ -222,6 +233,8 @@ extern void UnloadDefaultFont(void) UnloadTexture(defaultFont.texture); free(defaultFont.charValues); free(defaultFont.charRecs); + free(defaultFont.charOffsets); + free(defaultFont.charAdvanceX); } // Get the default font, useful to be used with extended parameters @@ -237,7 +250,8 @@ SpriteFont LoadSpriteFont(const char *fileName) // Check file extension if (strcmp(GetExtension(fileName),"rbmf") == 0) spriteFont = LoadRBMF(fileName); - else if (strcmp(GetExtension(fileName),"ttf") == 0) spriteFont = LoadTTF(fileName, 20); + else if (strcmp(GetExtension(fileName),"ttf") == 0) spriteFont = LoadTTF(fileName, 32); + else if (strcmp(GetExtension(fileName),"fnt") == 0) spriteFont = LoadBMFont(fileName); else { Image image = LoadImage(fileName); @@ -254,6 +268,16 @@ SpriteFont LoadSpriteFont(const char *fileName) spriteFont.numChars = numChars; spriteFont.texture = LoadTextureFromImage(image); // Convert loaded image to OpenGL texture spriteFont.size = spriteFont.charRecs[0].height; + + defaultFont.charOffsets = (Vector2 *)malloc(defaultFont.numChars*sizeof(Vector2)); + defaultFont.charAdvanceX = (int *)malloc(defaultFont.numChars*sizeof(int)); + + for (int i = 0; i < defaultFont.numChars; i++) + { + // NOTE: On image based fonts (XNA style), character offsets and xAdvance are not required (set to 0) + defaultFont.charOffsets[i] = (Vector2){ 0.0f, 0.0f }; + defaultFont.charAdvanceX[i] = 0; + } } else { @@ -263,6 +287,12 @@ SpriteFont LoadSpriteFont(const char *fileName) UnloadImage(image); } + + if (spriteFont.texture.id == 0) + { + TraceLog(WARNING, "[%s] SpriteFont could not be loaded, using default font", fileName); + spriteFont = GetDefaultFont(); + } return spriteFont; } @@ -270,11 +300,17 @@ SpriteFont LoadSpriteFont(const char *fileName) // Unload SpriteFont from GPU memory void UnloadSpriteFont(SpriteFont spriteFont) { - UnloadTexture(spriteFont.texture); - free(spriteFont.charValues); - free(spriteFont.charRecs); - - TraceLog(INFO, "Unloaded sprite font data"); + // NOTE: Make sure spriteFont is not default font (fallback) + if (spriteFont.texture.id != defaultFont.texture.id) + { + UnloadTexture(spriteFont.texture); + free(spriteFont.charValues); + free(spriteFont.charRecs); + free(spriteFont.charOffsets); + free(spriteFont.charAdvanceX); + + TraceLog(INFO, "Unloaded sprite font data"); + } } // Draw text (using default font) @@ -296,14 +332,14 @@ void DrawText(const char *text, int posX, int posY, int fontSize, Color color) void DrawTextEx(SpriteFont spriteFont, const char *text, Vector2 position, int fontSize, int spacing, Color tint) { int length = strlen(text); - int offsetX = 0; - int offsetY = 0; // Line break! + int textOffsetX = 0; + int textOffsetY = 0; // Line break! float scaleFactor; unsigned char letter; Rectangle rec; - scaleFactor = (float)fontSize/spriteFont.charRecs[0].height; + scaleFactor = (float)fontSize/spriteFont.size; // NOTE: Some ugly hacks are made to support Latin-1 Extended characters directly // written in C code files (codified by default as UTF-8) @@ -332,8 +368,9 @@ void DrawTextEx(SpriteFont spriteFont, const char *text, Vector2 position, int f { if ((unsigned char)text[i] == '\n') { - offsetY += ((spriteFont.size + spriteFont.size/2)*scaleFactor); - offsetX = 0; + // NOTE: Fixed line spacing of 1.5 lines + textOffsetY += ((spriteFont.size + spriteFont.size/2)*scaleFactor); + textOffsetX = 0; rec.x = -1; } else rec = spriteFont.charRecs[(int)text[i] - FONT_FIRST_CHAR]; @@ -341,9 +378,12 @@ void DrawTextEx(SpriteFont spriteFont, const char *text, Vector2 position, int f if (rec.x > 0) { - DrawTexturePro(spriteFont.texture, rec, (Rectangle){ position.x + offsetX, position.y + offsetY, rec.width*scaleFactor, rec.height*scaleFactor} , (Vector2){ 0, 0 }, 0.0f, tint); + DrawTexturePro(spriteFont.texture, rec, (Rectangle){ position.x + textOffsetX + spriteFont.charOffsets[(int)text[i] - FONT_FIRST_CHAR].x*scaleFactor, + position.y + textOffsetY + spriteFont.charOffsets[(int)text[i] - FONT_FIRST_CHAR].y*scaleFactor, + rec.width*scaleFactor, rec.height*scaleFactor} , (Vector2){ 0, 0 }, 0.0f, tint); - offsetX += (rec.width*scaleFactor + spacing); + if (spriteFont.charAdvanceX[(int)text[i] - FONT_FIRST_CHAR] == 0) textOffsetX += (rec.width*scaleFactor + spacing); + else textOffsetX += (spriteFont.charAdvanceX[(int)text[i] - FONT_FIRST_CHAR]*scaleFactor + spacing); } } } @@ -419,14 +459,15 @@ Vector2 MeasureTextEx(SpriteFont spriteFont, const char *text, int fontSize, int if (text[i] != '\n') { - textWidth += spriteFont.charRecs[(int)text[i] - FONT_FIRST_CHAR].width; + if (spriteFont.charAdvanceX[(int)text[i] - FONT_FIRST_CHAR] != 0) textWidth += spriteFont.charAdvanceX[(int)text[i] - FONT_FIRST_CHAR]; + else textWidth += (spriteFont.charRecs[(int)text[i] - FONT_FIRST_CHAR].width + spriteFont.charOffsets[(int)text[i] - FONT_FIRST_CHAR].x); } else { if (tempTextWidth < textWidth) tempTextWidth = textWidth; lenCounter = 0; textWidth = 0; - textHeight += (spriteFont.size + spriteFont.size/2); + textHeight += (spriteFont.size + spriteFont.size/2); // NOTE: Fixed line spacing of 1.5 lines } if (tempLen < lenCounter) tempLen = lenCounter; @@ -695,114 +736,181 @@ static SpriteFont LoadRBMF(const char *fileName) return spriteFont; } -// Generate a sprite font from TTF data (font size required) -// NOTE: This function is a mess, it should be completely redone! -static SpriteFont LoadTTF(const char *fileName, int fontSize) +// Load a BMFont file (AngelCode font file) +static SpriteFont LoadBMFont(const char *fileName) { - // Steps: - - // 1) Generate sprite sheet image with characters from TTF - // 2) Load image as SpriteFont + #define MAX_BUFFER_SIZE 256 SpriteFont font = { 0 }; + font.texture.id = 0; + + char buffer[MAX_BUFFER_SIZE]; + char *searchPoint = NULL; + + int fontSize = 0; + int texWidth, texHeight; + char texFileName[128]; + int numChars = 0; - Image image; - image.width = 512; - image.height = 512; - //image.pixels = (Color *)malloc(image.width*image.height*sizeof(Color)); + int base; // Useless data + + FILE *fntFile; - unsigned char *ttfBuffer = (unsigned char *)malloc(1 << 25); + fntFile = fopen(fileName, "rt"); - // TODO: Load TTF and generate bitmap font and chars data -> REVIEW! + if (fntFile == NULL) + { + TraceLog(WARNING, "[%s] FNT file could not be opened", fileName); + return font; + } - stbtt_packedchar chardata[128]; // Num characters: 128 (?) -> REVIEW! + // NOTE: We skip first line, it contains no useful information + fgets(buffer, MAX_BUFFER_SIZE, fntFile); + //searchPoint = strstr(buffer, "size"); + //sscanf(searchPoint, "size=%i", &fontSize); + + fgets(buffer, MAX_BUFFER_SIZE, fntFile); + searchPoint = strstr(buffer, "lineHeight"); + sscanf(searchPoint, "lineHeight=%i base=%i scaleW=%i scaleH=%i", &fontSize, &base, &texWidth, &texHeight); + + TraceLog(DEBUG, "[%s] Font size: %i", fileName, fontSize); + TraceLog(DEBUG, "[%s] Font texture scale: %ix%i", fileName, texWidth, texHeight); + + fgets(buffer, MAX_BUFFER_SIZE, fntFile); + searchPoint = strstr(buffer, "file"); + sscanf(searchPoint, "file=\"%128[^\"]\"", texFileName); + + TraceLog(DEBUG, "[%s] Font texture filename: %s", fileName, texFileName); + + fgets(buffer, MAX_BUFFER_SIZE, fntFile); + searchPoint = strstr(buffer, "count"); + sscanf(searchPoint, "count=%i", &numChars); + + TraceLog(DEBUG, "[%s] Font num chars: %i", fileName, numChars); + + // Compose correct path using route of .fnt file (fileName) and texFileName + char *texPath = NULL; + char *lastSlash = NULL; + + lastSlash = strrchr(fileName, '/'); // you need escape character + texPath = malloc(strlen(fileName) - strlen(lastSlash) + 1 + strlen(texFileName) + 1); + memcpy(texPath, fileName, strlen(fileName) - strlen(lastSlash)); + strcat(texPath, "/"); + strcat(texPath, texFileName); + strcat(texPath, "\0"); + + TraceLog(DEBUG, "[%s] Font texture loading path: %s", fileName, texPath); + + font.texture = LoadTexture(texPath); + font.size = fontSize; + font.numChars = numChars; + font.charValues = (int *)malloc(numChars*sizeof(int)); + font.charRecs = (Rectangle *)malloc(numChars*sizeof(Rectangle)); + font.charOffsets = (Vector2 *)malloc(numChars*sizeof(Vector2)); + font.charAdvanceX = (int *)malloc(numChars*sizeof(int)); + + free(texPath); + + int charId, charX, charY, charWidth, charHeight, charOffsetX, charOffsetY, charAdvanceX; + + for (int i = 0; i < numChars; i++) + { + fgets(buffer, MAX_BUFFER_SIZE, fntFile); + sscanf(buffer, "char id=%i x=%i y=%i width=%i height=%i xoffset=%i yoffset=%i xadvance=%i", + &charId, &charX, &charY, &charWidth, &charHeight, &charOffsetX, &charOffsetY, &charAdvanceX); + + // Save data properly in sprite font + font.charValues[i] = charId; + font.charRecs[i] = (Rectangle){ charX, charY, charWidth, charHeight }; + font.charOffsets[i] = (Vector2){ (float)charOffsetX, (float)charOffsetY }; + font.charAdvanceX[i] = charAdvanceX; + } + + // TODO: Font data could be not ordered by charId: 32,33,34,35... review charValues and charRecs order + + fclose(fntFile); - unsigned char *tempBitmap = (unsigned char *)malloc(image.width*image.height*sizeof(unsigned char)); // One channel bitmap returned! + TraceLog(INFO, "[%s] SpriteFont loaded successfully", fileName); - // Reference struct -/* - typedef struct - { - unsigned short x0,y0,x1,y1; // coordinates of bbox in bitmap - float xoff,yoff,xadvance; - float xoff2,yoff2; - } stbtt_packedchar; -*/ + return font; + +} - stbtt_pack_context pc; +// Generate a sprite font from TTF file data (font size required) +// TODO: Review texture packing method and generation (use oversampling) +static SpriteFont LoadTTF(const char *fileName, int fontSize) +{ + // NOTE: Generated font uses some hardcoded values + #define FONT_TEXTURE_WIDTH 512 // Font texture width + #define FONT_TEXTURE_HEIGHT 512 // Font texture height + #define FONT_FIRST_CHAR 32 // Font first character (32 - space) + #define FONT_NUM_CHARS 95 // ASCII 32..126 is 95 glyphs + unsigned char *ttfBuffer = (unsigned char *)malloc(1 << 25); + unsigned char *dataBitmap = (unsigned char *)malloc(FONT_TEXTURE_WIDTH*FONT_TEXTURE_HEIGHT*sizeof(unsigned char)); // One channel bitmap returned! + stbtt_bakedchar charData[FONT_NUM_CHARS]; // ASCII 32..126 is 95 glyphs + + SpriteFont font = { 0 }; + FILE *ttfFile = fopen(fileName, "rb"); + + if (ttfFile == NULL) + { + TraceLog(WARNING, "[%s] FNT file could not be opened", fileName); + return font; + } fread(ttfBuffer, 1, 1<<25, ttfFile); - stbtt_PackBegin(&pc, tempBitmap, image.width, image.height, 0, 1, NULL); - - //stbtt_PackSetOversampling(&pc, 1, 1); - //stbtt_PackFontRange(&pc, ttfBuffer, 0, fontSize, 32, 95, chardata[0]+32); - stbtt_PackSetOversampling(&pc, 2, 2); // Better results - stbtt_PackFontRange(&pc, ttfBuffer, 0, fontSize, 32, 95, chardata + 32); - //stbtt_PackSetOversampling(&pc, 3, 1); - //stbtt_PackFontRange(&pc, ttfBuffer, 0, fontSize, 32, 95, chardata[2]+32); - - stbtt_PackEnd(&pc); + // NOTE: Using stb_truetype crappy packing method, no guarante the font fits the image... + stbtt_BakeFontBitmap(ttfBuffer,0, fontSize, dataBitmap, FONT_TEXTURE_WIDTH, FONT_TEXTURE_HEIGHT, FONT_FIRST_CHAR, FONT_NUM_CHARS, charData); free(ttfBuffer); - - // Now we have image data in tempBitmap and chardata filled... -/* - for (int i = 0; i < 512*512; i++) + + // Convert image data from grayscale to to UNCOMPRESSED_GRAY_ALPHA + unsigned char *dataGrayAlpha = (unsigned char *)malloc(FONT_TEXTURE_WIDTH*FONT_TEXTURE_HEIGHT*sizeof(unsigned char)*2); // Two channels + int k = 0; + + for (int i = 0; i < FONT_TEXTURE_WIDTH*FONT_TEXTURE_HEIGHT; i++) { - image.pixels[i].r = tempBitmap[i]; - image.pixels[i].g = tempBitmap[i]; - image.pixels[i].b = tempBitmap[i]; - image.pixels[i].a = 255; - } -*/ - free(tempBitmap); - - // REFERENCE EXAMPLE -/* - //To draw, provide *text, posX, posY - //stbtt_aligned_quad letter; - //stbtt_GetPackedQuad(chardata[0], BITMAP_W, BITMAP_H, *text++, &posX, &posY, &letter, font ? 0 : integer_align); + dataGrayAlpha[k] = 255; + dataGrayAlpha[k + 1] = dataBitmap[i]; - void print(float x, float y, int fontNum, char *text) - { - glEnable(GL_TEXTURE_2D); - glBindTexture(GL_TEXTURE_2D, font_tex); - glBegin(GL_QUADS); - while (*text) { - stbtt_aligned_quad q; - stbtt_GetPackedQuad(chardata[fontNum], BITMAP_W, BITMAP_H, *text++, &x, &y, &q, fontNum ? 0 : integer_align); - drawBoxTC(q.x0,q.y0,q.x1,q.y1, q.s0,q.t0,q.s1,q.t1); - } - glEnd(); + k += 2; } + + free(dataBitmap); + + // Sprite font generation from TTF extracted data + Image image; + image.width = FONT_TEXTURE_WIDTH; + image.height = FONT_TEXTURE_HEIGHT; + image.mipmaps = 1; + image.format = UNCOMPRESSED_GRAY_ALPHA; + image.data = dataGrayAlpha; - print(100,160, 0, "This is a test"); -*/ -/* - font.numChars = 95; - font.charValues (int *)malloc(font.numChars*sizeof(int)); - font.charRecs = (Character *)malloc(font.numChars*sizeof(Character)); font.texture = LoadTextureFromImage(image); - - //stbtt_aligned_quad letter; - //int x = 0, y = 0; - + UnloadImage(image); // Unloads dataGrayAlpha + + font.size = fontSize; + font.numChars = FONT_NUM_CHARS; + font.charValues = (int *)malloc(font.numChars*sizeof(int)); + font.charRecs = (Rectangle *)malloc(font.numChars*sizeof(Rectangle)); + font.charOffsets = (Vector2 *)malloc(font.numChars*sizeof(Vector2)); + font.charAdvanceX = (int *)malloc(font.numChars*sizeof(int)); + for (int i = 0; i < font.numChars; i++) { - font.charValues[i] = i + 32; - - //stbtt_GetPackedQuad(chardata[0], 512, 512, i, &x, &y, &letter, 0); + font.charValues[i] = i + FONT_FIRST_CHAR; - font.charRecs[i].x = chardata[i + 32].x0; - font.charRecs[i].y = chardata[i + 32].y0; - font.charRecs[i].width = chardata[i + 32].x1 - chardata[i + 32].x0; - font.charRecs[i].height = chardata[i + 32].y1 - chardata[i + 32].y0; + font.charRecs[i].x = (int)charData[i].x0; + font.charRecs[i].y = (int)charData[i].y0; + font.charRecs[i].width = (int)charData[i].x1 - (int)charData[i].x0; + font.charRecs[i].height = (int)charData[i].y1 - (int)charData[i].y0; + + font.charOffsets[i] = (Vector2){ charData[i].xoff, charData[i].yoff }; + font.charAdvanceX[i] = (int)charData[i].xadvance; } -*/ - UnloadImage(image); return font; } \ No newline at end of file -- cgit v1.2.3 From f98c4dc82b71041ca9ba8364c4d255c88c772501 Mon Sep 17 00:00:00 2001 From: raysan5 Date: Mon, 18 Jan 2016 12:04:54 +0100 Subject: Corrected bug on fonts loading --- src/text.c | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) (limited to 'src/text.c') diff --git a/src/text.c b/src/text.c index 1a75b9e4..3755932d 100644 --- a/src/text.c +++ b/src/text.c @@ -269,14 +269,14 @@ SpriteFont LoadSpriteFont(const char *fileName) spriteFont.texture = LoadTextureFromImage(image); // Convert loaded image to OpenGL texture spriteFont.size = spriteFont.charRecs[0].height; - defaultFont.charOffsets = (Vector2 *)malloc(defaultFont.numChars*sizeof(Vector2)); - defaultFont.charAdvanceX = (int *)malloc(defaultFont.numChars*sizeof(int)); + spriteFont.charOffsets = (Vector2 *)malloc(spriteFont.numChars*sizeof(Vector2)); + spriteFont.charAdvanceX = (int *)malloc(spriteFont.numChars*sizeof(int)); - for (int i = 0; i < defaultFont.numChars; i++) + for (int i = 0; i < spriteFont.numChars; i++) { // NOTE: On image based fonts (XNA style), character offsets and xAdvance are not required (set to 0) - defaultFont.charOffsets[i] = (Vector2){ 0.0f, 0.0f }; - defaultFont.charAdvanceX[i] = 0; + spriteFont.charOffsets[i] = (Vector2){ 0.0f, 0.0f }; + spriteFont.charAdvanceX[i] = 0; } } else @@ -308,7 +308,7 @@ void UnloadSpriteFont(SpriteFont spriteFont) free(spriteFont.charRecs); free(spriteFont.charOffsets); free(spriteFont.charAdvanceX); - + TraceLog(INFO, "Unloaded sprite font data"); } } @@ -627,7 +627,7 @@ static SpriteFont LoadRBMF(const char *fileName) char charsDataType; // Char data type provided } rbmfInfoHeader; - SpriteFont spriteFont; + SpriteFont spriteFont = { 0 }; rbmfInfoHeader rbmfHeader; unsigned int *rbmfFileData = NULL; @@ -695,6 +695,8 @@ static SpriteFont LoadRBMF(const char *fileName) // Get characters data using rbmfCharWidthData, rbmfHeader.charHeight, charsDivisor, rbmfHeader.numChars spriteFont.charValues = (int *)malloc(spriteFont.numChars*sizeof(int)); spriteFont.charRecs = (Rectangle *)malloc(spriteFont.numChars*sizeof(Rectangle)); + spriteFont.charOffsets = (Vector2 *)malloc(spriteFont.numChars*sizeof(Vector2)); + spriteFont.charAdvanceX = (int *)malloc(spriteFont.numChars*sizeof(int)); int currentLine = 0; int currentPosX = charsDivisor; @@ -708,6 +710,10 @@ static SpriteFont LoadRBMF(const char *fileName) spriteFont.charRecs[i].y = charsDivisor + currentLine * ((int)rbmfHeader.charHeight + charsDivisor); spriteFont.charRecs[i].width = (int)rbmfCharWidthData[i]; spriteFont.charRecs[i].height = (int)rbmfHeader.charHeight; + + // NOTE: On image based fonts (XNA style), character offsets and xAdvance are not required (set to 0) + spriteFont.charOffsets[i] = (Vector2){ 0.0f, 0.0f }; + spriteFont.charAdvanceX[i] = 0; testPosX += (spriteFont.charRecs[i].width + charsDivisor); -- cgit v1.2.3 From 823abf666e09e96ccbf5230c96f2d3a61ff60895 Mon Sep 17 00:00:00 2001 From: raysan5 Date: Fri, 12 Feb 2016 12:22:56 +0100 Subject: Reviewed code TODOs --- src/text.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/text.c') diff --git a/src/text.c b/src/text.c index 3755932d..e4c7bbf3 100644 --- a/src/text.c +++ b/src/text.c @@ -346,7 +346,7 @@ void DrawTextEx(SpriteFont spriteFont, const char *text, Vector2 position, int f for(int i = 0; i < length; i++) { - // TODO: Right now we are supposing characters follow a continous order and start at FONT_FIRST_CHAR, + // TODO: Right now we are supposing characters that follow a continous order and start at FONT_FIRST_CHAR, // this sytem can be improved to support any characters order and init value... // An intermediate table could be created to link char values with predefined char position index in chars rectangle array -- cgit v1.2.3