blib/lib/SVG/Barcode.pm

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