%D \module
%D   [       file=hand-ini, % moved from enco-ini / pro
%D        version=2000.12.27, % 1998.12.03,
%D          title=\CONTEXT\ Handling Macros,
%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. 

\writestatus{loading}{Context Handling Macros (ini)}

%D {\em This module is experimental and implements font 
%D specific features, like hanging punctuation.}

\unprotect 

\startmessages  dutch  library: handlings 
  title: handling 
      1: font afhandeling --
      2: font afhandeling -- wordt geladen
      3: onbekende font afhandeling --
\stopmessages

\startmessages  english  library: handlings
  title: handling 
      1: font handling --
      2: font handling -- is loaded
      3: unknown font handling --
\stopmessages

\startmessages  german  library: handlings % to do 
  title: handling 
      1: font handling --
      2: font handling -- is loaded
      3: unknown font handling --
\stopmessages

\startmessages  czech  library: handlings % to do 
  title: handling 
      1: font handling --
      2: font handling -- is loaded
      3: unknown font handling --
\stopmessages

\startmessages  italian  library: handlings % to do 
  title: handling 
      1: font handling --
      2: font handling -- is loaded
      3: unknown font handling --
\stopmessages

\startmessages  norwegian  library: handlings % to do 
  title: handling 
      1: font handling --
      2: font handling -- is loaded
      3: unknown font handling --
\stopmessages

\startmessages  romanian  library: handlings % to do 
  title: handling 
      1: font handling --
      2: font handling -- is loaded
      3: unknown font handling --
\stopmessages

\newif\ifembasedprotruding \embasedprotrudingfalse
\newif\ifskipprotrudingdef \skipprotrudingdeffalse

\newdimen\lproddimen \newdimen\rproddimen \let\prodfont\font 

\ifx\undefined\pdfprotrudechars % we don't use pdftex 

  \let\enableprotruding    \relax
  \let\disableprotruding   \relax 
  \let\setprotrudingfactor \gobbletwoarguments

  \def\defineprotrudefactor  #1 #2 #3 {}
  \def\inheritprotrudefactor #1 #2    {}

\else

  \def\enableprotruding {\pdfprotrudechars2\relax}
  \def\disableprotruding{\pdfprotrudechars0\relax}

  \appendtoks \disableprotruding \to \everyforgetall % Here or not here? 

  \def\setprotrudingfactor#1#2%
    {\lproddimen=#1pt\multiply\lproddimen\plusthousand\divide\lproddimen \!!maxcard\relax
     \rproddimen=#2pt\multiply\rproddimen\plusthousand\divide\rproddimen \!!maxcard\relax}

  \def\dodefineprotrudefactor#1#2 #3 #4 %
    {\ifskipprotrudingdef \else
       \doifelsenothing{#2}
         {\doifnumberelse{\string#1}
            {\dododefineprotrudefactor {#1}{#3}{#4}}
            {\dododefineprotrudefactor{`#1}{#3}{#4}}}
         {\pushmacro\char \let\char\empty
          \doifnumberelse{\csname#1#2\endcsname}
            {\dododefineprotrudefactor{\csname#1#2\endcsname}{#3}{#4}}
            {}%
          \popmacro\char}%
     \fi}

  \def\doinheritprotrudefactor#1 #2 %
    {\ifskipprotrudingdef \else
       \pushmacro\char \let\char\empty
       \doifnumberelse{\csname#1\endcsname}
         {\dodoinheritprotrudefactor{\csname#1\endcsname}{`#2}}
         {}%
       \popmacro\char
     \fi}

  \beginETEX \fontcharwd

    % division before multiplication, else overflow with "sa>5"

    \newdimen\protrudethreshhold \protrudethreshhold=36pt

    \def\redodefineprotrudefactor#1%
      {\ifdim\fontdimen6\font>\protrudethreshhold
         \divide\scratchdimen\fontdimen6\font
         \multiply\scratchdimen\fontcharwd\prodfont#1\relax
       \else
         \multiply\scratchdimen\fontcharwd\prodfont#1\relax
         \divide\scratchdimen\fontdimen6\font
       \fi}

    \def\dododefineprotrudefactor#1#2#3%
      {\scratchdimen#2\lproddimen
       \ifembasedprotruding \else
         \redodefineprotrudefactor{#1}%
       \fi
       \lpcode\prodfont#1\scratchdimen 
       \scratchdimen#3\rproddimen
       \ifembasedprotruding \else
         \redodefineprotrudefactor{#1}%
       \fi
       \rpcode\prodfont#1\scratchdimen} 

  \endETEX

  \beginTEX

    \ifx\undefined\charbox \newbox\charbox \fi 

    \def\redodefineprotrudefactor
      {\ifdim\fontdimen6\font>36pt
         \divide\scratchdimen\fontdimen6\font
         \multiply\scratchdimen\wd\charbox
       \else
         \multiply\scratchdimen\wd\charbox
         \divide\scratchdimen\fontdimen6\font
       \fi}

    \def\dododefineprotrudefactor#1#2#3%
      {\setbox\charbox{\prodfont\char#1}% ! yet untested !   
       \scratchdimen#2\lproddimen
       \ifembasedprotruding \else
         \redodefineprotrudefactor
       \fi
       \lpcode\prodfont#1\scratchdimen 
       \scratchdimen#3\rproddimen
       \ifembasedprotruding \else
         \redodefineprotrudefactor
       \fi
       \rpcode\prodfont#1\scratchdimen} 
 
  \endTEX

  \def\dodoinheritprotrudefactor#1#2%
    {\lpcode\prodfont#1\lpcode\prodfont#2\relax
     \rpcode\prodfont#1\rpcode\prodfont#2\relax}
 
%  \def\dodefinefonthandling[#1][#2][#3]%
%   {\setvalue{\@fha@\@fha@#1}{#2}%
%    \getparameters[\@fha@\@fha@#1][\c!links=1,\c!rechts=1,#3]}
  
  \def\defineprotrudefactor#1 #2 #3 %
    {\setfonttoks
     \appendtoks\dodefineprotrudefactor#1 #2 #3 \to\fonttoks} 

  \def\inheritprotrudefactor#1 #2 %
    {\setfonttoks
     \appendtoks\doinheritprotrudefactor#1 #2 \to\fonttoks} 

\fi

\setprotrudingfactor{1}{1} 

\let\fonthandling\empty

\def\startfonthandling[#1]%
  {\def\fonthandling{#1}%
   \doifundefined{\@fha@\fonthandling}
     {\getparameters[\@fha@\@fha@#1][\c!links=1,\c!rechts=1]% new
      \expanded{\newtoks\csname\@fha@\fonthandling\endcsname}}}

\def\stopfonthandling%
  {\let\fonthandling\empty}

\def\setfonttoks%
  {\@EA\let\@EA\fonttoks\csname\@fha@\fonthandling\endcsname}

\def\definefonthandling%
  {\dotripleempty\dodefinefonthandling}

\def\dodefinefonthandling[#1][#2][#3]%
  {\setvalue{\@fha@\@fha@#1}{#2}%
   \getparameters[\@fha@\@fha@#1][\c!links=1,\c!rechts=1,#3]}

\def\setupfonthandling%
  {\dodoubleempty\dosetupfonthandling}

\def\dosetupfonthandling[#1][#2]%
  {\getparameters[\@fha@\@fha@#1][#2]}  

\def\enablehandling%
  {\dodoubleempty\doenablehandling}

\def\doenablehandling[#1][#2]% handling / symbolic fontname 
  {\fastenablehandling{#1}{#2}} % for the moment the same as:
 
\beginTEX

\def\fastenablehandling#1% 
  {\edef\askedfonthandling{#1}%
   \@EA\ifx\csname\@fha@\@fha@\askedfonthandling\endcsname\relax
     \expandafter\nofastenablehandling
   \else
     \expandafter\dofastenablehandling
   \fi}

\endTEX

\beginETEX \ifcsname

\def\fastenablehandling#1% also gets #2 passed 
  {\edef\askedfonthandling{#1}%
   \ifcsname\@fha@\@fha@\askedfonthandling\endcsname
     \expandafter\dofastenablehandling
   \else
     \expandafter\nofastenablehandling
   \fi}

\endETEX

\def\dofastenablehandling#1% 
  {\setprotrudingfactor 
     {\csname\@fha@\@fha@\askedfonthandling\c!links \endcsname}
     {\csname\@fha@\@fha@\askedfonthandling\c!rechts\endcsname}%
   \edef\fonthandling{\csname\@fha@\@fha@\askedfonthandling\endcsname}%
   \checkfonthandling{#1}%
   \@EA\rawprocesscommalist\@EA[\fonthandling]\dodoenablehandling
   \registerfonthandling{#1}}

\def\nofastenablehandling#1%  
  {\setprotrudingfactor 
     {\csname\@fha@\@fha@\askedfonthandling\c!links \endcsname}
     {\csname\@fha@\@fha@\askedfonthandling\c!rechts\endcsname}%
   \let\fonthandling\askedfonthandling
   \dodoenablehandling\fonthandling}

\beginTEX

\def\dodoenablehandling#1%
  {\@EA\ifx\csname\@fha@#1\endcsname\relax\else
     \the\csname\@fha@#1\endcsname
   \fi}

\endTEX

\beginETEX \ifcsname

\def\dodoenablehandling#1%
  {\ifcsname\@fha@#1\endcsname
     \the\csname\@fha@#1\endcsname
   \fi}

\endETEX

%D We must not use \type {\purefontname} here (was a bug)!

\def\checkfonthandling#1% we need a fast compare
  {\doifelsevalue{#1\s!handling\c!file}{\fontname\font}
     \skipprotrudingdeftrue\skipprotrudingdeffalse}

\def\registerfonthandling#1%
  {%\ifskipprotrudingdef\else\message{#1->#2->\fontname\font}\wait\fi
   \@EA\xdef\csname#1\s!handling\c!file\endcsname{\fontname\font}}

%D \macros 
%D   {usehandling}
%D
%D Handling definitions are collected in dedicated files and
%D loaded only once: 
%D
%D % \showsetup{\y!usehandling}

\def\dousehandling#1% is the same as encoding 
  {\doifundefined{\c!file\f!handlingprefix#1}%
     {\setvalue{\c!file\f!handlingprefix#1}{}%
      \makeshortfilename[\f!handlingprefix#1]%
      \startreadingfile
      \readsysfile{\shortfilename}
        {\showmessage{\m!handlings}{2}{#1}}
        {\showmessage{\m!handlings}{3}{#1}}%
      \stopreadingfile}}

\def\usehandling[#1]%
  {\processcommalist[#1]\dousehandling}

%D New: 

\def\overloadcharacter#1 #2 %
  {\setfonttoks
   \doifnumberelse{\string#2}
     {\appendtoks\dooverloadcharacter{#1}{\char#2 }\to\fonttoks}
     {\appendtoks\dooverloadcharacter{#1}{#2}\to\fonttoks}}

\def\dooverloadcharacter#1%
  {\setvalue{\characterencoding\string#1}}

%D Let's now see if this macro works: 

\setupfonthandling[\s!default][\c!links=1,\c!rechts=1]

\usehandling [def]

\protect \endinput 
