m-zint.mkiv /size: 3659 b    last modification: 2023-12-21 09:45
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        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]% [alternative=,text=]
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{m-zint.mkiv}
98
99\starttext
100
101%     \externalfigure[\cldcontext{moduledata.zint.generate("PDF417",[[Hans Hagen]])}]
102%     \blank
103%     \externalfigure[\cldcontext{moduledata.zint.generate("PDF417","Ton Otten")}]
104%     \blank
105%     \externalfigure[\cldcontext{moduledata.zint.generate("ISBN","9789490688011")}]
106%     \blank
107%     \barcode[text=Does It Work?,width=\textwidth]
108%     \blank
109%     \barcode[alternative=isbn,text=9789490688011,width=3cm]
110%     \blank
111%     \barcode[alternative=datamatrix,text=whatever,width=3cm,option=--square,suffix=png]
112    \barcode[alternative=maxicode,text=whatever,width=3cm,suffix=png]
113\stoptext
114
115
116