Introduction
The main purpose here is not to teach each detail of this software but to give sufficient material for the persons who find PostScript an interesting and enjoyable programming language for document preparation. Although we do not intend to write a technical paper or a textbook or a bible .
The main ideas behind PostScript were created twenty two years ago by John Gaffney at the Evans & Sutherland Computer Corporation. It was known as the "Design System" at the beginning but later it was developed under the name PostScript by Adobe Systems Incorporated as a platform and device independent tool for page designing. Adobe Systems Incorporated was formed by Chuck Geschke and John Warnock in 1982. C. Geschke and J. Warnock undertook a new design and implementation of the language, to which Doug Brotz, Billi Paxton, and Ed Taft made major contributions. Nowadays it is one of the major document preparation tools although it is not directly used by most end users. Its capabilities are at quite high levels however many lazy users who do not want to get into the details of this powerful language prefer To use the other end-user-oriented Tools Which Are Mainly based on the "what you see what you get" philosophy. to tell the truth, man wysiwyg Tools Use PostScript File Formats fo r maintaining the documentation products or for using as an interface for other jobs like printing. In these senses, it still survives as our old friend which serves as a powerful assistance to many computer jobs related to drawing, coloring, image processing, color separation, SIMULATION AND Animation Even if itself in the procedures. on The Other Hand, IF You Desire To Learn ITS Structure for Programming You Will Find It Not So Difficult.
The PostScript commands are executed through certain interpreters. Amongst these, a well-known software is freely available on the public domain of Internet. This is named "ghostscript" by Aladdin Enterprise. There is also a useful graphical interface, "ghostview", released by the same organization. gnu project has also its own versions for ghostscript. These are available in every Linux platform and all main Linux distributions contain them in standard installations. Here we deal with ghostscript and its certain specific structures and properties although the sample programs can be executed through other interpreters. Assume that we activate the X window server and open an xterm window. If we want to use ghostscript then each command or command groups will be given at the command prompt which will be appeared after issuing the commandInitializing ... Done. Ghostscript 2.6.2 (4/19/95) Copyright (C) 1990-1995 Aladdin Enterprises, Menlo Park, Ca. All Rights Reserved. Ghostscript Comes Wit H No Warranty: See The File Copying for Details. GS>
_
A specific blank window will be opened at the same time with the appearence of this command prompt. If we want to dismiss the ghostscript session, all we have to do is to issue quit command at GS> prompt. EOF (Ctrl-D) works Also for the same purpose.
Ghostscript can execute the commands by getting them from a file also. In this case we open a file named, for example, sample.ps. All commands to be executed by ghostscript are written in this file. Hence we can call the content of this file a PostScript program. The entire content of this program or its individual commands will be consecutively executed by ghostscript if we issue the following command at the shell prompt of the xterm window of linuxgs sample.ps
and the display (if the program aims at the creation of a display, of course. In fact, PostScript can be used for other purposes like mathematical calculations, stack operation etc. We will mention them in future articles of this series) will be appearing on the ghostscript window. If the program produces more pages than a single one then the ghostscript window shows the first page and a showpage prompt appears at the ghostscript commandline requesting enter key pressing for the next page.
PostScript uses Reverse Polish Notation. (RPN - like HP pocket calculators) In other words parameters for a command are given before the command The commands are separated by blank spaces It uses stack operations for data processing and command execution There are four available... stacks in PostScript usage:. the operand, dictionary, execution and graphics state stacks The operand stack holds arbitrary PostScript objects that are the operands and results of PostScript operators being executed We can give some examples from arithmetical operations For instance,..
20 30 Add 10 SUB
Products 40 Since
20 30 Add Creates 50 and
Sub produu 40 by using 50 together with 10.
Mul (multiplication) and
div (division) work in the same way. The name '=' is associatedwith a procedure that pops one object from the operand stack and writes a text representation of its value to the standard ouput file, followed by a newline. == has a similar action but it writes syntactic representation of its operand to the standard output file.pop command removes the top element from the operand stack and discards it. We will mention about the stacks in a more detailed manner later articles of this series.
How do WE USE PostScript for DRAWING FIGURES?
We can start drawing figures through PostScript by considering the geometrical structure of the page to be designed. The locations of the points on a page are denoted via pixel units which are one seventy second of an inch in PostScript language. The left bottom side of the rectangular page is assumed to be the location which has the (0,0) coordinates and the horizontal and the vertical sizes of the page are defined as 612 and 792 respectively. These sizes are for the letter type of papers. There are other possibilities for The definition of the paper sizes like
Note for 540 and 720 or LEGAL FOR 612 and 1008 OR A4 for 595 and 842.
.................. ..
The paper size definition can be found in the initialization file whose name is gs_statd.ps. It can be found in / usr / lib / ghostscript for Slackware. You can define any non-standard paper size by making convenient additions and modifications on this file AS WE WILL MENTION LATER IN THIS SERIES OF ARTICLES. Here We Assumer That We Use The default paper type.
THE Second Step after Taking The Paper in Desired Sizes in Drawing a Figure is to locate the curser at the beginning of the drawing. This can be recognized by quest il
where x and y denote respectively the horizontal and vertical coordinates of the point where the cursor will be located. x and y are pure numbers and considered in pixel units. As you can see the functional structure of the PostScript is a little bit different from the other programming languages like C, Fortran. moveto is the command which takes the action, in other words it moves the cursor to somewhere whose location is given by x and y. Since PostScript uses a stack the parameters are given consecutively to the stack first. Then the command is given. When it enters the stack it accepts the previous two items as its parameters. The syntax here is different for the end user who is familiar with languages like C, Fortran. for this reason we have emphasized on this topic here . As a conclusion we can say That Each Command Which Needs Some Parameters Must Be Given After ITS Parameters.
Now We are ready. Let us start with a rather Simple Figure
XX Yy Lineto
where xx and yy denote the location of the endpoint of the line segment. Its beginning point is the present location of the cursor, x and y. Hence, this command constructs a straight line segment from xy to xx yy. The actual drawing necessitates the path definition newpath and stroke command. Now let us write a PostScript program to draw a rectangle. This can be done by using the information given above. The program which is assumed saved in the file sample1.ps can be given as followsnewpath 100 100 moveto 300 100 LINETO 300 250 Lineto 100 250 Lineto 100 100 LINTO STROKE
Althought Lines, this Is Not Really Necessary. We Could Give The Same Line by Inserting A Blank Space Between Consecutive Commands as we moution before.
PostScript HAS Commenting Facility Like The Other Programming Languages. Whole Material Which Follows a Percentage Sign IN A LINE INTERPRETED As Comment by The Interpreter.
NewPath% Cursor Initialization 100 100 Moveto% Drawing The Rectangle 300 100 Lineto 300 250 Lineto 100 250 Lineto 100 100 LINETO STROKE
If we now issue gs sample1.ps command at the prompt of the xterm window then the usual ghostscript window appears containing the desired display of the rectangle. We do not need to invoke gs interpreter at every time of displaying. In fact, (sample1. PS) Run Command Does The Same Thing THROUGH The GS> Prompt.
.................. ...
The Straight Lines Do Not NEed To Be Vertical or Horizontal. They Can Be Constructed In Any Direction.
As you will see when we display the result through ghostscript, this program creates a triangle. You can create any kind of figures which are composed of broken lines or line segments by using moveto and linetocommands.There are two more commands to be used in the Above Figure Constructions. Thase Are Rmoveto and rlineto. They need Two parameters and can be used as Follows
X y rmoveto x y rlineto
where x and y are the horizontal and vertical distances between the initial and final points of each action. 'r' in rmove and rlineto stands for 'relative'. In other words the first command moves the cursor x units horizontally to right and y units to up from its present location. The second command behaves similarly but it draws a line and moves the cursor to the final point instead of only moving cursor. These are referring commands and the parameters are given by taking the initial location as the reference point.
All Above Examples Use A Default Linewidth for the Drawing. This value is 1 Pixel. However user can define the line width of the drawing at anywhere in the program this can be doms
X setLineWidth
Width in Pixels. The Effect of this Command Continues Until The next line width setting in the program of line width settings in a program.
Of Course, The Drawing In PostScript Is Not Limited to Straight Lines. The Circular Arcs Can Be Also Produced. For this purpose We can use the folloading command
X y r a b arc
where x, y, r, a and b stand for the horizontal and vertical coordinates of the center of the circular arc, the radius of the arc, the angle between the positive part of the horizontal axis and the central rays which pass through the beginning and the final points of the arc. The angles are measured counterclockwise. If the beginning point of the arc is not the present location of the cursor, then a straight line between the currentpoint (the location of the cursor) and the beginning point arc is also added to the figure. to understand the situations you can have a look at the display of the following PostScript program.3 setlinewidth 200 200 moveto 100 200 100 0 75 arc stroke 300 300 moveto 400 500 200 20 50 arc stroke
Therefore we must be careful to place the cursor at the beginning point of the arc if do not want the extra line mentioned above. However there is another way to get rid of this effect. In fact, the currentpoint can be made empty valued. In other words at the beginning of the path it has no parameter values. There is no assignment for the location of the cursor. Once the drawing is started the final point of the path becomes the current point. If we issue newpath command then PostScript erases the assignment for the cursor point and treats as if the drawing will start just from that instance. Therefore the above program can be modified by replacing the fifth line with a line which contains only newpath command. If this is done and displayed then the same output, Except The Extra Line Segment, of the Above Program Will be Obtained.
A path is begun by executing the newpath operator. This initializes the current path to be empty. The path is then up via built execution of some commands for adding segments to the current path.arc command can be used to draw a full circle. It is sufficient to give the beginning and the final angles of the arc as 0 and 360 respectively. If this is done under a newpath a complete circle is obtained. arc command can be used to produce ellipses also. This can be accomplished by using scaling property Of PostScript. The User Can Rescale of The Horizontal and Vertical Units Separately Via The Following Command.
X y scale
where x and y denote respectively the horizontal and vertical scaling factors. This means that the case where these factors are equal to 1 creates no effect on the drawing. The effect of the scaling is maintained until the next scaling command is issued. The next issued scaling command does not remove the effect of the previous one but it combines its own effect with the previous one. If we assume that the previous scaling command parameter has the scaling factors x1, y1 while the next scaling command parameter has x2, y2 factors then the combined effect of these commands has the scaling factors x1 * x2, y1 * y2. This point must not be kept far from the eyes to avoid undesired results in the displays, such that some paths may slide out of the paper surface. PostScript assumes .,,,,,,,,,,,,,,,,,,,,,,,,
3 setLineWidth 200 200 100 0 360 ARC STROKE NewPath 2 1 Scale 200 300 50 0 360 ARC STROKE NewPath 1 4 Scale 100 150 40 0 360 ARC STROKE
As can be noticed, the scaling affects each size including the line width. The line widths of the ellipses and the circle created by the above program are different due to this reason.PostScript also has two other commands for arc drawing. One of them, arcn, is mainly same with arc except the drawing direction. arcn draws arc clockwise. The third arc drawing command draws a circular arc which is tangent to two given lines at the endpoints. It can be issued as follows.
X1 Y1 X2 Y2 R Arcto XT1 YT1 XT2 YT2
where xt1, yt1, xt2, yt2 denote the horizontal and the vertical coordinates of the arc while the end points of the tangent lines have the the values x0, y0, x1, y1 and x1, y1, x2, y2 respectively and r stands for the radius of the arc. If the path is not new or the current point does not coincide with the beginning of the arc then a line joining the current point and the beginning point of the arc is added to the path. At the end of the Drawing the currentpoint becomes Xt2, yt2.
PostScript has also a Bezier algorithm based command which can be effectively used in the interpolation or extrapolation of a given data for plotting. This command is curveto and can be used to form the plots based on the interpolation or extrapolation of a given set of data. The Command Can Be Used As Follows.
X1 Y1 x2 Y2 x3 Y3 Curveto
where the curve starts at the current point whose coordinates are assumed to be x0, y0. It is tangent to the line between the points x0, y0 and x1, y1 at the beginning point. The curve ends at the point x3, y3 and is .
Writing facilities in postscript
PostScript has various fonts which are used as standard fonts for desk-top publishing. It has also font creating facilities which can be accessed by defining dictionary stacks where fonts are available. The following PostScript program can be given as an example./Times-Roman findfont 15 scalefont setfont 100 500 moveto (I love PostScript!) show / Times-Italic findfont 20 scalefont setfont 100 450 moveto (I love PostScript!) show / Times-Bold findfont 25 scalefont setfont 100 400 moveto (I love PostScript!) show / Helvetica findfont 30 scalefont setfont 100 350 moveto (I love PostScript!) show / Courier findfont 35 scalefont setfont 100 300 moveto (I love PostScript!) show / Helvetica-Bold findfont 40 scalefont setfont 100 250 moveto (I love PostScript!) show Showpage
As can be extracted from the program findfont command is used for getting an access to the desired font structure. The name of the font starts with / and is given to the command as a preceding parameter (where / tells the interpreter to push this value on the stack 'as is'. When we get into the details of stack operations later this point will be more clear. After the selection of the command the scaling is defined through the scalefont command. The scaling factor is given as a preceding number to this command. After scaling of the font, the command setfont makes the font ready for future usage in the program. After we locate the cursor through the moveto command with appropriate parameters, the text material encompassed by the parenthesis is given a parameter for the show command THE The Above Program Uses Different Fonts in Type And Size for the Same Text Located At Different Positions. The Numr of the Avaliable Fo nts for PostScript can be found by searching the locations of the fonts in the tree structure of the linux system you use. The text can be adjusted through lines, curves and by some other tool. So any kind of writing, in principle, is possible . IT is Just a Matter of Design.Painting and Color Usage In PostScript
PostScript language is equipped with several facilities to paint figures or create colorful pictures. Let us start by mentioning the coloring commands first. PostScript uses black as the default color unless a color specification command is issued. Hence the output of the all previous programs were black . and white pictures to use color in the output PostScript can use three different coloring command The first one is based on the rgb color format in this format each color is assumed to be composed of three main colors:.. red, green and blue. The color components can be given by individual intensity parameters which can take the values between 0 and 256. The intensity parameters can accept the decimal fractional values up to three digit precision like 111.223. Therefore the command expected by PostScript must be given as follows.xyz SetRGBColor
where x, y, z are the intensity parameters for the red, green and blue components and setrgbcolor is the command. In this convention 1 0 0 setrgbcolor creates red color while 0 1 0 setrgbcolor creating green. Black corresponds to the case where all intensity parameters take the value of 1. Color setting command continues to affect every drawing and painting until the next color setting command is issued. When this happens the effect of the first setting is completely removed and the new setting dominates everything. The number of usage for The color setting is unlimited. by approriately using the color settings you can want. It is a matter of art and up to you.
The second color setting facility is based on a four component color format. This is called cmyk color format. The four basic color components are cyan, magenta, yellow and black respectively. Each color component contributes the final color according to an intensity parameter which can Vary Between 0 and 1. The Corresponding PostScript Command Is Therefore as Follow XYZ setCMYKColor
where w, x, y, z are the intensity parameters for the color components cyan, magenta, yellow and black respectively. The fractional decimal values can also be used for intensity parameters. This command also continues its effect on everything until the next command is .
Third Command Can Be Used As Follows
X YZ Sethsbcolor
where x, y, z stand for the intensity paremeters of three different properties of the color. First one corresponds to hue which determines the location of the color in the spectrum of the light. The second one, saturation and the third one, brightness corresponds to the saturation and the brightness of the color. This format is preferred when the location of the color in the spectrum via a loop or the brightness and / or saturation become the properties to be controlled. The reader who is not familiar to hsbcolor he / She Can Check The Use of the xfig facility under linux.
The most important PostScript command for painting is fill and closepath. The following example program which is assumed to be saved in the file sample6.ps clarifies important aspects of the painting and coloring through PostScript.
1 1 0 0 setcmykcolor 100 100 moveto 300 100 lineto 300 250 lineto 100 250 lineto 100 100 lineto stroke 1 0.5 0.8 0 setcmykcolor 5 setlinewidth 200 200 moveto 400 300 lineto 300 300 lineto closepath fill stroke 1 0 0 setrgbcolor 3 setlinewidth 200 200 moveto 100 200 100 0 75 ARC STROKE 0 0 0 1 0.2 SetCMYKCOLOR 3 setLineWidth 200 200 0 360 Arc stroke 1 0 0 StrGBColor NewPath 2 1 Scale 200 300 50 0 360 ARC GSAVE FILL GRESTORE STROKE STROKE 0 1 0 setrgbcolor newpath 1 4 scale 100 150 40 0 360 arc gsave fill grestore strokewhere closepath command closes an open path by joining two end points by a straight line while fill serves to fill inside of the closed path with the current color.
PostScript Can Also Create Grey Tones. This Can Be Done Through THE COMMAND
x setgray
where x is the intensity of the graycolor and its value can change from 0, which corresponds to black, to 1 which corresponds to white. The following program which is saved under the name sample7.ps is constructed in a sufficiently self = explanatory manner.
0.2 setgray 10 setlinewidth 100 700 moveto 200 0 rlineto stroke newpath 0.3 setgray 100 600 moveto 200 0 rlineto stroke newpath 0.4 setgray 100 500 moveto 200 0 rlineto stroke newpath 0.5 setgray 100 400 moveto 200 0 rlineto stroke newpath 0.6 setgray 100 300 moveto 200 0 rlineto Stroke NewPath 0.7 Setgray 100 200 Moveto 200 0 rlineto stroke newpath 0.8 setgray 100 100 Moveto 200 0 rlineto stroke
Before the completion of this presentation we can recommend to write more complicated and comprehensive programs for the users who find PostScript as an enthusisastic tool. In the next articles of these series more details about the PostScript language. All questions and recommendations are welcome for our presentations WE Shall Give Sufficient Credit To THESE IN OUR COMING ARTICLES.THOR THANKS TO OLIVER Thilmann for His Very Helpful Comments for the Organization and Presentation of this article.