%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)}

% \pushmacro -> dedicated fast one

%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\iftracefonthandling % \tracefonthandlingtrue

\newif\ifskiphandlingdef  \skiphandlingdeffalse

% much in common with hz/protruding defs
% todo: fix others

\let\normalchar\char % also done in enco-ini

\def\dosetsomehandling#1#2#3 #4 % no define since directly set
  {\ifskiphandlingdef \else
     \doifnumberelse{\string#2}
       {#1{#2#3}{#4}}
       {\doifelsenothing{#3}
          {#1{`#2}{#4}}
          {\let\char\empty
           \doifnumberelse{\csname#2#3\endcsname}{#1{\csname#2#3\endcsname}{#4}}\donothing
           \let\char\normalchar}}%
   \fi}

\def\dosetpairhandling#1#2#3 #4 #5 % no define since directly set
  {\ifskiphandlingdef \else
     \doifnumberelse{\string#2}
       {#1{#2#3}{#4}{#5}}
       {\doifelsenothing{#3}
          {#1{`#2}{#4}{#5}}
          {\let\char\empty
           \doifnumberelse{\csname#2#3\endcsname}{#1{\csname#2#3\endcsname}{#4}{#5}}\donothing
           \let\char\normalchar}}%
   \fi}

\def\doinhsomehandling#1#2 #3 %
  {\ifskiphandlingdef \else
     \let\char\empty
     \doifnumberelse{\csname#2\endcsname}{#1{\csname#2\endcsname}{`#3}}\donothing
     \let\char\normalchar
   \fi}

% adjustspacing

\newdimen\adjustdimen \let\handledfont\font

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

  \let\enableadjusting     \relax
  \let\disableadjusting    \relax
  \let\setadjusting        \gobbletwoarguments

  \def\defineadjustfactor  #1 #2 {}
  \def\inheritadjustfactor #1 #2 {}

  \let\setfontadjusting    \gobbleoneargument

\else

  \def\enableadjusting  {\pdfadjustspacing\plustwo}
  \def\disableadjusting {\pdfadjustspacing\zerocount}

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

  \def\dododefineadjustfactor#1#2%
    {\adjustdimen#2pt\multiply\adjustdimen\plusthousand\divide\adjustdimen\maxcard\relax
     \efcode\handledfont#1\adjustdimen}

  \beginETEX

  \def\dododefineadjustfactor#1#2%
%     {\efcode\handledfont#1\dimexpr#2pt*\plusthousand/\maxcard\relax}
    {\efcode\handledfont#1\dimexpr#2\onepoint*\plusthousand/\maxcard\relax}

  \endETEX

  \def\dodoinheritadjustfactor#1#2%
    {\efcode\handledfont#1\efcode\handledfont#2\relax}

  \def\defineadjustfactor#1 #2 %
    {\setfonttoks
     \let\inherithandling\inheritadjustfactor
     \appendtoks\dosetsomehandling\dododefineadjustfactor#1 #2 \to\fonttoks}

  \def\inheritadjustfactor#1 #2 %
    {\setfonttoks
     \appendtoks\doinhsomehandling\dodoinheritadjustfactor#1 #2 \to\fonttoks}

  \ifnum\pdftexversion<120
    \let\@@pdfexpandbonus\plusthousand
  \else
    \def\@@pdfexpandbonus{autoexpand}%
  \fi

  \def\setfontadjusting#1%
    {\doifvalue{\@fha@\@fha@#1\c!type}\v!hz{\dosetfontadjusting{#1}}}

  \def\dosetfontadjusting#1%
    {\iftracefonthandling\showfontadjusting{#1}\fi
     \pdffontexpand\handledfont
       \csname\@fha@\@fha@#1\c!min \endcsname\space
       \csname\@fha@\@fha@#1\c!max \endcsname\space
       \csname\@fha@\@fha@#1\c!step\endcsname\space
       \@@pdfexpandbonus\relax}

  \def\showfontadjusting#1%
    {\writestatus\m!handlings{[adjust]\space#1\space
       \csname\@fha@\@fha@#1\c!min \endcsname/%
       \csname\@fha@\@fha@#1\c!max \endcsname/%
       \csname\@fha@\@fha@#1\c!step\endcsname\space
       \fontname\handledfont}}
\fi

% protruding

\newif\ifembasedprotruding \embasedprotrudingfalse

\newdimen\lproddimen \newdimen\rproddimen \let\handledfont\font

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

  \let\enableprotruding       \relax
  \let\disableprotruding      \relax
  \let\setprotrudingfactor    \gobbleoneargument

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

\else

  \def\enableprotruding {\pdfprotrudechars\plustwo}
  \def\disableprotruding{\pdfprotrudechars\zerocount}

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

  \lproddimen1000\onepoint \divide\lproddimen\maxcard
  \rproddimen1000\onepoint \divide\rproddimen\maxcard

  \def\setprotrudingfactor#1%
    {\doifvalue{\@fha@\@fha@#1\c!type}\v!hanging{\dosetprotrudingfactor{#1}}}

  \beginTEX

  \def\dosetprotrudingfactor#1% no \onepoint instead of pt
    {\lproddimen\csname\@fha@\@fha@#1\c!left\endcsname\s!pt
     \multiply\lproddimen\plusthousand\divide\lproddimen\maxcard\relax
     \rproddimen\csname\@fha@\@fha@#1\c!right\endcsname\s!pt
     \multiply\rproddimen\plusthousand\divide\rproddimen\maxcard\relax
     \iftracefonthandling\showprotrudingfactor{#1}\fi}

  \endTEX

  \beginETEX \dimexpr

  \def\dosetprotrudingfactor#1% no \onepoint instead of pt
    {\lproddimen\dimexpr\csname\@fha@\@fha@#1\c!left \endcsname\onepoint*\plusthousand/\maxcard\relax
     \rproddimen\dimexpr\csname\@fha@\@fha@#1\c!right\endcsname\onepoint*\plusthousand/\maxcard\relax
     \iftracefonthandling\showprotrudingfactor{#1}\fi}

  \endETEX

  \def\showprotrudingfactor#1%
    {\writestatus\m!handlings{[protrude]\space#1\space
       \csname\@fha@\@fha@#1\c!left \endcsname\space
       \csname\@fha@\@fha@#1\c!right\endcsname\space
       \fontname\handledfont}}

  \beginETEX \fontcharwd

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

    \newdimen\protrudethreshold \protrudethreshold=36pt

%     keep this

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

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

    \def\dododefineprotrudefactor#1#2#3%
      {\lpcode\handledfont#1\dimexpr#2\lproddimen
         \ifembasedprotruding \else
           \ifdim\fontdimen6\font>\protrudethreshold
             /\fontdimen6\font*\fontcharwd\handledfont#1%
           \else
             *\fontcharwd\handledfont#1/\fontdimen6\font
           \fi
         \fi
       \relax
       \rpcode\handledfont#1\dimexpr#3\rproddimen
         \ifembasedprotruding \else
           \ifdim\fontdimen6\font>\protrudethreshold
             /\fontdimen6\font*\fontcharwd\handledfont#1%
           \else
             *\fontcharwd\handledfont#1/\fontdimen6\font
           \fi
         \fi
       \relax}

  \endETEX

  \beginTEX

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

    \def\protrudethreshold{36\onepoint}

    \def\redodefineprotrudefactor
      {\ifdim\fontdimen6\font>\protrudethreshold
         \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{\handledfont\char#1}% ! yet untested !
       \scratchdimen#2\lproddimen
       \ifembasedprotruding \else
         \redodefineprotrudefactor
       \fi
       \lpcode\handledfont#1\scratchdimen
       \scratchdimen#3\rproddimen
       \ifembasedprotruding \else
         \redodefineprotrudefactor
       \fi
       \rpcode\handledfont#1\scratchdimen}

  \endTEX

  \def\dodoinheritprotrudefactor#1#2%
    {\lpcode\handledfont#1\lpcode\handledfont#2\relax
     \rpcode\handledfont#1\rpcode\handledfont#2\relax}

  \def\defineprotrudefactor#1 #2 #3 %
    {\setfonttoks
     \let\inherithandling\inheritprotrudefactor
     \appendtoks\dosetpairhandling\dododefineprotrudefactor#1 #2 #3 \to\fonttoks}

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

\fi

% font attributes

\let\notagcode\minusone

\ifx\tagcode\undefined

  \def\settagcode #1 #2 {}

\else

  \def\dodosettagcode#1#2%
    {\tagcode\handledfont#1 #2\relax}

  \def\settagcode#1 #2 %
    {\setfonttoks
     \appendtoks\dosetsomehandling\dodosettagcode#1 #2 \to\fonttoks}

\fi

% hook into font mechanism

\let\fonthandling\empty

\beginTEX

  \def\startfonthandling[#1]%
    {\def\fonthandling{#1}%
     \expandafter\ifx\csname\@fha@\fonthandling\endcsname\relax
       \expandafter\newtoks\csname\@fha@\fonthandling\endcsname
     \fi
     \setfonttoks}

\endTEX

\beginETEX

  \def\startfonthandling[#1]%
    {\def\fonthandling{#1}%
     \ifcsname\@fha@\fonthandling\endcsname\else
       \expandafter\newtoks\csname\@fha@\fonthandling\endcsname
     \fi
     \setfonttoks}

\endETEX

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

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

\def\definefonthandling
  {\dotripleempty\dodefinefonthandling}

\def\dodefinefonthandling[#1][#2][#3]%
  {\ifthirdargument
     \setvalue{\@fha@\@fha@#1}{#2}%
     \getparameters[\@fha@\@fha@#1][\c!type=\v!hanging,\c!left=1,\c!right=1,\c!min=20,\c!max=20,\c!step=5,#3]%
   \else
     \setvalue{\@fha@\@fha@\@fha@#1}{#2}%
   \fi}

\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#2%
  {\edef\askedfonthandling{#1}%
   \edef\filoffonthandling{#2}%
   \@EA\ifx\csname\@fha@\@fha@\@fha@\askedfonthandling\endcsname\relax
     \@EA\ifx\csname\@fha@\@fha@\askedfonthandling\endcsname\relax
       \@EAEAEA\nofastenablehandling
     \else
       \@EAEAEA\dofastenablehandling
     \fi
   \else
     \@EA\redofastenablehandling
   \fi}

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

\endTEX

\beginETEX \ifcsname

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

\def\xfastenablehandling#1%
  {\edef\askedfonthandling{#1}%
   \ifcsname\@fha@\@fha@\askedfonthandling\endcsname
     \@EA\dofastenablehandling
   \else
     \@EA\nofastenablehandling
   \fi}

\endETEX

\newtoks\everyenablefonthandling

\ifx\setprotrudingfactor\gobbleoneargument \else
  \appendtoks
     \setprotrudingfactor\askedfonthandling
  \to \everyenablefonthandling
\fi

\ifx\setfontadjusting\gobbleoneargument \else
  \appendtoks
    \setfontadjusting\askedfonthandling
  \to \everyenablefonthandling
\fi

\def\redofastenablehandling
  {\edef\fonthandling{\csname\@fha@\@fha@\@fha@\askedfonthandling\endcsname}%
   \rawprocesscommacommand[\fonthandling]\xfastenablehandling}

\def\dofastenablehandling
  {\the\everyenablefonthandling
   \edef\fonthandling{\csname\@fha@\@fha@\askedfonthandling\endcsname}%
   \checkfonthandling\filoffonthandling
   \rawprocesscommacommand[\fonthandling]\dodoenablehandling
   \registerfonthandling\filoffonthandling}

\def\nofastenablehandling
  {\the\everyenablefonthandling
   \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)!

% not robust as long as tex reuses definitions internally
%
% \def\checkfonthandling#1% we need a fast compare
%   {\doifelsevalue{#1\s!handling\c!file}{\fontname\font}
%      \skiphandlingdeftrue\skiphandlingdeffalse}
%
% \def\registerfonthandling#1%
%   {\@EA\xdef\csname#1\s!handling\c!file\endcsname{\fontname\font}}

\def\checkfonthandling   #1{\skiphandlingdeffalse}
\let\registerfonthandling   \gobbleoneargument

%D This one makes live easier (less definitions):

\def\inherithandling      #1 #2 %
  {\inheritprotrudefactor #1 #2 %
   \inheritadjustfactor   #1 #2 }

%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}%
     {\letvalueempty{\c!file\f!handlingprefix#1}%
      \makeshortfilename[\f!handlingprefix#1]%
      \startreadingfile
      \readsysfile\shortfilename
        {\showmessage\m!handlings2{#1}}
        {\showmessage\m!handlings3{#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,\c!min=20,\c!max=20,\c!stap=5]

\usehandling [def]

\protect \endinput
