%D \module
%D   [       file=page-bck, % copied from main-001
%D        version=1997.03.31,
%D          title=\CONTEXT\ Page Macros,
%D       subtitle=Backgrounds,
%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 Page Macros / Backgrounds}

\unprotect

\startmessages  dutch  library: layouts
      8: achtergronden berekenen
\stopmessages

\startmessages  english  library: layouts
      8: calculating backgrounds
\stopmessages

\startmessages  german  library: layouts
      8: berechne Hintergrund
\stopmessages

\startmessages  czech  library: layouts
      8: pocita se pozadi
\stopmessages

\startmessages  italian  library: layouts
      8: calcolo dello sfondo
\stopmessages

\startmessages  norwegian  library: layouts
      8: beregner bakgrunn
\stopmessages

\startmessages  romanian  library: layouts
      8: se calculeaza fundalurile
\stopmessages

%D \macros
%D   {recalculatebackgrounds}
%D
%D We use a couple of switches so that we can minimize the
%D amount of background calculations. The main switch is set
%D by the recalculate directive.
%D
%D \starttyping
%D \recalculatebackgrounds
%D \stoptyping
%D
%D Other modules may not directly set the switches
%D themselves.

\newif\ifnewbackground
\newif\ifsomebackground

%D For special purposes, users can question the \type
%D {*background} mode. This mode is only available when
%D typesetting the pagebody.
%D
%D \starttyping
%D \startmode[*background] ...
%D \stoptyping

\appendtoks
  \ifsomebackground \ifnewbackground \setsystemmode\v!background \fi \fi
\to \everybeforepagebody

%D \macros
%D   {addmainbackground, addtextbackground,
%D    addpagebackground, addprintbackground}
%D
%D Apart from the previously mentioned directive, the
%D interface between this module and the other modules
%D is made up by four macros that add background to parts of
%D the layout.
%D
%D \starttyping
%D \addmainbackground  <box>
%D \addtextbackground  <box>
%D \addpagebackground  <box>
%D \addprintbackground <box>
%D \stoptyping

%D To minimize calculations, we keep track of the state of the
%D background of each area. A previous implementation did
%D check each call to the background calculation macro, but
%D using an intermediate usage flag instead of testing each
%D time saves about 3\% on a run with a couple of backgrounds.
%D (On the 824 pages maps bibliography runtime went down from
%D 309 to 299 seconds.)

