#!/bin/sh # # pic2png -- compile PIC image descriptions to PNG bitmaps # # by Eric S. Raymond , July 2001 # # In Unixland, the magic is in knowing what to string together... # # Take a pic diagram on standard input, emit cropped PNG on standard output. # The pic markup should *not* be wrapped in .PS/.PE, this script will do that. # Does PNG because it's as good as any other bitmap format and freer than most. # A -U option on the command line enables gpic/groff "unsafe" mode. # All other options are passed to pnmtopng. # # Requires groff, ghostscript, and the pnm tools. All are open source. # Use, modify, and redistribute freely. Send me fixes and enhancements. # # Here are the assumptions behind the option processing: # # 1. Only the -U option of gpic(1) is relevant. -C doesn't matter because # we're generating our own .PS/.PE, -[ntcz] are irrelevant because we're # generating Postscript. # # 2. Ditto for groff(1), though it's a longer and more tedious demonstration. # # 3. No options of pnmcrop are relevant. We can assume the generated image # is going to be black-on-white because that's what pic generates. Even # if pic were somehow coerced into generating a non-white background, # pnmcrop's algorithm (look at the top corners) will find the right # thing because gs(1) is generating a full page. # # 4. Many options of pnmtopng(1) are potentially relevant (especially # -interlace, -transparent, -background, -text, and -compression. # # Thus, we pass -U to gpic and groff, and everything else to pnmtopng. # # $Id: pic2png,v 1.1 2001/07/18 03:07:41 esr Exp $ # groffpic_opts="" pnmtopng_opts="" while [ "$1" ] do case $1 in -unsafe) groffpic_opts="-U"; pngtopnm_opts="$pngtopnm_opts -U";; *) pnmtopmg_opts="$pnmtopmg_opts $1" ;; esac shift; done # Here goes: # 1. Wrap the input in dummy .PS/PE macros # 2. Process through pic to emit troff markup # 3. Process through groff to emit Postscript # 4. Process through ghostscript to emit a ppm bitmap # 5. Crop the ppm bitmap # 6. Turn the ppm into PNG (echo ".PS"; cat; echo ".PE") \ | pic $groffpic_opts \ | groff $groffpic_opts -Tps \ | gs -q -sDEVICE='ppmraw' -sOutputFile='-' -dNOPAUSE -dBATCH - \ | pnmcrop \ | pnmtopng $pnmtopmg_opts # End