File Coverage

blib/lib/HTML/Barcode.pm
Criterion Covered Total %
statement 3 55 5.4
branch 0 26 0.0
condition 0 3 0.0
subroutine 1 11 9.0
pod 4 4 100.0
total 8 99 8.0


line stmt bran cond sub pod time code
1             package HTML::Barcode;
2 1     1   13477 use Moo;
  1         9678  
  1         4  
3              
4             our $VERSION = '0.13';
5              
6             has 'text' => (
7             is => 'rw',
8             documentation => 'The information to put into the barcode.',
9             );
10             has 'foreground_color' => (
11             is => 'rw',
12             default => '#000',
13             documentation => 'A CSS color value for the foreground.',
14             );
15             has 'background_color' => (
16             is => 'rw',
17             default => '#fff',
18             documentation => 'A CSS color value for the background.',
19             );
20             has 'bar_width' => (
21             is => 'rw',
22             default => '2px',
23             documentation => 'A CSS value for the width of an individual bar.',
24             );
25             has 'bar_height' => (
26             is => 'rw',
27             default => '100px',
28             documentation => 'A CSS value for the height of an individual bar.',
29             );
30             has show_text => (
31             is => 'rw',
32             default => 1,
33             documentation => 'Boolean. Indicates whether or not to render the text below the barcode.',
34             );
35              
36             has 'css_class' => (
37             is => 'rw',
38             default => 'hbc',
39             trigger => \&_css_class_set,
40             documentation => 'The value for the "class" attribute applied to any container tags.',
41             );
42              
43             has 'embed_style' => (
44             is => 'rw',
45             default => 0,
46             documentation => 'Boolean. Embed the style information in HTML "style" attributes. This is NOT recommended.',
47             );
48              
49             has 'td_on_class' => (is => 'rw', lazy => 1, builder => '_build_td_on_class');
50             has 'td_off_class' => (is => 'rw', lazy => 1, builder => '_build_td_off_class');
51             sub _css_class_set {
52 0     0     my ($self) = @_;
53 0           $self->td_on_class($self->_build_td_on_class);
54 0           $self->td_off_class($self->_build_td_off_class);
55             }
56 0     0     sub _build_td_on_class { return $_[0]->css_class . '_on'; }
57 0     0     sub _build_td_off_class { return $_[0]->css_class . '_off'; }
58              
59             # You need to override this.
60 0     0 1   sub barcode_data { return []; }
61              
62             sub render {
63 0     0 1   my ($self) = @_;
64 0 0         return '' unless defined $self->text;
65 0           return q()
66             . $self->render_barcode();
67             }
68              
69             # If you're doing anything that can't be represented by a rectangular
70             # grid, you need to override this.
71             # This needs to also render the text, if $self->show_text is true.
72             sub render_barcode {
73 0     0 1   my ($self) = @_;
74 0 0         return '' unless defined $self->text;
75              
76 0           my $data = $self->barcode_data;
77 0           my $num_columns = 1;
78              
79 0           my @rows = ();
80 0 0         if (@$data > 0) {
81 0 0 0       if (ref $data->[0] && ref $data->[0] eq 'ARRAY') {
82 0           $num_columns = scalar @{$data->[0]};
  0            
83             push @rows, map {
84 0           $self->_generate_tr(
85 0           join('', map { $self->_generate_td($_) } @$_)
  0            
86             );
87             } @$data;
88             } else {
89 0           $num_columns = scalar @$data;
90             push @rows, $self->_generate_tr(
91 0           join('', map { $self->_generate_td($_) } @$data)
  0            
92             );
93             }
94             }
95              
96 0 0         if ($self->show_text) {
97 0           push @rows, $self->_generate_tr(
98             $self->_generate_td(undef, $num_columns, $self->text)
99             );
100             }
101              
102 0           return $self->_generate_table(join '', @rows);
103             }
104              
105             sub css {
106 0     0 1   my $self = shift;
107 0           my $class = $self->css_class;
108 0           my $on = $self->td_on_class;
109 0           my $off = $self->td_off_class;
110             return
111 0           "table.${class} {border-width:0;border-spacing:0;}"
112             . "table.${class} {border-width:0;border-spacing:0;}"
113             . "table.${class} tr, table.${class} td{border:0;margin:0;padding:0;}"
114             . "table.${class} td{text-align:center;}"
115             . "table.${class} td.${on},table.${class} td.${off} {width:" . $self->bar_width . ";height:" . $self->bar_height . ";}"
116             . "table.${class} td.${on} {background-color:" . $self->foreground_color . ";color:inherit;}"
117             . "table.${class} td.${off} {background-color:" . $self->background_color . ";color:inherit;}"
118             ;
119             }
120              
121             sub _generate_table {
122 0     0     my ($self, $contents) = @_;
123 0           my $class = $self->css_class;
124              
125 0           my $style = '';
126 0 0         if ($self->embed_style) {
127 0           $style = ' style="border:0;margin:0;padding:0;border-spacing:0;"';
128             }
129              
130 0           return qq{$contents
}; 131             } 132               133             sub _generate_tr { 134 0     0     my ($self, $contents) = @_; 135               136 0           my $style = ''; 137 0 0         if ($self->embed_style) { 138 0           $style = ' style="border:0;margin:0;padding:0;"'; 139             } 140               141 0           return qq($contents); 142             } 143               144             sub _generate_td { 145 0     0     my ($self, $on, $colspan, $content) = @_; 146               147 0           my $style = ''; 148 0 0         if ($self->embed_style) { 149 0 0         my $color = $on ? $self->foreground_color : $self->background_color; 150 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           151             } 152               153 0 0         if ($colspan) { 154 0           return qq{$content}; 155             } else { 156 0 0         my $class = $on ? $self->td_on_class : $self->td_off_class; 157 0           return qq{}; 158             } 159             } 160               161             =head1 NAME 162               163             HTML::Barcode - Render HTML representations of barcodes 164               165             =head1 SYNOPSIS 166               167             # HTML::Barcode::Code93 is just one example, there are others. 168             my $code = HTML::Barcode::Code93->new(text => 'MONKEY'); 169             print $code->render; 170               171             See the documentation for the specific L 172             for detailed instructions. 173               174             =head1 DESCRIPTION 175               176             This is a base class for creating HTML representations of barcodes. 177             Do not use it directly. If you are looking to generate a barcode, please see one of the following modules instead: 178               179             =head2 Known Types 180               181             Here are the types of barcodes you can generate with this distribution. 182             Others may exist, so try searching CPAN. 183               184             =over 4 185               186             =item L - Two dimensional QR codes. 187               188             =item L - Two dimensional Data Matrix barcodes. 189               190             =item L - Code 93 barcodes. 191               192             =item L - Code 128 barcodes. 193               194             =back 195               196             =head2 Subclassing 197               198             To add a new type of barcode, create a subclass of either L 199             for traditional barcodes, L for 2-dimensional barcodes, 200             or L if neither of those suit your needs. 201               202             Use one of the L as a starting point. 203               204             =head3 Required methods for your subclass 205               206             =over 4 207               208             =item barcode_data 209               210             You need to either override this, or override the C method 211             so it does not use this. 212               213             This should return an arrayref of true and false values (for "on" and "off"), 214             or an arrayref of arrayrefs (for 2D). 215               216             It is not recommended to publish this method in your API. 217               218             =item Other methods 219               220             Feel free to override any other methods, or use method modifiers 221             (C, C, C) as you see fit. 222               223             =back 224               225             =head1 METHODS 226               227             =head2 new (%attributes) 228               229             Default constructor provided by L, which can take values for 230             any of the L. 231               232             =head2 render 233               234             This is a convenience routine which returns C<< >> tags 235             and the rendered barcode. 236               237             If you are printing multiple barcodes or want to ensure your C