hand-ini.mkii / last modification: 2020-01-30 14:15
%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 ADE \& \CONTEXT\ Development Team}]
%C
%C This module is part of the \CONTEXT\ macro||package and is
%C therefore copyrighted by \PRAGMA. See mreadme.pdf for
%C details.

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

\unprotect

\newif\iftracefonthandling % \tracefonthandlingtrue

\newif\ifskiphandlingdef  \skiphandlingdeffalse

\newtoks\everyenablefonthandling

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

\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\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 #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%
    {\efcode\handledfont#1\dimexpr#2\onepoint*\plusthousand/\maxcard\relax}

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

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

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

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

  \newdimen\protrudethreshold \protrudethreshold=36pt

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

  \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

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

\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:

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

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

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

%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.mkii}
          {\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]

% Beware of fonttoks:
%
% \startfonthandling[morespacing]
%     \appendtoks
%         \fontdimen2\handledfont=.50em
%         \fontdimen3\handledfont=.25em
%         \fontdimen4\handledfont=.25em
%     \to \fonttoks
% \stopfonthandling
%
% \definefonthandling [morespacing] [morespacing]
%
% \definefontsynonym[xtexnansi-lmr7][texnansi-lmr7][handling=morespacing]
% \definefont[crap][texnansi-lmr7]
%
% \crap \the\fontdimen2\font, \the\fontdimen3\font, \the\fontdimen4\font

\usehandling [def]

\protect \endinput