blib/lib/SVG/Barcode.pm

filter: Statementstmt 71.4% 80 / 112 Branchbran 30.0% 6 / 20 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 80.0% 20 / 25 Podpod 100% 3 / 3 totaltotal 68.1% 109 / 160 SCAR 35.3
CC
19
Cov
65%
_plot_2d
30.0
_plot_1d
20.0
__ANON__
6.0
CRAP 34.3
↑ Summary
1 package SVG::Barcode;
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use v5.24;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use feature 'signatures';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶no warnings 'experimental::signatures';
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 1 ▶use Carp 'croak';
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use POSIX 'fmax';
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Sub::Util 'set_subname';
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 1 our $VERSION = '0.10';
14  
15 1 use constant DEFAULTS => {
16 background => 'white',
17 class => '',
18 foreground => 'black',
19 height => '',
20 id => '',
21 margin => 2,
22 width => '',
23 1 ▶};
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 1 _param(__PACKAGE__, $_, DEFAULTS->{$_}) for keys DEFAULTS->%*;
26  
27 # constructor
28  
29 ▶sub new ($class, %params) {
Subroutine new: called CC=2 SCAR=6.9
Pod documented
30 4 my $self = bless {DEFAULTS->%*, $class->DEFAULTS->%*}, $class;
31  
32 4 $self->$_($params{$_}) for keys %params;
33  
34 3 return $self;
35 }
36  
37 # methods
38  
39 ▶sub plot ($self, $text) {
Subroutine plot: called CC=3 SCAR=11.0
Pod documented
40 $self->{elements}
41 2 = [qq| <rect width="100%" height="100%" fill="$self->{background}"/>|];
42 2 $self->{vbwidth} = $self->{vbheight} = 0;
43  
44 2 $self->_plot($text);
45  
46 1 $self->{vbheight} += $self->{margin};
47 1 $self->{vbwidth} += $self->{margin};
48 1 my @attr = (qq|viewBox="0 0 $self->{vbwidth} $self->{vbheight}"|);
49 1 for my $name (qw|id class width height|) {
50 4 ▶ my $value = $self->$name or next;
Branch: 1 branch
BranchTrueFalseTotal
my $value = $self->$name or next 2 2 4
51 2 push @attr, qq|$name="$value"|;
52 }
53 1 my $attributes = join ' ', sort @attr;
54  
55 my $svg
56 = qq|<svg $attributes xmlns="http://www.w3.org/2000/svg">\n|
57 . join("\n", $self->{elements}->@*)
58 1 . qq|\n</svg>|;
59  
60 1 return $svg;
61 }
62  
63 # internal methods
64  
65 ▶sub _param ($class, $name, $default) {
Subroutine _param: called CC=1 SCAR=0.0
66 1 ▶ no strict 'refs'; ## no critic 'ProhibitNoStrict'
Subroutine BEGIN: called CC=1 SCAR=0.0
67 1 ▶ no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
68 7 ▶ *{"${class}::$name"} = set_subname $name, sub ($self, $newvalue = undef) {
Subroutine foreground: called CC=4 SCAR=13.9
Pod documented
69 27 ▶ if (defined $newvalue) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $newvalue) { } 7 20 27
70 7 ▶ $self->{$name} = $newvalue eq '' ? $default : $newvalue;
Branch: 1 branch
BranchTrueFalseTotal
$newvalue eq '' ? : 1 6 7
71 7 delete $self->{plotter};
72 7 return $self;
73 } else {
74 20 return $self->{$name};
75 }
76 7 };
77 }
78  
79 ▶sub _plot (@) {
Subroutine _plot: called CC=1 SCAR=0.0
80 1 croak 'Method _plot not implemented by subclass!';
81 }
82  
83 ▶sub _plot_1d ($self, $code, $sign) {
Subroutine _plot_1d: not called CC=4 SCAR=30.0
84 0 my @line;
85 0 my $width = $self->{linewidth};
86 0 my $height = $self->{lineheight};
87 my $add_line = sub {
88 0 ▶ if (@line) {
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
if (@line) 0 0 0
89 0 $self->_rect(@line);
90 0 @line = ();
91 }
92 0 };
93  
94 0 for my $x (0 .. $#$code) {
95 0 ▶ if ($code->[$x] eq $sign) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code->[$x] eq $sign) { } 0 0 0
96 0 ▶ if (@line) {
Branch: 1 branch
BranchTrueFalseTotal
if (@line) { } 0 0 0
97 0 $line[2] += $width;
98 } else {
99 0 @line = ($x * $width, 0, $width, $height);
100 }
101 } else {
102 0 $add_line->();
103 }
104 }
105 0 $add_line->();
106 }
107  
108 ▶sub _plot_2d ($self, $code, $sign) {
Subroutine _plot_2d: not called CC=5 SCAR=34.0
109 0 my $x_max = $code->[0]->@* - 1;
110 0 my $y_max = $code->@* - 1;
111  
112 0 my @dot;
113 0 my $dotsize = $self->{dotsize};
114 my $add_dot = sub {
115 0 ▶ if (@dot) {
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
if (@dot) 0 0 0
116 0 $self->_rect(@dot);
117 0 @dot = ();
118 }
119 0 };
120  
121 0 for my $y (0 .. $y_max) {
122 0 for my $x (0 .. $x_max) {
123 0 ▶ if ($code->[$y][$x] eq $sign) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code->[$y][$x] eq $sign) { } 0 0 0
124 0 ▶ if (@dot) {
Branch: 1 branch
BranchTrueFalseTotal
if (@dot) { } 0 0 0
125 0 $dot[2] += $dotsize;
126 } else {
127 0 @dot = ($x * $dotsize, $y * $dotsize, $dotsize, $dotsize);
128 }
129 } else {
130 0 $add_dot->();
131 }
132 }
133 0 $add_dot->();
134 }
135 }
136  
137 ▶sub _plot_text ($self, $text) {
Subroutine _plot_text: not called CC=2 SCAR=17.9
138 0 ▶ if (my $size = $self->{textsize}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $size = $self->{"textsize"}) 0 0 0
139 0 $self->_text($text, 0, $self->{lineheight} + $size, $size);
140 }
141 }
142  
143 4 ▶sub _rect ($self, $x, $y, $width, $height, $color = $self->{foreground}) {
Subroutine _rect: called CC=2 SCAR=6.9
144 4 my $x1 = $x + $self->{margin};
145 4 my $y1 = $y + $self->{margin};
146 4 $self->{vbwidth} = fmax $self->{vbwidth}, $x1 + $width;
147 4 $self->{vbheight} = fmax $self->{vbheight}, $y1 + $height;
148  
149 push $self->{elements}->@*,
150 4 qq| <rect x="$x1" y="$y1" width="$width" height="$height" fill="$color"/>|;
151  
152 4 return $self;
153 }
154  
155 1 ▶sub _text ($self, $text, $x, $y, $size, $color = $self->{foreground}) {
Subroutine _text: called CC=2 SCAR=6.9
156 1 my $escaped = $self->_xml_escape($text);
157 1 my $x1 = $x + $self->{margin};
158 1 my $y1 = $y + $self->{margin};
159 1 $self->{vbheight} = fmax $self->{vbheight}, $y1;
160  
161 push $self->{elements}->@*,
162 1 qq| <text x="$x1" y="$y1" font-size="$size" fill="$color">$escaped</text>|;
163  
164 1 return $self;
165 }
166  
167 # from Mojo::Util
168 1 my %XML = (
169 '&' => '&amp;',
170 '<' => '&lt;',
171 '>' => '&gt;',
172 '"' => '&quot;',
173 '\'' => '&#39;'
174 );
175  
176 ▶sub _xml_escape ($self, $str) {
Subroutine _xml_escape: called CC=1 SCAR=0.0
177 1 $str =~ s/([&<>"'])/$XML{$1}/ge;
178 1 return $str;
179 }
180  
181 1 1;
182  
183 =encoding utf8
184  
185 =head1 NAME
186  
187 SVG::Barcode - Base class for SVG 1D and 2D codes
188  
189 =head1 SYNOPSIS
190  
191 use SVG::Barcode::Subclass;
192  
193 my $plotter = SVG::Barcode::Subclass->new;
194  
195 $plotter->foreground; # black
196 $plotter->background; # white
197 $plotter->margin; # 2
198 $plotter->id;
199 $plotter->class;
200 $plotter->width;
201 $plotter->height;
202  
203 %params = (
204 foreground => 'red',
205 id => 'barcode',
206 );
207 $plotter = SVG::Barcode::Subclass->new(%params);
208  
209 my $svg = $plotter->plot($text);
210  
211 =head1 DESCRIPTION
212  
213 L<SVG::Barcode> is a base class for SVG 1D and 2D codes.
214  
215 You will not use it directly, it will be loaded by its subclasses:
216  
217 =over
218  
219 =item * L<SVG::Barcode::Code128>
220  
221 =item * L<SVG::Barcode::DataMatrix>
222  
223 =item * L<SVG::Barcode::QRCode>
224  
225 =back
226  
227 =head1 CONSTRUCTOR
228  
229 =head2 new
230  
231 $plotter = SVG::Barcode::Subclass->new; # create with defaults
232 $plotter = SVG::Barcode::Subclass->new(%params);
233  
234 =head1 METHODS
235  
236 =head2 plot
237  
238 $svg = $plotter->plot($text);
239  
240 Creates a barcode.
241  
242 =head1 PARAMETERS
243  
244 =head2 background
245  
246 $value = $plotter->background;
247 $plotter = $plotter->background($newvalue);
248 $plotter = $plotter->background(''); # white
249  
250 Getter and setter for the background color. Default C<white>.
251  
252 =head2 class
253  
254 $value = $plotter->class;
255 $plotter = $plotter->class($newvalue);
256 $plotter = $plotter->class(''); # ''
257  
258 Getter and setter for the class of the svg element. Default C<''>.
259  
260 =head2 foreground
261  
262 $value = $plotter->foreground;
263 $plotter = $plotter->foreground($newvalue);
264 $plotter = $plotter->foreground(''); # black
265  
266 Getter and setter for the foreground color. Default C<black>.
267  
268 =head2 height
269  
270 $value = $plotter->height;
271 $plotter = $plotter->height($newvalue);
272 $plotter = $plotter->height(''); # ''
273  
274 Getter and setter for the height of the svg element. Default C<''>.
275  
276 =head2 id
277  
278 $value = $plotter->id;
279 $plotter = $plotter->id($newvalue);
280 $plotter = $plotter->id(''); # ''
281  
282 Getter and setter for the id of the svg element. Default C<''>.
283  
284 =head2 margin
285  
286 $value = $plotter->margin;
287 $plotter = $plotter->margin($newvalue);
288 $plotter = $plotter->margin(''); # 2
289  
290 Getter and setter for the margin around the barcode. Default C<2>.
291  
292 =head2 width
293  
294 $value = $plotter->width;
295 $plotter = $plotter->width($newvalue);
296 $plotter = $plotter->width(''); # ''
297  
298 Getter and setter for the width of the svg element. Default C<''>.
299  
300 =head1 SEE ALSO
301  
302 L<SVG::Barcode::Code128>, L<SVG::Barcode::DataMatrix>, L<SVG::Barcode::QRCode>.
303  
304 =head1 AUTHOR & COPYRIGHT
305  
306 © 2019 by Tekki (Rolf Stöckli).
307  
308 This program is free software, you can redistribute it and/or modify it under the terms of the Artistic License version 2.0.
309  
310 =cut
↑ Summary