Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[rtext] Centralize Text #4623

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/raylib.h
Original file line number Diff line number Diff line change
Expand Up @@ -1485,7 +1485,9 @@ RLAPI void DrawTextCodepoints(Font font, const int *codepoints, int codepointCou
// Text font info functions
RLAPI void SetTextLineSpacing(int spacing); // Set vertical line spacing when drawing with line-breaks
RLAPI int MeasureText(const char *text, int fontSize); // Measure string width for default font
RLAPI Vector2 CentralizeText(const char *text, int fontSize, Vector2 anchorPosition, Vector2 areaSize); // Gives you the position (Vector2) for your text so it will be Centered in specific area for default font
RLAPI Vector2 MeasureTextEx(Font font, const char *text, float fontSize, float spacing); // Measure string size for Font
RLAPI Vector2 CentralizeTextEx(Font font, const char *text, float fontSize, float spacing, Vector2 anchorPosition, Vector2 areaSize); // Gives you the position (Vector2) for your text so it will be Centered in specific area for Font
RLAPI int GetGlyphIndex(Font font, int codepoint); // Get glyph index position in font for a codepoint (unicode character), fallback to '?' if not found
RLAPI GlyphInfo GetGlyphInfo(Font font, int codepoint); // Get glyph font info data for a codepoint (unicode character), fallback to '?' if not found
RLAPI Rectangle GetGlyphAtlasRec(Font font, int codepoint); // Get glyph rectangle in font atlas for a codepoint (unicode character), fallback to '?' if not found
Expand Down
30 changes: 30 additions & 0 deletions src/rtext.c
Original file line number Diff line number Diff line change
Expand Up @@ -1277,6 +1277,24 @@ int MeasureText(const char *text, int fontSize)
return (int)textSize.x;
}

// Gives you the position (Vector2) for your text so it will be Centered in specific area for default font
Vector2 CentralizeText(const char *text, int fontSize, Vector2 anchorPosition, Vector2 areaSize)
{
Vector2 textPosition = { 0.0f, 0.0f };

// Check if default font has been loaded
if (GetFontDefault().texture.id != 0)
{
int defaultFontSize = 10; // Default Font chars height in pixel
if (fontSize < defaultFontSize) fontSize = defaultFontSize;
int spacing = fontSize/defaultFontSize;

textPosition = CentralizeTextEx(GetFontDefault(), text, (float)fontSize, (float)spacing, anchorPosition, areaSize);
}

return textPosition;
}

// Measure string size for Font
Vector2 MeasureTextEx(Font font, const char *text, float fontSize, float spacing)
{
Expand Down Expand Up @@ -1334,6 +1352,18 @@ Vector2 MeasureTextEx(Font font, const char *text, float fontSize, float spacing
return textSize;
}

// Gives you the position (Vector2) for your text so it will be Centered in specific area for Font
Vector2 CentralizeTextEx(Font font, const char *text, float fontSize, float spacing, Vector2 anchorPosition, Vector2 areaSize)
{
Vector2 textPosition = { 0 };
Vector2 textSize = MeasureTextEx(font, text, fontSize, spacing);

textPosition.x = anchorPosition.x + ((areaSize.x - textSize.x) / 2.0f);
textPosition.y = anchorPosition.y + ((areaSize.y - textSize.y) / 2.0f);

return textPosition;
}

// Get index position for a unicode character on font
// NOTE: If codepoint is not found in the font it fallbacks to '?'
int GetGlyphIndex(Font font, int codepoint)
Expand Down