% engine=luatex

%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.

\writestatus{barcodes}{the 'zint' module is a better choice}

\usemodule[zint]

\let\normalbarcode\barcode

\unexpanded\def\barcode[#1]%
  {\normalbarcode[\c!text=\dummyparameter\c!code,\c!alternative=\dummyparameter\c!type,#1]}

%D We keep the following for historic reasons to show progress over years. The
%D original idea was to use escrito fro the code below but it never happened as
%D we went zint. I'll look into that when users really want to use pstricks but I
%D never had requests and the chemical mpoduel also moved on.

\iffalse

% \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

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

\fi

\continueifinputfile{m-barcodes.mkiv}

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

