| 1 |
|
| 2 |
\ifdefined\minimmploaded |
| 3 |
\wlog{(skipped)} |
| 4 |
\expandafter\endinput\fi |
| 5 |
\chardef\minimmploaded = \catcode`\: |
| 6 |
\catcode`\: = 11 |
| 7 |
|
| 8 |
\input minim-alloc |
| 9 |
\input minim-pdfresources |
| 10 |
|
| 11 |
% a default catcode table |
| 12 |
\newcatcodetable \minim:mp:catcodes:maketext |
| 13 |
{\catcode`\:=12 \catcode`\@=12 |
| 14 |
\savecatcodetable\csname minim:mp:catcodes:maketext\endcsname} |
| 15 |
% and an empty catcode table for reading metapost code |
| 16 |
\newcatcodetable \minim:mp:catcodes:mpcode |
| 17 |
{\def\\{}\catcodetable\minim:initcatcodes |
| 18 |
\catcode`\{=1 \catcode`\}=2 \catcode9=10 |
| 19 |
\catcode`\$=12 \catcode`\^=12 \catcode`\_=12 \catcode`\&=12 |
| 20 |
\catcode`\#=12 \catcode`\%=12 \catcode`\~=12 |
| 21 |
\catcode"0D=12 \savecatcodetable\csname\\minim:mp:catcodes:mpcode\endcsname} |
| 22 |
|
| 23 |
\directlua { require('minim-mp') } |
| 24 |
|
| 25 |
% The following are provided from the lua side. |
| 26 |
% |
| 27 |
% Note that only two of these are \protected – you are responsible for the |
| 28 |
% proper time and place of expansion of the others. |
| 29 |
% |
| 30 |
% \closemetapostinstance \instance |
| 31 |
% \runmetapost \instance { code } % \protected |
| 32 |
% \getnextmpimage \instance |
| 33 |
% \getnamedmpimage \instance {name} |
| 34 |
% \remainingmpimages \instance |
| 35 |
% \boxnextmpimage \instance box-nr |
| 36 |
% \boxnamedmpimage \instance box-nr {name} |
| 37 |
|
| 38 |
% \getallmpimages \instance |
| 39 |
\newcount\mp:tmpcount |
| 40 |
\protected\def\getallmpimages{% |
| 41 |
\afterassignment\getallmpimages:do\mp:tmpcount=} |
| 42 |
\def\getallmpimages:do{\loop |
| 43 |
\ifnum \remainingmpimages\mp:tmpcount>0\relax |
| 44 |
\getnextmpimage\mp:tmpcount \repeat} |
| 45 |
|
| 46 |
% \runmetapostimage \instance { code } |
| 47 |
\protected\def\runmetapostimage{\begingroup |
| 48 |
\afterassignment\runmetapostimage:do\mp:tmpcount=} |
| 49 |
\def\runmetapostimage:do{% |
| 50 |
\toksapp\everymp\mp:tmpcount{pre}{mp}{beginfig(0) }% |
| 51 |
\tokspre\everymp\mp:tmpcount{post}{mp}{ endfig;}% |
| 52 |
\toksapp\everymp\mp:tmpcount{post}{tex}{% |
| 53 |
\getallmpimages\mp:tmpcount \endgroup}% |
| 54 |
\runmetapost\mp:tmpcount} |
| 55 |
|
| 56 |
% \directmetapost [ options ] { code } |
| 57 |
\protected\def\directmetapost{% |
| 58 |
\withoptions[]\directmetapost:opt} |
| 59 |
\long\def\directmetapost:opt[#1]{% |
| 60 |
\newmetapostinstance[#1]\:mpinst: |
| 61 |
\everymp\:mpinst:{post}{tex}{% |
| 62 |
\getallmpimages\:mpinst: |
| 63 |
\closemetapostinstance\:mpinst:}% |
| 64 |
\runmetapost\:mpinst:} |
| 65 |
|
| 66 |
% \newmetapostinstance [ options ] \instance |
| 67 |
\protected\def\newmetapostinstance{\withoptions[]\newmetapostinstance:} |
| 68 |
\def\newmetapostinstance:[#1]#2{% |
| 69 |
\chardef#2=\directlua{ |
| 70 |
tex.sprint(require('minim-mp').open{#1}) |
| 71 |
}\relax} |
| 72 |
|
| 73 |
% tex side of metapost instance initialisation |
| 74 |
\newcount \mp:cur:inst |
| 75 |
\newtoks \mp:init:env \mp:init:env{% |
| 76 |
% four token lists for every instance |
| 77 |
\expandafter\newtoks\csname mp:inst:pre:tex:\the\mp:cur:inst\endcsname |
| 78 |
\expandafter\newtoks\csname mp:inst:pre:mp:\the\mp:cur:inst\endcsname |
| 79 |
\expandafter\newtoks\csname mp:inst:post:mp:\the\mp:cur:inst\endcsname |
| 80 |
\expandafter\newtoks\csname mp:inst:post:tex:\the\mp:cur:inst\endcsname} |
| 81 |
% the zeroth quarted applies to all |
| 82 |
\mp:cur:inst=0 \the\mp:init:env \mp:cur:inst=-1 |
| 83 |
|
| 84 |
% \everymp \id {pre|post}{mp|tex} % accesses the above token lists |
| 85 |
\def\everymp#1#2#3{\csname mp:inst:#2:#3:\the |
| 86 |
\ifcsname \string#1@instance\endcsname % for the latex environments |
| 87 |
\lastnamedcs\else\numexpr#1\fi |
| 88 |
\endcsname} |
| 89 |
|
| 90 |
% combining the relevant toks registers |
| 91 |
\newtoks\mp:inst:pre:tex \mp:inst:pre:tex{% |
| 92 |
\the\everymp0{pre}{tex}% |
| 93 |
\the\everymp\mp:cur:inst{pre}{tex}} |
| 94 |
\newtoks\mp:inst:post:tex \mp:inst:post:tex{% |
| 95 |
\the\everymp\mp:cur:inst{post}{tex}% |
| 96 |
\the\everymp0{post}{tex}} |
| 97 |
\newtoks\mp:mpcode % see lua function scan_mp_chunk() |
| 98 |
\newtoks\mp:makempcode \mp:makempcode{% |
| 99 |
\edef\minim:mp:tmp{% |
| 100 |
\the\everymp0{pre}{mp}% |
| 101 |
\the\everymp\mp:cur:inst{pre}{mp}% |
| 102 |
\unexpanded{\the\mp:mpcode}% |
| 103 |
\the\everymp\mp:cur:inst{post}{mp}% |
| 104 |
\the\everymp0{post}{mp}}% |
| 105 |
\edef\minim:mp:tmp{\minim:mp:tmp}% |
| 106 |
\mp:mpcode=\expandafter{\minim:mp:tmp}} |
| 107 |
|
| 108 |
% colour conversion: 1 grayscale 2 cmyk 3 rgb 4 rgb as spot |
| 109 |
\newcount \convertmpcolours |
| 110 |
\convertmpcolours = 0 |
| 111 |
|
| 112 |
\newcount\RtoG \RtoG = 2126 |
| 113 |
\newcount\GtoG \GtoG = 7152 |
| 114 |
\newcount\BtoG \BtoG = 0722 |
| 115 |
|
| 116 |
% miscellaneous bits and ends |
| 117 |
\newtoks \everymaketext |
| 118 |
\newtoks \everyverbatimtex |
| 119 |
\def\mpdim#1{\the\dimexpr#1\relax} |
| 120 |
|
| 121 |
\catcode`\: = \minimmploaded |
| 122 |
|