%D \module
%D   [       file=font-sty,
%D        version=2011.01.13, % (copied fron font-ini)
%D          title=\CONTEXT\ Font Macros,
%D       subtitle=Styles,
%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 / Styles}

\unprotect

%D \macros
%D   {definealternativestyle}
%D
%D In the main modules we are going to implement lots of parameterized commands and
%D one of these parameters will concern the font to use. To suit consistent use of
%D fonts we here implement a mechanism for defining the keywords that present a
%D particular style or alternative.
%D
%D \starttyping
%D \definealternativestyle [keywords] [\style] [\nostyle]
%D \stoptyping
%D
%D The first command is used in the normal textflow, while the second command takes
%D care of headings and alike. Consider the next two definitions:
%D
%D \starttyping
%D \definealternativestyle [bold] [\bf]  []
%D \definealternativestyle [cap]  [\cap] [\cap]
%D \stoptyping
%D
%D A change \type {\bf} in a heading which is to be set in \type {\tfd} does not look
%D that well, so therefore we leave the second argument of \type
%D {\definealternativestyle} empty. When we capatalize characters using the pseudo
%D small cap command \type {\cap}, we want this to take effect in both text and
%D headings, which is accomplished by assigning both arguments.

\installcorenamespace{alternativestyles}  % settings
\installcorenamespace{alternativestyle}   % instances

\installsetuponlycommandhandler \??alternativestyles {alternativestyles}

\setnewconstant \c_font_current_alternative_style_index \plusone

