UPCA.pm

filter: Statementstmt 100% 83 / 83 Branchbran 85.7% 12 / 14 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 100% 17 / 17 Podpod 100% 1 / 1 totaltotal 95.0% 114 / 120 CC 12 SCAR 9.9
CC
12
Cov
96%
_plot_1d
6.0
_plot
3.3
_text
3.0
CRAP 2.7
« UPCE.pm ↑ Summary
1 package SVG::Barcode::UPCA;
2 2 $SVG::Barcode::UPCA::VERSION = '0.9';
3 2 ▶use parent 'SVG::Barcode';
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use v5.24;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use feature 'signatures';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶no warnings 'experimental::signatures';
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 2 ▶use POSIX 'fmax';
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 our @EXPORT_OK = qw|plot_upca|;
14  
15 2 ▶use GD::Barcode::UPCA;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 2 ▶use constant DEFAULTS => {
Subroutine BEGIN: called CC=1 SCAR=0.0
18 lineheight => 50,
19 linewidth => 1,
20 quietzone => 9, # UPCA needs an explicit quiet zone left and right
21 textsize => 10,
22 2 };
23  
24 2 SVG::Barcode::_param(__PACKAGE__, $_, DEFAULTS->{$_}) for keys DEFAULTS->%*;
25  
26 # functions
27  
28 4 ▶sub plot_upca ($text, %params) {
Subroutine plot_upca: called CC=1 SCAR=0.0
Pod documented
29 3 return __PACKAGE__->new(%params)->plot($text);
30 }
31  
32 # internal methods
33  
34 # Add support for taller lines (typically at the sides and middle)
35 5 ▶sub _plot_1d ($self, $code, $sign, $signlong) {
Subroutine _plot_1d: called CC=6 SCAR=17.9
36 5 my @line;
37 5 my $width = $self->{linewidth};
38 5 my $height = $self->{lineheight};
39 260 ▶ my $add_line = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
40 260 ▶ if (@line) {
Branch: 1 branch
BranchTrueFalseTotal
if (@line) 150 110 260
41 150 $self->_rect(@line);
42 150 @line = ();
43 }
44 5 };
45 5 for my $x (0 .. $#$code) {
46 475 ▶ if ($code->[$x] eq $sign) {
Branch: 2 branches
BranchTrueFalseTotal
if ($code->[$x] eq $sign) { } 155 320 475
elsif ($code->[$x] eq $signlong) { } 65 255 320
47 155 ▶ if (@line) {
Branch: 1 branch
BranchTrueFalseTotal
if (@line) { } 55 100 155
48 55 $line[2] += $width;
49 } else {
50 100 @line = ($x * $width, 0, $width, $height);
51 }
52 } elsif ($code->[$x] eq $signlong) {
53 # Make a slightly taller line
54 65 ▶ if (@line) {
Branch: 1 branch
BranchTrueFalseTotal
if (@line) { } 15 50 65
55 15 $line[2] += $width;
56 } else {
57 50 @line = ($x * $width, 0, $width, $height * 1.1);
58 }
59 } else {
60 255 $add_line->();
61 }
62 }
63 5 $add_line->();
64 }
65  
66 5 ▶sub _plot ($self, $text) {
Subroutine _plot: called CC=3 SCAR=12.0
67 5 ▶ $self->{plotter} ||= GD::Barcode::UPCA->new($text)
Branch: 1 branch
BranchTrueFalseTotal
$self->{"plotter"} ||= "GD::Barcode::UPCA"->new($text) or die 'Cannot create GD::Barcode::UPCA plotter: ' . $GD::Barcode::UPCA::errStr 5 0 5
Condition33%1/3
$self->{"plotter"} ||= "GD::Barcode::UPCA"->new($text)
l!l&&r!l&&!r
050
MC/DC0%0/2
$self->{"plotter"} ||= "GD::Barcode::UPCA"->new($text)
$self->{"plotter"} "GD::Barcode::UPCA"->new($text)
Truth table33%1/3
A ||= B
A= $self->{"plotter"}B= "GD::Barcode::UPCA"->new($text)
ABresult
1X1
011
000
68 or die "Cannot create GD::Barcode::UPCA plotter: " . $GD::Barcode::UPCA::errStr;
69  
70 5 my @code = split //, $self->{plotter}->barcode();
71 5 $self->_plot_1d(\@code, '1', 'G');
72 5 $self->_plot_text($self->{plotter}->{text});
73 }
74  
75 # We have to add the quiet zones on the sides
76 150 ▶sub _rect ($self, $x, $y, $width, $height, $color = $self->{foreground}) {
Subroutine _rect: called CC=2 SCAR=6.9
77 150 my $x1 = $x + $self->{margin} + $self->{quietzone};
78 150 my $y1 = $y + $self->{margin};
79 150 $self->{vbwidth} = fmax($self->{vbwidth}, $x1 + $width + $self->{quietzone});
80 150 $self->{vbheight} = fmax($self->{vbheight}, $y1 + $height);
81 push $self->{elements}->@*,
82 150 qq| <rect x="$x1" y="$y1" width="$width" height="$height" fill="$color"/>|;
83 150 return $self;
84 }
85  
86 # Handle aligning the text below the barcode
87 # TODO: find a better way to calculate the positions relative to the bars
88 3 ▶sub _text ($self, $text, $x_offset, $y_offset, $size, $color = $self->{foreground}) {
Subroutine _text: called CC=3 SCAR=11.0
89 3 ▶ return $self if $size == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $size == 0 0 3 3
90  
91 3 my $escaped = $self->_xml_escape($text);
92 3 my $margin = $self->{margin};
93 3 my $qz = $self->{quietzone};
94 3 my $width = $self->{linewidth};
95  
96 # The full barcode string is 95 modules wide.
97 # The text is split into 3 parts:
98 # - 1st digit (system character)
99 # - next 5 digits
100 # - last 5 digits + check digit
101  
102 3 my ($sys_char, $left_digits, $right_digits, $check_digit) =
103 $escaped =~ m/^(\d)(\d{5})(\d{5})(\d)$/;
104  
105 3 my $y1 = $y_offset; # Position below the shortest bars
106 3 $self->{vbheight} = fmax $self->{vbheight}, $y1 + $size; # Ensure height accounts for text
107  
108 # System character (1st digit)
109 3 my $x_sys = $margin; # Just inside the margin
110 3 push $self->{elements}->@*, qq| <text x="$x_sys" y="$y1" font-size="$size" fill="$color">$sys_char</text>|;
111  
112 # Left 5 digits (modules 3-47, center at 25)
113 3 my $x_left = $margin + $qz + (40 * $width) - (length($left_digits) * $size / 2);
114 3 push $self->{elements}->@*, qq| <text x="$x_left" y="$y1" font-size="$size" fill="$color">$left_digits</text>|;
115  
116 # Right 5 digits + check digit (modules 48-92, center at 70)
117 3 my $x_right = $margin + $qz + (80 * $width) - (length($right_digits) * $size / 2);
118 3 push $self->{elements}->@*, qq| <text x="$x_right" y="$y1" font-size="$size" fill="$color">$right_digits</text>|;
119  
120 # Check digit (last digit)
121 3 my $x_check = $margin + $qz *2 + 90; # Approximately just inside the margin
122 3 push $self->{elements}->@*, qq| <text x="$x_check" y="$y1" font-size="$size" fill="$color">$check_digit</text>|;
123 3 return $self;
124 }
125  
126 2 1;
127  
128 __END__
« UPCE.pm ↑ Summary