File Coverage

blib/lib/QRCode/Encoder/QRSpec.pm
Criterion Covered Total %
statement 124 144 86.1
branch 13 30 43.3
condition 4 6 66.6
subroutine 19 22 86.3
pod 13 13 100.0
total 173 215 80.4


line stmt bran cond sub pod time code
1             package QRCode::Encoder::QRSpec;
2 4     4   49 use v5.24;
  4         16  
3 4     4   20 use warnings;
  4         9  
  4         207  
4 4     4   22 use experimental qw< signatures >;
  4         6  
  4         29  
5              
6 4     4   591 use Exporter qw< import >;
  4         9  
  4         15232  
7             our @EXPORT_OK = qw<
8             qrspec_ecc_spec
9             qrspec_data_size
10             qrspec_ecc_size
11             qrspec_width
12             qrspec_remainder
13             qrspec_min_version
14             qrspec_min_version_for
15             qrspec_length_indicator
16             qrspec_maximum_words
17             qrspec_mode_indicator
18             qrspec_alignment_patterns
19             qrspec_format_pattern
20             qrspec_version_pattern
21             qrspec_alignment_patterns
22             >;
23             our %EXPORT_TAGS = (all => [@EXPORT_OK]);
24              
25             # Liberally taken from libqrencode/qrspec.c, which is distributed with
26             # LGPL license
27              
28 4     4 1 7 sub qrspec_ecc_spec ($version, $level) {
  4         10  
  4         8  
  4         8  
29             state $ecc_table = [
30 4         430 map { _zip_hash([qw< L M Q H >], $_) } (
  120         320  
31             # L M Q H
32             [[ 1, 0], [ 1, 0], [ 1, 0], [ 1, 0]],
33             [[ 1, 0], [ 1, 0], [ 1, 0], [ 1, 0]],
34             [[ 1, 0], [ 1, 0], [ 2, 0], [ 2, 0]],
35             [[ 1, 0], [ 2, 0], [ 2, 0], [ 4, 0]],
36             [[ 1, 0], [ 2, 0], [ 2, 2], [ 2, 2]],
37             [[ 2, 0], [ 4, 0], [ 4, 0], [ 4, 0]],
38             [[ 2, 0], [ 4, 0], [ 2, 4], [ 4, 1]],
39             [[ 2, 0], [ 2, 2], [ 4, 2], [ 4, 2]],
40             [[ 2, 0], [ 3, 2], [ 4, 4], [ 4, 4]],
41             [[ 2, 2], [ 4, 1], [ 6, 2], [ 6, 2]],
42             [[ 4, 0], [ 1, 4], [ 4, 4], [ 3, 8]],
43             [[ 2, 2], [ 6, 2], [ 4, 6], [ 7, 4]],
44             [[ 4, 0], [ 8, 1], [ 8, 4], [12, 4]],
45             [[ 3, 1], [ 4, 5], [11, 5], [11, 5]],
46             [[ 5, 1], [ 5, 5], [ 5, 7], [11, 7]],
47             [[ 5, 1], [ 7, 3], [15, 2], [ 3, 13]],
48             [[ 1, 5], [10, 1], [ 1, 15], [ 2, 17]],
49             [[ 5, 1], [ 9, 4], [17, 1], [ 2, 19]],
50             [[ 3, 4], [ 3, 11], [17, 4], [ 9, 16]],
51             [[ 3, 5], [ 3, 13], [15, 5], [15, 10]],
52             [[ 4, 4], [17, 0], [17, 6], [19, 6]],
53             [[ 2, 7], [17, 0], [ 7, 16], [34, 0]],
54             [[ 4, 5], [ 4, 14], [11, 14], [16, 14]],
55             [[ 6, 4], [ 6, 14], [11, 16], [30, 2]],
56             [[ 8, 4], [ 8, 13], [ 7, 22], [22, 13]],
57             [[10, 2], [19, 4], [28, 6], [33, 4]],
58             [[ 8, 4], [22, 3], [ 8, 26], [12, 28]],
59             [[ 3, 10], [ 3, 23], [ 4, 31], [11, 31]],
60             [[ 7, 7], [21, 7], [ 1, 37], [19, 26]],
61             [[ 5, 10], [19, 10], [15, 25], [23, 25]],
62             [[13, 3], [ 2, 29], [42, 1], [23, 28]],
63             [[17, 0], [10, 23], [10, 35], [19, 35]],
64             [[17, 1], [14, 21], [29, 19], [11, 46]],
65             [[13, 6], [14, 23], [44, 7], [59, 1]],
66             [[12, 7], [12, 26], [39, 14], [22, 41]],
67             [[ 6, 14], [ 6, 34], [46, 10], [ 2, 64]],
68             [[17, 4], [29, 14], [49, 10], [24, 46]],
69             [[ 4, 18], [13, 32], [48, 14], [42, 32]],
70             [[20, 4], [40, 7], [43, 22], [10, 67]],
71             [[19, 6], [18, 31], [34, 34], [20, 61]],
72             )
73             ];
74              
75 4         61 my ($b1, $b2) = $ecc_table->[$version - 1]{$level}->@*;
76 4         15 my $data_size = qrspec_data_size($version, $level);
77 4         15 my $ecc_size = qrspec_ecc_size($version, $level);
78 4         41 my @retval;
79              
80 4         41 push @retval, {
81             count => $b1,
82             data => int($data_size / ($b1 + $b2)),
83             ecc => int($ecc_size / ($b1 + $b2)),
84             };
85              
86             push @retval, {
87             count => $b2,
88             data => ($retval[0]{data} + 1),
89             ecc => $retval[0]{ecc},
90 4 100       19 } if $b2;
91              
92 4         21 return @retval;
93             }
94              
95 2     2 1 4 sub qrspec_width ($version) { 17 + $version * 4 }
  2         5  
  2         4  
  2         6  
