summaryrefslogtreecommitdiff
path: root/graphics/pstricks/contrib/pst-barcode/doc/images/swissqrcode-1.eps
blob: c5055dac79414d6ddedf133c5e66418b0e9f1fa1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
%!PS-Adobe-3.0 EPSF-3.0
%%BoundingBox: 0 0 262 262
%%Creator: https://www.terryburton.co.uk/barcodewriter/generator/
%%Copyright: Barcode Writer in Pure PostScript
%%Pages: 1
%%LanguageLevel: 2
%%DocumentData: Clean7Bit
%%EndComments
%%BeginProlog
%%BeginResource: Category uk.co.terryburton.bwipp 0.0 2020040100 29629 32838
%%BeginData:          6 ASCII Lines
currentglobal
true setglobal
/Generic /Category findresource dup length 1 add dict copy dup
/InstanceType /setpacking where {pop /packedarraytype} {/arraytype} ifelse put
/uk.co.terryburton.bwipp exch /Category defineresource pop
setglobal
%%EndData
%%EndResource

%%BeginResource: uk.co.terryburton.bwipp raiseerror 0.0 2020040100 39041 38872
%%BeginData:         15 ASCII Lines
/setpacking where {pop currentpacking true setpacking} if
1 dict
begin
/raiseerror {
  $error exch /errorinfo exch put
  $error exch /errorname exch put
  $error /command null put
  $error /newerror true put
  stop
}
[/barcode] {null def} forall
bind def
/raiseerror dup load /uk.co.terryburton.bwipp defineresource pop
end
/setpacking where {pop setpacking} if
%%EndData
%%EndResource

%%BeginResource: uk.co.terryburton.bwipp renmatrix 0.0 2020040100 90671 89701
%%BeginData:        306 ASCII Lines
/setpacking where {pop currentpacking true setpacking} if
1 dict
dup /raiseerror dup /uk.co.terryburton.bwipp findresource put
begin
/renmatrix {

    20 dict begin

    /args exch def

    % Default options
    /width 1 def
    /height 1 def
    /barcolor (unset) def
    /backgroundcolor (unset) def
    /colormap (unset) def
    /dotty false def
    /inkspread 0 def
    /inkspreadh 0 def
    /inkspreadv 0 def
    /includetext false def
    /txt [] def
    /textcolor (unset) def
    /textxalign (unset) def
    /textyalign (unset) def
    /textfont (Courier) def
    /textsize 10 def
    /textxoffset 0 def
    /textyoffset 0 def
    /textgaps 0 def
    /alttext () def

    % Apply the renderer options and the user options
    args {def} forall
    opt {def} forall

    /width width cvr def
    /height height cvr def
    /barcolor barcolor cvlit def
    /backgroundcolor backgroundcolor cvlit def
    /inkspread inkspread cvr def
    /inkspreadh inkspreadh cvr def
    /inkspreadv inkspreadv cvr def
    /textcolor textcolor cvlit def
    /textxalign textxalign cvlit def
    /textyalign textyalign cvlit def
    /textfont textfont cvlit def
    /textsize textsize cvr def
    /textxoffset textxoffset cvr def
    /textyoffset textyoffset cvr def
    /textgaps textgaps cvr def
    /alttext alttext cvlit def

    inkspread 0 ne {/inkspreadh inkspread def} if
    inkspread 0 ne {/inkspreadv inkspread def} if

    /xyget { pixx mul add pixs exch get} bind def
    /cget { pixx mul add cache exch get and} bind def
    /cput {  % Optimise by only storing "stem" corners
        dup 4 mod 0 eq { % dir is up or left
            3 1 roll pixx mul add cache exch 2 copy get 4 -1 roll or put
        } {
            pop pop pop
        } ifelse
    } bind def

    /abcd {
        4 string 0 [
            5 -2 roll
            pixx mul add dup
            pixs exch 2 getinterval aload pop 3 -1 roll  % A B
            pixs exch pixx add 2 getinterval aload pop   % C D
        ] {3 copy 48 add put pop 1 add} forall pop
    } bind def

    /right {dir 1 ne {x y dir cput [x y]} if  /x x 1 add def  /dir 1 def} bind def
    /down  {dir 2 ne {x y dir cput [x y]} if  /y y 1 add def  /dir 2 def} bind def
    /left  {dir 4 ne {x y dir cput [x y]} if  /x x 1 sub def  /dir 4 def} bind def
    /up    {dir 8 ne {x y dir cput [x y]} if  /y y 1 sub def  /dir 8 def} bind def

    /trace {
        % Walk the outline of a region emitting edge coordinates of the path
        /y exch def  /x exch def
        % dir 1:right 2:down 4:left 8:up
        /dir x 1 add y 1 add xyget 1 eq {8} {4} ifelse def
        /sx x def  /sy y def  /sdir dir def

        % A B
        % C D
        mark {
            x y abcd
            1 {  % Common exit
                dup dup (0001) eq exch dup (0011) eq exch (1011) eq or or {pop right exit} if
                dup dup (0010) eq exch dup (1010) eq exch (1110) eq or or {pop down  exit} if
                dup dup (1000) eq exch dup (1100) eq exch (1101) eq or or {pop left  exit} if
                dup dup (0100) eq exch dup (0101) eq exch (0111) eq or or {pop up    exit} if
                dup (1001) eq {
                    dir 2 eq {pop left exit} {pop right exit} ifelse
                } {  % 0110
                    dir 1 eq {pop down exit} {pop up exit} ifelse
                } ifelse
            } repeat
            x sx eq y sy eq and dir sdir eq and {exit} if
        } loop
        counttomark array astore exch pop

    } bind def

    % Set RGB or CMYK color depending on length of given hex string
    /setanycolor {
        /anycolor exch def
        anycolor length 6 eq {
            (<      >) 8 string copy dup 1 anycolor putinterval cvx exec {255 div} forall setrgbcolor
        } if
        anycolor length 8 eq {
            (<        >) 10 string copy dup 1 anycolor putinterval cvx exec {255 div} forall setcmykcolor
        } if
    } bind def

    % Plot a layer of single colour
    /drawlayer {

        /pixsorig pixs def
        /pixs exch def

        % Pad the bitmap on all sides
        /pixs [
            pixx 2 add {0} repeat
            0 pixx pixs length 1 sub {
                0 exch
                pixs exch pixx getinterval aload pop
                0
            } for
            pixx 2 add {0} repeat
        ] def
        /pixx pixx 2 add def
        /pixy pixy 2 add def

        % Cache of visited corners for each direction
        /cache [ pixs length {0} repeat ] def

        % Construct paths by tracing regions avoiding duplication by using the cache
        /paths [
            0 1 pixy 2 sub {
                /j exch def
                0 1 pixx 2 sub {
                    /i exch def
                    /k i j abcd def
                    k (0001) eq k (1001) eq or {  % Black region stem corners
                        8 i j cget 0 eq {
                            i j trace
                        } if
                    } if
                    k (1110) eq {  % White region stem corner
                        4 i j cget 0 eq {
                            i j trace
                        } if
                    } if
                } for
            } for
        ] def

        % Revert the bitmap size
        /pixx pixx 2 sub def
        /pixy pixy 2 sub def

        % Draw the image
        newpath
        paths {
            /p exch def
            /len p length def
            p len 1 sub get aload pop
            p 0 get aload pop
            0 1 len 1 sub {                                % x1 y1 x2 y2
                /i exch def
                p i 1 add len mod get aload pop 6 -2 roll  % x3 y3 x1 y1 x2 y2
                5 index inkspreadh
                4 index 4 -1 roll lt {add} {sub} ifelse    % y3<y1 ? x2+i : x2-i
                4 1 roll
                4 index inkspreadv
                4 index 4 -1 roll gt {add} {sub} ifelse    % x3>x1 ? y2+i : y2-i
                4 -1 roll exch pixy exch sub
                i 0 eq {moveto} {lineto} ifelse
            } for                                          % x2 y2 x3 y3
            closepath
            pop pop pop pop
        } forall
        fill

        /pixs pixsorig def

    } bind def

    /drawlayerdots {

        /pixsorig pixs def
        /pixs exch def

        newpath
        0 1 pixs length 1 sub {
            dup pixx mod /x exch def
            pixx idiv /y exch def
            x y xyget 1 eq {
               x 0.5 add pixy y sub 0.5 sub moveto
               x 0.5 add pixy y sub 0.5 sub 0.5 inkspread sub 0 360 arc
            } if
        } for
        fill

        /pixs pixsorig def

    } bind def

    gsave

    % Draw the image
    /inkspread  inkspread  2 div def
    /inkspreadh inkspreadh 2 div def
    /inkspreadv inkspreadv 2 div def
    currentpoint translate
    width pixx div 72 mul height pixy div 72 mul scale
    0 0 moveto pixx 0 lineto pixx pixy lineto 0 pixy lineto closepath
    backgroundcolor (unset) ne { gsave backgroundcolor setanycolor fill grestore } if

    colormap (unset) eq {
        /colormap << 1 barcolor >> def
    } if

    colormap {
        setanycolor
        /key exch def
        [
            pixs { key eq {1} {0} ifelse } forall
        ] dotty {drawlayerdots} {drawlayer} ifelse
    } forall

    % Display the text for elements in the text array
    textcolor (unset) ne { textcolor setanycolor } if
    includetext {
        textxalign (unset) eq textyalign (unset) eq and alttext () eq and {
            /s 0 def /fn () def
            txt {
                {} forall
                2 copy s ne exch fn ne or {
                    2 copy /s exch def /fn exch def
                    selectfont
                } {
                    pop pop
                } ifelse
                moveto show
            } forall
        } {
            textfont textsize selectfont
            alttext () eq {
                /txt [ txt { 0 get {} forall } forall ] def
                /tstr txt length string def
                0 1 txt length 1 sub { dup txt exch get tstr 3 1 roll put } for
            } {
                /tstr alttext def
            } ifelse

            % Find true ascent of font
            tstr length 0 eq {
                0
            } {
                gsave
                newpath 0 0 moveto (0) false charpath pathbbox
                4 1 roll pop pop pop
                grestore
                currentfont /PaintType known {currentfont /PaintType get 2 eq} {false} ifelse
                currentfont /StrokeWidth known and {
                    currentfont /StrokeWidth get 2 div 0 exch
                    currentfont /FontMatrix get dtransform
                    dup mul exch dup mul add sqrt
                    add
                } if
            } ifelse
            /textascent exch def
            /textwidth tstr stringwidth pop tstr length 1 sub textgaps mul add def

            /textxpos textxoffset x textwidth sub 2 div add def
            textxalign (left) eq { /textxpos textxoffset def } if
            textxalign (right) eq { /textxpos x textxoffset sub textwidth sub def } if
            textxalign (offleft) eq { /textxpos textwidth textxoffset add neg def } if
            textxalign (offright) eq { /textxpos x textxoffset add def } if
            textxalign (justify) eq textwidth x lt and {
                /textxpos 0 def
                /textgaps x textwidth sub tstr length 1 sub div def
            } if
            /textypos textyoffset textascent add 1 add neg def
            textyalign (above) eq { /textypos textyoffset pixy add 1 add def } if
            textyalign (center) eq { /textypos textyoffset pixy textascent sub 2 div add def } if
            textxpos textypos moveto textgaps 0 tstr ashow
        } ifelse
    } if

    grestore

    end

}
[/barcode] {null def} forall
bind def
/renmatrix dup load /uk.co.terryburton.bwipp defineresource pop
end
/setpacking where {pop setpacking} if
%%EndData
%%EndResource

