Debian Codegraph @

texmf-dist/source/latex/pdfmanagement/l3pdfdict.dtx

raw
1 % \iffalse meta-comment
2 %
3 %% File: l3pdfdict.dtx
4 %
5 % Copyright (C) 2018-2026 The LaTeX Project
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 % http://www.latex-project.org/lppl.txt
13 %
14 % This file is part of the "LaTeX PDF management 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/latex3/pdfresources
22 %
23 % for those people who are interested.
24 %
25 %<*driver>
26 \DocumentMetadata{tagging=on,pdfstandard=ua-2}
27 \documentclass[full]{l3doc}
28 \usepackage{latex-lab-testphase-l3doc}
29
30 \usepackage{array,booktabs}
31 \hypersetup{pdfauthor=The LaTeX Project,
32 pdftitle=l3pdfdict (LaTeX PDF management bundle)}
33
34 \begin{document}
35 \DocInput{\jobname.dtx}
36 \end{document}
37 %</driver>
38 % \fi
39 % \title{^^A
40 % The \pkg{l3pdfdict} module---tools for PDF dictionaries ^^A
41 % \\
42 % \LaTeX{} PDF management bundle
43 % }
44 %
45 % \author{^^A
46 % The \LaTeX{} Project\thanks
47 % {^^A
48 % E-mail:
49 % \href{mailto:[email protected]}
50 % {[email protected]}^^A
51 % }^^A
52 % }
53 %
54 % \date{Version 0.97c, released 2026-05-26}
55 %
56 % \maketitle
57 % \begin{documentation}
58 %
59 % \section{\pkg{l3pdfdict} documentation}
60 % Many PDF objects are or contain dictionaries---structures
61 % containing a number of \mbox{(pdf-)}name/value pairs.
62 % Examples are attributes of links,
63 % filespec dictionaries, xform dictionaries, the catalog, the info dictionary.
64 % The commands in this module offer an number of
65 % tools to handle such dictionaries. The module setups a name space for the
66 % dictionary names and offers some commands to output dictionaries.
67 %
68 % The dictionaries work in many respects
69 % like property lists with a few PDF specific changes:
70 % \begin{itemize}
71 % \item The keys are always converted with \cs{str_convert_pdfname:n}
72 % to get a correct PDF name;
73 % \item a key with a empty value can not be added, it will be ignored;
74 % \item there is a dedicated function to output the property as space
75 % separated list with keys with slash: \texttt{/key1 value1 /key2 value2}.
76 % \end{itemize}
77 % Local and global dictionaries can be created.
78 %
79 % \subsection{User Commands}
80 % \begin{function}[updated = 2020-12-03]
81 % {\pdfdict_new:n}
82 % \begin{syntax}
83 % \cs{pdfdict_new:n} \Arg{dictionary name}
84 % \end{syntax}
85 % This function create a new local or global dictionary. Which one depends on
86 % \meta{dictionary name}: If it begins with the standard |g| the dictionary is global,
87 % with |l| the dictionary is local, other starting chars will give an error.
88 % It is recommended to begin the name in the standard expl3 naming scheme
89 % with one or two underscores and a module name,
90 % so |g_module_XXXX| or |g__module_XXXX|.
91 % \end{function}
92 %
93 % \begin{function}[added = 2020-06-16,updated = 2020-12-03]
94 % {\pdfdict_set_eq:nn,\pdfdict_gset_eq:nn}
95 % \begin{syntax}
96 % \cs{pdfdict_set_eq:nn} \Arg{local dictionary name_1} \Arg{dictionary name_2}\\
97 % \cs{pdfdict_gset_eq:nn} \Arg{global dictionary name_1} \Arg{dictionary name_2}
98 % \end{syntax}
99 % This functions copy \meta{dictionary name_2} into
100 % \meta{local/global dictionary name_1} locally or globally. If the
101 % dictionary \meta{local/global dictionary name_1} doesn't exist yet, it will be created.
102 % If \meta{dictionary name_2} doesn't exist yet, an error will be raised.
103 % \end{function}
104 %
105 % \begin{function}[added = 2020-04-06]
106 % {
107 % \pdfdict_put:nnn,\pdfdict_put:nne,\pdfdict_put:nee,
108 % \pdfdict_gput:nnn,\pdfdict_gput:nne,\pdfdict_gput:nee
109 % }
110 % \begin{syntax}
111 % \cs{pdfdict_put:nnn} \Arg{local dictionary} \Arg{name} \Arg{value} \\
112 % \cs{pdfdict_gput:nnn} \Arg{global dictionary} \Arg{name} \Arg{value}
113 % \end{syntax}
114 % This function puts key \meta{name} and value \meta{value} locally or globally in the
115 % \meta{dictionary} created with \cs{pdfdict_new:n}.
116 % \Arg{name} should be a PDF Name without the starting slash. It will be stored
117 % with \cs{str_convert_pdfname:n}, so will be automatically correctly escaped in case
118 % it contains slashes, spaces or other chars not allowed in a PDF name.
119 % \meta{value} should be a valid PDF value for this name in the
120 % target dictionary. The value is \emph{neither} converted \emph{nor} escaped automatically.
121 % If the value is blank nothing is added to the dictionary.
122 %
123 % When adding a value keep in mind that the expansion behaviour
124 % of the backends differ. Some backends expand a
125 % value always fully when writing to the PDF, with other backends commands
126 % could end as strings in the PDF. This makes controlling the
127 % expansion quite tricky. It is better to not rely on
128 % \meta{value} to be expanded nor not expanded by the backend commands.
129 % \end{function}
130 %
131 % \begin{function}[EXP,added = 2020-12-04]
132 % { \pdfdict_item:nn, \pdfdict_item:ne }
133 % \begin{syntax}
134 % \cs{pdfdict_item:nn} \Arg{key} \Arg{value}
135 % \end{syntax}
136 % A simple command to output key-value as |/key value|. This is
137 % needed to output dictionaries in mapping commands. The command doesn't
138 % do any escaping, it expects that the name has been escaped when the value
139 % has been stored into the dictionary.
140 % If the value is blank nothing is output.
141 % The command is expandable if the content is it.
142 % \end{function}
143 %
144 % \begin{function}[EXP,updated = 2020-12-03]
145 % { \pdfdict_use:n }
146 % \begin{syntax}
147 % \cs{pdfdict_use:n} \Arg{dictionary}
148 % \end{syntax}
149 % This outputs the property list of the dictionary as a list of
150 % |/key value| pairs.
151 % This can be used e.g. when writing a dictionary object with
152 % \cs{pdf_object_write:nne}
153 % \end{function}
154 %
155 % \begin{function}[updated = 2020-12-03]
156 % {\pdfdict_show:n }
157 % \begin{syntax}
158 % \cs{pdfdict_show:n} \Arg{dictionary}
159 % \end{syntax}
160 % This shows the content of \meta{dictionary} in the log and on the terminal.
161 % \end{function}
162 %\begin{function}[EXP, pTF,updated = 2020-12-03]
163 % { \pdfdict_if_exist:n }
164 % \begin{syntax}
165 % \cs{pdfdict_if_exist:n} \Arg{dictionary}
166 % \end{syntax}
167 % This tests if the dictionary exists.
168 % \end{function}
169 % \begin{function}[EXP, pTF,updated = 2020-12-03]
170 % { \pdfdict_if_empty:n }
171 % \begin{syntax}
172 % \cs{pdfdict_if_empty:n} \Arg{dictionary}
173 % \end{syntax}
174 % This tests if the dictionary is empty. The result is false if the
175 % dictionary doesn't exist.
176 % \end{function}
177 % \begin{function}[added = 2020-07-06]
178 % {\pdfdict_get:nnN }
179 % \begin{syntax}
180 % \cs{pdfdict_get:nnN} \Arg{dictionary} \Arg{name} \meta{tl var}
181 % \end{syntax}
182 % Recovers the \meta{value} stored by \cs{pdfdict_put:nnn} or
183 % \cs{pdfdict_gput:nnn}
184 % for \meta{name} and places this in the \meta{token list
185 % variable}. If \meta{name} is not found
186 % then the \meta{tl var} is set
187 % to the special marker \cs{q_no_value}. \meta{name} is first converted
188 % with \cs{str_convert_pdfname:n}. The \meta{token list
189 % variable} is set within the current \TeX{} group.
190 % \end{function}
191 %
192 % \begin{function}[updated = 2020-12-03]
193 % {
194 % \pdfdict_remove:nn, \pdfdict_gremove:nn
195 % }
196 % \begin{syntax}
197 % \cs{pdfdict_remove:nn} \Arg{local dictionary} \Arg{name}\\
198 % \cs{pdfdict_gremove:nn} \Arg{global dictionary} \Arg{name}
199 % \end{syntax}
200 % Removes \meta{name} and its associated \meta{value} from
201 % the \Arg{dictionary}
202 % The removal is local from local dictionaries
203 % and global from global dictionaries.
204 % If \meta{name} is not found no change occurs,
205 % \emph{i.e}~there is no need to test for the existence of a name before
206 % trying to remove it. \meta{name} is first converted
207 % with \cs{str_convert_pdfname:n}.
208 % \end{function}
209 % \end{documentation}
210 %
211 % \begin{implementation}
212 % \section{\pkg{l3pdfdict} implementation}
213 % \begin{macrocode}
214 %<@@=pdfdict>
215 %<*header>
216 \ProvidesExplPackage{l3pdfdict}{2026-05-26}{0.97c}
217 {Tools for PDF dictionaries (LaTeX PDF management bundle)}
218 %</header>
219 % \end{macrocode}
220 % \subsection{messages}
221 % \begin{macrocode}
222 %<*package>
223 \cs_new:Npn \@@_get_type:n #1
224 {
225 \str_case_e:nn { \str_head:n{#1} }
226 {
227 {g}{global}
228 {l}{local}
229 }
230 }
231 \msg_new:nnn { pdfdict } { show-dict }
232 { %#1: name of the dictionary
233 %#2: expanded content
234 %#3: type
235 The~#3~dictionary~'#1'~
236 \tl_if_empty:nTF {#2}
237 { is~empty \\>~ . }
238 { contains~the~pairs~(without~outer~braces): #2 . }
239 }
240 \msg_new:nnn { pdfdict } { unknown-dict }
241 {
242 The~dictionary~'#1'~is~unknown.
243 }
244 \msg_new:nnn { pdfdict } { dict-already-defined }
245 {
246 The~#2~dictionary~'#1'~is~already~defined.
247 }
248 \msg_new:nnn { pdfdict } { empty-value }
249 { The~value~#1~for~#2~is~blank~and~will~be~ignored }
250
251 \msg_new:nnn { pdfdict } { invalid-name }
252 { Name~'#1'~is~not~valid\\
253 Names~of~dictionaries~should~start~with~'g_'~or~'l_' }
254
255 % \end{macrocode}
256 %
257
258 % \subsection{Creating dictionaries}
259 % \begin{variable}
260 % {\g_@@_names_seq,\g_@@_gnames_seq}
261 % Two seq to store the used names for diagnostics.
262 % \begin{macrocode}
263 \seq_new:N \g_@@_lnames_seq
264 \seq_new:N \g_@@_gnames_seq
265 % \end{macrocode}
266 % \end{variable}
267 %
268 % \begin{macro}
269 % {
270 % \@@_name:n, \__kernel_pdfdict_name:n,
271 % \@@_new:n, \pdfdict_new:n,
272 % }
273 %
274 % This are the commands to create new dictionaries and to access their internal
275 % name. All internal names start with |g__pdfdict_/| or |l__pdfdict_/|.
276 %
277 % For the other modules we also need a kernel command to access the internal
278 % name to speed up the code and allow the use standard commands
279 % of the \texttt{prop} module to deal with the dictionaries. For example\\
280 % |\prop_clear:c { \__kernel_pdfdict_name:n { name }}|
281 %
282 % \begin{macrocode}
283 \cs_new:Npn \@@_name:n #1 % #1 dictionary name
284 {
285 \str_head:n{#1}_@@_/#1_prop
286 }
287 \cs_set_eq:NN \__kernel_pdfdict_name:n \@@_name:n
288
289 \cs_new_protected:Npn \@@_new:n #1
290 {
291 \@@_if_exist:nTF { #1 }
292 {
293 \msg_error:nnee
294 { pdfdict }
295 { dict-already-defined }
296 { \tl_to_str:n {#1} }
297 { \@@_get_type:n{#1} }
298 }
299 {
300 \str_case_e:nnF { \str_head:n{#1} }
301 {
302 {g}
303 {
304 \prop_new:c { \@@_name:n { #1 } }
305 \seq_gput_right:cn {g_@@_gnames_seq} { #1 }
306 }
307 {l}
308 {
309 \prop_new:c { \@@_name:n { #1 } }
310 \seq_gput_right:cn {g_@@_lnames_seq} { #1 }
311 }
312 }
313 {
314 \msg_error:nne{pdfdict}{invalid-name}{\tl_to_str:n{#1}}
315 }
316 }
317 }
318
319 \cs_set_eq:NN \pdfdict_new:n \@@_new:n
320 % \end{macrocode}
321 % \end{macro}
322 %
323 % \begin{macro}
324 % {
325 % \@@_set_eq:nn,\pdfdict_set_eq:nn,
326 % \@@_gset_eq:nn,\pdfdict_gset_eq:nn
327 % }
328 % \begin{macrocode}
329 \cs_new_protected:Npn \@@_set_eq:nn #1 #2
330 {
331 \@@_if_exist:nTF { #2 }
332 {
333 \@@_if_exist:nF { #1 }
334 {
335 \@@_new:n { #1 }
336 }
337 \prop_set_eq:cc { \@@_name:n {#1} }{ \@@_name:n {#2} }
338 }
339 {
340 \msg_error:nnn { pdfdict } { unknown-dict } { #1 }
341 }
342 }
343
344 \cs_set_eq:NN \pdfdict_set_eq:nn \@@_set_eq:nn
345
346 \cs_new_protected:Npn \@@_gset_eq:nn #1 #2
347 {
348 \@@_if_exist:nTF { #2 }
349 {
350 \@@_if_exist:nF { #1 }
351 {
352 \@@_new:n { #1 }
353 }
354 \prop_gset_eq:cc { \@@_name:n {#1} }{ \@@_name:n {#2} }
355 }
356 {
357 \msg_error:nnn { pdfdict } { unknown-dict } { #1 }
358 }
359 }
360
361 \cs_set_eq:NN \pdfdict_gset_eq:nn \@@_gset_eq:nn
362 % \end{macrocode}
363 % \end{macro}
364 %
365 %
366 % \begin{macro}[pTF]
367 % {
368 % \@@_if_exist:n, \pdfdict_if_exist:n,
369 % }
370 %
371 % Existence tests.
372 % \begin{macrocode}
373 %local
374 \prg_new_conditional:Npnn \@@_if_exist:n #1 { p , T , F , TF }
375 {
376 \prop_if_exist:cTF
377 { \@@_name:n { #1 } }
378 { \prg_return_true: }
379 { \prg_return_false: }
380 }
381 \prg_set_eq_conditional:NNn
382 \pdfdict_if_exist:n \@@_if_exist:n { p , T , F , TF }
383 % \end{macrocode}
384 % \end{macro}
385 %
386 %
387 % \begin{macro}[pTF]
388 % {
389 % \@@_if_empty:n, \pdfdict_if_empty:n,
390 % }
391 %
392 % Tests for emptiness.
393 % \begin{macrocode}
394 \prg_new_conditional:Npnn \@@_if_empty:n #1 { p , T , F , TF }
395 {
396 \prop_if_empty:cTF
397 { \@@_name:n { #1 } }
398 { \prg_return_true: }
399 { \prg_return_false: }
400 }
401
402 \prg_set_eq_conditional:NNn
403 \pdfdict_if_empty:n \@@_if_empty:n { p , T , F , TF }
404 % \end{macrocode}
405 % \end{macro}
406 %
407 %
408 % \begin{macro}
409 % {
410 % \@@_put:nnn, \pdfdict_put:nnn,
411 % \@@_gput:nnn,\pdfdict_gput:nnn
412 % }
413 % These are the commands to store values into the dictionaries.
414 % The main difference to adding values to a normal property list is,
415 % that the keys are converted with \cs{str_convert_pdfname:n}
416 % and that empty values are ignored.
417 % \begin{macrocode}
418 \cs_new_protected:Npn \@@_put:nnn #1 #2 #3 %#1 (local) dict, #2 name, #3 value
419 {
420 \tl_if_blank:nTF { #3 }
421 {
422 \msg_warning:nnnn { pdfdict }{ empty-value }{ #2 } { #1 }
423 }
424 {
425 \@@_if_exist:nTF { #1 }
426 {
427 \exp_args:Nne \prop_put:cnn
428 { \@@_name:n { #1 } }{ \str_convert_pdfname:n { #2 } } { #3 }
429 }
430 {
431 \msg_error:nnn { pdfdict } { unknown-dict } { #1 }
432 }
433 }
434 }
435
436 \cs_set_eq:NN \pdfdict_put:nnn \@@_put:nnn
437 \cs_generate_variant:Nn \pdfdict_put:nnn {nne,nno,nee,nnx}
438
439 \cs_new_protected:Npn \@@_gput:nnn #1 #2 #3 %#1 global dict, #2 name, #3 value
440 {
441 \tl_if_empty:nTF { #3 }
442 {
443 \msg_warning:nnnn { pdfdict }{ empty-value }{ #2 } { #1 }
444 }
445 {
446 \@@_if_exist:nTF { #1 }
447 {
448 \exp_args:Nne \prop_gput:cnn
449 { \@@_name:n { #1 } }{ \str_convert_pdfname:n { #2 } } { #3 }
450 }
451 {
452 \msg_error:nnn { pdfdict } { unknown-dict } { #1 }
453 }
454 }
455 }
456
457 \cs_set_eq:NN \pdfdict_gput:nnn \@@_gput:nnn
458 \cs_generate_variant:Nn \pdfdict_gput:nnn {nne,nno,nee,nnx}
459 % \end{macrocode}
460 % \end{macro}
461 %
462 % \begin{macro}
463 % {
464 % \@@_get:nnN, \pdfdict_get:nnN,
465 % }
466 % Recover the values. The name must be first escaped to match the stored name.
467 % \begin{macrocode}
468 \cs_new_protected:Npn \@@_get:nnN #1 #2 #3 %dict,key,macro
469 {
470 \@@_if_exist:nTF { #1 }
471 {
472 \exp_args:Nne \prop_get:cnN
473 { \@@_name:n { #1 } }
474 { \str_convert_pdfname:n { #2 } } #3
475 }
476 {
477 \msg_error:nnn { pdfdict } { unknown-dict } { #1 }
478 }
479 }
480
481 \cs_set_eq:NN \pdfdict_get:nnN \@@_get:nnN
482 % \end{macrocode}
483 % \end{macro}
484 %
485 % \begin{macro}
486 % {
487 % \@@_remove:nn, \pdfdict_remove:nn,
488 % \@@_gremove:nn,\pdfdict_gremove:nn
489 % }
490 % This removes a name/value pair from a dictionary.
491 % The name has to be passed through the escaping.
492 % \begin{macrocode}
493 \cs_new_protected:Npn \@@_remove:nn #1 #2 %dict,name
494 {
495 \@@_if_exist:nTF { #1 }
496 {
497 \exp_args:Nne \prop_remove:cn
498 { \@@_name:n { #1 } }{ \str_convert_pdfname:n { #2 } }
499 }
500 {
501 \msg_error:nnn { pdfdict } { unknown-dict } { #1 }
502 }
503 }
504 \cs_set_eq:NN \pdfdict_remove:nn \@@_remove:nn
505
506 \cs_new_protected:Npn \@@_gremove:nn #1 #2 %dict,name
507 {
508 \@@_if_exist:nTF { #1 }
509 {
510 \exp_args:Nne \prop_gremove:cn
511 { \@@_name:n { #1 } }{ \str_convert_pdfname:n { #2 } }
512 }
513 {
514 \msg_error:nnn { pdfdict } { unknown-dict } { #1 }
515 }
516 }
517
518 \cs_set_eq:NN \pdfdict_gremove:nn \@@_gremove:nn
519 % \end{macrocode}
520 % \end{macro}
521 %
522 % \begin{macro}
523 % { \@@_show:Nn, \pdfdict_show:n }
524 % This allows to show the content of dictionaries. It also displays if a
525 % dictionary is local or global. If both exists both are shown.
526 % \begin{macrocode}
527 \cs_new_protected:Npn \@@_show:Nn #1#2 %#1 message command, #2 dict
528 {
529 \prop_if_exist:cTF { \@@_name:n { #2 } }
530 {
531 #1
532 { pdfdict }
533 { show-dict }
534 { \tl_to_str:n {#2} }
535 { \prop_map_function:cN {\@@_name:n { #2 }} \msg_show_item:nn }
536 { \@@_get_type:n{#2} }
537 { }
538 }
539 {
540 #1 { pdfdict } { unknown-dict } { #2 } {}{}{}
541 }
542 }
543 \cs_new_protected:Npn \pdfdict_show:n #1
544 {
545 \@@_show:Nn \msg_show:nneeee {#1}
546 }
547 % \end{macrocode}
548 % \end{macro}
549 %
550 %
551 % \begin{macro}{\@@_item:nn, \@@_item:ne}
552 % \begin{macro}{\pdfdict_item:nn, \pdfdict_item:ne}
553 % \begin{macrocode}
554 \cs_new:Npn \@@_item:nn #1 #2 %#1 name, #2 value
555 {
556 \tl_if_blank:nF {#2} { /#1~#2~ }
557 }
558 \cs_generate_variant:Nn \@@_item:nn {ne}
559 \cs_set_eq:NN \pdfdict_item:nn \@@_item:nn
560 \cs_generate_variant:Nn \pdfdict_item:nn {ne}
561 % \end{macrocode}
562 % \end{macro}
563 % \end{macro}
564 %
565 %
566 % \begin{macro}
567 % {
568 % \@@_use:n,\pdfdict_use:n
569 % }
570 % \cs{@@_use:n} outputs a prop as needed in a dictionary:
571 % as a list of /\meta{key} \meta{value} pairs.
572 % \begin{NOTE}{UF}
573 % !! is e-expansion the right thing?
574 % \end{NOTE}
575 % \begin{macrocode}
576 \cs_new:Npn \@@_use:n #1 %#1 dict
577 {
578 \prop_map_function:cN { \@@_name:n { #1 } } \@@_item:ne
579 }
580
581 \cs_set_eq:NN \pdfdict_use:n \@@_use:n
582 % \end{macrocode}
583 % \end{macro}
584 %
585 %
586 % \begin{macrocode}
587 %</package>
588 % \end{macrocode}
589 %
590 % \end{implementation}
591 %
592 % \PrintIndex