aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorraysan5 <raysan5@gmail.com>2015-12-09 20:57:50 +0100
committerraysan5 <raysan5@gmail.com>2015-12-09 20:57:50 +0100
commitf144b6bae43465a62a45f8e45ab2099f215664dc (patch)
tree848b077d10fd53366156fa9de953b9ef55077090 /src
parent2bd72455080409f1d9ecc6c5576f58c1ff093c3f (diff)
downloadraylib-f144b6bae43465a62a45f8e45ab2099f215664dc.tar.gz
raylib-f144b6bae43465a62a45f8e45ab2099f215664dc.zip
MeasureTextEx() - Added support for multi-line size measure
Diffstat (limited to 'src')
-rw-r--r--src/text.c32
1 files changed, 27 insertions, 5 deletions
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;
}