Barcode.pm

filter: Statementstmt 25.7% 18 / 70 Branchbran 0.0% 0 / 26 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 16.6% 2 / 12 Podpod 100% 4 / 4 totaltotal 20.5% 24 / 117 SCAR 49.3
CC
15
Cov
18%
_generate_td
56.0
render_barcode
42.0
render
6.0
CRAP 138.2
↑ Summary Barcode/1D.pm »
1 package HTML::Barcode;
2 1 ▶use Any::Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 1 our $VERSION = '0.11';
5  
6 1 has 'text' => (
7 is => 'rw',
8 isa => 'Str',
9 documentation => 'The information to put into the barcode.',
10 );
11 1 has 'foreground_color' => (
12 is => 'rw',
13 isa => 'Str',
14 default => '#000',
15 documentation => 'A CSS color value for the foreground.',
16 );
17 1 has 'background_color' => (
18 is => 'rw',
19 isa => 'Str',
20 default => '#fff',
21 documentation => 'A CSS color value for the background.',
22 );
23 1 has 'bar_width' => (
24 is => 'rw',
25 isa => 'Str',
26 default => '2px',
27 documentation => 'A CSS value for the width of an individual bar.',
28 );
29 1 has 'bar_height' => (
30 is => 'rw',
31 isa => 'Str',
32 default => '100px',
33 documentation => 'A CSS value for the height of an individual bar.',
34 );
35 1 has show_text => (
36 is => 'rw',
37 isa => 'Bool',
38 default => 1,
39 documentation => 'Indicates whether or not to render the text below the barcode.',
40 );
41  
42 1 has 'css_class' => (
43 is => 'rw',
44 isa => 'Str',
45 default => 'hbc',
46 trigger => \&_css_class_set,
47 documentation => 'The value for the "class" attribute applied to any container tags.',
48 );
49  
50 1 has 'embed_style' => (
51 is => 'rw',
52 isa => 'Bool',
53 default => 0,
54 documentation => 'Embed the style information in HTML "style" attributes. This is NOT recommended.',
55 );
56  
57 1 has 'td_on_class' => (is => 'rw', 'isa' => 'Str', lazy => 1, builder => '_build_td_on_class');
58 1 has 'td_off_class' => (is => 'rw', 'isa' => 'Str', lazy => 1, builder => '_build_td_off_class');
59 sub _css_class_set {
60 0 ▶ my ($self) = @_;
Subroutine _css_class_set: not called CC=1 SCAR=6.9
61 0 $self->td_on_class($self->_build_td_on_class);
62 0 $self->td_off_class($self->_build_td_off_class);
63 }
64 0 ▶sub _build_td_on_class { return $_[0]->css_class . '_on'; }
Subroutine _build_td_on_class: not called CC=1 SCAR=6.9
65 0 ▶sub _build_td_off_class { return $_[0]->css_class . '_off'; }
Subroutine _build_td_off_class: not called CC=1 SCAR=6.9
66  
67 # You need to override this.
68 0 ▶sub barcode_data { return []; }
Subroutine barcode_data: not called CC=1 SCAR=6.9
Pod documented
69  
70 sub render {
71 0 ▶ my ($self) = @_;
Subroutine render: not called CC=2 SCAR=17.9
Pod documented
72 0 ▶ return '' unless defined $self->text;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->text 0 0 0
73 0 return q(<style type="text/css">) . $self->css . q(</style>)
74 . $self->render_barcode();
75 }
76  
77 # If you're doing anything that can't be represented by a rectangular
78 # grid, you need to override this.
79 # This needs to also render the text, if $self->show_text is true.
80 sub render_barcode {
81 0 ▶ my ($self) = @_;
Subroutine render_barcode: not called CC=6 SCAR=37.4
Pod documented
82 0 ▶ return '' unless defined $self->text;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->text 0 0 0
83  
84 0 my $data = $self->barcode_data;
85 0 my $num_columns = 1;
86  
87 0 my @rows = ();
88 0 ▶ if (@$data > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$data > 0) 0 0 0
89 0 ▶ if (ref $data->[0] && ref $data->[0] eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $data->[0] and ref $data->[0] eq 'ARRAY') { } 0 0 0
Condition0%0/3
ref $data->[0] and ref $data->[0] eq "ARRAY"
!ll&&!rl&&r
000
MC/DC0%0/2
ref $data->[0] and ref $data->[0] eq "ARRAY"
ref $data->[0] ref $data->[0] eq "ARRAY"
Truth table0%0/3
A and B
A= ref $data->[0]B= ref $data->[0] eq "ARRAY"
ABresult
0X0
100
111
90 0 $num_columns = scalar @{$data->[0]};
91 push @rows, map {
92 0 $self->_generate_tr(
93 0 join('', map { $self->_generate_td($_) } @$_)
94 );
95 } @$data;
96 } else {
97 0 $num_columns = scalar @$data;
98 push @rows, $self->_generate_tr(
99 0 join('', map { $self->_generate_td($_) } @$data)
100 );
101 }
102 }
103  
104 0 ▶ if ($self->show_text) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->show_text) 0 0 0
105 0 push @rows, $self->_generate_tr(
106 $self->_generate_td(undef, $num_columns, $self->text)
107 );
108 }
109  
110 0 return $self->_generate_table(join '', @rows);
111 }
112  
113 sub css {
114 0 ▶ my $self = shift;
Subroutine css: not called CC=1 SCAR=6.9
Pod documented
115 0 my $class = $self->css_class;
116 0 my $on = $self->td_on_class;
117 0 my $off = $self->td_off_class;
118 return
119 0 "table.${class} {border-width:0;border-spacing:0;}"
120 . "table.${class} {border-width:0;border-spacing:0;}"
121 . "table.${class} tr, table.${class} td{border:0;margin:0;padding:0;}"
122 . "table.${class} td{text-align:center;}"
123 . "table.${class} td.${on},table.${class} td.${off} {width:" . $self->bar_width . ";height:" . $self->bar_height . ";}"
124 . "table.${class} td.${on} {background-color:" . $self->foreground_color . ";color:inherit;}"
125 . "table.${class} td.${off} {background-color:" . $self->background_color . ";color:inherit;}"
126 ;
127 }
128  
129 sub _generate_table {
130 0 ▶ my ($self, $contents) = @_;
Subroutine _generate_table: not called CC=2 SCAR=17.9
131 0 my $class = $self->css_class;
132  
133 0 my $style = '';
134 0 ▶ if ($self->embed_style) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->embed_style) 0 0 0
135 0 $style = ' style="border:0;margin:0;padding:0;border-spacing:0;"';
136 }
137  
138 0 return qq{<table$style class="$class">$contents</table>};
139 }
140  
141 sub _generate_tr {
142 0 ▶ my ($self, $contents) = @_;
Subroutine _generate_tr: not called CC=2 SCAR=17.9
143  
144 0 my $style = '';
145 0 ▶ if ($self->embed_style) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->embed_style) 0 0 0
146 0 $style = ' style="border:0;margin:0;padding:0;"';
147 }
148  
149 0 return qq(<tr$style>$contents</tr>);
150 }
151  
152 sub _generate_td {
153 0 ▶ my ($self, $on, $colspan, $content) = @_;
Subroutine _generate_td: not called CC=7 SCAR=40.3
154  
155 0 my $style = '';
156 0 ▶ if ($self->embed_style) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->embed_style) 0 0 0
157 0 ▶ my $color = $on ? $self->foreground_color : $self->background_color;
Branch: 1 branch
BranchTrueFalseTotal
$on ? : 0 0 0
158 0 ▶ $style = ' style="border:0;margin:0;padding:0;width:' . ($colspan ? 'auto' : $self->bar_width) . ';height:' . ($colspan ? 'auto' : $self->bar_height) . ';background-color:' . $color . ';color:inherit;text-align:center;"';
Branch: 2 branches
BranchTrueFalseTotal
$colspan ? : 0 0 0
$colspan ? : 0 0 0
159 }
160  
161 0 ▶ if ($colspan) {
Branch: 1 branch
BranchTrueFalseTotal
if ($colspan) { } 0 0 0
162 0 return qq{<td$style colspan="$colspan">$content</td>};
163 } else {
164 0 ▶ my $class = $on ? $self->td_on_class : $self->td_off_class;
Branch: 1 branch
BranchTrueFalseTotal
$on ? : 0 0 0
165 0 return qq{<td$style class="$class"></td>};
166 }
167 }
168  
169 =head1 NAME
170  
171 HTML::Barcode - Render HTML representations of barcodes
172  
173 =head1 SYNOPSIS
174  
175 # HTML::Barcode::Code93 is just one example, there are others.
176 my $code = HTML::Barcode::Code93->new(text => 'MONKEY');
177 print $code->render;
178  
179 See the documentation for the specific L<types of barcodes|/"Known Types">
180 for detailed instructions.
181  
182 =head1 DESCRIPTION
183  
184 This is a base class for creating HTML representations of barcodes.
185 Do not use it directly. If you are looking to generate a barcode, please see one of the following modules instead:
186  
187 =head2 Known Types
188  
189 Here are the types of barcodes you can generate with this distribution.
190 Others may exist, so try searching CPAN.
191  
192 =over 4
193  
194 =item L<HTML::Barcode::QRCode> - Two dimensional QR codes.
195  
196 =item L<HTML::Barcode::DataMatrix> - Two dimensional Data Matrix barcodes.
197  
198 =item L<HTML::Barcode::Code93> - Code 93 barcodes.
199  
200 =item L<HTML::Barcode::Code128> - Code 128 barcodes.
201  
202 =back
203  
204 =head2 Subclassing
205  
206 To add a new type of barcode, create a subclass of either L<HTML::Barcode::1D|HTML::Barcode::1D/Subclassing>
207 for traditional barcodes, L<HTML::Barcode::2D|HTML::Barcode::2D/Subclassing> for 2-dimensional barcodes,
208 or L<HTML::Barcode> if neither of those suit your needs.
209  
210 Use one of the L<existing modules|/"Known Types"> as a starting point.
211  
212 =head3 Required methods for your subclass
213  
214 =over 4
215  
216 =item barcode_data
217  
218 You need to either override this, or override the C<render_barcode> method
219 so it does not use this.
220  
221 This should return an arrayref of true and false values (for "on" and "off"),
222 or an arrayref of arrayrefs (for 2D).
223  
224 It is not recommended to publish this method in your API.
225  
226 =item Other methods
227  
228 Feel free to override any other methods, or use method modifiers
229 (C<around>, C<before>, C<after>) as you see fit.
230  
231 =back
232  
233 =head1 METHODS
234  
235 =head2 new (%attributes)
236  
237 Default constructor provided by L<Moose> or L<Mouse>, which can take values for
238 any of the L<attributes|/ATTRIBUTES>.
239  
240 =head2 render
241  
242 This is a convenience routine which returns C<< <style>...</style> >> tags
243 and the rendered barcode.
244  
245 If you are printing multiple barcodes or want to ensure your C<style> tags
246 are in your HTML headers, then you probably want to output the barcode
247 and style separately with L</render_barcode> and
248 L</css>.
249  
250 =head2 render_barcode
251  
252 Returns only the rendered barcode. You will need to provide stylesheets
253 separately, either writing them yourself or using the output of L</css>.
254  
255 =head2 css
256  
257 Returns CSS needed to properly display your rendered barcode.
258  
259 =head1 ATTRIBUTES
260  
261 These attributes can be passed to L<new|/"new (%attributes)">, or used
262 as accessors.
263  
264 =head2 text
265  
266 B<Required> - The information to put into the barcode.
267  
268 =head2 foreground_color
269  
270 A CSS color value (e.g. '#000' or 'black') for the foreground. Default is '#000'.
271  
272 =head2 background_color
273  
274 A CSS color value background. Default is '#fff'.
275  
276 =head2 bar_width
277  
278 A CSS value for the width of an individual bar. Default is '2px'.
279  
280 =head2 bar_height
281  
282 A CSS value for the height of an individual bar. Default is '100px'.
283  
284 =head2 show_text
285  
286 Boolean. Indicates whether or not to render the text below the barcode.
287  
288 =head2 css_class
289  
290 The value for the C<class> attribute applied to any container tags
291 in the HTML (e.g. C<table> or C<div>).
292 C<td> tags within the table will have either css_class_on or css_class_off
293 classes applied to them.
294  
295 For example, if css_class is "barcode", you will get C<< <table class="barcode"> >> and its cells will be either C<< <td class="barcode_on"> >> or
296 C<< <td class="barcode_off"> >>.
297  
298 =head2 embed_style
299  
300 Rather than rendering CSS stylesheets, embed the style information
301 in HTML C<style> attributes. You should not use this option without
302 good reason, as it greatly increases the size of the generated markup,
303 and makes it impossible to override with stylesheets.
304  
305 =head1 AUTHOR
306  
307 Mark A. Stratman, C<< <stratman@gmail.com> >>
308  
309 =head1 SOURCE REPOSITORY
310  
311 L<http://github.com/mstratman/HTML-Barcode>
312  
313 =head1 LICENSE AND COPYRIGHT
314  
315 Copyright 2011 Mark A. Stratman.
316  
317 This program is free software; you can redistribute it and/or modify it
318 under the terms of either: the GNU General Public License as published
319 by the Free Software Foundation; or the Artistic License.
320  
321 See http://dev.perl.org/licenses/ for more information.
322  
323  
324 =cut
325  
326 1 ▶no Any::Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
327 1 1; # End of HTML::Barcode
↑ Summary Barcode/1D.pm »