blob: ddc925ab3a6e5ac84777d8c3d4022f6182e30499 (
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
|
\ifdefined\minimmploaded
\wlog{(skipped)}
\expandafter\endinput\fi
\chardef\minimmploaded = \catcode`\:
\catcode`\: = 11
\input minim-alloc
\input minim-pdfresources
% a default catcode table
\newcatcodetable \minim:mp:catcodes:maketext
{\catcode`\:=12 \catcode`\@=12
\savecatcodetable\csname minim:mp:catcodes:maketext\endcsname}
% and an empty catcode table for reading metapost code
\newcatcodetable \minim:mp:catcodes:mpcode
{\def\\{}\catcodetable\minim:initcatcodes
\catcode`\{=1 \catcode`\}=2 \catcode9=10
\catcode`\$=12 \catcode`\^=12 \catcode`\_=12 \catcode`\&=12
\catcode`\#=12 \catcode`\%=12 \catcode`\~=12
\catcode"0D=12 \savecatcodetable\csname\\minim:mp:catcodes:mpcode\endcsname}
\directlua { require('minim-mp') }
% The following are provided from the lua side.
%
% Note that only two of these are \protected – you are responsible for the
% proper time and place of expansion of the others.
%
% \closemetapostinstance \instance
% \runmetapost \instance { code } % \protected
% \getnextmpimage \instance
% \getnamedmpimage \instance {name}
% \remainingmpimages \instance
% \boxnextmpimage \instance box-nr
% \boxnamedmpimage \instance box-nr {name}
% \getallmpimages \instance
\newcount\mp:tmpcount
\protected\def\getallmpimages{%
\afterassignment\getallmpimages:do\mp:tmpcount=}
\def\getallmpimages:do{\loop
\ifnum \remainingmpimages\mp:tmpcount>0\relax
\getnextmpimage\mp:tmpcount \repeat}
% \runmetapostimage \instance { code }
\protected\def\runmetapostimage{\begingroup
\afterassignment\runmetapostimage:do\mp:tmpcount=}
\def\runmetapostimage:do{%
\toksapp\everymp\mp:tmpcount{pre}{mp}{beginfig(0) }%
\tokspre\everymp\mp:tmpcount{post}{mp}{ endfig;}%
\toksapp\everymp\mp:tmpcount{post}{tex}{%
\getallmpimages\mp:tmpcount \endgroup}%
\runmetapost\mp:tmpcount}
% \directmetapost [ options ] { code }
\protected\def\directmetapost{%
\withoptions[]\directmetapost:opt}
\long\def\directmetapost:opt[#1]{%
\newmetapostinstance[#1]\:mpinst:
\everymp\:mpinst:{post}{tex}{%
\getallmpimages\:mpinst:
\closemetapostinstance\:mpinst:}%
\runmetapost\:mpinst:}
% \newmetapostinstance [ options ] \instance
\protected\def\newmetapostinstance{\withoptions[]\newmetapostinstance:}
\def\newmetapostinstance:[#1]#2{%
\chardef#2=\directlua{
tex.sprint(require('minim-mp').open{#1})
}\relax}
% tex side of metapost instance initialisation
\newcount \mp:cur:inst
\newtoks \mp:init:env \mp:init:env{%
% four token lists for every instance
\expandafter\newtoks\csname mp:inst:pre:tex:\the\mp:cur:inst\endcsname
\expandafter\newtoks\csname mp:inst:pre:mp:\the\mp:cur:inst\endcsname
\expandafter\newtoks\csname mp:inst:post:mp:\the\mp:cur:inst\endcsname
\expandafter\newtoks\csname mp:inst:post:tex:\the\mp:cur:inst\endcsname}
% the zeroth quarted applies to all
\mp:cur:inst=0 \the\mp:init:env \mp:cur:inst=-1
% \everymp \id {pre|post}{mp|tex} % accesses the above token lists
\def\everymp#1#2#3{\csname mp:inst:#2:#3:\the
\ifcsname \string#1@instance\endcsname % for the latex environments
\lastnamedcs\else\numexpr#1\fi
\endcsname}
% combining the relevant toks registers
\newtoks\mp:inst:pre:tex \mp:inst:pre:tex{%
\the\everymp0{pre}{tex}%
\the\everymp\mp:cur:inst{pre}{tex}}
\newtoks\mp:inst:post:tex \mp:inst:post:tex{%
\the\everymp\mp:cur:inst{post}{tex}%
\the\everymp0{post}{tex}}
\newtoks\mp:mpcode % see lua function scan_mp_chunk()
\newtoks\mp:makempcode \mp:makempcode{%
\edef\minim:mp:tmp{%
\the\everymp0{pre}{mp}%
\the\everymp\mp:cur:inst{pre}{mp}%
\unexpanded{\the\mp:mpcode}%
\the\everymp\mp:cur:inst{post}{mp}%
\the\everymp0{post}{mp}}%
\edef\minim:mp:tmp{\minim:mp:tmp}%
\mp:mpcode=\expandafter{\minim:mp:tmp}}
% colour conversion: 1 grayscale 2 cmyk 3 rgb 4 rgb as spot
\newcount \convertmpcolours
\convertmpcolours = 0
\newcount\RtoG \RtoG = 2126
\newcount\GtoG \GtoG = 7152
\newcount\BtoG \BtoG = 0722
% miscellaneous bits and ends
\newtoks \everymaketext
\def\mpdim#1{\the\dimexpr#1\relax}
\catcode`\: = \minimmploaded
|