m-barcodes.mkiv /size: 3822 b    last modification: 2021-10-28 13:51
1% engine=luatex
2
3%D \module
4%D   [       file=m-barcodes,
5%D        version=2010.03.14,
6%D          title=\CONTEXT\ Extra Modules,
7%D       subtitle=Barcodes,
8%D         author=Hans Hagen,
9%D           date=\currentdate,
10%D      copyright={PRAGMA ADE \& \CONTEXT\ Development Team}]
11%C
12%C This module is part of the \CONTEXT\ macro||package and is
13%C therefore copyrighted by \PRAGMA. See mreadme.pdf for
14%C details.
15
16\writestatus{barcodes}{the 'zint' module is a better choice}
17
18\usemodule[zint]
19
20\let\normalbarcode\barcode
21
22\unexpanded\def\barcode[#1]%
23  {\normalbarcode[\c!text=\dummyparameter\c!code,\c!alternative=\dummyparameter\c!type,#1]}
24
25%D We keep the following for historic reasons to show progress over years. The
26%D original idea was to use escrito fro the code below but it never happened as
27%D we went zint. I'll look into that when users really want to use pstricks but I
28%D never had requests and the chemical mpoduel also moved on.
29
30\iffalse
31
32% \startTEXpage
33%     \startPSTRICKS
34%         \pspicture(-4mm,-1mm)(38mm,26mm)
35%             \psbarcode{9781860742712}{includetext guardwhitespace}{ean13}%
36%         \endpspicture
37%     \stopPSTRICKS
38% \stopTEXpage
39
40% 978-94-90688-01-1
41%
42%   978 = ean isbn identifier (979 also)
43%    94 = country code
44% 90688 = publisher code
45%    01 = title 1
46%     1 = checksum
47
48\usemodule[pstricks]
49
50\usePSTRICKSmodule[pst-barcode]
51
52\definefont[barcodefont][file:ocrb10]
53% \definefont[barcodefont][file:texgyreheros-regular]
54
55\startluacode
56moduledata.barcodes = { }
57
58local function split(code)
59    local t = { string.byte(code,1,#code) }
60    if #t >= 12 then
61        local s = 0
62        for i=1,11,2 do
63            s = s + (t[i]-48)
64        end
65        for i=2,12,2 do
66            s = s + 3 * (t[i]-48)
67        end
68        local m = s % 10
69        local c = (m > 0 and (10 - m)) or 0
70        return t, s, m, c
71    end
72end
73
74function moduledata.barcodes.isbn_1(original)
75    local code = string.gsub(original,"%-","")
76    local t, s, m, c = split(code)
77    if t then
78        if #t == 13 then
79            local e = ((c == t[13] - 48) and "correct") or "wrong"
80            logs.report("isbn code","code=%s, sum=%s, checksum=%s, modulo=%s, status=%s",original,s,m,c,e)
81        else
82            logs.report("isbn code","code=%s, sum=%s, checksum=%s, modulo=%s",original,s,m,c)
83            code= code .. c
84        end
85    end
86    context(code)
87end
88
89function moduledata.barcodes.isbn_2(original)
90    local code = string.gsub(original,"%-","")
91    local t, s, m, c = split(code)
92    if t and #t == 12 then
93        original = original .. "-" .. c
94    end
95    context(original)
96end
97\stopluacode
98
99\startsetups barcode:isbn
100    \scale
101        [width=5cm]
102        {
103            \vbox {
104                \hbox {
105                    \hskip3.7mm
106                    \scale[width=34mm]{\barcodefont ISBN \ctxlua{moduledata.barcodes.isbn_2("\getvariable{barcode}{code}")}}
107                }
108                \par
109                \normalexpanded { \noexpand \setPSTRICKS {
110                    \noexpand \pspicture(-4mm,-1mm)(38mm,26mm)
111                        \noexpand \psbarcode {
112                            \ctxlua{moduledata.barcodes.isbn_1("\getvariable{barcode}{code}")}
113                        } {
114                            includetext guardwhitespace
115                        } {
116                            ean13
117                        }
118                    \noexpand \endpspicture
119                }
120                \noexpand \processPSTRICKS }
121            }
122        }
123\stopsetups
124
125\unexpanded\def\barcode[#1]%
126  {\bgroup
127   \setvariables[barcode][type=isbn,#1]%
128   \directsetup{barcode:\getvariable{barcode}{type}}%
129   \egroup}
130
131\fi
132
133\continueifinputfile{m-barcodes.mkiv}
134
135\starttext
136    \startTEXpage
137        \barcode[type=isbn,code=978-94-90688-01-1]
138    \stopTEXpage
139\stoptext
140
141