%%BeginResource: uk.co.terryburton.bwipp qrcode 0.0 2020040100 303640 335045
%%BeginData:       1292 ASCII Lines
/setpacking where {pop currentpacking true setpacking} if
1 dict
dup /raiseerror dup /uk.co.terryburton.bwipp findresource put
dup /renmatrix dup /uk.co.terryburton.bwipp findresource put
begin
/qrcode {

    20 dict begin

    /options exch def
    /barcode exch def

    /dontdraw false def
    /format (unset) def    % full or micro. rMQR symbols are specified using version
    /version (unset) def   % 1-40 or M1-M4 or R7x43, etc
    /eclevel (unset) def   % full: L, M, Q, H; micro: L, M, Q; rmqr: M, H
    /parse false def
    /parsefnc false def
    /mask -1 def

    % Parse the input options
    options type /stringtype eq {
        1 dict begin
        options {
            token false eq {exit} if dup length string cvs (=) search
            true eq {cvlit exch pop exch def} {cvlit true def} ifelse
        } loop
        currentdict end /options exch def
    } if
    options {def} forall

    /mask mask cvi def

    % If version is supplied and format is not given then set format to correspond
    version (unset) ne {
        format (unset) eq {
            (full)
            version 0 1 getinterval (M) eq { pop (micro) } if
            version 0 1 getinterval (R) eq { pop (rmqr) } if
            /format exch def
        } if
    } {
        format (unset) eq { /format (full) def } if  % Default to full
    } ifelse

    % Parse ordinals of the form ^NNN to ASCII
    parse {
        /msg barcode length string def
        /j 0 def
        barcode
        { % loop
            (^) search {
                dup msg exch j exch putinterval
                length j add 1 add /j exch def
                pop
                dup 0 3 getinterval cvi msg exch j 1 sub exch put
                dup length 3 sub 3 exch getinterval
            } {
                dup msg exch j exch putinterval
                length j add /j exch def
                /barcode msg 0 j getinterval def
                exit
            } ifelse
        } loop
    } if

    /barlen barcode length def

    % Convert input into bytes accounting for FNC characters
    /fn1 -1 def
    /fncvals <<
        (FNC1) fn1
    >> def
    /msg barlen array def
    /i 0 def /j 0 def {
        i barlen eq {exit} if
        /char barcode i get def
        parsefnc char 94 eq and i barlen 4 sub lt and {
            barcode i 1 add get 94 ne {
                /char fncvals barcode i 1 add 4 getinterval get def
                /i i 4 add def
            } {
                /i i 1 add def
            } ifelse
        } if
        msg j char put
        /i i 1 add def
        /j j 1 add def
    } loop
    /msg msg 0 j getinterval def
    /msglen msg length def

    % If FNC1 in first position then escape % as %%
    /fnc1first false def
    msglen 0 gt {
        msg 0 get fn1 eq {
            /fnc1first true def
            /msg [ msg 1 msglen 1 sub getinterval { dup 37 eq {dup} if } forall ] def
            /msglen msg length def
        } if
    } if

    % Default error correction level
    eclevel (unset) eq {/eclevel format (micro) ne {(M)} {(L)} ifelse def} if

    % Enumerate vergrps
    [
        /v1to9    /v10to26  /v27to40
        /vM1      /vM2      /vM3      /vM4
        /vR7x43   /vR7x59   /vR7x77   /vR7x99   /vR7x139
        /vR9x43   /vR9x59   /vR9x77   /vR9x99   /vR9x139
        /vR11x27  /vR11x43  /vR11x59  /vR11x77  /vR11x99   /vR11x139
        /vR13x27  /vR13x43  /vR13x59  /vR13x77  /vR13x99   /vR13x139
        /vR15x43  /vR15x59  /vR15x77  /vR15x99  /vR15x139
        /vR17x43  /vR17x59  /vR17x77  /vR17x99  /vR17x139
    ] 0 exch {1 index def 1 add} forall pop

    % Convert from input into message bitstream
    /N 0 def  /A 1 def  /B 2 def  /K 3 def

    /Nexcl <<
       [
           16#30 1 16#39 {} for
       ] {-1} forall
    >> def

    /Aexcl <<
        [
            16#20 16#24 16#25 16#2A 16#2B 16#2D 16#2E 16#2F 16#3A
            16#41 1 16#5A {} for
            fn1
        ] {-1} forall
    >> def

    /Bexcl <<
        [
            16#00 1 16#1F {} for
            16#21 16#22 16#23 16#26 16#27 16#28 16#29 16#2C
            16#3B 1 16#40 {} for
            16#5B 1 16#7F {} for
            16#A0 1 16#DF {} for
        ] {-1} forall
    >> def

    /Kexcl <<
        [
            16#81 1 16#9F {} for
            16#E0 1 16#EB {} for
        ] {-1} forall
    >> def

    /mids [  % N  A  B  K
        [ (0001) (0010) (0100) (1000) ]  % v1to9
        [ (0001) (0010) (0100) (1000) ]  % v10to26
        [ (0001) (0010) (0100) (1000) ]  % v27to40
        [     ()   -1     -1     -1   ]  % vM1
        [    (0)    (1)   -1     -1   ]  % vM2
        [   (00)   (01)   (10)   (11) ]  % vM3
        [  (000)  (001)  (010)  (011) ]  % vM4
      32 {
        [  (001)  (010)  (011)  (100) ]  % rMQR
      } repeat
    ] def

    /cclens [  % N  A  B  K
        [ 10  9  8  8 ]  % v1to9
        [ 12 11 16 10 ]  % v10to26
        [ 14 13 16 12 ]  % v27to40
        [  3 -1 -1 -1 ]  % vM1
        [  4  3 -1 -1 ]  % vM2
        [  5  4  4  3 ]  % vM3
        [  6  5  5  4 ]  % vM4
        [  4  3  3  2 ]  % vR7x43
        [  5  5  4  3 ]  % vR7x59
        [  6  5  5  4 ]  % vR7x77
        [  7  6  5  5 ]  % vR7x99
        [  7  6  6  5 ]  % vR7x139
        [  5  5  4  3 ]  % vR9x43
        [  6  5  5  4 ]  % vR9x59
        [  7  6  5  5 ]  % vR9x77
        [  7  6  6  5 ]  % vR9x99
        [  8  7  6  6 ]  % vR9x139
        [  4  4  3  2 ]  % vR11x27
        [  6  5  5  4 ]  % vR11x43
        [  7  6  5  5 ]  % vR11x59
        [  7  6  6  5 ]  % vR11x77
        [  8  7  6  6 ]  % vR11x99
        [  8  7  7  6 ]  % vR11x139
        [  5  5  4  3 ]  % vR13x27
        [  6  6  5  5 ]  % vR13x43
        [  7  6  6  5 ]  % vR13x59
        [  8  7  6  6 ]  % vR13x77
        [  8  7  7  6 ]  % vR13x99
        [  8  8  7  7 ]  % vR13x139
        [  7  6  6  5 ]  % vR15x43
        [  7  7  6  5 ]  % vR15x59
        [  8  7  7  6 ]  % vR15x77
        [  8  7  7  6 ]  % vR15x99
        [  9  8  7  7 ]  % vR15x139
        [  7  6  6  5 ]  % vR17x43
        [  8  7  6  6 ]  % vR17x59
        [  8  7  7  6 ]  % vR17x77
        [  8  8  7  6 ]  % vR17x99
        [  9  8  8  7 ]  % vR17x139
    ] def

    /termlens [
      3 {
        4  % Full
      } repeat
        3  % vM1
        5  % vM2
        7  % vM3
        9  % vM4
      32 {
        3  % rMQR
      } repeat
    ] def

    % Encoding functions
    /tobin {
        string dup length 1 sub 0 exch 1 exch {1 index exch 48 put} for
        dup 3 -1 roll 2 2 index length string cvrs dup length 2 index length exch sub exch putinterval
    } bind def

    % Alphanumeric character to value map
    /charmap (0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ $%*+-./:) def
    /charvals 44 dict def
    0 1 44 {dup charmap exch get exch charvals 3 1 roll put} for

    /encA {
        /in exch def
        fnc1first {  % FNC1 as %
            /in [ in { dup -1 eq {pop 37} if } forall ] def
        } if
        /out in length 11 mul 2 idiv 1 add string def
        /k 0 def /m 0 def {
            k in length eq {exit} if
            k in length 1 sub lt {
                charvals in k get get 45 mul charvals in k 1 add get get add 11 tobin
                /k k 2 add def
            } {  % Final character
                charvals in k get get 6 tobin
                /k k 1 add def
            } ifelse
            dup out exch m exch putinterval
            length m add /m exch def
        } loop
        out 0 m getinterval
    } bind def

    /encN {
        /in exch def
        /out in length 10 mul 3 idiv 1 add string def
        /k 0 def /m 0 def {
            k in length eq {exit} if
            k in length 2 sub lt {
                in k 3 getinterval 0 exch {exch 10 mul 48 sub add} forall 10 tobin
                /k k 3 add def
            } {
                k in length 2 sub eq {  % Two final digits
                    in k 2 getinterval 0 exch {exch 10 mul 48 sub add} forall 7 tobin
                    /k k 2 add def
                } {  % One final digit
                    in k 1 getinterval 0 exch {exch 10 mul 48 sub add} forall 4 tobin
                    /k k 1 add def
                } ifelse
            } ifelse
            dup out exch m exch putinterval
            length m add /m exch def
        } loop
        out 0 m getinterval
    } bind def

    /encB {
        /in exch def
        fnc1first {  % FNC1 as GS
            /in [ in { dup -1 eq {pop 29} if } forall ] def
        } if
        /out in length 8 mul string def
        0 1 in length 1 sub {
            /k exch def
            in k get cvi 8 tobin
            out k 8 mul 3 -1 roll putinterval
        } for
        out
    } bind def

    /encK {
        /in exch def
        /out in length 2 idiv 13 mul string def
        /k 0 def /m 0 def {
            k in length eq {exit} if
            in k get 256 mul in k 1 add get add
            dup 16#E040 lt {16#8140} {16#C140} ifelse sub
            dup -8 bitshift 16#C0 mul exch 16#00FF and add
            13 tobin dup out exch m exch putinterval
            length m add /m exch def
            /k k 2 add def
        } loop
        out
    } bind def

    /encfuncs [ /encN /encA /encB /encK ] def

    /addtobits {
        dup bits j 3 -1 roll putinterval
        length j add /j exch def
    } bind def

    /numNs [ msglen {0} repeat 0 ] def
    /numAs [ msglen {0} repeat 0 ] def
    /numBs [ msglen {0} repeat 0 ] def
    /numKs [ msglen {0} repeat -1 ] def
    /nextNs [ msglen {0} repeat 9999 ] def
    /nextBs [ msglen {0} repeat 9999 ] def
    /nextAs [ msglen {0} repeat 9999 ] def
    /nextKs [ msglen {0} repeat 9999 ] def
    msglen 1 sub -1 0 {
        /i exch def
        /barchar msg i get def
        Kexcl barchar known {
            nextKs i 0 put
            numKs i numKs i 1 add get 1 add put
        } {
            nextKs i nextKs i 1 add get 1 add put
        } ifelse
        Nexcl barchar known {
            nextNs i 0 put
            numNs i numNs i 1 add get 1 add put
        } {
            nextNs i nextNs i 1 add get 1 add put
        } ifelse
        Bexcl barchar known {
            nextBs i 0 put
            numBs i numBs i 1 add get 1 add put
        } {
            nextBs i nextBs i 1 add get 1 add put
        } ifelse
        Aexcl barchar known {
            nextAs i 0 put
            numAs i numAs i 1 add get 1 add put
        } {
            nextAs i nextAs i 1 add get 1 add put
        } ifelse
    } for
    /numKs [ numKs {1 add 2 idiv} forall ] def

    /KbeforeB {numK exch ver get ge nextBs numK 2 mul i add get 0 eq and} bind def
    /KbeforeA {numK exch ver get ge nextAs numK 2 mul i add get 0 eq and} bind def
    /KbeforeN {numK exch ver get ge nextNs numK 2 mul i add get 0 eq and} bind def
    /KbeforeE {numK exch ver get ge numK 2 mul i add msglen eq and} bind def
    /AbeforeK {numA exch ver get ge nextKs numA i add get 0 eq and} bind def
    /AbeforeB {numA exch ver get ge nextBs numA i add get 0 eq and} bind def
    /AbeforeN {numA exch ver get ge nextNs numA i add get 0 eq and} bind def
    /AbeforeE {numA exch ver get ge numA i add msglen eq and} bind def
    /NbeforeK {numN exch ver get ge nextKs numN i add get 0 eq and} bind def
    /NbeforeB {numN exch ver get ge nextBs numN i add get 0 eq and} bind def
    /NbeforeA {numN exch ver get ge nextAs numN i add get 0 eq and} bind def
    /NbeforeE {numN exch ver get ge numN i add msglen eq and} bind def

    % Elements of the encoded message have differing lengths based on the
    % resulting symbol size. The symbol sizes with different element lengths
    % are batched into vergrps. To pick the smallest symbol that holds our
    % content we encode the message according to each available vergrp, based
    % on the format of symbol.

    % Determine which groups we need to encode
    version (unset) ne {
        % A specific version of symbol is given so encode in just the corresponding vergrp
        /verset <<
            % Full
             0 1  9 { 10 2 string cvrs v1to9   } for
            10 1 26 { 10 2 string cvrs v10to26 } for
            27 1 40 { 10 2 string cvrs v27to40 } for

            % Micro
            (M1) vM1  (M2) vM2  (M3) vM3  (M4) vM4

            % rMQR
                              (R7x43)  vR7x43   (R7x59)  vR7x59   (R7x77)  vR7x77   (R7x99)  vR7x99   (R7x139)  vR7x139
                              (R9x43)  vR9x43   (R9x59)  vR9x59   (R9x77)  vR9x77   (R9x99)  vR9x99   (R9x139)  vR9x139
            (R11x27) vR11x27  (R11x43) vR11x43  (R11x59) vR11x59  (R11x77) vR11x77  (R11x99) vR11x99  (R11x139) vR11x139
            (R13x27) vR13x27  (R13x43) vR13x43  (R13x59) vR13x59  (R13x77) vR13x77  (R13x99) vR13x99  (R13x139) vR13x139
                              (R15x43) vR15x43  (R15x59) vR15x59  (R15x77) vR15x77  (R15x99) vR15x99  (R15x139) vR15x139
                              (R17x43) vR17x43  (R17x59) vR17x59  (R17x77) vR17x77  (R17x99) vR17x99  (R17x139) vR17x139
        >> version get [ exch ] def
    } {
        % Just the format is specified so encode in each vergrp for the type of symbol
        format (full)  eq { /verset [v1to9 v10to26 v27to40] def } if
        format (micro) eq { /verset [vM1 vM2 vM3 vM4] def } if
        % rMQR symbols are accessed with an explicit version
    } ifelse

    % Encode unterminated bitsream for each compatible vergrp separately
    /msgbits [ 39 {-1} repeat ] def
    /e 10000 def
    verset {
        /ver exch def

        % Derive optimal sequence
        /mode -1 def /seq [] def /i 0 def {
            i msglen ge {exit} if
            /numK numKs i get def
            /numB numBs i get def
            /numA numAs i get def
            /numN numNs i get def
            ver vM1 eq numA 1 ge and {/seq -1 def exit} if
            ver vM1 eq numB 1 ge and {/seq -1 def exit} if
            ver vM1 eq numK 1 ge and {/seq -1 def exit} if
            ver vM2 eq numB 1 ge and {/seq -1 def exit} if
            ver vM2 eq numK 1 ge and {/seq -1 def exit} if
            {  % common exit
                ver vR7x43 ge {  % TODO rMQR is binary only for now
                    B exit
                } if
                mode -1 eq {  % Set initial mode
                    [ 1  1  1  e  e  1  1] KbeforeA {K exit} if
                    [ 1  1  1  e  e  1  1] KbeforeN {K exit} if
                    [ 5  5  6  e  e  2  3] KbeforeB {K exit} if
                    [ 1  1  1  e  e  1  1] KbeforeE {K exit} if
                    numK 1 ge {B exit} if
                    [ 6  7  8  e  e  3  4] AbeforeB {A exit} if
                    [ 1  1  1  e  1  1  1] AbeforeE {A exit} if
                    numA 1 ge {
                        ver vM2 ne {B} {A} ifelse exit
                    } if
                    [ 4  4  5  e  e  2  3] NbeforeB {N exit} if
                    [ 1  1  1  e  e  1  1] NbeforeB {B exit} if
                    [ 7  8  9  e  3  4  5] NbeforeA {N exit} if
                    [ 1  1  1  e  1  1  1] NbeforeA {A exit} if
                    numN 1 ge {N exit} if
                    B exit
                } if
                mode B eq {
                    [ 9 12 13  e  e  4  5] KbeforeB {K exit} if
                    [ 9 10 12  e  e  4  5] KbeforeA {K exit} if
                    [ 9 10 11  e  e  5  6] KbeforeN {K exit} if
                    [ 4  5  6  e  e  2  3] KbeforeE {K exit} if
                    [11 12 14  e  e  5  7] AbeforeK {A exit} if
                    [11 15 16  e  e  6  7] AbeforeB {A exit} if
                    [12 13 15  e  e  6  8] AbeforeN {A exit} if
                    [ 6  7  8  e  e  3  4] AbeforeE {A exit} if
                    [ 6  7  8  e  e  3  4] NbeforeK {N exit} if
                    [ 6  8  9  e  e  3  4] NbeforeB {N exit} if
                    [ 6  7  8  e  e  3  4] NbeforeA {N exit} if
                    [ 3  4  5  e  e  2  3] NbeforeE {N exit} if
                    B exit
                } if
                mode A eq {
                    numK 1 ge {K exit} if
                    numB 1 ge {B exit} if
                    [13 15 17  e  5  7  9] NbeforeA {N exit} if
                    [13 17 18  e  e  7  9] NbeforeB {N exit} if
                    [ 7  8  9  e  3  4  5] NbeforeE {N exit} if
                    numA 1 ge numN 1 ge or {A exit} if
                    B exit
                } if
                mode N eq {
                    numK 1 ge {K exit} if
                    numB 1 ge {B exit} if
                    numA 1 ge {A exit} if
                    numN 1 ge {N exit} if
                    B exit
                } if
                mode K eq {
                    numB 1 ge {B exit} if
                    numA 1 ge {A exit} if
                    numN 1 ge {N exit} if
                    numK 1 ge {K exit} if
                    B exit
                } if
            } loop
            dup K eq fnc1first and {pop B} if  % No kanji with fnc1first
            dup mode eq {  % Same mode encode directly
                pop
                /dat msg i mode K eq {2} {1} ifelse getinterval def
                /seq [
                    seq aload pop
                    [ exch aload pop dat aload pop ]
                ] def
            } {  % Change mode
                /mode exch def
                mode K eq {K msg i numK 2 mul getinterval} if
                mode B eq {B msg i numB getinterval} if
                mode A eq {A msg i numA getinterval} if
                mode N eq {N msg i numN getinterval} if
                /dat exch def /sw exch def
                /seq [ seq aload pop sw dat ] def
            } ifelse
            /i i dat length add def
        } loop

        % Encode the sequence
        {  % common exit
            seq -1 eq {exit} if  % Sequence not available
            /bits 23648 string def
            /j 0 def
            fnc1first {
                ver vR7x43 lt { (0101) } { (101) } ifelse addtobits
            } if
            /abort false def
            0 2 seq length 1 sub {
                /i exch def
                /mode seq i get def
                mids ver get mode get addtobits
                /cclen cclens ver get mode get def
                /chars seq i 1 add get def
                chars length 2 cclen exp cvi ge {  % Too many characters for cc indicator
                    /abort true def exit
                } if
                chars length mode K eq {2 idiv} if cclen tobin addtobits
                chars encfuncs mode get load exec addtobits
            } for
            abort {exit} if
            /bits bits 0 j getinterval def
            msgbits ver bits put
            exit
        } loop
    } forall

    % Lookup the most appropriate symbol specification
    /metrics [
        % format   vers       vergrp  rows cols align modules    error codewords        error correction blocks
        %                                                          L    M    Q    H       L1 L2 M1 M2 Q1 Q2 H1 H2
        [ (micro)  (M1)       vM1       11  11  98 99     36  [   2   99   99   99 ]  [  1  0 -1 -1 -1 -1 -1 -1 ] ]
        [ (micro)  (M2)       vM2       13  13  98 99     80  [   5    6   99   99 ]  [  1  0  1  0 -1 -1 -1 -1 ] ]
        [ (micro)  (M3)       vM3       15  15  98 99    132  [   6    8   99   99 ]  [  1  0  1  0 -1 -1 -1 -1 ] ]
        [ (micro)  (M4)       vM4       17  17  98 99    192  [   8   10   14   99 ]  [  1  0  1  0  1  0 -1 -1 ] ]
        [ (full)   (1)        v1to9     21  21  98 99    208  [   7   10   13   17 ]  [  1  0  1  0  1  0  1  0 ] ]
        [ (full)   (2)        v1to9     25  25  18 99    359  [  10   16   22   28 ]  [  1  0  1  0  1  0  1  0 ] ]
        [ (full)   (3)        v1to9     29  29  22 99    567  [  15   26   36   44 ]  [  1  0  1  0  2  0  2  0 ] ]
        [ (full)   (4)        v1to9     33  33  26 99    807  [  20   36   52   64 ]  [  1  0  2  0  2  0  4  0 ] ]
        [ (full)   (5)        v1to9     37  37  30 99   1079  [  26   48   72   88 ]  [  1  0  2  0  2  2  2  2 ] ]
        [ (full)   (6)        v1to9     41  41  34 99   1383  [  36   64   96  112 ]  [  2  0  4  0  4  0  4  0 ] ]
        [ (full)   (7)        v1to9     45  45  22 38   1568  [  40   72  108  130 ]  [  2  0  4  0  2  4  4  1 ] ]
        [ (full)   (8)        v1to9     49  49  24 42   1936  [  48   88  132  156 ]  [  2  0  2  2  4  2  4  2 ] ]
        [ (full)   (9)        v1to9     53  53  26 46   2336  [  60  110  160  192 ]  [  2  0  3  2  4  4  4  4 ] ]
        [ (full)   (10)       v10to26   57  57  28 50   2768  [  72  130  192  224 ]  [  2  2  4  1  6  2  6  2 ] ]
        [ (full)   (11)       v10to26   61  61  30 54   3232  [  80  150  224  264 ]  [  4  0  1  4  4  4  3  8 ] ]
        [ (full)   (12)       v10to26   65  65  32 58   3728  [  96  176  260  308 ]  [  2  2  6  2  4  6  7  4 ] ]
        [ (full)   (13)       v10to26   69  69  34 62   4256  [ 104  198  288  352 ]  [  4  0  8  1  8  4 12  4 ] ]
        [ (full)   (14)       v10to26   73  73  26 46   4651  [ 120  216  320  384 ]  [  3  1  4  5 11  5 11  5 ] ]
        [ (full)   (15)       v10to26   77  77  26 48   5243  [ 132  240  360  432 ]  [  5  1  5  5  5  7 11  7 ] ]
        [ (full)   (16)       v10to26   81  81  26 50   5867  [ 144  280  408  480 ]  [  5  1  7  3 15  2  3 13 ] ]
        [ (full)   (17)       v10to26   85  85  30 54   6523  [ 168  308  448  532 ]  [  1  5 10  1  1 15  2 17 ] ]
        [ (full)   (18)       v10to26   89  89  30 56   7211  [ 180  338  504  588 ]  [  5  1  9  4 17  1  2 19 ] ]
        [ (full)   (19)       v10to26   93  93  30 58   7931  [ 196  364  546  650 ]  [  3  4  3 11 17  4  9 16 ] ]
        [ (full)   (20)       v10to26   97  97  34 62   8683  [ 224  416  600  700 ]  [  3  5  3 13 15  5 15 10 ] ]
        [ (full)   (21)       v10to26  101 101  28 50   9252  [ 224  442  644  750 ]  [  4  4 17  0 17  6 19  6 ] ]
        [ (full)   (22)       v10to26  105 105  26 50  10068  [ 252  476  690  816 ]  [  2  7 17  0  7 16 34  0 ] ]
        [ (full)   (23)       v10to26  109 109  30 54  10916  [ 270  504  750  900 ]  [  4  5  4 14 11 14 16 14 ] ]
        [ (full)   (24)       v10to26  113 113  28 54  11796  [ 300  560  810  960 ]  [  6  4  6 14 11 16 30  2 ] ]
        [ (full)   (25)       v10to26  117 117  32 58  12708  [ 312  588  870 1050 ]  [  8  4  8 13  7 22 22 13 ] ]
        [ (full)   (26)       v10to26  121 121  30 58  13652  [ 336  644  952 1110 ]  [ 10  2 19  4 28  6 33  4 ] ]
        [ (full)   (27)       v27to40  125 125  34 62  14628  [ 360  700 1020 1200 ]  [  8  4 22  3  8 26 12 28 ] ]
        [ (full)   (28)       v27to40  129 129  26 50  15371  [ 390  728 1050 1260 ]  [  3 10  3 23  4 31 11 31 ] ]
        [ (full)   (29)       v27to40  133 133  30 54  16411  [ 420  784 1140 1350 ]  [  7  7 21  7  1 37 19 26 ] ]
        [ (full)   (30)       v27to40  137 137  26 52  17483  [ 450  812 1200 1440 ]  [  5 10 19 10 15 25 23 25 ] ]
        [ (full)   (31)       v27to40  141 141  30 56  18587  [ 480  868 1290 1530 ]  [ 13  3  2 29 42  1 23 28 ] ]
        [ (full)   (32)       v27to40  145 145  34 60  19723  [ 510  924 1350 1620 ]  [ 17  0 10 23 10 35 19 35 ] ]
        [ (full)   (33)       v27to40  149 149  30 58  20891  [ 540  980 1440 1710 ]  [ 17  1 14 21 29 19 11 46 ] ]
        [ (full)   (34)       v27to40  153 153  34 62  22091  [ 570 1036 1530 1800 ]  [ 13  6 14 23 44  7 59  1 ] ]
        [ (full)   (35)       v27to40  157 157  30 54  23008  [ 570 1064 1590 1890 ]  [ 12  7 12 26 39 14 22 41 ] ]
        [ (full)   (36)       v27to40  161 161  24 50  24272  [ 600 1120 1680 1980 ]  [  6 14  6 34 46 10  2 64 ] ]
        [ (full)   (37)       v27to40  165 165  28 54  25568  [ 630 1204 1770 2100 ]  [ 17  4 29 14 49 10 24 46 ] ]
        [ (full)   (38)       v27to40  169 169  32 58  26896  [ 660 1260 1860 2220 ]  [  4 18 13 32 48 14 42 32 ] ]
        [ (full)   (39)       v27to40  173 173  26 54  28256  [ 720 1316 1950 2310 ]  [ 20  4 40  7 43 22 10 67 ] ]
        [ (full)   (40)       v27to40  177 177  30 58  29648  [ 750 1372 2040 2430 ]  [ 19  6 18 31 34 34 20 61 ] ]
        [ (rmqr)   (R7x43)    vR7x43     7  43  22 99    104  [  99    7   99   10 ]  [ -1 -1  1  0 -1 -1  1  0 ] ]
        [ (rmqr)   (R7x59)    vR7x59     7  59  20 40    171  [  99    9   99   14 ]  [ -1 -1  1  0 -1 -1  1  0 ] ]
        [ (rmqr)   (R7x77)    vR7x77     7  77  26 52    261  [  99   12   99   22 ]  [ -1 -1  1  0 -1 -1  1  0 ] ]
        [ (rmqr)   (R7x99)    vR7x99     7  99  24 50    358  [  99   16   99   30 ]  [ -1 -1  1  0 -1 -1  1  0 ] ]
        [ (rmqr)   (R7x139)   vR7x139    7 139  28 56    545  [  99   24   99   44 ]  [ -1 -1  1  0 -1 -1  2  0 ] ]
        [ (rmqr)   (R9x43)    vR9x43     9  43  22 99    170  [  99    9   99   14 ]  [ -1 -1  1  0 -1 -1  1  0 ] ]
        [ (rmqr)   (R9x59)    vR9x59     9  59  20 40    267  [  99   12   99   22 ]  [ -1 -1  1  0 -1 -1  1  0 ] ]
        [ (rmqr)   (R9x77)    vR9x77     9  77  26 52    393  [  99   18   99   32 ]  [ -1 -1  1  0 -1 -1  1  1 ] ]
        [ (rmqr)   (R9x99)    vR9x99     9  99  24 50    532  [  99   24   99   44 ]  [ -1 -1  1  0 -1 -1  2  0 ] ]
        [ (rmqr)   (R9x139)   vR9x139    9 139  28 56    797  [  99   36   99   66 ]  [ -1 -1  1  1 -1 -1  3  0 ] ]
        [ (rmqr)   (R11x27)   vR11x27   11  27  98 99    122  [  99    8   99   10 ]  [ -1 -1  1  0 -1 -1  1  0 ] ]
        [ (rmqr)   (R11x43)   vR11x43   11  43  22 99    249  [  99   12   99   20 ]  [ -1 -1  1  0 -1 -1  1  0 ] ]
        [ (rmqr)   (R11x59)   vR11x59   11  59  20 40    376  [  99   16   99   32 ]  [ -1 -1  1  0 -1 -1  1  1 ] ]
        [ (rmqr)   (R11x77)   vR11x77   11  77  26 52    538  [  99   24   99   44 ]  [ -1 -1  1  0 -1 -1  1  1 ] ]
        [ (rmqr)   (R11x99)   vR11x99   11  99  24 50    719  [  99   32   99   60 ]  [ -1 -1  1  1 -1 -1  1  1 ] ]
        [ (rmqr)   (R11x139)  vR11x139  11 139  28 56   1062  [  99   48   99   90 ]  [ -1 -1  2  0 -1 -1  3  0 ] ]
        [ (rmqr)   (R13x27)   vR13x27   13  27  98 99    172  [  99    9   99   14 ]  [ -1 -1  1  0 -1 -1  1  0 ] ]
        [ (rmqr)   (R13x43)   vR13x43   13  43  22 99    329  [  99   14   99   28 ]  [ -1 -1  1  0 -1 -1  1  0 ] ]
        [ (rmqr)   (R13x59)   vR13x59   13  59  20 40    486  [  99   22   99   40 ]  [ -1 -1  1  0 -1 -1  2  0 ] ]
        [ (rmqr)   (R13x77)   vR13x77   13  77  26 52    684  [  99   32   99   56 ]  [ -1 -1  1  1 -1 -1  1  1 ] ]
        [ (rmqr)   (R13x99)   vR13x99   13  99  24 50    907  [  99   40   99   78 ]  [ -1 -1  1  1 -1 -1  1  2 ] ]
        [ (rmqr)   (R13x139)  vR13x139  13 139  28 56   1328  [  99   60   99  112 ]  [ -1 -1  2  1 -1 -1  2  2 ] ]
        [ (rmqr)   (R15x43)   vR15x43   15  43  22 99    409  [  99   18   99   36 ]  [ -1 -1  1  0 -1 -1  1  1 ] ]
        [ (rmqr)   (R15x59)   vR15x59   15  59  20 40    596  [  99   26   99   48 ]  [ -1 -1  1  0 -1 -1  2  0 ] ]
        [ (rmqr)   (R15x77)   vR15x77   15  77  26 52    830  [  99   36   99   72 ]  [ -1 -1  1  1 -1 -1  2  1 ] ]
        [ (rmqr)   (R15x99)   vR15x99   15  99  24 50   1095  [  99   48   99   88 ]  [ -1 -1  2  0 -1 -1  4  0 ] ]
        [ (rmqr)   (R15x139)  vR15x139  15 139  28 56   1594  [  99   72   99  130 ]  [ -1 -1  2  1 -1 -1  1  4 ] ]
        [ (rmqr)   (R17x43)   vR17x43   17  43  22 99    489  [  99   22   99   40 ]  [ -1 -1  1  0 -1 -1  1  1 ] ]
        [ (rmqr)   (R17x59)   vR17x59   17  59  20 40    706  [  99   32   99   60 ]  [ -1 -1  2  0 -1 -1  2  0 ] ]
        [ (rmqr)   (R17x77)   vR17x77   17  77  26 52    976  [  99   44   99   84 ]  [ -1 -1  2  0 -1 -1  1  2 ] ]
        [ (rmqr)   (R17x99)   vR17x99   17  99  24 50   1283  [  99   60   99  104 ]  [ -1 -1  2  1 -1 -1  4  0 ] ]
        [ (rmqr)   (R17x139)  vR17x139  17 139  28 56   1860  [  99   80   99  156 ]  [ -1 -1  4  0 -1 -1  2  4 ] ]
    ] def
    /eclval (LMQH) eclevel search pop length exch pop exch pop def
    0 1 metrics length 1 sub {
        /i exch def
        /m metrics i get def
        /frmt m 0 get def                             % Format of the symbol
        /vers m 1 get def                             % Version of symbol
        /vergrp m 2 get def                           % Version group
        /verind i 44 sub def                          % Version indicator for rMQR format info
        /rows m 3 get def                             % Length of side
        /cols m 4 get def                             % Length of side
        /asp2 m 5 get def                             % Position of second alignment symbol
        /asp3 m 6 get def                             % Position of third alignment symbol
        /nmod m 7 get def                             % Number of modules
        /ncws nmod 8 idiv def                         % Total number of codewords
        /rbit nmod 8 mod def                          % Number of remainder bits
        /lc4b false def                               % Last data codeword is 4 bits long
        vers (M1) eq vers (M3) eq or {                % Adjustments for M1 and M3 symbols
            /ncws ncws 1 add def
            /rbit 0 def
            /lc4b true def
        } if
        /ecws m 8 get eclval get def                  % Number of error correction codewords
        /dcws ncws ecws sub def                       % Number of data codewords
        /dmod dcws 8 mul lc4b {4} {0} ifelse sub def  % Number of data modules
        /ecb1 m 9 get eclval 2 mul get def            % First error correction blocks
        /ecb2 m 9 get eclval 2 mul 1 add get def      % Second error correction blocks
        /okay true def
        format frmt ne {/okay false def} if           % The format must match that supplied
        frmt (micro) eq fnc1first and {/okay false def} if  % FNC1 mode is only available in full and rmqr
        version (unset) ne version vers ne and {/okay false def} if  % The version must match that supplied
        ecb1 -1 eq ecb2 -1 eq or {/okay false def} if % Error correction level must be valid
        /verbits msgbits vergrp get def
        verbits -1 eq {                               % Bitstream must be available
            /okay false def
        } {
            verbits length dmod gt {/okay false def} if  % and fit into data modules
        } ifelse
        /term (000000000) 0 termlens vergrp get getinterval def
        okay {exit} if
    } for

    okay not {
        /bwipp.qrcodeNoValidSymbol (No valid symbol available) //raiseerror exec
    } if

    /format frmt def
    /version vers def
    /msgbits verbits def
    /dcpb dcws ecb1 ecb2 add idiv def                 % Base data codewords per block
    /ecpb ncws ecb1 ecb2 add idiv dcpb sub def        % Error correction codewords per block

    % Complete the message bits by adding the terminator, truncated if neccessary
    /term term 0 dmod msgbits length sub term length 2 copy gt {exch} if pop getinterval def
    msgbits length term length add string
    dup 0 msgbits putinterval
    dup msgbits length term putinterval
    /msgbits exch def

    % Expand the message bits by adding padding as necessary
    /pad dmod string def
    0 1 pad length 1 sub {pad exch 48 put} for
    pad 0 msgbits putinterval
    /padstrs [ (11101100) (00010001) ] def
    /padnum 0 def
    msgbits length 8 div ceiling 8 mul cvi 8 dmod lc4b {5} {1} ifelse sub {
        pad exch padstrs padnum get putinterval
        /padnum padnum 1 add 2 mod def
    } for

    % Evaluate the padded message into codewords
    /cws dcws array def
    0 1 cws length 1 sub {
        /c exch def
        /bpcw 8 def
        lc4b c cws length 1 sub eq and {/bpcw 4 def} if
        /cwb pad c 8 mul bpcw getinterval def
        /cw 0 def
        0 1 bpcw 1 sub {
            /i exch def
            /cw cw 2 bpcw i sub 1 sub exp cvi cwb i get 48 sub mul add def
        } for
        cws c cw put
    } for

    % Short final data byte in M1 and M3 symbols has high-order value
    lc4b {cws cws length 1 sub 2 copy get 4 bitshift put} if

    % Calculate the log and anti-log tables
    /rsalog [ 1 255 { dup 2 mul dup 256 ge {285 xor} if } repeat ] def
    /rslog 256 array def
    1 1 255 {dup rsalog exch get exch rslog 3 1 roll put} for

    % Function to calculate the product in the field
    /rsprod {
        2 copy 0 ne exch 0 ne and {
            rslog exch get exch rslog exch get add 255 mod rsalog exch get
        } {
            pop pop 0
        } ifelse
    } bind def

    % Generate the coefficients for the Reed-Solomon algorithm
    /coeffs [ 1 ecpb {0} repeat ] def
    0 1 ecpb 1 sub {
        /i exch def
        coeffs i 1 add coeffs i get put
        i -1 1 {
            /j exch def
            coeffs j coeffs j 1 sub get coeffs j get rsalog i get rsprod xor put
        } for
        coeffs 0 coeffs 0 get rsalog i get rsprod put
    } for
    /coeffs coeffs 0 coeffs length 1 sub getinterval def

    % Reed-Solomon algorithm to derive the error correction codewords
    /rscodes {
        /rscws exch def
        /rsnd rscws length def
        /rscws [ rscws {} forall ecpb {0} repeat ] def
        0 1 rsnd 1 sub {
            /m exch def
            /k rscws m get def
            0 1 ecpb 1 sub {
                /j exch def
                rscws m j add 1 add coeffs ecpb j sub 1 sub get k rsprod rscws m j add 1 add get xor put
            } for
        } for
        rscws rsnd ecpb getinterval
    } bind def

    % Divide codewords into two groups of blocks and calculate the error correction codewords
    /dcwsb ecb1 ecb2 add array def
    /ecwsb ecb1 ecb2 add array def
    0 1 ecb1 1 sub {  % First group of blocks has smaller number of data codewords
        /i exch def
        dcwsb i cws i dcpb mul dcpb getinterval put
        ecwsb i dcwsb i get rscodes put
    } for
    0 1 ecb2 1 sub {  % Second group of blocks has larger number of data codewords
        /i exch def
        dcwsb ecb1 i add cws ecb1 dcpb mul i dcpb 1 add mul add dcpb 1 add getinterval put
        ecwsb ecb1 i add dcwsb ecb1 i add get rscodes put
    } for

    % Reassemble the codewords
    /cws ncws array def
    /cw 0 def
    0 1 dcpb {  % Interleave the data codeword blocks
        /i exch def
        0 1 ecb1 ecb2 add 1 sub {
            /j exch def
            i dcwsb j get length lt {  % Ignore the end of short blocks
                cws cw dcwsb j get i get put
                /cw cw 1 add def
            } if
        } for
    } for
    0 1 ecpb 1 sub {  % Interleave the error codeword blocks
        /i exch def
        0 1 ecb1 ecb2 add 1 sub {
            /j exch def
            cws cw ecwsb j get i get put
            /cw cw 1 add def
        } for
    } for

    % Extend codewords by one if there are remainder bits
    rbit 0 gt {
        /pad cws length 1 add array def
        pad 0 cws putinterval
        pad pad length 1 sub 0 put
        /cws pad def
    } if

    % Fixups for the short final data byte in M1 and M3 symbols
    lc4b {
        cws dcws 1 sub 2 copy get -4 bitshift put
        dcws 1 sub 1 ncws 2 sub {
            /i exch def
            cws i cws i get 15 and 4 bitshift put
            cws i cws i 1 add get -4 bitshift 15 and cws i get or put
        } for
        cws ncws 1 sub cws ncws 1 sub get 15 and 4 bitshift put
    } if

    % Create the bitmap
    /pixs [ rows cols mul {-1} repeat ] def
    /qmv {cols mul add} bind def

    % Timing patterns
    format (full) eq {
        8 1 cols 9 sub {
            /i exch def
            pixs i 6 qmv i 1 add 2 mod put
            pixs 6 i qmv i 1 add 2 mod put
        } for
    } if
    format (micro) eq {
        8 1 cols 1 sub {
            /i exch def
            pixs i 0 qmv i 1 add 2 mod put
            pixs 0 i qmv i 1 add 2 mod put
        } for
    } if
    format (rmqr) eq {
        3 1 cols 4 sub {  % Along top and bottom
            /i exch def
            pixs i 0 qmv i 1 add 2 mod put
            pixs i rows 1 sub qmv i 1 add 2 mod put
        } for
        3 1 rows 4 sub {  % Along left and right
            /i exch def
            pixs 0 i qmv i 1 add 2 mod put
            pixs cols 1 sub i qmv i 1 add 2 mod put
        } for
        asp2 1 sub asp3 asp2 sub cols 13 sub {  % Down interior
            /i exch def
            3 1 rows 4 sub {
                /j exch def
                pixs i j qmv j 1 add 2 mod put
            } for
        } for
    } if

    % Finder patterns
    /fpat [
        [ 1 1 1 1 1 1 1 0 ]
        [ 1 0 0 0 0 0 1 0 ]
        [ 1 0 1 1 1 0 1 0 ]
        [ 1 0 1 1 1 0 1 0 ]
        [ 1 0 1 1 1 0 1 0 ]
        [ 1 0 0 0 0 0 1 0 ]
        [ 1 1 1 1 1 1 1 0 ]
        [ 0 0 0 0 0 0 0 0 ]
    ] def
    /fsubpat [
        [ 1 1 1 1 1 9 9 9 ]
        [ 1 0 0 0 1 9 9 9 ]
        [ 1 0 1 0 1 9 9 9 ]
        [ 1 0 0 0 1 9 9 9 ]
        [ 1 1 1 1 1 9 9 9 ]
        [ 9 9 9 9 9 9 9 9 ]
        [ 9 9 9 9 9 9 9 9 ]
        [ 9 9 9 9 9 9 9 9 ]
    ] def
    /fcorpat [
        [ 1 1 1 9 9 9 9 9 ]
        [ 1 0 9 9 9 9 9 9 ]
        [ 1 9 9 9 9 9 9 9 ]
        [ 9 9 9 9 9 9 9 9 ]
        [ 9 9 9 9 9 9 9 9 ]
        [ 9 9 9 9 9 9 9 9 ]
        [ 9 9 9 9 9 9 9 9 ]
        [ 9 9 9 9 9 9 9 9 ]
    ] def
    /fnullpat [
        [ 9 9 9 9 9 9 9 9 ]
        [ 9 9 9 9 9 9 9 9 ]
        [ 9 9 9 9 9 9 9 9 ]
        [ 9 9 9 9 9 9 9 9 ]
        [ 9 9 9 9 9 9 9 9 ]
        [ 9 9 9 9 9 9 9 9 ]
        [ 9 9 9 9 9 9 9 9 ]
        [ 9 9 9 9 9 9 9 9 ]
    ] def
    /fpats <<
        %          TL     TR       BL       BR
        (full)  [ fpat fpat     fpat     fnullpat ]
        (micro) [ fpat fnullpat fnullpat fnullpat ]
        (rmqr)  [ fpat fcorpat  fcorpat  fsubpat  ]
    >> format get def
    0 1 7 {
      /y exch def
      0 1 7 {
        /x exch def
        /fpb0 fpats 0 get y get x get def
        /fpb1 fpats 1 get y get x get def
        /fpb2 fpats 2 get y get x get def
        /fpb3 fpats 3 get y get x get def
        fpb0 9 ne y rows lt and {pixs x y qmv fpb0 put} if
        fpb1 9 ne {pixs cols x sub 1 sub y qmv fpb1 put} if
        fpb2 9 ne {pixs x rows y sub 1 sub qmv fpb2 put} if
        fpb3 9 ne {pixs cols x sub 1 sub rows y sub 1 sub qmv fpb3 put} if
      } for
    } for

    % Alignment patterns
    /putalgnpat {
        /py exch def
        /px exch def
        0 1 4 {
            /pb exch def
            0 1 4 {
                /pa exch def
                /algnb algnpat pb get pa get def
                algnb 9 ne {
                    pixs px pa add py pb add qmv algnb put
                } if
            } for
        } for
    } bind def
    format (full) eq {
        /algnpat [
            [ 1 1 1 1 1 ]
            [ 1 0 0 0 1 ]
            [ 1 0 1 0 1 ]
            [ 1 0 0 0 1 ]
            [ 1 1 1 1 1 ]
        ] def
        asp2 2 sub asp3 asp2 sub cols 13 sub {
            /i exch def
            i 4 putalgnpat
            4 i putalgnpat
        } for
        asp2 2 sub asp3 asp2 sub cols 9 sub {
            /x exch def
            asp2 2 sub asp3 asp2 sub rows 9 sub {
                /y exch def
                x y putalgnpat
            } for
        } for
    } if
    format (rmqr) eq {
        /algnpat [
            [ 1 1 1 9 9 ]
            [ 1 0 1 9 9 ]
            [ 1 1 1 9 9 ]
            [ 9 9 9 9 9 ]
            [ 9 9 9 9 9 ]
        ] def
        asp2 2 sub asp3 asp2 sub cols 13 sub {
            /i exch def
            i 0 putalgnpat
            i rows 3 sub putalgnpat
        } for
    } if

    % Format information modules
    /formatmap <<
        (full) [
            [ [ 0 8 ] [ 8 cols 1 sub ] ]  [ [ 1 8 ] [ 8 cols 2 sub ] ]  [ [ 2 8 ] [ 8 cols 3 sub ] ]
            [ [ 3 8 ] [ 8 cols 4 sub ] ]  [ [ 4 8 ] [ 8 cols 5 sub ] ]  [ [ 5 8 ] [ 8 cols 6 sub ] ]
            [ [ 7 8 ] [ 8 cols 7 sub ] ]  [ [ 8 8 ] [ cols 8 sub 8 ] ]  [ [ 8 7 ] [ cols 7 sub 8 ] ]
            [ [ 8 5 ] [ cols 6 sub 8 ] ]  [ [ 8 4 ] [ cols 5 sub 8 ] ]  [ [ 8 3 ] [ cols 4 sub 8 ] ]
            [ [ 8 2 ] [ cols 3 sub 8 ] ]  [ [ 8 1 ] [ cols 2 sub 8 ] ]  [ [ 8 0 ] [ cols 1 sub 8 ] ]
        ]
        (micro) [
            [ [ 1 8 ] ]  [ [ 2 8 ] ]  [ [ 3 8 ] ]  [ [ 4 8 ] ]  [ [ 5 8 ] ]
            [ [ 6 8 ] ]  [ [ 7 8 ] ]  [ [ 8 8 ] ]  [ [ 8 7 ] ]  [ [ 8 6 ] ]
            [ [ 8 5 ] ]  [ [ 8 4 ] ]  [ [ 8 3 ] ]  [ [ 8 2 ] ]  [ [ 8 1 ] ]
        ]
        (rmqr) [
            [ [ 11 3 ] [ cols 3 sub rows 6 sub ] ]  [ [ 11 2 ] [ cols 4 sub rows 6 sub ] ]  [ [ 11 1 ] [ cols 5 sub rows 6 sub ] ]
            [ [ 10 5 ] [ cols 6 sub rows 2 sub ] ]  [ [ 10 4 ] [ cols 6 sub rows 3 sub ] ]  [ [ 10 3 ] [ cols 6 sub rows 4 sub ] ]
            [ [ 10 2 ] [ cols 6 sub rows 5 sub ] ]  [ [ 10 1 ] [ cols 6 sub rows 6 sub ] ]  [ [  9 5 ] [ cols 7 sub rows 2 sub ] ]
            [ [  9 4 ] [ cols 7 sub rows 3 sub ] ]  [ [  9 3 ] [ cols 7 sub rows 4 sub ] ]  [ [  9 2 ] [ cols 7 sub rows 5 sub ] ]
            [ [  9 1 ] [ cols 7 sub rows 6 sub ] ]  [ [  8 5 ] [ cols 8 sub rows 2 sub ] ]  [ [  8 4 ] [ cols 8 sub rows 3 sub ] ]
            [ [  8 3 ] [ cols 8 sub rows 4 sub ] ]  [ [  8 2 ] [ cols 8 sub rows 5 sub ] ]  [ [  8 1 ] [ cols 8 sub rows 6 sub ] ]
        ]
    >> format get def
    formatmap {
        { {} forall qmv pixs exch 1 put } forall
    } forall

    % Version information modules
    format (full) eq cols 45 ge and {
        /versionmap [
            [ [ cols  9 sub 5 ] [ 5 cols  9 sub ] ]  [ [ cols 10 sub 5 ] [ 5 cols 10 sub ] ]
            [ [ cols 11 sub 5 ] [ 5 cols 11 sub ] ]  [ [ cols  9 sub 4 ] [ 4 cols  9 sub ] ]
            [ [ cols 10 sub 4 ] [ 4 cols 10 sub ] ]  [ [ cols 11 sub 4 ] [ 4 cols 11 sub ] ]
            [ [ cols  9 sub 3 ] [ 3 cols  9 sub ] ]  [ [ cols 10 sub 3 ] [ 3 cols 10 sub ] ]
            [ [ cols 11 sub 3 ] [ 3 cols 11 sub ] ]  [ [ cols  9 sub 2 ] [ 2 cols  9 sub ] ]
            [ [ cols 10 sub 2 ] [ 2 cols 10 sub ] ]  [ [ cols 11 sub 2 ] [ 2 cols 11 sub ] ]
            [ [ cols  9 sub 1 ] [ 1 cols  9 sub ] ]  [ [ cols 10 sub 1 ] [ 1 cols 10 sub ] ]
            [ [ cols 11 sub 1 ] [ 1 cols 11 sub ] ]  [ [ cols  9 sub 0 ] [ 0 cols  9 sub ] ]
            [ [ cols 10 sub 0 ] [ 0 cols 10 sub ] ]  [ [ cols 11 sub 0 ] [ 0 cols 11 sub ] ]
        ] def
    } {
        /versionmap [] def
    } ifelse
    versionmap {
        { {} forall qmv pixs exch 0 put } forall
    } forall

    % Reserve the solitary dark module in full symbols
    format (full) eq {
        pixs 8 rows 8 sub qmv 0 put
    } if

    % Calculate the mask patterns
    /maskfuncs <<
        (full) [
            {add 2 mod}
            {exch pop 2 mod}
            {pop 3 mod}
            {add 3 mod}
            {2 idiv exch 3 idiv add 2 mod}
            {mul dup 2 mod exch 3 mod add}
            {mul dup 2 mod exch 3 mod add 2 mod}
            {2 copy mul 3 mod 3 1 roll add 2 mod add 2 mod}
        ]
        (micro) [
            {exch pop 2 mod}
            {2 idiv exch 3 idiv add 2 mod}
            {mul dup 2 mod exch 3 mod add 2 mod}
            {2 copy mul 3 mod 3 1 roll add 2 mod add 2 mod}
        ]
        (rmqr) [
            {2 idiv exch 3 idiv add 2 mod}
        ]
    >> format get def
    mask -1 ne {  % User specifies a mask
        /maskfuncs [maskfuncs mask 1 sub get] def
        /bestmaskval mask 1 sub def
    } if
    /masks maskfuncs length array def
    0 1 masks length 1 sub {
        /m exch def
        /mask rows cols mul array def
        0 1 rows 1 sub {
            /j exch def
            0 1 cols 1 sub {
                /i exch def
                i j maskfuncs m get exec 0 eq
                pixs i j qmv get -1 eq and {1} {0} ifelse
                mask i j qmv 3 -1 roll put
            } for
        } for
        masks m mask put
    } for

    % Walk the symbol placing the bitstream
    /posx cols format (rmqr) ne {1} {2} ifelse sub def
    /posy rows 1 sub def
    /dir -1 def  % -1 is upwards, 1 is downwards
    /col 1 def   % 0 is left bit, 1 is right bit
    /num 0 def
    { % loop
        posx 0 lt {exit} if
        pixs posx posy qmv get -1 eq {
            cws num 8 idiv get 7 num 8 mod sub neg bitshift 1 and
            pixs posx posy qmv 3 -1 roll put
            /num num 1 add def
        } if
        col 1 eq {
            /col 0 def
            /posx posx 1 sub def
        } {
            /col 1 def
            /posx posx 1 add def
            /posy posy dir add def
            posy 0 lt posy rows ge or {  % Turn around at top and bottom
                /dir dir -1 mul def
                /posy posy dir add def
                /posx posx 2 sub def
                % Hop over the timing pattern in full size symbols
                format (full) eq posx 6 eq and {/posx posx 1 sub def} if
            } if
        } ifelse
    } loop

    % Evaluate runlength encoded rows or columns in full symbols
    /evalfulln1n3 {
        /scrle exch def
        % Detect runs of 5 or more like modules
        /scr1 0 scrle { dup 5 ge {add 2 sub dup} if pop } forall def
        % Detect 1:1:3:1:1 ratio next to 4 modules of whitespace
        /scr3 0 def
        3 2 scrle length 3 sub {  % Scan odd (dark) runs within bounds
            /j exch def
            scrle j get 3 mod 0 eq {  % Multiple of 3 black modules
                /fact scrle j get 3 idiv def
                scrle j 2 sub 5 getinterval {fact eq} forall and exch pop and and {
                    j 3 eq j 4 add scrle length ge or {  % At either extent of run
                        /scr3 scr3 40 add def
                    } {  % Bounded by dark modules
                        scrle j 3 sub get 4 ge scrle j 3 add get 4 ge or {
                            /scr3 scr3 40 add def
                        } if
                    } ifelse
                } if
            } if
        } for
        scr1 scr3
    } bind def

    % Evaluation algorithm for full symbols
    /evalfull {
        /sym exch def

        /n1 0 def /n2 0 def /n3 0 def
        /rle cols 1 add array def
        /lastpairs cols array def
        /thispairs cols array def
        /colsadd1 cols 1 add def
        0 1 cols 1 sub {
            /i exch def

            % Runlength encode (light, dark, light, ...) and evaluate each column
            mark 0 0
            i cols dup dup mul 1 sub {
                sym exch get exch 1 index eq {exch 1 add exch} {1 exch} ifelse
            } for
            pop
            rle 0 counttomark 2 sub getinterval astore
            evalfulln1n3 n3 add /n3 exch def n1 add /n1 exch def
            pop

            % Runlength encode (light, dark, light, ...) and evaluate each row
            /symrow sym i cols mul cols getinterval def
            mark 0 0
            symrow {
                exch 1 index eq {exch 1 add exch} {1 exch} ifelse
            } forall
            pop
            rle 0 counttomark 2 sub getinterval astore
            evalfulln1n3 n3 add /n3 exch def n1 add /n1 exch def
            pop

            % Count and score same coloured blocks
            /lastpairs thispairs /thispairs lastpairs def def
            symrow 0 get 1 eq {0} {1} ifelse
            symrow {exch 1 index add exch} forall
            pop
            thispairs astore pop
            i 0 gt {
                mark
                lastpairs aload pop thispairs aload pop
                n2 cols { exch colsadd1 index add 3 and 0 eq {3 add} if } repeat
                /n2 exch def
                cleartomark
            } if

        } for

        % Score dark/light imbalance
        /dark 0 sym {add} forall def
        /n4 dark 100 mul cols dup mul div 50 sub abs 5 div cvi 10 mul def

        n1 n2 add n3 add n4 add
    } bind def

    % Evaluation algorithm for micro symbols
    /evalmicro {
        /sym exch def
        /dkrhs 0 def /dkbot 0 def
        1 1 cols 1 sub {
            /i exch def
            /dkrhs dkrhs sym cols 1 sub i qmv get add def
            /dkbot dkbot sym i cols 1 sub qmv get add def
        } for
        dkrhs dkbot le {
            dkrhs 16 mul dkbot add neg
        } {
            dkbot 16 mul dkrhs add neg
        } ifelse
    } bind def

    % Evaluate the masked symbols to find the most suitable
    /bestscore 999999999 def
    0 1 masks length 1 sub {
        /m exch def
        /masksym rows cols mul array def
        0 1 rows cols mul 1 sub {
            /i exch def
            masksym i pixs i get masks m get i get xor put
        } for
        masks length 1 ne {  % Not rMQR nor user-specified mask
            format (full) eq {
                masksym evalfull /score exch def
            } {
                masksym evalmicro /score exch def
            } ifelse
            score bestscore lt {
                /bestsym masksym def
                /bestmaskval m def
                /bestscore score def
            } if
        } {
            /bestsym masksym def
        } ifelse
    } for
    /pixs bestsym def

    % Set the solitary dark module in full symbols
    format (full) eq {
        pixs 8 cols 8 sub qmv 1 put
    } if

    % Add the format information
    format (full) eq {
        /fmtvals [
            16#5412 16#5125 16#5e7c 16#5b4b 16#45f9 16#40ce 16#4f97 16#4aa0
            16#77c4 16#72f3 16#7daa 16#789d 16#662f 16#6318 16#6c41 16#6976
            16#1689 16#13be 16#1ce7 16#19d0 16#0762 16#0255 16#0d0c 16#083b
            16#355f 16#3068 16#3f31 16#3a06 16#24b4 16#2183 16#2eda 16#2bed
        ] def
        /ecid (MLHQ) eclevel search pop length exch pop exch pop def
        /fmtval fmtvals ecid 3 bitshift bestmaskval add get def
        0 1 formatmap length 1 sub {
            /i exch def
            formatmap i get {
                pixs exch aload pop qmv fmtval 14 i sub neg bitshift 1 and put
            } forall
        } for
    } if
    format (micro) eq {
        /fmtvals [
            16#4445 16#4172 16#4e2b 16#4b1c 16#55ae 16#5099 16#5fc0 16#5af7
            16#6793 16#62a4 16#6dfd 16#68ca 16#7678 16#734f 16#7c16 16#7921
            16#06de 16#03e9 16#0cb0 16#0987 16#1735 16#1202 16#1d5b 16#186c
            16#2508 16#203f 16#2f66 16#2a51 16#34e3 16#31d4 16#3e8d 16#3bba
        ] def
        /symid [ [0] [1 2] [3 4] [5 6 7] ] cols 11 sub 2 idiv get eclval get def
        /fmtval fmtvals symid 2 bitshift bestmaskval add get def
        0 1 formatmap length 1 sub {
            /i exch def
            pixs formatmap i get 0 get aload pop qmv fmtval 14 i sub neg bitshift 1 and put
        } for
    } if
    format (rmqr) eq {
        /fmtvals1 [
            16#1fab2 16#1e597 16#1dbdd 16#1c4f8 16#1b86c 16#1a749 16#19903 16#18626
            16#17f0e 16#1602b 16#15e61 16#14144 16#13dd0 16#122f5 16#11cbf 16#1039a
            16#0f1ca 16#0eeef 16#0d0a5 16#0cf80 16#0b314 16#0ac31 16#0927b 16#08d5e
            16#07476 16#06b53 16#05519 16#04a3c 16#036a8 16#0298d 16#017c7 16#008e2
            16#3f367 16#3ec42 16#3d208 16#3cd2d 16#3b1b9 16#3ae9c 16#390d6 16#38ff3
            16#376db 16#369fe 16#357b4 16#34891 16#33405 16#32b20 16#3156a 16#30a4f
            16#2f81f 16#2e73a 16#2d970 16#2c655 16#2bac1 16#2a5e4 16#29bae 16#2848b
            16#27da3 16#26286 16#25ccc 16#243e9 16#23f7d 16#22058 16#21e12 16#20137
        ] def
        /fmtvals2 [
            16#20a7b 16#2155e 16#22b14 16#23431 16#248a5 16#25780 16#269ca 16#276ef
            16#28fc7 16#290e2 16#2aea8 16#2b18d 16#2cd19 16#2d23c 16#2ec76 16#2f353
            16#30103 16#31e26 16#3206c 16#33f49 16#343dd 16#35cf8 16#362b2 16#37d97
            16#384bf 16#39b9a 16#3a5d0 16#3baf5 16#3c661 16#3d944 16#3e70e 16#3f82b
            16#003ae 16#01c8b 16#022c1 16#03de4 16#04170 16#05e55 16#0601f 16#07f3a
            16#08612 16#09937 16#0a77d 16#0b858 16#0c4cc 16#0dbe9 16#0e5a3 16#0fa86
            16#108d6 16#117f3 16#129b9 16#1369c 16#14a08 16#1552d 16#16b67 16#17442
            16#18d6a 16#1924f 16#1ac05 16#1b320 16#1cfb4 16#1d091 16#1eedb 16#1f1fe
        ] def
        /fmtvalu (MH) eclevel search pop length exch pop exch pop 5 bitshift verind add def
        /fmtval1 fmtvals1 fmtvalu get def
        /fmtval2 fmtvals2 fmtvalu get def
        0 1 formatmap length 1 sub {
            /i exch def
            pixs formatmap i get 0 get aload pop qmv fmtval1 17 i sub neg bitshift 1 and put
            pixs formatmap i get 1 get aload pop qmv fmtval2 17 i sub neg bitshift 1 and put
        } for
    } if

    % Add the version information
    format (full) eq cols 45 ge and {
        /vervals [
            16#07c94 16#085bc 16#09a99 16#0a4d3 16#0bbf6 16#0c762 16#0d847
            16#0e60d 16#0f928 16#10b78 16#1145d 16#12a17 16#13532 16#149a6
            16#15683 16#168c9 16#177ec 16#18ec4 16#191e1 16#1afab 16#1b08e
            16#1cc1a 16#1d33f 16#1ed75 16#1f250 16#209d5 16#216fd 16#228ba
            16#2379f 16#24b0b 16#2542e 16#26a64 16#27541 16#28c69
        ] def
        /verval vervals cols 17 sub 4 idiv 7 sub get def
        0 1 versionmap length 1 sub {
            /i exch def
            versionmap i get {
                pixs exch {} forall qmv verval 17 i sub neg bitshift 1 and put
            } forall
        } for
    } if

    % Return the arguments
    <<
    /ren //renmatrix
    /pixs pixs
    /pixx cols
    /pixy rows
    /height rows 2 mul 72 div
    /width cols 2 mul 72 div
    /opt options
    >>

    dontdraw not //renmatrix if

    end

}
[/barcode] {null def} forall
bind def
/qrcode dup load /uk.co.terryburton.bwipp defineresource pop
end
/setpacking where {pop setpacking} if
%%EndData
%%EndResource

