%%
%% therion.tex
%%
%% Copyright (C) 2001-2004 Martin Budaj
%%
%% -------------------------------------------------------------------- 
%% 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
%% 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
%% -------------------------------------------------------------------- 

% LuaTex compatibility definitions taken from the LuaTeX manual:
\ifx\directlua\undefined\else
    \protected\def\pdfliteral             {\pdfextension literal}
    \protected\def\pdfcolorstack          {\pdfextension colorstack}
    \protected\def\pdfsetmatrix           {\pdfextension setmatrix}
    \protected\def\pdfsave                {\pdfextension save\relax}
    \protected\def\pdfrestore             {\pdfextension restore\relax}
    \protected\def\pdfobj                 {\pdfextension obj }
    \protected\def\pdfrefobj              {\pdfextension refobj }
    \protected\def\pdfannot               {\pdfextension annot }
    \protected\def\pdfstartlink           {\pdfextension startlink }
    \protected\def\pdfendlink             {\pdfextension endlink\relax}
    \protected\def\pdfoutline             {\pdfextension outline }
    \protected\def\pdfdest                {\pdfextension dest }
    \protected\def\pdfthread              {\pdfextension thread }
    \protected\def\pdfstartthread         {\pdfextension startthread }
    \protected\def\pdfendthread           {\pdfextension endthread\relax}
    \protected\def\pdfinfo                {\pdfextension info }
    \protected\def\pdfcatalog             {\pdfextension catalog }
    \protected\def\pdfnames               {\pdfextension names }
    \protected\def\pdfincludechars        {\pdfextension includechars }
    \protected\def\pdffontattr            {\pdfextension fontattr }
    \protected\def\pdfmapfile             {\pdfextension mapfile }
    \protected\def\pdfmapline             {\pdfextension mapline }
    \protected\def\pdftrailer             {\pdfextension trailer }
    \protected\def\pdfglyphtounicode      {\pdfextension glyphtounicode }
    \def\pdftexversion    {\numexpr\pdffeedback version\relax}
    \def\pdftexrevision           {\pdffeedback revision}
    \def\pdflastlink      {\numexpr\pdffeedback lastlink\relax}
    \def\pdfretval        {\numexpr\pdffeedback retval\relax}
    \def\pdflastobj       {\numexpr\pdffeedback lastobj\relax}
    \def\pdflastannot     {\numexpr\pdffeedback lastannot\relax}
    \def\pdfxformname     {\numexpr\pdffeedback xformname\relax}
    \def\pdfcreationdate          {\pdffeedback creationdate}
    \def\pdffontname      {\pdffeedback fontname}    % modified
    \def\pdffontobjnum    {\pdffeedback fontobjnum}  % modified
    \def\pdffontsize      {\dimexpr\pdffeedback fontsize\relax}
    \def\pdfpageref       {\numexpr\pdffeedback pageref\relax}
    \def\pdfcolorstackinit        {\pdffeedback colorstackinit}
    \edef\pdfcompresslevel            {\pdfvariable compresslevel}
    \edef\pdfobjcompresslevel         {\pdfvariable objcompresslevel}
    \edef\pdfdecimaldigits            {\pdfvariable decimaldigits}
    \edef\pdfgamma                    {\pdfvariable gamma}
    \edef\pdfimageresolution          {\pdfvariable imageresolution}
    \edef\pdfimageapplygamma          {\pdfvariable imageapplygamma}
    \edef\pdfimagegamma               {\pdfvariable imagegamma}
    \edef\pdfimagehicolor             {\pdfvariable imagehicolor}
    \edef\pdfimageaddfilename         {\pdfvariable imageaddfilename}
    \edef\pdfpkresolution             {\pdfvariable pkresolution}
    \edef\pdfpkfixeddpi               {\pdfvariable pkfixeddpi}
    \edef\pdfinclusioncopyfonts       {\pdfvariable inclusioncopyfonts}
    \edef\pdfinclusionerrorlevel      {\pdfvariable inclusionerrorlevel}
    \edef\pdfignoreunknownimages      {\pdfvariable ignoreunknownimages}
    \edef\pdfgentounicode             {\pdfvariable gentounicode}
    \edef\pdfomitcidset               {\pdfvariable omitcidset}
    \edef\pdfpagebox                  {\pdfvariable pagebox}
    \edef\pdfminorversion             {\pdfvariable minorversion}
    \edef\pdfuniqueresname            {\pdfvariable uniqueresname}
    \edef\pdfhorigin                  {\pdfvariable horigin}
    \edef\pdfvorigin                  {\pdfvariable vorigin}
    \edef\pdflinkmargin               {\pdfvariable linkmargin}
    \edef\pdfdestmargin               {\pdfvariable destmargin}
    \edef\pdfthreadmargin             {\pdfvariable threadmargin}
    \edef\pdfxformmargin              {\pdfvariable xformmargin}
    \edef\pdfpagesattr                {\pdfvariable pagesattr}
    \edef\pdfpageattr                 {\pdfvariable pageattr}
    \edef\pdfpageresources            {\pdfvariable pageresources}
    \edef\pdfxformattr                {\pdfvariable xformattr}
    \edef\pdfxformresources           {\pdfvariable xformresources}
    \edef\pdfpkmode                   {\pdfvariable pkmode}
    \edef\pdfsuppressptexinfo         {\pdfvariable suppressoptionalinfo }
    \edef\pdftrailerid                {\pdfvariable trailerid }
    \let\pdfpagewidth      \pagewidth
    \let\pdfpageheight     \pageheight
    \let\pdfadjustspacing  \adjustspacing
    \let\pdfprotrudechars  \protrudechars
    \let\pdfnoligatures    \ignoreligaturesinfont
    \let\pdffontexpand     \expandglyphsinfont
    \let\pdfcopyfont       \copyfont
    \let\pdfxform          \saveboxresource
    \let\pdflastxform      \lastsavedboxresourceindex
    \let\pdfrefxform       \useboxresource
    \let\pdfximage         \saveimageresource
    \let\pdflastximage     \lastsavedimageresourceindex
    \let\pdflastximagepages\lastsavedimageresourcepages
    \let\pdfrefximage      \useimageresource
    \let\pdfsavepos        \savepos
    \let\pdflastxpos       \lastxpos
    \let\pdflastypos       \lastypos
    \let\pdfoutput         \outputmode
    \let\pdfdraftmode      \draftmode
    \let\pdfpxdimen        \pxdimen
    \let\pdfinsertht       \insertht
    \let\pdfnormaldeviate  \normaldeviate
    \let\pdfuniformdeviate \uniformdeviate
    \let\pdfsetrandomseed  \setrandomseed
    \let\pdfrandomseed     \randomseed
    \let\pdfprimitive      \primitive
    % the next three can't be defined inside \ifx..\fi;
    % if required, they could be loaded from external file
    %\let\ifpdfprimitive    \ifprimitive
    %\let\ifpdfabsnum       \ifabsnum
    %\let\ifpdfabsdim       \ifabsdim
