File: ch02s10s05.html

package info (click to toggle)
gimp-help 2%2B0.7-5
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 30,852 kB
  • ctags: 4
  • sloc: xml: 104,248; sh: 544; makefile: 262; perl: 42
file content (316 lines) | stat: -rw-r--r-- 12,594 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>10.5. Giving Our Script Some Guts</title>
    <link rel="stylesheet" href="gimp-help-plain.css" type="text/css" />
    <link rel="stylesheet" href="gimp-help-screen.css" type="text/css" />
    <meta name="generator" content="DocBook XSL Stylesheets V1.66.1" />
    <link rel="start" href="index.html" title="&#10;      &#10;    " />
    <link rel="up" href="ch02s10.html" title="10. A Script-Fu Tutorial" />
    <link rel="prev" href="ch02s10s04.html" title="10.4. Your First Script-Fu Script" />
    <link rel="next" href="ch02s10s06.html" title="10.6. Extending The Text Box Script" />
  </head>
  <body>
    <div xmlns="" class="navheader">
      <table width="100%" summary="Navigation header">
        <tr>
          <th colspan="3" align="center" id="chaptername">10.5. Giving Our Script Some Guts</th>
        </tr>
        <tr>
          <td width="20%" align="left"><a accesskey="p" href="ch02s10s04.html">Prev</a> </td>
          <th width="60%" align="center" id="sectionname">10.5. Giving Our Script Some Guts</th>
          <td width="20%" align="right"> <a accesskey="n" href="ch02s10s06.html">Next</a></td>
        </tr>
      </table>
      <hr />
    </div>
    <div class="sect2" lang="en" xml:lang="en">
      <div class="titlepage">
        <div>
          <div>
            <h3 class="title"><a id="id3317774"></a>10.5. Giving Our Script Some Guts</h3>
          </div>
        </div>
      </div>
      <p>
      Let us continue with our training and add some functionality to
      our script. 
    </p>
      <div class="simplesect" lang="en" xml:lang="en">
        <div class="titlepage">
          <div>
            <div>
              <h4 class="title"><a id="id3318345"></a>Creating A New Image</h4>
            </div>
          </div>
        </div>
        <p>
        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. 
      </p>
        <p>
        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'! 
      </p>
        <p>
        Let's begin by making a new image. We'll create a new
        variable, <tt class="varname">theImage</tt>, set to the result of calling Gimp's
        built-in function <tt class="code">gimp-image-new</tt>. 
      </p>
        <p>
  	As you can see from the DB Browser, the function
  	<tt class="code">gimp-image-new</tt> 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. 
      </p>
        <pre class="programlisting">
        (define (script-fu-text-box inTest inFont inFontSize inTextColor)
        (let*
              (
                 ; define our local variables
                 ; create a new image:
                 (theImageWidth  10)
                 (theImageHeight 10)
                 (theImage (car 
                                (gimp-image-new
                                 theImageWidth
                                 theImageHeight
                                 RGB
                                )
                           )
                 )
                 (theText)     ;a declaration for the text
                               ;we create later
      </pre>
        <p>
        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. 
      </p>
        <p>
        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. 
      </p>
      </div>
      <div class="simplesect" lang="en" xml:lang="en">
        <div class="titlepage">
          <div>
            <div>
              <h4 class="title"><a id="id3318135"></a>Adding A New Layer To The Image</h4>
            </div>
          </div>
        </div>
        <p>
        Now that we have an image, we need to add a layer to it. We'll
        call the <tt class="code">gimp-layer-new</tt> 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: 
      </p>
        <pre class="programlisting">
        ;create a new layer for the image:
           (theLayer 
                     (car
                          (gimp-layer-new
                           theImage
                           theImageWidth
                           theImageHeight
                           RGB_IMAGE
                           "layer 1"
                           100
                           NORMAL
                          )
                      )
            )
         ) ;end of our local variables
      </pre>
        <p>
        Once we have the new layer, we need to add it to the image:
      </p>
        <pre class="programlisting">
       (gimp-image-add-layer theImage theLayer 0)
      </pre>
        <p>
        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: 
      </p>
        <pre class="programlisting">
        (gimp-display-new theImage)
      </pre>
        <p>
       Save your work, select 
       <span class="guimenu">Xtns</span>-&gt;<span class="guisubmenu">Script-Fu</span>-&gt;<span class="guimenuitem">Refresh</span>, 
       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. 
      </p>
      </div>
      <div class="simplesect" lang="en" xml:lang="en">
        <div class="titlepage">
          <div>
            <div>
              <h4 class="title"><a id="id3318268"></a>Adding The Text</h4>
            </div>
          </div>
        </div>
        <p>
        Go ahead and remove the line to display the image (or comment
        it out with a ; as the first character of the line). 
      </p>
        <p>
        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-palette-set-back/foreground functions: 
      </p>
        <pre class="programlisting">
        (gimp-palette-set-background '(255 255 255) )
        (gimp-palette-set-foreground inTextColor)
      </pre>
        <p>
        With the colors properly set, let's now clean out the garbage
        currently in the image. We'll select everything in the image,
        and call clear: 
      </p>
        <pre class="programlisting">
        (gimp-selection-all theImage)
        (gimp-edit-clear theImage theLayer)
        (gimp-selection-none theImage)
      </pre>
        <p>
        With the image cleared, we're ready to add some text:
      </p>
        <pre class="programlisting">
        (set! theText
                      (car
                           (gimp-text
                            theImage
                            theLayer
                            0 0
                            inText
                            0
                            TRUE
                            inFontSize
                            PIXELS
                            "*" "*" "*"
                           )
                       )
        )  
      </pre>
        <p>
        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
        <tt class="varname">theText</tt>.  
      </p>
        <p>
        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: 
      </p>
        <pre class="programlisting">
        (set! theImageWidth   (car (gimp-drawable-width  theText) ) )  	
        (set! theImageHeight  (car (gimp-drawable-height theText) ) )

        (gimp-image-resize theImage theImageWidth theImageHeight 0 0)	
  	
        (gimp-layer-resize theLayer theImageWidth theImageHeight 0 0)
      </pre>
        <p>
        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. 
      </p>
        <p>
       With the image ready to go, we can now re-add our display line:
      </p>
        <pre class="programlisting">
        (gimp-display-new theImage)
      </pre>
        <p>
        Save your work, refresh the database and give your first
        script a run!
      </p>
      </div>
      <div class="simplesect" lang="en" xml:lang="en">
        <div class="titlepage">
          <div>
            <div>
              <h4 class="title"><a id="id3318304"></a>Clearing The Dirty Flag</h4>
            </div>
          </div>
        </div>
        <p>
        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. 
      </p>
        <p>
        To do this, we can clear the dirty flag after displaying the
        image: 
      </p>
        <pre class="programlisting">
        (gimp-image-clean-all theImage)
      </pre>
        <p>
        This will set dirty count to 0, making it appear to be a
        "clean" image. 
      </p>
        <p>
        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. 
      </p>
      </div>
    </div>
    <div class="navfooter">
      <hr />
      <table width="100%" summary="Navigation footer">
        <tr>
          <td width="40%" align="left"><a accesskey="p" href="ch02s10s04.html">Prev</a> </td>
          <td width="20%" align="center">
            <a accesskey="u" href="ch02s10.html">Up</a>
          </td>
          <td width="40%" align="right"> <a accesskey="n" href="ch02s10s06.html">Next</a></td>
        </tr>
        <tr>
          <td width="40%" align="left" valign="top">10.4. Your First Script-Fu Script </td>
          <td width="20%" align="center">
            <a accesskey="h" href="index.html">Home</a>
          </td>
          <td width="40%" align="right" valign="top"> 10.6. Extending The Text Box Script</td>
        </tr>
      </table>
    </div>
  </body>
</html>