summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/tex/context/base/m-barcodes.mkiv
blob: 0282f706dc08308d2597730f63067b257c93595c (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
116
117
118
119
120
121
122
123
124
%D \module
%D   [       file=m-barcodes,
%D        version=2010.03.14,
%D          title=\CONTEXT\ Extra Modules,
%D       subtitle=Barcodes,
%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 You can best use m-zint instead of this one.

% \startTEXpage
%     \startPSTRICKS
%         \pspicture(-4mm,-1mm)(38mm,26mm)
%             \psbarcode{9781860742712}{includetext guardwhitespace}{ean13}%
%         \endpspicture
%     \stopPSTRICKS
% \stopTEXpage

% 978-94-90688-01-1
%
%   978 = ean isbn identifier (979 also)
%    94 = country code
% 90688 = publisher code
%    01 = title 1
%     1 = checksum

\usemodule[pstricks]

\usePSTRICKSmodule[pst-barcode]

\definefont[barcodefont][file:ocrb10]
% \definefont[barcodefont][file:texgyreheros-regular]

\startluacode
moduledata.barcodes = { }

local function split(code)
    local t = { string.byte(code,1,#code) }
    if #t >= 12 then
        local s = 0
        for i=1,11,2 do
            s = s + (t[i]-48)
        end
        for i=2,12,2 do
            s = s + 3 * (t[i]-48)
        end
        local m = s % 10
        local c = (m > 0 and (10 - m)) or 0
        return t, s, m, c
    end
end

function moduledata.barcodes.isbn_1(original)
    local code = string.gsub(original,"%-","")
    local t, s, m, c = split(code)
    if t then
        if #t == 13 then
            local e = ((c == t[13] - 48) and "correct") or "wrong"
            logs.report("isbn code","code=%s, sum=%s, checksum=%s, modulo=%s, status=%s",original,s,m,c,e)
        else
            logs.report("isbn code","code=%s, sum=%s, checksum=%s, modulo=%s",original,s,m,c)
            code= code .. c
        end
    end
    context(code)
end

function moduledata.barcodes.isbn_2(original)
    local code = string.gsub(original,"%-","")
    local t, s, m, c = split(code)
    if t and #t == 12 then
        original = original .. "-" .. c
    end
    context(original)
end
\stopluacode

\startsetups barcode:isbn
    \scale
        [width=5cm]
        {
            \vbox {
                \hbox {
                    \hskip3.7mm
                    \scale[width=34mm]{\barcodefont ISBN \ctxlua{moduledata.barcodes.isbn_2("\getvariable{barcode}{code}")}}
                }
                \par
                \normalexpanded { \noexpand \setPSTRICKS {
                    \noexpand \pspicture(-4mm,-1mm)(38mm,26mm)
                        \noexpand \psbarcode {
                            \ctxlua{moduledata.barcodes.isbn_1("\getvariable{barcode}{code}")}
                        } {
                            includetext guardwhitespace
                        } {
                            ean13
                        }
                    \noexpand \endpspicture
                }
                \noexpand \processPSTRICKS }
            }
        }
\stopsetups

\def\barcode[#1]%
  {\bgroup
   \setvariables[barcode][type=isbn,#1]%
   \directsetup{barcode:\getvariable{barcode}{type}}%
   \egroup}

% \usemodule[barcodes]

\doifnotmode{demo}{\endinput}

\starttext
    \startTEXpage
        \barcode[type=isbn,code=978-94-90688-01-1]
    \stopTEXpage
\stoptext