3.5. Afegeix funcionalitats addicionals

Continueu amb la nostra formació i afegiu alguna funcionalitat al nostre script.

3.5.1. Crea una imatge nova

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. Open the Secció 12.9, «El navegador de procediments».

Comenceu fent una imatge nova. Creareu una nova variable, theImage, habilitada al resultat de cridar la funció gimp-image-new integrada al GIMP.

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 10×10 pixels 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 (script-fu-text-box inText 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
      

Nota: utilitzem el valor RGB per a especificar que la imatge és una imatge RGB. Es podria utilitzar també 0, però RGB és més descriptiu quan donem una ullada el codi.

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.

3.5.2. Afegeix una capa nova a la imatge

Ara que tenim una imatge, necessitem afegir-li una capa. Cridarem la funció gimp-layer-new per a crear la capa, passant-li l'ID de la imatge que hem creat. (A partir d'ara, en lloc de llistar la funció completa, només llistarem les línies que estem afegint. Podeu veure l'script complet ací). Ja que hem declarat les variables locals que utilitzarem, també tancarem els parèntesis al final de les nostres declaracions de les variables:

        ;create a new layer for the image:
           (theLayer
                     (car
                          (gimp-layer-new
                           theImage
                           "layer 1"
                           theImageWidth
                           theImageHeight
                           RGB-IMAGE
                           100
                           LAYER-MODE-NORMAL
                          )
                      )
            )
         ) ;end of our local variables
      

Una vegada tinguem la capa nova, cal afegir-la a la imatge:

        (gimp-image-add-layer theImage theLayer 0)
      

Ara, només per diversió, es veuran els fruits del vostre treball fins ací, afegint aquesta línia per a mostrar la imatge nova i buida:

(gimp-display-new theImage)

Deseu el vostre treball, reinicieu el GIMP, executeu l'script i apareixerà una imatge nova. Probablement contindrà deixalles (colors aleatoris), perquè no l'hem esborrat. Ho farem més tard.

3.5.3. Afegeix el text

Elimineu la línia que mostra la imatge (o poseu un comentari amb un (;) com el primer caràcter de la línia).

Abans d'afegir text a la imatge, necessitem seleccionar els colors del fons (background) i del primer pla (foreground), perquè així aparegui amb el color que l'usuari ha especificat. Utilitzarem les funcions gimp-context-set-back/foreground:

        (gimp-context-set-background '(255 255 255) )
        (gimp-context-set-foreground inTextColor)
      

Amb els colors seleccionats correctament, netegeu la brossa present en la imatge omplint la capa amb el color del fons:

        (gimp-drawable-fill theLayer BACKGROUND-FILL)
      

Amb la imatge neta, ara està llesta per a afegir-hi un text:

        (set! theText
                      (car
                           (gimp-text-fontname
                            theImage theLayer
                            0 0
                            inText
                            0
                            TRUE
                            inFontSize PIXELS
                            "Sans")
                       )
        )
      

Encara que hi ha una crida de funció llarga, és bastant més senzill si repasseu els paràmetres mentre mireu l'entrada de la funció en el navegador de procediments. Bàsicament, es crea una nova capa de text i s'assigna a la variable theText.

Ara que tenim el text, podem prendre la seva alçada i amplada, i redimensionar la imatge i la capa d'imatge per a la mida del text:

        (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)
      

És possible que us pregunteu quina és la diferència entre un dibuixable i una capa. La diferència entre els dos és que un dibuixable és alguna cosa en el que s'hi pot dibuixar, incloses capes, però també canals, màscares de capa, la selecció, etc.; una capa és una versió més específica d'un dibuixable. En la majoria dels casos, la distinció no és important.

Amb la imatge enllestida, s'hi pot tornar a afegir la línia de visualització:

        (gimp-display-new theImage)
      

Deseu el vostre treball, reinicieu el GIMP i executeu el vostre primer script!

3.5.4. Neteja el senyal brut

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.

Per a fer això, es pot netejar el senyal brut després de mostrar la imatge:

        (gimp-image-clean-all theImage)
      

Això posa a 0 el comptador del senyal brut, fent-lo aparèixer com una imatge «neta».

Afegir aquesta línia o no és qüestió del gust personal. S'utilitza en scripts que produeixin noves imatges, on els resultats són trivials, com en aquest cas. Si el vostre script és molt complicat, o si treballeu sobre una imatge existent, probablement no voldreu utilitzar aquesta funció.