49
choose Xtns Script-Fu Refresh Scripts , this new script will appear as Xtns Script-Fu Text
Text Box .
If you invoke this new script, it won't do anything, of course, but you can view the
prompts you created when registering the script (more information about what we did is
covered next).
Finally, if you invoke the Procedure Browser -- Xtns Procedure Browser ), you'll notice that
our script now appears in the database.
Steps For Registering The Script
To register our script with Gimp, we call the function script-fu-register, fill in the seven
required parameters and add our script's own parameters, along with a description and
default value for each parameter.
The Required Parameters
• The name of the function we defined. This is the function called when our script is
invoked (the entry-point into our script). This is necessary because we may define
additional functions within the same file, and Gimp needs to know which of these
functions to call. In our example, we only defined one function, text-box, which we
registered.
• The location in the menu where the script will be inserted. The exact location of the
script is specified like a path in Unix, with the root of the path being either toolbox or
right-click.
If your script does not operate on an existing image (and thus creates a new image,
like our Text Box script will), you'll want to insert it in the toolbox menu -- this is the
menu in Gimp's main window (where all the tools are located: the selection tools,
magnifying glass, etc.).
If your script is intended to work on an image being edited, you'll want to insert it in
the menu that appears when you right-click on an open image. The rest of the path
points to the menu lists, menus and sub-menus. Thus, we registered our Text Box
script in the Text menu of the Script-Fu menu of the Xtns menu of the toolbox ( Xtns
Script-Fu Text Text Box ).
If you notice, the Text sub-menu in the Script-Fu menu wasn't there when we began
-- Gimp automatically creates any menus not already existing.
• A description of your script, to be displayed in the Procedure Browser.
• Your name (the author of the script).
• Copyright information.
• The date the script was made, or the last revision of the script.
• The types of images the script works on. This may be any of the following: RGB,
RGBA, GRAY, GRAYA, INDEXED, INDEXEDA. Or it may be none at all -- in our case,
we're creating an image, and thus don't need to define the type of image on which
we work.
Registering The Script's Parameters
Once we have listed the required parameters, we then need to list the parameters that
correspond to the parameters our script needs. When we list these params, we give hints
as to what their types are. This is for the dialog which pops up when the user selects our
script. We also provide a default value.
This section of the registration process has the following format:
GNU Image Manipulation Program
Generated by docbook2odf
Page 92 of 421
Generated by docbook2odf
70
Param Type
Description
Example
SF-VALUE
Accepts numbers and
strings. Note that quotes
must be escaped for default
text, so better use SF-
STRING.
42
SF-STRING
Accepts strings.
"Some text"
SF-COLOR
Indicates that a color is
requested in this parameter.
'(0 102 255)
SF-TOGGLE
A checkbox is displayed, to
get a Boolean value.
TRUE or FALSE
SF-IMAGE
If your script operates on an
open image, this should be
the first parameter after the
required parameters. Gimp
will pass in a reference to
the image in this parameter.
3
SF-DRAWABLE
If your script operates on an
open image, this should be
the second parameter after
the SF-IMAGE param. It
refers to the active layer.
Gimp will pass in a reference
to the active layer in this
parameter.
17
Giving Our Script Some Guts
Let us continue with our training and add some functionality to our script.
Creating A New Image
In the previous lesson, we created an empty function and registered it with Gimp. In this
lesson, we want to provide functionality to our script -- we want to create a new image,
add the user's text to it and resize the image to fit the text exactly.
Once you know how to set variables, define functions and access list members, the rest is
all downhill -- all you need to do is familiarize yourself with the functions available in
Gimp's procedural database and call those functions directly. So fire up the DB Browser
and let's get cookin'!
Let's begin by making a new image. We'll create a new variable, theImage, set to the
result of calling Gimp's built-in function gimp-image-new.
As you can see from the DB Browser, the function gimp-image-new takes three
parameters -- the image's width, height and the type of image. Because we'll later resize
the image to fit the text, we'll make a 10x10 RGB image. We'll store the image's width
and sizes in some variables, too, as we'll refer to and manipulate them later in the script.
(define (scriptfutextbox inText inFont inFontSize inTextColor)
(let*
(
GNU Image Manipulation Program
Generated by docbook2odf
Page 93 of 421
Generated by docbook2odf
117
; define our local variables
; create a new image:
(theImageWidth 10)
(theImageHeight 10)
(theImage (car
(gimpimagenew
theImageWidth
theImageHeight
RGB
)
)
)
(theText)
;a declaration for the text
;we create later
Note: We used the value RGB to specify that the image is an RGB image. We could have
also used 0, but RGB is more descriptive when we glance at the code.
You should also notice that we took the head of the result of the function call. This may
seem strange, because the database explicitly tells us that it returns only one value -- the
ID of the newly created image. However, all Gimp functions return a list, even if there is
only one element in the list, so we need to get the head of the list.
Adding A New Layer To The Image
Now that we have an image, we need to add a layer to it. We'll call the gimp-layer-new
function to create the layer, passing in the ID of the image we just created. (From now on,
instead of listing the complete function, we'll only list the lines we're adding to it. You can
see the complete script here.) Because we've declared all of the local variables we'll use,
we'll also close the parentheses marking the end of our variable declarations:
;create a new layer for the image:
(theLayer
(car
(gimplayernew
theImage
theImageWidth
theImageHeight
RGBIMAGE
"layer 1"
100
NORMAL
)
)
)
) ;end d of f our local variables
Once we have the new layer, we need to add it to the image:
(gimpimageaddlayer theImage theLayer 0)
Now, just for fun, let's see the fruits of our labors up until this point, and add this line to
show the new, empty image:
(gimpdisplaynew theImage)
Save your work, select Xtns Script-Fu Refresh Scripts , run the script and a new image
should pop up. It will probably contain garbage (random colors), because we haven't
erased it. We'll get to that in a second.
GNU Image Manipulation Program
Generated by docbook2odf
Page 94 of 421
Generated by docbook2odf
100
Adding The Text
Go ahead and remove the line to display the image (or comment it out with a ; as the first
character of the line).
Before we add text to the image, we need to set the background and foreground colors so
that the text appears in the color the user specified. We'll use the gimp-context-set-
back/foreground functions:
(gimpcontextsetbackground '(255 255 255) )
(gimpcontextsetforeground inTextColor)
With the colors properly set, let's now clean out the garbage currently in the image by
filling the drawable with the background color:
(gimpdrawablefill theLayer BACKGROUNDFILL)
With the image cleared, we're ready to add some text:
(set! theText
(car
(gimptextfontname
theImage theLayer
0 0
inText
0
TRUE
inFontSize PIXELS
"Sans")
)
)
Although a long function call, it's fairly straightforward if you go over the parameters
while looking at the function's entry in the DB Browser. Basically, we're creating a new
text layer and assigning it to the variable theText.
Now that we have the text, we can grab its width and height and resize the image and
the image's layer to the text's size:
(set! theImageWidth
(car (gimpdrawablewidth theText) ) ) )
(set! theImageHeight (car r (gimpdrawableheight theText) ) ) )
(gimpimageresize theImage theImageWidth theImageHeight 0 0)
(gimplayerresize theLayer theImageWidth theImageHeight 0 0)
If you're like me, you're probably wondering what a drawable is when compared to a
layer. The difference between the two is that a drawable is anything that can be drawn
into, including layers but also channels, layer masks, the selection, etc; a layer is a more
specific version of a drawable. In most cases, the distinction is not important.
With the image ready to go, we can now re-add our display line:
(gimpdisplaynew theImage)
Save your work, refresh the database and give your first script a run!
GNU Image Manipulation Program
Generated by docbook2odf
Page 95 of 421
Generated by docbook2odf
56
Clearing The Dirty Flag
If you try to close the image created without first saving the file, Gimp will ask you if you
want to save your work before you close the image. It asks this because the image is
marked as dirty, or unsaved. In the case of our script, this is a nuisance for the times
when we simply give it a test run and don't add or change anything in the resulting image
-- that is, our work is easily reproducible in such a simple script, so it makes sense to get
rid of this dirty flag.
To do this, we can clear the dirty flag after displaying the image:
(gimpimagecleanall theImage)
This will set dirty count to 0, making it appear to be a "clean" image.
Whether to add this line or not is a matter of personal taste. I use it in scripts that
produce new images, where the results are trivial, as in this case. If your script is very
complicated, or if it works on an existing image, you will probably not want to use this
function.
Extending The Text Box Script
Handling Undo Correctly
When creating a script, you want to give your users the ability to undo their actions,
should they make a mistake. This is easily accomplished by calling the functions gimp-
undo-push-group-start and gimp-undo-push-group-end around the code that manipulates
the image. You can think of them as matched statements that let Gimp know when to
start and stop recording manipulations on the image, so that those manipulations can
later be undone.
If you are creating a new image entirely, it doesn't make sense to use these functions
because you're not changing an existing image. However, when you are changing an
existing image, you most surely want to use these functions.
Undoing a script works nearly flawlessly when using these functions.
Extending The Script A Little More
Now that we have a very handy-dandy script to create text boxes, let's add two features
to it:
• Currently, the image is resized to fit exactly around the text -- there's no room for
anything, like drop shadows or special effects (even though many scripts will
automatically resize the image as necessary). Let's add a buffer around the text, and
even let the user specify how much buffer to add as a percentage of the size of the
resultant text.
• This script could easily be used in other scripts that work with text. Let's extend it so
that it returns the image and the layers, so other scripts can call this script and use
the image and layers we create.
Modifying The Parameters And The Registration Function
To let the user specify the amount of buffer, we'll add a parameter to our function and the
registration function:
(define (scriptfutext
box inTest inFont inFontSize inTextColor inBufferAmount)
GNU Image Manipulation Program
Generated by docbook2odf
Page 96 of 421
Generated by docbook2odf
218
(let*
(
; define our local variables
; create a new image:
(theImageWidth 10)
(theImageHeight 10)
(theImage (car
(gimpimagenew
theImageWidth
theImageHeight
RGB
)
)
)
(theText)
;a declaration for r the e text
;we create later
(theBuffer)
;added
(theLayer
(car
(gimplayernew
theImage
theImageWidth
theImageHeight
RGBIMAGE
"layer 1"
100
NORMAL
)
)
)
) ;end of our r local l variables
[Code here]
)
(scriptfuregister
"scriptfutextbox"
;func name
"Text Box"
;menu label
"Creates a simple text box, sized to fit\
around the user's choice of text,\
font, font size, , and d color."
;description
"Michael Terry"
;author
"copyright 1997, Michael Terry"
;copyright notice
"October 27, 1997"
;date created
""
;image type that the script works s on
SFSTRING
"Text:"
"Text Box"
;a string variable
SFFONT
"Font:"
"Charter"
;a font variable
SFADJUSTMENT "Font t size"
'(50 1 1000 1 10 0 1)
;a spinbutton
SFCOLOR
"Color:"
'(0 0 0)
;color variable
SFADJUSTMENT "Buffer r amount" '(35 0 100 0 1 1 10 0 1 0)
;a slider
)
(scriptfumenuregister "scriptfutextbox" "<Toolbox>/Xtns/Script
Fu/Text")
Adding The New Code
We're going to add code in two places: right before we resize the image, and at the end of
the script (to return the new image, the layer and the text).
GNU Image Manipulation Program
Generated by docbook2odf
Page 97 of 421
Generated by docbook2odf
82
After we get the text's height and width, we need to resize these values based on the
buffer amount specified by the user. We won't do any error checking to make sure it's in
the range of 0-100% because it's not life-threatening, and because there's no reason why
the user can't enter a value like "200" as the percent of buffer to add.
(set! theBuffer (* theImageHeight (/ inBufferAmount t 100) ) ) )
(set! theImageHeight (+ theImageHeight theBuffer theBuffer) )
(set! theImageWidth (+ + theImageWidth theBuffer r theBuffer) )
All we're doing here is setting the buffer based on the height of the text, and adding it
twice to both the height and width of our new image. (We add it twice to both dimensions
because the buffer needs to be added to both sides of the text.)
Now that we have resized the image to allow for a buffer, we need to center the text
within the image. This is done by moving it to the (x, y) coordinates of (theBuffer,
theBuffer). I added this line after resizing the layer and the image:
(gimplayersetoffsets theText theBuffer r theBuffer)
Go ahead and save your script, and try it out after refreshing the database.
All that is left to do is return our image, the layer, and the text layer. After displaying the
image, we add this line:
(list theImage theLayer theText)
This is the last line of the function, making this list available to other scripts that want to
use it.
To use our new text box script in another script, we could write something like the
following:
(set! theResult (scriptfutextbox
"Some text"
"Charter" "30"
'(0 0 0 0)
"35"
)
)
(gimpimageflatten (car theResult))
Congratulations, you are on your way to your Black Belt of Script-Fu!
GNU Image Manipulation Program
Generated by docbook2odf
Page 98 of 421
Generated by docbook2odf
25
Creating Shortcuts to Menu Functions
Many functions which are accessible via the image menu have a default keyboard
shortcut. You may want to create a new shortcut for a command that you use a lot and
doesn't have one or, more rarely, edit an existing shortcut. There are two methods for
doing this.
First, you have to activate this capability by checking the Use dynamic keyboard
shortcuts option in the Interface item of the Preferences menu. This option is usually not
checked, to prevent accidental key presses from creating an unwanted shortcut.
While you're doing that, also check the Save keyboard shortcuts on exit option so that
your shortcut will be saved.
To create a keyboard shortcut, simply place the mouse pointer on a command in the
menu: it will then be highlighted. Be careful that the mouse pointer doesn't move and
type a sequence of three keys, keeping the keys pressed. You will see this sequence
appear on the right of the command.
If you type a sequence which has already been used, the command which is associated
with this shortcut will be executed and no new shortcut will be created.
It is best to use the CtrlAltKey sequence for your custom shortcuts.
You get to this Editor by clicking on Configure keyboard shortcuts in the "Interface" item
of the Preferences menu.
As shown in this dialog, you can select the command you want to create a shortcut for, in
the "Action" area. Then you type your key sequence as above. In principle, the Space bar
GNU Image Manipulation Program
Generated by docbook2odf
Page 99 of 421
Generated by docbook2odf
12
should clear a shortcut. (In practice, it clears it, but doesn't delete it.)
This shortcut editor also allows you to control the tool parameter settings with the
keyboard. At the top of this dialog, you can find a Context menu that takes you to the tool
parameters. To make your work easier, tool types are marked with small icons.
Custom Keyboard shortcuts are stored in one of Gimp's hidden directory
(/home/[username]/.gimp2.2/menurc) under Linux, and C:\Documentsand
Settings\[Username]\.gimp2.2\menurc under Windows XP. It is a simple text file that
you can transport from one computer to another.
GNU Image Manipulation Program
Generated by docbook2odf
Page 100 of 421
Generated by docbook2odf
Documents you may be interested
Documents you may be interested