16. Ghost Script

Postscript is a language used for printers. Documents in postscript form are font commands and strings intermixed. Actual drawing programs, and embedded things are also possible

Ghostscript is a postscript interpreter, and graphics renderer (that's font rendering, and 2-d shape NOT 3D texture rendering - ok?).

gs has a number of device drivers. It can produce epson 9 or 24 pin dot-matrix escape codes (pages of graphics, a line at a time), or output for PCL printers, or SVGA_LIB display, or X11 display, or .pmb files.

Running gs -? gives you a list of devices and options. man gs explains them better. /usr/lib/ghostscript/doc explains a lot.

I installed it with other Slackware packages, during installation. To add packages, browse to where the package is, and run

	cd /cdrom/slakware/z9/. # where z9 is the releven disk directory
	installpkg pkgname.tgz  # where pkgname is the package name
installpkg used to have a problem updating index files, but it now works, and packages installed this way, appear in the listings, in the same way as those installed by the setup program.

16.1 Display tiger.ps on a linux graphics console using SVGA_LIB
16.2 Display tiger.ps on an X11 graphics console
16.3 Text or graphics
16.4 tk ref card - example ps
16.5 mkdir /tmp/temp
16.6 gs -dSAFER -sDEVICE=x11 tk.ps
16.7 gs -dSAFER -sDEVICE=x11 -r200 tk.ps
16.8 gs -dSAFER -sDEVICE=pbm -r180 tk.ps
16.9 xloadimage -rotate 270 tk.2.pbm
16.10 gs -dSAFER ...
16.11 Output to an epson 24 bin dot-matrix printer
16.12 /usr/lib/ghostscript/doc/*

16.1

Display tiger.ps on a linux graphics console using SVGA_LIB

	gs -dSAFER -sDEVICE=linux /usr/lib/ghostscript/examples/tiger.ps
or:
	gs -dSAFER -sDEVICE=linux -r 640x480x266 tiger.ps 

You can't be running mc-3 on the virtual console, because that gives you a different child virtual tty that is not a graphics console.

Use CTRL-C to interrupt the program. This is messy, but gs is really meant for batch convertion, not interactive chit-chat, but it does quite ok.

This looks very nice but it is graphics, not text. Ie graphics does look nice, but TEXT has to be readable, as well as having an overall look.

16.2

Display tiger.ps on an X11 graphics console

gs is an X11 utiltiy, not an application. All you get is an X11 window with a native x11 bitmap in it. The window can be easily moved. It can be closed by the desktop (Quit button from the frame). And not a lot else.

	gs -dSAFER -sDEVICE=x11 tiger.ps
or:
	gs -dSAFER -sDEVICE=x11 -r75 tiger.ps
The -r75, sets the scale factor resolution in dpi. A Higher number (eg 150) has more dots per inch (so you tell it), so it generates a bigger picture to cover the same dimensions. It used to be limited to 75 or 100, but any number seems to work.

16.3

Text or graphics

Graphics built from shapes, tends to scale well, however text is another problem. An A4 page reduced to your screen will be unreadable (it is on mine), so you use a scrollable viewer like ghostview and change the magnification.

There are two problems. The first is that the screen tries to be WYSIWYG, which is good for looking at page layouts, but is not so good for actually reading the text.

The second, is that the (automatically) selected font may not scale well to the required resolution, and produce hard to read characters.

Personally, I suspect that the fonts, or the font rendering is faulty. I may be wrong (I suspected the same with efax fonts until I looked at an xpm view). My theory is that at 300x300 dpi, the lost lines (and column pixels) are not noticable, but at 50 dpi it becomes so noticable, as to be unreadable. It may also depend on the font selected.

Running xmag to view a few characters, on a gs output page, and on a readable x11 text shows the difference. The letters appear to have lost their bottom line, or a line in the middle (eg across the center bar in 'e'). (xmag is available from the openwin desktop menu, click-right on the background (xearth :-), and click right on tools.

Here are a few examples, for you to see how screen displays will look. You make up your mind.

16.4

tk ref card - example ps

This is a free .ps file, with a useful list of tk command options. If you print it double-sided on A4 card, it becomes a foldable ref-card.

You can use any .ps file in these examples. I fetched ftp://ftp.aud.alcatel.com/tcp/docs/tkrefcard.tar.gz, which contained a pair of files: tk.ps, and tk.tex. Whilst you are there fetch tclhtml.tar.gz which contains tcl documentation in html format.

There are actually several pages (4 sides, two cards). gs will display one then ask you for return.

16.5

mkdir /tmp/temp

Create a scratch directory. Put tk.ps in there, and any mini-sripts you write. Delete it all afterwards, as some of these .pbm files can be huge.

16.6

gs -dSAFER -sDEVICE=x11 tk.ps

This looks the the printed page but is sideways, and the characters appear blurred.

16.7

gs -dSAFER -sDEVICE=x11 -r200 tk.ps

This is readable (albeit too big and slow), but the quality of the characters are dreadful when compared to native X11 fonts.

The 'blurring' appears to be related to how the high resolution stroked lines have been scaled to the low resolution pixel coordinates. eg bad smudging algorithm.

16.8

gs -dSAFER -sDEVICE=pbm -r180 tk.ps

The idea here is to output the pages to .pbm files (bitmap files), which can then be viewed using xloadimage, or xv, or ...

I wanted to give gs the opportunity to drive a plain pix-map (which is what it does at all resolutions - more or less), and output that pixmap directly.

I put the following commands into a temp scripts. This creates big .pbm files, so delete them soon.

	DEV=pbm
	ext=$DEV
	< /dev/null	gs	\
		-sDEVICE=$DEV	\
		-sOutputFile=tk.%d.$ext	\
		-dNoPause		\
		-r180x180		\
		-dSAFER			\
		tk.ps 

16.9

xloadimage -rotate 270 tk.2.pbm

This turns the image 90' anti-clockwise. This is only needed because it is a ref-card, and prints landscape instead of portrait. It added quite a large CPU and memory overhead, but it got there. I used xmag to view individual characters. I chose -r180, because of epson -r360x180.

If you have a 9 pin (not 24) you might experiment with -sDEVICE=eps9high at 120x216, then other variations.

16.10

gs -dSAFER ...

Always use the -dSAFER option to gs, otherwise if you view a file containing a trojan, it will be run as a program, as your UID (ie as you) and may do something naughty.

gs -dSAFER -sDEVICE=linux -r 640x480x266 tiger.ps
This is because ghostscript is a full blown programming language, and can call other binaries, such as rm -rf $HOME, or something more subtle, which you might not notice, but leaves you with no remaining security.

The -dSAFER option tells the interpreter to attempt to detect and limit such behavior. It allows you to use gs to drive a printer, without really knowing where the original file came from, or having to run in a special environment.

16.11

Output to an epson 24 bin dot-matrix printer

This is easy, but is a combination of tricks. Firstly you know that ghostscript does all the work for you (it says that here!).

Secondly - and most importantly, you know that RES=360x180 (it also says that in the ghostscript docs - but it isn't obvious!) This is special to the epson driver and printer. Without this you get a shoddy 9 pin output!

Thirdly, you know that your epson requires manually fed pages (to print double sided), and that it is a near certainty that either the paper will jam+tear, or the operator will put the paper in upside down, requiring two pages to be reprinted.

Armed with this knowledge, you create the following shell script, read it and run it. "$1" is the name of the file, less the .ps extension. It creates SEVERAL files, each file containing the escape codes required for a page of epson output. Regrettably I don't know any way of previewing these files other than printing them.

	file="$1"
	RES=360x180
	gs				\
	 -dSAFER			\
	 -sDEVICE=epson -r$RES		\
	 -sOutputFile="epson.$1.pg.%d"	\
	 -dA4				\
	  "$file".ps

You then send each file (page of epson codes) to the printer. I used another shell script that asked me did I want to print this page [Y/N. This is to skip 20 pages, and to pause between pages.

	for file in "$@"
	do
		if yes_no print $file
		then
			dd bs=2k of=/dev/lp1 if=$file
		fi
	done
dd is a standard unix utility (duplicate data to/from device or file). The 2k blocksize helps keep the buffer full. You could also use
cp $file /dev/lp1

For those who want it, the yes_no script is:

	#!/bin/sh
	# yes_no

	question="$* [y/n] "
	while :
	do
		echo -n "$question"
		read line
		case $line
		in	
			y|Y|Yes|YES|yes)	
				echo "YES: $*"
				exit 0
		;;	n|N|No|NO|no)
				echo "NO: $*"
				exit 1
		;;	*)
			echo
			echo "Wrong answer ($line) - this is a yes_no question"
			echo
			if tty -s 
			then
				: continue
			else
				echo "stty -s says this is not Interactive"
				echo "resonse is No"
				exit 1
			fi
		esac
	done

In addition, you also want the tunelp program, to select IRQ7 instead of polling. It is MUCH faster (you may also need modprobe lp to get the lp driver statically loaded if it is a module). man tunelp.

If you already have your printer setup for tractor feed and unattended output, your life will be much easier. If you have a laser printer, you want to read the printing HOWTO, and have the magic filter call ghostscript for you.

This took over 4 hours (70 pages is a lot for a dot-matrix, where every page has to be aligned). It also had a nasty problem, where the first graphics line is LOST! This may be a problem with gs, or it may be due to sending the text directly to the printer, ie the first line confused the printer. I'm not certain, but I couldn't get it to work, and it only lost the page numbers. I did try to shift the page down a line, using a gs option, but that didn't help.

16.12

/usr/lib/ghostscript/doc/*

Look in here for further information.