\fi

\ifx\pdfoutput\undefined
     \newlinechar`\^^J
     \immediate\write16{^^J^^JProcess me with pdfTeX, please!^^J^^J}
     \expandafter\end
\fi

\def\requiredformat{plain}
\ifx\requiredformat\fmtname\else
     \newlinechar`\^^J
     \immediate\write16{^^J^^JTherion works only with plain TeX format!^^J}
     \immediate\write16{Your format is: \fmtname^^J^^J}
     \expandafter\end
\fi

\def\wlog#1{}


\parindent=0pt
\raggedbottom
\nopagenumbers
\tracingstats=1
\pdfimageresolution=300

%\pdfinfo{/Creator <feff%
%  03b803b703c103af03bf03bd0020%
%  002800740068006500720069006f006e0029002c0020%
%  004d0065007400610050006f00730074002c0020%
%  005400650058>}%

\newbox\xxx
\newbox\mapbox
\newbox\navbox
\newcount\pointerE
\newcount\pointerW
\newcount\pointerN
\newcount\pointerS
\newtoks\pointerU
\newtoks\pointerD
\newcount\pagenum
\newtoks\pagename
\newtoks\pagelabel
\newtoks\opacity \opacity{0.7}
\newtoks\surfaceopacity \surfaceopacity{0.7}
\newif\ifpagenumbering \pagenumberingfalse
\newtoks\bgcolor
\newif\iflegendbgfill

\let\ne\noexpand
\def\notdef{notdef}

% hyperlinks

\def\flatlink#1#2#3#4#5{%
  \setbox\xxx=\hbox{}\wd\xxx=#3bp\ht\xxx=#4bp%
  \rlap{\kern#1bp\raise#2bp\hbox{%
  \vbox to0bp{\vss\hbox to 0bp{%
  \pdfstartlink attr {/Border [0 0 0]} goto name {#5}%
  \box\xxx\pdfendlink\hss}}}}}%

