m-zint.mkiv /size: 3280 b    last modification: 2021-10-28 13:51
1%D \module
2%D   [       file=m-zint,
3%D        version=2010.12.07,
4%D          title=\CONTEXT\ Extra Modules,
5%D       subtitle=Zint Barcode Generator,
6%D         author=Hans Hagen,
7%D           date=\currentdate,
8%D      copyright={PRAGMA ADE \& \CONTEXT\ Development Team}]
9%C
10%C This module is part of the \CONTEXT\ macro||package and is
11%C therefore copyrighted by \PRAGMA. See mreadme.pdf for
12%C details.
13
14%D Using \type {zint} seems to be the easiest way to generate (PDF417) barcodes so
15%D therefore we now have this module. There are proper (also windows) binaries at:
16%D
17%D \starttyping
18%D http://www.zint.org.uk
19%D \stoptyping
20%D
21%D There is a bit more code than needed as we want to be able to feed names.
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" -- '"c:/program files/zint/zint.exe"'
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        logs.simple("using 'zint' to generate '%s'",base)
72        io.savedata(temp,data)
73        os.execute(format('%s --barcode=%s --output="%s" --input="%s" %s',zint,code,name,temp,options or ""))
74        os.remove(temp)
75    end
76    return name
77end
78
79\stopluacode
80
81\unprotect
82
83\unexpanded\def\barcode[#1]% [alternative=,text=]
84  {\bgroup
85   \getdummyparameters
86     [\c!alternative=,\c!text=,#1]%
87   \externalfigure
88     [\cldcontext{moduledata.zint.generate("\dummyparameter\c!alternative",\!!bs\dummyparameter\c!text\!!es)}]%
89     [#1,\c!alternative=,\c!text=]%
90   \egroup}
91
92\protect
93
94\continueifinputfile{m-zint.mkiv}
95
96\starttext
97
98    \externalfigure[\cldcontext{moduledata.zint.generate("PDF417",[[Hans Hagen]])}]
99    \blank
100    \externalfigure[\cldcontext{moduledata.zint.generate("PDF417","Ton Otten")}]
101    \blank
102    \externalfigure[\cldcontext{moduledata.zint.generate("ISBN","9789490688011")}]
103    \blank
104    \barcode[text=Does It Work?,width=\textwidth]
105    \blank
106    \barcode[alternative=isbn,text=9789490688011,width=3cm]
107
108\stoptext
109
110
111