%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}{}

\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}%
   \ifcsname\??graphictexdepth#1\endcsname
     \lastnamedcs
   \else
     \syst_boxes_lower_nextbox_dp
   \fi
   \box\nextbox
   \endgroup}

\def\getTeXtext#1%
  {\begincsname\??graphictextext#1\endcsname}

\letvalue{\??graphictexdepth         d}\syst_boxes_lower_nextbox_dp
\letvalue{\??graphictexdepth  \s!depth}\syst_boxes_lower_nextbox_dp
\letvalue{\??graphictexdepth         n}\donothing
\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}}

%D Some new fun (but exprimental for a while):

\unexpanded\def\MPLIBoutlinetext#1#2#3% index kind text
  {\begingroup
   \setbox\scratchbox\hbox{#3}%
   \clf_MPLIBconvertoutlinetext\numexpr#1\relax{#2}\scratchbox
   \endgroup}

%D For now here. We don't do the whole thing, because then one can as
%D well just export to pdf. We assume consistent integration.

\definefontsynonym[serif-normal-normal] [\s!Serif]
\definefontsynonym[serif-normal-italic] [\s!SerifItalic]
\definefontsynonym[serif-normal-oblique][\s!SerifSlanted]
\definefontsynonym[serif-bold-normal]   [\s!SerifBold]
\definefontsynonym[serif-bold-italic]   [\s!SerifBoldItalic]
\definefontsynonym[serif-bold-oblique]  [\s!SerifBoldSlanted]

\definefontsynonym[sans-normal-normal]  [\s!Sans]
\definefontsynonym[sans-normal-italic]  [\s!SansItalic]
\definefontsynonym[sans-normal-oblique] [\s!SansSlanted]
\definefontsynonym[sans-bold-normal]    [\s!SansBold]
\definefontsynonym[sans-bold-italic]    [\s!SansBoldItalic]
\definefontsynonym[sans-bold-oblique]   [\s!SansBoldSlanted]

\definefontsynonym[mono-normal-normal]  [\s!Mono]
\definefontsynonym[mono-normal-italic]  [\s!MonoItalic]
\definefontsynonym[mono-normal-oblique] [\s!MonoSlanted]
\definefontsynonym[mono-bold-normal]    [\s!MonoBold]
\definefontsynonym[mono-bold-italic]    [\s!MonoBoldItalic]
\definefontsynonym[mono-bold-oblique]   [\s!MonoBoldSlanted]

\installcorenamespace {svgfamily}

\setvalue{\??svgfamily      }{serif}
\setvalue{\??svgfamily serif}{serif}
\setvalue{\??svgfamily  sans}{sans}
\setvalue{\??svgfamily  mono}{mono}

%D This can't change because otherwise \type {\scale[width=4cm]{graphic}} fails.

\unexpanded\def\svgscaled#1%
  {\fastsxsy{#1}{#1}\svgnormal}

\unexpanded\def\svgnormal#1#2#3#4% family weight style text
  {\bgroup
   \edef\p_family{#1}%
   \ifx\p_family\empty\else
      \edef\p_font{\ifcsname\??svgfamily#1\endcsname\lastnamedcs\else#1\fi-#2-#3}%
      \predefinedfont[\s!spec:\p_font*\s!default\space @ 10bp]%
   \fi
   #4%
   \egroup}

\unexpanded\def\svgcolored#1#2#3#4% r g b text
  {\colored[r=#1,g=#2,b=#3]{#4}}

\unexpanded\def\svgplaced#1#2#3#4% dx dy width text
  {\doifelsenothing{#3}%
     {\hpack}%
     {\hpack to #3}%
     {\raise#2\onebasepoint\hbox{\kern#1\onebasepoint\relax#4}}}

\newdimen\grph_svg_scratchwidth % maybe \startsvgpos etc

\unexpanded\def\svgposchar#1#2#3% dx dy char
  {\setbox\scratchbox\hpack\bgroup
     \raise#2\onebasepoint\hbox\bgroup
        \kern\dimexpr#1\onebasepoint-\grph_svg_scratchwidth\relax
        \char#3\relax
     \egroup
   \egroup
   \advance\grph_svg_scratchwidth\wd\scratchbox
   \box\scratchbox}

\unexpanded\def\svgchar#1% char
  {\char#1\relax}

\definelayer[svgmps][\c!method=\v!fit]

\unexpanded\def\svgstartlayer
  {\resetlayer[svgmps]%
   \setlayer[svgmps]\bgroup}

\unexpanded\def\svgstoplayer
  {\egroup
   \flushlayer[svgmps]}

\unexpanded\def\svgsetlayer#1#2%
  {\setlayer[svgmps][\c!x=#1\onebasepoint,\c!y=#2\onebasepoint]} % {#3}

\unexpanded\def\svghashed#1%
  {\clf_svghashed#1\relax}

\protect \endinput

