#!/bin/sh
# code to display a font

# add a -- at the end
if [ "$#" -eq 0 ]; then set -- Times-Roman --; else set -- "$@" --; fi

DATE=`date`

###############################################################################
cat <<EndOfPart1
%!PS-Adobe-1.0
%%Title: font map for $*
%%Creator: showfont.sh
%%Copyright: script by Albert Dvornik <bert@mit.edu>
%%+       PostScript code by <eichin@athena.mit.edu>, <amgreene@athena.mit.edu>
%%CreationDate: $DATE
%%Pages: (atend)
%
%%%%----%%%%----%%%%----%%%%----%%%%----%%%%----%%%%----%%%%----%%%%----%%%%
%%BeginProlog
EndOfPart1
###############################################################################

while [ "$#" -gt 0 ]; do
  case "$1" in --) shift; break ;; esac
  if [ -r "$1" ]; then
    echo "%%BeginDocument: $1"
    cat "$1"
    echo "%%EndDocument"
    echo "% (done with $1)"
    echo "%%%%----%%%%----%%%%----%%%%----%%%%----%%%%----%%%%----%%%%----%%%%"
    shift
  else
    font="$1"
    shift
    set -- "$@" "$font"
  fi
done

###############################################################################
cat <<'EndOfPart2'

%%% Standard definitions 
/inch { 72 mul } def
/pageside 11 inch def
/pagetop 8.5 inch def

%%% Specific local definitions 
/sidelen 9 inch def                  % the height of the box of chars
/toplen  6 inch def                  % the width of the box of chars 

/nrowsofboxes 32 1 add def           % Number of rows (chars + labels) 
/ncolsofboxes  8 1 add def           % Number of columns (chars + labels) 

/xbox  toplen ncolsofboxes div def   % X dimension of the box 
/ybox sidelen nrowsofboxes div def   % Y dimension of the box 
/underpad 2 def                      % padding under the characters
 
% the font size is a bit smaller than the box 
/boxfontsize ybox underpad 2 mul sub def

/labtmp (\\00x) def
/labelfont /Times-Roman findfont boxfontsize scalefont def

%%% Font map code

/mcenter      % width-of-box width-of-thing ==> offset-from-left
{ sub 2 div } def

/center       % (string) width-of-box ==> (string) offset-from-left
{ 1 index stringwidth pop mcenter } def

/onechar ( ) def

/octallabel   % row-number ==> (string)
{ dup 8 mod cvi onechar cvs labtmp exch 2 exch putinterval
      8 div cvi onechar cvs labtmp exch 1 exch putinterval
  labtmp } def

/doline       % line-number ==> -
{
 /linnum exch def
 currentpoint   % mark where we are
 toplen 0 rlineto stroke   % draw the line 
 linnum 0 ge {
 2 copy moveto   % go back to the side
 labelfont setfont
 linnum octallabel xbox center
   underpad rmoveto 
   show   % draw the side label

 dispfont setfont
 moveto
 xbox 0 rmoveto 
 currentpoint
 0 1 7
   {
     linnum 8 mul add cvi onechar 0 3 -1 roll put   % make a 1-char string 
     onechar xbox center underpad rmoveto show 
     moveto
     xbox 0 rmoveto 
     currentpoint
   }
 for
 } if
 pop pop   % clear currentpoint 
} def

/docolumn {   % column-number ==> -
 /colnum exch def   % save column number 
 0 sidelen rlineto   % mark vertical line 
 currentpoint stroke   % draw it and reference top
 ybox sub moveto   % jump back down
 colnum 0 eq { (octal) } 
 { colnum 9 eq { ( ) } { colnum 1 sub cvi ( ) cvs } ifelse }
 ifelse
 xbox center underpad rmoveto show
} def

/fnstring 20 string def
/font2name {
  dup /FontInfo known
  { dup /FontInfo get dup /FullName known
    { /FullName get false } { pop true } ifelse }
  { true } ifelse
  { dup /FontName known
    { dup /FontName get fnstring cvs fnstring } { (--unknown--) } ifelse } if
  exch pop
} def

/ShowFont {       % fontname ==> -
 /fontname exch def
 % open the font and scale it, so we have it around
 /dispfont fontname cvn findfont boxfontsize scalefont def 
 /fontname dispfont font2name def
 % move to the upper corner 
 pagetop toplen mcenter 
 pageside sidelen mcenter
 translate   % moves to lower left corner of boxes

 0 0 moveto
 currentpoint
 31 -1 -2
  {
    doline
    moveto 0 ybox rmoveto currentpoint
  } 
 for

 pop pop
 0 0 0 0 moveto

 labelfont setfont
 0 1 9
  {
    docolumn
    moveto xbox 0 rmoveto currentpoint
  }
 for

 pop pop          % clear the stack 
 fontname toplen center   % center the name across the top (name) x-off 
 sidelen underpad add   % find y 
 moveto show
 showpage
} def

EndOfPart2
###############################################################################

# now display the fonts

echo "%%EndProlog"
echo

i=0
for f in "$@"; do
    i=`expr $i + 1`
    echo "%%Page: ($f) $i"
    echo "($f) ShowFont"
    echo
done

echo "%%Trailer"
echo "%%Pages: $i"
echo "%%EOF"
