#!/usr/bin/perl

# Simple label-to-PostScript script

# Original Author: David Harper (www.obliquity.com)
#
# From: David Harper <adh@sanger.ac.uk>
# Newsgroups: comp.lang.postscript
# Subject: Re: Label printing
# Date: Mon, 19 Feb 2001 14:19:53 +0000
# Message-ID: <3A912B89.EE2E4372@sanger.ac.uk>
#
# Feel free to alter this for your own use. It doesn't come with any
# guarantees -- you get what you pay for :-)

# 14/Jun/2002 reed -- modified the code!

# Input file format:
# one line with lines separated by tabs

$debugging = 0; # 1 to print debugging info on label

$labeloutline = 0; # 1 to print a outline around each label

$bold_first_line = 0; # 1 to bold first line

if ($ENV{'LABELOUTLINE'}) {
  $labeloutline = 1;
}
if ($ENV{'SELFLABEL'}) {
  $selflabel = 1;
}

#
# You can alter these values if you wish. The names are pretty
# much self-explanatory. Leading is the vertical spacing between
# successive lines, so it needs to be larger than the fontsize.
#
$fontsize = 10;
$leading = 13;
#
# The first line (the name of the addressee) is set in $boldfont
# and the rest (the actual address) in $plainfont.
#
$plainfont = "/Helvetica";
$boldfont = "/Helvetica-Bold";
#
# There's a large part of the world which doesn't use 8.5-by-11 inch
# paper, you know :-)
#
$A4_Height = 297;
$A4_Width = 210;
#%%BoundingBox: 54 72 558 720
#
# You can opt to have your own address printed at a small pointsize
# vertically downward at the right hand edge of each label. Leave
# @selfaddr undefined to switch this feature off.
#
$selffont = $plainfont;
$selfsize = 5;
$selflead = 5.5;
@selfaddr = ('My Name','My Street','My City','My Zipcode');
#
# Dimensions for Avery L7163 labels, in millimetres.
#
#$LabelHeight = 38.14;
#$LabelWidth = 99.1;
#$XOrigin = 5;
#$XGutter = 2.5;
#$YOrigin = 15;
#
$LabelHeight = 25.4;
$LabelWidth = 66.675;
$XOrigin = 4;
$XGutter = 4;
$YOrigin = 13;
# How many rows per column? How many columns per page?
#
$RowsPerColumn = 10;
$ColumnsPerPage = 3;

$XMargin = 4;
$YMargin = 3;

print <<"PROLOGUE";
%!PS-Adobe-2.0
%%Orientation: Portrait
%%DocumentPaperSizes: Letter
%%Creator: David Harper at Obliquity Consulting (www.obliquity.com)
%%Title: (Address labels)
%%Pages: (atend)
%%EndComments

%%BeginProlog
%
/inch {72 mul} def
/mm {72 mul 25.4 div} def
%
/rm $plainfont findfont $fontsize scalefont def
/bf $boldfont findfont $fontsize scalefont def
/sf $selffont findfont $selfsize scalefont def
%
%%EndProlog

%%BeginSetup
%%EndSetup

PROLOGUE

$page = 0;
$row = 0;
$column = 3; # was 2 then 0

while ($line = <STDIN>) {
    if ($column >= $ColumnsPerPage) {
	$column = 0;
	$row = 0;
	$onpage = 0;
	print "\nshowpage\n" if ($page > 0);
	$page += 1;
	print "\n\n%%Page: $page $page\n\n";
    }

    $x0 = $XOrigin + $column * ($LabelWidth + $XGutter);
    $y0 = $YOrigin + $row * $LabelHeight;

    printf "gsave\n%6.2f mm %6.2f mm translate\n", $x0, $y0;

    printf STDERR
      "Label %d on page %d is at row %d, column %d (x %6.2f, y %6.2f)\n",
      $onpage, $page, $row, $column, $x0, $y0;

    if ($labeloutline) {
	print "0.5 setlinewidth\n";
	printf "newpath 0 0 moveto 0 %6.2f mm rlineto %6.2f mm 0 rlineto 0
%6.2f mm neg rlineto closepath stroke\n",
	$LabelHeight, $LabelWidth, $LabelHeight;
    }

    chop($line);
    @fields = split(/\t/, $line);

    print "gsave\n";

    printf "%6.2f mm %6.2f mm %6.2f mm sub %d sub translate\n",
      $XMargin, $LabelHeight, $YMargin, $leading;

    if ($bold_first_line) {
      print "bf setfont\n";
    }
    else {
      print "rm setfont\n";
    }

    print "0 0 newpath moveto (", $fields[0], ") show\n";
    shift @fields;

    if ($bold_first_line) {
      print "rm setfont\n";
    }

    if ($debugging) {
      print "0 $leading neg translate 0 0 newpath moveto (";
      printf "Label %d on page %d) show\n", $onpage, $page;
      print "0 $leading neg translate 0 0 newpath moveto (";
      printf "row %d, column %d) show\n", $row,$column;
      print "0 $leading neg translate 0 0 newpath moveto (";
      printf "x %6.2f, y %6.2f) show\n", $x0, $y0;
    }
    else {
      while ($addr = shift @fields) {
	print "0 $leading neg translate 0 0 newpath moveto ($addr) show\n";
      }
    }

    print "grestore\n";

    if ($selflabel) {
	printf "%6.2f mm %6.2f mm translate\n", $LabelWidth, $LabelHeight;
	print "-90 rotate\n";
	print "2 mm 2 mm neg translate\nsf setfont\n";
	foreach $addr (@selfaddr) {
          print "0 $selflead neg translate 0 0 newpath moveto ($addr) show\n";
	}
    }

    print "grestore\n";

    $onpage += 1;

    $row += 1;
    if ($row >= $RowsPerColumn) {
	$row = 0;
	$column += 1;
    }
}

print "\nshowpage\n" if ($onpage > 0);

print "\n%%Pages: $page\n\n%%EOF\n";

exit(0);
### End of script


