%D \module
%D   [       file=font-emp,
%D        version=20120106, % (moved from font-ini)
%D          title=\CONTEXT\ Font Macros,
%D       subtitle=Emphasis,
%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 Font Macros / Emphasis}

\unprotect

%D \macros
%D  {em,emphasistypeface,emphasisboldface}
%D
%D The next macro started as a copy of Donald Arseneau's \type {\em} (\TUGNEWS\
%D Vol.~3, no.~1, 1994). His implementation was a bit more sophisticated version of
%D the standard \LATEX\ one. We further enhanced the macro, so now it also adapts
%D itself to boldface mode. Because we favor {\sl slanted} type over {\it italic},
%D we made the emphasis adaptable, for instance:
%D
%D \starttyping
%D \def\emphasistypeface {\it}
%D \def\emphasisboldface {\bi}
%D \stoptyping
%D
%D But we prefer:
%D
%D \starttyping
%D \protected\def\emphasistypeface{\sl}
%D \protected\def\emphasisboldface{\bs}
%D \stoptyping
%D
%D or even better:

\lettonothing\m_font_emphasized_typeface

\def\font_emphasis_checked_typeface#slanted#italic%
  {\edef\m_font_emphasized_typeface{\font_bodyfontvariable\s!em}%
   \ifx\m_font_emphasized_typeface\v!slanted
     #slanted%
   \orelse\ifx\m_font_emphasized_typeface\v!italic
     #italic%
   \orelse\ifempty\m_font_emphasized_typeface
     #slanted%
   \else
     \csname\m_font_emphasized_typeface\endcsname
   \fi}

\permanent\protected\def\emphasistypeface{\font_emphasis_checked_typeface\sl\it}
\permanent\protected\def\emphasisboldface{\font_emphasis_checked_typeface\bs\bi}

\permanent\protected\def\normalboldface % public, upgraded from mkvi / WS
  {\relax
   \ifx\fontalternative\s!tf\bf\orelse
   \ifx\fontalternative\s!it\bi\orelse
   \ifx\fontalternative\s!sl\bs\orelse
   \ifx\fontalternative\s!bf   \orelse
   \ifx\fontalternative\s!bi   \orelse
   \ifx\fontalternative\s!bs   \else
                            \bf\fi}

\permanent\protected\def\normalitalicface % public, upgraded from mkvi
  {\relax
   \ifx\fontalternative\s!tf\it\orelse
   \ifx\fontalternative\s!it   \orelse
   \ifx\fontalternative\s!sl\it\orelse
   \ifx\fontalternative\s!bf\bi\orelse
   \ifx\fontalternative\s!bi\bi\orelse
   \ifx\fontalternative\s!bs\bi\else
                            \it\fi}

\permanent\protected\def\normalslantedface % public, upgraded from mkvi
  {\relax
   \ifx\fontalternative\s!tf\sl\orelse
   \ifx\fontalternative\s!it\sl\orelse
   \ifx\fontalternative\s!sl   \orelse
   \ifx\fontalternative\s!bf\bs\orelse
   \ifx\fontalternative\s!bi\bs\orelse
   \ifx\fontalternative\s!bs\bs\else
                            \sl\fi}

\permanent\protected\def\normaltypeface % public, upgraded from mkvi
  {\relax
   \ifx\fontalternative\s!tf   \orelse
   \ifx\fontalternative\s!it\tf\orelse
   \ifx\fontalternative\s!sl\tf\orelse
   \ifx\fontalternative\s!bf   \orelse
   \ifx\fontalternative\s!bi\bf\orelse
   \ifx\fontalternative\s!bs\bf\else
                            \tf\fi}

\permanent\protected\def\swaptypeface % public
  {\relax
   \ifx\fontalternative\s!it \tf               \orelse
   \ifx\fontalternative\s!sl \tf               \orelse
   \ifx\fontalternative\s!bf \emphasisboldface \orelse
   \ifx\fontalternative\s!bs \bf               \orelse
   \ifx\fontalternative\s!bi \bf               \else
                             \emphasistypeface \fi}

\aliased\let\typeface   \normaltypeface    % public
\aliased\let\boldface   \normalboldface    % public
\aliased\let\slantedface\normalslantedface % public
\aliased\let\italicface \normalitalicface  % public
\aliased\let\swapface   \swaptypeface      % public

