1if not modules then modules = { } end modules [ ' libs-imp-ghostscript ' ] = {
2 version = 1 . 001 ,
3 comment = " companion to luat-lib.mkxl " ,
4 author = " Hans Hagen, PRAGMA-ADE, Hasselt NL " ,
5 copyright = " PRAGMA ADE / ConTeXt Development Team " ,
6 license = " see context related readme files " ,
7}
8
9local libname = " zint "
10local libfile = " libzint "
11
12
13local zintlib = resolvers . libraries . validoptional ( libname )
14
15if not zintlib then return end
16
17local function okay ( )
18 if resolvers . libraries . optionalloaded ( libname , libfile ) then
19 okay = function ( ) return true end
20 else
21 okay = function ( ) return false end
22 end
23 return okay ( )
24end
25
26local zint = utilities . zint or { }
27utilities . zint = zint
28
29local zintlib_execute = zintlib . execute
30
31local next , type , unpack = next , type , unpack
32local lower , gsub = string . lower , string . gsub
33
34local mapping = {
35 [ " code 11 " ] = 1 , [ " pharma one-track " ] = 51 , [ " aztec code " ] = 92 ,
36 [ " standard 2of5 " ] = 2 , [ " pzn " ] = 52 , [ " daft code " ] = 93 ,
37 [ " interleaved 2of5 " ] = 3 , [ " pharma two-track " ] = 53 , [ " micro qr code " ] = 97 ,
38 [ " iata 2of5 " ] = 4 , [ " pdf417 " ] = 55 , [ " hibc code 128 " ] = 98 ,
39 [ " data logic " ] = 6 , [ " pdf417 trunc " ] = 56 , [ " hibc code 39 " ] = 99 ,
40 [ " industrial 2of5 " ] = 7 , [ " maxicode " ] = 57 , [ " hibc data matrix " ] = 102 ,
41 [ " code 39 " ] = 8 , [ " qr code " ] = 58 , [ " hibc qr code " ] = 104 ,
42 [ " extended code 39 " ] = 9 , [ " code 128-b " ] = 60 , [ " hibc pdf417 " ] = 106 ,
43 [ " ean " ] = 13 , [ " ap standard customer " ] = 63 , [ " hibc micropdf417 " ] = 108 ,
44 [ " ean + check " ] = 14 , [ " ap reply paid " ] = 66 , [ " hibc codablock-f " ] = 110 ,
45 [ " gs1-128 " ] = 16 , [ " ap routing " ] = 67 , [ " hibc aztec code " ] = 112 ,
46 [ " codabar " ] = 18 , [ " ap redirection " ] = 68 , [ " dotcode " ] = 115 ,
47 [ " code 128 " ] = 20 , [ " isbn " ] = 69 , [ " han xin code " ] = 116 ,
48 [ " leitcode " ] = 21 , [ " rm4scc " ] = 70 , [ " rm mailmark " ] = 121 ,
49 [ " identcode " ] = 22 , [ " data matrix " ] = 71 , [ " aztec runes " ] = 128 ,
50 [ " code 16k " ] = 23 , [ " ean-14 " ] = 72 , [ " code 32 " ] = 129 ,
51 [ " code 49 " ] = 24 , [ " vin (north america) " ] = 73 , [ " comp ean " ] = 130 ,
52 [ " code 93 " ] = 25 , [ " codablock-f " ] = 74 , [ " comp gs1-128 " ] = 131 ,
53 [ " flattermarken " ] = 28 , [ " nve-18 " ] = 75 , [ " comp databar omni " ] = 132 ,
54 [ " gs1 databar omni " ] = 29 , [ " japanese post " ] = 76 , [ " comp databar ltd " ] = 133 ,
55 [ " gs1 databar ltd " ] = 30 , [ " korea post " ] = 77 , [ " comp databar expom " ] = 134 ,
56 [ " gs1 databar expom " ] = 31 , [ " gs1 databar stack " ] = 79 , [ " comp upc-a " ] = 135 ,
57 [ " telepen alpha " ] = 32 , [ " gs1 databar stack omni " ] = 80 , [ " comp upc-e " ] = 136 ,
58 [ " upc-a " ] = 34 , [ " gs1 databar eso " ] = 81 , [ " comp databar stack " ] = 137 ,
59 [ " upc-a + check " ] = 35 , [ " planet " ] = 82 , [ " comp databar stack omni " ] = 138 ,
60 [ " upc-e " ] = 37 , [ " micropdf " ] = 84 , [ " comp databar eso " ] = 139 ,
61 [ " upc-e + check " ] = 38 , [ " usps onecode " ] = 85 , [ " channel code " ] = 140 ,
62 [ " postnet " ] = 40 , [ " uk plessey " ] = 86 , [ " code one " ] = 141 ,
63 [ " msi plessey " ] = 47 , [ " telepen numeric " ] = 87 , [ " grid matrix " ] = 142 ,
64 [ " fim " ] = 49 , [ " itf-14 " ] = 89 , [ " upnqr " ] = 143 ,
65 [ " logmars " ] = 50 , [ " kix code " ] = 90 , [ " rmqr " ] = 145 ,
66}
67
68table . setmetatableindex ( mapping , function ( t , k )
69 local s = gsub ( lower ( k ) , " [^a-z0-9] " , " " )
70 local v = rawget ( t , s ) or false
71 t [ k ] = v
72 return v
73end )
74
75local report = logs . reporter ( " zint " )
76local context = context
77local shown = false
78
79
80
81local function execute ( specification )
82 if okay ( ) then
83 local code = specification . code
84 local text = specification . text
85 if code then
86 local id = mapping [ code ]
87 if id then
88 specification . code = id
89 local result = zintlib_execute ( specification )
90 if result then
91
92 context . startMPcode ( )
93 local rectangles = result . rectangles
94 local hexagons = result . hexagons
95 local circles = result . circles
96 local strings = result . strings
97 if rectangles then
98 local n = # rectangles
99 for i = 1 , n do
100 local r = rectangles [ i ]
101 context ( " %sofill unitsquare xysized (%N,%N) shifted (%N,%N); " ,
102 i = = n and " d " or " n " , r [ 3 ] , r [ 4 ] , r [ 1 ] , r [ 2 ] )
103
104 end
105
106 end
107 if hexagons then
108 local n = # hexagons
109 for i = 1 , # hexagons do
110 context ( " %sofill (%N,%N)--(%N,%N)--(%N,%N)--(%N,%N)--(%N,%N)--(%N,%N)--cycle; " ,
111 i = = n and " d " or " n " , unpack ( hexagons [ i ] ) )
112 end
113 end
114 if circles then
115 local n = # circles
116 for i = 1 , # circles do
117 local c = circles [ i ]
118 context ( " %sofill unitcircle scaled %N shifted (%N,%N); " ,
119 i = = n and " d " or " n " , c [ 3 ] , c [ 1 ] , c [ 2 ] )
120 end
121 end
122 if strings then
123
124 for i = 1 , # strings do
125 local s = strings [ i ]
126 context ( ' draw textext("%s") scaled (%N/10) shifted (%N,%N); ' ,
127 s [ 4 ] , s [ 3 ] , s [ 1 ] , s [ 2 ] )
128 end
129 end
130 context . stopMPcode ( )
131 end
132 else
133 report ( " unknown barcode alternative %a " , code )
134 if not shown then
135 report ( " " )
136 report ( " valid barcode alternatives: " )
137 report ( " " )
138 local list = table . sortedkeys ( mapping )
139 for i = 1 , # list do
140 report ( " %s " , list [ i ] )
141 end
142 report ( " " )
143 shown = true
144 end
145 end
146 end
147 end
148end
149
150optional . loaded . zint = { execute = execute }
151
152interfaces . implement {
153 name = " zint " ,
154 actions = execute ,
155 arguments = {
156 {
157 { " code " } ,
158 { " text " } ,
159 }
160 }
161}
162 |