MeasureTextBoxed and DrawTextBoxedEx to DrawTextBoxed.

This commit is contained in:
jussi
2025-06-11 22:44:39 +03:00
parent a38e76873b
commit 15deeccc4b
7 changed files with 97 additions and 82 deletions

23
API.md
View File

@@ -8041,20 +8041,11 @@ Draw multiple character (codepoint)
--- ---
> mouseCharId, textOffset = RL.DrawTextBoxed(Font font, string text, Rectangle rec, float fontSize, float spacing, bool wordWrap, Color tint, bool limitHeight, int|nil tabSize ) > mouseCharId, textOffset = RL.DrawTextBoxed( Font font, string text, Rectangle rec, float fontSize, float spacing, bool wordWrap, Color tint, bool limitHeight, Vector2|nil textOffset, int|nil tabSize )
Draw text using font inside rectangle limits. Draw text inside rectangle limits. Return character id from mouse position (default 0).
Support for tint change with "\a#FFFFFFFF"
- Success return int, Vector2
---
> 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 textOffset can be used to set start position inside rectangle. Usefull to pass from previous
DrawTextBoxedEx for continuous text. DrawTextBoxed for continuous text.
Support for tint change with "\a#FFFFFFFF" Support for tint change with "\a#FFFFFFFF"
- Success return int, Vector2 - Success return int, Vector2
@@ -8095,6 +8086,14 @@ Measure string size for Font
--- ---
> size, textOffset = RL.MeasureTextBoxed( Font font, string text, Rectangle rec, float fontSize, float spacing, bool wordWrap, Color tint, bool limitHeight, Vector2 textOffset, int|nil tabSize )
Measure text inside rectangle limits.
- Success return Vector2, Vector2
---
> index = RL.GetGlyphIndex( Font font, int codepoint ) > index = RL.GetGlyphIndex( Font font, int codepoint )
Get glyph index position in font for a codepoint (unicode character), fallback to '?' if not found Get glyph index position in font for a codepoint (unicode character), fallback to '?' if not found

View File

