%D \module
%D   [       file=core-fig,
%D        version=1997.03.31,
%D          title=\CONTEXT\ Core Macros,
%D       subtitle=Figure Inclusion,
%D         author=Hans Hagen,
%D           date=\currentdate,
%D      copyright={PRAGMA / Hans Hagen \& Ton Otten}]
%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{loading}{Context Core Macros / Figure Inclusion}

\unprotect

% figurefilemode checken
% zowieso alles checken
% movie scanner

%D Scanning for illustrations is automated to the max. Right
%D from the beginning \CONTEXT\ supported figure inclusion
%D using a dedicated figure directory file. Apart from the fact
%D that such a file enables us to include graphics that cannot
%D be parsed by \TEX\ for dimensions, by using this file we can
%D also quite easily generate figure directories. Only when
%D \PDFTEX\ started offering \PDF\ inclusion, I felt the need
%D to automate dimension detection to a higher degree.
%D Fortunately \TEXUTIL\ can scan more types now as well as
%D that we can run \TEXUTIL\ from within \TEX.

\startmessages  dutch  library: figures
   title: figuren
       1: figuur -- is niet te vinden
       2: figuur -- wordt niet preset
       3: maten van figuur -- geleend van --
       4: maten van -- geladen uit figuurfile zelf
       5: maten van -- geladen uit figuurfile --
       6: maten van -- berekend door TeXUtil
       7: figuurfile -- moet opnieuw worden aangemaakt
       8: figuurobject -- wordt opnieuw gebruikt
       9: suffix -- bij figuur -- wordt niet afgehandeld
\stopmessages

\startmessages  english  library: figures
   title: figures
       1: figure -- can not be found
       2: figure -- is not preset
       3: dimensions of figure -- borrowed from --
       4: dimensions of -- loaded from figurefile itself
       5: dimensions of -- loaded from figurefile --
       6: dimensions of -- calculated by TeXUtil
       7: you have to regenerate figure file --
       8: figureobject -- is reused
       9: suffix -- by figure -- is not handled
\stopmessages

\startmessages  german  library: figures
   title: Abbildungen
       1: Abbildung -- kann nicht gefunden werden
       2: Abbildung -- wird nicht erstellt
       3: Dimensionen von -- uebernommen von --
       4: Dimensionen von -- geladen aus der Abbildungsdatei selbst
       5: Dimensionen von -- geladen aus Abbildungsdatei --
       6: Dimensionen von -- ausgerechnet durch TeXUtil
       7: Sie muessen eine neue Abbildungsdatei -- erstellen
       8: Abbildungobjekt -- wurde wiederverwandt
       9: Dateiendung -- der Abbildung -- wird nicht unterstuetzt
\stopmessages

\startmessages  czech  library: figures
   title: obrazy
       1: obraz -- nelze nalezt
       2: obraz -- nepritomen
       3: dimenze obrazu -- vypujceny od --
       4: dimenze obrazu -- nacteny primo z jeho souboru
       5: dimenze obrazu -- nacteny ze souboru obrazu --
       6: dimenze obrazu -- spocteny programem TeXUtil
       7: musite znovu vygenerovat soubor obrazu --
       8: obrazovy objekt -- je znovu pouzit
       9: pripona -- obrazu -- neni uvazovana
\stopmessages

%D Due to the mere fact that \DVI|/|\PDF\ drivers differ in their
%D needs for figure dimensions, we have to provide the width,
%D height, horizontal and vertical scale. Also we want to
%D specify at the user level either width and|/|or height, scale,
%D or a factor related to the current document bodyfont size.
%D Even better: we can also specify isometric scaling and
%D automatically let \CONTEXT\ calculate the maximum possible
%D dimensions. Whatever we calculate, the results will come
%D available in the next registers.

\newcount \figxsca
\newcount \figysca
\newdimen \fighei
\newdimen \figwid

%D Because looking for dimensions can take many steps (locating
%D the figure, maybe on more directories, scanning the figure
%D on dimension, or when not found, trying to find them in the
%D utility file, and again when not found, trying to generate
%D such a file, and, as a last resort, trying to use the
%D dimensions. Now when things do not work out the way we want,
%D we can set a switch and get some information on what takes
%D place.

\newif\iftraceexternalfigures % \traceexternalfigurestrue

\let\traceexternalfigures \traceexternalfigurestrue

%D Another switch tells \CONTEXT\ to locate and calculate a
%D figure, but does not actually insert it. Especially when we
%D use \PDFTEX\ this saves a lot of time on trialruns. (Keep
%D in mind that \PDFTEX\ is both a \TEX\ pre|| and postcessor.)

\newif\ifskipexternalfigures  % \skipexternalfigurestrue

%D A last switch inhibits running \TEXUTIL. Lets do it when
%D possible.

\newif\ifrunutilityfile       % \runutilityfiletrue

%D When I ever decide to change the format of the figure
%D directory file that \TEXUTIL\ produces, the next number
%D needs to be changed.

\edef\figureversion{1996.06.01}

%D We keep track of the current state by setting a variable
%D which value is related to the method that provided the
%D dimensions.

\chardef\figurefilemode=0

%D The next values are set:
%D
%D \startopsomming[opelkaar]
%D \sym  0  the dimensions are not found
%D \sym  1  the dimensions are not preset at all
%D \sym  2  the dimensions are taken from other
%D \sym  3  the dimensions are taken from figure
%D \sym  4  the dimensions are taken from texutil.tuf
%D \sym  5  the dimensions are generated by texutil.tmp
%D \stopopsomming
%D
%D In our search for the right file, that is, when no
%D filetype is specified, we scan for the next set of files.
%D As one can see, we prefer outlines over bitmaps.

\def\figuretypes%
  {\c!eps,\c!mps,\c!pdf,\c!png,\c!jpg,\c!tif} % ,\c!tex,\c!tmp} % \c!mov

%D Instead of using a comma separated list, we could have use a
%D faster alternative, but the current implementation is not
%D that slow either.
%D
%D Sorry for those who want to understand every bit, but I
%D will only sparse comment on the next macros. These macros
%D evolved out of the original macros and thereby lost all of
%D their beauty.
%D
%D We save the progess state in a macro. The main reason for
%D this is that otherwise the log would end up intermingled
%D with \TEX's hard coded file loading messages and launching
%D \TEXUTIL.

