Skip to content

Commit

Permalink
add optional alignment arg to draw_text
Browse files Browse the repository at this point in the history
closes agraef#64
  • Loading branch information
ben-wes committed Oct 22, 2024
1 parent 4301f74 commit 90343c5
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
11 changes: 11 additions & 0 deletions pd.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
18 changes: 17 additions & 1 deletion pdlua_gfx.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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);

Expand All @@ -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));
Expand Down

0 comments on commit 90343c5

Please sign in to comment.