Specs.pm

filter: Statementstmt 28.8% 17 / 59 Branchbran 2.6% 1 / 38 Conditioncond 0.0% 0 / 17 MC/DCmcdc 0.0% 0 / 11 Subroutinesub 33.3% 3 / 9 Podpod 100% 6 / 6 totaltotal 19.2% 27 / 140 CC 27 SCAR 47.0
CC
27
Cov
16%
find
240.0
layout
30.0
units
12.0
CRAP 110.4
↑ Summary Specs/base/label.pm »
1  
2 package Paper::Specs;
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use vars qw($VERSION %brands $brand $units $layout %units $strict $debug);
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 2 $VERSION='0.10';
7  
8 2 $units = 'in';
9 2 $layout = 'normal';
10 2 $brand = '';
11 2 $strict = 1;
12 2 $debug = 0;
13  
14 =head1 NAME
15  
16 Paper::Specs - Size and layout information for paper stock, forms, and labels.
17  
18 =head1 SYNOPSIS
19  
20 use Paper::Specs units => 'cm';
21 my $form = Paper::Specs->find( brand => 'Avery', code => '1234');
22  
23 use Paper::Specs units => 'cm', brand => 'Avery';
24 my $form = Paper::Specs->find( code => '1234');
25  
26 # location of first label on sheet
27 my ($xpos, $ypos) = $form->label_location( 1, 1);
28 my ($h, $w) = $form->label_size;
29  
30 =head1 IMPORTANT NOTES
31  
32 I appologise in advance for the hasty nature of this code. I want to
33 get it out to support some other code I am writing. I promise to revisit
34 it shortly to clear up the rough patches - however your valuable input
35 is most welcome.
36  
37 CAVEAT ALPHA CODE - This is a preliminary module and will be subject to
38 fluctuations in API and structure based on feedback from users.
39  
40 I expect that there will be some interest in this code and it should
41 firm up quickly.
42  
43 If this module does not deliver what you are looking for then you
44 are encouraged to contact the author and voice your needs!
45  
46 OTHER LABELS - I know about the Labels.xml file which is part of OpenOffice
47 but have not figured out how it is encoded. I have the gLabels specifications
48 file too. I plan to use these to help populate the data for this module.
49  
50 =head1 Paper::Specs methods
51  
52 =over
53  
54 =item Import options
55  
56 You can supply any of the methods for this class when it is imported:
57  
58 use Paper::Specs
59 strict => 1,
60 units => 'cm';
61  
62 =cut
63  
64 1 ▶sub import {
Subroutine import: called CC=3 SCAR=16.5
65  
66 1 my $class=shift;
67  
68 1 ▶ while ( @_ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 1 1
69 0 my ($arg, $val) = (shift, shift);
70 0 ▶ $class->$arg($val) if UNIVERSAL::can( $class, $arg);
Branch: 1 branch
BranchTrueFalseTotal
if UNIVERSAL::can($class, $arg) 0 0 0
71 }
72  
73 }
74  
75 =item @forms = Paper::Specs->find( criteria )
76  
77 Returns a list of forms that matches the criteria. There are
78 two fields for criteria: brand and code. The brand can be set
79 for the class via the Paper::Specs->brand method.
80  
81 You must supply at least a brand or a code to find. If no
82 brand is supplied then all known brands will be searched.
83  
84 If you set the module to strict, its default, the find must
85 return extactly zero or one forms as a scalar. Otherwise
86 it will throw an exception.
87  
88 See the beginning of this module for examples of finds.
89  
90 =cut
91  
92 0 ▶sub find {
Subroutine find: not called CC=15 SCAR=54.8
Pod documented
93  
94 0 my $self = shift;
95 0 ▶ my $class = ref($self) ? ref($self) : $self;
Branch: 1 branch
BranchTrueFalseTotal
ref $self ? : 0 0 0
96  
97  
98 0 my %opts = @_;
99 0 ▶ my $brand = $opts{'brand'} || $brand;
Condition0%0/3
$opts{'brand'} || $Paper::Specs::brand
l!l&&r!l&&!r
000
MC/DC0%0/2
$opts{'brand'} || $Paper::Specs::brand
$opts{'brand'} $Paper::Specs::brand
Truth table0%0/3
A || B
A= $opts{'brand'}B= $Paper::Specs::brand
ABresult
1X1
011
000
100 0 my $code = $opts{'code'};
101  
102 0 ▶ die "We need a code or a brand to search for\n" unless $code || $brand;
Branch: 1 branch
BranchTrueFalseTotal
unless $code or $brand 0 0 0
Condition0%0/3
$code or $brand
l!l&&r!l&&!r
000
MC/DC0%0/2
$code or $brand
$code $brand
Truth table0%0/3
A or B
A= $codeB= $brand
ABresult
1X1
011
000
103  
104 0 my @found=();
105 0 ▶ foreach my $brand ( ($brand || $self->brands) ) {
Condition0%0/3
$brand || $self->brands
l!l&&r!l&&!r
000
MC/DC0%0/2
$brand || $self->brands
$brand $self->brands
Truth table0%0/3
A || B
A= $brandB= $self->brands
ABresult
1X1
011
000
106  
107 0 my $sclass = "${class}::${brand}";
108 0 eval "use $sclass";
109 # skip ones that do not load - lame but effective for now
110 0 ▶ if ($Paper::Specs::debug) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Paper::Specs::debug) 0 0 0
111 0 ▶ warn $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
112 }
113 0 ▶ next if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
114  
115 0 push @found, $sclass->find( $code );
116  
117 }
118  
119 0 ▶ if ($self->strict) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->strict) { } 0 0 0
120  
121 0 ▶ if (@found && scalar(@found) == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@found and scalar @found == 1) 0 0 0
Condition0%0/3
@found and scalar @found == 1
!ll&&!rl&&r
000
MC/DC0%0/2
@found and scalar @found == 1
@found scalar @found == 1
Truth table0%0/3
A and B
A= @foundB= scalar @found == 1
ABresult
0X0
100
111
122 0 return $found[0];
123 }
124  
125 0 ▶ if (@found) {
Branch: 1 branch
BranchTrueFalseTotal
if (@found) 0 0 0
126 0 die 'More than one form matches and $self is in strict mode\n';
127 }
128  
129 0 return ();
130  
131 } else {
132 0 ▶ return wantarray ? @found : \@found;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
133 }
134  
135 }
136  
137 # Nice to snoop the lib for all installed Paper modules
138 # next iteration of code, this is good enough for now
139 2 %brands=(
140 'standard' => 1,
141 #'APLI' => 1,
142 #'Agipa' => 1,
143 #'Alpi' => 1,
144 #'Ascom' => 1,
145 'Avery' => 1,
146 #'DataBecker' => 1,
147 #'Ednet' => 1,
148 #'Epson' => 1,
149 #'Great Gizmos' => 1,
150 #'Herlitz' => 1,
151 #'Herma' => 1,
152 #'Imation-SoniX' => 1,
153 #'LeLabel' => 1,
154 #'Leitz' => 1,
155 #'Memorex' => 1,
156 #'Meritline' => 1,
157 #'Neato' => 1,
158 #'Sigel' => 1,
159 #'Southworth' => 1,
160 #'Stomper' => 1,
161 #'Zweckform' => 1,
162 #'unknown' => 1,
163 'photo' => 1,
164 );
165  
166 =item @brands = Paper::Specs->brands
167  
168 Returns a list or reference to a list of the brands for the paper forms that
169 this module is aware of. One brand, 'standard' is reserved for well known paper
170 formats such as letter, A4, etc.
171  
172 =cut
173  
174 0 ▶sub brands {
Subroutine brands: not called CC=2 SCAR=17.9
Pod documented
175  
176 0 ▶ return wantarray ? ( keys %brands ) : [ keys %brands ];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
177  
178 }
179  
180 =item $new_value = Paper::Specs->convert( value, units )
181  
182 Converts 'value' which is in 'units' to new value which is in Paper::Specs->units units.
183  
184 =cut
185  
186 0 ▶sub convert {
Subroutine convert: not called CC=2 SCAR=17.9
Pod documented
187  
188 0 my $value = shift;
189 0 ▶ my $src_units = shift || 'in';
Condition0%0/2
shift() || 'in'
l!l
00
MC/DC0%0/1
shift() || 'in'
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
190  
191 0 return $value / $units{$src_units} * $units{$units};
192  
193 }
194  
195 2 %units = (
196 'in' => 1,
197 'cm' => 2.54,
198 'mm' => 25.4,
199 'pt' => 72,
200 );
201  
202 =item $units = Paper::Specs->units( units )
203  
204 Gets/sets the units that you wish to work with in your code. If you are
205 using metric then you might want 'mm' or 'cm'. If you are using empirial
206 then you might want 'in' or 'pt' (points = 1/72 in).
207  
208 Current units supported are: in, cm, mm, and pt.
209  
210 =cut
211  
212 0 ▶sub units {
Subroutine units: not called CC=3 SCAR=24.8
Pod documented
213  
214 0 my $self=shift;
215  
216 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
217 0 ▶ $units = shift if exists $units{$_[0]};
Branch: 1 branch
BranchTrueFalseTotal
if exists $Paper::Specs::units{$_[0]} 0 0 0
218 }
219  
220 0 return $units;
221  
222 }
223  
224 =item Paper::Specs->layout( 'normal' | 'pdf' )
225  
226 This sets the co-ordinate system for some forms such as labels. 'normal' puts
227 (0,0) at the top left corner. 'pdf' puts (0,0) at the lower left corner.
228  
229 As well 'pdf' calls units('pt'). You can reset this afterwards if you are working
230 in a different unit system.
231  
232 =cut
233  
234  
235 0 ▶sub layout {
Subroutine layout: not called CC=5 SCAR=34.0
Pod documented
236  
237 0 my $self=shift;
238  
239 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
240 0 $layout = lc shift;
241 0 ▶ $layout = 'normal' unless $layout eq 'normal' || $layout eq 'pdf';
Branch: 1 branch
BranchTrueFalseTotal
unless $Paper::Specs::layout eq "normal" or $Paper::Specs::layout eq "pdf" 0 0 0
Condition0%0/3
$Paper::Specs::layout eq "normal" or $Paper::Specs::layout eq "pdf"
l!l&&r!l&&!r
000
MC/DC0%0/2
$Paper::Specs::layout eq "normal" or $Paper::Specs::layout eq "pdf"
$Paper::Specs::layout eq "normal" $Paper::Specs::layout eq "pdf"
Truth table0%0/3
A or B
A= $Paper::Specs::layout eq "normal"B= $Paper::Specs::layout eq "pdf"
ABresult
1X1
011
000
242 0 ▶ if ( $layout eq 'pdf' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Paper::Specs::layout eq "pdf") 0 0 0
243 0 $self->units('pt');
244 }
245 }
246  
247 0 return $layout;
248  
249 }
250  
251 =item Paper::Specs->strict( 0 | 1 )
252  
253 Sets the strictness of this module. If it is strict then it will throw exceptions via 'die' for things
254 like finding more than one form on a find method.
255  
256 The default is to be strict.
257  
258 =cut
259  
260 0 ▶sub strict {
Subroutine strict: not called CC=3 SCAR=24.8
Pod documented
261  
262 0 my $self=shift;
263 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
264 0 ▶ $strict = $_[0] ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$_[0] ? : 0 0 0
265 }
266 0 return $strict;
267  
268 }
269  
270 =back
271  
272 =head1 Paper::Specs items
273  
274 You get little object references back when you find specifications. These
275 objects can supply you with information that you are looking for but do not
276 actually store any values.
277  
278 You should test that the object is of the type you are looking for
279  
280 if ($form->type ne 'label') {
281 die "Feed me labels Seymore\n";
282 }
283  
284 Other than that - most forms should be based on a sheet (of paper) but will have
285 different methods depending on what they are.
286  
287 =head1 Paper::Specs::sheet methods / $form->type eq 'sheet'
288  
289 These methods apply forms of type 'sheet' and all that are derived from it. (all other forms and stock)
290  
291 =over 4
292  
293 =item ($h, $w) = $form->sheet_size
294  
295 Returns the height and width of your sheet.
296  
297 =item $size = $form->sheet_width
298  
299 Width of the stock
300  
301 =item $size = $form->sheet_height
302  
303 Height of the stock
304  
305 =back
306  
307 =head1 Paper::Specs::label methods / $form->type eq 'label'
308  
309 These methods apply to forms of type 'label' and all that are derived from it.
310  
311 Inherits methods from Paper::Specs::sheet.
312  
313 =over 4
314  
315 =item ( $width, $height ) = $form->label_size
316  
317 Returns just that; a list with the width and height of a label in it
318  
319 =item ( $x, $y ) = $form->label_location( $r, $c )
320  
321 Returns the location of the upper left corner for label at row $r and col $c
322 based on your current format/co-ordinate system.
323  
324 =item $size = $form->margin_left
325  
326 Space between left edge of paper and first column of labels.
327  
328 =item $size = $form->margin_right
329  
330 Space between right edge of paper and last column of labels.
331  
332 =item $size = $form->margin_top
333  
334 Space between top edge of paper and first row of labels.
335  
336 =item $size = $form->margin_bottom
337  
338 Space between bottom edge of paper and last row of labels.
339  
340 =item $size = $form->label_height
341  
342 Height of one label
343  
344 =item $size = $form->label_width
345  
346 Width of one label
347  
348 =item $count = label_rows
349  
350 Number of rows of labels on a sheet
351  
352 =item $count = label_cols
353  
354 Number of columns of labels on a sheet
355  
356 =item $size = $form->gutter_cols
357  
358 Inner space between labels - column gutter.
359  
360 =item $size = $form->gutter_rows
361  
362 Inner space between labels - row gutter.
363  
364 =back
365  
366 =head1 SEE ALSO
367  
368 Paper::Specs homepage - L<http://perl.jonallen.info/projects/paper-specs>
369  
370 =head1 BUGS
371  
372 Please report your bugs and suggestions for improvement to <jj@jonallen.info>.
373  
374 =head1 AUTHORS
375  
376 Originally written by Jay Lawrence <jlawrenc@cpan.org>
377  
378 From version 0.06 onwards this module is maintained by Jon Allen (JJ) <jj@jonallen.info> / L<http://perl.jonallen.info>
379  
380 =head1 COPYRIGHT and LICENCE
381  
382 Copyright (c)2001-2003 - Jay Lawrence, Infonium Inc. All rights reserved.
383  
384 Modifications from version 0.06 onwards Copyright (c) 2004-2005 Jon Allen (JJ).
385  
386 This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
387  
388 Software distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. This software
389 is not affiliated with the Apache Software Foundation (ASF).
390  
391 =cut
392  
393 2 1;
↑ Summary Specs/base/label.pm »