| 1 | package PDF::QRCode; | ||
| 2 | use Text::QRCode; | ||
| 3 | use Sub::Util qw(set_subname); | ||
| 4 | our $VERSION = '0.1.0'; | ||
| 5 | =pod | ||
| 6 | |||
| 7 | =begin markdown | ||
| 8 | |||
| 9 |  | ||
| 10 | |||
| 11 | =end markdown | ||
| 12 | |||
| 13 | |||
| 14 | =head1 NAME | ||
| 15 | |||
| 16 | PDF::QRCode - add qrcode method to a PDF::API2 or PDF::Builder. | ||
| 17 | |||
| 18 | =head1 SYNOPSIS | ||
| 19 | |||
| 20 | use PDF::API2; # or PDF::Builder | ||
| 21 | use PDF::QRCode; | ||
| 22 | |||
| 23 | my $pdf = PDF::API2->new(-file=>'qr.pdf'); | ||
| 24 | $pdf->mediabox('a4'); | ||
| 25 | my $gfx = $pdf->page->gfx; | ||
| 26 | $gfx->qrcode(x => 100, y => 100, | ||
| 27 | level => 'L', size => 40, text => 'Hello World'); | ||
| 28 | $pdf->save; | ||
| 29 | |||
| 30 | =head1 DESCRIPTION | ||
| 31 | |||
| 32 | The L<PDF::QRCode> module monkey patches the 'qrcode' method into the | ||
| 33 | L<PDF::API2::Content> or L<PDF::Builder::Content> class, so that you can use it directly from there. See the example above | ||
| 34 | |||
| 35 | =head2 $gfx->qrcode(%cfg) | ||
| 36 | |||
| 37 | Adds a qr code to the given gfx content. It expects the following parameters: | ||
| 38 | |||
| 39 | =over | ||
| 40 | |||
| 41 | =item x | ||
| 42 | |||
| 43 | horizontal position | ||
| 44 | |||
| 45 | =item y | ||
| 46 | |||
| 47 | vertical position | ||
| 48 | |||
| 49 | =item size | ||
| 50 | |||
| 51 | width/height | ||
| 52 | |||
| 53 | =item text | ||
| 54 | |||
| 55 | the content of the qrcode | ||
| 56 | |||
| 57 | =item level (optional) | ||
| 58 | |||
| 59 | qr code level C<L>, C<M>, C<Q>, C<H> | ||
| 60 | |||
| 61 | =back | ||
| 62 | |||
| 63 | =cut | ||
| 64 | |||
| 65 | my $qrcode = sub { | ||
| 66 | my $ct = shift; | ||
| 67 | my %cfg = ref $_[0] eq 'HASH' ? %$_[0] : (@_); | ||
| 68 | $ct->save(); | ||
| 69 | $ct->translate($cfg{x},$cfg{y}); | ||
| 70 | my $code = Text::QRCode->new( | ||
| 71 | level => $cfg{level} || 'L', # L M Q H | ||
| 72 | mode => '8-bit', | ||
| 73 | casesensitive => '1' | ||
| 74 | )->plot($cfg{text}); | ||
| 75 | my $width = scalar @$code; | ||
| 76 | my $scale = $cfg{size}/$width; | ||
| 77 | $ct->scale($scale,$scale); | ||
| 78 | my $oy = 0; | ||
| 79 | for my $row (@$code) { | ||
| 80 | my $ox = 0; | ||
| 81 | for my $dot (@$row) { | ||
| 82 | if ($dot eq '*') { | ||
| 83 | $ct->add($ox,$oy,1,-1,'re'); | ||
| 84 | } | ||
| 85 | $ox++; | ||
| 86 | } | ||
| 87 | $oy--; | ||
| 88 | } | ||
| 89 | $ct->fill(); | ||
| 90 | $ct->restore(); | ||
| 91 | }; | ||
| 92 | |||
| 93 | sub _monkey_patch { | ||
| 94 | my ($class, %patch) = @_; | ||
| 95 | no strict 'refs'; | ||
| 96 | no warnings 'redefine'; | ||
| 97 | *{"${class}::$_"} = set_subname("${class}::$_", $patch{$_}) for keys %patch; | ||
| 98 | } | ||
| 99 | |||
| 100 | my $loaded; | ||
| 101 | if ($INC{'PDF/API2/Content.pm'}) { | ||
| 102 | $loaded = 1; | ||
| 103 | _monkey_patch 'PDF::API2::Content', 'qrcode', $qrcode; | ||
| 104 | } | ||
| 105 | |||
| 106 | if ($INC{'PDF/Builder/Content.pm'}) { | ||
| 107 | $loaded = 1; | ||
| 108 | _monkey_patch 'PDF::Builder::Content', 'qrcode', $qrcode; | ||
| 109 | } | ||
| 110 | |||
| 111 | if (not $loaded) { | ||
| 112 | die __PACKAGE__ . " not loaded since neither PDF::API2 nor PDF::Builder is present. Make sure to 'use' one of them before 'using' this package.\n"; | ||
| 113 | } | ||
| 114 | 1; | ||
| 115 | |||
| 116 | =head1 AUTHOR | ||
| 117 | |||
| 118 | S<Tobias Oetiker, E<lt>tobi@oetiker.chE<gt>> | ||
| 119 | |||
| 120 | =head1 COPYRIGHT | ||
| 121 | |||
| 122 | Copyright OETIKER+PARTNER AG 2020 | ||
| 123 | |||
| 124 | =head1 LICENSE | ||
| 125 | |||
| 126 | This library is free software; you can redistribute it and/or modify | ||
| 127 | it under the same terms as Perl itself, either Perl version 5.10 or, | ||
| 128 | at your option, any later version of Perl 5 you may have available. | ||
| 129 | |||
| 130 | =cut |