LJ Archive

GNU Ghostscript

Robert A. Kiesling

Issue #47, March 1998

Need to preview and print PostScript Files? Here's a utility that will do just that.

Viewing an Encapsulated PostScript recycling logo in a TeX document should have been simple enough. Instead, a blank space marked the position on the display where the logo should have appeared. A quick look through all of my LaTeX documentation, including the xdvi man page, provided no answer. There seemed to be no way for xdvi to display a TeX \special command in the dvi output; in this case, an embedded PostScript image. I considered this to be a major shortcoming of xdvi, and, of course, TeX. After all, isn't combining text and graphics on a page part of what computer publishing is all about? Why should TeX, one of the most sophisticated typesetting systems in the world, be constrained by the lack of a PostScript previewer? Or the requirement for a PostScript printer, for that matter? It is said that the longest journey begins with a single step. The journey I'm about to describe has brought me a considerable distance toward professional-quality publishing, via a utility that had been sitting quietly in a subdirectory of my /usr partition all along.

The utility is Ghostscript. GNU Ghostscript, Version 3.33, is the Swiss Army knife of graphics programs. Essentially, it's a PostScript language interpreter. More specifically, Ghostscript renders PostScript code to any bitmapped device on your system, whether it be the screen, printer, or G3 fax file. The man page says, “Ghostscript is a programming language similar to Adobe System's PostScript language, which is in turn similar to Forth.” (This statement comes almost verbatim from the book: Adobe Systems, Incorporated: PostScript: Language Tutorial and Cookbook, Addison-Wesley, 1986.) Ghostscript also substitutes fonts from its own library when rendering documents if the fonts are not otherwise available, scales and rotates text, and performs any of the other typographical feats for which PostScript is famous. Despite the apparent complexity of the task, no knowledge of the PostScript language is required. For the most part, a little experimentation with the command-line options is all that's needed. Yet Ghostscript's PostScript interpreter is fully accessible.

If I want to print that EPS recycling logo at the bottom of a page of DVI output, I would insert the following commands in my LaTeX input file:

\usepackage{graphics} ... \vfil \includegraphics{recycle.eps}

Then, it is simply a matter of running the input file through teTeX, using the command line:

pslatex letter.tex
and then converting teTeX's dvi output to PostScript with dvips:
dvips -f <letter.dvi >letter.ps
The -f command-line option tells dvips to act as a filter, reading from standard input and writing to standard output. Now, we have a PostScript file ready for printing. Ghostscript images this file in a format that's understandable to any bitmapped device on your system, whether it is a VGA display or a laser printer.

Ghostscript's command-line options are numerous. Look at ghostscript's USE.DOC file for an complete description. You can view a brief summary with the command:

gs -help | less

For example, if I want to print the file letter.ps on a Hewlett Packard DeskJet printer, I would use the command line:

gs -q -dNOPAUSE -sDEVICE=deskjet
/-sOutputFile=-\
         letter.ps quit.ps >gs.out
The first option, -q, suppresses messages to the console. However, to run Ghostscript in batch mode, the -q option on its own is not enough. That's because Ghostscript uses its own PostScript code at run time to output the image.

From the command line, we define several variables, the first being NOPAUSE, which tells Ghostscript's showpage() routine not to pause after each page is output. When viewing output on a screen, it is best to leave NOPAUSE unset and let Ghostscript prompt you to view each page in turn.

Next, we set the DEVICE variable. In the example above, the output device is a HP Deskjet. Ghostscript's dictionary has output parameters defined for all the devices documented in its command-line help.

The next variable which needs defining is OutputFile. Here, we set it to - or standard output. This is the file to which Ghostscript will write its image. In most instances, however, Ghostscript provides no means for that image to be displayed. We'll get to that in a moment. Then we specify letter.ps on the command line as our target input file.

When we write a file with the pslatex command, LaTeX uses the font metrics available to it; that is, it uses Computer Modern Roman as its default font. However, the pslatex command has been implemented in teTeX so that dvips, which does the DVI to PostScript conversion, will request PostScript fonts. These fonts can be provided via either the printer or via software. In the latter case, Ghostscript produces the output image using Type 1 fonts from its own library.

