From 90a73b5504b434605208b021723787e46b7ad704 Mon Sep 17 00:00:00 2001 From: Nir Cohen Hershkovitz Date: Fri, 20 Dec 2024 15:51:18 +0200 Subject: [PATCH] Centralize Text Added two methods into rtext.c - CentralizeText (for default font), CentralizeTextEx (for specific font). Used the same logic as in "MeasureText" method: In "CentralizeText" method check if the default font has been loaded, if it doesn't - return the zero vector. if it does - modify the fontSize, calculate the spacing, and then call to "CentralizeTextEx" method that will calculate the desired textPosition. Added the methods declarations into raylib.h --- src/raylib.h | 2 ++ src/rtext.c | 30 ++++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/src/raylib.h b/src/raylib.h index 7e1a1f8380a9..5cdc72971cc0 100644 --- a/src/raylib.h +++ b/src/raylib.h @@ -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 diff --git a/src/rtext.c b/src/rtext.c index 005568dbfb83..9a4b495539bb 100644 --- a/src/rtext.c +++ b/src/rtext.c @@ -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) { @@ -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)