MeasureTextEx() - Added support for multi-line size measure

This commit is contained in:
raysan5 2015-12-09 20:57:50 +01:00
parent 2bd7245508
commit f144b6bae4

View file

@ -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 (fontSize <= spriteFont.charRecs[0].height) scaleFactor = 1.0f; if (tempTextWidth < textWidth) tempTextWidth = textWidth;
else scaleFactor = (float)fontSize / spriteFont.charRecs[0].height;
if (fontSize <= spriteFont.size) scaleFactor = 1.0f;
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;
} }