#!/usr/local/bin/perl

require "newgetopt.pl";

$a = ""; $c = "";
$cca = ""; $ccb = "";
$opt_pagescale = 1;
$opt_dotscale = .50;
$opt_numspace = 8;
$opt_linespace = 7;
$opt_lw = .2;
$opt_scale = 1;
$shape = "D";
$fill = "";
$opt_sx = 15;
$opt_oy = 421;

$zzz = &NGetOpt("d", "cc", "f:f", "lw=f", "n", "z", "s", "t", "dotscale=f", "pagescale=f", "scale=f", "sx=f","oy=f", "landscape", "portrait", "numspace=f","linespace=f");

if ($opt_landscape) { $landscape_rotate = "90 rotate"; }

if (defined $opt_f && $opt_f < 2) {
    $fill = <<"FILL";
	gsave
	$opt_f setgray fill
	grestore
FILL
}

if ($opt_cc) {
    $cca = "    -1 0 {";
    $ccb = "    } for";
}

$shape_code =<<"DOT";
/D {
$cca
	dotscale mul
	.001 add
	0 0 3 -1 roll  % flip current point.
	dup 0 moveto           % silly hack for current point
	0 360 arc
	closepath
$fill
	stroke
$ccb

    numspace 0 translate
} bind def
DOT

if ($opt_d) {
    $shape = "D";
} elsif ($opt_n) {
    $shape = "N"; $a = "("; $c = ")";
    $shape_code = <<"NUM";
/N {
    0 0 moveto dup fixfontsize centershow

    numspace 0 translate
} bind def

/fixfontsize {
    fontUsed findfont fsz scalefont setfont
    /curwid exch stringwidth pop def
    curwid maxnumwidth gt {
	fontUsed findfont maxnumwidth curwid div fsz mul
	    scalefont setfont
    } if
} bind def
NUM

} elsif ($opt_s) {
    $shape = "S";
    $shape_code = <<"SQUARE";
/S {
$cca
	dotscale mul dup 2 copy
	2 div dup neg moveto
	0 exch rlineto
	neg 0 rlineto
	neg 0 exch rlineto
	closepath
$fill
	stroke
$ccb

    numspace 0 translate
} bind def
SQUARE

} elsif ($opt_t) {
    $shape = "T";
    $shape_code =<<"TRI";
/T {
$cca
	dotscale mul dup 2 copy
	neg tw2 mul exch neg moveto
	tw mul 0 rlineto
	0 exch lineto
	closepath
$fill
	stroke
$ccb

    numspace 0 translate
} bind def
TRI

}

# print STDERR "$opt_c $opt_d $opt_cc $opt_s $opt_dscale $opt_pscale\n";

print STDOUT<<"FOO";
%!
%
% Triangle thingy
% 23 Aug 1995, Brian Ward, ward\@blah.math.tu-graz.ac.at
%
% This, I think, is really the best way to do the printed output. With
% PostScript, I can scale the font accordingly if the number is too big
% and force it to fit in a spot on the paper.
%

% /trisize 40 def
/sclused $opt_scale  def
/dotscale $opt_dotscale def
/pagescale $opt_pagescale def
/nstr 30 string def
/numspace $opt_numspace def
/maxnumwidth 6 def
/linespacing $opt_linespace neg def
/fsz 5 def

/tw2 linespacing neg numspace div def
/tw tw2 2 mul def

/fontUsed /Times-Roman def

% The following line really isn't my code, but I remember it from somewhere...
/centershow { dup stringwidth pop 2 div neg 0 rmoveto show } def

/nline {
   /blen exch 1 sub 2 div numspace mul neg def
   0 linespacing translate
   gsave
   blen 0 translate
}  bind def

/eline {
   grestore 
} bind def

$shape_code

gsave
pagescale pagescale scale
$opt_sx $opt_oy translate
$landscape_rotate
$opt_lw setlinewidth
sclused sclused scale

FOO

while(<>) {
    chop; s/[\(\)]//g;
    @foo = split (/\s+/,$_);
    $bar = $#foo + 1;
    print STDOUT "$bar nline ";
    if ($opt_z) {
	foreach $b (@foo) {
	    if ("$b" eq "0") { $b = "."; }
	    print STDOUT "$a$b$c $shape ";
	}
    } else {
	foreach $b (@foo) {
	    print STDOUT "$a$b$c $shape ";
	}
    }
    print STDOUT "eline\n";
}

print STDOUT<<"BAR";
grestore
showpage
BAR

