home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / share / gimp / 2.0 / help / en / gimp-using-script-fu-tutorial-extending-text-box.html < prev    next >
Encoding:
Extensible Markup Language  |  2008-05-03  |  12.2 KB  |  294 lines

  1. <?xml version="1.0" encoding="UTF-8" standalone="no"?>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml">
  4.   <head>
  5.     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  6.     <title>3.6.¬† Extending The Text Box Script</title>
  7.     <link rel="stylesheet" href="gimp-help-plain.css" type="text/css" />
  8.     <link rel="stylesheet" href="gimp-help-screen.css" type="text/css" />
  9.     <link rel="stylesheet" href="gimp-help-custom.css" type="text/css" />
  10.     <link rel="alternate stylesheet" href="gimp22.css" type="text/css" title="gimp22" />
  11.     <meta name="generator" content="DocBook XSL Stylesheets V1.73.2" />
  12.     <link rel="start" href="index.html" title="GNU Image Manipulation Program" />
  13.     <link rel="up" href="gimp-using-script-fu-tutorial.html" title="3.¬† A Script-Fu Tutorial" />
  14.     <link rel="prev" href="gimp-using-script-fu-tutorial-script.html" title="3.5.¬† Giving Our Script Some Guts" />
  15.     <link rel="next" href="gimp-using-script-fu-tutorial-result.html" title="3.7.¬† Your script and its working" />
  16.   </head>
  17.   <body>
  18.     <div class="navheader">
  19.       <table width="100%" summary="Navigation header">
  20.         <tr>
  21.           <th colspan="3" align="center">3.6.¬†
  22.       <span lang="en" xml:lang="en">Extending The Text Box Script</span>
  23.     </th>
  24.         </tr>
  25.         <tr>
  26.           <td width="20%" align="left"><a accesskey="p" href="gimp-using-script-fu-tutorial-script.html"><img src="../images/prev.png" alt="Prev" /></a>¬†</td>
  27.           <th width="60%" align="center">3.¬†
  28.     <span lang="en" xml:lang="en">A Script-Fu Tutorial</span>
  29.   </th>
  30.           <td width="20%" align="right">¬†<a accesskey="n" href="gimp-using-script-fu-tutorial-result.html"><img src="../images/next.png" alt="Next" /></a></td>
  31.         </tr>
  32.       </table>
  33.       <hr />
  34.     </div>
  35.     <div class="sect2" lang="en" xml:lang="en">
  36.       <div class="titlepage">
  37.         <div>
  38.           <div>
  39.             <h3 class="title"><a id="gimp-using-script-fu-tutorial-extending-text-box"></a>3.6.¬†
  40.       <span lang="en" xml:lang="en">Extending The Text Box Script</span>
  41.     </h3>
  42.           </div>
  43.         </div>
  44.       </div>
  45.       <div class="sect3" lang="en" xml:lang="en">
  46.         <div class="titlepage">
  47.           <div>
  48.             <div>
  49.               <h4 class="title"><a id="id2619882"></a>3.6.1.¬†
  50.         <span lang="en" xml:lang="en">Handling Undo Correctly</span>
  51.       </h4>
  52.             </div>
  53.           </div>
  54.         </div>
  55.         <p>
  56.         When creating a script, you want to give your users the ability to
  57.         undo their actions, should they make a mistake. This is easily
  58.         accomplished by calling the functions
  59.         <code class="code">gimp-undo-push-group-start</code>
  60.         and <code class="code">gimp-undo-push-group-end</code>
  61.         around the code that manipulates the image. You can think of them as
  62.         matched statements that let GIMP know when to start and stop recording
  63.         manipulations on the image, so that those manipulations can later be
  64.         undone.
  65.       </p>
  66.         <p>
  67.         If you are creating a new image entirely, it doesn't make sense to use
  68.         these functions because you're not changing an existing image.
  69.         However, when you are changing an existing image, you most surely want
  70.         to use these functions.
  71.       </p>
  72.         <p>
  73.         Undoing a script works nearly flawlessly when using these functions.
  74.       </p>
  75.       </div>
  76.       <div class="sect3" lang="en" xml:lang="en">
  77.         <div class="titlepage">
  78.           <div>
  79.             <div>
  80.               <h4 class="title"><a id="id2619932"></a>3.6.2.¬†
  81.         <span lang="en" xml:lang="en">Extending The Script A Little More</span>
  82.       </h4>
  83.             </div>
  84.           </div>
  85.         </div>
  86.         <p>
  87.         Now that we have a very handy-dandy script to create text
  88.         boxes, let's add two features to it:
  89.       </p>
  90.         <div class="itemizedlist">
  91.           <ul type="disc">
  92.             <li>
  93.               <p>
  94.             Currently, the image is resized to fit exactly around the
  95.             text -- there's no room for anything, like drop shadows or
  96.             special effects (even though many scripts will automatically
  97.             resize the image as necessary). Let's add a buffer around
  98.             the text, and even let the user specify how much buffer to
  99.             add as a percentage of the size of the resultant text.
  100.           </p>
  101.             </li>
  102.             <li>
  103.               <p>
  104.             This script could easily be used in other scripts that work
  105.             with text. Let's extend it so that it returns the image and
  106.             the layers, so other scripts can call this script and use
  107.             the image and layers we create.
  108.           </p>
  109.             </li>
  110.           </ul>
  111.         </div>
  112.       </div>
  113.       <div class="sect3" lang="en" xml:lang="en">
  114.         <div class="titlepage">
  115.           <div>
  116.             <div>
  117.               <h4 class="title"><a id="id2619983"></a>3.6.3.¬†
  118.         <span lang="en" xml:lang="en">
  119.           Modifying The Parameters And The Registration Function
  120.         </span>
  121.       </h4>
  122.             </div>
  123.           </div>
  124.         </div>
  125.         <p>
  126.         To let the user specify the amount of buffer, we'll add a parameter to
  127.         our function and the registration function:
  128.       </p>
  129.         <pre class="programlisting">
  130.         (define (script-fu-text-box inTest inFont inFontSize inTextColor inBufferAmount)
  131.         (let*
  132.               (
  133.                  ; define our local variables
  134.                  ; create a new image:
  135.                  (theImageWidth  10)
  136.                  (theImageHeight 10)
  137.                  (theImage (car
  138.                                 (gimp-image-new
  139.                                  theImageWidth
  140.                                  theImageHeight
  141.                                  RGB
  142.                                 )
  143.                            )
  144.                  )
  145.                  (theText)          ;a declaration for the text
  146.                                     ;we create later
  147.                  (theBuffer)        ;<span class="emphasis"><em>added</em></span>
  148.                  (theLayer
  149.                            (car
  150.                                (gimp-layer-new
  151.                                 theImage
  152.                                 theImageWidth
  153.                                 theImageHeight
  154.                                 RGB-IMAGE
  155.                                 "layer 1"
  156.                                 100
  157.                                 NORMAL
  158.                                )
  159.                            )
  160.                  )
  161.               ) ;end of our local variables
  162.          <em class="replaceable"><code>[Code here]</code></em>
  163.        )
  164.       </pre>
  165.         <pre class="programlisting">
  166.         (script-fu-register
  167.           "script-fu-text-box"                        ;func name
  168.           "Text Box"                                  ;menu label
  169.           "Creates a simple text box, sized to fit\
  170.             around the user's choice of text,\
  171.             font, font size, and color."              ;description
  172.           "Michael Terry"                             ;author
  173.           "copyright 1997, Michael Terry"             ;copyright notice
  174.           "October 27, 1997"                          ;date created
  175.           ""                     ;image type that the script works on
  176.           SF-STRING      "Text:"         "Text Box"   ;a string variable
  177.           SF-FONT        "Font:"         "Charter"    ;a font variable
  178.           SF-ADJUSTMENT  "Font size"     '(50 1 1000 1 10 0 1)
  179.                                                       ;a spin-button
  180.           SF-COLOR       "Color:"        '(0 0 0)     ;color variable
  181.           SF-ADJUSTMENT  "Buffer amount" '(35 0 100 1 10 1 0)
  182.                                                       ;a slider
  183.         )
  184.         (script-fu-menu-register "script-fu-text-box" "<Toolbox>/Xtns/Script-Fu/Text")
  185.       </pre>
  186.       </div>
  187.       <div class="sect3" lang="en" xml:lang="en">
  188.         <div class="titlepage">
  189.           <div>
  190.             <div>
  191.               <h4 class="title"><a id="id2620082"></a>3.6.4.¬†
  192.         <span lang="en" xml:lang="en">Adding The New Code</span>
  193.       </h4>
  194.             </div>
  195.           </div>
  196.         </div>
  197.         <p>
  198.         We're going to add code in two places: right before we resize
  199.         the image, and at the end of the script (to return the new
  200.         image, the layer and the text).
  201.       </p>
  202.         <p>
  203.         After we get the text's height and width, we need to resize
  204.         these values based on the buffer amount specified by the
  205.         user. We won't do any error checking to make sure it's in the
  206.         range of 0-100% because it's not life-threatening, and because
  207.         there's no reason why the user can't enter a value like "200"
  208.         as the percent of buffer to add.
  209.       </p>
  210.         <pre class="programlisting">
  211.         (set! theBuffer (* theImageHeight (/ inBufferAmount 100) ) )
  212.         (set! theImageHeight (+ theImageHeight theBuffer theBuffer) )
  213.         (set! theImageWidth  (+ theImageWidth  theBuffer theBuffer) )
  214.       </pre>
  215.         <p>
  216.         All we're doing here is setting the buffer based on the height of the
  217.         text, and adding it twice to both the height and width of our new
  218.         image. (We add it twice to both dimensions because the buffer needs to
  219.         be added to both sides of the text.)
  220.       </p>
  221.         <p>
  222.         Now that we have resized the image to allow for a buffer, we
  223.         need to center the text within the image. This is done by
  224.         moving it to the (x, y) coordinates of (<code class="varname">theBuffer</code>,
  225.         <code class="varname">theBuffer</code>). I added this line after
  226.         resizing the layer and the image:
  227.       </p>
  228.         <pre class="programlisting">
  229.         (gimp-layer-set-offsets theText theBuffer theBuffer)
  230.       </pre>
  231.         <p>
  232.         Go ahead and save your script, and try it out after refreshing the
  233.         database.
  234.       </p>
  235.         <p>
  236.         All that is left to do is return our image, the layer, and the text
  237.         layer. After displaying the image, we add this line:
  238.       </p>
  239.         <pre class="programlisting">
  240.         (list theImage theLayer theText)
  241.       </pre>
  242.         <p>
  243.         This is the last line of the function, making this list available to
  244.         other scripts that want to use it.
  245.       </p>
  246.         <p>
  247.         To use our new text box script in another script, we could write
  248.         something like the following:
  249.       </p>
  250.         <pre class="programlisting">
  251.         (set! theResult (script-fu-text-box
  252.                          "Some text"
  253.                          "Charter" "30"
  254.                          '(0 0 0)
  255.                          "35"
  256.                         )
  257.         )
  258.         (gimp-image-flatten (car theResult))
  259.       </pre>
  260.         <p>
  261.         Congratulations, you are on your way to your Black Belt of Script-Fu!
  262.       </p>
  263.       </div>
  264.     </div>
  265.     <div class="navfooter">
  266.       <hr />
  267.       <table width="100%" summary="Navigation footer">
  268.         <tr>
  269.           <td width="40%" align="left"><a accesskey="p" href="gimp-using-script-fu-tutorial-script.html"><img src="../images/prev.png" alt="Prev" /></a>¬†</td>
  270.           <td width="20%" align="center">
  271.             <a accesskey="u" href="gimp-using-script-fu-tutorial.html">
  272.               <img src="../images/up.png" alt="Up" />
  273.             </a>
  274.           </td>
  275.           <td width="40%" align="right">¬†<a accesskey="n" href="gimp-using-script-fu-tutorial-result.html"><img src="../images/next.png" alt="Next" /></a></td>
  276.         </tr>
  277.         <tr>
  278.           <td width="40%" align="left" valign="top"><a accesskey="p" href="gimp-using-script-fu-tutorial-script.html">3.5.¬†
  279.       <span lang="en" xml:lang="en">Giving Our Script Some Guts</span>
  280.     </a>¬†</td>
  281.           <td width="20%" align="center">
  282.             <a accesskey="h" href="index.html">
  283.               <img src="../images/home.png" alt="Home" />
  284.             </a>
  285.           </td>
  286.           <td width="40%" align="right" valign="top">¬†<a accesskey="n" href="gimp-using-script-fu-tutorial-result.html">3.7.¬†
  287.       <span lang="en" xml:lang="en">Your script and its working</span>
  288.     </a></td>
  289.         </tr>
  290.       </table>
  291.     </div>
  292.   </body>
  293. </html>
  294.