Debian Codegraph @

texmf-dist/source/latex/newpax/newpax.dtx

raw
1 % \iffalse meta-comment
2 %
3 % File: newpax.dtx
4 % Copyright 2006-2008, 2011, 2012 Heiko Oberdiek (original pax.sty)
5 % Copyright (C) 2021--2026 Ulrike Fischer
6 %
7 % It may be distributed and/or modified under the conditions of the
8 % LaTeX Project Public License (LPPL), either version 1.3c of this
9 % license or (at your option) any later version. The latest version
10 % of this license is in the file
11 %
12 % https://www.latex-project.org/lppl.txt
13 %
14 % This file is part of the "newpax bundle" (The Work in LPPL)
15 % and all files in that bundle must be distributed together.
16 %
17 % -----------------------------------------------------------------------
18 %
19 % The development version of the bundle can be found at
20 %
21 % https://github.com/u-fischer/newpax
22 %
23 % for those people who are interested.
24 %
25 % \fi
26 %
27 % \begin{macrocode}
28 %<*package>
29 %<@@=newpax>
30 \NeedsTeXFormat{LaTeX2e}[2022-11-01]
31 \ProvidesPackage{newpax}%
32 [2026-05-09 v0.58 Annotation support for PDF graphics based on pax.sty adapted by (UF)]%
33 % \end{macrocode}
34 % Test if the pdfmanagement is loaded:
35 % \begin{macrocode}
36 \ExplSyntaxOn
37 \IfPDFManagementActiveTF
38 {}
39 { %error for now, perhaps warning later.
40 \PackageError{newpax}
41 {
42 PDF~resource~management~code~not~found!\MessageBreak
43 newpax~will~no~work.
44 }
45 {
46 Load~it~with \MessageBreak
47 \string\RequirePackage{pdfmanagement}\MessageBreak
48 before~loading~the~class
49 }
50 }
51
52 \ExplSyntaxOff
53 \RequirePackage{graphicx}
54 \RequirePackage{etoolbox}
55
56 \ExplSyntaxOn
57 % \end{macrocode}
58 % \subsection{Variables}
59 % Variables inherited from the pax code use \cs{NEWPAX}
60 % as prefix.
61 % \begin{variable}{\l_@@_tmpa_box,\g_@@_tmpa_tl}
62 % \begin{macrocode}
63 \box_new:N \l_@@_tmpa_box
64 \tl_new:N\g_@@_tmpa_tl
65 % \end{macrocode}
66 % \end{variable}
67
68 % \begin{variable}{ \l_@@_use_attributes_bool}
69 % Used by the \texttt{usefileattributes} key.
70 % \begin{macrocode}
71 \bool_new:N \l_@@_use_attributes_bool
72 % \end{macrocode}
73 % \end{variable}
74 %
75 % \begin{variable}{ \l_@@_addannots_bool}
76 % Used by the \texttt{addannots} key.
77 % \begin{macrocode}
78 \bool_new:N \l_@@_addannots_bool
79 % \end{macrocode}
80 % \end{variable}
81 %
82 % \begin{variable}{ \l_@@_dests_all_bool}
83 % Used by the \texttt{dests} key.
84 % if true newpax will insert more destinations.
85 % \begin{macrocode}
86 \bool_new:N \l_@@_dests_all_bool
87 % \end{macrocode}
88 % \end{variable}
89
90 % \begin{variable}{ \l_@@_destsuffic_tl}
91 % Used by the \texttt{addannots} key.
92 % \begin{macrocode}
93 \tl_new:N \l_@@_destsuffix_tl
94 % \end{macrocode}
95 % \end{variable}
96 %
97 % \begin{variable}{\NEWPAX@fileextension}
98 % the extension of the generated file, the default it newpax.
99 % \begin{macrocode}
100 \tl_new:N \NEWPAX@fileextension
101 % \end{macrocode}
102 % \end{variable}
103 %
104 % \begin{variable}{\NEWPAX@Gin@box@opts}
105 % this hold the options of a annotation
106 % \begin{macrocode}
107 \tl_new:N \NEWPAX@Gin@box@opts
108 % \end{macrocode}
109 % \end{variable}
110 %
111 % \begin{macro}{\ifNEWPAX@Gin@clip}
112 % \begin{macrocode}
113 \newif\ifNEWPAX@Gin@clip
114 % \end{macrocode}
115 % \end{macro}
116 %
117 % \subsection{Variants}
118 %
119 % \begin{macro}{\newpax@str@if@eq@@@@nnT}
120 % a latex2e variant of \cs{str_if_eq:nnT} to replace
121 % \cs{pdf@strcmp}
122 % \begin{macrocode}
123 \cs_set_eq:NN \newpax@str@if@eq@@@@nnT\str_if_eq:nnT
124 % \end{macrocode}
125 % \end{macro}
126 %
127 % \begin{macrocode}
128 \cs_generate_variant:Nn \pdfannot_dict_put:nnn {nnx,xnx,xnn}
129 \cs_generate_variant:Nn \pdfannot_link_goto_begin:nw {xw}
130 \cs_generate_variant:Nn \pdf_destination:nn {xx}
131 % \end{macrocode}
132 %
133 % \subsection{User setup command}
134 % \begin{macrocode}
135 \NewDocumentCommand\newpaxsetup { m }
136 {
137 \keys_set:nn {newpax}{ #1}
138 }
139
140 \keys_define:nn {newpax}
141 {
142 usefileattributes .bool_set:N = \l_@@_use_attributes_bool,
143 destsuffix .code:n = {\tl_set:Nn \l_@@_destsuffix_tl{@#1}},
144 addannots .bool_set:N = \l_@@_addannots_bool,
145 addannots .default:n = true,
146 addannots .initial:n = true,
147 paxextension .choices:nn =
148 {pax,newpax}
149 {\tl_set:Nn \NEWPAX@fileextension {#1}},
150 paxextension .initial:n = newpax,
151 dests .choice:,
152 dests/all .code:n = {\bool_set_true:N \l_@@_dests_all_bool},
153 dests/used .code:n = {\bool_set_false:N \l_@@_dests_all_bool},
154 dests .initial:n = used
155 }
156
157 % \end{macrocode}
158 % \subsection{Helper commands to create the annotations}
159 %
160 % \begin{macro}{\@NEWPAX@setattributes@n}
161 % This adds or removes attributes from the annot dictionaries.
162 % The argument is a link type like URI or Goto.
163 % \begin{macrocode}
164 \cs_new_protected:Npn \@NEWPAX@setattributes@n #1 %link type
165 {
166 \bool_if:NT \l_@@_use_attributes_bool
167 {
168 \tl_if_empty:NTF \NEWPAX@key@C
169 {
170 \pdfannot_dict_remove:nn {link/#1} { C }
171 }
172 {
173 \pdfannot_dict_put:nnx
174 {link/#1}
175 { C }
176 { \NEWPAX@key@C }
177 }
178 \tl_if_empty:NTF \NEWPAX@key@Border
179 {
180 \pdfannot_dict_remove:nn {link/#1} { Border }
181 }
182 {
183 \pdfannot_dict_put:nnx
184 {link/#1}
185 { Border }
186 { \NEWPAX@key@Border }
187 }
188 \tl_if_empty:NTF \NEWPAX@key@BS
189 {
190 \pdfannot_dict_remove:nn {link/#1} { BS }
191 }
192 {
193 \pdfannot_dict_put:nnx
194 {link/#1}
195 { BS }
196 { \NEWPAX@key@BS }
197 }
198 \tl_if_empty:NTF \NEWPAX@key@H
199 {
200 \pdfannot_dict_remove:nn {link/#1} { H }
201 }
202 {
203 \pdfannot_dict_put:nnx
204 {link/#1}
205 { H }
206 { \NEWPAX@key@H }
207 }
208 }
209 }
210
211 % \end{macrocode}
212 % \end{macro}
213 %
214 %
215 % \begin{macro}{\@NEWPAX@linkgoto@xnn}
216 % this creates a goto link to the destination given in the first argument,
217 % and the size given in the second and third argument.
218 % \begin{macrocode}
219 \cs_new_protected:Npn \@NEWPAX@linkgoto@xnn #1 #2 #3 %#1 dest #2 width #3 height
220 {
221 \group_begin:
222 \@NEWPAX@setattributes@n {GoTo}
223 \leavevmode
224 \pdfannot_link_goto_begin:xw
225 { #1 }
226 \@NEWPAX@ensurelinkbox@n{\hbox_to_wd:nn {#2}{ { \rule{0pt}{#3} }\hfill}}
227 \pdfannot_link_goto_end:
228 \group_end:
229 }
230 % \end{macrocode}
231 % \end{macro}
232 %
233 % \begin{macro}{\@NEWPAX@link@setaction@nn}
234 % This puts the action into the dict
235 % if the annot is a link. \#1 is a type like
236 % URI or Goto, \#2 the actual action.
237 % \begin{macrocode}
238 \cs_new_protected:Npn \@NEWPAX@link@setaction@nn #1 #2
239 {
240 \pdfannot_dict_put:nnn{link/#1}{Subtype}{/Link}
241 \pdfannot_dict_put:nnx{link/#1}{A}
242 {<</Type/Action/S/#1 #2>>}
243 }
244 % \end{macrocode}
245 % \end{macro}
246 %
247 %
248 % \begin{macro}{\@NEWPAX@annotboxlink@nnn}
249 % This creates an annotation link box.
250 % \begin{macrocode}
251 \cs_new_protected:Npn \@NEWPAX@annotboxlink@nnn #1 #2 #3 %#1 type, #2 width #3 height
252 {
253 \group_begin:
254 \@NEWPAX@setattributes@n {#1}
255 \leavevmode
256 \pdfannot_box:nnnx {#2}{#3}{0pt}
257 {\pdfannot_dict_use:n{link/#1}}
258 \group_end:
259 }
260 % \end{macrocode}
261 % \end{macro}
262 %
263 % \begin{macro}{\@NEWPAX@destination@xx}
264 % This creates a destination. The first argument is the
265 % name, the second the placement type (e.g. fit or xyz).
266 % \begin{macrocode}
267 \cs_new_protected:Npn \@NEWPAX@destination@xx #1 #2
268 {
269 \pdf_destination:xx {#1}{#2}
270 }
271 % \end{macrocode}
272 % \end{macro}
273 %
274 %
275 % \begin{macro}{\@NEWPAX@ensurelinkbox@n}
276 % XeTeX needs in some places a specific box to ensure the size.
277 % TODO: check the mailing list for the alternative.
278 % 2026-05-09: changed definition to adapt to hyperref 7.0q which moved the
279 % definition to begin document.
280 % \begin{macrocode}
281 \cs_new_eq:NN \@NEWPAX@ensurelinkbox@n \use:n
282 \AddToHook{package/hyperref/after}
283 {
284 \sys_if_engine_xetex:T
285 {
286 \cs_set:Npn \@NEWPAX@ensurelinkbox@n #1{ \XeTeXLinkBox {#1} }
287 }
288 }
289 % \end{macrocode}
290 % \end{macro}
291 %
292 % \subsection{Handling graphics commands}
293 % We must redefine \cs{includegraphics} to inject the code which
294 % reinserts the annotation. This can't be done with hooks as the
295 % standard code splits everything too much.
296 %
297 % \begin{macro}{\NEWPAX@ORG@includegraphics}
298 % At first we redefine \cs{includegraphics} to use our new command.
299 % \begin{macrocode}
300 \NewCommandCopy\NEWPAX@ORG@includegraphics\includegraphics
301 \RenewDocumentCommand\includegraphics{sO{}m}
302 {
303 \IfBooleanTF {#1}
304 {
305 \NEWPAX@includegraphics[clip,#2]{#3}
306 }
307 {
308 \NEWPAX@includegraphics[#2]{#3}
309 }
310 }
311 % \end{macrocode}
312 % \end{macro}
313 %
314 % \begin{macro}{\NEWPAX@includegraphics}
315 % \begin{macrocode}
316 \def\NEWPAX@includegraphics[#1]#2{%
317 \group_begin:
318 \hbox_set:Nn \l_@@_tmpa_box {\NEWPAX@ORG@includegraphics[{#1}]{#2}}%
319 \tl_set:Nx \NEWPAX@inc@width
320 { \dim_eval:n { \box_wd:N \l_@@_tmpa_box } }%
321 \tl_set:Nx \NEWPAX@inc@height
322 { \dim_eval:n { \box_dp:N \l_@@_tmpa_box + \box_ht:N \l_@@_tmpa_box } }%
323 \mode_leave_vertical:
324 \hbox_to_wd:nn
325 { \box_wd:N \l_@@_tmpa_box }
326 {
327 \hbox_overlap_right:n { \box_use:N \l_@@_tmpa_box }
328 \bool_if:nT
329 { \l_@@_addannots_bool
330 &&
331 (
332 \int_compare_p:nNn
333 {
334 \cs_if_exist:NTF\pdfpages@includegraphics@status
335 {
336 \pdfpages@includegraphics@status
337 }{0}
338 }
339 <
340 {2}
341 )
342 }
343 {
344 \box_move_down:nn
345 {\box_dp:N \l_@@_tmpa_box }
346 {
347 \hbox:n
348 {
349 \NEWPAX@AddAnnots{#1}{#2}%
350 }
351 }
352 }
353 \hfill
354 }
355 \group_end:
356 }
357 % \end{macrocode}
358 % \end{macro}
359 %
360 % \subsection{Handling the newpax file}
361 % The following keys were previously defined
362 % with kvoptions, the names of the internal commands are kept for now.
363 % The keys are used in the optional argument of \cs{includegraphics}
364 % and extend their functions.
365 % \begin{macrocode}
366 \keys_define:nn {newpax/Gin}
367 {
368 ,page .tl_set:N = \NEWPAX@Gin@page
369 ,page .initial:n = 1
370 ,angle .tl_set:N = \NEWPAX@Gin@angle
371 ,angle .initial:n = 0
372 ,clip .legacy_if_set:n = NEWPAX@Gin@clip
373 ,viewport .code:n =
374 {
375 \tl_put_right:Nn\NEWPAX@Gin@box@opts
376 {\NEWPAX@viewport#1\\}
377 }
378 ,trim .code:n =
379 {
380 \tl_put_right:Nn\NEWPAX@Gin@box@opts
381 {\NEWPAX@trim#1\\}
382 }
383 ,unknown .code:n = {}
384 }
385 \ExplSyntaxOff
386
387 % \end{macrocode}
388 %
389 % Calculate coordinates
390 % \begin{macro}{\NEWPAX@viewport,\NEWPAX@trim,\NEWPAX@defaultbp,\NEWPAX@def@bp}
391 % \begin{macrocode}
392 \def\NEWPAX@viewport#1 #2 #3 #4\\{%
393 \NEWPAX@defaultbp\NEWPAX@vllx{#1}%
394 \NEWPAX@defaultbp\NEWPAX@vlly{#2}%
395 \NEWPAX@defaultbp\NEWPAX@vurx{#3}%
396 \NEWPAX@defaultbp\NEWPAX@vury{#4}%
397 \edef\NEWPAX@page@llx{\dimexpr\NEWPAX@page@llx+\NEWPAX@vllx\relax}%
398 \edef\NEWPAX@page@lly{\dimexpr\NEWPAX@page@lly+\NEWPAX@vlly\relax}%
399 \edef\NEWPAX@page@urx{\dimexpr\NEWPAX@page@llx+\NEWPAX@vlly\relax}%
400 \edef\NEWPAX@page@ury{\dimexpr\NEWPAX@page@lly+\NEWPAX@vury\relax}%
401 }
402 \def\NEWPAX@trim#1 #2 #3 #4\\{%
403 \NEWPAX@defaultbp\NEWPAX@tllx{#1}%
404 \NEWPAX@defaultbp\NEWPAX@tlly{#2}%
405 \NEWPAX@defaultbp\NEWPAX@turx{#3}%
406 \NEWPAX@defaultbp\NEWPAX@tury{#4}%
407 \edef\NEWPAX@page@llx{\dimexpr\NEWPAX@page@llx+\NEWPAX@tllx\relax}%
408 \edef\NEWPAX@page@lly{\dimexpr\NEWPAX@page@lly+\NEWPAX@tlly\relax}%
409 \edef\NEWPAX@page@urx{\dimexpr\NEWPAX@page@urx-\NEWPAX@turx\relax}%
410 \edef\NEWPAX@page@ury{\dimexpr\NEWPAX@page@ury-\NEWPAX@tury\relax}%
411 }
412 \def\NEWPAX@defaultbp#1#2{%
413 \afterassignment\NEWPAX@def@bp\dimen@#2bp\relax{#1}{#2}%
414 }
415 \def\NEWPAX@def@bp#1\relax#2#3{%
416 \if!#1!%
417 \edef#2{#3bp}%
418 \else
419 \edef#2{#3}%
420 \fi
421 }
422 % \end{macrocode}
423 % \end{macro}
424 %
425 % \begin{macro}{\NEWPAX@AddAnnots}
426 % This command is the main command that reads and parses the newpax file.
427 % \begin{macrocode}
428 \def\NEWPAX@AddAnnots#1#2{%
429 \SetKeys[newpax/Gin]{#1}%
430 \Grot@setangle{\NEWPAX@Gin@angle}%
431 % a little careful, is type of angle int or real?
432 \loop
433 \ifdim\NEWPAX@Gin@angle\p@<360\p@
434 \else
435 \edef\NEWPAX@Gin@angle{\the\numexpr-360+\number\NEWPAX@Gin@angle}%
436 \repeat
437 \loop
438 \ifdim\NEWPAX@Gin@angle\p@<\z@
439 \edef\NEWPAX@Gin@angle{\strip@pt\dimexpr\NEWPAX@Gin@angle\p@+360\p@}%
440 \repeat
441 \ifcase0\ifnum\NEWPAX@Gin@angle=0 1\fi
442 \ifnum\NEWPAX@Gin@angle=90 1\fi
443 \ifnum\NEWPAX@Gin@angle=180 1\fi
444 \ifnum\NEWPAX@Gin@angle=270 1\fi
445 \PackageWarning{newpax}{Unsupported value for option angle}%
446 \fi
447 \filename@parse{#2}%
448 \def\NEWPAX@file{%\filename@area
449 \filename@base.\NEWPAX@fileextension }%
450 \let\[\NEWPAX@parser
451 \def\<{<}%
452 \def\>{>}%
453 \endlinechar=-1 %
454 \begingroup
455 \catcode`\#=12 \catcode`\%=12
456 \InputIfFileExists\NEWPAX@file{}{\typeout{* Missing: \NEWPAX@file}}\endgroup
457 }
458 % \end{macrocode}
459 % \end{macro}
460 % The parsing commands
461 % With \cs{NEWPAX@parser} normally \cs{NEWPAX@cmd@XXX} is called, where
462 % XXX is file, pagenum, annot, page or dest.
463 % \begin{macrocode}
464 \def\NEWPAX@parser#1{\NEWPAX@call{cmd}{#1}{}}%
465 % \end{macrocode}
466 % \begin{macrocode}
467 \def\NEWPAX@call#1#2#3{%
468 \@ifundefined{NEWPAX@#1@#2}\NEWPAX@skip{#3\csname NEWPAX@#1@#2\endcsname}%
469 }
470 \def\NEWPAX@skip#1\\{}
471 \def\NEWPAX@stop#1\\{}
472
473 % \end{macrocode}
474 % The command to handle the page entry.
475 % \begin{macrocode}
476 \def\NEWPAX@cmd@page#1#2{%
477 \NEWPAX@filter@page{#1}{%
478 \NEWPAX@getrect{page}#2\@nil
479 \NEWPAX@Gin@box@opts
480 \ifcase0\ifnum\NEWPAX@Gin@angle=90 1\fi
481 \ifnum\NEWPAX@Gin@angle=270 1\fi
482 \else
483 \let\NEWPAX@temp\NEWPAX@inc@width
484 \let\NEWPAX@inc@width\NEWPAX@inc@height
485 \let\NEWPAX@inc@height\NEWPAX@temp
486 \fi
487 \Gscale@div\NEWPAX@scale@x\NEWPAX@inc@width{%
488 \dimexpr\NEWPAX@page@urx-\NEWPAX@page@llx\relax
489 }%
490 \Gscale@div\NEWPAX@scale@y\NEWPAX@inc@height{%
491 \dimexpr\NEWPAX@page@ury-\NEWPAX@page@lly\relax
492 }%
493 \NEWPAX@skip
494 }%
495 }
496 \def\NEWPAX@filter@page#1{%
497 \ifnum\NEWPAX@Gin@page=#1 %
498 \expandafter\@firstofone
499 \else
500 \ifnum\NEWPAX@Gin@page<#1 %
501 \csname fi\endcsname
502 \csname fi\endcsname
503 \expandafter\NEWPAX@stop\@gobblefour
504 \fi
505 \expandafter\NEWPAX@skip
506 \fi
507 }
508 \def\NEWPAX@getrect#1#2 #3 #4 #5\@nil{%
509 \@namedef{NEWPAX@#1@llx}{#2bp}%
510 \@namedef{NEWPAX@#1@lly}{#3bp}%
511 \@namedef{NEWPAX@#1@urx}{#4bp}%
512 \@namedef{NEWPAX@#1@ury}{#5bp}%
513 }
514
515 \def\NEWPAX@cmd@annot#1#2{%
516 \NEWPAX@filter@page{#1}{%
517 \NEWPAX@call{annot}{#2}{}%
518 }%
519 }
520 \def\NEWPAX@annot@Link#1#2#3{%
521 \def\NEWPAX@link@type{#2}%
522 \NEWPAX@call{link}{#2}{%
523 \begingroup
524 \NEWPAX@getrect{annot}#1\@nil
525 \SetKeys[newpax/key]{#3}%
526 }%
527 \NEWPAX@skip
528 }
529
530 % \end{macrocode}
531
532 % Now the main command which inserts annotations.
533 % \begin{macro}{\NEWPAX@pdf@annot}
534 % \begin{macrocode}
535 \newif\ifNEWPAX@ok
536 \NEWPAX@oktrue
537
538 \newif\ifNEWPAX@GoTo
539 % \end{macrocode}
540 % We need to sanitize destnames in case they contain umlauts and so backslashes for the
541 % octal numbers.
542 % \begin{macrocode}
543 \def\NEWPAX@sanitize@destname{%
544 \edef\0{\string\0}%
545 \edef\1{\string\1}%
546 \edef\2{\string\2}%
547 \edef\3{\string\3}%
548 \edef\8{\string\8}%
549 \edef\9{\string\9}}
550
551 \def\NEWPAX@pdf@annot#1{%
552 \ifNEWPAX@Gin@clip
553 \ifdim\NEWPAX@annot@llx<\NEWPAX@page@llx
554 \let\NEWPAX@annot@llx\NEWPAX@page@llx
555 \fi
556 \ifdim\NEWPAX@annot@lly<\NEWPAX@page@lly
557 \let\NEWPAX@annot@lly\NEWPAX@page@lly
558 \fi
559 \ifdim\NEWPAX@annot@urx>\NEWPAX@page@urx
560 \let\NEWPAX@annot@urx\NEWPAX@page@urx
561 \fi
562 \ifdim\NEWPAX@annot@ury>\NEWPAX@page@ury
563 \let\NEWPAX@annot@ury\NEWPAX@page@ury
564 \fi
565 \NEWPAX@okfalse
566 \ifdim\NEWPAX@annot@llx<\NEWPAX@annot@urx\relax
567 \ifdim\NEWPAX@annot@lly<\NEWPAX@annot@ury\relax
568 \NEWPAX@oktrue
569 \fi
570 \fi
571 \else
572 \NEWPAX@oktrue
573 \fi
574 \ifNEWPAX@ok
575 \ifcase 0\ifnum\NEWPAX@Gin@angle=90 1\fi
576 \ifnum\NEWPAX@Gin@angle=180 2\fi
577 \ifnum\NEWPAX@Gin@angle=270 3\fi\space
578 % angle = 0
579 \def\NEWPAX@raise{%
580 \NEWPAX@scale@y\dimexpr\NEWPAX@annot@lly-\NEWPAX@page@lly\relax
581 }%
582 \def\NEWPAX@right{%
583 \NEWPAX@scale@x\dimexpr\NEWPAX@annot@llx-\NEWPAX@page@llx\relax
584 }%
585 \or % angle = 90
586 \def\NEWPAX@raise{%
587 \NEWPAX@scale@x\dimexpr\NEWPAX@annot@llx-\NEWPAX@page@llx\relax
588 }%
589 \def\NEWPAX@right{%
590 \NEWPAX@scale@y\dimexpr\NEWPAX@page@ury-\NEWPAX@annot@ury\relax
591 }%
592 \or % angle = 180
593 \def\NEWPAX@raise{%
594 \NEWPAX@scale@y\dimexpr\NEWPAX@page@ury-\NEWPAX@annot@ury\relax
595 }%
596 \def\NEWPAX@right{%
597 \NEWPAX@scale@x\dimexpr\NEWPAX@page@urx-\NEWPAX@annot@urx\relax
598 }%
599 \or % angle = 270
600 \def\NEWPAX@raise{%
601 \NEWPAX@scale@x\dimexpr\NEWPAX@page@urx-\NEWPAX@annot@urx\relax
602 }%
603 \def\NEWPAX@right{%
604 \NEWPAX@scale@y\dimexpr\NEWPAX@annot@lly-\NEWPAX@page@lly\relax
605 }%
606 \fi
607 \@namedef{%
608 NEWPAX@%
609 \ifcase0\ifnum\NEWPAX@Gin@angle=90 1\fi
610 \ifnum\NEWPAX@Gin@angle=270 1\fi\space
611 width%
612 \else
613 height%
614 \fi
615 }{%
616 \NEWPAX@scale@x\dimexpr\NEWPAX@annot@urx-\NEWPAX@annot@llx\relax
617 }%
618 \@namedef{%
619 NEWPAX@%
620 \ifcase0\ifnum\NEWPAX@Gin@angle=90 1\fi
621 \ifnum\NEWPAX@Gin@angle=270 1\fi\space
622 height%
623 \else
624 width%
625 \fi
626 }{%
627 \NEWPAX@scale@y\dimexpr\NEWPAX@annot@ury-\NEWPAX@annot@lly\relax
628 }%
629 \raise\NEWPAX@raise\hb@xt@\z@{%
630 \kern\NEWPAX@right
631 \ifNEWPAX@GoTo
632 %additional box for lualatex ...
633 \hbox{%
634 \@NEWPAX@linkgoto@xnn
635 {\NEWPAX@file @\NEWPAX@DestName}%
636 {\NEWPAX@width}%
637 {\NEWPAX@height}%
638 }%
639 \else
640 \hbox{%
641 \expandafter\@NEWPAX@link@setaction@nn\expandafter{\NEWPAX@link@type}{#1}%
642 \expandafter
643 \@NEWPAX@annotboxlink@nnn
644 \expandafter {\NEWPAX@link@type}{\NEWPAX@width}{\NEWPAX@height}{}}%
645 \fi
646 \hss
647 }%
648 \fi
649 \endgroup
650 }
651 % \end{macrocode}
652 % \end{macro}
653 %
654 % What do they do??
655 % \begin{macrocode}
656 \def\NEWPAX@htype@GoToR{file}
657 \def\NEWPAX@htype@GoTo{link}
658 \def\NEWPAX@htype@Named{link}
659 \def\NEWPAX@htype@URI{url}
660 % \end{macrocode}
661 %
662 % \begin{macrocode}
663 \ExplSyntaxOn
664 \def\NEWPAX@link@URI{%
665 \NEWPAX@pdf@annot{%
666 /URI\tl_to_str:V\NEWPAX@key@URI
667 }%
668 }
669 \def\NEWPAX@link@Named{%
670 \NEWPAX@pdf@annot{%
671 /N \pdf_name_from_unicode_e:n{\NEWPAX@key@Name} %the value is from a pdf so we can assume it is correctly escaped??
672 }%
673 }
674 \ExplSyntaxOff
675 \def\NEWPAX@link@GoToR{%
676 \NEWPAX@pdf@annot{%
677 /F\NEWPAX@key@File
678 /D%
679 \ifx\NEWPAX@key@DestName\@empty
680 [\NEWPAX@key@DestPage\space\NEWPAX@key@DestView]%
681 \else
682 \NEWPAX@key@DestName
683 \fi
684 }%
685 }
686 % \end{macrocode}
687 %
688 % Goto links are the most challenging: here it is not enough to create an
689 % link annotation, one also have to ensure that the destination they point to exist
690 % and are correctly created. The destinations can be on previous (or later)
691 % pages that means that one has to use the aux to record what exists and what
692 % is needed.
693 %
694 % At first ensure that the commands are defined to avoid errors if newpax is removed
695 % \begin{macrocode}
696 \AddToHook{begindocument}
697 {
698 \immediate\write\@mainaux{\string\providecommand{\string\NEWPAX@DestReq}[2]{}}
699 \immediate\write\@mainaux{\string\providecommand{\string\NEWPAX@DestProv}[2]{}}
700 }
701 \AddToHook
702 {include/before}
703 {
704 \immediate\write\@partaux{\string\providecommand{\string\NEWPAX@DestReq}[2]{}}
705 \immediate\write\@partaux{\string\providecommand{\string\NEWPAX@DestProv}[2]{}}
706 }
707 % \end{macrocode}
708 %
709 % After the aux file has been read the commands should do nothing:
710 % \begin{macrocode}
711 \AtBeginDocument{%
712 \let\NEWPAX@DestReq\@gobbletwo
713 \let\NEWPAX@DestProv\@gobbletwo
714 }
715 % \end{macrocode}
716
717 % \begin{macro}{\NEWPAX@DestReq}
718 % This command records the destinations that are required and should be reinserted.
719 % \begin{macrocode}
720 \def\NEWPAX@DestReq#1#2{{%
721 \NEWPAX@sanitize@destname
722 \expandafter\gdef\csname NEWPAX@REQ@#1@#2\endcsname{}%
723 }}
724 % \end{macrocode}
725 % \end{macro}
726
727 % \begin{macro}{\NEWPAX@DestProv}
728 % This records the destinations that are provided.
729 % \begin{macrocode}
730 \def\NEWPAX@DestProv#1#2{{%
731 \NEWPAX@sanitize@destname
732 \expandafter\gdef\csname NEWPAX@PROV@#1@#2\endcsname{}%
733 }}
734 % \end{macrocode}
735 % \end{macro}
736
737 % \begin{macro}[EXP]{\NEWPAX@DestName}
738 % This command expands either to the number or the name if it exists.
739 % \begin{macrocode}
740 \ExplSyntaxOn
741 \cs_new:Npn \NEWPAX@DestName
742 {
743 \tl_if_empty:eTF
744 { \NEWPAX@key@DestName }
745 { \NEWPAX@key@DestLabel}
746 { \NEWPAX@key@DestName }
747 \l_@@_destsuffix_tl
748 }
749 % \cs_set:Npn \NEWPAX@DestName { \NEWPAX@key@DestLabel }
750 % \end{macrocode}
751 % \end{macro}
752 % \begin{macrocode}
753 \def\NEWPAX@link@GoTo{%
754 \ifnum0<0\NEWPAX@key@DestLabel\relax
755 \expandafter\@firstofone
756 \else
757 \endgroup
758 \expandafter\@gobble
759 \fi
760 {%
761 \if@filesw
762 \protected@write\@auxout{}{%
763 \string\NEWPAX@DestReq{\NEWPAX@file}{\NEWPAX@DestName}%
764 }%
765 \fi
766 % Generate link, if destination exists
767 \@ifundefined{NEWPAX@PROV@\NEWPAX@file @\NEWPAX@DestName}{%
768 \endgroup
769 }{%
770 \NEWPAX@GoTotrue
771 \NEWPAX@pdf@annot{}%
772 }%
773 }%
774 }
775 % \end{macrocode}
776 % This is the command which reads the destination info.
777 % \begin{macrocode}
778 \def\NEWPAX@cmd@dest#1#2#3#4{%
779 \def\NEWPAX@key@DestLabel{#2}%
780 \def\NEWPAX@key@DestName{}%
781 \keys_set_groups:nnn {newpax/key} {destinit} {#4}
782 \NEWPAX@filter@page{#1}{%
783 \if@filesw
784 \protected@write\@auxout{}{%
785 \string\NEWPAX@DestProv{\NEWPAX@file}{\NEWPAX@DestName}%
786 }%
787 \bool_if:NT\l_@@_dests_all_bool
788 {
789 \protected@write\@auxout{}
790 {
791 \string\NEWPAX@DestReq{\NEWPAX@file}{\NEWPAX@DestName}%
792 }
793 }
794 \fi
795 \@ifundefined{NEWPAX@REQ@\NEWPAX@file @\NEWPAX@DestName}{%
796 }{%
797 \begingroup
798 \let\NEWPAX@key@DestY\NEWPAX@page@ury
799 \let\NEWPAX@key@DestX\NEWPAX@page@llx
800 \keys_set_filter:nnn {newpax/key} {destinit} {#4}
801 \let\NEWPAX@dest@llx\NEWPAX@key@DestX
802 \let\NEWPAX@dest@urx\NEWPAX@key@DestX
803 \let\NEWPAX@dest@lly\NEWPAX@key@DestY
804 \let\NEWPAX@dest@ury\NEWPAX@key@DestY
805 \ifx\NEWPAX@key@DestRect\@empty
806 \else
807 \def\NEWPAX@temp{dest}%
808 \expandafter\NEWPAX@getrect\expandafter\NEWPAX@temp
809 \NEWPAX@key@DestRect\@nil
810 \fi
811 \ifNEWPAX@Gin@clip
812 \ifx\NEWPAX@dest@llx<\NEWPAX@page@llx
813 \let\NEWPAX@dest@llx\NEWPAX@page@llx
814 \fi
815 \ifx\NEWPAX@dest@lly<\NEWPAX@page@lly
816 \let\NEWPAX@dest@lly\NEWPAX@page@lly
817 \fi
818 \ifx\NEWPAX@dest@urx>\NEWPAX@page@urx
819 \let\NEWPAX@dest@urx\NEWPAX@page@urx
820 \fi
821 \ifx\NEWPAX@dest@ury>\NEWPAX@page@ury
822 \let\NEWPAX@dest@ury\NEWPAX@page@ury
823 \fi
824 % at least prevent destinations outside the window
825 \ifx\NEWPAX@dest@llx>\NEWPAX@page@urx
826 \NEWPAX@dest@llx\NEWPAX@page@urx
827 \fi
828 \ifx\NEWPAX@dest@lly>\NEWPAX@page@ury
829 \NEWPAX@dest@lly\NEWPAX@page@ury
830 \fi
831 \ifx\NEWPAX@dest@urx<\NEWPAX@page@llx
832 \NEWPAX@dest@urx\NEWPAX@page@llx
833 \fi
834 \ifx\NEWPAX@dest@ury<\NEWPAX@page@lly
835 \NEWPAX@dest@ury\NEWPAX@page@lly
836 \fi
837 \fi
838 % I don't know, what is the best idea for rotated stuff,
839 % perhaps using the corner llx/ury
840 \ifcase 0\ifnum\NEWPAX@Gin@angle=90 1\fi
841 \ifnum\NEWPAX@Gin@angle=180 2\fi
842 \ifnum\NEWPAX@Gin@angle=270 3\fi\space
843 % angle = 0
844 \def\NEWPAX@raise{%
845 \NEWPAX@scale@y\dimexpr\NEWPAX@dest@lly-\NEWPAX@page@lly\relax
846 }%
847 \def\NEWPAX@right{%
848 \NEWPAX@scale@x\dimexpr\NEWPAX@dest@llx-\NEWPAX@page@llx\relax
849 }%
850 \or % angle = 90
851 \def\NEWPAX@raise{%
852 \NEWPAX@scale@x\dimexpr\NEWPAX@dest@llx-\NEWPAX@page@llx\relax
853 }%
854 \def\NEWPAX@right{%
855 \NEWPAX@scale@y\dimexpr\NEWPAX@page@ury-\NEWPAX@dest@ury\relax
856 }%
857 \or % angle = 180
858 \def\NEWPAX@raise{%
859 \NEWPAX@scale@y\dimexpr\NEWPAX@page@ury-\NEWPAX@dest@ury\relax
860 }%
861 \def\NEWPAX@right{%
862 \NEWPAX@scale@x\dimexpr\NEWPAX@page@urx-\NEWPAX@dest@urx\relax
863 }%
864 \or % angle = 270
865 \def\NEWPAX@raise{%
866 \NEWPAX@scale@x\dimexpr\NEWPAX@page@urx-\NEWPAX@dest@urx\relax
867 }%
868 \def\NEWPAX@right{%
869 \NEWPAX@scale@y\dimexpr\NEWPAX@dest@lly-\NEWPAX@page@lly\relax
870 }%
871 \fi
872 \edef\NEWPAX@name
873 {\NEWPAX@file @\NEWPAX@DestName}%
874 \let\NEWPAX@type\@empty
875 \newpax@str@if@eq@@@@nnT{#3}{FITR}{\def\NEWPAX@type{xyz}}%too lazy for now for better fitr
876 \newpax@str@if@eq@@@@nnT{#3}{XYZ}
877 {%
878 \def\NEWPAX@type{xyz}%
879 \ifx\NEWPAX@key@DestZoom\@empty
880 \else
881 \edef\NEWPAX@type{\fpeval{\NEWPAX@key@DestZoom *100}}%
882 \fi
883 }
884 \newpax@str@if@eq@@@@nnT{#3}{FIT}{\def\NEWPAX@type{fit}}
885 \newpax@str@if@eq@@@@nnT{#3}{FITB}{\def\NEWPAX@type{fitb}}
886 \newpax@str@if@eq@@@@nnT{#3}{FITH}{\def\NEWPAX@type{fith}}
887 \newpax@str@if@eq@@@@nnT{#3}{FITBH}{\def\NEWPAX@type{fitbh}}
888 \newpax@str@if@eq@@@@nnT{#3}{FITV}{\def\NEWPAX@type{fitv}}
889 \newpax@str@if@eq@@@@nnT{#3}{FITBV}{\def\NEWPAX@type{fitbv}}
890 \ifx\NEWPAX@type\@empty
891 \def\NEWPAX@type{xyz}%
892 \fi
893 \raise\NEWPAX@raise\hb@xt@\z@{%
894 \kern\NEWPAX@right
895 \hbox{\@NEWPAX@destination@xx {\NEWPAX@name}{\NEWPAX@type}}%
896 \hss
897 }%
898 \endgroup
899 % \end{macrocode}
900 % we undefine the REQ-command to avoid that the dest is set again.
901 % \begin{macrocode}
902 \cs_undefine:c{NEWPAX@REQ@\NEWPAX@file @\NEWPAX@DestName}
903 }%
904 \NEWPAX@skip
905 }%
906 }
907 % \end{macrocode}
908 % These are keys used in the newpax file. The command names are inherited from kvoptions.
909 % We must predefined \cs{NEWPAX@key@DestName} as that is not done by the key setup.
910 % \begin{macrocode}
911 \def\NEWPAX@key@DestName{}
912 \keys_define:nn {newpax/key}
913 {
914 ,URI .tl_set:N = \NEWPAX@key@URI
915 ,Name .tl_set:N = \NEWPAX@key@Name
916 ,DestName .code:n =
917 {
918 \group_begin:
919 \NEWPAX@sanitize@destname
920 \tl_gset:Ne \g_@@_tmpa_tl{#1}
921 \group_end:
922 \cs_set_eq:NN\NEWPAX@key@DestName\g_@@_tmpa_tl
923 }
924 ,DestName .groups:n = {destinit}
925 ,DestPage .tl_set:N = \NEWPAX@key@DestPage
926 ,DestView .tl_set:N = \NEWPAX@key@DestView
927 ,File .tl_set:N = \NEWPAX@key@File
928 ,C .tl_set:N = \NEWPAX@key@C
929 ,Border .tl_set:N = \NEWPAX@key@Border
930 ,BS .tl_set:N = \NEWPAX@key@BS
931 ,H .tl_set:N = \NEWPAX@key@H
932 ,DestLabel .tl_set:N = \NEWPAX@key@DestLabel
933 ,DestLabel .groups:n = {destinit}
934 ,DestRect .tl_set:N = \NEWPAX@key@DestRect
935 ,DestZoom .tl_set:N = \NEWPAX@key@DestZoom
936 ,DestX .code:n = { \NEWPAX@defaultbp\NEWPAX@key@DestX{#1} }
937 ,DestY .code:n = { \NEWPAX@defaultbp\NEWPAX@key@DestY{#1} }
938 }
939 \ExplSyntaxOff
940
941 %</package>
942 % \end{macrocode}
943 % \begin{macrocode}
944 %<*lua>
945 local ProvidesLuaModule = {
946 name = "newpax",
947 version = "0.58", --TAGVERSION
948 date = "2026-05-09", --TAGDATE
949 description = "newpax lua code",
950 license = "The LATEX Project Public License 1.3c"
951 }
952
953 if luatexbase and luatexbase.provides_module then
954 luatexbase.provides_module (ProvidesLuaModule)
955 end
956
957
958
959 local OPEN = pdfe.open
960 local GETSIZE = pdfe.getsize
961 local GETINFO = pdfe.getinfo
962 local GETPAGE = pdfe.getpage
963 local GETNAME = pdfe.getname
964 local GETARRAY = pdfe.getarray
965 local GETDICTIONARY = pdfe.getdictionary
966 local GETFROMDICTIONARY = pdfe.getfromdictionary
967 local GETFROMARRAY = pdfe.getfromarray
968 local PAGESTOTABLE = pdfe.pagestotable
969 local DICTIONARYTOTABLE = pdfe.dictionarytotable
970 local ARRAYTOTABLE = pdfe.arraytotable
971 local TYPE = pdfe.type
972 local GETFROMREFERENCE = pdfe.getfromreference
973 local FILENAMEONLY = file.nameonly
974
975 local strENTRY_BEG = "\\["
976 local strENTRY_END = "\\\\\n"
977 local strCMD_BEG = "{"
978 local strCMD_END = "}"
979 local strARG_BEG = "{"
980 local strARG_END = "}"
981 local strKVS_BEG = "{"
982 local strKVS_END = "\n}"
983 local strKVS_EMPTY = "{}"
984 local strKV_BEG = "\n "
985 local strKV_END = ","
986 local strKEY_BEG = ""
987 local strKEY_END = ""
988 local strVALUE_BEG = "={"
989 local strVALUE_END = "}"
990 local strHEX_STR_BEG = "\\<"
991 local strHEX_STR_END = "\\>"
992
993
994 local strLIT_STR_BEG = "("
995 local strLIT_STR_END = ")"
996 local strDICT_BEG= "<<"
997 local strDICT_END= ">>"
998 local strNAME= "/"
999 local strARRAY_BEG= "["
1000 local strARRAY_END= "]"
1001 local strARRAY_SEP = " "
1002 local strRECT_SEP = " "
1003
1004
1005 local constCMD_ANNOT = "annot"
1006 local constCMD_BASEURL = "baseurl" -- unused
1007 local constCMD_DEST = "dest"
1008 local constCMD_FILE = "file"
1009 local constCMD_INFO = "info" -- unused
1010 local constCMD_PAGENUM = "pagenum"
1011 local constCMD_PAGE = "page"
1012 local constCMD_PAX = "pax"
1013
1014 -- cmd file
1015 local constKEY_SIZE = "Size"
1016 local constKEY_DATE = "Date"
1017
1018 -- cmd annot attributes
1019 local constKEY_C = "C"
1020 local constKEY_BORDER = "Border"
1021 local constKEY_BS = "BS"
1022 local constKEY_H = "H"
1023
1024 -- cmd annot/link/URI
1025 local constKEY_URI = "URI"
1026 local constKEY_IS_MAP = "IsMap" -- not handled by pax, consider later
1027
1028
1029 -- cmd annot/link/GoToR
1030 local constKEY_FILE = "File"
1031 local constKEY_DEST_NAME = "DestName" -- handle??
1032 local constKEY_DEST_PAGE = "DestPage" --ok
1033 local constKEY_DEST_VIEW = "DestView" --ok
1034
1035 -- cmd annot/link/GoTo
1036 local constKEY_DEST_RECT = "DestRect"
1037 local constKEY_DEST_X = "DestX"
1038 local constKEY_DEST_Y = "DestY"
1039 local constKEY_DEST_ZOOM = "DestZoom"
1040 local constKEY_DEST_LABEL = "DestLabel" --ok
1041
1042 -- cmd annot/link/Named
1043 local constKEY_NAME = "Name"
1044
1045 -- destination views
1046 local constDEST_XYZ = "XYZ"
1047 local constDEST_FIT = "Fit"
1048 local constDEST_FITH = "FitH"
1049 local constDEST_FITV = "FitV"
1050 local constDEST_FITR = "FitR"
1051 local constDEST_FITB = "FitB"
1052 local constDEST_FITBH = "FitBH"
1053 local constDEST_FITBV = "FitBV"
1054
1055
1056 -- get/build data
1057 -- returns table,pagecount where table objref ->page number
1058 local function getpagesdata (pdfedoc)
1059 local type,pagecount,detail = GETFROMDICTIONARY (pdfedoc.Catalog.Pages,"Count")
1060 local pagestable = PAGESTOTABLE (pdfedoc)
1061 local pagereftonum ={}
1062 for i=1,#pagestable do
1063 pagereftonum[pagestable[i][3]]=i
1064 end
1065 return pagereftonum, pagecount
1066 end
1067
1068 -- builds a table destination name -> obj reference (pdfedict) from the Names tree
1069
1070 local function processnamesarray (pdfearray,targettable)
1071 local tkey={}
1072 for i=1,#pdfearray do
1073 local type,value,detail= GETFROMARRAY(pdfearray,i)
1074 if (i % 2 == 1) then
1075 tkey = value
1076 else
1077 tvalue = value
1078 targettable[tkey]=tvalue
1079 end
1080 end
1081 end
1082
1083 local function findallnamesarrays (pdfedict,deststable)
1084 local namesarray = GETARRAY(pdfedict,"Names")
1085 if namesarray then
1086 processnamesarray (namesarray,deststable)
1087 else
1088 local kidsarray = GETARRAY(pdfedict,"Kids")
1089 if kidsarray then
1090 for i=1,#kidsarray do
1091 findallnamesarrays (kidsarray[i],deststable)
1092 end
1093 end
1094 end
1095 end
1096
1097 -- returns destnames -> objref table
1098 local function getdestreferences (pdfedoc)
1099 local deststable= {}
1100 local catnames = GETDICTIONARY (pdfedoc.Catalog, "Names")
1101 if catnames then
1102 local destsdict = GETDICTIONARY (catnames, "Dests")
1103 if destsdict then
1104 findallnamesarrays (destsdict,deststable)
1105 end
1106 end
1107 return deststable
1108 end
1109
1110 -- destinations can be an dict (/D [array]) or only an array!
1111
1112 local function getdestdata (name,pagereftonum,destnamestoref)
1113 local destref
1114 local type,ref,pagenum,destx,desty = nil, nil, 1,0,0
1115 local data = {{0,0}, {5,constDEST_XYZ}}
1116 -- if we get directly an array
1117 if (TYPE(name) == "pdfe.array") then
1118 data = ARRAYTOTABLE(name)
1119 type, ref, pageref = GETFROMARRAY(name,1)
1120 pagenum = pagereftonum[pageref]
1121 else
1122 destref = destnamestoref[name]
1123 end
1124 if destref then
1125 type,value,detail = GETFROMREFERENCE(destref)
1126 if TYPE(value) == "pdfe.dictionary" then
1127 local destarray = GETARRAY(value,"D")
1128 if destarray then
1129 data = ARRAYTOTABLE(destarray)
1130 type, ref, pageref = GETFROMARRAY(destarray,1)
1131 pagenum = pagereftonum[pageref]
1132 end
1133 elseif TYPE(value) == "pdfe.array" then
1134 data = ARRAYTOTABLE(value)
1135 type, ref, pageref = GETFROMARRAY(value,1)
1136 pagenum = pagereftonum[pageref]
1137 end
1138 end
1139 -- print("XXXXXXXXXX",table.serialize(data),pagenum)
1140 return pagenum, data
1141 end
1142
1143
1144 -- output functions
1145 -- write the info
1146 -- XXXXXX encode/escape the file name?
1147 local function outputENTRY_file (file, pdfedoc)
1148 local bytes = GETSIZE(pdfedoc)
1149 local date = (GETINFO(pdfedoc) and GETINFO(pdfedoc).CreationDate) or "D:22222222222222"
1150 -- file
1151 local a = strENTRY_BEG
1152 a = a .. strCMD_BEG .. constCMD_FILE .. strCMD_END
1153 a = a .. strARG_BEG .. strLIT_STR_BEG .. file .. strLIT_STR_END .. strARG_END
1154 a = a .. strKVS_BEG
1155 a = a .. strKV_BEG .. constKEY_SIZE .. strVALUE_BEG .. bytes .. strVALUE_END .. strKV_END
1156 a = a .. strKV_BEG .. constKEY_DATE .. strVALUE_BEG .. date .. strVALUE_END.. strKV_END
1157 a = a .. strKVS_END
1158 a = a .. strENTRY_END
1159 return a
1160 end
1161
1162 local function outputENTRY_pagenum (pages)
1163 local a = strENTRY_BEG
1164 a = a .. strCMD_BEG .. constCMD_PAGENUM .. strCMD_END
1165 a = a .. strARG_BEG .. pages .. strARG_END
1166 a = a .. strENTRY_END
1167 return a
1168 end
1169
1170 local function outputENTRY_page (pdfedoc,page) -- page=integer
1171 -- trimbox, bleedbox, cropbox,artbox,rotate etc could be put in
1172 -- the second argument as keyval:
1173 -- TrimBox={0 0 300 350},
1174 -- but pax skips the argument anyway, so not really useful
1175 local mediabox = pdfe.getbox(GETPAGE(pdfedoc,page),"MediaBox")
1176 local a=""
1177 if mediabox then
1178 a = strENTRY_BEG
1179 a = a .. strCMD_BEG .. constCMD_PAGE .. strCMD_END
1180 a = a .. strARG_BEG .. page .. strARG_END
1181 a = a .. strARG_BEG
1182 a = a .. mediabox[1]
1183 for j = 2, 4 do
1184 a = a .. strRECT_SEP.. mediabox[j]
1185 end
1186 a = a .. strARG_END
1187 a = a .. strKVS_EMPTY
1188 a = a .. strENTRY_END
1189 end
1190 return a
1191 end
1192
1193
1194 local function outputCMD_annot (pdfedict,page,type)
1195 local rectangle = ARRAYTOTABLE(GETARRAY(pdfedict,"Rect"))
1196 local a = strCMD_BEG .. constCMD_ANNOT .. strCMD_END
1197 a = a .. strARG_BEG .. page .. strARG_END
1198 a = a .. strARG_BEG .. GETNAME(pdfedict,"Subtype") .. strARG_END
1199 a = a .. strARG_BEG
1200 a = a .. rectangle[1][2]
1201 for k = 2, 4 do
1202 a = a.. strRECT_SEP..rectangle[k][2]
1203 end
1204 a = a .. strARG_END
1205 a = a .. strARG_BEG .. type .. strARG_END
1206 return a
1207 end
1208
1209
1210 local function outputKV_color (pdfedict)
1211 local color = GETARRAY(pdfedict,constKEY_C)
1212 local a =""
1213 if color then
1214 local colortable = ARRAYTOTABLE(color)
1215 a = strKV_BEG .. constKEY_C .. strVALUE_BEG .. strARRAY_BEG
1216 for i=1,#colortable do
1217 a = a.. colortable[i][2] .. strARRAY_SEP
1218 end
1219 a = a .. strARRAY_END .. strVALUE_END .. strKV_END
1220 end
1221 return a
1222 end
1223
1224 local function outputKV_key (pdfedict,key)
1225 local name = GETNAME(pdfedict,key)
1226 local a = ""
1227 if name then
1228 a = strKV_BEG .. key .. strVALUE_BEG .. strNAME .. name .. strVALUE_END .. strKV_END
1229 end
1230 return a
1231 end
1232
1233 -- XXX handle fourth argument
1234 local function outputKV_Border (pdfedict)
1235 local border = GETARRAY(pdfedict,constKEY_BORDER)
1236 local a =""
1237 if border then
1238 local bordertable = ARRAYTOTABLE(border)
1239 a = strKV_BEG .. constKEY_BORDER .. strVALUE_BEG .. strARRAY_BEG
1240 for i=1,3 do
1241 a = a .. bordertable[i][2] .. strARRAY_SEP
1242 end
1243 -- fourth argument later, it is an array (type 7)
1244 a = a ..strARRAY_END .. strVALUE_END .. strKV_END
1245 end
1246 return a
1247 end
1248
1249 local function outputKV_BS (pdfedict)
1250 local bsstyle = GETDICTIONARY(pdfedict,constKEY_BS)
1251 local a =""
1252 if bsstyle then
1253 local bsstyledict = DICTIONARYTOTABLE(bsstyle)
1254 a = strKV_BEG .. constKEY_BS .. strVALUE_BEG ..strDICT_BEG
1255 for k,v in pairs (bsstyledict) do
1256 a = a .. strNAME.. k
1257 if v[1]== 5 then
1258 a = a .. strNAME .. v[2]
1259 else
1260 a = a ..strRECT_SEP .. v[2]
1261 end
1262 end
1263 a = a .. strDICT_END .. strVALUE_END .. strKV_END
1264 end
1265 return a
1266 end
1267
1268
1269 local function outputKV_uri (pdfedict)
1270 local type, value, hex = GETFROMDICTIONARY(pdfedict,constKEY_URI)
1271 if TYPE(value) == "pdfe.reference" then
1272 type,value,hex = GETFROMREFERENCE(value)
1273 end
1274 local a= strKV_BEG .. constKEY_URI .. strVALUE_BEG
1275 if hex then
1276 a = a .. strHEX_STR_BEG .. value .. strHEX_STR_END
1277 else
1278 a = a .. strLIT_STR_BEG ..value .. strLIT_STR_END
1279 end
1280 a = a .. strVALUE_END .. strKV_END
1281 return a
1282 end
1283
1284 local function outputKV_N (pdfedict)
1285 local name = GETNAME(pdfedict,"N")
1286 local a= strKV_BEG .. constKEY_NAME .. strVALUE_BEG .. name .. strVALUE_END .. strKV_END
1287 return a
1288 end
1289
1290 -- if a gotoR has a filespec filespec we use this
1291 -- to output the reference. It is rather crude and handles only names and strings
1292 local function outputDICT (dictionary)
1293 local tkeys = {}
1294 local dict = DICTIONARYTOTABLE(dictionary)
1295 for name,value in pairs(dict) do
1296 table.insert(tkeys,name)
1297 end
1298 table.sort(tkeys)
1299 local a = "<<"
1300 for _,k in ipairs (tkeys) do
1301 v=dict[k]
1302 a = a .. strNAME.. k
1303 if v[1]== 5 then -- it is a name
1304 b = string.gsub(v[2], "/", "#2F")
1305 a = a .. strNAME .. b
1306 elseif v[1] == 6 then -- it is a string
1307 local b
1308 if v[3] then
1309 b = "<" .. v[2] .. ">"
1310 else
1311 b = "(" .. v[2] .. ")"
1312 end
1313 a = a ..strRECT_SEP .. b
1314 -- everything else is ignored for now!
1315 end
1316 end
1317 a = a .. ">>"
1318 return a
1319 end
1320
1321 local function outputKV_gotor (pdfedict) -- action dictionary
1322 local type, value, hex = GETFROMDICTIONARY(pdfedict,"F")
1323 local desttype, destvalue, destdetail = GETFROMDICTIONARY(pdfedict,"D")
1324 local a = strKV_BEG .. constKEY_FILE .. strVALUE_BEG
1325 if TYPE(value) == "pdfe.reference" then
1326 local x,dictionary = GETFROMREFERENCE(value)
1327 if TYPE(dictionary) == "pdfe.dictionary" then
1328 a = a .. outputDICT (dictionary)
1329 else
1330 print("ERROR!? this is not a dictionary!!")
1331 end
1332 else
1333 if hex then
1334 a = a .. strHEX_STR_BEG .. value .. strHEX_STR_END
1335 else
1336 a = a .. strLIT_STR_BEG .. value .. strLIT_STR_END
1337 end
1338 end
1339 a = a .. strVALUE_END .. strKV_END
1340 if desttype == 7 then
1341 local type, pagenum = GETFROMARRAY(GETARRAY (pdfedict,"D"),1)
1342 local type, fittype = GETFROMARRAY(GETARRAY (pdfedict,"D"),2)
1343 a = a .. strKV_BEG .. constKEY_DEST_PAGE .. strVALUE_BEG .. pagenum .. strVALUE_END .. strKV_END
1344 a = a .. strKV_BEG .. constKEY_DEST_VIEW .. strVALUE_BEG .. strNAME .. fittype .. strVALUE_END .. strKV_END
1345 elseif desttype == 6 then
1346 a = a .. strKV_BEG .. constKEY_DEST_NAME .. strVALUE_BEG ..
1347 strLIT_STR_BEG .. destvalue .. strLIT_STR_END .. strVALUE_END .. strKV_END
1348 end
1349 return a
1350 end
1351
1352 -- this outputs the key DestLabel with a count
1353
1354 local function outputKV_goto (count)
1355 local a = strKV_BEG .. constKEY_DEST_LABEL .. strVALUE_BEG .. count .. strVALUE_END .. strKV_END
1356 return a
1357 end
1358
1359 -- this outputs the key DestName with a name
1360 local function outputKV_goto_name (name)
1361 local a = strKV_BEG .. constKEY_DEST_NAME .. strVALUE_BEG .. name .. strVALUE_END .. strKV_END
1362 return a
1363 end
1364
1365 local function outputENTRY_dest (destcount,name,pagereftonum,destnamestoref,pdfedoc)
1366 local pagenum, data = getdestdata(name,pagereftonum,destnamestoref)
1367 local mediabox = pdfe.getbox(GETPAGE(pdfedoc,pagenum),"MediaBox")
1368 local a = strENTRY_BEG
1369 a = a .. strCMD_BEG .. constCMD_DEST .. strCMD_END
1370 a = a .. strARG_BEG .. pagenum .. strARG_END
1371 a = a .. strARG_BEG .. destcount .. strARG_END
1372 -- name
1373 a = a .. strARG_BEG .. data[2][2] .. strARG_END
1374 a = a .. strKVS_BEG
1375 if data[2][2] == constDEST_XYZ then
1376 if data[3] and data[3][2] then
1377 a = a .. strKV_BEG .. constKEY_DEST_X .. strVALUE_BEG .. data[3][2] .. strVALUE_END .. strKV_END
1378 else
1379 a = a .. strKV_BEG .. constKEY_DEST_X .. strVALUE_BEG .. mediabox[1] .. strVALUE_END .. strKV_END
1380 end
1381 if data[4] and data[4][2] then
1382 a = a .. strKV_BEG .. constKEY_DEST_Y .. strVALUE_BEG .. data[4][2] .. strVALUE_END .. strKV_END
1383 else
1384 a = a .. strKV_BEG .. constKEY_DEST_Y .. strVALUE_BEG .. mediabox[4] .. strVALUE_END .. strKV_END
1385 end
1386 if data[5] and data[5][2] then
1387 a = a .. strKV_BEG .. constKEY_DEST_ZOOM .. strVALUE_BEG .. data[5][2] .. strVALUE_END .. strKV_END
1388 end
1389 elseif data[2][2] == constDEST_FIT then -- nothing to do
1390 elseif data[2][2] == constDEST_FITB then -- nothing to do
1391 elseif data[2][2] == constDEST_FITH then
1392 if data[3] and data[3][2] then
1393 a = a .. strKV_BEG .. constKEY_DEST_Y .. strVALUE_BEG .. data[3][2] .. strVALUE_END .. strKV_END
1394 end
1395 elseif data[2][2] == constDEST_FITBH then
1396 if data[3] and data[3][2] then
1397 a = a .. strKV_BEG .. constKEY_DEST_Y .. strVALUE_BEG .. data[3][2] .. strVALUE_END .. strKV_END
1398 end
1399 elseif data[2][2] == constDEST_FITV then
1400 if data[3] and data[3][2] then
1401 a = a .. strKV_BEG .. constKEY_DEST_X .. strVALUE_BEG .. data[3][2] .. strVALUE_END .. strKV_END
1402 end
1403 elseif data[2][2] == constDEST_FITBV then
1404 if data[3] and data[3][2] then
1405 a = a .. strKV_BEG .. constKEY_DEST_X .. strVALUE_BEG .. data[3][2] .. strVALUE_END .. strKV_END
1406 end
1407 elseif data[2][2] == constDEST_FITR and data[6] then
1408 a = a .. strKV_BEG .. constKEY_DEST_RECT .. strVALUE_BEG
1409 a = a .. data[3][2] .. strRECT_SEP
1410 a = a .. data[4][2] .. strRECT_SEP
1411 a = a .. data[5][2] .. strRECT_SEP
1412 a = a .. data[6][2] .. strVALUE_END .. strKV_END
1413 end
1414 -- output also the name. This perhaps need a check if the name exists
1415 -- print("DEBUG TYPE name", TYPE(name))
1416 -- if name is a reference, resolve it once, issue #30
1417 if (TYPE(name) == "pdfe.reference") then
1418 _,name = GETFROMREFERENCE(name)
1419 end
1420 -- check also if name is still a reference issue #30
1421 if not (TYPE(name) == "pdfe.array" or TYPE(name) == "pdfe.reference") then
1422 a = a .. strKV_BEG .. constKEY_DEST_NAME .. strVALUE_BEG .. name .. strVALUE_END .. strKV_END
1423 end
1424 a = a .. strKVS_END .. strENTRY_END
1425 return a
1426 end
1427
1428
1429
1430
1431
1432
1433 -- the main function
1434
1435 local function __writepax (ext,file)
1436 local fileVAR = assert(kpse.find_file(file ..".pdf"),"file `"..file..".pdf` not found")
1437 local filebaseVAR = FILENAMEONLY(fileVAR)
1438 -- getting the data for the concrete document:
1439 local writeVAR = io.open (filebaseVAR .."."..ext,"w") -- always in current directory
1440 local function WRITE(content)
1441 writeVAR:write(content)
1442 end
1443 local docVAR = OPEN (fileVAR)
1444 local pagereftonumVAR, pagecountVAR = getpagesdata (docVAR)
1445 local destcountVAR = 0
1446 -- build from names table:
1447 local destnamestorefVAR = getdestreferences (docVAR)
1448 local collected_destinations = {}
1449 local useddestnames = {}
1450 -- output ...
1451 WRITE(strENTRY_BEG .. "{pax}{0.1l}" .. strENTRY_END)
1452 WRITE(outputENTRY_file(fileVAR,docVAR))
1453 WRITE(outputENTRY_pagenum(pagecountVAR))
1454 for i=1, pagecountVAR do
1455 WRITE(outputENTRY_page(docVAR,i))
1456 local annots=GETPAGE(docVAR,i).Annots
1457 if annots then
1458 for j = 0,#annots-1 do
1459 local annot = GETDICTIONARY (annots,j)
1460 annottable = DICTIONARYTOTABLE (annot)
1461 if annottable.Dest then
1462 destcountVAR=destcountVAR + 1
1463 WRITE (strENTRY_BEG)
1464 WRITE (outputCMD_annot(annot,i,"GoTo"))
1465 WRITE (strKVS_BEG) -- begin KVS data
1466 WRITE ( outputKV_color(annot) )
1467 WRITE ( outputKV_key(annot,constKEY_H) )
1468 WRITE ( outputKV_Border (annot) )
1469 WRITE ( outputKV_BS (annot) )
1470 WRITE ( outputKV_goto (destcountVAR) )
1471 local type,annotgoto,hex = GETFROMDICTIONARY(annot,"Dest")
1472 -- if the Dest is a string add the string as a name:
1473 if type == 6 then
1474 WRITE ( outputKV_goto_name(annotgoto) )
1475 end
1476 WRITE(strKVS_END) -- end KVS
1477 WRITE(strENTRY_END) -- end annot data
1478 table.insert(collected_destinations, outputENTRY_dest(destcountVAR,
1479 annotgoto,pagereftonumVAR,destnamestorefVAR,docVAR))
1480 else
1481 local annotaction = GETDICTIONARY(annot,"A")
1482 -- print("DEBUG A:",table.serialize(DICTIONARYTOTABLE(annotaction)))
1483 local annotactiontype =""
1484 if annotaction then
1485 annotactiontype = GETNAME(annotaction,"S")
1486 WRITE (strENTRY_BEG)
1487 WRITE (outputCMD_annot(annot,i,annotactiontype))
1488 WRITE (strKVS_BEG) -- begin KVS data
1489 WRITE ( outputKV_color(annot) )
1490 WRITE ( outputKV_key(annot,constKEY_H) )
1491 WRITE ( outputKV_Border (annot) )
1492 WRITE ( outputKV_BS (annot) )
1493 if annotactiontype == constKEY_URI then
1494 WRITE ( outputKV_uri(annotaction) )
1495 elseif annotactiontype =="GoTo" then
1496 local desttype, destname, destdetail = GETFROMDICTIONARY(annotaction,"D")
1497 -- print("DEBUG annotaction", desttype, destname, destdetail)
1498 destcountVAR=destcountVAR + 1
1499 WRITE ( outputKV_goto (destcountVAR) )
1500 -- this needs perhaps a check if destname actually exists.
1501 if desttype == 6 then
1502 WRITE ( outputKV_goto_name(destname) )
1503 end
1504 elseif annotactiontype=="GoToR" then
1505 WRITE ( outputKV_gotor(annotaction) )
1506 elseif annotactiontype=="Named" then
1507 WRITE ( outputKV_N (annotaction) )
1508 end
1509 WRITE(strKVS_END) -- end KVS
1510 WRITE(strENTRY_END) -- end annot data
1511 if annotactiontype =="GoTo" then
1512 local type,annotactiongoto,hex = GETFROMDICTIONARY(annotaction,"D")
1513 if type==6 then
1514 -- record used name
1515 useddestnames[annotactiongoto] = 1
1516 end
1517 table.insert(collected_destinations, outputENTRY_dest(destcountVAR,annotactiongoto,pagereftonumVAR,destnamestorefVAR,docVAR))
1518 end
1519 end
1520 end
1521 end
1522 end
1523 end
1524 for i=1,#collected_destinations do
1525 WRITE (collected_destinations[i])
1526 end
1527 -- write out the rest of the destinations.
1528 -- We order first the table so that the newpax file doesn't change between
1529 -- compilations, see issue #25 and PR #25
1530 local destnamessorted = {}
1531 for k,v in pairs (destnamestorefVAR) do
1532 -- ignore use dests and page destinations.
1533 i=string.find(k,"page.",1)
1534 if not useddestnames[k] and not i then
1535 table.insert (destnamessorted,k)
1536 end
1537 end
1538 table.sort(destnamessorted)
1539 for i = 1, #destnamessorted do
1540 k=destnamessorted[i]
1541 destcountVAR=destcountVAR + 1
1542 WRITE(outputENTRY_dest(destcountVAR,k,pagereftonumVAR,destnamestorefVAR,docVAR))
1543 end
1544 io.close(writeVAR)
1545 end
1546
1547 local function writepax (file)
1548 __writepax ("pax",file)
1549 end
1550
1551 local function writenewpax (file)
1552 __writepax ("newpax",file)
1553 end
1554
1555 newpax = {}
1556 newpax.writepax = writepax
1557 newpax.writenewpax = writenewpax
1558
1559 return newpax
1560
1561 %</lua>
1562 % \end{macrocode}