1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23\startluacode
24
25moduledata.zint = { }
26
27local format, lower, gsub = string.format, string.lower, string.gsub
28local patterns = lpeg.patterns
29
30local zint = "zint"
31local defaultcode = "PDF417"
32
33local whitespace = patterns.whitespace
34local spaces = whitespace^0
35local key = (spaces / "") * patterns.digit^0 * (patterns.colon * spaces / "")
36local value = (whitespace / "" + (1 - key))^1
37local pattern = lpeg.Cf(lpeg.Ct("") * (lpeg.Cg((lpeg.Cs(key) / tonumber) * (lpeg.Cs(value) / lower)) + patterns.anything)^0,rawset)
38
39local reverse
40
41local function cleancode(code)
42 if not code or code == "" then
43 code = defaultcode
44 end
45 return lower(gsub(code," ",""))
46end
47
48local function numberofcode(code)
49 if not reverse then
50 local types = os.resultof(format("%s --types",zint)) or ""
51 local formats = lpeg.match(pattern,types)
52 if not formats or not next(formats) then
53 return code
54 end
55 reverse = table.swapped(formats) or { }
56 end
57 code = cleancode(code)
58 return reverse[code] or code
59end
60
61function moduledata.zint.generate(code,data,suffix,options)
62 if not data or data == "" then
63 data = "unset"
64 end
65 local code = cleancode(code)
66 local base = format("zint-%s-%s",code,md5.hex(data))
67 local name = file.addsuffix(base,suffix or "eps")
68 if not lfs.isfile(name) then
69 local temp = file.addsuffix(base,"tmp")
70 local code = numberofcode(code)
71 io.savedata(temp,data)
72 local command = format('%s --barcode=%s %s --output="%s" --input="%s"',zint,code,options or "",name,temp)
73 logs.simple("using 'zint' to generate '%s'",base)
74 logs.simple("command '%s'",command)
75 print(command)
76 os.execute(command)
77 os.remove(temp)
78 end
79 return name
80end
81
82\stopluacode
83
84\unprotect
85
86\unexpanded\def\barcode[#1]
87 {\bgroup
88 \getdummyparameters
89 [\c!alternative=,\c!text=,\c!suffix=eps,\c!option=,#1]
90 \externalfigure
91 [\cldcontext{moduledata.zint.generate("\dummyparameter\c!alternative",\!!bs\dummyparameter\c!text\!!es,"\dummyparameter\c!suffix","\dummyparameter\c!option")}]
92 [#1,\c!option=,\c!alternative=,\c!text=]
93 \egroup}
94
95\protect
96
97\continueifinputfile{mzint.mkiv}
98
99\starttext
100
101
102
103
104
105
106
107
108
109
110
111
112 \barcode[alternative=maxicode,text=whatever,width=3cm,suffix=png]
113\stoptext
114
115
116 |