diff --git a/src/raylib.h b/src/raylib.h index cceaa2101066..539a31355263 100644 --- a/src/raylib.h +++ b/src/raylib.h @@ -1381,6 +1381,7 @@ RLAPI void DrawTextCodepoint(Font font, int codepoint, Vector2 position, float f RLAPI void DrawTextCodepoints(Font font, const int *codepoints, int count, Vector2 position, float fontSize, float spacing, Color tint); // Draw multiple character (codepoint) // Text font info functions +RLAPI void SetTextLineSpacing(int spacing); // Set vertical line spacing when drawing with line-breaks RLAPI int MeasureText(const char *text, int fontSize); // Measure string width for default font RLAPI Vector2 MeasureTextEx(Font font, const char *text, float fontSize, float spacing); // Measure string size for Font RLAPI int GetGlyphIndex(Font font, int codepoint); // Get glyph index position in font for a codepoint (unicode character), fallback to '?' if not found diff --git a/src/rtext.c b/src/rtext.c index 61f2e04cb7a7..cb609cf6211a 100644 --- a/src/rtext.c +++ b/src/rtext.c @@ -111,7 +111,8 @@ static Font defaultFont = { 0 }; // Module specific Functions Declaration //---------------------------------------------------------------------------------- #if defined(SUPPORT_FILEFORMAT_FNT) -static Font LoadBMFont(const char *fileName); // Load a BMFont file (AngelCode font file) +static Font LoadBMFont(const char *fileName); // Load a BMFont file (AngelCode font file) +static int textLineSpacing = 15; // Text vertical line spacing in pixels #endif #if defined(SUPPORT_DEFAULT_FONT) @@ -123,7 +124,6 @@ extern void UnloadFontDefault(void); // Module Functions Definition //---------------------------------------------------------------------------------- #if defined(SUPPORT_DEFAULT_FONT) - // Load raylib default font extern void LoadFontDefault(void) { @@ -702,6 +702,8 @@ Image GenImageFontAtlas(const GlyphInfo *chars, Rectangle **charRecs, int glyphC totalWidth += chars[i].image.width + 2*padding; } +//#define SUPPORT_FONT_ATLAS_SIZE_CONSERVATIVE +#if defined(SUPPORT_FONT_ATLAS_SIZE_CONSERVATIVE) int rowCount = 0; int imageSize = 64; // Define minimum starting value to avoid unnecessary calculation steps for very small images @@ -711,6 +713,12 @@ Image GenImageFontAtlas(const GlyphInfo *chars, Rectangle **charRecs, int glyphC imageSize *= 2; // Double the size of image (to keep POT) rowCount = imageSize/(fontSize + 2*padding); // Calculate new row count for the new image size } +#else + // No need for a so-conservative atlas generation + float totalArea = totalWidth*fontSize*1.3f; + float imageMinSize = sqrtf(totalArea); + int imageSize = (int)powf(2, ceilf(logf(imageMinSize)/logf(2))); +#endif atlas.width = imageSize; // Atlas bitmap width atlas.height = imageSize; // Atlas bitmap height @@ -1071,9 +1079,8 @@ void DrawTextEx(Font font, const char *text, Vector2 position, float fontSize, f if (codepoint == '\n') { - // NOTE: Fixed line spacing of 1.5 line-height - // TODO: Support custom line spacing defined by user - textOffsetY += (int)((font.baseSize + font.baseSize/2.0f)*scaleFactor); + // NOTE: Line spacing is a global variable, use SetTextLineSpacing() to setup + textOffsetY += textLineSpacing; textOffsetX = 0.0f; } else @@ -1143,9 +1150,8 @@ void DrawTextCodepoints(Font font, const int *codepoints, int count, Vector2 pos if (codepoints[i] == '\n') { - // NOTE: Fixed line spacing of 1.5 line-height - // TODO: Support custom line spacing defined by user - textOffsetY += (int)((font.baseSize + font.baseSize/2.0f)*scaleFactor); + // NOTE: Line spacing is a global variable, use SetTextLineSpacing() to setup + textOffsetY += textLineSpacing; textOffsetX = 0.0f; } else @@ -1161,6 +1167,12 @@ void DrawTextCodepoints(Font font, const int *codepoints, int count, Vector2 pos } } +// Set vertical line spacing when drawing with line-breaks +void SetTextLineSpacing(int spacing) +{ + textLineSpacing = spacing; +} + // Measure string width for default font int MeasureText(const char *text, int fontSize) { @@ -1219,7 +1231,9 @@ Vector2 MeasureTextEx(Font font, const char *text, float fontSize, float spacing if (tempTextWidth < textWidth) tempTextWidth = textWidth; byteCounter = 0; textWidth = 0; - textHeight += ((float)font.baseSize*1.5f); // NOTE: Fixed line spacing of 1.5 lines + + // NOTE: Line spacing is a global variable, use SetTextLineSpacing() to setup + textHeight += (float)textLineSpacing; } if (tempByteCounter < byteCounter) tempByteCounter = byteCounter;