Debian Codegraph @

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

raw
1 % language=us runpath=texruns:manuals/luametatex
2
3 \environment luametatex-style
4
5 \startdocument[title=Libraries]
6
7 \startsection[title={Introduction}]
8
9 The engines has quite some libraries built in of which some are discussed in
10 dedicated chapters. Not all libraries will be detailed here, for instance, so
11 called optional libraries depend on system libraries and usage is wrapped in
12 modules because we delegate as much as possible to \LUA.
13
14 \stopsection
15
16 \startsection[title={Third party}]
17
18 There is not much to tell here other than it depends on the \LUA\ symbols being
19 visible and the \LUA\ version matching. We don't use this in \CONTEXT\ and have a
20 different mechanism instead: optional libraries.
21
22 \stopsection
23
24 \startsection[title={Core}]
25
26 The core libraries are those that interface with \TEX\ and \METAPOST, these are
27 discussed in dedicated chapters:
28
29 \starttabulate[|l|Tl|]
30 \FL
31 \BC chapter \BC library \NC \NR
32 \TL
33 \NC \LUA \NC lua luac \NC \NR
34 \NC \TEX \NC status tex texio \NC \NR
35 \NC \METAPOST \NC mp \NC \NR
36 \NC Nodes \NC node \NC \NR
37 \NC Tokens \NC token \NC \NR
38 \NC Callbacks \NC callback \NC \NR
39 \NC Fonts \NC font \NC \NR
40 \NC Languages \NC language \NC \NR
41 \NC Libraries \NC library \NC \NR
42 \LL
43 \stoptabulate
44
45 Some, like \type {node}, \type {token} and \type {tex} provide a lot of functions
46 but most are used in more higher level \CONTEXT\ specific functions and
47 interfaces. This means that in the code you will more often font \type {nodes}
48 and \type {tokens} being used as well as functions that the macro package adds to
49 the various built-in libraries.
50
51 \stopsection
52
53 \startsection[title={Auxiliary}]
54
55 \startsubsection[title=Extensions]
56
57 These are the libraries that are needed to implement various subsystems, like for
58 instance the backend and image inclusion. Although much can be done in pure \LUA\
59 for performance reasons helpers make sense. However, we try to minimize this,
60 which means that for instance the \type {zip} library provides what we need for
61 (de)compressing for instance \PDF\ streams but that unzipping files is done with
62 \LUA\ code wrapped around the core zip routines. The same is true for \PNG\
63 inclusion: all that was done in pure \LUA\ but a few critical helpers were
64 translated to \CCODE.
65
66 Some libraries extend existing ones, like for instance \type {file}, \type {io}
67 and \type {os} and \type {string}.
68
69 \stopsubsection
70
71 \startsubsection[title=Extra file helpers]
72
73 The original \type {lfs} module has been adapted a bit to our needs but for
74 practical reasons we kept the namespace. In \LUAMETATEX\ we operate in \UTF8
75 so for \MSWINDOWS\ system interfaces we convert from and to \UNICODE16.
76
77 The \type {attributes} checker returns a table with details.
78
79 \starttyping[option=LUA]
80 function lfs.attributes ( <t:string> name )
81 return <t:table> -- details
82 end
83 \stoptyping
84
85 The table has the following fields:
86
87 \starttabulate[|T|||]
88 \FL
89 \BC field \BC type \BC meaning \NC \NR
90 \TL
91 \NC \type {mode} \NC string \NC \type{file} \type{directory} \type{link} \type{other} \NC \NR
92 \NC \type {size} \NC integer \NC bytes \NC \NR
93 \NC \type {modification} \NC integer \NC time \NC \NR
94 \NC \type {access} \NC integer \NC time \NC \NR
95 \NC \type {change} \NC integer \NC time \NC \NR
96 \NC \type {permissions} \NC string \NC \type {rwxrwxrwx} \NC \NR
97 \NC \type {nlink} \NC integer \NC number of links \NC \NR
98 \LL
99 \stoptabulate
100
101 If you're not interested in details, then the next calls are more efficient:
102
103 \starttyping[option=LUA]
104 function lfs.isdir ( <t:string> name ) return <t:boolean> end
105 function lfs.isfile ( <t:string> name ) return <t:boolean> end
106 function lfs.iswriteabledir ( <t:string> name ) return <t:boolean> end
107 function lfs.iswriteablefile ( <t:string> name ) return <t:boolean> end
108 function lfs.isreadabledir ( <t:string> name ) return <t:boolean> end
109 function lfs.isreadablefile ( <t:string> name ) return <t:boolean> end
110 \stoptyping
111
112 The current (working) directory is fetch with:
113
114 \starttyping[option=LUA]
115 function lfs.currentdir ( )
116 return <t:string> -- directory
117 end
118 \stoptyping
119
120 These three return \type {true} is the action was a success:
121
122 \starttyping[option=LUA]
123 function lfs.chdir ( <t:string> name ) return <t:boolean> end
124 function lfs.mkdir ( <t:string> name ) return <t:boolean> end
125 function lfs.rmdir ( <t:string> name ) return <t:boolean> end
126 \stoptyping
127
128 Here the second and third argument are optional:
129
130 \starttyping[option=LUA]
131 function lfs.touch (
132 <t:string> name,
133 <t:integer> accesstime,
134 <t:integer> modificationtime
135 )
136 return <t:boolean> -- success
137 end
138 \stoptyping
139
140 The \type {dir} function is a traverser which in addition to the name returns
141 some more properties. Keep in mind that the traverser loops over a directory and
142 that it doesn't run well when used nested. This is a side effect of the operating
143 system. It is also the reason why we return some properties because querying them
144 via \type {attributes} would interfere badly. The directory iterator has two
145 variants:
146
147 \starttyping[option=LUA]
148 for
149 <t:string> name,
150 <t:string> mode
151 in lfs.dir (
152 <t:string> name
153 )
154 -- actions
155 end
156 \stoptyping
157
158 This one provides more details:
159
160 \starttyping[option=LUA]
161 for
162 <t:string> name,
163 <t:string> mode,
164 <t:integer> size,
165 <t:integer> mtime
166 in lfs.dir (
167 <t:string> name,
168 <t:true>
169 )
170 -- actions
171 end
172 \stoptyping
173
174 Here the boolean indicates if we want a symlink (\type {true}) or hard link
175 (\type {false}).
176
177 \starttyping[option=LUA]
178 function lfs.link (
179 <t:string> source,
180 <t:string> target,
181 <t:boolean> symlink
182 )
183 return <t:boolean> -- success
184 end
185 \stoptyping
186
187 The next one is sort of redundant but explicit:
188
189 \starttyping[option=LUA]
190 function lfs.symlink (
191 <t:string> source,
192 <t:string> target,
193 )
194 return <t:boolean> -- success
195 end
196 \stoptyping
197
198 Helpers like these are a bit operating system and user permission dependent:
199
200 \starttyping[option=LUA]
201 function lfs.setexecutable ( <t:string> name )
202 return <t:boolean> -- success
203 end
204 \stoptyping
205
206 \starttyping[option=LUA]
207 function lfs.symlinktarget ( <t:string> name )
208 return <t:string> -- target
209 end
210 \stoptyping
211
212 \stopsubsection
213
214 \startsubsection[title=Reading from a file]
215
216 Because we load fonts in \LUA\ and because these are binary files we have some
217 helpers that can read integers of various kind and some more. Originally we did
218 this in pure \LUA, which actually didn't perform that bad but this is of course
219 more efficient.
220
221 We have readers for signed and unsigned, little and big endian. All return a (64
222 bit) \LUA\ integer.
223
224 \starttyping[option=LUA]
225 function fio.readcardinal1 ( <t:file> handle ) return <t:integer> end
226 function fio.readcardinal2 ( <t:file> handle ) return <t:integer> end
227 function fio.readcardinal3 ( <t:file> handle ) return <t:integer> end
228 function fio.readcardinal4 ( <t:file> handle ) return <t:integer> end
229
230 function fio.readcardinal1le ( <t:file> handle ) return <t:integer> end
231 function fio.readcardinal2le ( <t:file> handle ) return <t:integer> end
232 function fio.readcardinal3le ( <t:file> handle ) return <t:integer> end
233 function fio.readcardinal4le ( <t:file> handle ) return <t:integer> end
234
235 function fio.readinteger1 ( <t:file> handle ) return <t:integer> end
236 function fio.readinteger2 ( <t:file> handle ) return <t:integer> end
237 function fio.readinteger3 ( <t:file> handle ) return <t:integer> end
238 function fio.readinteger4 ( <t:file> handle ) return <t:integer> end
239
240 function fio.readinteger1le ( <t:file> handle ) return <t:integer> end
241 function fio.readinteger2le ( <t:file> handle ) return <t:integer> end
242 function fio.readinteger3le ( <t:file> handle ) return <t:integer> end
243 function fio.readinteger4le ( <t:file> handle ) return <t:integer> end
244 \stoptyping
245
246 These float readers are rather specific for fonts:
247
248 \starttyping[option=LUA]
249 function fio.readfixed2 ( <t:file> handle ) return <t:number> end
250 function fio.readfixed4 ( <t:file> handle ) return <t:number> end
251 function fio.read2dot14 ( <t:file> handle ) return <t:number> end
252 \stoptyping
253
254 Of these two the first reads a line and the second a string the \CCODE\ way, so
255 ending with a newline and null character:
256
257 \starttyping[option=LUA]
258 function fio.readcline ( <t:file> handle ) return <t:string> end
259 function fio.readcstring ( <t:file> handle ) return <t:string> end
260 \stoptyping
261
262 The next set of readers reads multiple integers in one call:
263
264 \starttyping[option=LUA]
265 function fio.readbytes (
266 <t:file> handle
267 )
268 return <t:integer> -- one or more
269 end
270 \stoptyping
271
272 \starttyping[option=LUA]
273 function fio.readintegertable (
274 <t:file> handle,
275 <t:integer> size,
276 <t:integer> bytes
277 )
278 return <t:table>
279 end
280
281 function fio.readcardinaltable (
282 <t:file> handle,
283 <t:integer> size,
284 <t:integer> bytes
285 )
286 return <t:table>
287 end
288
289 function fio.readbytetable (
290 <t:file> handle
291 )
292 return <t:table>
293 end
294 \stoptyping
295
296 In case we need a random access the following have to be used:
297
298 \starttyping[option=LUA]
299 function fio.setposition ( <t:file> handle, <t:integer> ) return <t:integer> end
300 function fio.getposition ( <t:file> handle ) return <t:integer> end
301 function fio.skipposition ( <t:file> handle, <t:integer> ) return <t:integer> end
302 \stoptyping
303
304 The library also provide a few writers:
305
306 \starttyping[option=LUA]
307 function fio.writecardinal1 ( <t:file> handle, <t:integer> value ) end
308 function fio.writecardinal2 ( <t:file> handle, <t:integer> value ) end
309 function fio.writecardinal3 ( <t:file> handle, <t:integer> value ) end
310 function fio.writecardinal4 ( <t:file> handle, <t:integer> value ) end
311
312 function fio.writecardinal1le ( <t:file> handle, <t:integer> value ) end
313 function fio.writecardinal2le ( <t:file> handle, <t:integer> value ) end
314 function fio.writecardinal3le ( <t:file> handle, <t:integer> value ) end
315 function fio.writecardinal4le ( <t:file> handle, <t:integer> value ) end
316 \stoptyping
317
318 \stopsubsection
319
320 \startsubsection[title=Reading from a string]
321
322 These readers take a string and position. We could have used a userdata approach
323 but it saves little. (Nowadays we can more easily store the position with the
324 userdata so maybe some day \unknown).
325
326 \starttyping[option=LUA]
327 function sio.readcardinal1 ( <t:string> s, <t:integer> p ) return <t:integer> end
328 function sio.readcardinal2 ( <t:string> s, <t:integer> p ) return <t:integer> end
329 function sio.readcardinal3 ( <t:string> s, <t:integer> p ) return <t:integer> end
330 function sio.readcardinal4 ( <t:string> s, <t:integer> p ) return <t:integer> end
331
332 function sio.readcardinal1le ( <t:string> s, <t:integer> p ) return <t:integer> end
333 function sio.readcardinal2le ( <t:string> s, <t:integer> p ) return <t:integer> end
334 function sio.readcardinal3le ( <t:string> s, <t:integer> p ) return <t:integer> end
335 function sio.readcardinal4le ( <t:string> s, <t:integer> p ) return <t:integer> end
336
337 function sio.readinteger1 ( <t:string> s, <t:integer> p ) return <t:integer> end
338 function sio.readinteger2 ( <t:string> s, <t:integer> p ) return <t:integer> end
339 function sio.readinteger3 ( <t:string> s, <t:integer> p ) return <t:integer> end
340 function sio.readinteger4 ( <t:string> s, <t:integer> p ) return <t:integer> end
341
342 function sio.readinteger1le ( <t:string> s, <t:integer> p ) return <t:integer> end
343 function sio.readinteger2le ( <t:string> s, <t:integer> p ) return <t:integer> end
344 function sio.readinteger3le ( <t:string> s, <t:integer> p ) return <t:integer> end
345 function sio.readinteger4le ( <t:string> s, <t:integer> p ) return <t:integer> end
346 \stoptyping
347
348 Here are the (handy for fonts) float readers:
349
350 \starttyping[option=LUA]
351 function sio.readfixed2 ( <t:string> s, <t:integer> p ) return <t:number> end
352 function sio.readfixed4 ( <t:string> s, <t:integer> p ) return <t:number> end
353 function sio.read2dot14 ( <t:string> s, <t:integer> p ) return <t:number> end
354 \stoptyping
355
356 A \CCODE\ line (terminated by a newline) and string (terminated by null) are read
357 by:
358
359 \starttyping[option=LUA]
360 function sio.readcline ( <t:string> s, <t:integer> p ) return <t:string> end
361 function sio.readcstring ( <t:string> s, <t:integer> p ) return <t:string> end
362 \stoptyping
363
364 \starttyping[option=LUA]
365 function sio.readbytes (
366 <t:string> str,
367 <t:integer> pos
368 )
369 return <t:integer> -- one or more
370 end
371 \stoptyping
372
373 \starttyping[option=LUA]
374 function sio.readintegertable (
375 <t:string> str,
376 <t:integer> pos,
377 <t:integer> size,
378 <t:integer> bytes
379 )
380 return <t:table>
381 end
382
383 function sio.readcardinaltable (
384 <t:string> str,
385 <t:integer> pos,
386 <t:integer> size,
387 <t:integer> bytes
388 )
389 return <t:table>
390 end
391
392 function sio.readbytetable (
393 <t:string> str,
394 <t:integer> pos
395 )
396 return <t:table>
397 end
398 \stoptyping
399
400 Here are a few straightforward converters:
401
402 \starttyping[option=LUA]
403 function sio.tocardinal1 ( <t:string> ) return <t:integer> end
404 function sio.tocardinal2 ( <t:string> ) return <t:integer> end
405 function sio.tocardinal3 ( <t:string> ) return <t:integer> end
406 function sio.tocardinal4 ( <t:string> ) return <t:integer> end
407
408 function sio.tocardinal1le ( <t:string> ) return <t:integer> end
409 function sio.tocardinal2le ( <t:string> ) return <t:integer> end
410 function sio.tocardinal3le ( <t:string> ) return <t:integer> end
411 function sio.tocardinal4le ( <t:string> ) return <t:integer> end
412 \stoptyping
413
414 \stopsubsection
415
416 \startsubsection[title=Extra file helpers]
417
418 This function gobble characters upto a newline. When characters are gobbled.
419 \type {true} is returned when we end up at a newline or when something is gobbled
420 before the file ends, other wise we get \type {false}. A \type {nil} return value
421 indicates a bad handle.
422
423 \starttyping[option=LUA]
424 function io.gobble( <t:file> )
425 return <t:boolean> | <t:nil>
426 end
427 \stoptyping
428
429 Function like type {io.open} \type {io.popen} are patched to support files on
430 \MSWINDOWS\ that use wide \UNICODE.
431
432 \stopsubsection
433
434 \startsubsection[title=Extra operating system helpers]
435
436 The \type {os} library has a few extra functions and variables so for complete
437 overview you need to look in the \LUA\ manual.
438
439 We can sleep for the given number of seconds. When the optional \type {units}
440 arguments is (for instance) 1000 we assume milliseconds.
441
442 \starttyping[option=LUA]
443 function os.sleep (
444 <t:integer> seconds,
445 <t:integers> units
446 )
447 -- no return values
448 end
449 \stoptyping
450
451 The \type {os.uname} function returns a table with specific operating system
452 information acquired at runtime. The fields in the returned table are:
453 \showenginekeylist {os.getunamefields()}.
454
455 \starttyping[option=LUA]
456 function os.uname ( )
457 return <t:table>
458 \stoptyping
459
460 The \type {os.gettimeofday} function returns the current \quote {\UNIX\ time},
461 but as a float. Keep in mind that there might be platforms where this function is
462 not available.
463
464 \starttyping[option=LUA]
465 function os.gettimeofday ( )
466 return <t:number>
467 end
468 \stoptyping
469
470 When we execute a command the return code is returned. Interpretation is up to
471 the caller.
472
473 \starttyping[option=LUA]
474 function os.execute ( <t:string> )
475 return <t:integer> -- return code
476 end
477 \stoptyping
478
479 This one enable interpreting \ANSI\ escape sequences in the console. It is only
480 implemented for \MSWINDOWS. In \CONTEXT\ you can run with \type {--ansi}.
481
482 \starttyping[option=LUA]
483 function os.enableansi ( )
484 return <t:boolean>
485 end
486 \stoptyping
487
488 This one only returns something useful for \MSWINDOWS. One can of course just set
489 your the system for \UTF8. It's just a reporter meant for debugging issues.
490
491 \starttyping[option=LUA]
492 function os.getcodepage ( )
493 return
494 <t:integer> oemcodepage,
495 <t:integer> applicationcodepage
496 end
497 \stoptyping
498
499 The \type {os.setenv} function sets a variable in the environment. Passing
500 \type {nil} instead of a value string will remove the variable.
501
502 \starttyping[option=LUA]
503 function os.setenv (
504 <t:string> key,
505 <t:string> value
506 )
507 -- no return values
508 end
509 \stoptyping
510
511 The possible values of \type {os.type} are: \showenginekeylist
512 {os.gettypevalues()}.
513
514 \starttyping[option=LUA]
515 local currenttype = os.type
516 \stoptyping
517
518 The \type {os.name} string gives a more precise indication of the operating
519 system. The possible values are: \showenginekeylist {os.getnamevalues()}.
520
521 \starttyping[option=LUA]
522 local currentname = os.name
523 \stoptyping
524
525 On \MSWINDOWS\ the original \type {os.rename}, \type {os.remove} and \type
526 {os.getenv} functions are replaced by variants that interface to and convert from
527 \UNICODE16\ to \UTF8.
528
529 \stopsubsection
530
531 \startsubsection[title=Extra string helpers]
532
533 The \type {string} library has gotten a couple of extra functions too, some of
534 which are iterators. There are some \UNICODE\ related helpers too. When we
535 started \LUA\ had no \UTF8 function, now it has a few, but we keep using our own,
536 if only because they were there before. We also add plenty extra functions in the
537 \type {string} name space at the \LUA\ end.
538
539 This first function runs over a string and pick sup single characters:
540
541 \starttyping[option=LUA]
542 for <t:string> c in string.characters ( <t:string> s ) do
543 -- some action
544 end
545 \stoptyping
546
547 \startbuffer
548 \startluacode
549 for c in string.characters("τεχ") do
550 context("[%02X]",string.byte(c))
551 end
552 \stopluacode
553 \stopbuffer
554
555 \typebuffer
556
557 gives: \inlinebuffer.
558
559 \starttyping[option=LUA]
560 for <t:string> l, <t:string> r in string.characterpairs ( <t:string> s ) do
561 -- some action
562 end
563 \stoptyping
564
565 \startbuffer
566 \startluacode
567 for l, r in string.characterpairs("τεχ") do
568 context("[%02X %02X]",string.byte(l),string.byte(r))
569 end
570 \stopluacode
571 \stopbuffer
572
573 \typebuffer
574
575 gives: \inlinebuffer.
576
577 \starttyping[option=LUA]
578 for <t:string> c in string.utfcharacters( <t:string> s ) do
579 -- some action
580 end
581 \stoptyping
582
583 \startbuffer
584 \startluacode
585 for c in string.utfcharacters("τεχ") do
586 context("[%s]",c)
587 end
588 \stopluacode
589 \stopbuffer
590
591 \typebuffer
592
593 gives: \inlinebuffer.
594
595 Instead of getting strings back we can also get integers.
596
597 \starttyping[option=LUA]
598 for <t:integer> c in string.bytes ( <t:string> s ) do
599 -- some action
600 end
601 \stoptyping
602
603 \startbuffer
604 \startluacode
605 for b in string.bytes("τεχ") do
606 context("[%02X]",b)
607 end
608 \stopluacode
609 \stopbuffer
610
611 \typebuffer
612
613 gives: \inlinebuffer.
614
615 \starttyping[option=LUA]
616 for <t:integer> l, <t:integer> r in string.bytepairs ( <t:string> s ) do
617 -- some action
618 end
619 \stoptyping
620
621 \startbuffer
622 \startluacode
623 for l, r in string.bytepairs("τεχ") do
624 context("[%02X %02X]",l,r)
625 end
626 \stopluacode
627 \stopbuffer
628
629 \typebuffer
630
631 gives: \inlinebuffer.
632
633 \starttyping[option=LUA]
634 for <t:integer> u in string.utfvalues( <t:string> s ) do
635 -- some action
636 end
637 \stoptyping
638
639 \startbuffer
640 \startluacode
641 for c in string.utfvalues("τεχ") do
642 context("[%U]",c)
643 end
644 \stopluacode
645 \stopbuffer
646
647 \typebuffer
648
649 gives: \inlinebuffer.
650
651 The \type {bytetable} function splits a string in bytes.
652
653 \starttyping[option=LUA]
654 function string.bytetable ( <s:string> s) do
655 return <t:table> -- with bytes
656 end
657 \stoptyping
658
659 Here is a line splitter:
660
661 \starttyping[option=LUA]
662 function string.linetable ( <s:string> s) do
663 return <t:table> -- with lines
664 end
665 \stoptyping
666
667 This one converts an integer (code point) into an \UTF\ string:
668
669 \starttyping[option=LUA]
670 function string.utfcharacter ( <t:string> s )
671 return <t:string>
672 end
673 \stoptyping
674
675 We also have a variant that takes a table. The table can have integers, strings,
676 and subtables.
677
678 \starttyping[option=LUA]
679 function string.utfcharacter ( <t:table> s )
680 return <t:string>
681 end
682 \stoptyping
683
684 This an \UTF8\ variant of \type {string.byte} and it returns the code points of
685 the split on the stack.
686
687 \starttyping[option=LUA]
688 function string.utfvalue ( <t:string> s )
689 return <t:integer> -- zero or more
690 end
691 \stoptyping
692
693 Instead of a list on the stack you can get a table:
694
695 \starttyping[option=LUA]
696 function string.utfvaluetable ( <t:string> s )
697 return <t:table> -- indexed
698 end
699 \stoptyping
700
701 The name says it all:
702
703 \starttyping[option=LUA]
704 function string.utflength ( <tr:string> s )
705 return <t:integer>
706 end
707 \stoptyping
708
709 Here we split a string in characters that are collected in an indexed table:
710
711 \starttyping[option=LUA]
712 function string.utfcharactertable ( <t:string> s )
713 return <t:table> -- indexed
714 end
715 \stoptyping
716
717 In \CONTEXT\ we mostly use \type {string.formatters} which is often more
718 efficient then \type {string.format} and also has additional formatting options,
719 one being for instance \type {N} which is like \type {f} but strips trailing zero
720 and returns efficient zeros and ones. Here is a similar low level formatter:
721
722 \starttyping[option=LUA]
723 function string.f6 ( <t:number> n )
724 return <t:string>
725 end
726
727 function string.f6 ( <t:number> n, <t:string> f )
728 return <t:string>
729 end
730 \stoptyping
731
732 In the first case it returns a string with at most 6 digits while the second one
733 uses given format but tail strips the result.
734
735 \starttyping[option=LUA]
736 function string.tounicode16 ( <t:integer> code ) return <t:string> end
737 \stoptyping
738
739 \starttyping[option=LUA]
740 function string.toutf8 ( <t:table> codes ) return <t:string> end
741 -------- string.toutf16 ( <t:table> codes ) return <t:string> end
742 function string.toutf32 ( <t:table> codes ) return <t:string> end
743 \stoptyping
744
745 The next one has quite some variation in calling:
746
747 \starttyping[option=LUA]
748 function string.utf16toutf8 ( <t:string> str, <t:true> )
749 return <t:string> -- big endian
750 end
751 \stoptyping
752
753 \starttyping[option=LUA]
754 function string.utf16toutf8 ( <t:string> str, <t:false> )
755 return <t:string> -- little endian
756 end
757 \stoptyping
758
759 \starttyping[option=LUA]
760 function string.utf16toutf8 ( <t:string> str, <t:nil>, <t:true> )
761 return <t:string> -- check bom, default to big endian
762 end
763 \stoptyping
764
765 \starttyping[option=LUA]
766 function string.utf16toutf8 ( <t:string> str, <t:nil>, <t:false> )
767 return <t:string> end -- check bom, default to little endian
768 end
769 \stoptyping
770
771 \starttyping[option=LUA]
772 function string.utf16toutf8 ( <t:string> str, <t:nil>, <t:nil> )
773 return <t:string> end -- check bom, default to little endian
774 end
775 \stoptyping
776
777 The next packer is used for creating bitmaps:
778
779 \starttyping[option=LUA]
780 function string.packrowscolumns ( <t:table> data )
781 return <t:string>
782 end
783 \stoptyping
784
785 \startbuffer
786 \startluacode
787 local t = {
788 { 65, 66, 67 },
789 { 68, 69, 70 },
790 }
791 context(string.packrowscolumns(t))
792 \stopluacode
793 \stopbuffer
794
795 For example:
796
797 \typebuffer
798
799 gives: \inlinebuffer
800
801 \startbuffer
802 \startluacode
803 local t = {
804 { { 114, 103, 98 }, { 114, 103, 98 } },
805 { { 114, 103, 98 }, { 114, 103, 98 } },
806 }
807
808 context(string.packrowscolumns(t))
809 \stopluacode
810 \stopbuffer
811
812 While:
813
814 \typebuffer
815
816 gives: \inlinebuffer
817
818 A string with hexadecimals can be converted with the following. Spaces are
819 ignored. We use this for instance in the \METAPOST\ potrace interface to
820 permits nice input.
821
822 \starttyping[option=LUA]
823 function string.hextocharacters ( <t:string> data )
824 return <t:string>
825 end
826 \stoptyping
827
828 \startbuffer
829 \startluacode
830 local t = [[
831 414243 44 4546 47
832 414243 44 4546 47
833 ]]
834
835 context(string.hextocharacters(t))
836 \stopluacode
837 \stopbuffer
838
839 So:
840
841 \typebuffer
842
843 gives: \inlinebuffer
844
845 These take strings and return integers:
846
847 \starttyping[option=LUA]
848 function string.octtointeger ( <t:string> octstr ) return <t:integer> end
849 function string.dectointeger ( <t:string> decstr ) return <t:integer> end
850 function string.hextointeger ( <t:string> hexstr ) return <t:integer> end
851 function string.chrtointeger ( <t:string> chrstr ) return <t:integer> end
852 \stoptyping
853
854 \stopsubsection
855
856 \startsubsection[title=Extra table helpers]
857
858 This returns the keys of the given table:
859
860 \starttyping[option=LUA]
861 function table.getkeys ( < t:table> )
862 return <t:table>
863 end
864 \stoptyping
865
866 \stopsubsection
867
868 \startsubsection[title=Byte encoding and decoding]
869
870 We use some helpers from \type {pplib}.
871
872 \starttyping[option=LUA]
873 function basexx.encode16 ( <t:string> str, <t:boolean> newline )
874 return <t:string>
875 end
876 function basexx.encode64 ( <t:string> str, <t:boolean> newline )
877 return <t:string>
878 end
879 function basexx.encode85 ( <t:string> str, <t:boolean> newline )
880 return <t:string>
881 end
882
883 function basexx.decode16 ( <t:string> str ) return <t:string> end
884 function basexx.decode64 ( <t:string> str ) return <t:string> end
885 function basexx.decode85 ( <t:string> str ) return <t:string> end
886
887 function basexx.encodeRL ( <t:string> str ) return <t:string> end
888 function basexx.decodeRL ( <t:string> str ) return <t:string> end
889
890 function basexx.encodeLZW ( <t:string> str ) return <t:string> end
891 function basexx.decodeLZW ( <t:string> str ) return <t:string> end
892 \stoptyping
893
894 The last two functions accept an optional bitset with coder flags that we leave
895 for the user to ponder about. The \type {newline} directive in the first three is
896 optional.
897
898 \stopsubsection
899
900 \startsubsection[title=\PNG\ decoding]
901
902 These function started out as pure \LUA\ functions (extrapolated from the
903 descriptions in the standard) but eventually became library helpers. It is
904 worth noticing that \PDF\ supports \JPEG\ directly so there we can just use
905 \LUA\ to interpret the file and pass relevant data. Support for \PNG\ is
906 actually just support for \PNG\ compression, so there we need to do more
907 work and filter the content:
908
909 \starttyping[option=LUA]
910 function decode.applyfilter (
911 <t:string> data,
912 <t:integer> nx,
913 <t:integer> ny,
914 <t:integer> slice
915 )
916 return <t:string>
917 end
918 \stoptyping
919
920 We also need to split off the mask as ie becomes a separate object:
921
922 \starttyping[option=LUA]
923 function decode.splitmask (
924 <t:string> data,
925 <t:integer> nx,
926 <t:integer> ny,
927 <t:integer> bpp,
928 <t:integer> bytes
929 )
930 return
931 <t:string>, -- bitmap
932 <t:string> -- mask
933 end
934 \stoptyping
935
936 If present we have to deinterlace:
937
938 \starttyping[option=LUA]
939 function decode.interlace (
940 <t:string> data,
941 <t:integer> nx,
942 <t:integer> ny,
943 <t:integer> slice,
944 <t:integer> pass
945 )
946 return <t:string>
947 end
948 \stoptyping
949
950 And maybe expand compressed:
951
952 \starttyping[option=LUA]
953 function decode.expand (
954 <t:string> data,
955 <t:integer> nx,
956 <t:integer> ny,
957 <t:integer> parts,
958 <t:integer> xline,
959 <t:integer> factor
960 )
961 return <t:string>
962 end
963 \stoptyping
964
965 These are just helpers that permit integration in the \CONTEXT\ graphic
966 ecosystem (including \METAPOST):
967
968 \starttyping[option=LUA]
969 function decode.tocmyk ( <t:string data )
970 return <t:string>
971 end
972 \stoptyping
973
974 For usage see the \CONTEXT\ sources.
975
976 \starttyping[option=LUA]
977 function decode.tomask (
978 <t:string> content,
979 <t:string> mapping,
980 <t:integer> xsize,
981 <t:integer> ysize,
982 <t:integer> colordepth
983 )
984 return <t:string>
985 end
986 \stoptyping
987
988 There are to variants:
989
990 \starttyping[option=LUA]
991 function decode.makemask (
992 <t:string> content,
993 <t:integer> mapping
994 )
995 return <t:string>
996 end
997
998 function decode.makemask (
999 <t:string> content,
1000 <t:table> mapping
1001 )
1002 return <t:string>
1003 end
1004 \stoptyping
1005
1006 \stopsubsection
1007
1008 \startsubsection[title=MD5 hashing]
1009
1010 In the meantime we use some helpers from \type {pplib} because we have that
1011 anyway. These are useful when we need a reasonable unique hash of limited length:
1012
1013 \starttyping[option=LUA]
1014 function md5.sum ( <t:string> ) return <t:string> end
1015 function md5.hex ( <t:string> ) return <t:string> end
1016 function md5.HEX ( <t:string> ) return <t:string> end
1017 \stoptyping
1018
1019 \startbuffer
1020 \startluacode
1021 context.type(md5.HEX("normally this is unique enough"))
1022 \stopluacode
1023 \stopbuffer
1024
1025 Using a hexadecimal representation of the 16 byte calculated checksum is less
1026 sensitive for escaping. This:
1027
1028 \typebuffer
1029
1030 gives: \inlinebuffer.
1031
1032 \stopsubsection
1033
1034 \startsubsection[title=SHA2 hashing]
1035
1036 Because \type {pplib} comes with some SHA2 support we can borrow its helpers
1037 instead of the \LUA\ code we used before (which was anyway fun to write).
1038
1039 \starttyping[option=LUA]
1040 function sha2.digest256 ( <t:string> data ) return <t:string> end
1041 function sha2.digest384 ( <t:string> data ) return <t:string> end
1042 function sha2.digest512 ( <t:string> data ) return <t:string> end
1043 function sha2.sum256 ( <t:string> data ) return <t:string> end
1044 function sha2.sum384 ( <t:string> data ) return <t:string> end
1045 function sha2.sum512 ( <t:string> data ) return <t:string> end
1046 function sha2.hex256 ( <t:string> data ) return <t:string> end
1047 function sha2.hex384 ( <t:string> data ) return <t:string> end
1048 function sha2.hex512 ( <t:string> data ) return <t:string> end
1049 function sha2.HEX256 ( <t:string> data ) return <t:string> end
1050 function sha2.HEX384 ( <t:string> data ) return <t:string> end
1051 function sha2.HEX512 ( <t:string> data ) return <t:string> end
1052 \stoptyping
1053
1054 \startbuffer
1055 \startluacode
1056 context.type(sha2.HEX256("normally this is unique enough"))
1057 \stopluacode
1058 \stopbuffer
1059
1060 The number refers to bytes, so with 256 we get a 32 byte hash that we show in
1061 hexadecimal because that is less sensitive for escaping:
1062
1063 \typebuffer
1064
1065 gives: \inlinebuffer.
1066
1067 \stopsubsection
1068
1069 \startsubsection[title=AES encryption]
1070
1071 In the next encryption functions the key should be 16, 24 or 32 bytes long.
1072
1073 \starttyping[option=LUA]
1074 function aes.encode (
1075 <t:string> data,
1076 <t:string> key
1077 )
1078 return <t:string>
1079 end
1080
1081 function aes.decode (
1082 <t:string> data,
1083 <t:string> key
1084 )
1085 return <t:string>
1086 end
1087 \stoptyping
1088
1089 This returns a string. The default length is 16; the optional length is limited
1090 to 32.
1091
1092 \starttyping[option=LUA]
1093 function aes.random ( <t:integer> length )
1094 return <t:string>
1095 end
1096 \stoptyping
1097
1098 Here is an example:
1099
1100 \startbuffer
1101 \startluacode
1102 context.type ( basexx.encode16 ( aes.encode (
1103 "normally this is unique enough",
1104 "The key of live!"
1105 ) ) )
1106 \stopluacode
1107 \stopbuffer
1108
1109 \typebuffer
1110
1111 This gives: \inlinebuffer, where we hexed the result because
1112 it is unlikely to be valid \UTF8.
1113
1114 \stopsubsection
1115
1116 \startsubsection[title=ZIP (de)compression]
1117
1118 We provide the minimum needed to support compression in the backend but even this
1119 limited set makes it possible to implement a zip file compression utility which
1120 is indeed what we do in \CONTEXT. We use \type {minizip} as codebase, without the
1121 zip utility code. The meaning and application of the various arguments can be
1122 found (and are better explained) on the internet.
1123
1124 \starttyping[option=LUA]
1125 function xzip.compress (
1126 <t:string> data,
1127 <t:integer> compresslevel,
1128 <t:integer> method,
1129 <t:integer> window,
1130 <t:integer> memory,
1131 <t:integer> strategy
1132 )
1133 return <t:string>
1134 end
1135
1136 function xzip.compresssize (
1137 <t:string> data,
1138 <t:integer> buffersize,
1139 <t:integer> compresslevel,
1140 <t:integer> window
1141 )
1142 return <t:string>
1143 end
1144
1145 function xzip.decompress (
1146 <t:string> data,
1147 <t:integer> window
1148 )
1149 return <t:string>
1150 end
1151
1152 function xzip.decompresssize (
1153 <t:string> data,
1154 <t:integer> targetsize,
1155 <t:integer> window
1156 )
1157 return <t:string>
1158 end
1159
1160 function xzip.adler32 (
1161 <t:string> buffer,
1162 <t:integer> checksum
1163 )
1164 return <t:integer>
1165 end
1166
1167 function xzip.crc32 (
1168 <t:string> buffer,
1169 <t:integer> checksum
1170 )
1171 return <t:integer>
1172 end
1173 \stoptyping
1174
1175 \stopsubsection
1176
1177 \startsubsection[title=Potrace]
1178
1179 The excellent potrace manual explains everything about this library therefore
1180 here we just show the interface. Possible fields in specification are:
1181 \showenginekeylist {table.sortedkeys (potrace.getnewfields ())}
1182
1183 \starttyping[option=LUA]
1184 function potrace.new ( <t:table> specification )
1185 return <t:userdata> -- instance
1186 end
1187 \stoptyping
1188
1189
1190 \starttyping[option=LUA]
1191 function potrace.free ( <t:userdata> instance)
1192 -- no return values
1193 end
1194 \stoptyping
1195
1196 The process is controlled by the specification: \showenginekeylist {table.sortedkeys
1197 (potrace.getprocessfields ())}, where permitted policy values are \showenginekeylist
1198 {potrace.getpolicyvalues()}.
1199
1200 \starttyping[option=LUA]
1201 function potrace.process ( <t:userdata> instance, <t:table> specification )
1202 return <t:boolean> -- success
1203 end
1204 \stoptyping
1205
1206 Results are collected in a table that we can feed into \METAPOST, The table has
1207 subtables per traced shape and these contain indexed tables with two (pair) or
1208 six (curve) entries. There is a boolean \type {sign} field and an integer \type
1209 {index} field. In the next function only the first argument is mandate.
1210
1211 \starttyping[option=LUA]
1212 function potrace.totable (
1213 <t:userdata> instance,
1214 <t:boolean> debug,
1215 <t:integer> first,
1216 <t:integer> last
1217 )
1218 return <t:table>
1219 end
1220 \stoptyping
1221
1222 \stopsubsection
1223
1224 \startsubsection[title=Sparse hashes]
1225
1226 The sparse library is just there because we use similar code to store all these
1227 character related codes that way (\type {\lccode}) and such). The entries can be
1228 1 (\type {0xFF}), 2 (\type {0xFFFF}) or 4 (\type {0xFFFFFFFF}) bytes wide. When 0
1229 is used as width then nibbles (\type {0xF}) are assumed.
1230
1231 \starttyping[option=LUA]
1232 function sparse.new (
1233 <t:integer> bytes,
1234 <t:integer> default
1235 )
1236 return <t:userdata>
1237 end
1238 \stoptyping
1239
1240 You set a value by index. Optionally there can be the \type {"global"} keyword
1241 before the second argument.
1242
1243 \starttyping[option=LUA]
1244 function sparse.set (
1245 <t:userdata> instance,
1246 <t:integer> index,
1247 <t:integer> value
1248 )
1249 return <t:integer>
1250 end
1251 \stoptyping
1252
1253 We get back integers as that is what we store:
1254
1255 \starttyping[option=LUA]
1256 function sparse.get ( <t:userdata> instance ) return <t:integer> end
1257 function sparse.min ( <t:userdata> instance ) return <t:integer> end
1258 function sparse.max ( <t:userdata> instance ) return <t:integer> end
1259 \stoptyping
1260
1261 The range is fetched with:
1262
1263 \starttyping[option=LUA]
1264 function sparse.range ( <t:userdata> instance )
1265 return
1266 <t:integer>, -- min
1267 <t:integer> -- max
1268 end
1269 \stoptyping
1270
1271 We can iterate over the hash:
1272
1273 \starttyping[option=LUA]
1274 for
1275 <t:integer> index,
1276 <t:integer> value
1277 in sparse.traverse (
1278 <t:userdata> instance
1279 ) do
1280 -- actions
1281 end
1282 \stoptyping
1283
1284 This is a somewhat strange one but it permits packing all values in a string.
1285 It's another way to create bitmaps.
1286
1287 \starttyping[option=LUA]
1288 function sparse.concat (
1289 <t:userdata> instance
1290 <t:integer> min,
1291 <t:integer> max,
1292 <t:integer> how -- 0=byte, 1=lsb 2=msb
1293 )
1294 return <t:string>
1295 end
1296 \stoptyping
1297
1298 Setting values obeys grouping in \TEX, but we can restore any time:
1299
1300 \starttyping[option=LUA]
1301 function sparse.restore ( <t:userdata> instance )
1302 -- nothing to return
1303 end
1304 \stoptyping
1305
1306 We can also wipe all values:
1307
1308 \starttyping[option=LUA]
1309 function sparse.wipe (<t:userdata> instance )
1310 -- nothing to return
1311 end
1312 \stoptyping
1313
1314 \stopsubsection
1315
1316 \startsubsection[title=Posits]
1317
1318 We implement posits as userdata . We use the library from the posit team,
1319 although it is not complete so we might roll out our own variant (as we need less
1320 anyway). The advance of userdata is that we can use the binary and relation
1321 operators.
1322
1323 Here are the housekeeping functions. Some are more tolerant with respect to
1324 arguments, take the allocator:
1325
1326 \starttyping[option=LUA]
1327 function posit.new ( ) return <t:posit> end
1328 function posit.new ( <t:string> s ) return <t:posit> end
1329 function posit.new ( <t:number> n ) return <t:posit> end
1330 \stoptyping
1331
1332 When a posit is expected a number or string is also accepted which is then
1333 converted to a posit.
1334
1335 \starttyping[option=LUA]
1336 function posit.copy ( <t:posit> p ) return <t:posit> end
1337 function posit.tostring ( <t:posit> p ) return <t:string> end
1338 function posit.tonumber ( <t:posit> p ) return <t:number> end
1339 function posit.integer ( <t:posit> p ) return <t:integer end
1340 function posit.rounded ( <t:posit> p ) return <t:integer> end
1341 function posit.toposit ( <t:number> n ) return <t:posit> end
1342 function posit.fromposit ( <t:posit> p ) return <t:number> end
1343 \stoptyping
1344
1345 \starttyping[option=LUA]
1346 function posit.NaN ( <t:posit> p ) return <t:boolean> end
1347 function posit.NaR ( <t:posit> p ) return <t:boolean> end
1348 \stoptyping
1349
1350 Here are the logical operators:
1351
1352 \starttyping[option=LUA]
1353 function posit.bor ( <t:posit> p1, <t:posit> p2 ) return <t:posit> end
1354 function posit.bxor ( <t:posit> p1, <t:posit> p2 ) return <t:posit> end
1355 function posit.band ( <t:posit> p1, <t:posit> p2 ) return <t:posit> end
1356 \stoptyping
1357
1358 Ans shifters:
1359
1360 \starttyping[option=LUA]
1361 function posit.shift ( <t:posit> p1, <t:integer> n ) return <t:posit> end
1362 function posit.rotate ( <t:posit> p, <t:integer> n ) return <t:posit> end
1363 \stoptyping
1364
1365 There is a limited repertoire of math functions (basically what we needed for
1366 \METAPOST):
1367
1368 \starttyping[option=LUA]
1369 function posit.abs ( <t:posit> p ) return <t:posit> end
1370 function posit.conj ( <t:posit> p ) return <t:posit> end
1371 function posit.acos ( <t:posit> p ) return <t:posit> end
1372 function posit.asin ( <t:posit> p ) return <t:posit> end
1373 function posit.atan ( <t:posit> p ) return <t:posit> end
1374 function posit.ceil ( <t:posit> p ) return <t:posit> end
1375 function posit.cos ( <t:posit> p ) return <t:posit> end
1376 function posit.exp ( <t:posit> p ) return <t:posit> end
1377 function posit.exp2 ( <t:posit> p ) return <t:posit> end
1378 function posit.floor ( <t:posit> p ) return <t:posit> end
1379 function posit.log ( <t:posit> p ) return <t:posit> end
1380 function posit.log10 ( <t:posit> p ) return <t:posit> end
1381 function posit.log1p ( <t:posit> p ) return <t:posit> end
1382 function posit.log2 ( <t:posit> p ) return <t:posit> end
1383 function posit.logb ( <t:posit> p ) return <t:posit> end
1384 function posit.round ( <t:posit> p ) return <t:posit> end
1385 function posit.sin ( <t:posit> p ) return <t:posit> end
1386 function posit.sqrt ( <t:posit> p ) return <t:posit> end
1387 function posit.tan ( <t:posit> p ) return <t:posit> end
1388
1389 function posit.modf ( <t:posit> p )
1390 return
1391 <t:posit>,
1392 <t:posit>
1393 end
1394
1395 function posit.min ( <t:posit> p1, <t:posit> p2 ) return <t:posit> end
1396 function posit.max ( <t:posit> p1, <t:posit> p2 ) return <t:posit> end
1397 function posit.pow ( <t:posit> p1, <t:posit> p2 ) return <t:posit> end
1398 \stoptyping
1399
1400 % unsupported : acosh asinh atan2 atanh cbrt copysign cosh deg erf erfc expm1
1401 % fabs fdim fma fmax fmin fmod frexp gamma hypot isfinite isinf
1402 % isnan isnormal j0 j1 jn ldexp lgamma modf nearbyint nextafter rad
1403 % remainder remquo scalbn sinh tanh tgamma trunc y0 y1 yn
1404 % supported : add sub mul div unm pow / eq le lt / bor bxor band shl shr
1405 % unsupported : idiv mod
1406
1407 \stopsubsection
1408
1409 \startsubsection[title=Complex numbers]
1410
1411 \starttyping[option=LUA]
1412 function xcomplex.new ( )
1413 return <t:complex>
1414 end
1415
1416 function xcomplex.new (
1417 <t:number> re,
1418 <t:number> im
1419 )
1420 return <t:complex>
1421 end
1422 \stoptyping
1423
1424 \starttyping[option=LUA]
1425 function xcomplex.tostring ( <t:complex> z )
1426 return <t:string>
1427 end
1428 \stoptyping
1429
1430 \starttyping[option=LUA]
1431 function xcomplex.topair ( <t:complex> z )
1432 return
1433 <t:number>, -- re
1434 <t:number> -- im
1435 end
1436 \stoptyping
1437
1438 There is a bunch of functions that take a complex number:
1439
1440 \starttyping[option=LUA]
1441 function xcomplex.abs ( <t:complex> z ) return <t:complex> end
1442 function xcomplex.arg ( <t:complex> z ) return <t:complex> end
1443 function xcomplex.imag ( <t:complex> z ) return <t:complex> end
1444 function xcomplex.real ( <t:complex> z ) return <t:complex> end
1445 function xcomplex.onj ( <t:complex> z ) return <t:complex> end
1446 function xcomplex.proj ( <t:complex> z ) return <t:complex> end
1447 function xcomplex.exp ( <t:complex> z ) return <t:complex> end
1448 function xcomplex.log ( <t:complex> z ) return <t:complex> end
1449 function xcomplex.sqrt ( <t:complex> z ) return <t:complex> end
1450 function xcomplex.sin ( <t:complex> z ) return <t:complex> end
1451 function xcomplex.cos ( <t:complex> z ) return <t:complex> end
1452 function xcomplex.tan ( <t:complex> z ) return <t:complex> end
1453 function xcomplex.asin ( <t:complex> z ) return <t:complex> end
1454 function xcomplex.acos ( <t:complex> z ) return <t:complex> end
1455 function xcomplex.atan ( <t:complex> z ) return <t:complex> end
1456 function xcomplex.sinh ( <t:complex> z ) return <t:complex> end
1457 function xcomplex.cosh ( <t:complex> z ) return <t:complex> end
1458 function xcomplex.tanh ( <t:complex> z ) return <t:complex> end
1459 function xcomplex.asinh ( <t:complex> z ) return <t:complex> end
1460 function xcomplex.acosh ( <t:complex> z ) return <t:complex> end
1461 function xcomplex.atanh ( <t:complex> z ) return <t:complex> end
1462
1463 function xcomplex.pow ( <t:complex> z1, <t:complex> z2 ) return <t:complex> end
1464 \stoptyping
1465
1466 We added the \type {cerf} functions but none can wonder if we should carry that
1467 burden around (instead of just assuming a library to be used).
1468
1469 The complex error function \type {erf(z)}:
1470
1471 \starttyping[option=LUA]
1472 function cerf.erf ( <t:complex> z )
1473 return <t:complex>
1474 end
1475 \stoptyping
1476
1477 The complex complementary error function \type {erfc(z) = 1 - erf(z)}:
1478
1479 \starttyping[option=LUA]
1480 function cerf.erfc ( <t:complex> z )
1481 return <t:complex>
1482 end
1483 \stoptyping
1484
1485 The underflow-compensating function \type {erfcx(z) = exp(z^2) erfc(z)}:
1486
1487 \starttyping[option=LUA]
1488 function cerf.erfcx ( <t:complex> z )
1489 return <t:complex>
1490 end
1491 \stoptyping
1492
1493 The imaginary error function \type {erfi(z) = -i erf(iz)}:
1494
1495 \starttyping[option=LUA]
1496 function cerf.erfi ( <t:complex> z )
1497 return <t:complex>
1498 end
1499 \stoptyping
1500
1501 Dawson's integral \type {D(z) = sqrt(pi)/2 * exp(-z^2) * erfi(z)}:
1502
1503 \starttyping[option=LUA]
1504 function cerf.dawson ( <t:complex> z )
1505 return <t:complex>
1506 end
1507 \stoptyping
1508
1509 The convolution of a Gaussian and a Lorentzian:
1510
1511 \starttyping[option=LUA]
1512 function cerf.voigt (
1513 <t:number> n1,
1514 <t:number> n2,
1515 <t:number> n3
1516 )
1517 return <t:number>
1518 end
1519 \stoptyping
1520
1521 The half width at half maximum of the Voigt profile:
1522
1523 \starttyping[option=LUA]
1524 function cerf.voigt_hwhm (
1525 <t:number> n1,
1526 <t:number> n2
1527 )
1528 return <t:number>
1529 end
1530 \stoptyping
1531
1532 \stopsubsection
1533
1534 \startsubsection[title=Decimal numbers]
1535
1536 Because in \METAPOST\ we support the decimal number system, we also provide this
1537 at the \TEX\ end Apart from the usual support for operators there are some
1538 functions available.
1539
1540 \starttyping[option=LUA]
1541 function xdecimal.new ( ) return <t:decimal> end
1542 function xdecimal.new ( <t:number> n ) return <t:decimal> end
1543 function xdecimal.new ( <t:string> s ) return <t:decimal> end
1544 \stoptyping
1545
1546 \starttyping[option=LUA]
1547 function xdecimal.copy ( <t:decimal> a ) return <t:decimal> end
1548 function xdecimal.tostring ( <t:decimal> a ) return <t:string> end
1549 function xdecimal.tonumber ( <t:decimal> a ) return <t:number> end
1550 \stoptyping
1551
1552 \starttyping[option=LUA]
1553 function xdecimal.setprecision ( <t:integer> digits )
1554 --nothing to return
1555 end
1556
1557 function xdecimal.getprecision ( )
1558 return <t:integer>
1559 end
1560 \stoptyping
1561
1562 \starttyping[option=LUA]
1563 function xdecimal.bor ( <t:decimal> a, <t:decimal> b ) return <t:decimal> end
1564 function xdecimal.bxor ( <t:decimal> a, <t:decimal> b ) return <t:decimal> end
1565 function xdecimal.band ( <t:decimal> a, <t:decimal> b ) return <t:decimal> end
1566 \stoptyping
1567
1568 \starttyping[option=LUA]
1569 function xdecimal.shift ( <t:decimal> a, <t:integer> n ) return <t:decimal> end
1570 function xdecimal.rotate ( <t:decimal> a, <t:integer> n ) return <t:decimal> end
1571 \stoptyping
1572
1573 \starttyping[option=LUA]
1574 function xdecimal.abs ( <t:decimal> a ) return <t:decimal> end
1575 function xdecimal.trim ( <t:decimal> a ) return <t:decimal> end
1576 function xdecimal.conj ( <t:decimal> a ) return <t:decimal> end
1577 function xdecimal.abs ( <t:decimal> a ) return <t:decimal> end
1578 function xdecimal.sqrt ( <t:decimal> a ) return <t:decimal> end
1579 function xdecimal.ln ( <t:decimal> a ) return <t:decimal> end
1580 function xdecimal.log ( <t:decimal> a ) return <t:decimal> end
1581 function xdecimal.exp ( <t:decimal> a ) return <t:decimal> end
1582 function xdecimal.minus ( <t:decimal> a ) return <t:decimal> end
1583 function xdecimal.plus ( <t:decimal> a ) return <t:decimal> end
1584
1585 function xdecimal.min ( <t:decimal> a, <t:decimal> b ) return <t:decimal> end
1586 function xdecimal.max ( <t:decimal> a, <t:decimal> b ) return <t:decimal> end
1587 function xdecimal.pow ( <t:decimal> a, <t:decimal> b ) return <t:decimal> end
1588 \stoptyping
1589
1590 \stopsubsection
1591
1592 \startsubsection[title=Math helpers]
1593
1594 The \type {xmath} library provides function and a few constants:
1595
1596 \starttyping[option=LUA]
1597 local infinty = xmath.inf
1598 local notanumber = xmath.nan
1599 local pi = xmath.pi
1600 \stoptyping
1601
1602 There are more helpers than the average used needs. We also use these
1603 to extend the \METAPOST\ repertoire.
1604
1605 \starttyping[option=LUA]
1606 function xmath.acos ( <t:number> a ) return <t:number> end
1607 function xmath.acosh ( <t:number> a ) return <t:number> end
1608 function xmath.asin ( <t:number> a ) return <t:number> end
1609 function xmath.asinh ( <t:number> a ) return <t:number> end
1610 function xmath.atan ( <t:number> a ) return <t:number> end
1611 function xmath.atan ( <t:number> a, <t:number> b ) return <t:number> end
1612 function xmath.atan2 ( <t:number> a ) return <t:number> end
1613 function xmath.atan2 ( <t:number> a, <t:number> b ) return <t:number> end
1614 function xmath.atanh ( <t:number> a ) return <t:number> end
1615 function xmath.cbrt ( <t:number> a ) return <t:number> end
1616 function xmath.ceil ( <t:number> a ) return <t:number> end
1617 function xmath.copysign ( <t:number> a, <t:number> b ) return <t:number> end
1618 function xmath.cos ( <t:number> a ) return <t:number> end
1619 function xmath.cosh ( <t:number> a ) return <t:number> end
1620 function xmath.deg ( <t:number> a ) return <t:number> end
1621 function xmath.erf ( <t:number> a ) return <t:number> end
1622 function xmath.erfc ( <t:number> a ) return <t:number> end
1623 function xmath.exp ( <t:number> a ) return <t:number> end
1624 function xmath.exp2 ( <t:number> a ) return <t:number> end
1625 function xmath.expm1 ( <t:number> a ) return <t:number> end
1626 function xmath.fabs ( <t:number> a ) return <t:number> end
1627 function xmath.fdim ( <t:number> a, <t:number> b ) return <t:number> end
1628 function xmath.floor ( <t:number> a ) return <t:number> end
1629 function xmath.fmax ( ... ) return <t:number> end
1630 function xmath.fmin ( ... ) return <t:number> end
1631 function xmath.fmod ( <t:number> a, <t:number> b ) return <t:number> end
1632 function xmath.frexp ( <t:number> a, <t:number> b ) return <t:number> end
1633 function xmath.gamma ( <t:number> a ) return <t:number> end
1634 function xmath.hypot ( <t:number> a, <t:number> b ) return <t:number> end
1635 function xmath.isfinite ( <t:number> a ) return <t:number> end
1636 function xmath.isinf ( <t:number> a ) return <t:number> end
1637 function xmath.isnan ( <t:number> a ) return <t:number> end
1638 function xmath.isnormal ( <t:number> a ) return <t:number> end
1639 function xmath.j0 ( <t:number> a ) return <t:number> end
1640 function xmath.j1 ( <t:number> a ) return <t:number> end
1641 function xmath.jn ( <t:number> a, <t:number> b ) return <t:number> end
1642 function xmath.ldexp ( <t:number> a, <t:number> b ) return <t:number> end
1643 function xmath.lgamma ( <t:number> a ) return <t:number> end
1644 function xmath.l0 ( <t:number> a ) return <t:number> end
1645 function xmath.l1 ( <t:number> a ) return <t:number> end
1646 function xmath.ln ( <t:number> a, <t:number> b ) return <t:number> end
1647 function xmath.log ( <t:number> a [,b]) return <t:number> end
1648 function xmath.log10 ( <t:number> a ) return <t:number> end
1649 function xmath.log1p ( <t:number> a ) return <t:number> end
1650 function xmath.log2 ( <t:number> a ) return <t:number> end
1651 function xmath.logb ( <t:number> a ) return <t:number> end
1652 function xmath.modf ( <t:number> a, <t:number> b ) return <t:number> end
1653 function xmath.nearbyint ( <t:number> a ) return <t:number> end
1654 function xmath.nextafter ( <t:number> a, <t:number> b ) return <t:number> end
1655 function xmath.pow ( <t:number> a, <t:number> b ) return <t:number> end
1656 function xmath.rad ( <t:number> a ) return <t:number> end
1657 function xmath.remainder ( <t:number> a, <t:number> b ) return <t:number> end
1658 function xmath.remquo ( <t:number> a, <t:number> b ) return <t:number> end
1659 function xmath.round ( <t:number> a ) return <t:number> end
1660 function xmath.scalbn ( <t:number> a, <t:number> b ) return <t:number> end
1661 function xmath.sin ( <t:number> a ) return <t:number> end
1662 function xmath.sinh ( <t:number> a ) return <t:number> end
1663 function xmath.sqrt ( <t:number> a ) return <t:number> end
1664 function xmath.tan ( <t:number> a ) return <t:number> end
1665 function xmath.tanh ( <t:number> a ) return <t:number> end
1666 function xmath.tgamma ( <t:number> a ) return <t:number> end
1667 function xmath.trunc ( <t:number> a ) return <t:number> end
1668 function xmath.y0 ( <t:number> a ) return <t:number> end
1669 function xmath.y1 ( <t:number> a ) return <t:number> end
1670 function xmath.yn ( <t:number> a ) return <t:number> end
1671
1672 function xmath.fma (
1673 <t:number> a,
1674 <t:number> b,
1675 <t:number> c
1676 )
1677 return <t:number>
1678 end
1679 \stoptyping
1680
1681 \stopsubsection
1682
1683 \stopsection
1684
1685 \startsection[title={Optional}]
1686
1687 \startsubsection[title=Loading]
1688
1689 The optional libraries are (indeed) optional. Compilation of \LUAMETATEX\ doesn't
1690 depend on them being present. Loading (and binding) is delayed. In practice we
1691 only see a few being of interest and used, like \type {zint} for barcodes, \type
1692 {mysql} for database processing and \type {graphicmagick} for an occasional
1693 runtime conversion. Some are just there to show the principles and were used to
1694 test the interfaces and loading.
1695
1696 A library can be loaded, and thereby registered in the \quote {optional}
1697 namespace, assuming that \type {--permitloadlib} is given with:
1698
1699 \starttyping[option=LUA]
1700 function library.load (
1701 <t:string> filename,
1702 <t:string> openname,
1703 )
1704 return
1705 <t:function>, -- target
1706 <t:string> -- foundname
1707 end
1708 \stoptyping
1709
1710 but there are no guarantees that it will work.
1711
1712 \stopsubsection
1713
1714 \startsubsection[title=Management]
1715
1716 {\em Todo: something about how optionals are implemented and are supposed to work.}
1717
1718 \stopsubsection
1719
1720 \startsubsection[title=TDS (kpse)]
1721
1722 The optional \type {kpse} library deals with lookups in the \TEX\ Directory
1723 Structure and before it can be used it has to be initialized:
1724
1725 \starttyping[option=LUA]
1726 function optional.kpse.initialize ( <t:string> filename )
1727 return <t:boolean>
1728 end
1729 \stoptyping
1730
1731 By setting the program name the library knows in what namespace to resolve
1732 filenames and variables.
1733
1734 \starttyping[option=LUA]
1735 function optional.kpse.set_program_name (
1736 <t:string> binaryname,
1737 <t:string> programname
1738 )
1739 -- no return values
1740 end
1741 \stoptyping
1742
1743 The main finder has one or more arguments. When the second and later arguments
1744 can be a boolean, string or number. The boolean indicates if the file must exist.
1745 A string sets the file type and a number does the same.
1746
1747 \starttyping[option=LUA]
1748 function optional.kpse.find_file(
1749 <t:string> filename,
1750 <t:string> filetype.
1751 <t:boolean> mustexist
1752 )
1753 return <t:string>
1754 end
1755 \stoptyping
1756
1757 You can also ask for a list of found files:
1758
1759 \starttyping[option=LUA]
1760 function optional.kpse.find_files (
1761 <t:string> userpath,
1762 <t:string> filename
1763 )
1764 return <t:table>
1765 end
1766 \stoptyping
1767
1768 These return variables, values and paths:
1769
1770 \starttyping[option=LUA]
1771 function optional.kpse.expand_path ( <t:string> name ) return <t:string> end
1772 function optional.kpse.expand_var ( <t:string> name ) return <t:string> end
1773 function optional.kpse.expand_braces ( <t:string> name ) return <t:string> end
1774 function optional.kpse.var_value ( <t:string> name ) return <t:string> end
1775 \stoptyping
1776
1777 If possible this returns the (first found) filename that is readable:
1778
1779 \starttyping[option=LUA]
1780 function optional.kpse.readable_file ( <t:string> filename )
1781 return <t:string>
1782 end
1783 \stoptyping
1784
1785 The list of supported file types can be fetched with:
1786
1787 \starttyping[option=LUA]
1788 function optional.kpse.get_file_types ( )
1789 return <t:table>
1790 end
1791 \stoptyping
1792
1793 \stopsubsection
1794
1795 \startsubsection[title=Graphics]
1796
1797 \startsubsubsubject[title=ghostscript]
1798
1799 The \type {ghostscript} library has to be initialized:
1800
1801 \starttyping[option=LUA]
1802 function optional.ghostscript.initialize ( <t:string> filename )
1803 return <t:boolean>
1804 end
1805 \stoptyping
1806
1807 A conversion is executed with the following command. Here the table is a mixed
1808 list of strings and numbers that represent the otherwise command like arguments.
1809
1810 \starttyping[option=LUA]
1811 function optional.ghostscript.execute ( <t:table> )
1812 return
1813 <t:boolean>, -- success
1814 <t:string>, -- result
1815 <t:string> -- message
1816 end
1817 \stoptyping
1818
1819 \stopsubsubsubject
1820
1821 \startsubsubsubject[title=graphicsmagick]
1822
1823 The \type {graphicsmagick} library has to be initialized:
1824
1825 \starttyping[option=LUA]
1826 function optional.graphicsmagick.initialize ( <t:string> filename )
1827 return <t:boolean>
1828 end
1829 \stoptyping
1830
1831 A conversion is executed with the following command.
1832
1833 \starttyping[option=LUA]
1834 function optional.graphicsmagick.execute (
1835 {
1836 inputfilename = <t:string>,
1837 outputfilename = <t:string>,
1838 blur = {
1839 radius = <t:number>,
1840 sigma = <t:number>,
1841 },
1842 noise - {
1843 type = <t:integer>,
1844 },
1845 }
1846 )
1847 return <t:boolean>
1848 end
1849 \stoptyping
1850
1851 The noise types can be fetched with:
1852
1853 \starttyping[option=LUA]
1854 function optional.graphicsmagick.noisetypes ( )
1855 return <t:table>
1856 end
1857 \stoptyping
1858
1859 \stopsubsubsubject
1860
1861 \startsubsubsubject[title=imagemagick]
1862
1863 The \type {imagemagick} library is initialized with:
1864
1865 \starttyping[option=LUA]
1866 function optional.imagemagick.initialize ( <t:string> filename )
1867 return <t:boolean>
1868 end
1869 \stoptyping
1870
1871 After that you can execute convert commands. The options table is a sequence of
1872 strings, numbers and booleans that gets passes, in the same order, but where a
1873 boolean becomes one of the strings \type {true} or \type {false}.
1874
1875 \starttyping[option=LUA]
1876 function optional.imagemagick.execute (
1877 {
1878 inputfilename = <t:string>,
1879 outputfilename = <t:string>,
1880 options = <t:table>,
1881 }
1882 )
1883 return <t:boolean>
1884 end
1885 \stoptyping
1886
1887 \stopsubsubsubject
1888
1889 \startsubsubsubject[title=zint]
1890
1891 The \type {zint} library is initialized with:
1892
1893 \starttyping[option=LUA]
1894 function optional.zint.initialize ( <t:string> filename )
1895 return <t:boolean>
1896 end
1897 \stoptyping
1898
1899 As with the other graphic libraries we execute a command but here we implement a
1900 converter a bit more specific because we want back a result that we can handle in
1901 a combination of \TEX\ and \METAPOST.
1902
1903 \starttyping[option=LUA]
1904 function optional.zint.execute (
1905 {
1906 code = <t:integer>,
1907 text = <t:string>,
1908 option = <t:string>, -- "square"
1909 }
1910 )
1911 return <t:table>
1912 end
1913 \stoptyping
1914
1915 We get back a table that has graphic components, where each components table can
1916 zero or more subtables.
1917
1918 \starttyping[option=LUA]
1919 result = {
1920 rectangles = {
1921 { <t:integer> x, <t:integer> y, <t:integer> w, <t:integer> h }, ...
1922 },
1923 hexagons = {
1924 { <t:integer> x, <t:integer> y, <t:integer> d }, ...
1925 },
1926 circles = {
1927 { <t:integer> x, <t:integer> y, <t:nteger> d }, ...
1928 },
1929 strings = {
1930 { <t:integer> x, <t:integer> y, <t:integer> s, <t:string> t }, ...
1931 }
1932 }
1933 \stoptyping
1934
1935 \stopsubsubsubject
1936
1937 \stopsubsection
1938
1939 \startsubsection[title=Compression]
1940
1941 \startsubsubsubject[title=lz4]
1942
1943 The library is initialized with:
1944
1945 \starttyping[option=LUA]
1946 function optional.lz4.initialize ( )
1947 return <t:boolean> -- success
1948 end
1949 \stoptyping
1950
1951 There are compressors and decompressors. If you want the more efficient
1952 decompressor, make sure to save the size with the compressed stream and pass that
1953 when decompressing.
1954
1955 \starttyping[option=LUA]
1956 function optional.lz4.compress (
1957 <t:string> data,
1958 <t:integer> acceleration -- default 1
1959 )
1960 return <t:string>
1961 end
1962
1963 function optional.lz4.decompresssize (
1964 <t:string> data,
1965 <t:integer> size
1966 )
1967 return <t:string>
1968 end
1969 \stoptyping
1970
1971 These are the frame based variants:
1972
1973 \starttyping[option=LUA]
1974 function optional.lz4.framecompress ( <t:string> data )
1975 return <t:string>
1976 end
1977
1978 function optional.lz4.framedecompress ( return <t:string> )
1979 return <t:string>
1980 end
1981 \stoptyping
1982
1983 \stopsubsubsubject
1984
1985 \startsubsubsubject[title=lzma]
1986
1987 The library is initialized with:
1988
1989 \starttyping[option=LUA]
1990 function optional.lzma.initialize ( )
1991 return <t:boolean> -- success
1992 end
1993 \stoptyping
1994
1995 The compressor can take an estimated size which makes it possible to preallocate
1996 a buffer.
1997
1998 \starttyping[option=LUA]
1999 function optional.lzma.compress (
2000 <t:string> data,
2001 <t:integer> level,
2002 <t:integer> size -- estimated
2003 )
2004 return <t:string>
2005 end
2006 \stoptyping
2007
2008 The decompressor can be told what the final size is which is more efficient.
2009
2010 \starttyping[option=LUA]
2011 function optional.lzma.decompress (
2012 <t:string> data,
2013 <t:integer> size -- estimated
2014 )
2015 return <t:string>
2016 end
2017 \stoptyping
2018
2019 \stopsubsubsubject
2020
2021 \startsubsubsubject[title=lzo]
2022
2023 The library is initialized with:
2024
2025 \starttyping[option=LUA]
2026 function optional.lzo.initialize ( )
2027 return <t:boolean> -- success
2028 end
2029 \stoptyping
2030
2031 There is not much to tell about:
2032
2033 \starttyping[option=LUA]
2034 function optional.lzo.compress ( <t:string> data )
2035 return <t:string>
2036 end
2037 \stoptyping
2038
2039 and
2040
2041 \starttyping[option=LUA]
2042 function optional.lzo.decompresssize (
2043 <t:string> data,
2044 <t:integer> size
2045 )
2046 return <t:string>
2047 end
2048 \stoptyping
2049
2050 \stopsubsubsubject
2051
2052 \startsubsubsubject[title=zstd]
2053
2054 The library is initialized with:
2055
2056 \starttyping[option=LUA]
2057 function optional.zstd.initialize ( )
2058 return <t:boolean> -- success
2059 end
2060 \stoptyping
2061
2062 The compressor:
2063
2064 \starttyping[option=LUA]
2065 function optional.zstd.compress (
2066 <t:string> data,
2067 <t:integer> level
2068 )
2069 return <t:string>
2070 end
2071 \stoptyping
2072
2073 The decompressor:
2074
2075 \starttyping[option=LUA]
2076 function optional.zstd.decompress ( <t:string> data )
2077 return <t:string>
2078 end
2079 \stoptyping
2080
2081 \stopsubsubsubject
2082
2083 \stopsubsection
2084
2085 \startsubsection[title=Databases]
2086
2087 \startsubsubsubject[title=mysql]
2088
2089 We start with the usual initializer:
2090
2091 \starttyping[option=LUA]
2092 function optional.mysql.initialize ( )
2093 return <t:boolean> -- success
2094 end
2095 \stoptyping
2096
2097 Opening the database is done with:
2098
2099 \starttyping[option=LUA]
2100 function optional.mysql.open (
2101 <t:string> database,
2102 <t:string> username,
2103 <t:string> password,
2104 <t:string> host,
2105 <t:integer> port -- optional
2106 )
2107 return <t:userdata> -- instance
2108 end
2109 \stoptyping
2110
2111 The database is kept \quote {open} but can be closed with:
2112
2113 \starttyping[option=LUA]
2114 function optional.mysql.close ( <t:userdata> instance )
2115 -- no return values
2116 end
2117 \stoptyping
2118
2119 A query is executed with:
2120
2121 \starttyping[option=LUA]
2122 function optional.mysql.execute (
2123 <t:userdata> instance,
2124 <t:string> query,
2125 <t:function> callback
2126 )
2127 return <t:boolean> -- success
2128 end
2129 \stoptyping
2130
2131 The callback is a \LUA\ function that looks like this:
2132
2133 \starttyping[option=LUA]
2134 function callback(nofcolumns,values,fields)
2135 ...
2136 end
2137 \stoptyping
2138
2139 It gets called for every row of the result. The fields table is only filled the
2140 first time, if at all.
2141
2142 This interface is rather minimalistic but in \CONTEXT\ we wrap all in a more
2143 advanced setup. It's among the oldest \LUA\ code in the distribution and evolved
2144 with the possibilities (client as well as external libraries) and is quite
2145 performing also due to the use of templates, caching, built-in conversions etc.
2146
2147 If there is an error we can fetch the message with:
2148
2149 \starttyping[option=LUA]
2150 function optional.mysql.getmessage ( <t:userdata> instance )
2151 return <t:string> | <t:nil> -- last error message
2152 end
2153 \stoptyping
2154
2155 \stopsubsubsubject
2156
2157 \startsubsubsubject[title=postgress]
2158
2159 This library has the same interface as the \type {mysql} interface, so it can be
2160 used instead.
2161
2162 \stopsubsubsubject
2163
2164 \startsubsubsubject[title=sqlite]
2165
2166 This library has the same interface as the \type {mysql} interface, so it can be
2167 used instead. The only function that differs is the opener:
2168
2169 \starttyping[option=LUA]
2170 function optional.sqlite.open ( <t:string> filename )
2171 return <t:userdata> -- instance
2172 end
2173 \stoptyping
2174
2175 \stopsubsubsubject
2176
2177 \stopsubsection
2178
2179 \startsubsection[title=Whatever]
2180
2181 \startsubsubsubject[title=cerf]
2182
2183 This library is plugged in the \type {xcomplex} so there is no need to discuss it
2184 here unless we decide to move it to an optional loaded library, which might
2185 happen eventually (depends on need).
2186
2187 \stopsubsubsubject
2188
2189 \startsubsubsubject[title=curl]
2190
2191 The library is initialized with:
2192
2193 \starttyping[option=LUA]
2194 function optional.curl.initialize ( )
2195 return <t:boolean> -- success
2196 end
2197 \stoptyping
2198
2199 The fetcher stays kind of close to how the library wants it so we have no fancy
2200 interface. We have pairs where the first member is an integer indicating the
2201 option. The library only has string and integer options so booleans are effective
2202 zeros or ones. A \LUA\ boolean therefore becomes an integer.
2203
2204 \starttyping[option=LUA]
2205 function optional.curl.fetch (
2206 {
2207 <t:integer>, <t:string> | <t:integer> | <t:boolean>,
2208 ...
2209 }
2210 )
2211 end
2212 \stoptyping
2213
2214 A \URL\ can be (un)escaped:
2215
2216 \starttyping[option=LUA]
2217 function optional.curl.escape ( <t:string> data )
2218 return <t:string>
2219 end
2220
2221 function optional.curl.unescape ( <t:string> data )
2222 return <t:string>
2223 end
2224 \stoptyping
2225
2226 The current version of the library:
2227
2228 \starttyping[option=LUA]
2229 function optional.curl.getversion ( )
2230 return <t:string>
2231 end
2232 \stoptyping
2233
2234 \stopsubsubsubject
2235
2236 \startsubsubsubject[title=hb]
2237
2238 This module is mostly there to help Idris Hamid (The Oriental \TEX\ Project
2239 develop his fonts in such away that they work with other libraries (also
2240 uniscribe). We need to initialize this library with the following function. Best
2241 have the library in the \TEX\ tree because either more are present or the
2242 operating system updates them. As we don't use this in \CONTEXT\ we're also not
2243 sure of things work ok but we can assume stable interfaces anyway. See the plugin
2244 module for more info.
2245
2246 \starttyping[option=LUA]
2247 function optional.hb.initialize ( )
2248 return <t:boolean> -- success
2249 end
2250 \stoptyping
2251
2252 It probably makes sense to check for the version because (in the \TEXLIVE\ code
2253 base) it is one of the most frequently updated code bases and for \TEX\ stability
2254 and predictability (when working on a specific project) is important. When you
2255 initialize
2256
2257 \starttyping[option=LUA]
2258 function optional.hb.getversion ( )
2259 return <t:string>
2260 end
2261 \stoptyping
2262
2263 \starttyping[option=LUA]
2264 function optional.hb.getshapers ( )
2265 return <t:table> -- strings
2266 end
2267 \stoptyping
2268
2269 \starttyping[option=LUA]
2270 function optional.hb.loadfont (
2271 <t:integer> id,
2272 <t:string> name
2273 )
2274 return <t:userdata> -- instance
2275 end
2276 \stoptyping
2277
2278 A run over characters happens with the next one. You get back a list of tables
2279 that specify to be handled glyphs. The interface is pretty much the same as what
2280 Kai Eigner came up with at the time he wanted to compare the results with the
2281 regular font loader, for which the \LUATEX\ and \LUAJITTEX) \FFI\ interfaces were
2282 used.
2283
2284 \starttyping[option=LUA]
2285 function optional.hb.shapestring (
2286 <t:userdata> font,
2287 <t:string> script,
2288 <t:string> language,
2289 <t:string> direction,
2290 <t:table> shapers,
2291 <t:table> features,
2292 <t:string> text
2293 <t:boolean> reverse
2294 <t:integer> utfbits, -- default 8
2295 )
2296 return {
2297 {
2298 <t:integer>, -- codepoint
2299 <t:integer>, -- cluster
2300 <t:integer>, -- xoffset
2301 <t:integer>, -- yoffset
2302 <t:integer>, -- xadvance
2303 <t:integer>, -- uadvance
2304 },
2305 ...
2306 }
2307 end
2308 \stoptyping
2309
2310 \stopsubsubsubject
2311
2312 \startsubsubsubject[title=mujs]
2313
2314 This is just a fun experiment that permits \JAVASCRIPT\ code to be used instead of
2315 \LUA. It was actually one of the first optional libraries I played with and as
2316 with the other optionals there is a module that wraps it. The library is
2317 initialized with:
2318
2319 \starttyping[option=LUA]
2320 function optional.mujs.initialize ( )
2321 return <t:boolean> -- success
2322 end
2323 \stoptyping
2324
2325 There are a few \quote {mandate} callbacks than need to be implemented:
2326
2327 \starttyping[option=LUA]
2328 function optional.mujs.setfindfile (
2329 function ( <t:string> name )
2330 return <t:string>
2331 end
2332 )
2333 -- no return values
2334 end
2335
2336 function optional.mujs.setopenfile (
2337 function ( <t:string> name )
2338 return <t:integer> id
2339 end
2340 )
2341 -- no return values
2342 end
2343
2344 function optional.mujs.setclosefile (
2345 function ( <t:integer> id )
2346 -- no return values
2347 end
2348 )
2349 -- no return values
2350 end
2351
2352 function optional.mujs.setreadfile (
2353 function ( <t:integer> id )
2354 return <t:string> | <t:nil>
2355 end
2356 )
2357 -- no return values
2358 end
2359
2360 function optional.mujs.setseekfile (
2361 function ( <t:integer> id, <t:integer> position )
2362 return <t:integer>
2363 end
2364 )
2365 -- no return values
2366 end
2367
2368 function optional.mujs.setconsole ( )
2369 function ( <t:string> category, <t:string> message )
2370 -- no return values
2371 end
2372 )
2373 -- no return values
2374 end
2375 \stoptyping
2376
2377 The library implements a few \JAVASCRIPT\ functions, like the ones
2378 printing to \TEX, they take an optional catcodes reference:
2379
2380 \starttyping
2381 texprint (catcodes, ...)
2382 texsprint(catcodes, ...)
2383 \stoptyping
2384
2385 and a reporter:
2386
2387 \starttyping
2388 console (category, message)
2389 \stoptyping
2390
2391 The next function resets the interpreter:
2392
2393 \starttyping[option=LUA]
2394 function optional.mujs.reset ( )
2395 -- no return value
2396 end
2397 \stoptyping
2398
2399 A snippet of \JAVASCRIPT\ can be executed with:
2400
2401 \starttyping[option=LUA]
2402 function optional.mujs.execute ( <t:string> filename )
2403 -- no return value
2404 end
2405 \stoptyping
2406
2407 This loads a \JAVASCRIPT\ file:
2408
2409 \starttyping[option=LUA]
2410 function optional.mujs.dofile ( <t:string> filename )
2411 -- no return value
2412 end
2413 \stoptyping
2414
2415 \stopsubsubsubject
2416
2417 \startsubsubsubject[title=openssl]
2418
2419 We use this module for some \PDF\ features. Given the frequent updates to the
2420 (external) code base, it's for sure not something one wants in the engine. We use
2421 only a small subset of functionality. The library is initialized with:
2422
2423 \starttyping[option=LUA]
2424 function optional.openssl.initialize ( )
2425 return <t:boolean> -- success
2426 end
2427 \stoptyping
2428
2429 When signing succeeds the first return value is \type {true} and possibly there
2430 is a string as second return value. When \type {false} is returned the second
2431 argument is an error code.
2432
2433 \starttyping[option=LUA]
2434 function optional.openssl.sign (
2435 {
2436 certfile = <t:string>,
2437 datafile = <t:string>,
2438 data = <t:string>,
2439 password = <t:string>,
2440 resultfile = <t:string>,
2441 }
2442 )
2443 return
2444 <t:boolean>, -- success
2445 <t:string> | <t:integer> | <t:nil>
2446 end
2447 \stoptyping
2448
2449 Verifying needs similar data:
2450
2451 \starttyping[option=LUA]
2452 function optional.openssl.verify (
2453 {
2454 certfile - <t:string>,
2455 datafile - <t:string>,
2456 data - <t:string>,
2457 signature- <t:string>,
2458 password - <t:string>,
2459 }
2460 )
2461 return
2462 <t:boolean>, -- success
2463 <t:integer> | <t:nil>
2464 end
2465 \stoptyping
2466
2467 This needs no explanation:
2468
2469 \starttyping[option=LUA]
2470 function optional.openssl.getversion ( )
2471 return <t:integer>
2472 end
2473 \stoptyping
2474
2475 \stopsubsubsubject
2476
2477 \stopsubsection
2478
2479 \startsubsection[title=Foreign]
2480
2481 {\em Todo: something about how the foreign interface can be used (inspired by
2482 alien). Also see \typ {libs-imp-foreign.mkxl}.}
2483
2484 \stopsubsection
2485
2486 \usemodule[matrix]
2487
2488 \startsubsection[title=Vector]
2489
2490 % This chapter was written with Archive's Voleuses film music in the background. I
2491 % couldn't find the lyrics of \quote {A world that can exist} yet. And one day I
2492 % need to see that movie.
2493
2494 This module has been added for the sake of \METAPOST\ graphics. Usually we talk
2495 in terms of matrices. The code evolved from a module in \CONTEXT. There are some
2496 helpers in the \METAPOST\ library that are \quote {vector aware}. We give some
2497 examples of usage and render the matrices involved.
2498
2499 % use lua buffers
2500
2501 \startbuffer
2502 \startluacode
2503 document.v44 = vector.new {
2504 { 11, 12, 13, 14 },
2505 { 21, 22, 23, 24 },
2506 { 31, 32, 33, 34 },
2507 { 41, 42, 43, 44 }
2508 }
2509
2510 document.v14 = vector.new {
2511 { 1, 2, 3, 4 }
2512 }
2513
2514 document.v41 = vector.new {
2515 { 1 }, { 2 }, { 3 }, { 4 }
2516 }
2517
2518 document.v22 = vector.new {
2519 { 1.1 , 2.2 },
2520 { 8.8 , 9.9 }
2521 }
2522
2523 document.v33 = vector.new {
2524 { 1 , 2, 0.5 },
2525 { 3 , 4, 1.0 },
2526 { 5 , 6, 2.0 }
2527 }
2528 \stopluacode
2529 \stopbuffer
2530
2531 \typebuffer[option=TEX] \getbuffer
2532
2533 These matrices have different dimensions (rows and columns):
2534
2535 \startlinecorrection
2536 \startcombination[nx=5,ny=1,distance=2em]
2537 {\ctxlua{vector.typeset(document.v44)}} {\hbox{\tt 4 × 4}}
2538 {\ctxlua{vector.typeset(document.v14)}} {\hbox{\tt 1 × 4}}
2539 {\ctxlua{vector.typeset(document.v41)}} {\hbox{\tt 4 × 1}}
2540 {\ctxlua{vector.typeset(document.v22)}} {\hbox{\tt 2 × 2}}
2541 {\ctxlua{vector.typeset(document.v33)}} {\hbox{\tt 3 × 3}}
2542 \stopcombination
2543 \stoplinecorrection
2544
2545 If performance is important, you can avoid the tables and specify the dimensions
2546 with (optionally) a series of values.
2547
2548 \starttyping
2549 local a = vector.new(2,3, 1,2,3, 4,5,6)
2550 local b = vector.new(3,2, 1,2, 3,4, 5,6)
2551 local a = vector.new(2,2, 1,2 ,3,4)
2552 local b = vector.new(2,1, 5,6)
2553 \stoptyping
2554
2555 A vector is a user data object that supports addition, subtraction,
2556 multiplication and division. Here we show multiplication:
2557
2558 \startbuffer
2559 \startluacode
2560 document.vdemo1 = 4 * document.v41
2561 document.vdemo2 = document.v41 * 4
2562 document.vdemo3 = document.v44 * document.v41
2563 \stopluacode
2564 \stopbuffer
2565
2566 \typebuffer[option=TEX] \getbuffer
2567
2568 When two matrices are multiplied, you get a matrix product, with numbers the
2569 elements are multiplied.
2570
2571 \startlinecorrection
2572 \startcombination[nx=4,ny=1,distance=2em]
2573 {\ctxlua{vector.typeset(document.v44 )}} {}
2574 {\ctxlua{vector.typeset(document.vdemo1)}} {\hbox{\tt 4 ×}}
2575 {\ctxlua{vector.typeset(document.vdemo2)}} {\hbox{\tt × 4}}
2576 {\ctxlua{vector.typeset(document.vdemo3)}} {\hbox{\tt × }}
2577 \stopcombination
2578 \stoplinecorrection
2579
2580 In addition to \type {+} (\type {add}), \type {-} (\type {sub}), \type {*} (\type
2581 {mul}) and \type {/} (\type {div}) you can use the unary minus \type {-} (\type
2582 {negate}) and test for equality with \type {=} (\type {equal}). The concat
2583 operator \type {..} (\type {concat}) is also supported.
2584
2585 The functions applied to a vector return a new vector. In most cases they also
2586 accept a table and then convert that on the fly to a vector. The \type {copy}
2587 function can be used to make a copy but is not really needed.
2588
2589 \startbuffer
2590 \startluacode
2591 document.v1 = vector.new { { 1, 2 }, { 3, 4 } }
2592 document.v2 = document.v1
2593 document.v3 = vector.copy(document.v1)
2594 \stopluacode
2595 \stopbuffer
2596
2597 \typebuffer[option=TEX] \getbuffer
2598
2599 Here the two assignments to \type {v2} and \type {v3} are apparently
2600 equivalent but this is what we really have:
2601
2602 \startlines
2603 \cldcontext{tostring(document.v1)}
2604 \cldcontext{tostring(document.v2)}
2605 \cldcontext{tostring(document.v3)}
2606 \stoplines
2607
2608 \startbuffer
2609 \startluacode
2610 vector.set (document.v1, 4, 100)
2611 vector.set (document.v2, 4, 200)
2612 vector.set (document.v3, 4, 300)
2613 vector.setrc(document.v1, 1, 1, 400)
2614 vector.setrc(document.v2, 1, 1, 500)
2615 vector.setrc(document.v3, 1, 1, 600)
2616 \stopluacode
2617 \stopbuffer
2618
2619 \typebuffer[option=TEX] \getbuffer
2620
2621 The copy actually is a copy while the assignment is an alias, as with tables. The
2622 only time that this shows is when we do in place assignments as here.
2623
2624 \startlinecorrection
2625 \startcombination[nx=2,ny=3,distance=2em]
2626 {\ctxlua{vector.typeset(document.v1)}} {} {\hbox{\tt \cldcontext{tostring(document.v1)}}} {}
2627 {\ctxlua{vector.typeset(document.v2)}} {} {\hbox{\tt \cldcontext{tostring(document.v2)}}} {}
2628 {\ctxlua{vector.typeset(document.v3)}} {} {\hbox{\tt \cldcontext{tostring(document.v3)}}} {}
2629 \stopcombination
2630 \stoplinecorrection
2631
2632 There are also \type {get} and \type {getrc}. The reason why we have \type {set}
2633 and \type {get} is that they are equivalent to the direct accessors (using \type
2634 {[]}).
2635
2636 \startbuffer
2637 \startluacode
2638 document.v1[3] = 2 * document.v1[3]
2639 document.v2[3] = 2 * document.v2[3]
2640 document.v3[3] = 2 * document.v3[3]
2641 \stopluacode
2642 \stopbuffer
2643
2644 \typebuffer[option=TEX] \getbuffer
2645
2646 Watch how we multiply the shared vector object twice:
2647
2648 \startlinecorrection % idem as above
2649 \startcombination[nx=2,ny=3,distance=2em]
2650 {\ctxlua{vector.typeset(document.v1)}} {} {\hbox{\tt \cldcontext{tostring(document.v1)}}} {}
2651 {\ctxlua{vector.typeset(document.v2)}} {} {\hbox{\tt \cldcontext{tostring(document.v2)}}} {}
2652 {\ctxlua{vector.typeset(document.v3)}} {} {\hbox{\tt \cldcontext{tostring(document.v3)}}} {}
2653 \stopcombination
2654 \stoplinecorrection
2655
2656 \startbuffer
2657 \startluacode
2658 document.vdemo1 = vector.round (document.v22)
2659 document.vdemo2 = vector.floor (document.v22)
2660 document.vdemo3 = vector.ceiling(document.v22)
2661 \stopluacode
2662 \stopbuffer
2663
2664 \typebuffer[option=TEX] \getbuffer
2665
2666 There are various operations on vectors and you get back a new one; we don't
2667 replace in the passed vectors.
2668
2669 \startlinecorrection
2670 \startcombination[nx=4,ny=1,distance=2em]
2671 {\ctxlua{vector.typeset(document.v22 )}} {}
2672 {\ctxlua{vector.typeset(document.vdemo1)}} {\hbox{\type {round}}}
2673 {\ctxlua{vector.typeset(document.vdemo2)}} {\hbox{\type {floor}}}
2674 {\ctxlua{vector.typeset(document.vdemo3)}} {\hbox{\type {ceiling}}}
2675 \stopcombination
2676 \stoplinecorrection
2677
2678 \startbuffer
2679 \startluacode
2680 document.vdemo1 = vector.identity(3)
2681 document.vdemo2 = vector.identity(4)
2682 document.vdemo3 = vector.identity(2)
2683 \stopluacode
2684 \stopbuffer
2685
2686 The identity matrix can be created with:
2687
2688 \typebuffer[option=TEX] \getbuffer
2689
2690 \startlinecorrection
2691 \startcombination[nx=3,ny=1,distance=2em]
2692 {\ctxlua{vector.typeset(document.vdemo1)}} {\hbox{\type {3 × 3}}}
2693 {\ctxlua{vector.typeset(document.vdemo2)}} {\hbox{\type {4 × 4}}}
2694 {\ctxlua{vector.typeset(document.vdemo3)}} {\hbox{\type {2 × 2}}}
2695 \stopcombination
2696 \stoplinecorrection
2697
2698 \startbuffer
2699 \startluacode
2700 document.vdemo1 = vector.transpose(document.v44)
2701 document.vdemo2 = vector.swap(document.v41)
2702 document.vdemo3 = vector.swap(document.v14)
2703 \stopluacode
2704 \stopbuffer
2705
2706 \typebuffer[option=TEX] \getbuffer
2707
2708 Transposing has a simple companion that makes a vector with a single row into one
2709 with a single column and vise versa.
2710
2711 \startlinecorrection
2712 \startcombination[nx=6,ny=1,distance=2em]
2713 {\ctxlua{vector.typeset(document.v44 )}} {}
2714 {\ctxlua{vector.typeset(document.vdemo1)}} {\hbox{\tt transpose}}
2715 {\ctxlua{vector.typeset(document.v41 )}} {}
2716 {\ctxlua{vector.typeset(document.vdemo2)}} {\hbox{\tt swap}}
2717 {\ctxlua{vector.typeset(document.v14 )}} {}
2718 {\ctxlua{vector.typeset(document.vdemo3)}} {\hbox{\tt swap}}
2719 \stopcombination
2720 \stoplinecorrection
2721
2722 \startbuffer
2723 \startluacode
2724 document.vdemo1 = vector.inverse (document.v33)
2725 document.vdemo2 = vector.rowechelon(document.v33,true)
2726 document.vdemo3 = vector.rowechelon(document.v33)
2727 \stopluacode
2728 \stopbuffer
2729
2730 \typebuffer[option=TEX] \getbuffer
2731
2732 \startlinecorrection
2733 \startcombination[nx=5,ny=1,distance=2em]
2734 {\ctxlua{vector.typeset(document.v33 )}} {\hbox {v33}}
2735 {\ctxlua{vector.typeset(document.vdemo1)}} {\hbox {inverse}}
2736 {\ctxlua{vector.typeset(document.vdemo1*document.v33)}} {\hbox {verified}}
2737 {\ctxlua{vector.typeset(document.vdemo2)}} {\hbox {row echelon}}
2738 {\ctxlua{vector.typeset(document.vdemo3)}} {\hbox {reduced echelon}}
2739 \stopcombination
2740 \stoplinecorrection
2741
2742 The first one normalize each column vector in a matrix. The other one homogenize,
2743 dividing every entry in a row by the last element in the row, given that it is
2744 non-zero.
2745
2746 \startbuffer
2747 \startluacode
2748 document.vdemo1 = vector.normalize (document.v33)
2749 document.vdemo2 = vector.homogenize(document.v33)
2750 \stopluacode
2751 \stopbuffer
2752
2753 \typebuffer[option=TEX] \getbuffer
2754
2755 \startlinecorrection
2756 \startcombination[nx=3,ny=1,distance=2em]
2757 {\ctxlua{vector.typeset(document.v33 )}} {}
2758 {\ctxlua{vector.typeset(document.vdemo1)}} {}
2759 {\ctxlua{vector.typeset(document.vdemo2)}} {}
2760 \stopcombination
2761 \stoplinecorrection
2762
2763 Internally we use a criterium for determining if we have a zero entry:
2764 \cldcontext {vector.getepsilon()} that can be queried by \type {getepsilon}. The
2765 \type {truncate} function will reduce numbers outside this interval to zero. The
2766 \type {iszero} function takes a number and returns \type {true} when its within
2767 these bounds.
2768
2769 \startbuffer
2770 \startluacode
2771 document.vdemo0 = vector.new {
2772 { 0.00000005, 0.001 },
2773 { 0.001, 0.00000001 },
2774 }
2775
2776 document.vdemo1 = vector.truncate(document.vdemo0)
2777 \stopluacode
2778 \stopbuffer
2779
2780 \typebuffer[option=TEX] \getbuffer
2781
2782 \startlinecorrection
2783 \startcombination[nx=2,ny=1,distance=2em]
2784 {\ctxlua{vector.typeset(document.vdemo0, { template = 8 } )}} {}
2785 {\ctxlua{vector.typeset(document.vdemo1, { template = 3 } )}} {}
2786 \stopcombination
2787 \stoplinecorrection
2788
2789 The \type {determinant} function return a number so we don't show this here but
2790 we do mention the various products. These are optimized for the dimensions.
2791
2792 We feed \typ {inner} with two vectors (row or column) and get the inner product
2793 of them in return.
2794
2795 \startbuffer
2796 \startluacode
2797 document.num0 = vector.inner(document.v41,document.v41)
2798 document.num1 = vector.inner(document.v14,document.v14)
2799 \stopluacode
2800 \stopbuffer
2801
2802 \typebuffer[option=TEX] \getbuffer
2803
2804 \startlinecorrection
2805 \startcombination[nx=2,ny=1,distance=2em]
2806 {\ctxlua{vector.typeset(document.num0)}} {}
2807 {\ctxlua{vector.typeset(document.num1)}} {}
2808 \stopcombination
2809 \stoplinecorrection
2810
2811 The \typ {crossproduct} needs two 3-vectors, and returns one 3-vector. They can
2812 either be row or column vectors, and we get the same type in return.
2813
2814 \startbuffer
2815 \startluacode
2816 document.cvec0 = vector.crossproduct(vector.new {{1,2,3}}, vector.new {{4,5,6}})
2817 document.cvec1 = vector.crossproduct(vector.new {{1},{2},{3}}, vector.new {{4},{5},{6}})
2818 \stopluacode
2819 \stopbuffer
2820
2821 \typebuffer[option=TEX] \getbuffer
2822
2823 \startlinecorrection
2824 \startcombination[nx=2,ny=1,distance=2em]
2825 {\ctxlua{vector.typeset(document.cvec0)}} {}
2826 {\ctxlua{vector.typeset(document.cvec1)}} {}
2827 \stopcombination
2828 \stoplinecorrection
2829
2830 % \startlines \em
2831 % MS todo: inner
2832 % MS todo: product
2833 % MS todo: crossproduct
2834 % \stoplines
2835
2836 \startbuffer
2837 \startluacode
2838 document.vdemo0 = vector.new {
2839 { 11, 12, 13, 14 },
2840 { 21, 22, 23, 24 },
2841 { 31, 32, 33, 34 },
2842 { 41, 42, 43, 44 },
2843 }
2844
2845 document.vdemo1 = vector.slice(document.vdemo0,2,2,1,1)
2846 document.vdemo2 = vector.slice(document.vdemo0,2,2,2,2)
2847 document.vdemo3 = vector.slice(document.vdemo0,2,2,3,3)
2848 \stopluacode
2849 \stopbuffer
2850
2851 \typebuffer[option=TEX] \getbuffer
2852
2853 \startlinecorrection
2854 \startcombination[nx=4,ny=1,distance=2em]
2855 {\ctxlua{vector.typeset(document.vdemo0)}} {}
2856 {\ctxlua{vector.typeset(document.vdemo1)}} {}
2857 {\ctxlua{vector.typeset(document.vdemo2)}} {}
2858 {\ctxlua{vector.typeset(document.vdemo3)}} {}
2859 \stopcombination
2860 \stoplinecorrection
2861
2862 \startbuffer
2863 \startluacode
2864 document.vdemo1 = vector.delete(document.vdemo0,2,2)
2865 document.vdemo2 = vector.delete(document.vdemo0,2,1)
2866 document.vdemo3 = vector.delete(document.vdemo0,2,2,true)
2867 document.vdemo4 = vector.delete(document.vdemo0,2,1,true)
2868 \stopluacode
2869 \stopbuffer
2870
2871 \typebuffer[option=TEX] \getbuffer
2872
2873 \startlinecorrection
2874 \startcombination[nx=7,ny=1,distance=2em]
2875 {\ctxlua{vector.typeset(document.vdemo0)}} {}
2876 {\ctxlua{vector.typeset(document.vdemo1)}} {}
2877 {\ctxlua{vector.typeset(document.vdemo2)}} {}
2878 {\ctxlua{vector.typeset(document.vdemo3)}} {}
2879 {\ctxlua{vector.typeset(document.vdemo4)}} {}
2880 \stopcombination
2881 \stoplinecorrection
2882
2883 \startbuffer
2884 \startluacode
2885 document.vdemo1 = vector.remove(document.vdemo0,2,2)
2886 document.vdemo2 = vector.remove(document.vdemo0,2,1)
2887 document.vdemo3 = vector.remove(document.vdemo0,2,2,true)
2888 document.vdemo4 = vector.remove(document.vdemo0,2,1,true)
2889 \stopluacode
2890 \stopbuffer
2891
2892 \typebuffer[option=TEX] \getbuffer
2893
2894 \startlinecorrection
2895 \startcombination[nx=5,ny=1,distance=2em]
2896 {\ctxlua{vector.typeset(document.vdemo0)}} {}
2897 {\ctxlua{vector.typeset(document.vdemo1)}} {}
2898 {\ctxlua{vector.typeset(document.vdemo2)}} {}
2899 {\ctxlua{vector.typeset(document.vdemo3)}} {}
2900 {\ctxlua{vector.typeset(document.vdemo4)}} {}
2901 \stopcombination
2902 \stoplinecorrection
2903
2904 \startbuffer
2905 \startluacode
2906 document.vdemo0 = vector.new {
2907 { 11, 12, 13, 14 },
2908 { 21, 22, 23, 24 },
2909 { 31, 32, 33, 34 },
2910 { 41, 42, 43, 44 },
2911 }
2912
2913 document.vdemo0r = vector.new {
2914 { 101, 201, 301, 401 },
2915 { 102, 202, 302, 402 },
2916 }
2917
2918 document.vdemo0c = vector.new {
2919 { 101, 201 },
2920 { 102, 202 },
2921 { 103, 203 },
2922 { 104, 204 },
2923 }
2924
2925 document.vdemo0rc = vector.new {
2926 { 101, 201 },
2927 { 102, 202 },
2928 }
2929 \stopluacode
2930 \stopbuffer
2931
2932 \typebuffer[option=TEX] \getbuffer
2933
2934 \startlinecorrection
2935 \startcombination[nx=4,ny=1,distance=2em]
2936 {\ctxlua{vector.typeset(document.vdemo0)}} {}
2937 {\ctxlua{vector.typeset(document.vdemo0r)}} {}
2938 {\ctxlua{vector.typeset(document.vdemo0c)}} {}
2939 {\ctxlua{vector.typeset(document.vdemo0rc)}} {}
2940 \stopcombination
2941 \stoplinecorrection
2942
2943 \startbuffer
2944 \startluacode
2945 document.vdemo1 = vector.insert(document.vdemo0,document.vdemo0c,0)
2946 document.vdemo2 = vector.insert(document.vdemo0,document.vdemo0c,1)
2947 document.vdemo3 = vector.insert(document.vdemo0,document.vdemo0c,2)
2948 document.vdemo4 = vector.insert(document.vdemo0,document.vdemo0c,3)
2949 document.vdemo5 = vector.insert(document.vdemo0,document.vdemo0c,4)
2950 \stopluacode
2951 \stopbuffer
2952
2953 \typebuffer[option=TEX] \getbuffer
2954
2955 \startlinecorrection
2956 \startcombination[nx=5,ny=1,distance=2em]
2957 {\ctxlua{vector.typeset(document.vdemo1)}} {}
2958 {\ctxlua{vector.typeset(document.vdemo2)}} {}
2959 {\ctxlua{vector.typeset(document.vdemo3)}} {}
2960 {\ctxlua{vector.typeset(document.vdemo4)}} {}
2961 {\ctxlua{vector.typeset(document.vdemo5)}} {}
2962 \stopcombination
2963 \stoplinecorrection
2964
2965 \startbuffer
2966 \startluacode
2967 document.vdemo1 = vector.insert(document.vdemo0,document.vdemo0r,0,true)
2968 document.vdemo2 = vector.insert(document.vdemo0,document.vdemo0r,1,true)
2969 document.vdemo3 = vector.insert(document.vdemo0,document.vdemo0r,2,true)
2970 document.vdemo4 = vector.insert(document.vdemo0,document.vdemo0r,3,true)
2971 document.vdemo5 = vector.insert(document.vdemo0,document.vdemo0r,4,true)
2972 \stopluacode
2973 \stopbuffer
2974
2975 \typebuffer[option=TEX] \getbuffer
2976
2977 \startlinecorrection
2978 \startcombination[nx=5,ny=1,distance=2em]
2979 {\ctxlua{vector.typeset(document.vdemo1)}} {}
2980 {\ctxlua{vector.typeset(document.vdemo2)}} {}
2981 {\ctxlua{vector.typeset(document.vdemo3)}} {}
2982 {\ctxlua{vector.typeset(document.vdemo4)}} {}
2983 {\ctxlua{vector.typeset(document.vdemo5)}} {}
2984 \stopcombination
2985 \stoplinecorrection
2986
2987 \startbuffer
2988 \startluacode
2989 document.vdemo1 = vector.replace(document.vdemo0,document.vdemo0rc,1,1)
2990 document.vdemo2 = vector.replace(document.vdemo0,document.vdemo0rc,2,2)
2991 document.vdemo3 = vector.replace(document.vdemo0,document.vdemo0rc,3,3)
2992 \stopluacode
2993 \stopbuffer
2994
2995 \typebuffer[option=TEX] \getbuffer
2996
2997 \startlinecorrection
2998 \startcombination[nx=3,ny=1,distance=2em]
2999 {\ctxlua{vector.typeset(document.vdemo1)}} {}
3000 {\ctxlua{vector.typeset(document.vdemo2)}} {}
3001 {\ctxlua{vector.typeset(document.vdemo3)}} {}
3002 \stopcombination
3003 \stoplinecorrection
3004
3005 \startbuffer
3006 \startluacode
3007 document.vdemo1 = vector.exchange(document.vdemo0,1,1)
3008 document.vdemo2 = vector.exchange(document.vdemo0,1,2,true)
3009 \stopluacode
3010 \stopbuffer
3011
3012 \typebuffer[option=TEX] \getbuffer
3013
3014 \startlinecorrection
3015 \startcombination[nx=3,ny=1,distance=2em]
3016 {\ctxlua{vector.typeset(document.vdemo1)}} {}
3017 {\ctxlua{vector.typeset(document.vdemo2)}} {}
3018 \stopcombination
3019 \stoplinecorrection
3020
3021 This leaves a couple of auxiliary function that we just mention:
3022
3023 \starttabulate[|Tl|Tl|l|]
3024 \NC isvector \NC (v) \NC return \type{true} when the argument is a vector \NC \NR
3025 \NC onerow \NC (n,...,m) \NC a fast way to create a single row vector \NC \NR
3026 \NC onecolumn \NC (n,...,m) \NC a fast way to create a single column vector \NC \NR
3027 \NC type \NC (v) \NC return the string \type {vector} if we pass one \NC \NR
3028 \NC tostring \NC (v) \NC returns the dimensions and pointer value \NC \NR
3029 \NC totable \NC (v) \NC returns a regular table representation \NC \NR
3030 \NC determinant \NC (v) \NC returns the determinant \NC \NR
3031 \NC issingular \NC (v,[n]) \NC returns true if the determinant is \m{< 0.001}\NC \NR
3032 %NC gettype \NC (v) \NC returns the type of the vector (experimental) \NC \NR
3033 \NC setstacking \NC (v,n) \NC set the stacking property (\METAPOST) \NC \NR
3034 \NC getstacking \NC (v) \NC get the stacking property (\METAPOST) \NC \NR
3035 \NC getdimensions \NC (v) \NC returns the number of rows and columns \NC \NR
3036 \stoptabulate
3037
3038 We already mentioned that often you can also provide a table instead
3039 of a vector:
3040
3041 \startbuffer
3042 \startluacode
3043 document.t1 = {
3044 { 1, 2, 3 },
3045 { 1, 2, 3 },
3046 { 1, 2, 3 },
3047 }
3048
3049 document.t2 = {
3050 { 11, 2, 3 },
3051 { 1, 12, 3 },
3052 { 1, 2, 13 },
3053 }
3054
3055 document.v1 = vector.new(document.t1)
3056 document.v2 = vector.new(document.t2)
3057
3058 document.v1t1 = vector.product(document.v1,document.t2)
3059 document.t1v1 = vector.product(document.t1,document.v1)
3060 document.v1v2 = vector.product(document.v1,document.v2)
3061 document.t1t2 = vector.product(document.t1,document.t2)
3062 \stopluacode
3063 \stopbuffer
3064
3065 \typebuffer[option=TEX] \getbuffer
3066
3067 \startlinecorrection
3068 \startcombination[nx=4,ny=1,distance=2em]
3069 {\ctxlua{vector.typeset(document.v1t1)}} {\type{v t}}
3070 {\ctxlua{vector.typeset(document.t1v1)}} {\type{t v}}
3071 {\ctxlua{vector.typeset(document.v1v2)}} {\type{v v}}
3072 {\ctxlua{vector.typeset(document.t1t2)}} {\type{t t}}
3073 \stopcombination
3074 \stoplinecorrection
3075
3076 When you define and fill a vector, you can use the setters mentioned before. Here
3077 we show a few ways, assuming that \type {ns} and \type {nt} are known and \type
3078 {calulate} does some useful magic.
3079
3080 \starttyping[option=LUA]
3081 local t = { } -- we could preallocate
3082
3083 for s = 0, ns do
3084 for t = 0, nt do
3085 local x, y, z = calculate(s,t)
3086 t[#t+1] = { x, y, z, 1 }
3087 end
3088 end
3089
3090 local v = newvector(t)
3091 \stoptyping
3092
3093 We can avoid the fourth entry which is always~0 which gives smaller
3094 intermediate tables and just append these ones later using
3095 the \type {append} function.
3096
3097 \starttyping[option=LUA]
3098 local t = { } -- we could preallocate
3099
3100 for s = 0, ns do
3101 for t = 0, nt do
3102 local x, y, z = calculate(s,t)
3103 t[#t+1] = { x, y, z }
3104 end
3105 end
3106
3107 local v = newvector(t)
3108 v = vector.append(v,1)
3109 \stoptyping
3110
3111 We can avoid the \LUA\\ table completely and just populate the
3112 vector as we go. The \type {setnext} does the trick:
3113
3114 \starttyping[option=LUA]
3115 local setnext = vector.setnext
3116
3117 local v = newvector((ns + 1) * (nt + 1), 4)
3118 for s = 0, ns do
3119 for t = 0, nt do
3120 local x, y, z = calculate(s,t)
3121 setnext(v, x, y, z, 1)
3122 end
3123 end
3124 \stoptyping
3125
3126 That last variant is the most natural and also the most efficient approach but
3127 taste can differ.
3128
3129 \stopsubsection
3130
3131 \stopsection
3132
3133 \stopdocument
3134
3135 % Initial chapter documenting moment, musical timstamp: The Warning - You Oughta
3136 % Know - (Alanis Morissette) Cover, 2024, plus some energetic live shows on screen ...
3137 %
3138 % Coincidentally, at that time (October 30, 2024), after finishing this chapter we
3139 % had exactly 600 pages, but still plenty to go.