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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
|
% language=uk
\environment still-environment
\starttext
\startchapter[title=The \LUATEX\ \PDF\ backend]
\startsection[title=Introduction]
The original design of \TEX\ has a clear separation between the frontend and
backend code. In principle, shipping out a page boils down to traversing the
to|-|be|-|shipped|-|out box and translating the glyph, rule, glue, kern and list
nodes into positioning just glyphs and rules on a canvas. The \DVI\ backend is
therefore relatively simple, as the \DVI\ output format delegates to other
programs the details of font inclusion and such into the final format; it just
describes the pages.
Because we eventually want color and images as well, there is a mechanism to pass
additional information to post|-|processing programs. One can insert \type
{\special}s with directives like \type {insert image named foo.jpg}. The frontend
as well as the backend are not concerned with what goes into a special; the \DVI\
post|-|processor of course is.
The \PDF\ backend, on the other hand, is more complex as it immediately produces
the final typeset result and, as such, offers possibilities to insert verbatim
code (\type {\pdfliteral}), images (\type {\pdfximage} cum suis), annotations,
destinations, threads and all kinds of objects, reuse typeset content (\type
{\pdfxform} cum suis); in the end, there are all kinds of \type {\pdf...}
commands. The way these were implemented in \LUATEX\ prior to 0.82 violates the
separation between frontend and backend, an inheritance from \PDFTEX. Additional
features such as protrusion and expansion add to that entanglement. However,
because \PDF\ is an evolving standard, occasionally we need to adapt the related
code. A separation of code makes sure that the frontend can become stable (and
hopefully frozen) at some point. \footnote {In practice nowadays, the backend
code changes little, because the \PDF\ produced by \LUATEX\ is rather simple and
is easily adapted to the changing standard.}
In \LUATEX\ we had already started making this separation of specialized code,
such as a cleaner implementation of font expansion, but all these \type {\pdf...}
commands were still pervasive, leading to fuzzy dependencies, checks for backend
modes, etc.\ so a logical step was to straighten all this out. That way we give
\LUATEX\ a cleaner core constructed from traditional \TEX, extended with \ETEX,
\ALEPH|/|\OMEGA, and \LUATEX\ functionality.
\stopsection
\startsection[title=Extensions]
A first step, then, was to transform generic (i.e.\ independent from the backend)
functionality which was still (sort of) bound to \ALEPH\ and \PDFTEX, into core
functionality. A second step was to reorganize the backend specific \PDF\ code,
i.e.\ move it out of the core and into the group of extension commands. This
extension group is somewhat special and originates in traditional \TEX; it is the
way to add your own functionality to \TEX, the program.
As an example for future programmers, Don Knuth added four (connected) primitives
as extensions: \type {\openout}, \type {\closeout}, \type {\write} and \type
{\special}. The \ALEPH\ and \PDFTEX\ engines, on the other hand, put some
functionality in extensions and some in the core. This arose from the fact that
dealing with variables in extensions is often inconvenient, as they are then seen
as (unexpandable) commands instead of integers, token lists, etc. That the
write|-|related commands are there is almost entirely due to being the
demonstration of the mechanism; everything related to {\em reading} files is in
the core. There is one property that perhaps forces us to keep the writers there,
and that's the \type {\immediate} prefix. \footnote {Unfortunately we're stuck
with \type {\immediate} in the backend; a \type {deferred} keyword would have
been handier, especially since other backend|-|related commands can also be
immediate.}
In the process of separating, we reshuffled the code base a bit; the current use
of the extensions mechanism still suits as an example and also gives us backward
compatibility. However, new backend primitives will not be added there but rather
in specific plugins (if needed at all).
\stopsection
\startsection[title=From whatsits to nodes]
The \PDF\ backend introduced two new concepts into the core: (reusable) images
and (reusable) content (wrapped in boxes). In keeping with good \TEX\ practice,
these were implemented as whatsits (a node type for extensions); but this
created, as a side effect, an anomaly in the handling of such nodes. Consider
looping over a node list where we need to check dimensions of nodes; in \LUA, we
can write something like this:
\starttyping
while n do
if n.id == glyph then
-- wd ht dp
elseif n.id == rule then
-- wd ht dp
elseif n.id == kern then
-- wd
elseif n.id == glue then
-- size stretch shrink
elseif n.id == whatsits then
if n.subtype == pdfxform then
-- wd ht dp
elseif n.subtype == pdfximage then
-- wd ht dp
end
end
n = n.next
end
\stoptyping
So for each node in the list, we need to check these two whatsit subtypes. But as
these two concepts are rather generic, there is no evident need to implement it
this way. Of course the backend has to provide the inclusion and reuse, but the
frontend can be agnostic about this. That is, at the input end, in specifying
these two injects, we only have to make sure we pass the right information (so
the scanner might differentiate between backends).
Thus, in \LUATEX\ these two concepts have been promoted to core features:
\starttabulate[|l|l|]
\NC \type {\pdfxform} \NC \type {\saveboxresource} \NC \NR
\NC \type {\pdfximage} \NC \type {\saveimageresource} \NC \NR
\NC \type {\pdfrefxform} \NC \type {\useboxresource} \NC \NR
\NC \type {\pdfrefximage} \NC \type {\useimageresource} \NC \NR
\NC \type {\pdflastxform} \NC \type {\lastsavedboxresourceindex} \NC \NR
\NC \type {\pdflastximage} \NC \type {\lastsavedimageresourceindex} \NC \NR
\NC \type {\pdflastximagepages} \NC \type {\lastsavedimageresourcepages} \NC \NR
\stoptabulate
The index should be considered an arbitrary number set to whatever the backend
plugin decides to use as an identifier. These are no longer whatsits, but a
special type of rule; after all, \TEX\ is only interested in dimensions. Given
this change, the previous code can be simplified to:
\starttyping
while n do
if n.id == glyph then
-- wd ht dp
elseif n.id == rule then
-- wd ht dp
elseif n.id == kern then
-- wd
elseif n.id == glue then
-- size stretch shrink
end
n = n.next
end
\stoptyping
The only consequence for the previously existing rule type (which, in fact, is
also something that needs to be dealt with in the backend, depending on the
target format) is that a normal rule now has subtype~0 while the box resource has
subtype~1 and the image subtype~2.
If a package writer wants to retain the \PDFTEX\ names, the previous table can be
used; just prefix \type{\let}. For example, the first line would be (spaces
optional, of course):
\starttyping
\let\pdfxform\saveboxresource
\stoptyping
\stopsection
\startsection[title=Direction nodes]
A similar change has been made for ``direction'' nodes, which were also
previously whatsits. These are now normal nodes so again, instead of consulting
whatsit subtypes, we can now just check the id of a node.
It should be apparent that all of these changes from whatsits to normal nodes
already greatly simplify the code base.
\stopsection
\startsection[title=Promoted commands]
Many more commands have been promoted to the core. Here is an additional list of
original \PDFTEX\ commands and their new counterparts (this time with the \type
{\let} included):
\starttyping
\let\pdfpagewidth \pagewidth
\let\pdfpageheight \pageheight
\let\pdfadjustspacing \adjustspacing
\let\pdfprotrudechars \protrudechars
\let\pdfnoligatures \ignoreligaturesinfont
\let\pdffontexpand \expandglyphsinfont
\let\pdfcopyfont \copyfont
\let\pdfnormaldeviate \normaldeviate
\let\pdfuniformdeviate \uniformdeviate
\let\pdfsetrandomseed \setrandomseed
\let\pdfrandomseed \randomseed
\let\ifpdfabsnum \ifabsnum
\let\ifpdfabsdim \ifabsdim
\let\ifpdfprimitive \ifprimitive
\let\pdfprimitive \primitive
\let\pdfsavepos \savepos
\let\pdflastxpos \lastxpos
\let\pdflastypos \lastypos
\let\pdftexversion \luatexversion
\let\pdftexrevision \luatexrevision
\let\pdftexbanner \luatexbanner
\let\pdfoutput \outputmode
\let\pdfdraftmode \draftmode
\let\pdfpxdimen \pxdimen
\let\pdfinsertht \insertht
\stoptyping
\stopsection
\startsection[title=Backend commands]
There are many commands that start with \type {\pdf} and, over the history of
development of \PDFTEX\ and \LUATEX, some have been added, some have been
renamed, others removed. Instead of the many, we now have just one: \type
{\pdfextension}. A couple of usage examples:
\starttyping
\pdfextension literal {1 0 0 2 0 0 cm}
\pdfextension obj {/foo (bar)}
\stoptyping
Here, we pass a keyword that tells for what to scan and what to do with it. A
backward|-|compatible interface is easy to write. Although it delegates a bit
more management of these \type {\pdf} commands to the macro package, the
responsibility for dealing with such low|-|level, error|-|prone calls is there
anyway. The full list of \type {\pdfextension}s is given here. The scanning after
the keyword is the same as for \PDFTEX.
\starttyping
\protected\def\pdfliteral {\pdfextension literal }
\protected\def\pdfcolorstack {\pdfextension colorstack }
\protected\def\pdfsetmatrix {\pdfextension setmatrix }
\protected\def\pdfsave {\pdfextension save\relax}
\protected\def\pdfrestore {\pdfextension restore\relax}
\protected\def\pdfobj {\pdfextension obj }
\protected\def\pdfrefobj {\pdfextension refobj }
\protected\def\pdfannot {\pdfextension annot }
\protected\def\pdfstartlink {\pdfextension startlink }
\protected\def\pdfendlink {\pdfextension endlink\relax}
\protected\def\pdfoutline {\pdfextension outline }
\protected\def\pdfdest {\pdfextension dest }
\protected\def\pdfthread {\pdfextension thread }
\protected\def\pdfstartthread {\pdfextension startthread }
\protected\def\pdfendthread {\pdfextension endthread\relax}
\protected\def\pdfinfo {\pdfextension info }
\protected\def\pdfcatalog {\pdfextension catalog }
\protected\def\pdfnames {\pdfextension names }
\protected\def\pdfincludechars {\pdfextension includechars }
\protected\def\pdffontattr {\pdfextension fontattr }
\protected\def\pdfmapfile {\pdfextension mapfile }
\protected\def\pdfmapline {\pdfextension mapline }
\protected\def\pdftrailer {\pdfextension trailer }
\protected\def\pdfglyphtounicode{\pdfextension glyphtounicode }
\stoptyping
\stopsection
\startsection[title=Backend variables]
As with commands, there are many variables that can influence the \PDF\ backend.
The most important one was, of course, that which set the output mode
(\type{\pdfoutput}). Well, that one is gone and has been replaced by \type
{\outputmode}. A value of~1 means that we produce \PDF.
One complication of variables is that (if we want to be compatible), we need to
have them as real \TEX\ registers. However, as most of them are optional, an easy
way out is simply not to define them in the engine. In order to be able to still
deal with them as registers (which is backward compatible), we define them as
follows:
\starttyping
\edef\pdfminorversion {\pdfvariable minorversion}
\edef\pdfcompresslevel {\pdfvariable compresslevel}
\edef\pdfobjcompresslevel {\pdfvariable objcompresslevel}
\edef\pdfdecimaldigits {\pdfvariable decimaldigits}
\edef\pdfhorigin {\pdfvariable horigin}
\edef\pdfvorigin {\pdfvariable vorigin}
\edef\pdfgamma {\pdfvariable gamma}
\edef\pdfimageresolution {\pdfvariable imageresolution}
\edef\pdfimageapplygamma {\pdfvariable imageapplygamma}
\edef\pdfimagegamma {\pdfvariable imagegamma}
\edef\pdfimagehicolor {\pdfvariable imagehicolor}
\edef\pdfimageaddfilename {\pdfvariable imageaddfilename}
\edef\pdfignoreunknownimages {\pdfvariable ignoreunknownimages}
\edef\pdfinclusioncopyfonts {\pdfvariable inclusioncopyfonts}
\edef\pdfinclusionerrorlevel {\pdfvariable inclusionerrorlevel}
\edef\pdfpkmode {\pdfvariable pkmode}
\edef\pdfpkresolution {\pdfvariable pkresolution}
\edef\pdfgentounicode {\pdfvariable gentounicode}
\edef\pdflinkmargin {\pdfvariable linkmargin}
\edef\pdfdestmargin {\pdfvariable destmargin}
\edef\pdfthreadmargin {\pdfvariable threadmargin}
\edef\pdfformmargin {\pdfvariable formmargin}
\edef\pdfuniqueresname {\pdfvariable uniqueresname}
\edef\pdfpagebox {\pdfvariable pagebox}
\edef\pdfpagesattr {\pdfvariable pagesattr}
\edef\pdfpageattr {\pdfvariable pageattr}
\edef\pdfpageresources {\pdfvariable pageresources}
\edef\pdfxformattr {\pdfvariable xformattr}
\edef\pdfxformresources {\pdfvariable xformresources}
\stoptyping
You can set them as follows (the values shown here are the initial values):
\starttyping
\pdfcompresslevel 9
\pdfobjcompresslevel 1
\pdfdecimaldigits 3
\pdfgamma 1000
\pdfimageresolution 71
\pdfimageapplygamma 0
\pdfimagegamma 2200
\pdfimagehicolor 1
\pdfimageaddfilename 1
\pdfpkresolution 72
\pdfinclusioncopyfonts 0
\pdfinclusionerrorlevel 0
\pdfignoreunknownimages 0
\pdfreplacefont 0
\pdfgentounicode 0
\pdfpagebox 0
\pdfminorversion 4
\pdfuniqueresname 0
\pdfhorigin 1in
\pdfvorigin 1in
\pdflinkmargin 0pt
\pdfdestmargin 0pt
\pdfthreadmargin 0pt
\stoptyping
Their removal from the frontend has helped again to clean up the code and, by
making them registers, their use is still compatible. A call to \type
{\pdfvariable} defines an internal register that keeps the value (of course this
value can also be influenced by the backend itself). Although they are real
registers, they live in a protected namespace:
\startbuffer
\meaning\pdfcompresslevel
\stopbuffer
\typebuffer
which gives:
{\tt\getbuffer}
It's perhaps unfortunate that we have to remain compatible because a setter and
getter would be much nicer. I am still considering writing the extension
primitive in \LUA\ using the token scanner, but it might not be possible to
remain compatible then. This is not so much an issue for \CONTEXT\ that always
has had backend drivers, but, rather, for other macro packages that have users
expecting the primitives (or counterparts) to be available.
\startsection[title=Backend feedback]
The backend can report on some properties that were also accessible via \type
{\pdf...} primitives. Because these are read|-|only variables, another primitive
now handles them: \type {\pdffeedback}. This primitive can be used to define
compatible alternatives:
\starttyping
\def\pdflastlink {\numexpr\pdffeedback lastlink\relax}
\def\pdfretval {\numexpr\pdffeedback retval\relax}
\def\pdflastobj {\numexpr\pdffeedback lastobj\relax}
\def\pdflastannot {\numexpr\pdffeedback lastannot\relax}
\def\pdfxformname {\numexpr\pdffeedback xformname\relax}
\def\pdfcreationdate {\pdffeedback creationdate}
\def\pdffontname {\numexpr\pdffeedback fontname\relax}
\def\pdffontobjnum {\numexpr\pdffeedback fontobjnum\relax}
\def\pdffontsize {\dimexpr\pdffeedback fontsize\relax}
\def\pdfpageref {\numexpr\pdffeedback pageref\relax}
\def\pdfcolorstackinit {\pdffeedback colorstackinit}
\stoptyping
The variables are internal, so they are anonymous. When we ask for the meaning of
some that were previously defined:
\starttyping
\meaning\pdfhorigin
\meaning\pdfcompresslevel
\meaning\pdfpageattr
\stoptyping
we will get, similar to the above:
\starttyping
macro:->[internal backend dimension]
macro:->[internal backend integer]
macro:->[internal backend tokenlist]
\stoptyping
\stopsection
\startsection[title=Removed primitives]
Finally, here is the list of primitives that have been removed, with no
\TEX|-|level equivalent available. Many were experimental, and they can be easily
be provided to \TEX\ using \LUA.
\startcolumns[n=2]
\starttyping
\knaccode
\knbccode
\knbscode
\pdfadjustinterwordglue
\pdfappendkern
\pdfeachlinedepth
\pdfeachlineheight
\pdfelapsedtime
\pdfescapehex
\pdfescapename
\pdfescapestring
\pdffiledump
\pdffilemoddate
\pdffilesize
\pdffirstlineheight
\pdfforcepagebox
\pdfignoreddimen
\pdflastlinedepth
\pdflastmatch
\pdflastximagecolordepth
\pdfmatch
\pdfmdfivesum
\pdfmovechars
\pdfoptionalwaysusepdfpagebox
\pdfoptionpdfinclusionerrorlevel
\pdfprependkern
\pdfresettimer
\pdfshellescape
\pdfsnaprefpoint
\pdfsnapy
\pdfsnapycomp
\pdfstrcmp
\pdfunescapehex
\pdfximagebbox
\shbscode
\stbscode
\stoptyping
\stopcolumns
\stopsection
\startsection[title=Conclusion]
The advantage of a clean backend separation, supported by just the three
primitives \type {\pdfextension}, \type {\pdfvariable} and \type {\pdffeedback},
as well as a collection of registers, is that we can now further clean the code
base, which remains a curious mix of combined engine code, sometimes and
sometimes not converted to C from \PASCAL. A clean separation also means that if
someone wants to tune the backend for a special purpose, the frontend can be left
untouched. We will get there eventually.
All the definitions shown here are available in the file \type {luatex-pdf.tex},
which is part of the \CONTEXT\ distribution.
\stopsection
\stopchapter
\stoptext
|