NAME
PostScript::Simple - Produce PostScript files from Perl
SYNOPSIS
use PostScript::Simple;
# create a new PostScript object
$p = new PostScript::Simple(papersize => "A4",
colour => 1,
units => "in");
# create a new page
$p->newpage;
# draw some lines and other shapes
$p->line(1,1, 1,4);
$p->linextend(2,4);
$p->box(1.5,1, 2,3.5);
$p->circle(2,2, 1);
$p->setlinewidth( 0.01 );
$p->curve(1,5, 1,7, 3,7, 3,5);
$p->curveextend(3,3, 5,3, 5,5);
# draw a rotated polygon in a different colour
$p->setcolour(0,100,200);
$p->polygon("rotate=45", 1,1, 1,2, 2,2, 2,1, 1,1);
# add some text in red
$p->setcolour("red");
$p->setfont("Times-Roman", 20);
$p->text(1,1, "Hello");
# write the output to a file
$p->output("file.ps");
DESCRIPTION
PostScript::Simple allows you to have a simple method of writing PostScript files from Perl. It has graphics primitives that allow lines, curves, circles, polygons and boxes to be drawn. Text can be added to the page using standard PostScript fonts.
The images can be single page EPS files, or multipage PostScript files. The image size can be set by using a recognised paper size ("A4", for example) or by giving dimensions. The units used can be specified ("mm" or "in", etc) and are the same as those used in TeX. The default unit is a bp, or a PostScript point, unlike TeX.
PREREQUISITES
This module requires strict and Exporter.
EXPORT
None.
CONSTRUCTOR
new(options)-
Create a new PostScript::Simple object. The different options that can be set are:
- units
-
Units that are to be used in the file. Common units would be
mm,in,pt,bp, andcm. Others are as used in TeX. (Default:bp) - xsize
-
Specifies the width of the drawing area in units.
- ysize
-
Specifies the height of the drawing area in units.
- papersize
-
The size of paper to use, if
xsizeorysizeare not defined. This allows a document to easily be created using a standard paper size without having to remember the size of paper using PostScript points. Valid choices are currently "A3", "A4", "A5", and "Letter". - landscape
-
Use the landscape option to rotate the page by 90 degrees. The paper dimensions are also rotated, so that clipping will still work. (Default: 0)
- clip
-
If set to 1, the image will be clipped to the xsize and ysize. This is most useful for an EPS image. (Default: 0)
- colour
-
Specifies whether the image should be rendered in colour or not. If set to 0 (default) all requests for a colour are mapped to a greyscale. Otherwise the colour requested with
setcolourorlineis used. This option is present because most modern laser printers are only black and white. (Default: 0) - eps
-
Generate an EPS file, rather than a standard PostScript file. If set to 1, no newpage methods will actually create a new page. This option is probably the most useful for generating images to be imported into other applications, such as TeX. (Default: 1)
- page
-
Specifies the initial page number of the (multi page) document. The page number is set with the Adobe DSC comments, and is used nowhere else. It only makes finding your pages easier. See also the
newpagemethod. (Default: 1) - reencode
-
Requests that a font re-encode function be added and that the 13 standard PostScript fonts get re-encoded in the specified encoding. The most popular choice (other than undef) is 'ISOLatin1Encoding' which selects the iso8859-1 encoding and fits most of western Europe, including the Scandinavia. Refer to Adobes Postscript documentation for other encodings.
The output file is, by default, re-encoded to ISOLatin1Encoding. To stop this happening, use 'reencode => undef'.
Example:
$ref = new PostScript::Simple(landscape => 1, eps => 0, xsize => 4, ysize => 3, units => "in");Create a document that is 4 by 3 inches and prints landscape on a page. It is not an EPS file, and must therefore use the
newpagemethod.$ref = new PostScript::Simple(eps => 1, colour => 1, xsize => 12, ysize => 12, units => "cm", reencode => "ISOLatin1Encoding");Create a 12 by 12 cm EPS image that is in colour. Note that "
eps => 1" does not have to be specified because this is the default. Re-encode the standard fonts into the iso8859-1 encoding, providing all the special characters used in Western Europe. Thenewpagemethod should not be used.
OBJECT METHODS
All object methods return 1 for success or 0 in some error condition (e.g. insufficient arguments). Error message text is also drawn on the page.
newpage([number])-
Generates a new page on a PostScript file. If specified,
numbergives the number (or name) of the page. This method should not be used for EPS files.The page number is automatically incremented each time this is called without a new page number, or decremented if the current page number is negative.
Example:
$p->newpage(1); $p->newpage; $p->newpage("hello"); $p->newpage(-6); $p->newpage;will generate five pages, numbered: 1, 2, "hello", -6, -7.
output(filename)-
Writes the current PostScript out to the file named
filename. Will destroy any existing file of the same name.Use this option whenever output is required to disk. The current PostScript document in memory is not cleared, and can still be extended.
setcolour((red, green, blue)|(name))-
Sets the new drawing colour to the values specified in
red,greenandblue. The values range from 0 to 255.Alternatively, a colour name may be specified. Those currently defined are listed at the top of the PostScript::Simple module in the
%pscolourshash.Example:
# set new colour to brown $p->setcolour(200,100,0); # set new colour to black $p->setcolour("black"); setlinewidth(width)-
Sets the new line width to
widthunits.Example:
# draw a line 10mm long and 4mm wide $p = new PostScript::Simple(units => "mm"); $p->setlinewidth(4); $p->line(10,10, 20,10); line(x1,y1, x2,y2 [,red, green, blue])-
Draws a line from the co-ordinates (x1,x2) to (x2,y2). If values are specified for
red,greenandblue, then the colour is set before the line is drawn.Example:
# set the colour to black $p->setcolour("black"); # draw a line in the current colour (black) $p->line(10,10, 10,20); # draw a line in red $p->line(20,10, 20,20, 255,0,0); # draw another line in red $p->line(30,10, 30,20); linextend(x,y)-
Assuming the previous command was
line,linextend,curveorcurvextend, extend that line to include another segment to the co-ordinates (x,y). Behaviour after any other method is unspecified.Example:
$p->line(10,10, 10,20); $p->linextend(20,20); $p->linextend(20,10); $p->linextend(10,10);Notes
The
polygonmethod may be more appropriate. polygon(["options",] x1,y1, x2,y2, ..., xn,yn [,filled])-
The
polygonmethod is multi-function, allowing many shapes to be created and manipulated. Polygon draws lines from (x1,y1) to (x2,y2) and then from (x2,y2) to (x3,y3) up to (xn-1,yn-1) to (xn,yn).If the value of
filledis 1 then the PostScript output is set to fill the object rather than just draw the lines.The options may be set as follows. Note that no whitespace may appear in each option, which must be specified as name=value1,value2. Each option must be separated by whitespace.
- rotate=angle[,x,y]
-
Rotate the polygon by
angledegrees anti-clockwise. If x and y are specified then use the co-ordinate (x,y) as the centre of rotation, otherwise use the co-ordinate (x1,y1) from the main polygon. - filled=1
-
Synonymous with the option
filledabove. - offset=x,y
-
Displace the object by the vector (x,y).
Example:
# draw a square with lower left point at (10,10) $p->polygon(10,10, 10,20, 20,20, 20,10, 10,10); # draw a filled square with lower left point at (20,20) $p->polygon("offset=10,10", 10,10, 10,20, 20,20, 20,10, 10,10, 1); # draw a filled square with lower left point at (10,10) # rotated 45 degrees $p->polygon("rotate=45 filled=1", 10,10, 10,20, 20,20, 20,10, 10,10); circle(x,y, r [,filled])-
Plot a circle with centre at (x,y) and radius of r.
If
filledis 1 then fill the circle.Example:
$p->circle(40,40, 20); box(x1,y1, x2,y2 [,filled])-
Draw a rectangle from lower left co-ordinates (x1,y1) to upper right co-ordinates (y1,y2). If
filledis 1 then fill the rectangle.Example:
$p->box(10,10, 20,30);Notes
The
polygonmethod is far more flexible, but this method is quicker! setfont(font, size)-
Set the current font to the PostScript font
font. Set the size in PostScript points tosize.Notes
This method must be called on every page before the
textmethod is used. text(x,y, string[, alignment])-
Plot text on the current page with the lower left co-ordinates at (x,y) and using the current font. The text is specified in
string.Alignment can be 'left', 'centre' and 'right', if not defined it will default to 'left'.
Example:
$p->setfont("Times-Roman", 12); $p->text(40,40, "The frog sat on the leaf in the pond."); $p->text(140,40, "This is centered.",'centre'); - curve( x1, y1, x2, y2, x3, y3, x4, y4 )
-
Create a curve from (x1, y1) to (x4, y4). (x2, y2) and (x3, y3) are the control points for the start- and end-points respectively.
- curvextend( x1, y1, x2, y2, x3, y3 )
-
Assuming the previous command was
line,linextend,curveorcurvextend, extend that path with another curve segment to the co-ordinates (x3, y3). (x1, y1) and (x2, y2) are the control points. Behaviour after any other method is unspecified. - newpath
-
This method is used internally to begin a new drawing path - you should generally NEVER use it.
- moveto( x, y )
-
This method is used internally to move the cursor to a new point at (x, y) - you will generally NEVER use this method.
BUGS
Some current functionality may not be as expected, and/or may not work correctly.
More functions need to be added. See the README file.
AUTHOR
The PostScript::Simple module was initially created by Matthew Newton, with ideas and suggestions from Mark Withall.
Contributions to the work (either code or suggestions / improvements) have been gratefully received from: Andreas Marcel, P Kent, Flemming Frandsen, Michael Tomuschat