Skip to content

Commit

Permalink
handle FONT_{NORM,SCALE} flags in LuaFonts::Print
Browse files Browse the repository at this point in the history
  • Loading branch information
rtri committed Oct 14, 2019
1 parent 639a473 commit 0217ae5
Showing 1 changed file with 49 additions and 83 deletions.
132 changes: 49 additions & 83 deletions rts/Lua/LuaFonts.cpp
Expand Up @@ -35,7 +35,7 @@ bool LuaFonts::CreateMetatable(lua_State* L)
HSTR_PUSH_CFUNC(L, "__index", meta_index); HSTR_PUSH_CFUNC(L, "__index", meta_index);
LuaPushNamedString(L, "__metatable", "protected metatable"); LuaPushNamedString(L, "__metatable", "protected metatable");


//! push userdata callouts // push userdata callouts
REGISTER_LUA_CFUNC(Print); REGISTER_LUA_CFUNC(Print);


REGISTER_LUA_CFUNC(Begin); REGISTER_LUA_CFUNC(Begin);
Expand Down Expand Up @@ -63,10 +63,10 @@ bool LuaFonts::CreateMetatable(lua_State* L)


inline void CheckDrawingEnabled(lua_State* L, const char* caller) inline void CheckDrawingEnabled(lua_State* L, const char* caller)
{ {
if (!LuaOpenGL::IsDrawingEnabled(L)) { if (LuaOpenGL::IsDrawingEnabled(L))
luaL_error(L, "%s(): OpenGL calls can only be used in Draw() " return;
"call-ins, or while creating display lists", caller);
} luaL_error(L, "%s(): OpenGL calls can only be used in Draw() call-ins, or while creating display lists", caller);
} }




Expand All @@ -86,9 +86,8 @@ inline CglFont* tofont(lua_State* L, int idx)