\def\textlink#1#2{%
  \pdfstartlink attr {/Border [0 0 0]} goto name {#2}#1\pdfendlink}%


\def\includechars#1:#2\endinclude{\pdfincludechars#1{#2}}

\newbox\tmpboxa
\newbox\tmpboxb

\newdimen\legendsymbolwd
\newdimen\legendsymbolht \legendsymbolht=12pt
\newdimen\tmpdimen

\def\legendsymbolbox#1#2{%
  \setbox\tmpboxa=\hbox{\pdfrefxform#1}%
  \setbox\tmpboxb=\hbox{\the\legendtextsize(}%
  \legendsymbolwd=\wd\tmpboxa
  \legendsymbolht=\ht\tmpboxa
  \advance\legendsymbolht by -\ht\tmpboxb
  \advance\legendsymbolht by -1dd
  \tmpdimen=\hsize
    \advance\tmpdimen by 10pt 
    \divide\tmpdimen by\legendcolumns 
    \advance\tmpdimen by-10pt
  \hbox to \tmpdimen{%
     \box\tmpboxa\kern10pt\raise\legendsymbolht\vtop{%
       \advance\tmpdimen by -\legendsymbolwd
       \advance\tmpdimen by -10pt
       \hsize=\tmpdimen\rightskip=0pt plus \tmpdimen minus 0pt\relax\the\legendtextsize #2\vskip1dd}\hss}%
}

\let\colorlegendsymbolbox=\legendsymbolbox

\def\colorlegendbox#1{%
  \setbox\xxx=\hbox to 36bp{\PL{q #1 0 0 36 24 re f Q}\hfil}%
  \ht\xxx=25bp\dp\xxx=0bp
  \immediate\pdfxform resources{/ExtGState \the\resid\space 0 R \colorres}\xxx
}


\let\PL\pdfliteral

\newdimen\xcorr
\newdimen\ycorr

\def\PB#1#2#3{\rlap{\kern#1bp\raise#2bp\hbox{\pdfrefxform#3}}} %placebox

\def\setref#1#2{\expandafter\edef\csname thREFS:#1\endcsname{#2}}
\def\getref#1{%
  \ifcsname thREFS:#1\endcsname
    \csname thREFS:#1\endcsname
  \else
    \csname #1\endcsname   % fallback for northarrow/scalebar when using registers
  \fi
}

\def\gridcoord#1#2{\hbox to0pt{%
  \ifnum#1=1\hss\fi
  \ifnum#1=4\hss\fi
  \ifnum#1=7\hss\fi
  \ifnum#1=8\hss\fi
  \ifnum#1=2\hss\fi
    \vbox to0pt{%
      \ifnum#1>3\vss\fi
      \kern2pt%
      \hbox{\kern2pt#2\kern2pt}%
      \kern2pt%
      \ifnum#1<7\vss\fi
    }%
  \ifnum#1=3\hss\fi
  \ifnum#1=6\hss\fi
  \ifnum#1=9\hss\fi
  \ifnum#1=8\hss\fi
  \ifnum#1=2\hss\fi
  }%
} 

\def\PBcorr#1#2#3{%
  \xcorr=#1bp\advance\xcorr by\extraW\advance\xcorr by\overlap%
  \ycorr=#2bp\advance\ycorr by\extraS\advance\ycorr by\overlap%
  \rlap{\kern\xcorr\raise\ycorr\hbox{\pdfrefxform#3}}} %placebox

\def\bitmap#1#2#3#4#5#6#7{%
  \pdfliteral{q #1 #2 #3 #4 #5 #6 cm}%
  \rlap{\pdfrefximage#7}%
  \pdfliteral{Q}%
}

\def\bitmapcorr#1#2#3#4#5#6#7{%
  \xcorr=0bp\advance\xcorr by\extraW\advance\xcorr by\overlap
  \ycorr=0bp\advance\ycorr by\extraS\advance\ycorr by\overlap
  \dimtobp{\xcorr}\edef\wdth{\tmpdef}%
  \dimtobp{\ycorr}\edef\hght{\tmpdef}%
  \pdfliteral{q 1 0 0 1 \wdth\space\hght\space cm #1 #2 #3 #4 #5 #6 cm}% two-step transformation to avoid overflow
  \rlap{\pdfrefximage#7}%
  \pdfliteral{Q}%
}

\newdimen\tmpdimenX
\newdimen\tmpdimenY
\newdimen\tmpdimenXa
\newdimen\tmpdimenYa
\newdimen\tmpdimenXc
\newdimen\tmpdimenYc
\newdimen\tmpdimenXf
\newdimen\tmpdimenYf
\newdimen\tmpdimenXh
\newdimen\tmpdimenYh
\def\calibrX#1{\tmpdimen=#1\advance\tmpdimen by\overlap\advance\tmpdimen by\extraW}
\def\calibrY#1{\tmpdimen=#1\advance\tmpdimen by\overlap\advance\tmpdimen by\extraS}

\def\TITLE#1{\eject\hbox{}\vskip5cm\centerline{\size[24]#1}\vfil\eject}

% processing of the E-W-N-S references

\def\showpointer#1{\ifnum\the#1=0\else\the#1\fi}

% processing of the up and down references

\def\showpointerlist#1{\edef\test{\the#1}\ifx\test\notdef\else
                   \expandafter\process\the#1\endarg\fi}

\def\process#1||#2\endarg{
  \expandafter\processpointeritem#1\endarg%
  \if@#2@\def\next##1\endarg{}
  \else\let\next=\process\fi
  \next#2\endarg}

\def\processpointeritem#1|#2|#3\endarg{\hbox{\textlink{\arr\ #2 (#1)}{#3}}}

\def\arr{}        % to be redefined in the \dopage macro


\def\pagesetup#1#2#3#4#5#6{
  \pdfpagewidth#1
  \pdfpageheight#2
  \hsize#3
  \vsize#4
  \pdfhorigin#5
  \pdfvorigin#6
  \hoffset0pt
  \voffset0pt\relax
}

\def\linestyle{1 J 1 j 1.5 w}

\def\framed#1{% 
  \dimtobp{\wd#1}\edef\wdth{\tmpdef}%
  \dimtobp{\ht#1}\edef\hght{\tmpdef}%
  \hbox to \wd#1{\rlap{\box#1}\PL{q \linestyle\space 0 0 \wdth\space\hght\space re S Q}\hfill}%
}

{\catcode`\p=12\catcode`\t=12\gdef\SKIPPT#1pt{#1}}

\def\ratio#1#2{\expandafter\SKIPPT\the\dimexpr\numexpr\dimexpr#1*65536/\dimexpr#2\relax\relax sp\relax}

\def\dimtobp#1{%
  \tmpdimen=#1%
  \tmpdimen=0.996264\tmpdimen
  \edef\tmpdef{\the\tmpdimen}%
  \edef\tmpdef{\expandafter\SKIPPT\tmpdef}%
}

\def\cmykcolor[#1 #2 #3 #4]{%
  \tmpdimen=#1pt%
  \divide\tmpdimen100
  \edef\tmpdef{\the\tmpdimen}%
  \edef\tmpdef{\expandafter\SKIPPT\tmpdef}%
  \pdfliteral{\tmpdef}%
  \tmpdimen=#2pt%
  \divide\tmpdimen100
  \edef\tmpdef{\the\tmpdimen}%
  \edef\tmpdef{\expandafter\SKIPPT\tmpdef}%
  \pdfliteral{\tmpdef}%
  \tmpdimen=#3pt%
  \divide\tmpdimen100
  \edef\tmpdef{\the\tmpdimen}%
  \edef\tmpdef{\expandafter\SKIPPT\tmpdef}%
  \pdfliteral{\tmpdef}%
  \tmpdimen=#4pt%
  \divide\tmpdimen100
  \edef\tmpdef{\the\tmpdimen}%
  \edef\tmpdef{\expandafter\SKIPPT\tmpdef}%
  \pdfliteral{\tmpdef}%
  \pdfliteral{k}
}

\def\rgbcolor[#1 #2 #3]{%
  \tmpdimen=#1pt%
  \divide\tmpdimen100
  \edef\tmpdef{\the\tmpdimen}%
  \edef\tmpdef{\expandafter\SKIPPT\tmpdef}%
  \pdfliteral{\tmpdef}%
  \tmpdimen=#2pt%
  \divide\tmpdimen100
  \edef\tmpdef{\the\tmpdimen}%
  \edef\tmpdef{\expandafter\SKIPPT\tmpdef}%
  \pdfliteral{\tmpdef}%
  \tmpdimen=#3pt%
  \divide\tmpdimen100
  \edef\tmpdef{\the\tmpdimen}%
  \edef\tmpdef{\expandafter\SKIPPT\tmpdef}%
  \pdfliteral{\tmpdef}%
  \pdfliteral{rg}
}
\let\color\rgbcolor

\def\graycolor[#1]{%
  \tmpdimen=#1pt%
  \divide\tmpdimen100
  \edef\tmpdef{\the\tmpdimen}%
  \edef\tmpdef{\expandafter\SKIPPT\tmpdef}%
  \pdfliteral{\tmpdef}%
  \pdfliteral{g}
}

\def\modelcmyk{cmyk}
\def\modelrgb{rgb}
\def\modelgray{grayscale}
\def\black{\pdfliteral{\ifx\OutputColormodel\modelcmyk 0 0 0 1 k\else
           \ifx\OutputColormodel\modelrgb 0 0 0 rg\else
           0 g\fi\fi}}

% actual placing of the page elements

\def\dopage{%
 \vbox{\centerline{\framed{\mapbox}}
  \bigskip
  \line{%
    \vbox to \ht\navbox{
      \hbox{\size[20]\the\pagelabel
        \ifpagenumbering\space(\the\pagenum)\fi
        \space\size[16]\the\pagename}
      \ifpagenumbering
        \medskip
        \hbox{\qquad\qquad
          \vtop{%
            \hbox to 0pt{\hss\strut\showpointer\pointerN\hss}
            \hbox to 0pt{\llap{\showpointer\pointerW\hskip0.8em}%
              \raise3pt\hbox to 0pt{\hss$\uparrow$\hss}%
              \raise-1pt\hbox to 0pt{\hss$\downarrow$\hss}%
              \raise1pt\hbox to 0pt{\hss$\leftarrow\kern-7pt\rightarrow$\hss}%
              \rlap{\hskip0.8em\showpointer\pointerE}}
              \hbox to 0pt{\hss\strut\showpointer\pointerS\hss}
          }\qquad\qquad
          \vtop{
            \def\arr{$\uparrow$}
            \showpointerlist\pointerU
            \def\arr{$\downarrow$}
            \showpointerlist\pointerD
          }
        }
      \fi
      \vss
    }
    \hss
    \vbox to \ht\navbox{
      \ifnortharrow\hbox to 0pt{\hss\northarrow\qquad}\fi
      \vss
      \ifscalebar\hbox to 0pt{\hss\scalebar\qquad}\fi
    }
    \box\navbox
  }
 }
}

\newif\iflegend
\newif\ifcolorlegend
\newtoks\legendcontent

\newtoks\cavename
\newtoks\comment
\newtoks\legendtitle
\newtoks\colorlegendtitle
\newif\ifnortharrow
\newif\ifscalebar
\newif\ifaltitudebar
\newtoks\explotitle
\newtoks\explodate
\newtoks\exploteam
\newtoks\topotitle
\newtoks\topodate
\newtoks\topoteam
\newtoks\cartotitle
\newtoks\cartodate
\newtoks\cartoteam
\newtoks\copyrights
\newtoks\cavelength
\newtoks\cavedepth
\newtoks\cavelengthtitle
\newtoks\cavedepthtitle
\newtoks\cavemaxx
\newtoks\caveminx
\newtoks\cavemaxy
\newtoks\caveminy
\newtoks\cavemaxz
\newtoks\caveminz
\newtoks\thversion
\newtoks\outcscode
\newtoks\outcsname
\newtoks\northdir
\newtoks\magdecl
\newtoks\gridconv
\newtoks\currentdate
\newtoks\legendtextcolor\legendtextcolor={\black}
\newtoks\legendtextsize\legendtextsize={\size[12]}
\newtoks\legendtextheadersize\legendtextheadersize={\size[26]}
\newtoks\legendtextsectionsize\legendtextsectionsize={\size[14]}

\def\scalebar{\pdfrefxform\getref{THWscalebar}}
\def\altitudebar{\pdfrefxform\getref{THWaltitudebar}}
\def\northarrow{\pdfrefxform\getref{THWnortharrow}}
\def\gridfont{\rm\size[10]}
\newdimen\legendwidth %\legendwidth=14cm
\newcount\legendcolumns \legendcolumns=2

\legendcontent={%
  \hsize=\legendwidth
  \black\the\legendtextcolor 
  \ifnortharrow
    \setbox\tmpboxa=\hbox{\northarrow}%
    \tmpdimen=\dimexpr(\wd\tmpboxa+10pt)%
    \vbox to 0pt{\line{\hfil\box\tmpboxa}\vss}%
  \fi
  \edef\tmp{\the\cavename} \ifx\tmp\empty \else
     \nointerlineskip
     \vskip\lineskip % this emulates the old behavior, where \lineskip was added
                     % because only a small (default) \baselineskip value was applied to \the\cavename
     {\rightskip=\tmpdimen plus 10em\the\legendtextheadersize\the\cavename\par}\vskip1cm
  \fi
  \ifscalebar\scalebar\vskip1cm\fi
  {\rightskip=0pt plus 3em\parskip=3bp
    \edef\tmp{\the\comment} \ifx\tmp\empty \else
      {\the\legendtextsize\the\comment\par}\medskip
    \fi
    \everypar{\hangindent=2em\hangafter=1}
    \edef\tmp{\the\cavelength} \ifx\tmp\empty \else
      {\the\legendtextsize\si\the\cavelengthtitle: \ss\the\cavelength\par}
    \fi
    \edef\tmp{\the\cavedepth} \ifx\tmp\empty \else
      {\the\legendtextsize\si\the\cavedepthtitle: \ss\the\cavedepth\par}
    \fi
    \edef\tmp{\the\exploteam} \ifx\tmp\empty \else
      {\the\legendtextsize\si\the\explotitle: \ss\the\exploteam\quad\si\the\explodate\par}
    \fi
    \edef\tmp{\the\topoteam} \ifx\tmp\empty \else
      {\the\legendtextsize\si\the\topotitle: \ss\the\topoteam\quad\si\the\topodate\par}
    \fi
    \edef\tmp{\the\cartoteam} \ifx\tmp\empty \else
      {\the\legendtextsize\si\the\cartotitle: \ss\the\cartoteam\quad\si\the\cartodate\par}
    \fi
    \edef\tmp{\the\copyrights} \ifx\tmp\empty \else
      {\the\legendtextsize\ss\the\copyrights\par}
    \fi
  }
  \formattedlegend
  \black
}

\def\maplayout{
  \legendbox{NW}{\the\legendcontent}
  \legendbox{NE}{\northarrow}
}


\def\formattedlegend{%
  \ifcolorlegend
  \vskip1cm{\the\legendtextsectionsize\the\colorlegendtitle}\bigskip
  \insertcolorlegend
  \fi
  \iflegend
  \vskip1cm{\the\legendtextsectionsize\the\legendtitle}\bigskip
  \insertlegend
  \fi}

\let\atlaslegend=\formattedlegend

\def\atlastitlepages{
  \edef\tmp{\the\cavename} \ifx\tmp\empty \else
    \TITLE{\the\cavename}
  \fi
  {\rightskip=0pt plus 3em\parskip=3bp
    \edef\tmp{\the\comment} \ifx\tmp\empty \else
      {\size[12]\the\comment\par}\medskip
    \fi
    \everypar{\hangindent=2em\hangafter=1}
    \edef\tmp{\the\exploteam} \ifx\tmp\empty \else
      {\size[12]\si\the\explotitle: \ss\the\exploteam\par}
    \fi
    \edef\tmp{\the\topoteam} \ifx\tmp\empty \else
      {\size[12]\si\the\topotitle: \ss\the\topoteam\par}
    \fi
    \edef\tmp{\the\cartoteam} \ifx\tmp\empty \else
      {\size[12]\si\the\cartotitle: \ss\the\cartoteam\par}
    \fi
    \edef\tmp{\the\copyrights} \ifx\tmp\empty \else
      {\size[12]\ss\the\copyrights\par}
    \fi
    \edef\tmp{\the\cavelength} \ifx\tmp\empty \else
      {\the\legendtextsize\si\the\cavelengthtitle: \ss\the\cavelength\par}
    \fi
    \edef\tmp{\the\cavedepth} \ifx\tmp\empty \else
      {\the\legendtextsize\si\the\cavedepthtitle: \ss\the\cavedepth\par}
    \fi
  }
}

\newdimen\sx
\newdimen\sy
\newdimen\sxd
\newdimen\syd

\def\LEGN{N}
\def\LEGE{E}
\def\LEGW{W}
\def\LEGS{S}
\def\LEGNE{NE}
\def\LEGNW{NW}
\def\LEGSE{SE}
\def\LEGSW{SW}
\def\LEGC{C}


\newdimen\extraN \extraN=0mm
\newdimen\extraS \extraS=0mm
\newdimen\extraE \extraE=0mm
\newdimen\extraW \extraW=0mm
\newdimen\adjustedHS
\newdimen\adjustedVS
\newdimen\adjustedX
\newdimen\adjustedY
\newdimen\framethickness\framethickness=0cm

\def\legendbox#1#2#3#4{%
  \unskip
  \setbox\tmpboxa=\vbox{#4}%
  \sx=\wd\tmpboxa
  \sy=\ht\tmpboxa \advance\sy by \dp\tmpboxa
  \sxd=\hsize \divide\sxd by 100 \sxd=#1\sxd
  \syd=\vsize \divide\syd by 100 \syd=#2\syd
  \edef\test{#3}%
  \ifx\test\LEGN
    \divide\sx by 2 \advance\sxd by -\sx
                    \advance\syd by -\sy
  \else\ifx\test\LEGW
    \divide\sy by 2 \advance\syd by -\sy 
  \else\ifx\test\LEGE
                    \advance\sxd by -\sx
    \divide\sy by 2 \advance\syd by -\sy 
  \else\ifx\test\LEGS
    \divide\sx by 2 \advance\sxd by -\sx
  \else\ifx\test\LEGNW
                    \advance\syd by -\sy 
  \else\ifx\test\LEGNE
                    \advance\sxd by -\sx
                    \advance\syd by -\sy
  \else\ifx\test\LEGSW
  \else\ifx\test\LEGSE
                    \advance\sxd by -\sx
  \else\ifx\test\LEGC
    \divide\sx by 2 \advance\sxd by -\sx
    \divide\sy by 2 \advance\syd by -\sy 
  \fi\fi\fi\fi\fi\fi\fi\fi\fi
  %
  \ifdim\sxd<-\extraW \global\extraW=-\sxd\fi
  \ifdim\syd<-\extraS \global\extraS=-\syd\fi
  \sx=\wd\tmpboxa
  \sy=\ht\tmpboxa \advance\sy by \dp\tmpboxa
  \advance\sx by \sxd \advance\sx by -\hsize
  \advance\sy by \syd \advance\sy by -\vsize
  \ifdim\sx>\extraE \global\extraE=\sx \fi
  \ifdim\sy>\extraN \global\extraN=\sy \fi
  %
  \unskip
%  \advance\sxd by \extraW  -- presunute do thpdf.cxx
%  \advance\syd by \extraS

  % vypln pod legendou
  \iflegendbgfill
   \begingroup
     \advance\sxd-1cm
     \advance\syd-1cm
     \sx=\wd\tmpboxa \advance\sx by 2cm
    \dimtobp{\sxd}\edef\shiftx{\tmpdef}%
    \dimtobp{\syd}\edef\shifty{\tmpdef}%
%    \dimtobp{\wd\tmpboxa}\edef\wdth{\tmpdef}%
     \dimtobp{\sx}\edef\wdth{\tmpdef}%
    \sy=\ht\tmpboxa \advance\sy by \dp\tmpboxa
      \advance\sy by 2 cm
    \dimtobp{\sy}\edef\hght{\tmpdef}%
    \rlap{\PL{q \the\bgcolor\space \shiftx\space \shifty\space \wdth\space \hght\space re f Q}}%
   \endgroup    
  \fi
  %
  \advance\syd by \dp\tmpboxa

  \rlap{\kern\sxd\raise\syd\box\tmpboxa}%
  \ignorespaces
}

\newtoks\includeprefix\includeprefix={.}
\newif\ifabspath
\def\checkcolon #1:#2\endparam{\def\tmp{#2}\ifx\tmp\empty\else\abspathtrue\fi}
\def\checkstartslash #1#2\endparam{\if/#1\abspathtrue\fi}

% convert a relative path to absolute and save it under the name #1
\def\savepath#1#2{\expandafter\edef\csname thABS:#1\endcsname{\the\includeprefix #2}}

\def\loadpicture#1{%
  \ifcsname thABS:#1\endcsname   % use a saved absolute path if it exists
    \pdfximage{\csname thABS:#1\endcsname}%
  \else
    \abspathfalse
    \checkcolon #1:\endparam
    \checkstartslash #1\endparam
    \pdfximage{\ifabspath\else\the\includeprefix\fi #1}%
  \fi
  \pdfrefximage\pdflastximage
}

\def\inputrel#1{\input \the\includeprefix #1}

\def\insertmaps{%
  \input th_formdef
  \input th_pagedef
  \input th_pages
  \immediate\write-1{ \the\count10 \space numeric registers used out of 
            \ifx\eTeXversion\undefined256\else32768\fi}
}
    
\def\insertlegend{%
  \input th_legend
}

\def\insertcolorlegend{%
  \input th_legendcolor
}

\def\insertkeyplan#1#2{%
  \tmpdimen=#1
  \tmpdimen=2\tmpdimen
  \setbox0=\hbox{\pdfximage width \tmpdimen {#2}\pdfrefximage\pdflastximage}
%  \pdfxform0\keyplan=\pdflastxform
  \setbox1=\hbox to #1 {\copy0\hss}
  \setbox2=\hbox to #1 {\hss\box0}
  \eject  
  \ifodd\pageno\hbox{}\vfil\eject\fi
  \line{\hss\pdfxform1\pdfrefxform\pdflastxform}
  \vfil
  \eject
  \line{\pdfxform2\pdfrefxform\pdflastxform\hss}
  \vfil
  \eject
}


\newcount\stopcount
\newcount\stoprow
\newcount\rows
\newcount\rowstmp
\newcount\i
\newcount\j


\def\placemaps{
  \message{ [Warning: \string\placemaps\space is deprecated; 
            use \string\insertmaps] }
  \insertmaps
}

%% Petr Olsak's multicolumn macros (slightly modified)

\newdimen\colsep  \colsep=1em % horiz. mezera mezi sloupci
\newcount\tempnum             % pracovní proměnná
\splittopskip=\baselineskip
\def\roundtolines #1{%% zaokrouhlí na celé násobky vel. řádku
   \divide #1 by\baselineskip \multiply #1 by\baselineskip}
\def\corrsize #1{%% #1 := #1 + \splittopskip - \topskip
   \advance #1 by \splittopskip \advance #1 by-\topskip}

\def\begmulti#1{\par\bigskip\penalty0 \def\Ncols{#1}
   \setbox0=\vbox\bgroup\penalty0
   %% \hsize := šířka sloupce = (\hsize+\colsep) / n - \colsep
   \advance\hsize by\colsep
   \divide\hsize by\Ncols  \advance\hsize by-\colsep}
\def\endmulti{\vfil\egroup \setbox1=\vsplit0 to0pt
   %% \dimen1 := velikost zbylého místa na stránce
   \ifdim\pagegoal=\maxdimen \dimen1=\vsize \corrsize{\dimen1}
   \else \dimen1=\pagegoal \advance\dimen1 by-\pagetotal \fi
   \ifdim \dimen1<2\baselineskip
     \vfil\break \dimen1=\vsize \corrsize{\dimen1} \fi
   %% \dimen0 := výška n sloupcové sazby po rozdělení do sloupců
   %% = (\ht0 + (n-1)\baselineskip) / n, zaokruhleno na řádky
   \dimen0=\Ncols\baselineskip \advance\dimen0 by-\baselineskip
   \advance\dimen0 by \ht0 \divide\dimen0 by\Ncols
   \roundtolines{\dimen0}
   %% Rozdělit sazbu n sloupců do stránek nebo nerozdělit ?
   \ifdim \dimen0>\dimen1 \splitpart
   \else \makecolumns{\dimen0} \fi
   \ifvoid0 \else \errmessage{Lost text in multiple columns?} \fi
   \bigskip}
\def\makecolumns#1{\setbox1=\hbox{}\tempnum=0
   \loop \ifnum\Ncols>\tempnum
      \setbox1=\hbox{\unhbox1 \vsplit0 to#1 \hss}
      \advance\tempnum by1
   \repeat
   \hbox{}\nobreak\vskip-\splittopskip \nointerlineskip
   \line{\unhbox1\unskip}}
\def\splitpart{\roundtolines{\dimen1}
   \makecolumns{\dimen1} \advance\dimen0 by-\dimen1
   %% \dimen0 := výška _zbylé_ n sloupcové sazby
   %% \dimen1 := prázdné místo na stránce = (cca) \vsize
   \vfil\break
   \dimen1=\vsize \corrsize{\dimen1}
   %% Rozdělit zbylou sazbu n sloupců do více stránek ?
   \ifvoid0 \else
      \ifdim \dimen0>\dimen1 \splitpart
      \else \makecolumns{\dimen0} \fi \fi}

%% end of multicolumn macros

\pdfcolorstack 0 set {\ifx\OutputColormodel\modelcmyk 0 0 0 1 k 0 0 0 1 K\else
           \ifx\OutputColormodel\modelrgb 0 0 0 rg 0 0 0 RG\else
           0 g 0 G\fi\fi}

\input th_enc
\input th_texts
\input th_resources
\input th_fontdef

\nonstopmode

