%D \module
%D   [       file=font-tra,
%D        version=2009.01.02, % or so
%D          title=\CONTEXT\ Font Macros,
%D       subtitle=Tracing,
%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

\writestatus{loading}{ConTeXt Font Macros / Tracing}

%D just use fontid

%D \macros
%D   {showbodyfont}
%D
%D One can call for a rather simple overview of a bodyfont and the
%D relations between its alternative fonts.
%D
%D \showsetup{showbodyfont}
%D
%D The current bodyfont (here we omitted the argument) looks like:
%D
%D \showbodyfont
%D
%D The implementation is rather straightforward in using
%D \type{\halign}.

\fetchruntimecommand \showbodyfont  {\f!fontprefix\s!run}

%D \macros
%D   {showfontstrip, testminimalbaseline, showminimalbaseline}
%D
%D The next command can come in handy when combining
%D different fonts into a collection (typeface) and
%D determining optimal baseline distances.
%D
%D \showfontstrip \blank \showminimalbaseline

\fetchruntimecommand \showfontstrip       {\f!fontprefix\s!run}
\fetchruntimecommand \testminimalbaseline {\f!fontprefix\s!run}
\fetchruntimecommand \showminimalbaseline {\f!fontprefix\s!run}

%D \macros
%D   {showkerning}
%D
%D A goody is:
%D
%D \showkerning{Can you guess what kerning is?}

\fetchruntimecommand \showkerning {\f!fontprefix\s!run}

%D \macros
%D   {showbodyfontenvironment,showfont,showfontstyle,showligatures}
%D
%D The current bodyfontenvironment is:
%D
%D \showbodyfontenvironment
%D
%D This overview is generated using:
%D
%D \showsetup{showbodyfontenvironment}

\fetchruntimecommand \showbodyfontenvironment {\f!fontprefix\s!run}

%D
%D The following command generates a fontmap:
%D
%D \startbuffer
%D \showfont[SansBold at 12pt]
%D \stopbuffer
%D
%D \typebuffer
%D \getbuffer

\fetchruntimecommand \showfont           {\f!fontprefix\s!run}
\fetchruntimecommand \showfontstyle      {\f!fontprefix\s!run}
\fetchruntimecommand \showligature       {\f!fontprefix\s!run}
\fetchruntimecommand \showligatures      {\f!fontprefix\s!run}
\fetchruntimecommand \showcharratio      {\f!fontprefix\s!run}
\fetchruntimecommand \showfontparameters {\f!fontprefix\s!run}

\unexpanded\def\showchardata          #1{\ctxcommand{showchardata("#1")}}
\unexpanded\def\showfontdata            {\ctxcommand{showfontparameters()}}

%D \macros
%D   {doiffontpresentelse}
%D
%D \starttyping
%D \doiffontpresentelse{texnansi-lmr10}{YES}{NO}
%D \doiffontpresentelse{adam-lindsay-modern-serif}{YES}{NO}
%D \stoptyping

\unexpanded\def\doifelsefontpresent#1{\clf_doifelsefontpresent{#1}}

\let\doiffontpresentelse\doifelsefontpresent

% experimental, maybe this becomes a module

\newbox\otfcollector

\unexpanded\def\startotfcollecting{\ctxlua{nodes.tracers.steppers.start()}}
\unexpanded\def\stopotfcollecting {\ctxlua{nodes.tracers.steppers.stop()}}
\unexpanded\def\resetotfcollecting{\ctxlua{nodes.tracers.steppers.reset()}}

% Rather experimental:
%
% \page \showotfcomposition{arabtype*arab-default at 48pt}{-1}{الضَّرَّ} \page
% \page \showotfcomposition{arabtype*arab-default at 48pt}{-1}{لِلّٰهِ}   \page

\unexpanded\def\showotfstepglyphs#1%
  {\ctxlua{nodes.tracers.steppers.glyphs(\number\otfcollector,#1)}%
   \unhbox\otfcollector}

\unexpanded\def\otfstepspace
  {\removeunwantedspaces
   \hskip.5\emwidth \s!plus .125\emwidth \s!minus .125\emwidth\relax}

\unexpanded\def\otfstepcharcommand#1#2#3% font char class
  {\otfstepspace
   \doif{#3}{mark}{\underbar}{U+\hexnumber{#2}}:\ruledhbox{\ctxlua{nodes.tracers.fontchar(#1,#2)}}%
   \otfstepspace}

\unexpanded\def\otfstepfontcommand#1#2#3% id font size
  {\begingroup
   \tttf #1: #2 @ \the\dimexpr#3\scaledpoint\relax
   \endgroup}

\unexpanded\def\otfstepmessagecommand#1#2%
  {\begingroup
   \tttf\language\minusone
   \veryraggedright
   \forgetparindent
   \forgeteverypar
   \hangindent\emwidth
   \hangafter\plusone
   \dontleavehmode\detokenize{#1}\removeunwantedspaces
   \doifsomething{#2}{,\space\detokenize{#2}}\endgraf
   \endgroup
   \blank}

\unexpanded\def\showotfstepfont
  {\ctxlua{nodes.tracers.steppers.font("otfstepfontcommand")}}

\unexpanded\def\showotfstepchars#1%
  {\ctxlua{nodes.tracers.steppers.codes(#1,"otfstepcharcommand","otfstepspace")}}

\unexpanded\def\showotfstepmessages#1%
  {\ctxlua{nodes.tracers.steppers.messages(#1,"otfstepmessagecommand",true)}}

\unexpanded\def\showotfstepfeatures
  {\ctxlua{nodes.tracers.steppers.features()}}

\unexpanded\def\otfnoffeaturesteps
  {\ctxlua{nodes.tracers.steppers.nofsteps()}}

% \unexpanded\def\showotfsteps
%   {\begingroup
%    \veryraggedright
%    \forgetparindent
%    \forgeteverypar
%    \dontleavehmode\bgroup\tttf \language\minusone features: \showotfstepfeatures\egroup
%    \blank
%    \dontleavehmode\bgroup\tttf result:\egroup
%    \blank
%    \startlinecorrection
%    \ruledhbox\bgroup\box\otfcompositionbox\egroup
%    \stoplinecorrection
%    \dorecurse\otfnoffeaturesteps
%      {\blank
%       \showotfstepmessages\recurselevel
%       \blank
%       \startlinecorrection
%       \dontleavehmode\bgroup\resetallattributes\pardir TLT\textdir TLT\relax\tttf\recurselevel: \showotfstepchars\recurselevel\egroup
%       \stoplinecorrection
%       \blank
%       \startlinecorrection
%       \ruledhbox % can be mode
%         \bgroup\resetallattributes\showotfstepglyphs\recurselevel\egroup % reset is new, we don't want additional processing
%       \stoplinecorrection
%       \blank}%
%    \endgroup}

\unexpanded\def\showotfsteps
  {\begingroup
   \veryraggedright
   \forgetparindent
   \forgeteverypar
   \tt
   \lefttoright
   \hbox to \hsize \bgroup
     \hbox to 6\emwidth{\bf font\hss}%
     \vtop \bgroup
       \hsize\dimexpr\hsize-6\emwidth\relax
       \language\minusone
       \bf
       \showotfstepfont
     \egroup
   \egroup
   \blank
   \hbox to \hsize \bgroup
     \hbox to 6\emwidth{\bf features\hss}%
     \vtop \bgroup
       \hsize\dimexpr\hsize-6\emwidth\relax
       \language\minusone
       \showotfstepfeatures
     \egroup
   \egroup
   \blank
   \scratchcounter\otfnoffeaturesteps\relax
   \dorecurse\scratchcounter
     {\blank
      \begingroup
        \advance\leftskip6\emwidth
        \showotfstepmessages\recurselevel
        \par
      \endgroup
      \blank
      \dontleavehmode
      \hbox to \hsize \bgroup
        \hbox to 6\emwidth \bgroup
          \bf
          \ifnum\recurselevel=\scratchcounter result\else step \recurselevel\fi
          \hss
        \egroup
        \vtop \bgroup
          \hsize\dimexpr\hsize-6\emwidth\relax
          \resetallattributes
          \pardir TLT\textdir TLT\relax
          \dontleavehmode
          \ifnum\recurselevel=\scratchcounter
            \ruledhbox{\box\otfcompositionbox}%
          \else
            \ruledhbox{\showotfstepglyphs\recurselevel}%
          \fi
          \quad
          \showotfstepchars\recurselevel
          \hfill
          \par
        \egroup
      \egroup
      \blank}%
   \endgroup}

\unexpanded\def\startotfsample
  {\enabletrackers[*otf.sample]% beware, kind of global
   \startotfcollecting
   \begingroup
   \veryraggedright
   \forgetparindent
   \forgeteverypar}

\unexpanded\def\stopotfsample
  {\endgroup
   \stopotfcollecting
   \disabletrackers[*otf.sample]% beware, kind of global: otf.sample
   \showotfsteps
   \resetotfcollecting}

\newbox\otfcompositionbox

% this should go in spac-ali:

\installcorenamespace{otfcompositiondir}

\letvalue{\??otfcompositiondir  -1}\righttoleft
\letvalue{\??otfcompositiondir r2l}\righttoleft
\letvalue{\??otfcompositiondir l2r}\lefttoright
\letvalue{\??otfcompositiondir  +1}\lefttoright
\letvalue{\??otfcompositiondir   1}\lefttoright

\unexpanded\def\showotfcomposition#1#2#3% {font*features at size}, rl=-1, text
  {\begingroup
   \forgetparindent
   \forgeteverypar
 % \setupcolors[\c!state=\v!start]%
   \setupalign[\v!verytolerant,\v!flushleft]%
   \startotfsample
   \nohyphens
   \global\setbox\otfcompositionbox\hbox{\definedfont[#1]\relax\getvalue{\??otfcompositiondir#2}\relax#3}%
   \stopotfsample
   \endgroup}

% new

\unexpanded\def\savefont[#1]% not yet in i-*.xml
  {\begingroup
   \getdummyparameters[#1]%
   \clf_savefont {
      filename {\dummyparameter\c!file}
      fontname {\dummyparameter\c!name}
      method   {\dummyparameter\c!method}
   }%
   \endgroup}

\protect \endinput
