Debian Codegraph @

texmf-dist/tex/optex/pdfextra/pdfextra.opm

raw
1 % pdfextra package
2 % Michal Vlasák <[email protected]>
3 % https://github.com/vlasakm/pdfextra
4 % Zero-Clause BSD license
5
6 \_def\_pdfextra_version{0.3}
7 \_codedecl \RM {Extra PDF features (v\_pdfextra_version)}
8 \_namespace{pdfextra}
9
10 \_doc
11 \sec Package initialization
12
13 We ensure that hyperlinking is active. Our fallback \`\_linkcolor` must
14 exist. We also use it for `\hyperlinks` if the user didn't enable
15 `\hyperlinks` yet (we don't want to override user setting).
16 \_cod
17
18 \_ifdefined\_ilinkcolor\_else
19 \_ifdefined\_linkcolor
20 \_ea\_let\_ea\_linkcolor \_ifdefined\Blue\Blue\_else\_empty\_fi
21 \_else
22 \_let\_linkcolor\linkcolor
23 \_fi
24 \_fi
25
26 \_ifx\_dest\_destactive\_else
27 \_hyperlinks\_linkcolor\_linkcolor
28 \_fi
29
30 \_doc
31 We are in the \OpTeX/ package namespace. A couple of shortcuts are defined
32 here: \`\.isdefined`, \`\.trycs`, \`\.cs` \`\.slet`, \`\.slet`, \`\.sdef` and
33 \`\.sxdef`. They all hard code the package name, because we already have too
34 many levels of indirection.
35 \_cod
36
37 \_def\.isdefined#1{\_isdefined{_pdfextra_#1}}
38 \_def\.trycs#1{\_trycs{_pdfextra_#1}}
39 \_def\.cs#1{\_cs{_pdfextra_#1}}
40 \_def\.slet#1#2{\_slet{_pdfextra_#1}{_pdfextra_#2}}
41 \_def\.sdef#1{\_sdef{_pdfextra_#1}}
42 \_def\.sxdef#1{\_sxdef{_pdfextra_#1}}
43
44 \_doc
45 \sec Helper macros
46 The macros here are just helpers for the macros to follow. They are not
47 useful generally, but proved useful in the expandable context of writing to
48 PDF files.
49
50 Already the first one limits the use to \LuaTeX/ (but who needs other engines
51 anyways :). \`\.emptyor`<possibly empty text><text to use when first argument is
52 nonempty> checks whether the first argument is empty, if not it expands the
53 second argument which can use the text from the first argument with
54 \`\.nonempty`. \`\.attrorempty`<attribute name><value> builds upon the first
55 one and is really useful for PDF dictionaries, when we don't want to write an
56 attribute without a value (a default specified by standard will be used instead).
57 \_cod
58
59 \_def\.emptyor#1#2{%
60 \_immediateassignment\_edef\.nonempty{#1}%
61 \_ifx\.nonempty\_empty\_else #2\_fi
62 }
63 \_def\.attrorempty#1#2{\.emptyor{#2}{/#1 \.nonempty}}
64
65 \_doc
66 There is a dillema for handling colors. While typesetting it is possible to
67 use greyscale, CMYK or RGB colors. But there are contexts where it is
68 possible to only use RGB colors. We want to provide the user with two
69 possibilities of specifying colors:
70
71 \begitems
72 * RGB color using PDF triplet (e.g. `1 0 0`),
73 * \OpTeX/ color using control sequence (e.g. `\Blue`)
74 \enditems
75
76 Both are handled by \`\.colortorgbdef`<cs><color specification>, which
77 defines <cs> to the corresponding PDF RGB triplet. The indirection with
78 defining a macro is because we want to use the processed color within
79 expansion only contexts where grouping is not possible.
80 \_cod
81
82 \_def\.colortorgbdef#1#2{\_bgroup
83 \_def\_setrgbcolor##1{##1}%
84 \_def\_setcmykcolor##1{\_cmyktorgb ##1 ;}%
85 \_def\_setgreycolor##1{##1 ##1 ##1}%
86 \_xdef#1{#2}%
87 \_egroup
88 }
89
90 \_doc
91 \`\.xaddto``\macro`{<text>} is a natural extension of \OpTeX's `\addto` that
92 expands <text> and is global.\nl
93 \`\.tmp` is used throught the package for temporary values.
94 \_cod
95
96 \_def\.xaddto#1#2{\_edef\.tmp{#2}%
97 \_global\_ea\_addto\_ea#1\_ea{\.tmp}%
98 }
99
100 \_doc
101 This package defines a few commands in the form
102 `\macro[<name>][<optional arguments>]{<text>}`. To make it possible to omit
103 the `[<optional arguments>]` \`\.secondoptdef` is defined.
104
105 `\.secondoptdef\<macro><parameters>{<body>}`
106 defines `\macro` with first mandatory argument in brackets (saved to
107 \`\.name`). Second optional argument in brackets is scanned using helper
108 macro defined with `\optdef` and is saved to `\_opt` token list). Additional
109 <parameters> can be specified as with `\optdef` (numbered from `#1`).
110 \_cod
111
112 \_def\.secondoptdef#1{%
113 \_def#1[##1]{\_def\.name{##1}\.cs{sopt:\_string#1}}%
114 \_ea\_optdef\_csname _pdfextra_sopt:\_string#1\_endcsname[]%
115 }
116
117 \_doc
118 When processing comma separated lists sometimes it is needed to ignore the
119 remaining text. For this we use \`\.untilend` macro which ignores everything
120 up to dummy \`\.end`. This is analogous to \OpTeX/'s `\_finbody` used for the
121 same purpose. Sometimes `\.end` is used as sentinel and compared in `\ifx`
122 tests, hence we define it to a unique value.
123 \_cod
124
125 \_def\.untilend#1\.end{}
126 \_def\.end{_pdfextra_end}
127
128 \_doc
129 For various uses it is necessary to know the number of page where something
130 happens. This has to be handled asynchronously with `\write`. Here we use
131 \OpTeX/ specific `.ref` file and associated macros, but this could be
132 replaced as long as the same interface is exposed.
133
134 \`\.setpageof`<name> writes \`\.Xpageof`<name> to the `.ref` file. In the next
135 \TeX/ run `\.Xpageof` finds out the page number (`\gpageno`) from \OpTeX/'s
136 `\_currpage` and saves it so that \`\.pageof`<name> can retrieve it. In the
137 first run we can't be sure of the page where the content will end up. As a
138 rough estimate we take the current page~-- this actually works well for
139 slides where page breaks are manual.
140
141 When `.ref` file is read along with the defintion of `\.Xpageof` this package
142 has not been loaded yet. Hence we can't use namespaced variants of
143 `\.isdefined`, etc.
144 \_cod
145
146 \_refdecl{%
147 \_def\.Xpageof#1{\_isdefined{_pdfextra_pageof:#1}\_iffalse
148 \_sxdef{_pdfextra_pageof:#1}{\_ea\_ignoresecond\_currpage}\_fi
149 }%
150 }
151
152 \_def\.setpageof#1{\_openref \_ewref\.Xpageof{{#1}}}
153
154 \_def\.pageof#1{%
155 \.trycs{pageof:#1}{%
156 \_the\_numexpr\_gpageno+1\_relax % best effort = current page num
157 }%
158 }
159
160 \_doc
161 \label[files]
162 \sec Handling of files
163
164 Handling of files is a big topic of this package. Files are everywhere~--
165 files containing multimedia, JavaScript script files, attachments, externally
166 referred files\dots Therefore a more sophisticated mechanism for handling
167 files is needed. The mechanism introduced in this section handles all three
168 cases of a {\em file specification}:
169
170 \begitems
171 * files embedded in the PDF (\"e", embedded file),
172 * files determined by path (\"x", external file),
173 * files determined by URL (\"u", url file).
174 \enditems
175
176 Although ideally all three would be interchangible this is not always the
177 case, because e.g. some media files must be embedded and linking to external
178 resources does not work with embedded files.
179
180 In most cases there are two many names and other associated values involved:
181 \begitems
182 * Some kind of a \"friendly" name. This one is sometimes shown by PDF viewers.
183 * The real name of the file. Also shown but in different contexts.
184 * The path or URL used to determine the file.
185 * MIME type of the file.
186 \enditems
187
188 For example when talking about \OpTeX/'s documentation we might have a
189 friendly name of \"opdoc", file name of \"`optex-doc.pdf`", URL of
190 \"\url{http://petr.olsak.net/ftp/olsak/optex/optex-doc.pdf}" and MIME type of
191 \"application/pdf". Different subset of them is required in different
192 contexts, but the user should only have to specify the friendly name (by
193 which they will refer to the file) and the path/URL of the file. The rest
194 will be deduced. The friendly name is used as a handle and {\em is usable}
195 in all places where file specification is required (although it may not
196 produce conforming output, see above).
197
198 In this two step process~-- definition and (re)use~-- we introduce a command
199 for defining files: \`\filedef``/<type> [<friendly name>]{<path or URL>}`. The
200 macro itself does general definitions and dispatches the type dependant work
201 to other macros in the form `_filedef:<type>`.
202 \_cod
203
204 \_def\.filedef/#1#2[#3]#4{%
205 \.sxdef{filename:#3}{(\.filename{#4})}%
206 \_edef\.tmp{\.exttomime{\.fileext{#4}}}%
207 \_ifx\.tmp\_empty
208 \_opwarning{MIME type of '#4' unknown, using '\.defaultmimetype'}%
209 \_edef\.tmp{\.defaultmimetype}%
210 \_fi
211 \.sxdef{filemime:#3}{\.tmp}%
212 \.cs{filedef:#1}{#3}{#4}%
213 }
214 \_nspublic \filedef ;
215
216 \_doc
217 Types \"e", \"x", \"u" are predefined, anything else would essentialy be a
218 variant of these.
219
220 External file (\"x") is determined only by path.
221 \_cod
222
223 \.sdef{filedef:x}#1#2{%
224 \.slet{filespec:#1}{filename:#1}%
225 }
226
227 \_doc
228 URL file (\"u") is determined by URL. Using all sorts of characters is
229 allowed by using `\_detokenize`. This time it is necessary to create full
230 {\em file specification}~-- a dictionary, where the \"file system" is URL.
231 \_cod
232
233 \.sdef{filedef:u}#1#2{%
234 \.sdef{filespec:#1}{<</FS /URL /F (\_detokenize{#2})>>}%
235 }
236
237 \_doc
238 Embedded files (\"e") are the most interesting ones. For further use (e.g.
239 for displaying the embedded files as attachments) MIME type is required. It
240 is saved in the stream as a `\Subtype`, encoded as a PDF name (e.g.
241 `/video#2Fmp4`). The embedded file stream must be wrapped in a full {\em file
242 specification}, which has the `/EF` (\"embedded file") entry. Also the
243 friendly name is used for some purpose by PDF viewers, so it set in `/Desc`
244 (description).
245 \_cod
246
247 \.sdef{filedef:e}#1#2{%
248 \_edef\.tmp{\.cs{filemime:#1}}%
249 \_isfile{#2}\_iffalse
250 \_opwarning{file '#2' not found}%
251 \_fi
252 \_pdfobj stream
253 attr{/Type /EmbeddedFile /Subtype \_ea\.mimetoname\_ea[\.tmp]}
254 file {#2}%
255 \_pdfrefobj\_pdflastobj
256 \.sxdef{filestream:#1}{\_the\_pdflastobj\_space 0 R}%
257 \_pdfobj {<</Type /Filespec
258 /F \.cs{filename:#1}
259 /Desc (#1)
260 /EF << /F \_the\_pdflastobj \_space 0 R >>%
261 >>}%
262 \_pdfrefobj\_pdflastobj
263 \.sxdef{filespec:#1}{\_the\_pdflastobj\_space 0 R}%
264 }
265
266 \_doc
267 Now the less interesting part~-- determining the file names from paths and
268 determining MIME types. The file name is the part after the last \"`/`" (if
269 any). The file extension is the part after last \"`.`" (if any).
270 \_cod
271
272 \_def\.filename#1{\_ea\.filenameA#1/\.end}
273 \_def\.filenameA#1/#2{\_ifx\.end#2#1\_else\_afterfi{\.filenameA#2}\_fi}
274
275 \_def\.fileext#1{\_ea\.fileextA#1.\.end}
276 \_def\.fileextA#1.#2{\_ifx\.end#2#1\_else\_afterfi{\.fileextA#2}\_fi}
277
278 \_doc
279 MIME type is determined from file extension (e.g. `mp4` is \"video/mp4"). For
280 mapping of file extensions to MIME types we abuse \TeX/'s hash table which
281 gets populated with \"known MIME types". This necessarily means that the
282 database is incomplete. Users can define their own additional mappings, or
283 they can contribute generally useful ones to this package.
284
285 The default MIME type (used for unknown file extensions) is
286 \"application/octet-stream"~-- binary data.
287
288 The uninteresting MIME type database itself is at the very end (\ref[mime]).
289 \_cod
290
291 \_def\.mimetoname[#1/#2]{/#1\_csstring\#2F#2}
292
293 \_def\.defaultmimetype{application/octet-stream}
294 \_def\.exttomime#1{\.trycs{mimetype:#1}{}}
295
296 \_doc
297 Here we define an \OpTeX/ style \"is-macro" that checks whether the file has
298 already been defined~-- \`\.isfiledefined``{<name>}\iftrue` (or `\iffalse`).
299 The case where the file has not
300 been defined using `\filedef` can be handled in a lot of ways. As a default
301 we interpret <name> as path and try to embed it. Because the path from <name>
302 is used as the \"friendly name" the file will be embedded only once even when
303 requested more times.
304 \_cod
305
306 \_def\.isfiledefined#1#2{\.isdefined{filespec:#1}\_iftrue\_else
307 \_afterfi{\.fileundefined{#1}}\_fi#2%
308 }
309
310 \_def\.fileundefined#1{\_isfile{#1}\_iftrue\.filedef/e[#1]{#1}\_else
311 \_opwarning{file '#1' not found, ignored}\_ea\_unless\_fi
312 }
313
314 % strict requirement of preceeding `\filedef` can be set like this:
315 %\_def\.fileundefined#1{\_opwarning{file '#1' is not defined, ignored}\_unless}
316
317 \_doc
318 \label[actions]
319 \sec PDF actions
320
321 The core of interactivity in PDF are actions. They are all initialy handled
322 by \`\pdfaction``[<action spec>]`. <action spec> is a comma separated list of
323 `<type>:<arguments>`. Leading spaces in the elements of the list are ignored
324 using undelimited-delimited argument pair trick.
325
326 An invocation could look like this:
327
328 \begtt
329 \pdfaction[
330 js:{app.alert("Yay JavaScript, going to page 5");},
331 ilink:pg:5,
332 transition:Wipe,
333 ]
334 \endtt
335
336 This is why we have to be very careful when loading the contents between `[]`
337 to arguments. In particular, we can't split immediatly using `[#1:#2]`,
338 because this would discard the braces guarding the comma in the JavaScript
339 code. However we also need to find out the {\em type} of action which is
340 taken as a type of the first action (`js` in this case).
341 \`\.pdfactiontype``[<action spec>]` does this~-- we don't mind that there the
342 braces are lost.
343
344 `\pdfaction` processes the list, to create a chain of actions using `/Next`
345 field. The handling of each action type is up to macro
346 `\_pdfextra_<type>action`, which receives `[<type>:<arguments>]`. Because of
347 this a single type handler can handle multiple different actions, as is the
348 case with `\.ilinkaction` which is the fallback for unknown action types.
349 \_cod
350
351 \_def\.pdfaction[#1#2]{\.pdfactionA#1#2,\.stop\.end}
352 \_def\.pdfactionA#1,#2#3\.end{%
353 <<%
354 \.pdfactionB[#1]%
355 % next action
356 \_ifx\.stop#3\_else\_space
357 /Next \_afterfi{\.pdfactionA#2#3\.end} % intentional space
358 \_fi
359 >>
360 }
361 \_def\.pdfactionB[#1:#2]{\.trycs{#1action}{\_ea\.ilinkaction}[#1:#2]}
362 \_nspublic \pdfaction ;
363
364 \_def\.pdfactiontype[#1:#2]{#1}
365
366 \_doc
367 \label[actions-additional]
368 \secc Additional actions
369 Some PDF objects, like pages and some annotations, can also have \"additional
370 actions". These are actions which will be executed when an event happens~--
371 like page getting opened for `/O` action in page's additonal actions or `/PO`
372 in annotation's additional actions. For constructing these additional actions
373 we define a helper macro \`\.pdfaactions`. The use is as something follows:
374 \begtt \catcode`<=13 \adef|{\csstring<}
375 /AA || \.pdfaactions{ {O} {<action spec 1>} {C} {<action spec 2>} } >>
376 \endtt
377 To produce something this:
378 \begtt \catcode`<=13 \adef|{\csstring<}
379 /AA || /O ||<action 1>>> /C ||<action 2>>> >>
380 \endtt
381 \_cod
382
383 \_def\.pdfaactions#1{<<\.pdfaactionsA #1\.end\.end>>}
384 \_def\.pdfaactionsA#1#2{\_ifx\.end#1\_else /#1 \_ea\.pdfaction\_ea[#2]\_ea\.pdfaactionsA\_fi}
385
386 \_doc
387 \label[actions-link]
388 \secc Link annotations
389 The main use of actions~-- annotations of `/Subtype /Link`. Annotation of
390 this type creates an active rectangular area on the page that executes a PDF
391 action (or chain of them in the general case).
392 \`\hlink``[<action spec>]<text>` is macro that typesets <text> and makes area
393 occupied by it active according to <action spec>. All action types are
394 supported, the mechanism is completely generic.
395
396 The `\pdfstartlink`/`\pdfendlink` primitives are used to denote the part of
397 the page where <text> appears as active. \LuaTeX/ will then handle even the
398 situations where <text> gets broken across multiple lines (by creating
399 multiple rectangular annotations to cover all `\hbox`es).
400 \_cod
401
402 \_def\.hlink[#1]#2{\_bgroup\_def\#{\_csstring\#}%
403 \_edef\.type{\.pdfactiontype[#1]}%
404 \_quitvmode\_pdfstartlink \.linkdimens
405 attr{\_pdfborder{\.type}}%
406 user{/Subtype /Link /A \.pdfaction[#1]}\_relax
407 \_localcolor\.linkcolor{\.type}#2\_pdfendlink\_egroup
408 }
409
410 \_nspublic \hlink ;
411
412 \_doc
413 Use `\hlink` as the backing command for OpTeX's \"higher level" linking
414 commands (`\ilink` and `\ulink`).
415
416 The lower level ones (`\xlink` and its predecessor `\link` actually have
417 completely different semantics with regards to color, so we keep them as they
418 are.
419 \_cod
420
421 \_protected\_def\_ilink[#1]#2{\.hlink[#1]{#2}}
422 \_protected\_def\_ulink[#1]#2{{\_escapechar=-1 \_ea}\_expanded
423 {\_noexpand\.hlink[url:\_detokenize{#1}]}{#2}}
424
425 \_public \link \ilink \ulink ;
426
427 %\_protected\_def\_link[#1]#2#3{\_hlink[#1]{#3}}
428 %\_protected\_def\_xlink#1#2#3#4{\_hlink[#1:#2]{#4}}
429
430 \_doc
431 Two customizations of `\hlinks` are possible:
432 \begitems
433 * Dimensions of rectangular areas created by
434 `\pdfstartlink`/`\pdfendlink`. This is done using \`\.linkdimens`
435 (analogous to \OpTeX's `\linkdimens`). Dimensions that are unset are taken
436 from the respective `\hbox`es. \`\lininglinks` sets the dimensions for
437 running text~-- it covers all space of a line using `\baselineskip`.
438 \`\nolininglinks` sets no dimensions, this is useful for buttons, that may
439 have larger height/depth than a line.
440 * The color is determined from the type of link (that is, the first action in
441 <action spec>) by checking `\_<type>linkcolor` (compatible with \OpTeX/).
442 As a fallback `\_ilinkcolor` is used (set by \OpTeX's `\hyperlinks`) for all links
443 except for URLs, where `\_elinkcolor` is used instead. If even these
444 fallback colors are not defined (`\hyperlinks` isn't used), then the most
445 generic `\_linkcolor` will be taken or no color will be set.
446 \enditems
447 \_cod
448
449 \_def\.lininglinks{%
450 \_def\.linkdimens{height.75\_baselineskip depth.25\_baselineskip}%
451 }
452 \_def\.nolininglinks{\_def\.linkdimens{}}
453 \.lininglinks
454
455 \_nspublic \lininglinks \nolininglinks ;
456
457 \_def\.linkcolor#1{\_trycs{_#1linkcolor}{\_trycs{_ilinkcolor}{\_trycs{_linkcolor}{}}}}
458
459 % \_urllinkcolor = \_elinkcolor with fallbacks
460 \_def\_urllinkcolor{\.linkcolor{e}}
461
462 \_doc
463 \secc Open action
464 The document itself has one action defined in the document catalog. It is
465 called `/OpenAction`. We allow the user to set it using the familiar
466 <action spec> syntax with the command \`\openaction``[<action spec>]`.
467
468 Internally we could directly set it by appending to the catalog
469 using the primitive `\pdfcatalog`, but \LuaTeX/ (pdf\TeX/ really) allows
470 setting the action with special syntax. This has the benefit that it is not
471 allowed to set the action more than once.
472 \_cod
473
474 \_def\.openaction[#1]{\_pdfcatalog{} openaction user{\.pdfaction[#1]}\_relax}
475 \_nspublic \openaction ;
476
477 \_doc
478 \label[actions-jump]
479 \secc Jump actions
480
481 These are the most typical actions. Even \LuaTeX/ itself handles them,
482 although we don't use the possibility for maintaining generality. There are a
483 few types of jump actions:
484 \begitems
485 * `/GoTo` actions are the classic internal links to named destinations in the
486 PDF file (created by `\pdfdest` primitive or \OpTeX/'s `\dest`). The
487 destination names include also the type of internal link (e.g.
488 `ref:section1`). They are handled by \`\.ilinkaction``[<type>:<name>]`.
489 * `/URI` actions which are in most cases used as \"goto URL" actions. These
490 are not that useful directly, because special characters should be handled
491 before this actions is used (like with `\url`). The low level use is
492 \`\.urlaction``[url:<url>]`.
493 * \"Goto remote" actions, which can jump to a destination in another PDF file~--
494 either determined by name, or by page number. The external files are
495 expected to be defined by `\filedef` (but not the embedded variant). The
496 use is either \`\.extrefaction``[extref:<name>:<named destination>]` for
497 links to named destination or \`\.extpgrefaction``[extpgref:<name>:<page number>]`
498 for page destinations. Customization is possible with \`\.extrefextra`, by
499 default opening in a new windows is requested.
500 \enditems
501 \_cod
502
503 \_def\.ilinkaction[#1:#2]{/S /GoTo /D (#1:#2)}
504
505 \_def\.urlaction[#1:#2]{/S /URI /URI (#2)}
506
507 \_def\.extrefaction[#1:#2:#3]{/S /GoToR
508 /F \.cs{filespec:#2}
509 /D (#3)
510 \.extrefextra
511 }
512 \_def\.extpgrefaction[#1:#2:#3]{/S /GoToR
513 /F \.cs{filespec:#2}
514 /D [\_the\_numexpr#3-1\_relax\_space /Fit]
515 \.extrefextra
516 }
517
518 \_def\.extrefextra{/NewWindow true}
519
520
521 \_doc
522 Transition action is not really a jump action in of itself, but is only
523 useful when chained after jump actions, so we define it here. Transitions
524 (as page attributes) are handled more thoroughly in section~\ref[transitions].
525
526 The use would look something like:\nl
527 \`\.transitionaction`%
528 `[transition:<animation type>:<duration>:<raw PDF attributes>]`, where all fields
529 omitted from right take the default values.
530 \_cod
531
532 \_def\.transitionaction[#1:#2]{/S /Trans \.attrorempty{Trans}{\.maketrans[#2]}}
533
534 \_doc
535 \secc Named actions
536
537 User can request arbitrary \"named" action with
538 \`\.namedaction``[named:<name>]`. See user documentation for details.
539 \_cod
540
541 \_def\.namedaction[#1:#2]{/S /Named /N /#2}
542
543 \_doc
544 \secc JavaScript actions
545
546 JavaScript actions have two forms, either \`\.jsaction``[js:<name>]` or
547 `\.jsaction``[js:<script>]`. The first variant uses contents of `\filedef`'d
548 <name>, the second one uses <script> directly. There is no special catcode
549 handling.
550
551 \_cod
552
553 \_def\.jsaction[#1:#2]{/S /JavaScript
554 /JS \_ifcsname _pdfextra_filestream:#2\_endcsname \_lastnamedcs \_else
555 (#2)
556 \_fi
557 }
558
559 \_doc
560 \sec Page attributes
561
562 PDF represents pages as dictionaries. The dictionaries get generated by \LuaTeX/,
563 which fills in some attributes {\em attributes} (like `/Content` with
564 contents of the page and `/Annots` with array of annotations). We can add
565 more using `\pdfpageattr` primitive token list register. While not that many
566 are generally useful, there are a few interesting ones. For example
567 transitions can be set using page attributes, or we might want to set
568 additional actions (`/AA`) to listen for page events.
569
570 While the so called \"page objects" are in a tree structure (for fast lookup),
571 only the leaves are real \"pages". PDF allows some attributes to be inherited
572 from parent page objects, but not all of them and certainly not those we are
573 interested in.
574
575 The mechanism introduced in this section is optional, because it takes
576 complete control over `\pdfpageattr`. It gets activated when
577 \`\initpageattributes` is first used (which happens automatically for some
578 functionality exposed by this package), but may be activated by the user for
579 any other purpose. Only attributes listed in \`\pageattributes` are
580 processed.
581
582 We set the attributes anew for each page, by hooking into \OpTeX/'s
583 `\_begoutput`. Because `\pdfpageattr` token list doesn't get expanded before
584 written out to PDF, we expand it using the assignment in `\edef` trick. The
585 token list gets expanded, but the assignment is not made until it reaches
586 main processor when the temporary control sequence gets expanded.
587 \_cod
588
589 % pdfpagattr managament (default for all pages vs current page override)
590 \_def\.pageattributes{{Trans}{Dur}{Rotate}{AA}}
591 \_def\.initpageattributes{%
592 % add hook for setting primitive \pdfpageattr
593 \_addto\_begoutput{\_edef\.tmp{\_pdfpageattr={\.pdfpageattributes}}\.tmp}%
594 % no need to do this twice
595 \_let\.initpageattributes=\_relax
596 }
597 \_nspublic \pageattributes \initpageattributes ;
598
599 \_doc
600 The user interface we want to expose has two parts:
601 \begitems
602 * setting the page attribute for just this one page (\`\.pdfcurrentpageattr`),
603 * setting the default attribute (used when current page value is not set)
604 (\`\.pdfdefaultpageattr`).
605 \enditems
606
607 The first one of course brings in the typical \TeX/ problem of knowing the
608 page where something occurs. As always, the page number contained in
609 `\gpageno` during processing of said content may of course not actually be
610 the number of the page where the content ends up! Hence, we need to note the
611 page number with a delayed write, using `\.setpageof` and later `\.pageof`.
612 The different settings of page attributes should have distinct names, we use
613 the \`\.pageattrcount` counter for this.
614 \_cod
615
616 \_newcount\.pageattrcount
617 \_def\.pdfcurrentpageattr#1#2{\.initpageattributes
618 \_incr\.pageattrcount
619 \.setpageof{pageattr:\_the\.pageattrcount}%
620 \.sxdef{pdfpgattr:\.pageof{pageattr:\_the\.pageattrcount}:#1}{#2}%
621 }
622 \_def\.pdfdefaultpageattr#1#2{\.initpageattributes
623 \.sxdef{pdfpgattr:#1}{#2}%
624 }
625
626 \_doc
627 Finally, the macro \`\pdfpageattributes` takes care of setting generating the
628 contents of `\pdfpageattr`. For each attribute in `\pageattributes` it first
629 checks its current page value, only then the default value. If neither is
630 set, nothing is added.
631 \_cod
632
633 \_def\.pdfpageattributes{\_ea\.pdfpageattributesA\.pageattributes\.end}
634 \_def\.pdfpageattributesA#1{\_ifx\.end#1\_else
635 % use current page override or "default"
636 % don't emit anything if the value is empty
637 \.attrorempty{#1}{%
638 \.trycs{pdfpgattr:\_the\_gpageno:#1}{\.trycs{pdfpgattr:#1}{}}%
639 }%
640 \_ea\.pdfpageattributesA\_fi
641 }
642
643 \_doc
644 Each attributes then has two switches for the respective default and current
645 values. For defining a few of them a helper is introduced:\nl\indent
646 \`\.pdfpageattributesetters`` <attribute> \<default setter> \<current setter> {<value>}`,
647 \nl
648 where <attribute> is name of the attribute without the slash
649 (e.g. `MediaBox`), the two control sequences name the future user setters,
650 which will take single argument in brackets
651 (e.g. `\mediabox` and `\thismediabox`) and the <value> can use the
652 argument.
653 \_cod
654
655 \_def\.pdfpageattributesetters#1 #2#3#4{%
656 \.sdef{\_csstring#2}[##1]{\.pdfdefaultpageattr{#1}{#4}}%
657 \.sdef{\_csstring#3}[##1]{\.pdfcurrentpageattr{#1}{#4}}%
658 \_nspublic #2 #3 ;
659 }
660
661 \_doc
662 Some of the useful attributes are `/Rotate`, which rotates the pages visually
663 (can be set with \`\defaultpagerotate` and \`\pagerotate`), and the additional
664 actions (`/AA`, see section~\ref[actions-additional], set using
665 \`\defaultpageactions` \`\pageactions`).
666 \_cod
667
668 \.pdfpageattributesetters Rotate \defaultpagerotate \pagerotate {#1}
669
670 \.pdfpageattributesetters AA \defaultpageactions \pageactions {\.pdfaactions{#1}}
671
672 \_doc
673 \label[transitions]
674 \secc Transitions, page durations
675
676 There are predefined types of transitions, like `/Wipe`, `/Box`, `/Split`,
677 etc. Most have other customizible attributes~-- usually directions set in
678 different ways depending on the animation type at hand, but the most
679 important attribute is the duration of the animation. Parsing friendly user
680 notation in the form of
681 `[<animation type>:<duration>:<other raw attributes>]`, where fields from the
682 right may be omitted to produce the default value, is handled by
683 \`\.maketrans`. This macro is also used by transition actions
684 (see~\ref[actions-jump]). The defaults are simply those defined by PDF
685 standard (no transition, 1 second duration and the respective default
686 directions).
687 \_cod
688
689 \_def\.maketrans[#1]{\.maketransA#1:::\.end}
690 \_def\.maketransA#1:#2:#3:#4\.end{%
691 \.emptyor{#1}{<</S /\.nonempty \.attrorempty{D}{#2} #3>>}
692 }
693
694 \_doc
695 The attribute setters for transitions (\`\transitions`, \`\transition`) are a
696 simple wrappers. Similiar is the
697 setting of page duration in seconds after which PDF viewer automatically
698 advances to the next page (\`\defaultpageduration`, \`\pageduration`).
699 \_cod
700
701 \.pdfpageattributesetters Trans \transitions \transition {\.maketrans[#1]}
702
703 \.pdfpageattributesetters Dur \defaultpageduration \pageduration {#1}
704
705 \_doc
706 \label[nametrees]
707 \sec Name trees -- attachments and document level JavaScript
708
709 These don't have any last place to be in, so they are documented separately,
710 here. Attaching files using `/FileAttachment` annotations:
711 \begitems \style n
712 * is intended more towards viewers of the document for extra additions and
713 * doesn't work in the viewers as well as one would like.
714 \enditems
715
716 That is why instead embed files using normal `\filedef` and then allow them
717 to be added to the document level `/EmbeddedFiles` entry, which means they
718 will be shown in the user interface by PDF viewers. `/EmbeddedFiles` is a
719 document level name tree (contained inside `/Names` entry of `/Catalog`) that
720 maps names of files to their objects. Although we simplify matters by
721 constructing more of an array.
722
723 What works very similiarly is document level JavaScript. It is a name tree
724 within `/JavaScript` field. It maps names of JavaScript actions to their
725 object numbers. The names aren't very useful, but the actions have their
726 purpose. They are executed in turn after the document is opened. Hence they
727 can be used to predefine JavaScript functions in the global context, to be
728 used later within actions explicitly activated by the user.
729
730 The user level commands are \`\attach``[<name>]` (to attach a previously
731 `\filedef`'d name with fallback to embedding now if it is a valid path) and
732 \`\dljavascript``[<name>]{<script>}` (adds action that executes <script>
733 after document is opened, <name> is more or less meaningless).
734
735 Internally the commands construct lists of what ends up in the resulting name
736 array, i.e. pairs {\visiblesp `(<name>) <object number> 0 R `}. Intermediate
737 macros \`\.embeddedfiles` and \`\.dljavascripts` are used for this.
738
739 In the case of file attachments, nothing happens if file is defined and not
740 found by the fallback.
741
742 \_cod
743
744 % file attachment
745 \_def\.embeddedfiles{}
746 \_def\.attach[#1]{\.isfiledefined{#1}\_iftrue
747 \.xaddto\.embeddedfiles{(#1) \.cs{filespec:#1} }\_fi
748 }
749 \_nspublic \attach ;
750
751 \_def\.dljavascripts{}
752 \_def\.dljavascript[#1]#2{%
753 \_immediate\_pdfobj{<< \.jsaction[js:{#2}] >>}%
754 \.xaddto\.dljavascripts{(#1) \_the\_pdflastobj \_space 0 R }%
755 }
756 \_nspublic \dljavascript ;
757
758 \_doc
759 Renditions (see \ref[mm-renditions]) also need their name tree. This package
760 mostly doesn't play well with Unicode filenames, that is why they are
761 forbidden. However, Renditions that are accessed from JavaScript have to be
762 named/present in a `/Renditions` name tree, with the names encoded in the PDF
763 encoding (UTF-16BE).\nl
764 The names and object references are collected in \`\.renditions`. Unicode
765 encoding is hacked with \`\.pdfstringtounicode`.
766 \_cod
767
768 \_def\.pdfstringstrip(#1){#1}%
769 \_def\.pdfstringtounicode#1#2{%
770 \_ea\_pdfunidef\_ea#1\_ea{\_ea\.pdfstringstrip\_expanded{\.cs{filename:#2}}}%
771 }
772 \_def\.renditions{}
773
774 \_doc
775 Object creation, which is common to all name trees, is handled by\nl\indent
776 \`\.makenamearray``<name tree name><name tree content>`.\nl
777 It doesn't do anything
778 for empty lists, to not bloat PDF files when this mechanism isn't used.
779 \_cod
780
781 \_def\.makenamearray#1#2{\_ifx#2\_empty\_else
782 \_immediate\_pdfobj {<< /Names [ #2 ] >>}%
783 \_pdfnames{/#1 \_the\_pdflastobj \_space 0 R }\_fi
784 }
785
786 \_doc
787 The lists themselves can only be written out to the PDF file at the very end
788 of the run. We use \OpTeX/'s `\_byehook`, which is run in `\_bye`. But `\bye`
789 itself may be predefined by the user, for example when using some of the
790 \OpTeX/ tricks. We just hope that the user keeps `\_byehook`.
791 \_cod
792
793 \_addto\_byehook{%
794 \.makenamearray{EmbeddedFiles}\.embeddedfiles
795 \.makenamearray{JavaScript}\.dljavascripts
796 \.makenamearray{Renditions}\.renditions
797 }
798
799 \_doc
800 \sec Viewer preferences
801
802 There are a few customizations of display (and other preferences of PDF
803 viewers) possible in the document catalog or its subdictionary
804 `/ViewerPreferences`. Most are not that useful. The interesting
805 ones are implemented by \`\fullscreen`, \`\showoutlines`, \`\showattached`.
806 They all set the page mode using \`\.setpagemode`. We don't handle respecting
807 the last setting (using \`\_byehook`). To prevent invalid PDF files, we set
808 `\.setpagemode` to `\_relax` after use.
809 \_cod
810
811 \_def\.setpagemode#1{\_pdfcatalog{/PageMode /#1}\_glet\.setpagemode=\_relax}
812
813 \_def\.fullscreen{\.setpagemode{FullScreen}}
814 \_def\.showoutlines{\.setpagemode{UseOutlines}}
815 \_def\.showattached{\.setpagemode{UseAttachments}}
816
817 \_nspublic \fullscreen \showoutlines \showattached ;
818
819 \_doc
820 Only the setting of duplex / double sided printing and display is in the
821 nested dictionary. It is handled by \`\duplexdisplay`. The simplistic version
822 does not handle more attributes in `/ViewerPreferences`. We also set the
823 meaning to `\_relax` to prevent more (erroneous) uses.
824 \_cod
825
826 \_def\.duplexdisplay{\_pdfcatalog{%
827 /PageLayout /TwoPageRight
828 /ViewerPreferences <<
829 /Duplex /DuplexFlipLongEdge
830 >>}%
831 \_glet\.duplexdisplay=\_relax
832 }
833
834 \_def\duplexdisplay{\.duplexdisplay}
835
836 \_doc
837 \sec Multimedia
838
839 PDF essentially allows insertion of different types of multimedia:
840 \begitems
841 * images,
842 * audio/video,
843 * 3D art.
844 \enditems
845
846 The first is pretty standard and handled normally by the engine (\LuaTeX).
847 Others are possible, but have to be done manually according to one of the
848 mechanisms specified by PDF standard:
849
850 \begitems
851 * Sounds (audio only),
852 * Movies (video and/or audio),
853 * Renditions (video and/or audio),
854 * 3D annotations (3D art),
855 * Rich Media (video and/or audio, 3D art)
856 \enditems
857
858 Sadly all these mechanisms are badly flawed, each in different ways. At least
859 we try to use the one that works in the viewers.
860
861 For audio/video \"Movies" are the simplest mechanism, but they have been
862 deprecated in PDF 2.0 and no longer work in Acrobat/Foxit (same for
863 \"Sounds").
864
865 \"Renditions" are complicated, partly dependant on JavaScript, but at least
866 supported by Acrobat, Foxit, Evince and Okular.
867
868 \"Rich media" annotations were designed for Flash. This use case is no longer possible
869 today, but the obscurities remain. They are unnecessiraly complicated, but can
870 be used without Flash too. Although the result is very plain for audio/video~--
871 no controls can be displayed and there are no associated actions.
872
873 \"3D annotations" are reasonably simple, but also flawed. They cannot reuse
874 embedded file as a source for 3D data. Hence it is better and more consistent
875 to use Rich Media for 3D annotations. It even has additional benefits, like
876 the possibility of using multiple initialization scripts.
877
878 In the end, this package exposes two user commands corresponding to two
879 mechanisms~-- first are Renditions (`\render`) for audio/video that works in most
880 browsers and Rich Media (`\RM`) mainly for 3D art, but also for audio/video
881 with limited possibilities.
882
883 Both mechanisms have an annotation at their core. Annotations is essentially
884 a rectangular area on page. The area corresponds to where the
885 multimedium will show up. After activating the area somehow (by user click,
886 or action) the multimedium will start playing. Before annotations the
887 rectangular area will show something that is called \"normal appearance".
888 This appearance is of type form XObject. Those are really similiar to pages~--
889 they have dimensions, contents made up of PDF graphics operators, \dots,
890 but they are reusable. Not that useful for annotations where we will need the
891 form only once, but nice anyways. pdf\TeX/ has primitives for creating them~--
892 `\pdfxform` and friends. They essentially do the same code like `\shipout`
893 does, but instead of page, they make this reusable object. One can then
894 either use this reusable object in another page/form, but we will indirectly
895 refer to it for the appearance.
896
897 Important aspect of annotations is that they are really only rectangular
898 areas on the page, but they are not really part of the page. They sort of sit
899 on another level and are not influenced by PDF graphic operators which make
900 the page. In pdf\TeX/ annotations are handled by {\em whatsit} nodes. While
901 most nodes map to known primitive \TeX/ concepts (like typeset characters,
902 boxes, rules, etc.) Whatsits are essentially commands for \TeX/ that are
903 delayed until page is being shipped out (written to PDF file). `\write`,
904 `\special`, and most pdf\TeX/ commands create whatsits. For annotationos this
905 is important, because this means that the engine only stores the information
906 about annotation that we specify, but creates it at due time, when it should
907 be written to PDF.
908
909 Because whatsits are essentially dimensionless and we want it to be a part of
910 normal \TeX/ typesetting material we create the annotation (whatsit) in
911 `\hbox`. This box will be otherwise empty, because the apperance of the
912 rectangular area is determined by the normal appearance field (`/N` in
913 `/AP`). We set the dimensions of the box to the dimensions of normal
914 appearance. Everything will line up nicely, because when processed, the
915 annotation will take dimensions from the box.
916
917 All of these concepts are implemented in:\nl\indent
918 \`\.boxedannot``[<type>:<name>]{<appearance>}{<special text>}{<annotation attributes>}`\nl
919 <type> is used to determine the annotation border (same principle as with
920 Link annotations, section~\ref[actions-link]), <name> will be used as the
921 annotation name (`/NM`), <special text> is used for influencing the
922 `\pdfannot` primitive, and <annotation attributes> will become the body of
923 the annotation.
924
925 \_cod
926
927 \_def\.boxedannot[#1:#2]#3#4#5{%
928 \_setbox0=\_hbox{#3}\_setbox2=\_null
929 \_ht2=\_ht0 \_wd2=\_wd0 \_dp2=\_dp0
930 \_preshipout0 \box0
931 \_immediate\_pdfxform0
932 % box with annotation both stretching to dimensions of appearance
933 \_hbox{\.setpageof{#1:#2}%
934 \_pdfannot #4 {#5
935 /AP <</N \_the\_pdflastxform \_space 0 R>>
936 \_pdfborder{#1}
937 /NM (#2)
938 /Contents (#1 '#2')
939 }%
940 \_copy2
941 }%
942 }
943
944 \_doc
945 There is another weird thing common to both multimedia mechanisms~--
946 the redefinition of `\.name`. It is initially
947 set by `\.secondoptdef` to <name>, but may be redefined by user supplied
948 `name` key-value parameter. This should be used when there are multiple uses
949 of the same content. Otherwise samely named annotations would be
950 indistinguishable both for PDF viewer and our handling of actions (which
951 would all refer only to the first instance).
952
953 To somewhat overcome this, trying to use the same <name> (within the same
954 type of annotaiton) will use dummy name from \`\.unnamedannotcount` (for
955 uniqueness). This means that <name> will always refer to the first instance.
956 \`\.redefinename` handles this.
957 \_cod
958
959 \_newcount\.unnamedannotcount
960 \_def\.redefinename#1{%
961 \.isdefined{#1:\.name}\_iftrue
962 \_incr\.unnamedannotcount
963 \_edef\.name{\_the\.unnamedannotcount}%
964 \_else
965 \_edef\.name{\_kv{name}}%
966 \_fi
967 }
968
969 \_doc
970 \label[mm-renditions]
971 \secc Renditions (audio/video)
972
973 There are three main types of PDF objects involved in the Renditions
974 (\"Multimedia") mechanism:
975
976 \begitems
977 * Screen annotations define the area for playing multimedia.
978 * Rendition objects define the multimedia to play.
979 * Rendition actions associate Rendition objects with Screen annotations.
980 \enditems
981
982 You can theoretically arbitrarily mix and match rendition objects and screen
983 annotations by invoking different actions. In practice Evince and Okular do
984 really simplistic parsing and don't fully support the actions fully. But by
985 keeping it simple it is possible to make it work almost the same in all
986 viewers that support renditions.
987
988 Different sources of audio/video should be possible. In fact all three file
989 specifications (embedded files, files specified by URL/path) could work.
990 Again in practice embedded file is the safest bet, that works in all viewers
991 that support renditions.
992
993 The user facing command is:\nl\indent
994 \`\render``[<name>][<optional key-value paramers>]{<horizontal material>}`\nl\noindent
995 <name> is the friendly name set using `\filedef` or file path if <name> isn't
996 `\filedef`d and is to be embedded. The key-value parameters in brackets
997 can be entirely omitted. They can influence the playback (except for `controls`
998 most are not well supported). Default values are taken from
999 `\.renderdefaults`.
1000
1001 `\render` doesn't do anything (except print warning) if file <name> isn't
1002 defined and <name> isn't a path to file that can be embedded.
1003
1004 The first PDF object it defines is Rendition, which specifies information
1005 about the multimedium (name, file specification, MIME type and options from
1006 key-value parameters). Some of the fields are in `/BE` (\"best effort")
1007 dictionaries. This is due to the very general design of Renditions, which
1008 theoretically allows the PDF viewer to choose from multiple Renditions if
1009 they know they can't support some of the requested features. But that is not
1010 much useful in practice, so we just don't complicate it.
1011
1012 Next defined object is Screen annotation, which complicates thing by
1013 requiring (`/P`) reference to the page where the annotation is (handled by
1014 `\setpageof` and `\pageof` pair). Important field is `/A` which specifies
1015 actions that shall be executed when the screen area is clicked. We let the
1016 user change the action, but the sensible default of starting to play the
1017 multimedium is used (and this is the only thing that works in some viewers
1018 anyways). Additional actions `/AA` may be used to react to events like mouse
1019 over or page open/close~-- the most probable use case is autoplay on page
1020 open, for which shortcut of \`\renditionautoplay` is defined.
1021
1022 The code is slightly complicated by the fact, that actions need to reference
1023 the Rendition and Screen objects. In the case of the action contained in
1024 Screen annotation this essentialy involves a self reference. Hence it is
1025 needed to first reserve an object number and later use it for the annotation.
1026 Because the object numbers may also be needed by actions defined later, we need
1027 to save them to `\_pdfextra_rendition:<name>` and `\_pdfextra_screen:<name>`
1028 respectively, but also define aliases with empty names, so users can easily
1029 reference the latest rendition.
1030
1031 \_cod
1032
1033 \.secondoptdef\.render#1{\.isfiledefined{\.name}\_iftrue\_bgroup
1034 \_ea\_readkv\_ea{\_ea\.renderdefaults\_ea,\_the\_opt}%
1035 \.colortorgbdef\.bgcolor{\_kv{background}}%
1036 % rendition object ("media specifaction")
1037 \.pdfstringtounicode\.uiname\.name
1038 \_pdfobj {<</Type /Rendition
1039 /S /MR
1040 /N \.uiname
1041 /C <<%/Type /MediaClip
1042 /S /MCD % subtype MediaClipData
1043 /D \.cs{filespec:\.name}
1044 /CT (\.cs{filemime:\.name})
1045 /P << /TF (TEMPALWAYS) >> % allow creating temporary files
1046 >>
1047 /P <<%/Type /MediaPlayParams
1048 /BE << /C \_kv{controls} /V \_kv{volume} /RC \_kv{repeat} >>
1049 >>
1050 /SP <<%/Type /MediaScreenParams
1051 /BE << /O \_kv{opacity} /B [\.bgcolor] >>
1052 >>
1053 >>}\_pdfrefobj\_pdflastobj
1054 \.xaddto\.renditions{\.uiname \_the\_pdflastobj \_space 0 R }%
1055 \.redefinename{rendition}%
1056 \.sxdef{rendition:\.name}{\_the\_pdflastobj}%
1057 % screen annotation ("screen space allocation")
1058 \_pdfannot reserveobjnum% "self" reference will be needed inside screen annot.
1059 \.sxdef{screen:\.name}{\_the\_pdflastannot}%
1060 % aliases to latest rendition/screen with empty name
1061 \_global\.slet{rendition:}{rendition:\.name}%
1062 \_global\.slet{screen:}{screen:\.name}%
1063 \_edef\.action{\_kv{action}}\_edef\.aactions{\_kv{aactions}}%
1064 \.boxedannot[rendition:\.name]{#1}{useobjnum\_the\_pdflastannot}{%
1065 /Subtype /Screen
1066 % reference to page of the rendition (\setpageof done by \.boxedannot)
1067 % the spaces are weird, but \pdfpageref eats them
1068 /P \_pdfpageref\.pageof{rendition:\.name} \_space 0 R
1069 /A \_ea\.pdfaction\_ea[\.action]
1070 /AA \_ea\.pdfaactions\_ea{\.aactions}
1071 }%
1072 \_egroup\_fi
1073 }
1074 \_nspublic \render ;
1075
1076 \_doc
1077 Here are the defaults used for `\render`~-- \`\.renderdefaults`. Users can
1078 redefine them all together or override as needed with key-value parameters.
1079 The defaults correspond to values specified by PDF standard. Other values may
1080 not be respected by all viewers.
1081 \_cod
1082
1083 \_def\.renderdefaults{%
1084 name=\.name,
1085 controls=false,
1086 volume=100,
1087 repeat=1,
1088 opacity=1.0,
1089 background=1 1 1,
1090 action=rendition::play,
1091 aactions={},
1092 }
1093
1094 \_doc
1095 Most probable use of additional actions is to start auto-start playing of the
1096 multimedium. For this purpose \`\renditionautoplay` is defined as a shorthand
1097 for action to play the lastly defined rendition on page visible event.
1098 \_cod
1099
1100 \_def\.renditionautoplay{{PV}{rendition::play}}
1101 \_nspublic \renditionautoplay ;
1102
1103 \_doc
1104 \seccc Rendition actions
1105
1106 Rendition actions unfortunately use cryptic symbolic numbers (`0`, `1`, `2`
1107 and `3`) for actions that could be called `play`, `stop`, `pause` and
1108 `resume` respectively. Except for these predefined actions (that use `/OP`)
1109 running of JavaScript is possible using `/JS (<script>)` with potential
1110 fallback to `/OP`. This is dangerous teritory, because support of the right
1111 API in the viewer is very low. Although it is possible to define such action
1112 type by:
1113
1114 \begtt
1115 \.sdef{renditionaction:myaction}{/JS (app.alert("something useful");) /OP 0}
1116 \endtt
1117
1118 The use of rendition action is:
1119 \`\.renditionaction``[rendition:<name>:<action type>]`. Empty name refers to
1120 last rendition, so e.g.`\.renditionaction``[rendition::pause]` is possible.
1121 \_cod
1122
1123 \.sdef{renditionaction:play}{/OP 0}
1124 \.sdef{renditionaction:stop}{/OP 1}
1125 \.sdef{renditionaction:pause}{/OP 2}
1126 \.sdef{renditionaction:resume}{/OP 3}
1127 \_def\.renditionaction[#1:#2:#3]{/S /Rendition
1128 \.cs{renditionaction:#3}
1129 /R \.cs{rendition:#2} 0 R
1130 /AN \.cs{screen:#2} 0 R%
1131 }
1132
1133 \_doc
1134 \secc Rich Media (3D/audio/video)
1135
1136 Some principles seen with Renditions (section~\ref[mm-renditions]) apply here
1137 too. But additionally we deal with 3D specifics and unfortunate Flash
1138 leftovers.
1139
1140 Unlike Renditions both page area and multimedium specifaction are handled in
1141 a single annotation~-- the Rich Media annotation. The code is unfortunately
1142 obscured due to the weird requirements, but this is essentially what we are
1143 trying to create with \`\RM`:
1144 \begtt
1145 /Type /Annot
1146 /Subtype /RichMedia
1147 /RichMediaSettings <<
1148 /Activation <<
1149 /Condition /PV
1150 /Scripts [ 14 0 R ]
1151 >>
1152 /Deactivation << /Condition /XD >>
1153 >>
1154 /RichMediaContent <<
1155 /Assets << /Names [ (kladka.prc) 2 0 R (wireframe.js) 14 0 R ] >>
1156 /Configurations [ <<
1157 /Type /RichMediaConfiguration
1158 /Subtype /3D
1159 /Instances [ <<
1160 /Type /RichMediaInstance
1161 /Subtype /3D
1162 /Asset 2 0 R
1163 >> ]
1164 >> ]
1165 >>
1166 \endtt
1167
1168 The activation/deactivation will be dealt with later. But we see that to
1169 insert a simple 3D file, we have to pack it inside a file specification
1170 (indirect reference to object `2 0 R`), then in \"instance", inside a
1171 \"configuration" inside \"content". As if it wasn't enough the names
1172 (normally contained in the file specification) have to be specified again in
1173 `Assets` name tree that uselessly maps names to file specifications. Because
1174 this is a 3D Rich Media annotation there are other files at play~--
1175 initialization scripts. These are specified in `/Scripts` and are executed in
1176 turn when the annotation is activated. Not shown is, that some
1177 \"configurations" and \"instances" actually have to be specified indirectly.
1178
1179 If it wasn't for Flash we could do with something like:
1180
1181 \begtt
1182 /Type /Annot
1183 /Subtype /RichMedia
1184 /Activation /PV
1185 /Scripts [ 14 0 R ]
1186 /Deactivation /XD
1187 /Content 2 0 R
1188 \endtt
1189
1190 Which contains equivalent information. But unfortunately here we are\dots
1191 \_cod
1192
1193 \.secondoptdef\.RM#1{\.isfiledefined{\.name}\_iftrue
1194 \_edef\.tmp{\.cs{filemime:\.name}}%
1195 \_edef\.subtype{\_ea\.mimetormsubtype\_ea[\.tmp]\_space}%
1196 \_ifx\.subtype\_space
1197 \_opwarning{unknown rich media type for '\.name', ignored}\_else
1198 \_bgroup
1199 \_ea\_readkv\_ea{\_ea\.RMdefaults\_ea,\_the\_opt}%
1200 % Instance that has the media file as an asset
1201 \_pdfobj {<</Type /RichMediaInstance
1202 /Subtype /\.subtype
1203 /Asset \.cs{filespec:\.name}
1204 >>}\_pdfrefobj\_pdflastobj
1205 % Configuration with one single instance (the above)
1206 \_pdfobj {<</Type /RichMediaConfiguration
1207 /Subtype /\.subtype
1208 /Instances [ \_the\_pdflastobj \_space 0 R ]
1209 >>}\_pdfrefobj\_pdflastobj \_edef\.configuration{\_the\_pdflastobj}%
1210 \_edef\.names{\.cs{filename:\.name} \.cs{filespec:\.name} }% initial asset
1211 \.redefinename{rm}%
1212 \_def\.scriptfilespecs{}%
1213 \_edef\.views{\_kv{views}}\_edef\.scripts{\_kv{scripts}}%
1214 \_ifx\.views\_empty \_edef\.views{\.name}\_fi
1215 \_ea\.DDDscripts\_ea{\.scripts}%
1216 % annotation in hbox
1217 \.boxedannot[rm:\.name]{#1}{}{%
1218 /Subtype /RichMedia
1219 /RichMediaSettings <<
1220 /Activation <<
1221 /Condition \.cs{activation:\_kv{activation}}
1222 \.emptyor{\.scriptfilespecs}{/Scripts [ \.nonempty ]}
1223 /Presentation << /Toolbar \_kv{toolbar} \.RMpresentationextra >>
1224 >>
1225 /Deactivation << /Condition \.cs{deactivation:\_kv{deactivation}} >>
1226 >>
1227 /RichMediaContent <<
1228 /Assets << /Names [ \.names ] >>
1229 /Configurations [ \.configuration \_space 0 R ]
1230 \.emptyor{\_ea\.DDDviews\_ea{\.views}}{/Views [ \.nonempty ]}
1231 >>
1232 }%
1233 \.sxdef{rm:\.name}{\_the\_pdflastannot}%
1234 \_global\.slet{rm:}{rm:\.name}%
1235 \_egroup\_fi\_fi
1236 }
1237 \_nspublic \RM ;
1238
1239 \_doc
1240 The code is similiar to `\render`, but we also ignore everything if we don't
1241 recognize the type of media (`Video`, `Sound` or `3D`). For that we use a
1242 simple mapping from MIME types with \`\.mimetormsubtype`. This means that
1243 although we aim Rich Media mostly for 3D art it may also be used for Video
1244 and Sound.
1245 \_cod
1246
1247 \_def\.mimetormsubtype[#1/#2]{\.cs{rmtype:#1}}
1248
1249 \.sdef{rmtype:model}{3D}
1250 \.sdef{rmtype:video}{Video}
1251 \.sdef{rmtype:audio}{Sound}
1252
1253 \_doc
1254 Then we also need to construct the weird name \"tree" (essentialy an array in
1255 our case) and script array. \`\.DDDscripts` and \`\.DDDviews` do this.
1256 Name tree is accumulated in `\.names`, and starts with the media file. After
1257 that each script is added to \`\.names` and \`\.scriptfilespecs`. The scripts
1258 are passed as a comma separated array. Ignoring initial spaces is done using
1259 undelimited-delimited argument pair trick.
1260 \_cod
1261
1262 \_def\.DDDscripts#1{\.DDDscriptsA#1,,,\.end}
1263 \_def\.DDDscriptsA#1#2,{\_ifx,#1\_ea\.untilend\_else
1264 \.isfiledefined{#1#2}\_iftrue%
1265 \_addto\.scriptfilespecs{\.cs{filespec:#1#2} }%
1266 \_addto\.names{\.cs{filename:#1#2} \.cs{filespec:#1#2} }%
1267 \_fi
1268 \_ea\.DDDscriptsA\_fi
1269 }
1270
1271 \_doc
1272 For 3D views we need to process yet another comma separated list, this time
1273 with \`\.DDDviews`. The result has to be separated by spaces and we also
1274 don't want to emit something if the specified view was invalid. Unfortunately
1275 this is expansion only context, so we can't issue a warning.
1276
1277 As a user convenience, before `\.DDDview` is executed, view with the name of
1278 `\.name` is tried instead of empty view array. This means that for simple 3D
1279 art with one view, one can create view with the same name as the 3D object
1280 and not have to specify anything. We also take the name only after it is
1281 redefined from optional key-value parameters~-- this is so we can support
1282 even the case of e.g. `screw` 3D model used twice, once with `name=screw1`,
1283 another time with `name=screw2` (with the corresponding `screw1` and `screw2`
1284 views). This is probably less useful, but\dots
1285 \_cod
1286
1287 \_def\.DDDviews#1{\.DDDviewsA#1,,,\.end}
1288 \_def\.DDDviewsA#1#2,{\_ifx,#1\_ea\.untilend\_else
1289 \.isdefined{3dview:#1#2}\_iftrue
1290 \_lastnamedcs\_space \_fi
1291 \_ea\.DDDviewsA\_fi
1292 }
1293
1294 \_doc
1295 The activation/deactivation names are kind of cryptic, so we give them
1296 descriptive names. Default is explicit (de)activation. Instead of `/PV` (page
1297 visible) and `/PI` (page invisible) it would be possible to use \"page open"
1298 and \"page close". These are slightly different in cases when more pages are
1299 shown on screen at once, because only one page is \"open", while multiple are
1300 \"visible".
1301 \_cod
1302
1303 \.sdef{activation:explicit}{/XA}
1304 \.sdef{activation:auto}{/PV}
1305 \.sdef{deactivation:explicit}{/XD}
1306 \.sdef{deactivation:auto}{/PI}
1307
1308 \_doc
1309 Additional means of customization are here. \`\.RMdefaults` contains the
1310 default key-value parameters. \`\.RMpresentationextra` can be used to set
1311 more attributes in `/RichMediaPresentation` dictionary (although those are
1312 more specific and not generally useful).
1313 \_cod
1314
1315 \_def\.RMdefaults{%
1316 name=\.name,
1317 activation=explicit,
1318 deactivation=explicit,
1319 toolbar=true,
1320 views=,
1321 scripts=,
1322 }
1323 \_def\.RMpresentationextra{}
1324
1325 \_doc
1326 For scripting using JavaScript actions one needs to access the 3D context of
1327 the 3D / Rich Media annotation. This requires the page number. We can't use
1328 `this.pageNum`, because the script strictly doesn't have to be on
1329 the same page. We use `\.pageof` (`\.setpageof` was done in `\.boxedannot`)
1330 to retrieve the page number in next run. Also PDF indexes page numbers from
1331 0. \`\DDDannot``{<name>}`. and \`\DDDcontext``{<name>}` allow this.
1332 \_cod
1333
1334 \_def\.DDDannot#1{%
1335 this.getAnnotRichMedia(\_the\_numexpr\.pageof{rm:#1}-1\_relax, '#1')%
1336 }
1337 \_def\.DDDcontext#1{\.DDDannot{#1}.context3D}
1338
1339 \_nspublic \DDDannot \DDDcontext ;
1340
1341 \_doc
1342 \label[mm-3dviews]
1343 \secc 3D views
1344
1345 This is the interesting part about 3D art. They can have a set of predefined
1346 views~-- although a user may start from one, they can interactively change
1347 all the aspects by dragging with mouse or messing with the settings shown by
1348 right click menu.
1349
1350 There are several transformations that have to be done before it is possible
1351 to display 3D scene on a computer screen:
1352
1353 \begitems \style n
1354 * 3D transformation from the coordinate system of 3D artwork (\"model") to the \"world
1355 coordinate system".
1356 * 3D transformation from the world coordinate system to camera coordinate
1357 system.
1358 * projection to 2D (3D to 2D transformation).
1359 \enditems
1360
1361 When talking about PDF, positive $x$ goes to the right, positive $y$ up, and
1362 positive $z$ \"away" from us (\"into the page"). This means we are working
1363 with a left handed coordinate system. In camera space, the camera sits at
1364 $(0, 0, 0)$ facing towards positive $z$ with positive $x$ and $y$ going right
1365 and up respectively. Projection (one way or another) discards the $z$
1366 coordinate.
1367
1368 Although the transformations are not strictly linear, they are essentially
1369 done using multiplication by {\em transformation matrices}. The matrix for
1370 \"model to world" (or \"model") transformation is part of the 3D art file and can't
1371 be changed. However, we can make it up, because we can fully control the
1372 second transformation (\"world to camera" or \"view" transformation)~--
1373 although we don't specify the \"world to camera" matrix but rather its
1374 inverse, the \"camera to world" matrix (`/C2W`). This matrix has the $4 \times 4$
1375 form, which also allows {\em linear transformation} and {\em translation}:
1376
1377 $$
1378 M_{c2w} = \pmatrix{a & d & g & t_x \cr b & e & h & t_y \cr c & f & i & t_z \cr 0 & 0 & 0 & 1}
1379 $$
1380
1381 Here we use the column major convention, which is also the order how we would
1382 write the matrix to PDF file, where it is an array of 12 elements:
1383
1384 \begtt \catcode`\$=3
1385 /C2W [$a$ $b$ $c$ $d$ $e$ $f$ $g$ $h$ $i$ $t_x$ $t_y$ $t_z$]
1386 \endtt
1387
1388 In the rendering pipeline everything is transformed from world coordinates to
1389 camera space coordinates. We can think about the process also in the other
1390 way. Using $M_{c2w}$ we specify camera's position and orientation in the
1391 world coordinate system. Due to how transformation using matrix
1392 multiplication works, the first column in the $M_{c2w}$ matrix (vector $(a,
1393 b, c)^T$) specifies how \"positive $x$ direcetion" (\"right") from camera
1394 space ends up in world coordinate system. Similiarly for $(d, e, f)^T$ being
1395 the image of positive $y$ (\"up") and $(g, h, i)^T$ being the image of
1396 positive $z$ (\"forward"). The last column, $(t_x, t_y, t_z)^T$ represents
1397 translation from camera space to world. Translation of origin (camera
1398 position) will leave it in the point with coordinates $(t_x, t_y, t_z)$.
1399 Because of these associations with the intuitive meanings of $x$,
1400 $y$, $z$ in camera space we also sometimes call the vectors in the first
1401 three columns of $M_{c2w}$ \"right", \"up" and \"forward" and the last one
1402 \"eye":
1403
1404 $$
1405 \vec{R} = \pmatrix{a \cr b \cr c}\!, \quad
1406 \vec{U} = \pmatrix{d \cr e \cr f}\!, \quad
1407 \vec{F} = \pmatrix{g \cr h \cr i}\!, \quad
1408 \vec{E} = \pmatrix{t_x \cr t_y \cr t_z}\!.
1409 $$
1410
1411 We usually want $\vec{R}$, $\vec{U}$ and $\vec{F}$ to form an orthonormal set
1412 of vectors, i.e. all of unit length and each pair is orthogonal. The
1413 orthoganility will come from the way we calculate them, but the normality has
1414 to be ensured by normalizing the vectors after computing them, which will not
1415 be explicitly written out in the following text. $\vec{E}$ is a positional,
1416 not directional, vector and it's length will be preserved.
1417
1418 Now we only need a convenient way to calculate all four vectors. A wide spread
1419 method is sometimes called \"look at". It essentially involves having two
1420 points: \"eye" ($E$, position of the camera) and \"target" ($T$, the point
1421 where the camera is pointing at). The camera position is already provided:
1422 $$
1423 \vec{E} = E
1424 $$
1425 From these two points alone we can easily calculate the forward vector, which
1426 corresponds to the direction of the camera:
1427 $$
1428 \vec{F} = \overrightarrow{ET} = T - E
1429 $$
1430 When we now imagine the point $E$ and vector $\vec{F}$ looking towards $T$ we
1431 can see that there is a degree of freedom~-- the camera can rotate about the
1432 forward vector. There is no other way than to arbitrarily choose
1433 either up or right vector. Usually we choose an arbitrary \"global up" vector
1434 $U_G$, which will influence the general direction of the final up vector.
1435 This is because we use it to calculate the right vector:
1436 $$
1437 \vec{R} = \vec{U}_G \times \vec{F}
1438 $$
1439 The cross product makes it so that:
1440 \begitems \style n
1441 * $\vec{R}$ is perpendicular to $\vec{F}$
1442 * it is also perpendicular to global up vector ($\vec{U}_G$) which we used to
1443 get rid of remaining degree of freedom.
1444 \enditems
1445 Now that we have two orthonormal vectors (with the normalization not being
1446 explicit) we can calculate the remaining up vector:
1447 $$
1448 \vec{U} = \vec{F} \times \vec{R}
1449 $$
1450
1451 The last mysterious part about the calculation are the cross products. They
1452 are of course not commutitative, so why e.g. $\vec{U}_G \times \vec{F}$ and
1453 not the other way around? This is because we have to preserve the relations
1454 these vectors had as directions of positive axes of the original camera
1455 space. There we had positive $x$ going right, positive $y$ up and positive
1456 $z$ forward in a left handed coordinate system. This means that following
1457 holds (according the left hand rule):
1458
1459 $$\eqalign{
1460 \vec{R} &= \vec{U} \times \vec{F} \cr % j * k = i
1461 \vec{U} &= \vec{F} \times \vec{R} \cr % k * i = j
1462 \vec{F} &= \vec{R} \times \vec{U} \cr % i * j = k
1463 }$$
1464
1465 The scheme has one flaw though. When the directions of global up vector
1466 $\vec{U}_G$ and forward vector $\vec{F}$ are linearly dependent the computed
1467 right vector will be $(0, 0, 0)$. Hence some handling of this special case is
1468 needed.
1469
1470 The \"look at" method is essentially what is used in Alexander Grahn's
1471 package movie15\fnote{\url{https://www.ctan.org/pkg/movie15}}. Although the
1472 input aren't two points, but rather a \"center of orbit" point ($COO$, our
1473 \"target"), \"center of orbit to camera vector" ($\overrightarrow{C2C}$,
1474 default is $(0, -1, 0)$) and distance of camera from the center of orbit
1475 ($ROO$). The value used for the arbitrary \"global up" vector is $(0, 0, 1)$.
1476 When forward vector is $(0, 0, z)$, then global up is chosen to be $(0, -1,
1477 0)$ or $(0, 1, 0)$ to handle the \"0 right vector" issue.
1478
1479 Because the movie15 method of providing the parameters is used in essentially
1480 all packages that handle PDF 3D art (movie15, media9, rmannot, Con\TeX/t) we
1481 also follow the suite.
1482
1483 \`\DDDview`[<view name>][<key-value parameters>] is the command for defining
1484 3D views. These have to be saved into separate PDF objects anyways, using
1485 this interface we allow their reuse. If <view name> is same as <name> of
1486 `\RM` argument and no other views are specified <view name> view is
1487 automatically used (see `\RM` for details).
1488
1489 Key-value parameters are not optional this time, because rarely one suffices
1490 with default values~-- different 3D views are about customization. Handling
1491 of them is not as straightforward as before. We initially read the key-value
1492 parameters only to determine the `method` used for calculating the `/C2W`
1493 matrix. Then we reread key-value parameters again, this time with also with
1494 the default values for this particular method. Not that the general 3d views
1495 details are changed, but the methods themselves have key-value parameters of
1496 their own, and we support specifying them in this \"flat" way.
1497
1498 Additionally we allow the different methods used to compute `/C2W` to not be
1499 expandable. Hence they are executed outside of expansion only context and are
1500 fully processed~-- the text they add to 3D view PDF object is temporarily
1501 stored in \`\.viewparams`.
1502
1503 The rest is simply setting sensible defaults (or user overrides) for
1504 internal/external name of the view (`/IN` and `/XN`, one is used for
1505 scripting, one is shown by the PDF viewer), background color, rendering mode,
1506 and lighting. Cross sections and nodes are currently not supported, although
1507 users can hook in their own code using `\.DDDviewextra`,
1508 `\.DDDrendermodeextra` or `\.DDDprojectionextra`.
1509
1510 We have to be careful about setting rendering mode and lighting scheme,
1511 because they normally fall back to the values specified in 3D art file, which
1512 we can't access, so better not set them to anything if they are empty.
1513 \_cod
1514
1515 \_def\.DDDview[#1][#2]{\_bgroup
1516 \_readkv{\.DDDviewdefaults,#2}%
1517 \_edef\.tmp{\.DDDviewdefaults,\.cs{3dview:\_kv{method}:defaults}}%
1518 \_readkv{\.tmp,#2}%
1519 \.colortorgbdef\.bgcolor{\_kv{background}}%
1520 \.cs{3dview:\_kv{method}}% sets \.viewparams (/MS, /C2W, /CO)
1521 \_pdfobj {<</Type /3DView
1522 /XN (#1)
1523 /IN (#1)
1524 \.viewparams % /MS, /C2W, /CO
1525 /P <<
1526 \.cs{3dprojection:\_kv{projection}}
1527 \.DDDprojectionextra
1528 >>
1529 /BG <<%/Type /3DBG
1530 /Subtype /SC
1531 /C [\.bgcolor]
1532 >>
1533 \.emptyor{\_kv{rendermode}}{%
1534 /RM <<%/Type /3DRenderMode
1535 /Subtype /\.nonempty
1536 \.DDDrendermodeextra >> }%
1537 \.emptyor{\_kv{lighting}}{%
1538 /LS <<%/Type /3DLightingScheme
1539 /Subtype /\.nonempty >> }%
1540 >>}%
1541 \_pdfrefobj\_pdflastobj
1542 \.sxdef{3dview:#1}{\_the\_pdflastobj \_space 0 R}%
1543 \_egroup
1544 }
1545
1546 \_nspublic \DDDview ;
1547
1548
1549 \_doc
1550 \`\.DDDviewdefaults` stores default key-value parameters for 3D views. They
1551 are mostly the PDF standard defaults or what movie15/media9 uses (for
1552 compatibility).\`\.DDDviewextra`, \`\.DDDrendermodeextra` or
1553 \`\.DDDprojectionextra` can be used by the users to hook themself into 3D
1554 view object creation.
1555 \_cod
1556
1557 \_def\.DDDviewdefaults{
1558 projection=perspective,
1559 scale=1,
1560 ps=Min,
1561 FOV=30,
1562 background=1 1 1,
1563 rendermode=,
1564 lighting=,
1565 method=media9,
1566 }
1567 \_def\.DDDprojectionextra{}
1568 \_def\.DDDrendermodeextra{}
1569 \_def\.DDDviewextra{}
1570
1571 \_doc
1572 There are two different projection methods:
1573 \begitems
1574 * Orthographic: $z$ coordinate is simply thrown away, `scale` is used for scaling
1575 the result. For technical parts where we want lines that are parallel stay
1576 parallel in the view.
1577 * Perspective: is the way human eye sees. `FOV` can be used to set field of
1578 view. (`ps` parameter for additonal scaling to fit width/height is also
1579 available, but the default is fine for casual users).
1580 \enditems
1581 \_cod
1582
1583 \.sdef{3dprojection:ortho}{/Subtype /O /OS \_kv{scale}}
1584 \.sdef{3dprojection:perspective}{/Subtype /P /FOV \_kv{FOV} /PS /\_kv{ps}}
1585
1586
1587 \_doc
1588 We offer the possibility of setting`/C2W` matrix and `/CO` (distance from
1589 camera to center of orbit) directly using `method=manual`.
1590 \_cod
1591
1592 \.sdef{3dview:manual:defaults}{
1593 matrix=1 0 0 0 1 0 0 0 1 0 0 0 ,
1594 centeroforbit=0,
1595 }
1596 \.sdef{3dview:manual}{\_edef\.viewparams{
1597 /MS /M
1598 /C2W [\_kv{matrix}]
1599 /CO \_kv{centeroforbit}
1600 }}
1601
1602 \_doc
1603 Another simple way of specifying camera position/orientation is to use a
1604 named setting of U3D file using a U3D path with `method=u3d`.
1605 \_cod
1606
1607 \.sdef{3dview:u3d:defaults}{
1608 u3dpath=,
1609 }
1610 \.sdef{3dview:u3d}{%
1611 \_pdfunidef\.tmp{\_kv{u3dpath}}%
1612 \_edef\.viewparams{
1613 /MS /U3D
1614 /U3DPath \.tmp \_space
1615 }}
1616
1617 \_doc
1618 The most advanced method of setting `/C2W` matrix and `/CO` is
1619 `method=media9`. It is thoroughly explained above, the few differences are
1620 because the input values are not two points. Also for conciseness \"x", \"y"
1621 and \"z" are used instead of right, up and forward. The calculations are
1622 done in Lua, for simplicity.
1623
1624 We expect the user to supply the numbers in the form \"`1 2 3`", but in Lua we
1625 need them comma separated (\"1, 2, 3"). \`\.luatriplet` does this. Just in
1626 case the code is somehow adapted without ensuring that $x$ and $z$ are
1627 orthonormal, we normalize also $y$ after the second cross product.
1628 \_cod
1629
1630 \_def\.luatriplet#1 #2 #3 {#1, #2, #3}
1631
1632 \.sdef{3dview:media9:defaults}{%
1633 roo=0,
1634 coo=0 0 0,
1635 c2c=0 -1 0,
1636 }
1637 \.sdef{3dview:media9}{\_edef\.viewparams{
1638 /MS /M
1639 /C2W [\_directlua{
1640 local function normalize(x, y, z)
1641 local len = math.sqrt(x*x + y*y + z*z)
1642 if len \csstring\~= 0 then return x/len, y/len, z/len else return 0, 0, 0 end
1643 end
1644 local function cross(ux, uy, uz, vx, vy, vz)
1645 return uy*vz - uz*vy, uz*vx - ux*vz, ux*vy - uy*vx
1646 end
1647 local function printmat(...)
1648 local arr = table.pack(...)
1649 for k, v in ipairs(arr) do
1650 arr[k] = string.format("\_pcent.6f", v)
1651 end
1652 tex.print(table.concat(arr, " "))
1653 end
1654
1655 local roo = \_kv{roo}
1656 local coo_x, coo_y, coo_z = \_ea\.luatriplet\_expanded{\_kv{coo}}
1657 local c2c_x, c2c_y, c2c_z = normalize(\_ea\.luatriplet\_expanded{\_kv{c2c}})
1658
1659 local eye_x, eye_y, eye_z = coo_x + c2c_x*roo, coo_y + c2c_y*roo, coo_z + c2c_z*roo
1660
1661 local z_x, z_y, z_z = -c2c_x, -c2c_y, -c2c_z
1662
1663 local up_x, up_y, up_z = 0, 0, 1
1664 if math.abs(z_x) + math.abs(z_y) < 0.0000001 then % z_x == 0 and z_y == 0
1665 if z_z < 0.0000001 then % z_z <= 0
1666 up_x, up_y, up_z = 0, 1, 0
1667 else
1668 up_x, up_y, up_z = 0, -1, 0
1669 end
1670 end
1671
1672 local x_x, x_y, x_z = normalize(cross(up_x, up_y, up_z, z_x, z_y, z_z))
1673 local y_x, y_y, y_z = normalize(cross(z_x, z_y, z_z, x_x, x_y, x_z))
1674
1675 local eye_x, eye_y, eye_z = coo_x - z_x*roo, coo_y - z_y*roo, coo_z - z_z*roo
1676
1677 printmat(x_x, x_y, x_z, y_x, y_y, y_z, z_x, z_y, z_z, eye_x, eye_y, eye_z)
1678 }]
1679 /CO \_kv{roo}
1680 }}
1681
1682 \_doc
1683 Last, but not least, is an action for setting the 3D view of a 3D/RM annotation
1684 using an action. \`\.goto3dviewaction``[goto3dview:<name>:<view>]`. <name> is
1685 name of the annotation which will be influenced. <view> is passed directly to
1686 PDF. Therefore it can be either an index to the view array (starting at 0) or name
1687 of view in parentheses~-- \"`(<view name>)`".
1688 \_cod
1689
1690 \.sdef{goto3dviewaction}[#1:#2:#3]{/S /GoTo3DView
1691 /TA \.cs{rm:#2} 0 R
1692 /V #3
1693 }
1694
1695 \_doc
1696 \label[mime]
1697 \sec MIME type database
1698
1699 This is the uninteresting MIME type database teased in section~\ref[files].
1700 Ideally this would only be a subset of what IANA defines at
1701 \url{https://www.iana.org/assignments/media-types/media-types.xhtml}.
1702 But there are additions like `model/u3d` and `model/prc`, which don't
1703 seem to be official, yet. Other \"unofficial" MIME types are taken from
1704 Mozilla's \"common" lists:
1705 \begitems
1706 * \url{https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types}.
1707 * \url{https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types/Common_types}
1708 \enditems
1709
1710
1711 \`\.mimetype``{<extension>}{<MIME type>}` is a shortcut of mapping
1712 <extension> to <MIME type>.
1713 \_cod
1714
1715 \_def\.mimetype#1#2{\_sdef{_pdfextra_mimetype:#1}{#2}}
1716
1717 \.mimetype{js}{application/javascript}
1718 \.mimetype{pdf}{application/pdf}
1719
1720 \.mimetype{prc}{model/prc}
1721 \.mimetype{u3d}{model/u3d}
1722
1723 \.mimetype{wav}{audio/x-wav}
1724 \.mimetype{mp3}{audio/mpeg}
1725 \.mimetype{opus}{audio/opus}
1726
1727 \.mimetype{avi}{video/x-msvideo}
1728 \.mimetype{mp4}{video/mp4}
1729 \.mimetype{webm}{video/webm}
1730
1731 \.mimetype{tex}{application/x-tex}
1732
1733
1734 % more or less legacy formats
1735 \.mimetype{au}{audio/basic}
1736 \.mimetype{aiff}{audio/x-aiff}
1737 \.mimetype{mov}{video/quicktime}
1738 \.mimetype{mpg}{video/mpeg}
1739 \.mimetype{wmv}{video/x-ms-wmv}
1740
1741 \_endnamespace
1742 \_endcode