96              
97             {
98             state $table = [
99             map { $_->{ec} = _zip_hash([qw< L M Q H >], $_->{ec}); $_ } (
100             { words => 26, remainder => 0, ec => [ 7, 10, 13, 17]},
101             { words => 44, remainder => 7, ec => [ 10, 16, 22, 28]},
102             { words => 70, remainder => 7, ec => [ 15, 26, 36, 44]},
103             { words => 100, remainder => 7, ec => [ 20, 36, 52, 64]},
104             { words => 134, remainder => 7, ec => [ 26, 48, 72, 88]},
105             { words => 172, remainder => 7, ec => [ 36, 64, 96, 112]},
106             { words => 196, remainder => 0, ec => [ 40, 72, 108, 130]},
107             { words => 242, remainder => 0, ec => [ 48, 88, 132, 156]},
108             { words => 292, remainder => 0, ec => [ 60, 110, 160, 192]},
109             { words => 346, remainder => 0, ec => [ 72, 130, 192, 224]},
110             { words => 404, remainder => 0, ec => [ 80, 150, 224, 264]},
111             { words => 466, remainder => 0, ec => [ 96, 176, 260, 308]},
112             { words => 532, remainder => 0, ec => [ 104, 198, 288, 352]},
113             { words => 581, remainder => 3, ec => [ 120, 216, 320, 384]},
114             { words => 655, remainder => 3, ec => [ 132, 240, 360, 432]},
115             { words => 733, remainder => 3, ec => [ 144, 280, 408, 480]},
116             { words => 815, remainder => 3, ec => [ 168, 308, 448, 532]},
117             { words => 901, remainder => 3, ec => [ 180, 338, 504, 588]},
118             { words => 991, remainder => 3, ec => [ 196, 364, 546, 650]},
119             { words =>1085, remainder => 3, ec => [ 224, 416, 600, 700]},
120             { words =>1156, remainder => 4, ec => [ 224, 442, 644, 750]},
121             { words =>1258, remainder => 4, ec => [ 252, 476, 690, 816]},
122             { words =>1364, remainder => 4, ec => [ 270, 504, 750, 900]},
123             { words =>1474, remainder => 4, ec => [ 300, 560, 810, 960]},
124             { words =>1588, remainder => 4, ec => [ 312, 588, 870, 1050]},
125             { words =>1706, remainder => 4, ec => [ 336, 644, 952, 1110]},
126             { words =>1828, remainder => 4, ec => [ 360, 700, 1020, 1200]},
127             { words =>1921, remainder => 3, ec => [ 390, 728, 1050, 1260]},
128             { words =>2051, remainder => 3, ec => [ 420, 784, 1140, 1350]},
129             { words =>2185, remainder => 3, ec => [ 450, 812, 1200, 1440]},
130             { words =>2323, remainder => 3, ec => [ 480, 868, 1290, 1530]},
131             { words =>2465, remainder => 3, ec => [ 510, 924, 1350, 1620]},
132             { words =>2611, remainder => 3, ec => [ 540, 980, 1440, 1710]},
133             { words =>2761, remainder => 3, ec => [ 570, 1036, 1530, 1800]},
134             { words =>2876, remainder => 0, ec => [ 570, 1064, 1590, 1890]},
135             { words =>3034, remainder => 0, ec => [ 600, 1120, 1680, 1980]},
136             { words =>3196, remainder => 0, ec => [ 630, 1204, 1770, 2100]},
137             { words =>3362, remainder => 0, ec => [ 660, 1260, 1860, 2220]},
138             { words =>3532, remainder => 0, ec => [ 720, 1316, 1950, 2310]},
139             { words =>3706, remainder => 0, ec => [ 750, 1372, 2040, 2430]},
140             )
141             ];
142              
143 8     8 1 17 sub qrspec_data_size ($version, $level) {
  8         14  
  8         32  
  8         15  
144 8         20 my $item = $table->[$version - 1];
145 8         55 return $item->{words} - $item->{ec}{$level};
146             }
147              
148 4     4 1 8 sub qrspec_ecc_size ($version, $level) {
  4         8  
  4         10  
  4         21  
149 4         16 return $table->[$version - 1]{ec}{$level};
150             }
151              
152 4     4 1 8 sub qrspec_remainder ($version) { $table->[$version - 1]{remainder} }
  4         11  
  4         6  
  4         24  
153              
154 13     13 1 23 sub qrspec_min_version ($size, $level) {
  13         22  
  13         24  
  13         20  
155 13         21 state $arefs = {};
156              
157             # first run goes through all items in the table, so this function
158             # is inefficient if called once per process but gets better when it
159             # is used multiple times per process.
160 13   66     48 my $aref = $arefs->{$level} //= do {
161 6         18 [ map { $_->{words} - $_->{ec}{$level} } $table->@* ]
  240         627  
162             };
163              
164             # do not bother looking for a version if none is possible
165 13 50       38 return if $size > $aref->[-1];
166              
167             # binary search over $aref
168 13         33 my ($lo, $hi) = (0, $aref->$#*);
169 13         56 while ($lo < $hi) {
170 78         149 my $mi = int(($lo + $hi) / 2);
171 78         118 my $misz = $aref->[$mi];
172 78 100       168 if ($misz < $size) { $lo = $mi + 1 } # move ahead
  6 50       17  
173 0         0 elsif ($misz == $size) { $lo = $hi = $mi } # exact match
174 72         159 else { $hi = $mi } # set upper limit
175             }
176 13         48 return $lo + 1;
177             }
178             }
179              
180             {
181             state $table = {
182             numeric => '0001',
183             alphanumeric => '0010',
184             byte => '0100',
185             kanji => '1000',
186             eci => '0111',
187             structured_append => '0011',
188             fnc1_1 => '0101',
189             fnc1_2 => '1001',
190             terminator => '0000',
191             };
192 4     4 1 10 sub qrspec_mode_indicator ($mode) { $table->{$mode} }
  4         9  
  4         8  
  4         41  
193             }
194              
195             {
196             state $table = {
197             numeric => [10, 12, 14],
198             alphanumeric => [ 9, 11, 13],
199             byte => [ 8, 16, 16],
200             kanji => [ 8, 10, 12],
201             };
202              
203 13     13 1 24 sub qrspec_min_version_for ($mode, $size, $level) {
  13         25  
  13         22  
  13         23  
  13         19  
204 12         20 state $size_bits_for = {
205 12     12   61 numeric => sub ($s) { 10 * int($s / 3) + [0, 4, 7]->[$s % 3] },
  12         18  
  12         21  
206 1     1   7 alphanumeric => sub ($s) { 11 * int($s / 2) + 6 * ($s % 2) },
  1         2  
  1         2  
  1         2  
207 0     0   0 byte => sub ($s) { return 8 * $s },
  0         0  
  0         0  
  0         0  
208 0     0   0 kanji => sub ($s) { return 13 * $s },
  0         0  
  0         0  
  0         0  
209 13         70 };
210              
211 13         41 my $min_bits = 4 + $size_bits_for->{$mode}->($size);
212 13         34 my $lengths = $table->{$mode};
213 13         41 for my $i (0 .. $lengths->$#*) {
214 13         29 my $n_bits = $min_bits + $lengths->[$i];
215 13         27 my $rem = $n_bits % 8;
216 13 50       42 my $n_words = (($n_bits - $rem) / 8) + ($rem ? 1 : 0);
217 13         59 my $version = qrspec_min_version($n_words, $level);
218 13 0       35 my $j = $version <= 9 ? 0 : $version <= 26 ? 1 : 2;
    50          
219 13 50       106 return $version if $i == $j;
220             }
221 0         0 return;
222             }
223              
224 4     4 1 10 sub qrspec_length_indicator ($mode, $version) {
  4         9  
  4         8  
  4         7  
225 4 0       16 my $l = $version <= 9 ? 0 : $version <= 26 ? 1 : 2;
    50          
226 4         14 return $table->{$mode}[$l];
227             }
228              
229 0     0 1 0 sub qrspec_maximum_words ($mode, $version) {
  0         0  
  0         0  
  0         0  
230 0 0       0 my $l = $version <= 9 ? 0 : $version <= 26 ? 1 : 2;
    0          
231 0         0 my $bits = $table->{$mode}[$l];
232 0         0 my $words = (1 << $bits) - 1;
233 0 0       0 $words *= 2 if $mode eq 'kanji';
234 0         0 return $words;
235             }
236             }
237              
238 32     32 1 55 sub qrspec_format_pattern ($level, $mask_id) {
  32         74  
  32         57  
  32         56  
239 32         109 state $formats_for = {
240             L => [0x77c4, 0x72f3, 0x7daa, 0x789d, 0x662f, 0x6318, 0x6c41, 0x6976],
241             M => [0x5412, 0x5125, 0x5e7c, 0x5b4b, 0x45f9, 0x40ce, 0x4f97, 0x4aa0],
242             Q => [0x355f, 0x3068, 0x3f31, 0x3a06, 0x24b4, 0x2183, 0x2eda, 0x2bed],
243             H => [0x1689, 0x13be, 0x1ce7, 0x19d0, 0x0762, 0x0255, 0x0d0c, 0x083b],
244             };
245 32         146 return $formats_for->{$level}[$mask_id];
246             }
247              
248 4     4 1 8 sub qrspec_version_pattern ($version) {
  4         7  
  4         8  
249 4         21 state $version_pattern_for = [
250             0x07c94, 0x085bc, 0x09a99, 0x0a4d3, # 7-10
251             0x0bbf6, 0x0c762, 0x0d847, 0x0e60d, 0x0f928, # 11-15
252             0x10b78, 0x1145d, 0x12a17, 0x13532, 0x149a6, # 16-20
253             0x15683, 0x168c9, 0x177ec, 0x18ec4, 0x191e1, # 21-25
254             0x1afab, 0x1b08e, 0x1cc1a, 0x1d33f, 0x1ed75, # 26-30
255             0x1f250, 0x209d5, 0x216f0, 0x228ba, 0x2379f, # 31-35
256             0x24b0b, 0x2542e, 0x26a64, 0x27541, 0x28c69, # 36-40
257             ];
258 4 100       31 return $version <= 6 ? undef : $version_pattern_for->[$version - 7];
259             }
260              
261 4     4 1 55 sub qrspec_alignment_patterns ($version) {
  4         13  
  4         9  
262 4         149 state $base = [
263             [18 ], [22 ], [26 ], [30 ], # 2- 5
264             [34 ], [22, 38], [24, 42], [26, 46], [28, 50], # 6-10
265             [30, 54], [32, 58], [34, 62], [26, 46], [26, 48], # 11-15
266             [26, 50], [30, 54], [30, 56], [30, 58], [34, 62], # 16-20
267             [28, 50], [26, 50], [30, 54], [28, 54], [32, 58], # 21-25
268             [30, 58], [34, 62], [26, 50], [30, 54], [26, 52], # 26-30
269             [30, 56], [34, 60], [30, 58], [34, 62], [30, 54], # 31-35
270             [24, 50], [28, 54], [32, 58], [26, 54], [30, 58], # 35-40
271             ];
272 4         26 state $cache = { 1 => [] };
273 4   66     30 my $aref = $cache->{$version} //= do {
274 2         9 my @offset = (6, $base->[$version - 2]->@*);
275 2         6 my $width = qrspec_width($version);
276 2         5 while ('necessary') {
277 2         4 my $next = 2 * $offset[-1] - $offset[-2];
278 2 50       18 last if $next + 2 >= $width;
279 0         0 push @offset, $next;
280             }
281 2         8 \@offset;
282             };
283 4         19 return $aref->@*;
284             }
285              
286 280     280   435 sub _zip_hash ($aref1, $aref2) {
  280         410  
  280         407  
  280         433  
287 280         424 my %hash;
288 280         907 @hash{$aref1->@*} = $aref2->@*;
289 280         631 return \%hash;
290             }
291              
292             1;