NAME
PostScript::File - Base class for creating Adobe PostScript files
SYNOPSIS
use PostScript::File qw(check_tilde check_file
incpage_number incpage_roman);
At its simplest, an 'hello world' program:
use PostScript::File;
my $ps = new PostScript::File();
$ps->add_to_page( <<END_PAGE );
/Helvetica findfont
12 scalefont
setfont
72 300 moveto
(hello world) show
END_PAGE
$ps->output( "~/tests/first" );
DESCRIPTION
This module provides the outline for an Adobe PostScript file. Functions allow access to each of Adobe's Document Structuring Convention (DSC) sections and control how the pages are constructed. It is possible to construct and output files in either normal PostScript (*.ps files) or as Encapsulated Postscript (*.epsf or *.epsi files). By default a minimal file is output, but support for font encoding, postscript error reporting and debugging can be built in if required.
Documents can typically be built using only these functions:
new The constructor, with many options
add_function Add postscript functions to the prolog
add_to_page Add postscript to construct each page
newpage Begins a new page in the document
output Construct the file and saves it
The rest of the module involves fine-tuning this. Some settings only really make sense when given once, while others can control each page independently. See new for the functions that duplicate option settings, they all have get_ counterparts. The following provide additional support.
get/set_bounding_box
get/set_page_bounding_box
get/set_page_clipping
get/set_page_landscape
set_page_margins
set_margins
get_ordinal
get_pagecount
draw_bounding_box
clip_bounding_box
The functions which insert entries into each of the DSC sections all begin with 'add_'. They also have get_ counterparts.
add_comment
add_preview
add_default
add_resource
add_function
add_setup
add_page_setup
add_to_page
add_page_trailer
add_trailer
Finally, there are a few stand-alone functions. These are not methods and are available for export if requested.
check_tilde
check_file
incpage_number
incpage_roman
CONSTRUCTOR
- new( options )
-
Create a new PostScript::File object, either a set of pages or an Encapsulated PostScript (EPS) file. Options are hash keys and values. All values should be in the native postscript units of 1/72 inch.
Example
$ref = new PostScript::File (
eps => 1,
landscape => 1,
width => 216,
height => 288,
left => 36,
right => 44,
clipping => 1 );
This creates an encapsulated postscript document, 4 by 3 inch pages printing landscape with left and right margins of around half an inch. The width is always the shortest side, even in landscape mode. 3*72=216 and 4*72=288. Being in landscape mode, these would be swapped. The bounding box used for clipping would then be from (50,0) to (244,216).
In addition, the following keys are recognized which do not have corresponding set_ functions. First, four options which control how much gets put into the resulting file.
headings
-
Enable PostScript comments such as the date of creation and user's name.
errors
-
By default postscript fails silently. Setting this to 1 prints fatal error messages on the bottom left of the paper. For user functions, a postscript function report_error is defined. This expects a message string on the stack, which it prints before stopping.
debug
-
If set to 1, a range of functions are added to the file to support debugging postscript.
When this is 0, these same functions are replaced by dummy functions of the same name. This switch is similar to the 'C'
NDEBUG
macro in that debugging statements may be left in the postscript code but their effect is removed. Of course, being an interpreted language, it is not quite the same as the calls still takes up space - they just do nothing. See "POSTSCRIPT DEBUGGING SUPPORT" for details of the functions.A value of 2 not only loads the debug functions but calls them to give some reassuring output at the start and a stack dump at the end of each page.
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 only recognized value so far is 'ISOLatin1Encoding' which selects the iso8859-1 encoding and fits most of western Europe, including the Scandinavia.
Next, there are a few initialization settings that are only relevant when the file object is constructed.
left
bottom
right
top
-
These are margins in from the paper edges (before any landscape transformation) to specify non-printable areas on the paper. They are all positive offsets, so
left=36
andright=36
will leave a half inch no-go margin on each side of the portrait sheet. Remember to specifyclipping
if that is what is wanted. clipcmd
-
The bounding box is used for clipping if this is set to "clip" or is drawn with "stroke". This also makes the whole page area available for debugging output. (Default: "clip").
fontsuffix
-
This string is appended to each font name as it is reencoded. (Default: "-iso")
The standard fonts are named Courier, Courier-Bold, Courier-BoldOblique, Courier-Oblique, Helvetica, Helvetica-Bold, Helvetica-BoldOblique, Helvetica-Oblique, Times-Roman, Times-Bold, Times-BoldItalic, Times-Italic, and Symbol. The string value is appended to these to make the new names.
Example
$ps = new PostScript::File(
fontsuffix => "-iso",
reencode => "ISOLatin1Encoding"
);
"Courier" still has the standard mapping while "Courier-iso" includes the additional European characters.
Debugging support makes most sense in the postscript code rather than perl. However, it is convenient to be able to set defaults for the output position and so on. See "POSTSCRIPT DEBUGGING SUPPORT" for further details.
db_ytop
-
The top line of debugging output. Defaults to 6 below the top of the page.
db_base
-
Debug printing will not occur below this point. (Default: 6)
db_xpos
-
The left edge, where debug output starts. (Default: 6)
db_xtab
-
The amount indented by
db_indent
. (Default: 10) db_xgap
-
Typically, the output comprises single values such as a column showing the stack contents.
db_xgap
specifies the width of each column. By default, this is calculated to allow 4 columns across the page. db_color
-
This is the whole postscript command (with any parameters) to specify the colour of the text printed by the debug routines. (Default: "0 setgray")
db_font
-
The name of the font to use. (Default: "Courier")
db_fontsize
-
The size of the font. Postscript uses its own units, but they are almost points. (Default: 10)
db_bufsize
-
The size of string buffers used. Output must be no longer than this. (Default: 256)
db_active
-
Set to 0 to temporarily suppress the debug output. (Default: 1)
Finally, there are options which may be set later but are provided here for convenience. See the relevant set_ function for details.
Option Function
====== ========
eps set_eps
paper set_paper
width set_width
height set_height
clipping set_clipping
landscape set_landscape
file set_filename
dir set_filename
title set_title
version set_version
langlevel set_langlevel
extensions set_extensions
order set_order
page set_page_number
incpage_handler set_incpage_handler
errx set_error_position
erry set_error_position
errmsg set_error_message
errfont set_error_font
errsize set_error_fontsize
MAJOR METHODS
- newpage( [page] )
-
Generate a new PostScript page, unless in a EPS file when it is ignored.
If
page
is not specified the page number is increased each time a new page is requested.page
can be a string or a number. If anything other than a simple integer, you probably should register your own counting function with set_incpage_handler. Of course there is no need to do this if a page string is given to every newpage call. - output( [filename] )
-
Writes the current PostScript out to file. It is printed to STDOUT if no filename has been given either here, to new or set_filename.
Use this option whenever output is required to disk. The current PostScript document in memory is not cleared, and can still be extended.
ACCESS METHODS
Use these get_ and set_ methods to access a PostScript::File object's data.
- get_eps()
- set_eps( [eps] )
-
Inspect and change whether an Encapsulated or a normal PostScript file will be created. (Default: 0)
- get_filename()
- set_filename( file, [dir] )
-
file
-
An optional fully qualified path-and-file, a simple file name, or "" which stands for the special file File::Spec->devnull().
dir
-
An optional directory
dir
. If present (andfile
is not already an absolute path), it is prepended tofile
.
Specify the root file name for the output file(s) and ensure the resulting absolute path exists. This should not include any extension.
.ps
will be added for ordinary postscript files. EPS files have an extension of.epsf
without or.epsi
with a preview image.If
eps
has been set, multiple pages will have the page label appendend to the file name.
Example
$ps->new PostScript::File( eps => 1 );
$ps->set_filename( "pics", "~/book" );
$ps->newpage("vi");
... draw page
$ps->newpage("7");
... draw page
$ps->newpage();
... draw page
$ps->output();
The three pages for user 'chris' on a unix system would be:
/home/chris/book/pics-vi.epsf
/home/chris/book/pics-7.epsf
/home/chris/book/pics-8.epsf
It would be wise to use set_page_bounding_box explicitly for each page if using multiple pages in EPS files.
- get_paper()
- set_paper( [paper] )
-
Inspect and change the paper size of each page. A document can be created using a standard paper size without having to remember the size of paper using PostScript points. Valid choices are currently A0, A1, A2, A3, A4, A5, A6, A7, A8, A9, B0, B1, B2, B3, B4, B5, B6, B7, B8, B9, B10, Executive, Folio, 'Half-Letter', Letter, 'US-Letter', Legal, 'US-Legal', Tabloid, 'SuperB', Ledger, 'Comm #10 Envelope', 'Envelope-Monarch', 'Envelope-DL', 'Envelope-C5', 'EuroPostcard'. (Default: "A4")
This also sets width and height.
- get_width()
- set_width( width )
-
Inspect and change the page width, i.e. the shortest edge of the paper. (Default taken from paper)
The paper size is set to "Custom".
- get_height()
- set_height( height )
-
Inspect and change the page width, i.e. the longest edge of the paper. (Default taken from paper)
The paper size is set to "Custom".
- get_landscape()
- set_landscape( [landscape] )
-
Inspect and change whether the page is oriented horizontally (
1
) or vertically (0
). (Default: 0)In landscape mode the coordinates are rotated 90 degrees and the origin moved to the bottom left corner. Thus the coordinate system appears the same to the user, with the origin at the bottom left.
Note that this setting only affects new pages. Call set_page_landscape to alter existing pages.
- get_clipping()
- set_clipping( [clipping] )
-
Inspect and change whether printing will be clipped to the file's bounding box. (Default: 0)
- get_page_landscape( [page] )
- set_page_landscape( [[page,] landscape] )
-
Inspect and change whether the page specified is oriented horizontally (
1
) or vertically (0
). The default is the global setting as returned by get_landscape. Ifpage
is omitted, the current page is assumed. - get_page_clipping( [page] )
- set_page_clipping( [[page,] clipping] )
-
Inspect and change whether printing will be clipped to the page's bounding box. (Default: 0)
- get_page_number()
- set_page_number( [page] )
-
Inspect and change the number or label for the current page. (Default: "1")
This will be automatically incremented using the function set by set_incpage_hander.
- get_incpage_handler()
- set_incpage_handler( [handler] )
-
Inspect and change the function used to increment the page number or label. The following suitable values for
handler
refer to functions defined in the module:\&PostScript::File::incpage_number \&PostScript::File::incpage_roman
The default (incpage_number) increments numbers and letters, the other one handles roman numerals up to 39.
handler
should be a reference to a subroutine that takes the current page label as its only argument and returns the new one. Use this to increment pages using roman numerals or custom orderings. - get_order()
- set_order( [order] )
-
Inspect and change the order the pages have been defined. It should one of "ascend", "descend" or "special" if a document manager must not reorder the pages. (No default)
- get_ordinal( [page] )
-
Return the internal number for the page label specified. (Default: current page)
Example
Say pages are numbered "i", "ii", "iii, "iv", "1", "2", "3".
get_ordinal("i") == 0
get_ordinal("iv") == 3
get_ordinal("1") == 4
- get_pagecount()
-
Return the number of pages currently known.
- get_title()
- set_title( title )
-
Inspect and change the document's title as recorded in PostScript's Document Structuring Conventions. (No default)
- get_version()
- set_version( [version] )
-
Inspect and change the document's version as recorded in PostScript's Document Structuring Conventions. This should be a string with a major, minor and revision numbers. For example "1.5 8" signifies revision 8 of version 1.5. (No default)
- get_langlevel()
- set_langlevel( [langlevel] )
-
Inspect and change whether an Encapsulated or a normal PostScript file will be created. (No default)
- get_extensions()
- set_extensions( [extension] )
-
Inspect and change whether an Encapsulated or a normal PostScript file will be created. (No default)
- get_bounding_box()
- set_bounding_box( x0, y0, x1, y1 )
-
Inspect or change the bounding box for the whole document, showing only the area inside.
Clipping is enabled. Call with set_clipping with 0 to stop clipping.
- set_margins( left, bottom, right, top [, clip] )
-
An alternative way of changing the default bounding box. Unlike the options given to new, the parameters here are the gaps around the image, not the paper. So
left=36
will set the left side in by half an inch, this might be a short side iflandscape
is set.Clipping is enabled. Call with set_clipping with 0 to stop clipping.
- get_page_bounding_box( [page] )
- set_page_bounding_box( [page], x0, y0, x1, y1 )
-
Inspect or change the bounding box for a specified page. If
page
is not specified, the current page is assumed, otherwise it should be a page label already given to newpage or set_page_number. The page bounding box defaults to the paper area.Note that this automatically enables clipping for the page. If this isn't what you want, call set_page_clipping with 0.
- set_page_margins( [page], left, bottom, right, top )
-
An alternative way of changing a single page's bounding box. Unlike the options given to new, the parameters here are the gaps around the image, not the paper. So
left=36
will set the left side in by half an inch, this might be a short side iflandscape
is set.Note that this automatically enables clipping for the page. If this isn't what you want, call set_page_clipping with 0.
- get_error_position()
- set_error_position( x, y )
- get_error_message()
- set_error_message( message )
- get_error_fontsize()
- set_error_fontsize( size )
- get_error_font()
- set_error_font( font )
-
These provide a way of altering how postscript errors are reported on the page. It makes no sense to set these more than once as they are directly embedded into the postscript code.
CONTENT METHODS
- get_comments()
- add_comment( comment )
-
Most of the required and recommended comments are set directly, so this function should rarely be needed. It is provided for completeness so that comments such as
DocumentNeededResources:
can be added. The comment should be the bare PostScript DSC name and value, with additional lines merely prefixed by+
.
Example
$ps->add_comment("ProofMode: NotifyMe");
$ps->add_comment("Requirements: manualfeed");
$ps->add_comment("DocumentNeededResources:");
$ps->add_comment("+ Paladin");
$ps->add_comment("+ Paladin-Bold");
- get_preview()
- add_preview( width, height, depth, lines, preview )
-
Use this to add a Preview in EPSI format - an ASCII representation of a bitmap. If an EPS file has a preview it becomes an EPSI file rather than EPSF.
- get_defaults()
- add_default( default )
-
Use this to add any PostScript DSC comments to the Defaults section. These would be typically values like PageCustomColors: or PageRequirements:.
- get_resources()
- add_resource( type, name, params, resource )
-
type
-
A string indicating the DSC type of the resource. It should be one of Document, Resource, File, Font, ProcSet or Feature (case sensitive).
name
-
An arbitrary identifier of this resource.
params
-
Some resource types require parameters. See the Adobe documentation for details.
resource
-
A string containing the postscript code. Probably best provided a 'here' document.
Use this to add fonts or images. add_function is provided for functions.
Example
$ps->add_resource( "File", "My_File1",
"", <<END_FILE1 );
...postscript resource definition
END_FILE1
Note that get_resources returns all resources added, including those added by any inheriting modules.
- get_functions()
- add_function( name, code )
-
Add user defined functions to the PostScript prolog. Despite the name, it is better to add related functions in the same code section.
name
is an arbitrary identifier of this resource. Best used with a 'here' document:$ps->add_function( "My_Functions", <<END_FUNCTIONS ); % postscript code can be freely indented % as leading spaces and blank lines % (and comments, if desired) are stripped % foo does this... /foo { ... definition of foo } bind def % bar does that... /bar { ... definition of bar } bind def END_FUNCTIONS
Note that get_functions (in common with the others) will return all user defined functions possibly including those added by other classes.
- get_setup()
- set_setup( code )
-
Direct access to the
%%Begin(End)Setup
section. Use this forsetpagedevice
,statusdict
or other settings that initialize the device or document. - get_page_setup()
- set_page_setup( code )
-
Code added here is output before each page. As there is no special provision for %%Page... DSC comments, they should be included here.
Note that any settings defined here will be active for each page seperately. Use add_setup if you want to carry settings from one page to another.
- get_page( [page] )
- add_to_page( [page], code )
-
The main function for building the postscript output.
page
can be any label, typically one given to set_page_number. (Default: current page)If
page
is not recognized, a new page is added with that label. Note that this is added on the end, not in the order you might expect. So adding "vi" to page set "iii, iv, v, 6, 7, 8" would create a new page after "8" not after "v".
Examples
$ps->add_to_page( <<END_PAGE );
...postscript building this page
END_PAGE
$ps->add_to_page( "3", <<END_PAGE );
...postscript building page 3
END_PAGE
The first example adds code onto the end of the current page. The second one either adds additional code to page 3 if it exists, or starts a new one.
- get_page_trailer()
- set_page_trailer( code )
-
Code added here is output after each page. It may refer to settings made during set_page_setup or add_to_page.
- get_trailer()
- set_trailer( code )
-
Add code to the PostScript
%%Trailer
section. Use this for any tidying up after all the pages are output.
POSTSCRIPT DEBUGGING SUPPORT
This section documents the postscript functions which provide debugging output. Please note that any clipping or bounding boxes will also hide the debugging output which by default starts at the top left of the page. Typical new options required for debugging would include the following.
$ps = PostScript::File->new (
errors => "page",
debug => 2,
clipcmd => "stroke" );
The debugging output is printed on the page being drawn. In practice this works fine, especially as it is possible to move the output around. Where the text appears is controlled by a number of postscript variables, most of which may also be given as options to new.
The main controller is db_active
which needs to be non-zero for any output to be seen. It might be useful to set this to 0 in new, then at some point in your code enable it.
/db_active 1 def
(this will now show) db_show
At any time, the next output will appear at db_xpos
and db_ypos
. These can of course be set directly. However, after most prints, the equivalent of a 'newline' is executed. It moves down db_fontsize
and left to db_xpos
. If, however, that would take it below db_ybase
, db_ypos
is reset to db_ytop
and the x coordinate will have db_xgap
added to it, starting a new column.
The positioning of the debug output is changed by setting db_xpos
and db_ytop
to the top left starting position, with db_ybase
guarding the bottom. Extending to the right is controlled by not printing too much! Judicious use of db_active
can help there.
Postscript functions
- x0 y0 x1 y1 cliptobox
-
This function is only available if 'clipping' is set. By calling the perl method draw_bounding_box (and resetting with clip_bounding_box) it is possible to use this to identify areas on the page.
$ps->draw_bounding_box(); $ps->add_to_page( <<END_CODE ); ... my_l my_b my_r my_t clipbox ... END_CODE $ps->clip_bounding_box();
- msg report_error
-
If 'errors' is enabled, this call allows you to report a fatal error from within your postscript code. It expects a string on the stack and it does not return.
All the db_
variables (including function names) are defined within their own dictionary (debugdict
). But this can be ignored by all calls originating from within code passed to add_to_page (usually including add_function code) as the dictionary is automatically put on the stack before each page and taken off as each finishes.
- any db_show
-
The workhorse of the system. This takes the item off the top of the stack and outputs a string representation of it. So you can call it on numbers or strings and it will show them. Arrays are printed using
db_print
. - n msg db_nshow
-
This shows top
n
items on the stack. It requires a number and a string on the stack, which it removes. It prints outmsg
then the topn
items on the stack, assuming there are that many. Note that the stack contents is printed top first, the last item printed is the lowest one inspected. It can be used to do a labelled stack dump.count (at this point) db_nshow
- db_stack
-
Prints out the contents of the stack, top first. No stack requirements.
- array db_print
-
The closest this module has to a print statement. It takes an array of strings and/or numbers off the top of the stack and prints them with a space in between each item.
[ (myvar1=) myvar1 (str2=) str2 ] db_print
will print something like the following.
myvar= 23.4 str2= abc
When printing something from the stack you need to take into account the array-building items, too. In the next example, at the point '2 index' is given, the stack holds '222 111 [ (top=)' but '5 index' is required to get at 222 because the stack now holds '222 111 [ (top=) 111 (next=)'.
222 111 [ (top=) 2 index (next=) 5 index ] db_print
willl output this.
top= 111 next= 222
It is important that the output does not exceed the string buffer size. The default is 256, but it can be changed by giving new the option
bufsize
. - x y msg db_point
-
It is common to have coordinates as the top two items on the stack. This call inspects them. It pops the message off the stack, leaving x and y in place, then prints all three.
450 666 (starting point=) db_print moveto
would produce:
starting point= ( 450 , 666 )
- db_newcol
-
Starts the next debugging column. No stack requirements.
- db_down
-
Does a 'carriage-return,line-feed'. No stack requirements.
- db_indent
-
Moves output right by
db_xtab
. No stack requirements. Useful for indenting output within loops. - db_unindent
-
Moves output left by
db_xtab
. No stack requirements.
EXPORTED FUNCTIONS
- incpage_number( label )
-
The default function for set_incpage_handler which just increases the number passed to it. A useful side effect is that letters are also incremented.
- incpage_roman( label )
-
An alternative function for set_incpage_handler which increments lower case roman numerals. It only handles values from "i" to "xxxix", but that should be quite enough for numbering the odd preface.
- check_file( file, [dir, [create]] )
-
file
-
An optional fully qualified path-and-file or a simple file name. If omitted, the special file File::Spec->devnull() is returned.
dir
-
An optional directory
dir
. If present (andfile
is not already an absolute path), it is prepended tofile
. create
-
If non-zero, ensure the file exists. It may be necessary to set
dir
to "" or undef.
This ensures the filename returned is valid and in a directory tree which is created if it doesn't exist.
Any leading '~' is expanded to the users home directory. If no absolute directory is given either as part of
file
, it is placed within the current directory. Intervening directories are always created. Ifcreate
is set,file
is created as an empty file, possible erasing any previous file of the same name.File::Spec|File::Spec is used throughout so file access should be portable.
- check_tilde( dir )
-
Expands any leading '~' to the home directory.
BUGS
When making EPS files, the landscape transformation throws the coordinates off. To work around this, avoid the landscape flag and set width and height differently.
Not really a bug, but an inconvenience which may need to be changed. Calling set_margins immediately after new does not affect the first page, because it has already been created. It is necessary to call set_page_margins seperately to alter the first page.
Most of these functions have only had a couple of tests, so please feel free to report all you find.
AUTHOR
Chris Willmot, chris@willmot.org.uk
This module was adapted from Matthew Newton's PostScript::Simple.
SEE ALSO
PostScript Language Document Structuring Conventions Specification Version 3.0 published by Adobe, 1992.
Encapsulated PostScript File Format Specification Version 3.0 published by Adobe, 1992.
19 POD Errors
The following errors were encountered while parsing the POD:
- Around line 175:
You forgot a '=back' before '=head3'
You forgot a '=back' before '=head3'
- Around line 195:
=back without =over
- Around line 261:
You forgot a '=back' before '=head3'
- Around line 270:
=back without =over
- Around line 942:
'=item' outside of any '=over'
- Around line 981:
You forgot a '=back' before '=head3'
- Around line 1009:
'=item' outside of any '=over'
- Around line 1248:
You forgot a '=back' before '=head3'
- Around line 1269:
'=item' outside of any '=over'
- Around line 1476:
You forgot a '=back' before '=head1'
- Around line 1478:
'=item' outside of any '=over'
- Around line 1486:
You forgot a '=back' before '=head3'
- Around line 1502:
'=item' outside of any '=over'
- Around line 1566:
You forgot a '=back' before '=head3'
- Around line 1591:
'=item' outside of any '=over'
- Around line 1679:
You forgot a '=back' before '=head3'
- Around line 1717:
'=item' outside of any '=over'
- Around line 1894:
'=item' outside of any '=over'
- Around line 2022:
You forgot a '=back' before '=head1'