Debian Codegraph @

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

raw
1 % language=us runpath=texruns:manuals/luametatex
2
3 \environment luametatex-style
4
5 \startdocument[title=PDF]
6
7 \startsection[title={Introduction}]
8
9 There is no backend, not even a \DVI\ one. In \CONTEXT\ the main backend is a
10 \PDF\ backend and it is written in \LUA. The \PDF\ format makes it possible to
11 embed \JPEG\ and \PNG\ encoded images as well as \PDF\ images. All these have to
12 be dealt with in \LUA. Although we can parse \PDF\ files with \LUA, the engine
13 has a dedicated \PDF\ library on board written by Paweł Jackowski.
14
15 A \PDF\ file is basically a tree of objects and one descends into the tree via
16 dictionaries (key|/|value) and arrays (index|/|value). There are a few topmost
17 dictionaries that start at the document root and those are accessed more
18 directly.
19
20 Although everything in \PDF\ is basically an object we have to wrap a few in so
21 called userdata \LUA\ objects.
22
23 \starttabulate[|l|l|]
24 \FL
25 \BC PDF \BC \LUA \NC \NR
26 \TL
27 \NC null \NC <t:nil> \NC \NR
28 \NC boolean \NC <t:boolean> \NC \NR
29 \NC integer \NC <t:integer> \NC \NR
30 \NC float \NC <t:number> \NC \NR
31 \NC name \NC <t:string> \NC \NR
32 \NC string \NC <t:string> \NC \NR
33 \NC array \NC <t:userdata> \NC \NR
34 \NC dictionary \NC <t:userdata> \NC \NR
35 \NC stream \NC <t:userdata> \NC \NR
36 \NC reference \NC <t:userdata> \NC \NR
37 \LL
38 \stoptabulate
39
40 The interface is rather limited to creating an instance and getting objects and
41 values. Aspects like compression and encryption are mostly dealt with
42 automatically. In \CONTEXT\ users use an interface layer around these, if they
43 use this kind of low level code at all as it assumes familiarity with how \PDF\
44 is constructed.
45
46 \stopsection
47
48 \startsection[title={\LUA\ interfaces}]
49
50 \startsubsection[title={Opening and closing}]
51
52 There are two ways to open a \PDF\ file:
53
54 \starttyping[option=LUA]
55 function pdfe.open ( <t:string> filename )
56 return <t:pdf> -- pdffile
57 end
58
59 function pdfe.openfile( <t:file> filehandle )
60 return <t:pdf> -- pdffile
61 end
62 \stoptyping
63
64 Instead of from file, we can read from a string:
65
66 \starttyping[option=LUA]
67 function pdfe.new ( <t:string> somestring, <t:integer> somelength )
68 return <t:pdf> -- pdffile
69 end
70 \stoptyping
71
72 Closing the instance is done with:
73
74 \starttyping[option=LUA]
75 function pdfe.close ( <t:pdf> pdffile )
76 -- no return values
77 end
78 \stoptyping
79
80 When we used \type {pdfe.open} the library manages the file and closes it when
81 done. You can check if a document opened as expected by calling:
82
83 \starttyping[option=LUA]
84 function pdfe.getstatus ( <t:pdf> pdffile )
85 return <t:integer> -- status
86 end
87 \stoptyping
88
89 A table of possible return codes can be queried with:
90
91 \starttyping[option=LUA]
92 function pdfe.getstatusvalues ( )
93 return <t:table> -- values
94 end
95 \stoptyping
96
97 Currently we have these:
98
99 \ctxlua{moduledata.pdfe.codes("getstatusvalues")}
100
101 An encrypted document can be decrypted by the next command where instead of
102 either password you can give \type {nil} and hope for the best:
103
104 \starttyping[option=LUA]
105 function pdfe.unencrypt (
106 <t:pdf> pdffile,
107 <t:string> userpassword,
108 <t:string> ownerpassword
109 )
110 return <t:integer> -- status
111 end
112 \stoptyping
113
114 \stopsubsection
115
116 \startsubsection[title=Getting basic information]
117
118 A successfully opened document can provide some information:
119
120 \starttyping[option=LUA]
121 function pdfe.getsize( <t:pdf> pdffile )
122 return <t:integer> -- nofbytes
123 end
124
125 function pdfe.getversion( <t:pdf> pdffile )
126 return
127 <t:integer>, -- major
128 <t:integer> -- minor
129 end
130
131 function pdfe.getnofobjects ( <t:pdf> pdffile )
132 return <t:integer> -- nofobjects
133 end
134
135 function pdfe.getnofpages ( <t:pdf> pdffile )
136 return <t:integer> -- nofpages
137 end
138
139 function pdfe.memoryusage ( <t:pdf> pdffile )
140 return
141 <t:integer>, -- bytes
142 <t:integer> -- waste
143 end
144 \stoptyping
145
146 \stopsubsection
147
148 \startsubsection[title={The main structure}]
149
150 For accessing the document structure you start with the so called catalog, a
151 dictionary:
152
153 \starttyping[option=LUA]
154 function pdfe.getcatalog( <t:pdf> pdffile )
155 return <t:userdata> -- dictionary
156 end
157 \stoptyping
158
159 The other two root dictionaries are accessed with:
160
161 \starttyping[option=LUA]
162 function pdfe.gettrailer ( <t:pdf> pdffile )
163 return <t:userdata> -- dictionary
164 end
165
166 function pdfe.getinfo ( <t:pdf> pdffile )
167 return <t:userdata> -- dictionary
168 end
169 \stoptyping
170
171 \stopsubsection
172
173 \startsubsection[title={Getting content}]
174
175 A specific page can conveniently be reached with the next command, which returns
176 a dictionary.
177
178 \starttyping[option=LUA]
179 function pdfe.getpage ( <t:pdf> pdffile, <t:integer> pagenumber )
180 return <t:userdata> -- dictionary
181 end
182 \stoptyping
183
184 Another convenience command gives you the (bounding) box of a (normally page)
185 which can be inherited from the document itself. An example of a valid box name
186 is \type {MediaBox}.
187
188 \starttyping[option=LUA]
189 function pdfe.getbox ( <t:pdf> pdffile, <t:string> boxname )
190 return <t:table> -- boundingbox
191 end
192 \stoptyping
193
194 \stopsubsection
195
196 \startsubsection[title={Getters}]
197
198 Common values in dictionaries and arrays are strings, integers, floats, booleans
199 and names (which are also strings) and these are also normal \LUA\ objects. In
200 some cases a value is a userdata object and you can use this helper to get some
201 more information:
202
203 \starttyping[option=LUA]
204 function pdfe.type ( <t:whatever> value )
205 return type -- string
206 end
207 \stoptyping
208
209 Stings are special because internally they are delimited by parenthesis (often
210 \typ {pdfdoc} encoding) or angle brackets (hexadecimal or 16 bit \UNICODE).
211
212 \starttyping[option=LUA]
213 function pdfe.getstring (
214 <t:userdata> object,
215 <t:string> key | <t:integer> index
216 )
217 return
218 <t:string> -- decoded value
219 end
220 \stoptyping
221
222 When you ask for more you get more:
223
224 \starttyping[option=LUA]
225 function pdfe.getstring (
226 <t:userdata> object,
227 <t:string> key | <t:integer> index,
228 <t:boolean> more
229 )
230 return
231 <t:string>, -- original
232 <t:boolean> -- hexencoded
233 end
234 \stoptyping
235
236 Basic types are fetched with:
237
238 \starttyping[option=LUA]
239 function pdfe.getinteger ( <t:userdata>, <t:string> key | <t:integer> index )
240 return <t:integer> -- value
241 end
242
243 function pdfe.getnumber ( <t:userdata>, <t:string> key | <t:integer> index )
244 return <t:number> -- value
245 end
246
247 function pdfe.getboolean ( <t:userdata>, <t:string> key | <t:integer> index )
248 return <t:boolean> -- value
249 end
250 \stoptyping
251
252 A name is (in the \PDF\ file) a string prefixed by a slash, like \typ
253 [option=PDF] {<< /Type /Foo >>}, for instance keys in a dictionary or keywords in
254 an array or constant values.
255
256 \starttyping[option=LUA]
257 function pdfe.getname ( <t:userdata>, <t:string> key | <t:integer> index )
258 return <t:string> -- value
259 end
260 \stoptyping
261
262 Normally you will use an index in an array and key in a dictionary but dictionaries
263 also accept an index. The size of an array or dictionary is available with the
264 usual \type {#} operator.
265
266 \starttyping[option=LUA]
267 function pdfe.getdictionary ( <t:userdata>, <t:string> key | <t:integer> index )
268 return <t:userdata> -- dictionary
269 end
270
271 function pdfe.getarray ( <t:userdata>, <t:string> key | <t:integer> index )
272 return <t:userdata> -- array
273 end
274
275 function pdfe.getstream ( <t:userdata>, <t:string> key | <t:integer> index )
276 return
277 <t:userdata> -- stream
278 <t:userdata> -- dictionary
279 end
280 \stoptyping
281
282 These commands return dictionaries, arrays and streams, which are dictionaries
283 with a blob of data attached.
284
285 Before we come to an alternative access mode, we mention that the objects provide
286 access in a different way too, for instance this is valid:
287
288 \starttyping[option=LUA]
289 print(pdfe.open("foo.pdf").Catalog.Type)
290 \stoptyping
291
292 At the topmost level there are \type {Catalog}, \type {Info}, \type {Trailer}
293 and \type {Pages}, so this is also okay:
294
295 \starttyping[option=LUA]
296 print(pdfe.open("foo.pdf").Pages[1])
297 \stoptyping
298
299 \stopsubsection
300
301 \startsubsection[title={Streams}]
302
303 Streams are sort of special. When your index or key hits a stream you get back a
304 stream object and dictionary object. The dictionary you can access in the usual
305 way and for the stream there are the following methods:
306
307 \starttyping[option=LUA]
308 function pdfe.openstream ( <t:userdata> stream, <t:boolean> decode)
309 return <t:boolean> okay
310 end
311
312 function pdfe.closestream ( <t:userdata> stream )
313 -- no return values
314 end
315
316 function pdfe.readfromstream ( <t:userdata> stream )
317 return
318 <t:string> str,
319 <t:integer> size
320 end
321
322 function pdfe.readwholestream ( <t:userdata> stream, <t:boolean> decode)
323 return
324 <t:string> str,
325 <t:integer> size
326 end
327 \stoptyping
328
329 You either read in chunks, or you ask for the whole. When reading in chunks, you
330 need to open and close the stream yourself. The \type {decode} parameter controls
331 if the stream data gets uncompressed.
332
333 As with dictionaries, you can access fields in a stream dictionary in the usual
334 \LUA\ way too. You get the content when you \quote {call} the stream. You can
335 pass a boolean that indicates if the stream has to be decompressed.
336
337 \stopsubsection
338
339 \startsubsection[title={Low level getters}]
340
341 In addition to the getters described before, there is also a bit lower level
342 interface available.
343
344 \starttyping[option=LUA]
345 function pdfe.getfromdictionary ( <t:userdata>, <t:integer> index )
346 return
347 <t:string> key,
348 <t:string> type,
349 <t:whatever> value,
350 <t:whatever> detail
351 end
352
353 function pdfe.getfromarray ( <t:userdata>, <t:integer> index )
354 return
355 <t:integer> type,
356 <t:whatever> value,
357 <t:integerr> detail
358 end
359 \stoptyping
360
361 The \type {type} is one of the following:
362
363 \startfourrows
364 \ctxlua{moduledata.pdfe.codes("getfieldtypes")}
365 \stopfourrows
366
367 This list was acquired with:
368
369 \starttyping[option=LUA]
370 function pdfe.getfieldtypes ( )
371 return <t:table> -- types
372 end
373 \stoptyping
374
375 Here \type {detail} is a bitset with possible bits:
376
377 \startfourrows
378 \ctxlua{moduledata.pdfe.codes("getencodingvalues")}
379 \stopfourrows
380
381 This time we used:
382
383 \starttyping[option=LUA]
384 function pdfe.getencodingvalues ( )
385 return <t:table> -- values
386 end
387 \stoptyping
388
389 \stopsubsection
390
391 \startsubsection[title={Getting tables}]
392
393 All entries in a dictionary or table can be fetched with the following commands
394 where the return values are a hashed or indexed table.
395
396 \starttyping[option=LUA]
397 function pdfe.dictionarytotable ( <t:userdata> )
398 return <t:table> -- hash
399 end
400
401 function pdfe.arraytotable ( <t:userdata> )
402 return <t:table> -- array
403 end
404 \stoptyping
405
406 You can get a list of pages with:
407
408 \starttyping[option=LUA]
409 function pdfe.pagestotable(<t:pdf> pdffile)
410 return {
411 {
412 <t:userdata>, -- dictionary
413 <t:integer>, -- size
414 <t:integer>, -- objectnumber
415 },
416 ...
417 }
418 end
419 \stoptyping
420
421 \stopsubsection
422
423 \startsubsection[title={References}]
424
425 In order to access a \PDF\ file efficiently there is lazy evaluation of
426 references so when you run into a reference as value or array entry you have to
427 resolve it explicitly. An unresolved references object can be resolved with:
428
429 \starttyping
430 function pdfe.getfromreference( <t:integer> reference ) -- NEEDS CHECKING
431 return
432 <t:integer>, -- type
433 <t:whatever>, -- value
434 <t:whatever> -- detail
435 \stoptyping
436
437 So, as second value you can get back a new \type {pdfe} userdata object that you
438 can query.
439
440 \stopsubsection
441
442 \stopsection
443
444 \stopdocument