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
|
if not modules then modules = { } end modules ['back-pdp'] = {
version = 1.001,
comment = "companion to lpdf-ini.mkiv",
author = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
copyright = "PRAGMA ADE / ConTeXt Development Team",
license = "see context related readme files"
}
-- This is temporary ... awaiting a better test .. basically we can
-- always use this: pdf primitives.
local context = context
local lpdfreserveobject = lpdf.reserveobject
local lpdfcompresslevel = lpdf.compresslevel
local lpdfobj = lpdf.obj
local lpdfpagereference = lpdf.pagereference
local lpdfxformname = lpdf.xformname
local jobpositions = job.positions
local gethpos = jobpositions.gethpos
local getvpos = jobpositions.getvpos
local tokenscanners = tokens.scanners
local scanword = tokenscanners.word
local scankeyword = tokenscanners.keyword
local scanstring = tokenscanners.string
local scaninteger = tokenscanners.integer
local scandimension = tokenscanners.dimension
local trace = false trackers.register("commands", function(v) trace = v end)
local report = logs.reporter("command")
local nodepool = nodes.pool
local newsavepos = nodepool.savepos
local newliteral = nodepool.literal
local newsave = nodepool.save
local newrestore = nodepool.restore
local newsetmatrix = nodepool.setmatrix
local implement = interfaces.implement
local constants = interfaces.constants
local variables = interfaces.variables
-- helper
local function scanwhd()
local width, height, depth
while true do
if scankeyword("width") then
width = scandimension()
elseif scankeyword("height") then
height = scandimension()
elseif scankeyword("depth") then
depth = scandimension()
else
break
end
end
if width or height or depth then
return width or 0, height or 0, depth or 0
else
-- we inherit
end
end
-- positions
local function savepos()
context(newsavepos())
end
local function lastxpos()
context(gethpos())
end
local function lastypos()
context(getvpos())
end
implement { name = "savepos", actions = savepos }
implement { name = "lastxpos", actions = lastxpos }
implement { name = "lastypos", actions = lastypos }
-- literals
local function pdfliteral()
context(newliteral(scanword() or "origin",scanstring()))
end
implement { name = "pdfliteral", actions = pdfliteral }
-- box resources
local boxresources = tex.boxresources
local savebox = boxresources.save
local usebox = boxresources.use
local lastindex = 0
local function saveboxresource()
local immediate = true
local kind = scankeyword("type") and scaninteger() or 0
local attributes = scankeyword("attr") and scanstring() or nil
local resources = scankeyword("resources") and scanstring() or nil
local margin = scankeyword("margin") and scandimension() or 0 -- register
local boxnumber = scaninteger()
--
lastindex = savebox(boxnumber,attributes,resources,immediate,kind,margin)
if trace then
report("\\saveboxresource: index %i",lastindex)
end
end
local function lastsavedboxresourceindex()
if trace then
report("\\lastsaveboxresource: index %i",lastindex)
end
context("%i",lastindex)
end
local function useboxresource()
local width, height, depth = scanwhd()
local index = scaninteger()
local node = usebox(index,width,height,depth)
if trace then
report("\\useboxresource: index %i",index)
end
context(node)
end
implement { name = "saveboxresource", actions = saveboxresource }
implement { name = "lastsavedboxresourceindex", actions = lastsavedboxresourceindex }
implement { name = "useboxresource", actions = useboxresource }
-- image resources (messy: will move)
local imageresources = { }
local lastindex = 0
local lastpages = 1
local function saveimageresource()
local width, height, depth = scanwhd()
local page = 1
local immediate = true
local margin = 0 -- or dimension
local attributes = scankeyword("attr") and scanstring() or nil
if scankeyword("named") then
scanstring() -- ignored
elseif scankeyword("page") then
page = scaninteger()
end
local userpassword = scankeyword("userpassword") and scanstring() or nil
local ownerpassword = scankeyword("ownerpassword") and scanstring() or nil
local visiblefilename = scankeyword("visiblefilename") and scanstring() or nil
local colorspace = scankeyword("colorspace") and scaninteger() or nil
local pagebox = scanword() or nil
local filename = scanstring()
-- pcall
context.getfiguredimensions( { filename }, {
[constants.userpassword] = userpassword,
[constants.ownerpassword] = ownerpassword,
[constants.page] = page or 1,
[constants.size] = pagebox,
})
context.relax()
lastindex = lastindex + 1
lastpages = 1
imageresources[lastindex] = {
filename = filename,
page = page or 1,
size = pagebox,
width = width,
height = height,
depth = depth,
attr = attributes,
-- margin = margin,
}
end
local function lastsavedimageresourceindex()
context("%i",lastindex or 0)
end
local function lastsavedimageresourcepages()
context("%i",lastpages or 0) -- todo
end
local function useimageresource()
local width, height, depth = scanwhd()
if scankeyword("keepopen") then
-- ignored
end
local index = scaninteger()
local l = imageresources[index]
if l then
if not (width or height or depth) then
width = l.width
height = l.height
depth = l.depth
end
-- pcall
context.externalfigure( { l.filename }, {
[constants.userpassword] = l.userpassword,
[constants.ownerpassword] = l.ownerpassword,
[constants.width] = width and (width .. "sp") or nil,
[constants.height] = height and (height .. "sp") or nil,
[constants.page] = l.page or 1,
[constants.size] = pagebox,
})
context.relax()
else
print("no image resource",index)
end
end
implement { name = "saveimageresource", actions = saveimageresource }
implement { name = "lastsavedimageresourceindex", actions = lastsavedimageresourceindex }
implement { name = "lastsavedimageresourcepages", actions = lastsavedimageresourcepages }
implement { name = "useimageresource", actions = useimageresource }
-- objects
local lastobjnum = 0
local function pdfobj()
if scankeyword("reserveobjnum") then
lastobjnum = lpdfreserveobject()
if trace then
report("\\pdfobj reserveobjnum: object %i",lastobjnum)
end
else
local immediate = true
local objnum = scankeyword("useobjnum") and scaninteger() or lpdfreserveobject()
local uncompress = scankeyword("uncompressed") or lpdfcompresslevel() == 0
local streamobject = scankeyword("stream") and true or false
local attributes = scankeyword("attr") and scanstring()
local fileobject = scankeyword("file")
local content = scanstring()
local object = {
immediate = immediate,
attr = attributes,
objnum = objnum,
type = streamobject and "stream" or nil,
compresslevel = uncompress and 0 or nil,
}
if fileobject then
object.filename = content
else
object.string = content
end
lpdfobj(object)
lastobjnum = objnum
if trace then
report("\\pdfobj: object %i",lastobjnum)
end
end
end
local function pdflastobj()
context("%i",lastobjnum)
if trace then
report("\\lastobj: object %i",lastobjnum)
end
end
local function pdfrefobj()
local objnum = scaninteger()
if trace then
report("\\refobj: object %i (todo)",objnum)
end
end
implement { name = "pdfobj", actions = pdfobj }
implement { name = "pdflastobj", actions = pdflastobj }
implement { name = "pdfrefobj", actions = pdfrefobj }
-- annotations
local lastobjnum = 0
local function pdfannot()
if scankeyword("reserveobjnum") then
lastobjnum = lpdfreserveobject()
if trace then
report("\\pdfannot reserveobjnum: object %i",lastobjnum)
end
else
local width = false
local height = false
local depth = false
local data = false
local object = false
local attr = false
--
if scankeyword("useobjnum") then
object = scancount()
report("\\pdfannot useobjectnum is not (yet) supported")
end
local width, height, depth = scanwhd()
if scankeyword("attr") then
attr = scanstring()
end
data = scanstring()
context(backends.nodeinjections.annotation(width or 0,height or 0,depth or 0,data or ""))
end
end
implement { name = "pdfannot", actions = pdfannot }
local function pdfdest()
local name = false
local zoom = false
local view = false
local width = false
local height = false
local depth = false
if scankeyword("num") then
report("\\pdfdest num is not (yet) supported")
elseif scankeyword("name") then
name = scanstring()
end
if scankeyword("xyz") then
view = "xyz"
if scankeyword("zoom") then
report("\\pdfdest zoom is ignored")
zoom = scancount() -- will be divided by 1000 in the backend
end
elseif scankeyword("fitbh") then
view = "fitbh"
elseif scankeyword("fitbv") then
view = "fitbv"
elseif scankeyword("fitb") then
view = "fitb"
elseif scankeyword("fith") then
view = "fith"
elseif scankeyword("fitv") then
view = "fitv"
elseif scankeyword("fitr") then
view = "fitr"
width, height, depth = scanwhd()
elseif scankeyword("fit") then
view = "fit"
end
context(backends.nodeinjections.destination(width or 0,height or 0,depth or 0,{ name or "" },view or "fit"))
end
implement { name = "pdfdest", actions = pdfdest }
-- management
local function pdfsave()
context(newsave())
end
local function pdfrestore()
context(newrestore())
end
local function pdfsetmatrix()
context(newsetmatrix(scanstring()))
end
-- extras
local function pdfpageref()
context(lpdfpagereference())
end
local function pdfxformname()
context(lpdfxformname())
end
-- extensions: literal dest annot save restore setmatrix obj refobj colorstack
-- startlink endlink startthread endthread thread outline glyphtounicode fontattr
-- mapfile mapline includechars catalog info names trailer
local extensions = {
literal = pdfliteral,
obj = pdfobj,
refobj = pdfrefobj,
dest = pdfdest,
annot = pdfannot,
save = pdfsave,
restore = pdfrestore,
setmatrix = pdfsetmatrix,
}
local function pdfextension()
local w = scanword()
if w then
local e = extensions[w]
if e then
e()
else
report("\\pdfextension: unknown %a",w)
end
end
end
implement { name = "pdfextension", actions = pdfextension }
-- feedbacks: colorstackinit creationdate fontname fontobjnum fontsize lastannot
-- lastlink lastobj pageref retval revision version xformname
local feedbacks = {
lastobj = pdflastobj,
pageref = pdfpageref,
xformname = pdfxformname,
}
local function pdffeedback()
local w = scanword()
if w then
local f = feedbacks[w]
if f then
f()
else
report("\\pdffeedback: unknown %a",w)
end
end
end
implement { name = "pdffeedback", actions = pdffeedback }
-- variables: (integers:) compresslevel decimaldigits gamma gentounicode
-- ignoreunknownimages imageaddfilename imageapplygamma imagegamma imagehicolor
-- imageresolution inclusioncopyfonts inclusionerrorlevel majorversion minorversion
-- objcompresslevel omitcharset omitcidset pagebox pkfixeddpi pkresolution
-- recompress suppressoptionalinfo uniqueresname (dimensions:) destmargin horigin
-- linkmargin threadmargin vorigin xformmargin (tokenlists:) pageattr pageresources
-- pagesattr pkmode trailerid xformattr xformresources
-- local variables = {
-- }
--
-- local function pdfvariable()
-- local w = scanword()
-- if w then
-- local f = variables[w]
-- if f then
-- f()
-- else
-- print("invalid variable",w)
-- end
-- else
-- print("missing variable")
-- end
-- end
--------- { name = "pdfvariable", actions = pdfvariable }
|