Airlock
view release on metacpan or search on metacpan
lib/Airlock/QR.pm view on Meta::CPAN
package Airlock::QR;
# ABSTRACT: QR codes as SVG, terminal blocks or data URI, in pure Perl
use Moo;
use Carp qw( croak );
use GD::Barcode::QRcode;
use MIME::Base64 qw( encode_base64 );
use Types::Standard qw( ArrayRef CodeRef Enum Int Str );
use namespace::autoclean;
our $VERSION = '0.001';
has text => (
is => 'ro',
isa => Str,
required => 1
);
has ecc => (
is => 'ro',
isa => Enum[qw( L M Q H )],
default => 'M'
);
has quiet => (
is => 'ro',
isa => Int,
default => 4
);
has encoder => (
is => 'ro',
isa => CodeRef,
predicate => 'has_encoder'
);
has matrix => (
is => 'lazy',
isa => ArrayRef[ArrayRef],
init_arg => undef
);
sub max_bytes { 1000 }
sub _build_matrix {
my ( $self ) = @_;
my $bytes = $self->text;
utf8::encode($bytes) if utf8::is_utf8($bytes);
croak __PACKAGE__.'->matrix needs a text' unless length $bytes;
croak __PACKAGE__.'->matrix text is longer than '.$self->max_bytes.' bytes'
if length $bytes > $self->max_bytes;
my $pattern = $self->has_encoder ? $self->encoder->( $bytes, $self->ecc ) : $self->_encode($bytes);
my @rows = map { [ split // ] } grep { length } split /\n/, $pattern // '';
croak __PACKAGE__.'->matrix encoder returned no square pattern of 0 and 1'
if !@rows || grep { @$_ != @rows || grep { $_ ne '0' && $_ ne '1' } @$_ } @rows;
while ( @rows > 21 && !grep { $_ } @{ $rows[0] }, @{ $rows[-1] }, map { $_->[0], $_->[-1] } @rows ) {
@rows = map { [ @{$_}[ 1 .. $#$_ - 1 ] ] } @rows[ 1 .. $#rows - 1 ];
}
return [ map { [ map { $_ + 0 } @$_ ] } @rows ];
}
sub _encode {
my ( $self, $bytes ) = @_;
my $qr = GD::Barcode::QRcode->new( $bytes, { Ecc => $self->ecc, ModuleSize => 1 } )
or croak __PACKAGE__.'->matrix cannot encode: '.( $GD::Barcode::errStr // 'unknown error' );
return $qr->barcode;
}
sub size { scalar @{ $_[0]->matrix } }
sub svg {
my ( $self ) = @_;
my $quiet = $self->quiet;
my $side = $self->size + 2 * $quiet;
my @path;
my $y = $quiet;
for my $row ( @{ $self->matrix } ) {
my $line = join '', @$row;
push @path, 'M'.( $-[0] + $quiet ).' '.$y.'h'.( $+[0] - $-[0] ).'v1h-'.( $+[0] - $-[0] ).'z' while $line =~ /1+/g;
$y++;
}
return '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 '.$side.' '.$side.'" shape-rendering="crispEdges">'
.'<rect width="'.$side.'" height="'.$side.'" fill="#fff"/>'
.'<path fill="#000" d="'.join( '', @path ).'"/></svg>';
}
sub data_uri {
my ( $self ) = @_;
return 'data:image/svg+xml;base64,'.encode_base64( $self->svg, '' );
}
sub terminal {
my ( $self, %arg ) = @_;
my $ansi = exists $arg{ansi} ? $arg{ansi} : 1;
my $quiet = $self->quiet;
my $side = $self->size + 2 * $quiet;
my @rows = (
( [ (0) x $side ] ) x $quiet,
( map { [ (0) x $quiet, @$_, (0) x $quiet ] } @{ $self->matrix } ),
( [ (0) x $side ] ) x $quiet
);
push @rows, [ (0) x $side ] if @rows % 2;
my @glyph = $ansi ? ( ' ', "\x{2584}", "\x{2580}", "\x{2588}" ) : ( "\x{2588}", "\x{2580}", "\x{2584}", ' ' );
my $out = '';
while ( my ( $top, $bottom ) = splice @rows, 0, 2 ) {
( run in 1.014 second using v1.01-cache-2.11-cpan-234fa220139 )