2009/11/13

PostScript Line-Drawing Details, setdash, aload, astore, roll

10.2-LineDrawingDetails.ps

% --- variables ------------------------
/width 500 def
/height 700 def
/ypos 700 def
/font-size 15 def
/dec-point 15 def
/Times-Roman findfont font-size scalefont setfont

% --- procedures -----------------------
/prt-n {
    (   ) cvs show
} def

/borders {
    -2.5 0 moveto
    0 height rlineto
    width 0 moveto
    0 height rlineto
    stroke
} def

/newline {
    /ypos ypos dec-point sub def
} def

/doLine {
    0 ypos moveto
    width 0 rlineto
    stroke
    5 ypos 2 add moveto
    ypos prt-n
    newline
} def

/dot {
    setdash doLine
} def

% --- main -----------------------------
50 50 translate
15 setlinewidth
borders

.5 setlinewidth
[] 0 dot
/dotary [0 1 2 3 4 5] def
height dec-point idiv {
    /dotary dotary aload pop 6 1 roll 6 array astore def
    [4 4] dotary 0 get dot
} repeat

showpage

 

 

PostScript(R) Language Tutorial and Cookbook (APL)

PostScript(R) Language Tutorial and Cookbook (APL)

posted with amazlet at 09.10.07

Adobe Systems Inc.
Addison-Wesley Professional
売り上げランキング: 235737

おすすめ度の平均: 5.0

5 まず最初に読むべき入門書
5 効率の良いPostScript書くなら…

Amazon.co.jp で詳細を見る

PostScript FontTransformations, makefont, newline

9.3-FontTransformations.ps 

% --- variables ------------------------
/basefont /Times-Roman findfont def
/LM 72 def
/line-height 53 def

% --- procedures -----------------------
/newline {
    currentpoint line-height sub
    exch pop LM
    exch moveto
} def

/up {
    40 add
} def

% --- main -----------------------------
LM 600 moveto

basefont [12 up 0 0 12 up 0 0] makefont setfont
("Talking of axes,") show newline

basefont [17 up 0 0 12 up 0 0] makefont setfont
(said the Duchess,) show newline

basefont [7 up 0 0 12 up 0 0] makefont setfont
("Off with her head!") show newline

basefont [12 up 0 6.93 up 12 up 0 0] makefont setfont
(        - Lewis Carrol) show
showpage

 

PostScript(R) Language Tutorial and Cookbook (APL)

PostScript(R) Language Tutorial and Cookbook (APL)

posted with amazlet at 09.10.07

Adobe Systems Inc.
Addison-Wesley Professional
売り上げランキング: 235737

おすすめ度の平均: 5.0

5 まず最初に読むべき入門書
5 効率の良いPostScript書くなら…

Amazon.co.jp で詳細を見る

PostScript kshow

9.1-DifferentShows-kshow.ps

% --- variables ------------------------
/TM 832 def % Tom
/BM -12 def % Bottom
/LM 0 def % Left
/RM 612 def % Right
/line-height 28 def
/font-size-1 30 def
/font-size-2 250 def
/message (KPF) def

% --- procedures ------------------------
/newline {
    currentpoint line-height sub
    exch pop LM
    exch moveto
} def

/nllfNec {
    currentpoint pop RM gt {
        newline
    } if
} def

/done? {
    currentpoint exch pop
    BM lt
} def

/fillpage {
    /strg exch def
    {
        { pop pop nllfNec } strg kshow
        ( ) show
        done? {
            exit
        } if
    } loop
} def

% --- main -----------------------------
/Times-Bold findfont font-size-1 scalefont setfont
LM TM moveto
.7 setgray
message fillpage

/Times-Roman findfont font-size-2 scalefont setfont

RM LM sub
message stringwidth pop sub
2 div
400 moveto

0 setgray
message show

showpage

 

PostScript(R) Language Tutorial and Cookbook (APL)

