File Coverage

blib/lib/HTML/Barcode.pm
Criterion Covered Total %
statement 6 58 10.3
branch 0 26 0.0
condition 0 3 0.0
subroutine 2 12 16.6
pod 4 4 100.0
total 12 103 11.6


line stmt bran cond sub pod time code
1             package HTML::Barcode;
2 1     1   20559 use Any::Moose;
  1         32172  
  1         7  
3              
4             our $VERSION = '0.11';
5              
6             has 'text' => (
7             is => 'rw',
8             isa => 'Str',
9             documentation => 'The information to put into the barcode.',
10             );
11             has 'foreground_color' => (
12             is => 'rw',
13             isa => 'Str',
14             default => '#000',
15             documentation => 'A CSS color value for the foreground.',
16             );
17             has 'background_color' => (
18             is => 'rw',
19             isa => 'Str',
20             default => '#fff',
21             documentation => 'A CSS color value for the background.',
22             );
23             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             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             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             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             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             has 'td_on_class' => (is => 'rw', 'isa' => 'Str', lazy => 1, builder => '_build_td_on_class');
58             has 'td_off_class' => (is => 'rw', 'isa' => 'Str', lazy => 1, builder => '_build_td_off_class');
59             sub _css_class_set {
60 0     0     my ($self) = @_;
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     0     sub _build_td_on_class { return $_[0]->css_class . '_on'; }
65 0     0     sub _build_td_off_class { return $_[0]->css_class . '_off'; }
66              
67             # You need to override this.
68 0     0 1   sub barcode_data { return []; }
69              
70             sub render {
71 0     0 1   my ($self) = @_;
72 0 0         return '' unless defined $self->text;
73 0           return q()
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     0 1   my ($self) = @_;
82 0 0         return '' unless defined $self->text;
83              
84 0           my $data = $self->barcode_data;
85 0           my $num_columns = 1;
86              
87 0           my @rows = ();
88 0 0         if (@$data > 0) {
89 0 0 0       if (ref $data->[0] && ref $data->[0] eq 'ARRAY') {
90 0           $num_columns = scalar @{$data->[0]};
  0            
91 0           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 0           push @rows, $self->_generate_tr(
99 0           join('', map { $self->_generate_td($_) } @$data)
100             );
101             }
102             }
103              
104 0 0         if ($self->show_text) {
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     0 1   my $self = shift;
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     0     my ($self, $contents) = @_;
131 0           my $class = $self->css_class;
132              
133 0           my $style = '';
134 0 0         if ($self->embed_style) {
135 0           $style = ' style="border:0;margin:0;padding:0;border-spacing:0;"';
136             }
137              
138 0           return qq{$contents
}; 139             } 140               141             sub _generate_tr { 142 0     0     my ($self, $contents) = @_; 143               144 0           my $style = ''; 145 0 0         if ($self->embed_style) { 146 0           $style = ' style="border:0;margin:0;padding:0;"'; 147             } 148               149 0           return qq($contents); 150             } 151               152             sub _generate_td { 153 0     0     my ($self, $on, $colspan, $content) = @_; 154               155 0           my $style = ''; 156 0 0         if ($self->embed_style) { 157 0 0         my $color = $on ? $self->foreground_color : $self->background_color; 158 0 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;"';     0           159             } 160               161 0 0         if ($colspan) { 162 0           return qq{$content}; 163             } else { 164 0 0         my $class = $on ? $self->td_on_class : $self->td_off_class; 165 0           return qq{}; 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 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 - Two dimensional QR codes. 195               196             =item L - Two dimensional Data Matrix barcodes. 197               198             =item L - Code 93 barcodes. 199               200             =item L - 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 207             for traditional barcodes, L for 2-dimensional barcodes, 208             or L if neither of those suit your needs. 209               210             Use one of the L 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 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, C, C) as you see fit. 230               231             =back 232               233             =head1 METHODS 234               235             =head2 new (%attributes) 236               237             Default constructor provided by L or L, which can take values for 238             any of the L. 239               240             =head2 render 241               242             This is a convenience routine which returns C<< >> tags 243             and the rendered barcode. 244               245             If you are printing multiple barcodes or want to ensure your C