core-inc.mkii / last modification: 2008-02-26 13:52
%D \module
%D   [       file=core-inc, % moved from core-fig
%D        version=2006.08.26, % overhaul of 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.

%  todo: directory : system -> \allinputpaths (so that we can \usesubpath)

%D This is a reimplementation of the original module, which
%D over time had evolved into a pretty complex whole. This
%D was partly due to the fact that we needed to handle many
%D formats, deal with substitute graphics, handle fallbacks
%D and driver specifics (objects), etc. In the meantime we
%D have more clever backends, moved away from texutil to
%D rlxtools, can use runtime or betweentime runs etc. Also,
%D more memory permits a cleaner implementation. Time to
%D move on. We can now also assume that scaling is available.
%D
%D Another mess that can go is the llx/lly handling since
%D drivers now automatically can determine such things.

%D Messages 3 and 5 needs to be translated!

\unprotect

\startmessages  dutch  library: figures
   title: figuren
       1: figuur -- is niet te vinden
       2: figuur -- wordt niet preset
       3: maten van -- worden extern vastgesteld
       4: maten van -- geladen uit figuurfile zelf
       5: maten van -- zijn onbekend
       6: maten van -- berekend door rlxtools
       8: figuurobject -- wordt opnieuw gebruikt
\stopmessages

\startmessages  english  library: figures
   title: figures
       1: figure -- can not be found
       2: figure -- is not preset
       3: dimensions of -- are determined externally
       4: dimensions of -- loaded from figurefile itself
       5: dimensions of -- are unknown
       6: dimensions of -- calculated by rlxtools
       8: figureobject -- is reused
\stopmessages

\startmessages  german  library: figures
   title: Abbildungen
       1: Abbildung -- kann nicht gefunden werden
       2: Abbildung -- wird nicht erstellt
       3: dimensions of -- are determined externally
       4: Dimensionen von -- geladen aus der Abbildungsdatei selbst
       5: Dimensions of -- are unknown
       6: Dimensionen von -- ausgerechnet durch rlxtools
       8: Abbildungobjekt -- wurde wiederverwandt
\stopmessages

\startmessages  czech  library: figures
   title: obrazy
       1: obraz -- nelze nalezt
       2: obraz -- nepritomen
       3: dimensions of -- are determined externally
       4: dimenze obrazu -- nacteny primo z jeho souboru
       5: dimensions of -- are unknown
       6: dimenze obrazu -- spocteny programem rlxtools
       8: obrazovy objekt -- je znovu pouzit
\stopmessages

\startmessages  italian  library: figures
   title: figure
       1: figura -- non trovata
       2: la figura -- non è preimpostata
       3: dimensions of -- are determined externally
       4: dimensioni di -- caricate dal file di immagini stesso
       5: dimensions of -- are unknown
       6: dimensioni di -- calcolate da rlxtools
       8: oggetto-figura -- riutilizzato
\stopmessages

\startmessages  romanian  library: figures
   title: figuri
       1: figura -- nu poate fi gasita
       2: figura -- nu este presetata
       3: dimensions of -- are determined externally
       4: dimensiunea figurii -- se incarca din fisierul insusi
       5: dimensions of -- are unknown
       6: dimensiunea figurii -- este calculata de rlxtools
       8: obiectul figura -- este refolosit
\stopmessages

\startmessages  french  library: figures
   title: figures
       1: la figure -- ne peut être trouvée
       2: la figure -- n'est pas pré-sélectionnée
       3: dimensions of -- are determined externally
       4: les dimensions de -- chargées implicitement à partir du fichier de figure
       5: dimensions of -- are unknown
       6: les dimensions de -- calculées par rlxtools
       8: figureobject -- est réutilisé
\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.

\letempty \@@DriverImageBox
\letempty \@@DriverImageOptions
\letempty \@@DriverImageWidth
\letempty \@@DriverImageHeight
\letempty \@@DriverImageFile
\letempty \@@DriverImageLabel
\letempty \@@DriverImageType
\letempty \@@DriverImageMethod
\letempty \@@DriverImagePage

%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

\let\traceexternalfigures\traceexternalfigurestrue

\def\doshowfigurestate
  {\iftraceexternalfigures
     \expandafter\writestatus\expandafter\m!figures
   \else
     \expandafter\gobbleoneargument
   \fi}

\def\doshowfiguremessage
  {\iftraceexternalfigures
     \expandafter\gobbletwoarguments
   \else
     \expandafter\showmessage\expandafter\m!figures
   \fi}

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

\newif\ifskipexternalfigures  % can be set elsewhere

% \newif\ifrunutilityfile
% \newif\ifconsultutilityfile
%
% Let's save two hash entries:

\let\runutilityfiletrue    \relax \let\runutilityfilefalse    \relax
\let\consultutilityfiletrue\relax \let\consultutilityfilefalse\relax

%D Intermediate, private.

\newdimen\determinedfigurewidth
\newdimen\determinedfigureheight

\let\naturalfigureheight\!!zeropoint
\let\naturalfigurewidth \!!zeropoint

\def\defaultfigurewidth {8\lineheight}
\def\defaultfigureheight{6\lineheight}

\def\defaultfigurepathsignal{(\v!default)}

\def\checknaturalfiguredimensions
  {\edef\naturalfigurewidth{\the\dimexpr\ifzeropt\determinedfigurewidth
     \defaultfigurewidth \else\determinedfigurewidth \fi\relax}%
   \edef\naturalfigureheight{\the\dimexpr\ifzeropt\determinedfigureheight
     \defaultfigureheight\else\determinedfigureheight\fi\relax}}

%D Locating figures. Dilemma: we do support eps and svg parsing but drivers
%D don't always support it.

\def\figuretypes{\c!mps,\c!pdf,\c!eps,\c!svg,\c!svg z,\c!png,\c!tif,jb2,\c!jpg}

\def\supportedfiguretypes{\figuretypes}

\def\checksupportedfiguretypes
  {\begingroup
   \global\let\supportedfiguretypes\empty
   \def\docommand##1%
     {\doiffileinsertionsupportedelse{##1}
        {\doglobal\addtocommalist{##1}\supportedfiguretypes}
        \donothing}%
   \processcommacommand[\figuretypes]\docommand
   \gdef\checksupportedfiguretypes{\let\figuretypes\supportedfiguretypes}%
   \endgroup
   \checksupportedfiguretypes}

%D The next box is used to store the graphic. It's globally assigned.

\newbox\foundexternalfigure

\chardef\figurestatus\zerocount % nothing found

\def\noffigurepages{\nofinsertpages}

%D Variables.

\newtoks\everyexternalfigureresets

\def\resetfigurevariables
  {\the\everyexternalfigureresets}

%D Example usage:

\appendtoks
   \global\let\externalfigurelog\empty
\to\everyexternalfigureresets

%D Intermediate, private

\def\resetprivatefigurevariables
  {\let          \wantedfigurefull      \empty
   \let          \wantedfigurepath      \empty
   \let          \wantedfigurename      \empty
   \let          \wantedfigurebase      \empty
   \let          \wantedfiguretype      \empty
   \let          \wantedfigurefullname  \empty
   \let          \wantedfiguretypespec  \empty
   \let          \wantedfiguremethod    \empty
   \let          \wantedfigurepage      \empty
   \let          \wantedfigureoptions   \empty
   \let          \wantedfigureconversion\empty
   \let          \wantedfigureprefix    \empty
   \let          \wantedfiguretypelist  \figuretypes
   \let          \figurepathlist        \empty
   \chardef      \figurestatus          \zerocount
   \let          \expandedfigurename    \empty
   \global\let   \analyzedfigurewidth   \!!zeropoint % set by indentifying code
   \global\let   \analyzedfigureheight  \!!zeropoint % set by indentifying code
   \global\setbox\foundexternalfigure   \emptybox
   \def          \frozenfigurestamp    {\externalfigurestamp}} % no edef

\resetprivatefigurevariables

\appendtoks
   \resetprivatefigurevariables
\to\everyexternalfigureresets

%D Private/public.

\def\resetpublicfigurevariables
  {\let\figurewidth         \!!zeropoint
   \let\figureheight        \!!zeropoint
   \let\figurenaturalwidth  \!!zeropoint
   \let\figurenaturalheight \!!zeropoint
   \let\figurelabel         \empty
   \let\figurefileoriginal  \empty
   \let\figurefileoptions   \empty
   \let\figurefilename      \empty
   \let\figurefiletype      \empty
   \let\figurefilepage      \!!zerocount
   \let\figurefileconversion\empty
   \let\figurefileprefix    \empty
   \let\figurefilepath      \empty
   \let\figurefilecache     \empty}

\resetpublicfigurevariables

\appendtoks
   \resetpublicfigurevariables
\to\everyexternalfigureresets

\newcounter\figurenestinglevel

\def\pushpublicfigurevariables
  {\ifcase\figurenestinglevel\else
     \doshowfigurestate{variables : push}%
     \globalpushmacro\figurewidth
     \globalpushmacro\figureheight
     \globalpushmacro\figurenaturalwidth
     \globalpushmacro\figurenaturalheight
     \globalpushmacro\figurelabel
     \globalpushmacro\figurefileoriginal
     \globalpushmacro\figurefileoptions
     \globalpushmacro\figurefilename
     \globalpushmacro\figurefiletype
     \globalpushmacro\figurefilepage
     \globalpushmacro\figurefileconversion
     \globalpushmacro\figurefileprefix
     \globalpushmacro\figurefilepath
     \globalpushmacro\figurefilecache
   \fi}

\def\poppublicfigurevariables
  {\ifcase\figurenestinglevel\else
     \doshowfigurestate{variables : pop}%
     \globalpopmacro\figurefilecache
     \globalpopmacro\figurefilepath
     \globalpopmacro\figurefileprefix
     \globalpopmacro\figurefileconversion
     \globalpopmacro\figurefilepage
     \globalpopmacro\figurefiletype
     \globalpopmacro\figurefilename
     \globalpopmacro\figurefileoptions
     \globalpopmacro\figurefileoriginal
     \globalpopmacro\figurelabel
     \globalpopmacro\figurenaturalheight
     \globalpopmacro\figurenaturalwidth
     \globalpopmacro\figureheight
     \globalpopmacro\figurewidth
  \fi}

\def\setpublicfigurevariables % todo: type vs typespec
  {\xdef\figurewidth         {\the\wd\foundexternalfigure}%
   \xdef\figureheight        {\the\ht\foundexternalfigure}%
   \xdef\figurenaturalwidth  {\naturalfigurewidth}%
   \xdef\figurenaturalheight {\naturalfigureheight}%
   \xdef\figurelabel         {\wantedfigurelabel}%
   \xdef\figurefilepath      {\wantedfigurepath}%
   \xdef\figurefilename      {\wantedfigurename}%
   \xdef\figurefiletype      {\wantedfiguretypespec}%
   \xdef\figurefilepage      {\wantedfigurepage}%
   \xdef\figurefileoptions   {\wantedfigureoptions}%
   \xdef\figurefileconversion{\wantedfigureconversion}%
   \xdef\figurefilecache     {\wantedconversioncache}%
   \xdef\figurefileprefix    {\wantedconversionprefix}%
   \xdef\figurefileoriginal  {\wantedconversionname}%
   \xdef\figurefullname      {\wantedfigurepath/\wantedfigurename.\wantedfiguretypespec}%
   \ifcase\figurestatus
     \let\figurefiletype\empty % ?
   \fi}

\def\setpublicfigurescalevariables
  {\edef\figurescalewidth   {\finalscaleboxwidth }%
   \edef\figurescaleheight  {\finalscaleboxheight}%
   \edef\figurescalexscale  {\finalscaleboxxscale}%
   \edef\figurescaleyscale  {\finalscaleboxyscale}}

\def\resetpublicfigurescalevariables
  {\let\figurescalewidth   \!!zeropoint
   \let\figurescaleheight  \!!zeropoint
   \let\figurescalexscale  \!!plusone
   \let\figurescaleyscale  \!!plusone}

\resetpublicfigurescalevariables

\appendtoks
   \resetpublicfigurescalevariables
\to \everyexternalfigureresets

%D The next one is for instance used in symbols. Since
%D we only need to reset some parameters, we can
%D better use the fast alternative:
%D
%D \starttyping
%D \def\resetexternalfigures
%D   {\getparameters[\??ef]
%D      [\c!option=,\c!maxwidth=,\c!maxheight=,
%D       \c!foregroundcolor=,\c!color=,
%D      %\c!conversion=,\c!prefix=,\c!splitcolor=,
%D       \c!frame=\v!off,\c!background=]}
%D \stoptyping
%D
%D This one dropped the runtime of the \MAPS\ bibliography
%D from over 110 seconds down to less than 105 seconds. The
%D tremendously faster (but uglier) implementation is:

\def\resetexternalfigures
  {\let\@@efoption         \empty % \let\@@efprefix\empty
   \let\@@efmaxwidth       \empty % \let\@@efcache \empty
   \let\@@efmaxheight      \empty % \let\@@efframe \v!off
   \let\@@efforegroundcolor\empty
   \let\@@efcolor          \empty
   \let\@@efconversion     \empty
   \let\@@efbackground     \empty}

%D The following code will move:

\appendtoks \resetexternalfigures \to \everyoverlay
\appendtoks \resetexternalfigures \to \everybeforepagebody % not really needed
%appendtoks \resetexternalfigures \to \everysymbol

%D We need this one for bookkeeping:

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

%D Features:

% converted   -> prefix, suffix
% alternative -> other suffix
% buffer      -> prefix

%D Still messy:

\newtoks\everyfiguretypepresets

\def\presetfiguretypeprocessing
  {\the\everyfiguretypepresets}

\def\presetspecialfigure#1%
  {\doif\wantedfiguretype{#1}%
     {\let\@@efobject\v!no
      \let\@@efpreset\v!no
      \ifx\@@efwidth \empty\def\@@efwidth {\defaultfigurewidth }\fi
      \ifx\@@efheight\empty\def\@@efheight{\defaultfigureheight}\fi}}

\appendtoks
  \presetspecialfigure\c!mov
  \presetspecialfigure\c!avi
\to \everyfiguretypepresets

\def\checkformpsfigurefiles % to be checked
  {\doif\wantedfigurename{mprun}
     {\doshowfigurestate{type check : forcing mps (mprun)}%
      \doifnotinstring{^\bufferprefix}{^\wantedfigurename}
        {\edef\wantedfigurename{\bufferprefix\wantedfigurename}}%
      \let\wantedfiguremethod  \c!mps
      \let\wantedfiguretypespec\c!mps}%
   \doifnumberelse\wantedfiguretype
     {\doshowfigurestate{type check : forcing mps (number)}%
      \let\wantedfiguremethod  \c!mps
      \let\wantedfiguretypespec\c!mps}
     \donothing
   \doif\wantedfiguretypespec\c!mps
     {\let\wantedfiguretypelist\wantedfiguretypespec
      \ifcase\EPSspecial\else\ifinobject\else
        \doglobal\increment\forcedMPSobject
        \edef\externalfigurestamp{\c!mps::\forcedMPSobject}%
        \let\@@efobject\v!yes
      \fi\fi}}

\appendtoks
  \checkformpsfigurefiles
\to \everyfiguretypepresets

\def\checkfortexfigurefiles % to be checked (brrr: c!) / brrr: eftype
  {\doifinset\wantedfiguretype{\c!tex,\c!tmp}
     {\let\wantedfiguretypespec  \wantedfiguretype}%
   \doifinset\wantedfiguretypespec{\c!tex,\c!tmp,\v!buffer}
     {\doshowfigurestate{type check : forcing tex (\wantedfiguretypespec)}%
      \let\wantedfiguretypelist\wantedfiguretypespec
      \let\wantedfiguremethod  \c!tex
      \let\@@efobject\v!no
      \doifnothing\wantedfiguretype{\let\wantedfiguretype\c!tmp}%
      % there can be a non buffer \jobname.tmp (made by texexec)
      \doifnotinstring{^\bufferprefix}{^\wantedfigurename}
        {\edef\wantedfigurename{\bufferprefix\wantedfigurename}}}}

\appendtoks
  \checkfortexfigurefiles
\to \everyfiguretypepresets

\def\checkforunknownfigurefiles
  {\doifnothing\wantedfiguretype
     {\dogetcommacommandelement\plusone\from\@@eftype\to\commalistelement
      \edef\wantedfigurefullname{\wantedfigurename.\commalistelement}}}

\appendtoks
  \checkforunknownfigurefiles
\to \everyfiguretypepresets

%  note * : this is needed because reusable graphics
%  combined with funny page aspect aspect ratio's can lead to
%  strange side effects of preceding factor=max specs. This
%  surfaced in the metafun manual, where the two side by
%  side clipped cow heads [the second one was a reused object]
%  where the second one inherited some characteristics from
%  the factor=max one some 30 pages back. Sigh.

\chardef\splitexternalfigure\zerocount % 0 nosplit 1 split/yes 2 split/no

\def\checkfigurecolorsettings
  {% seperation, seldom used
   \doifseparatingcolorselse
     {\let\@@efforegroundcolor\empty
      \doifelsenothing\@@efsplit
        {\chardef\splitexternalfigure\zerocount}
        {\doifcolorchannelelse\@@efsplit
           {\let\@@efobject\v!no  % why?
            \chardef\splitexternalfigure\plusone}
           {\chardef\splitexternalfigure\plustwo}}}
     {\chardef\splitexternalfigure\zerocount}%
   % fake color in gray bitmaps, assumes that
   % a transparent color is used
   \doifsomething\@@efforegroundcolor
     {\def\@@efbackground{\v!foreground,\v!color}%
      \def\@@efbackgroundcolor{\@@efforegroundcolor}}%
   \doifsomething\@@efcolor
     {\doifcolorelse\@@efcolor
        {\checkpredefinedcolor[\@@efcolor]%
         \doregisterfigurecolor\@@efcolor}}%
        \donothing}

\def\setextrafiguredriveroptions
  {\let\@@DriverImageOptions\empty
   \doifsomething\@@efpage          {\addtocommalist\@@efpage  \@@DriverImageOptions}%
   \doif         \@@efpreview \v!yes{\addtocommalist\v!preview \@@DriverImageOptions}%
   \doif         \@@efcontrols\v!yes{\addtocommalist\v!controls\@@DriverImageOptions}%
   \doif         \@@efrepeat  \v!yes{\addtocommalist\v!repeat  \@@DriverImageOptions}%
   \doifinsetelse\@@efsize{mediabox,cropbox,artbox,bleedbox,trimbox}
     {\let \@@DriverImageBox \@@efsize}%
  {\doifinsetelse\@@efsize{media,crop,art,bleed,trim}
     {\edef\@@DriverImageBox{\@@efsize box}}%
     {\let \@@DriverImageBox \empty}}%
   \let\wantedfigureoptions\@@DriverImageOptions}

\def\checkiffigureobjectpresent
  {\doifnot\@@efobject\v!no
     {\doifobjectssupportedelse
        {\doifobjectfoundelse{FIG}\externalfigurestamp
           {\doshowfigurestate{object found : \externalfigurestamp}%
            \getobjectdimensions{FIG}\externalfigurestamp
            \edef\frozenfigurestamp{\externalfigurestamp}%
            \xdef\analyzedfigurewidth {\the\dimexpr\objectwidth \relax}%
            \xdef\analyzedfigureheight{\the\dimexpr\objectheight\relax}%
            \setanalyzedfiguredimensions\plusone}
           {\doshowfigurestate{unknown object: \externalfigurestamp}}}
        {}}}

\def\checkifknownfigureobjectpresent
  {\ifx\wantedfiguretype\empty
     \let\savedwantedfiguretype\wantedfiguretype
     \def\docommand##1%
       {\ifcase\figurestatus
          \edef\wantedfiguretype{##1}%
          \checkiffigureobjectpresent
        \fi}%
     \processcommacommand[\figuretypes]\docommand
     \ifcase\figurestatus
       \let\wantedfiguretype\savedwantedfiguretype
     \fi
   \fi}

\def\checkforfigurefile
  {\ifcase\figurestatus
     \ifconditional\externalfigureflush
        \analyzefigurefiles
     \fi
   \fi}

\def\externalfigurestamp % needs \edef'd macros!
  {\wantedfigurename
   \ifx\wantedfiguretype\empty\else
     \ifx\wantedfiguretype\s!unknown\else
       -\wantedfiguretype
     \fi
   \fi
   \ifx\wantedfiguretypespec\empty\else
     \ifx\wantedfiguretypespec\s!unknown\else
       \ifx\wantedfiguretypespec\wantedfiguretype\else
         -\wantedfiguretypespec
       \fi
     \fi
   \fi
   \ifnum\wantedfigurepage>\zeropoint
     -\wantedfigurepage
   \fi}

\def\checkfigurerenderingoptions
  {\ifcase\figurestatus
     \let\@@efframe\v!on
   \fi
   \doif\@@exoption\v!frame
     {\let\@@efframe\v!on}%
   \doif\@@exoption\v!empty
     {\skipexternalfigurestrue
      \let\@@efframe\v!off}}

\newtoks\externalfigurepostprocessors

\def\resetfigureusersettings
  {\let\@@eftype      \s!unknown      \let\@@efmethod   \empty         \let\@@efpreset\v!yes
   \let\@@eflabel     \empty          \let\@@efsize     \empty         \let\@@efpage  \!!zerocount
   \let\@@efobject    \@@exobject     \let\@@efdisplay  \empty
   \let\@@efsplit     \empty          \let\@@efcolor    \empty         \let\@@efsymbol\v!no
   \let\@@efcontrols  \v!no           \let\@@efpreview  \v!no          \let\@@efrepeat\v!no
   \let\@@efhfactor   \empty          \let\@@efwfactor  \empty         \let\@@effactor\empty
   \let\@@efmaxwidth  \@@exmaxwidth   \let\@@efmaxheight\@@exmaxheight
   \let\@@efxscale    \empty          \let\@@efyscale   \empty         \let\@@efscale \empty
   \let\@@efsx        \!!plusone      \let\@@efsy       \!!plusone
   \let\@@efwidth     \empty          \let\@@efheight   \empty
   \let\@@eflines     \empty          \let\@@efgrid     \empty
   \let\@@efconversion\@@exconversion \let\@@efprefix   \@@exprefix    \let\@@efcache \@@excache}

%D Types and Methods are a bit history. Anyhow, user scan use the
%D type to force the handler. So, what to do with the method. We can
%D use that one to force a handler with a given suffix, so when no
%D type is given, but a suffix is part of the name, the method will
%D determine the handler.

\def\checkfigureusersettings
  {\doif\@@efreset\v!yes\resetexternalfigures
   \doifelsenothing\@@eflabel
     {\doifnothing\wantedfigurelabel{\let\wantedfigurelabel\wantedfigurename}}%
     {\let\wantedfigurelabel\@@eflabel}%
   \doifsomething\@@eftype
     {\doifnot\@@eftype\s!unknown
        {\edef\wantedfiguretypespec{\@@eftype}%
         \let\wantedfiguremethod\wantedfiguretypespec}}%
   \doifnothing\wantedfigurepage % can be set by plug in
     {\let\wantedfigurepage\@@efpage}%
   \doif\wantedfigurepage\empty
     {\let\wantedfigurepage\!!zerocount}% 0 is signal !
   \doifsomething\@@efmethod % rather untested misusage of the remapper
      {\doifsomething\wantedfiguretype
         {\definegraphictypesynonym[\wantedfiguretype][\@@method]}}}

% #1 is now obsolete

\def\calculateexternalfigure[#1][#2][#3][#4][#5][#6]% \cmd label filename parent_id preset current
  {\doshowfigurestate{begin}%
   \dontcomplain
   % let's limit the search, which means that e.g. svg has to be given explicitly
   \checksupportedfiguretypes
   % recently added; we presume local use
   \restorecatcodes
   % collected resets (token list)
   \resetfigurevariables
\resetwantedconversionvariables % new here
   % analyze filename and set wanted variables
   \analyzefigurefilename{#3}{#2}%
   \doanalyzefiguredimensionsfromfile
   % handle user settings
   \resetfigureusersettings
   \dosetefparameters{#4}{#5}{#6}%
   \checkfigureusersettings
   \checkfigurecolorsettings
   % adapt settings based on suffix and/or type
   \presetfiguretypeprocessing
   % now we really start
   \checkiffigureobjectpresent   % first guess, we may not yet know the typespec
   \checkifknownfigureobjectpresent
   \checkforfigurefilepresence
   \checkiffigureobjectpresent   % to be sure, in case we now know the typespec
   \checkfigurerenderingoptions  % was later, moved here
   \checknaturalfiguredimensions % inherit from global values and/or fallbacks
   % by now we know what we're dealing with (put in box and scale)
   \setextrafiguredriveroptions
   \prepackageexternalfigureobject
   % set public variables in case postprocessing needs them
   \pushpublicfigurevariables
   \setpublicfigurevariables
   \setpublicfigureconversionvariables
   \setpublicfigurescalevariables
   % package final graphic, only now we can apply backgrounds and such
   \doglobal\increment\figurenestinglevel
   \finishexternalfigure
   \doglobal\decrement\figurenestinglevel
   % restore variables
   \poppublicfigurevariables
   \doshowfigurestate{end}}

\def\checkforfigurefilepresence
  {\checkforconvertedfigure
   \checkforfigurefile}

%D Figure objects.

\def\setfigureobject
  {\doshowfigurestate{object set : \externalfigurestamp}%
   \setobject{FIG}\externalfigurestamp}

% \def\getfigureobject
%   {\doshowfigurestate{object used : \externalfigurestamp}%
%    \getobject{FIG}\externalfigurestamp}

\def\getfigureobject
  {\doshowfigurestate{object used : \frozenfigurestamp}%
   \getobject{FIG}\frozenfigurestamp}

\def\prepackageexternalfigureobject
  {\ifcase\figurestatus
     \doshowfiguremessage1\expandedfigurename
     \doshowfigurestate{state : figure not found (\expandedfigurename)}%
     \global\setbox\foundexternalfigure\naturalvbox
       {\doscalebox\??ef{\blackrule[\c!width=\naturalfigurewidth,\c!height=\naturalfigureheight]}}%
   \or
     \doshowfiguremessage8\expandedfigurename
     \doshowfigurestate{state : reusing existing figure}%
     \global\setbox\foundexternalfigure\naturalvbox
       {\doscalebox\??ef{\dowithfigure{\getfigureobject}}}%
     \xdef\noffigurepages{\number\getvalue{\externalfigurestamp\c!n}}%
   \or
     \doshowfiguremessage2\expandedfigurename
     \doshowfigurestate{state : using special figure}%
     \setbox\scratchbox\naturalvbox % make a dummy
       {\doscalebox\??ef{\blackrule[\c!width=\naturalfigurewidth,\c!height=\naturalfigureheight]}}%
     \global\setbox\foundexternalfigure\naturalvbox to \finalscaleboxheight
       {\vfill
        \hsize\finalscaleboxwidth
        \dowithfigure{\insertscaledfiguredriverdata}}%
     \xdef\noffigurepages{\number\nofinsertpages}%
   \else
     \ifdim\naturalfigurewidth>\zeropoint
       \ifnum\figurestatus>\!!ten\relax
         \doshowfiguremessage3\expandedfigurename
       \else
         \doshowfiguremessage4\expandedfigurename
       \fi
     \else
       \doshowfiguremessage5\expandedfigurename
     \fi
     \doshowfigurestate{state : using found figure}% 3=self 4=rlx
     \doifelse\@@efobject\v!no
       {\donefalse}
       {\doifobjectssupportedelse\donetrue\donefalse}%
     \ifdone
       % make an object and use it
       \packageexternalfigureobject
       \setfigureobject\vbox{\box\foundexternalfigure}%
       \setxvalue{\externalfigurestamp\c!n}{\number\nofinsertpages}%
       \global\setbox\foundexternalfigure\naturalvbox
         {\doscalebox\??ef{\dowithfigure{\getfigureobject}}}%
       \xdef\noffigurepages{\number\getvalue{\externalfigurestamp\c!n}}%
     \else
       % maybe a tex figure
       \global\setbox\foundexternalfigure\naturalvbox
         {\doscalebox\??ef{\dowithfigure{\box\foundexternalfigure}}}%
       \xdef\noffigurepages{\number\nofinsertpages}%
     \fi
   \fi
   \global\wd\foundexternalfigure\finalscaleboxwidth
   \global\ht\foundexternalfigure\finalscaleboxheight
   \global\let\lastfigureobjectname\externalfigurestamp
   \doresetobjects} % clean up driver left overs

\def\packageexternalfigureobject
  {\global\setbox\foundexternalfigure\vbox to \naturalfigureheight
     {\vfill
      \ifdim\wd\foundexternalfigure=\zeropoint
        \setextrafiguredriveroptions
        \insertunscaledfiguredriverdata
      \else\ifskipexternalfigures
        \ruledhbox{\backgroundline[\@@efsplitcolor]{\fakebox\foundexternalfigure}}%
      \else
        \box\foundexternalfigure
      \fi\fi}