3. En Script-Fu-handledning

I denna träningskurs introducerar vi grunderna i Scheme som är nödvändiga för att använda Script-Fu, och bygger sedan ett praktiskt skript som du kan lägga till i din verktygslåda med skript. Skriptet efterfrågar text från användaren, och skapar sedan en ny bild vars storlek perfekt passar texten. Vi kommer sedan förbättra skriptet så det tillåter en utrymmesbuffert runt texten. Vi kommer avsluta med några förslag om hur du kan förbättra din kunskap om Script-Fu.

[Notera] Notera

Detta avsnitt anpassades från en handledning som skrevs till användarhandboken för GIMP 1 av Mike Terry.

3.1. Bekanta dig med Scheme

3.1.1. Börja med Scheme

Scheme är en dialekt av Lisp-familjen av programspråk. GIMP använder TinyScheme som är en lättviktstolk för en delmängd av den så kallade R5RS-standarden.

Den första saken du behöver lära dig är att:

Varje sats i Scheme omgivs av parenteser ().

Den andra saken du behöver veta är att:

Funktionsnamnet/operatorn är alltid det första objektet i parenteserna, och resten av objekten är parametrar till funktionen.

However, not everything enclosed in parentheses is a function — they can also be items in a list — but we'll get to that later. This notation is referred to as prefix notation, because the function prefixes everything else. If you're familiar with postfix notation, or own a calculator that uses Reverse Polish Notation (such as most HP calculators), you should have no problem adapting to formulating expressions in Scheme.

Den tredje saken att förstå är att:

Mathematical operators are also considered functions, and thus are listed first when writing mathematical expressions.

This follows logically from the prefix notation that we just mentioned.

3.1.2. Examples Of Prefix, Infix, And Postfix Notations

Here are some quick examples illustrating the differences between prefix, infix, and postfix notations. We'll add a 1 and 23 together:

  • Prefix notation: + 1 23 (the way Scheme will want it)

  • Infix notation: 1 + 23 (the way we normally write it)

  • Postfix notation: 1 23 + (the way many HP calculators will want it)

3.1.3. Practicing In Scheme

In GIMP, select FiltersDevelopmentScript-FuScript-Fu Console from the main menu. This will start up the Script-Fu Console window, which allows us to work interactively in Scheme.

3.1.4. Script-Fu-konsolfönstret

At the bottom of this window is a text entry field for commands. Here, we can test out simple Scheme commands interactively. Let's start out easy, and add some numbers:

(+ 3 5)

Typing this in and hitting Enter yields the expected answer of 8 in the center window.

Figur 13.1. Använd Script-Fu-konsolen.

Använd Script-Fu-konsolen.

The + function can take more arguments, so we can add more than one number:

(+ 3 5 6)

Detta ger också det förväntade svaret 14.

So far, so good — we type in a Scheme statement and it's executed immediately in the Script-Fu Console window. Now for a word of caution…

3.1.5. Se upp för extra parenteser

If you're like me, you're used to being able to use extra parentheses whenever you want to — like when you're typing a complex mathematical equation and you want to separate the parts by parentheses to make it clearer when you read it. In Scheme, you have to be careful and not insert these extra parentheses incorrectly. For example, say we wanted to add 3 to the result of adding 5 and 6 together:

3 + (5 + 6) + 7 = ?

Knowing that the + operator can take a list of numbers to add, you might be tempted to convert the above to the following:

(+ 3 (5 6) 7)

However, this is incorrect — remember, every statement in Scheme starts and ends with parens, so the Scheme interpreter will think that you're trying to call a function named 5 in the second group of parens, rather than summing those numbers before adding them to 3.

The correct way to write the above statement would be:

(+ 3 (+ 5 6) 7)

3.1.6. Make Sure You Have The Proper Spacing, Too

If you are familiar with other programming languages, like C/C++, Perl or Java, you know that you don't need white space around mathematical operators to properly form an expression:

        3+5, 3 +5, 3+ 5

These are all accepted by C/C++, Perl and Java compilers. However, the same is not true for Scheme. You must have a space after a mathematical operator (or any other function name or operator) in Scheme for it to be correctly interpreted by the Scheme interpreter.

Practice a bit with simple mathematical equations in the Script-Fu Console until you're totally comfortable with these initial concepts.