
% This optional plugin (see ptx-plugins) provides ptx2pdf users with 
% access to QR codes. The actual work is done by contrib/QRcode.tex
%
% Copyright (c) 2023 by SIL International written by David Gardner
%
%
% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,  
% EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF  
% MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND  
% NONINFRINGEMENT. IN NO EVENT SHALL SIL INTERNATIONAL BE LIABLE FOR  
% ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF  
% CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION  
% WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
%
% Except as contained in this notice, the name of SIL International  
% shall not be used in advertising or otherwise to promote the sale,  
% use or other dealings in this Software without prior written  
% authorization from SIL International.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\makeatletter
\newwrite\QRtmpfile
\plugin@startif{QR}

\input QRcode.tex
\QRdimen=2cm
\def\QRref{\s@tf@rstr@f{\p@gefirstmark}\ifx\@@firstref\empty\else\x@\QRdata\x@! \@@firstref!\QRcode\box\QRcodebox\fi}

\x@\def\csname MS:zqrcode\endcsname{%
  \bgroup\everypar{}%
    \let\pic@lign\relax%
    \get@ttribute{cat}%
    \ifx\attr@b\relax\else\x@\s@tc@tprefix\x@{\attr@b}\fi
    \get@ttribute{size}%
    \ifx\attr@b\relax
      \getp@ram{fontsize}{zqrcode}{\styst@k}\ifx\p@ram\relax\else\QRdimen=\p@ram\FontSizeUnit\fi
    \else
      \m@kenumber{\attr@b}\QRdimen=\@@result
    \fi
    \get@ttribute{vmax}%
    \ifx\attr@b\relax\else\m@kenumber{\attr@b}\let\QRmaxVersion\@@result\fi
    \get@ttribute{vmin}%
    \ifx\attr@b\relax\else\m@kenumber{\attr@b}\let\QRminVersion\@@result\fi
    \get@ttribute{unicode}%
    \ifx\attr@b\tr@e \temptrue \else\tempfalse\fi
    \get@ttribute{data}%
    \bgroup 
      \deactiv@tecustomch@rs
      \catcode`\/=12
      \iftemp 
        \trace{m}{QRcode writing temp file QRtmp.tex}%
        \immediate\openout\QRtmpfile="QRtmp.tex"\relax
        \immediate\write\QRtmpfile{\string\XeTeXinputencoding "bytes"}%
        \immediate\write\QRtmpfile{\string\QRdata"\attr@b"}%
        \immediate\write\QRtmpfile{\string\endinput}%
        \immediate\closeout\QRtmpfile
        \input "QRtmp.tex"\relax%
      \else
        \x@\m@kenumb@rE\attr@b\END%
        \trace{m}{QRcode rescanned as '\@@result'}%
        \x@\QRdata"\@@result"\relax%
      \fi
      \QRcode%
      \trace{m}{QR code is \the\wd\QRcodebox x\the\ht\QRcodebox, req \the\QRbounding/\the\QRinside}%
      \xdef\QR@inside{\the\QRinside}%
      \dimen0=0.5\QRinside
      \global\setbox\QRcodebox\hbox{\kern-\dimen0\box\QRcodebox\kern\dimen0}%
    \egroup
    \getp@ram{color}{zqrcode}{\styst@k}%
    \get@ttribute{color}\ifx\attr@b\relax\else\let\p@ram\attr@b\fi
    \get@ttribute{colour}\ifx\attr@b\relax\else\let\p@ram\attr@b\fi
    \ifx\p@ram\relax\else\setfgc@l{\p@ram}\setbox\QRcodebox\hbox{\fgc@l\box\QRcodebox\endfgc@l}\fi
    \getp@ram{background}{zqrcode}{\styst@k}%
    \get@ttribute{background}\ifx\attr@b\relax\else\let\p@ram\attr@b\fi
    \ifx\p@ram\relax\else\setbgc@l{\p@ram}\setbox\QRcodebox\hbox{\bgc@l\vrule depth 0pt height \QRbounding width \QRbounding\endbgc@l\kern -\QRbounding\box\QRcodebox}\fi
    \get@ttribute{spacebeside}%
    \ifx\attr@b\relax\getp@ram{spacebeside}{zqrcode}{\styst@k}%
      \ifx\p@ram\relax\def\sp@cebeside{0pt}\else
        \edef\sp@cebeside{\p@ram pt}%
      \fi
    \else
      \m@kenumber{\attr@b}\let\sp@cebeside\@@result
    \fi
    \dimen0=\QRbounding
    \advance\dimen0 -\QR@inside
    \divide\dimen0 by 2
    \get@ttribute{novpadding}%
    \ifx\attr@b\tr@e 
      \trace{m}{QR vbox adjustment to \QR@inside as requested}%
      \setbox\QRcodebox\vbox to \QR@inside{\vss\box\QRcodebox\vss}%
    \fi
    \get@ttribute{nohpadding}%
    \ifx\attr@b\tr@e 
      \trace{m}{QR hbox adjustment by \the\QRbounding - \QR@inside\space as requested}%
      \setbox\QRcodebox\hbox{\kern -\dimen0\box\QRcodebox\kern-\dimen0}%
    \else\ifx\attr@b\@lignLeft
        \setbox\QRcodebox\hbox{\kern -\dimen0\box\QRcodebox}%
      \else\ifx\attr@b\@lignRight
          \setbox\QRcodebox\hbox{\box\QRcodebox\kern-\dimen0}%
        \fi
      \fi
    \fi
    \dimen0=\QRbounding
    \get@ttribute{pgpos}%
    \ifx\attr@b\relax \box\QRcodebox\else
      \m@kenumberns{\attr@b}\edef\attr@b{\@@result}%
      \ifx\attr@b\loc@InText\box\QRcodebox
      \else
        \ifcsname ins-\attr@b\endcsname
          \x@\let\x@\p@cins\csname ins-\attr@b\endcsname
          \picUsesInstrue
        \else
          \picUsesInsfalse
          \edef\hs@ze{\the\hsize}%
          \def\w@tsit{QRcode}%
          \x@\p@rsePicUse\attr@b\end
        \fi
        \setbox0\box\QRcodebox
        \figh@scaptionfalse
        \pl@cefigure
      \fi
    \fi
  \egroup  
}
\plugin@endif
