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
|
%D \module
%D [ file=pack-obj,
%D version=1998.01.15,
%D title=\CONTEXT\ Packaging Macros,
%D subtitle=Objects,
%D author=Hans Hagen,
%D date=\currentdate,
%D copyright={PRAGMA ADE \& \CONTEXT\ Development Team}]
%C
%C This module is part of the \CONTEXT\ macro||package and is
%C therefore copyrighted by \PRAGMA. See mreadme.pdf for
%C details.
\writestatus{loading}{ConTeXt Packaging Macros / Objects}
\unprotect
\let\objectreference\gobblefourarguments % catch mkii tuo stuff
\registerctxluafile{pack-obj}{1.001}
% \startluacode
% local texbox, texdimen, texcount, texwrite = tex.box, tex.dimen, tex.count, tex.write
% local pdfxform, pdfrefxform = pdf.xform, pdf.refxform
%
% function pdf.xform (l) texbox["objectbox"] = nil return l end
% function pdf.refxform(l) return node.copy_list(l) end
%
% backends.codeinjections.register = pdf.xform
% backends.codeinjections.restore = pdf.refxform
%
% local codeinjections = backends.codeinjections
%
% objects = objects or { }
%
% local data = { }
%
% objects.data = data
% objects.n = 0
%
% function objects.register(name)
% objects.n = objects.n + 1
% local list = texbox.objectbox
% nodes.handlers.finalize(list)
% data[name] = {
% codeinjections.restore(list),
% texdimen.objectwd,
% texdimen.objectht,
% texdimen.objectdp,
% texdimen.objectoff,
% }
% end
%
% function objects.restore(name)
% local d = data[name]
% if d then
% texbox .objectbox = codeinjections.restore(d[1])
% texdimen.objectwd = d[2]
% texdimen.objectht = d[3]
% texdimen.objectdp = d[4]
% texdimen.objectoff = d[5]
% else
% texbox .objectbox = nil
% texdimen.objectwd = 0
% texdimen.objectht = 0
% texdimen.objectdp = 0
% texdimen.objectoff = 0
% end
% end
%
% function objects.reference(name)
% local d = data[name]
% texwrite((d and d[1]) or 0)
% end
%
% function objects.enhance(name)
% local d = data[name]
% if d then
% d[6] = texcount.realpageno
% end
% end
%
% function objects.page(name)
% local d = data[name]
% texwrite((d and d[6]) or texcount.realpageno)
% end
%
% function objects.doifelse(name)
% commands.testcase(data[name])
% end
% \stopluacode
%
% \unprotect
%
% \newbox \objectbox
% \newtoks \everyobject
% \newif \ifinobject
%
% \newdimen\objectoff \def\objectmargin{\the\objectoff}
% \newdimen\objectwd \def\objectwidth {\the\objectwd }
% \newdimen\objectht \def\objectheight{\the\objectht }
% \newdimen\objectdp \def\objectdepth {\the\objectdp }
%
% \def\objectoffset{1cm}
%
% \everyobject{\the\pdfbackendeveryxform}
%
% \let\doresetobjects\relax
%
% \def\setobject #1#2{\begingroup\objectoff\objectoffset\inobjecttrue\the\everyobject\dowithnextbox{\dosetobject{#1}{#2}}}
% \def\settightobject#1#2{\begingroup\objectoff\zeropoint \inobjecttrue\the\everyobject\dowithnextbox{\dosetobject{#1}{#2}}}
%
% \let\objectsetvbox\vbox %\def\objectsetvbox{\ruledvbox}
% \let\objectgetvbox\vbox %\def\objectgetvbox{\ruledvbox}
% \let\objectsethbox\hbox %\def\objectsethbox{\ruledhbox}
% \let\objectgethbox\hbox %\def\objectgethbox{\ruledhbox}
%
% \def\dosetobject#1#2%
% {\objectwd\wd\nextbox
% \objectht\ht\nextbox
% \objectdp\dp\nextbox
% \ifdim\objectoff=\zeropoint\relax
% \setbox\objectbox\box\nextbox
% \else
% \setbox\objectbox\objectsetvbox spread 2\objectoff{\vss\objectsethbox spread 2\objectoff{\hss\flushnextbox\hss}\vss}%
% \fi
% \ctxlua{objects.register("#1::#2")}%
% \endgroup}
%
% \def\getobject#1#2%
% {\begingroup
% \ctxlua{objects.restore("#1::#2")}%
% \ifdim\objectoff=\zeropoint\relax \else
% \setbox\objectbox\objectgetvbox to \dimexpr\objectht+\objectdp\relax
% {\vss\objectgethbox to \objectwd{\hss\box\objectbox\hss}\vss}%
% \wd\objectbox\objectwd
% \ht\objectbox\objectht
% \dp\objectbox\objectdp
% \fi
% \box\objectbox
% \endgroup}
%
% \def\getpageobject#1#2%
% {\begingroup
% \ctxlua{objects.restore("#1::#2")}%
% \ifdim\objectoff=\zeropoint\relax
% \setbox\objectbox\objectgethbox{\ctxlatelua{objects.enhance("#1::#2")}\box\objectbox}
% \else
% \setbox\objectbox\objectgetvbox to \dimexpr\objectht+\objectdp\relax
% {\vss\objectgethbox to \objectwd{\ctxlatelua{objects.enhance("#1::#2")}\hss\box\objectbox\hss}\vss}%
% \wd\objectbox\objectwd
% \ht\objectbox\objectht
% \dp\objectbox\objectdp
% \fi
% \box\objectbox
% \endgroup}
%
% \def\setobjectdirectly #1#2{\ctxlua{objects.register("#1::#2")}}
% \def\getobjectdirectly #1#2{\ctxlua{objects.restore ("#1::#2")}}
% \def\getobjectdimensions #1#2{\ctxlua{objects.restore ("#1::#2")}}
% \def\doifobjectfoundelse #1#2{\ctxlua{objects.doifelse("#1::#2")}}
% \def\doifobjectreferencefoundelse#1#2{\ctxlua{objects.doifelse("#1::#2")}}
%
% \let\objectreferenced\relax
% \let\driverreferenced\relax
%
% \def\doregisterobjectreference{\writestatus{objects}{obsolete: register object reference}\gobblethreearguments}
% \def\dooverloadobjectreference{\writestatus{objects}{obsolete: overload object reference}\gobblethreearguments}
% \def\dosetobjectreference {\writestatus{objects}{obsolete: set object reference}\gobblethreearguments}
% \def\dosetdriverreference {\writestatus{objects}{obsolete: set driver reference}\gobblethreearguments}
%
% \def\defaultobjectreference{0}
% \def\defaultobjectpage {\realfolio}
%
% \def\dogetobjectreference #1#2#3{\xdef#3{\ctxlua{objects.reference("#1::#2)}}}
% \def\dogetobjectreferencepage#1#2#3{\xdef#3{\ctxlua{objects.page("#1::#2))}}}
%
% \protect
%
% \starttext
% test \setobject{a}{b}\ruledhbox{xxx}\getobject{a}{b} test
% \vskip3cm
% test \settightobject{a}{b}\ruledhbox{xxx}\getobject{a}{b} test
% test \settightobject{a}{c}\ruledhbox{xxx}\getobject{a}{c} test
% \dorecurse{5000}{test \setobject{a}{b}\ruledhbox{xxx}\getobject{a}{b} test }
% \stoptext
%D \macros
%D {setobject,getobject,ifinobject}
%D
%D Boxes can be considered reuable objects. Unfortunaltely once
%D passed to the \DVI\ file, such objects cannot be reused. In
%D \PDF\ however, reusing is possible and sometimes even a
%D necessity. Therefore, \CONTEXT\ supports reusable objects.
%D
%D During the \TEX\ processing run, boxes can serve the purpose
%D of objects, and the \DVI\ driver module implements objects
%D using packed boxes.
%D
%D The \PDF\ and \PDFTEX\ driver modules implement objects
%D using \PDF\ forms. There is no (real) restriction on the
%D number of objects there.
%D
%D The first application of objects in \CONTEXT\ concerned
%D \METAPOST\ graphics and fill||in form fields. The first
%D application can save lots of bytes, while the latter use is
%D more a necessity than byte saving.
%D
%D \starttyping
%D \setobject{class}{name}\somebox{}
%D \getobject{class}{name}
%D \stoptyping
%D
%D Here \type{\somebox} can be whatever box specification suits
%D \TEX. We save the dimensions of an object, although some
%D drivers will do so themselves. This means that when for
%D instance using \PDFTEX\ we could save a hash entry plus some
%D 20+ memory locations per object by delegating this
%D housekeeping to the driver. The current approach permits
%D us to keep the box characteristic too.
\newif\ifinobject
\def\objectplaceholder{NOT YET FLUSHED}%
\def\presetobject#1#2% \global added
{\ifcsname\??ob:#1::#2\endcsname\else
\global\@EA\let\csname\??ob:#1::#2\endcsname\objectplaceholder
\fi}
\def\dosetobject#1#2#3% \initializepaper this will move to \everyshipout
{% \initializepaper
\ifcsname\??ob:#2::#3\endcsname
\expandafter\gobblefivearguments
\else % tzt, overload internal referenced objects to save entries
\expandafter\dodosetobject
\fi
{#1}{#2}{#3}}
\def\resetobject#1#2%
{\letbeundefined{\??ob:#1::#2}}
%D \macros
%D {finalizeobjectbox}
%D
%D This one provides a hook for last minute object box processing
%D we need this in \MKIV.
\ifx\finalizeobjectbox\undefined
\let\finalizeobjectbox\gobbleoneargument
\fi
%D Somehow there is a rounding error problem in either \PDFTEX\
%D or in viewers, or maybe it is conforming the specs. The next
%D variable compensate for it by removing the rather tight
%D clip.
\def\objectoffset{1cm}
\def\dodosetobject#1#2#3%
{\bgroup
\globalpushmacro\crossreferenceobject \objectreferenced
\inobjecttrue
\dowithnextbox
{\globalpopmacro\crossreferenceobject
\dododosetobject{#1}{#2}{#3}\egroup}}
% in luatex version < 66 we had a 1bp compensation (hardcoded in luatex)
\def\dododosetobject#1#2#3%
{\begingroup
\scratchdimen\objectoffset
\@EA\xdef\csname\??ob:#2::#3\endcsname
{\noexpand\dohandleobject{#2}{#3}%
{\ifhbox\nextbox\hbox\else\vbox\fi}%
{\number\nextboxwd}{\number\nextboxht}{\number\nextboxdp}%
{\number\scratchdimen}}%
\expanded % freeze the dimensions since \dostartobject may use \nextbox
{\dostartobject{#2}{#3}{\the\nextboxwd}{\the\nextboxht}{\the\nextboxdp}}%
\ifcase#1\relax\else \ifdim\objectoffset>\zeropoint
\setbox\nextbox\vbox spread 2\scratchdimen
{\forgetall \offinterlineskip
\vss\hbox spread 2\scratchdimen{\hss\flushnextbox\hss}\vss}%
\fi \fi
\flushnextbox
\dostopobject
\endgroup}
\def\getobject#1#2%
{\ifcsname\??ob:#1::#2\endcsname
\begingroup
\let\dohandleobject\dogetobject
\csname\??ob:#1::#2\expandafter\endcsname
\else
{\infofont[object #1::#2]}%
\fi}
\def\dogetobject#1#2#3#4#5#6#7% don't change this, should work for dvi & pdf
{% \initializepaper
\forgetall
% todo: if no attr then faster
\setbox\scratchbox\vbox attr \viewerlayerattribute \attribute\viewerlayerattribute
{\doinsertobject{#1}{#2}}%
\setbox\scratchbox#3%
{\vbox to #5\scaledpoint
{\ifdim\ht\scratchbox>#5\scaledpoint
\vss\hbox to #4\scaledpoint{\hss\box\scratchbox\hss}\vss
\else\ifdim\wd\scratchbox>#4\scaledpoint
\vss\hbox to #4\scaledpoint{\hss\box\scratchbox\hss}\vss
\else
%\vss\box\scratchbox
\vss\hbox to #4\scaledpoint{\box\scratchbox\hss}% fix Chof
\fi\fi}}%
\box\scratchbox
\endgroup}
%D If needed one can ask for the dimensions of an object with:
%D
%D \starttyping
%D \getobjectdimensions{class}{name}
%D \stoptyping
%D
%D The results are reported in \type {\objectwidth}, \type
%D {\objectheight} and \type {\objectdepth}.
\def\dogetobjectdimensions#1#2#3#4#5#6#7%
{\def\objectwidth {#4\s!sp}%
\def\objectheight{#5\s!sp}%
\def\objectdepth {#6\s!sp}%
\def\objectmargin{#7\s!sp}}
\def\getobjectdimensions#1#2%
{\let\dohandleobject\dogetobjectdimensions
\let\objectwidth \!!zeropoint
\let\objectheight\!!zeropoint
\let\objectdepth \!!zeropoint
\csname\??ob:#1::#2\endcsname}
%D Apart from this kind of objects, that have typeset content,
%D we can have low level driver specific objects. Both types
%D can have references to internal representations, hidden for
%D the user. We keep track of such references by means of a
%D dedicated cross reference mechanism. Normally, objects are
%D defined before they are used, but forward referencing
%D sometimes occurs.
%D
%D \starttyping
%D \dosetobjectreference {class} {identifier} {reference value} {page}
%D \dogetobjectreference {class} {identifier} \csname
%D \stoptyping
%D
%D These commands are to be called by the \type{\startobject},
%D \type{\stopobject} and \type{\insertobject} specials.
\def\objectreferenced{\global\chardef\crossreferenceobject\plusone}
\def\driverreferenced{\global\chardef\crossreferenceobject\zerocount}
\objectreferenced
% no undefined test ! ! ! ! (pdftex fails on undefined objects)
\def\doregisterobjectreference#1#2#3{\normalexpanded{\noexpand\ctxlatelua{job.objects.save("#1::#2",#3,\noexpand\the\realpageno)}}}
\def\dooverloadobjectreference#1#2#3{\ctxlua{job.objects.set("#1::#2",#3,\the\realpageno)}}
\def\dosetobjectreference
{\ifcase\crossreferenceobject
\objectreferenced
\expandafter\dooverloadobjectreference
\else
\expandafter\doregisterobjectreference
\fi}
\def\dosetdriverreference
{\driverreferenced\dosetobjectreference}
\def\defaultobjectreference#1#2{0} % driver dependent
\def\defaultobjectpage #1#2{\realfolio}
\def\dogetobjectreference #1#2#3{\xdef#3{\ctxlua{job.objects.number("#1::#2","\defaultobjectreference{#1}{#2}")}}}
\def\dogetobjectreferencepage#1#2#3{\xdef#3{\ctxlua{job.objects.page("#1::#2","\defaultobjectpage{#1}{#2}")}}}
\def\setobject {\driverreferenced\dosetobject1}
\def\settightobject{\driverreferenced\dosetobject0}
%D \macros
%D {doifobjectfoundelse,doifobjectreferencefoundelse}
%D
%D To prevent redundant definition of objects, one can use
%D the next tests:
%D
%D \starttyping
%D \doifobjectfoundelse{class}{object}{do then}{do else}
%D \doifobjectreferencefoundelse{class}{object}{do then}{do else}
%D \stoptyping
\def\doifobjectfoundelse#1#2%
{\ifcsname\??ob:#1::#2\endcsname
\expandafter\firstoftwoarguments
\else
\expandafter\secondoftwoarguments
\fi}
\def\doifobjectreferencefoundelse#1#2{\ctxlua{job.objects.doifelse("#1::#2")}}
\protect \endinput
|