Skip to content
Permalink
Browse files

handle FONT_{NORM,SCALE} flags in LuaFonts::Print

  • Loading branch information...
rtri
rtri committed Oct 14, 2019
1 parent 639a473 commit 0217ae542e519cebdeb8d9ee03a8734298a977d2
Showing with 49 additions and 83 deletions.
  1. +49 −83 rts/Lua/LuaFonts.cpp
@@ -35,7 +35,7 @@ bool LuaFonts::CreateMetatable(lua_State* L)
HSTR_PUSH_CFUNC(L, "__index", meta_index);
LuaPushNamedString(L, "__metatable", "protected metatable");

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

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

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

luaL_error(L, "%s(): OpenGL calls can only be used in Draw() call-ins, or while creating display lists", caller);
}


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

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

CglFont** font = (CglFont**)luaL_checkudata(L, 1, "Font");
delete *font;
@@ -99,7 +98,7 @@ int LuaFonts::meta_gc(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");
lua_pushvalue(L, 2);
lua_rawget(L, -2);
@@ -108,13 +107,11 @@ int LuaFonts::meta_index(lua_State* L)

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);

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

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

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

case 's': { options |= FONT_SHADOW; break; }
switch (*(c++)) {
case 'c': { options |= FONT_CENTER; } break;
case 'r': { options |= FONT_RIGHT; } break;

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

case 's': { options |= FONT_SHADOW; } break;
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;
}
c++;
}
}

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

return 0;
}

@@ -270,12 +268,14 @@ int LuaFonts::End(lua_State* L)
int LuaFonts::WrapText(lua_State* L)
{
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 maxHeight = luaL_optfloat(L, 4, 1e9);
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_pushnumber(L, lines);
@@ -297,10 +297,12 @@ int LuaFonts::GetTextWidth(lua_State* L)
int LuaFonts::GetTextHeight(lua_State* L)
{
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;
int lines;
const float height = f->GetTextHeight(text,&descender,&lines);

lua_pushnumber(L, height);
lua_pushnumber(L, descender);
lua_pushnumber(L, lines);
@@ -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);

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

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)) {
const int count = 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;

LuaUtils::ParseFloatArray(L, 2, &color.x, 4);
} 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);
color.x = luaL_optfloat(L, 2, color.x);
color.y = luaL_optfloat(L, 3, color.y);
color.z = luaL_optfloat(L, 4, color.z);
color.w = luaL_optfloat(L, 5, color.w);
} 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);
return 0;
}
if (outline)
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;
}

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)
{
@@ -402,5 +370,3 @@ int LuaFonts::BindTexture(lua_State* L)
return 0;
}

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

0 comments on commit 0217ae5

Please sign in to comment.
You can’t perform that action at this time.