int LuaFonts::meta_gc(lua_State* L) int LuaFonts::meta_gc(lua_State* L)
{ {
if (lua_isnil(L, 1)) { if (lua_isnil(L, 1))
return 0; return 0;
}


CglFont** font = (CglFont**)luaL_checkudata(L, 1, "Font"); CglFont** font = (CglFont**)luaL_checkudata(L, 1, "Font");
delete *font; delete *font;
Expand All @@ -99,7 +98,7 @@ int LuaFonts::meta_gc(lua_State* L)


int LuaFonts::meta_index(lua_State* L) int LuaFonts::meta_index(lua_State* L)
{ {
//! first check if there is a function // first check if there is a function
luaL_getmetatable(L, "Font"); luaL_getmetatable(L, "Font");
lua_pushvalue(L, 2); lua_pushvalue(L, 2);
lua_rawget(L, -2); lua_rawget(L, -2);
Expand All @@ -108,13 +107,11 @@ int LuaFonts::meta_index(lua_State* L)


lua_pop(L, 1); lua_pop(L, 1);


//! couldn't find a function, so check properties // couldn't find a function, so check properties
CglFont* f = tofont(L, 1); CglFont* f = tofont(L, 1);


if (lua_israwstring(L, 2)) { if (lua_israwstring(L, 2)) {
const char* key = lua_tostring(L, 2); switch (hashString(lua_tostring(L, 2))) {

switch (hashString(key)) {
case hashString("size"): { case hashString("size"): {
lua_pushnumber(L, f->GetSize()); lua_pushnumber(L, f->GetSize());
return 1; return 1;
Expand Down Expand Up @@ -216,30 +213,31 @@ int LuaFonts::Print(lua_State* L)
if ((args >= 6) && lua_isstring(L, 6)) { if ((args >= 6) && lua_isstring(L, 6)) {
const char* c = lua_tostring(L, 6); const char* c = lua_tostring(L, 6);
while (*c != 0) { while (*c != 0) {
switch (*c) { switch (*(c++)) {
case 'c': { options |= FONT_CENTER; break; } case 'c': { options |= FONT_CENTER; } break;
case 'r': { options |= FONT_RIGHT; break; } case 'r': { options |= FONT_RIGHT; } break;


case 'a': { options |= FONT_ASCENDER; break; } case 'a': { options |= FONT_ASCENDER; } break;
case 't': { options |= FONT_TOP; break; } case 't': { options |= FONT_TOP; } break;
case 'v': { options |= FONT_VCENTER; break; } case 'v': { options |= FONT_VCENTER; } break;
case 'x': { options |= FONT_BASELINE; break; } case 'x': { options |= FONT_BASELINE; } break;
case 'b': { options |= FONT_BOTTOM; break; } case 'b': { options |= FONT_BOTTOM; } break;
case 'd': { options |= FONT_DESCENDER; break; } case 'd': { options |= FONT_DESCENDER; } break;


case 's': { options |= FONT_SHADOW; break; } case 's': { options |= FONT_SHADOW; } break;
case 'o': case 'o':
case 'O': { options |= FONT_OUTLINE; break; } case 'O': { options |= FONT_OUTLINE; } break;

case 'n': { options ^= FONT_NEAREST; } break;


case 'n': { options ^= FONT_NEAREST; break; } case 'N': { options |= FONT_NORM; } break;
case 'S': { options |= FONT_SCALE; } break;
default: break; default: break;
} }
c++;
} }
} }


f->glPrint(x, y, size, options, text); f->glPrint(x, y, size, options, text);

return 0; return 0;
} }


Expand Down Expand Up @@ -270,12 +268,14 @@ int LuaFonts::End(lua_State* L)
int LuaFonts::WrapText(lua_State* L) int LuaFonts::WrapText(lua_State* L)
{ {
CglFont* f = tofont(L, 1); CglFont* f = tofont(L, 1);
string text(luaL_checkstring(L, 2),lua_strlen(L, 2));
std::string text(luaL_checkstring(L, 2), lua_strlen(L, 2));

const float maxWidth = luaL_checkfloat(L, 3); const float maxWidth = luaL_checkfloat(L, 3);
const float maxHeight = luaL_optfloat(L, 4, 1e9); const float maxHeight = luaL_optfloat(L, 4, 1e9);
const float size = luaL_optfloat(L, 5, f->GetSize()); const float size = luaL_optfloat(L, 5, f->GetSize());


const int lines = f->WrapInPlace(text,size,maxWidth,maxHeight); const int lines = f->WrapInPlace(text, size, maxWidth, maxHeight);


lua_pushsstring(L, text); lua_pushsstring(L, text);
lua_pushnumber(L, lines); lua_pushnumber(L, lines);
Expand All @@ -297,10 +297,12 @@ int LuaFonts::GetTextWidth(lua_State* L)
int LuaFonts::GetTextHeight(lua_State* L) int LuaFonts::GetTextHeight(lua_State* L)
{ {
CglFont* f = tofont(L, 1); CglFont* f = tofont(L, 1);
const string text(luaL_checkstring(L, 2),lua_strlen(L, 2));
const std::string text(luaL_checkstring(L, 2), lua_strlen(L, 2));
float descender; float descender;
int lines; int lines;
const float height = f->GetTextHeight(text,&descender,&lines); const float height = f->GetTextHeight(text,&descender,&lines);

lua_pushnumber(L, height); lua_pushnumber(L, height);
lua_pushnumber(L, descender); lua_pushnumber(L, descender);
lua_pushnumber(L, lines); lua_pushnumber(L, lines);
Expand All @@ -311,73 +313,39 @@ int LuaFonts::GetTextHeight(lua_State* L)
/******************************************************************************/ /******************************************************************************/
/******************************************************************************/ /******************************************************************************/


int LuaFonts::SetTextColor(lua_State* L) static int SetTextColorShared(lua_State* L, bool outline)
{ {
CglFont* f = tofont(L, 1); CglFont* f = tofont(L, 1);


const int args = lua_gettop(L); // number of arguments const int args = lua_gettop(L); // number of arguments


if (args < 2) if (args < 2)
luaL_error(L, "Incorrect arguments to font:SetTextColor([\"textColor\"])"); luaL_error(L, "[%s] insufficient arguments to font:SetText%sColor(font,table|number*)", __func__, outline? "Outline": "");


float4 color; float4 color = {1.0f, 1.0f, 1.0f, 1.0f};


if (lua_istable(L, 2)) { if (lua_istable(L, 2)) {
const int count = LuaUtils::ParseFloatArray(L, 2, &color.x, 4); LuaUtils::ParseFloatArray(L, 2, &color.x, 4);

if (count < 3)
luaL_error(L, "Incorrect arguments to font:SetTextColor([\"textColor\"])");

if (count == 3)
color.w = 1.0f;

} else if (args >= 4) { } else if (args >= 4) {
color.x = luaL_checkfloat(L, 2); color.x = luaL_optfloat(L, 2, color.x);
color.y = luaL_checkfloat(L, 3); color.y = luaL_optfloat(L, 3, color.y);
color.z = luaL_checkfloat(L, 4); color.z = luaL_optfloat(L, 4, color.z);
color.w = luaL_optfloat(L, 5, 1.0f); color.w = luaL_optfloat(L, 5, color.w);
} else if (!lua_isnil(L, 2)) { } else if (!lua_isnil(L, 2)) {
luaL_error(L, "Incorrect arguments to font:SetTextColor([\"textColor\"])"); luaL_error(L, "[%s] incorrect arguments to font:SetText%sColor(font,table|number*)", __func__, outline? "Outline": "");
} }


f->SetTextColor(&color); if (outline)
return 0; f->SetOutlineColor(&color);
} else
f->SetTextColor(&color);



int LuaFonts::SetOutlineColor(lua_State* L)
{
CglFont* f = tofont(L, 1);

const int args = lua_gettop(L); // number of arguments

if (args < 2)
luaL_error(L, "Incorrect arguments to font:SetOutlineColor([\"outlineColor\"])");

float4 color;

if (lua_istable(L, 2)) {
const int count = LuaUtils::ParseFloatArray(L, 2, &color.x, 4);

if (count < 3)
luaL_error(L, "Incorrect arguments to font:SetOutlineColor([\"outlineColor\"])");

if (count == 3)
color.w = 1.0f;

} else if (args >= 4) {
color.x = luaL_checkfloat(L, 2);
color.y = luaL_checkfloat(L, 3);
color.z = luaL_checkfloat(L, 4);
color.w = luaL_optfloat(L, 5, 1.0f);
} else if (!lua_isnoneornil(L, 2)) {
luaL_error(L, "Incorrect arguments to font:SetOutlineColor([\"outlineColor\"])");
}

f->SetOutlineColor(&color);
return 0; return 0;
} }


int LuaFonts::SetTextColor(lua_State* L) { return (SetTextColorShared(L, false)); }
int LuaFonts::SetOutlineColor(lua_State* L) { return (SetTextColorShared(L, true)); }



int LuaFonts::SetAutoOutlineColor(lua_State* L) int LuaFonts::SetAutoOutlineColor(lua_State* L)
{ {
Expand All @@ -402,5 +370,3 @@ int LuaFonts::BindTexture(lua_State* L)
return 0; return 0;
} }


/******************************************************************************/
/******************************************************************************/

0 comments on commit 0217ae5

Please sign in to comment.