Debian Codegraph @

texmf-dist/doc/luametatex/base/luametatex-metapost.tex

raw
1 % language=us runpath=texruns:manuals/luametatex
2
3 \environment luametatex-style
4
5 \startdocument[title=Metapost]
6
7 \startsection[title={Introduction}]
8
9 Four letters in the name \LUAMETATEX\ refer to the graphical subsystem \METAPOST,
10 originally writen by John Hobby as follow up on \METAFONT. This library was
11 introduced in \LUATEX\ in order to generate graphics runtime instead of via a
12 separate system call. The library in \LUATEX\ is also used for the stand-alone
13 program so it has a \POSTSCRIPT\ backend as well as font related frontend. The
14 version used in \LUAMETATEX\ has neither. The lack of a backend can be explained
15 from the fact that we have to provide one anyway: the \PDF\ output is generated
16 by \LUA, which at that time was derived from the converter that I wrote for
17 \PDFTEX, although there the starting point is the \POSTSCRIPT\ output. Removing
18 the font related code also makes sense, because in \MKIV\ we never used it: we
19 need to support \OPENTYPE\ and also want to use properly typeset text so we used
20 a different approach (\type [option=MP] {textext} and friends).
21
22 Another difference with the \LUATEX\ library is that we don't support the binary
23 number model, which removes a dependency. We kept decimal number support and also
24 opened that up to the \TEX\ end via \LUA. In addition we support the posit number
25 model, mostly because we also have that at the \TEX\ end to suit the 32 bit
26 model. The repertoire of scanners and injectors has been enlarged which makes it
27 easier and more efficient to interface between the \LUAMETATEX\ subsystems. We
28 also added functionality to \METAPOST, the language and processor. From the users
29 perspective the library is downward compatible but at the same time it offers
30 more.
31
32 Just as \LUATEX\ is frozen, the \METAPOST\ library that it uses can be considered
33 frozen. In \LUAMETATEX\ we have plans for some more extensions. We don't discuss
34 the already present new functionality here in detail, for that we have separate
35 manuals, organized under the \LUAMETAFUN\ umbrella. After all, most of what we
36 did was done in the perspective of using \CONTEXT. Users don't use the functions
37 discussed below because they only make sense in a more integrated approach as
38 with \LUAMETAFUN.
39
40 \stopsection
41
42 \startsection[title={Instances}]
43
44 Before you can process \METAPOST\ code an instance needs to be created. There can
45 be multiple instances active at the same time. They are isolated from each other
46 so they can use different number models and macro sets. Although you can do
47 without files, normally you will load (for instance) macros from a file. This
48 means that we need to interface the library to the file system. If we want to run
49 \LUA, we need to be able to load \LUA\ code. All this is achieved via callbacks
50 that have to be set up when an instance is created.
51
52 \starttyping[option=LUA]
53 function mplib.new (
54 {
55 random_seed = <t:integer>,
56 interaction = <t:string>,
57 job_name = <t:string>,
58 find_file = <t:function>,
59 open_file = <t:function>,
60 run_script = <t:function>,
61 run_internal = <t:function>,
62 make_text = <t:function>,
63 math_mode = <t:string>,
64 utf8_mode = <t:boolean>,
65 text_mode = <t:boolean>,
66 show_mode = <t:boolean>,
67 halt_on_error = <t:boolean>,
68 run_logger = <t:function>,
69 run_overload = <t:function>,
70 run_error = <t:function>,
71 run_warning = <t:function>,
72 bend_tolerance = <t:number>,
73 move_tolerance = <t:number>,
74 }
75 )
76 return <t:mp>
77 end
78 \stoptyping
79
80 The library is fed with \METAPOST\ snippets via an \type {execute} function. We
81 will discuss this in a while.
82
83 \starttyping[option=LUA]
84 function mplib.execute ( <t:mp> instance )
85 return <t:table> -- results
86 end
87 \stoptyping
88
89 An instance can be released with:
90
91 \starttyping[option=LUA]
92 function mplib.finish ( <t:mp> instance )
93 return <t:table> -- results
94 end
95 \stoptyping
96
97 Keeping an instance open is more efficient than creating one per graphic
98 especially when a format has to be loaded. When you execute code, there can be
99 results that for instance can be converted into \PDF\ and included in the
100 currently made document. If one closes an instance it can be that there are
101 pending results to take care of, although in practice that is unlikely to happen.
102
103 When the \type {utf8_mode} parameter is set to \type {true} characters with codes
104 128 upto 255 can be part of identifiers. There is no checking if we have valid
105 \UTF\ but it permits to use that encoding. In \CONTEXT, of course, we enable
106 this. When \type {text_mode} is \type {true} you can use the characters with
107 \ASCII\ \type {STX} (2) and \type {ETC} (3) to fence string literals so that we
108 can use double quotes in strings without the need to escape them. The \type
109 {math_mode} parameter controls the number model that this instance will use.
110 Valid values are \type {scaled} (default), \type {double} (default in \CONTEXT),
111 \type {binary} (not supported), \type {decimal} (less performing but maybe
112 useful) and \type {posit} (so that we can complements the \TEX\ end).
113
114 Valid \type {interaction} values are \type {batch}, \type {nonstop}, \type
115 {scroll}, \type {errorstop} (default) and \type {silent} but in \CONTEXT\ only
116 the last one makes sense. Setting the \type {random_seed} parameter makes it
117 possible to reproduce graphics and prevent documents to be different each run
118 when the size of graphics are different due to randomization. The \type
119 {job_name} parameter is used in reporting and therefore it is mandate.
120
121 Both tolerance parameters default to \im {131 / 65536 = \cldcontext
122 {"\letterpercent N", 131 / 65536}} and help to make the output smaller: \quote
123 {bend} relate to straight lines and \quote {move} to effectively similar points.
124 You can adapt the tolerance any time with:
125
126 \starttyping[option=LUA]
127 function mplib.settolerance (
128 <t:mp> instance,
129 <t:number> bendtolerance,
130 <t:number> movetolerance
131 )
132 -- no return values
133 end
134 \stoptyping
135
136 \starttyping[option=LUA]
137 function mplib.gettolerance ( <t:mp> instance )
138 return
139 <t:number>, -- bendtolerance
140 <t:number> -- movetolerance
141 end
142 \stoptyping
143
144 Next we detail the functions that are hooked into the instance because without
145 them being passed to the engine not that much will happen. We start with the
146 finder. Here \type {mode} is \type {w} or \type {r}. Normally a lookup of a file
147 that is to be read from is done by a dedicated lookup mechanism that knows about
148 the ecosystem the library operates in (like the \TEX\ Directory Structure).
149
150 \starttyping[option=LUA]
151 function find_file (
152 <t:string> filename,
153 <t:string> mode,
154 <t:string> filetype | <t:integer> index
155 )
156 return <t:string> -- foundname
157 end
158 \stoptyping
159
160 % todo filetype vs index
161
162 A (located) file is opened with the \type {open_file} callback that has to
163 return a table with a \type {close} method and a \type {reader} or a \type
164 {writer} dependent of the \type {mode}.
165
166 \starttyping[option=LUA]
167 function open_file (
168 <t:string> filename,
169 <t:string> mode,
170 <t:string> filetype | <t:integer> index
171 )
172 return {
173 close = function()
174 -- return nothing
175 end,
176 reader = function()
177 return <t:string>
178 end,
179 writer = function(<t:string>)
180 -- return nothing
181 end
182 }
183 end
184 \stoptyping
185
186 This approach is not that different from the way we do this at the \TEX\ so like
187 there a reader normally returns lines. The way \METAPOST\ writes to and read from
188 files using primitives is somewhat curious which is why the file type can be a
189 number indicating what handle is used. However, apart from reading files that
190 have code using \type [option=MP] {input} one hardly needs the more low level
191 read and write related primitives.
192
193 The runner is what makes it possible to communicate between \METAPOST\ and
194 \LUA\ and thereby \TEX. There are two possible calls:
195
196 \starttyping[option=LUA]
197 function run_script ( <t:string> code | <t:integer> reference )
198 return <t:string> metapost
199 end
200 \stoptyping
201
202 The second approach makes it possible to implement efficient interfaces where
203 code is turned into functions that are kept around. At the \METAPOST\ end we
204 therefore have, as in \LUATEX:
205
206 \starttyping[option=MP]
207 runscript "some code that will be loaded and run"
208 % more code
209 runscript "some code that will be loaded and run"
210 \stoptyping
211
212 which can of course be optimized by caching, but more interesting is:
213
214 \starttyping[option=MP]
215 newinternal my_function ; my_function := 123 ;
216 runscript my_function ;
217 % more code
218 runscript my_function ;
219 \stoptyping
220
221 which of course has to be dealt with in \LUA. The return value can be a string but also
222 a direct object:
223
224 \starttyping[option=LUA]
225 function run_script (
226 <t:string> code | <t:integer> reference,
227 <t:boolean> direct
228 )
229 return
230 <t:boolean> | <t:number> | <t:string> | <t:table>, -- result
231 <t:boolean> -- success
232 end
233 \stoptyping
234
235 When the second argument is true, the results are injected directly and tables
236 become pairs, colors, paths, transforms, depending on how many elements there
237 are.
238
239 In \METAPOST\ internal variables are quantities that are stored a bit differently
240 and are accessed without using the expression scanner. The \type {run_internal}
241 function triggers when internal \METAPOST\ variables flagged with \type
242 {runscript} are initialized, saved or restored. The first argument is an action,
243 the second the value of internal. When we initialize an internal a third and
244 fourth argument are passed.
245
246 \starttyping[option=LUA]
247 function run_internal (
248 <t:integer> action,
249 <t:integer> internal,
250 <t:integer> category,
251 <t:string> name
252 )
253 -- no return values
254 end
255 \stoptyping
256
257 The category is one of the types that \METAPOST\ also uses elsewhere: integer,
258 boolean, numeric or known. From this you can deduce that internals in \LUAMETATEX\
259 can not only be numbers but also strings or booleans. The possible actions are:
260
261 \ctxlua{moduledata.mplib.codes("getinternalactions")}
262
263 There is of course bit extra overhead involved than normal but that can be
264 neglected especially because we can have more efficient calls to \LUA\ using
265 references stored in internals. It also has the benefit that one can implement
266 additional tracing.
267
268 \METAPOST\ is a graphic language and system and typesetting text is not what it
269 is meant for so that gets delegated to (for instance) \TEX\ using \type
270 [option=MP] {btex} which grabs text upto \type [option=MP] {etex} and passes it
271 to this callback:
272
273 \starttyping[option=LUA]
274 function make_text ( <t:string> str, <t:integer> mode )
275 return <t:string> -- metapost
276 end
277 \stoptyping
278
279 Here mode is only relevant if you want to intercept \typ {verbatimtex} which is
280 something that we don't recommend doing in \CONTEXT, just like we don't recommend
281 using \type [option=MP] {btex}. But, if you use these, keep in mind that spaces
282 matter. The parameter \type [option=MP] {texscriptmode} controls how spaces and
283 newlines get honored. The default value is~1. Possible values are:
284
285 \starttabulate[|l|p|]
286 % \DB name \BC meaning \NC \NR
287 % \TB
288 \FL
289 \BC value \BC meaning \NC \NR
290 \ML
291 \NC \type {0} \NC no newlines \NC \NR
292 \NC \type {1} \NC newlines in \type [option=MP] {verbatimtex} \NC \NR
293 \NC \type {2} \NC newlines in \type [option=MP] {verbatimtex} and \type [option=MP] {etex} \NC \NR
294 \NC \type {3} \NC no leading and trailing strip in \type [option=MP] {verbatimtex} \NC \NR
295 \NC \type {4} \NC no leading and trailing strip in \type [option=MP] {verbatimtex} and \type [option=MP] {btex} \NC \NR
296 \LL
297 \stoptabulate
298
299 That way the \LUA\ handler (assigned to \type {make_text}) can do what it likes.
300 An \type [option=MP] {etex} has to be followed by a space or \type [option=MP]
301 {;} or be at the end of a line and preceded by a space or at the beginning of a
302 line. But let's repeat: these commands are kind of old school and not to be used
303 in \LUAMETAFUN.
304
305 Logging, which includes the output of \type {message} and \type {show}, is also
306 handled by a callback:
307
308 \starttyping[option=LUA]
309 function run_logger ( <t:integer> target, <t:string> str )
310 -- no return values
311 end
312 \stoptyping
313
314 The possible log targets are:
315
316 \ctxlua{moduledata.mplib.codes("getlogtargets")}
317
318 An overload handler will take care of potentially dangerous overloading of for
319 instance primitives, macro package definitions and special variables.
320
321 \starttyping[option=LUA]
322 function run_overload ( <t:integer> property, <t:string> name, <t:integer> mode )
323 return <t:boolean> -- resetproperty
324 end
325 \stoptyping
326
327 The \type {mode} value is the currently set \type [option=MP] {overloadmode}
328 internal. The \METAPOST\ command \typ [option=MP] {setproperty} can be used to
329 relate an integer value to a quantity and when that value is positive a callback
330 is triggered when that quantity gets redefined. Primitives get a property value~1
331 by the engine.
332
333 \startluacode
334 context.starttabulate { "|rT|lT|" }
335 for k, v in table.sortedhash(mplib.propertycodes) do
336 context.NC() context(k)
337 context.NC() context(v)
338 context.NC() context.NR()
339 end
340 context.stoptabulate()
341 \stopluacode
342
343 Overload protect is something very \CONTEXT\ and also present at the \TEX\ end.
344 All \TEX\ and \METAPOST\ quantities have such properties assigned.
345
346 When an error is issued it is often best to just quit the run and fix the issue,
347 just because the instance can now be in a confused state,
348
349 \starttyping[option=LUA]
350 function run_error (
351 <t:string> message,
352 <t:string> helpinfo,
353 <t:integer> interactionmode
354 )
355 -- no return values
356 end
357 \stoptyping
358
359 You can get some statistics concerning an instance but in practice that is not so
360 relevant for users. In \CONTEXT\ these go to the log file.
361
362 \starttyping[option=LUA]
363 function mplib.getstatistics ( <t:mp> instance )
364 return <t:table>
365 end
366 \stoptyping
367
368 The next set of numbers reflect for the current state of the \type {metafun:1}
369 instance that is active for this specific run. Numeric entries are:
370
371 \startthreerows
372 \startluacode
373 local t = mplib.getstatistics(metapost.getinstancebyname("metafun:1"))
374 context.starttabulate { "|l|r|" }
375 for k, v in table.sortedhash(t) do
376 if type(v) ~= "table" then
377 context.BC() context(k)
378 context.NC() context(v)
379 context.NC() context.NR()
380 end
381 end
382 context.stoptabulate()
383 \stopluacode
384 \stopthreerows
385
386 Memory usage is more detailed and comes in sub-tables:
387
388 \startluacode
389 local t = mplib.getstatistics(metapost.getinstancebyname("metafun:1"))
390 context.starttabulate { "|l|l|r|r|r|r|r|" }
391 context.BC()
392 context.BC() context("state")
393 context.BC() context("kept")
394 context.BC() context("max")
395 context.BC() context("pool")
396 context.BC() context("size")
397 context.BC() context("used")
398 context.NC() context.NR()
399 for k, v in table.sortedhash(t) do
400 if type(v) == "table" then
401 context.BC() context(k)
402 context.NC() context(v.state)
403 context.NC() context(v.kept)
404 context.NC() context(v.max)
405 context.NC() context(v.pool)
406 context.NC() context(v.size)
407 context.NC() context(v.used)
408 context.NC() context.NR()
409 end
410 end
411 context.stoptabulate()
412 \stopluacode
413
414 In this version of \MPLIB\ this is informational only. The objects are all
415 allocated dynamically, so there is no chance of running out of space unless the
416 available system memory is exhausted. There is no need to configure memory.
417
418 The scanner in an instance can be in a specific state:
419
420 \starttyping[option=LUA]
421 function mplib.getstatus ( <t:mp> instance )
422 return <t:integer>
423 end
424 \stoptyping
425
426 where possible states are:
427
428 \startfourrows
429 \ctxlua{moduledata.mplib.codes("getstates")}
430 \stopfourrows
431
432 Macro names and variable names are stored in a hash table. You can get a list
433 with entries with \type {gethashentries}, which takes an instance as first
434 argument. When the second argument is \type {true} more details will be provided.
435 With \type {gethashentry} you get info about the given macro or variable.
436
437 \starttyping[option=LUA]
438 function mplib.gethashentries ( <t:mp> instance, <t:boolean> details )
439 <t:table> hashentries
440 end
441 \stoptyping
442
443 \starttyping[option=LUA]
444 function mplib.gethashentry ( <t:mp> instance, <t:string> name )
445 return
446 <t:integer> -- command
447 <t:integer> -- property
448 <t:integer> -- subcommand
449 end
450 \stoptyping
451
452 Say that we have defined:
453
454 \starttyping[option=MP]
455 numeric a ; numeric b ; numeric c ; a = b ; c := b ;
456 \stoptyping
457
458 \startMPcalculation
459 numeric a ; numeric b ; numeric c ; a = b ; c := b ;
460 \stopMPcalculation
461
462 We get values like:
463
464 \startluacode
465 local r = {
466 { "a", mplib.gethashentry(metapost.getinstancebyname("metafun:1"),"a") },
467 { "b", mplib.gethashentry(metapost.getinstancebyname("metafun:1"),"b") },
468 { "c", mplib.gethashentry(metapost.getinstancebyname("metafun:1"),"c") },
469 { "d", mplib.gethashentry(metapost.getinstancebyname("metafun:1"),"d") },
470 { "def", mplib.gethashentry(metapost.getinstancebyname("metafun:1"),"def") },
471 { "vardef", mplib.gethashentry(metapost.getinstancebyname("metafun:1"),"vardef") },
472 { "fullcircle", mplib.gethashentry(metapost.getinstancebyname("metafun:1"),"fullcircle") },
473 }
474 context.starttabulate { "|T|r|r|r|" }
475 for i=1,#r do
476 context.NC() context(r[i][1])
477 context.NC() context(r[i][2])
478 context.NC() context(r[i][3])
479 context.NC() context(r[i][4])
480 context.NC() context.NR()
481 end
482 context.stoptabulate()
483 \stopluacode
484
485 These numbers represent commands, properties and subcommands, and thereby also
486 assume some knowledge about how \METAPOST\ works internally. As this kind of
487 information is only useful when doing low level development we leave it at that.
488
489 \stopsection
490
491 \startsection[title=Processing]
492
493 It is up to the user to decide what to pass to the \type {execute} function as
494 long as it is valid code. Think of each chunk being a syntactically correct file.
495 Statements cannot be split over chunks.
496
497 \starttyping[option=LUA]
498 function mplib.execute ( <t:mp> instance, <t:string> code )
499 return {
500 status = <t:integer>,
501 fig = <t:table>,
502 }
503 end
504 \stoptyping
505
506 In contrast with the normal stand alone \type {mpost} command, there is no
507 implied \quote {input} at the start of the first chunk. When no string is passed
508 to the execute function, there will still be one triggered because it then
509 expects input from the terminal and you can emulate that channel with the
510 callback you provide. In practice this is not something you need to be worry
511 about.
512
513 When code is fed into the library at some point it will shipout a picture. The
514 result always has a \type {status} field and an indexed \type {fig} table that
515 has the graphics produced, although that is not mandate, for instance macro
516 definitions can happen or variables can be set in which case graphics will be
517 constructed later.
518
519 \starttyping[option=LUA]
520 <t:userdata> o = <t:mpobj>:objects ( )
521 <t:table> b = <t:mpobj>:boundingbox ( )
522 <t:number> w = <t:mpobj>:width ( )
523 <t:number> h = <t:mpobj>:height ( )
524 <t:number> d = <t:mpobj>:depth ( )
525 <t:number> i = <t:mpobj>:italic ( )
526 <t:integer> c = <t:mpobj>:charcode ( )
527 <t:number> t = <t:mpobj>:tolerance ( )
528 <t:boolean> s = <t:mpobj>:stacking ( )
529 \stoptyping
530
531 When you access a object that object gets processed before its properties are
532 returned and in the process we loose the original. This means that some
533 information concerning the whole graphic is also no longer reliably available.
534 For instance, you can check if a figure uses stacking with the \type {stacking}
535 function but because objects gets freed after being accessed, no information
536 about stacking is available then.
537
538 The \type {charcode}, \type {width}, \type {height}, \type {depth} and \type
539 {italic} are a left-over from \METAFONT. They are values of the \METAPOST\
540 variables \typ [option=MP] {charcode}, \typ [option=MP] {fontcharwd}, \typ
541 [option=MP] {fontcharht}, \typ [option=MP] {fontchardp} and \typ [option=MP]
542 {fontcharit} at the time the graphic is shipped out.
543
544 You can call \type {fig:objects()} only once for any one \type {fig} object! In
545 the end the graphic is a list of such userdata objects with accessors that
546 depends on what specific data we have at hand. You can check out what fields with
547 the following helper:
548
549 \starttyping[option=LUA]
550 function mplib.getfields ( <t:integer> object | <t:mpobj> object | <t:nil> )
551 return <t:table>
552 end
553 \stoptyping
554
555 You get a simple table with one list of fields, or a table with all possible
556 fields, organized per object type. In practice this helper is only used for
557 documentation.
558
559 \startluacode
560 local t = mplib.getobjecttypes()
561 local f = mplib.getfields()
562 context.starttabulate { "|lT|lT|plT|" }
563 for i=1,#t do
564 context.NC() context(i)
565 context.NC() context(t[i])
566 context.NC() context("% t",f[i])
567 context.NC() context.NR()
568 end
569 context.stoptabulate()
570 \stopluacode
571
572 All graphical objects have a field \type {type} (the second column in the table
573 above) that gives the object type as a string value. When you have a non circular
574 pen an envelope is uses defined by \type {path} as well as \type {htap} and the
575 backend has to make sure that this gets translated into the proper \PDF\
576 operators. Discussing this is beyond this manual. A \type {color} table has one,
577 three or four values depending on the color space used. The \typ {prescript} and
578 \typ {postscript} strings are the accumulated values of these operators,
579 separated by newline characters. The \typ {stacking} number is just that: a
580 number, which can be used to put shapes in front or other shapes, some order, but
581 it depends on the macro package as well as the backend to deal with that; it's
582 basically just a numeric tag.
583
584 Each \type {dash} is a hash with two items. We use the same model as \POSTSCRIPT\
585 for the representation of the dash list; \type {dashes} is an array of \quote
586 {on} and \quote {off}, values, and \type {offset} is the phase of the pattern.
587
588 There is helper function \type {peninfo} that returns a table containing a bunch
589 of vital characteristics of the used pen:
590
591 \starttyping[option=LUA]
592 function mplib.peninfo ( <t:mpobj> object )
593 return {
594 width = <t:number>,
595 rx = <t:number>,
596 ry = <t:number>,
597 sx = <t:number>,
598 sy = <t:number>,
599 tx = <t:number>,
600 ty = <t:number>,
601 }
602 end
603 \stoptyping
604
605 \stopsection
606
607 \startsection[title=Internals]
608
609 There are a couple of helpers that can be used to query the meaning of specific
610 codes and states.
611
612 \starttyping[option=LUA]
613 function mplib.gettype ( <mopobj> object )
614 return <t:integer> -- typenumber
615 end
616 \stoptyping
617
618 \starttyping[option=LUA]
619 function mplib.gettypes ( )
620 return <t:table> -- types
621 end
622 \stoptyping
623
624 \startfourrows
625 \ctxlua{moduledata.mplib.codes("gettypes")}
626 \stopfourrows
627
628 \starttyping[option=LUA]
629 function mplib.getcolormodels ( )
630 return <t:table> -- colormodels
631 end
632 \stoptyping
633
634 \startfourrows
635 \ctxlua{moduledata.mplib.codes("getcolormodels")}
636 \stopfourrows
637
638 \starttyping[option=LUA]
639 function mplib.getcodes ( )
640 return <t:table> -- codes
641 end
642 \stoptyping
643
644 \startfourrows
645 \ctxlua{moduledata.mplib.codes("getcodes")}
646 \stopfourrows
647
648 \starttyping[option=LUA]
649 function mplib.getstates ( )
650 return <t:table> -- states
651 end
652 \stoptyping
653
654 \startfourrows
655 \ctxlua{moduledata.mplib.codes("getstates")}
656 \stopfourrows
657
658 Knots is how the \quote {points} in a curve are called internally and in paths we
659 can find these:
660
661 \starttyping[option=LUA]
662 function mplib.getknotstates ( )
663 return <t:table> -- knotstates
664 end
665 \stoptyping
666
667 \startfourrows
668 \ctxlua{moduledata.mplib.codes("getknotstates")}
669 \stopfourrows
670
671 \starttyping[option=LUA]
672 function mplib.getscantypes ( )
673 return <t:table> -- scantypes
674 end
675 \stoptyping
676
677 \startfourrows
678 \ctxlua{moduledata.mplib.codes("getscantypes")}
679 \stopfourrows
680
681 As with \TEX\ we can log to the console, a log file or both. But one will normally
682 intercept log message anyway.
683
684 \starttyping[option=LUA]
685 function mplib.getlogtargets ( )
686 return <t:table> -- logtargets
687 end
688 \stoptyping
689
690 \startfourrows
691 \ctxlua{moduledata.mplib.codes("getlogtargets")}
692 \stopfourrows
693
694 \starttyping[option=LUA]
695 function mplib.getinternalactions ( )
696 return <t:table> -- internalactions
697 end
698 \stoptyping
699
700 \startfourrows
701 \ctxlua{moduledata.mplib.codes("getinternalactions")}
702 \stopfourrows
703
704 \starttyping[option=LUA]
705 function mplib.getobjecttypes ( )
706 return <t:table> -- objecttypes
707 end
708 \stoptyping
709
710 \startfourrows
711 \ctxlua{moduledata.mplib.codes("getobjecttypes")}
712 \stopfourrows
713
714 The next one is of course dependent on what one runs. These statistics are for
715 all instances:
716
717 \starttyping[option=LUA]
718 function mplib.getcallbackstate ( )
719 return <t:table> -- callbackstate
720 end
721 \stoptyping
722
723 \starttworows
724 \ctxlua{moduledata.mplib.codes("getcallbackstate")}
725 \stoptworows
726
727 The text counter is only counting what gets intercepted by \METAPOST\ and as you
728 can see below, the recommended \type {textext} is handled differently and not
729 counted at all.
730
731 \startlinecorrection
732 \startcombination[nx=2,ny=1,width=.4tw]
733 {\startMPcode
734 fill fullcircle xyscaled (4cm,2cm) withcolor "maincolor" ;
735 draw textext("okay") withcolor "white" ;
736 \stopMPcode} {intercepted by \CONTEXT}
737 {\startMPcode
738 fill fullcircle xyscaled (4cm,2cm) withcolor "maincolor" ;
739 draw btex okay etex withcolor "white" ;
740 \stopMPcode} {intercepted by \METAPOST}
741 \stopcombination
742 \stoplinecorrection
743
744 So we get this now. The file count goes up because from the perspective of
745 \METAPOST\ code that gets executed and passed as string is just like reading from
746 file. The relative high number that we see here reflects that we load quite some
747 \METAFUN\ macros when an instance is initialized.
748
749 \startthreerows
750 \ctxlua{moduledata.mplib.codes("getcallbackstate")}
751 \stopthreerows
752
753 \stopsection
754
755 \startsection[title=Information]
756
757 The \METAPOST\ library in \LUATEX\ starts with version~2 so in \LUAMETATEX\ we
758 start with version~3, assuming that there will me no major update to the older
759 library.
760
761 \starttyping[option=LUA]
762 function mplib.version ( )
763 return <t:string>
764 end
765 \stoptyping
766
767 When there is an error you can ask for some more context:
768
769 \starttyping[option=LUA]
770 function mplib.showcontext ( <t:mp> instance )
771 return <t:string>
772 end
773 \stoptyping
774
775 \stopsection
776
777 \startsection[title=Methods]
778
779 For historical reasons we provide a few functions as methods to an instance: \typ
780 {execute}, \typ {finish}, \typ {getstatistics}, \typ {getstatus} and \typ
781 {solvepath}, just in case someone goes low level.
782
783 \stopsection
784
785 \startsection[title=Scanners]
786
787 There are quite some scanners available and they all take an instance as first
788 argument. Some have optional arguments that give some control. A very basic one
789 is the following. Scanning for a next token in \METAPOST\ is different from \TEX\
790 because while \TEX\ just gets the token, \METAPOST\ can delay in cases where an
791 expression is seen. This means that you can inspect what is coming but do some
792 further scanning based on that. Examples of usage can be found in \CONTEXT\ as it
793 permits to come up with extensions that behave like new primitives or implement
794 interfaces that are otherwise hard to do in pure \METAPOST.
795
796 \starttyping[option=LUA]
797 function mplib.scannext ( <t:mp> instance, <t:boolean> keep )
798 return <t:integer> token, <t:integer> mode, <t:integer> kind
799 end
800 \stoptyping
801
802 here the optional \type {keep} boolean argument default to false but when true we
803 basically have a look ahead scan. Contrary to \TEX\ a next token is not expanded. If we
804 want to pick up the result from an expression we use the next one where again we can
805 push back the result:
806
807 \startfourrows
808 \ctxlua{moduledata.mplib.codes("getscantypes")}
809 \stopfourrows
810
811 \starttyping[option=LUA]
812 function mplib.scanexpression ( <t:mp> instance, <t:boolean> keep )
813 return <t:integer> -- kind
814 end
815 \stoptyping
816
817 The difference between \typ {scantoken} and \typ {scannext} is that the first one
818 scans for a token and the later for a value and yes, one has to play a bit with
819 this to see when one gets what.
820
821 \starttyping[option=LUA]
822 function mplib.scantoken ( <t:mp> instance, <t:boolean> keep )
823 return
824 <t:integer>, -- token
825 <t:integer>, -- mode
826 <t:integer> -- kind
827 end
828 \stoptyping
829
830 \starttyping[option=LUA]
831 function mplib.scansymbol ( <t:mp> instance, <t:boolean> expand, <t:boolean> keep )
832 return <t:string>
833 end
834 \stoptyping
835
836 \starttyping[option=LUA]
837 function mplib.scanproperty ( <t:mp> instance )
838 return <t:integer>
839 end
840 \stoptyping
841
842 These are scanners for the simple data types:
843
844 \starttyping[option=LUA]
845 function mplib.scannumeric ( <t:mp> instance ) return <t:number> end -- scannumber
846 function mplib.scaninteger ( <t:mp> instance ) return <t:integer> end
847 function mplib.scanboolean ( <t:mp> instance ) return <t:boolean> end
848 function mplib.scanstring ( <t:mp> instance ) return <t:string> end
849 \stoptyping
850
851 The scanners that return data types with more than one value can will return a
852 table when the second argument is \type {true}:
853
854 \starttyping[option=LUA]
855 function mplib.scanpair ( <t:mp> instance, <t:boolean astable )
856 return
857 <t:number>, -- x
858 t:number> -- y
859 end
860 \stoptyping
861
862 \starttyping[option=LUA]
863 function mplib.scancolor (
864 <t:mp> instance,
865 <t:boolean astable
866 )
867 return
868 <t:number>, -- r
869 <t:number>, -- g
870 <t:number> -- b
871 end
872 \stoptyping
873
874 \starttyping[option=LUA]
875 function mplib.scancmykcolor ( <t:mp> instance, <t:boolean astable )
876 return
877 <t:number>, -- c
878 <t:number>, -- m
879 <t:number>, -- y
880 <t:number> -- k
881 end
882 \stoptyping
883
884 \starttyping[option=LUA]
885 function mplib.scantransform ( <t:mp> instance, <t:boolean astable )
886 return
887 <t:number>, -- x
888 <t:number>, -- y
889 <t:number>, -- xx
890 <t:number>, -- yx
891 <t:number>, -- xy
892 <t:number> -- yy
893 end
894 \stoptyping
895
896 The path scanned is more complex. First an expression is scanned and when okay
897 it is converted to a table. The compact option gives:
898
899 \starttyping[option=LUA]
900 {
901 cycle = <t:boolean>, -- close
902 pen = <t:boolean>,
903 {
904 <t:number>, -- x_coordinate
905 <t:number>, -- y_coordinate
906 },
907 ...
908 }
909 \stoptyping
910
911 otherwise we get the more detailed:
912
913 \starttyping[option=LUA]
914 {
915 curved = <t:boolean>,
916 pen = <t:boolean>,
917 {
918 [1] = <t:number>, -- x_coordinate
919 [2] = <t:number>, -- y_coordinate
920 [3] = <t:number>, -- x_left
921 [4] = <t:number>, -- y_left
922 [5] = <t:number>, -- x_right
923 [6] = <t:number>, -- y_right
924 left_type = <t:integer>,
925 right_type = <t:integer>,
926 curved = <t:boolean>,
927 state = <t:integer>,
928 },
929 ...
930 }
931 \stoptyping
932
933 Possible (knot, the internal name for a point) states are:
934
935 \startfourrows
936 \ctxlua{moduledata.mplib.codes("getknotstates")}
937 \stopfourrows
938
939 The path scanner function that produces such tables is:
940
941 \starttyping[option=LUA]
942 function mplib.scanpath (
943 <t:mp> instance,
944 <t:boolean> compact,
945 <t:integer> kind,
946 <t:boolean> check
947 )
948 return <t:table>
949 end
950 \stoptyping
951
952 This pen scanner returns similar tables:
953
954 \starttyping[option=LUA]
955 function mplib.scanpen (
956 <t:mp> instance,
957 <t:boolean> compact,
958 <t:integer> kind,
959 <t:boolean> check
960 )
961 return <t:table>
962 end
963 \stoptyping
964
965 The next is not really a scanner. It skips a token that matches the given command
966 and returns a boolean telling if that succeeded.
967
968 \starttyping[option=LUA]
969 function mplib.skiptoken ( <t:mp> instance, <t:integer> command )
970 return <t:boolean>
971 end
972 \stoptyping
973
974 \stopsection
975
976 \startsection[title=Injectors]
977
978 The scanners are complemented by injectors. Instead of strings that have to be
979 parsed by \METAPOST\ they inject the right data structures directly.
980
981 \starttyping[option=LUA]
982 function mplib.injectnumeric ( <t:mp> instance, <t:number> value ) end
983 function mplib.injectinteger ( <t:mp> instance, <t:integer> value ) end
984 function mplib.injectboolean ( <t:mp> instance, <t:boolean> value ) end
985 function mplib.injectstring ( <t:mp> instance, <t:string> value ) end
986 \stoptyping
987
988 In following injectors accept a table as well as just the values. which can more
989 efficient:
990
991 \starttyping[option=LUA]
992 function mplib.injectpair ( <t:mp> instance, <t:table> value ) end
993 function mplib.injectcolor ( <t:mp> instance, <t:table> value ) end
994 function mplib.injectcmykcolor ( <t:mp> instance, <t:table> value ) end
995 function mplib.injecttransform ( <t:mp> instance, <t:table> value ) end
996 \stoptyping
997
998 Injecting a path is not always trivial because we have to connect the points
999 emulating \type [option=MP] {..}, \type [option=MP] {...}, \type [option=MP]
1000 {---} and even \type [option=MP] {&&} and \type [option=MP] {cycle}. A path is
1001 passed as table. The table can be nested and has entries like these:
1002
1003 \starttyping[option=LUA]
1004 {
1005 {
1006 x_coord = <t:number>,
1007 y_coord = <t:number>,
1008 x_left = <t:number>,
1009 y_left = <t:number>,
1010 x_right = <t:number>,
1011 y_right = <t:number>,
1012 left_curl = <t:number>,
1013 right_curl = <t:number>,
1014 left_tension = <t:number>,
1015 right_tension = <t:number>,
1016 direction_x = <t:number>,
1017 direction_y = <t:number>,
1018 },
1019 {
1020 [1] = <t:number>, -- x_coordinate
1021 [2] = <t:number>, -- x_coordinate
1022 [3] = <t:number>, -- x_left
1023 [4] = <t:number>, -- y_left
1024 [5] = <t:number>, -- x_right
1025 [6] = <t:number>, -- y_right
1026 },
1027 "append",
1028 "cycle",
1029 }
1030 \stoptyping
1031
1032 Here \type [option=MP] {append} is like \type [option=MP] {&&} which picks up the
1033 pen, and \type [option=MP] {cycle}, not surprisingly, behaves like the \type
1034 [option=MP] {cycle} operator.
1035
1036 \starttyping[option=LUA]
1037 function mplib.injectpath ( <t:mp> instance, <t:table> value )
1038 -- return nothing
1039 end
1040 \stoptyping
1041
1042 \starttyping[option=LUA]
1043 function mplib.injectwhatever ( <t:mp> instance, <t:hybrid> value )
1044 -- return nothing
1045 end
1046 \stoptyping
1047
1048 When a path is entered and has to be injected some preparation takes place out of
1049 the users sight. A special variant of the path processor is the following, where
1050 the path is adapted and the boolean indicates success.
1051
1052 \starttyping[option=LUA]
1053 function mplib.solvepath ( <t:mp> instance, <t:table> value )
1054 return <t:boolean>
1055 end
1056 \stoptyping
1057
1058 A still somewhat experimental injectors is the following one, that can be used to
1059 fetch information from the \TEX\ end. Valid values for \type {expected} are 1
1060 (integer), 2 (cardinal, 3 (dimension), 5 (boolean) and 7 (string).
1061
1062 \starttyping[option=LUA]
1063 function mplib.expandtex (
1064 <t:mp> instance,
1065 <t:integer> expected,
1066 <t:string> macro,
1067 <t:whatever> arguments
1068 )
1069 return <t:whatever>
1070 end
1071 \stoptyping
1072
1073 \stopsection
1074
1075 \stopdocument