Document.pm untested

filter: Statementstmt 0.0% 0 / 73 Branchbran 0.0% 0 / 8 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 0.0% 0 / 10 Podpod 0.0% 0 / 7 totaltotal 0.0% 0 / 103 CC 5 SCAR 24.8
CC
5
Cov
0%
new
30.0
addText
2.0
addHeader
2.0
CRAP 12.0
1 # -*- Perl -*-
2 # Document.pm
3 #
4 # This module allows for the easy construction of multi-page textual
5 # reports with the PostScript::TextBlock module.
6 #
7 package PostScript::Document;
8 use strict;
9 use PostScript::TextBlock;
10  
11 use vars qw($VERSION);
12 $VERSION = '0.06';
13  
14 # Some standard paper sizes
15 #
16 my @papers = qw( Letter Legal Ledger Tabloid A0 A1 A2 A3 A4 A5 A6 A7 A8
17 A9 B0 B1 B2 B3 B4 B5 B6 B7 B8 B9 Envelope10 EnvelopeC5
18 EnvelopeDL Folio Executive );
19  
20 # Dimensions of standard papers
21 #
22 my %width = ( Letter => 612, Legal => 612,
23 Ledger => 1224, Tabloid => 792,
24 A0 => 2384, A1 => 1684,
25 A2 => 1191, A3 => 842,
26 A4 => 595, A5 => 420,
27 A6 => 297, A7 => 210,
28 A8 => 148, A9 => 105,
29 B0 => 2920, B1 => 2064,
30 B2 => 1460, B3 => 1032,
31 B4 => 729, B5 => 516,
32 B6 => 363, B7 => 258,
33 B8 => 181, B9 => 127,
34 B10 => 91, Envelope10 => 297,
35 EnvelopeC5 => 461, EnvelopeDL => 312,
36 Folio => 595, Executive => 522
37 );
38 my %height = ( Letter => 792, Legal => 1008,
39 Ledger => 792, Tabloid => 1224,
40 A0 => 3370, A1 => 2384,
41 A2 => 1684, A3 => 1191,
42 A4 => 842, A5 => 595,
43 A6 => 420, A7 => 297,
44 A8 => 210, A9 => 148,
45 B0 => 4127, B1 => 2920,
46 B2 => 2064, B3 => 1460,
47 B4 => 1032, B5 => 729,
48 B6 => 516, B7 => 363,
49 B8 => 258, B9 => 181,
50 B10 => 127, Envelope10 => 684,
51 EnvelopeC5 => 648, EnvelopeDL => 624,
52 Folio => 935, Executive => 756
53 );
54  
55 # Valid attribute names
56 #
57 my @paramnames = qw( paper width height
58 rmargin lmargin tmargin bmargin );
59  
60 # Default values of document attributes
61 #
62 my %defaults = (
63 paper => 'Letter',
64 width => $width{'Letter'},
65 height => $height{'Letter'},
66 rmargin => 36, # .5 inches
67 lmargin => 36,
68 tmargin => 36,
69 bmargin => 36,
70 );
71  
72 sub new {
73 # The constructor method
74 #
75 my $proto = shift; # allow use as a class or object method
76 my $class = ref($proto) || $proto; # see perltoot man page
77 my %params = ();
78  
79 # Allow a user to specify only a paper size and set the
80 # width and height accordingly
81 #
82 if (defined ($params{'paper'})) {
83 $params{'width'} = $width{$params{'paper'}}
84 if defined($width{$params{'paper'}});
85 $params{'height'} = $height{$params{'paper'}}
86 if defined($height{$params{'paper'}});
87 }
88
89 # Use the default value if a value is not provided
90 #
91 foreach (@paramnames) {
92 $params{$_} = $defaults{$_} unless (defined($params{$_}));
93 }
94
95 my $self = { content => new PostScript::TextBlock,
96 header => new PostScript::TextBlock,
97 footer => new PostScript::TextBlock,
98 %params };
99
100 bless($self,$class);
101 return $self;
102 }
103  
104 # The addText() method will add an element of text with a given font, size, and
105 # leading to the document. Because the document's content is a single TextBlock,
106 # the addText() method simply calls the addText() method of the TextBlock. The
107 # addHeader() and addFooter() methods add text to the document header and footer
108 # in a similar fashion.
109  
110 sub addText {
111 # Add text to the document
112 #
113 my $self = shift;
114 my %params = @_;
115
116 # Call the PostScript::TextBlock::addText method
117 #
118 $self->{'content'}->addText(%params);
119 }
120  
121 sub addHeader {
122 # Add a textual header to the document
123 #
124 my $self = shift;
125 my %params = @_;
126
127 $self->{'header'}->addText(%params);
128 }
129  
130 sub addFooter {
131 # Add a textual footer to the document
132 #
133 my $self = shift;
134 my %params = @_;
135
136 $self->{'footer'}->addText(%params);
137 }
138  
139 # The Write() method will create the PostScript code for the document and return
140 # it as a string. The method calls the Write() method for the TextBlock
141 # representing the content and, if the block does not fit on one page, takes the
142 # remainder, creates a new page and iteratively draws the text and create new
143 # pages until there is no more text to draw. The Write() method will also print
144 # appropriate structure comments in compliance with the Document Structuring
145 # Conventions. The printHeader() and printFooter() methods are called to draw
146 # the header and footer text with the creation of each new page.
147  
148 sub Write {
149 # The Write() method is called without parameters. It
150 # returns a string containing the complete PostScript code
151 # for the Document.
152 #
153 my $self = shift;
154 my $pages = 1;
155
156 # Should follow the Document Structuring Conventions
157 #
158 my $returnval = "%!PS-Adobe-3.0\n".
159 "%%Creator: The Perl PostScript Package\n".
160 "%%Pages: (atend)\n".
161 "%%BoundingBox: 0 0 $self->{'width'} $self->{'height'}\n".
162 "%%EndComments\n".
163 "%%BeginProlog\n".
164 "%%EndProlog\n";
165  
166 my $w = $self->{'width'} - $self->{'rmargin'} - $self->{'lmargin'};
167 my $h = $self->{'height'} - $self->{'tmargin'} - $self->{'bmargin'};
168 my $x = $self->{'lmargin'};
169 my $y = $h + $self->{'bmargin'};
170  
171 $returnval .= "%%Page: 1\n";
172
173 $returnval .= $self->printHeader($pages);
174 $returnval .= $self->printFooter($pages);
175
176 my ($code, $remainder) = $self->{content}->Write($w, $h, $x, $y);
177 $returnval .= $code;
178 $returnval .= "showpage\n";
179
180 # Print the rest of the pages, if any
181 #
182 while ($remainder->numElements) {
183 $pages++;
184 $returnval .= "%%Page: $pages\n";
185 $returnval .= $self->printHeader($pages);
186 $returnval .= $self->printFooter($pages);
187
188 ($code, $remainder) = $remainder->Write($w, $h, $x, $y);
189 $returnval .= $code;
190 $returnval .= "showpage\n";
191 }
192 $returnval .= "%%Trailer\n".
193 "%%Pages: $pages\n".
194 "%%EOF\n";
195 return $returnval;
196 }
197  
198  
199 sub printHeader {
200 # Create the PostScript code to generate the header
201 # Always starts .25 inches (18 pts) from the top edge of paper
202 #
203 my $self = shift;
204 my $pagenum = shift;
205
206 # Do a search for the ##Page meta string that specifies a page number
207 # and replace it with the page number...
208 #
209 my $header = $self->{'header'};
210 foreach my $element (@$header) {
211 $element->{'text'} =~ s/\#\#Page/$pagenum/g;
212 }
213
214 my ($code, $remainder) =
215 $self->{'header'}->Write( $self->{'width'} -
216 $self->{'rmargin'} -$self->{'lmargin'},
217 $self->{'tmargin'} - 18,
218 $self->{'lmargin'},
219 $self->{'height'}-18
220 );
221
222 # We should put a save/restore pair around the code so that
223 # it doesn't disrupt the current graphics state
224 #
225 return "/savedpage save def\n".$code."savedpage restore\n";
226 }
227  
228 sub printFooter {
229 # Create the PostScript code to generate the footer
230 # Always starts 2 pts from the bottom margin, otherwise same as header.
231 #
232 my $self = shift;
233 my $pagenum = shift;
234
235 my $footer = $self->{'footer'};
236 foreach my $element (@$footer) {
237 print STDERR $pagenum;
238 $element->{'text'} =~ s/\#\#Page/$pagenum/g;
239 }
240
241 my ($code, $remainder) =
242 $self->{'footer'}->Write( $self->{'width'} - $self->{'rmargin'} -
243 $self->{'lmargin'},
244 $self->{'bmargin'} - 18,
245 $self->{'lmargin'},
246 $self->{'bmargin'} - 2
247 );
248 return "/savedpage save def\n".$code."savedpage restore\n";
249 }
250  
251 1;
252