how to select and manipulate a portion of text inside a text layer using python-fu

172 Views Asked by At

I wrote a gimp python plugin to create an image, add a text layer, and write some text into it. Similar to this one. I want to apply a different formatting to a portion of this text but I can't find the function to do it when I browse Filters > Script-Fu > Console > Browser.

This gif illustrates what I want. It was done manually: enter image description here

1

There are 1 best solutions below

0
On

Very complicated. You can get the info in the "parasite" attached to the text layer:

data=layer.parasite_find('gimp-text-layer').data

So for instance, for an image like this:

enter image description here

Data looks like this:

 (markup "<markup>SO<span font=\"URW Bookman L Bold\">ME</span> <span size=\"15728\">TE</span>XT</markup>")
(font "Bungee")
(font-size 80)
(font-size-unit pixels)
(antialias yes)
(language "en")
(base-direction ltr)
(color (color-rgb 0 0 0))
(justify fill)
(box-mode dynamic)
(box-unit pixels)
(hinting yes)

Markup is possibly related to Cairo (which is the library used by Gimp to draw text and curves). However,

  1. The parasite is only available once the image has been saved to disk
  2. I have never checked what happens if you update the parasite.

Edited:

It seems creating or updating the parasite doesn't work and Gimp seems to ignored the parasite. It only considers 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).