Version 0 of Manipulating Postscript

Updated 2002-04-25 12:57:53

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):

 #!/bin/sh
 # 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 should tell it all. Acceptable font names are Courier, Times-Roman, Helvetica plus some that your local documentation may show.


Arts and crafts of Tcl-Tk programming