WARNING: BREAKING: ADDED: SetTextLineSpacing()

This commit is contained in:
Ray 2023-07-02 10:52:20 +02:00
parent 48e2663d03
commit 3e4e4b32fd
2 changed files with 24 additions and 9 deletions

View File

@ -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) 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 // 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 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 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 RLAPI int GetGlyphIndex(Font font, int codepoint); // Get glyph index position in font for a codepoint (unicode character), fallback to '?' if not found

View File

@ -112,6 +112,7 @@ static Font defaultFont = { 0 };
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
#if defined(SUPPORT_FILEFORMAT_FNT) #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 #endif
#if defined(SUPPORT_DEFAULT_FONT) #if defined(SUPPORT_DEFAULT_FONT)
@ -123,7 +124,6 @@ extern void UnloadFontDefault(void);
// Module Functions Definition // Module Functions Definition
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
#if defined(SUPPORT_DEFAULT_FONT) #if defined(SUPPORT_DEFAULT_FONT)
// Load raylib default font // Load raylib default font
extern void LoadFontDefault(void) extern void LoadFontDefault(void)
{ {
@ -702,6 +702,8 @@ Image GenImageFontAtlas(const GlyphInfo *chars, Rectangle **charRecs, int glyphC
totalWidth += chars[i].image.width + 2*padding; totalWidth += chars[i].image.width + 2*padding;
} }
//#define SUPPORT_FONT_ATLAS_SIZE_CONSERVATIVE
#if defined(SUPPORT_FONT_ATLAS_SIZE_CONSERVATIVE)
int rowCount = 0; int rowCount = 0;
int imageSize = 64; // Define minimum starting value to avoid unnecessary calculation steps for very small images 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) imageSize *= 2; // Double the size of image (to keep POT)
rowCount = imageSize/(fontSize + 2*padding); // Calculate new row count for the new image size 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.width = imageSize; // Atlas bitmap width
atlas.height = imageSize; // Atlas bitmap height 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') if (codepoint == '\n')
{ {
// NOTE: Fixed line spacing of 1.5 line-height // NOTE: Line spacing is a global variable, use SetTextLineSpacing() to setup
// TODO: Support custom line spacing defined by user textOffsetY += textLineSpacing;
textOffsetY += (int)((font.baseSize + font.baseSize/2.0f)*scaleFactor);
textOffsetX = 0.0f; textOffsetX = 0.0f;
} }
else else
@ -1143,9 +1150,8 @@ void DrawTextCodepoints(Font font, const int *codepoints, int count, Vector2 pos
if (codepoints[i] == '\n') if (codepoints[i] == '\n')
{ {
// NOTE: Fixed line spacing of 1.5 line-height // NOTE: Line spacing is a global variable, use SetTextLineSpacing() to setup
// TODO: Support custom line spacing defined by user textOffsetY += textLineSpacing;
textOffsetY += (int)((font.baseSize + font.baseSize/2.0f)*scaleFactor);
textOffsetX = 0.0f; textOffsetX = 0.0f;
} }
else 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 // Measure string width for default font
int MeasureText(const char *text, int fontSize) 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; if (tempTextWidth < textWidth) tempTextWidth = textWidth;
byteCounter = 0; byteCounter = 0;
textWidth = 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; if (tempByteCounter < byteCounter) tempByteCounter = byteCounter;