Support font chars padding on drawing #1432
Previous implementation did not consider any padding while drawing the characters on screen (despite being available on the font atlas), so, only minimum character area was drawn. If some text effect shader was required (shadow, glow, outline...), there was no space in the drawn quad to draw that pixels effect. This commit corrects that issue.
This commit is contained in:
parent
51e8f37688
commit
e404a18226
2 changed files with 22 additions and 26 deletions
|
@ -1264,8 +1264,8 @@ RLAPI void DrawText(const char *text, int posX, int posY, int fontSize, Color co
|
||||||
RLAPI void DrawTextEx(Font font, const char *text, Vector2 position, float fontSize, float spacing, Color tint); // Draw text using font and additional parameters
|
RLAPI void DrawTextEx(Font font, const char *text, Vector2 position, float fontSize, float spacing, Color tint); // Draw text using font and additional parameters
|
||||||
RLAPI void DrawTextRec(Font font, const char *text, Rectangle rec, float fontSize, float spacing, bool wordWrap, Color tint); // Draw text using font inside rectangle limits
|
RLAPI void DrawTextRec(Font font, const char *text, Rectangle rec, float fontSize, float spacing, bool wordWrap, Color tint); // Draw text using font inside rectangle limits
|
||||||
RLAPI void DrawTextRecEx(Font font, const char *text, Rectangle rec, float fontSize, float spacing, bool wordWrap, Color tint,
|
RLAPI void DrawTextRecEx(Font font, const char *text, Rectangle rec, float fontSize, float spacing, bool wordWrap, Color tint,
|
||||||
int selectStart, int selectLength, Color selectTint, Color selectBackTint); // Draw text using font inside rectangle limits with support for text selection
|
int selectStart, int selectLength, Color selectTint, Color selectBackTint); // Draw text using font inside rectangle limits with support for text selection
|
||||||
RLAPI void DrawTextCodepoint(Font font, int codepoint, Vector2 position, float scale, Color tint); // Draw one character (codepoint)
|
RLAPI void DrawTextCodepoint(Font font, int codepoint, Vector2 position, float fontSize, Color tint); // Draw one character (codepoint)
|
||||||
|
|
||||||
// Text misc. functions
|
// Text misc. functions
|
||||||
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
|
||||||
|
|
44
src/text.c
44
src/text.c
|
@ -498,9 +498,9 @@ Font LoadFontFromMemory(const char *fileType, const unsigned char *fileData, int
|
||||||
|
|
||||||
if (font.chars != NULL)
|
if (font.chars != NULL)
|
||||||
{
|
{
|
||||||
//font.charsPadding = FONT_TTF_DEFAULT_CHARS_PADDING;
|
font.charsPadding = FONT_TTF_DEFAULT_CHARS_PADDING;
|
||||||
|
|
||||||
Image atlas = GenImageFontAtlas(font.chars, &font.recs, font.charsCount, font.baseSize, FONT_TTF_DEFAULT_CHARS_PADDING, 0);
|
Image atlas = GenImageFontAtlas(font.chars, &font.recs, font.charsCount, font.baseSize, font.charsPadding, 0);
|
||||||
font.texture = LoadTextureFromImage(atlas);
|
font.texture = LoadTextureFromImage(atlas);
|
||||||
|
|
||||||
// Update chars[i].image to use alpha, required to be used on ImageDrawText()
|
// Update chars[i].image to use alpha, required to be used on ImageDrawText()
|
||||||
|
@ -839,17 +839,27 @@ void DrawText(const char *text, int posX, int posY, int fontSize, Color color)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Draw one character (codepoint)
|
// Draw one character (codepoint)
|
||||||
void DrawTextCodepoint(Font font, int codepoint, Vector2 position, float scale, Color tint)
|
void DrawTextCodepoint(Font font, int codepoint, Vector2 position, float fontSize, Color tint)
|
||||||
{
|
{
|
||||||
// Character index position in sprite font
|
// Character index position in sprite font
|
||||||
// NOTE: In case a codepoint is not available in the font, index returned points to '?'
|
// NOTE: In case a codepoint is not available in the font, index returned points to '?'
|
||||||
int index = GetGlyphIndex(font, codepoint);
|
int index = GetGlyphIndex(font, codepoint);
|
||||||
|
float scaleFactor = fontSize/font.baseSize; // Character quad scaling factor
|
||||||
|
|
||||||
// Character rectangle on screen
|
// Character destination rectangle on screen
|
||||||
// NOTE: Quad is scaled proportionally to base character width-height
|
// NOTE: We consider charsPadding on drawing
|
||||||
Rectangle rec = { position.x, position.y, font.recs[index].width*scale, font.recs[index].height*scale };
|
Rectangle dstRec = { position.x + font.chars[index].offsetX*scaleFactor - (float)font.charsPadding*scaleFactor,
|
||||||
|
position.y + font.chars[index].offsetY*scaleFactor - (float)font.charsPadding*scaleFactor,
|
||||||
|
(font.recs[index].width + 2.0f*font.charsPadding)*scaleFactor,
|
||||||
|
(font.recs[index].height + 2.0f*font.charsPadding)*scaleFactor };
|
||||||
|
|
||||||
DrawTexturePro(font.texture, font.recs[index], rec, (Vector2){ 0, 0 }, 0.0f, tint);
|
// Character source rectangle from font texture atlas
|
||||||
|
// NOTE: We consider chars padding when drawing, it could be required for outline/glow shader effects
|
||||||
|
Rectangle srcRec = { font.recs[index].x - (float)font.charsPadding, font.recs[index].y - (float)font.charsPadding,
|
||||||
|
font.recs[index].width + 2.0f*font.charsPadding, font.recs[index].height + 2.0f*font.charsPadding };
|
||||||
|
|
||||||
|
// Draw the character texture on the screen
|
||||||
|
DrawTexturePro(font.texture, srcRec, dstRec, (Vector2){ 0, 0 }, 0.0f, tint);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Draw text using Font
|
// Draw text using Font
|
||||||
|
@ -885,17 +895,7 @@ void DrawTextEx(Font font, const char *text, Vector2 position, float fontSize, f
|
||||||
{
|
{
|
||||||
if ((codepoint != ' ') && (codepoint != '\t'))
|
if ((codepoint != ' ') && (codepoint != '\t'))
|
||||||
{
|
{
|
||||||
Rectangle rec = { position.x + textOffsetX + font.chars[index].offsetX*scaleFactor,
|
DrawTextCodepoint(font, codepoint, (Vector2){ position.x + textOffsetX, position.y + textOffsetY }, fontSize, tint);
|
||||||
position.y + textOffsetY + font.chars[index].offsetY*scaleFactor,
|
|
||||||
font.recs[index].width*scaleFactor,
|
|
||||||
font.recs[index].height*scaleFactor };
|
|
||||||
|
|
||||||
// TODO: Consider chars padding
|
|
||||||
// NOTE: It could be required for outline/glow shader effects
|
|
||||||
//Rectangle charRec = { font.recs[index].x - (float)font.charsPadding, font.recs[index].y - (float)font.charsPadding,
|
|
||||||
// font.recs[index].width + 2.0f*font.charsPadding, font.recs[index].height + 2.0f*font.charsPadding };
|
|
||||||
|
|
||||||
DrawTexturePro(font.texture, font.recs[index], rec, (Vector2){ 0, 0 }, 0.0f, tint);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (font.chars[index].advanceX == 0) textOffsetX += ((float)font.recs[index].width*scaleFactor + spacing);
|
if (font.chars[index].advanceX == 0) textOffsetX += ((float)font.recs[index].width*scaleFactor + spacing);
|
||||||
|
@ -1018,14 +1018,10 @@ void DrawTextRecEx(Font font, const char *text, Rectangle rec, float fontSize, f
|
||||||
isGlyphSelected = true;
|
isGlyphSelected = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Draw current chracter glyph
|
// Draw current character glyph
|
||||||
if ((codepoint != ' ') && (codepoint != '\t'))
|
if ((codepoint != ' ') && (codepoint != '\t'))
|
||||||
{
|
{
|
||||||
DrawTexturePro(font.texture, font.recs[index],
|
DrawTextCodepoint(font, codepoint, (Vector2){ position.x + textOffsetX, position.y + textOffsetY }, fontSize, isGlyphSelected? selectTint : tint);
|
||||||
(Rectangle){ rec.x + textOffsetX + font.chars[index].offsetX*scaleFactor,
|
|
||||||
rec.y + textOffsetY + font.chars[index].offsetY*scaleFactor,
|
|
||||||
font.recs[index].width*scaleFactor, font.recs[index].height*scaleFactor },
|
|
||||||
(Vector2){ 0, 0 }, 0.0f, (!isGlyphSelected)? tint : selectTint);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue