%D \module
%D   [      file=s-typesetting-kerning,
%D        version=2014.12.14,
%D          title=\CONTEXT\ Style File,
%D       subtitle=Show Character Kerning,
%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.

\unprotect

\definecharacterkerning
  [typesetting-kerning-demo]
  [factor=.5]

\startbuffer[showcharacterkerning:boxes]
    \starttextrule{boxes}
        \showfontkerns
        \dontcomplain
        \startlines
            test \hbox{!} test
            test\hbox{!} test
            test \hbox{!}test
            test:$x$ test
            \setcharacterkerning[typesetting-kerning-demo]
            test \hbox{!} test
            test\hbox{!} test
            test \hbox{!}test
            test:$x$ test
        \stoplines
    \stoptextrule
\stopbuffer

\startbuffer[showcharacterkerning:ligatures]
    \starttextrule{ligatures}
        \dontcomplain
        \startlines
            effe flink effectief efficient fietsen
            \blank
            \setcharacterkerning[typesetting-kerning-demo]
            effe flink effectief efficient fietsen
            \blank \hsize\zeropoint
            effe
            flink
            effectief
            efficient
            fietsen
        \stoplines
    \stoptextrule
\stopbuffer

\startbuffer[showcharacterkerning:discretionaries]
    \starttextrule{discretionary}
        \dontcomplain
        \startlines
            \hbox{\samplediscretionary}
            \hbox{xxx\samplediscretionary}
            \hbox{\samplediscretionary xxx}
            \hbox{xxx\samplediscretionary xxx}
            \blank
            \setcharacterkerning[typesetting-kerning-demo]
            \hbox{\samplediscretionary}
            \hbox{xxx\samplediscretionary}
            \hbox{\samplediscretionary xxx}
            \hbox{xxx\samplediscretionary xxx}
            \blank \hsize\zeropoint
            \samplediscretionary
            xxx\samplediscretionary
            \samplediscretionary xxx
            xxx\samplediscretionary xxx
        \stoplines
    \stoptextrule
\stopbuffer

\startbuffer[showcharacterkerning:explicits]
    \starttextrule{explicits}
        \exhyphenchar    \hyphenasciicode
        \preexhyphenchar \lessthanasciicode
        \postexhyphenchar\morethanasciicode
        \def\TestDisc
          {\discretionary
             {\kern\emwidth<}%
             {>\kern\emwidth}%
             {\kern\emwidth=\kern\emwidth}%
          }
        \dontcomplain
        \startlines
            \hbox{super-charged}
            \hbox{super\-charged}
            \hbox{super\TestDisc charged}
            \hbox{super\discretionary{[}{]}{[]}charged}
            \blank
            \setcharacterkerning[typesetting-kerning-demo]
            \hbox{super-charged}
            \hbox{super\-charged}
            \hbox{super\TestDisc charged}
            \hbox{super\discretionary{[}{]}{[]}charged}
            \blank \hsize\zeropoint
            super-charged
            super\-charged
            super\TestDisc charged
            super\discretionary{[}{]}{[]}charged
        \stoplines
    \stoptextrule
\stopbuffer

\starttexdefinition unexpanded showcharacterkerning
    \getbuffer[showcharacterkerning:boxes]
    \getbuffer[showcharacterkerning:ligatures]
    \getbuffer[showcharacterkerning:discretionaries]
    \getbuffer[showcharacterkerning:explicits]
\stoptexdefinition


\starttexdefinition showcharacterkerningstepscompared #1
    \definecharacterkerning[crap][factor=\KerningStepFactor]%
    \setbox0=\ruledhbox{\color[color-1]{#1}\hss}
    \setbox2=\ruledhbox{\setcharacterkerning[crap]\color[color-2]{#1}}
    \setbox4=\ruledhbox{\setcharacterkerning[crap]\showfontkerns\showglyphs#1}
    \xdef\KerningStepPercentage{\ctxlua{context("\letterpercent 0.2f",(1-\number\wd0/\number\wd2)*100)}}
    \scratchwidth\wd0
    \vtop\bgroup
        \hbox{\box0\hskip-\scratchwidth\box2}
        \par
        \box4
    \egroup
\stoptexdefinition

\starttexdefinition showcharacterkerningsteps [#1]

    \start

    \getdummyparameters
      [\s!font=Regular,
       \c!sample={Wat een bende, rommelen met het font design!},
       \c!text={rommelen},
       \c!first=00,
       \c!last=95,
       \c!step=05,
       \c!option=, % \v!page
       #1]

    \doif{\dummyparameter\c!option}\v!page {
        \startTEXpage[\c!offset=1ex]
    }

        \definecolor[color-1][r=1,t=.5,a=1]
        \definecolor[color-2][b=1,t=.5,a=1]

        \definedfont[\dummyparameter\s!font*default sa 1]

        \doif {\dummyparameter\c!option}\v!page {
            \begingroup
                \tttf \dummyparameter\s!font\space @ default
            \endgroup
            \blank
        }

        \starttabulate[|cT|l|cT|l|cT|]

        \NC \tt\bf factor \NC \tt\bf sample \NC \tt\bf \letterpercent \NC \tt\bf text \NC \tt\bf \letterpercent \NC \NR \HL

        \dostepwiserecurse {\dummyparameter\c!first} {\dummyparameter\c!last} {\dummyparameter\c!step} {
            \NC
                \xdef\KerningStepFactor{\ctxlua{context("\letterpercent 0.3f",####1/1000)}}
                \KerningStepFactor
            \NC
                \showcharacterkerningstepscompared{\dummyparameter\c!sample}
            \NC
                \KerningStepPercentage
            \NC
                \showcharacterkerningstepscompared{\dummyparameter\c!text}
            \NC
                \KerningStepPercentage
            \NC \NR
        }

        \stoptabulate

    \doif{\dummyparameter\c!option}\v!page {
        \stopTEXpage
    }

    \stop

\stoptexdefinition

\protect

\continueifinputfile{s-typesetting-kerning.mkiv}

\starttext

  % \showcharacterkerning

    \showcharacterkerningsteps[font=file:FuturaStd-Book.otf,option=page]
    \showcharacterkerningsteps[font=file:FuturaStd-Medium.otf,option=page]
    \showcharacterkerningsteps[font=file:FuturaStd-Bold.otf,option=page]
    \showcharacterkerningsteps[font=file:FuturaStd-heavy.otf,option=page]

\stoptext

% {\hsize1mm efficient\discretionary{\kern1pt!\kern1pt}{\kern1pt!\kern1pt}{\kern1pt!\kern1pt}efficient\par}
% {\hsize1mm\definedfont[Regular]\setcharacterkerning[typesetting-kerning-demo]efficient\-efficient\par}

