Rev | Author | # | Line |
---|---|---|---|
13 | LawrenceDoliveiro | 1 | !!! YOU POSTSCRIPT LOVE {HONK} IF |
2 | |||
15 | LawrenceDoliveiro | 3 | A stack-based ProgrammingLanguage that resembles [Forth], but mixes in some functional programming principles to allow a considerably higher level of abstraction. Although a complete, general purpose language, it was developed for typesetting and is accompanied by a complex, powerful graphical model. Adobe Systems, the creators of ~PostScript, also produced a version for use on-screen, called Display ~PostScript, but that was never very popular, and was eventually abandoned. |
10 | AristotlePagaltzis | 4 | |
5 | PostScript programs come in __.ps__ files. GhostScript is a [Free] software-only implementation of the language and graphics model, allowing users to view such files onscreen as well as send them to cheap printers that do not understand PostScript natively. PostScript is also the language used at the core of [PDF]. | ||
6 | |||
7 | The [Printing HOWTO | http://www.tldp.org/HOWTO/Printing-HOWTO/] explains why PostScript happened to become so well established. | ||
8 | |||
9 | Here's a simple example of what PostScript code looks like: | ||
10 | |||
11 | ---- | ||
12 | |||
13 | <verbatim> | ||
14 | %!PS-Adobe-2.0 | ||
15 | %%BoundingBox: 0 0 400 400 | ||
16 | %%Pages: 1 | ||
17 | %%EndComments | ||
18 | |||
19 | % any line starting with % is treated as a comment. "%!" as the first two | ||
20 | % bytes of the file is the "magic marker" that implies postscript. | ||
21 | |||
22 | % Exception... | ||
23 | % need postscript version 2 or later to understand the above-style | ||
24 | % "special" comments starting with "%%". Also can use like that per-page | ||
25 | |||
26 | %%Page: 1 1 | ||
27 | |||
28 | % disclaimer: the commands and examples given here aren't taken from a | ||
29 | % reference manual or anything, so some of the comments might be wrong or | ||
30 | % mis-understand what is actually happening... | ||
31 | |||
32 | save % save the current colour, scale factor, etc variables to the stack | ||
33 | |||
34 | % set the active colour to red (this is an R G B setting, between 0 and 1) | ||
35 | 1.0 0 0 setrgbcolor | ||
36 | |||
37 | % scale is global... if you double the scale, you need to halve it again | ||
38 | % later, unless you use save and restore... | ||
39 | |||
40 | 2 1 scale % set the scaling factor to 2x for x and 1x (ie normal) for y | ||
41 | |||
42 | % draw a circle (well, it would be a circle if scaling was 1x) | ||
43 | |||
44 | % x-origin y-origin radius startangle endangle arc (before scaling) | ||
45 | |||
46 | 100 100 50 0 360 arc % draws an arc (might be invisible) | ||
47 | |||
48 | fill % fills the previous object with the active colour | ||
49 | |||
50 | restore % go back to colour, scale, etc of the last save | ||
51 | |||
52 | % create some constants | ||
53 | /x 100 def | ||
54 | /y 100 def | ||
55 | /radius 50 def | ||
56 | |||
57 | % draw a circle using the saved state | ||
58 | x y radius 0 360 arc fill | ||
59 | |||
60 | |||
61 | % make some alias commands | ||
62 | /black {0 0 0 setrgbcolor} bind def | ||
63 | /blue {0 0 1 setrgbcolor} bind def | ||
64 | |||
65 | % use them | ||
66 | blue | ||
67 | |||
68 | % move the "cursor" | ||
69 | 100 300 moveto | ||
70 | |||
71 | % print some text | ||
72 | % before we can print text, we need to select the active font. | ||
73 | /Times-Roman findfont % font name | ||
74 | 12 scalefont % font size | ||
75 | setfont % make that font the active font | ||
76 | |||
77 | (Hello. This is blue text) show | ||
78 | |||
79 | black | ||
80 | 100 250 moveto ( You need to escape \( and \) as \\\( and \\\)) show | ||
81 | |||
82 | % the "translate" command draws everything offset by this x and y, | ||
83 | % save and restore this - takes effect until the next translate or restore | ||
84 | % 200 0 translate % causes everything from here-on to be drawn | ||
85 | % 200 pts to the right | ||
86 | |||
87 | |||
88 | 50 200 moveto % set current position | ||
89 | 3 -5 (diagonal) ashow | ||
90 | |||
91 | % print out the memory usage info | ||
92 | |||
93 | /Times-Roman-Bold findfont % choose font | ||
94 | 15 scalefont % font size | ||
95 | setfont % make active | ||
96 | |||
97 | 200 225 moveto (Memory Info:) show | ||
98 | |||
99 | % print out information about the memory usage | ||
100 | vmstatus % ps function that puts 3 numbers on the stack... | ||
101 | % to be honest, I'm not sure what the numbers are. I suspect | ||
102 | % the 2nd is the mem used, and the 3rd is mem allocated. But ghostscript | ||
103 | % can dynamically increase the avail mem, so it isn't really a very useful | ||
104 | % number in most cases. If you send this file to a postscript printer it | ||
105 | % might actually mean something... | ||
106 | % | ||
107 | 3 1 roll % take top value on stack and put it 3rd | ||
108 | exch % swap the top 2 values | ||
109 | %%% - now have reversed the order of the 3 numbers from vmstatus | ||
110 | 200 204 moveto | ||
111 | |||
112 | /Times-Roman findfont 12 scalefont setfont | ||
113 | (Min(?): ) show | ||
114 | 10 string % create a 10-byte string | ||
115 | cvs % fill the string with the previous number on the stack | ||
116 | show ( bytes) show | ||
117 | 200 192 moveto | ||
118 | (Used: ) show | ||
119 | 10 string cvs show ( bytes) show | ||
120 | 200 180 moveto | ||
121 | (Max: ) show 10 string cvs show ( bytes) show | ||
122 | |||
123 | %% draw a small irregular polygon | ||
124 | 0 setlinewidth % | ||
125 | newpath | ||
126 | 260 300 moveto | ||
127 | 270 315 lineto | ||
128 | 300 315 lineto | ||
129 | 310 280 lineto | ||
130 | 300 260 lineto | ||
131 | closepath % back to the origin | ||
132 | 1 0 1 setrgbcolor % red + blue = purple | ||
133 | fill | ||
134 | |||
135 | % print something to gv's pop-up informative window, or stdout for gs | ||
136 | (Example simple postscript commands\n) print | ||
137 | flush % cause it to be sent (from buffered) | ||
138 | |||
139 | %%EndPage | ||
140 | |||
141 | showpage % render the page | ||
142 | |||
143 | %%EOF | ||
144 | |||
145 | </verbatim> | ||
146 | |||
147 | ---- | ||
148 | CategoryProgrammingLanguages, CategorySpecialPurposeProgrammingLanguages |