agraef / pd-lua

Lua bindings for Pd, updated for Lua 5.3+
https://agraef.github.io/pd-lua/
GNU General Public License v2.0
50 stars 11 forks source link

Allow setting text alignment mode #64

Open ben-wes opened 1 week ago

ben-wes commented 1 week ago

currently, the text coordinate always is at the upper left of the text bounding box - obviously resulting in top-left aligned text.

For some use cases (especially when displaying numbers), right alignment would be a nice option to have.

For this, we could add a new alignment setting (similar to the way that colors are set) like set_text_align(mode). An open question here is whether the vertical alignment is also necessary or if left, center, right are enough.

Tcl/Tk uses compass directions for this (see https://www.tcl.tk/man/tcl8.4/TkCmd/options.htm#M-anchor): n, ne, e, se, s, sw, w, nw, center ... that would be an option as well, but not sure if needed and if that would easily be adaptable for purr data and plugdata?

agraef commented 5 days ago

It's possible in purr-data as long as HTML SVG supports it. Of which I'm almost certain, but I'm not sure how much work it would be. (There's stacked lines of text in HTML SVG, but I'm not sure that the HTML text alignment styles are supported there. If they are then it should be rather trivial, otherwise you'd have to do it by calculating the bounding box of the text and applying transformation matrices.)

ben-wes commented 5 days ago

Thanks for the feedback! Looks like all options would be possible with combinations of alignment-baseline and text-anchor:

@timothyschoen : any thoughts on this? i might give the vanilla version a try.

ben-wes commented 5 days ago

adding to this: probably, horizontal alignment is more relevant in this context?

so maybe the function could have an optional second argument and expect set_text_align(horizontal_alignment, vertical_alignment) - like for example:

set_text_align(LEFT)
set_text_align(RIGHT, BOTTOM)

or set_text_align is just for horizontal alignment and we'd add set_text_align_vertical? so many options! ;)

i'd be fine with any variant (also the mode for both directions), but maybe one seems preferable from a more experienced point of view here?

timothyschoen commented 5 days ago

We could either have set_text_align, or add an extra argument to draw_text, so we'd have draw_text(x, y, w, fontheight, align = top_left), so the alignment is left by default for backward compatibility. Both are okay with me.

I'd define alignment options like this:

I'd prefer to limit it to one argument, and this should be clear enough?

ben-wes commented 4 days ago

sounds like a good option to me as well to set it directly in draw_text() as optional argument.

i'd stick with center instead of centred if fine for you since that's more common, i think. and having just center instead of center_center might not be consistent, but convenient.

to avoid misunderstandings from my side: should these be strings or values like SIGNAL and DATA?

timothyschoen commented 4 days ago

I think values are better here. I agree about using "center" instead!

ben-wes commented 4 days ago

thanks for the feedback! sorry for coming up with more and more questions here ... but i see in pd.lua that we have these definitions:

-- constants used in the signal and graphics API
DATA = 0
SIGNAL = 1
Colors = {background = 0, foreground = 1, outline = 2}

... not sure now if we would add all these alignment values on this level? but then again, i'm not sure how to implement that at all - so i'll probably have to better understand a few more parts of the code first. :)

ben-wes commented 4 days ago

see #65. we can discuss the details there if it looks good in general.