%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

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

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

\permanent\protected\def\startTeXtexts#1\stopTeXtexts
  {#1}

\permanent\protected\lettonothing\stopTeXtexts

\permanent\tolerant\protected\def\TeXtext[#1]#:#2#3% contrary to mkii we don't process yet but we do expand
  {\xdefcsname\??graphictextext#2\endcsname{\meta_textext_indeed{#1}{#3}}}

\protected\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}

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

\letcsname\??graphictexdepth         d\endcsname\syst_boxes_lower_nextbox_dp
\letcsname\??graphictexdepth  \s!depth\endcsname\syst_boxes_lower_nextbox_dp
\letcsname\??graphictexdepth         n\endcsname\donothing
\letcsname\??graphictexdepth\s!nodepth\endcsname\donothing

% old hackery, slightly modernized

\permanent\protected\def\definetextext[#1]%
  {\tolerant\defcsname\??graphictexarguments#1\endcsname}

\permanent\tolerant\def\sometxt[#1]#*[#S#2]%
  {\ifarguments
     \expandafter\meta_some_txt_a
   \or
     \expandafter\meta_some_txt_b
   \or
     \expandafter\meta_some_txt_c
   \or
   \fi{#1}{#2}}

\def\meta_some_txt_a#1#2#3{textext.drt("\clf_metapostescaped{#3}")}
\def\meta_some_txt_b#1#2#3{textext.drt("\mpsometxt{#1}{\clf_metapostescaped{#3}}")}
\def\meta_some_txt_c#1#2#3{textext.drt("\mpsometxt{#1}[#2]{\clf_metapostescaped{#3}}")}

\permanent\protected\def\mpsometxt#1%
  {\begincsname\??graphictexarguments#1\endcsname}

% \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
%
% \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:
%
% \protected\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}}

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

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

\permanent\protected\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[sansserif-normal-normal]  [\s!Sans]
\definefontsynonym[sansserif-normal-italic]  [\s!SansItalic]
\definefontsynonym[sansserif-normal-oblique] [\s!SansSlanted]
\definefontsynonym[sansserif-bold-normal]    [\s!SansBold]
\definefontsynonym[sansserif-bold-italic]    [\s!SansBoldItalic]
\definefontsynonym[sansserif-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}

\defcsname\??svgfamily      \endcsname{serif}
\defcsname\??svgfamily serif\endcsname{serif}
\defcsname\??svgfamily  sans\endcsname{sans}
\defcsname\??svgfamily  mono\endcsname{mono}

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

\permanent\protected\def\svgembeddedfigure#1%
  {\clf_embedimagefromstore {svg} #1 true\relax}

% todo: use the independent color and transparency mechanism

\permanent\protected\def\svghashed#1%
  {\clf_svghashed#1\relax}

\permanent\protected\def\svg_normal_pcode#1#2#3%
  {\glyph xoffset #1\onebasepoint yoffset #2\onebasepoint #3\relax}

\permanent\protected\def\svg_normal_pchar#1#2#3%
  {\glyph xoffset #1\onebasepoint yoffset #2\onebasepoint `#3\relax}

\permanent\protected\def\svg_normal_pspace#1#2%
  {\space}

\permanent\protected\def\svg_normal_space
  {\space}

\permanent\protected\def\svg_normal_code#1%
  {\char#1\relax}

\permanent\protected\def\svg_normal_char#1%
  {#1\relax}

\permanent\protected\def\svg_normal_start
  {\resetlayer[svgmps]%
   \setlayer[svgmps]\bgroup}

\permanent\protected\def\svg_normal_stop
  {\egroup
   \flushlayer[svgmps]}

\permanent\protected\def\svg_normal_color_c#1#2#3%#4%   % color
  {\colored[r=#1,g=#2,b=#3]}%{#4}}

\permanent\protected\def\svg_normal_color_o#1%#2%       % opace
  {\colored[a=1,t=#1]}%{#2}}

\permanent\protected\def\svg_normal_color_b#1#2#3#4%#5% % both
  {\colored[r=#1,g=#2,b=#3,a=1,t=#4]}% {#5}}

\permanent\protected\def\svg_normal_set#1#2#3%
  {\setlayer[svgmps]{\hbox xoffset #1\onebasepoint yoffset #2\onebasepoint to 10sp{#3}}}

\permanent\protected\def\svg_normal_font#1#2#3%#4%
  {\hbox\bgroup
   \ifempty{#2}\else
      \edef\p_font{\ifcsname\??svgfamily#1\endcsname\lastnamedcs\else#1\fi-#2-#3}%
      \ifcsname svg:\p_font\endcsname
        \lastnamedcs
      \else
        \predefinefont[svg:\p_font][\s!spec:\p_font*\s!default\space @ 10bp]%
        \csname svg:\p_font\endcsname
      \fi
   \fi
   \let\next}
 % #4%
 % \egroup}

\permanent\protected\def\svg_normal_size#1%#2%
  {\hbox\bgroup
   \glyphscale\numericscale#1\relax % we need a period !
   \let\next}
 % #2%
 % \egroup}

\permanent\protected\def\svg_traced_pcode#1#2#3%
  {\writestatus{SVG TEXT}{....[poscode #1 #2 \tocharacter#3]}%
   \svg_normal_pcode{#1}{#2}{#3}}

\permanent\protected\def\svg_traced_pchar#1#2#3%
  {\writestatus{SVG TEXT}{....[poschar #1 #2 #3]}%
   \svg_normal_pchar{#1}{#2}{#3}}

\permanent\protected\def\svg_traced_pspace#1#2%
  {\writestatus{SVG TEXT}{....[posspace #1 #2]}%
   \svg_normal_pspace{#1}{#2}}

\permanent\protected\def\svg_traced_code#1%
  {\writestatus{SVG TEXT}{....[char \tocharacter#1]}%
   \svg_normal_code{#1}}

\permanent\protected\def\svg_traced_char#1%
  {\writestatus{SVG TEXT}{....[char #1]}%
   \svg_normal_char{#1}}

\permanent\protected\def\svg_traced_space
  {\writestatus{SVG TEXT}{....[space]}%
   \svg_normal_space}

\permanent\protected\def\svg_traced_start
  {\writestatus{SVG TEXT}{[start layer]}%
   \svg_normal_start}

\permanent\protected\def\svg_traced_stop
  {\svg_normal_stop
   \writestatus{SVG TEXT}{[stop layer]}}

\permanent\protected\def\svg_traced_color_c#1#2#3#4%
  {\writestatus{SVG TEXT}{..[start color #1 #2 #3]}%
   \svg_normal_color_c{#1}{#2}{#3}{#4}%
   \writestatus{SVG TEXT}{..[stop color]}}

\permanent\protected\def\svg_traced_color_o#1#2%
  {\writestatus{SVG TEXT}{..[start opace #1]}%
   \svg_normal_color_o{#1}{#2}%
   \writestatus{SVG TEXT}{..[stop opace]}}

\permanent\protected\def\svg_traced_color_b#1#2#3#4#5%
  {\writestatus{SVG TEXT}{..[start color #1 #2 #3 opace #4]}%
   \svg_normal_color_b{#1}{#2}{#3}{#4}{#5}%
   \writestatus{SVG TEXT}{..[stop color opace]}}

\permanent\protected\def\svg_traced_set#1#2#3%
  {\writestatus{SVG TEXT}{...[start set layer #1 #2]}%
   \svg_normal_set{#1}{#2}{#3}%
   \writestatus{SVG TEXT}{...[stop set setlayer]}}

\permanent\protected\def\svg_traced_font#1#2#3#4%
  {\writestatus{SVG TEXT}{.[start font #1 #2 #3]}%
   \svg_normal_font{#1}{#2}{#3}{#4}%
   \writestatus{SVG TEXT}{.[stop font]}}

\permanent\protected\def\svg_traced_size#1#2%
  {\writestatus{SVG TEXT}{.[start size #1]}%
   \svg_normal_size{#1}{#2}%
   \writestatus{SVG TEXT}{.[stop size]}}

\permanent\protected\def\svg_normal_text
  {\enforced\let\svgpcode \svg_normal_pcode
   \enforced\let\svgpchar \svg_normal_pchar
   \enforced\let\svgpspace\svg_normal_pspace
   \enforced\let\svgcode  \svg_normal_code
   \enforced\let\svgchar  \svg_normal_char
   \enforced\let\svgspace \svg_normal_space
   \enforced\let\svgstart \svg_normal_start
   \enforced\let\svgstop  \svg_normal_stop
   \enforced\let\svgcolorc\svg_normal_color_c
   \enforced\let\svgcoloro\svg_normal_color_o
   \enforced\let\svgcolorb\svg_normal_color_b
   \enforced\let\svgset   \svg_normal_set
   \enforced\let\svgfont  \svg_normal_font
   \enforced\let\svgsize  \svg_normal_size}

\permanent\protected\def\svg_traced_text
  {\enforced\let\svgpcode \svg_traced_pcode
   \enforced\let\svgpchar \svg_traced_pchar
   \enforced\let\svgpspace\svg_traced_pspace
   \enforced\let\svgcode  \svg_traced_code
   \enforced\let\svgchar  \svg_traced_char
   \enforced\let\svgspace \svg_traced_space
   \enforced\let\svgstart \svg_traced_start
   \enforced\let\svgstop  \svg_traced_stop
   \enforced\let\svgcolorc\svg_traced_color_c
   \enforced\let\svgcoloro\svg_traced_color_o
   \enforced\let\svgcolorb\svg_traced_color_b
   \enforced\let\svgset   \svg_traced_set
   \enforced\let\svgfont  \svg_traced_font
   \enforced\let\svgsize  \svg_traced_size}

\installtextracker
   {svg.text}
   {\svg_traced_text}
   {\svg_normal_text}

\svg_normal_text

\protect \endinput
