From 90343c58423d04f642d9bc7c6e2957d0276942cd Mon Sep 17 00:00:00 2001 From: Ben Wesch Date: Tue, 22 Oct 2024 22:35:59 +0200 Subject: [PATCH 1/2] add optional alignment arg to draw_text closes #64 --- pd.lua | 11 +++++++++++ pdlua_gfx.h | 18 +++++++++++++++++- 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/pd.lua b/pd.lua index 28b5dd1..2c72762 100644 --- a/pd.lua +++ b/pd.lua @@ -628,6 +628,17 @@ DATA = 0 SIGNAL = 1 Colors = {background = 0, foreground = 1, outline = 2} +-- Text alignment constants +TOP_LEFT = 0 +TOP_CENTER = 1 +TOP_RIGHT = 2 +CENTER_LEFT = 3 +CENTER = 4 +CENTER_RIGHT = 5 +BOTTOM_LEFT = 6 +BOTTOM_CENTER = 7 +BOTTOM_RIGHT = 8 + -- pre-load pdx.lua (advanced live coding support); if you don't want this, -- just comment out the line below pdx = require 'pdx' diff --git a/pdlua_gfx.h b/pdlua_gfx.h index a3f32ab..282c905 100644 --- a/pdlua_gfx.h +++ b/pdlua_gfx.h @@ -441,6 +441,7 @@ static int draw_text(lua_State* L) { SETFLOAT(args + 2, luaL_checknumber(L, 3)); // y SETFLOAT(args + 3, luaL_checknumber(L, 4)); // w SETFLOAT(args + 4, luaL_checknumber(L, 5)); // h + // FIXME: ignoring the optional alignment parameter for plugdata for now plugdata_draw(gfx->object, gfx->current_layer, gensym("lua_draw_text"), 5, args); return 0; } @@ -1291,6 +1292,7 @@ static int draw_text(lua_State* L) { int font_height = luaL_checknumber(L, 5); font_height = sys_hostfontsize(font_height, glist_getzoom(cnv)); + int alignment = lua_gettop(L) >= 6 ? luaL_checkinteger(L, 6) : 0; // Default to TOP_LEFT transform_point(gfx, &x, &y); transform_size(gfx, &w, &font_height); @@ -1305,8 +1307,22 @@ static int draw_text(lua_State* L) { const char* tags[] = { gfx->object_tag, register_drawing(gfx), gfx->current_layer_tag }; #ifndef PURR_DATA + // Convert alignment value to tcl/tk anchor point + const char* anchor; + switch (alignment) { + case 1: anchor = "n"; break; // TOP_CENTER + case 2: anchor = "ne"; break; // TOP_RIGHT + case 3: anchor = "w"; break; // CENTER_LEFT + case 4: anchor = "center"; break; // CENTER + case 5: anchor = "e"; break; // CENTER_RIGHT + case 6: anchor = "sw"; break; // BOTTOM_LEFT + case 7: anchor = "s"; break; // BOTTOM_CENTER + case 8: anchor = "se"; break; // BOTTOM_RIGHT + default: anchor = "nw"; break; // TOP_LEFT + } + pdgui_vmess(0, "crr ii rs ri rs rS", cnv, "create", "text", - 0, 0, "-anchor", "nw", "-width", w, "-text", text, "-tags", 3, tags); + 0, 0, "-anchor", anchor, "-width", w, "-text", text, "-tags", 3, tags); t_atom fontatoms[3]; SETSYMBOL(fontatoms+0, gensym(sys_font)); From ce1e5dc76042b28fa6e67669cf502dabafabd8ba Mon Sep 17 00:00:00 2001 From: Ben Wesch Date: Wed, 23 Oct 2024 00:11:35 +0200 Subject: [PATCH 2/2] simplified arg check with luaL_optinteger --- pdlua_gfx.h | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/pdlua_gfx.h b/pdlua_gfx.h index 282c905..85f0c3a 100644 --- a/pdlua_gfx.h +++ b/pdlua_gfx.h @@ -1289,10 +1289,9 @@ static int draw_text(lua_State* L) { int x = luaL_checknumber(L, 2); int y = luaL_checknumber(L, 3); int w = luaL_checknumber(L, 4); - int font_height = luaL_checknumber(L, 5); - font_height = sys_hostfontsize(font_height, glist_getzoom(cnv)); + int font_height = sys_hostfontsize(luaL_checknumber(L, 5), glist_getzoom(cnv)); + int alignment = luaL_optinteger(L, 6, 0); // Defaults to TOP_LEFT - int alignment = lua_gettop(L) >= 6 ? luaL_checkinteger(L, 6) : 0; // Default to TOP_LEFT transform_point(gfx, &x, &y); transform_size(gfx, &w, &font_height);