-
Notifications
You must be signed in to change notification settings - Fork 2
/
palabels
executable file
·124 lines (101 loc) · 3.03 KB
/
palabels
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#!/usr/bin/perl
use Getopt::Std;
$img = "$ENV{'HOME'}/graphics/logoaddr.eps";
# Usage/help message.
$usage = <<USAGE;
Usage: palabels [options] [filename]
Print address labels on Avery 5164 sheets
-r m : start at row m (range: 1..3; default: 1)
-c n : start at column n (range 1..2; default: 1)
-h : print this message
If no filename is given, use STDIN. An address entry is a plain text
series of non-blank lines. Blank lines separate entries.
USAGE
# Set up geometry constants for Avery 5164 labels.
$topmargin = 0.75;
$poleft = 0.375;
$poright = 4.55;
$lheight = 3.333;
# Get starting point from command line if present.
getopts('hr:c:', \%opt);
die $usage if ($opt{h});
$row = int($opt{r}) || 1; # chop off any fractional parts and
$col = int($opt{c}) || 1;
# Bail out if position options are out of bounds
die $usage unless (($row >= 1 and $row <= 3) and
($col >= 1 and $col <= 2));
# Set initial horizontal and vertical positions.
if ($col == 1) {
$po = $poleft;
} else {
$po = $poright;
}
$sp = ($topmargin + ($row - 1)*$lheight);
# # Pipe output through groff to printer (manual feed).
# open OUT, "| groff -P-m | lpr";
# # open OUT, "> labels.rf"; # for debugging
# select OUT;
# Pipe output through groff to printer (manual feed).
open OUT, "| groff | lpr -o ManualFeed=True";
# Change to PDF before sending to printer.
# open OUT, "| groff | ps2pdf - - | lpr -o ManualFeed=True";
# Preview output instead of printing directly.
# open OUT, "| groff | ps2pdf - - | open -f -a /Applications/Preview.app";
# Print raw troff code for debugging.
# open OUT, "> labels.rf";
select OUT;
# Set up document.
print <<"SETUP";
.ll 3.5i
.ft H
.ps 14
.vs 16
.nf
SETUP
# The troff code for formatting a single entry, with placeholders for
# positioning on the page. The magic numbers embedded in the formatting
# commands make the layout look nice.
$label = <<ENTRY;
.sp |%fi
.po %fi
.vs 0
.in 0i
.ti 0.1i
.PSPIC -L "%s" 3i 0.5i
.sp .125i
.ps 24
\\l'3.5i'
.ps
.vs
.in .125i
.sp .375i
%s
ENTRY
# Slurp all the input into an array of entries.
$/ = "";
@entries = <>;
$bp = 0; # we don't want to start with a page break
foreach (@entries) {
# Break page if we ran off the end.
if ($bp) {
print "\n.bp\n"; # issue the page break command
$bp = 0; # reset flag
}
# Print the label.
s/\s+$//; # eliminate trailing whitespace
printf $label, $sp, $po, $img, $_;
# Now we set up for the next entry.
if ($col == 1){ # last entry was in the left column
$col = 2; # so the next will be in
$po = $poright; # the right column
} else { # last was in the right column
$col = 1; # so the next will be in
$po = $poleft; # the left column
$row++; # of the next row
if ($row > 3) { # we're at the end of the page
$bp = 1; # page break flag
$row = 1; # new page starts at top row
}
$sp = ($topmargin + ($row - 1)*$lheight);
}
}