MeasureTextEx() - Added support for multi-line size measure
This commit is contained in:
parent
2bd7245508
commit
f144b6bae4
1 changed files with 27 additions and 5 deletions
32
src/text.c
32
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)
|
Vector2 MeasureTextEx(SpriteFont spriteFont, const char *text, int fontSize, int spacing)
|
||||||
{
|
{
|
||||||
int len = strlen(text);
|
int len = strlen(text);
|
||||||
|
int tempLen = 0; // Used to count longer text line num chars
|
||||||
|
int lenCounter = 0;
|
||||||
|
|
||||||
int textWidth = 0;
|
int textWidth = 0;
|
||||||
|
int tempTextWidth = 0; // Used to count longer text line width
|
||||||
|
|
||||||
|
int textHeight = spriteFont.size;
|
||||||
float scaleFactor;
|
float scaleFactor;
|
||||||
|
|
||||||
for (int i = 0; i < len; i++)
|
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;
|
if (fontSize <= spriteFont.size) scaleFactor = 1.0f;
|
||||||
else scaleFactor = (float)fontSize / spriteFont.charRecs[0].height;
|
else scaleFactor = (float)fontSize/spriteFont.size;
|
||||||
|
|
||||||
Vector2 vec;
|
Vector2 vec;
|
||||||
vec.x = (float)textWidth * scaleFactor + (len - 1) * spacing; // Adds chars spacing to measure
|
vec.x = (float)tempTextWidth*scaleFactor + (tempLen - 1)*spacing; // Adds chars spacing to measure
|
||||||
vec.y = (float)spriteFont.charRecs[0].height * scaleFactor;
|
vec.y = (float)textHeight*scaleFactor;
|
||||||
|
|
||||||
return vec;
|
return vec;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue