Post Reply 

Vertical Offset of Text from Text Layer

Oct 1, 2013, 12:53
Post: #1
Vertical Offset of Text from Text Layer
Hi all,

Gimp 2.8.6

I'm trying to make a python plugin that inserts some text (with size defined in pixels) and then vertically centers it in a region. Things are generally going well, but I've hit a snag.


Hopefully the attachment shows it clearly. There are 3 different text layers, all of which have the same vertical position (as shown by the red line). As you can see, the spacing between the text and the top of the text layer varies with font size.

Is this spacing something I can calculate from the font size and/or from the font extents? Should it vary with fonts (I've tried a few and it seems consistent, but maybe I'm just getting lucky)?

Find all posts by this user
Quote this message in a reply
Oct 2, 2013, 07:38 (This post was last modified: Oct 2, 2013 10:41 by C$$.)
Post: #2
RE: Vertical Offset of Text from Text Layer
(Oct 1, 2013 15:46)ofnuts Wrote:  Not really because this will depend on your text... So aligning the top of the text is difficult(*).

Ah, but what is life without challenges?

In case anyone else wants to try this, here is what I came up with.
Assuming you have the text layer of interest stored in a variable called textLayer and the image stored in a variable called myImage, the following code should get you the y-position of the top pixel of the tallest text (as indicated by the red arrow in the graphic).


With this point, you can then figure out the offset to vertically align your text. It works with GIMP 2.8.6, but I have no idea if it will play nice with other versions.

textVectors = pdb.gimp_vectors_new_from_text_layer(myImage, textLayer)

#list comprehension with some slicing, kind of nasty
#pulls out just the y anchor values of each anchor point in every path of every stroke
yValues = [path.points[0][3::6] for path in textVectors.stroke]

#yValues is a list of lists, so flatten it
yValues = [value for sublist in yValues for value in sublist]

#find the min value
yPosition = min(yValues)

I figured out the points structure from this site

At first I couldn't understand why the ascent went above the tops of capital letters. Then I realized it was for accents, like umlaut (as shown in the picture).

Thanks for your help!
Find all posts by this user
Quote this message in a reply
Oct 2, 2013, 09:55
Post: #3
RE: Vertical Offset of Text from Text Layer
(Oct 2, 2013 08:24)ofnuts Wrote:  Your algorithm isn't correct because you are only looking at the anchor points. Counter-example:

Hahaha, but isn't this your algorithm? Wink

(Oct 1, 2013 15:46)ofnuts Wrote:  (*) but not impossible: obtain the path from the text layers, and check the smallest(**) Y values in the anchor points in the strokes

But yes, I would think you are right. it's not hard to imagine a situation where a spline curves above the anchors.

Although, for my application, it seems to work alright. The paths have enough points that some of the anchors are invariably on the top of the text. I don't know if this is a function of the font I'm using, the text size, or something else. I would guess there is some way to set the number of points you want in a path, but so far that setting eludes me.

(Oct 2, 2013 08:24)ofnuts Wrote:  If you are into python and Gimp paths, I have a a collection of scripts there where you can get some sample code:

Thank you for sharing these scripts. I need to move forward with my 90% solution, but I plan on coming back and nailing this down 100% in the near future.

Find all posts by this user
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Script-fu: How to create new, addressable layer using gimp-floating-sel-to-layer Bertholom√§us 1 464 Aug 31, 2017 14:32
Last Post: Bertholomäus
  Setting text markup from python script Burbly 1 617 Sep 16, 2014 12:29
Last Post: Burbly
  Accented characters wihtin "text string" prevent script from loading Shadowes 1 435 Aug 28, 2014 15:37
Last Post: Shadowes
  Change menuitem text in plugin Barbie 1 420 Aug 11, 2014 03:16
Last Post: Barbie
  Getting Layer Effects on GIMP 2.8 for Mac Q A 4 1,377 May 28, 2014 23:58
Last Post: Esper

Forum Jump:

GIMP ForumPortalArchiveContactTermsRSS