#!/bin/sh

# USAGE: dyeps2pbm  < .eps  > .pbm
#
#    OR: fig2dev -L eps -F  < .fig  | dyeps2pbm  > .pbm
#
# Optional: $1 = tape width (dymo pixels), 96 for LMPC or 128 for LMPC2
#
# (fig2dev is from the transfig package)
# Also see notes below.

WID="$1"
if [ -z "$WID" ] ; then
  #WID=96	# LabelManager PC
  WID=128	# LabelManager PC-II
fi

if true ; then
#if false ; then

  # First possibility: gs renders directly to black-and-white

  gs -q -sDEVICE=pbmraw -sOutputFile=- -r180x180 -dDEVICEHEIGHT="$WID" \
     -dEPSCrop -dNOPLATFONTS -dGridFitTT=1 \
     -dNOPAUSE -dSAFER -dBATCH - |
    pnmflip -cw |
    pnmcut 0 0 "$WID" 0

else

  # Second possibility: gs renders to color, we do the rest

  gs -q -sDEVICE=ppmraw -sOutputFile=- -r180x180 -dDEVICEHEIGHT="$WID" \
     -dEPSCrop -dNOPLATFONTS -dGridFitTT=1 -dDOINTERPOLATE \
     -dNOPAUSE -dSAFER -dBATCH - |
    pnmflip -cw |
    pnmcut 0 0 "$WID" 0 |
    ppmtopgm |

    # Then choose one:

    pgmtopbm -floyd -value 0.5
    #pgmtopbm -threshold -value 0.5
    #pgmtopbm -hilbert -clump 2
    #pgmtopbm -dither8
    #pgmtopbm -cluster3
    #pgmtopbm -cluster4
    #pgmtopbm -cluster8
fi

# Note: -dDEVICEHEIGHT="$WID" is only a suggestion, used when the document
# doesn't specify its own size. We can't force with -g since we don't want a
# fixed width; gs doesn't seem to allow forcing only height.
#
# Xfig .eps output always starts at PS-coord (0,0) = bottom left.
# [Not so with .ps output, though!]
# With pnmflip | pnmcut as shown, we keep the Xfig .eps bottom intact and
# remove anything that is too _high_.
# But, since Xfig 18mm height produces 130 output pixels of which the top two
# rows are empty, it turns out to fit exactly.
