\ifnum\outputmode > 0
  \DocumentMetadata{ lang=en-US, pdfversion=2.0, pdfstandard=ua-2,
  tagging=on, tagging-setup={math/setup=mathml-SE} }
%  \DocumentMetadata{ debug=BBox }
  \documentclass{article}
\else
  \documentclass[dvipdfmx]{article}
  \pagewidth=\paperwidth
  \pageheight=\paperheight
  \special{papersize=\the\paperwidth,\the\paperheight}
\fi
\usepackage{fontspec}
\setmainfont{latin modern roman}
\newfontfamily\hangulfont{NotoSansCJKKR}[Script=Hangul,Language=Korean]
\usepackage{luamplib}
\usepackage{geometry,lua-unicode-math,graphicx,xcolor,hyperref}
\everymplib{ beginfig(0); }
\everyendmplib{ endfig; }
\mpliblegacybehavior{true}%
\begin{document}
\title{testing LuaMPlib}
\parindent0pt
\mplibalttext{testing}
\tracingcommands1
A%
\begin{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; %% actual <tab> to make sure it works
\end{mplibcode}%
B%

\everymplib{}\everyendmplib{}% reset toks
A%
\begin{mplibcode}
verbatimtex \lower.2em etex
beginfig(0);
draw origin--(1cm,0) withcolor \mpcolor{teal};
draw btex g etex withcolor \mpcolor{orange};
draw btex\tracingcommands0
  multi%
  \begin{huge}line\end{huge}
  texcode
  with \%
  etex shifted (10,0);
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;
\end{mplibcode}%
B%

A%
\begin{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{red!70!blue};
endfor;
endfig;
\end{mplibcode}%
B%

\mplibforcehmode
\mplibcodeinherit{enable}%
\mplibglobaltextext{enable}%
\everymplib{ beginfig(0);}\everyendmplib{ endfig;}%
\begin{mplibcode}[text]
 label(btex $\sqrt 2$ etex, origin);
 draw fullcircle scaled 20;
 picture pic; pic := currentpicture;
\end{mplibcode}%
\begin{mplibcode}
 currentpicture := pic scaled 2;
\end{mplibcode}%
\mplibnoforcehmode
\everymplib{}\everyendmplib{}%
\mplibcodeinherit{disable}%
\mplibglobaltextext{disable}%

\mplibsetformat{metafun}%
\begin{mplibcode}[alt=alt inside mp code]
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 \mplibalttext{MetaPost}\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 \mplibalttext{MetaPost again}\kern10pt etex
beginfig(2);
circular_shade(bbox p,0,blue,.7white);
draw p withcolor white;
endfig;
\end{mplibcode}%

\begin{mplibgroup}{mympbox}%
\begin{mplibcode}[off]
%verbatimtex \global\setbox\mympbox etex
beginfig(0);
breadth=.667\mpdim\linewidth;
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;
\end{mplibcode}%
\end{mplibgroup}%
\usemplibgroup[artifact]{mympbox}%
\usemplibgroup[artifact]{mympbox}%
\usemplibgroup[artifact]{mympbox}%
\usemplibgroup[artifact]{mympbox}%

\mplibnoforcehmode
\mplibnumbersystem{double}%
\begin{mplibcode}[off]
beginfig(0);
u := 10**5*(10**-4);
draw unitsquare scaled u;
endfig;
\end{mplibcode}%
\mplibsetformat{plain}%
\begin{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;
\end{mplibcode}%
\mplibtextextlabel{enable}%
\begin{mplibcode}[text]
beginfig(0);
dotlabel.rt("$\sqrt 2$",origin);
endfig;
\end{mplibcode}%
\leavevmode
\begin{mplibcode}[artifact]
   D := sqrt(2)**7;
   beginfig(0);
   draw fullcircle scaled D;
   VerbatimTeX("\gdef\Dia{" & decimal D & "}");
   endfig;
\end{mplibcode}%
diameter:\Dia bp.%
\begin{mplibcode}[off]
  vardef rotatedlabel@#(expr str, loc, angl) =
    draw thelabel@#(str, loc) rotatedaround(loc, angl)
  enddef;

  beginfig(1);
    rotatedlabel.top(textext "Rotated!", origin, 40);
  endfig;
\end{mplibcode}%
\tracingcommands0

\begin{mplibcode}[text]
beginfig(1)
  draw mplibgraphictext "\bfseries Funny$\sqrt 2$"
  fakebold 2 % fontspec option
  drawcolor blue fillcolor "red!50" % l3color expression
  scaled 3 rotated 30 ;
draw mplibgraphictext "\bfseries\itshape xyz" scaled 3 shifted (40,0);
endfig;
\end{mplibcode}%

%\mplibsetformat{metafun}%
\begin{mplibcode}[alt=testing shading]
beginfig(1)
fill unitsquare xscaled \mpdim\textwidth yscaled 1cm
    withshadingmethod "linear"
    withshadingvector (0,1)
    withshadingstep (
       withshadingfraction .5
       withshadingcolors (red,"blue!50")
    )
    withshadingstep (
       withshadingfraction 1
       withshadingcolors ("blue!50",green)
    )
    ;
endfig;
\end{mplibcode}%
\leavevmode
\mpfig[alt=Que]
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)
  withshadingstroke "yes"
  withpen pencircle scaled 2 withcolor 2/3red
  ;
\endmpfig
\everymplib[@mpfig]{ drawoptions(withcolor mplibrgbtexcolor "olive"); }%
\mpfig* input boxes \endmpfig
\mpfig circleit.a(btex Box 1 etex); drawboxed(a); \endmpfig
\def\mpfiginstancename{mympfig}%
\mpfig[tag=Formula,alt=$1/(1-x^2)$]
draw mpliboutlinetext.b ("$\displaystyle\frac{1}{1-x^2}$")
    (withcolor .6[red,white])
    (withpen pencircle scaled .2 withcolor red)
    scaled 4 ;
\endmpfig

\mpfig[alt=Question]
  draw mpliboutlinetext.r
    ("Question")
    ( withpen pencircle scaled .3 )
    (
      withshadingmethod "linear"
      withshadingdirection (2.5, 0.5)
      withshadingcolors(red, blue)
    )
    scaled 4;
\endmpfig

\leavevmode
\mppattern{mypatt}
[
  matrix = "rotated -45 scaled 2/3",
]
\mpfig[off]
  draw (origin -- 4 right) shifted 2 up withcolor 2/3 red;
  setbounds currentpicture to unitsquare scaled 4;
\endmpfig
\endmppattern
\mpfig
draw unitsquare shifted -center unitsquare scaled 45 withpostscript "collect" ;
filldraw fullcircle scaled 100 withmppattern "mypatt" withpen pencircle scaled 1
  withcolor \mpcolor{red!50!blue!50} withpostscript "evenodd" ;
\endmpfig
\begin{mppattern}{pattuncolored}
  [
    colored = false,
    matrix = "slanted .3 rotated 30",
  ]
  \SuspendTagging{}%
  \tiny\TeX
\end{mppattern}\relax
\mpfig[actualtext=TeX]
  picture tex;
  tex = mpliboutlinetext.p ("\bfseries \TeX");
  for i=1 upto mpliboutlinenum:
    draw image(
    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
    )
    withfademethod "linear"
    ;
  endfor
\endmpfig

\leavevmode
\mpfig[alt=test fading]
  picture mill; mill = btex \includegraphics[width=100bp]{mill} etex;
  draw mill;
  mill := mill shifted 125right;
  draw mill
    withfademethod "circular"
    withfadecenter (center mill, center mill)
    withfaderadius (30, abs(lrcorner mill - llcorner mill)/2)
    withfadeopacity (1, 0)
    ;
\endmpfig

\def\test#1{%
  \mpfig
  fill unitsquare shifted -center unitsquare scaled 200
    withshadingmethod "linear"
    withshadingcolors (.3[red,white], .3[blue,white])
    ;
  label( btex MetaPost etex scaled 3, origin) ;
  draw image(
    fill fullcircle scaled 100 shifted 25left
      withcolor .8white
      withtransparency (2,1)
      ;
    fill fullcircle scaled 100 shifted 25right
      withcolor .8white
      withtransparency (2,1)
      ;
  )
  asgroup "#1"
  withgroupname "test:#1"
  withtransparency (1,.5)
  ;
  currentpicture := currentpicture scaled 2/3;
  label.ulft(TEX"#1", lrcorner currentpicture) withcolor 3/4white;
  \endmpfig
}%
\hbox{\test{isolated,knockout}\,\test{isolated}
  \mpfig usemplibgroup "test:isolated" scaled 2/3 rotated 15 ; \endmpfig
}
\hbox{\test{knockout}\,\test{}
  \mpfig usemplibgroup "test:knockout" scaled 2/3 rotated 15 ; \endmpfig
}%

\bigskip
\mpfig[text]
  draw mplibgraphictext "\textbf{MPLIB}"
    fakebold 1 fillcolor "red!70" drawcolor .7red scaled 7
    withmppattern "mypatt" ;
\endmpfig

\leavevmode
\begin{mplibgroup}{mytex}[matrix="rotated 15"] \SuspendTagging{}\TeX \end{mplibgroup}%
\csname tag_mc_artifact_group_begin:n\endcsname {}%
\hbox to0pt{\hss\vrule width.5pt height5pt depth5pt\hss}%
\hbox to0pt{\hss\vrule width10pt height.25pt depth.25pt\hss}%
\csname tag_mc_artifact_group_end:\endcsname
\usemplibgroup[actualtext=TeX]{mytex}%
\mpfig[actualtext=TeX] usemplibgroup "mytex"; draw (left--right) scaled 5; draw (up--down) scaled 5; \endmpfig
\mpfig[text]
  draw mplibgraphictext "\bfseries\TeX" rotated 15 scaled 4
    withshadingmethod "linear"
    withshadingvector (3,0)
    withshadingstep (
      withshadingfraction 1/2
      withshadingcolors (red,green)
    )
    withshadingstep (
      withshadingfraction 1
      withshadingcolors (green,blue)
    )
    ;
\endmpfig

\leavevmode
\mpfig[off]
  string Test; Test="abçdéf";
  for k=0 upto mpliblength(Test)-1:
    draw TEX(mplibsubstring (k,k+1) of Test) scaled 2 shifted (20k,0);
  endfor
\endmpfig
\qquad
\mpliblegacybehavior{false}%
\begingroup\ifdefined\tagpdfsetup\tagpdfsetup{text/lang=ko-KR}\fi
\mpfig[actualtext=나랏말씀이]
  verbatimtex \hangulfont etex;
  string Test; Test="나랏말ᄊᆞ미";
  for k=0 upto mplibuclength(Test)-1:
    draw TEX(mplibucsubstring (k,k+1) of Test) scaled 1.5 shifted (20k,0);
  endfor
\endmpfig
\endgroup

\mplibforcehmode
\mppattern{p1}
  \mpfig[off]
    fill fullcircle scaled 3
    withcolor .7red
    ;
  \endmpfig
\endmppattern
\mppattern{p2}
  \mpfig[off]
    fill fullcircle scaled 9
    withmppattern "p1"
    ;
  \endmpfig
\endmppattern
\mppattern{p3}
  \mpfig[off]
    fill fullcircle scaled 27
    withmppattern "p2"
    ;
  \endmpfig
\endmppattern
\mpfig[alt=testing nested pattern]
  fill fullcircle scaled 108
  withmppattern "p3"
  ;
\endmpfig
\mpfig
picture pic;
pic = mplibglyph "R" of "texgyrepagella-regular.otf" scaled 1/10;
mplibfillandstrokeglyph pic
  slanted 0.25
  withpen pencircle scaled 1
  withmplibcolors ("orange", 2/3red)
  ;
\endmpfig
\mpfig
  mplibdrawglyph mplibglyph "R" of "utmr8a.pfb" scaled 1/10 withcolor 2/3red;
\endmpfig

\mplibnoforcehmode
\mpliblegacybehavior{true}%

\vskip 2\baselineskip
\mplibcodeinherit{disable}%

\everymplib[instanceOne]{beginfig(1);}
\everyendmplib[instanceOne]{endfig;}

\begin{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;
\end{mplibcode}%
Current instance name is: \currentmpinstancename \vskip 2\baselineskip

\begin{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;
\end{mplibcode}%
Current instance name is: \currentmpinstancename \vskip 2\baselineskip

\begin{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;
\end{mplibcode}%
Current instance name is: \currentmpinstancename (should be empty) \vskip 2\baselineskip

\begin{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;
\end{mplibcode}%
Current instance name is: \currentmpinstancename (should be empty) \vskip 2\baselineskip

\begin{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;
\end{mplibcode}%
Current instance name is: \currentmpinstancename \vskip 2\baselineskip

\begin{center}* \qquad * \qquad *\end{center}

\section{My Section}\label{sec:my_section}

pattern after masking
\mpfig*
  picture pic; pic = btex \sffamily\bfseries\Huge Meta etex scaled 2;
\endmpfig
\mplibgroup{mymask}[asgroup="masking"]
  \mpfig[off]
    draw pic withcolor 1;
  \endmpfig
\endmplibgroup
\mpfig[alt=test general masking]
  draw image(
    for i=-7 upto 8 :
      fill unitsquare
      xscaled (xpart lrcorner pic + 3 bboxmargin)
      yscaled 5
      rotated 15
      shifted (0, 5i)
      withcolor ((i+7)/14)[blue,red] ;
    endfor
  ) withmaskinggroup "mymask"
    withmaskingbgcolor 1/10 ;
  clip currentpicture to bbox pic;
\endmpfig
\mppattern{mypatt2}[ xstep = 3, ystep = 3 ]
\mpfig[off]
fill fullcircle scaled 2 withcolor 2/3 red;
\endmpfig
\endmppattern
\mpfig
draw mplibgraphictext "\bfseries POST" fakebold 1/2 drawcolor 2/3red scaled 5
withmppattern "mypatt2";
\endmpfig

pattern inside masking
\mppattern{mypattern}[colored=false]
\SuspendTagging\relax
\tiny\TeX
\endmppattern
\mplibgroup{mygroup}[asgroup="masking"]
\mpfig[off]
path p; p = fullcircle xscaled 120 yscaled 50 ;
fill p withmppattern "mypattern" withcolor 1 ;
\endmpfig
\endmplibgroup
\mpfig
draw image(fill p
  withshadingmethod "linear" withshadingcolors (red,blue)
  withmaskinggroup "mygroup"
) asgroup "" withgroupname "mygroup2" ;
\endmpfig
\mpfig
usemplibgroup "mygroup2" rotated 15 ;
\endmpfig

masking inside pattern\marginpar{\leavevmode\llap{acrobat/foxit/firefox/chrome}}
\mplibgroup{mymask2}[asgroup="masking"]
\mpfig[off]
picture tex;
tex = TEX "\tiny\TeX";
draw tex withcolor 1;
\endmpfig
\endmplibgroup
\mppattern{mypattern}
\mpfig[off]
bboxmargin:=1/5;
fill bbox tex
  withshadingmethod"linear" withshadingcolors(2/3[white,red], 2/3[white,blue])
  withmaskinggroup "mymask2" withmaskingbgcolor 1/10;
\endmpfig
\endmppattern
\mpfig
fill p withmppattern "mypattern";
\endmpfig
\mplibgroup{mygroup3}
\mpfig[off]
fill p withmppattern "mypattern" ;
\endmpfig
\endmplibgroup
\mpfig
usemplibgroup "mygroup3" rotated 15 ;
\endmpfig

just shading in pattern
\mppattern{myshpatt}
\mpfig[off]
bboxmargin:=1/5;
fill bbox tex
withshadingmethod"linear" withshadingcolors(2/3[white,red], 2/3[white,blue])
withtransparency (1, 1/10)
;
draw tex
withshadingmethod"linear" withshadingcolors(2/3[white,red], 2/3[white,blue]) ;
\endmpfig
\endmppattern
\mpfig
fill p withmppattern "myshpatt";
\endmpfig
\mplibgroup{mygroup4}
\mpfig[off]
fill p withmppattern "myshpatt" ;
\endmpfig
\endmplibgroup
\mpfig
usemplibgroup "mygroup4" rotated 15 ;
\endmpfig

shading pattern and xobject\marginpar{\leavevmode\llap{acrobat}}
\mpfig*
picture pic[];
pic1 = image(
fill fullcircle scaled 44 withoutcolor;
fill fullcircle scaled 44 shifted 22right withoutcolor;
) ;
pic2 = image( draw pic1
withshadingmethod "circular"
withshadingcolors (red, blue) % shading inside group
) ;
\endmpfig
\mpfig
draw pic2
asgroup ""
withtransparency (1, 2/3) ;
\endmpfig
\mplibgroup{mygrshpic}[asgroup=""]
\mpfig[off]
draw pic2 ;
\endmpfig
\endmplibgroup
\mpfig
usemplibgroup "mygrshpic"
withtransparency (1, 2/3) ;
\endmpfig
\mpfig % shading outside group
draw pic1
asgroup ""
withshadingmethod "circular"
withshadingcolors (red, blue)
withtransparency (1, 2/3) ;
\endmpfig

\leavevmode\qquad\qquad but this is the best:\marginpar{\leavevmode\llap{acrobat/foxit/firefox/chrome/preview}}
\mplibgroup{myshading}
\mpfig[off]
  draw pic2;
\endmpfig
\endmplibgroup
\mpfig
  usemplibgroup "myshading" asgroup "" withtransparency (1, 2/3) ;
\endmpfig

\mpfig[text]
  drawdot origin withpen pencircle scaled 2;
  draw btex go to section \ref{sec:my_section} \dots etex rotated 15 scaled 2 shifted (50,10);
\endmpfig
\end{document}
