%D \module
%D   [       file=typo-krn,
%D        version=2009.03.27, % code moved from core-spa.mkiv
%D          title=\CONTEXT\ Typesetting Macros,
%D       subtitle=Spacing,
%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.

\writestatus{loading}{ConTeXt Typesetting Macros / Kerning}

\unprotect

\registerctxluafile{typo-krn}{autosuffix}

\definesystemattribute[kern][public]

% more
%
% {\setcharacterkerning[extrakerning]\input davis\relax}

\installcorenamespace{characterkerning}

\installcommandhandler \??characterkerning {characterkerning} \??characterkerning

\permanent\protected\def\setcharacterkerning[#1]%
  {\edef\currentcharacterkerning{#1}%
   \ifx\currentcharacterkerning\s!reset
     \resetcharacterkerning
   \else
     \typo_kerning_set
   \fi}

\def\typo_kerning_set
  {\usecharacterkerningstyleandcolor\c!style\c!color % goodie, maybe also strut
   \useaddfontfeatureparameter\characterkerningparameter
   \clf_setcharacterkerning{\characterkerningparameter\c!factor}}

\permanent\protected\def\resetcharacterkerning % fast one
  {\c_attr_kern\attributeunsetvalue}

\appendtoks
    \resetcharacterkerning
\to \everyresettypesetting

\definecharacterkerning [extrakerning] [\c!factor=.125] % used in manuals

%D Added after discussion on list (posted by WS, adapted abit by HH) \unknown\ this
%D needs to be interfaced (\type {\v!kerncharacters}).
%D
%D \starttyping
%D \setuphead[section][style=\sca,textstyle={\kerncharacters[.5]}] \section{Section}
%D \stoptyping
%D
%D We could combine this with the previous definition command but then we always
%D would get a command defined which is not beforehand a good idea.
%D
%D Here we need to keep the groupedcommand solution as it is used as modifier.

\appendtoks
  \frozen\instance\setuevalue{\currentcharacterkerning}%
    {\doifelsenextoptional
       {\typo_kerning_apply_yes{\currentcharacterkerning}}%
       {\typo_kerning_apply_nop{\currentcharacterkerning}}}%
\to \everydefinecharacterkerning

\protected\def\typo_kerning_apply_yes#1[#2]%
  {\groupedcommand{\typo_kerning_apply_yes_indeed{#1}{#2}}\donothing}

\protected\def\typo_kerning_apply_nop#1%
  {\groupedcommand{\typo_kerning_apply_nop_indeed{#1}}\donothing}

\def\typo_kerning_apply_yes_indeed#1#2%
  {\edef\currentcharacterkerning{#1}%
   \setupcurrentcharacterkerning[\c!factor=#2]%
   \typo_kerning_set}

\def\typo_kerning_apply_nop_indeed#1%
  {\edef\currentcharacterkerning{#1}%
   \typo_kerning_set}

\definecharacterkerning [\v!kerncharacters] [\c!factor=.125]
\definecharacterkerning [\v!letterspacing ] [\v!kerncharacters] [\c!features=letterspacing]

%D \macros
%D   {stretched}
%D
%D Stretching characters in a word is a sort of typographical murder. Nevertheless
%D we support this manipulation for use in for instance titles.
%D
%D \starttyping
%D \hbox to 5cm{\stretched{to the limit}}
%D \stretched{to the limit}
%D \stretched[width=10cm]{to the limit}
%D \stoptyping
%D
%D \typebuffer
%D
%D or
%D
%D \startexample
%D \getbuffer
%D \stopexample
%D
%D \showsetup{stretched}
%D
%D This command replaces the old \MKII\ variant.

\definecharacterkerning
  [\v!stretched]
  [\c!factor=\v!max,
   \c!width=\availablehsize]

\permanent\protected\def\setupstretched
  {\setupcharacterkerning[\v!stretched]}

\pushoverloadmode

\enforced\permanent\tolerant\protected\def\stretched[#1]#:#2%
  {\begingroup
   \let\currentcharacterkerning\v!stretched
   \ifparameter#1\or
     \setupcurrentcharacterkerning[#1]%
   \fi
   \edef\p_width{\characterkerningparameter\c!width}%
   \ifempty\p_width \else
     \hbox to \p_width
   \fi
   \bgroup
   \usecharacterkerningstyleandcolor\c!style\c!color
   \useaddfontfeatureparameter\characterkerningparameter
   \typo_kerning_set
   #2%
   \egroup
   \endgroup}

\popoverloadmode

\protect \endinput
