[Part I. GIMP基礎功]

[Part II. 一種效果的誕生]
[Part III. Script-Fu的秘密]

22.5 程式news-text.scm分三部份

  • 若用PSPad開啟程式檔(或任何文字編輯器),看到的Script-Fu程式碼,變數會對得很整齊。
  • 下圖顯示,括號的對齊,括號內的東西,是屬於 let* 函數的參數。


  • HTML會把大量的空白省略,因此程式碼的排版變成了下面這樣子。
  • 我替Script-Fu程式碼加上行號乎,方便讀者閱讀。

程式news-text.scm分三部份:
  • 1~21行:註解,功能說明與版權聲明。
  • 22~75行:定義函式,在測試程式的過程中,可將它改為 (define (script-fu-newsprint-text2 … ))
  • 76~95行:註冊函式,可將它改為(script-fu-register "script-fu-newsprint-text2" … )

第1到第21行:註解說明
1: ; Newsprint text
2: ; Copyright (c) 1998 Austin Donnelly <austin@greenend.org.uk>
3: ;
4: ;
5: ;Based on alien glow code from Adrian Likins
6: ;
7: ; This program is free software; you can redistribute it and/or modify
8: ; it under the terms of the GNU General Public License as published by
9: ; the Free Software Foundation; either version 2 of the License, or
10: ; (at your option) any later version.
11: ;
12: ; This program is distributed in the hope that it will be useful,
13: ; but WITHOUT ANY WARRANTY; without even the implied warranty of
14: ; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15: ; GNU General Public License for more details.
16: ;
17: ; You should have received a copy of the GNU General Public License
18: ; along with this program; if not, write to the Free Software
19: ; Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20:
21:

第22到第75行:定義函式
22: (define (script-fu-newsprint-text string font font-size cell-size
23: density blur-radius text-color bg-color)
24: (let* (
25: (text-ext (gimp-text-get-extents-fontname string font-size PIXELS font))
26: (width (+ (car text-ext) 20 blur-radius))
27: (height (+ (list-ref text-ext 1) 20 blur-radius))
28: (img (car (gimp-image-new width height RGB)))
29: (bg-layer (car (gimp-layer-new img width height RGB-IMAGE "Background" 100 NORMAL-MODE)))
30: (text-layer (car (gimp-layer-new img width height RGBA-IMAGE "Text layer"
100 NORMAL-MODE)))
31: (text-mask 0)
32: (grey (/ (* density 255) 100))
33: )
34:
35: (gimp-context-push)
36:
37: (gimp-image-undo-disable img)
38: (gimp-image-add-layer img bg-layer 1)
39: (gimp-image-add-layer img text-layer -1)
40:
41: (gimp-context-set-background bg-color)
42: (gimp-edit-clear bg-layer)
43: (gimp-edit-clear text-layer)
44:
45: (gimp-context-set-foreground text-color)
46: (gimp-floating-sel-anchor
47: (car (gimp-text-fontname img text-layer
48: (/ (+ 20 blur-radius) 2) (/ (+ 20 blur-radius) 2)
49: string 0 TRUE font-size PIXELS font)))
50:
51: (set! text-mask (car (gimp-layer-create-mask text-layer ADD-ALPHA-MASK)))
52: (gimp-layer-add-mask text-layer text-mask)
53:
54: (gimp-selection-layer-alpha text-layer)
55: (gimp-context-set-background (list grey grey grey))
56: (gimp-edit-fill text-mask BACKGROUND-FILL)
57: (gimp-selection-none img)
58: (if (> blur-radius 0)
59: (plug-in-gauss-iir RUN-NONINTERACTIVE img text-mask blur-radius 1 1)
60: )
61:
62: (plug-in-newsprint RUN-NONINTERACTIVE img text-mask cell-size
63: 0 0 45.0 3 45.0 0 45.0 0 45.0 0 3)
64:
65: (gimp-edit-fill text-layer FOREGROUND-FILL)
66: (gimp-layer-remove-mask text-layer MASK-APPLY)
67:
68: (gimp-image-undo-enable img)
69:
70: (gimp-display-new img)
71:
72: (gimp-context-pop)
73: )
74: )
75:

第76到第95行:註冊函式
76: (script-fu-register "script-fu-newsprint-text"
77: _"Newsprint Te_xt..."
78: _"Create a logo in the style of newspaper printing"
79: "Austin Donnelly"
80: "Austin Donnelly"
81: "1998"
82: ""
83: SF-STRING _"Text" "Newsprint"
84: SF-FONT _"Font" "Sans"
85: SF-ADJUSTMENT _"Font size (pixels)" '(100 2 1000 1 10 0 1)
86: SF-ADJUSTMENT _"Cell size (pixels)" '(7 1 100 1 10 0 1)
87: SF-ADJUSTMENT _"Density (%)" '(60 0 100 1 10 0 0)
88: SF-ADJUSTMENT _"Blur radius" '(0 0 100 1 5 0 0)
89: SF-COLOR _"Text color" "black"
90: SF-COLOR _"Background color" "white"
91: )
92:
93: (script-fu-menu-register "script-fu-newsprint-text"
94: "<Image>/File/Create/Logos")
95:



Script-Fu 必也行號乎
  • Script-Fu 程式碼過長時,要一行一行地解說,就需要加上行號。
  • 加行號是為了便於解說,實際執行的 Script-Fu 程式碼是沒有行號的。
  • 任何一款純文字的編輯器,都可以編輯 Script-Fu 程式碼。

以下這一大段圖文解說,與 Script-Fu 無任何關係,可以選擇略過不讀。

  • 我是使用 PSPad 加上 PDF Creator來產生行號。
  • 因此,讀者你也可以使用 PSPad 來編輯 Script-Fu 程式碼。 PSPad 可以標示行號,如此,閱讀以下的程式說明,會比較容易對照。

  • 以下,示範利用 PSPad ,加上 PDF Creator,來產生行號。
  • Step 1. 下圖,以 PSPad 開啟 news-text.scm 。 PSPad 的「檢視」功能表內,有「行號」這一個項目。在 PSPad 內,「顯示的行號」看得到,卻無法複製,但是行號可列印在紙上。因此,可利用虛擬的 PDF 印表機,讓所有的程式碼加上行號。


  • Step 2. 虛擬的印表機有很多,我使用 PDF Creator。請先下載並安裝 PDF Creator。而後,在 PSPad 內,就可列印文字檔news-text.scm,列印時,印表機選擇 PDF Creator,如下圖所示。


  • Step 3. 接著, PDF Creator會出現一個儲存檔案的對話盒,如下圖所示,使用預設的檔名news-text.pdf,按下Save按鈕,如此,純文字檔就轉換A4尺寸的 PDF 檔。


  • Step 4. 如下圖所示,以 Adobe Reader 開啟 news-text.pdf 。 Adobe Reader 可將 PDF 檔再轉為純文字檔,如此,就可得加上行號的程式碼。