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

\unprotect 

\startmessages  dutch  library: layouts
      7: beeldmerken berekenen
\stopmessages

\startmessages  english  library: layouts
      7: calculating logospace
\stopmessages

\startmessages  german  library: layouts
      7: berechne Platz des Logo
\stopmessages

\startmessages  czech  library: layouts
      7: pocita se misto pro logo
\stopmessages

\startmessages  italian  library: layouts
      7: calcolo dello spazio per logo
\stopmessages

\startmessages  norwegian  library: layouts
      7: beregner plass for logo
\stopmessages

\startmessages  romanian  library: layouts
      7: se calculeaza spatiul pentru logo
\stopmessages

%D Although logos can conveniently be implemented on top of
%D background and text areas, we provide a dedicated mechanism
%D here. One reason is that such a separate mechanism cannot
%D interfere with the other ones, but an even more important
%D reason is that logos are kind of special in the sense that
%D they have a short life span and may change after the first
%D page. 

%D \macros
%D  {recalculatelogos,addlogobackground}
%D
%D The interface to the other low level page building routines
%D is provided by a macro that signals changes in layout
%D specifications: 
%D 
%D \starttypen 
%D \recalculatelogos
%D \stoptypen 
%D
%D as well as a simple placement macro: 
%D
%D \starttypen 
%D \addlogobackground <box> 
%D \stoptypen 
%D
%D In no way the following boolean switch should be used 
%D directly. 

\newif\ifnewlogos

\def\recalculatelogos
  {\global\newlogostrue} 

%D The current state of logos is registered in a status 
%D variable \type {\logostatus}. 
%D 
%D \starttabulatie[|l|l|l|]
%D \NC 0 \NC don't place              \NC remains 0 \NC \NR 
%D \NC 1 \NC place now                \NC remains 1 \NC \NR 
%D \NC 2 \NC calulate and place       \NC becomes 1 \NC \NR 
%D \NC 3 \NC calculate and place once \NC becomes 2 \NC \NR 
%D \stoptabulatie 

\chardef\logostatus=0

\def\addlogobackground#1% todo: dimension spec 
  {\ifcase\logostatus \else
     \ifcase\logostatus
       % no logos to take care of  
     \or % 1 
       \ifnewlogos 
         \chardef\logostatus2
         \setlogoboxes
         \chardef\logostatus1
         \global\newlogosfalse
       \fi 
     \or % 2 
       \setlogoboxes
       \chardef\logostatus1
     \or % 3 
       \setlogoboxes
       \global\chardef\logostatus2
     \fi
     \setbox#1\vbox
       {\offinterlineskip
        \doifmarginswapelse
          {\copy\leftlogos}
          {\copy\rightlogos}
        \box#1}%
   \fi}

%D For efficiency reasons (and since logos seldom change inside
%D a document) we can save the left and right hand (or first
%D and following page) logos in boxes. The areas are slightly
%D different from the ones used in backgrounds and text
%D placement, but still related to the page layout. The {\em
%D left} and {\em right edge}, as well as {\em top} and {\em
%D bottom} touch the bounding box of the paper and are
%D therefore not the sams as their background adn text
%D counterparts. In addition there are {\em left}, {\em right}
%D and {\em middle} areas as well as a {\em page} one. 

\newbox\leftlogos
\newbox\rightlogos

\def\setlogoboxes
  {\showmessage\m!layouts7\empty
   \dosetlogobox\leftlogos\relax
   \ifdubbelzijdig
     \dosetlogobox\rightlogos\doswapmargins
   \fi}

\def\dosetlogobox#1#2%
  {\global\setbox#1\vbox to \papierhoogte
     {\dontcomplain           % needed here ? 
      \calculatereducedvsizes % needed here ? 
      \offinterlineskip
      #2\relax
      \vskip-\kopwit
      \dodosetlogobox\v!boven\blap 
      \vskip\kopwit
      \dodosetlogobox\v!hoofd\blap 
      \vskip\hoofdhoogte 
      \vskip\hoofdafstand
      \dodosetlogobox\v!tekst\blap 
      \vskip\teksthoogte
      \vskip\voetafstand 
      \vskip\voethoogte
      \dodosetlogobox\v!voet \tlap  
      \vfilll
      \dodosetlogobox\v!onder\tlap 
      \vskip\kopwit} 
  \smashbox#1}

\def\dodosetlogobox#1#2%
  {\hbox % width equals \zetbreedte 
     {\def\docommando##1%
        {\donefalse
         \ifnum\logostatus=3 \ExpandBothAfter
           \doifinset{\getvalue{\??lo#1##1}}\requestedlogos\donetrue
         \else
           \doifvalue{\??lo#1##1\c!status}\v!start\donetrue
         \fi
         \ifdone
           #2{\hbox{\getvalue{\??lo#1##1\c!commando}}}%
         \fi}%
      \def\dodocommando##1##2##3##4##5##6%
        {\hsmash
           {\hskip-\texthoffset
            \hbox to \papierbreedte
              {\rlap{\docommando##1}\hss\llap{\docommando##6}}%
            \hskip-\papierbreedte
            \hbox to \papierbreedte
              {\hskip\texthoffset
               \hskip-\linkermargebreedte
               \hskip-\linkermargeafstand
               \hbox to \linkermargebreedte{\docommando##2\hss}%
               \hskip\linkermargeafstand
               \hbox to \zetbreedte{\docommando##3\hss\docommando##4}%
               \hskip\rechtermargeafstand
               \hbox to \rechtermargebreedte{\hss\docommando##5}%
               \hfill}}}%
      \normalbaselines
      \settexthoffset
      \hsmash
        {\hbox to \zetbreedte{\hss\docommando\c!midden\hss}}%
      \hsmash
        {\hskip-\texthoffset
         \hbox to \papierbreedte{\docommando\v!pagina\hss}}%
      \swapmargins
      \doifbothsidesoverruled
        \dodocommando
          \v!linkerrand \v!linkermarge  \v!links
          \v!rechts     \v!rechtermarge \v!rechterrand
      \orsideone
        \dodocommando
          \v!linkerrand \v!linkermarge  \v!links
          \v!rechts     \v!rechtermarge \v!rechterrand
      \orsidetwo
        \dodocommando
          \v!rechterrand \v!rechtermarge \v!rechts
          \v!links       \v!linkermarge  \v!linkerrand
      \od}}

%D The user interface is relatively simple and provides 
%D macros for assigning logos to logo areas as well as 
%D forcing placement. 
%D
%D \showsetup{\y!definelogo}
%D \showsetup{\y!placelogos}

\let\definedlogos  \empty
\let\requestedlogos\empty

\long\def\dodefinelogo[#1][#2][#3][#4]%
  {\addtocommalist{#1}\definedlogos
   \long\setvalue{\??lo#2#3}{#1}%
   \getparameters[\??lo#2#3][#4]%
   \global\chardef\logostatus2 }

\def\definelogo
  {\doquadrupleargument\dodefinelogo}

\def\placelogos
  {\dosingleempty\doplacelogos}

\def\doplacelogos[#1]%
  {\xdef\requestedlogos{\iffirstargument#1\else\definedlogos\fi}%
   \global\chardef\logostatus3 }

\protect \endinput 
