# txt2eps.awk 
# 
# dumb script to produce eps text labels
# format of input is
# x y "txt" align=0{-1=left:0=center:1=right} rot=90{0-360} fontsize fontname

BEGIN {

  if(TS == "") TS = "*"

  print "%!PS-Adobe-2.0 EPSF-2.0"
  print "%%Title: plain.eps"
  print "%%Creator: gnuplot inspired text formatter"
  print "%%CreationDate: Thu Oct 31 08:41:01 2002"
  print "%%DocumentFonts: (atend)"
  print "%%BoundingBox: 0 0 200 200"
  print "%%Orientation: Portrait"
  print "%%EndComments"
  print "/gnudict 256 dict def"
  print "gnudict begin"
  print "/gnulinewidth 5.000 def"
  print "/userlinewidth gnulinewidth def"
  print "/vshift -46 def"
  print "/dl {10 mul} def"
  print "/M {moveto} bind def"
  print "/L {lineto} bind def"
  print "/R {rmoveto} bind def"
  print "/V {rlineto} bind def"
  print "/Lshow { currentpoint stroke M"
  print "  0 vshift R show } def"
  print "/Rshow { currentpoint stroke M"
  print "  dup stringwidth pop neg vshift R show } def"
  print "/Cshow { currentpoint stroke M"
  print "  dup stringwidth pop -2 div vshift R show } def"
  print "/DL { false {setrgbcolor false {pop []} if 0 setdash }"
  print " {pop pop pop false {pop []} if 0 setdash} ifelse } def"
  print "/BL { stroke userlinewidth 2 mul setlinewidth } def"
  print "/AL { stroke userlinewidth 2 div setlinewidth } def"
  print "/UL { dup gnulinewidth mul /userlinewidth exch def"
  print "      10 mul /udl exch def } def"
  print "/PL { stroke userlinewidth setlinewidth } def"
  print "/LTb { BL [] 0 0 0 DL } def"
  print "/LTa { AL [1 udl mul 2 udl mul] 0 setdash 0 0 0 setrgbcolor } def"
  print "/LT0 { PL [] 1 0 0 DL } def"
  print "/LT1 { PL [4 dl 2 dl] 0 1 0 DL } def"
  print "/LT2 { PL [2 dl 3 dl] 0 0 1 DL } def"
  print "/LT6 { PL [2 dl 2 dl 2 dl 4 dl] 0 0 0 DL } def"
  print "/S /Helvetica findfont def"
  print "end"
  print "%%EndProlog"

  print "gnudict begin"
  print "gsave"
  print "50 50 translate"
  print ".1 .1 scale"
  print "0 setgray"
  print "newpath"
  print "S 140 scalefont setfont"
  print "1.000 UL"
}

/^[^#]/ && NF > 2 {

  align = $4 + 0
  rot = $5 + 0

  if($6 != "") {
    font = "Helvetica"
    fontsize = $6 + 0
  }
  else
    font = ""

  if($7 != "") font = $7

  gsub(TS, " ", $3)
  gsub(TS, " ", font)

  print $1 " " $2 " M"

  if(font != "")
    print "(" font ") findfont " fontsize " scalefont setfont"

  if(rot != 0) {
    print "currentpoint gsave translate"
    print rot " rotate 0 0 M"
  }

  if(align < 0)
    sali = "Lshow"
  else if(align > 0)
    sali = "Rshow"
  else
    sali = "Cshow"

  print "(" $3 ") " sali

  if(rot != 0) print "grestore"
}

END {
  print "end"
  print "showpage"
  print "%%Trailer"
}
