Linux
by Kamran Husain
IN THIS CHAPTER
- What Is Ghostscript?
- Installation
- The Ghostscript Interpreter
- Output Devices
- The Screen Blanker
- Sending Output to a File
- Paper Size Configuration
- Environment Variables for Ghostscript
- Configuring for X Window
- Command-Line Arguments
- Using Other Fonts
- Getting More Help
- Comments on Output
This chapter covers the installation and use of Ghostscript, a PostScript interpreter.
Almost all the documentation that is related to Linux is in PostScript. If you have
a PostScript printer, you simply send the file to the printer for a hardcopy.
Ghostscript is actually a set of programs. Collectively, these programs provide
two important features:
- An interpreter for the PostScript language
- A library to display PostScript images
-
NOTE: PostScript is a
trademark of Adobe Systems, Incorporated.
Ghostscript has been ported to several platforms from PCs, OS/2, Apple Macintoshes,
and Ataris to various flavors of UNIX, for example, Sun workstations and IBM AIX
machines. If you support X11, you will probably be able to run Ghostscript. Now you
have it on your Linux machine!
Several executable and font files come with Ghostscript. Some of the executables
are also used by other packages under Linux. For example, HylaFAX (Chapter 64, "HylaFAX")
uses the main Ghostscript program, gs, as its default viewer. In short, if you use
Linux, you will find a need for Ghostscript.
The Ghostscript package that you get with Linux is from Aladdin Enterprises. The
information about Ghostscript is best summarized in its README file: "L.
Peter Deutsch, president of Aladdin Enterprises, was the original creator, and is
the primary developer and maintainer, of Ghostscript. Aladdin Enterprises owns the
copyright on Ghostscript; Ghostscript is distributed with the GNU General Public
License, and is also available for commercial licensing." Here is the primary
contact for getting information about Ghostscript:
Aladdin Enterprises
P.O. Box 60264
Palo Alto, CA 94306
To run Ghostscript, you need the executable program and some external initialization
files:
/usr/lib/ghostscript/bdftops.ps
/usr/lib/ghostscript/decrypt.ps
/usr/lib/ghostscript/font2c.ps
/usr/lib/ghostscript/gs_dbt_e.ps
/usr/lib/ghostscript/gs_dps1.ps
/usr/lib/ghostscript/gs_fonts.ps
/usr/lib/ghostscript/gs_init.ps
/usr/lib/ghostscript/gs_lev2.ps
/usr/lib/ghostscript/gs_statd.ps
/usr/lib/ghostscript/gs_sym_e.ps
/usr/lib/ghostscript/gs_type0.ps
/usr/lib/ghostscript/gslp.ps
/usr/lib/ghostscript/impath.ps
/usr/lib/ghostscript/landscap.ps
/usr/lib/ghostscript/level1.ps
/usr/lib/ghostscript/prfont.ps
/usr/lib/ghostscript/ps2ascii.ps
/usr/lib/ghostscript/ps2epsi.ps
/usr/lib/ghostscript/ps2image.ps
/usr/lib/ghostscript/pstoppm.ps
/usr/lib/ghostscript/quit.ps
/usr/lib/ghostscript/showpage.ps
/usr/lib/ghostscript/type1ops.ps
/usr/lib/ghostscript/wrfont.ps
/usr/lib/ghostscript/uglyr.gsf
/usr/lib/ghostscript/Fontmap
The Ghostscript file set includes a set of fonts (.gsf files). The fonts
for Ghostscript are stored in /usr/lib/ghostscript/fonts. You should have
them on your disk if you chose to install Ghostscript when you installed Linux. The
name of the executable is gs.
You can build Ghostscript on your machine if you get a later version than what
you have. The versions have not changed in a while, so the files on the CD-ROM will
most likely be the ones you want to work with. The makefiles that come with the sources
install all the files and directories under your /usr/local directory.
The Ghostscript interpreter is the gs command in /usr/bin/gs.
This is an interactive interpreter that also reads in files for its input. To invoke
the gs interpreter, use this command:
gs [filename1] ... [filenameN]
Each file should contain PostScript data. The interpreter reads in the files in
sequence and interprets them as PostScript files. The gs interpreter continues to
read files from the input after the files have been read, so you can issue more PostScript
commands directly if you want to.
To stop the interpretation, just type quit and press Enter. The interpreter
also exits gracefully if it encounters end-of-file (Ctrl-D) or the interrupt character
(Ctrl-C).
See Figure 25.1 for the input to the interpreter. (Your version number might be
different than that shown in the figure.) The output is shown in a separate window,
as shown in Figure 25.2. The file we are viewing in this example is the PostScript
output from a C program that generates images in 3D. I wrote this program to generate
3D images and plots of some devices via a program that talked directly to a PostScript
printer. Rather than waste paper, I used gs to view the intermediate results. My
file was called samp1.ps. This is the command I used:
$ gs samp1.ps
FIGURE
25.1. The gs interpreter.
The way Ghostscript's interpreter, gs, works is that it reads and interprets PostScript
files. The resulting image data is sent to an output device. In the general case,
the output is sent to your display area. You are not, however, limited to just one
output device because Ghostscript can be built with support of output to multiple
output devices. Ghostscript normally opens the first listed device and directs all
output to it.
If you type gs without a filename, you are placed directly into the interactive
interpreter. All the commands you type in the interpreter are acted on immediately
by gs and are rendered in the display window. You will rarely use this option unless
you want to override something like a device setting within a program.
A sister program, called ghostview, comes with Ghostscript. With ghostview you
can look at PostScript files.
FIGURE
25.2. The output from the gs interpreter.
Ghostview was written by, and is copyrighted by, Tim Theisen. The ghostview
command shows PostScript documents using Ghostscript. Use the following command line
to view a text file:
$ ghostview [filename]
The ghostview program creates an X window, opens the file, and displays it. Ghostview
reads from stdin if the filename is -. Ghostview has a bewildering
number of options, but you don't have to use any of them. Most of the options are
there to override any X resources that you set in .Xdefaults. Let's look
at the main window, which consists of a main viewer and control menus, as shown in
Figure 25.3. Now look at the same PostScript file shown in Figure 25.2, and see how
it looks when viewed using ghostview. The difference between the two outputs is that
you can view PostScript files in a native X window with gs. With ghostview, you have
some more power to magnify or shrink the output, view multiple pages, and so on.
A typical zoomed output is shown in Figure 25.4.
Clicking anywhere within the viewport causes a zoom window to pop up. The window
is centered on the location that was clicked. Clicking with the left mouse button
pops up a low-resolution zoom window; with the center mouse button, a medium-resolution
zoom window; and with the right mouse button, a high-resolution zoom window.
The important point is that ghostview needs gs for its rendering. You cannot run
ghostview without gs. You can run gs without ghostview, but your viewing actions
are quite limited.
FIGURE
25.3. Output from the ghostview
program.
FIGURE
25.4. Zoomed output from the ghostview
program.
The output from gs is in the native format of the device it thinks it's talking
to. You do not have to run X Window to be able to use the output from gs. The gs
program supports many different output devices. (You do, however, need to run X Window
to be able to use ghostview.)
To list all available devices, type devicenames == in the interactive
environment.
The type of device for gs is set in its command-line argument, sDEVICE,
or via the environment variable, GS_DEVICE. For example, to set the device
to a deskjet, you would use something like this:
-sDEVICE=deskjet
-
NOTE: Command-line arguments
to gs can appear anywhere on the command line. After these arguments have been specified,
they apply to all subsequent files listed in the command line. These arguments do
not apply to any files specified before the argument. So if you have important arguments
like sDEVICE, specify these arguments before you specify the PostScript
file.
-
TIP: PostScript files
are usually specified with the .ps extension.
The following command generates PostScript output for a specific device:
gs -sDEVICE=deskjet showme.ps
In Linux, you do not have to be running X to be able to use gs. The device for
Linux is called linux. The device is really the svgalib driver, and you
must have permissions set for being able to write to your console. This is the command
to display a PostScript file on your console:
$ gs -sDEVICE=linux filename.ps
You are likely to run into an error if your monitor does not support full color
at 1024x768, the default output resolution. You can change the resolution to a more
reasonable one with the -r option. Thirty options are listed in the help
docs for gs, ranging from 320x200x16 all the way to 1280x1024x64K. The one that works
best for text documents is 640x480x2 for a black-and-white rendering on a standard
VGA. Your mileage may vary. The command to render my PostScript then becomes:
$ gs -sDEVICE=linux -r=640x480x16 filename.ps
The magnification factor can be set with the -dMAGSTEP option. A value
less than 1.0 shrinks the rendered image; a value greater than 1.0 expands it. For
example, to increase the magnification to 2 times the original output, you can use
the following command:
$ gs -sDEVICE=linux -r=640x480x16 -dMAGSTEP=2.0 filename.ps
This command shrinks the image to half its size:
$ gs -sDEVICE=linux -r=640x480x16 -dMAGSTEP=0.5 filename.ps
The image can be offset from the upper-left-corner default with the -dX0=
and -dY0= options. Values are given in inches, unless you append with cm
for centimeters, or with a percent (%) sign as a percentage of the drawing
area.
If you find that the screen blanks out after rendering its image, you have one
showpage directive too many in the PostScript file. A PostScript generating
program generates an extra showpage to force a printer to kick out its recently rendered
image. As far as gs is concerned, it prints a whole new screen for you. So if you
want to avoid this refresh, you should remove the last showpage directive
in your source file.
Devices do not have to be listed on the command line with the sDEVICE
option. You can define the GS_DEVICE environment variable to be your default
device name. This is the order of precedence for these device options:
- 1. Consider any device set in the interactive environment with the select
device command. If it's not set, move to the next step.
2. Consider using the command-line argument to the -sDEVICE= option.
If it's not set, move to the next step.
3. Use the setting in the GS_DEVICE environment variable. If it's
not set, move to the next step.
4. If none of the three preceding options can be used, use the first device
in the list of linked devices.
You can save the output from gs to a file or pipe it to a device with the -sOUTPUTFILE=
or -sOutputFile= option. To send the output to a single file, mine.out,
with all the pages concatenated, use this command-line argument:
-sOutputFile=mine.out
To send the output to a printer, use this argument:
-sOutputFile=\|lpr
To send output to standard output, use this option:
-sOutputFile=-
(In this case, you must also use the -q switch to prevent Ghostscript
from writing messages to stdout.)
Ghostscript defaults the output page to a U.S. letter-size paper. The command
argument is -sPAPERSIZE=. The list of available paper sizes is listed in
/usr/lib/ghostscript/gs_statd.ps, which I could not get gs to display. Some
paper sizes that worked are -sPAPERSIZE=a4, -sPAPERSIZE=legal,
and -sPAPERSIZE=11x17. A file's specification overrides any command-line
argument specification for a page size.
Ghostscript uses the environment variables GS_LIB, GS_LIB_DEFAULT,
and GS_FONTPATH to find files. The names of locations are separated by colons
just as with the PATH variable. The gs program always looks in the current
directory for a file, and then it looks in the directories listed in the GS_LIB
variable. If nothing for the file is found via GS_LIB, it looks via GS_LIB_DEFAULT.
The GS_FONTPATH environment variable lists the locations of PostScript font
files to add to gs's fonts. You can override these paths with the -I command-line
argument to gs.
Temporary files for gs are created in the /tmp directory. gs leaves quite
a few dangling temporary files at times. The names of such files begin with gs_.
If you see too many of these files in /tmp space, you can delete them by
using the rm /tmp/gs_* command.
Ghostscript looks for the resources under the program name ghostscript and class
name Ghostscript in the .Xdefaults file. To set a resource, put the resource
in .Xdefaults in the following form:
Ghostscript*resourceName: value
Force the server to use these values with this command:
% xrdb -merge ~/.Xdefaults
Table 25.1 lists the available resources. Table 25.1. Ghostscript
resources.
Name |
Class |
Default |
background |
Background |
white |
foreground |
Foreground |
black |
borderColor |
BorderColor |
black |
borderWidth |
BorderWidth |
1 |
geometry |
Geometry |
NULL |
xResolution |
Resolution |
calculated |
yResolution |
Resolution |
calculated |
useExternalFonts |
UseExternalFonts |
true |
useScalableFonts |
UseScalableFonts |
true |
logExternalFonts |
LogExternalFonts |
false |
externalFontTolerance |
ExternalFontTolerance |
10.0 |
palette |
Palette |
Color |
maxGrayRamp |
MaxGrayRamp |
128 |
maxRGBRamp |
MaxRGBRamp |
5 |
useBackingPixmap |
UseBackingPixmap |
true |
useXPutImage |
UseXPutImage |
true |
useXSetTile |
UseXSetTile |
true |
Here are a few points on these resources:
- The geometry resource affects only window placement. It does not affect the position
of the image on the display in the window.
- All resolution numbers are given in pixels per inch.
- The maxRGBRamp and maxGrayRamp control the maximum number of
colors that Ghostscript allocates ahead of time for dithering.
Ghostscript takes a lot of command-line arguments. Generally, you would want to
put these in a shell script file instead of having to type them all the time. Some
of the most often-used arguments are listed here. For a comprehensive list, check
out the documents in /usr/lib/ghostscript/doc. Let's look at the most commonly
used arguments:
- @list Lists all the input filenames. This beats typing long names on
every execution of gs with lots of input files.
- -Idirectories Adds the list of directories at the head of the library
files' search path.
- -q Suppresses normal startup messages.
- - Uses standard input rather than a file.
- -dNOPAUSE Disables the prompt and pause at the end of each page.
Remember that almost all of these resources can be set with an equivalent command-line
argument. For example, using the -title argument is the same as setting
Ghostview.showTitle resource to True, whereas using the -notitle
argument is the same as setting the Title resource to False. Similarly,
you can turn the date on or off with the -date and -nodate arguments.
The versions of Ghostscript come with a set of default fonts you can use with
any program capable of reading PostScript Type 1 and Type 3 fonts. If you have created
your own Type 1 or Type 3 fonts, or have purchased some fonts from another source,
you can use these with Ghostscript. Place your fonts in the lib/ghostscript/fonts
directory. Then edit the lib/ghostscript/FontMap file to add a line telling
Ghostscript where to find the font. For example:
/Helvetica (hlv_xyz.pfb);
In this example, hlv_xyz.pfb is the name of the font file you've just
added. You do not have to reset any X resources for this to work. Simply place the
file in the directory, edit the FontMap, and restart gs.
The gs interpreter recognizes several arguments. You can get help regarding these
options either by reading the man pages or from within gs. Either the -h
or the -? option tells gs to print a help screen. The help screen also lists
all the available devices for gs on your system.
Ghostscript newsgroups abound. Search for ghostscript or gs in a list of newsgroup
names. Several files can be found in the /usr/lib/ghostscript/docs directory
that can provide more information. Here are some of these files:
- readme.doc Information about problems, major changes, and new features
in the current release.
- NEWS A detailed history of changes in the most recent Ghostscript releases.
- use.doc Information about how to install and use Ghostscript.
- devices.doc Detailed information about specific devices that Ghostscript
supports.
- unix-lpr.doc More detailed information about some of the shell scripts
and batch files distributed with Ghostscript.
- make.doc How to install, compile, and link Ghostscript.
- gs.1 The man page for Ghostscript.
- fonts.doc Information about the fonts distributed with Ghostscript,
including how to add or replace fonts.
- language.doc A description of the Ghostscript language and its differences
from the documented PostScript language.
- psfiles.doc Information about the .ps files distributed with
Ghostscript other than fonts.
- drivers.doc Information about the interface between Ghostscript and
device drivers.
- ps2epsi.doc Documentation for the PostScript-to-EPSI conversion utility.
Most commercial word processing packages can import EPSI documents for typesetting.
- hershey.doc Information about the Hershey fonts, which are the basis
of some Ghostscript fonts.
- history.doc A history of changes in older Ghostscript releases.
- lib.doc Information about the Ghostscript library.
You can generate PostScript output from various sources. A common output would
be generated from the pbm utilities (see Chapter 69, "Graphics Tools in Linux").
For example, to generate the PostScript output from the contents of a text file using
the pnmtops program, you can use the following command:
$ pbmtext < textfile.txt | pnmtops | ghostview -
Do not use this command:
$ pbmtext < textfile.txt | pnmtops | gs -
The gs program immediately terminates itself when it sees the end-of-file from
the text file. The ghostview program handles pages better and lets you zoom and scale
the image. It's really quite helpful to look at what the output will look like on
a black-and-white printer.
You can generate raw pbm files for use with the PBM file directly. Use
the following constants for each type of PBM file:
- -sDEVICE=pbm
- -sDEVICE=pbmraw
- -sDEVICE=pgm
- -sDEVICE=pgmraw
- -sDEVICE=pnm
- -sDEVICE=pnmraw
You can even generate bitmaps for use with Windows with -sDEVICE=bmp
options. Various types of bitmaps are supported: 1, 16, or 256 bit.
Also, the gs program can be used to generate Group 3 FAX files for output with
FAX cards. (The G3 generator with the PBM utilities does not work correctly.) Use
the -sDEVICE=dfaxhigh and -sDEVICE=dfaxlow device names to generate
these outputs and pump them into your FAX modem. I prefer to use sendfax directly
(see Chapter 64), but this is essentially how sendfax works. A modified version of
facsimile format is an enhanced TIFF file, for which you can use the -sDEVICE=tiffg3
file.
You can write your own programs to generate output for displaying PostScript directly.
My ps3d program reads its input from a text file and generates a 3D perspective drawing
with some limited surface rendering. It is by no means a complete commercial package
yet. I will place it in the public domain shortly. Understanding the workings of
this program requires some knowledge of programming in PostScript. This ps3d program
will be available via ftp from www.ikra.com as soon as it's thoroughly commented.
Basically, the sample program is run this way:
$ ps3d < myfile.scr | ghostview -
Or, if you prefer to store the output to a file instead, you can run the program
this way:
$ ps3d < myfile.scr > myout.ps.
This chapter scratched the surface of a very important PostScript processing program
called Ghostscript. The executable file for Ghostscript is called gs. You can use
gs to decode PostScript files for displaying or printing hard copies.
Ghostscript is also used by other programs as a filter for working with PostScript
printers that cannot switch from PostScript to text and back just by looking at a
document. In this case, the gs command is used to convert text to PostScript
with an output filter for lp before sending the text to a PostScript printer. As
another example, FlexFAX uses gs as its primary PostScript output filter program.
Ghostview is an X Window-based program that provides a canvas for Ghostscript
to write its output to. Ghostview enables you to scroll through a multipage document,
zoom in and out, and save marked pages. The capability to view EPSI or PostScript
via Ghostscript saves time and paper when you're typesetting documents.
Contact
reference@developer.com with questions or comments.
Copyright 1998
EarthWeb Inc., All rights reserved.
PLEASE READ THE ACCEPTABLE USAGE STATEMENT.
Copyright 1998 Macmillan Computer Publishing. All rights reserved.