PostScript(R) Language Tutorial and Cookbook (APL)

posted with amazlet at 09.10.07

Adobe Systems Inc.
Addison-Wesley Professional
売り上げランキング: 235737

おすすめ度の平均: 5.0

5 まず最初に読むべき入門書
5 効率の良いPostScript書くなら…

Amazon.co.jp で詳細を見る

PostScript CharacterEncoding, char, cvs, string

9.2-CharacterEncoding.ps

% --- variables ------------------------
/font-size 16 def
/line-height 16 def
/Times-Roman findfont font-size scalefont setfont
/char 1 string def
/nstr 3 string def

% --- procedures -----------------------
/newline {
    currentpoint line-height sub
    exch pop LM
    exch moveto
} def

/prt-n {
    nstr cvs show
} def

/prtchar {
    char 0
    3 -1 roll put
    char show
} def

/PrintCodeandChar {
    dup prt-n
    (  ) show
    prtchar
    newline
} def

% --- main -----------------------------
/LM 72 def
LM 800 moveto
161 1 208 { PrintCodeandChar } for

/LM 288 def
LM 800 moveto
225 1 251 { PrintCodeandChar } for

showpage

 

PostScript(R) Language Tutorial and Cookbook (APL)

PostScript(R) Language Tutorial and Cookbook (APL)

posted with amazlet at 09.10.07

Adobe Systems Inc.
Addison-Wesley Professional
売り上げランキング: 235737

おすすめ度の平均: 5.0

5 まず最初に読むべき入門書
5 効率の良いPostScript書くなら…

Amazon.co.jp で詳細を見る

PostScript forall, aload, get

8.2-ArrayOperators.ps8.2-ArrayOperators-forall.ps8.2-ArrayOperators-aload.ps

 

8.2-ArrayOperators.ps

% --- variables ------------------------
/LM 72 def % Left margin
/Tempstr 30 string def
/nextline-height 50 def
/Helvetica findfont 60 scalefont setfont

% --- procedures -----------------------
/crlf { % next line
    currentpoint nextline-height sub
    exch pop
    LM exch
    moveto
} def

/aryshow {
    /ary exch def
    0 1 ary length 1 sub
    {
        ary exch get
        Tempstr cvs
        show crlf
    } for
} def

% --- main -----------------------------
LM 600 moveto

[
    (mouse)
    27
    /aName
    [
        6
        12
    ]
    { crlf }
    LM
    /Helvetica findfont
]
aryshow
showpage

 

8.2-ArrayOperators-forall.ps 

% --- variables ------------------------
/LM 72 def
/RM 512 def
/ypos 720 def
/lineheight 50 def
/font-size 50 def

% --- procedure ------------------------
/crlf {
    ypos lineheight sub
    /ypos exch def
    LM ypos moveto
} def

/prtstr {
    dup stringwidth pop
    currentpoint pop
    add RM gt
    {
        crlf
    } if
    show
} def

/format {
    {
        prtstr ( ) show
    } forall
} def

% --- main -----------------------------
/Times-Italic findfont font-size scalefont setfont
LM ypos moveto

[(Concience)(is)(inner)(voice)
    (that)(warns)(us)(somebody)(many)(be)
    (looking)( - Mencken)] format

showpage

 

8.2-ArrayOperators-aload.ps

% --- variables ------------------------
/LM 30 def
/font-size 30 def
/line-hight 30 def
/msg (The five boxing wizards jump quickly.) def

% --- procedures ------------------------
/newline {
    currentpoint line-hight sub
    exch pop LM exch
    moveto
} def

/PrintSample {
    aload pop
    findfont font-size scalefont setfont
    show
    newline
} def

/FontList [
    [msg /Helvetica]
    [msg /Times-Roman]
    [msg /Symbol]
] def

% --- main -----------------------------
LM 600 moveto
FontList { PrintSample } forall
showpage

 

 

