Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add optional alignment arg to draw_text #65

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
21 changes: 18 additions & 3 deletions 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 @@ -1288,8 +1289,8 @@ 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

transform_point(gfx, &x, &y);
transform_size(gfx, &w, &font_height);
Expand All @@ -1305,8 +1306,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