%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

\startmessages  french  library: handlings
  title: manipulation
      1: manipulation -- de police
      2: la manipulation -- de police est chargée
      3: manipulation -- inconnue de police
\stopmessages

\newif\iftracefonthandling % \tracefonthandlingtrue

\newif\ifskiphandlingdef  \skiphandlingdeffalse

\newtoks\everyenablefonthandling

% 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\dosettriplethandling#1#2#3 #4 #5 #6 % no define since directly set
%   {\ifskiphandlingdef \else
%      \doifnumberelse{\string#2}
%        {#1{#2#3}{#4}{#5}{#6}}
%        {\doifelsenothing{#3}
%           {#1{`#2}{#4}{#5}{#6}}
%           {\let\char\empty
%            \doifnumberelse{\csname#2#3\endcsname}{#1{\csname#2#3\endcsname}{#4}{#5}{#6}}\donothing
%            \let\char\normalchar}}%
%    \fi}

\def\dosetquartethandling#1#2#3 #4 #5 #6 #7 % no define since directly set
  {\ifskiphandlingdef \else
     \doifnumberelse{\string#2}
       {#1{#2#3}{#4}{#5}{#6}{#7}}
       {\doifelsenothing{#3}
          {#1{`#2}{#4}{#5}{#6}{#7}}
          {\let\char\empty
           \doifnumberelse{\csname#2#3\endcsname}{#1{\csname#2#3\endcsname}{#4}{#5}{#6}{#7}}\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}

\def\doinhsomehandling#1#2#3 #4 % to be checked
  {\ifskiphandlingdef \else
     \if#3\relax\relax
       #1{`#2}{`#4}%
     \else
       \let\char\empty
       \doifnumberelse{\csname#2#3\endcsname}{#1{\csname#2#3\endcsname}{`#4}}\donothing
       \let\char\normalchar
     \fi
   \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!max \endcsname\space
       \csname\@fha@\@fha@#1\c!min \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!max \endcsname/%
       \csname\@fha@\@fha@#1\c!min \endcsname/%
       \csname\@fha@\@fha@#1\c!step\endcsname\space
       \fontname\handledfont}}
\fi

\ifx\setfontadjusting\gobbleoneargument \else
  \appendtoks
    \setfontadjusting\askedfonthandling
  \to \everyenablefonthandling
\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\emwidth>\protrudethreshold
    %      \divide\scratchdimen\emwidth
    %      \multiply\scratchdimen\fontcharwd\handledfont#1\relax
    %    \else
    %      \multiply\scratchdimen\fontcharwd\handledfont#1\relax
    %      \divide\scratchdimen\emwidth
    %    \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\emwidth>\protrudethreshold
             /\emwidth*\fontcharwd\handledfont#1%
           \else
             *\fontcharwd\handledfont#1/\emwidth
           \fi
         \fi
       \relax
       \rpcode\handledfont#1\dimexpr#3\rproddimen
         \ifembasedprotruding \else
           \ifdim\emwidth>\protrudethreshold
             /\emwidth*\fontcharwd\handledfont#1%
           \else
             *\fontcharwd\handledfont#1/\emwidth
           \fi
         \fi
       \relax}

  \endETEX

  \beginTEX

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

    \def\protrudethreshold{36\onepoint}

    \def\redodefineprotrudefactor
      {\ifdim\emwidth>\protrudethreshold
         \divide\scratchdimen\emwidth
         \multiply\scratchdimen\wd\charbox
       \else
         \multiply\scratchdimen\wd\charbox
         \divide\scratchdimen\emwidth
       \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

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

% Basic support for a new pdftex feature: space factors
%
% \knbccode -- kern before char code
% \knbscode -- kern before space code
% \stbscode -- stretch before space code
% \shbscode -- shrink before space code
%
% Setting \knsbcode\font`\.=200 means that if a period sits before
% a interword space (glue), then the interword glue will be increased
% by an amount of 1em*200/1000, i.e. the value is given in thousandths
% of an em. (HTT)

\newdimen\spfacdimen

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

  \let\enablespacehandling  \relax
  \let\disablespacehandling \relax
  \let\enablekernhandling   \relax
  \let\disablekernhandling  \relax

  \let\setspacehandling     \gobbleoneargument

  \def\definespacefactor    #1 #2 #3 #4 #5 {}
  \def\inheritspacefactor   #1 #2 {}

\else

  \def\enablespacehandling {\pdfadjustinterwordglue\plusone }
  \def\disablespacehandling{\pdfadjustinterwordglue\minusone}
  \def\enablekernhandling  {\pdfprependkern        \plusone }
  \def\disablekernhandling {\pdfprependkern        \minusone}

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

  \def\dosetspacehandling#1%
    {\spfacdimen\csname\@fha@\@fha@#1\c!factor\endcsname\s!pt
     \multiply\spfacdimen\plusthousand\divide\spfacdimen\maxcard\relax}

  \def\setspacehandling#1%
    {\doifvalue{\@fha@\@fha@#1\c!type}\v!spacing{\dosetspacehandling{#1}}}

  \def\dododefinespacefactor#1#2#3#4#5%
    {\sfcode#1\plusthousand
     \knbccode\handledfont#1\dimexpr#2\spfacdimen\relax
     \knbscode\handledfont#1\dimexpr#3\spfacdimen\relax
     \stbscode\handledfont#1\dimexpr#4\spfacdimen\relax
     \shbscode\handledfont#1\dimexpr#5\spfacdimen\relax}

  \def\dodoinheritspacefactor#1#2%
    {\sfcode#1\plusthousand
     \knbccode\handledfont#1\knbccode\handledfont#2\relax
     \knbscode\handledfont#1\knbscode\handledfont#2\relax
     \stbscode\handledfont#1\stbscode\handledfont#2\relax
     \shbscode\handledfont#1\shbscode\handledfont#2\relax}

  \def\definespacefactor#1 #2 #3 #4 #5 %
    {\setfonttoks
     \let\inherithandling\inheritspacefactor
     \appendtoks\dosetquartethandling\dododefinespacefactor#1 #2 #3 #4 #5 \to\fonttoks}

  \def\inheritspacefactor#1 #2 %
    {\setfonttoks
     \appendtoks\doinhsomehandling\dodoinheritspacefactor#1 #2 \to\fonttoks}

\fi

\ifx\setspacehandling\gobbleoneargument \else
  \appendtoks
    \setspacehandling\askedfonthandling
  \to \everyenablefonthandling
\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,%
        \c!factor=1,%
        #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

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

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

\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{usehandling}

\def\dousehandling#1% is the same as encoding
  {\doifundefined{\c!file\f!handlingprefix#1}%
     {\letvalueempty{\c!file\f!handlingprefix#1}%
      \makeshortfilename[\truefilename{\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