\permanent\tolerant\protected\def\definealternativestyle[#commands]#spacer[#variantone]#spacer[#varianttwo]%
  {\processcommalist[#commands]{\font_basics_define_alternative_style_indeed{#variantone}{#varianttwo}}}

% \let\definestyle\definealternativestyle % later redefined

\newconstant\c_fonts_basics_alternative_style_method

% maybe permanent

\def\font_basics_define_alternative_style_indeed#variantone#varianttwo#command%
  {\protected\defcsname\??alternativestyle#command\endcsname{\font_helpers_apply_alternative_style{#variantone}{#varianttwo}}%
   \ifcsname#command\endcsname
     % no redefinition
   \orelse\ifnum\c_fonts_basics_alternative_style_method=\plusone
      \ifparameter#command\or
        \permanent\protected\edefcsname#command\endcsname{\triggergroupedcommandcs\begincsname\??alternativestyle#command\endcsname}%
      \else
        \permanent\protected\defcsname#command\endcsname{\triggergroupedcommand{#variantone}}%
      \fi
   \else
     \permanent\protected\defcsname#command\endcsname{\triggergroupedcommand{#variantone}}%
   \fi}

\def\font_helpers_apply_alternative_style
  {\ifcase\c_font_current_alternative_style_index
     \expandafter\gobbletwoarguments
   \or
     \expandafter\firstoftwoarguments
   \or
     \expandafter\secondoftwoarguments
   \else
     \expandafter\firstoftwoarguments
   \fi}

\permanent\def\applyalternativestyle#name% public .. expandable? frozen?
  {\begincsname\??alternativestyle#name\endcsname}

\appendtoks
    \c_fonts_basics_alternative_style_method
      \ifcstok{\alternativestylesparameter\c!method}\v!auto
        \plusone
      \else
        \zerocount
      \fi
\to \everysetupalternativestyles

%D Maybe too generic, but probably ok is the following. (Maybe one day we will use a
%D dedicated grouped command for styles.)

% \appendtoks
%     \let\groupedcommand\thirdofthreearguments
% \to \everysimplifycommands

%D This command also defines the keyword as command. This means that the example
%D definition of \type {bold} we gave before, results in a command \type {\bold}
%D which can be used as:
%D
%D \startbuffer
%D He's a \bold{bold} man with a {\bold head}.
%D \stopbuffer
%D
%D \typebuffer
%D
%D or
%D
%D \startexample
%D \definealternativestyle[bold][\bf][]\getbuffer
%D \stopexample
%D
%D Such definitions are of course unwanted for \type {\cap} because this would
%D result in an endless recursive call. Therefore we check on the existance of both
%D the command and the substitution. The latter is needed because for instance \type
%D {\type} is an entirely diferent command. That command handles verbatim, while the
%D style command would just switch to teletype font. This is just an example of a
%D tricky naming coincidence.
%D
%D \macros
%D   {doconvertfont,noconvertfont,
%D    dontconvertfont,redoconvertfont}
%D
%D After having defined such keywords, we can call for them by using
%D
%D \starttyping
%D \doconvertfont{keyword}{text}
%D \stoptyping
%D
%D We deliberately pass an argument. This enables us to assign converters that
%D handle one argument, like \type {\cap}.
%D
%D By default the first specification is used to set the style, exept when we say
%D \type {\dontconvertfont}, after which the second specification is used. We can
%D also directly call for \type {\noconvertfont}. In nested calls, we can restore
%D the conversion by saying \type {\redoconvertfont}.
%D
%D These commands are not grouped! Grouping is most probably done by the calling
%D macro's and would lead to unnecessary overhead.

\lettonothing\m_current_convert_font
\lettonothing\m_current_convert_font_dt

\permanent\protected\def\doconvertfont#specification% takes second argument / this command is obsolete
  {\edef\m_current_convert_font{#specification}%
   \ifempty\m_current_convert_font
    %\expandafter\firstofoneargument
   \else
     \expandafter\font_helpers_do_convert_font
   \fi}

\def\font_helpers_do_convert_font
  {\edef\m_current_convert_font_dt{\detokenize\expandafter{\m_current_convert_font}}%
   \ifcsname\??alternativestyle\m_current_convert_font_dt\endcsname
     \expandafter\lastnamedcs
   \orelse\ifcsname\m_current_convert_font_dt\endcsname
     \expandafter\lastnamedcs
   \else
     \expandafter\m_current_convert_font
   \fi}

%D Low level switches (downward compatible, but we keep them as one can use them in
%D styles):
%D
%D \starttyping
%D \usemodule[abr-02]
%D \setuphead[chapter][style=\bfb]
%D \setupfooter[style=\dontconvertfont\bf]
%D \chapter{This is \TEX}
%D \stoptyping

\permanent\protected\def\dontconvertfont{\c_font_current_alternative_style_index\plustwo} % needs checking in usage
\permanent\protected\def\redoconvertfont{\c_font_current_alternative_style_index\plusone} % needs checking in usage

%D The new one:

\newconditional\fontattributeisset

\mutable\lettonothing\currentdetokenizedstyleparameter

\permanent\protected\def\dousestyleparameter#value%
  {\cdef\currentstyleparameter{#value}%
   \ifempty\currentstyleparameter\else
     \expandafter\dousecurrentstyleparameter
   \fi}

\permanent\protected\def\dousestylehashparameter#hash#parameter%
  {\ifcsname#hash#parameter\endcsname
     \expandafter\dousestyleparameter\lastnamedcs
   \fi}

\permanent\protected\def\dousecurrentstyleparameter % empty check outside here
  {\cdef\currentdetokenizedstyleparameter{\detokenize\expandafter{\currentstyleparameter}}%
   \fontattributeisset\conditionaltrue % reset is done elsewhere
   \ifcsname\??alternativestyle\currentdetokenizedstyleparameter\endcsname
     \lastnamedcs
   \orelse\ifcsname\currentdetokenizedstyleparameter\endcsname
     \lastnamedcs
   \else
     \currentstyleparameter
   \fi}

%D New commands (not yet interfaced):
%D
%D \startbuffer
%D \definestyle[one][style=bold,color=darkblue]
%D
%D test \one{test} test
%D test \style[one]{test} test
%D test \style[color=red]{test} test
%D test \style[Serif at 20pt]{test} test
%D \stopbuffer
%D
%D \typebuffer \startlines \getbuffer \stoplines

% definitions .. no tagging here

\installcorenamespace{style}
\installcorenamespace{stylecheck}
\installcorenamespace{stylehack}

\installcommandhandler \??style {style} \??style

\setupstyle
  [%\c!style=,
   %\c!color=,
   \c!method=\v!command]

\appendtoks
    \letcsname\??stylecheck\currentstyle\endcsname\relax
    \ifcstok{\styleparameter\c!method}\v!command
        \frozen\instance\protected\edefcsname\e!start\currentstyle\endcsname{\font_styles_apply_start{\currentstyle}}%
        \frozen\instance\protected\edefcsname\e!stop \currentstyle\endcsname{\font_styles_apply_stop}%
        \frozen\instance\protected\edefcsname        \currentstyle\endcsname{\font_styles_apply_grouped{\currentstyle}}% no longer groupedcommand here
    \fi
\to \everydefinestyle

\protected\def\font_styles_apply_start#name%
  {\begingroup
   \font_styles_use_defined{#name}}

\protected\def\font_styles_apply_stop
  {\endgroup}

\protected\def\font_styles_apply_grouped#name% assumes that the next is { or \bgroup
  {\bgroup % \beginsimplegroup
   \cdef\currentstyle{\font_styles_use_defined{#name}}%
   \afterassignment\currentstyle
   \let\nexttoken}

\protected\def\font_styles_use_defined#name%
  {\cdef\currentstyle{#name}%
   \usestylestyleandcolor\c!style\c!color}

\protected\def\font_styles_use_generic#specification%
  {\let\currentstyle\s!unknown % reasonable generic tag
   \resetstyleparameter\c!style
   \resetstyleparameter\c!color
   \setupcurrentstyle[#specification]%
   \usestylestyleandcolor\c!style\c!color}

% commands

\installcorenamespace{styleargument}

\permanent\protected\def\style[#name]% as this is can be a switch we use groupedcommand % frozen?
  {\csname\??styleargument
     \ifcsname\??stylecheck#name\endcsname
       2% defined as style
     \orelse\ifcsname\??stylehack#name\endcsname
       4% defined as command
     \orelse\ifcsname#name\endcsname
       1% defined as command
     \else
       3% specification
     \fi
   \endcsname{#name}}

\defcsname\??styleargument1\endcsname#name%
  {\expandafter\triggergroupedcommandcs\begincsname#name\endcsname}

\defcsname\??styleargument2\endcsname#name%
  {\triggergroupedcommand{\font_styles_use_defined{#name}}} % or {\font_styles_apply_grouped{#name}}

\defcsname\??styleargument3\endcsname#specification%
  {\doifelseassignment{#specification}\font_styles_assignment\font_styles_direct{#specification}}

\defcsname\??styleargument4\endcsname#name%
  {\expandafter\triggergroupedcommandcs\begincsname\??stylehack#name\endcsname}

\defcsname\??stylehack\s!math\endcsname% dirty trick
  {\groupedcommandcs\normalstartimath\normalstopimath}

\def\font_styles_assignment#specification{\triggergroupedcommand{\font_styles_use_generic{#specification}}}
\def\font_styles_direct    #specification{\triggergroupedcommand{\definedfont[#specification]}}

% environments

\installcorenamespace{styleenvironment}

\permanent\protected\def\startstyle[#name]%
  {\begingroup
   \csname\??styleenvironment
     \ifcsname\??stylecheck#name\endcsname
       2% defined as style
     \orelse\ifcsname#name\endcsname
       1% defined as command
     \else
       3% specification
     \fi
   \endcsname{#name}}

\permanent\protected\def\stopstyle
  {\endgroup
   \autoinsertnextspace} % will be configurable, maybe also in \definestartstop

\defcsname\??styleenvironment1\endcsname#name%
  {\csname#name\endcsname}

\defcsname\??styleenvironment2\endcsname#name%
  {\font_styles_use_defined{#name}}

\defcsname\??styleenvironment3\endcsname#specification%
  {\doifelseassignment{#specification}\font_styles_start_assignment\font_styles_start_direct{#specification}}

\def\font_styles_start_assignment#specification{\font_styles_use_generic{#specification}}
\def\font_styles_start_direct    #specification{\definedfont[#specification]\relax}

%D Still experimental (might even go away).

% \definestylecollection[mine]

% \definestyleinstance[mine][default][sorry]
% \definestyleinstance[mine][tt][bs][ttbs:\rm\sl]
% \definestyleinstance[mine][tt][bf][ttbf:\rm\sl]
% \definestyleinstance[mine][bf][\sl]
% \definestyleinstance[mine][sl][\tt]

% {\bf test \mine test \sl test \mine test \bs oeps \mine oeps {\tt test \mine \bf test}}

% too messy for this:
%
% \definestylecollection[mine]
% \definestyleinstance[mine][tt][tf][\nohyphencollapsing]
% {\tt\mine test--test \bf test--test}

\installcorenamespace{stylecollection}

\permanent\tolerant\protected\def\definestylecollection[#name]%
  {\ifarguments\or
     \protected\instance\edefcsname#name\endcsname{\styleinstance[#name]}% \frozen ?
     \def\font_styles_define_style_collection_a#style%
       {\def\font_styles_define_style_collection_b#alternative{\undefinevalue{\??stylecollection#name:#style:#alternative}}%
        \font_helpers_process_alternative_list\font_styles_define_style_collection_b
        \font_styles_define_style_collection_b\s!default}%
     \font_helpers_process_style_list\font_styles_define_style_collection_a
     \font_styles_define_style_collection_a\s!default
  \fi}

\let\font_styles_define_style_collection  \relax
\let\font_styles_define_style_collection_a\relax
\let\font_styles_define_style_collection_b\relax

\permanent\tolerant\protected\def\definestyleinstance[#instance]#*[#2]#*[#3]#*[#4]% [name] [rm|ss|tt|..] [sl|bf|...] [whatever]
  {\ifparameter#instance\or
     \ifarguments\or\or
       \letcsname\??stylecollection#instance::#2\endcsname\empty
     \or
       \defcsname\??stylecollection#instance::#2\endcsname{#3}%
     \or
       \defcsname\??stylecollection#instance:#2:#3\endcsname{#4}%
     \fi
     \ifcsname#instance\endcsname\else\font_styles_define_style_collection[#instance]\fi
  \fi}

\permanent\protected\def\styleinstance[#instance]% maybe \lastnamedcs here too
  {\csname\??stylecollection#instance:%
     \ifcsname\??stylecollection#instance:\fontstyle:\fontalternative\endcsname
       \fontstyle:\fontalternative
     \orelse\ifcsname\??stylecollection#instance:\fontstyle:\s!default\endcsname
       \fontstyle:\s!default
     \orelse\ifcsname\??stylecollection#instance::\fontalternative\endcsname
       :\fontalternative
     \else
       :\s!default
     \fi
   \endcsname}

%D Variant selectors
%D
%D \starttyping
%D \mathematics {\vsone{\utfchar{0x2229}}}
%D \mathematics {\utfchar{0x2229}\vsone{}}
%D \stoptyping

\permanent\protected\edef\vsone#character{#character\tocharacter"FE00 } % used
\permanent\protected\edef\vstwo#character{#character\tocharacter"FE01 } % not used but handy for testing

%D For historic reasons we keep the following around but they are no longer that
%D relevant for \MKIV. But \unknown\ they will be dropped in \LMTX !

\permanent\protected\def\doattributes#1#2#3#4%
  {\begingroup  % geen \bgroup, anders in mathmode lege \hbox
   \dousestylehashparameter{#1}{#2}%
   \dousecolorhashparameter{#1}{#3}%
   #4%
   \endgroup}

\permanent\protected\def\dostartattributes#1#2#3%
  {\begingroup  % geen \bgroup, anders in mathmode lege \hbox
   \dousestylehashparameter{#1}{#2}%
   \dousecolorhashparameter{#1}{#3}}

\aliased\let\dostopattributes\endgroup

%D New but it needs to be supported explicitly (as in natural tables).

\newconditional\c_font_styles_math

\protected\def\font_styles_math_reset
  {\c_font_styles_math\conditionalfalse}

\protected\def\font_styles_math_start
  {\ifconditional\c_font_styles_math
     \startimath
   \fi
   \relax}

\protected\def\font_styles_math_stop
  {\relax
   \ifconditional\c_font_styles_math
     \stopimath
   \fi}

\definealternativestyle[\v!math][\c_font_styles_math\conditionaltrue]

\protect \endinput

