From 067e4091ba9b34d1cc568ada266efde9cbe5c413 Mon Sep 17 00:00:00 2001 From: jussi Date: Sat, 3 May 2025 18:27:39 +0300 Subject: DrawTextBoxed fix. --- API.md | 4 ++-- ReiLua_API.lua | 6 ++++-- changelog | 2 ++ devnotes | 1 - src/text.c | 28 +++++++++++++++++++--------- 5 files changed, 27 insertions(+), 14 deletions(-) diff --git a/API.md b/API.md index 3316eab..5dc55cc 100644 --- a/API.md +++ b/API.md @@ -8023,7 +8023,7 @@ Draw multiple character (codepoint) --- -> mouseCharId, textOffset = RL.DrawTextBoxed(Font font, string text, Rectangle rec, float fontSize, float spacing, bool wordWrap, Color tint, bool limitHeight ) +> mouseCharId, textOffset = RL.DrawTextBoxed(Font font, string text, Rectangle rec, float fontSize, float spacing, bool wordWrap, Color tint, bool limitHeight, int|nil tabSize ) Draw text using font inside rectangle limits. @@ -8031,7 +8031,7 @@ Draw text using font inside rectangle limits. --- -> mouseCharId, textOffset = RL.DrawTextBoxedEx( Font font, string text, Rectangle rec, float fontSize, float spacing, bool wordWrap, Color tint, bool limitHeight, Vector2 textOffset ) +> mouseCharId, textOffset = RL.DrawTextBoxedEx( Font font, string text, Rectangle rec, float fontSize, float spacing, bool wordWrap, Color tint, bool limitHeight, Vector2 textOffset, int|nil tabSize ) Draw text using font inside rectangle limits. Return character id from mouse position (default 0). textOffset can be used to set start position inside rectangle. Usefull to pass from previous diff --git a/ReiLua_API.lua b/ReiLua_API.lua index e9c37a6..8086204 100644 --- a/ReiLua_API.lua +++ b/ReiLua_API.lua @@ -4538,9 +4538,10 @@ function RL.DrawTextCodepoints( font, codepoints, position, fontSize, spacing, ---@param wordWrap boolean ---@param tint table ---@param limitHeight boolean +---@param tabSize integer|nil ---@return any mouseCharId ---@return any textOffset -function RL.DrawTextBoxed( font, text, rec, fontSize, spacing, wordWrap, tint, limitHeight ) end +function RL.DrawTextBoxed( font, text, rec, fontSize, spacing, wordWrap, tint, limitHeight, tabSize ) end ---Draw text using font inside rectangle limits. Return character id from mouse position (default 0). ---textOffset can be used to set start position inside rectangle. Usefull to pass from previous @@ -4555,9 +4556,10 @@ function RL.DrawTextBoxed( font, text, rec, fontSize, spacing, wordWrap, tint, l ---@param tint table ---@param limitHeight boolean ---@param textOffset table +---@param tabSize integer|nil ---@return any mouseCharId ---@return any textOffset -function RL.DrawTextBoxedEx( font, text, rec, fontSize, spacing, wordWrap, tint, limitHeight, textOffset ) end +function RL.DrawTextBoxedEx( font, text, rec, fontSize, spacing, wordWrap, tint, limitHeight, textOffset, tabSize ) end -- Text - Text font info functions diff --git a/changelog b/changelog index f91c6a5..ad01bdc 100644 --- a/changelog +++ b/changelog @@ -46,6 +46,8 @@ DETAILED CHANGES: - ADDED: Fast tilemap example. - ADDED: RectPack using stbrp_pack_rects. Atlas packer tutorial. - ADDED: SetImageData. + - FIXED: DrawTextBoxed Could draw text outside rect when using special characters. + Also accepts optional tabSize argument. ------------------------------------------------------------------------ Release: ReiLua version 0.8.0 Using Raylib 5.0 and Forked Raygui 4.0 diff --git a/devnotes b/devnotes index 9dddc9e..ffc82c5 100644 --- a/devnotes +++ b/devnotes @@ -1,5 +1,4 @@ Current { - * Check if DrawTextBoxedEx works correctly. } Backlog { diff --git a/src/text.c b/src/text.c index ad2cbc4..4456d5e 100644 --- a/src/text.c +++ b/src/text.c @@ -25,14 +25,12 @@ static float measureWord( Font font, char* text, float fontSize, float spacing ) } static int DrawTextBoxed( Font font, char* text, Rectangle rec, float fontSize, -float spacing, bool wordWrap, Color tint, bool limitHeight, Vector2* textOffset ) { - const int tabSize = 4; /* How many spaces. */ +float spacing, bool wordWrap, Color tint, bool limitHeight, Vector2* textOffset, int tabSize ) { int lineSpacing = state->lineSpacing; if ( rec.width <= 0 || ( rec.height <= limitHeight ? ( textOffset->y + lineSpacing ) : 0 ) ) { return 0; } - int len = TextLength( text ); float scaleFactor = fontSize / font.baseSize; float wordWidth = 0.0; @@ -52,6 +50,10 @@ float spacing, bool wordWrap, Color tint, bool limitHeight, Vector2* textOffset if ( codepoint == '\n' ) { textOffset->x = 0; textOffset->y += lineSpacing; + measure = true; + } + else if ( codepoint == '\t' ) { + measure = true; } else if ( wordWrap && ( wordWidth < rec.width ) ) { if ( measure && codepoint != ' ' ) { @@ -63,7 +65,7 @@ float spacing, bool wordWrap, Color tint, bool limitHeight, Vector2* textOffset textOffset->y += lineSpacing; } } - else if ( codepoint == ' ' || codepoint == '\n' || codepoint == '\t' ) { + else if ( codepoint == ' ' ) { measure = true; } } @@ -533,7 +535,7 @@ int ltextDrawTextCodepoints( lua_State* L ) { } /* -> mouseCharId, textOffset = RL.DrawTextBoxed(Font font, string text, Rectangle rec, float fontSize, float spacing, bool wordWrap, Color tint, bool limitHeight ) +> mouseCharId, textOffset = RL.DrawTextBoxed(Font font, string text, Rectangle rec, float fontSize, float spacing, bool wordWrap, Color tint, bool limitHeight, int|nil tabSize ) Draw text using font inside rectangle limits. @@ -549,15 +551,19 @@ int ltextDrawTextBoxed( lua_State* L ) { Color tint = uluaGetColor( L, 7 ); bool limitHeight = uluaGetBoolean( L, 8 ); Vector2 textOffset = { 0, 0 }; - - lua_pushinteger( L, DrawTextBoxed( *font, text, rec, fontSize, spacing, wordWrap, tint, limitHeight, &textOffset ) ); + int tabSize = 4; + + if ( !lua_isnil( L, 9 ) && !lua_isnone( L, 9 ) ) { + tabSize = luaL_checkinteger( L, 9 ); + } + lua_pushinteger( L, DrawTextBoxed( *font, text, rec, fontSize, spacing, wordWrap, tint, limitHeight, &textOffset, tabSize ) ); uluaPushVector2( L, textOffset ); return 2; } /* -> mouseCharId, textOffset = RL.DrawTextBoxedEx( Font font, string text, Rectangle rec, float fontSize, float spacing, bool wordWrap, Color tint, bool limitHeight, Vector2 textOffset ) +> mouseCharId, textOffset = RL.DrawTextBoxedEx( Font font, string text, Rectangle rec, float fontSize, float spacing, bool wordWrap, Color tint, bool limitHeight, Vector2 textOffset, int|nil tabSize ) Draw text using font inside rectangle limits. Return character id from mouse position (default 0). textOffset can be used to set start position inside rectangle. Usefull to pass from previous @@ -575,8 +581,12 @@ int ltextDrawTextBoxedEx( lua_State* L ) { Color tint = uluaGetColor( L, 7 ); bool limitHeight = uluaGetBoolean( L, 8 ); Vector2 textOffset = uluaGetVector2( L, 9 ); + int tabSize = 4; - lua_pushinteger( L, DrawTextBoxed( *font, text, rec, fontSize, spacing, wordWrap, tint, limitHeight, &textOffset ) ); + if ( !lua_isnil( L, 10 ) && !lua_isnone( L, 10 ) ) { + tabSize = luaL_checkinteger( L, 10 ); + } + lua_pushinteger( L, DrawTextBoxed( *font, text, rec, fontSize, spacing, wordWrap, tint, limitHeight, &textOffset, tabSize ) ); uluaPushVector2( L, textOffset ); return 2; -- cgit v1.2.3