summaryrefslogtreecommitdiff
path: root/tex/context/modules/mkiv/m-zint.mkiv
blob: 87e0d382c5bc5345ff465f76bb37c971dc2359c9 (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
%D \module
%D   [       file=m-zint,
%D        version=2010.12.07,
%D          title=\CONTEXT\ Extra Modules,
%D       subtitle=Zint Barcode Generator,
%D         author=Hans Hagen,
%D           date=\currentdate,
%D      copyright={PRAGMA ADE \& \CONTEXT\ Development Team}]
%C
%C This module is part of the \CONTEXT\ macro||package and is
%C therefore copyrighted by \PRAGMA. See mreadme.pdf for
%C details.

%D Using \type {zint} seems to be the easiest way to generate (PDF417) barcodes so
%D therefore we now have this module. There are proper (also windows) binaries at:
%D
%D \starttyping
%D http://www.zint.org.uk
%D \stoptyping
%D
%D There is a bit more code than needed as we want to be able to feed names.

\startluacode

moduledata.zint = { }

local format, lower, gsub  = string.format, string.lower, string.gsub
local patterns = lpeg.patterns

local zint        = "zint" -- '"c:/program files/zint/zint.exe"'
local defaultcode = "PDF417"

local whitespace  = patterns.whitespace
local spaces      = whitespace^0
local key         = (spaces / "") * patterns.digit^0 * (patterns.colon * spaces / "")
local value       = (whitespace / "" + (1 - key))^1
local pattern     = lpeg.Cf(lpeg.Ct("") * (lpeg.Cg((lpeg.Cs(key) / tonumber) * (lpeg.Cs(value) / lower)) + patterns.anything)^0,rawset)

local reverse

local function cleancode(code)
    if not code or code == "" then
        code = defaultcode
    end
    return lower(gsub(code," ",""))
end

local function numberofcode(code)
    if not reverse then
        local types = os.resultof(format("%s --types",zint)) or ""
        local formats = lpeg.match(pattern,types)
        if not formats or not next(formats) then
            return code
        end
        reverse = table.swapped(formats) or { }
    end
    code = cleancode(code)
    return reverse[code] or code
end

function moduledata.zint.generate(code,data,suffix,options)
    if not data or data == "" then
        data = "unset"
    end
    local code = cleancode(code)
    local base = format("zint-%s-%s",code,md5.hex(data))
    local name = file.addsuffix(base,suffix or "eps")
    if not lfs.isfile(name) then
        local temp = file.addsuffix(base,"tmp")
        local code = numberofcode(code)
        io.savedata(temp,data)
        local command = format('%s --barcode=%s %s --output="%s" --input="%s"',zint,code,options or "",name,temp)
        logs.simple("using 'zint' to generate '%s'",base)
        logs.simple("command '%s'",command)
        print(command)
        os.execute(command)
        os.remove(temp)
    end
    return name
end

\stopluacode

\unprotect

\unexpanded\def\barcode[#1]% [alternative=,text=]
  {\bgroup
   \getdummyparameters
     [\c!alternative=,\c!text=,\c!suffix=eps,\c!option=,#1]%
   \externalfigure
     [\cldcontext{moduledata.zint.generate("\dummyparameter\c!alternative",\!!bs\dummyparameter\c!text\!!es,"\dummyparameter\c!suffix","\dummyparameter\c!option")}]%
     [#1,\c!option=,\c!alternative=,\c!text=]%
   \egroup}

\protect

\continueifinputfile{m-zint.mkiv}

\starttext

%     \externalfigure[\cldcontext{moduledata.zint.generate("PDF417",[[Hans Hagen]])}]
%     \blank
%     \externalfigure[\cldcontext{moduledata.zint.generate("PDF417","Ton Otten")}]
%     \blank
%     \externalfigure[\cldcontext{moduledata.zint.generate("ISBN","9789490688011")}]
%     \blank
%     \barcode[text=Does It Work?,width=\textwidth]
%     \blank
%     \barcode[alternative=isbn,text=9789490688011,width=3cm]
%     \blank
    \barcode[alternative=datamatrix,text=whatever,width=3cm,option=--square]

\stoptext