PostScript(R) Language Tutorial and Cookbook (APL)

PostScript(R) Language Tutorial and Cookbook (APL)

posted with amazlet at 09.10.07

Adobe Systems Inc.
Addison-Wesley Professional
売り上げランキング: 235737

おすすめ度の平均: 5.0

5 まず最初に読むべき入門書
5 効率の良いPostScript書くなら…

Amazon.co.jp で詳細を見る

 

PostScript forall

GS>[1 2 3 4 5] { dup mul } forall
GS<5>stack
25
16
9
4
1
GS<5>[1 2 3 4 5] { 10 mul } forall
GS<10>stack
50
40
30
20
10
25
16
9
4
1
GS<10>

2009/11/12

PostScript ループ for, loop, repeat, 再帰

7.2-Loops-for.ps7.2-Loops-loop.ps7.2-Loops-Recur.psWS0819

7.2-Loops-for.ps
/Times-Italic findfont 300 scalefont setfont 
/printZip {
    0 0 moveto
    (Zip) show
} def 
/printing{
    .99 -.03 0
    {
        setgray
        printZip
        -1 .5 translate
    } for
} def 
80 500 translate
printing
1 setgray printZip 
0 -400 translate
printing 
showpage

7.2-Loops-loop.ps 
% --- procedures ---------------------------------
/pagewidth {
    8.5 72 mul
} def 
/doCircle {
    xpos ypos radius 0 360 arc stroke
} def 
/increase-x {
    xpos radius add
    /xpos exch def
} def 
/lineofcircles {
    /ypos exch def
    /radius exch def
    /xpos 0 def
    {
        xpos pagewidth le
        {
            doCircle
            increase-x
        }
        {
            exit
        } ifelse
    } loop
} def 
% --- programs ----------------------------------
10 100 lineofcircles
30 100 lineofcircles
90 100 lineofcircles 
/seed-x 30 def
/inc-x 30 def 
5 {
    seed-x 400 lineofcircles
    /seed-x seed-x inc-x add def
} repeat 
10 {
    -10 rotate
    -150 0 translate
    10 800 lineofcircles
} repeat 
showpage
7.2-Loops-Recur.ps
% --- variables ----------------------------------
/LM 72 def % ?
/Times-Roman findfont 50 scalefont setfont
/nstr 9 string def 
% --- procedures ---------------------------------
/factorial {
    dup 1 gt
    {
        dup 1 sub
        factorial mul
    } if
} def 
/newline {
    currentpoint 40 sub
    exch pop
    LM exch
    moveto
} def 
/prt-n {
    nstr cvs show
} def 
/prtFactorial {
    dup prt-n
    (! = ) show
    factorial prt-n
    newline
} def 
% --- main ---------------------------------------
LM 600 moveto
1 1 12 { prtFactorial } for 
LM 100 moveto
10 factorial 7 string cvs show 
showpage
WS0816 WS0817 WS0818
WS0819
画像は順にmaxdepthが1, 3, 7, 12の場合
PostScriptで手軽にクロージャ使えたら良いなー。と思ったが、どうやら少し込み入ったことになりそうだったので高階関数"的"なものの真似事をしてみた。手続きを手続きに渡してるわけだから"的"はいらないか。
クロージャも高階手続きももっと手軽に使えないと意味がないと思うので何か良い方法はないのかと考えてみる。今度。
% --- variables ------------------------
/depth 0 def
/maxdepth 12 def
/arrow-size 300 def 
% --- procedures -----------------------
/down {
    /depth depth 1 add def
} def 
/up {
    /depth depth 1 sub def
} def 
/slide-v {
    /proc exch def
    /proc load
    /depth depth 1 proc def
} def 
/DoLine {
    0 arrow-size rlineto
    currentpoint
    stroke
    translate
    0 0 moveto
} def 
/FractArrow {
    gsave
    .7 .7 scale
    10 setlinewidth
    % down
    { add } slide-v
    DoLine
    depth maxdepth le
    {
        135 rotate
        FractArrow
        -270 rotate
        FractArrow
    } if
    % up
    { sub } slide-v
    grestore
} def 
/def-pos {
    300 400 moveto
} def 
/rotate-90 {
    90 rotate
} def 
% --- main -----------------------------
def-pos 
4 {
    gsave
    FractArrow
    grestore
    rotate-90
} repeat 
stroke 
showpagen
追加:
WS0820WS0821WS0822
% --- variables ------------------------
/depth 0 def
/maxdepth 12 def
/arrow-size 300 def
/scale-point .7 def 
% --- procedures -----------------------
/down {
    /depth depth 1 add def
} def 
/up {
    /depth depth 1 sub def
} def 
/slide-v {
    /proc exch def
    /proc load
    /depth depth 1 proc def
} def 
/DoLine {
    0 arrow-size rlineto
    currentpoint
    stroke
    translate
    0 0 moveto
} def 
/FractArrow {
    gsave
    scale-point dup scale
    10 setlinewidth
    % down
    { add } slide-v
    DoLine
    depth maxdepth le
    {
        135 rotate
        FractArrow
        -270 rotate
        FractArrow
    } if
    % up
    { sub } slide-v
    grestore
} def 
/def-pos {
    300 400 moveto
} def 
/rotate-90 {
    90 rotate
} def 
% --- main -----------------------------
def-pos 
4 {
    gsave
    FractArrow
    grestore
    rotate-90
} repeat 
stroke 
showpagen