%D \startbuffer[demo]
%D \starttabulate[|l|c|c|c|c|c|c|c|]
%D \NC                   \NC \tex{tf}             \NC \tex{it}             \NC \tex{sl}             \NC \tex{bf}
%D                       \NC \tex{bi}             \NC \tex{bs}             \NC \tex{sc}             \NC \NR
%D \NC                   \NC \tf              txt \NC \it              txt \NC \sl              txt \NC \bf              txt
%D                       \NC \bi              txt \NC \bs              txt \NC \sc              txt \NC \NR
%D \NC \tex{typeface}    \NC \tf \typeface    txt \NC \it \typeface    txt \NC \sl \typeface    txt \NC \bf \typeface    txt
%D                       \NC \bi \typeface    txt \NC \bs \typeface    txt \NC \sc \typeface    txt \NC \NR
%D \NC \tex{boldface}    \NC \tf \boldface    txt \NC \it \boldface    txt \NC \sl \boldface    txt \NC \bf \boldface    txt
%D                       \NC \bi \boldface    txt \NC \bs \boldface    txt \NC \sc \boldface    txt \NC \NR
%D \NC \tex{slantedface} \NC \tf \slantedface txt \NC \it \slantedface txt \NC \sl \slantedface txt \NC \bf \slantedface txt
%D                       \NC \bi \slantedface txt \NC \bs \slantedface txt \NC \sc \slantedface txt \NC \NR
%D \NC \tex{italicface}  \NC \tf \italicface  txt \NC \it \italicface  txt \NC \sl \italicface  txt \NC \bf \italicface  txt
%D                       \NC \bi \italicface  txt \NC \bs \italicface  txt \NC \sc \italicface  txt \NC \NR
%D \NC \tex{swapface}    \NC \tf \swapface    txt \NC \it \swapface    txt \NC \sl \swapface    txt \NC \bf \swapface    txt
%D                       \NC \bi \swapface    txt \NC \bs \swapface    txt \NC \sc \swapface    txt \NC \NR
%D \stoptabulate
%D \stopbuffer
%D
%D {\small \typebuffer[demo]}
%D
%D This gives:
%D
%D \getbuffer[demo]
%D
%D But with:
%D
%D \startbuffer
%D \setupbodyfontenvironment [default] [em=blue] % affects \swapface
%D \stopbuffer
%D
%D \typebuffer
%D
%D We get:
%D
%D {\getbuffer\getbuffer[demo]}

%D To be set with the default body font environment: \type {em} being \type
%D {slanted} or \type {italic}.

% maybe a \csname...\fontalternative\endcsname

\newconditional\c_font_emphasis_needed

\permanent\protected\def\em
  {\dontleavehmode
  %\relax
   \ifdim\slantperpoint>\zeropoint
     \c_font_emphasis_needed\conditionaltrue
   \else
     \c_font_emphasis_needed\conditionalfalse
   \fi
   \font_emphasis_set_emphasis_boldface % new
   \ifx\fontalternative\s!it
     \enforced\permanent\protected\def\emphasistypeface{\it}\tf
   \orelse\ifx\fontalternative\s!sl
     \enforced\permanent\protected\def\emphasistypeface{\sl}\tf
   \orelse\ifx\fontalternative\s!bf
     \emphasisboldface
   \orelse\ifx\fontalternative\s!bs
     \enforced\permanent\protected\def\emphasisboldface{\bs}\bf
   \orelse\ifx\fontalternative\s!bi
     \enforced\permanent\protected\def\emphasisboldface{\bi}\bf
   \else
     \emphasistypeface
   \fi
   \ifconditional\c_font_emphasis_needed\else
     \expandafter\aftergroup
   \fi
   \emphasiscorrection}

%D The next feature was not present in previous versions. It takes care of \type
%D {\em \bf ...} situations.

\let\font_emphasis_saved_emphasis_boldface\relax

\def\font_emphasis_set_emphasis_boldface
  {\let\font_emphasis_saved_emphasis_boldface\bf
   \let\font_emphasis_set_emphasis_boldface\relax
   \enforced\let\bf\font_emphasis_bold_bold}

\protected\def\font_emphasis_bold_bold
  {%\relax
   \enforced\let\bf\relax % new
   \ifx\fontalternative\s!it
     \bi
   \orelse\ifx\fontalternative\s!sl
     \bs
   \else
     \font_emphasis_saved_emphasis_boldface
   \fi
   \enforced\let\bf\font_emphasis_saved_emphasis_boldface}

%D Donald's (adapted) macros take the next character into account when placing
%D italic correction. As a bonus we also look for something that looks like a dash,
%D in which case we don't correct.

\aliased\let\normalitaliccorrection\/ % the tex primitive

\let\italiccorrection\normalitaliccorrection % will be overloaded

\permanent\protected\def\emphasiscorrection % public, not in raw alignment groups, else omit problem
  {\ifhmode\ifnum\currentgrouptype=\aligngroupcode\else
     \expandafter\expandafter\expandafter\font_emphasis_look
   \fi\fi}

\def\font_emphasis_look
  {\begingroup
   \futurelet\nexttoken\font_emphasis_test}