Finally, we input the file quit.ps. This file is part of Ghostscript's standard library and is simply the command quit followed by a newline character. It is read into the Ghostscript interpreter just like any other input.

Viewing Images

Viewing images on-screen is only a little more difficult. Ghostscript for Linux comes with both X11 and SVGALib support. The executables for both versions should be in the /usr/bin directory: gs-with-X11 and gs-without-X11. You can run either version using its actual file name, but the more common implementation uses a soft link of the actual executable to the gs command. In either case, the executable's permissions should be setuid root so it can access the display. We'll describe how to use Ghostscript as a PostScript previewer with both the X Window System and SVGALib.

Ghostscript with X11

The X Window System provides a lot of the display support that Ghostscript would need to provide otherwise. As a result, it is by far the way to use Ghostscript as a previewer. As with the above example, we want to pass letter.tex through LaTeX and then convert the output to PostScript with the command line:

pslatex letter.tex; \
        dvips -f <letter.dvi >letter.ps

Again, we need to specify the DEVICE string to gs, which is simply X11 for the X display. Ghostscript treats an X11 display frame as the standard output, and the X display services provide the geometry to display an entire page. A virtual window manager like FVWM already provides the facilities to scroll the view over the entire page. The ghostscript command, then, is simply:

gs -r72 -sDEVICE=X11 letter.ps
This will provide us with an actual-size page on the screen, because we've overridden the default resolution with the -r switch.

A VGA display provides about 72 dots per inch resolution, so a legible, actual-size full U.S. letter page will not fit on the screen. This is why we rely on FVWM or another virtual window manager to scroll the view across the entire page. Ghostscript draws a page-high window on your X display. About half of the page is visible at a time. You can, of course, view the full page at twice its actual size by specifying your output resolution as 36dpi.

The simplest way to execute the Ghostscript command is in an xterm window. The page appears in a window which displays the child process of the Ghostscript command. Ghostscript writes its messages to standard error, which here is the xterm. Pressing enter in the xterm window tells showpage to display each successive page of Ghostscript output. You can set the default resolution in your ~/.Xdefaults file by adding the lines:

Ghostscript*xResolution: 72 Ghostscript*yResolution: 72

and then merge the defaults with the other X server defaults:

xrdb -merge ~/.Xdefaults
If the resolution isn't specified, the page is displayed on the screen at 300dpi, about one-quarter its actual size, which is visible in a 640x480 view without scrolling.

Ghostscript with SVGALib

Things get a little more difficult when using a VGA display without X11 support. A standard Linux tty device provides no ready-made provision for paging over a full, U.S. letter-size image. Also, Ghostscript's SVGALib routines must be provided with geometry and resolution information to preview images. The following information is specific to my Compaq laptop, Chanel3, which has a 16 color, 640x480 standard VGA display, GNU Ghostscript Version 3.33 and SVGALib version libvga.so.1.2.10. You'll need to adjust the parameters to suit your hardware, but the basic procedure should be similar.

The settings that Ghostscript recognizes for various hardware configurations are listed in use.doc file and the gs man page. The DEVICE string is “linux” for a Linux virtual console; that is:

/dev/tty1 - /dev/tty9

This string corresponds to the virtual console's /etc/termcap entries. The -r resolution, parameter is one of several dozen VGA modes that SVGALib recognizes. Ghostscript defines single-digit mode numbers which correspond to standard width-by-height notation. There is a complete list in the Ghostscript man page. In this case, Mode 4 is 16-color, 640x480 VGA. Much of the following information depends on whether SVGALib provides information on display geometry for your particular display. Ghostscript requires this information to display anything. It must be provided, for one thing, with the aspect ratio of the display (the ratio of the display's width to its height) in order to scale the fonts correctly.

The geometry parameter (-g) tells Ghostscript the display dimensions. Ghostscript scales the output page to the geometry we specify. We'll address this problem in a moment. With these parameters set, our Ghostscript command is:

gs -sDEVICE=linux -r4 -g640x480 letter.ps

