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 | |
---|---|
Detta avsnitt anpassades från en handledning som skrevs till användarhandboken för GIMP 1 av Mike Terry. |
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.
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)
Now, let's practice what we have just learned. Start up GIMP, if you have not already done so, and choose
→ → . This will start up the Script-Fu Console window, which allows us to work interactively in Scheme. In a matter of moments, the Script-Fu Console will appear:At the bottom of this window is an entry-field ought to be entitled Current Command. 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.
Now, what if we wanted to add more than one number? The ”+” function can take two or more arguments, so this is not a problem:
(+ 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…
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)
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.