%D \module
%D   [       file=core-mps,
%D        version=1999.07.10,
%D          title=\CONTEXT\ Core Macros,
%D       subtitle=\METAPOST\ Connectivity,
%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 / MetaPost Connectivity}

\unprotect 

%D Let's first preload a few auxiliary \METAPOST\ files. 

\appendtoks 
  input mp-tool ; 
  input mp-spec ; 
  _inline_shading_ := true ; 
\to \MPinitializations 

%D The next few macros tell \METAPOST\ how the \CONTEXT\ 
%D pagebody looks. 

\def\writeMPpagedata%
  {\bgroup
   \def\writeMPpagedata ##1 ##2%
     {\scratchdimen=##2%
      \immediate\write\scratchwrite{##1 := \the\scratchdimen;}}%
   \immediate\openout\scratchwrite=\TEXbufferfile{mp-state}\relax
   \writeMPpagedata PaperHeight         \papierhoogte      
   \writeMPpagedata PaperWidth          \papierbreedte     
   \writeMPpagedata PrintPaperHeight    \printpapierhoogte 
   \writeMPpagedata PrintPaperWidth     \printpapierbreedte  
   \writeMPpagedata TopSpace            \kopwit              
   \writeMPpagedata BackSpace           \rugwit              
   \writeMPpagedata MakeupHeight        \zethoogte           
   \writeMPpagedata MakeupWidth         \zetbreedte          
   \writeMPpagedata TopHeight           \bovenhoogte         
   \writeMPpagedata TopDistance         \bovenafstand            
   \writeMPpagedata HeaderHeight        \hoofdhoogte         
   \writeMPpagedata HeaderDistance      \hoofdafstand            
   \writeMPpagedata TextHeight          \teksthoogte         
   \writeMPpagedata FooterDistance      \voetafstand             
   \writeMPpagedata FooterHeight        \voethoogte          
   \writeMPpagedata BottomDistance      \onderafstand            
   \writeMPpagedata BottomHeight        \onderhoogte         
   \writeMPpagedata LeftEdgeWidth       \linkerrandbreedte   
   \writeMPpagedata LeftEdgeDistance    \linkerrandafstand       
   \writeMPpagedata LeftMarginWidth     \linkermargebreedte  
   \writeMPpagedata LeftMarginDistance  \linkermargeafstand      
   \writeMPpagedata TextWidth           \tekstbreedte        
   \writeMPpagedata RightMarginDistance \rechtermargeafstand     
   \writeMPpagedata RightMarginWidth    \rechtermargebreedte 
   \writeMPpagedata RightEdgeDistance   \rechterrandafstand      
   \writeMPpagedata RightEdgeWidth      \rechterrandbreedte
   \writeMPpagedata PageOffset          {\getvalue{\??ma\v!pagina\c!offset}}%
   \writeMPpagedata PageDepth           {\getvalue{\??ma\v!pagina\c!diepte}}%
   \immediate\closeout\scratchwrite
   \egroup}

\prependtoks \flushMPpagedata  \to \everyMPgraphic 

\let\flushMPpagedata\relax

\def\setMPpagedata%
  {\gdef\flushMPpagedata{\writeMPpagedata\global\let\flushMPpagedata\relax}}

%D We have to make sure that the right page data is loaded, 
%D especially when we run multiple jobs on one path. 

\prependtoks \prepareMPpagedata \to \everyMPgraphic 

\def\prepareMPpagedata%
  {\appendtoks 
     input mp-page ;
     def LoadPageState =
       scantokens "input \TEXbufferfile{mp-state}" ;
     enddef ; 
   \to \MPinitializations
   \let\prepareMPpagedata\relax}

%D \macros 
%D   {startuniqueMPgraphic, uniqueMPgraphic}
%D
%D This macros is probably of most use to myself, since I like
%D to use graphics that adapt themselves. The next \METAPOST\
%D kind of graphic is both unique and reused when possible. 
%D 
%D \starttypen 
%D \defineoverlay[example][\uniqueMPgraphic{test}]
%D
%D \startuniqueMPgraphic {test}
%D   draw unitsquare xscaled \overlaywidth yscaled \overlayheight ; 
%D \stopuniqueMPgraphic 
%D \stoptypen

\long\def\startuniqueMPgraphic#1#2\stopuniqueMPgraphic
  {\setvalue{MP:#1}%
     {\startreusableMPgraphic{\overlaystamp:#1}#2\stopreusableMPgraphic
      \reuseMPgraphic{\overlaystamp:#1}}}

\def\uniqueMPgraphic#1%
  {\getvalue{MP:#1}}

\def\overlaystamp%
  {\overlaywidth:\overlayheight:\overlaydepth:\overlaycolor}

%D We redefine a macro from \type {supp-mps.tex}: 

\def\MPdatafile%
  {\bufferprefix mpd-\the\currentMPgraphic.tmp}

%D We also have to make sure that \METAPOST\ knows this: 

\appendtoks 
  _data_prefix_ := "\bufferprefix mpd-" ; 
  _data_suffix_ := ".tmp" ; 
\to \MPinitializations 

%D Happy drawing. 

\protect 

\endinput