Remember that gs is really a link to the executable file /usr/bin/gs-without-X11. Here, we tell Ghostscript to display a full page by specifying a display geometry that's twice the size of the actual screen. This gives us a page that is slightly more than twice as large as the video display. To double the size of the virtual display—the “device space” in PostScript jargon—we use the command line:

gs -sDEVICE=linux -r4 -g640x960 letter.ps
which gives us an actual-size view of the top half of the printed page. To view the bottom half of the page, we can specify an offset of the image's Y origin as half of a U.S. letter-size page:
gs -sDEVICE=linux -r4 -g640x960 -dYO=5.5
letter.ps
The default units are inches. The image's X origin can be shifted similarly.

Ghostscript as a Document Post-Processor and Previewer

Integrating Ghostscript into your system is not that difficult. For example, if you routinely write documents in Emacs' LaTeX mode, the following bash script takes the DVI output of Emacs' tex-buffer command, converts it to PostScript, and then post-processes the output through Ghostscript. Finally, it sends the output to the print spooler. This script, gsprint (see Listing 1), can be called by Emacs' tex-print command directly. Note that the commands which call Ghostscript and then spool the output to the lpr daemon should all be typed on one line.

An even shorter version of this script, gspreview (see Listing 2), previews the document and can be called by Emacs' tex-view command under X11. Emacs provides the name of the TeX DVI file as the argument to its tex-print and tex-view commands. All you need to do is specify the names of the external commands. First, make sure that the scripts are located in a directory in the search path (I use /usr/local/bin for my shell scripts). Give them execute permission with the command:

chmod a+x gsprint gspreview

Then add the elisp code shown in Listing 3 to your .emacs file. Whenever you use the tex-print or tex-view commands (ctrl-c ctrl-p and ctrl-c ctrl-v, respectively) in TeX-mode or LaTeX-mode, these shell scripts are called and their commands executed, using the DVI output of the most recent TeX command.

The next bash script, which I named pvga (see Listing 4), uses Ghostscript to preview output on non-X VGA displays. It takes as its argument the name of the TeX DVI output file and two optional arguments: a list of pages to be output and the Y-origin offset for each page. This script can be run from the command line or used as the core routine of a more complex VGA previewer. The list of pages that you want to view, formatted according to the dvips documentation, must be specified before the Y offset.

PostScript in a (Virtual) Box

You can easily replace TeX's Computer Modern fonts with Ghostscript's scalable fonts. By default, dvips calls the MakeTeXPK program, which in turn calls MetaFont, to generate the physical Computer Modern fonts not present on the hard disk.

Printing is faster with bitmap fonts rather than scalable fonts, but scalable fonts that use Adobe's standard encodings provide the complete Adobe character set, including kerning and ligature pairs, which the Computer Modern fonts do not provide. With reasonably fast hardware, you can turn off dvips' font-generation feature and hardly notice a difference in speed. Dvips provides the -V command line switch for this purpose. The bash script vgspreview (see Listing 5) is a modification of gspreview, above. Remember to specify zero after the -V switch, which turns the font generation facility off.

Conclusion

There are many other tasks that Ghostscript can perform with ease:

  1. Create faxes.

  2. Create PDF files that can be read by Adobe's Acrobat reader.

  3. Generate a number of different graphics formats.

  4. Work with other companies' GUI displays, notably Windows and Macintosh.

Since Ghostscript interprets the PostScript language, you can program directly in PostScript, either via Ghostscript's command interpreter or with \special commands embedded in your TeX and LaTeX files. This article has only scratched the surface of the capabilities of this free program and the many ways in which Ghostscript can perform feats of industry-standard imaging right on your desktop.

Glossary

When Robert Kiesling is not involved with the complexities of PostScript and TeX, he is at work on his “real” writing. This includes several novels, as well as fiction, poetry and nonfiction, which have appeared in literary magazines and newspapers nationwide. When he is not busy with either of the above, he is occupied by maintaining the Linux FAQ, providing editorial support to small presses and answering e-mail at rkies@cpan.org.

LJ Archive