ADDED: DrawTextRec() and example
This commit is contained in:
parent
95d3f24c68
commit
0619571149
4 changed files with 247 additions and 1 deletions
125
src/text.c
125
src/text.c
|
@ -779,6 +779,131 @@ void DrawTextEx(Font font, const char *text, Vector2 position, float fontSize, f
|
|||
}
|
||||
}
|
||||
|
||||
// Draw text using font inside rectangle limits
|
||||
void DrawTextRec(Font font, const char *text, Rectangle rec, float fontSize, float spacing, bool wordWrap, Color tint)
|
||||
{
|
||||
int length = strlen(text);
|
||||
int textOffsetX = 0; // Offset between characters
|
||||
int textOffsetY = 0; // Required for line break!
|
||||
float scaleFactor = 0.0f;
|
||||
|
||||
unsigned char letter = 0; // Current character
|
||||
int index = 0; // Index position in sprite font
|
||||
|
||||
scaleFactor = fontSize/font.baseSize;
|
||||
|
||||
enum { MEASURE_WORD = 0, DRAW_WORD = 1 };
|
||||
int state = wordWrap ? MEASURE_WORD : DRAW_WORD;
|
||||
int lastTextOffsetX = 0;
|
||||
int wordStart = 0;
|
||||
|
||||
bool firstWord = true;
|
||||
|
||||
for (int i = 0; i < length; i++)
|
||||
{
|
||||
int glyphWidth = 0;
|
||||
letter = (unsigned char)text[i];
|
||||
|
||||
if (letter != '\n')
|
||||
{
|
||||
if ((unsigned char)text[i] == 0xc2) // UTF-8 encoding identification HACK!
|
||||
{
|
||||
// Support UTF-8 encoded values from [0xc2 0x80] -> [0xc2 0xbf](¿)
|
||||
letter = (unsigned char)text[i + 1];
|
||||
index = GetGlyphIndex(font, (int)letter);
|
||||
i++;
|
||||
}
|
||||
else if ((unsigned char)text[i] == 0xc3) // UTF-8 encoding identification HACK!
|
||||
{
|
||||
// Support UTF-8 encoded values from [0xc3 0x80](À) -> [0xc3 0xbf](ÿ)
|
||||
letter = (unsigned char)text[i + 1];
|
||||
index = GetGlyphIndex(font, (int)letter + 64);
|
||||
i++;
|
||||
}
|
||||
else index = GetGlyphIndex(font, (unsigned char)text[i]);
|
||||
|
||||
glyphWidth = (font.chars[index].advanceX == 0)?
|
||||
(int)(font.chars[index].rec.width*scaleFactor + spacing):
|
||||
(int)(font.chars[index].advanceX*scaleFactor + spacing);
|
||||
}
|
||||
|
||||
// NOTE: When word wrap is active first we measure a `word`(measure until a ' ','\n','\t' is found)
|
||||
// then set all the variables back to what they were before the measurement, change the state to
|
||||
// draw that word then change the state again and repeat until the end of the string...when the word
|
||||
// doesn't fit inside the rect we simple increase `textOffsetY` to draw it on the next line
|
||||
if (state == MEASURE_WORD)
|
||||
{
|
||||
// Measuring state
|
||||
if ((letter == ' ') || (letter == '\n') || (letter == '\t') || ((i + 1) == length))
|
||||
{
|
||||
int t = textOffsetX + glyphWidth;
|
||||
|
||||
if (textOffsetX+1>=rec.width)
|
||||
{
|
||||
textOffsetY += (int)((font.baseSize + font.baseSize/2)*scaleFactor);
|
||||
lastTextOffsetX = t - lastTextOffsetX;
|
||||
textOffsetX = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
textOffsetX = lastTextOffsetX;
|
||||
lastTextOffsetX = t;
|
||||
}
|
||||
|
||||
glyphWidth = 0;
|
||||
state = !state; // Change state
|
||||
t = i;
|
||||
i = firstWord?-1:wordStart;
|
||||
wordStart = t;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// Drawing state
|
||||
int t = textOffsetX + glyphWidth;
|
||||
|
||||
if (letter == '\n')
|
||||
{
|
||||
textOffsetY += (int)((font.baseSize + font.baseSize/2)*scaleFactor);
|
||||
lastTextOffsetX = t - lastTextOffsetX;
|
||||
if (lastTextOffsetX < 0) lastTextOffsetX = 0;
|
||||
textOffsetX = 0;
|
||||
}
|
||||
else if ((letter != ' ') && (letter != '\t'))
|
||||
{
|
||||
if ((t + 1) >= rec.width)
|
||||
{
|
||||
textOffsetY += (int)((font.baseSize + font.baseSize/2)*scaleFactor);
|
||||
textOffsetX = 0;
|
||||
}
|
||||
|
||||
if ((textOffsetY + (int)((font.baseSize + font.baseSize/2)*scaleFactor)) > rec.height) break;
|
||||
|
||||
DrawTexturePro(font.texture, font.chars[index].rec,
|
||||
(Rectangle){ rec.x + textOffsetX + font.chars[index].offsetX*scaleFactor,
|
||||
rec.y + textOffsetY + font.chars[index].offsetY*scaleFactor,
|
||||
font.chars[index].rec.width*scaleFactor,
|
||||
font.chars[index].rec.height*scaleFactor }, (Vector2){ 0, 0 }, 0.0f, tint);
|
||||
}
|
||||
|
||||
if (wordWrap)
|
||||
{
|
||||
if ((letter == ' ') || (letter == '\n') || (letter == '\t'))
|
||||
{
|
||||
// After drawing a word change the state
|
||||
firstWord = false;
|
||||
i = wordStart;
|
||||
textOffsetX = lastTextOffsetX;
|
||||
glyphWidth = 0;
|
||||
state = !state;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
textOffsetX += glyphWidth;
|
||||
}
|
||||
}
|
||||
|
||||
// Measure string width for default font
|
||||
int MeasureText(const char *text, int fontSize)
|
||||
{
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue