Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
PostScript
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
== The language == PostScript is a [[Turing-complete]] programming language, belonging to the [[concatenative programming language|concatenative]] group of programming languages. It is an [[Interpreted language|interpreted]], [[Stack-oriented programming language|stack-based]] language similar to [[Forth (programming language)|Forth]] but with strong dynamic [[Type system|typing]], data structures inspired by those found in [[Lisp (programming language)|Lisp]], [[Scope (computer science)|scoped memory]] and, since language level 2, [[Garbage collection (computer science)|garbage collection]]. The language syntax uses [[reverse Polish notation]], which makes the order of operations unambiguous, but reading a program requires some practice, because one has to keep the layout of the [[Stack (data structure)|stack]] in mind. Most ''operators'' (what other languages term ''functions'') take their arguments from the stack, and place their results onto the stack. ''[[Literal (computer science)|Literal]]s'' (for example, numbers) have the effect of placing a copy of themselves on the stack. Sophisticated data structures can be built on the ''array'' and ''dictionary'' types, but cannot be declared to the type system, which sees them all only as arrays and dictionaries, so any further typing discipline to be applied to such user-defined "types" is left to the code that implements them. The character "%" is used to introduce comments in PostScript programs. As a convention, every PostScript program should start with the characters "%!PS" as an [[interpreter directive]] so that all devices will properly interpret it as PostScript. PostScript programs are typically divided into two parts, conventionally called the ''prolog'' and the ''script''. The prolog contains procedures and is written by a programmer. The script passes data to those procedures. The script is often generated automatically, using a programming language other than PostScript.<ref>Adobe Systems Inc. ''PostScript Language Reference Manual , 2nd ed., Appendix G, Document Structuring Conventions-Version 3.0''. Addison Wesley, 1990, p. 611.</ref> === "Hello world" === A [[Hello World program]], the customary way to show a small example of a complete program in a given language, might look like this in PostScript (level 2): <syntaxhighlight lang="postscript"> %!PS /Courier % name the desired font 20 selectfont % choose the size in points and establish % the font as the current one 72 500 moveto % position the current point at % coordinates 72, 500 (the origin is at the % lower-left corner of the page) (Hello world!) show % paint the text in parentheses showpage % print all on the page </syntaxhighlight> or if the output device has a console <syntaxhighlight lang="postscript"> %!PS (Hello world!) = </syntaxhighlight> === Units of length === PostScript uses the [[Point (typography)|point]] as its unit of length. However, unlike some of the other versions of the point, PostScript uses exactly 72 points to the inch. Thus: : {{math|1=1 point = {{sfrac|1|72}} inch = {{sfrac|25.4|72}} mm = {{sfrac|127|360}} mm = 352.777β¦ micrometers}} For example, in order to draw a vertical line of 4 cm length, it is sufficient to type: <syntaxhighlight lang="postscript"> 0 0 moveto 0 113.385827 rlineto stroke </syntaxhighlight> More readably and idiomatically, one might use the following equivalent, which demonstrates a simple procedure definition and the use of the mathematical operators <code>mul</code> and <code>div</code>: <syntaxhighlight lang="postscript"> /cm {72 mul 2.54 div} def % 1 inch = 2.54 cm exactly 0 0 moveto 0 4 cm rlineto stroke </syntaxhighlight> (Technically, most printers have a construction-implied unprintable margin around the physical borders of the sheet, and the 0 0 coordinates are calibrated to its corner,<ref name=PLR/>{{rp |at=section 4.3.1}} so you might have to use a different starting point to actually see something.) Most implementations of PostScript use [[single-precision]] reals<ref name=PLR/>{{rp |at=appendix B}} (24-bit mantissa), so it is not meaningful to use more than 9 decimal digits to specify a real number, and performing calculations may produce unacceptable round-off errors.
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)