%%BeginResource: uk.co.terryburton.bwipp swissqrcode 0.0 2020040100 57786 57647
%%BeginData:        126 ASCII Lines
/setpacking where {pop currentpacking true setpacking} if
1 dict
dup /raiseerror dup /uk.co.terryburton.bwipp findresource put
dup /renmatrix dup /uk.co.terryburton.bwipp findresource put
dup /qrcode dup /uk.co.terryburton.bwipp findresource put
begin
/swissqrcode {

    20 dict begin            % Confine variables to local scope

    /options exch def  % We are given an option string
    /barcode exch def  % We are given a barcode string

    /dontdraw false def

    /parse false def

    % Parse the input options
    options type /stringtype eq {
        1 dict begin
        options {
            token false eq {exit} if dup length string cvs (=) search
            true eq {cvlit exch pop exch def} {cvlit true def} ifelse
        } loop
        currentdict end /options exch def
    } if
    options {def} forall

    % Parse ordinals of the form ^NNN to ASCII
    parse {
        /msg barcode length string def
        /j 0 def
        barcode
        { % loop
            (^) search {
                dup msg exch j exch putinterval
                length j add 1 add /j exch def
                pop
                dup 0 3 getinterval cvi msg exch j 1 sub exch put
                dup length 3 sub 3 exch getinterval
            } {
                dup msg exch j exch putinterval
                length j add /j exch def
                /barcode msg 0 j getinterval def
                exit
            } ifelse
        } loop
    } if

    /barlen barcode length def

    % Validate the input length
    barcode length 997 gt {
        /bwipp.swissqrcodeBadLength (Swiss QR Code input must not exceed 997 digits) //raiseerror exec
    } if

    % Get the result of encoding with qrcode
    options (dontdraw) true put
    /args barcode options //qrcode exec def

    args (opt) options put
    args

    % The 7mm-wide Swiss Cross is defined by the specification as being an
    % overlaid image, i.e. not made out of "modules", so very likely a
    % different pitch. It cannot therefore be represented by our standard image
    % dictionary and would be a nightmare to gridfit. The application assumes a
    % sufficient high-resolution print process that such issues do not matter.
    %
    % So for now we simply do as we're told and paint over the top of the
    % barcode image that has been scaled to 46mm, user be damned!
    %
    dontdraw not {

        gsave
        currentpoint translate
        72 25.4 div dup scale  % pt to mm

        % Clipping path with 7mm hole for the Swiss Cross
        gsave
        newpath
         0  0 moveto
        46  0 lineto
        46 46 lineto
         0 46 lineto
        closepath
        19.5 19.5 moveto
        19.5 26.5 lineto
        26.5 26.5 lineto
        26.5 19.5 lineto
        closepath
        clip

        % Scale the QR Code to fit within 46mm
        gsave
        0 0 moveto
        46 args (pixx) get div 2 div dup scale
        //renmatrix exec
        grestore

        grestore  % Clipping

        % Draw Swiss Cross
        19.5 dup translate
        7 83 div dup scale

        newpath
         6  6 moveto
         6 77 lineto
        77 77 lineto
        77  6 lineto
        closepath
        49 18 moveto
        49 34 lineto
        65 34 lineto
        65 49 lineto
        49 49 lineto
        49 65 lineto
        34 65 lineto
        34 49 lineto
        18 49 lineto
        18 34 lineto
        34 34 lineto
        34 18 lineto
        closepath
        0 0 0 setrgbcolor fill

        grestore

    } if

    end

}
[/barcode] {null def} forall
bind def
/swissqrcode dup load /uk.co.terryburton.bwipp defineresource pop
end
/setpacking where {pop setpacking} if
%%EndData
%%EndResource
%%EndProlog
%%Page: 1 1
gsave
1 1 translate
2 2 scale
0 0  moveto
(SPC
0200
1
CH5800791123000889012
S
Robert Schneider AG
Rue du Lac
1268
2501
Biel
CH







199.95
CHF
K
Pia-Maria Rutschmann-Schnyder
Grosse Marktgasse 28
9400 Rorschach


CH
SCOR
RF18539007547034

EPD)
<>
/swissqrcode /uk.co.terryburton.bwipp findresource exec
grestore
showpage
%%Trailer
%%EOF