Switch to full style
Discuss general Gimp topics here
Post a reply

manipulate a portion of text inside a text layer using python-fu

Mon Dec 12, 2022 8:12 pm

I'm writing a python plugin that writes some text into my layer and I would really like to select a portion of that text and apply a different formatting to it, like changing the font-size and its color.

I heard a little bit about the concept of parasites, which from my understanding is the metadata associated with my .xcf. When I do this formatting manually I can see this parasite attached to the text as a markup, but I can't find the right way to create a parasite and attach it to my text layer. I've been browsing the "Python Procedure Browser" but this part is a little blurred.

Re: manipulate a portion of text inside a text layer using python-fu

Tue Dec 13, 2022 3:54 am

delki8 wrote:I'm writing a python plugin that writes some text into my layer and I would really like to select a portion of that text and apply a different formatting to it, like changing the font-size and its color.

I heard a little bit about the concept of parasites, which from my understanding is the metadata associated with my .xcf. When I do this formatting manually I can see this parasite attached to the text as a markup, but I can't find the right way to create a parasite and attach it to my text layer. I've been browsing the "Python Procedure Browser" but this part is a little blurred.


To add a parasite:

Code:
         p=gimp.Parasite('some_name',0,some_data)
         textLayer.parasite_attach(p)


However the parasite is ignored by Gimp, which only looks at it when loading the file. And when the file is saved, it generates a new parasite from the actual layer contents.

It may be easier for you to format several individual text layers side-by-side. To align them to the same baseline: if you use "pdb.gimp_text_get_extents_fontname()" with a character that has a flat bottom (I typically use "X") the "ascent" is the distance of the baseline from the top of the layer (round character such as "O" can extent slightly under the baseline).

Re: manipulate a portion of text inside a text layer using python-fu

Thu Dec 15, 2022 5:17 am

Thank you ofnuts, I had thought about aligning them word by word but I thought this would be too tedious to implement consistently, so I left the part of increasing the font size to be run manually. The script I'm writing performs a few other tasks so this one in specific will be considered a trade-off to have the rest of it working automatically.

Thank you for the time you've put into your solution proposal.
Post a reply