summaryrefslogtreecommitdiff
path: root/doc/context/sources/general/manuals/swiglib/swiglib-mkiv.tex
blob: fc7a269bb5d4f70dc28d0333f13b111ef0c4b9b2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
% language=uk

% author  : Hans Hagen, PRAGMA ADE, NL
% license : Creative Commons, Attribution-NonCommercial-ShareAlike 3.0 Unported

\usemodule[art-01,abr-02]

\definecolor
  [maincolor]
  [r=.4]

\definecolor
  [extracolor]
  [g=.4]

\setupbodyfont
  [11pt]

\setuptype
  [color=maincolor]

\setuptyping
  [color=maincolor]

\definefontsynonym
  [TitlePageMono]
  [file:lmmonoproplt10-bold*default]

\setuphead
  [color=maincolor]

\usesymbols
  [cc]

\setupinteraction
  [hidden]

\loadfontgoodies[lm]

\startdocument
  [metadata:author=Hans Hagen,
   metadata:title=SwigLib basics,
   author=Hans Hagen,
   affiliation=PRAGMA ADE,
   location=Hasselt NL,
   title=SwigLib basics,
   support=www.contextgarden.net,
   website=www.pragma-ade.nl]

\startluasetups[swiglib]
    for i=1,640 do
        context.definedfont { string.formatters["TitlePageMono at %p"](65536*(10+math.random(5))) }
        context("SwigLib ")
    end
    context.removeunwantedspaces()
\stopluasetups

\startMPpage

StartPage ;

    fill Page enlarged 1cm withcolor \MPcolor{extracolor} ;

    draw textext("\framed[loffset=2pt,roffset=2pt,frame=off,width=\paperwidth,align={normal,paragraph,verytolerant,stretch}]{\luasetup{swiglib}}")
        xysized (PaperWidth,PaperHeight)
        shifted center Page
        withcolor .8white ;

    draw textext.ulft("\definedfont[TitlePageMono]basics")
        xsized .75PaperWidth
        shifted lrcorner Page
        shifted (-1cm,2cm)
        withcolor \MPcolor{maincolor} ;

%     draw textext.ulft("\definedfont[TitlePageMono]in context mkiv")
%         xsized .6PaperWidth
%         shifted lrcorner Page
%         shifted (-1cm,6cm)
%         withcolor \MPcolor{maincolor} ;

StopPage ;

\stopMPpage

\dontcomplain

\startsubject[title=Contents]

\placelist[section][alternative=a]

\stopsubject

\startsection[title=Introduction]

The \SWIGLIB\ project is related to \LUATEX\ and aims as adding portable library
support to this \TEX\ engine without too much fixed binding. The project does not
provide \LUA\ code, unless really needed, because it assumes that macro packages
have different demands. It also fits in the spirit of \TEX\ and \LUA\ to minimize
the core components.

The technical setup is by Luigi Scarso and documentation about how to build the
libraries is part of the \SWIGLIB\ repository. Testing happens with help of the
\CONTEXT\ (garden) infrastructure. This short document only deals with usage in
\CONTEXT\ but also covers rather plain usage.

\blank \start \em todo: reference to Luigi's manual \stop \blank

\stopsection

\startsection[title=Inside \CONTEXT]

The recommended way to load a library in \CONTEXT\ is by using the
\type {swiglib} function. This function lives in the global namespace.

\starttyping
local gm = swiglib("gmwand.core")
\stoptyping

After this call you have the functionality available in the \type {gm}
namespace. This way of loading makes \CONTEXT\ aware that such a library
has been loading and it will report the loaded libraries as part of the
statistics.

If you want, you can use the more ignorant \type {require} instead but in
that case you need to be more explicit.

\starttyping
local gm = require("swiglib.gmwand.core")
\stoptyping

Here is an example of using such a library (by Luigi):

\startbuffer
\startluacode
local gm       = swiglib("gmwand.core")
local findfile = resolvers.findfile

gm.InitializeMagick(".")

local magick_wand  = gm.NewMagickWand()
local drawing_wand = gm.NewDrawingWand()
local pixel_wand   = gm.NewPixelWand();

gm.MagickSetSize(magick_wand,800,600)
gm.MagickReadImage(magick_wand,"xc:gray")

gm.DrawPushGraphicContext(drawing_wand)

gm.DrawSetFillColor(drawing_wand,pixel_wand)

gm.DrawSetFont(drawing_wand,findfile("dejavuserifbold.ttf"))
gm.DrawSetFontSize(drawing_wand,96)
gm.DrawAnnotation(drawing_wand,200,200,"ConTeXt 1")

gm.DrawSetFont(drawing_wand,findfile("texgyreschola-bold.otf"))
gm.DrawSetFontSize(drawing_wand,78)
gm.DrawAnnotation(drawing_wand,250,300,"ConTeXt 2")

gm.DrawSetFont(drawing_wand,findfile("lmroman10-bold.otf"))
gm.DrawSetFontSize(drawing_wand,48)
gm.DrawAnnotation(drawing_wand,300,400,"ConTeXt 3")

gm.DrawPopGraphicContext(drawing_wand)

gm.MagickDrawImage(magick_wand,drawing_wand)

gm.MagickWriteImages(magick_wand,"./swiglib-mkiv-gm-1.png",1)
gm.MagickWriteImages(magick_wand,"./swiglib-mkiv-gm-1.jpg",1)
gm.MagickWriteImages(magick_wand,"./swiglib-mkiv-gm-1.pdf",1)

gm.DestroyDrawingWand(drawing_wand)
gm.DestroyPixelWand(pixel_wand)
gm.DestroyMagickWand(magick_wand)
\stopluacode
\stopbuffer

