%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
%D \type{\em} (\TUGNEWS\ Vol.~3, no.~1, 1994). His
%D implementation was a bit more sophisticated version of the
%D standard \LATEX\ one. We further enhanced the macro, so now
%D it also adapts itself to boldface mode. Because we favor
%D {\sl slanted} type over {\it italic}, we made the emphasis
%D 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 \unexpanded\def\emphasistypeface{\sl}
%D \unexpanded\def\emphasisboldface{\bs}
%D \stoptyping
%D
%D or even better:

\let\m_font_emphasized_typeface\empty

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

\unexpanded\def\emphasistypeface{\font_emphasis_checked_typeface\sl\it}
\unexpanded\def\emphasisboldface{\font_emphasis_checked_typeface\bs\bi}

\unexpanded\def\normalboldface % public
  {\relax\ifx\fontalternative\s!it\bi
   \else \ifx\fontalternative\s!sl\bs
   \else                          \bf\fi\fi}

\unexpanded\def\normaltypeface % public
  {\relax
   \ifx\fontalternative\s!bi \it \else
   \ifx\fontalternative\s!bs \sl \else
                             \tf \fi\fi}

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

\let\typeface\normaltypeface % public
\let\boldface\normalboldface % public
\let\swapface\swaptypeface   % public

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

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

\newconditional\c_font_emphasis_needed

\unexpanded\def\em
  {\relax
   \ifdim\slantperpoint>\zeropoint
     \settrue\c_font_emphasis_needed
   \else
     \setfalse\c_font_emphasis_needed
   \fi
   \font_emphasis_set_emphasis_boldface % new
   \ifx\fontalternative\s!it
     \unexpanded\def\emphasistypeface{\it}\tf
   \else\ifx\fontalternative\s!sl
     \unexpanded\def\emphasistypeface{\sl}\tf
   \else\ifx\fontalternative\s!bf
     \emphasisboldface
   \else\ifx\fontalternative\s!bs
     \unexpanded\def\emphasisboldface{\bs}\bf
   \else\ifx\fontalternative\s!bi
     \unexpanded\def\emphasisboldface{\bi}\bf
   \else
     \emphasistypeface
   \fi\fi\fi\fi\fi
   \ifconditional\c_font_emphasis_needed\else
     \expandafter\aftergroup
   \fi
   \emphasiscorrection}

%D The next feature was not present in previous versions. It
%D takes care of \type {\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
   \let\bf\font_emphasis_bold_bold}

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

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

\let\italiccorrection=\/ % tex primitive

\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,% still ok?
     \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
    \ifdim\ht\scratchbox=\zeropoint % probably a space
      \expandafter\font_emphasis_b
    \else\ifdim\ht\scratchbox<.3\exheight
      \expandafter\expandafter\expandafter\endgroup
    \else
      \expandafter\expandafter\expandafter\font_emphasis_b
    \fi\fi}

\def\font_emphasis_b
  {\scratchskip\lastskip
   \ifdim\scratchskip=\zeropoint\relax % == \ifzeropt\scratchskip
     \italiccorrection\relax
   \else
     \unskip\italiccorrection\hskip\scratchskip
   \fi
   \endgroup}

%D We end with some examples which show the behavior when
%D some punctuation is met. We also show how the mechanism
%D 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
%D their style as good as possible. These macros are obsolete
%D in \MKIV.

\unexpanded\def\emphbf{\groupedcommand{\bf\def\emphit{\bi}\def\emphsl{\bs}}{}}
\unexpanded\def\emphit{\groupedcommand{\it\def\emphbf{\bi}\def\emphsl{\sl}}{}}
\unexpanded\def\emphsl{\groupedcommand{\sl\def\emphbf{\bs}\def\emphit{\it}}{}}
\unexpanded\def\emphtf{\groupedcommand{\tf\def\emphbf{\bf}\def\emphit{\it}\def\emphsl{\sl}}{}}

\unexpanded\def\emph  {\groupedcommand{\em}{}}

\unexpanded\def\emphasized{\bgroup\em\let\nexttoken}

%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