\def\@@eftrace#1%
  {\iftraceexternalfigures
     \edef\externalfigurelog{\externalfigurelog[#1]\space}%
   \fi}

\def\analyzefigurefiles%
  {\let\externalfigurelog\empty
   \let\@@efcurrenttype\empty
   \let\@@efcurrentpath\empty
   \let\@@efcurrentfile\empty
   \doanalyzefigurefiles\doanalyzefigurefilesA
   \doanalyzefigurefiles\doanalyzefigurefilesB
   \doanalyzefigurefiles\doanalyzefigurefilesC}

%D The previous macro suggests that there are three main
%D methods applied. First we pass over all types and
%D directories specified and as soon as we find a suitable
%D candidate, we try to find its dimensions. When we cannot in
%D any way find the dimensions, directly, using the utility
%D file, or using \TEXUTIL\ directly, we revert to the second
%D method, and make a pass over all utility files. The last
%D method scans the utility files for files with the same name,
%D but different type.

\def\doanalyzefigurefiles#1%
  {\let\dodododoanalyzefigurefiles=#1%
   \processcommacommand[\@@eftype]\dodoanalyzefigurefiles}

\def\dodoanalyzefigurefiles#1%
  {\ifcase\figurestatus
     \def\@@efcurrenttype{#1}%
     \processcommacommand[\@@ex@@gebied]\dododoanalyzefigurefiles
   \fi}

\def\dododoanalyzefigurefiles#1%
  {\ifcase\figurestatus
     \def\@@efcurrentpath{#1}%
\sanitizefilename#1\to\@@efcurrentpath
     \doiffileinsertionsupportedelse{\@@efcurrenttype}
       {\edef\@@efcurrentfile%
          {\@@efcurrentpath\f!pathseparator\@@effilename.\figureextension{\@@efcurrenttype}}%
        \dodododoanalyzefigurefiles}
       {}%
   \fi}

%D Here is our first method: we scan the file directly, parse
%D the utility file next, and finaly run \TEXUTIL. The latter
%D two of course only take place when the first scan fails.

\def\doanalyzefigurefilesA%
  {\doiffileelse{\@@efcurrentfile}
     {\getfiguredimensionsA
      \getfiguredimensionsB
      \getfiguredimensionsC}
     {}}

%D It is possible to let \TEX\ determine the dimensions itself.
%D The next macro shows how such a test is implemented. The
%D special driver \type {supp-tpd} shows some more.

%D The check on extension prevents problems when drivers are
%D not loaded well, in which case the tex one comes first.
%D
%D Should be a special!

\def\dogetfiguresizetex#1#2#3#4#5#6% file pagina ...
  {\doifinset{\@@efextension}{\c!tex,\c!tmp}
     {\doifnumberelse{\@@efextension}
        {\executedfalse}
        {\ExpandBothAfter\doifinset{\@@efextension}{\figuretypes}
           {\executedfalse}}}%
   \ifexecuted
     \global\setbox\foundexternalfigure=\vbox
       {\insidefloattrue
        \forgetall
        \blanko[\v!blokkeer] % niet meer weg !
        \startreadingfile
        \readfile{#1}{}{}%
        \stopreadingfile
        \endgraf
        \removelastskip}%
     \global\setbox\foundexternalfigure=\hbox
       {\raise\dp\foundexternalfigure\box\foundexternalfigure}%
     #3=\!!zeropoint
     #4=\!!zeropoint
     #5=\wd\foundexternalfigure
     #6=\ht\foundexternalfigure
   \fi}

\let\dogetfiguresizetmp\dogetfiguresizetex

%D Here we start scanning the other types:

\def\getfiguredimensionsA%
  {\ifcase\figurestatus
     \@@eftrace{analyzing \@@efcurrentfile\space as \@@efcurrenttype}%
     \!!widthb=\!!zeropoint
     \executeifdefined{dogetfiguresize\@@efcurrenttype}\gobblesixarguments
       {\@@efcurrentfile}{\@@efpagina}%
       {\!!widtha}{\!!heighta}{\!!widthb}{\!!heightb}%
% new from here
     \ifdim\!!widtha=\!!zeropoint\relax\ifdim\!!heighta=\!!zeropoint\relax
       \ifdim\!!widthb=\!!zeropoint\relax\ifdim\!!heightb=\!!zeropoint\relax
         \executedfalse % nothing decent found
       \fi\fi
     \fi\fi
     \ifexecuted % searching for mps, eps found, but no valid mps
       \doif{\@@efcurrenttype}{\c!mps}{\ifcase\EPScreator\executedfalse\fi}%
     \fi
% until here
     \ifexecuted
       \chardef\figurestatus=3
       \doifelse{\@@efcurrenttype}{\c!eps}
         {\ifcase\EPScreator
           \@@eftrace{found}%
          \else
            \let\@@efcurrenttype\c!mps
            \@@eftrace{mps found}%
          \fi}
         {\@@eftrace{found}}%
       \geteparameters % e !
         [\??ep]
         [\c!x=\the\!!widtha,\c!y=\the\!!heighta,
          \c!w=\the\!!widthb,\c!h=\the\!!heightb]%
       \let\@@eftype\@@efcurrenttype
       \let\@@effullname\@@efcurrentfile
     \else
       \@@eftrace{not found}%
     \fi
   \fi}

\def\dogetfiguresizepdf#1#2#3#4#5#6%
  {\dogetPDFmediabox{#1}{#3}{#4}{#5}{#6}}

\def\dogetfiguresizeeps#1#2#3#4#5#6%
  {\dogetEPSboundingbox{#1}{#3}{#4}{#5}{#6}}

\def\dogetfiguresizemps%
  {\dogetfiguresizeeps}

\def\getfiguredimensionsB%
  {\ifcase\figurestatus\ifcase\figurefilemode\else
     \def\@@efloadname{\@@efcurrentpath\f!pathseparator\@@exfile}%
     \edef\@@effilenametype{\@@effilename.\@@efcurrenttype}%
     \@@eftrace{analyzing \@@efloadname\space on \@@effilenametype}%
     \pushendofline
     \let\presetfigure=\presetfigureA
     \readsetfile{\@@efcurrentpath}{\@@exfile}\relax\relax
     \popendofline
     \@@eftrace{\ifcase\figurestatus not \fi found}%
   \fi\fi}

\def\presetfigureA[#1][#2]%
  {\ifcase\figurestatus
     \@EA\DOIF\@EA{\@@effilenametype}{#1} % hm, tzt ook nog eens met pad/naam
       {\getparameters[\??ep][#2]%
        \ExpandBothAfter\doif{\@@epe}{\@@efcurrenttype}
          {\chardef\figurestatus=4
           \let\@@eftype\@@efcurrenttype
           \let\@@effullname\@@efcurrentfile}}%
   \else
     \endinput
   \fi}

\def\getfiguredimensionsC%
  {\ifcase\figurestatus\ifrunutilityfile\ifcase\figurefilemode\else
     \doif{\@@efcurrenttype}{\c!tex}
       {\doiffileelse{\@@efcurrentfile}
          {\edef\@@effilenametype{\@@effilename.\@@efcurrenttype}%
           \@@eftrace{running texutil on \@@effilenametype}%
           \def\@@efloadname{\f!utilityfilename.\f!temporaryextension}%
           \executesystemcommand
             {texutil --fig --out=\@@efloadname\space\@@effilenametype}%
           \@@eftrace{analyzing \@@efloadname\space on \@@effilenametype}%
           \pushendofline
           \let\presetfigure=\presetfigureB
          \readsetfile{.}{\@@efloadname}\relax\relax
            \popendofline
           \@@eftrace{\ifcase\figurestatus not \fi found}}
          {}}%
   \fi\fi\fi}

\def\presetfigureB[#1][#2]%
  {\getparameters[\??ep][#2]%
   \chardef\figurestatus=6    % ??????????????????
   \let\@@eftype\@@efcurrenttype
   \let\@@effullname\@@efcurrentfile}

%D The second pass over types and directories uses the
%D utilility files.

\def\doanalyzefigurefilesB%
  {\ifcase\figurestatus\ifcase\figurefilemode\else
     \def\@@efloadname{\@@efcurrentpath\f!pathseparator\@@exfile}%
     \edef\@@effilenametype{\@@effilename.\figureextension{\@@efcurrenttype}}%
     \@@eftrace{analyzing \@@efloadname\space on \@@effilenametype}%
     \pushendofline
     \let\presetfigure=\presetfigureC
     \readsetfile{\@@efcurrentpath}{\@@exfile}\relax\relax
     \popendofline
     \@@eftrace{\ifcase\figurestatus not \fi found}%
   \fi\fi}

\def\presetfigureC[#1][#2]%
  {\ifcase\figurestatus
     \@EA\DOIF\@EA{\@@effilenametype}{#1}
       {\getparameters[\??ep][#2]%
        \doif{\@@epe}{\@@efcurrenttype}
          {\chardef\figurestatus=4
           \let\@@eftype\@@efcurrenttype
           \let\@@effullname\@@efcurrentfile}}%
   \else
     \endinput
   \fi}

%D The last and third pass mainly differs from the second in
%D being more tolerant.

\def\doanalyzefigurefilesC%
  {\ifcase\figurestatus\ifcase\figurefilemode\else
     \def\@@efloadname{\@@efcurrentpath\f!pathseparator\@@exfile}%
     \@@eftrace{analyzing \@@efloadname\space on \@@effilename.* surrogate}%
     \pushendofline
     \let\presetfigure=\presetfigureD
     \readsetfile{\@@efcurrentpath}{\@@exfile}\relax\relax
     \popendofline
     \@@eftrace{\ifcase\figurestatus not \fi found}%
   \fi\fi}

\def\presetfigureD[#1][#2]%
  {\ifcase\figurestatus
     \@EA\DOIFINSTRINGELSE\@EA{\@@effilename.}{#1}
       {\getparameters[\??ep][#2]%
        \ExpandBothAfter\doifinsetelse{\@@epe}{\@@efcurrenttype}
          {\chardef\figurestatus=4
           \let\@@eftype\@@efcurrenttype
           \let\@@effullname\@@efcurrentfile}
          {}}
       {}%
   \else
     \endinput
   \fi}

%D While loading the utlity file (often \type {texutil.tuf})
%D the next command (when present) aborts reading when the
%D versions don't match.

\def\thisisfigureversion#1%
  {\doifnot{\figureversion}{#1}
     {\showmessage{\m!figures}{7}{\@@efloadname}\endinput}}

%D Some files, take for instance movies, cannot easilly be
%D parsed on dimensions, that is, not yet. Although the current
%D mechanism has no problems with this, as long as the user
%D specified width and height reflect the right aspect ratio.
%D Nevertheless, when one does not want any scanning done, one
%D can disable \type{preset}. When no preset is needed, we only
%D locate the file.

\def\locatepresetfigurefiles%
  {\processcommacommand[\@@eftype]\dolocatepresetfigurefiles}

\def\dolocatepresetfigurefiles#1%
  {\def\@@efcurrenttype{#1}%
   \processcommacommand[\@@ex@@gebied]\dodolocatepresetfigurefiles}

\def\dodolocatepresetfigurefiles#1%
  {\ifcase\figurestatus
     \doiffileinsertionsupportedelse{\@@efcurrenttype}
       {\edef\@@efcurrentfile%
          {#1\f!pathseparator\@@effilename.\figureextension{\@@efcurrenttype}}%
        \@@eftrace{only searching for \@@efcurrentfile}%
        \doiffileelse{\@@efcurrentfile}
          {\chardef\figurestatus=1
           \let\@@eftype\@@efcurrenttype
           \let\@@effullname\@@efcurrentfile}
          {}}
       {}%
   \fi}

%D All these macros are in some way called by the macro \type
%D {\analyzefigurefiles}, which in turn is called by the next
%D macro.

% bools gebruiken

\def\setnaturalfiguresize%
  {\doifsomething{\@@efbreedte}
     {\figwid=\@@efbreedte}%
   \doifsomething{\@@efhoogte}
     {\fighei=\@@efhoogte}%
   \doifsomething{\@@efschaal}
     {\figxsca=\@@efschaal
      \figysca=\@@efschaal}}

\def\setfactorfiguresize%
  {\doifinsetelse{\@@effactor}{\v!max,\v!passend,\v!ruim}
     {\doapplyfiguresize
      \ifdim\@@epw>\@@eph\relax
        \docalculatefigurenorm\figwid\@@effactor\@@efbreedte\hsize\@@efhsize
        \docalculatefigurescales\figwid\@@epw\fighei\@@eph
      \else
        \docalculatefigurenorm\fighei\@@effactor\@@efhoogte\teksthoogte\@@efvsize
        \docalculatefigurescales\fighei\@@eph\figwid\@@epw
      \fi
      \!!doneatrue}
     {\doifinsetelse{\@@efhfactor}{\v!max,\v!passend,\v!ruim}
        {\doapplyfiguresize
         \docalculatefigurenorm\fighei\@@efhfactor\@@efhoogte\teksthoogte\@@efvsize
         \docalculatefigurescales\fighei\@@eph\figwid\@@epw
         \!!doneatrue}
        {\doifinsetelse{\@@efbfactor}{\v!max,\v!passend,\v!ruim}
           {\doapplyfiguresize
            \docalculatefigurenorm\figwid\@@efbfactor\@@efbreedte\hsize\@@efhsize
            \docalculatefigurescales\figwid\@@epw\fighei\@@eph
            \!!doneatrue}
           {\docalculatefigurenorm\fighei\@@effactor\@@efhoogte\teksthoogte\@@efvsize
            \docalculatefigurenorm\fighei\@@efhfactor\@@efhoogte\teksthoogte\@@efvsize
            \docalculatefigurenorm\figwid\@@efbfactor\@@efbreedte\hsize\hsize
            \!!doneafalse}}}%
   \if!!donea
     \ifdim\figwid>\@@efhsize\relax
       \global\fighei=\!!zeropoint\relax
       \global\figwid=\@@efhsize\relax
     \else\ifdim\fighei>\@@efvsize\relax
       \global\fighei=\@@efvsize\relax
       \global\figwid=\!!zeropoint\relax
     \fi\fi
   \fi}

\def\setscalefiguresize%
  {\doifsomething{\@@efschaal}
     {\doapplyfigurescale\figwid\@@epw\figxsca
      \doapplyfigurescale\fighei\@@eph\figysca
      \global\figwid=\!!zeropoint
      \global\fighei=\!!zeropoint
      \doifelsenothing{\@@efbreedte}
        {\doifsomething{\@@efhoogte}
           {\ifdim\@@eph>\@@efhoogte
              \global\fighei=\@@efhoogte
            \fi}}
        {\ifdim\@@epw>\@@efbreedte
           \global\figwid=\@@efbreedte
         \fi}}}

\def\setdimensionfiguresize%
  {\ifdim\figwid>\!!zeropoint\relax
     \ifdim\fighei>\!!zeropoint\relax
       \docalculatefigurescale\fighei\@@eph\figysca
       \docalculatefigurescale\figwid\@@epw\figxsca
     \else
       \docalculatefigurescales\figwid\@@epw\fighei\@@eph
     \fi
   \else
     \ifdim\fighei>\!!zeropoint\relax
       \docalculatefigurescales\fighei\@@eph\figwid\@@epw
     \else
       \doapplyfigurescale\figwid\@@epw\figxsca
       \doapplyfigurescale\fighei\@@eph\figysca
     \fi
   \fi}

\def\setupexternalfigures%
  {\dosingleempty\dosetupexternalfigures}

\def\dosetupexternalfigures[#1]%
  {\getparameters[\??ex][#1]%
   \doifelsenothing{\@@explaats}
     {\scratchcounter=3 }
     {\doifelsenothing{\@@exfile}
        {\scratchcounter=3 }
        {\scratchcounter=0
         \ExpandBothAfter\doifinset{\v!lokaal}{\@@explaats}
           {\advance\scratchcounter by 1 }%
         \ExpandBothAfter\doifinset{\v!globaal}{\@@explaats}
           {\advance\scratchcounter by 2 }}}%
   \chardef\figurefilemode=\scratchcounter}

\def\docalculatefigurenorm#1#2#3#4#5%
  {\processaction
      [#2]
      [     \v!max=>#1=#4\relax,
        \v!passend=>#1=#5\relax,
           \v!ruim=>#1=#5\relax
                    \advance #1 by -4\@@exkorps\relax,
        \s!default=>\doifsomething{#3}{#1=#3\relax},
        \s!unknown=>#1=\@@exkorps\relax
                    \divide#1 by \!!ten\relax
                    \multiply#1 by #2\relax]}

\def\docalculatefigurescales#1#2#3#4%
  {\dimen0=#1\relax                     % #1 = new 1-value
   \dimen2=#2\relax                     % #2 = old 1-value
   \divide\dimen2 by \!!thousand
   \divide\dimen0 by \dimen2
   \figxsca=\dimen0                     %      x scale
   \figysca=\dimen0                     %      y scale
   \dimen2=#4\relax                     % #4 = old 2-value
   \divide\dimen2 by \!!thousand
   \multiply\dimen2 by \dimen0
   #3=\dimen2 }                         % #3 = new 2-value

\def\docalculatefigurescale#1#2#3%
  {\dimen0=#1\relax                     % #1 = new value
   \dimen2=#2\relax                     % #2 = old value
   \divide\dimen2 by \!!thousand
   \divide\dimen0 by \dimen2
   #3=\dimen0 }                         % #3 = schaal

\def\doapplyfigurescale#1#2#3%
  {#1=#2\relax
   \ifcase0\@@efschaal\relax
     #3=\!!thousand
   \else
     #3=\@@efschaal
   \fi
   \relax % important !  
   \ifnum#3=\!!thousand\else
     \divide#1 by \!!thousand
     \multiply#1 by #3\relax
   \fi}

\def\doapplyfiguresize%
  {\ifinner
     \teksthoogte=\vsize
     \scratchdimen=\teksthoogte
   \else
     \ifdim\pagegoal<\maxdimen
       \ifdim\pagetotal<\pagegoal
         \scratchdimen=\pagegoal
         \advance\scratchdimen by -\pagetotal
       \else
         \scratchdimen=\teksthoogte
       \fi
     \else
       \scratchdimen=\teksthoogte
     \fi
   \fi
   \doifelsenothing{\@@efhoogte}
     {\edef\@@efvsize{\the\scratchdimen}}
     {\let\@@efvsize=\@@efhoogte}%
   \doifelsenothing{\@@efbreedte}
     {\edef\@@efhsize{\the\hsize}}
     {\let\@@efhsize=\@@efbreedte}}

\def\convertfigureinsertscale#1#2#3#4%
  {\scratchdimen=#1\relax
   \ifnum#3=\!!thousand\else % better 1000 100 10 ranges, evt round 2sp 
     \divide\scratchdimen by \!!thousand
     \multiply\scratchdimen by #3\relax
   \fi
   \scratchdimen=-\scratchdimen  % beter hier - dan in driver
   \edef#2{\number\scratchdimen}%
   \scratchdimen=#3pt
   \divide\scratchdimen by \!!ten
   \edef#4{\@EA\withoutpt\@EA{\the\scratchdimen}}}

\newbox\foundexternalfigure

\def\presetfiguremov%
  {\let\@@eftype      \c!mov
   \let\@@efextension \c!mov
   \let\@@efobject    \v!nee
   \let\@@efpreset    \v!nee
   \ifx\@@efbreedte\empty\def\@@breedte{4cm}\fi
   \ifx\@@efhoogte \empty\def\@@hoogte {3cm}\fi}

% The page number (frame) is passed as first option.

\newcounter\forcedMPSobject % better something \every<type>

%D We have arrived at one of the main macros, the one that
%D tries to analyze the figure, preloads it when possible, and
%D scales is according to the specifications. This macro is
%D quite unreadable, for which I appologize. The main
%D complication is that we have to catch all kind of border
%D cases, like \METAPOST\ graphics and buffers. 

\def\calculateexternalfigure[#1][#2][#3][#4][#5][#6]%
  {\mindermeldingen
   \global\setbox\foundexternalfigure=\box\voidb@x
   \beforesplitstring#3\at.\to\@@effilename
   \aftersplitstring #3\at.\to\@@efextension
\doifelse{\@@effilename}{mprun}
  {\edef\@@effilepref{\bufferprefix}}
  {\let \@@effilepref\empty}%
\edef\@@effilename{\@@effilepref\@@effilename}%
   \restorecatcodes  % recently added; we presume local use
   \def\@@eflabel{#2}%
   \global\let\externalfigurelog\empty
   \getparameters
     [\??ep]
     [\c!e=\s!unknown,
      \c!w=15\korpsgrootte,\c!h=10\korpsgrootte,
      \c!x=\!!zeropoint,\c!y=\!!zeropoint,
      \c!t=,\c!s=,\c!a=,\c!f=\@@effilename]%
   \getparameters
     [\??ef]
     [\c!type=\s!unknown,\c!methode=\@@eftype,\c!symbool=\v!nee,
      \c!object=\@@exobject,\c!preset=\v!ja,
      \c!pagina=0,\c!preview=\v!nee,\c!herhaal=\v!nee,
      \c!schaal=,\c!breedte=,\c!hoogte=,\c!scherm=,
      \c!factor=,\c!hfactor=,\c!bfactor=,
      \c!achtergrond=,\c!achtergrondkleur=,\c!achtergrondraster=\@@rsraster,
      \c!hoek=,\c!straal=.5\korpsgrootte,\c!kader=\v!uit]%
   \doif{\@@efextension}{\c!mov}{\presetfiguremov}%
   #1[#4][#5][#6]%
   \doif{\@@eftype}{\c!mov}{\presetfiguremov}%
   \doif{\@@eftype}{\v!buffer}
     {\ifx\@@efextension\empty
        \let\@@efextension\c!tmp
      \fi
      \let\@@eftype\c!tex}%
   \processaction
     [\@@efextension]
     [   \c!tex=>\let\@@eftype\c!tex,
         \c!tmp=>\let\@@eftype\c!tex
                 \edef\@@effilepref{\bufferprefix}%
                 \edef\@@effilename{\@@effilepref\@@effilename},
         \c!mov=>\presetfiguremov]%
   \edef\figuretypes{\figuretypes,\c!tex}%
   \doifelse{\@@efobject}{\v!nee}
     {\donefalse}
     {\doifspecialavailableelse\dostartscaling
        {\doifobjectssupportedelse
           {\edef\@@efobjectname{\@@effilename-\@@eftype-\@@efextension-\@@efpagina}%
            \doifobjectfoundelse{FIG}{\@@efobjectname}{\donetrue}{\donefalse}}
           {\donefalse}}
        {\donefalse}}%
   \doif{\@@effilename}{\jobname}
     {\@EA\removefromcommalist\@EA{\jobsuffix}\figuretypes
      \let\@@efextension=\empty
      \showmessage{\m!figures}{9}{\@@effilename,\jobsuffix}%
      \donefalse}%
   \ifdone
     \getobjectdimensions{FIG}{\@@efobjectname}%
     \geteparameters % e !
       [\??ep]
       [\c!x=\!!zeropoint,\c!y=\!!zeropoint,
        \c!w=\objectwidth,\c!h=\objectheight]%
     \chardef\figurestatus=5
     \edef\@@effullname{\@@effilepref#3}%
   \else
     \doifelse{#2}{\s!figurepreset}
       {\def\figureextension##1{\@@efextension}%
        \edef\@@effullname{\@@effilepref#3}}%
       {\ifx\@@efextension\empty
          \dogetcommacommandelement1\from\@@eftype\to\commalistelement
          \edef\@@effullname{\@@effilename.\commalistelement}%
          \def\figureextension##1{##1}%
        \else
          \@EA\doifnumberelse\@EA{\@@efextension}
            {\def\@@eftype{\c!mps}}{}%
          \edef\@@effullname{\@@effilename.\@@efextension}%
          \def\figureextension##1{\@@efextension}%
        \fi}%
     \ifcase\figurefilemode
       \let\@@ex@@gebied\f!currentpath
     \or
       \let\@@ex@@gebied\f!currentpath
     \or
       \let\@@ex@@gebied\@@exgebied
     \or
       \edef\@@ex@@gebied{\f!currentpath\ifx\@@exgebied\empty\else,\fi\@@exgebied}%
     \fi
     \doifelse{\@@efpreset}{\v!nee}
       {\doifelse{\@@eftype}{\s!unknown}
          {\chardef\figurestatus=0
           \let\@@eftype=\figuretypes
           \locatepresetfigurefiles}
          {\chardef\figurestatus=1 }}
       {\doifelse{\@@eftype}{\s!unknown}
          {\let\@@eftype=\figuretypes}
          {\@EA\removefromcommalist\@EA{\@@eftype}\figuretypes
           \edef\@@eftype{\ifx\@@eftype\empty\else\@@eftype,\fi\figuretypes}}%
        \ifx\@@efextension\empty\else
          \ExpandBothAfter\doifinsetelse{\@@efextension}{\@@eftype}
            {\@EA\removefromcommalist\@EA{\@@efextension}\@@eftype
             \edef\@@eftype{\@@efextension,\@@eftype}}%
            {}%
        \fi
        \doifelse{#2}{\s!figurepreset}
          {\chardef\figurestatus=4
           \def\@@efloadname{\f!currentpath\f!pathseparator\@@exfile}%
           \let\@@eftype=\@@epe}
          {\chardef\figurestatus=0
           \analyzefigurefiles}}%
     \let\@@epe=\@@eftype
     \edef\@@efextension{\figureextension{\@@eftype}}% dirty trick
     \figwid=\!!zeropoint \fighei=\!!zeropoint \figxsca=1 \figysca=1
     \doif{\@@exoptie}{\v!kader}
       {\let\@@efkader=\v!aan}%
   \fi
   \ifcase\figurestatus
     \def\@@efkader{\v!aan}%
     \showmessage{\m!figures}{1}{\@@effilename}%
   \or
     \showmessage{\m!figures}{2}{\@@effullname}%
   \or
     \showmessage{\m!figures}{3}{\@@effullname,\@@eflenttype}%
   \or
     \showmessage{\m!figures}{4}{\@@effullname}%
   \or
     \showmessage{\m!figures}{5}{\@@effullname,\@@efloadname}%
   \or
     \doifnot{\@@efsymbool}{\v!ja}
       {\showmessage{\m!figures}{8}{\@@effullname}}%
   \fi
   \ifdim\@@epw=\!!zeropoint \chardef\figurestatus=1 \fi
   \ifdim\@@eph=\!!zeropoint \chardef\figurestatus=1 \fi
   \ifnum\figurestatus=1 % unknown dimensions, take width or height or scale
     \setnaturalfiguresize
\xdef\naturalfigurewidth{\the\figwid}%
\xdef\naturalfigureheight{\the\fighei}%
     \let\@@efkader=\v!uit
   \else
\global\let\naturalfigurewidth\@@epw
\global\let\naturalfigureheight\@@eph
     \setfactorfiguresize
     \setscalefiguresize
     \setdimensionfiguresize
   \fi
   \convertfigureinsertscale\@@epx\figx\figxsca\scax
   \convertfigureinsertscale\@@epy\figy\figysca\scay
   \iftraceexternalfigures
     \message
       {\externalfigurelog
        [\@@effullname:
         t={\@@eftype}\space m={\@@efmethode}\space l=\@@eflabel\space
         w=\number\figwid\space h=\number\fighei\space
         sx=\scax\space sy=\scay\space ox=\figx\space oy=\figy]}%
   \fi
   \doif{\@@exoptie}{\v!leeg}
     {\skipexternalfigurestrue
      \let\@@efkader=\v!uit}%
\doifelsenothing{\@@efpagina} % NIEUW
  {\let\@@efoptions\empty}
  {\let\@@efoptions\@@efpagina}%
   \doif{\@@efpreview}{\v!ja}
     {\addtocommalist\v!preview\@@efoptions}%
   \doif{\@@efherhaal}{\v!ja}
     {\addtocommalist\v!herhaal\@@efoptions}%
\doif{\@@eftype}{\c!mps}
  {\ifcase\EPSspecial\else\ifinobject\else
     \@@eftrace{special mps, object forced}%
     \doglobal\increment\forcedMPSobject
     \edef\@@efobjectname{\c!mps::\forcedMPSobject}%
     \let\@@efobject\v!ja
   \fi\fi}%
\global\let\lastfigureobjectname\@@efobjectname
    \doifelse{\@@efobject}{\v!nee}
     {\donefalse}
     {\doifobjectssupportedelse{\donetrue}{\donefalse}}%
   \ifdone
     \doifobjectfoundelse{FIG}{\@@efobjectname}
       {}
       {\bgroup
        \figwid=\@@epw\fighei=\@@eph
        \scratchdimen=\@@epx\scratchdimen=-\scratchdimen
        \edef\@@epx{\number\scratchdimen}%
        \scratchdimen=\@@epy\scratchdimen=-\scratchdimen
        \edef\@@epy{\number\scratchdimen}%
        \scratchdimen=\@@epw\edef\@@epw{\number\scratchdimen}%
        \scratchdimen=\@@eph\edef\@@eph{\number\scratchdimen}%
        \setbox0=\vbox to \fighei
          {\vfill
           \ifdim\wd\foundexternalfigure=\!!zeropoint
             \doinsertfile
               {\@@eftype,\@@efmethode}{\@@effullname,\@@eflabel}
               {100}{100}
               {\@@epx}{\@@epy}
               {\@@epw}{\@@eph}
               {\@@efoptions}%
           \else\ifskipexternalfigures
             \ruledhbox{\fakebox\foundexternalfigure}
           \else
             \box\foundexternalfigure
           \fi\fi}%
        \wd0=\figwid
        \setobject{FIG}{\@@efobjectname}\vbox{\box0}%
        \egroup}%
   \fi
   \xdef\figurewidth {\the\figwid}%
   \xdef\figureheight{\the\fighei}%
   \global\setbox\foundexternalfigure=\vbox to \fighei
     {\vfill
      \hsize\figwid
      \ifdone
        \dimen0=\scax pt\divide\dimen0 by 100\edef\scax{\@EA\withoutpt\the\dimen0}%
        \dimen0=\scay pt\divide\dimen0 by 100\edef\scay{\@EA\withoutpt\the\dimen0}%
        \schaal[sx=\scax,sy=\scay]{\dowithfigure{\getobject{FIG}{\@@efobjectname}}}%
      \else\ifdim\wd\foundexternalfigure=\!!zeropoint
        \dowithfigure
          {\doinsertfile
             {\@@eftype,\@@efmethode}{\@@effullname,\@@eflabel}
             {\scax}{\scay}
             {\figx}{\figy}
             {\number\figwid}{\number\fighei}
             {\@@efoptions}}%
      \else
        \dimen0=\scax pt\divide\dimen0 by 100\edef\scax{\@EA\withoutpt\the\dimen0}%
        \dimen0=\scay pt\divide\dimen0 by 100\edef\scay{\@EA\withoutpt\the\dimen0}%
        \schaal[sx=\scax,sy=\scay]{\dowithfigure{\box\foundexternalfigure}}%
      \fi\fi}%
   \global\wd\foundexternalfigure=\figwid}

%D In \PDF\ one can specify an alternative graphic. This means
%D that for instance a low resolution graphic can be used for
%D viewing and a high res one for printing. Because this
%D feature depends much on the driver, here we only take care
%D of perparations. It is up to the special driver to handle
%D the inclusion. The driver routines can change the content of
%D box \type {\foundexternalfigure} if suitable.
%D
%D One complication is for instance that an alternative may
%D not itself have an alternative, and these kind of situations
%D are best handled by the driver.

\let\lastfigureobjectname\empty

\def\calculateexternalscreenfigure[#1][#2][#3][#4][#5][#6]%
  {\ifx\@@efscherm\empty\else
     \doifnot{\@@efobject}{\v!nee}
       {\doifobjectssupportedelse
          {\doifspecialavailableelse\doregisterfigure
             {\bgroup
              #1[#4][#5][#6]%
              \doregisterfigure{FIG}{\lastfigureobjectname}%
              \let\@@ef@@scherm\@@efscherm
              \calculateexternalfigure[#1][\@@ef@@scherm][\@@ef@@scherm][#4,\c!scherm=][#5][#6]%
              \egroup}
             {}}
          {}}%
   \fi}

\let\dowithfigure\relax

\let\naturalfigureheight=\!!zeropoint
\let\naturalfigurewidth =\!!zeropoint
\let\figureheight       =\!!zeropoint
\let\figurewidth        =\!!zeropoint

\def\getfiguredimensions%
  {\dodoubleempty\dogetfiguredimensions}

\def\dogetfiguredimensions[#1][#2]%
  {{\let\immediate\relax % very dirty but prevents flushing
    \setbox0=\hbox{\externalfigure[#1][#2,\c!scherm=,\c!object=\v!nee]}}}

\presetlocalframed[\??ef]

\def\doplaceexternalfigure% used direct and indirect
  {\dosixtupleempty\dodoplaceexternalfigure}

\def\dodoplaceexternalfigure[#1][#2][#3][#4][#5][#6]%
  {\bgroup
   \setupexternalfigures
   \calculateexternalfigure[#1][#2][#3][#4][#5][#6]%
   \setupexternalfigures
   \calculateexternalscreenfigure[#1][#2][#3][#4][#5][#6]%
   \xdef\figurewidth {\the\figwid}%
   \xdef\figureheight{\the\fighei}%
   \ifskipexternalfigures
     \localframed
       [\??ef]
       [\c!breedte=\figwid,
        \c!hoogte=\fighei,
        \c!kader=\v!aan]
       {\ttx name: #2 \\ file: #3 \\ skipped}%
   \else\ifcase\figurestatus
     \localframed
       [\??ef]
       [\c!breedte=\figwid,
        \c!hoogte=\fighei,
        \c!kader=\v!aan]
       {\ttx name: #2 \\ file: #3 \\ unknown}%
   \else
     \localframed
       [\??ef]
       [\c!breedte=\figwid,
        \c!hoogte=\fighei,
        \c!offset=\v!overlay]
       {\vfilll\box\foundexternalfigure}%
   \fi\fi
   \egroup}

\def\getexternalfigure#1%
  {\getvalue{\??ef\??ef#1}}

\def\dopresetfigure[#1][#2][#3]%
  {\getparameters[\??ef][#1,#3]%
   \getparameters[\??ep][#2]}

\def\doprecopfigure[#1][#2][#3]%
  {\def\doplaceexternalfigure[##1][##2][##3][##4][##5]%
     {\getparameters[\??ef][##4,#2,#3]%
      \getparameters[\??ep][##5]}%
   \getvalue{\??ef\??ef#1}}

\def\getnumberoffigurepages#1%
  {\gdef#1{1}% the default 
   \dogetnofinsertpages#1} 

% \useexternalfigure[alpha][koe]
% \useexternalfigure[beta] [koe]       [breedte=1cm]
% \useexternalfigure[gamma][koe][alpha]
% \useexternalfigure[delta][koe][alpha][breedte=2cm]
%
% volle breedte: \externalfigure[koe]                 \par
% 3cm breed:     \externalfigure[koe]  [breedte=3cm]  \par
% volle breedte: \externalfigure[alpha]               \par
% 1cm breed:     \externalfigure[beta]                \par
% volle breedte: \externalfigure[gamma]               \par
% 2cm breed:     \externalfigure[delta]               \par
% 4cm breed:     \externalfigure[beta] [breedte=4cm]  \par
% 5cm breed:     \externalfigure[gamma][breedte=5cm]  \par

\def\douseexternalfigure[#1][#2][#3][#4]%
  {\doifelsenothing{#1}
     {\doifsomething{#2}
        {\dosetuseexternalfigure[#2][#2][#3][#4]}}
     {\doifelsenothing{#2}
        {\dosetuseexternalfigure[#1][#1][#3][#4]}
        {\dosetuseexternalfigure[#1][#2][#3][#4]}}}

\def\dosetuseexternalfigure[#1][#2][#3][#4]%
  {\doifinstringelse{=}{#3}
     {\setvalue{\??ef\??ef#1}{\doplaceexternalfigure[\dopresetfigure][#1][#2][#3][#4]}}
     {\setvalue{\??ef\??ef#1}{\doplaceexternalfigure[\doprecopfigure][#1][#2][#3][#4]}}%
   \doifundefined{#1}{\setvalue{#1}{\getexternalfigure{#1}}}} % upward compatible

\def\useexternalfigure%
  {\doquadrupleempty\douseexternalfigure}

\def\doexternalfigure[#1][#2][#3]%
  {\bgroup
   \doifelsenothing{#1}
     {\framed[\c!breedte=4cm,\c!hoogte=3cm]{external\\figure}}
     {\doifundefinedelse{\??ef\??ef#1}
        {\useexternalfigure[\s!dummy][#1][#2][#3]%
         \getexternalfigure{\s!dummy}[#3]}
        {\getexternalfigure{#1}[#2]}}%
   \egroup}

\unexpanded\def\externalfigure%
  {\dotripleempty\doexternalfigure}

%D Two alternatives, more settings needed.

\def\showexternalfigurea%
  {\bgroup
   \mindermeldingen
   \def\presetfigure[##1][##2]%
     {\useexternalfigure
        [\s!figurepreset][##1]
        [\c!kader=\v!aan,\c!breedte=6cm][##2]%
      \getvalue{\e!start\v!figuur\e!tekst}[\v!links][]
        {\v!geen}
        {\hbox
           {\getvalue{\s!figurepreset}%
            \tfskip
            \framed[\c!breedte=\figurewidth,\c!hoogte=\figureheight]{}}}%
        {\tfa ##1}%
        \blanko
        \tfx
        \def\docommando####1%
          {\beforesplitstring####1\at=\to\asciia
           \aftersplitstring ####1\at=\to\asciib
           \convertcommand\asciib\to\asciib
           \doifsomething{\asciib}
             {\hsmash{\hbox to .75em{\asciia\hss}: \asciib}\endgraf}}%
        \processcommalist[##2]\docommando
        \strut
        \endgraf
      \getvalue{\e!stop\v!figuur\e!tekst}}%
   \pushendofline
   \readjobfile{\@@exfile}{}{}% was \readlocfile
   \popendofline
   \egroup}

\def\showexternalfigureb% instelbaar maken
  {\bgroup
   \def\total{5}%
   \global\let\allfigures=\empty
   \doglobal\newcounter\figurecounter
   \mindermeldingen
   \def\docommando##1{##1&}%
   \def\figurecaptions%
     {\crcr
      \noalign{\nobreak\vskip.5em}%
      \@EA\globalprocesscommalist\@EA[\allfigures]\docommando
      \global\let\allfigures=\empty
      \crcr
      \noalign{\vskip1em\goodbreak}}%
   \def\presetfigure[##1][##2]%
     {\vbox
        {\divide\hsize by \total
         \advance\hsize by -1em
         \useexternalfigure
           [\s!figurepreset][##1]
           [\c!kader=\v!aan,\c!factor=\v!max,\c!breedte=\hsize][##2]%
         \getvalue{\s!figurepreset}}%
      \doglobal\addtocommalist{##1}\allfigures
      \getvalue{\s!figurepreset}%
      \doglobal\increment\figurecounter
      \ifnum\figurecounter=\total
        \doglobal\newcounter\figurecounter
        \def\next{\figurecaptions}%
      \else
        \def\next{&}%
      \fi
      \next}%
   \pushendofline
   \tabskip=\!!zeropoint \!!plus 1fill
   \halign to \hsize
     {&\hss##\hss\cr\readjobfile{\@@exfile}{}{}\crcr % was \readlocfile
      \figurecaptions}
   \popendofline
   \egroup}

\def\showexternalfigurec%
  {\bgroup
   \def\presetfigure[##1][##2]{\pagefigure[##1]}
   \pushendofline
   \readjobfile{\@@exfile}{}{}
   \popendofline
   \egroup}

\def\pagefigure%
  {\dodoubleempty\dopagefigure}

\def\dopagefigure[#1][#2]%
  {\bgroup
   \pagina
   \getfiguredimensions
     [#1][#2]
   \getparameters
     [\??ex][\c!offset=\!!zeropoint,#2]
   \scratchdimen=\@@exoffset
   \dimen0=\figurewidth \advance\dimen0 2\scratchdimen\edef\figurewidth {\the\dimen0}
   \dimen0=\figureheight\advance\dimen0 2\scratchdimen\edef\figureheight{\the\dimen0}
   \definieerpapierformaat
     [\s!dummy][\c!breedte=\figurewidth,\c!hoogte=\figureheight]
   \stelpapierformaatin
     [\s!dummy][\s!dummy]
   \setupcolors
     [\c!status=\v!start]
   \stellayoutin
     [\c!kopwit=\@@exoffset,  \c!rugwit=\@@exoffset,
       \c!hoofd=\!!zeropoint,   \c!voet=\!!zeropoint,
      \c!hoogte=\v!midden,   \c!breedte=\v!midden]
   \externalfigure
     [#1]
     [#2,\c!breedte=\textwidth,\c!hoogte=\textheight]
   \pagina
   \egroup}

% \starttext \pagefigure[two.1] \stoptext

\def\doshowexternalfigures[#1]%
  {\bgroup
   \setupcolors[\c!status=\v!start]% to prevent mps color conversion
   \getparameters[\??ex][\c!variant=a,\c!offset=\!!zeropoint,#1]%
   \getvalue{showexternalfigure\@@exvariant}%
   \egroup}

\def\showexternalfigures%
  {\dosingleempty\doshowexternalfigures}

%D Still undocumented!

\newcount\efreference
\newdimen\efxsteps
\newdimen\efysteps

\def\calculateefsteps%
  {\ifnum0\@@exxmax=0
     \ifnum0\@@exymax=0
       \def\@@exymax{24}%
     \fi
     \efysteps=\figureheight \divide\efysteps by \@@exymax
     \efxsteps=\efysteps
     \dimen0=\figurewidth
     \advance\dimen0 by \efysteps
     \divide\dimen0 by \efysteps
     \edef\@@exxmax{\number\dimen0}%
   \else
     \efxsteps=\figurewidth  \divide\efxsteps by \@@exxmax
     \efysteps=\figureheight \divide\efysteps by \@@exymax
   \fi}

\def\efcomment#1(#2,#3)#4(#5,#6)%    {kader}(x,y)(h,b)[...]{tekst}
  {\def\complexefdocomment[##1]##2%
     {\positioneer(#2,#3)%
        {\setnostrut
         \framed
           [\c!breedte=#5\efxsteps,
            \c!hoogte=#6\exysteps,
            \c!offset=\v!geen,
            \c!kader=#1,
            ##1]%
           {##2}}}%
   \complexorsimpleempty\efdocomment}

\def\efnocomment(#1,#2)#3(#4,#5)%    (x,y)(h,b)[...]{tekst}
  {\def\complexefdonocomment[##1]##2{}%
   \complexorsimpleempty\efdonocomment}

\def\efdomarker(#1,#2)#3#4%    (h,b){kader}{tekst}
  {\framed
     [\c!breedte=#1\efxsteps,
      \c!hoogte=#2\efysteps,
      \c!offset=\v!geen,
      \c!kader=#3]%
     {#4}}

\def\effigure#1%
  {\positioneer(0,0){\getvalue{#1}}}

\def\efdoarea(#1,#2)#3#4%    (h,b){kader}{tekst}
  {\bgroup
   \setnostrut
   \framed
     [\c!breedte=#1\efxsteps,
      \c!hoogte=#2\efysteps,
      \c!offset=\!!zeropoint,
      \c!kader=#3]
     {#4}%
   \egroup}

\def\efgoto(#1,#2)#3[#4]%    (h,b)kader[ref]
  {\setbox0=\vbox{\efdoarea(#1,#2)#3{}}%
   \naarbox{\copy0}[#4]}

\def\efmark(#1,#2)#3(#4,#5)#6[#7]%
  {\advance\efreference by 1
   \positioneer(#1,#2)
     {\hbox{\the\efreference}}%
   \positioneer(#1,#2)
     {\gotosomeinternal
        {\s!vwb}{#7}{\realfolio}
        {\efdomarker(#4,#5){\v!aan}{\thisissomeinternal{\s!vwa}{#7}}}}}

\def\eftext#1(#2,#3)#4(#5,#6)#7[#8]%
  {\advance\efreference by 1
   \hbox
     {\quad
      \thisissomeinternal{\s!vwb}{#8}%
      \gotosomeinternal
        {\s!vwa}{#8}{\realfolio}
        {\hbox to 1.5em{\the\efreference\presetgoto\hfill}}%
      \quad#1 (#2,#3) (#5,#6) [#8]\hfill}%
   \endgraf}

\def\efthisis(#1,#2)#3[#4]%
  {\efdoarea(#1,#2){#3}{\pagereference[#4]}}

\newbox\colorbarbox

\def\makecolorbar[#1]%
  {\def\docommando##1%
     {\color[##1]
        {\blackrule
           [\c!breedte=2em,
            \c!hoogte=1ex,
            \c!diepte=\!!zeropoint]}%
      \endgraf}%
   \global\setbox\colorbarbox=\vbox
     {\forgetall
      \processcommalist[#1]\docommando}%
   \global\setbox\colorbarbox=\vbox
     {\hskip2em\box\colorbarbox}%
   \global\wd\colorbarbox=\!!zeropoint}

\def\placestartfigure[#1][#2][#3]#4\placestopfigure[#5]%
  {\hbox
     {\setbox0=\hbox
        {\useexternalfigure[\s!dummy][#2][#3,#5]%
         \externalfigure[\s!dummy]}%
      \calculateefsteps
      \startpositioneren
        \def\referring(##1,##2)##3(##4,##5)##6[##7]%
          {\positioneer(##1,##2)
             {\efgoto(##4,##5){\@@exhokjes}[##7]}}%
        \def\marking(##1,##2)##3(##4,##5)##6[##7]%
          {\positioneer(##1,##2)
             {\efthisis(##4,##5){\@@exhokjes}[##7]}}%
        \def\remark%
          {\efnocomment}%
        \def\colorbar##1[##2]%
          {}%
        \positioneer(0,0){\box0}%
        \linewidth=1pt
        \stelpositionerenin
          [\c!eenheid=pt,
           \c!xschaal=\withoutpt{\the\efxsteps},
           \c!yschaal=\withoutpt{\the\efysteps},
           \c!factor=1]%
        \ignorespaces#4%
        \def\referring(##1,##2)##3(##4,##5)##6[##7]%
          {}%
        \let\marking=\referring
        \def\remark%
          {\efcomment\v!nee}%
        \def\colorbar##1[##2]%
          {\makecolorbar[##2]}%
        \ignorespaces#4%
      \stoppositioneren
      \box\colorbarbox}}

\def\dodostartfigure[#1][#2][#3]#4\stopfigure%
  {\doifelse{\@@exoptie}{\v!test}
     {\teststartfigure[#1][#2][#3]#4\teststopfigure
      \def\@@exhokjes{\v!aan}}
     {\def\@@exhokjes{\v!uit}}%
   \setvalue{\??ef\??ef#1}%
     {\dosingleempty{\placestartfigure[#1][#2][#3]#4\placestopfigure}}%
   \doifundefined{#1}{\setvalue{#1}{\getexternalfigure{#1}}}}

% De onderstaande macro mag niet zondermeer worden aangepast
% en is afgestemd op gebruik in de handleiding.

\def\teststartfigure[#1][#2][#3]#4\teststopfigure%
  {\begingroup
     \setbox0=\hbox
       {\useexternalfigure[\s!dummy][#2][\c!bfactor=\v!max]%
        \externalfigure[\s!dummy]}%
     \def\referring%
       {\efmark}%
     \def\marking%
       {\efmark}%
     \def\remark%
       {\efcomment\v!ja}%
     \def\colorbar##1[##2]%
       {}%
     \efreference=0
     \setbox0=\vbox
       {\hsize240pt
        \startpositioneren
          \calculateefsteps
          \positioneer(0,0)
            {\box0}%
          \positioneer(0,0)
            {\rooster
               [\c!nx=\@@exxmax,
                \c!dx=\withoutpt{\the\efxsteps},
                \c!ny=\@@exymax,
                \c!dy=\withoutpt{\the\efysteps},
                \c!xstap=1,
                \c!ystap=1,
                \c!schaal=1,
                \c!offset=\v!nee,
                \c!eenheid=pt]}%
          \stelpositionerenin%
            [\c!eenheid=pt,
             \c!xschaal=\withoutpt{\the\efxsteps},
             \c!yschaal=\withoutpt{\the\efysteps},
             \c!factor=1]%
          \linewidth=1pt
          \ignorespaces#4\relax
        \stoppositioneren
        \vfill}%
     \efreference=0
     \def\referring%
       {\eftext{$\rightarrow$}}%
     \def\marking%
       {\eftext{$\leftarrow$}}%
     \def\remark%
       {\efnocomment}%
     \def\colorbar##1[##2]%
       {}%
     \setbox2=\vbox
       {{\tfa\doifelsenothing{#1}{#2}{#1}}
        \blanko
        \tfxx#4
        \vfilll}%
     \ifdim\ht0>\ht2
       \ht2=\ht0
     \else
       \ht0=\ht2
     \fi
     \hbox
       {\hskip3em
        \vtop{\vskip12pt\box0\vskip6pt}%
        \vtop{\vskip12pt\box2\vskip6pt}}%
   \endgroup}

\def\dodostartfigure[#1][#2][#3]#4\stopfigure%
  {\doifelse{\@@exoptie}{\v!test}
     {\teststartfigure[#1][#2][#3]#4\teststopfigure
      \def\@@exhokjes{\v!aan}}
     {\def\@@exhokjes{\v!uit}}%
   \setvalue{\??ef\??ef#1}%
     {\dosingleempty{\placestartfigure[#1][#2][#3]#4\placestopfigure}}%
   \doifundefined{#1}{\setvalue{#1}{\getexternalfigure{#1}}}}

\long\def\dostartfigure#1%
  {\dotripleargument\dodostartfigure#1\stopfigure}

\def\startfigure%
  {\grabuntil{\e!stop\v!figuur}\dostartfigure}

%D \macros
%D   {clip, setupclipping}
%D
%D Although related to figures, clipping can be applied to
%D arbitrary content. We can use \METAPOST\ to provide a non
%D rectangular clipping path.
%D
%D \starttypen
%D \startMPclip{fun}
%D   clip currentpicture to fullcircle
%D     shifted (.5,.5) xscaled \width yscaled \height ;
%D \stopMPclip
%D \stoptypen
%D
%D We get a rectangular piece of the figure when we say:
%D
%D \starttypen
%D \clip[x=2,y=1]{\externalfigure[photo]}
%D \stoptypen
%D
%D When we want to clip to the oval we defined a few lines ago,
%D we say:
%D
%D \starttypen
%D \clip[nx=1,ny=1,x=1,y=1,mp=fun]{\externalfigure[photo]}
%D \stoptypen
%D
%D The general characteristics of clipping can be set up with
%D
%D \showsetup{setupclipping}

% \def\doclip[#1]% nb top->bottom left->right
%   {\bgroup
%    \getparameters[\??cp][#1]%
%    \dowithnextbox
%      {\ifdim\@@cpbreedte>\!!zeropoint
%         \dimen0=\@@cpbreedte
%         \dimen4=\@@cphoffset
%       \else
%         \dimen0=\wd\nextbox
%         \divide\dimen0 \@@cpnx
%         \dimen4=\@@cpx\dimen0
%         \advance\dimen4 -\dimen0
%       \fi
%       \relax % sure
%       \ifdim\@@cphoogte>\!!zeropoint
%         \dimen2=\@@cphoogte
%         \dimen6=\ht\nextbox
%         \advance\dimen6 by -\@@cpvoffset
%         \advance\dimen6 by -\dimen2
%       \else
%         \dimen2=\ht\nextbox
%         \divide\dimen2 \@@cpny
%         \dimen6=-\@@cpy\dimen2
%         \advance\dimen6 \ht\nextbox
%       \fi
%       \setbox\nextbox=\hbox
%         {\hskip-\dimen4\lower\dimen6\box\nextbox}%
%       \wd\nextbox=\!!zeropoint
%       \ht\nextbox=\!!zeropoint
%       \dp\nextbox=\!!zeropoint
%       \setbox\nextbox=\hbox
%         {\dostartclipping{\@@cpmp}{\number\dimen0}{\number\dimen2}%
%          \box\nextbox
%          \dostopclipping}%
%       \wd\nextbox=\dimen0
%       \ht\nextbox=\dimen2
%       \dp\nextbox=\!!zeropoint
%       \box\nextbox
%       \egroup}%
%    \hbox}

\def\doclip[#1]% nb top->bottom left->right
  {\bgroup
   \getparameters[\??cp][#1]%
   \dowithnextbox
     {\ifdim\@@cpbreedte>\!!zeropoint
        \dimen0=\@@cpbreedte
        \dimen4=\@@cphoffset
      \else
        \dimen0=\wd\nextbox
        \divide\dimen0 \@@cpnx
        \dimen4=\@@cpx\dimen0
        \advance\dimen4 -\dimen0
        \dimen0=\@@cpsx\dimen0
      \fi
      \relax % sure
      \ifdim\@@cphoogte>\!!zeropoint
        \dimen2=\@@cphoogte
        \dimen6=\ht\nextbox
        \advance\dimen6 by -\@@cpvoffset
        \advance\dimen6 by -\dimen2
      \else
        \dimen2=\ht\nextbox
        \divide\dimen2 \@@cpny
        \dimen6=-\@@cpy\dimen2
        \advance\dimen6 -\@@cpsy\dimen2
        \advance\dimen6  \dimen2
        \dimen2=\@@cpsy\dimen2
        \advance\dimen6 \ht\nextbox
      \fi
      \setbox\nextbox=\hbox
        {\hskip-\dimen4\lower\dimen6\box\nextbox}%
      \wd\nextbox=\!!zeropoint
      \ht\nextbox=\!!zeropoint
      \dp\nextbox=\!!zeropoint
      \setbox\nextbox=\hbox
        {\dostartclipping{\@@cpmp}{\number\dimen0}{\number\dimen2}%
         \box\nextbox
         \dostopclipping}%
      \wd\nextbox=\dimen0
      \ht\nextbox=\dimen2
      \dp\nextbox=\!!zeropoint
      \box\nextbox
      \egroup}%
   \hbox}

\def\clip{\dosingleempty\doclip}

\def\setupclipping%
  {\dodoubleargument\getparameters[\??cp]}

%D defining sound tracks: 
%D
%D \starttypen 
%D \useexternalsoundtrack[label][file]
%D \stoptypen 
%D
%D associated actions: StartSound StopSound PauseSound ResumeSound
%D
%D Todo: like external figures, also search on path, 
%D although, they need to be present ar viewing time, so ...

\def\useexternalsoundtrack%
  {\dodoubleargument\douseexternalsoundtrack}

\def\douseexternalsoundtrack[#1][#2]% 
  {\setgvalue{\??sd#1}{#2}}

\def\checksoundtrack#1%
  {\iflocation
     \doifdefined{\??sd#1}{\doifvaluesomething{\??sd#1}
       {\doinsertsoundtrack{\getvalue{\??sd#1}}{#1}{}%
        \setgvalue{\??sd#1}{}}}%
   \fi}

\setexecutecommandcheck {startsound} \checksoundtrack

%D The next constant (variable) can be set by the special 
%D driver when asked for. 

\def\noffigurepages{1}

\appendtoks \setupexternalfigures[\c!optie=\v!leeg] \to \everyfastmode
\appendtoks \runMPgraphicsfalse                     \to \everyfastmode
\appendtoks \insertMPgraphicsfalse                  \to \everyfastmode

\appendtoks \flushMPgraphics \to \everylastshipout

\setupexternalfigures
  [\c!optie=,
   \c!object=\v!ja,
   \c!korps=\korpsgrootte,
   \c!gebied=,
   \c!file=\f!utilityfilename.\f!figureextension,
   \c!straal=.5\korpsgrootte,
   \c!hoek=\v!recht,
   \c!kader=\v!uit,
   \c!plaats={\v!lokaal,\v!globaal}]

\setupexternalfigures
  [\c!hokjes=\v!uit,
   \c!ymax=24,
   \c!xmax=]

\useexternalfigure
  [buffer] [\jobname] [\c!type=\v!buffer,\c!object=\v!nee]

\setupclipping
  [\c!nx=2,\c!x=1,\c!sx=1,
   \c!ny=2,\c!y=1,\c!sy=1,
   \c!breedte=\!!zeropoint,\c!hoogte=\!!zeropoint,
   \c!hoffset=\!!zeropoint,\c!voffset=\!!zeropoint,
   mp=]

\protect

\endinput
