blib/lib/PostScript/Columns.pm

filter: Statementstmt 100% 118 / 118 Branchbran 60.0% 18 / 30 Conditioncond 50.0% 9 / 18 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 100% 4 / 4 Podpod 0.0% 0 / 1 totaltotal 82.7% 149 / 180 CC 29 SCAR 33.5
CC
29
Cov
87%
pscolumns
31.3
BEGIN
1.0
BEGIN
1.0
CRAP 28.4
↑ Summary
1
2 =head1 NAME
3
4 PostScript::Columns - Squeeze a text file into multiple columns.
5
6 =head1 SYNOPSIS
7
8 use PostScript::ColDoc;
9
10 $psdoc= pscolumns(
11 -margins => [30,20], # NSEW or NS,EW or N,EW,S or N,E,W,S (like CSS)
12 -headfont => 'NimbusMonL-Bold',
13 -headsize => 12,
14 -head => $head,
15 -font => 'NimbusMonL-Regu',
16 -size => 10,
17 -text => $text,
18 # default font/size for foot:
19 -foot => "Page \$p of \$pp", # will interpolate later :)
20 );
21
22 # use all defaults, no footer
23 $doc= pscolumns(
24 -size => 5,
25 -head => "Left\nLeft Also\tTest Document\tRight",
26 -text => $text,
27 -foot => scalar(localtime)."\tFoot\tPage \$p of \$pp",
28 );
29
30
31 =head1 DESCRIPTION
32
33 Creates a PostScript document with a user-defined header and footer,
34 then attempts to squeeze the data into as many columns as possible.
35
36 =head1 AVAILABLE FONTS
37
38 Only the monospace PostScript fonts are available:
39
40 =over 4
41
42 =item C<NimbusMonL-Regu>
43
44 =item C<NimbusMonL-Bold>
45
46 =item C<NimbusMonL-ReguObli>
47
48 =item C<NimbusMonL-BoldObli>
49
50 =back
51
52 =head1 OPTIONS
53
54 =over 4
55
56 =item -margins
57
58 Array ref that specifies page margins, in I<points> (1/72 of an inch).
59 North, East, West South are expressed as
60 four elements: [ N, E, S, W ], three elements [ N, E_W, S ], two elements [ N_S, E_W ],
61 or one element [ N_S_E_W ].
62 (This is the same order that CSS uses.)
63
64 B<Note:> Different printers may require drastically different margins.
65 You'll have to experiment each time you use this module with a new printer.
66
67 =item -headfont
68
69 Name of the font to use for the header (see L<"AVAILABLE FONTS">).
70
71 =item -headsize
72
73 Size of the font to use for the header (in points).
74
75 =item -head
76
77 String to use as header.
78 Upper-right, centered, and upper-left fields are tab-separated.
79 In the string, C<$p> will be replaced by the current page number,
80 and C<$pp> with the total number of pages.
81
82 =item -font
83
84 Name of the font to use for the text (see L<"AVAILABLE FONTS">).
85
86 =item -size
87
88 Size of the font to use for the text (in points).
89
90 =item -text
91
92 Columnar text.
93
94 =item -footfont
95
96 Name of the font to use for the footer (see L<"AVAILABLE FONTS">).
97
98 =item -footsize
99
100 Size of the font to use for the footer (in points).
101
102 =item -foot
103
104 String to use as footer.
105 Lower-right, centered, and lower-left fields are tab-separated.
106 In the string, C<$p> will be replaced by the current page number,
107 and C<$pp> with the total number of pages.
108
109 =back
110
111 =head1 AUTHOR
112
113 v, E<lt>five@rant.scriptmania.comE<gt>
114
115 =head1 SEE ALSO
116
117 perl(1).
118
119 =cut
120
121 package PostScript::Columns;
122
123 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
124 1 ▶use vars qw($VERSION @ISA @EXPORT @EXPORT_OK);
Subroutine BEGIN: called CC=1 SCAR=0.0
125 1 ▶use vars qw(%wratio);
Subroutine BEGIN: called CC=1 SCAR=0.0
126
127 1 $VERSION = '1.23';
128 1 require Exporter;
129 1 @ISA = qw(Exporter);
130 1 @EXPORT = qw(pscolumns);
131
132 sub pscolumns
133 1 ▶{ # Hey! I know what's mine! ;)
Subroutine pscolumns: called CC=29 SCAR=34.4
Pod undocumented
134 1 my %arg= @_;
135 1 my($now,$who)= (scalar localtime, getlogin);
136 ## initial metrics
137 1 ▶ my($margin_N,$margin_E,$margin_S,$margin_W)= @{$arg{-margins}||[]};
Condition50%1/2
$arg{'-margins'} || []
l!l
01
MC/DC0%0/1
$arg{'-margins'} || []
$arg{'-margins'}
Truth table50%1/2
A
A= $arg{'-margins'}
Aresult
11
00
138 1 ▶ $margin_N= 30 unless defined $margin_N;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $margin_N 1 0 1
139 1 ▶ $margin_E= 20 unless defined $margin_E;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $margin_E 1 0 1
140 1 ▶ $margin_S= $margin_N unless defined $margin_S;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $margin_S 1 0 1
141 1 ▶ $margin_W= $margin_E unless defined $margin_W;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $margin_W 1 0 1
142 1 ▶ my $font= ( $wratio{$arg{-font}} ? $arg{-font} : 'NimbusMonL-Regu' );
Branch: 1 branch
BranchTrueFalseTotal
$PostScript::Columns::wratio{$arg{'-font'}} ? : 0 1 1
143 1 ▶ my $font_Y= $arg{-size} || 7;
Condition50%1/2
$arg{'-size'} || 7
l!l
10
MC/DC0%0/1
$arg{'-size'} || 7
$arg{'-size'}
Truth table50%1/2
A
A= $arg{'-size'}
Aresult
11
00
144 1 my $font_X= $font_Y * $wratio{$font};
145 1 ▶ my $line_Y= $arg{-linewidth} || 0.2;
Condition50%1/2
$arg{'-linewidth'} || 0.2
l!l
01
MC/DC0%0/1
$arg{'-linewidth'} || 0.2
$arg{'-linewidth'}
Truth table50%1/2
A
A= $arg{'-linewidth'}
Aresult
11
00
146 ## head metrics
147 1 my @head_right= split /\t/, $arg{-head};
148 1 my @head_left= split /\n/, $head_right[0];
149 1 my @head_center= split /\n/, $head_right[1];
150 1 @head_right= split /\n/, $head_right[2];
151 1 my($head_lines)= sort {$b<=>$a}
152 (scalar @head_left, scalar @head_center, scalar @head_right);
153 1 ▶ my $head_font= $arg{-headfont} || 'NimbusMonL-Bold';
Condition50%1/2
$arg{'-headfont'} || 'NimbusMonL-Bold'
l!l
01
MC/DC0%0/1
$arg{'-headfont'} || 'NimbusMonL-Bold'
$arg{'-headfont'}
Truth table50%1/2
A
A= $arg{'-headfont'}
Aresult
11
00
154 1 ▶ my $head_font_Y= $arg{-headsize} || 10;
Condition50%1/2
$arg{'-headsize'} || 10
l!l
01
MC/DC0%0/1
$arg{'-headsize'} || 10
$arg{'-headsize'}
Truth table50%1/2
A
A= $arg{'-headsize'}
Aresult
11
00
155 1 my $head_font_X= $head_font_Y * $wratio{$head_font};
156 1 my $head_Y= $head_font_Y * $head_lines;
157 ## foot metrics
158 1 my @foot_right= split /\t/, $arg{-foot};
159 1 my @foot_left= split /\n/, $foot_right[0];
160 1 my @foot_center= split /\n/, $foot_right[1];
161 1 @foot_right= split /\n/, $foot_right[2];
162 1 my($foot_lines)= sort {$b<=>$a}
163 (scalar @foot_left, scalar @foot_center, scalar @foot_right);
164 1 ▶ my $foot_font= $arg{-footfont} || 'NimbusMonL-Regu';
Condition50%1/2
$arg{'-footfont'} || 'NimbusMonL-Regu'
l!l
01
MC/DC0%0/1
$arg{'-footfont'} || 'NimbusMonL-Regu'
$arg{'-footfont'}
Truth table50%1/2
A
A= $arg{'-footfont'}
Aresult
11
00
165 1 ▶ my $foot_font_Y= $arg{-footsize} || 8;
Condition50%1/2
$arg{'-footsize'} || 8
l!l
01
MC/DC0%0/1
$arg{'-footsize'} || 8
$arg{'-footsize'}
Truth table50%1/2
A
A= $arg{'-footsize'}
Aresult
11
00
166 1 my $foot_font_X= $foot_font_Y * $wratio{$foot_font};
167 1 my $foot_Y= $foot_font_Y * $foot_lines;
168 ## text metrics
169 1 ▶ local $_= $arg{-text} || ' '; s/\t/ /g;
Condition50%1/2
$arg{'-text'} || ' '
l!l
10
MC/DC0%0/1
$arg{'-text'} || ' '
$arg{'-text'}
Truth table50%1/2
A
A= $arg{'-text'}
Aresult
11
00
170 1 my @text= split /\n/;
171 1 my($maxlen)= sort {$b<=>$a} map {length} @text;
172 1 my $col_X= $maxlen * $font_X;
173 1 my $paper_Y= 792;
174 1 my $paper_X= 612;
175 1 my $right= $paper_X-$margin_E;
176 1 my $head_top= $paper_Y-$margin_N;
177 1 my $head_line_top= $head_top-$head_Y+($head_font_Y/2);
178 1 my $text_top= $head_top-$head_Y-($font_Y/2);
179 1 my $foot_top= $foot_Y+$margin_S;
180 1 my $foot_line_top= $foot_top+($foot_font_Y);
181 1 my $text_Y= $text_top-$foot_line_top-4;
182 1 my $text_X= $paper_X-$margin_E-$margin_W;
183 1 my $rows= int( $text_Y / $font_Y );
184 1 ▶ my $cols= int( $text_X / $col_X ) || 1;
Condition50%1/2
int $text_X / $col_X || 1
l!l
10
MC/DC0%0/1
int $text_X / $col_X || 1
int $text_X / $col_X
Truth table50%1/2
A
A= int $text_X / $col_X
Aresult
11
00
185 1 $col_X= $text_X / $cols;
186 1 my $pagelines= $rows * $cols;
187 1 my $pp= int( ( @text / $pagelines ) +0.999 );
188 1 my $ps= <<".";
189 %!PS-Adobe-3.0
190 %%Title: Columnar Document
191 %%Creator: PostScript::Columns v$VERSION
192 %%CreationDate: $now
193 %%For: $who
194 %%BoundingBox: 0 0 $paper_X $paper_Y
195 %%Pages: $pp
196 %Columns: $cols @ $col_X pt
197 %%EndComments
198 .
199 1 my $ps_head= "/$head_font findfont $head_font_Y scalefont setfont\n";
200 1 for my $y (map {$head_top-$head_font_Y*$_} reverse (0..$head_lines-1))
201 {
202 2 local $_= pop @head_left;
203 2 s/(\(|\)|\\)/\\$1/g;
204 2 ▶ $ps_head.= "$margin_W $y moveto ($_) show\n" if $_;
Branch: 1 branch
BranchTrueFalseTotal
if $_ 2 0 2
205 2 $_= pop @head_center;
206 2 my $x= ( $paper_X - ($head_font_X * length) )/2;
207 2 s/(\(|\)|\\)/\\$1/g;
208 2 ▶ $ps_head.= "$x $y moveto ($_) show\n" if $_;
Branch: 1 branch
BranchTrueFalseTotal
if $_ 1 1 2
209 2 $_= pop @head_right;
210 2 $x= $paper_X - $margin_E - ($head_font_X * length);
211 2 s/(\(|\)|\\)/\\$1/g;
212 2 ▶ $ps_head.= "$x $y moveto ($_) show\n" if $_;
Branch: 1 branch
BranchTrueFalseTotal
if $_ 1 1 2
213 }
214 1 $ps_head.= "$margin_W $head_line_top moveto $right $head_line_top ".
215 "lineto $line_Y setlinewidth stroke\n".
216 "/$foot_font findfont $foot_font_Y scalefont setfont\n";
217 1 for my $y (map {$foot_top-$foot_font_Y*$_} (0..$foot_lines-1))
218 {
219 1 local $_= shift @foot_left;
220 1 s/(\(|\)|\\)/\\$1/g;
221 1 ▶ $ps_head.= "$margin_W $y moveto ($_) show\n" if $_;
Branch: 1 branch
BranchTrueFalseTotal
if $_ 1 0 1
222 1 $_= shift @foot_center;
223 1 my $x= ( $paper_X - ($foot_font_X * length) )/2;
224 1 s/(\(|\)|\\)/\\$1/g;
225 1 ▶ $ps_head.= "$x $y moveto ($_) show\n" if $_;
Branch: 1 branch
BranchTrueFalseTotal
if $_ 1 0 1
226 1 $_= shift @foot_right;
227 1 $x= $paper_X - $margin_E - ($foot_font_X * length);
228 1 s/(\(|\)|\\)/\\$1/g;
229 1 ▶ $ps_head.= "$x $y moveto ($_) show\n" if $_;
Branch: 1 branch
BranchTrueFalseTotal
if $_ 1 0 1
230 }
231 1 $ps_head.= "$margin_W $foot_line_top moveto $right $foot_line_top ".
232 "lineto $line_Y setlinewidth stroke\n";
233 1 $ps_head.= "/$font findfont $font_Y scalefont setfont\n";
234 1 $ps_head=~ s/\$pp\b/$pp/g;
235 1 my $p;
236 1 ▶ PAGE: while(@text)
Branch: 1 branch
BranchTrueFalseTotal
if (@text) 4 0 4
237 {
238 4 $p++;
239 4 (my $thishead= $ps_head)=~ s/\$p\b/$p/g;
240 4 $ps.= "\n%%Page: (Page $p) $p\n".$thishead;
241 4 for my $x (map {$margin_W+$col_X*$_} (0..$cols-1))
242 {
243 8 for my $y (map {$text_top-$font_Y*$_} (0..$rows-1))
244 {
245 968 ▶ next unless local $_= shift @text;
Branch: 1 branch
BranchTrueFalseTotal
unless local $_ = shift @text 0 968 968
246 968 ▶ last if /\x0c/; # end col at formfeed
Branch: 1 branch
BranchTrueFalseTotal
if /\x0c/ 0 968 968
247 968 s/(\(|\)|\\)/\\$1/g;
248 968 $ps.= "$x $y moveto ($_) show\n";
249 968 ▶ last PAGE unless @text;
Branch: 1 branch
BranchTrueFalseTotal
unless @text 1 967 968
250 }
251 }
252 3 $ps.= "showpage\n";
253 }
254 1 $ps.= "showpage\n";
255 1 return $ps;
256 }
257
258 1 %wratio=
259 ( # Hmmm.... not a lot of variance. Keep?
260 'NimbusMonL-Regu' => 0.6,
261 'NimbusMonL-Bold' => 0.6,
262 'NimbusMonL-ReguObli' => 0.6,
263 'NimbusMonL-BoldObli' => 0.6,
264 );
265
266 1 1;
↑ Summary