%D \module
%D   [       file=meta-tex,
%D        version=2006.06.07,
%D          title=\CONTEXT\ Support Macros,
%D       subtitle=\METAPOST\ fast text insertion,
%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.

\registerctxluafile{meta-tex}{1.001}

\unprotect

% Ok, we support this in MkIV because Mojca kept the pressure on. It
% looks a bit like a hack. But in fact this method is obsolete and
% eventually might go away.

\installcorenamespace{graphictextext}
\installcorenamespace{graphictexdepth}
\installcorenamespace{graphictexarguments}

\unexpanded\def\startTeXtexts#1\stopTeXtexts
  {#1}

\let\stopTeXtexts\relax

\unexpanded\def\TeXtext
  {\dosingleempty\meta_textext}

\def\meta_textext[#1]#2#3% contrary to mkii we don't process yet but we do expand
  {\setxvalue{\??graphictextext#2}{\meta_textext_indeed{#1}{#3}}}

\unexpanded\def\meta_textext_indeed#1#2%
  {\begingroup
   \setbox\nextbox\hbox{#2}%
   \executeifdefined{\??graphictexdepth#1}{\getvalue{\??graphictexdepth\s!depth}}%
   \box\nextbox
   \endgroup}

\def\getTeXtext#1%
  {\getvalue{\??graphictextext#1}}

\setvalue{\??graphictexdepth d}{\setbox\nextbox\hbox{\lower\dp\nextbox\box\nextbox}}  % unchecked
\letvalue{\??graphictexdepth n}\donothing                                             % unchecked

\setvalue{\??graphictexdepth\s!depth  }{\getvalue{\??graphictexdepth d}}
\letvalue{\??graphictexdepth\s!nodepth}\donothing

% \definetextext[framed]{\framed}
%
% \startMPcode
%     draw \sometxt[framed]{black} rotated 45 ;
% \stopMPcode
%
% But Mojca wanted more! Two arguments.
%
% \definetextext[framed]{\framed}
%
% \startMPcode
%     draw \sometxt{This is for} rotated 45 ;
%     draw \sometxt[framed][foregroundstyle=bold]{Mojca's};
%     draw \sometxt[framed]{eyes only!} rotated -45 ;
% \stopMPcode

\unexpanded\def\definetextext[#1]%
  {\def\currenttextext{#1}%
   \doifelsenextoptionalcs\meta_textext_define_one\meta_textext_define_zero}

\def\meta_textext_define_one {\setvalue{\??graphictexarguments1:\currenttextext}}
\def\meta_textext_define_zero{\setvalue{\??graphictexarguments0:\currenttextext}}

\def\sometxt#1#{\meta_some_txt{#1}}

\def\meta_some_txt#1#2% we need to capture embedded quotes (a bit messy as later on textext is filtered
  {textext.drt("\mpsometxt#1{\clf_metapostescaped{#2}}")}

\unexpanded\def\mpsometxt % no _ catcode
  {\doifelsenextoptionalcs\meta_some_txt_indeed_yes\meta_some_txt_indeed_nop}

\def\meta_some_txt_indeed_yes[#1]%
  {\def\currenttextext{#1}%
   \csname\??graphictexarguments
     \ifcsname\??graphictexarguments0:#1\endcsname0\else
     \ifcsname\??graphictexarguments1:#1\endcsname1\else
                                       ?\fi\fi
   \endcsname}

\def\meta_some_txt_indeed_nop
  {}

\setvalue{\??graphictexarguments ?}{}
\setvalue{\??graphictexarguments 0}{\csname\??graphictexarguments\currenttextext\endcsname}
\setvalue{\??graphictexarguments 1}{\dosingleempty\meta_gx_one}

\def\meta_gx_one{\csname\??graphictexarguments1:\currenttextext\endcsname}

% \definetextext[framed][#1]#2{\framed[width=4cm]{\switchtobodyfont[#1]#2}}
% \definetextext[simple]{\framed[width=8cm]}
%
%  \starttext
%
%  \startMPcode
%      draw \sometxt{This is for} rotated 45 ;
%      draw \sometxt[framed][ss,16pt] {Mojca's};
%      draw \sometxt[framed]{eyes only!} rotated -45 ;
%      draw \sometxt[simple]{Quotes "indeed" work!} rotated 180 ;
%  \stopMPcode
%
% \stoptext
%
% a lua one:
%
% \unexpanded\def\definetextext[#1]#2{\setvalue{@@st@@#1}{#2}}
%
%       \def\sometxt         #1#{\dosometxt{#1}}  % grab optional [args]
%       \def\dosometxt      #1#2{\ctxlua{metapost.sometxt(\!!bs#1\!!es,\!!bs#2\!!es)}}
%
%            \def\sometxta         #1{textext.drt("#1")}
%            \def\sometxtb       #1#2{textext.drt("\getvalue{@@st@@#1}{#2}")}
%            \def\sometxtc     #1#2#3{textext.drt("\getvalue{@@st@@#1}{\switchtobodyfont[#2]#3}")}

% Best use the mp macro instead since it provides positioning.
%
% \startuseMPgraphic{testgraphic}
% draw \sometxt{\ruledhbox{\strut hans hagen}} scaled 3;
% % draw textext.drt("\ruledhbox{\strut hans hagen}") scaled 3;
% % draw textext.dlft("\ruledhbox{\strut hans hagen}") scaled 3;
% % draw textext.lft("\ruledhbox{\strut hans hagen}") scaled 3;
% draw llcorner currentpicture -- urcorner currentpicture withcolor yellow;
% draw lrcorner currentpicture -- ulcorner currentpicture withcolor yellow;
% draw boundingbox currentpicture withcolor blue ;
% draw origin withcolor red withpen pencircle scaled 1pt;
% \stopuseMPgraphic
%
% {\showstruts\useMPgraphic{testgraphic}}

\unexpanded\def\MPexponent   #1#2{\mathematics{#1\times10^{#2}}}
\unexpanded\def\MPformatted  #1#2{\clf_metapostformatted{#1}{#2}}
\unexpanded\def\MPgraphformat#1#2{\clf_metapostgraphformat{#1}{#2}}

\protect \endinput
