Page 1 of 1

Text box layer centering in python-fu

Posted: 24 Jun 2019, 16:29
by vndep
Any thoughts on an efficient way to center a textbox in a python script?

For background, the script starts with an image and adds a layer of text. I want to make sure that the center of the textbox aligns with a specific X,Y coordinate in the image. To clarify, I am not thinking about left/center/right justification within the textbox.

I do not know in advance what text will be in the textbox. It could be of any length and include narrow characters (such as "i") or wide characters (such as "W")

My original thought was to somehow grab the X,Y coordinates of the four corners of the textbox (or maybe height and length) and then do some algebra to figure out to where I should move the textbox.

The only function that seems relevant is pdb.gimp_selection_bounds; however the parameter for that function is an image rather than a layer.

Anticipating a follow up question, I am not having any luck finding the python function that would allow me to move the textbox, either.

Thanks in advance!

Re: Text box layer centering in python-fu

Posted: 24 Jun 2019, 18:58
by tim
if you use fontname function to create that layer of text the layer will be sized based on your text so it's already a good measure of your text.width and height.
That text layer will have layer.width and layer.height.
Which you could use to move around using offsets (just search for "offsets" in procedure browser in GIMP).
two functions should show up. One is to get the offsets.
The other is to set the offsets of the layer.
Use the one to set the offsets which is of the top left corner of layer.
That should be sufficient. If you want even more accuracy you can use gimp_text_get_extents_fontname but I usually get away with using created text layers width and height.

Re: Text box layer centering in python-fu

Posted: 24 Jun 2019, 19:40
by vndep
That totally works! Thanks!

Re: Text box layer centering in python-fu

Posted: 25 Jun 2019, 08:12
by Mapsking
Out of curiosity, couldn't you program it to use the built-in alignment tool?

Re: Text box layer centering in python-fu

Posted: 25 Jun 2019, 13:54
by tim
I don't think so as I type "align" into procedure browser, only one method showed up and I don't think it allows you to use it like the front end alignment tool.