summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorjussi2025-06-11 22:44:39 +0300
committerjussi2025-06-11 22:44:39 +0300
commit15deeccc4bcbe5b68f002f8cc91ee4ed8ced68fb (patch)
tree3e4fc6739a32d947909e54eb4301cd26bc4828d7
parenta38e76873b5ca3080c8046ae4759866d8cccc4e5 (diff)
downloadreilua-enhanced-15deeccc4bcbe5b68f002f8cc91ee4ed8ced68fb.tar.gz
reilua-enhanced-15deeccc4bcbe5b68f002f8cc91ee4ed8ced68fb.tar.bz2
reilua-enhanced-15deeccc4bcbe5b68f002f8cc91ee4ed8ced68fb.zip
MeasureTextBoxed and DrawTextBoxedEx to DrawTextBoxed.
-rw-r--r--API.md23
-rw-r--r--ReiLua_API.lua40
-rw-r--r--changelog2
-rw-r--r--examples/textBoxed/main.lua11
-rw-r--r--include/text.h2
-rw-r--r--src/lua_core.c2
-rw-r--r--src/text.c99
7 files changed, 97 insertions, 82 deletions
diff --git a/API.md b/API.md
index 7c68141..0aaf35f 100644
--- a/API.md
+++ b/API.md
@@ -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.
-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).
+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
-DrawTextBoxedEx for continuous text.
+DrawTextBoxed for continuous text.
Support for tint change with "\a#FFFFFFFF"
- 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 )
Get glyph index position in font for a codepoint (unicode character), fallback to '?' if not found
diff --git a/ReiLua_API.lua b/ReiLua_API.lua
index 93c3cf4..47e696e 100644
--- a/ReiLua_API.lua
+++ b/ReiLua_API.lua
@@ -4528,25 +4528,9 @@ function RL.DrawTextCodepoint( font, codepoint, position, fontSize, tint ) end
---@return any RL.DrawTextCodepoints
function RL.DrawTextCodepoints( font, codepoints, position, fontSize, spacing, tint ) end
----Draw text using font inside rectangle limits.
----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).
+---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
----DrawTextBoxedEx for continuous text.
+---DrawTextBoxed for continuous text.
---Support for tint change with "\a#FFFFFFFF"
---- Success return int, Vector2
---@param font any
@@ -4557,11 +4541,11 @@ function RL.DrawTextBoxed( font, text, rec, fontSize, spacing, wordWrap, tint, l
---@param wordWrap boolean
---@param tint table
---@param limitHeight boolean
----@param textOffset table
+---@param textOffset table|nil
---@param tabSize integer|nil
---@return any mouseCharId
---@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
@@ -4591,6 +4575,22 @@ function RL.MeasureText( text, fontSize ) end
---@return any size
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
---- Success return int
---@param font any
diff --git a/changelog b/changelog
index 208ecea..0ffd8f2 100644
--- a/changelog
+++ b/changelog
@@ -53,6 +53,8 @@ DETAILED CHANGES:
- ADDED: SoundAlias garbage collection.
- ADDED: Frustum math from raylib extras.
- 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
diff --git a/examples/textBoxed/main.lua b/examples/textBoxed/main.lua
index c6b497f..623c445 100644
--- a/examples/textBoxed/main.lua
+++ b/examples/textBoxed/main.lua
@@ -32,26 +32,27 @@ end
function RL.draw()
RL.ClearBackground( RL.RAYWHITE )
RL.DrawRectangleLines( rect, RL.GREEN )
+ textOffset = { 0, 0 }
- _, textOffset = RL.DrawTextBoxedEx(
+ _, textOffset = RL.DrawTextBoxed(
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.",
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(),
" Hyperlink.",
rect,
textSize, spacing, wordwrap, linkColor, limitHeight, textOffset
)
- _, textOffset = RL.DrawTextBoxedEx(
+ _, textOffset = RL.DrawTextBoxed(
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.",
rect,
textSize, spacing, wordwrap, RL.RED, limitHeight, textOffset
)
- RL.DrawTextBoxedEx(
+ RL.DrawTextBoxed(
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.",
rect,
diff --git a/include/text.h b/include/text.h
index dd8b9c6..5b7d070 100644
--- a/include/text.h
+++ b/include/text.h
@@ -22,12 +22,12 @@ int ltextDrawTextPro( lua_State* L );
int ltextDrawTextCodepoint( lua_State* L );
int ltextDrawTextCodepoints( lua_State* L );
int ltextDrawTextBoxed( lua_State* L );
-int ltextDrawTextBoxedEx( lua_State* L );
/* Text font info functions. */
int ltextSetTextLineSpacing( lua_State* L );
int ltextGetTextLineSpacing( lua_State* L );
int ltextMeasureText( lua_State* L );
int ltextMeasureTextEx( lua_State* L );
+int ltextMeasureTextBoxed( lua_State* L );
int ltextGetGlyphIndex( lua_State* L );
int ltextGetGlyphInfo( lua_State* L );
int ltextGetGlyphInfoByIndex( lua_State* L );
diff --git a/src/lua_core.c b/src/lua_core.c
index d039208..c4afe80 100644
--- a/src/lua_core.c
+++ b/src/lua_core.c
@@ -2222,12 +2222,12 @@ void luaRegister() {
assingGlobalFunction( "DrawTextCodepoint", ltextDrawTextCodepoint );
assingGlobalFunction( "DrawTextCodepoints", ltextDrawTextCodepoints );
assingGlobalFunction( "DrawTextBoxed", ltextDrawTextBoxed );
- assingGlobalFunction( "DrawTextBoxedEx", ltextDrawTextBoxedEx );
/* Text font info functions. */
assingGlobalFunction( "SetTextLineSpacing", ltextSetTextLineSpacing );
assingGlobalFunction( "GetTextLineSpacing", ltextGetTextLineSpacing );
assingGlobalFunction( "MeasureText", ltextMeasureText );
assingGlobalFunction( "MeasureTextEx", ltextMeasureTextEx );
+ assingGlobalFunction( "MeasureTextBoxed", ltextMeasureTextBoxed );
assingGlobalFunction( "GetGlyphIndex", ltextGetGlyphIndex );
assingGlobalFunction( "GetGlyphInfo", ltextGetGlyphInfo );
assingGlobalFunction( "GetGlyphInfoByIndex", ltextGetGlyphInfoByIndex );
diff --git a/src/text.c b/src/text.c
index e2315c7..914a75d 100644
--- a/src/text.c
+++ b/src/text.c
@@ -56,15 +56,19 @@ static unsigned char getHexValue( const char* ptr, size_t offset ) {
return getDigit( ptr[ offset ] ) * 16 + getDigit( ptr[ offset + 1 ] );
}
-static int DrawTextBoxed( Font font, char* text, Rectangle rec, float fontSize,
-float spacing, bool wordWrap, Color tint, bool limitHeight, Vector2* textOffset, int tabSize ) {
+/* If draw is false, will only measure. */
+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;
if ( rec.width <= 0 || ( rec.height <= limitHeight ? ( textOffset->y + lineSpacing ) : 0 ) ) {
+ if ( !draw ) {
+ textOffset->y -= lineSpacing;
+ }
return 0;
}
int len = TextLength( text );
- float scaleFactor = fontSize / font.baseSize;
+ float scaleFactor = fontSize / font->baseSize;
float wordWidth = 0.0;
bool measure = true;
Vector2 mousePos = GetMousePosition();
@@ -73,11 +77,11 @@ float spacing, bool wordWrap, Color tint, bool limitHeight, Vector2* textOffset,
for ( int i = 0; i < len; ) {
int codepointByteCount = 0;
int codepoint = GetCodepointNext( &text[i], &codepointByteCount );
- int index = GetGlyphIndex( font, codepoint );
+ int index = GetGlyphIndex( *font, codepoint );
- float codepointWidth = font.glyphs[ index ].advanceX == 0
- ? (float)font.recs[ index ].width * scaleFactor + spacing
- : (float)font.glyphs[ index ].advanceX * scaleFactor + spacing;
+ float codepointWidth = font->glyphs[ index ].advanceX == 0
+ ? (float)font->recs[ index ].width * scaleFactor + spacing
+ : (float)font->glyphs[ index ].advanceX * scaleFactor + spacing;
if ( codepoint == '\n' ) {
textOffset->x = 0;
@@ -85,7 +89,7 @@ float spacing, bool wordWrap, Color tint, bool limitHeight, Vector2* textOffset,
measure = true;
}
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;
}
else if ( codepoint == '\a' ) {
@@ -101,7 +105,7 @@ float spacing, bool wordWrap, Color tint, bool limitHeight, Vector2* textOffset,
}
else if ( wordWrap && ( wordWidth < rec.width ) ) {
if ( measure && codepoint != ' ' ) {
- wordWidth = measureWord( font, &text[i], fontSize, spacing );
+ wordWidth = measureWord( *font, &text[i], fontSize, spacing );
measure = false;
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 ( !draw ) {
+ textOffset->y -= lineSpacing;
+ }
break;
}
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;
}
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"
- Success return int, Vector2
@@ -595,40 +606,12 @@ int ltextDrawTextBoxed( lua_State* L ) {
int tabSize = 4;
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 ) ) {
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 );
return 2;
@@ -700,6 +683,36 @@ int ltextMeasureTextEx( lua_State* L ) {
}
/*
+> 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 )
Get glyph index position in font for a codepoint (unicode character), fallback to '?' if not found