% $Id: luagraphlib.tex 19 2021-02-22 20:40:11Z reinhard $

% Copyright (C) 2020 Reinhard Kotucha <reinhard.kotucha@web.de>
% 
% This program is free software; you can redistribute it and/or
% modify it under the terms of the GNU General Public License
% as published by the Free Software Foundation; either version 2
% of the License, or (at your option) any later version.
% 
% This program is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
% GNU General Public License for more details.
% 
% You should have received a copy of the GNU General Public License
% along with this program; if not, write to the Free Software
% Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
% MA 02110-1301, USA.

%%\batchmode

\newdimen\canvasrule \canvasrule=.1pt

% Define \pdfcomment.  The catcode of the percent character must be 12.
\bgroup\catcode`\%=12
  \gdef\pdfcomment#1{\pdfextension literal{%% #1}}
\egroup

\catcode`\@=11

\unless\ifdefined\documentclass
\newdimen\@tempdima
\newdimen\@tempdimb
\long\def\@ifnextchar#1#2#3{% stolen from LaTeX kernel
  \let\reserved@d=#1%
  \def\reserved@a{#2}%
  \def\reserved@b{#3}%
  \futurelet\@let@token\@ifnch}
\def\@ifnch{%
  \ifx\@let@token\reserved@d
    \expandafter\reserved@a
  \else
    \expandafter\reserved@b
  \fi}
\fi


\def\luagraphic#1{\pdfextension save\relax
  \pdfextension setmatrix{2.8346 0 0 2.8346}% 72bp/25.4mm
  \directlua{#1}\pdfextension restore}%


% The canvas environment is similar to the LaTeX picture environment.
% However, there is no concept of \unitlength.  Values in arguments
% must have units.  Thus you can use variables like \textwidth or
% \columnwidth as arguments.  Furthermore it's possible to use
% expressions (e-TeX's \dimexpr).

\def\canvas(#1,#2){%
  \@tempdima=\dimexpr#1\relax
  \@tempdimb=\dimexpr#2\relax

  \ifdefined\unitlength \unitlength=1mm \fi

  \def\clap##1{\hbox to0pt{\hss##1\hss}}%

  \def\luastrut##1{\bgroup
    \setbox0\hbox{##1}%
    \vrule width 0pt height\ht0 depth\dp0\relax
    \egroup}%

  \def\luaframedbox##1##2##3{\bgroup
    \setbox0\hbox{\luastrut{##2}##1}%
    \directlua{framedbox('\the\wd0','\the\ht0','\the\dp0','##3')}%
    \egroup}%

  
  \def\hline##1{\pdfcomment{canvas-hline}%
     \hbox to 0pt{\kern-\the\dimexpr \canvasrule/2\relax
     \vbox to 0pt{\vss
       \hrule
          width \the\dimexpr ##1 + \canvasrule\relax
          height \the\dimexpr \canvasrule/2\relax
          depth \the\dimexpr \canvasrule/2\relax
        \vss}\hss}}%

  \def\vline##1{\pdfcomment{canvas-vline}%
    \hbox to 0pt{\hss\vbox to 0pt{\vss
        \hrule
        width \canvasrule
        height ##1
        depth 0pt
      }\hss}}%

  \def\center##1{\bgroup
    \rlap{\smash{\kern\dimexpr\@tempdima/2\relax
        \raise\dimexpr\@tempdimb/2\relax\hbox{##1}}}\egroup}%

  \def\origin##1{\bgroup
    \rlap{\smash{\kern0pt\raise0pt\hbox{##1}}}\egroup}%

  \def\@at(##1,##2)##3{\bgroup
    \let\w\@tempdima \let\h\@tempdimb
    \rlap{\smash{\kern\dimexpr##1\relax
        \raise\dimexpr##2\relax\hbox{##3}}}\egroup}%

  \def\at{\@ifnextchar(%)  %% comment needed for Emacs
    {\@at}{}}%
  
  \setbox0 \hbox to \@tempdima\bgroup
  \vbox to\@tempdimb\bgroup\offinterlineskip
  \hline{\@tempdima}%
  \vskip\@tempdimb
  \endlinechar=-1
  \hbox to\@tempdima\bgroup\vline{\@tempdimb}\ignorespaces
}

\def\endcanvas{%
  \hss\vline{\@tempdimb}\egroup
  \hline{\@tempdima}\egroup\egroup
  \leavevmode\box0
}
\catcode`\@=12

\def\luagraphicfile#1{\directlua{dofile(kpse.find_file('#1'))}}

\ifdefined\documentclass\endinput\fi

\pdfvariable horigin 0pt
\pdfvariable vorigin 0pt

\hoffset=30bp % ~1cm
\voffset=30bp % ~1cm

\hsize=19cm
\vsize=27cm

\parindent0pt
\parskip=\baselineskip
\nopagenumbers
\newlinechar=`\^^J

\def\begindocument{\everypar={}}
\let\enddocument\bye

\everypar{
  \message{^^J^^J! ERROR: Attempt to typeset before
    \string\begindocument.  Aborting...}
  \batchmode\errmessage{}\END}

\def\newpage{\vfill\eject}

% Set up LaTeX like environment. We have to save TeX's \end before and
% re-define \bye.
\let\END\end
\outer\def\bye{\par\vfill\supereject\END}

\def\begin#1{\bgroup\csname#1\endcsname}
\def\end#1{\csname end#1\endcsname\egroup}



% Local Variables:
%  mode: TeX
%  TeX-master: "foo.tex"
%  indent-tabs-mode: nil
%  coding: utf-8-unix
% End:
% vim:set tabstop=2 expandtab: