diff options
| author | jussi | 2024-01-03 18:23:19 +0200 |
|---|---|---|
| committer | jussi | 2024-01-03 18:23:19 +0200 |
| commit | 70a2bcba18aa9855380c132f89e26b61bfd2cb40 (patch) | |
| tree | 7f5d18d0d82afc352b5c75c68b136f4e2f6850d0 /src | |
| parent | 192d471fb3caaa6d73796185e5cadc62075743f7 (diff) | |
| download | reilua-enhanced-70a2bcba18aa9855380c132f89e26b61bfd2cb40.tar.gz reilua-enhanced-70a2bcba18aa9855380c132f89e26b61bfd2cb40.tar.bz2 reilua-enhanced-70a2bcba18aa9855380c132f89e26b61bfd2cb40.zip | |
Rest of font loading/unloading functions. GlyphInfo type to userdata. GlyphInfo management functions.
Diffstat (limited to 'src')
| -rw-r--r-- | src/lights.c | 2 | ||||
| -rw-r--r-- | src/lua_core.c | 120 | ||||
| -rw-r--r-- | src/models.c | 10 | ||||
| -rw-r--r-- | src/platforms/core_desktop.c | 6 | ||||
| -rw-r--r-- | src/rlgl.c | 2 | ||||
| -rw-r--r-- | src/text.c | 433 | ||||
| -rw-r--r-- | src/textures.c | 8 |
7 files changed, 485 insertions, 96 deletions
diff --git a/src/lights.c b/src/lights.c index 7131c99..bef40a5 100644 --- a/src/lights.c +++ b/src/lights.c @@ -179,7 +179,7 @@ int llightsGetLightColor( lua_State *L ) { Get light enabled -- Success return boolean +- Success return bool */ int llightsIsLightEnabled( lua_State *L ) { Light *light = uluaGetLight( L, 1 ); diff --git a/src/lua_core.c b/src/lua_core.c index 45ea370..820cc20 100644 --- a/src/lua_core.c +++ b/src/lua_core.c @@ -156,6 +156,25 @@ static void defineFont() { lua_setfield( L, -2, "__gc" ); } + /* GlyphInfo. */ +static int gcGlyphInfo( lua_State *L ) { + if ( state->gcUnload ) { + GlyphInfo *glyph = luaL_checkudata( L, 1, "GlyphInfo" ); + unloadGlyphInfo( glyph ); + } + return 0; +} + +static void defineGlyphInfo() { + lua_State *L = state->luaState; + + luaL_newmetatable( L, "GlyphInfo" ); + lua_pushvalue( L, -1 ); + lua_setfield( L, -2, "__index" ); + lua_pushcfunction( L, gcGlyphInfo ); + lua_setfield( L, -2, "__gc" ); +} + /* Wave. */ static int gcWave( lua_State *L ) { if ( state->gcUnload ) { @@ -357,7 +376,7 @@ static void defineGlobals() { lua_setglobal( L, "RL" ); lua_getglobal( L, "RL" ); -/* Note! Docgen relyes on this line format. */ +/* Note! Docgen rely on this line format. */ /*DOC_DEFINES_START*/ /* System/Window config flags */ assignGlobalInt( FLAG_VSYNC_HINT, "FLAG_VSYNC_HINT" ); // Set to try enabling V-Sync on GPU @@ -1004,6 +1023,7 @@ bool luaInit( int argn, const char **argc ) { defineCamera3D(); defineShader(); defineFont(); + defineGlyphInfo(); defineWave(); defineSound(); defineMusic(); @@ -1697,8 +1717,13 @@ void luaRegister() { assingGlobalFunction( "LoadFont", ltextLoadFont ); assingGlobalFunction( "LoadFontEx", ltextLoadFontEx ); assingGlobalFunction( "LoadFontFromImage", ltextLoadFontFromImage ); + assingGlobalFunction( "LoadFontFromMemory", ltextLoadFontFromMemory ); + assingGlobalFunction( "LoadFontFromData", ltextLoadFontFromData ); assingGlobalFunction( "IsFontReady", ltextIsFontReady ); + assingGlobalFunction( "LoadFontData", ltextLoadFontData ); + assingGlobalFunction( "GenImageFontAtlas", ltextGenImageFontAtlas ); assingGlobalFunction( "UnloadFont", ltextUnloadFont ); + assingGlobalFunction( "ExportFontAsCode", ltextExportFontAsCode ); /* Text drawing functions. */ assingGlobalFunction( "DrawFPS", ltextDrawFPS ); assingGlobalFunction( "DrawText", ltextDrawText ); @@ -1713,11 +1738,24 @@ void luaRegister() { assingGlobalFunction( "MeasureText", ltextMeasureText ); assingGlobalFunction( "GetGlyphIndex", ltextGetGlyphIndex ); assingGlobalFunction( "GetGlyphInfo", ltextGetGlyphInfo ); + assingGlobalFunction( "GetGlyphInfoByIndex", ltextGetGlyphInfoByIndex ); assingGlobalFunction( "GetGlyphAtlasRec", ltextGetGlyphAtlasRec ); + assingGlobalFunction( "GetGlyphAtlasRecByIndex", ltextGetGlyphAtlasRecByIndex ); assingGlobalFunction( "GetFontBaseSize", ltextGetFontBaseSize ); assingGlobalFunction( "GetFontGlyphCount", ltextGetFontGlyphCount ); assingGlobalFunction( "GetFontGlyphPadding", ltextGetFontGlyphPadding ); assingGlobalFunction( "GetFontTexture", ltextGetFontTexture ); + /* GlyphInfo management functions. */ + assingGlobalFunction( "LoadGlyphInfo", ltextLoadGlyphInfo ); + assingGlobalFunction( "UnloadGlyphInfo", ltextUnloadGlyphInfo ); + assingGlobalFunction( "SetGlyphInfoValue", ltextSetGlyphInfoValue ); + assingGlobalFunction( "SetGlyphInfoOffset", ltextSetGlyphInfoOffset ); + assingGlobalFunction( "SetGlyphInfoAdvanceX", ltextSetGlyphInfoAdvanceX ); + assingGlobalFunction( "SetGlyphInfoImage", ltextSetGlyphInfoImage ); + assingGlobalFunction( "GetGlyphInfoValue", ltextGetGlyphInfoValue ); + assingGlobalFunction( "GetGlyphInfoOffset", ltextGetGlyphInfoOffset ); + assingGlobalFunction( "GetGlyphInfoAdvanceX", ltextGetGlyphInfoAdvanceX ); + assingGlobalFunction( "GetGlyphInfoImage", ltextGetGlyphInfoImage ); /* Audio. */ /* Audio device management functions. */ @@ -2623,59 +2661,6 @@ NPatchInfo uluaGetNPatchInfo( lua_State *L, int index ) { return npatch; } -GlyphInfo uluaGetGlyphInfo( lua_State *L, int index ) { - luaL_checktype( L, index, LUA_TTABLE ); - GlyphInfo glyph = { 0 }; - - int t = index, i = 0; - lua_pushnil( L ); - - while ( lua_next( L, t ) != 0 ) { - /* Do not check type since there should be table and ints. */ - if ( lua_isnumber( L, -2 ) ) { - switch ( i ) { - case 0: - glyph.value = lua_tointeger( L, -1 ); - break; - case 1: - glyph.offsetX = lua_tointeger( L, -1 ); - break; - case 2: - glyph.offsetY = lua_tointeger( L, -1 ); - break; - case 3: - glyph.advanceX = lua_tointeger( L, -1 ); - break; - case 4: - glyph.image = *uluaGetImage( L, lua_gettop( L ) ); - break; - default: - break; - } - } - else if ( lua_isstring( L, -2 ) ) { - if ( strcmp( "value", (char*)lua_tostring( L, -2 ) ) == 0 ) { - glyph.value = lua_tointeger( L, -1 ); - } - else if ( strcmp( "offsetX", (char*)lua_tostring( L, -2 ) ) == 0 ) { - glyph.offsetX = lua_tointeger( L, -1 ); - } - else if ( strcmp( "offsetY", (char*)lua_tostring( L, -2 ) ) == 0 ) { - glyph.offsetY = lua_tointeger( L, -1 ); - } - else if ( strcmp( "advanceX", (char*)lua_tostring( L, -2 ) ) == 0 ) { - glyph.advanceX = lua_tointeger( L, -1 ); - } - else if ( strcmp( "image", (char*)lua_tostring( L, -2 ) ) == 0 ) { - glyph.image = *uluaGetImage( L, lua_gettop( L ) ); - } - } - i++; - lua_pop( L, 1 ); - } - return glyph; -} - BoneInfo uluaGetBoneInfo( lua_State *L, int index ) { luaL_checktype( L, index, LUA_TTABLE ); BoneInfo bone = { 0 }; @@ -2819,6 +2804,13 @@ Font* uluaGetFont( lua_State *L, int index ) { return luaL_checkudata( L, index, "Font" ); } +GlyphInfo* uluaGetGlyphInfo( lua_State *L, int index ) { + if ( lua_islightuserdata( L, index ) ) { + return (GlyphInfo*)lua_touserdata( L, index ); + } + return luaL_checkudata( L, index, "GlyphInfo" ); +} + Wave* uluaGetWave( lua_State *L, int index ) { if ( lua_islightuserdata( L, index ) ) { return (Wave*)lua_touserdata( L, index ); @@ -3047,20 +3039,6 @@ void uluaPushBoundingBox( lua_State *L, BoundingBox box ) { lua_rawseti( L, -2, 2 ); } -void uluaPushGlyphInfo( lua_State *L, GlyphInfo glyphInfo, Image *image ) { - lua_createtable( L, 5, 0 ); - lua_pushinteger( L, glyphInfo.value ); - lua_setfield( L, -2, "value" ); - lua_pushinteger( L, glyphInfo.offsetX ); - lua_setfield( L, -2, "offsetX" ); - lua_pushinteger( L, glyphInfo.offsetY ); - lua_setfield( L, -2, "offsetY" ); - lua_pushinteger( L, glyphInfo.advanceX ); - lua_setfield( L, -2, "advanceX" ); - lua_pushlightuserdata( L, image ); - lua_setfield( L, -2, "image" ); -} - void uluaPushBoneInfo( lua_State *L, BoneInfo boneInfo ) { lua_createtable( L, 2, 0 ); lua_pushstring( L, boneInfo.name ); @@ -3130,6 +3108,12 @@ void uluaPushFont( lua_State *L, Font font ) { luaL_setmetatable( L, "Font" ); } +void uluaPushGlyphInfo( lua_State *L, GlyphInfo glyph ) { + GlyphInfo *glyphP = lua_newuserdata( L, sizeof( GlyphInfo ) ); + *glyphP = glyph; + luaL_setmetatable( L, "GlyphInfo" ); +} + void uluaPushWave( lua_State *L, Wave wave ) { Wave *waveP = lua_newuserdata( L, sizeof( Wave ) ); *waveP = wave; diff --git a/src/models.c b/src/models.c index b65fccf..84e0b1a 100644 --- a/src/models.c +++ b/src/models.c @@ -981,7 +981,7 @@ int lmodelsDrawBillboardPro( lua_State *L ) { > RL.UpdateMesh( Mesh mesh, Mesh{} meshData ) Update mesh vertex data in GPU. -Note! Mainly intented to be used with custom meshes. +NOTE: Mainly intented to be used with custom meshes. */ int lmodelsUpdateMesh( lua_State *L ) { Mesh *mesh = uluaGetMesh( L, 1 ); @@ -1820,7 +1820,7 @@ int lmodelsSetMaterialParams( lua_State *L ) { /* > texture = RL.GetMaterialTexture( Material material, int mapType ) -Get texture from material map type. Returns -1 if no texture +Get texture from material map type. Return as lightuserdata - Success return Texture */ @@ -1828,7 +1828,7 @@ int lmodelsGetMaterialTexture( lua_State *L ) { Material *material = uluaGetMaterial( L, 1 ); int mapType = luaL_checkinteger( L, 2 ); - uluaPushTexture( L, material->maps[ mapType ].texture ); + lua_pushlightuserdata( L, &material->maps[ mapType ].texture ); return 1; } @@ -1868,9 +1868,9 @@ int lmodelsGetMaterialValue( lua_State *L ) { /* > shader = RL.GetMaterialShader( Material material ) -Get material shader +Get material shader. Return as lightuserdata -- Success return Shader. Return as lightuserdata +- Success return Shader */ int lmodelsGetMaterialShader( lua_State *L ) { Material *material = uluaGetMaterial( L, 1 ); diff --git a/src/platforms/core_desktop.c b/src/platforms/core_desktop.c index fa47d4a..a9c9527 100644 --- a/src/platforms/core_desktop.c +++ b/src/platforms/core_desktop.c @@ -517,7 +517,7 @@ static void joystickEvent( int jid, int event ) { > GLFWpentabletdataEvent = { int type, float x, float y, float z, float pressure, float pitch, float yaw, float roll } Called when the pen tablet data is updated. Type GLFW_PEN_TABLET_DATA_EVENT -NOTE! Experimental. Needs glfw PR https://github.com/glfw/glfw/pull/1445 +NOTE: Experimental. Needs glfw PR https://github.com/glfw/glfw/pull/1445 */ static void penTabletDataEvent( double x, double y, double z, double pressure, double pitch, double yaw, double roll ) { lua_State *L = state->luaState; @@ -559,7 +559,7 @@ static void penTabletDataEvent( double x, double y, double z, double pressure, d > GLFWpentabletcursorEvent = { int type, int identifier } Called when the pen tablet cursor has changed. Type GLFW_PEN_TABLET_CURSOR_EVENT -NOTE! Experimental. Needs glfw PR https://github.com/glfw/glfw/pull/1445 +NOTE: Experimental. Needs glfw PR https://github.com/glfw/glfw/pull/1445 */ static void penTabletCursorEvent( unsigned int identifier ) { lua_State *L = state->luaState; @@ -589,7 +589,7 @@ static void penTabletCursorEvent( unsigned int identifier ) { > GLFWpentabletproximityEvent = { int type, int proxState } Called when the pen tablet proximity has changed. Type GLFW_PEN_TABLET_PROXIMITY_EVENT -NOTE! Experimental. Needs glfw PR https://github.com/glfw/glfw/pull/1445 +NOTE: Experimental. Needs glfw PR https://github.com/glfw/glfw/pull/1445 */ static void penTabletProximityEvent( int proxState ) { lua_State *L = state->luaState; @@ -1163,7 +1163,7 @@ int lrlglUnloadVertexBuffer( lua_State *L ) { /* > RL.rlSetVertexAttribute( int index, int compSize, int type, bool normalized, int stride, int pointer ) -Set vertex attribute. Note! Pointer should be given in size of bytes +Set vertex attribute. NOTE: Pointer should be given in size of bytes */ int lrlglSetVertexAttribute( lua_State *L ) { int index = luaL_checkinteger( L, 1 ); @@ -4,6 +4,10 @@ #include "textures.h" #include "lua_core.h" +void unloadGlyphInfo( GlyphInfo *glyph ) { + UnloadImage( glyph->image ); +} + // DrawTextBoxed is modified DrawTextBoxedSelectable from raylib [text] example - Rectangle bounds // Draw text using font inside rectangle limits @@ -157,6 +161,18 @@ bool wordWrap, Color *tints, int tintCount, Color *backTints, int backTintCount return mouseChar; } +static inline void getCodepoints( lua_State *L, int codepoints[], int index ) { + int t = index; + int i = 0; + lua_pushnil( L ); + + while ( lua_next( L, t ) != 0 ) { + codepoints[i] = lua_tointeger( L, -1 ); + i++; + lua_pop( L, 1 ); + } +} + /* ## Text - Font loading/unloading functions */ @@ -210,16 +226,7 @@ int ltextLoadFontEx( lua_State *L ) { int codepointCount = uluaGetTableLen( L, 3 ); int codepoints[ codepointCount ]; - int t = 3; - int i = 0; - lua_pushnil( L ); - - while ( lua_next( L, t ) != 0 ) { - codepoints[i] = lua_tointeger( L, -1 ); - - i++; - lua_pop( L, 1 ); - } + getCodepoints( L, codepoints, 3 ); uluaPushFont( L, LoadFontEx( lua_tostring( L, 1 ), fontSize, codepoints, codepointCount ) ); return 1; @@ -252,6 +259,94 @@ int ltextLoadFontFromImage( lua_State *L ) { } /* +> font = RL.LoadFontFromMemory( string fileType, Buffer fileData, int fontSize, int{} codepoints ) + +Load font from memory buffer, fileType refers to extension: i.e. '.ttf'. NOTE: fileData type should be unsigned char + +- Success return Font +*/ +int ltextLoadFontFromMemory( lua_State *L ) { + const char *fileType = luaL_checkstring( L, 1 ); + Buffer *fileData = uluaGetBuffer( L, 2 ); + int fontSize = luaL_checkinteger( L, 3 ); + + if ( lua_istable( L, 4 ) ) { + int codepointCount = uluaGetTableLen( L, 4 ); + int codepoints[ codepointCount ]; + + getCodepoints( L, codepoints, 4 ); + uluaPushFont( L, LoadFontFromMemory( fileType, fileData->data, fileData->size, fontSize, codepoints, codepointCount ) ); + + return 1; + } + /* If no codepoints provided. */ + uluaPushFont( L, LoadFontFromMemory( fileType, fileData->data, fileData->size, fontSize, NULL, 0 ) ); + + return 1; +} + +/* +> font = RL.LoadFontFromData( Font{} fontData ) + +Load Font from data + +- Success return Font +*/ +int ltextLoadFontFromData( lua_State *L ) { + luaL_checktype( L, 1, LUA_TTABLE ); + + Font font = { 0 }; + + int t = 1; + lua_pushnil( L ); + + while ( lua_next( L, t ) != 0 ) { + if ( strcmp( "baseSize", (char*)lua_tostring( L, -2 ) ) == 0 ) { + font.baseSize = luaL_checkinteger( L, -1 ); + } + else if ( strcmp( "glyphCount", (char*)lua_tostring( L, -2 ) ) == 0 ) { + font.glyphCount = luaL_checkinteger( L, -1 ); + } + else if ( strcmp( "glyphPadding", (char*)lua_tostring( L, -2 ) ) == 0 ) { + font.glyphPadding = luaL_checkinteger( L, -1 ); + } + else if ( strcmp( "texture", (char*)lua_tostring( L, -2 ) ) == 0 ) { + font.texture = *uluaGetTexture( L, lua_gettop( L ) ); + } + else if ( strcmp( "recs", (char*)lua_tostring( L, -2 ) ) == 0 ) { + int recCount = uluaGetTableLen( L, lua_gettop( L ) ); + font.recs = malloc( recCount * sizeof( Rectangle ) ); + int t2 = lua_gettop( L ); + int i = 0; + lua_pushnil( L ); + + while ( lua_next( L, t2 ) != 0 ) { + font.recs[i] = uluaGetRectangle( L, lua_gettop( L ) ); + i++; + lua_pop( L, 1 ); + } + } + else if ( strcmp( "glyphs", (char*)lua_tostring( L, -2 ) ) == 0 ) { + int glyphCount = uluaGetTableLen( L, lua_gettop( L ) ); + font.glyphs = malloc( glyphCount * sizeof( GlyphInfo ) ); + int t2 = lua_gettop( L ); + int i = 0; + lua_pushnil( L ); + + while ( lua_next( L, t2 ) != 0 ) { + font.glyphs[i] = *uluaGetGlyphInfo( L, lua_gettop( L ) ); + i++; + lua_pop( L, 1 ); + } + } + lua_pop( L, 1 ); + } + uluaPushFont( L, font ); + + return 1; +} + +/* > isReady = RL.IsFontReady( Font font ) Check if a font is ready @@ -267,6 +362,84 @@ int ltextIsFontReady( lua_State *L ) { } /* +> glyphs = RL.LoadFontData( Buffer fileData, int fontSize, int{} codepoints, int type ) + +Load font data for further use. NOTE: fileData type should be unsigned char + +- Success return GlyphInfo{} +*/ +int ltextLoadFontData( lua_State *L ) { + Buffer *fileData = uluaGetBuffer( L, 1 ); + int fontSize = luaL_checkinteger( L, 2 ); + int type = luaL_checkinteger( L, 4 ); + int codepointCount = 95; // In case no chars count provided, default to 95. + + if ( lua_istable( L, 3 ) ) { + codepointCount = uluaGetTableLen( L, 3 ); + int codepoints[ codepointCount ]; + + getCodepoints( L, codepoints, 3 ); + GlyphInfo *glyphs = LoadFontData( fileData->data, fileData->size, fontSize, codepoints, codepointCount, type ); + lua_createtable( L, codepointCount, 0 ); + + for ( int i = 0; i < codepointCount; i++ ) { + uluaPushGlyphInfo( L, glyphs[i] ); + lua_rawseti( L, -2, i + 1 ); + } + UnloadFontData( glyphs, codepointCount ); + + return 1; + } + /* If no codepoints provided. */ + GlyphInfo *glyphs = LoadFontData( fileData->data, fileData->size, fontSize, NULL, 0, type ); + lua_createtable( L, codepointCount, 0 ); + + for ( int i = 0; i < codepointCount; i++ ) { + uluaPushGlyphInfo( L, glyphs[i] ); + lua_rawseti( L, -2, i + 1 ); + } + UnloadFontData( glyphs, codepointCount ); + + return 1; +} + +/* +> image, rectangles = RL.GenImageFontAtlas( GlyphInfo{} glyphs, int fontSize, int padding, int packMethod ) + +Generate image font atlas using chars info. NOTE: Packing method: 0-Default, 1-Skyline + +- Success Image, Rectangle{} +*/ +int ltextGenImageFontAtlas( lua_State *L ) { + int fontSize = luaL_checkinteger( L, 2 ); + int padding = luaL_checkinteger( L, 3 ); + int packMethod = luaL_checkinteger( L, 4 ); + + int glyphCount = uluaGetTableLen( L, 1 ); + GlyphInfo glyphs[ glyphCount ]; + Rectangle *glyphRecs; + + int t = 1; + int i = 0; + lua_pushnil( L ); + + while ( lua_next( L, t ) != 0 ) { + glyphs[i] = *uluaGetGlyphInfo( L, lua_gettop( L ) ); + i++; + lua_pop( L, 1 ); + } + uluaPushImage( L, GenImageFontAtlas( glyphs, &glyphRecs, glyphCount, fontSize, padding, packMethod ) ); + lua_createtable( L, glyphCount, 0 ); + + for ( i = 0; i < glyphCount; i++ ) { + uluaPushRectangle( L, glyphRecs[i] ); + lua_rawseti( L, -2, i + 1 ); + } + + return 2; +} + +/* > RL.UnloadFont( Font font ) Unload font from GPU memory (VRAM) @@ -280,6 +453,22 @@ int ltextUnloadFont( lua_State *L ) { } /* +> RL.ExportFontAsCode( Font font, string fileName ) + +Export font as code file, returns true on success + +- Success return bool +*/ +int ltextExportFontAsCode( lua_State *L ) { + Font *font = uluaGetFont( L, 1 ); + const char *fileName = luaL_checkstring( L, 2 ); + + lua_pushboolean( L, ExportFontAsCode( *font, fileName ) ); + + return 1; +} + +/* ## Text - Text drawing functions */ @@ -509,8 +698,7 @@ int ltextGetGlyphIndex( lua_State *L ) { /* > glyphInfo = RL.GetGlyphInfo( Font font, int codepoint ) -Get glyph font info data for a codepoint (unicode character), fallback to '?' if not found. -Return Image as lightuserdata +Get glyph font info data for a codepoint (unicode character), fallback to '?' if not found - Success return GlyphInfo */ @@ -519,7 +707,30 @@ int ltextGetGlyphInfo( lua_State *L ) { int codepoint = luaL_checkinteger( L, 2 ); int id = GetGlyphIndex( *font, codepoint ); - uluaPushGlyphInfo( L, font->glyphs[id], &font->glyphs[id].image ); + uluaPushGlyphInfo( L, font->glyphs[id] ); + + return 1; +} + +/* +> glyphInfo = RL.GetGlyphInfoByIndex( Font font, int index ) + +Get glyph font info data by index + +- Failure return nil +- Success return GlyphInfo +*/ +int ltextGetGlyphInfoByIndex( lua_State *L ) { + Font *font = uluaGetFont( L, 1 ); + int index = luaL_checkinteger( L, 2 ); + + if ( 0 <= index && index < font->glyphCount ) { + uluaPushGlyphInfo( L, font->glyphs[ index ] ); + } + else { + TraceLog( state->logLevelInvalid, "Glyph index %d out of bounds", index ); + lua_pushnil( L ); + } return 1; } @@ -541,6 +752,29 @@ int ltextGetGlyphAtlasRec( lua_State *L ) { } /* +> rect = RL.GetGlyphAtlasRecByIndex( Font font, int index ) + +Get glyph rectangle in font atlas by index + +- Failure return nil +- Success return Rectangle +*/ +int ltextGetGlyphAtlasRecByIndex( lua_State *L ) { + Font *font = uluaGetFont( L, 1 ); + int index = luaL_checkinteger( L, 2 ); + + if ( 0 <= index && index < font->glyphCount ) { + uluaPushRectangle( L, font->recs[ index ] ); + } + else { + TraceLog( state->logLevelInvalid, "Glyph index %d out of bounds", index ); + lua_pushnil( L ); + } + + return 1; +} + +/* > baseSize = RL.GetFontBaseSize( Font font ) Get font base size (default chars height) @@ -595,8 +829,179 @@ Get font texture atlas containing the glyphs. Return as lightuserdata int ltextGetFontTexture( lua_State *L ) { Font *font = uluaGetFont( L, 1 ); - // uluaPushTexture( L, font->texture ); lua_pushlightuserdata( L, &font->texture ); return 1; } + +/* +## Text - GlyphInfo management functions +*/ + +/* +> glyphInfo = RL.LoadGlyphInfo( GlyphInfo{} glyphInfoData ) + +Load GlyphInfo from data + +- Success return GlyphInfo +*/ +int ltextLoadGlyphInfo( lua_State *L ) { + luaL_checktype( L, 1, LUA_TTABLE ); + + GlyphInfo glyph = { 0 }; + + int t = 1; + lua_pushnil( L ); + + while ( lua_next( L, t ) != 0 ) { + if ( strcmp( "value", (char*)lua_tostring( L, -2 ) ) == 0 ) { + glyph.value = (unsigned int)luaL_checkinteger( L, -1 ); + } + else if ( strcmp( "offsetX", (char*)lua_tostring( L, -2 ) ) == 0 ) { + glyph.offsetX = luaL_checkinteger( L, -1 ); + } + else if ( strcmp( "offsetY", (char*)lua_tostring( L, -2 ) ) == 0 ) { + glyph.offsetY = luaL_checkinteger( L, -1 ); + } + else if ( strcmp( "advanceX", (char*)lua_tostring( L, -2 ) ) == 0 ) { + glyph.advanceX = luaL_checkinteger( L, -1 ); + } + else if ( strcmp( "image", (char*)lua_tostring( L, -2 ) ) == 0 ) { + glyph.image = *uluaGetImage( L, lua_gettop( L ) ); + } + lua_pop( L, 1 ); + } + uluaPushGlyphInfo( L, glyph ); + + return 1; +} + +/* +> RL.UnloadGlyphInfo( GlyphInfo glyphInfo ) + +Unload glyphInfo image from CPU memory (RAM) +*/ +int ltextUnloadGlyphInfo( lua_State *L ) { + GlyphInfo *glyph = uluaGetGlyphInfo( L, 1 ); + + unloadGlyphInfo( glyph ); + + return 0; +} + +/* +> RL.SetGlyphInfoValue( GlyphInfo glyphInfo, int value ) + +Set glyphInfo character value (Unicode) +*/ +int ltextSetGlyphInfoValue( lua_State *L ) { + GlyphInfo *glyph = uluaGetGlyphInfo( L, 1 ); + int value = luaL_checkinteger( L, 2 ); + + glyph->value = value; + + return 0; +} + +/* +> RL.SetGlyphInfoOffset( GlyphInfo glyphInfo, Vector2 offset ) + +Set glyphInfo character offset when drawing +*/ +int ltextSetGlyphInfoOffset( lua_State *L ) { + GlyphInfo *glyph = uluaGetGlyphInfo( L, 1 ); + Vector2 offset = uluaGetVector2( L, 2 ); + + glyph->offsetX = (int)offset.x; + glyph->offsetY = (int)offset.y; + + return 0; +} + +/* +> RL.SetGlyphInfoAdvanceX( GlyphInfo glyphInfo, int advanceX ) + +Set glyphInfo character advance position X +*/ +int ltextSetGlyphInfoAdvanceX( lua_State *L ) { + GlyphInfo *glyph = uluaGetGlyphInfo( L, 1 ); + int advanceX = luaL_checkinteger( L, 2 ); + + glyph->advanceX = advanceX; + + return 0; +} + +/* +> RL.SetGlyphInfoImage( GlyphInfo glyphInfo, Image image ) + +Set glyphInfo character image data +*/ +int ltextSetGlyphInfoImage( lua_State *L ) { + GlyphInfo *glyph = uluaGetGlyphInfo( L, 1 ); + Image image = *uluaGetImage( L, 2 ); + + glyph->image = image; + + return 0; +} + +/* +> value = RL.GetGlyphInfoValue( GlyphInfo glyphInfo ) + +Get glyphInfo character value (Unicode) + +- Success return int +*/ +int ltextGetGlyphInfoValue( lua_State *L ) { + GlyphInfo *glyph = uluaGetGlyphInfo( L, 1 ); + + lua_pushinteger( L, glyph->value ); + + return 1; +} + +/* +> offset = RL.GetGlyphInfoOffset( GlyphInfo glyphInfo ) + +Get glyphInfo character offset when drawing + +- Success return Vector2 +*/ +int ltextGetGlyphInfoOffset( lua_State *L ) { + GlyphInfo *glyph = uluaGetGlyphInfo( L, 1 ); + + uluaPushVector2( L, (Vector2){ glyph->offsetX, glyph->offsetY } ); + + return 1; +} + +/* +> advanceX = RL.GetGlyphInfoAdvanceX( GlyphInfo glyphInfo ) + +Get glyphInfo character advance position X + +- Success return int +*/ +int ltextGetGlyphInfoAdvanceX( lua_State *L ) { + GlyphInfo *glyph = uluaGetGlyphInfo( L, 1 ); + + lua_pushinteger( L, glyph->advanceX ); + + return 1; +} + +/* +> image = RL.GetGlyphInfoImage( GlyphInfo glyphInfo ) + +Get glyphInfo character image data. Return as lightuserdata + +- Success return Image +*/ +int ltextGetGlyphInfoImage( lua_State *L ) { + GlyphInfo *glyph = uluaGetGlyphInfo( L, 1 ); + + lua_pushlightuserdata( L, &glyph->image ); + + return 1; +} diff --git a/src/textures.c b/src/textures.c index 24c8232..f88f9b3 100644 --- a/src/textures.c +++ b/src/textures.c @@ -1134,9 +1134,9 @@ int ltexturesLoadTextureFromData( lua_State *L ) { Texture texture = { 0 }; int t = 1; - lua_pushnil( L ); + lua_pushnil( L ); - while ( lua_next( L, t ) != 0 ) { + while ( lua_next( L, t ) != 0 ) { if ( strcmp( "id", (char*)lua_tostring( L, -2 ) ) == 0 ) { texture.id = (unsigned int)luaL_checkinteger( L, -1 ); } @@ -1152,8 +1152,8 @@ int ltexturesLoadTextureFromData( lua_State *L ) { else if ( strcmp( "format", (char*)lua_tostring( L, -2 ) ) == 0 ) { texture.format = luaL_checkinteger( L, -1 ); } - lua_pop( L, 1 ); - } + lua_pop( L, 1 ); + } uluaPushTexture( L, texture ); return 1; |
