%D \module
%D   [       file=meta-pag,
%D        version=1999.07.10,
%D          title=\METAPOST\ Graphics,
%D       subtitle=Initialization,
%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 These definitions used to be part of the old \type
%D {core-mps} file, later changed into \type {meta-ini}, but
%D keeping them separate is cleaner.

\writestatus{loading}{MetaPost Graphics / Page Data Management}

\unprotect

\appendtoks
  if unknown context_page : input mp-page ; fi ;
\to \MPinitializations

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

\startuseMPgraphic{page data}
  boolean PageStateAvailable, OnRightPage, InPageBody ;
  PageStateAvailable := OnRightPage := true ;
  InPageBody         := \ifinpagebody true \else false \fi ;
  def LoadPageState =
    OnRightPage         :=      \MPonrightpage ;
    RealPageNumber      :=  \the\realpageno ;
    PageNumber          :=  \the\pageno ;
    NOfPages            :=      \lastpage ;
    PaperHeight         :=  \the\papierhoogte ;
    PaperWidth          :=  \the\papierbreedte ;
    PrintPaperHeight    :=  \the\printpapierhoogte ;
    PrintPaperWidth     :=  \the\printpapierbreedte ;
    TopSpace            :=  \the\kopwit ;
    BottomSpace         :=  \the\bodemwit ;
    BackSpace           :=  \the\rugwit ;
    CutSpace            :=  \the\snijwit ;
    MakeupHeight        :=  \the\zethoogte ;
    MakeupWidth         :=  \the\zetbreedte ;
    TopHeight           :=  \the\bovenhoogte ;
    TopDistance         := \@the\bovenafstand ;
    HeaderHeight        :=  \the\hoofdhoogte ;
    HeaderDistance      := \@the\hoofdafstand ;
    TextHeight          :=  \the\teksthoogte ;
    FooterDistance      := \@the\voetafstand ;
    FooterHeight        :=  \the\voethoogte ;
    BottomDistance      := \@the\onderafstand ;
    BottomHeight        :=  \the\onderhoogte ;
    LeftEdgeWidth       :=  \the\linkerrandbreedte ;
    LeftEdgeDistance    := \@the\linkerrandafstand ;
    LeftMarginWidth     :=  \the\linkermargebreedte ;
    LeftMarginDistance  := \@the\linkermargeafstand ;
    TextWidth           :=  \the\tekstbreedte ;
    RightMarginDistance := \@the\rechtermargeafstand ;
    RightMarginWidth    :=  \the\rechtermargebreedte ;
    RightEdgeDistance   := \@the\rechterrandafstand ;
    RightEdgeWidth      :=  \the\rechterrandbreedte ;
    InnerMarginDistance := \@the\innermargindistance ;
    InnerMarginWidth    :=  \the\innermarginwidth ;
    OuterMarginDistance := \@the\outermargindistance ;
    OuterMarginWidth    :=  \the\outermarginwidth ;
    InnerEdgeDistance   := \@the\inneredgedistance ;
    InnerEdgeWidth      :=  \the\inneredgewidth ;
    OuterEdgeDistance   := \@the\outeredgedistance ;
    OuterEdgeWidth      :=  \the\outeredgewidth ;
    PageOffset          :=  \the\pageoffset ;
    PageDepth           :=  \the\pagedepth ;
  enddef ;
\stopuseMPgraphic

\def\MPonrightpage{true}

\def\freezeMPpagelayout%
  {% the \edef\MPonrightpage{....} alternative is slower
   \doifbothsides
     \def\MPonrightpage{true}%
   \orsideone
     \def\MPonrightpage{true}%
   \orsidetwo
     \def\MPonrightpage{false}%
   \od}

\iffixedlayoutdimensions

  \let\freezeMPlayout\relax

\else

  \def\freezeMPlayout% must be done more efficient
    {\freezedimenmacro\bovenafstand
     \freezedimenmacro\hoofdafstand
     \freezedimenmacro\voetafstand
     \freezedimenmacro\onderafstand
     \freezedimenmacro\linkerrandafstand
     \freezedimenmacro\linkermargeafstand
     \freezedimenmacro\rechtermargeafstand
     \freezedimenmacro\rechterrandafstand}

\fi

%D We need to freeze the pagelayout before the backgrounds
%D are build, because the overlay will temporarily become
%D zero (overlay).

\appendtoks
  \freezeMPpagelayout
\to \everybeforepagebody

%D By freezing these value every graphic, we can use layout
%D variables that change halfways a page, whatever use that
%D has.

\prependtoks
  \calculatereducedvsizes % this is really needed
  \freezeMPpagelayout
  \freezeMPlayout % to be used grouped
\to \everyMPgraphic

\appendtoks
  \includeMPgraphic{page data}
\to \MPinitializations

%D The next feature provides information about for instance
%D column positions. This is an experimental feature,
%D introduced when we needed backgrounds in columns (fill||in
%D questions as implemented in a private module).
%D
%D See \type {mp-page.mp} for the definition of the macros:
%D
%D \startabulatie[|tl|l|p|]
%D \NC ResetTextAreas        \NC no arguments \NC 
%D     reset areas on page                    \NC \NR
%D \NC RegisterTextArea      \NC x, y, w, h   \NC 
%D     adds area to the list                  \NC \NR
%D \NC TextAreaX,Y,W,H,XY,WH \NC x and/or y   \NC 
%D     reports offsets and dimensions         \NC \NR 
%D \stoptabulatie
%D
%D The \type {TextArea*} macros can be used to determine 
%D overlap. 

\newcounter\currentMPtextareadata

\newtoks\MPsavedtextareadata 
\newtoks\MPtextareadata      
\newtoks\MPlocaltextareadata      

% optimaliseren voor herhaling 

\def\registerMPtextarea#1%
  {\ifpositioning 
     \bgroup
     \doglobal\increment\currentMPtextareadata
    %\hpos{gbd:\currentMPtextareadata}{#1}%
     \hpos{gbd:\currentMPtextareadata}%
       {\iftracetextareas\boxrulewidth1.5pt\ruledhbox\fi{#1}}%
     \edef\!!stringa{gbd:\currentMPtextareadata}%
     \edef\!!stringa{RegisterTextArea(%
       \MPx\!!stringa,\MPy\!!stringa,%
       \MPw\!!stringa,\MPh\!!stringa,\MPd\!!stringa);}%
     \@EA \doglobal \@EA \appendtoks \!!stringa \to \MPtextareadata
     \egroup
   \else
     \hbox{#1}%
   \fi}

\def\registerMPlocaltextarea#1%
  {\ifpositioning   
     \bgroup
     \doglobal\increment\currentMPtextareadata
    %\hpos{gbd:\currentMPtextareadata}{#1}%
     \hpos{gbd:\currentMPtextareadata}%
       {\iftracetextareas\boxrulewidth3pt\ruledhbox\fi{#1}}%
     \edef\!!stringa{gbd:\currentMPtextareadata}%
     \edef\!!stringa{RegisterLocalTextArea(%
       \MPx\!!stringa,\MPy\!!stringa,%
       \MPw\!!stringa,\MPh\!!stringa,\MPd\!!stringa);}%
     \global\MPlocaltextareadata\@EA{\!!stringa}%
     \egroup
   \else
     \hbox{#1}%
   \fi}

\def\resetMPlocaltextarea
  {\global\MPlocaltextareadata\emptytoks}

\appendtoks
  \includeMPgraphic{area data}%
\to \MPinitializations

\startuseMPgraphic{area data}
  ResetTextAreas ; 
  \the\MPsavedtextareadata
  SaveTextAreas ; 
  ResetTextAreas ; 
  \the\MPtextareadata
  \the\MPlocaltextareadata
\stopuseMPgraphic

\appendtoks
  \global\MPsavedtextareadata\MPtextareadata
  \global\MPtextareadata\emptytoks
  \global\MPlocaltextareadata\emptytoks
\to \everyshipout

\protect \endinput
