luatex-mplib.tex /size: 3605 b    last modification: 2020-07-01 14:35
1%D \module
2%D   [       file=luatex-mplib,
3%D        version=2009.12.01,
4%D          title=\LUATEX\ Support Macros,
5%D       subtitle=\METAPOST\ to \PDF\ conversion,
6%D         author=Taco Hoekwater \& Hans Hagen,
7%D      copyright={PRAGMA ADE \& \CONTEXT\ Development Team}]
8
9%D This is the companion to the \LUA\ module \type {supp-mpl.lua}. Further
10%D embedding is up to others. A simple example of usage in plain \TEX\ is:
11%D
12%D \starttyping
13%D \pdfoutput=1
14%D
15%D \input luatex-mplib.tex
16%D
17%D \setmplibformat{plain}
18%D
19%D \mplibcode
20%D   beginfig(1);
21%D     draw fullcircle
22%D       scaled 10cm
23%D       withcolor red
24%D       withpen pencircle xscaled 4mm yscaled 2mm rotated 30 ;
25%D   endfig;
26%D \endmplibcode
27%D
28%D \end
29%D \stoptyping
30
31\def\setmplibformat#1{\def\mplibformat{#1}}
32\def\setmplibmode  #1{\def\mplibmode  {#1}}
33
34\def\setupmplibcatcodes
35  {\catcode`\{=12 % could be optional .. not really needed
36   \catcode`\}=12 % could be optional .. not really needed
37   \catcode`\#=12
38   \catcode`\^=12
39   \catcode`\~=12
40   \catcode`\_=12
41   \catcode`\%=12
42   \catcode`\&=12
43   \catcode`\$=12
44   \obeylines}
45
46\def\mplibcode
47  {\bgroup
48   \setupmplibcatcodes
49   \domplibcode}
50
51\long\def\domplibcode#1\endmplibcode
52  {\egroup
53   \directlua{metapost.process('\mplibformat',[[#1]],'\mplibmode')}}
54
55%D We default to \type {plain} \METAPOST:
56
57\def\mplibformat{plain}
58\def\mplibmode  {scaled}
59
60%D We use a dedicated scratchbox:
61
62\ifx\mplibscratchbox\undefined \newbox\mplibscratchbox \fi
63
64%D Now load the needed \LUA\ code.
65
66\directlua{dofile(kpse.find_file('luatex-mplib.lua'))}
67% \directlua{dofile(resolvers.findfile('luatex-mplib.lua'))}
68
69%D The following code takes care of encapsulating the literals:
70
71\def\startMPLIBtoPDF#1#2#3#4%
72  {\hbox\bgroup
73   \xdef\MPllx{#1}\xdef\MPlly{#2}%
74   \xdef\MPurx{#3}\xdef\MPury{#4}%
75   \xdef\MPwidth{\the\dimexpr#3bp-#1bp\relax}%
76   \xdef\MPheight{\the\dimexpr#4bp-#2bp\relax}%
77   \parskip0pt%
78   \leftskip0pt%
79   \parindent0pt%
80   \everypar{}%
81   \setbox\mplibscratchbox\vbox\bgroup
82   \noindent}
83
84\def\stopMPLIBtoPDF
85  {\egroup
86   \setbox\mplibscratchbox\hbox
87     {\hskip-\MPllx bp%
88      \raise-\MPlly bp%
89      \box\mplibscratchbox}%
90   \setbox\mplibscratchbox\vbox to \MPheight
91     {\vfill
92      \hsize\MPwidth
93      \wd\mplibscratchbox0pt%
94      \ht\mplibscratchbox0pt%
95      \dp\mplibscratchbox0pt%
96      \box\mplibscratchbox}%
97   \wd\mplibscratchbox\MPwidth
98   \ht\mplibscratchbox\MPheight
99   \box\mplibscratchbox
100   \egroup}
101
102%D The body of picture, except for text items, is taken care of by:
103
104\ifnum\pdfoutput>0
105    \let\MPLIBtoPDF\pdfliteral
106\else
107    \def\MPLIBtoPDF#1{\special{pdf:literal direct #1}} % not ok yet
108\fi
109
110%D Text items have a special handler:
111
112\def\MPLIBtextext#1#2#3#4%
113  {\begingroup
114   \setbox\mplibscratchbox\hbox
115     {\font\temp=#1 at #2bp%
116      \temp
117      #3}%
118   \setbox\mplibscratchbox\hbox
119     {\raise#4sp%
120      \box\mplibscratchbox}%
121   \wd\mplibscratchbox0pt%
122   \ht\mplibscratchbox0pt%
123   \dp\mplibscratchbox0pt%
124   \box\mplibscratchbox
125   \endgroup}
126
127\def\MPLIBpdftext#1#2%
128  {\ifcsname mplib::#1\endcsname
129     % already done, forgotten outside convert group
130     \message{<reusing mplib: #1>}%
131   \else
132     \message{<embedding mplib: #1>}%
133     \immediate\pdfximage{#1}% we cannot remove the file as it is included last
134     \expandafter\edef\csname mplib::#1\endcsname{\the\pdflastximage}%
135   \fi
136   \setbox\mplibscratchbox\hbox
137     {\raise#2sp\hbox{\pdfrefximage\csname mplib::#1\endcsname}}%
138   \wd\mplibscratchbox0pt%
139   \ht\mplibscratchbox0pt%
140   \dp\mplibscratchbox0pt%
141   \box\mplibscratchbox}
142
143\endinput
144