3. Script-Fu 作成演習

この訓練過程は Script-Fu を扱う上で必須の Scheme 言語の基礎を身につけ、 例題で便利なスクリプトを書いて道具箱に収めるまでを修めていただきます。 例題のスクリプトはユーザーに語句の入力を促し、 そのテキストの大きさにぴったり合う画像を新たに作成します。 つづいてテキストまわりに余裕をもたせる空隙をつくれるようにスクリプトを改造します。 少しずつ Script-Fu の知識が身につくよう、 ここでとりあげる議題は最小限にとどめることにしました。

[注記] 注記

This section was adapted from a tutorial written for the GIMP 1 User Manual by Mike Terry.

3.1. Scheme に精通

3.1.1. Start with Scheme

Scheme is a dialect of the Lisp family of programming languages. GIMP uses TinyScheme, which is a lightweight interpreter of a subset of the so-called R5RS standard.

最もはじめに学んでほしいことは、

Scheme 言語ではどんな構文も丸括弧 () でくくられること

そのつぎに覚えてほしいことは、

関数名や演算子は常に括弧内の先頭に置かれ、 つづくパラメーターがその関数や演算子に渡されること

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.

3 つ目にご理解いただきたいことは、

算数で使われている演算子も関数の一種とみなされており、 数式を書くときその最初に置かなくてはならないこと

です。 こちらの規則は前置記法の考え方がわかればすんなり理解できると思います。

3.1.2. 前置記法と中置記法と後置記法のそれぞれの例

この例は 前置中置後置 のそれぞれの記法の違いをかいつまんで際立たせてみました。 単純な 1 に 23 を足す計算をしています。

  • 前置記法では + 1 23 と書きます。 Scheme言語はこの方式です。

  • 中置記法では 1 + 23 と書きます。 普通の書きかたです。

  • 後置記法では 1 23 + と書きます。 多数の HP 社製電卓がこの方式です。 [1 と 23 とを 足す という日本語文によく馴染む記法です。]

3.1.3. 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 コンソールのウィンドウ

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)

このように記入したら Enter を押して送ると期待通りの 8 という答えが中央のウィンドウに表示されます。

図13.1 Script-Fu コンソールを使う

Script-Fu コンソールを使う

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

(+ 3 5 6)

この式も期待通りの答えとなる 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. 余計な括弧に注意しよう

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 = ?

前置記法は+記号を数値の羅列の先頭に置くのですから、 書き直せばつぎのようにするのではないでしょうか。

(+ 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.

正しい記法はつぎのようにします。

(+ 3 (+ 5 6) 7)

3.1.6. 空白も適切に入れるよう気をつけよう

C/C++ や Perl や Java のような他のプログラミング言語に慣れ親しんだ人なら演算子の周囲に空白をどう置くか注意しなくても数式は適切に書けると思っているはずです。

        3+5, 3 +5, 3+ 5
      

C/C++ と Perl と Java のコンパイラーは上の数式のいずれも受け入れます。 ところが同じ規則が Scheme には通用しません。 Scheme では数式記号や関数名や演算子のあとに空白を置かなければ Scheme インタープリターに正しく解釈してもらえません。

Script-Fu コンソールを使ってもうすこしの間簡単な数式で練習を積み、 以上の基本的な構えにすっかり慣れるようにしてください。