PostScript(R) Language Tutorial and Cookbook (APL)
PostScript(R) Language Tutorial and Cookbook (APL)
posted with amazlet at 09.10.07
Adobe Systems Inc.
Addison-Wesley Professional
売り上げランキング: 235737
おすすめ度の平均: 5.0
5 まず最初に読むべき入門書
5 効率の良いPostScript書くなら…
Amazon.co.jp で詳細を見る

PostScript 条件分岐

7.1-ConditionExecution-1.ps
% ---variables---------------------------------------------
/LM 72 def
/RM 212 3 mul def
/ypos 720 def
/lineheight 40 def

% ---procedures--------------------------------------------
/newline{
    ypos lineheight sub
    /ypos exch def
    LM ypos moveto
} def

/prtstr{
    dup stringwidth pop
    currentpoint pop
    add RM
gt
    {newline} if
    show
} def

/choosefont{
    exch findfont exch scalefont setfont
} def

% ---main program------------------------------------------
/Times-Italic findfont 50 scalefont setfont
LM ypos moveto
(If ) prtstr
(you ) prtstr
(tell ) prtstr
(the ) prtstr
(truth, ) prtstr
(you ) prtstr
(don't ) prtstr
(have ) prtstr
(to ) prtstr
(remember ) prtstr
(anything. ) prtstr
(- Mark ) prtstr
(Twain ) prtstr

showpage
7.1-ConditionExecution-2.ps
% --- variables ----------------------------------------
/scalefactor 1 def
/counter 0 def

% --- procedures ---------------------------------------
/DecreaseScale {
    scalefactor .2 sub
    /scalefactor exch def
} def

/IncreaseConter {
    /counter counter 1 add def
} def

/trappath {
    0 0 moveto
    90 0 rlineto
    -20 45 rlineto
    -50 0 rlineto
    closepath
} def

/doATrap {
    gsave
    1 scalefactor scale
    trappath
    counter 2 mod
    0 eq {.5} {0} ifelse
    setgray fill
    grestore
} def

% --- programs -----------------------------------------
0 0 moveto
250 350 translate

10 {
    IncreaseConter
    doATrap
    DecreaseScale
    0 20 translate
} repeat

showpage

  • eq =
  • ne ≠
  • gt >
  • lt <
  • ge >=
  • le <=