\def\font_emphasis_test
  {\ifcat\noexpand\nexttoken,% we can do better in luatex ... some day ...
     \expandafter\font_emphasis_a
   \else
     \expandafter\font_emphasis_b
   \fi}

\def\font_emphasis_a
  {\futurelet\nexttoken\font_emphasis_correction}

\def\font_emphasis_correction
   {\setbox\scratchbox\hbox{\nexttoken}% tricky as next can be a macro
    \ifzeropt\ht\scratchbox % probably a space
      \expandafter\font_emphasis_b
    \orelse\ifdim\ht\scratchbox<.3\exheight
      \expandafter\expandafter\expandafter\endgroup
    \else
      \expandafter\expandafter\expandafter\font_emphasis_b
    \fi}

\def\font_emphasis_b
  {\scratchskip\lastskip
   \ifzeropt\scratchskip
     \italiccorrection\relax
   \else
     \unskip\italiccorrection\hskip\scratchskip
   \fi
   \endgroup}

%D We end with some examples which show the behavior when some punctuation is met.
%D We also show how the mechanism adapts itself to bold, italic and slanted typing.
%D
%D \startbuffer
%D test {test}test       \par
%D test {\sl test}test   \par
%D test {\em test}test   \par
%D test {\em test}--test \par
%D
%D test {test}, test     \par
%D test {\em test}, test \par
%D
%D test {\em test {\em test {\em test} test} test} test \par
%D test {\bf test {\em test {\em test} test} test} test \par
%D test {\sl test {\em test {\em test} test} test} test \par
%D test {\it test {\em test {\em test} test} test} test \par
%D \stopbuffer
%D
%D \typebuffer
%D
%D We get:
%D
%D \startpacked
%D \getbuffer
%D \stoppacked

%D \macros
%D   {emphbf,emphit,emphsl,emphtf}
%D
%D The next emphasis alternatives are for \THANH. They adapt their style as good as
%D possible. These macros are obsolete in \MKIV.

% \protected\def\emphbf{\groupedcommand{\bf\def\emphit{\bi}\def\emphsl{\bs}}{}}
% \protected\def\emphit{\groupedcommand{\it\def\emphbf{\bi}\def\emphsl{\sl}}{}}
% \protected\def\emphsl{\groupedcommand{\sl\def\emphbf{\bs}\def\emphit{\it}}{}}
% \protected\def\emphtf{\groupedcommand{\tf\def\emphbf{\bf}\def\emphit{\it}\def\emphsl{\sl}}{}}
% \protected\def\emph  {\groupedcommand{\em}{}}


\permanent\protected\def\font_tf{\tf} \permanent\protected\def\font_bf{\bf}
\permanent\protected\def\font_sl{\sl} \permanent\protected\def\font_bs{\bs}
\permanent\protected\def\font_it{\it} \permanent\protected\def\font_bi{\bi}

\protected\def\font_emphasis_bf{\bf
                                \enforced\aliased\let\emphit\font_bi
                                \enforced\aliased\let\emphsl\font_bs}
\protected\def\font_emphasis_it{\it
                                \enforced\aliased\let\emphbf\font_bi
                                \enforced\aliased\let\emphsl\font_sl}
\protected\def\font_emphasis_sl{\sl
                                \enforced\aliased\let\emphbf\font_bs
                                \enforced\aliased\let\emphit\font_it}
\protected\def\font_emphasis_tf{\tf
                                \enforced\aliased\let\emphbf\font_bf
                                \enforced\aliased\let\emphit\font_it
                                \enforced\aliased\let\emphsl\font_sl}

\permanent\protected\def\emphbf{\dontleavehmode\triggergroupedcommandcs\font_emphasis_bf}
\permanent\protected\def\emphit{\dontleavehmode\triggergroupedcommandcs\font_emphasis_it}
\permanent\protected\def\emphsl{\dontleavehmode\triggergroupedcommandcs\font_emphasis_sl}
\permanent\protected\def\emphtf{\dontleavehmode\triggergroupedcommandcs\font_emphasis_tf}
\permanent\protected\def\emph  {\dontleavehmode\triggergroupedcommandcs\em}

\permanent\protected\def\emphasized{\dontleavehmode\bgroup\em\let\nexttoken}

% maybe some day:
%
% \definehighlight[\v!emphasize][\c!style=\em]

%D \startbuffer
%D TEXT {\emphbf text \emphit text  \emphtf text  \emphsl text}  TEXT
%D TEXT  \emphbf{text \emphit{text} \emphtf{text} \emphsl{text}} TEXT
%D \stopbuffer
%D
%D \typebuffer
%D \startlines
%D \getbuffer
%D \stoplines

\protect \endinput
