-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflare-annot.lua
899 lines (784 loc) · 24.7 KB
/
flare-annot.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
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
--
-- 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')
--- Annotations
-- @section annotations
--- Returns annotation array of current page.
-- @return pdfe annotation array
function Page:getAnnots()
return pdfe.getpage(self.pdf, self.GinKV.page).Annots
end
--- Copies all annotations into the LaTeX document.
function Page:copyAnnots()
local annots = self:getAnnots()
if annots then
for idx = 1, #annots do
self.annotId = idx
local annot = annots[idx]
local objnum = luatex.getreference(annots, idx)
if not self:getUserInput('remove', self.page, self.annotId) then
self:insertAnnot(annot, objnum)
self:writeToCache_AnnotObjOld(objnum)
end
end
end
end
--- Inserts annotation `annot` into the LaTeX document.
-- @pdfe annot Annotation dictionary
-- @number objnum_old object number of annotation
function Page:insertAnnot(annot, objnum_old)
local annotbox = self:rect2tab(annot.Rect)
local mediabox = self:getMediaBox()
local pos = self:getTeXPos(mediabox, annotbox)
self.ctm = self:getCTM()
local scale = 0.5 * (self.ctm.a + self.ctm.d)
local data, objnum_new = self:formatAnnotation(annot, objnum_old)
if data == nil then
return
end
local annot = node.new(node.id('whatsit'), node.subtype('pdf_annot'))
annot.width = self:bp2sp(pos.width) * scale
annot.height = self:bp2sp(pos.height) * scale
annot.depth = tex.sp('0bp')
annot.data = data
annot.objnum = objnum_new
local hglue = node.new(node.id('glue'), node.subtype('userskip'))
hglue.width = self:bp2sp(pos.hshift) * scale
hglue.next = annot
local hbox = node.new(node.id('hlist'), node.subtype('box'))
hbox.dir = 'TLT'
hbox.width = tex.sp('0bp')
hbox.head = hglue
local vglue = node.new(node.id('glue'), node.subtype('userskip'))
vglue.width = self:bp2sp(pos.vshift) * scale * -1
vglue.next = hbox
local vbox = node.new(node.id('vlist'), node.subtype('box'))
vbox.dir = 'TLT'
vbox.width = tex.sp('0bp')
vbox.head = vglue
self.node_annot = annot
self.node_hglue = hglue
self.node_vglue = vglue
node.write(vbox)
self:writeToCache_AnnotObjNew(objnum_new)
end
--- Formats an arbitrary annotation.
-- Distributes work to more specialized formatting functions.
-- Returns a string to be used as the `data` field of a `pdf_annot` whatsit-node,
-- or `nil` if this annotation type is not supported.
-- @pdfe annot annotation dictionary
-- @number objnum object number of annotation
-- @return String
-- @return Object number
function Page:formatAnnotation(annot, objnum)
if self:getUserInput('@@@') then
return nil
end
local func = self:getAnnotFunc(annot)
if func then
local t, objnum = func(self, annot)
return self:formatTable(t, false), objnum
else
pkg.warning(
string.format("Annotations of type '%s' not supported", annot.Subtype))
return nil
end
end
function Page:getAnnotFunc(annot)
return load('return getAnnot' .. annot.Subtype, nil, 't', self)()
end
--- Converts a Lua table into _one_ string.
-- Functions like `getAnnotText()`, `getAnnotCircle()`, ... return tables
-- whose keys are PDF dictionary keys and whose values are formatted strings.
-- This function converts such a table into a long string to be used
-- as the `data` field of a `pdf_annot` whatsit node.
-- @table t table
-- @boolean delim dictionary delimiters
-- @return Formatted string
function Page:formatTable(t, delim)
if delim == nil then
delim = true
end
local spairs = pairs
if _G._UNITTEST then
spairs = sorted_pairs
end
if type(t) == 'table' then
if t.type == 'array' then
local str = ''
for _, v in spairs(t) do
str = str .. ' ' .. self:formatTable(v, true)
end
if delim then
return '[' .. str .. ' ]'
else
return str:sub(2)
end
else
local str = ''
for k, v in spairs(t) do
str = string.format('%s /%s %s', str, k, self:formatTable(v, true))
end
if delim then
return '<<' .. str .. ' >>'
else
return str:sub(2)
end
end
else
return t
end
end
--- Returns common annotation entries.
-- @pdfe annot annotation dictionary
-- @number objnum object number of annotation
-- @return Table
function Page:getAnnotCommonEntries(annot, objnum)
return {
Subtype = self:getName(annot, 'Subtype'),
Contents = self:getString(annot, 'Contents'),
P = self:getP(annot, 'P'),
NM = self:getString(annot, 'NM'),
M = self:getString(annot, 'M'),
F = self:getInteger(annot, 'F'),
AP = self:getDictionary(annot, 'AP'),
AS = self:getName(annot, 'AS'),
Border = self:formatBorder(annot, true),
C = self:getArray(annot, 'C'),
StructParent = self:getInteger(annot, 'StructParent'),
OC = self:getDictionary(annot, 'OC'),
}
end
--- Returns markup annotation entries.
-- @pdfe annot annotation dictionary
-- @number objnum object number
-- @return Table
function Page:getAnnotMarkupEntries(annot, objnum)
return {
T = self:getString(annot, 'T'),
Popup = self:getDictionary(annot, 'Popup'),
CA = self:getNumber(annot, 'CA'),
RC = self:getStringOrStream(annot, 'RC'),
CreationDate = self:getString(annot, 'CreationDate'),
IRT = self:formatIRT(annot, objnum),
Subj = self:getString(annot, 'Subj'),
RT = self:getName(annot, 'RT'),
IT = self:getName(annot, 'IT'),
ExData = self:getDictionary(annot, 'ExData'),
}
end
--- Formats the value of a `/P` entry, eg: `4 0 R`
-- @return Formatted string
function Page:getP()
-- Does it make sense to provide user input for /P at all?
local user = self:getUserInput('P')
if type(user) == 'string' then
return user
else
return string.format('%s 0 R', pdf.getpageref(self.page))
end
end
--- Formats the value of an `/IRT` entry, eg: `4 0 R`
-- @pdfe annot annotation dictionary
-- @number objnum object number of annotation
-- @return Formatted string
function Page:formatIRT(annot, objnum)
local ptype, pval, objnum_orig = pdfe.getfromdictionary(annot, 'IRT')
if ptype == nil then
return nil
end
if ptype ~= luatex.pdfeObjType.reference then
-- PDF spec says that /IRT is a dictionary! Really? Hmm ...
-- Let's suppose that /IRT is a reference, wait for
-- real world PDFs, and issue a warning for now:
pkg.warning(
string.format('/IRT shall be a reference, but is type %s', ptype))
pkg.bugs()
return nil
end
local annot_obj_new = self:getFromCache_AnnotObjNew(objnum)
if annot_obj_new == nil then
return nil
else
return self:makeRef(annot_obj_new)
end
end
--- Returns a `Square` annotation dictionary.
-- @pdfe annot annotation dictionary
-- @return Table
function Page:getAnnotSquare(annot)
local t = {
BS = self:getBorderStyle(annot, 'BS'),
IC = self:getArray(annot, 'IC'),
BE = self:getDictionary(annot, 'BE'),
RD = self:getArray(annot, 'RD'),
}
self:appendTable(t, self:getAnnotCommonEntries(annot))
self:appendTable(t, self:getAnnotMarkupEntries(annot))
return t, pdf.reserveobj('annot')
end
--- Returns a `Circle` annotation dictionary.
-- @pdfe annot annotation dictionary
-- @return Table
-- @return Object number
function Page:getAnnotCircle(annot)
return self:getAnnotSquare(annot)
end
--- Returns a a `Text` annotation dictionary.
-- @pdfe annot annotation dictionary
-- @return Table
-- @return Object number
function Page:getAnnotText(annot)
local t = {
Open = self:getBoolean(annot, 'Open'),
Name = self:getName(annot, 'Name'),
State = self:getString(annot, 'State'),
StateModel = self:getString(annot, 'StateModel'),
}
self:appendTable(t, self:getAnnotCommonEntries(annot))
self:appendTable(t, self:getAnnotMarkupEntries(annot))
return t, pdf.reserveobj('annot')
end
--- Return an array of coordinates, eg. `QuadPoints`.
-- @pdfe annot annotation dictionary
-- @string key key
-- @return Table
function Page:getCoordinatesArray(annot, key)
if annot[key] then
local ctm = self:readFromCache('ctm')
ctm = ctm or self.IdentityCTM
local coords = annot[key]
local newcoords = types.pdfarray:new()
local idx = 1
while idx <= #coords do
local x, y = coords[idx], coords[idx + 1]
local xn, yn = self:applyCTM(ctm, x, y)
newcoords[#newcoords + 1] = xn
newcoords[#newcoords + 1] = yn
idx = idx + 2
end
return newcoords
else
return nil
end
end
--- Return an array of array of coordinates, eg. `InkList`.
-- @pdfe annot annotation dictionary
-- @string key key
-- @return Table
function Page:getCoordinatesArrayArray(annot, key)
if annot[key] then
local ctm = self:readFromCache('ctm')
ctm = ctm or self.IdentityCTM
local t = types.pdfarray:new()
for _, coords in ipairs(annot[key]) do
local newcoords = types.pdfarray:new()
local idx = 1
while idx <= #coords do
local x, y = coords[idx], coords[idx + 1]
local xn, yn = self:applyCTM(ctm, x, y)
newcoords[#newcoords + 1] = xn
newcoords[#newcoords + 1] = yn
idx = idx + 2
end
t[#t + 1] = newcoords
end
return t
else
return nil
end
end
--- Returns a `Text Markup` annotion dictionary.
-- @pdfe annot annotation dictionary.
-- @return Table
-- @return Object number
function Page:getAnnotTextMarkup(annot)
local t = {
QuadPoints = self:getCoordinatesArray(annot, 'QuadPoints')
}
self:appendTable(t, self:getAnnotCommonEntries(annot))
self:appendTable(t, self:getAnnotMarkupEntries(annot))
return t, pdf.reserveobj('annot')
end
--- Returns a `FreeText` annotation dictionary.
-- @pdfe annot annotation dictionary
-- @return Table
-- @return Object number
function Page:getAnnotFreeText(annot)
local t = {
DA = self:getString(annot, 'DA'),
Q = self:getInteger(annot, 'Q'),
RC = self:getStringOrStream(annot, 'RC'),
DS = self:getString(annot, 'DS'),
CL = self:getCoordinatesArray(annot, 'CL'),
IT = self:getName(annot, 'IT'),
BE = self:getDictionary(annot, 'BE'),
RD = self:getAnnotFreeText_RD(annot, 'RD'),
BS = self:getBorderStyle(annot, 'BS'),
LE = self:getDictionary(annot, 'LE'),
}
self:appendTable(t, self:getAnnotCommonEntries(annot))
self:appendTable(t, self:getAnnotMarkupEntries(annot))
return t, pdf.reserveobj('annot')
end
--- Returns the `RD` rectangle of a `FreeText` annotation.
-- @pdfe obj dictionary
-- @string key key
-- @return Table
function Page:getAnnotFreeText_RD(obj, key)
local array = obj[key]
if not array then
return nil
end
local ctm = self:readFromCache('ctm')
ctm = ctm or self.IdentityCTM
local scale = 0.5 * (ctm.a + ctm.d)
local t = pdfarray:new()
for idx = 1, #array do
t[#t + 1] = array[idx] * scale
end
return t
end
--- Returns a `Link` annotation dictionary.
-- @pdfe annot annotation dictionary
-- @return Table
-- @return Object number
function Page:getAnnotLink(annot)
local t = {
A = self:getAction(annot['A']),
-- Dest
H = self:getName(annot, 'H'),
PA = self:getDictionary(annot, 'PA'),
QuadPoints = self:getCoordinatesArray(annot, 'QuadPoints'),
}
self:appendTable(t, self:getAnnotCommonEntries(annot))
return t, pdf.reserveobj('annot')
end
--- Returns a `Line` annotation dicationary.
-- @pdfe annot annotation dictionary
-- @return Table
-- @return Object number
function Page:getAnnotLine(annot)
local t = {
L = self:getCoordinatesArray(annot, 'L'),
BS = self:getBorderStyle(annot, 'BS'),
LE = self:getArray(annot, 'LE'),
IC = self:getArray(annot, 'IC'),
LL = self:getNumber(annot, 'LL', true),
LLE = self:getNumber(annot, 'LLE', true),
Cap = self:getBoolean(annot, 'Cap'),
IT = self:getName(annot, 'IT'),
LLO = self:getNumber(annot, 'LLO', true),
CP = self:getName(annot, 'CP'),
-- TODO: Measure dictionary must be adjusted by the scaling factor
Measure = self:getDictionary(annot, 'Measure'),
CO = self:getArray(annot, 'CO', true),
}
self:appendTable(t, self:getAnnotCommonEntries(annot))
self:appendTable(t, self:getAnnotMarkupEntries(annot))
return t, pdf.reserveobj('annot')
end
--- Returns a `Highlight` annotation dictionary.
-- @pdfe annot annotation dictionary.
-- @return Table
-- @return Object number
function Page:getAnnotHighlight(annot)
return self:getAnnotTextMarkup(annot)
end
--- Returns an `Underline` annotation dictionary.
-- @pdfe annot annotation dictionary
-- @return Table
-- @return Object number
function Page:getAnnotUnderline(annot)
return self:getAnnotTextMarkup(annot)
end
--- Returns a `StrikeOut` annotation dictionary.
-- @pdfe annot annotation dictionary
-- @return Table
-- @return Object number
function Page:getAnnotStrikeOut(annot)
return self:getAnnotTextMarkup(annot)
end
--- Returns a `Squiggly` annotation dictionary.
-- @pdfe annot annotation dictionary
-- @return Table
-- @return Object number
function Page:getAnnotSquiggly(annot)
return self:getAnnotTextMarkup(annot)
end
--- Returns a `Polygon` annotation dictionary.
-- @pdfe annot annotation dictionary
-- @return Table
-- @return Object number
function Page:getAnnotPolygon(annot)
local t = {
Vertices = self:getCoordinatesArray(annot, 'Vertices'),
LE = self:getArray(annot, 'LE'),
BS = self:getBorderStyle(annot, 'BS'),
IC = self:getArray(annot, 'IC'),
BE = self:getDictionary(annot, 'BE', true),
IT = self:getName(annot, 'IT'),
-- TODO: Scaling of measure dicionary necessary.
}
self:appendTable(t, self:getAnnotCommonEntries(annot))
self:appendTable(t, self:getAnnotMarkupEntries(annot))
return t, pdf.reserveobj('annot')
end
--- Returns a `PolyLine` annotation dictionary.
-- @pdfe annot annotation dictionary
-- @return Table
-- @return Object number
function Page:getAnnotPolyLine(annot)
return self:getAnnotPolygon(annot)
end
--- Returns a `Stamp` annotation dictionary.
-- @pdfe annot annotation dictionary
-- @return Table
-- @return Object number
function Page:getAnnotStamp(annot)
local t = {
Name = self:getName(annot, 'Name'),
}
self:appendTable(t, self:getAnnotCommonEntries(annot))
self:appendTable(t, self:getAnnotMarkupEntries(annot))
return t, pdf.reserveobj('annot')
end
--- Returns an `Ink` annotation dictionary.
-- @pdfe annot annotation dictionary
-- @return Table
-- @return Object number
function Page:getAnnotInk(annot)
local t = {
InkList = self:getCoordinatesArrayArray(annot, 'InkList'),
BS = self:getBorderStyle(annot, 'BS'),
}
self:appendTable(t, self:getAnnotCommonEntries(annot))
self:appendTable(t, self:getAnnotMarkupEntries(annot))
return t, pdf.reserveobj('annot')
end
--- Returns a `FileAttachment` annotation table.
-- @pdfe annot annotation dictionary
-- @return Table.
-- @return Object number
function Page:getAnnotFileAttachment(annot)
local t = {
FS = self:getFileSpecification(annot, 'FS'),
Name = self:getName(annot, 'Name'),
}
self:appendTable(t, self:getAnnotCommonEntries(annot))
self:appendTable(t, self:getAnnotMarkupEntries(annot))
return t, pdf.reserveobj('annot')
end
--- Returns an `FS` (file specification) dicationary.
-- @pdfe dict dictionary
-- @string key key
-- @return Table
function Page:getFileSpecification(dict, key)
-- file spec string
local str = pdfe.getstring(dict, key)
if str then
return str
end
-- file spec dictionary
local dict = pdfe.getdictionary(dict, key)
local t = {
Type = '/Filespec',
FS = self:getName(dict, 'FS'),
F = self:getString(dict, 'F'),
UF = self:getString(dict, 'UF'),
DOS = self:getString(dict, 'DOS'),
Mac = self:getString(dict, 'Mac'),
Unix = self:getString(dict, 'Unix'),
ID = self:getArray(dict, 'ID'),
V = self:getBoolean(dict, 'V'),
EF = self:getEmbeddedFileDict(dict, 'EF'),
RF = self:getRelatedFileDict(dict, 'RF'),
Desc = self:getString(dict, 'Desc'),
CI = self:getDictionary(dict, 'CI'),
}
return t
end
--- Returns an `EF` (embedded file) dicionary
-- @pdfe dict dictionary
-- @string key key
-- @return Table
function Page:getEmbeddedFileDict(dict, key)
dict = pdfe.getdictionary(dict, key)
if dict == nil then
return nil
end
local t = {
F = self:getEmbeddedFileStreamDict(dict, 'F'),
UF = self:getEmbeddedFileStreamDict(dict, 'UF'),
DOS = self:getEmbeddedFileStreamDict(dict, 'DOS'),
Mac= self:getEmbeddedFileStreamDict(dict, 'Mac'),
Unix = self:getEmbeddedFileStreamDict(dict, 'Unix'),
}
return t
end
--- Returns an embedded file stream dictionary.
-- @pdfe dict dictionary
-- @string key key
-- @return Table
function Page:getEmbeddedFileStreamDict(dict, key)
if pdfe.type(dict[key]) == 'pdfe.stream' then
-- TODO: do not return an arbitrary stream, but a stream
-- with the special stream dictionary of Page:getEmbeddedFileParams()
return self:getStream(dict, key)
else
return nil
end
end
--- Returns an embedded file parameter dictionary.
-- @pdfe dict dictionary
-- @string key key
-- @return table
function Page:getEmbeddedFileParams(dict, key)
dict = pdfe.getdictionary(dict, key)
if dict == nil then
return nil
end
local t = {
Size = self:getInteger(dict, 'Size'),
CreationDate = self:getString(dict, 'CreationDate'),
ModDate = self:getString(dict, 'ModDate'),
Mac = self:getMacFileInfo(dict, 'Mac'),
CheckSum = self:getString(dict, 'CheckSum'),
}
return t
end
--- Returns a Mac file info dictionary.
-- @pdfe dict dictionary
-- @string key key
-- @return Table
function Page:getMacFileInfo(dict, key)
dict = pdfe.getdictionary(dict, key)
if dict == nil then
return nil
end
local t = {
Subtype = self:getInteger(dict, 'Subtype'),
Creator = self:getInteger(dict, 'Creator'),
ResFork = self:getStream(dict, 'ResFork'),
}
return t
end
--- Returns an `RF` (related files) dictionary.
-- @pdfe dict dictionary
-- @string key key
-- @return Table
function Page:getRelatedFileDict(dict, key)
dict = pdfe.getdictionary(dict, key)
if dict == nil then
return nil
end
local t = {
F = self:getRelatedFileArray(dict['F']),
UF = self:getRelatedFileArray(dict['UF']),
DOS = self:getRelatedFileArray(dict['DOS']),
Mac= self:getRelatedFileArray(dict['Mac']),
Unix = self:getRelatedFileArray(dict['Unix']),
}
return t
end
--- Returns a file array (an entry of an `RF` dictionary).
-- @pdfe array array
-- @return table
function Page:getRelatedFileArray(array)
local t = types.pdfarray:new()
for idx = 1, #array, 2 do
t[#t + 1] = self:getString(array, idx)
t[#t + 1] = self:getStream(array, idx + 1)
end
end
--- Returns a `Widget` annotation dictionary.
-- @pdfe annot annotation dictionary
-- @return Table
-- @return Object number
function Page:getAnnotWidget(annot)
local objnum = pdf.reserveobj('annot')
local t = {
H = self:getName(annot, 'H'),
MK = self:getAppearanceCharacteristicsDict(annot, 'MK'),
A = self:getDictionary(annot, 'A'),
AA = self:getDictionary(annot, 'AA'),
BS = self:getDictionary(annot, 'BS'),
Parent = self:createField(annot.Parent, objnum),
}
self:appendTable(t, self:getAnnotCommonEntries(annot))
return t, objnum
end
--- Creates a field object.
-- @pdfe field field dictionary
-- @number objnum_kid object number of kid
-- @return Reference
function Page:createField(field, objnum_kid)
if field == nil then
return nil
end
local t = self:getField(field, objnum_kid)
local objnum_field = pdf.immediateobj(self:formatTable(t))
self:addFieldToAcroForm(objnum_field)
return self:makeRef(objnum_field)
end
--- Add a field to the `Fields` entry of an `AcroForm` dictionary.
-- @number objnum object number of field
function Page:addFieldToAcroForm(objnum)
local str = string.format(
'\\ExplSyntaxOn' ..
'\\FLR_AddFieldToAcroForm:n{%d}', objnum)
tex.print(str)
end
--- Returns a field ('FT') dictionary.
-- @pdfe field field dictionary
-- @number objnum_kid object number of kid
-- @return Table
function Page:getField(field, objnum_kid)
return {
FT = self:getName(field, 'FT'),
Kids = types.pdfarray:new({self:makeRef(objnum_kid)}),
T = self:getString(field, 'T'),
TU = self:getString(field, 'TU'),
TM = self:getString(field, 'TM'),
Ff = self:getInteger(field, 'Ff'),
V = self:getObj(field, 'V'),
DV = self:getObj(field, 'DV'),
AA = self:getDictionary(field, 'AA'),
}
end
function Page:getAppearanceCharacteristicsDict(obj, key)
local annot = obj[key]
if annot == nil then
return nil
end
local t = {
R = self:getInteger(annot, 'R'),
BC = self:getArray(annot, 'BC'),
GB = self:getArray(annot, 'BG'),
CA = self:getString(annot, 'CA'),
RC = self:getString(annot, 'RC'),
AC = self:getString(annot, 'AC'),
I = self:getStream(annot, 'I'),
RI = self:getStream(annot, 'RI'),
IX = self:getStream(annot, 'IX'),
IF = self:getIconFitDict(annot, 'IF'),
TP = self:getInteger(annot, 'TP'),
}
return t
end
function Page:getIconFitDict(obj, key)
local annot = obj[key]
if annot == nil then
return nil
end
local t = {
SW = self:getName(annot, 'SW'),
S = self:getName(annot, 'S'),
A = self:getArray(annot, 'A'),
FB = self:getBoolean(annot, 'FB'),
}
return t
end
--- Formats a `/Border` item.
-- @pdfe annot annotation dictionary
-- @numbool scale scaling factor
-- @return Formatted string
function Page:formatBorder(annot, scale)
scale = scale or false
local user = self:getUserInput('Border')
if type(user) == 'string' then
return user
end
if type(user) == 'table' and user.op == 'scale' then
scale = user.val
end
return self:formatBorder_hlp(annot, scale)
end
--- Helper function for @{Page:formatBorder} which scales
-- a `/Border` array.
-- @pdfe annot aAnnotation dictionary
-- @numbool scale scaling factor
-- @return Formatted string
function Page:formatBorder_hlp(annot, scale)
local border = annot.Border
if border then
local hradius = pdfe.getnumber(border, 0)
local vradius = pdfe.getnumber(border, 1)
local width = pdfe.getnumber(border, 2)
local dash = pdfe.getarray(border, 3)
dash = self:formatBorderDash(dash, scale)
return string.format('[ %.5g %.5g %.5g %s]',
self:scaleNumber(hradius, scale),
self:scaleNumber(vradius, scale),
self:scaleNumber(width, scale),
dash)
else
return nil
end
end
--- Formats a border dash item.
-- @pdfe dash pdfe array of dash
-- @numbool scale scaling factor
-- @return Formated string
function Page:formatBorderDash(dash, scale)
local user = self:getUserInput('BorderDash')
if type(user) == 'string' then
return user
end
if type(user) == 'table' and user.op == 'scale' then
scale = user.val
end
if dash then
local val1 = pdfe.getnumber(dash, 0)
local val2 = pdfe.getnumber(dash, 1)
return string.format('[ %.5g %.5g ]',
self:scaleNumber(val1, scale),
self:scaleNumber(val2, scale))
else
return ''
end
end
function Page:getBorderStyle(annot, key)
local bs = annot[key]
if bs == nil then
return nil
end
local t = types.pdfdictionary:new({
Type = '/Border',
W = self:getNumber(bs, 'W', true),
S = self:getName(bs, 'S'),
D = self:getArray(bs, 'D', true),
})
return t
end
--- Auxiliary
-- @section Auxiliary
--- Appends table `tab2` to table `tab1`.
-- @table tab1
-- @table tab2
function Page:appendTable(tab1, tab2)
for k, v in pairs(tab2) do
tab1[k] = v
end
end
return Page