blib/lib/SVG/Barcode/QRCode.pm

filter: Statementstmt 92.3% 24 / 26 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 9 / 9 Podpod n/a totaltotal 94.2% 33 / 35 CC 1 SCAR 0.3
CC
1
Cov
92%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
↑ Summary
1 package SVG::Barcode::QRCode;
2 2 ▶use parent 'SVG::Barcode';
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use v5.24;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use feature 'signatures';
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶no warnings 'experimental::signatures';
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 2 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
11 our @EXPORT_OK = qw|plot_qrcode|;
12  
13 2 ▶use Text::QRCode;
Subroutine BEGIN: called CC=1 SCAR=2.6
14  
15 our $VERSION = '0.01';
16  
17 use constant DEFAULTS => {
18 background => 'white',
19 foreground => 'black',
20 level => 'M',
21 margin => 10,
22 size => 5,
23 version => 0,
24 };
25  
26 # functions
27  
28 sub plot_qrcode ($text, $params = {}) {
29 return __PACKAGE__->new($params)->plot($text);
30 }
31  
32 # internal methods
33  
34 sub _plot ($self, $text) {
35 $self->{plotter} ||= Text::QRCode->new($self->%{qw|level version|});
36  
37 my @qrcode = $self->{plotter}->plot($text)->@*;
38 my $dimension = @qrcode;
39  
40 my @dot;
41 my $size = $self->{size};
42 my $add_dot = sub {
43 if (@dot) {
44 $self->_rect(@dot);
45 @dot = ();
46 }
47 };
48  
49 for my $y (0 .. $dimension - 1) {
50 for my $x (0 .. $dimension - 1) {
51 if ($qrcode[$y][$x] eq '*') {
52 if (@dot) {
53 $dot[2] += $size;
54 } else {
55 @dot = ($x * $size, $y * $size, $size, $size);
56 }
57 } else {
58 $add_dot->();
59 }
60 }
61 $add_dot->();
62 }
63 }
64  
65 1;
66  
67 =encoding utf8
68  
69 =head1 NAME
70  
71 SVG::Barcode::QRCode - Generator for SVG based QR Codes
72  
73 =head1 SYNOPSIS
74  
75 use SVG::Barcode::QRCode;
76  
77 my %params = (
78 background => 'white',
79 foreground => 'black',
80 level => 'M',
81 margin => 10,
82 size => 5,
83 version => 0,
84 );
85 my $qrcode = SVG::Barcode::QRCode->new(\%params);
86 my $svg = $qrcode->plot('https://perldoc.pl');
87 my $svg2 = $qrcode->param(foreground => 'red')->plot('https://perldoc.pl');
88  
89 # use as function
90 use SVG::Barcode::QRCode 'plot_qrcode';
91  
92 my $svg = plot_qrcode('https://perldoc.pl', \%params);
93  
94 =head1 DESCRIPTION
95  
96 L<SVG::Barcode::QRCode> is a Generator for SVG based QR Codes.
97  
98 =head1 FUNCTIONS
99  
100 =head2 plot_qrcode
101  
102 use SVG::Barcode::QRCode 'plot_qrcode';
103  
104 my $svg = plot_qrcode($text, \%params);
105  
106 Returns a QR Code using the provided text and parameters.
107  
108 =head1 CONSTRUCTOR
109  
110 =head2 new
111  
112 $qrcode = SVG::Barcode::QRCode->new(\%params);
113 $qrcode = SVG::Barcode::QRCode->new; # create with defaults
114  
115 Creates a new QR Code plotter. Inherited from L<SVG::Barcode/new>.
116  
117 Accepted parameters are:
118  
119 =over 4
120  
121 =item background
122  
123 Color of the background. Default C<'white'>.
124  
125 =item foreground
126  
127 Color of the dots. Default C<'black'>.
128  
129 =item level
130  
131 Error correction level, one of C<'L'> (low), C<'M'> (medium), C<'Q'> (quartile), C<'H'> (high). Default C<'M'>.
132  
133 =item margin
134  
135 Margin around the code. Default C<10>.
136  
137 =item size
138  
139 Size of the dots. Default C<5>.
140  
141 =item version
142  
143 Symbol version from C<1> to C<40>. C<0> will adapt the version to the required capacity. Default C<0>.
144  
145 =back
146  
147 =head1 METHODS
148  
149 =head2 param
150  
151 Getter and setter for the parameters. Inherited from L<SVG::Barcode/param>.
152  
153 =head2 plot
154  
155 Creates a SVG code. Inherited from L<SVG::Barcode/plot>.
156  
157 =head1 SEE ALSO
158  
159 L<SVG::Barcode>, L<Text::QRCode>.
160  
161 =head1 AUTHOR & COPYRIGHT
162  
163 © 2019 by Tekki (Rolf Stöckli).
164  
165 This program is free software, you can redistribute it and/or modify it under the terms of the Artistic License version 2.0.
166  
167 =cut
↑ Summary