\def\checkbackground#1%
  {\edef\!!stringe{\??ma#1}%
   \doifelsevaluenothing{\!!stringe\c!background   }
  {\doifelsevaluenothing{\!!stringe\c!foregroundcolor}
         {\doifelsevalue{\!!stringe\c!frame       }\v!on\!!doneatrue
         {\doifelsevalue{\!!stringe\c!leftframe }\v!on\!!doneatrue
         {\doifelsevalue{\!!stringe\c!rightframe}\v!on\!!doneatrue
         {\doifelsevalue{\!!stringe\c!topframe  }\v!on\!!doneatrue
         {\doifelsevalue{\!!stringe\c!bottomframe  }\v!on\!!doneatrue
                                                         \!!doneafalse}}}}}
                                                         \!!doneatrue}
                                                         \!!doneatrue
   \if!!donea
     \setusage  \!!stringe
   \else
     \resetusage\!!stringe
   \fi}

\def\ifsomebackgroundfound#1%
  {\ifusage{\??ma#1}}

% \def\doifsomebackgroundelse#1#2#3%
%   {\ifusage{\??ma#1}#2\else#3\fi}

\def\doifsomebackgroundelse#1%
  {\ifusage{\??ma#1}%
     \expandafter\firstoftwoarguments
   \else
     \expandafter\secondoftwoarguments
   \fi}

%D The background mechanism falls back on the \type {\framed}
%D macro. This means that all normal frame and overlay
%D features can be used.

\def\addsomebackground#1#2#3#4% area box width height / zero test added
  {\ifsomebackgroundfound#1\ifdim#3>\zeropoint\ifdim#4>\zeropoint
     \doifvaluesomething{\??ma#1\c!setups}{\setups[\getvalue{\??ma#1\c!setups}]}% should not produce funny spaces !
     \setbox#2\vbox\fastlocalframed
       [\??ma#1]
       [\c!component=#1,\c!strut=\v!no,\c!offset=\v!overlay,\c!setups=,%
        \c!width=#3,\c!height=#4]
       {\dp#2\zeropoint\box#2}%
   \fi\fi\fi}

%D There are quite some backgrounds. At the bottom layer,
%D there is the {\em paper} background. This one is only
%D used for special purposes, like annotations to documents.

\def\addprintbackground#1%
  {\addsomebackground
     \v!paper#1\printpaperwidth\printpaperheight}

%D The page backgrounds can be put behind the {\em left
%D page}, the {\em right page} or {\em each page}. As with
%D the paper background, these are calculated on each page.

\def\addpagebackground#1%
  {\doifbothsidesoverruled
     \addsomebackground\v!rightpage#1\paperwidth\paperheight
   \orsideone
     \addsomebackground\v!rightpage#1\paperwidth\paperheight
   \orsidetwo
     \addsomebackground\v!leftpage #1\paperwidth\paperheight
   \od
     \addsomebackground\v!page       #1\paperwidth\paperheight}

%D Then there are the 25 areas that make up the layout: {\em
%D top, header, text, footer, bottom} times {\em left edge,
%D left margin, text, right margin, right edge}. These are
%D only recalculated when they change or when the \type
%D {status} is set to \type {repeat}.

\newbox\leftbackground
\newbox\rightbackground

\def\addmainbackground#1% todo: dimension spec
  {\ifsomebackground
     \ifnewbackground \setbackgroundboxes \fi
     \setbox#1\vbox
       {\offinterlineskip
        \doifmarginswapelse
          {\copy\leftbackground}{\copy\rightbackground}%
        \box#1}%
   \fi}

%D Finaly there is an aditional {\em text} background, again
%D useful for special purposes only. This one is calculated
%D each time. The hidden backgrounds are not meant for users!

\newconditional\hiddenbackgroundenabled

\def\addtextbackground#1%
  {\ifconditional\hiddenbackgroundenabled
     \addsomebackground\v!hidden#1\makeupwidth\textheight % mine !
   \fi
   \addsomebackground\v!text#1\makeupwidth\textheight}

% \def\addtextbackground#1%
%   {\ifconditional\hiddenbackgroundenabled
%      \addsomebackground\v!verborgen   #1\zetbreedte\teksthoogte % mine !
%    \fi
%    \doifbothsidesoverruled
%      \addsomebackground\v!rechtertekst#1\zetbreedte\teksthoogte
%    \orsideone
%      \addsomebackground\v!rechtertekst#1\zetbreedte\teksthoogte
%    \orsidetwo
%      \addsomebackground\v!linkertekst #1\zetbreedte\teksthoogte
%    \od
%      \addsomebackground\v!tekst       #1\zetbreedte\teksthoogte}

%D The next couple of macros implement the area backgrounds.
%D As said, these are cached in dedicated boxes. The offsets
%D and depth of the page are used for alignment purposes.

\newdimen\pageoffset % bleed
\newdimen\pagedepth

\let\pagebackgroundhoffset = \!!zeropoint
\let\pagebackgroundvoffset = \!!zeropoint
\let\pagebackgrounddepth   = \!!zeropoint

% \def\setbackgroundboxes
%   {\showmessage\m!layouts8\empty
%    \setbackgroundbox\leftbackground\relax
%    \ifdubbelzijdig
%      \setbackgroundbox\rightbackground\doswapmargins
%    \fi
%    \doifnot\@@mastatus\v!herhaal{\global\newbackgroundfalse}}

%D We need a bit more clever mechanism in order to handle
%D layers well. This means that we cannot calculate both
%D background at the same time since something may have
%D changed halfway a page.

\chardef\newrightbackground0
\chardef\newleftbackground 0

\def\recalculatebackgrounds
  {\global\newbackgroundtrue}

\def\setbackgroundboxes
  {\ifnewbackground
     \global\chardef\newrightbackground\plusone
     \global\chardef\newleftbackground\plusone
     \global\setbox\leftbackground\emptybox
     \global\setbox\rightbackground\emptybox
   \fi
   \doifbothsides
     \ifcase\newleftbackground \else
       % \showmessage\m!layouts8\empty
       \setbackgroundbox\leftbackground\relax
       \global\chardef\newleftbackground\zerocount
       \global\chardef\newrightbackground\zerocount
     \fi
   \orsideone
     \ifcase\newleftbackground \else
       % \showmessage\m!layouts8\empty
       \setbackgroundbox\leftbackground\relax
       \global\chardef\newleftbackground\zerocount
      %\global\chardef\newrightbackground\zerocount
     \fi
   \orsidetwo
     \ifcase\newrightbackground \else
       % \showmessage\m!layouts8\empty
       \setbackgroundbox\rightbackground\doswapmargins
       \global\chardef\newrightbackground\zerocount
     \fi
   \od
   \ifx\@@mastate\v!repeat\else\global\newbackgroundfalse\fi}

\def\addmainbackground#1% todo: dimension spec
  {\ifsomebackground
     \setbackgroundboxes
     \setbox#1\vbox
       {\offinterlineskip
        \doifmarginswapelse
          {\copy\leftbackground}
          {\copy\rightbackground}
        \box#1}%
   \fi}

\def\setbackgroundoffsets
  {\ifsomebackground \ifnewbackground
     \global\let\pagebackgroundhoffset\!!zeropoint
     \global\let\pagebackgroundvoffset\!!zeropoint
     \global\let\pagebackgrounddepth  \!!zeropoint
     \doifsomebackgroundelse{\v!text\v!text}\donetrue\donefalse
     \ifdone\else\doifsomebackgroundelse\v!text\donetrue\donothing\fi
     \ifdone
       \bgroup
       \scratchdimen\getvalue{\??ma\v!page\c!offset}%
       \doifsomebackgroundelse{\v!top\v!text}\donothing
         {\doifsomebackgroundelse{\v!bottom\v!text}\donothing
            {\xdef\pagebackgroundhoffset{\the\scratchdimen}}}%
       \doifsomebackgroundelse{\v!text\v!rightedge}\donothing
         {\doifsomebackgroundelse{\v!text\v!leftedge}\donothing
            {\xdef\pagebackgroundvoffset{\the\scratchdimen}%
             \scratchdimen\getvalue{\??ma\v!page\c!depth}%
             \xdef\pagebackgrounddepth{\the\scratchdimen}}}%
       \egroup
     \fi
   \fi \fi}

\appendtoks \setbackgroundoffsets \to \everybeforepagebody

\newconditional\swapbackgroundmargins \settrue\swapbackgroundmargins

\def\setbackgroundbox#1#2%
  {\global\setbox#1\vbox
     {\dontcomplain
      \swapmargins
      \ifconditional\swapbackgroundmargins
        \doifmarginswapelse \donothing
          {\swapmacros\v!rightmargin\v!leftmargin
           \swapmacros\v!rightedge \v!leftedge}%
      \fi
      \calculatereducedvsizes
      \offinterlineskip
      #2\relax
      \vskip-\topheight
      \vskip-\topdistance
      \dodopagebodybackground\v!top\topheight
      \vskip\topdistance
      \dodopagebodybackground\v!header\headerheight
      \vskip\headerdistance
      \dodopagebodybackground\v!text\textheight
      \vskip\footerdistance
      \dodopagebodybackground\v!footer\footerheight
      \vskip\bottomdistance
      \dodopagebodybackground\v!bottom\bottomheight
      \vfilll}%
  \smashbox#1}

\def\dodopagebodybackground#1#2%
  {\ifdim#2>\zeropoint % added, faster
     \setbox\scratchbox\vbox to #2
       \bgroup\hbox\bgroup
       % \swapmargins
         \goleftonpage
         \dododopagebodybackground\leftedgewidth   #2#1\v!leftedge
         \hskip\leftedgedistance
         \dododopagebodybackground\leftmarginwidth #2#1\v!leftmargin
         \hskip\leftmargindistance
         \dododopagebodybackground\makeupwidth     #2#1\v!text
         \hskip\rightmargindistance
         \dododopagebodybackground\rightmarginwidth#2#1\v!rightmargin
         \hskip\rightedgedistance
         \dododopagebodybackground\rightedgewidth  #2#1\v!rightedge
       \egroup\egroup
     \wd\scratchbox\zeropoint
     \box\scratchbox\relax
   \fi}

\def\dododopagebodybackground#1#2#3#4% width height pos pos
  {\ifsomebackgroundfound{#3#4}%
     \ifdim#2>\zeropoint\relax
       \ifdim#1>\zeropoint\relax
         \doifvaluesomething{\??ma#3#4\c!setups}{\setups[\getvalue{\??ma#3#4\c!setups}]}% should not produce funny spaces !
         \fastlocalframed
           [\??ma#3#4]
           [\c!component=#3-#4,\c!width=#1,\c!height=#2,\c!offset=\v!overlay,\c!setups=]
           {\getvalue{\??ma#3#4\c!command}}% {\hsize=#1\vsize=#2....}
       \else
         \hskip#1%
       \fi
     \else
       \hskip#1%
     \fi
   \else
     \hskip#1%
   \fi}

%D The background mechanism is quite demanding in terms or
%D resources. We used to delay these definitions till runtime
%D usage, but since today's \TEX's are large, we now do the
%D work on forehand.
%D
%D \starttyping
%D \setupbackgrounds [settings]
%D \setupbackgrounds [paper,page,text,..] [settings]
%D \setupbackgrounds [top,...] [leftedge,...] [settings]
%D \stoptyping
%D
%D \showsetup{setupbackgrounds}
%D
%D Because the number of arguments runs from one to three,
%D we need to check for it.

\def\setupbackgrounds
  {\dotripleempty\dosetupbackgrounds}

\def\dosetupbackgrounds[#1][#2][#3]%
  {\ifthirdargument
     \global\somebackgroundtrue
     \def\docommando##1%
       {\doifinsetelse{##1}{\v!paper,\v!page,\v!leftpage,\v!rightpage}
          {\getparameters[\??ma##1][#3]\checkbackground{##1}}
          {\def\dodocommando####1%
             {\getparameters[\??ma##1####1][#3]\checkbackground{##1####1}}%
           \processcommalist[#2]\dodocommando}}%
     \processcommalist[#1]\docommando
   \else\ifsecondargument
     \global\somebackgroundtrue
     \doifcommonelse{#1}{\v!text,\v!hidden,%
                        %\v!linkertekst,\v!rechtertekst,%
                         \v!paper,\v!page,\v!leftpage,\v!rightpage}
       {\def\docommando##1%
          {\getparameters[\??ma##1][#2]\checkbackground{##1}}%
        \processcommalist[#1]\docommando}%
       {\setupbackgrounds
          [#1]%
          [\v!leftedge,\v!leftmargin,\v!text,\v!rightmargin,\v!rightedge]%
          [#2]}%
   \else\iffirstargument
     \getparameters[\??ma][#1]%
   \fi\fi\fi
   \doifelsevalue{\??ma\v!page\c!offset}\v!overlay
     {\global\pageoffset\zeropoint}
     {\global\pageoffset\getvalue{\??ma\v!page\c!offset}}%
   \global\pagedepth\getvalue{\??ma\v!page\c!depth}%
   \xdef\pagebackgroundoffset{\the\pageoffset}%
   \xdef\pagebackgrounddepth {\the\pagedepth }%
   \doifelse\@@mastate\v!stop
     {\global\newbackgroundfalse}
     {\global\newbackgroundtrue }}

\let\pagebackgroundoffset\!!zeropoint
\let\pagebackgrounddepth \!!zeropoint

\appendtoks\global\newbackgroundfalse\to\everyjob

%D Each areas (currently there are $1+3+25+1=30$ of them)
%D has its own low level framed object associated.

\presetlocalframed [\??ma\v!paper]
\presetlocalframed [\??ma\v!page]
\presetlocalframed [\??ma\v!leftpage]
\presetlocalframed [\??ma\v!rightpage]

\copyparameters
  [\??ma\v!paper\c!frame][\??ma\v!page]
  [\c!offset,\c!depth,\c!radius,\c!corner,\c!color,\c!screen]

\copyparameters
  [\??ma\v!paper\c!background][\??ma\v!page]
  [\c!offset,\c!depth,\c!radius,\c!corner,\c!color,\c!screen]

\copyparameters
  [\??ma\v!page\c!frame][\??ma\v!page]
  [\c!offset,\c!depth,\c!radius,\c!corner,\c!color,\c!screen]

\copyparameters
  [\??ma\v!page\c!background][\??ma\v!page]
  [\c!offset,\c!depth,\c!radius,\c!corner,\c!color,\c!screen]

\copyparameters
  [\??ma\v!leftpage\c!frame][\??ma\v!leftpage]
  [\c!offset,\c!depth,\c!radius,\c!corner,\c!color,\c!screen]

\copyparameters
  [\??ma\v!leftpage\c!background][\??ma\v!leftpage]
  [\c!offset,\c!depth,\c!radius,\c!corner,\c!color,\c!screen]

\copyparameters
  [\??ma\v!rightpage\c!frame][\??ma\v!rightpage]
  [\c!offset,\c!depth,\c!radius,\c!corner,\c!color,\c!screen]

\copyparameters
  [\??ma\v!rightpage\c!background][\??ma\v!rightpage]
  [\c!offset,\c!depth,\c!radius,\c!corner,\c!color,\c!screen]

%D We save some keying by defining the areas using
%D intermediate commands. The inheritance macro makes sure
%D that copies are efficient.

\def\dodocommando#1#2%
  {\copylocalframed
     [\??ma#1#2][\??ma\v!page]%
   \getparameters
     [\??ma#1#2]
     [\c!background=,\c!frame=,\c!color=,\c!screen=\@@rsscreen,
      \c!bottomframe=,\c!topframe=,\c!leftframe=,\c!rightframe=]%
   \inheritparameter[\??ma][#1#2\c!color][\v!page\c!color]%
   \inheritparameter[\??ma][#1#2\c!screen][\v!page\c!screen]%
   \inheritparameter[\??ma][#1#2\c!framecolor][\v!page\c!framecolor]%
   \inheritparameter[\??ma][#1#2\c!backgroundcolor][\v!page\c!backgroundcolor]%
   \inheritparameter[\??ma][#1#2\c!backgroundscreen][\v!page\c!backgroundscreen]}

%D The stand alone text area inherits from the page too.

\dodocommando\v!text       \empty
%dodocommando\v!linkertekst \empty
%dodocommando\v!rechtertekst\empty
\dodocommando\v!hidden   \empty

%D We now define all 25 main areas in a row.

\def\docommando#1%
  {\dodocommando#1\v!leftedge
   \dodocommando#1\v!leftmargin
   \dodocommando#1\v!text
   \dodocommando#1\v!rightmargin
   \dodocommando#1\v!rightedge}

\docommando\v!top
\docommando\v!header
\docommando\v!text
\docommando\v!footer
\docommando\v!bottom

%D We need some cleanup now.

\let\dodocommando\relax \let\docommando\relax

%D We now set up the individual areas to use reasonable
%D defaults.

\setupbackgrounds
  [\c!state=\c!start]

\setupbackgrounds
  [\v!paper,\v!page,\v!leftpage,\v!rightpage]
  [\c!frame=\v!off,
   \c!radius=.5\korpsgrootte,
   \c!corner=\v!rectangular,
   \c!background=,
   \c!screen=\@@rsscreen,
   \c!color=,
  %\c!frameoffset=\getvalue{\??ma\v!page\c!offset},
  %\c!backgroundoffset=\getvalue{\??ma\v!page\c!offset},
   \c!offset=\!!zeropoint, % later set to \v!overlay, watch out !
   \c!depth=\!!zeropoint]

\def\docommando#1%
  {\inheritparameter[\??ma][#1\c!frameoffset][\v!page\c!offset]%
   \inheritparameter[\??ma][#1\c!backgroundoffset][\v!page\c!offset]}

\docommando\v!paper
\docommando\v!page
\docommando\v!leftpage
\docommando\v!rightpage

%D Again we clean up temporary macros.

\let\docommando\relax

%D The hidden layer can be populated by extending the
%D following comma separated list. This only happens in core
%D modules.

% todo page-2   .. page+2   achter pagina -> bleed
%      spread-2 .. spread+2 achter spread -> spread (repeat 2 times)

\def\enablehiddenbackground
  {\global\settrue\hiddenbackgroundenabled
   \global\somebackgroundtrue
   \recalculatebackgrounds}

\def\disablehiddenbackground
  {\global\setfalse\hiddenbackgroundenabled}

\def\hiddenbackground
  {\v!text-2,\v!text-1,\v!foreground,\v!text+1,\v!text+2}

\setupbackgrounds
  [\v!hidden]
  [\c!background=\hiddenbackground]

% The next series is used in local (for instance floating)
% backgrounds.

\presetlocalframed
  [\??ma\v!local]

\def\localbackground
  {\v!local-2,\v!local-1,\v!foreground,\v!local+1,\v!local+2}

\defineoverlay[\v!local-2][\positionoverlay{\v!local-2}]
\defineoverlay[\v!local-1][\positionoverlay{\v!local-1}]
\defineoverlay[\v!local+1][\positionoverlay{\v!local+1}]
\defineoverlay[\v!local+2][\positionoverlay{\v!local+2}]

\def\addlocalbackgroundtobox
  {\ifconditional\hiddenbackgroundenabled
     \expandafter\doaddlocalbackground
   \else
     \resetglobal \expandafter\gobbleoneargument
   \fi}

\def\doaddlocalbackground#1%
  {\edef\next
     {\noexpand\redoglobal\wd#1\the\wd#1%
      \noexpand\redoglobal\ht#1\the\ht#1%
      \noexpand\dodoglobal\dp#1\the\dp#1}%
   \dodoglobal\setbox#1\hbox
     {\fastlocalframed
        [\??ma\v!local]
        [\c!component=local,\c!frame=\v!off,\c!offset=\v!overlay,\c!setups=,%
         \c!width=\wd#1,hoogte=\ht#1,% no diepte=\dp#1 in cont-exp.tex, to be checked !
         \c!background=\localbackground]%
        {\registerMPlocaltextarea{\box#1}}}%
   \next
   \doglobal\increment\localpositionnumber\relax} % afterwards !

% Test how previous macro behaves with depth:
%
% \startcolumnset
% \input tufte
% \placefigure{none}{\framed[lines=5]{xxx}}
% \input tufte
% \placefigure{none}{\starttabulate\NC test\nc test\NC\NR\stoptabulate}
% \input tufte
% \stopcolumnset

%D Because we haven't really set up backgrounds yet, we set
%D the main efficiency switch to false.

\somebackgroundfalse

\protect \endinput

%D Removed \quote {features}:
%D
%D \starttyping
%D \startinteraction
%D \doifmarginswapelse
%D   {\copy\leftbackground}
%D   {\copy\rightbackground}%
%D \stopinteraction
%D \stoptyping
%D
%D \starttyping
%D \edef\setpagebackgrounddepth%
%D   {\dp#2=\the\dp#2}%
%D \setbox#2=\vbox\localframed[\??ma#1]{...}
%D \setpagebackgrounddepth
%D \stoptyping