\typebuffer \getbuffer

In practice you will probably stay away from manipulating text this way, but it
illustrates that you can use the regular \CONTEXT\ helpers to locate files.

\startlinecorrection[big]
    \startcombination[3*1]
        {\externalfigure[swiglib-mkiv-gm-1.png][width=.3\textwidth]} {png}
        {\externalfigure[swiglib-mkiv-gm-1.pdf][width=.3\textwidth]} {pdf}
        {\externalfigure[swiglib-mkiv-gm-1.jpg][width=.3\textwidth]} {jpg}
    \stopcombination
\stoplinecorrection

You'd better make sure to use unique filenames for such graphics. Of course a more
clever mechanism would only run time consuming tasks once for each iteration of a
document.

\stopsection

\startsection[title=Outside \CONTEXT]

In the \CONTEXT\ distribution we ship some generic macros and code for usage in
plain \TEX\ but there is no reason why they shouldn't work in other macro packages
as well. A rather plain example is this:

\starttyping
\input luatex-swiglib.tex

\directlua {
    dofile("luatex-swiglib-test.lua")
}

\pdfximage {luatex-swiglib-test.jpg} \pdfrefximage\pdflastximage

\end
\stoptyping

Assuming that you made the \type {luatex-plain} format, such a file can be processed using:

\starttyping
luatex --fmt=luatex=plain luatex-swiglib-test.tex
\stoptyping

The loaded \LUA\ file \type {luatex-swiglib-test.lua} liike like this:

\starttyping
local gm = swiglib("gmwand.core")

gm.InitializeMagick(".")

local magick_wand  = gm.NewMagickWand()
local drawing_wand = gm.NewDrawingWand()

gm.MagickSetSize(magick_wand,800,600)
gm.MagickReadImage(magick_wand,"xc:red")
gm.DrawPushGraphicContext(drawing_wand)
gm.DrawSetFillColor(drawing_wand,gm.NewPixelWand())
gm.DrawPopGraphicContext(drawing_wand)
gm.MagickDrawImage(magick_wand,drawing_wand)
gm.MagickWriteImages(magick_wand,"./luatex-swiglib-test.jpg",1)

gm.DestroyDrawingWand(drawing_wand)
gm.DestroyMagickWand(magick_wand)
\stoptyping

Instead of loading a library with the \type {swiglib} function, you can also
use \type {require}:

\starttyping
local gm = require("swiglib.gmwand.core")
\stoptyping

Watch the explicit \type {swiglib} reference. Both methods are equivalent.

\stopsection

\startsection[title={The libraries}]

Most libraries are small but some can be rather large and have additional files.
This is why we keep them separated. On my system they are collected in the
platform binary tree:

\starttyping
e:/tex-context/tex/texmf-mswin/bin/lib/luatex/lua/swiglib/gmwand
e:/tex-context/tex/texmf-mswin/bin/lib/luatex/lua/swiglib/mysql
e:/tex-context/tex/texmf-mswin/bin/lib/luatex/lua/swiglib/....
\stoptyping

One can modulate on this:

\starttyping
...tex/texmf-mswin/bin/lib/luatex/lua/swiglib/mysql/core.dll
...tex/texmf-mswin/bin/lib/luajittex/lua/swiglib/mysql/core.dll
...tex/texmf-mswin/bin/lib/luatex/context/lua/swiglib/mysql/core.dll
\stoptyping

are all valid. When versions are used you can provide an additional argument to the
\type {swiglib} loader:

\starttyping
tex/texmf-mswin/bin/lib/luatex/lua/swiglib/mysql/5.6/core.dll
\stoptyping

This works with:

\starttyping
local mysql = swiglib("mysql.core","5.6")
\stoptyping

as well as:

\starttyping
local mysql = swiglib("mysql.core")
\stoptyping

It is hard to predict how operating systems look up libraries and especially
nested loads, but as long as the root of the \type {swiglib} path is known to the
file search routine. We've kept the main conditions for success simple: the core
library is called \type {core.dll} or \type {core.so}. Each library has an
(automatically called) initialize function named \type {luaopen_core}. There is no
reason why (sym)links from the \type {swiglib} path to someplace else shouldn't
work.

In \type {texmfcnf.lua} you will find an entry like:

\starttyping
CLUAINPUTS = ".;$SELFAUTOLOC/lib/{$engine/context,$engine}/lua//"
\stoptyping

Which in practice boils down to a search for \type {luatex} or \type {luajittex}
specific libraries. When both binaries are compatible and there are no \type
{luajittex} binaries, the regular \type {luatex} libraries will be used.

The \type {swiglib} loader function mentioned in previous sections load libraries
in a special way: it changes dir to the specific path and then loads the library
in the usual way. After that it returns to the path where it started out. After
this, when the library needs additional libraries (and for instance graphicmagick
needs a lot of them) it will first look on its own path (which is remembered).

The \MKIV\ lookups are somewhat more robust in the sense that they first check
for matches on engine specific paths. This comes in handy when the search
patterns are too generic and one can match on for instance \type {luajittex}
whilc \type {luatex} is used.

\stopsection

\startsection[title=Colofon]

\starttabulate[|B|p|]
\NC author    \NC \getvariable{document}{author}, \getvariable{document}{affiliation}, \getvariable{document}{location} \NC \NR
\NC version   \NC \currentdate \NC \NR
\NC website   \NC \getvariable{document}{website} \endash\ \getvariable{document}{support} \NC \NR
\NC copyright \NC \symbol[cc][cc-by-sa-nc] \NC \NR
\NC comment   \NC the swiglib infrastructure is implemented by Luigi Scarso \NC \NR
\stoptabulate

\stopsection

\stopdocument