%\input luaotfload.sty
\input miniltx
\input color
\definecolor{orange}{cmyk}{0,.5,1,0}
\input luamplib.sty
\mpliblegacybehavior{true}%
\everymplib{ beginfig(0); }\everyendmplib{ endfig; }
\tracingcommands1
A%
\mplibcode
%% test all printable ascii chars in comments
%%    (  2  <  F  P  Z  d   n   x
%%    )  3  =  G  Q  [  e   o   y
%%    *  4  >  H  R  \  f   p   z
%% !  +  5  ?  I  S  ]  g   q   {
%% "  ,  6  @  J  T  ^  h   r   |
%% #  -  7  A  K  U  _  i   s   }
%% $  .  8  B  L  V  `  j   t   ~
%% %  /  9  C  M  W  a  k   u  DEL
%% &  0  :  D  N  X  b  l   v
%% ´  1  ;  E  O  Y  c  m   w
   fill fullcircle scaled 20;
\endmplibcode
B\par
\everymplib{}\everyendmplib{}% reset toks
A%
\mplibcode
verbatimtex \lower.2em etex
beginfig(0);
draw origin--(1cm,0) withcolor red;
draw btex g etex withcolor blue;
draw btex\tracingcommands0 multi
    line
    tex code
    100 \% color
  etex shifted (20,0);
draw textext "\tracingcommands0 textext 50\% color" shifted (10,-10) withcolor .5white;
draw bbox currentpicture;
endfig;

beginfig(18);
numeric u;
u = 1cm;
draw (0,2u)--(0,0)--(4u,0);
pickup pencircle scaled 1pt;
draw (0,0){up}
  for i=1 upto 8: ..(i/2,sqrt(i/2))*u  endfor;
label.lrt(btex $\sqrt x$ etex, (3,sqrt 3)*u);
label.bot(btex $x$ etex, (2u,0));
label.lft(btex $y$ etex, (0,u));
endfig;
\endmplibcode
B\par
A%
\mplibcode
beginfig(2);
numeric u; u=1cm;
z1=-z2=(-u,0);
for i = 1 upto 3:
  draw z1..(0, i*u)..z2;
  label.top(TEX("$e_{" & decimal(i) & "}$"), (0, i*u)) withcolor \mpcolor{orange};
endfor;
endfig;
\endmplibcode
B\par\mplibforcehmode
\mplibcodeinherit{enable}%
\mplibglobaltextext{enable}%
\everymplib{ beginfig(0);}\everyendmplib{ endfig;}%
\mplibcode
 label(btex $\sqrt{2}$ etex, origin);
 draw fullcircle scaled 20;
 picture pic; pic := currentpicture;
\endmplibcode
\mplibcode
 currentpicture := pic scaled 2;
\endmplibcode
\mplibnoforcehmode
\everymplib{}\everyendmplib{}%
\mplibcodeinherit{disable}%
\mplibglobaltextext{disable}\par
\mplibsetformat{metafun}%
\mplibcode
message "Hello World!";
verbatimtex \moveright 0.4\hsize etex
beginfig(0);
path p; p:= fullcircle scaled 2cm yshifted .5cm;
fill p withcolor transparent("normal", 0.5, red);
fill p rotated 120 withcolor transparent("normal", 0.5, green);
fill p rotated 240 withcolor transparent("normal", 0.5, blue);
endfig;
verbatimtex \leavevmode etex
picture p; p := btex MetaPost etex scaled 2;
beginfig(1);
linear_shade(bbox p,0,blue,.7white);
draw p withcolor white;
endfig;
verbatimtex \kern10pt etex
beginfig(2);
circular_shade(bbox p,0,blue,.7white);
draw p withcolor white;
endfig;
\endmplibcode

\newbox\mympbox
\def\prependtomplibbox{\global\setbox\mympbox}%
\mplibcode
%verbatimtex \global\setbox\mympbox etex
beginfig(0);
breadth=.667\mpdim\hsize;
height=2pt;
x1=0; x2=x6=.333x4; x5=x3=.667x4;
x4=breadth;
y1=y4=height/2; y2=y3=height; y5=y6=0;
fill z1--z2--z3--z4--z5--z6--cycle;
endfig;
\endmplibcode
\copy\mympbox
\copy\mympbox
\copy\mympbox
\copy\mympbox

\mplibnoforcehmode
\mplibnumbersystem{double}%
\mplibcode
beginfig(0);
u := 10**5*(10**-4);
draw unitsquare scaled u;
endfig;
\endmplibcode
\mplibsetformat{plain}%
\mplibcode
  input graph;
  beginfig(0);
  draw begingraph(100,100);
    gdraw (10,10)--(30,35)--(50,25)--(70,80)--(90,90);
    autogrid(otick.bot,);
    for y=20,40,60,80:
      grid.lft(format("%e",1000y), y) withcolor .85white;
    endfor
    endgraph;
  endfig;
\endmplibcode
\mplibtextextlabel{enable}%
\mplibcode
beginfig(0);
dotlabel.rt("$\sqrt2$",origin);
endfig;
\endmplibcode
\leavevmode
\mplibcode
   D := sqrt(2)**7;
   beginfig(0);
   draw fullcircle scaled D;
   VerbatimTeX("\gdef\Dia{" & decimal D & "}");
   endfig;
\endmplibcode
diameter:\Dia bp.%
\mplibcode
  vardef rotatedlabel@#(expr str, loc, angl) =
    draw thelabel@#(str, loc) rotatedaround(loc, angl)
  enddef;

  beginfig(1);
    rotatedlabel.top(textext "Rotated!", origin, 45);
  endfig;
\endmplibcode
\par
\mplibcode
beginfig(1) % in dvi mode, not work for type1 font
draw mplibgraphictext "\bf Funny$\sqrt{2}$"
  fakebold 2 % fontspec option
  drawcolor blue fillcolor .5[red,white]
  scaled 3 rotated 30 ;
draw mplibgraphictext "\it xyz"
  fillcolor .7white
  scaled 3 shifted (40,0);
endfig;
\endmplibcode
\par
%\mplibsetformat{metafun}%
\mplibcode
beginfig(1)
fill unitsquare xscaled \mpdim\hsize yscaled 1cm
    withshadingmethod "linear"
    withshadingvector (0,1)
    withshadingstep (
       withshadingfraction .5
       withshadingcolors (red,blue)
    )
    withshadingstep (
       withshadingfraction 1
       withshadingcolors (blue,green)
    )
    ;
endfig;
\endmplibcode
\leavevmode
\mpfig
color yellow; yellow = (1,1,0);
picture Q, u, e;
Q := mplibglyph "Q" of "texgyrepagella-bolditalic.otf" scaled .1;
u := mplibglyph "u" of "texgyrepagella-bolditalic.otf" scaled .1 shifted lrcorner Q;
e := mplibglyph "e" of "texgyrepagella-bolditalic.otf" scaled .1 shifted lrcorner u;
i:=0;
totallen := length Q + length u + length e;
for pic=Q, u, e:
  for item within pic:
    i:=i+1;
    fill pathpart item
    if i < totallen: withpostscript "collect"; fi
  endfor
endfor
  withshadingmethod "linear"
  withshadingdirection (0.5,2.5)
  withshadingcolors (.7red,.7yellow)
  ;
\endmpfig
\everymplib[@mpfig]{ drawoptions(withcolor mplibrgbtexcolor"orange"); }%
\mpfig* input boxes \endmpfig
\mpfig circleit.a(btex\tracingcommands0 Box 1 etex); drawboxed(a); \endmpfig
\def\mpfiginstancename{mympfig}%
\mpfig
draw mpliboutlinetext.b ("$\overbrace{x+\cdots+x}^k$")
    (withcolor .6[red,white])
    (withpen pencircle scaled .2 withcolor red)
    scaled 3 ;
\endmpfig
\par
\mpfig
  draw mpliboutlinetext.r
    ("Question")
    ( withpen pencircle scaled .3 )
    (
      withshadingmethod "linear"
      withshadingdirection (2.5, 0.5)
      withshadingcolors(red, blue)
    )
    scaled 4;
\endmpfig
\par\leavevmode
\mppattern{mypatt}
  \mpfig
    picture q;
    q := btex Q etex;
    fill bbox q withcolor .8[red,white];
    draw q withcolor .8red;
  \endmpfig
\endmppattern
\mpfig
  fill fullcircle scaled 100 withpostscript "collect";
  fill unitsquare shifted - center unitsquare scaled 45
    withmppattern "mypatt"
    withpostscript "evenodd"
    ;
\endmpfig
\mppattern{pattuncolored}
  [
    colored = false,
    matrix = "slanted .3 rotated 30",
  ]
  \fiverm\TeX
\endmppattern
\mpfig
  picture tex;
  tex = mpliboutlinetext.p ("\bf \TeX");
  draw image(
  for i=1 upto mpliboutlinenum:
    j:=0;
    for item within mpliboutlinepic[i]:
      j:=j+1;
      filldraw pathpart item scaled 10
      if j < length mpliboutlinepic[i]:
          withpostscript "collect"
      else:
          withmppattern "pattuncolored"
          withpen pencircle scaled 1/2
          withcolor (i/4)[red,blue]
      fi;
    endfor
  endfor
  )
  withfademethod "linear"
  ;
\endmpfig
\par\leavevmode
\mpfig
fill fullcircle scaled 125 withcolor 2/3[blue,white];
fill fullcircle scaled 80  withcolor 2/3[red,white];
draw image(
  draw (left--right) rotated 45 scaled 50
    withpen pencircle scaled 20
    ;
  draw (left--right) rotated -45 scaled 50
    withpen pencircle scaled 20
    ;
  )
  asgroup ""
  withgroupname "testTRgroup"
  withtransparency (1, .3)
  ;
\endmpfig
\mpfig
fill fullcircle scaled 125 withcolor 2/3[red,white];
fill fullcircle scaled 80  withcolor 2/3[blue,white];
usemplibgroup "testTRgroup"
  withfademethod "circular"
  withfaderadius (0,60)
  ;
\endmpfig
\mplibgroup{mytex}[matrix="rotated 15"] \TeX \endmplibgroup
\hbox to0pt{\hss\vrule width.5pt height5pt depth5pt\hss}%
\hbox to0pt{\hss\vrule width10pt height.25pt depth.25pt\hss}%
\usemplibgroup{mytex}%
\mpfig usemplibgroup "mytex"; draw (left--right) scaled 5; draw (up--down) scaled 5; \endmpfig
\par
\mpfig
  draw mplibgraphictext "\bf MPLIB"
  fakebold 1 fillcolor .7[white,blue] drawcolor .7blue scaled 7
  withmppattern "pattuncolored" ;
\endmpfig
\leavevmode
\mpfig
  draw btex \bf\TeX etex rotated 30 scaled 3
    withshadingmethod "linear"
    withshadingcolors (red,blue)
    withshadingvector (0,1)
    ;
\endmpfig
\mppattern{p1}
  \mpfig
    fill fullcircle scaled 3
    withcolor .7red
    ;
  \endmpfig
\endmppattern
\mppattern{p2}
  \mpfig
    fill fullcircle scaled 12
    withmppattern "p1"
    ;
  \endmpfig
\endmppattern
\mpfig
  fill fullcircle scaled 48
  withmppattern "p2"
  ;
\endmpfig
\tracingcommands0

\vskip 2\baselineskip
\mplibcodeinherit{disable}
\everymplib[instanceOne]{beginfig(1);}
\everyendmplib[instanceOne]{endfig;}

\mplibcode[instanceOne]
  picture TeX;
  TeX := btex \TeX etex;
a := 1cm;
draw fullcircle scaled a;
draw btex a circle with $d=a$ etex shifted (a,0);
draw TeX;
\endmplibcode
Current instance name is: \currentmpinstancename \vskip 2\baselineskip

\mplibcode[instanceTwo]
beginfig(1);
if not known a:
  draw btex code is not inherited from an instance with a different name etex;
else:
  errmessage("Variable was inherited from a different instance");
fi;
endfig;
\endmplibcode
Current instance name is: \currentmpinstancename \vskip 2\baselineskip

\mplibcode
beginfig(1);
if not known a:
  draw btex code is not inherited if instance name is not listed etex;
else:
  errmessage("Variable was inherited from a different instance");
fi;
a := 1cm;
endfig;
\endmplibcode
Current instance name is: \currentmpinstancename (should be empty) \vskip 2\baselineskip

\mplibcode
beginfig(1);
if not known a:
  draw btex code is not inherited if mplibcodeinherit is disabled and instance name is not explicitly set etex;
else:
  errmessage("Variable was inherited when code inheritance is turned off and instance name is not set");
fi;
endfig;
\endmplibcode
Current instance name is: \currentmpinstancename (should be empty) \vskip 2\baselineskip

\mplibcode[instanceOne]
draw unitsquare scaled a;
draw btex a square with side $=a$, inherited from the same instance etex shifted (3/2a, 1/2a);
  draw TeX;
\endmplibcode
Current instance name is: \currentmpinstancename \vskip 2\baselineskip

\bye