-
Notifications
You must be signed in to change notification settings - Fork 0
/
pdfextra.opm
1743 lines (1475 loc) · 67.6 KB
/
pdfextra.opm
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
% pdfextra package
% Michal Vlasák <lahcim8@gmail.com>
% https://github.com/vlasakm/pdfextra
% Zero-Clause BSD license
\_def\_pdfextra_version{0.3}
\_def\_pdfextra_date{2022-12-03}
\_codedecl \RM {Extra PDF features <v\_pdfextra_version, \_pdfextra_date>}
\_namespace{pdfextra}
\_doc
\sec Package initialization
We ensure that hyperlinking is active. Our fallback \`\_linkcolor` must
exist. We also use it for `\hyperlinks` if the user didn't enable
`\hyperlinks` yet (we don't want to override user setting).
\_cod
\_ifdefined\_ilinkcolor\_else
\_ifdefined\_linkcolor
\_ea\_let\_ea\_linkcolor \_ifdefined\Blue\Blue\_else\_empty\_fi
\_else
\_let\_linkcolor\linkcolor
\_fi
\_fi
\_ifx\_dest\_destactive\_else
\_hyperlinks\_linkcolor\_linkcolor
\_fi
\_doc
We are in the \OpTeX/ package namespace. A couple of shortcuts are defined
here: \`\.isdefined`, \`\.trycs`, \`\.cs` \`\.slet`, \`\.slet`, \`\.sdef` and
\`\.sxdef`. They all hard code the package name, because we already have too
many levels of indirection.
\_cod
\_def\.isdefined#1{\_isdefined{_pdfextra_#1}}
\_def\.trycs#1{\_trycs{_pdfextra_#1}}
\_def\.cs#1{\_cs{_pdfextra_#1}}
\_def\.slet#1#2{\_slet{_pdfextra_#1}{_pdfextra_#2}}
\_def\.sdef#1{\_sdef{_pdfextra_#1}}
\_def\.sxdef#1{\_sxdef{_pdfextra_#1}}
\_doc
\sec Helper macros
The macros here are just helpers for the macros to follow. They are not
useful generally, but proved useful in the expandable context of writing to
PDF files.
Already the first one limits the use to \LuaTeX/ (but who needs other engines
anyways :). \`\.emptyor`<possibly empty text><text to use when first argument is
nonempty> checks whether the first argument is empty, if not it expands the
second argument which can use the text from the first argument with
\`\.nonempty`. \`\.attrorempty`<attribute name><value> builds upon the first
one and is really useful for PDF dictionaries, when we don't want to write an
attribute without a value (a default specified by standard will be used instead).
\_cod
\_def\.emptyor#1#2{%
\_immediateassignment\_edef\.nonempty{#1}%
\_ifx\.nonempty\_empty\_else #2\_fi
}
\_def\.attrorempty#1#2{\.emptyor{#2}{/#1 \.nonempty}}
\_doc
There is a dillema for handling colors. While typesetting it is possible to
use greyscale, CMYK or RGB colors. But there are contexts where it is
possible to only use RGB colors. We want to provide the user with two
possibilities of specifying colors:
\begitems
* RGB color using PDF triplet (e.g. `1 0 0`),
* \OpTeX/ color using control sequence (e.g. `\Blue`)
\enditems
Both are handled by \`\.colortorgbdef`<cs><color specification>, which
defines <cs> to the corresponding PDF RGB triplet. The indirection with
defining a macro is because we want to use the processed color within
expansion only contexts where grouping is not possible.
\_cod
\_def\.colortorgbdef#1#2{\_bgroup
\_def\_setrgbcolor##1{##1}%
\_def\_setcmykcolor##1{\_cmyktorgb ##1 ;}%
\_def\_setgreycolor##1{##1 ##1 ##1}%
\_xdef#1{#2}%
\_egroup
}
\_doc
\`\.xaddto``\macro`{<text>} is a natural extension of \OpTeX's `\addto` that
expands <text> and is global.\nl
\`\.tmp` is used throught the package for temporary values.
\_cod
\_def\.xaddto#1#2{\_edef\.tmp{#2}%
\_global\_ea\_addto\_ea#1\_ea{\.tmp}%
}
\_doc
This package defines a few commands in the form
`\macro[<name>][<optional arguments>]{<text>}`. To make it possible to omit
the `[<optional arguments>]` \`\.secondoptdef` is defined.
`\.secondoptdef\<macro><parameters>{<body>}`
defines `\macro` with first mandatory argument in brackets (saved to
\`\.name`). Second optional argument in brackets is scanned using helper
macro defined with `\optdef` and is saved to `\_opt` token list). Additional
<parameters> can be specified as with `\optdef` (numbered from `#1`).
\_cod
\_def\.secondoptdef#1{%
\_def#1[##1]{\_def\.name{##1}\.cs{sopt:\_string#1}}%
\_ea\_optdef\_csname _pdfextra_sopt:\_string#1\_endcsname[]%
}
\_doc
When processing comma separated lists sometimes it is needed to ignore the
remaining text. For this we use \`\.untilend` macro which ignores everything
up to dummy \`\.end`. This is analogous to \OpTeX/'s `\_finbody` used for the
same purpose. Sometimes `\.end` is used as sentinel and compared in `\ifx`
tests, hence we define it to a unique value.
\_cod
\_def\.untilend#1\.end{}
\_def\.end{_pdfextra_end}
\_doc
For various uses it is necessary to know the number of page where something
happens. This has to be handled asynchronously with `\write`. Here we use
\OpTeX/ specific `.ref` file and associated macros, but this could be
replaced as long as the same interface is exposed.
\`\.setpageof`<name> writes \`\.Xpageof`<name> to the `.ref` file. In the next
\TeX/ run `\.Xpageof` finds out the page number (`\gpageno`) from \OpTeX/'s
`\_currpage` and saves it so that \`\.pageof`<name> can retrieve it. In the
first run we can't be sure of the page where the content will end up. As a
rough estimate we take the current page~-- this actually works well for
slides where page breaks are manual.
When `.ref` file is read along with the defintion of `\.Xpageof` this package
has not been loaded yet. Hence we can't use namespaced variants of
`\.isdefined`, etc.
\_cod
\_refdecl{%
\_def\.Xpageof#1{\_isdefined{_pdfextra_pageof:#1}\_iffalse
\_sxdef{_pdfextra_pageof:#1}{\_ea\_ignoresecond\_currpage}\_fi
}%
}
\_def\.setpageof#1{\_openref \_ewref\.Xpageof{{#1}}}
\_def\.pageof#1{%
\.trycs{pageof:#1}{%
\_the\_numexpr\_gpageno+1\_relax % best effort = current page num
}%
}
\_doc
\label[files]
\sec Handling of files
Handling of files is a big topic of this package. Files are everywhere~--
files containing multimedia, JavaScript script files, attachments, externally
referred files\dots Therefore a more sophisticated mechanism for handling
files is needed. The mechanism introduced in this section handles all three
cases of a {\em file specification}:
\begitems
* files embedded in the PDF (\"e", embedded file),
* files determined by path (\"x", external file),
* files determined by URL (\"u", url file).
\enditems
Although ideally all three would be interchangible this is not always the
case, because e.g. some media files must be embedded and linking to external
resources does not work with embedded files.
In most cases there are two many names and other associated values involved:
\begitems
* Some kind of a \"friendly" name. This one is sometimes shown by PDF viewers.
* The real name of the file. Also shown but in different contexts.
* The path or URL used to determine the file.
* MIME type of the file.
\enditems
For example when talking about \OpTeX/'s documentation we might have a
friendly name of \"opdoc", file name of \"`optex-doc.pdf`", URL of
\"\url{http://petr.olsak.net/ftp/olsak/optex/optex-doc.pdf}" and MIME type of
\"application/pdf". Different subset of them is required in different
contexts, but the user should only have to specify the friendly name (by
which they will refer to the file) and the path/URL of the file. The rest
will be deduced. The friendly name is used as a handle and {\em is usable}
in all places where file specification is required (although it may not
produce conforming output, see above).
In this two step process~-- definition and (re)use~-- we introduce a command
for defining files: \`\filedef``/<type> [<friendly name>]{<path or URL>}`. The
macro itself does general definitions and dispatches the type dependant work
to other macros in the form `_filedef:<type>`.
\_cod
\_def\.filedef/#1#2[#3]#4{%
\.sxdef{filename:#3}{(\.filename{#4})}%
\_edef\.tmp{\.exttomime{\.fileext{#4}}}%
\_ifx\.tmp\_empty
\_opwarning{MIME type of '#4' unknown, using '\.defaultmimetype'}%
\_edef\.tmp{\.defaultmimetype}%
\_fi
\.sxdef{filemime:#3}{\.tmp}%
\.cs{filedef:#1}{#3}{#4}%
}
\_nspublic \filedef ;
\_doc
Types \"e", \"x", \"u" are predefined, anything else would essentialy be a
variant of these.
External file (\"x") is determined only by path.
\_cod
\.sdef{filedef:x}#1#2{%
\.slet{filespec:#1}{filename:#1}%
}
\_doc
URL file (\"u") is determined by URL. Using all sorts of characters is
allowed by using `\_detokenize`. This time it is necessary to create full
{\em file specification}~-- a dictionary, where the \"file system" is URL.
\_cod
\.sdef{filedef:u}#1#2{%
\.sdef{filespec:#1}{<</FS /URL /F (\_detokenize{#2})>>}%
}
\_doc
Embedded files (\"e") are the most interesting ones. For further use (e.g.
for displaying the embedded files as attachments) MIME type is required. It
is saved in the stream as a `\Subtype`, encoded as a PDF name (e.g.
`/video#2Fmp4`). The embedded file stream must be wrapped in a full {\em file
specification}, which has the `/EF` (\"embedded file") entry. Also the
friendly name is used for some purpose by PDF viewers, so it set in `/Desc`
(description).
\_cod
\.sdef{filedef:e}#1#2{%
\_edef\.tmp{\.cs{filemime:#1}}%
\_isfile{#2}\_iffalse
\_opwarning{file '#2' not found}%
\_fi
\_pdfobj stream
attr{/Type /EmbeddedFile /Subtype \_ea\.mimetoname\_ea[\.tmp]}
file {#2}%
\_pdfrefobj\_pdflastobj
\.sxdef{filestream:#1}{\_the\_pdflastobj\_space 0 R}%
\_pdfobj {<</Type /Filespec
/F \.cs{filename:#1}
/Desc (#1)
/EF << /F \_the\_pdflastobj \_space 0 R >>%
>>}%
\_pdfrefobj\_pdflastobj
\.sxdef{filespec:#1}{\_the\_pdflastobj\_space 0 R}%
}
\_doc
Now the less interesting part~-- determining the file names from paths and
determining MIME types. The file name is the part after the last \"`/`" (if
any). The file extension is the part after last \"`.`" (if any).
\_cod
\_def\.filename#1{\_ea\.filenameA#1/\.end}
\_def\.filenameA#1/#2{\_ifx\.end#2#1\_else\_afterfi{\.filenameA#2}\_fi}
\_def\.fileext#1{\_ea\.fileextA#1.\.end}
\_def\.fileextA#1.#2{\_ifx\.end#2#1\_else\_afterfi{\.fileextA#2}\_fi}
\_doc
MIME type is determined from file extension (e.g. `mp4` is \"video/mp4"). For
mapping of file extensions to MIME types we abuse \TeX/'s hash table which
gets populated with \"known MIME types". This necessarily means that the
database is incomplete. Users can define their own additional mappings, or
they can contribute generally useful ones to this package.
The default MIME type (used for unknown file extensions) is
\"application/octet-stream"~-- binary data.
The uninteresting MIME type database itself is at the very end (\ref[mime]).
\_cod
\_def\.mimetoname[#1/#2]{/#1\_csstring\#2F#2}
\_def\.defaultmimetype{application/octet-stream}
\_def\.exttomime#1{\.trycs{mimetype:#1}{}}
\_doc
Here we define an \OpTeX/ style \"is-macro" that checks whether the file has
already been defined~-- \`\.isfiledefined``{<name>}\iftrue` (or `\iffalse`).
The case where the file has not
been defined using `\filedef` can be handled in a lot of ways. As a default
we interpret <name> as path and try to embed it. Because the path from <name>
is used as the \"friendly name" the file will be embedded only once even when
requested more times.
\_cod
\_def\.isfiledefined#1#2{\.isdefined{filespec:#1}\_iftrue\_else
\_afterfi{\.fileundefined{#1}}\_fi#2%
}
\_def\.fileundefined#1{\_isfile{#1}\_iftrue\.filedef/e[#1]{#1}\_else
\_opwarning{file '#1' not found, ignored}\_ea\_unless\_fi
}
% strict requirement of preceeding `\filedef` can be set like this:
%\_def\.fileundefined#1{\_opwarning{file '#1' is not defined, ignored}\_unless}
\_doc
\label[actions]
\sec PDF actions
The core of interactivity in PDF are actions. They are all initialy handled
by \`\pdfaction``[<action spec>]`. <action spec> is a comma separated list of
`<type>:<arguments>`. Leading spaces in the elements of the list are ignored
using undelimited-delimited argument pair trick.
An invocation could look like this:
\begtt
\pdfaction[
js:{app.alert("Yay JavaScript, going to page 5");},
ilink:pg:5,
transition:Wipe,
]
\endtt
This is why we have to be very careful when loading the contents between `[]`
to arguments. In particular, we can't split immediatly using `[#1:#2]`,
because this would discard the braces guarding the comma in the JavaScript
code. However we also need to find out the {\em type} of action which is
taken as a type of the first action (`js` in this case).
\`\.pdfactiontype``[<action spec>]` does this~-- we don't mind that there the
braces are lost.
`\pdfaction` processes the list, to create a chain of actions using `/Next`
field. The handling of each action type is up to macro
`\_pdfextra_<type>action`, which receives `[<type>:<arguments>]`. Because of
this a single type handler can handle multiple different actions, as is the
case with `\.ilinkaction` which is the fallback for unknown action types.
\_cod
\_def\.pdfaction[#1#2]{\.pdfactionA#1#2,\.stop\.end}
\_def\.pdfactionA#1,#2#3\.end{%
<<%
\.pdfactionB[#1]%
% next action
\_ifx\.stop#3\_else\_space
/Next \_afterfi{\.pdfactionA#2#3\.end} % intentional space
\_fi
>>
}
\_def\.pdfactionB[#1:#2]{\.trycs{#1action}{\_ea\.ilinkaction}[#1:#2]}
\_nspublic \pdfaction ;
\_def\.pdfactiontype[#1:#2]{#1}
\_doc
\label[actions-additional]
\secc Additional actions
Some PDF objects, like pages and some annotations, can also have \"additional
actions". These are actions which will be executed when an event happens~--
like page getting opened for `/O` action in page's additonal actions or `/PO`
in annotation's additional actions. For constructing these additional actions
we define a helper macro \`\.pdfaactions`. The use is as something follows:
\begtt \catcode`<=13 \adef|{\csstring<}
/AA || \.pdfaactions{ {O} {<action spec 1>} {C} {<action spec 2>} } >>
\endtt
To produce something this:
\begtt \catcode`<=13 \adef|{\csstring<}
/AA || /O ||<action 1>>> /C ||<action 2>>> >>
\endtt
\_cod
\_def\.pdfaactions#1{<<\.pdfaactionsA #1\.end\.end>>}
\_def\.pdfaactionsA#1#2{\_ifx\.end#1\_else /#1 \_ea\.pdfaction\_ea[#2]\_ea\.pdfaactionsA\_fi}
\_doc
\label[actions-link]
\secc Link annotations
The main use of actions~-- annotations of `/Subtype /Link`. Annotation of
this type creates an active rectangular area on the page that executes a PDF
action (or chain of them in the general case).
\`\hlink``[<action spec>]<text>` is macro that typesets <text> and makes area
occupied by it active according to <action spec>. All action types are
supported, the mechanism is completely generic.
The `\pdfstartlink`/`\pdfendlink` primitives are used to denote the part of
the page where <text> appears as active. \LuaTeX/ will then handle even the
situations where <text> gets broken across multiple lines (by creating
multiple rectangular annotations to cover all `\hbox`es).
\_cod
\_def\.hlink[#1]#2{\_bgroup\_def\#{\_csstring\#}%
\_edef\.type{\.pdfactiontype[#1]}%
\_quitvmode\_pdfstartlink \.linkdimens
attr{\_pdfborder{\.type}}%
user{/Subtype /Link /A \.pdfaction[#1]}\_relax
\_localcolor\.linkcolor{\.type}#2\_pdfendlink\_egroup
}
\_nspublic \hlink ;
\_doc
Use `\hlink` as the backing command for OpTeX's \"higher level" linking
commands (`\ilink` and `\ulink`).
The lower level ones (`\xlink` and its predecessor `\link` actually have
completely different semantics with regards to color, so we keep them as they
are.
\_cod
\_protected\_def\_ilink[#1]#2{\.hlink[#1]{#2}}
\_protected\_def\_ulink[#1]#2{{\_escapechar=-1 \_ea}\_expanded
{\_noexpand\.hlink[url:\_detokenize{#1}]}{#2}}
\_public \link \ilink \ulink ;
%\_protected\_def\_link[#1]#2#3{\_hlink[#1]{#3}}
%\_protected\_def\_xlink#1#2#3#4{\_hlink[#1:#2]{#4}}
\_doc
Two customizations of `\hlinks` are possible:
\begitems
* Dimensions of rectangular areas created by
`\pdfstartlink`/`\pdfendlink`. This is done using \`\.linkdimens`
(analogous to \OpTeX's `\linkdimens`). Dimensions that are unset are taken
from the respective `\hbox`es. \`\lininglinks` sets the dimensions for
running text~-- it covers all space of a line using `\baselineskip`.
\`\nolininglinks` sets no dimensions, this is useful for buttons, that may
have larger height/depth than a line.
* The color is determined from the type of link (that is, the first action in
<action spec>) by checking `\_<type>linkcolor` (compatible with \OpTeX/).
As a fallback `\_ilinkcolor` is used (set by \OpTeX's `\hyperlinks`) for all links
except for URLs, where `\_elinkcolor` is used instead. If even these
fallback colors are not defined (`\hyperlinks` isn't used), then the most
generic `\_linkcolor` will be taken or no color will be set.
\enditems
\_cod
\_def\.lininglinks{%
\_def\.linkdimens{height.75\_baselineskip depth.25\_baselineskip}%
}
\_def\.nolininglinks{\_def\.linkdimens{}}
\.lininglinks
\_nspublic \lininglinks \nolininglinks ;
\_def\.linkcolor#1{\_trycs{_#1linkcolor}{\_trycs{_ilinkcolor}{\_trycs{_linkcolor}{}}}}
% \_urllinkcolor = \_elinkcolor with fallbacks
\_def\_urllinkcolor{\.linkcolor{e}}
\_doc
\secc Open action
The document itself has one action defined in the document catalog. It is
called `/OpenAction`. We allow the user to set it using the familiar
<action spec> syntax with the command \`\openaction``[<action spec>]`.
Internally we could directly set it by appending to the catalog
using the primitive `\pdfcatalog`, but \LuaTeX/ (pdf\TeX/ really) allows
setting the action with special syntax. This has the benefit that it is not
allowed to set the action more than once.
\_cod
\_def\.openaction[#1]{\_pdfcatalog{} openaction user{\.pdfaction[#1]}\_relax}
\_nspublic \openaction ;
\_doc
\label[actions-jump]
\secc Jump actions
These are the most typical actions. Even \LuaTeX/ itself handles them,
although we don't use the possibility for maintaining generality. There are a
few types of jump actions:
\begitems
* `/GoTo` actions are the classic internal links to named destinations in the
PDF file (created by `\pdfdest` primitive or \OpTeX/'s `\dest`). The
destination names include also the type of internal link (e.g.
`ref:section1`). They are handled by \`\.ilinkaction``[<type>:<name>]`.
* `/URI` actions which are in most cases used as \"goto URL" actions. These
are not that useful directly, because special characters should be handled
before this actions is used (like with `\url`). The low level use is
\`\.urlaction``[url:<url>]`.
* \"Goto remote" actions, which can jump to a destination in another PDF file~--
either determined by name, or by page number. The external files are
expected to be defined by `\filedef` (but not the embedded variant). The
use is either \`\.extrefaction``[extref:<name>:<named destination>]` for
links to named destination or \`\.extpgrefaction``[extpgref:<name>:<page number>]`
for page destinations. Customization is possible with \`\.extrefextra`, by
default opening in a new windows is requested.
\enditems
\_cod
\_def\.ilinkaction[#1:#2]{/S /GoTo /D (#1:#2)}
\_def\.urlaction[#1:#2]{/S /URI /URI (#2)}
\_def\.extrefaction[#1:#2:#3]{/S /GoToR
/F \.cs{filespec:#2}
/D (#3)
\.extrefextra
}
\_def\.extpgrefaction[#1:#2:#3]{/S /GoToR
/F \.cs{filespec:#2}
/D [\_the\_numexpr#3-1\_relax\_space /Fit]
\.extrefextra
}
\_def\.extrefextra{/NewWindow true}
\_doc
Transition action is not really a jump action in of itself, but is only
useful when chained after jump actions, so we define it here. Transitions
(as page attributes) are handled more thoroughly in section~\ref[transitions].
The use would look something like:\nl
\`\.transitionaction`%
`[transition:<animation type>:<duration>:<raw PDF attributes>]`, where all fields
omitted from right take the default values.
\_cod
\_def\.transitionaction[#1:#2]{/S /Trans \.attrorempty{Trans}{\.maketrans[#2]}}
\_doc
\secc Named actions
User can request arbitrary \"named" action with
\`\.namedaction``[named:<name>]`. See user documentation for details.
\_cod
\_def\.namedaction[#1:#2]{/S /Named /N /#2}
\_doc
\secc JavaScript actions
JavaScript actions have two forms, either \`\.jsaction``[js:<name>]` or
`\.jsaction``[js:<script>]`. The first variant uses contents of `\filedef`'d
<name>, the second one uses <script> directly. There is no special catcode
handling.
\_cod
\_def\.jsaction[#1:#2]{/S /JavaScript
/JS \_ifcsname _pdfextra_filestream:#2\_endcsname \_lastnamedcs \_else
(#2)
\_fi
}
\_doc
\sec Page attributes
PDF represents pages as dictionaries. The dictionaries get generated by \LuaTeX/,
which fills in some attributes {\em attributes} (like `/Content` with
contents of the page and `/Annots` with array of annotations). We can add
more using `\pdfpageattr` primitive token list register. While not that many
are generally useful, there are a few interesting ones. For example
transitions can be set using page attributes, or we might want to set
additional actions (`/AA`) to listen for page events.
While the so called \"page objects" are in a tree structure (for fast lookup),
only the leaves are real \"pages". PDF allows some attributes to be inherited
from parent page objects, but not all of them and certainly not those we are
interested in.
The mechanism introduced in this section is optional, because it takes
complete control over `\pdfpageattr`. It gets activated when
\`\initpageattributes` is first used (which happens automatically for some
functionality exposed by this package), but may be activated by the user for
any other purpose. Only attributes listed in \`\pageattributes` are
processed.
We set the attributes anew for each page, by hooking into \OpTeX/'s
`\_begoutput`. Because `\pdfpageattr` token list doesn't get expanded before
written out to PDF, we expand it using the assignment in `\edef` trick. The
token list gets expanded, but the assignment is not made until it reaches
main processor when the temporary control sequence gets expanded.
\_cod
% pdfpagattr managament (default for all pages vs current page override)
\_def\.pageattributes{{Trans}{Dur}{Rotate}{AA}}
\_def\.initpageattributes{%
% add hook for setting primitive \pdfpageattr
\_addto\_begoutput{\_edef\.tmp{\_pdfpageattr={\.pdfpageattributes}}\.tmp}%
% no need to do this twice
\_let\.initpageattributes=\_relax
}
\_nspublic \pageattributes \initpageattributes ;
\_doc
The user interface we want to expose has two parts:
\begitems
* setting the page attribute for just this one page (\`\.pdfcurrentpageattr`),
* setting the default attribute (used when current page value is not set)
(\`\.pdfdefaultpageattr`).
\enditems
The first one of course brings in the typical \TeX/ problem of knowing the
page where something occurs. As always, the page number contained in
`\gpageno` during processing of said content may of course not actually be
the number of the page where the content ends up! Hence, we need to note the
page number with a delayed write, using `\.setpageof` and later `\.pageof`.
The different settings of page attributes should have distinct names, we use
the \`\.pageattrcount` counter for this.
\_cod
\_newcount\.pageattrcount
\_def\.pdfcurrentpageattr#1#2{\.initpageattributes
\_incr\.pageattrcount
\.setpageof{pageattr:\_the\.pageattrcount}%
\.sxdef{pdfpgattr:\.pageof{pageattr:\_the\.pageattrcount}:#1}{#2}%
}
\_def\.pdfdefaultpageattr#1#2{\.initpageattributes
\.sxdef{pdfpgattr:#1}{#2}%
}
\_doc
Finally, the macro \`\pdfpageattributes` takes care of setting generating the
contents of `\pdfpageattr`. For each attribute in `\pageattributes` it first
checks its current page value, only then the default value. If neither is
set, nothing is added.
\_cod
\_def\.pdfpageattributes{\_ea\.pdfpageattributesA\.pageattributes\.end}
\_def\.pdfpageattributesA#1{\_ifx\.end#1\_else
% use current page override or "default"
% don't emit anything if the value is empty
\.attrorempty{#1}{%
\.trycs{pdfpgattr:\_the\_gpageno:#1}{\.trycs{pdfpgattr:#1}{}}%
}%
\_ea\.pdfpageattributesA\_fi
}
\_doc
Each attributes then has two switches for the respective default and current
values. For defining a few of them a helper is introduced:\nl\indent
\`\.pdfpageattributesetters`` <attribute> \<default setter> \<current setter> {<value>}`,
\nl
where <attribute> is name of the attribute without the slash
(e.g. `MediaBox`), the two control sequences name the future user setters,
which will take single argument in brackets
(e.g. `\mediabox` and `\thismediabox`) and the <value> can use the
argument.
\_cod
\_def\.pdfpageattributesetters#1 #2#3#4{%
\.sdef{\_csstring#2}[##1]{\.pdfdefaultpageattr{#1}{#4}}%
\.sdef{\_csstring#3}[##1]{\.pdfcurrentpageattr{#1}{#4}}%
\_nspublic #2 #3 ;
}
\_doc
Some of the useful attributes are `/Rotate`, which rotates the pages visually
(can be set with \`\defaultpagerotate` and \`\pagerotate`), and the additional
actions (`/AA`, see section~\ref[actions-additional], set using
\`\defaultpageactions` \`\pageactions`).
\_cod
\.pdfpageattributesetters Rotate \defaultpagerotate \pagerotate {#1}
\.pdfpageattributesetters AA \defaultpageactions \pageactions {\.pdfaactions{#1}}
\_doc
\label[transitions]
\secc Transitions, page durations
There are predefined types of transitions, like `/Wipe`, `/Box`, `/Split`,
etc. Most have other customizible attributes~-- usually directions set in
different ways depending on the animation type at hand, but the most
important attribute is the duration of the animation. Parsing friendly user
notation in the form of
`[<animation type>:<duration>:<other raw attributes>]`, where fields from the
right may be omitted to produce the default value, is handled by
\`\.maketrans`. This macro is also used by transition actions
(see~\ref[actions-jump]). The defaults are simply those defined by PDF
standard (no transition, 1 second duration and the respective default
directions).
\_cod
\_def\.maketrans[#1]{\.maketransA#1:::\.end}
\_def\.maketransA#1:#2:#3:#4\.end{%
\.emptyor{#1}{<</S /\.nonempty \.attrorempty{D}{#2} #3>>}
}
\_doc
The attribute setters for transitions (\`\transitions`, \`\transition`) are a
simple wrappers. Similiar is the
setting of page duration in seconds after which PDF viewer automatically
advances to the next page (\`\defaultpageduration`, \`\pageduration`).
\_cod
\.pdfpageattributesetters Trans \transitions \transition {\.maketrans[#1]}
\.pdfpageattributesetters Dur \defaultpageduration \pageduration {#1}
\_doc
\label[nametrees]
\sec Name trees -- attachments and document level JavaScript
These don't have any last place to be in, so they are documented separately,
here. Attaching files using `/FileAttachment` annotations:
\begitems \style n
* is intended more towards viewers of the document for extra additions and
* doesn't work in the viewers as well as one would like.
\enditems
That is why instead embed files using normal `\filedef` and then allow them
to be added to the document level `/EmbeddedFiles` entry, which means they
will be shown in the user interface by PDF viewers. `/EmbeddedFiles` is a
document level name tree (contained inside `/Names` entry of `/Catalog`) that
maps names of files to their objects. Although we simplify matters by
constructing more of an array.
What works very similiarly is document level JavaScript. It is a name tree
within `/JavaScript` field. It maps names of JavaScript actions to their
object numbers. The names aren't very useful, but the actions have their
purpose. They are executed in turn after the document is opened. Hence they
can be used to predefine JavaScript functions in the global context, to be
used later within actions explicitly activated by the user.
The user level commands are \`\attach``[<name>]` (to attach a previously
`\filedef`'d name with fallback to embedding now if it is a valid path) and
\`\dljavascript``[<name>]{<script>}` (adds action that executes <script>
after document is opened, <name> is more or less meaningless).
Internally the commands construct lists of what ends up in the resulting name
array, i.e. pairs {\visiblesp `(<name>) <object number> 0 R `}. Intermediate
macros \`\.embeddedfiles` and \`\.dljavascripts` are used for this.
In the case of file attachments, nothing happens if file is defined and not
found by the fallback.
\_cod
% file attachment
\_def\.embeddedfiles{}
\_def\.attach[#1]{\.isfiledefined{#1}\_iftrue
\.xaddto\.embeddedfiles{(#1) \.cs{filespec:#1} }\_fi
}
\_nspublic \attach ;
\_def\.dljavascripts{}
\_def\.dljavascript[#1]#2{%
\_immediate\_pdfobj{<< \.jsaction[js:{#2}] >>}%
\.xaddto\.dljavascripts{(#1) \_the\_pdflastobj \_space 0 R }%
}
\_nspublic \dljavascript ;
\_doc
Renditions (see \ref[mm-renditions]) also need their name tree. This package
mostly doesn't play well with Unicode filenames, that is why they are
forbidden. However, Renditions that are accessed from JavaScript have to be
named/present in a `/Renditions` name tree, with the names encoded in the PDF
encoding (UTF-16BE).\nl
The names and object references are collected in \`\.renditions`. Unicode
encoding is hacked with \`\.pdfstringtounicode`.
\_cod
\_def\.pdfstringstrip(#1){#1}%
\_def\.pdfstringtounicode#1#2{%
\_ea\_pdfunidef\_ea#1\_ea{\_ea\.pdfstringstrip\_expanded{\.cs{filename:#2}}}%
}
\_def\.renditions{}
\_doc
Object creation, which is common to all name trees, is handled by\nl\indent
\`\.makenamearray``<name tree name><name tree content>`.\nl
It doesn't do anything
for empty lists, to not bloat PDF files when this mechanism isn't used.
\_cod
\_def\.makenamearray#1#2{\_ifx#2\_empty\_else
\_immediate\_pdfobj {<< /Names [ #2 ] >>}%
\_pdfnames{/#1 \_the\_pdflastobj \_space 0 R }\_fi
}
\_doc
The lists themselves can only be written out to the PDF file at the very end
of the run. We use \OpTeX/'s `\_byehook`, which is run in `\_bye`. But `\bye`
itself may be predefined by the user, for example when using some of the
\OpTeX/ tricks. We just hope that the user keeps `\_byehook`.
\_cod
\_addto\_byehook{%
\.makenamearray{EmbeddedFiles}\.embeddedfiles
\.makenamearray{JavaScript}\.dljavascripts
\.makenamearray{Renditions}\.renditions
}
\_doc
\sec Viewer preferences
There are a few customizations of display (and other preferences of PDF
viewers) possible in the document catalog or its subdictionary
`/ViewerPreferences`. Most are not that useful. The interesting
ones are implemented by \`\fullscreen`, \`\showoutlines`, \`\showattached`.
They all set the page mode using \`\.setpagemode`. We don't handle respecting
the last setting (using \`\_byehook`). To prevent invalid PDF files, we set
`\.setpagemode` to `\_relax` after use.
\_cod
\_def\.setpagemode#1{\_pdfcatalog{/PageMode /#1}\_glet\.setpagemode=\_relax}
\_def\.fullscreen{\.setpagemode{FullScreen}}
\_def\.showoutlines{\.setpagemode{UseOutlines}}
\_def\.showattached{\.setpagemode{UseAttachments}}
\_nspublic \fullscreen \showoutlines \showattached ;
\_doc
Only the setting of duplex / double sided printing and display is in the
nested dictionary. It is handled by \`\duplexdisplay`. The simplistic version
does not handle more attributes in `/ViewerPreferences`. We also set the
meaning to `\_relax` to prevent more (erroneous) uses.
\_cod
\_def\.duplexdisplay{\_pdfcatalog{%
/PageLayout /TwoPageRight
/ViewerPreferences <<
/Duplex /DuplexFlipLongEdge
>>}%
\_glet\.duplexdisplay=\_relax
}
\_def\duplexdisplay{\.duplexdisplay}
\_doc
\sec Multimedia
PDF essentially allows insertion of different types of multimedia:
\begitems
* images,
* audio/video,
* 3D art.
\enditems
The first is pretty standard and handled normally by the engine (\LuaTeX).
Others are possible, but have to be done manually according to one of the
mechanisms specified by PDF standard:
\begitems
* Sounds (audio only),
* Movies (video and/or audio),
* Renditions (video and/or audio),
* 3D annotations (3D art),
* Rich Media (video and/or audio, 3D art)
\enditems
Sadly all these mechanisms are badly flawed, each in different ways. At least
we try to use the one that works in the viewers.
For audio/video \"Movies" are the simplest mechanism, but they have been
deprecated in PDF 2.0 and no longer work in Acrobat/Foxit (same for
\"Sounds").
\"Renditions" are complicated, partly dependant on JavaScript, but at least
supported by Acrobat, Foxit, Evince and Okular.
\"Rich media" annotations were designed for Flash. This use case is no longer possible
today, but the obscurities remain. They are unnecessiraly complicated, but can
be used without Flash too. Although the result is very plain for audio/video~--
no controls can be displayed and there are no associated actions.
\"3D annotations" are reasonably simple, but also flawed. They cannot reuse
embedded file as a source for 3D data. Hence it is better and more consistent
to use Rich Media for 3D annotations. It even has additional benefits, like
the possibility of using multiple initialization scripts.
In the end, this package exposes two user commands corresponding to two
mechanisms~-- first are Renditions (`\render`) for audio/video that works in most
browsers and Rich Media (`\RM`) mainly for 3D art, but also for audio/video
with limited possibilities.
Both mechanisms have an annotation at their core. Annotations is essentially
a rectangular area on page. The area corresponds to where the
multimedium will show up. After activating the area somehow (by user click,
or action) the multimedium will start playing. Before annotations the
rectangular area will show something that is called \"normal appearance".
This appearance is of type form XObject. Those are really similiar to pages~--
they have dimensions, contents made up of PDF graphics operators, \dots,
but they are reusable. Not that useful for annotations where we will need the
form only once, but nice anyways. pdf\TeX/ has primitives for creating them~--
`\pdfxform` and friends. They essentially do the same code like `\shipout`
does, but instead of page, they make this reusable object. One can then
either use this reusable object in another page/form, but we will indirectly
refer to it for the appearance.
Important aspect of annotations is that they are really only rectangular
areas on the page, but they are not really part of the page. They sort of sit
on another level and are not influenced by PDF graphic operators which make
the page. In pdf\TeX/ annotations are handled by {\em whatsit} nodes. While
most nodes map to known primitive \TeX/ concepts (like typeset characters,
boxes, rules, etc.) Whatsits are essentially commands for \TeX/ that are
delayed until page is being shipped out (written to PDF file). `\write`,
`\special`, and most pdf\TeX/ commands create whatsits. For annotationos this
is important, because this means that the engine only stores the information
about annotation that we specify, but creates it at due time, when it should
be written to PDF.
Because whatsits are essentially dimensionless and we want it to be a part of
normal \TeX/ typesetting material we create the annotation (whatsit) in
`\hbox`. This box will be otherwise empty, because the apperance of the
rectangular area is determined by the normal appearance field (`/N` in
`/AP`). We set the dimensions of the box to the dimensions of normal
appearance. Everything will line up nicely, because when processed, the
annotation will take dimensions from the box.
All of these concepts are implemented in:\nl\indent
\`\.boxedannot``[<type>:<name>]{<appearance>}{<special text>}{<annotation attributes>}`\nl
<type> is used to determine the annotation border (same principle as with
Link annotations, section~\ref[actions-link]), <name> will be used as the
annotation name (`/NM`), <special text> is used for influencing the
`\pdfannot` primitive, and <annotation attributes> will become the body of
the annotation.
\_cod
\_def\.boxedannot[#1:#2]#3#4#5{%
\_setbox0=\_hbox{#3}\_setbox2=\_null
\_ht2=\_ht0 \_wd2=\_wd0 \_dp2=\_dp0
\_preshipout0 \box0
\_immediate\_pdfxform0
% box with annotation both stretching to dimensions of appearance
\_hbox{\.setpageof{#1:#2}%
\_pdfannot #4 {#5
/AP <</N \_the\_pdflastxform \_space 0 R>>
\_pdfborder{#1}
/NM (#2)
/Contents (#1 '#2')
}%
\_copy2
}%
}
\_doc
There is another weird thing common to both multimedia mechanisms~--
the redefinition of `\.name`. It is initially
set by `\.secondoptdef` to <name>, but may be redefined by user supplied
`name` key-value parameter. This should be used when there are multiple uses
of the same content. Otherwise samely named annotations would be
indistinguishable both for PDF viewer and our handling of actions (which
would all refer only to the first instance).
To somewhat overcome this, trying to use the same <name> (within the same
type of annotaiton) will use dummy name from \`\.unnamedannotcount` (for
uniqueness). This means that <name> will always refer to the first instance.
\`\.redefinename` handles this.
\_cod
\_newcount\.unnamedannotcount
\_def\.redefinename#1{%
\.isdefined{#1:\.name}\_iftrue
\_incr\.unnamedannotcount
\_edef\.name{\_the\.unnamedannotcount}%
\_else
\_edef\.name{\_kv{name}}%
\_fi
}
\_doc
\label[mm-renditions]
\secc Renditions (audio/video)
There are three main types of PDF objects involved in the Renditions
(\"Multimedia") mechanism:
\begitems
* Screen annotations define the area for playing multimedia.
* Rendition objects define the multimedia to play.
* Rendition actions associate Rendition objects with Screen annotations.
\enditems
You can theoretically arbitrarily mix and match rendition objects and screen
annotations by invoking different actions. In practice Evince and Okular do
really simplistic parsing and don't fully support the actions fully. But by
keeping it simple it is possible to make it work almost the same in all
viewers that support renditions.
Different sources of audio/video should be possible. In fact all three file
specifications (embedded files, files specified by URL/path) could work.
Again in practice embedded file is the safest bet, that works in all viewers
that support renditions.
The user facing command is:\nl\indent
\`\render``[<name>][<optional key-value paramers>]{<horizontal material>}`\nl\noindent
<name> is the friendly name set using `\filedef` or file path if <name> isn't
`\filedef`d and is to be embedded. The key-value parameters in brackets
can be entirely omitted. They can influence the playback (except for `controls`
most are not well supported). Default values are taken from
`\.renderdefaults`.