-
Notifications
You must be signed in to change notification settings - Fork 0
/
flare-obj.lua
528 lines (452 loc) · 13.7 KB
/
flare-obj.lua
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
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
--
-- Copyright 2021-2023 Andreas MATTHIAS
--
-- This work may be distributed and/or modified under the
-- conditions of the LaTeX Project Public License, either version 1.3c
-- of this license or (at your option) any later version.
-- The latest version of this license is in
-- http://www.latex-project.org/lppl.txt
-- and version 1.3c or later is part of all distributions of LaTeX
-- version 2008 or later.
--
-- This work has the LPPL maintenance status `maintained'.
--
-- The Current Maintainer of this work is Andreas MATTHIAS.
--
---
-- @classmod Page
local Page = {}
local pkg = require('flare-pkg')
local types = require('flare-types')
local pdfarray = types.pdfarray
local pdfdictionary = types.pdfdictionary
local luatex = require('flare-luatex')
--- Keys which are ignored and not copied.
local ignoredKeys = {
Length = true,
P = true,
}
--- Basic types
-- @section basic types
--- Returns a boolean value.
-- @pdfe obj dictionary or Array
-- @keyidx key key or index (one-based indexing)
-- @return Boolean or user string
function Page:getBoolean(obj, key)
local user = self:getUserInput(key)
if type(user) == 'string' then
return user
else
key = self:zero_based_indexing(obj, key)
return pdfe.getboolean(obj, key)
end
end
--- Returns an integer.
-- @pdfe obj dictionary or array
-- @keyidx key key or index (one-based indexing)
-- @number scale scaling factor
-- @return Integer or user string
function Page:getInteger(obj, key, scale)
local user = self:getUserInput(key)
if type(user) == 'string' then
return user
else
key = self:zero_based_indexing(obj, key)
return self:scaleNumber(pdfe.getinteger(obj, key), scale)
end
end
--- Returns a number.
-- @pdfe obj dictionary or array
-- @keyidx key key or index (one-based indexing)
-- @number scale scaling factor
-- @return Number or user string
function Page:getNumber(obj, key, scale)
local user = self:getUserInput(key)
if type(user) == 'string' then
return user
else
key = self:zero_based_indexing(obj, key)
return self:scaleNumber(pdfe.getnumber(obj, key), scale)
end
end
--- Returns a formatted pdf name object, eg: `/Bar`.
-- @pdfe obj dictionary or array
-- @keyidx key key or index (one-based indexing)
-- @return Formatted string
function Page:getName(obj, key)
local user = self:getUserInput(key)
if type(user) == 'string' then
return user
else
key = self:zero_based_indexing(obj, key)
local val = pdfe.getname(obj, key)
if val then
return string.format('/%s', val)
else
return nil
end
end
end
--- Returns a pdf string, eg: `(test)`.
-- @pdfe obj dictionary or array
-- @keyidx key key or index (one-based indexing)
-- @return Formatted string
function Page:getString(obj, key)
local user = self:getUserInput(key)
if type(user) == 'string' then
return user
else
key = self:zero_based_indexing(obj, key)
local str, hex = pdfe.getstring(obj, key, false)
if str then
str, hex = self:clean_utf16(str, hex)
if hex then
return string.format('<%s>', str)
else
return string.format('(%s)', str)
end
else
return nil
end
end
end
--- Checks if string is utf-16 encoded.
-- @string str string
-- @return Boolean
function Page:is_utf16(str)
if str:sub(1, 2) == '\xFE\xFF' then
return true
else
return false
end
end
--- Converts a utf-16 string into a hex string.
-- @string str UTF-16 string
-- @return Hex-string
function Page:utf16_to_hex(str)
t = {}
for c in str:gmatch('.') do
t[#t + 1] = string.format('%02X', string.byte(c))
end
return table.concat(t)
end
--- Checks, if string `str` is a utf-16 string and converts it into
-- a hex string. Otherwise return the string unmodified.
-- Parameter `str` and `hex` have the same meaning as in
-- `pdfe.getstring()`.
-- @string str
-- @boolean hex
-- @return String
-- @return Boolean
function Page:clean_utf16(str, hex)
if self:is_utf16(str) then
return self:utf16_to_hex(str), true
else
return str, hex
end
end
--- Scales a number.
-- If `scale` is a number it acts as a scaling factor. If it is
-- `true` then the scaling factor of the page (image) is used
-- for scaling. If it is `false` no scaling is applied.
-- @numbool num number
-- @number scale scaling factor
-- @return Scaled number
function Page:scaleNumber(num, scale)
if num == nil then
return nil
elseif tonumber(scale) then
return num * scale
elseif scale == true then
return num * 0.5 * (self.ctm.a + self.ctm.d)
else
return num
end
end
--- Returns a @{Types:pdfarray}.
-- @pdfe obj dictionary or array
-- @keyidx key key or index (one-based indexing)
-- @number scale scaling factor
-- @return @{Types:pdfarray}
function Page:getArray(obj, key, scale)
local user = self:getUserInput(key)
if type(user) == 'string' then
return user
else
if obj == nil then
return nil
end
local array = obj[key]
if array then
local t = pdfarray:new()
for idx = 1, #array do
t[#t + 1] = self:getObj(array, idx, scale)
end
return t
else
return nil
end
end
end
--- Returns a @{Types:pdfdictionary}.
-- Contrary to most other functions which use an indirect object
-- reference (`obj[key]`), this function uses the object directly (`dict`).
-- @pdfe array array
-- @number scale scaling factor
-- @return @{Types:pdfdictionary}
function Page:getArray2(array, scale)
local t = pdfarray:new()
for idx, v in ipairs(pdfe.arraytotable(array)) do
t[#t + 1] = self:getObj(array, idx, scale)
end
return t
end
--- Returns a @{Types:pdfdictionary}.
-- @pdfe obj dictionary or array
-- @keyidx key key or index (one-based indexing)
-- @number scale scaling factor
-- @return @{Types:pdfdictionary}
function Page:getDictionary(obj, key, scale)
local user = self:getUserInput(key)
if type(user) == 'string' then
return user
else
if obj == nil then
return nil
end
local dict = obj[key]
if dict == nil then
return nil
end
local t = pdfdictionary:new()
for k, _ in pairs(pdfe.dictionarytotable(dict)) do
t[k] = self:getObj(dict, k, scale)
end
return t
end
end
--- Returns a @{Types:pdfdictionary}.
-- Contrary to most other functions which use an indirect object
-- reference (`obj[key]`), this function uses the object directly (`dict`).
-- @pdfe dict dicionary
-- @number scale scaling factor
-- @return @{Types:pdfdictionary}
function Page:getDictionary2(dict, scale)
local t = pdfdictionary:new()
for k, _ in pairs(pdfe.dictionarytotable(dict)) do
t[k] = self:getObj(dict, k, scale)
end
return t
end
--- Copies a stream and returns a reference to it.
-- @pdfe obj dictionary
-- @string key key
-- @number scale scaling factor
-- @return Formatted pdf reference, eg `9 0 R`
function Page:getStream(obj, key, scale)
local user = self:getUserInput(key)
if type(user) == 'string' then
return user
else
local stream, dict = pdfe.getstream(obj, key)
local content = pdfe.readwholestream(stream, true)
local dict = self:getDictionary2(dict, scale)
local n = pdf.immediateobj('stream', content, self:formatTable(dict, false))
return self:makeRef(n)
end
end
--- Returns a reference, eg: `3 0 R`.
-- As a side effect it copies the referenced pdf object.
-- @pdfe obj dictionary
-- @string key key
-- @number scale scaling factor
-- @return Formatted string
function Page:getReference(obj, key, scale)
local user = self:getUserInput(key)
if type(user) == 'string' then
return user
else
local _, ref, _ = luatex.getfromobj(obj, key)
local ptype, pobj, pdetail = pdfe.getfromreference(ref)
if ptype == luatex.pdfeObjType.none or
ptype == luatex.pdfeObjType.null then
return nil
elseif ptype == luatex.pdfeObjType.boolean or
ptype == luatex.pdfeObjType.integer or
ptype == luatex.pdfeObjType.number or
ptype == luatex.pdfeObjType.name or
ptype == luatex.pdfeObjType.string then
local n = pdf.immediateobj(string.format('%s', pvalue))
return self:makeRef(n)
elseif ptype == luatex.pdfeObjType.array then
local array = self:getArray2(pobj, scale)
local str = self:formatTable(array)
local n = pdf.immediateobj(str)
return string.format('%s 0 R', n)
elseif ptype == luatex.pdfeObjType.dictionary then
local dict = self:getDictionary2(pobj, scale)
local str = self:formatTable(dict)
local n = pdf.immediateobj(str)
return string.format('%s 0 R', n)
elseif ptype == luatex.pdfeObjType.stream then
return self:getStream(obj, key)
elseif ptype == luatex.pdfeObjType.reference then
-- Note, that getfromreference() never returns a reference.
-- If there are nested references, getfromreference() will
-- always return the final pdf object.
pkg.error('Internal error: Page:formatRefrence(), reference of reference')
else
pkg.error('Internal error: Page:formatRefrence(), type unknown:' .. ptype)
end
end
end
--- Returns a string or a reference to a stream.
-- Distributes work to @{Page:getString} and @{Page:getStream}.
-- @pdfe obj dictionary
-- @string key key
-- @return String or reference to stream
function Page:getStringOrStream(obj, key)
if ignoredKeys[key] then
return nil
end
local user = self:getUserInput(key)
if type(user) == 'string' then
return user
else
local val = pdfe.getstring(obj, key)
if val ~= nil then
return self:getString(obj, key)
end
local val =pdfe.getstream(obj, key)
if val ~= nil then
return self:getStream(obj, key)
end
return nil
end
end
--- Returns a pdf object.
-- @pdfe obj dictionary or array
-- @keyidx key key or index (one-based indexing)
-- @number scale scaling factor
-- @return Pdf object
function Page:getObj(obj, key, scale)
local ptype, _, _ = luatex.getfromobj(obj, key)
if ptype == nil or
ptype == luatex.pdfeObjType.none or
ptype == luatex.pdfeObjType.null then
return ''
elseif ptype == luatex.pdfeObjType.boolean then
return self:getBoolean(obj, key)
elseif ptype == luatex.pdfeObjType.integer then
return self:getInteger(obj, key, scale)
elseif ptype == luatex.pdfeObjType.number then
return self:getNumber(obj, key, scale)
elseif ptype == luatex.pdfeObjType.name then
return self:getName(obj, key)
elseif ptype == luatex.pdfeObjType.string then
return self:getString(obj, key)
elseif ptype == luatex.pdfeObjType.array then
return self:getArray(obj, key, scale)
elseif ptype == luatex.pdfeObjType.dictionary then
return self:getDictionary(obj, key, scale)
elseif ptype == luatex.pdfeObjType.stream then
return self:getStream(obj, key, scale)
elseif ptype == luatex.pdfeObjType.reference then
return self:getReference(obj, key, scale)
else
pkg.error('Not a valid pdfe type: ' .. ptype)
end
end
--- Auxiliary
-- @section auxiliary
--- Throws an error if `obj` is a pdfe-array.
-- This is an internal error which can happen only if a developer added new
-- annotations incorrectly. It is not an end-user error.
-- @pdfe obj
function Page:errorIfArray(obj)
if pdfe.type(obj) == 'pdfe.array' then
pkg.error('Type pdfe.array not allowed')
end
end
--- Converts from one- to zero-based indexing.
-- Return `key` as a zero-based index if `key` is a `number`
-- (ie. `obj` is a `pdfe-array`). Otherwise (ie. `obj` is a
-- dictionary) returns `key` unmodified.
-- @pdfe obj dictionary or array
-- @keyidx key key or index (one-based indexing)
-- @return Key or index (zero-based indexing)
function Page:zero_based_indexing(obj, key)
if type(key) == 'number' then
return key - 1
else
return key
end
end
--- Returns a PDF reference.
-- @number objnum object number
-- @return String
function Page:makeRef(objnum)
if objnum then
return string.format('%d 0 R', objnum)
else
return 'null'
end
end
--- User input
-- @section user_input
--- Formats user input.
-- Normally returns a formatted string representing user input or
-- `nil` if no user input is available. But it might return a table
-- of type `{op = <op>, val = <val>}` if user input cannot be represented
-- as a formatted string, eg. the `scale` parameter.
-- @string key key
-- @return Formatted string or `nil`
function Page:getUserInput(key)
local page = self.GinKV.page
local annotId = self.annotId
local trails = {
{page, annotId},
{page, 'all'},
{'all', annotId},
{'all', 'all'}
}
local user = nil
for _, v in pairs(trails) do
local page = v[1]
local annotId = v[2]
if self:checkUserInput(page, annotId, key) then
user = self.FlareKV[page][annotId][key]
end
end
-- local user = self:getUserInput(key)
if user == nil then
return nil
elseif user.op == 'replace' then
return string.format('%s', user.val)
elseif user.op == 'ref' then
return string.format('%s 0 R', user.val)
elseif user.op == 'remove' then
if user.val == true then
return ''
else
return nil
end
else
return user
end
end
--- Returns `true` if user input available.
-- @numstr page page number or `'all'`
-- @numstr annotId annotation ID or `'all'`
-- @string key key
-- @return Boolean
function Page:checkUserInput(page, annotId, key)
if self.FlareKV[page] and
self.FlareKV[page][annotId] and
self.FlareKV[page][annotId][key] ~= nil then
return true
else
return false
end
end
return Page