@@ -4528,25 +4528,9 @@ function RL.DrawTextCodepoint( font, codepoint, position, fontSize, tint ) end
---@return any RL.DrawTextCodepoints ---@return any RL.DrawTextCodepoints
function RL.DrawTextCodepoints( font, codepoints, position, fontSize, spacing, tint ) end function RL.DrawTextCodepoints( font, codepoints, position, fontSize, spacing, tint ) end
---Draw text using font inside rectangle limits. ---Draw text inside rectangle limits. Return character id from mouse position (default 0).
---Support for tint change with "\a#FFFFFFFF"
---- Success return int, Vector2
---@param font any
---@param text string
---@param rec table
---@param fontSize number
---@param spacing number
---@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, 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 ---textOffset can be used to set start position inside rectangle. Usefull to pass from previous
---DrawTextBoxedEx for continuous text. ---DrawTextBoxed for continuous text.
---Support for tint change with "\a#FFFFFFFF" ---Support for tint change with "\a#FFFFFFFF"
---- Success return int, Vector2 ---- Success return int, Vector2
---@param font any ---@param font any
@@ -4557,11 +4541,11 @@ function RL.DrawTextBoxed( font, text, rec, fontSize, spacing, wordWrap, tint, l
---@param wordWrap boolean ---@param wordWrap boolean
---@param tint table ---@param tint table
---@param limitHeight boolean ---@param limitHeight boolean
---@param textOffset table ---@param textOffset table|nil
---@param tabSize integer|nil ---@param tabSize integer|nil
---@return any mouseCharId ---@return any mouseCharId
---@return any textOffset ---@return any textOffset
function RL.DrawTextBoxedEx( font, text, rec, fontSize, spacing, wordWrap, tint, limitHeight, textOffset, tabSize ) end function RL.DrawTextBoxed( font, text, rec, fontSize, spacing, wordWrap, tint, limitHeight, textOffset, tabSize ) end
-- Text - Text font info functions -- Text - Text font info functions
@@ -4591,6 +4575,22 @@ function RL.MeasureText( text, fontSize ) end
---@return any size ---@return any size
function RL.MeasureTextEx( font, text, fontSize, spacing ) end function RL.MeasureTextEx( font, text, fontSize, spacing ) end
---Measure text inside rectangle limits.
---- Success return Vector2, Vector2
---@param font any
---@param text string
---@param rec table
---@param fontSize number
---@param spacing number
---@param wordWrap boolean
---@param tint table
---@param limitHeight boolean
---@param textOffset table
---@param tabSize integer|nil
---@return any size
---@return any textOffset
function RL.MeasureTextBoxed( font, text, rec, fontSize, spacing, wordWrap, tint, limitHeight, textOffset, tabSize ) end
---Get glyph index position in font for a codepoint (unicode character), fallback to '?' if not found ---Get glyph index position in font for a codepoint (unicode character), fallback to '?' if not found
---- Success return int ---- Success return int
---@param font any ---@param font any

View File

@@ -53,6 +53,8 @@ DETAILED CHANGES:
- ADDED: SoundAlias garbage collection. - ADDED: SoundAlias garbage collection.
- ADDED: Frustum math from raylib extras. - ADDED: Frustum math from raylib extras.
- FIXED: uluaGet* pops lua stack correctly when userdata given in table. - FIXED: uluaGet* pops lua stack correctly when userdata given in table.
- ADDED: MeasureTextBoxed.
- CHANGE: DrawTextBoxedEx to DrawTextBoxed.
------------------------------------------------------------------------ ------------------------------------------------------------------------
Release: ReiLua version 0.8.0 Using Raylib 5.0 and Forked Raygui 4.0 Release: ReiLua version 0.8.0 Using Raylib 5.0 and Forked Raygui 4.0

View File

@@ -32,26 +32,27 @@ end
function RL.draw() function RL.draw()
RL.ClearBackground( RL.RAYWHITE ) RL.ClearBackground( RL.RAYWHITE )
RL.DrawRectangleLines( rect, RL.GREEN ) RL.DrawRectangleLines( rect, RL.GREEN )
textOffset = { 0, 0 }
_, textOffset = RL.DrawTextBoxedEx( _, textOffset = RL.DrawTextBoxed(
RL.GetFontDefault(), RL.GetFontDefault(),
"\tYou can change the size of the box by pressing right mouse and toggle the wordwrap by pressing space. First we will write some text before the hyperlink to show that it is indeed is as powerful feature as adverticed.", "\tYou can change the size of the box by pressing right mouse and toggle the wordwrap by pressing space. First we will write some text before the hyperlink to show that it is indeed is as powerful feature as adverticed.",
rect, rect,
textSize, spacing, wordwrap, RL.RED, limitHeight, { 0, 0 } textSize, spacing, wordwrap, RL.RED, limitHeight, textOffset
) )
mouseCharId, textOffset = RL.DrawTextBoxedEx( mouseCharId, textOffset = RL.DrawTextBoxed(
RL.GetFontDefault(), RL.GetFontDefault(),
" Hyperlink.", " Hyperlink.",
rect, rect,
textSize, spacing, wordwrap, linkColor, limitHeight, textOffset textSize, spacing, wordwrap, linkColor, limitHeight, textOffset
) )
_, textOffset = RL.DrawTextBoxedEx( _, textOffset = RL.DrawTextBoxed(
RL.GetFontDefault(), RL.GetFontDefault(),
" Then we demonstrate this further by writin more text after the link. Isn't this just amazing! Don't forget to press left mouse to print text to your console when hovering mouse over the hyperlink.", " Then we demonstrate this further by writin more text after the link. Isn't this just amazing! Don't forget to press left mouse to print text to your console when hovering mouse over the hyperlink.",
rect, rect,
textSize, spacing, wordwrap, RL.RED, limitHeight, textOffset textSize, spacing, wordwrap, RL.RED, limitHeight, textOffset
) )
RL.DrawTextBoxedEx( RL.DrawTextBoxed(
RL.GetFontDefault(), RL.GetFontDefault(),
" \a#D28484FF Also supports \a#0000FFFF changing text \a#CCD834FF color in the \a#00FFFFFF middle of \a#B623A3FF the \a#00FF00FF text draw.", " \a#D28484FF Also supports \a#0000FFFF changing text \a#CCD834FF color in the \a#00FFFFFF middle of \a#B623A3FF the \a#00FF00FF text draw.",
rect, rect,

View File

@@ -22,12 +22,12 @@ int ltextDrawTextPro( lua_State* L );
int ltextDrawTextCodepoint( lua_State* L ); int ltextDrawTextCodepoint( lua_State* L );
int ltextDrawTextCodepoints( lua_State* L ); int ltextDrawTextCodepoints( lua_State* L );
int ltextDrawTextBoxed( lua_State* L ); int ltextDrawTextBoxed( lua_State* L );
int ltextDrawTextBoxedEx( lua_State* L );
/* Text font info functions. */ /* Text font info functions. */
int ltextSetTextLineSpacing( lua_State* L ); int ltextSetTextLineSpacing( lua_State* L );
int ltextGetTextLineSpacing( lua_State* L ); int ltextGetTextLineSpacing( lua_State* L );
int ltextMeasureText( lua_State* L ); int ltextMeasureText( lua_State* L );
int ltextMeasureTextEx( lua_State* L ); int ltextMeasureTextEx( lua_State* L );
int ltextMeasureTextBoxed( lua_State* L );
int ltextGetGlyphIndex( lua_State* L ); int ltextGetGlyphIndex( lua_State* L );
int ltextGetGlyphInfo( lua_State* L ); int ltextGetGlyphInfo( lua_State* L );
int ltextGetGlyphInfoByIndex( lua_State* L ); int ltextGetGlyphInfoByIndex( lua_State* L );

View File

@@ -2222,12 +2222,12 @@ void luaRegister() {
assingGlobalFunction( "DrawTextCodepoint", ltextDrawTextCodepoint ); assingGlobalFunction( "DrawTextCodepoint", ltextDrawTextCodepoint );
assingGlobalFunction( "DrawTextCodepoints", ltextDrawTextCodepoints ); assingGlobalFunction( "DrawTextCodepoints", ltextDrawTextCodepoints );
assingGlobalFunction( "DrawTextBoxed", ltextDrawTextBoxed ); assingGlobalFunction( "DrawTextBoxed", ltextDrawTextBoxed );
assingGlobalFunction( "DrawTextBoxedEx", ltextDrawTextBoxedEx );
/* Text font info functions. */ /* Text font info functions. */
assingGlobalFunction( "SetTextLineSpacing", ltextSetTextLineSpacing ); assingGlobalFunction( "SetTextLineSpacing", ltextSetTextLineSpacing );
assingGlobalFunction( "GetTextLineSpacing", ltextGetTextLineSpacing ); assingGlobalFunction( "GetTextLineSpacing", ltextGetTextLineSpacing );
assingGlobalFunction( "MeasureText", ltextMeasureText ); assingGlobalFunction( "MeasureText", ltextMeasureText );
assingGlobalFunction( "MeasureTextEx", ltextMeasureTextEx ); assingGlobalFunction( "MeasureTextEx", ltextMeasureTextEx );
assingGlobalFunction( "MeasureTextBoxed", ltextMeasureTextBoxed );
assingGlobalFunction( "GetGlyphIndex", ltextGetGlyphIndex ); assingGlobalFunction( "GetGlyphIndex", ltextGetGlyphIndex );
assingGlobalFunction( "GetGlyphInfo", ltextGetGlyphInfo ); assingGlobalFunction( "GetGlyphInfo", ltextGetGlyphInfo );
assingGlobalFunction( "GetGlyphInfoByIndex", ltextGetGlyphInfoByIndex ); assingGlobalFunction( "GetGlyphInfoByIndex", ltextGetGlyphInfoByIndex );

View File

@@ -56,15 +56,19 @@ static unsigned char getHexValue( const char* ptr, size_t offset ) {
return getDigit( ptr[ offset ] ) * 16 + getDigit( ptr[ offset + 1 ] ); return getDigit( ptr[ offset ] ) * 16 + getDigit( ptr[ offset + 1 ] );
} }
static int DrawTextBoxed( Font font, char* text, Rectangle rec, float fontSize, /* If draw is false, will only measure. */
float spacing, bool wordWrap, Color tint, bool limitHeight, Vector2* textOffset, int tabSize ) { static int DrawTextBoxed( Font* font, char* text, Rectangle rec, float fontSize,
float spacing, bool wordWrap, Color tint, bool limitHeight, Vector2* textOffset, int tabSize, bool draw ) {
int lineSpacing = state->lineSpacing; int lineSpacing = state->lineSpacing;
if ( rec.width <= 0 || ( rec.height <= limitHeight ? ( textOffset->y + lineSpacing ) : 0 ) ) { if ( rec.width <= 0 || ( rec.height <= limitHeight ? ( textOffset->y + lineSpacing ) : 0 ) ) {
if ( !draw ) {
textOffset->y -= lineSpacing;
}
return 0; return 0;
} }
int len = TextLength( text ); int len = TextLength( text );
float scaleFactor = fontSize / font.baseSize; float scaleFactor = fontSize / font->baseSize;
float wordWidth = 0.0; float wordWidth = 0.0;
bool measure = true; bool measure = true;
Vector2 mousePos = GetMousePosition(); Vector2 mousePos = GetMousePosition();
@@ -73,11 +77,11 @@ float spacing, bool wordWrap, Color tint, bool limitHeight, Vector2* textOffset,
for ( int i = 0; i < len; ) { for ( int i = 0; i < len; ) {
int codepointByteCount = 0; int codepointByteCount = 0;
int codepoint = GetCodepointNext( &text[i], &codepointByteCount ); int codepoint = GetCodepointNext( &text[i], &codepointByteCount );
int index = GetGlyphIndex( font, codepoint ); int index = GetGlyphIndex( *font, codepoint );
float codepointWidth = font.glyphs[ index ].advanceX == 0 float codepointWidth = font->glyphs[ index ].advanceX == 0
? (float)font.recs[ index ].width * scaleFactor + spacing ? (float)font->recs[ index ].width * scaleFactor + spacing
: (float)font.glyphs[ index ].advanceX * scaleFactor + spacing; : (float)font->glyphs[ index ].advanceX * scaleFactor + spacing;
if ( codepoint == '\n' ) { if ( codepoint == '\n' ) {
textOffset->x = 0; textOffset->x = 0;
@@ -85,7 +89,7 @@ float spacing, bool wordWrap, Color tint, bool limitHeight, Vector2* textOffset,
measure = true; measure = true;
} }
else if ( codepoint == '\t' ) { else if ( codepoint == '\t' ) {
textOffset->x += (float)font.recs[ GetGlyphIndex( font, ' ' ) ].width * tabSize * scaleFactor + spacing; textOffset->x += (float)font->recs[ GetGlyphIndex( *font, ' ' ) ].width * tabSize * scaleFactor + spacing;
measure = true; measure = true;
} }
else if ( codepoint == '\a' ) { else if ( codepoint == '\a' ) {
@@ -101,7 +105,7 @@ float spacing, bool wordWrap, Color tint, bool limitHeight, Vector2* textOffset,
} }
else if ( wordWrap && ( wordWidth < rec.width ) ) { else if ( wordWrap && ( wordWidth < rec.width ) ) {
if ( measure && codepoint != ' ' ) { if ( measure && codepoint != ' ' ) {
wordWidth = measureWord( font, &text[i], fontSize, spacing ); wordWidth = measureWord( *font, &text[i], fontSize, spacing );
measure = false; measure = false;
if ( rec.width < ( textOffset->x + wordWidth ) ) { if ( rec.width < ( textOffset->x + wordWidth ) ) {
@@ -121,13 +125,18 @@ float spacing, bool wordWrap, Color tint, bool limitHeight, Vector2* textOffset,
} }
if ( limitHeight && ( rec.height < ( textOffset->y + lineSpacing ) ) ) { if ( limitHeight && ( rec.height < ( textOffset->y + lineSpacing ) ) ) {
if ( !draw ) {
textOffset->y -= lineSpacing;
}
break; break;
} }
if ( codepoint != '\a' && codepoint != '\n' && codepoint != '\t' && !( textOffset->x == 0 && codepoint == ' ' ) && codepointWidth < rec.width ) { if ( codepoint != '\a' && codepoint != '\n' && codepoint != '\t' && !( textOffset->x == 0 && codepoint == ' ' ) && codepointWidth < rec.width ) {
DrawTextCodepoint( font, codepoint, (Vector2){ round( rec.x + textOffset->x ), round( rec.y + textOffset->y ) }, fontSize, tint ); if ( draw ) {
DrawTextCodepoint( *font, codepoint, (Vector2){ round( rec.x + textOffset->x ), round( rec.y + textOffset->y ) }, fontSize, tint );
}
if ( CheckCollisionPointRec( mousePos, (Rectangle){ rec.x + textOffset->x - 1, rec.y + textOffset->y, codepointWidth, (float)font.baseSize * scaleFactor } ) ) { if ( CheckCollisionPointRec( mousePos, (Rectangle){ rec.x + textOffset->x - 1, rec.y + textOffset->y, codepointWidth, (float)font->baseSize * scaleFactor } ) ) {
mouseChar = i + 1; mouseChar = i + 1;
} }
textOffset->x += codepointWidth; textOffset->x += codepointWidth;
@@ -575,9 +584,11 @@ 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, int|nil tabSize ) > mouseCharId, textOffset = RL.DrawTextBoxed( Font font, string text, Rectangle rec, float fontSize, float spacing, bool wordWrap, Color tint, bool limitHeight, Vector2|nil textOffset, int|nil tabSize )
Draw text using font inside rectangle limits. Draw text 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
DrawTextBoxed for continuous text.
Support for tint change with "\a#FFFFFFFF" Support for tint change with "\a#FFFFFFFF"
- Success return int, Vector2 - Success return int, Vector2
@@ -595,40 +606,12 @@ int ltextDrawTextBoxed( lua_State* L ) {
int tabSize = 4; int tabSize = 4;
if ( !lua_isnil( L, 9 ) && !lua_isnone( L, 9 ) ) { if ( !lua_isnil( L, 9 ) && !lua_isnone( L, 9 ) ) {
tabSize = luaL_checkinteger( L, 9 ); textOffset = uluaGetVector2( 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, 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
DrawTextBoxedEx for continuous text.
Support for tint change with "\a#FFFFFFFF"
- Success return int, Vector2
*/
int ltextDrawTextBoxedEx( lua_State* L ) {
Font* font = uluaGetFont( L, 1 );
char* text = (char*)luaL_checkstring( L, 2 );
Rectangle rec = uluaGetRectangle( L, 3 );
float fontSize = luaL_checknumber( L, 4 );
float spacing = luaL_checknumber( L, 5 );
bool wordWrap = uluaGetBoolean( L, 6 );
Color tint = uluaGetColor( L, 7 );
bool limitHeight = uluaGetBoolean( L, 8 );
Vector2 textOffset = uluaGetVector2( L, 9 );
int tabSize = 4;
if ( !lua_isnil( L, 10 ) && !lua_isnone( L, 10 ) ) { if ( !lua_isnil( L, 10 ) && !lua_isnone( L, 10 ) ) {
tabSize = luaL_checkinteger( L, 10 ); tabSize = luaL_checkinteger( L, 10 );
} }
lua_pushinteger( L, DrawTextBoxed( *font, text, rec, fontSize, spacing, wordWrap, tint, limitHeight, &textOffset, tabSize ) ); lua_pushinteger( L, DrawTextBoxed( font, text, rec, fontSize, spacing, wordWrap, tint, limitHeight, &textOffset, tabSize, true ) );
uluaPushVector2( L, textOffset ); uluaPushVector2( L, textOffset );
return 2; return 2;
@@ -699,6 +682,36 @@ int ltextMeasureTextEx( lua_State* L ) {
return 1; return 1;
} }
/*
> size, textOffset = RL.MeasureTextBoxed( Font font, string text, Rectangle rec, float fontSize, float spacing, bool wordWrap, Color tint, bool limitHeight, Vector2 textOffset, int|nil tabSize )
Measure text inside rectangle limits.
- Success return Vector2, Vector2
*/
int ltextMeasureTextBoxed( lua_State* L ) {
Font* font = uluaGetFont( L, 1 );
char* text = (char*)luaL_checkstring( L, 2 );
Rectangle rec = uluaGetRectangle( L, 3 );
float fontSize = luaL_checknumber( L, 4 );
float spacing = luaL_checknumber( L, 5 );
bool wordWrap = uluaGetBoolean( L, 6 );
Color tint = uluaGetColor( L, 7 );
bool limitHeight = uluaGetBoolean( L, 8 );
Vector2 textOffset = uluaGetVector2( L, 9 );
int tabSize = 4;
if ( !lua_isnil( L, 10 ) && !lua_isnone( L, 10 ) ) {
tabSize = luaL_checkinteger( L, 10 );
}
DrawTextBoxed( font, text, rec, fontSize, spacing, wordWrap, tint, limitHeight, &textOffset, tabSize, false );
Vector2 size = { rec.width, textOffset.y + state->lineSpacing };
uluaPushVector2( L, size );
uluaPushVector2( L, textOffset );
return 2;
}
/* /*
> index = RL.GetGlyphIndex( Font font, int codepoint ) > index = RL.GetGlyphIndex( Font font, int codepoint )