Debian Codegraph @

texmf-dist/metapost/minim-mp/minim-mp.mp

raw
1
2 delimiters ();
3
4 % This file is specific to the minim metapost processor (minim-mp) and cannot
5 % be used with luamplib or other metapost implementations. See minim.mp for
6 % general-purpose extensions.
7
8 message "Setting up the minim MetaPost extensions";
9
10 % 1 lua scripts and functions
11
12 def luafunction = gobble inner_luafunction enddef;
13 vardef inner_luafunction @# (text args) = endgroup
14 % we remove the grouping so that the function may return any fragment of
15 % metapost code, in particular lists to-be-parsed as text parameters
16 runscript ("return " & (str @#) & build_lua_arguments(args))
17 gobble begingroup enddef;
18
19 vardef build_lua_arguments (text args) =
20 save_boolean first; first = true;
21 "(" for a = args:
22 & if first: hide(first:=false) "" else: "," fi
23 & (quote_for_lua a)
24 endfor & ")"
25 enddef;
26
27 vardef luavariable @# =
28 runscript ("return quote("&(str @#)&")") enddef;
29 vardef setluavariable @# expr val=
30 runscript ((str @#)&" = "&quote_for_lua(val)) enddef;
31
32 % quoting expressions for lua
33
34 vardef lua_string(expr s) =
35 runscript ("[[quote_for_lua]]"&s) enddef;
36
37 vardef hexadecimal tertiary n =
38 % TODO: support other number systems
39 save d, a, m; a = ASCII("a"); m := abs n;
40 save_string res; res =
41 if n < 0: "-0x" else: "0x" fi
42 for i = 1 upto 7: &
43 hide(d := floor m div 256; m := 16(m - 256d);)
44 if d < 10: decimal d else: char(a+d-10) fi
45 if i = 3: & "." fi
46 endfor; res enddef;
47
48 vardef scaledpoints tertiary n =
49 % use the fact that 800bp = 803pt
50 save a, m; m := abs n;
51 a1 = m div (1025/1024); % 1025/1024 = 800epsilon * 82
52 m := m mod (1025/1024);
53 a2 = m div 800epsilon; % max 82
54 m := (m mod 800epsilon) div epsilon;
55 a3 = floor(m*803/800 + 1/2);
56 save_string res; res =
57 if n < 0: "-(" else: "(" fi
58 & decimal(a1) & "*803*82 + "
59 & decimal(a2) & "*803 + "
60 & decimal(a3) & ")";
61 res enddef;
62
63 vardef quote_for_lua tertiary a =
64 save_string res; res =
65 if string a:
66 lua_string(a)
67 elseif numeric a:
68 hexadecimal(a)
69 elseif pair a:
70 "{"& (hexadecimal xpart a) &
71 ","& (hexadecimal ypart a) & "}"
72 elseif boolean a:
73 if a: "true" else: "false" fi
74 elseif color a:
75 "{"& (hexadecimal redpart a) &
76 ","& (hexadecimal greenpart a) &
77 ","& (hexadecimal bluepart a) & "}"
78 elseif cmykcolor a:
79 "{"& (hexadecimal cyanpart a) &
80 ","& (hexadecimal magentapart a) &
81 ","& (hexadecimal yellowpart a) &
82 ","& (hexadecimal blackpart a) & "}"
83 elseif transform a:
84 "{"& (hexadecimal xxpart a) &
85 ","& (hexadecimal xypart a) &
86 ","& (hexadecimal yxpart a) &
87 ","& (hexadecimal yypart a) &
88 ","& (hexadecimal xpart a) &
89 ","& (hexadecimal ypart a) & "}"
90 elseif pen a:
91 hide(errmessage("I cannot pass a pen value to lua");)
92 "nil"
93 elseif picture a:
94 hide(errmessage("I cannot pass a picture value to lua");)
95 "nil"
96 else: % probably intentionally vacuous, which is fine
97 "nil"
98 fi; res enddef;
99
100 % constructing lua tables
101
102 def make_lua_dict (text t) = begingroup
103 % Note that though the argument may contain = signs and commas, the rest of
104 % the arguments must consist of suffixes.
105 save_boolean first; first := true;
106 "{ " forsuffixes kv =
107 hide(let = _EQ_ undefined)
108 t hide(let = _EQ_ _EQ_) : &
109 if first: hide(first := false;) else: ", "& fi
110 begingroup make_lua_keyval kv endgroup
111 endfor & " }" endgroup
112 enddef;
113 let _EQ_ = =;
114
115 vardef make_lua_keyval @# expr e =
116 (str @#) & " = " & quote_for_lua e
117 enddef;
118
119 % 1 tex registers
120
121 string _SUFFIX_HACK_[];
122 vardef index_or_suffix (suffix s) =
123 save_string res; res =
124 if string _SUFFIX_HACK_ s: % s is a number
125 "["&decimal(s)&"]"
126 else: % s is a suffix
127 "."&(str s)
128 fi ; res enddef;
129
130 vardef tex.count @# =
131 runscript ("return tex.count" & index_or_suffix(@#)) enddef;
132 vardef tex.attribute @# =
133 runscript ("return tex.attribute" & index_or_suffix(@#)) enddef;
134 vardef tex.dimen @# =
135 runscript ("return sp_to_pt(tex.dimen" & index_or_suffix(@#) & ")") enddef;
136 vardef tex.skip @# =
137 runscript ("return sp_to_pt(tex.skip" & index_or_suffix(@#) & ".width)") enddef;
138 vardef tex.toks @# =
139 runscript ("return quote(tex.toks" & index_or_suffix(@#) & ")") enddef;
140
141 vardef set tex.count @# expr val =
142 runscript ("tex.count" & index_or_suffix(@#) & " = " & decimal(val)) enddef;
143 vardef set tex.attribute @# expr val =
144 runscript ("tex.attribute" & index_or_suffix(@#) & " = " & decimal(val)) enddef;
145 vardef set tex.dimen @# expr val =
146 runscript ("tex.dimen" & index_or_suffix(@#) & " = " & scaledpoints(val)) enddef;
147 vardef set tex.toks @# expr val =
148 runscript ("tex.toks" & index_or_suffix(@#) & " = " & lua_string(val)) enddef;
149
150 % 1 typesetting
151
152 boolean debug_tex_bboxes;
153 debug_tex_bboxes := false;
154
155 let textext = maketext;
156 let TEX = maketext;
157
158 def _set_maketext_result_ (expr nr)(text tr) = image (
159 fill unitsquare withprescript "TEXBOX:"&decimal nr;
160 save_path bb; bb := unitsquare transformed tr;
161 if debug_tex_bboxes: draw bb dashed evenly; fi
162 setbounds currentpicture to bb;) enddef;
163
164 % baseline pair or numeric -> fill statement
165 vardef baseline expr o =
166 fill if numeric o : (0,o) else: o fi
167 -- cycle withprescript "BASELINE:"; enddef;
168
169 vardef find_baseline expr p =
170 save_pair bl, ibl;
171 for c within p:
172 if clipped c or bounded c:
173 ibl := find_baseline c;
174 if known ibl: bl := ibl; fi
175 elseif "BASELINE:" = substring (0,9) of prescriptpart c:
176 bl := point 0 of pathpart c;
177 fi
178 endfor bl enddef;
179
180 % actual typesetting
181
182 primarydef t infont f = image( luafunction minim_infont (t, f); ) enddef;
183
184 def glyph expr c of f = luafunction get_glyph (c, f) enddef;
185
186 def contours expr t of f = luafunction get_contours (t, f) enddef;
187
188 % labels can optionally be typeset with maketext
189 % and the default font must be set explicitly
190 string defaultfont;
191 boolean maketextlabels;
192 maketextlabels := true;
193 vardef thelabel@#(expr s,z) = % Position s near z
194 save_picture p;
195 p = if picture s : s
196 elseif maketextlabels : maketext s
197 elseif known defaultfont : s infont defaultfont
198 else : s infont luafunction font.current()
199 fi;
200 p shifted (z + labeloffset*laboff@# -
201 (labxf@#*lrcorner p + labyf@#*ulcorner p
202 + (1-labxf@#-labyf@#)*llcorner p
203 ) ) enddef;
204
205 % 1 box resources (xforms)
206
207 % boxresource number -> fill statement + setbounds
208 vardef boxresource primary nr = image(
209 fill unitsquare withprescript "BOXRESOURCE:" & decimal nr ;
210 save btf; transform btf;
211 btf = luafunction get_boxresource_dimensions (nr);
212 save_path bb; bb = unitsquare transformed btf;
213 if debug_tex_bboxes: draw bb dashed evenly; fi
214 setbounds currentpicture to bb;
215 currentpicture := currentpicture
216 shifted luafunction get_boxresource_center (nr);
217 ) enddef;
218
219 % id = saveboxresource (attributes) image ( ... );
220 vardef saveboxresource (text attrs) expr p =
221 save id; id = luafunction reserve_xform_id();
222 % save the dimensions
223 save t, o; transform t; pair o;
224 (0,0) transformed t = llcorner p;
225 (1,0) transformed t = lrcorner p;
226 (0,1) transformed t = ulcorner p;
227 save bl; bl = -ypart find_baseline p;
228 o = (-xpart llcorner p, if known bl: bl else: 0 fi);
229 luafunction set_boxresource_dimensions (id, t shifted o, -o);
230 % shipout the xform
231 shipout image(draw p;
232 special "XFORMATTRS:" & make_lua_dict (attrs);
233 special "XFORMINDEX:" & decimal id;
234 ); id enddef;
235
236 % 1 extended graphics state
237
238 vardef modify_scripts text t =
239 % Note: this is a no-op when the picture is empty
240 save_picture curr;
241 save_string pre, post;
242 % extract script parts
243 curr := image(drawdot origin t;);
244 pre = prescriptpart curr;
245 post = postscriptpart curr;
246 % add the prescript
247 if pre <> "":
248 curr := currentpicture; clearit;
249 draw curr withprescript pre;
250 fi
251 % add the postscript
252 if post <> "":
253 % here we find the reason for this routine: metapost *should* apply the
254 % postscript to the last element of a picture, but chooses the first
255 % instead.
256 curr := currentpicture; clearit;
257 setbounds curr to llcorner curr -- lrcorner curr --
258 urcorner curr -- ulcorner curr -- cycle;
259 % these bounds force an extra nesting level of the next function
260 draw with_postscript_to_last(curr, post);
261 fi
262 enddef;
263
264 vardef with_postscript_to_last (expr p, script) =
265 image( save i; i := 0;
266 for c within p:
267 if incr i < length(p): draw c;
268 elseif clipped c:
269 save_picture q; q:= with_postscript_to_last(c, script);
270 clip q to pathpart c;
271 addto currentpicture also q;
272 elseif bounded c:
273 save_picture q; q:= with_postscript_to_last(c, script);
274 setbounds q to pathpart c;
275 addto currentpicture also q;
276 else:
277 draw c withpostscript script;
278 fi
279 endfor) enddef;
280
281 def append_postscript expr s =
282 if length(currentpicture) = 0: special s;
283 else: modify_scripts withpostscript s;
284 fi enddef;
285
286 def savegstate =
287 append_postscript "gstate:save"; enddef;
288 def restoregstate =
289 append_postscript "gstate:restore"; enddef;
290 def setgstate =
291 append_postscript "extgstate:" & make_lua_dict enddef;
292
293 def withgstate (text t) =
294 withprescript "extgstate:" & make_lua_dict (t)
295 withprescript "gstate:save"
296 withpostscript "gstate:restore" enddef;
297
298 % 1 transparency
299
300 newinternal string blend_mode;
301 newinternal string transparency_group_attrs;
302
303 % setalpha (α)
304 vardef setalpha expr alpha =
305 save a; a = alpha;
306 save_string bm; bm = "/" & blend_mode;
307 setgstate(CA=a, ca=a if bm <> "/":, BM=bm fi) enddef;
308
309 % <drawing_cmd> withalpha (α)
310 def withalpha = gobble _withalpha_ enddef;
311 string _blend_mode_;
312 vardef _withalpha_ (expr alpha) =
313 _with_alpha_ := alpha;
314 _blend_mode_ := "/" & blend_mode;
315 endgroup
316 withgstate(CA=_with_alpha_,ca=_with_alpha_ if _blend_mode_ <> "/":, BM=_blend_mode_ fi)
317 gobble begingroup enddef;
318
319 % transparent (α) image (...)
320 vardef transparent (expr alpha) expr p =
321 save_string attrs; attrs = "<< /S/Transparency "&transparency_group_attrs&">>";
322 save g; g = saveboxresource (Group=attrs) p;
323 savegstate; setalpha (alpha);
324 draw boxresource g;
325 restoregstate;
326 enddef;
327
328 % 1 even-odd rule and multidraw
329
330 def nofill expr c = fill c withprescript "OTYPE:nofill" enddef;
331 def eofill expr c = fill c withprescript "OTYPE:eofill" enddef;
332 def eofilldraw expr c = filldraw c withprescript "OTYPE:eofilldraw" enddef;
333
334 def multidraw (text paths) text opts = draw image(
335 for p = paths: ; nofill p opts endfor
336 withprescript "OTYPE:outline";) enddef;
337 def multifill (text paths) text opts = draw image(
338 for p = paths: ; nofill p opts endfor
339 withprescript "OTYPE:fill";) enddef;
340 def multifilldraw (text paths) text opts = draw image(
341 for p = paths: ; nofill p opts endfor
342 withprescript "OTYPE:filldraw";) enddef;
343 def multieofill (text paths) text opts = draw image(
344 for p = paths: ; nofill p opts endfor
345 withprescript "OTYPE:eofill";) enddef;
346 def multieofilldraw (text paths) text opts = draw image(
347 for p = paths: ; nofill p opts endfor
348 withprescript "OTYPE:eofilldraw";) enddef;
349
350 % 1 patterns
351
352 def withpattern(suffix s) = withprescript
353 ("fillpattern: " & decimal(_patterns_.s)) enddef;
354 def beginpattern(suffix s) =
355 begingroup clearxy; clearit;
356 interim defaultcolormodel:=1;
357 charcode:=incr _patterns_._last_;
358 _patterns_.s:=charcode; drawoptions();
359 pickup pencircle scaled 0.4pt;
360 save painttype; painttype:=2;
361 save _withcolor; let _withcolor = withcolor; save withcolor;
362 def withcolor = hide(painttype:=1) _withcolor enddef;
363 save matrix; transform matrix; enddef;
364 def endpattern (expr xstep, ystep) =
365 modify_scripts
366 withprescript "pdf:/Artifact BMC"
367 withpostscript "pdf:EMC";
368 if unknown matrix : matrix:=identity; fi
369 special "definepattern:" for e = charcode, tilingtype, painttype,
370 xstep, ystep, xxpart matrix, xypart matrix, yxpart matrix, yypart matrix:
371 & " " & decimal(e) endfor;
372 shipit; endgroup enddef;
373 newinternal tilingtype; tilingtype:=1;
374 _patterns_._last_ := 0;
375
376 % 1 miscellaneous
377
378 def debug_pdf = luafunction enable_debugging(); enddef;
379
380 vardef texmessage text msg = luafunction texmessage (msg) enddef;
381
382 %