Updated 2014-01-20 13:12:55 by RLE

Richard Suchenwirth - I received a set of GIF images which I wanted to print out. Having the netpbm tools installed, it was easy to command
 $ giftopnm x.gif | pnmtops | lpr

But what was printed were the bare images; I wanted to see the filename on the page too. Time for a little Tcl tool that can be plugged into this process chain:
 $ giftopnm x.gif | pnmtops | psannotate.tcl x.gif | lpr

Here it is (runs on Unix if chmod +x-ed):
 # psannotate.tcl -- SD PA RC D2, R. Suchenwirth 2002 -*-tcl-*-\
 exec tclsh "$0" ${1+"$@"}

 # insert a string into a Postscript stream that is assumed to have a single image
 # as generated from giftopnm|pnmtops, for instance

 set usage {usage: psannotate -font Courier -size 10 -pos 40,780 words to display
     Input: Postscript stream from stdin; output: Postscript stream to stdout
     Switches are optional, defaults are shown above
     Position is x,y in 1/72 inch in from bottom left corner
     "words to display" should not contain unbalanced parentheses
 array set opt {-font Courier -size 10 -pos "40 780"}
 set text {}
 for {set i 0} {$i<$argc} {incr i} {
    set arg [lindex $argv $i]
    switch -- $arg {
        -f - -font   {incr i; set opt(-font) [lindex $argv $i]}
        -s - -size   {incr i; set opt(-size) [expr [lindex $argv $i]*1]}
        -p - -pos    {incr i; set opt(-pos)  [split [lindex $argv $i] ,]}
        -h - --help - -help {puts stderr $usage; exit}
        default {lappend text $arg}
 while {[gets stdin line]>=0} {
    if {[regexp showpage $line]} {
        puts "
 /$opt(-font) findfont dup length dict /D exch def
 {1 index /FID ne {D 3 1 roll put} {pop pop}ifelse}forall
 D /Encoding ISOLatin1Encoding put
 /I D definefont pop
 /I findfont $opt(-size) scalefont setfont
 $opt(-pos) moveto ([join $text]) show
    puts stdout $line

You may have to remove the leading space in the first line (added for Wiki markup). The magic Postscript code that is inserted directly before a showpage command enables ISO Latin1 (iso8859-1) character set - what else it does I forgot, but it works ;-) The usage message should tell it all. Acceptable font names are Courier, Times-Roman, Helvetica plus some that your local documentation may show.

The defaults may need tweaking if you use other tools like gif2ps that does the conversion directly - here the following worked well (units are inches there):
 gif2ps x.gif | psannotate.tcl -pos 0.9,1.03 -s 0.016 x.gif | lpr

What "the magic Postscript" code does is that it makes a copy of the font specified in -font option and replaces the encoding vector in the copy with the ISOLatin1Encoding ditto; this is what is known as reencoding the font. The new temporary font is named I. An awkward point is that the code above also defines D to be the dictionary of the font; this can be avoided by replacing the long puts with:
        puts "
 /$opt(-font) findfont dup length dict begin
 {1 index /FID ne {def} {pop pop} ifelse} forall
 /Encoding ISOLatin1Encoding def
 currentdict end /I exch definefont pop
 /I findfont $opt(-size) scalefont setfont
 $opt(-pos) moveto ([join $text]) show

Unbalanced parentheses can be included in the "words to display" if they are escped with backslashes. Likewise, an explicit backslash should be escaped by another backslash. /Lars Hellström

On comp.lang.tcl, the message titled Canvas Postscript Output - Multiple canvases per page? [1] discusses some postscript to allow including multiple Encapsulated Postscript (EPS) files in one output file.

Another article, multi-page canvas postscript [2] discusses generating postscript so that canvas output is paged if it does not fit an A4 size piece of paper.

Yet another article, Getting postscript output separated into pages., [3] is a pointer to an example of code that manipulates the canvas postscript into multiple pages.