| 1 | package SVG::Barcode::UPCE; | ||||||||||||||||||||||||||||
| 2 | 2 | $SVG::Barcode::UPCE::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_upce|; | |||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 2 | ▶ | use GD::Barcode::UPCE; | ||||||||||||||||||||||||||
|
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, # UPCE 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_upce ($text, %params) { | ||||||||||||||||||||||||||
|
Subroutine
plot_upce: 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 | 110 | ▶ | my $add_line = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 40 | 110 | ▶ | if (@line) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 41 | 85 | $self->_rect(@line); | |||||||||||||||||||||||||||
| 42 | 85 | @line = (); | |||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||
| 44 | 5 | }; | |||||||||||||||||||||||||||
| 45 | 5 | for my $x (0 .. $#$code) { | |||||||||||||||||||||||||||
| 46 | 255 | ▶ | if ($code->[$x] eq $sign) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 47 | 125 | ▶ | if (@line) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 48 | 65 | $line[2] += $width; | |||||||||||||||||||||||||||
| 49 | } else { | ||||||||||||||||||||||||||||
| 50 | 60 | @line = ($x * $width, 0, $width, $height); | |||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||
| 52 | } elsif ($code->[$x] eq $signlong) { | ||||||||||||||||||||||||||||
| 53 | # Make a slightly taller line | ||||||||||||||||||||||||||||
| 54 | 25 | ▶ | if (@line) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 55 | 0 | $line[2] += $width; | |||||||||||||||||||||||||||
| 56 | } else { | ||||||||||||||||||||||||||||
| 57 | 25 | @line = ($x * $width, 0, $width, $height * 1.1); | |||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||
| 59 | } else { | ||||||||||||||||||||||||||||
| 60 | 105 | $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::UPCE->new($text) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$self->{"plotter"} ||= "GD::Barcode::UPCE"->new($text)
MC/DC0%0/2
$self->{"plotter"} ||= "GD::Barcode::UPCE"->new($text)
$self->{"plotter"}
"GD::Barcode::UPCE"->new($text)
Truth table33%1/3
A ||= B
A= $self->{"plotter"}B= "GD::Barcode::UPCE"->new($text)
| |||||||||||||||||||||||||||||
| 68 | or die "Cannot create GD::Barcode::UPCE plotter: " . $GD::Barcode::UPCE::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 | 85 | ▶ | sub _rect ($self, $x, $y, $width, $height, $color = $self->{foreground}) { | ||||||||||||||||||||||||||
|
Subroutine
_rect: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 77 | 85 | my $x1 = $x + $self->{margin} + $self->{quietzone}; | |||||||||||||||||||||||||||
| 78 | 85 | my $y1 = $y + $self->{margin}; | |||||||||||||||||||||||||||
| 79 | 85 | $self->{vbwidth} = fmax($self->{vbwidth}, $x1 + $width + $self->{quietzone}); | |||||||||||||||||||||||||||
| 80 | 85 | $self->{vbheight} = fmax($self->{vbheight}, $y1 + $height); | |||||||||||||||||||||||||||
| 81 | push $self->{elements}->@*, | ||||||||||||||||||||||||||||
| 82 | 85 | qq| <rect x="$x1" y="$y1" width="$width" height="$height" fill="$color"/>|; | |||||||||||||||||||||||||||
| 83 | 85 | 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
| |||||||||||||||||||||||||||||
| 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, $middle_digits, $check_digit) = | |||||||||||||||||||||||||||
| 103 | $escaped =~ m/^(\d)(\d{6})(\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 | # Middle 6 digits | ||||||||||||||||||||||||||||
| 113 | 3 | my $x_left = $margin + $qz + (40 * $width) - (length($middle_digits) * $size / 2); | |||||||||||||||||||||||||||
| 114 | 3 | push $self->{elements}->@*, qq| <text x="$x_left" y="$y1" font-size="$size" fill="$color">$middle_digits</text>|; | |||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | # Check digit (last digit) | ||||||||||||||||||||||||||||
| 117 | 3 | my $x_check = $margin + $qz + 55; # Approximately just inside the margin | |||||||||||||||||||||||||||
| 118 | 3 | push $self->{elements}->@*, qq| <text x="$x_check" y="$y1" font-size="$size" fill="$color">$check_digit</text>|; | |||||||||||||||||||||||||||
| 119 | 3 | return $self; | |||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | 2 | 1; | |||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | __END__ | ||||||||||||||||||||||||||||