summaryrefslogtreecommitdiff
path: root/macros/luatex/generic/minim-mp/minim-mp.sty
blob: 6240c80b9a42d6c6ec2d746c7647d4728fb26adc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82

\ProvidesPackage{minim-mp}[2023/03/03 Run MetaPost code from LaTeX]

\input minim-mp

% work around latex’s \protect mechanism
\let\mnm@protect=\protect
\everymaketext{\let\protect=\mnm@protect}

% the defaultfont is the current \normalfont
\def\mnm@setnormalfont{{\normalfont
    \xdef\mnm@normalfont{\expandafter\csstring\the\font}}}

% a one-off metapost environment
\newenvironment{metapost}[1][]{%
    \begingroup \catcode`\#=12
    \let\mpcolor = \minimpcolor
    \csname:metapost:\endcsname[#1]}
    {\csname end:metapost:\endcsname \endgroup}
\NewDocumentEnvironment{:metapost:}{O{}+b}{%
    \mnm@setnormalfont
    \let\protect=\noexpand
    \directmetapost[#1]{%
        defaultfont:="\mnm@normalfont"; #2; }%
    \let\protect=\mnm@protect}{}

% separate metapost instances
\newcommand\newmetapostenvironment[2][]{%
    \newmetapostinstance[#1]\mnm@tmp
    \expandafter\let\csname #2@instance\endcsname\mnm@tmp
    \newenvironment{#2}{%
         \begingroup \catcode`\#=12 \catcode`\%=12
         \let\mpcolor = \minimpcolor
         \csname:#2:\endcsname}
        {\csname end:#2:\endcsname \endgroup}%
    \NewDocumentEnvironment{:#2:}{+b}{%
        \mnm@setnormalfont
        \let\protect=\noexpand
        \runmetapostimage
            \csname #2@instance\endcsname
            {defaultfont:="\mnm@normalfont";##1;}%
        \let\protect=\mnm@protect}{}}

% \mpcolor support
\def\minimpcolor#1#{\dominimpcolor{#1}}
\def\dominimpcolor#1#2{%
    \directlua{ tex.scantoks('mpcolorspectoks', 0, '#1{#2}')
                tex.runtoks 'mpcolorruntoks' }
    (\the\mpcolorvaltoks)}
\newtoks\mpcolorspectoks \newtoks\mpcolorruntoks \newtoks\mpcolorvaltoks
\mpcolorruntoks{\expandafter\extractcolorspec\the\mpcolorspectoks\mptmpcolor
    \expandafter\mpcolorvaltoks\expandafter\@gobble\mptmpcolor}

% as a rather fragile drop-in replacement for luamplib
\DeclareOption{luamplib}{%
    \def\mplibtextextlabel#1{} % always enabled
    \def\mplibshowlog#1{\directlua{
        require 'minim-mp'.on_line = %
            \ifcsname minimp@switch@#1\endcsname
                true \else false \fi }}
    \newmetapostenvironment[jobname='\jobname']{@mplibcode}
    \def\mplibcodeinherit#1{%
        \ifcsname minimp@switch@#1\endcsname
            \let\mplibcode=\@mplibcode
            \let\endmplibcode=\@mplibcode
        \else
            \let\mplibcode=\metapost
            \let\endmplibcode=\endmetapost
        \fi}
    \mplibcodeinherit{disable}
    \def\mplibsetformat#1{\directlua{
        require 'minim-mp'.default_format = '#1.mp' }}
    \def\mplibnumbersystem#1{\directlua{
        require 'minim-mp'.default_math = '#1' }}
} \ProcessOptions*

\let\minimp@switch@@enable=\relax
\let\minimp@switch@@enabled=\relax
\let\minimp@switch@@true=\relax
\let\minimp@switch@@yes=\relax