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
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
|
if not modules then modules = { } end modules ['lpdf-ano'] = {
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"
}
-- when using rotation: \disabledirectives[refences.sharelinks] (maybe flag links)
-- todo: /AA << WC << ... >> >> : WillClose actions etc
-- internal references are indicated by a number (and turned into <autoprefix><number>)
-- we only flush internal destinations that are referred
local next, tostring, tonumber, rawget = next, tostring, tonumber, rawget
local rep, format, find = string.rep, string.format, string.find
local min = math.min
local lpegmatch = lpeg.match
local formatters = string.formatters
local backends, lpdf = backends, lpdf
local trace_references = false trackers.register("references.references", function(v) trace_references = v end)
local trace_destinations = false trackers.register("references.destinations", function(v) trace_destinations = v end)
local trace_bookmarks = false trackers.register("references.bookmarks", function(v) trace_bookmarks = v end)
local log_destinations = false directives.register("destinations.log", function(v) log_destinations = v end)
local report_reference = logs.reporter("backend","references")
local report_destination = logs.reporter("backend","destinations")
local report_bookmark = logs.reporter("backend","bookmarks")
local variables = interfaces.variables
local v_auto = variables.auto
local v_page = variables.page
local v_name = variables.name
local factor = number.dimenfactors.bp
local settings_to_array = utilities.parsers.settings_to_array
local allocate = utilities.storage.allocate
local setmetatableindex = table.setmetatableindex
local nodeinjections = backends.pdf.nodeinjections
local codeinjections = backends.pdf.codeinjections
local registrations = backends.pdf.registrations
local getpos = codeinjections.getpos
local gethpos = codeinjections.gethpos
local getvpos = codeinjections.getvpos
local javascriptcode = interactions.javascripts.code
local references = structures.references
local bookmarks = structures.bookmarks
local flaginternals = references.flaginternals
local usedinternals = references.usedinternals
local usedviews = references.usedviews
local runners = references.runners
local specials = references.specials
local handlers = references.handlers
local executers = references.executers
local nodepool = nodes.pool
----- pdfannotation_node = nodepool.pdfannotation
----- pdfdestination_node = nodepool.pdfdestination
----- latelua_node = nodepool.latelua
local latelua_function_node = nodepool.lateluafunction -- still node ... todo
local texgetcount = tex.getcount
local pdfdictionary = lpdf.dictionary
local pdfarray = lpdf.array
local pdfreference = lpdf.reference
local pdfunicode = lpdf.unicode
local pdfconstant = lpdf.constant
local pdfflushobject = lpdf.flushobject
local pdfshareobjectreference = lpdf.shareobjectreference
local pdfreserveobject = lpdf.reserveobject
local pdfpagereference = lpdf.pagereference
local pdfdelayedobject = lpdf.delayedobject
local pdfregisterannotation = lpdf.registerannotation -- forward definition (for the moment)
local pdfnull = lpdf.null
local pdfaddtocatalog = lpdf.addtocatalog
local pdfaddtonames = lpdf.addtonames
local pdfaddtopageattributes = lpdf.addtopageattributes
local pdfrectangle = lpdf.rectangle
-- todo: 3dview
local pdf_annot = pdfconstant("Annot")
local pdf_uri = pdfconstant("URI")
local pdf_gotor = pdfconstant("GoToR")
local pdf_goto = pdfconstant("GoTo")
local pdf_launch = pdfconstant("Launch")
local pdf_javascript = pdfconstant("JavaScript")
local pdf_link = pdfconstant("Link")
local pdf_n = pdfconstant("N")
local pdf_t = pdfconstant("T")
local pdf_fit = pdfconstant("Fit")
local pdf_named = pdfconstant("Named")
local autoprefix = "#"
-- Bah, I hate this kind of features .. anyway, as we have delayed resolving we
-- only support a document-wide setup and it has to be set before the first one
-- is used. Also, we default to a non-intrusive gray and the outline is kept
-- thin without dashing lines. This is as far as I'm prepared to go. This way
-- it can also be used as a debug feature.
local pdf_border_style = pdfarray { 0, 0, 0 } -- radius radius linewidth
local pdf_border_color = nil
local set_border = false
local function pdfborder()
set_border = true
return pdf_border_style, pdf_border_color
end
lpdf.border = pdfborder
directives.register("references.border",function(v)
if v and not set_border then
if type(v) == "string" then
local m = attributes.list[attributes.private('color')] or { }
local c = m and m[v]
local v = c and attributes.colors.value(c)
if v then
local r, g, b = v[3], v[4], v[5]
-- if r == g and g == b then
-- pdf_border_color = pdfarray { r } -- reduced, not not ... bugged viewers
-- else
pdf_border_color = pdfarray { r, g, b } -- always rgb
-- end
end
end
if not pdf_border_color then
pdf_border_color = pdfarray { .6, .6, .6 } -- no reduce to { 0.6 } as there are buggy viewers out there
end
pdf_border_style = pdfarray { 0, 0, .5 } -- < 0.5 is not show by acrobat (at least not in my version)
end
end)
-- the used and flag code here is somewhat messy in the sense
-- that it belongs in strc-ref but at the same time depends on
-- the backend so we keep it here
-- the caching is somewhat memory intense on the one hand but
-- it saves many small temporary tables so it might pay off
local pagedestinations = setmetatableindex(function(t,k)
k = tonumber(k)
if not k or k <= 0 then
return pdfnull()
end
local v = rawget(t,k)
if v then
-- report_reference("page number expected, got %s: %a",type(k),k)
return v
end
local v = k > 0 and pdfarray {
pdfreference(pdfpagereference(k)),
pdf_fit,
} or pdfnull()
t[k] = v
return v
end)
local pagereferences = setmetatableindex(function(t,k)
k = tonumber(k)
if not k or k <= 0 then
return nil
end
local v = rawget(t,k)
if v then
return v
end
local v = pdfdictionary { -- can be cached
S = pdf_goto,
D = pagedestinations[k],
}
t[k] = v
return v
end)
local defaultdestination = pdfarray { 0, pdf_fit }
-- fit is default (see lpdf-nod)
local destinations = { } -- to be used soon
local function pdfregisterdestination(name,reference)
local d = destinations[name]
if d then
report_destination("ignoring duplicate destination %a with reference %a",name,reference)
else
destinations[name] = reference
end
end
lpdf.registerdestination = pdfregisterdestination
local maxslice = 32 -- could be made configureable ... 64 is also ok
luatex.registerstopactions(function()
if log_destinations and next(destinations) then
local logsnewline = logs.newline
local log_destinations = logs.reporter("system","references")
local log_destination = logs.reporter("destination")
logs.pushtarget("logfile")
logsnewline()
log_destinations("start used destinations")
logsnewline()
local n = 0
for destination, pagenumber in table.sortedhash(destinations) do
log_destination("% 4i : %-5s : %s",pagenumber,usedviews[destination] or defaultview,destination)
n = n + 1
end
logsnewline()
log_destinations("stop used destinations")
logsnewline()
logs.poptarget()
report_destination("%s destinations saved in log file",n)
end
end)
local function pdfnametree(destinations)
local slices = { }
local sorted = table.sortedkeys(destinations)
local size = #sorted
if size <= 1.5*maxslice then
maxslice = size
end
for i=1,size,maxslice do
local amount = min(i+maxslice-1,size)
local names = pdfarray { }
for j=i,amount do
local destination = sorted[j]
local pagenumber = destinations[destination]
names[#names+1] = tostring(destination) -- tostring is a safeguard
names[#names+1] = pdfreference(pagenumber)
end
local first = sorted[i]
local last = sorted[amount]
local limits = pdfarray {
first,
last,
}
local d = pdfdictionary {
Names = names,
Limits = limits,
}
slices[#slices+1] = {
reference = pdfreference(pdfflushobject(d)),
limits = limits,
}
end
local function collectkids(slices,first,last)
local k = pdfarray()
local d = pdfdictionary {
Kids = k,
Limits = pdfarray {
slices[first].limits[1],
slices[last ].limits[2],
},
}
for i=first,last do
k[#k+1] = slices[i].reference
end
return d
end
if #slices == 1 then
return slices[1].reference
else
while true do
if #slices > maxslice then
local temp = { }
local size = #slices
for i=1,size,maxslice do
local kids = collectkids(slices,i,min(i+maxslice-1,size))
temp[#temp+1] = {
reference = pdfreference(pdfflushobject(kids)),
limits = kids.Limits,
}
end
slices = temp
else
return pdfreference(pdfflushobject(collectkids(slices,1,#slices)))
end
end
end
end
local function pdfdestinationspecification()
if next(destinations) then -- safeguard
local r = pdfnametree(destinations)
-- pdfaddtocatalog("Dests",r)
pdfaddtonames("Dests",r)
if not log_destinations then
destinations = nil
end
end
end
lpdf.nametree = pdfnametree
lpdf.destinationspecification = pdfdestinationspecification
lpdf.registerdocumentfinalizer(pdfdestinationspecification,"collect destinations")
-- todo
local destinations = { }
local f_xyz = formatters["<< /D [ %i 0 R /XYZ %0.3F %0.3F null ] >>"]
local f_fit = formatters["<< /D [ %i 0 R /Fit ] >>"]
local f_fitb = formatters["<< /D [ %i 0 R /FitB ] >>"]
local f_fith = formatters["<< /D [ %i 0 R /FitH %0.3F ] >>"]
local f_fitv = formatters["<< /D [ %i 0 R /FitV %0.3F ] >>"]
local f_fitbh = formatters["<< /D [ %i 0 R /FitBH %0.3F ] >>"]
local f_fitbv = formatters["<< /D [ %i 0 R /FitBV %0.3F ] >>"]
local f_fitr = formatters["<< /D [ %i 0 R /FitR %0.3F %0.3F %0.3F %0.3F ] >>"]
local v_standard = variables.standard
local v_frame = variables.frame
local v_width = variables.width
local v_minwidth = variables.minwidth
local v_height = variables.height
local v_minheight = variables.minheight
local v_fit = variables.fit
local v_tight = variables.tight
-- nicer is to create dictionaries and set properties but it's a bit overkill
-- The problem with the following settings is that they are guesses: we never know
-- if a box is part of something larger that needs to be in view, or that we are
-- dealing with a vbox or vtop so the used h/d values cannot be trusted in a tight
-- view. Of course some decent additional offset would be nice so maybe i'll add
-- that some day. I never use anything else than 'fit' anyway as I think that the
-- document should fit the device (and vice versa). In fact, with todays swipe
-- and finger zooming this whole view is rather useless and as with any zooming
-- one looses the overview and keeps zooming.
-- todo: scaling
-- local destinationactions = {
-- [v_standard] = function(r,w,h,d) return f_xyz (r,gethpos()*factor,(getvpos()+h)*factor) end, -- local left,top with no zoom
-- [v_frame] = function(r,w,h,d) return f_fitr (r,pdfrectangle(w,h,d)) end, -- fit rectangle in window
-- [v_width] = function(r,w,h,d) return f_fith (r,(getvpos()+h)*factor) end, -- top coordinate, fit width of page in window
-- [v_minwidth] = function(r,w,h,d) return f_fitbh(r,(getvpos()+h)*factor) end, -- top coordinate, fit width of content in window
-- [v_height] = function(r,w,h,d) return f_fitv (r,gethpos()*factor) end, -- left coordinate, fit height of page in window
-- [v_minheight] = function(r,w,h,d) return f_fitbv(r,gethpos()*factor) end, -- left coordinate, fit height of content in window [v_fit] = f_fit, -- fit page in window
-- [v_tight] = f_fitb, -- fit content in window
-- [v_fit] = f_fit,
-- }
local destinationactions = {
[v_standard] = function(r,w,h,d,o) -- local left,top with no zoom
local tx, ty = getpos()
return f_xyz(r,tx*factor,(ty+h+2*o)*factor) -- we can assume margins
end,
[v_frame] = function(r,w,h,d,o) -- fit rectangle in window
return f_fitr(r,pdfrectangle(w,h,d,o))
end,
[v_width] = function(r,w,h,d,o) -- top coordinate, fit width of page in window
return f_fith(r,(getvpos()+h+o)*factor)
end,
[v_minwidth] = function(r,w,h,d,o) -- top coordinate, fit width of content in window
return f_fitbh(r,(getvpos()+h+o)*factor)
end,
[v_height] = function(r,w,h,d,o) -- left coordinate, fit height of page in window
return f_fitv(r,(gethpos())*factor)
end,
[v_minheight] = function(r,w,h,d,o) -- left coordinate, fit height of content in window
return f_fitbv(r,(gethpos())*factor)
end,
[v_tight] = f_fitb, -- fit content in window
[v_fit] = f_fit, -- fit content in window
}
local mapping = {
[v_standard] = v_standard, xyz = v_standard,
[v_frame] = v_frame, fitr = v_frame,
[v_width] = v_width, fith = v_width,
[v_minwidth] = v_minwidth, fitbh = v_minwidth,
[v_height] = v_height, fitv = v_height,
[v_minheight] = v_minheight, fitbv = v_minheight,
[v_fit] = v_fit, fit = v_fit,
[v_tight] = v_tight, fitb = v_tight,
}
local defaultview = v_fit
local defaultaction = destinationactions[defaultview]
local offset = 0 -- 65536*5
directives.register("destinations.offset", function(v)
offset = string.todimen(v) or 0
end)
-- A complication is that we need to use named destinations when we have views so we
-- end up with a mix. A previous versions just output multiple destinations but now
-- that we moved all to here we can be more sparse.
local pagedestinations = setmetatableindex(function(t,k) -- not the same as the one above!
local v = pdfdelayedobject(f_fit(k))
t[k] = v
return v
end)
local function flushdestination(width,height,depth,names,view)
local r = pdfpagereference(texgetcount("realpageno"))
if (references.innermethod ~= v_name) and (view == defaultview or not view or view == "") then
r = pagedestinations[r]
else
local action = view and destinationactions[view] or defaultaction
r = pdfdelayedobject(action(r,width,height,depth,offset))
end
for n=1,#names do
local name = names[n]
if name then
pdfregisterdestination(name,r)
end
end
end
function nodeinjections.destination(width,height,depth,names,view)
-- todo check if begin end node / was comment
view = view and mapping[view] or defaultview
if trace_destinations then
report_destination("width %p, height %p, depth %p, names %|t, view %a",width,height,depth,names,view)
end
local method = references.innermethod
local noview = view == defaultview
local doview = false
-- we could save some aut's by using a name when given but it doesn't pay off apart
-- from making the code messy and tracing hard .. we only save some destinations
-- which we already share anyway
if method == v_page then
for n=1,#names do
local name = names[n]
local used = usedviews[name]
if used and used ~= true then
-- already done, maybe a warning
elseif type(name) == "number" then
-- if noview then
-- usedviews[name] = view
-- names[n] = false
-- else
usedviews[name] = view
names[n] = false
-- end
else
usedviews[name] = view
end
end
elseif method == v_name then
for n=1,#names do
local name = names[n]
local used = usedviews[name]
if used and used ~= true then
-- already done, maybe a warning
elseif type(name) == "number" then
local used = usedinternals[name]
usedviews[name] = view
names[n] = autoprefix .. name
doview = true
else
usedviews[name] = view
doview = true
end
end
else
for n=1,#names do
local name = names[n]
if usedviews[name] then
-- already done, maybe a warning
elseif type(name) == "number" then
if noview then
usedviews[name] = view
names[n] = false
else
local used = usedinternals[name]
if used and used ~= defaultview then
usedviews[name] = view
names[n] = autoprefix .. name
doview = true
else
-- names[n] = autoprefix .. name
names[n] = false
end
end
else
usedviews[name] = view
doview = true
end
end
end
if doview then
return latelua_function_node(function() flushdestination(width,height,depth,names,view) end)
end
end
-- we could share dictionaries ... todo
local function pdflinkpage(page)
return pagereferences[page]
end
local function pdflinkinternal(internal,page)
local method = references.innermethod
if internal then
flaginternals[internal] = true -- for bookmarks and so
local used = usedinternals[internal]
if used == defaultview or used == true then
return pagereferences[page]
else
if type(internal) ~= "string" then
internal = autoprefix .. internal
end
return pdfdictionary {
S = pdf_goto,
D = internal,
}
end
else
return pagereferences[page]
end
end
local function pdflinkname(destination,internal,page)
local method = references.innermethod
if method == v_auto then
local used = defaultview
if internal then
flaginternals[internal] = true -- for bookmarks and so
used = usedinternals[internal] or defaultview
end
if used == defaultview then -- or used == true then
return pagereferences[page]
else
return pdfdictionary {
S = pdf_goto,
D = destination,
}
end
elseif method == v_name then
-- flaginternals[internal] = true -- for bookmarks and so
return pdfdictionary {
S = pdf_goto,
D = destination,
}
else
return pagereferences[page]
end
end
-- annotations
local function pdffilelink(filename,destination,page,actions)
if not filename or filename == "" or file.basename(filename) == tex.jobname then
return false
end
filename = file.addsuffix(filename,"pdf")
if not destination or destination == "" then
destination = pdfarray { (page or 0) - 1, pdf_fit }
end
return pdfdictionary {
S = pdf_gotor, -- can also be pdf_launch
F = filename,
D = destination or defaultdestination, -- D is mandate
NewWindow = actions.newwindow and true or nil,
}
end
local function pdfurllink(url,destination,page)
if not url or url == "" then
return false
end
if destination and destination ~= "" then
url = url .. "#" .. destination
end
return pdfdictionary {
S = pdf_uri,
URI = url,
}
end
local function pdflaunch(program,parameters)
if not program or program == "" then
return false
end
return pdfdictionary {
S = pdf_launch,
F = program,
D = ".",
P = parameters ~= "" and parameters or nil
}
end
local function pdfjavascript(name,arguments)
local script = javascriptcode(name,arguments) -- make into object (hash)
if script then
return pdfdictionary {
S = pdf_javascript,
JS = script,
}
end
end
local function pdfaction(actions)
local nofactions = #actions
if nofactions > 0 then
local a = actions[1]
local action = runners[a.kind]
if action then
action = action(a,actions)
end
if action then
local first = action
for i=2,nofactions do
local a = actions[i]
local what = runners[a.kind]
if what then
what = what(a,actions)
end
if action == what then
-- ignore this one, else we get a loop
elseif what then
action.Next = what
action = what
else
-- error
return nil
end
end
return first, actions.n
end
end
end
lpdf.action = pdfaction
function codeinjections.prerollreference(actions) -- share can become option
if actions then
local main, n = pdfaction(actions)
if main then
local bs, bc = pdfborder()
main = pdfdictionary {
Subtype = pdf_link,
Border = bs,
C = bc,
H = (not actions.highlight and pdf_n) or nil,
A = pdfshareobjectreference(main),
F = 4, -- print (mandate in pdf/a)
}
return main("A"), n
end
end
end
-- local function use_normal_annotations()
--
-- local function reference(width,height,depth,prerolled) -- keep this one
-- if prerolled then
-- if trace_references then
-- report_reference("width %p, height %p, depth %p, prerolled %a",width,height,depth,prerolled)
-- end
-- return pdfannotation_node(width,height,depth,prerolled)
-- end
-- end
--
-- local function finishreference()
-- end
--
-- return reference, finishreference
--
-- end
-- eventually we can do this for special refs only
local hashed = { }
local nofunique = 0
local nofused = 0
local nofspecial = 0
local share = true
local f_annot = formatters["<< /Type /Annot %s /Rect [ %0.3F %0.3F %0.3F %0.3F ] >>"]
directives.register("references.sharelinks", function(v) share = v end)
setmetatableindex(hashed,function(t,k)
local v = pdfdelayedobject(k)
if share then
t[k] = v
end
nofunique = nofunique + 1
return v
end)
local function finishreference(width,height,depth,prerolled) -- %0.2f looks okay enough (no scaling anyway)
local annot = hashed[f_annot(prerolled,pdfrectangle(width,height,depth))]
nofused = nofused + 1
return pdfregisterannotation(annot)
end
local function finishannotation(width,height,depth,prerolled,r)
local annot = f_annot(prerolled,pdfrectangle(width,height,depth))
if r then
pdfdelayedobject(annot,r)
else
r = pdfdelayedobject(annot)
end
nofspecial = nofspecial + 1
return pdfregisterannotation(r)
end
function nodeinjections.reference(width,height,depth,prerolled)
if prerolled then
if trace_references then
report_reference("link: width %p, height %p, depth %p, prerolled %a",width,height,depth,prerolled)
end
return latelua_function_node(function() finishreference(width,height,depth,prerolled) end)
end
end
function nodeinjections.annotation(width,height,depth,prerolled,r)
if prerolled then
if trace_references then
report_reference("special: width %p, height %p, depth %p, prerolled %a",width,height,depth,prerolled)
end
return latelua_function_node(function() finishannotation(width,height,depth,prerolled,r or false) end)
end
end
-- beware, we register during a latelua sweep so we have to make sure that
-- we finalize after that (also in a latelua for the moment as we have no
-- callback yet)
local annotations = nil
function lpdf.registerannotation(n)
if annotations then
annotations[#annotations+1] = pdfreference(n)
else
annotations = pdfarray { pdfreference(n) } -- no need to use lpdf.array cum suis
end
end
pdfregisterannotation = lpdf.registerannotation
function lpdf.annotationspecification()
if annotations then
local r = pdfdelayedobject(tostring(annotations)) -- delayed so okay in latelua
pdfaddtopageattributes("Annots",pdfreference(r))
annotations = nil
end
end
lpdf.registerpagefinalizer(lpdf.annotationspecification,"finalize annotations")
statistics.register("pdf annotations", function()
if nofused > 0 or nofspecial > 0 then
return format("%s links (%s unique), %s special",nofused,nofunique,nofspecial)
else
return nil
end
end)
-- runners and specials
local splitter = lpeg.splitat(",",true)
runners["inner"] = function(var,actions)
local internal = false
local name = nil
local method = references.innermethod
local vi = var.i
local page = var.r
if vi then
local vir = vi.references
if vir then
-- todo: no need for it when we have a real reference ... although we need
-- this mess for prefixes anyway
local reference = vir.reference
if reference and reference ~= "" then
reference = lpegmatch(splitter,reference) or reference
var.inner = reference
local prefix = var.p
if prefix and prefix ~= "" then
var.prefix = prefix
name = prefix .. ":" .. reference
else
name = reference
end
end
internal = vir.internal
if internal then
flaginternals[internal] = true
end
end
end
if name then
return pdflinkname(name,internal,page)
elseif internal then
return pdflinkinternal(internal,page)
elseif page then
return pdflinkpage(page)
else
-- real bad
end
end
runners["inner with arguments"] = function(var,actions)
report_reference("todo: inner with arguments")
return false
end
runners["outer"] = function(var,actions)
local file, url = references.checkedfileorurl(var.outer,var.outer)
if file then
return pdffilelink(file,var.arguments,nil,actions)
elseif url then
return pdfurllink(url,var.arguments,nil,actions)
end
end
runners["outer with inner"] = function(var,actions)
return pdffilelink(references.checkedfile(var.outer),var.inner,var.r,actions)
end
runners["special outer with operation"] = function(var,actions)
local handler = specials[var.special]
return handler and handler(var,actions)
end
runners["special outer"] = function(var,actions)
report_reference("todo: special outer")
return false
end
runners["special"] = function(var,actions)
local handler = specials[var.special]
return handler and handler(var,actions)
end
runners["outer with inner with arguments"] = function(var,actions)
report_reference("todo: outer with inner with arguments")
return false
end
runners["outer with special and operation and arguments"] = function(var,actions)
report_reference("todo: outer with special and operation and arguments")
return false
end
runners["outer with special"] = function(var,actions)
report_reference("todo: outer with special")
return false
end
runners["outer with special and operation"] = function(var,actions)
report_reference("todo: outer with special and operation")
return false
end
runners["special operation"] = runners["special"]
runners["special operation with arguments"] = runners["special"]
function specials.internal(var,actions) -- better resolve in strc-ref
local i = tonumber(var.operation)
local v = i and references.internals[i]
if not v then
-- error
report_reference("no internal reference %a",i or "<unset>")
else
flaginternals[i] = true
return pdflinkinternal(i,v.references.realpage)
end
end
-- realpage already resolved
specials.i = specials.internal
local pages = references.pages
function specials.page(var,actions)
local file = var.f
if file then
return pdffilelink(references.checkedfile(file),nil,var.operation,actions)
else
local p = var.r
if not p then -- todo: call special from reference code
p = pages[var.operation]
if type(p) == "function" then -- double
p = p()
else
p = references.realpageofpage(tonumber(p))
end
end
return pdflinkpage(p or var.operation)
end
end
function specials.realpage(var,actions)
local file = var.f
if file then
return pdffilelink(references.checkedfile(file),nil,var.operation,actions)
else
return pdflinkpage(var.operation)
end
end
function specials.userpage(var,actions)
local file = var.f
if file then
return pdffilelink(references.checkedfile(file),nil,var.operation,actions)
else
local p = var.r
if not p then -- todo: call special from reference code
p = var.operation
if p then -- no function and special check here. only numbers
p = references.realpageofpage(tonumber(p))
end
-- if p then
-- var.r = p
-- end
end
return pdflinkpage(p or var.operation)
end
end
function specials.deltapage(var,actions)
local p = tonumber(var.operation)
if p then
p = references.checkedrealpage(p + texgetcount("realpageno"))
return pdflinkpage(p)
end
end
-- sections
-- function specials.section(var,actions)
-- local sectionname = var.operation
-- local destination = var.arguments
-- local internal = structures.sections.internalreference(sectionname,destination)
-- if internal then
-- var.special = "internal"
-- var.operation = internal
-- var.arguments = nil
-- specials.internal(var,actions)
-- end
-- end
specials.section = specials.internal -- specials.section just need to have a value as it's checked
-- todo, do this in references namespace ordered instead (this is an experiment)
local splitter = lpeg.splitat(":")
function specials.order(var,actions) -- references.specials !
local operation = var.operation
if operation then
local kind, name, n = lpegmatch(splitter,operation)
local order = structures.lists.ordered[kind]
order = order and order[name]
local v = order[tonumber(n)]
local r = v and v.references.realpage
if r then
var.operation = r -- brrr, but test anyway
return specials.page(var,actions)
end
end
end
function specials.url(var,actions)
return pdfurllink(references.checkedurl(var.operation),var.arguments,nil,actions)
end
function specials.file(var,actions)
return pdffilelink(references.checkedfile(var.operation),var.arguments,nil,actions)
end
function specials.fileorurl(var,actions)
local file, url = references.checkedfileorurl(var.operation,var.operation)
if file then
return pdffilelink(file,var.arguments,nil,actions)
elseif url then
return pdfurllink(url,var.arguments,nil,actions)
end
end
function specials.program(var,content)
local program = references.checkedprogram(var.operation)
return pdflaunch(program,var.arguments)
end
function specials.javascript(var)
return pdfjavascript(var.operation,var.arguments)
end
specials.JS = specials.javascript
executers.importform = pdfdictionary { S = pdf_named, N = pdfconstant("AcroForm:ImportFDF") }
executers.exportform = pdfdictionary { S = pdf_named, N = pdfconstant("AcroForm:ExportFDF") }
executers.first = pdfdictionary { S = pdf_named, N = pdfconstant("FirstPage") }
executers.previous = pdfdictionary { S = pdf_named, N = pdfconstant("PrevPage") }
executers.next = pdfdictionary { S = pdf_named, N = pdfconstant("NextPage") }
executers.last = pdfdictionary { S = pdf_named, N = pdfconstant("LastPage") }
executers.backward = pdfdictionary { S = pdf_named, N = pdfconstant("GoBack") }
executers.forward = pdfdictionary { S = pdf_named, N = pdfconstant("GoForward") }
executers.print = pdfdictionary { S = pdf_named, N = pdfconstant("Print") }
executers.exit = pdfdictionary { S = pdf_named, N = pdfconstant("Quit") }
executers.close = pdfdictionary { S = pdf_named, N = pdfconstant("Close") }
executers.save = pdfdictionary { S = pdf_named, N = pdfconstant("Save") }
executers.savenamed = pdfdictionary { S = pdf_named, N = pdfconstant("SaveAs") }
executers.opennamed = pdfdictionary { S = pdf_named, N = pdfconstant("Open") }
executers.help = pdfdictionary { S = pdf_named, N = pdfconstant("HelpUserGuide") }
executers.toggle = pdfdictionary { S = pdf_named, N = pdfconstant("FullScreen") }
executers.search = pdfdictionary { S = pdf_named, N = pdfconstant("Find") }
executers.searchagain = pdfdictionary { S = pdf_named, N = pdfconstant("FindAgain") }
executers.gotopage = pdfdictionary { S = pdf_named, N = pdfconstant("GoToPage") }
executers.query = pdfdictionary { S = pdf_named, N = pdfconstant("AcroSrch:Query") }
executers.queryagain = pdfdictionary { S = pdf_named, N = pdfconstant("AcroSrch:NextHit") }
executers.fitwidth = pdfdictionary { S = pdf_named, N = pdfconstant("FitWidth") }
executers.fitheight = pdfdictionary { S = pdf_named, N = pdfconstant("FitHeight") }
local function fieldset(arguments)
-- [\dogetfieldset{#1}]
return nil
end
function executers.resetform(arguments)
arguments = (type(arguments) == "table" and arguments) or settings_to_array(arguments)
return pdfdictionary {
S = pdfconstant("ResetForm"),
Field = fieldset(arguments[1])
}
end
local formmethod = "post" -- "get" "post"
local formformat = "xml" -- "xml" "html" "fdf"
-- bit 3 = html bit 6 = xml bit 4 = get
local flags = {
get = {
html = 12, fdf = 8, xml = 40,
},
post = {
html = 4, fdf = 0, xml = 32,
}
}
function executers.submitform(arguments)
arguments = (type(arguments) == "table" and arguments) or settings_to_array(arguments)
local flag = flags[formmethod] or flags.post
flag = (flag and (flag[formformat] or flag.xml)) or 32 -- default: post, xml
return pdfdictionary {
S = pdfconstant("SubmitForm"),
F = arguments[1],
Field = fieldset(arguments[2]),
Flags = flag,
-- \PDFsubmitfiller
}
end
local pdf_hide = pdfconstant("Hide")
function executers.hide(arguments)
return pdfdictionary {
S = pdf_hide,
H = true,
T = arguments,
}
end
function executers.show(arguments)
return pdfdictionary {
S = pdf_hide,
H = false,
T = arguments,
}
end
local pdf_movie = pdfconstant("Movie")
local pdf_start = pdfconstant("Start")
local pdf_stop = pdfconstant("Stop")
local pdf_resume = pdfconstant("Resume")
local pdf_pause = pdfconstant("Pause")
local function movie_or_sound(operation,arguments)
arguments = (type(arguments) == "table" and arguments) or settings_to_array(arguments)
return pdfdictionary {
S = pdf_movie,
T = format("movie %s",arguments[1] or "noname"),
Operation = operation,
}
end
function executers.startmovie (arguments) return movie_or_sound(pdf_start ,arguments) end
function executers.stopmovie (arguments) return movie_or_sound(pdf_stop ,arguments) end
function executers.resumemovie(arguments) return movie_or_sound(pdf_resume,arguments) end
function executers.pausemovie (arguments) return movie_or_sound(pdf_pause ,arguments) end
function executers.startsound (arguments) return movie_or_sound(pdf_start ,arguments) end
function executers.stopsound (arguments) return movie_or_sound(pdf_stop ,arguments) end
function executers.resumesound(arguments) return movie_or_sound(pdf_resume,arguments) end
function executers.pausesound (arguments) return movie_or_sound(pdf_pause ,arguments) end
function specials.action(var)
local operation = var.operation
if var.operation and operation ~= "" then
local e = executers[operation]
if type(e) == "table" then
return e
elseif type(e) == "function" then
return e(var.arguments)
end
end
end
local function build(levels,start,parent,method,nested)
local startlevel = levels[start].level
local noflevels = #levels
local i, n = start, 0
local child, entry, m, prev, first, last, f, l
while i and i <= noflevels do
local current = levels[i]
if current.usedpage == false then
-- safeguard
i = i + 1
else
local level = current.level
local title = current.title
local reference = current.reference
local opened = current.opened
local reftype = type(reference)
local block = nil
local variant = "unknown"
if reftype == "table" then
-- we're okay
variant = "list"
block = reference.block
elseif reftype == "string" then
local resolved = references.identify("",reference)
local realpage = resolved and structures.references.setreferencerealpage(resolved) or 0
if realpage > 0 then
variant = "realpage"
realpage = realpage
reference = structures.pages.collected[realpage]
block = reference and reference.block
end
elseif reftype == "number" then
if reference > 0 then
variant = "realpage"
realpage = reference
reference = structures.pages.collected[realpage]
block = reference and reference.block
end
else
-- error
end
current.block = block
if variant == "unknown" then
-- error, ignore
i = i + 1
-- elseif (level < startlevel) or (i > 1 and block ~= levels[i-1].reference.block) then
elseif (level < startlevel) or (i > 1 and block ~= levels[i-1].block) then
if nested then -- could be an option but otherwise we quit too soon
if entry then
pdfflushobject(child,entry)
else
report_bookmark("error 1")
end
return i, n, first, last
else
report_bookmark("confusing level change at level %a around %a",level,title)
startlevel = level
end
end
if level == startlevel then
if trace_bookmarks then
report_bookmark("%3i %w%s %s",realpage,(level-1)*2,(opened and "+") or "-",title)
end
local prev = child
child = pdfreserveobject()
if entry then
entry.Next = child and pdfreference(child)
pdfflushobject(prev,entry)
end
local action = nil
if variant == "list" then
action = pdflinkinternal(reference.internal,reference.realpage)
elseif variant == "realpage" then
action = pagereferences[realpage]
else
-- hm, what to do
end
entry = pdfdictionary {
Title = pdfunicode(title),
Parent = parent,
Prev = prev and pdfreference(prev),
A = action,
}
-- entry.Dest = pdflinkinternal(reference.internal,reference.realpage)
if not first then
first, last = child, child
end
prev = child
last = prev
n = n + 1
i = i + 1
elseif i < noflevels and level > startlevel then
i, m, f, l = build(levels,i,pdfreference(child),method,true)
if entry then
entry.Count = (opened and m) or -m
if m > 0 then
entry.First = pdfreference(f)
entry.Last = pdfreference(l)
end
else
report_bookmark("error 2")
end
else
-- missing intermediate level but ok
i, m, f, l = build(levels,i,pdfreference(child),method,true)
if entry then
entry.Count = (opened and m) or -m
if m > 0 then
entry.First = pdfreference(f)
entry.Last = pdfreference(l)
end
pdfflushobject(child,entry)
else
report_bookmark("error 3")
end
return i, n, first, last
end
end
end
pdfflushobject(child,entry)
return nil, n, first, last
end
function codeinjections.addbookmarks(levels,method)
if levels and #levels > 0 then
-- inspect(levels)
local parent = pdfreserveobject()
local _, m, first, last = build(levels,1,pdfreference(parent),method or "internal",false)
local dict = pdfdictionary {
Type = pdfconstant("Outlines"),
First = pdfreference(first),
Last = pdfreference(last),
Count = m,
}
pdfflushobject(parent,dict)
pdfaddtocatalog("Outlines",lpdf.reference(parent))
end
end
-- this could also be hooked into the frontend finalizer
lpdf.registerdocumentfinalizer(function() bookmarks.place() end,1,"bookmarks") -- hm, why indirect call
|