%D \module
%D   [       file=page-app, % from meta-fig 
%D        version=1998.01.15,
%D          title=\CONTEXT\ Core Macros,
%D       subtitle=Independent page building, 
%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.

%D The fitting page code is moved from \type {meta-fig} to 
%D here. 

\unprotect 

\def\dostartfittingpage[#1][#2]%
  {\pagina % this is kind of tricky! there can be preceding page refs 
   \bgroup % resulting in a zero height page; test fig-make ! 
   \dontcomplain
   \setbox\scratchbox\hbox
     \bgroup
     \getparameters[#1][#2]%
     \schaal[\c!schaal=\getvalue{#1\c!schaal}]\bgroup\localframed[#1]\bgroup}

\definepapersize
  [\??fp\s!dummy]
  [\c!breedte=\fittingwd,
   \c!hoogte=\fittinght]

\definelayout
  [\??fp\s!dummy]
  [\c!breedte=\fittingwd,\c!hoogte=\fittinght,\c!plaats=\v!midden,
   \c!kopwit=\!!zeropoint,\c!rugwit=\!!zeropoint,
   \c!snijwit=\!!zeropoint,\c!bodemwit=\!!zeropoint,
   \c!tekstafstand=\!!zeropoint,\c!regels=0,\c!grid=\v!nee,
   \c!hoofd=\!!zeropoint,\c!voet=\!!zeropoint]

\def\dostopfittingpage
  {\egroup\egroup\egroup
   \edef\fittingwd{\the\wd\scratchbox}%
   \edef\fittinght{\the\ht\scratchbox}%
   \startlocallayout
   \ifdim\fittinght<\lineheight 
     % write status : too small 
     \setbox\scratchbox\vbox to \lineheight{\vss\box\scratchbox\vss}%
     \edef\fittinght{\the\lineheight}%
   \fi
   \setuppapersize
     [\??fp\s!dummy][\??fp\s!dummy]%
   \setuplayout
     [\??fp\s!dummy]%
   \startmakeup[\v!standaard][\c!tekststatus=\v!leeg,\c!dubbelzijdig=\v!nee,\c!pagina=]%
     \centerbox{\box\scratchbox}%
   \stopmakeup
   \stoplocallayout
   \egroup}

%D \TEX\ pages (for \METAPOST\ pages, see \type {meta-fig}): 

\presetlocalframed[\??tg]

\def\setupTEXpage
  {\dodoubleargument\getparameters[\??tg]}

\def\startTEXpage
  {\dosingleempty\dostartTEXpage}

\def\dostartTEXpage[#1]%
  {\dostartfittingpage[\??tg][#1]\gobblespacetokens}

\def\stopTEXpage
  {\removelastspace
   \dostopfittingpage}

\setupTEXpage
  [\c!schaal=1000,
   \c!strut=\v!nee,
   \c!uitlijnen=\v!normaal, % needed, else problems !
   \c!offset=\v!overlay,
   \c!breedte=\v!passend,
   \c!hoogte=\v!passend,
   \c!kader=\v!uit]

%D Application pages (for an example, see \type {m-pstric}): 

\def\@@texapp{texapp}
\def\@@texdim{texdim}

\def\saveTEXapplication#1#2%
  {\immediate\openout\scratchwrite=\bufferprefix\@@texdim.tmp
   \immediate\write\scratchwrite{\dimen#1=\the\ht\scratchbox}%
   \immediate\write\scratchwrite{\dimen#2=\the\wd\scratchbox}%
   \immediate\closeout\scratchwrite}

\def\restoreTEXapplication
  {\readlocfile{\bufferprefix\@@texdim.tmp}\donothing\donothing}

\def\startTEXapplication
  {\dosingleempty\dostartTEXapplication}

\long\def\dostartTEXapplication[#1]#2#3\stopTEXapplication
  {\bgroup
   \bgroup
   \let\f!temporaryextension\c!tex
   \setbuffer[\@@texapp]%
     \starttext 
     #2% preamble 
     \startTEXpage[#1]%
       \setbox\scratchbox\hbox{#3}%
       \saveTEXapplication02% dimensions 
       \box\scratchbox
     \stopTEXpage
     \stoptext 
   \endbuffer
   \egroup
   \doifelse\jobsuffix{dvi}\donetrue\donefalse
   \executesystemcommand{texexec \bufferprefix\@@texapp.tex --once --batch}%
   \ifdone % eps 
     \executesystemcommand{dvips -E* -o \@@texapp.eps \@@texapp}%
   \else % pdf 
     \executesystemcommand{dvips \bufferprefix\@@texapp}%
     \executesystemcommand{ps2pdf \bufferprefix\@@texapp.ps \bufferprefix\@@texapp.pdf}%
   \fi
   \restoreTEXapplication % dimensions 
   \setbox\scratchbox\hbox
     {\expanded{\externalfigure
        [\bufferprefix\@@texapp.\ifdone eps\else pdf\fi]
        [\c!object=\v!nee]}}%
   \setbox\scratchbox\hbox
     {\lower\ht\scratchbox\hbox{\raise\dimen2\box\scratchbox}}%
   \wd\scratchbox\dimen0
   \ht\scratchbox\dimen2
   \dp\scratchbox\zeropoint
   \box\scratchbox
   \egroup}

\protect \endinput 
