Debian Codegraph @

texmf-dist/source/latex/pdfmanagement/l3pdffield-checkbox.dtx

raw
1 % \iffalse meta-comment
2 %
3 %% File: l3pdffield-checkbox.dtx
4 %
5 % Copyright (C) 2021-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{} % no tagging yet: parent-child, no ua-2 yet, labels of widgets missing.
27 \documentclass[full]{l3doc}
28 %\usepackage{latex-lab-testphase-l3doc}
29
30 \usepackage{array,booktabs}
31 \usepackage{l3pdffield,bearwear}
32 \hypersetup{pdfauthor=The LaTeX Project,
33 pdftitle=l3pdffield (LaTeX PDF management bundle)}
34 \begin{document}
35 \DocInput{\jobname.dtx}
36 \end{document}
37 %</driver>
38 % \fi
39 % \NewDocElement[
40 % idxgroup=checkbox keys,
41 % idxtype = {checkbox key},
42 % printtype= \textit{checkbox key}
43 % ]{Checkboxkey}{checkboxkey}
44 % \providecommand\hook[1]{\texttt{#1}}
45 % \ExplSyntaxOn
46 % \pdffield_appearance:nn {pdffield/bear/Yes}
47 % {
48 % \tikz\bear\bearwear[shirt=red,body~deco={\node[font=\tiny\bfseries,white]~at~(beartummy){Yes};}];
49 % }
50 % \pdffield_appearance:nn {pdffield/bear/Off}
51 % {
52 % \tikz\bear\bearwear[body~deco={\node[font=\tiny\bfseries,white]~at~(beartummy){Off};}];
53 % }
54 % \ExplSyntaxOff
55 % \title{^^A
56 % The \pkg{l3pdffield-checkbox} module\\ Commands to create checkbox form fields ^^A
57 % \\ \LaTeX{} PDF management bundle
58 % }
59 %
60 % \author{^^A
61 % The \LaTeX{} Project\thanks
62 % {^^A
63 % E-mail:
64 % \href{mailto:[email protected]}
65 % {[email protected]}^^A
66 % }^^A
67 % }
68 %
69 % \date{Version 0.97c, released 2026-05-26}
70 %
71 % \maketitle
72 % \begin{documentation}
73 % \section{\pkg{l3pdffield-checkbox} Introduction}
74 % This is the documentation for checkbox fields, for general information about form fields
75 % check the documentation l3pdffield.
76 %
77 %
78 %
79 % Please keep in mind
80 % \begin{itemize}
81 % \item Not every PDF viewer supports checkboxes.
82 % \item The handling can depend on settings in the PDF viewer. In adobe reader for
83 % example I had to disable an option to avoid that it tries to create an appearance
84 % itself
85 % \item Standards like pdf/A disable features of form fields too
86 % (as you typically can't change the PDF).
87 % \end{itemize}
88 % \section{Checkboxes}
89 % Click me:
90 % \ExplSyntaxOn
91 % \pdffield_checkbox:n{name=bear,appearance=pdffield/bear,width=23pt,height=30pt,depth=10pt}
92 % \ExplSyntaxOff
93 %
94 % \bigskip
95 % \subsection{Commands}
96 % \begin{function}{\pdffield_checkbox:n}
97 % \begin{syntax}
98 % \cs{pdffield_checkbox:n} \Arg{key val list}
99 % \end{syntax}
100 % This creates a checkbox to check and uncheck. The list of allowed keys is described below.
101 % The \meta{key val list} should at least set the name, without it the default name
102 % |checkbox| is used. Checkboxes with the same
103 % name belong to the same field and are checked and unchecked together. The default appearance
104 % is a quadratic frame with a \cs{texttimes} in it for the checked case.
105 % The default appearance is setup at the first use and will use the font family
106 % active at that time.
107 % \end{function}
108 %
109 %
110 % \subsection{Keys}
111 %
112 % The new checkbox command accept all field and annot keys from l3pdffield.
113 % A few keys are disabled or are forced to specific values.
114 % The |appearance| keys have a more checkbox specific
115 % behaviour, other keys have other defaults than with the basic commands.
116 % Additionally there
117 % are a small number of keys specific to a checkbox.
118 % For convenience a number of important keys are documented here too, even if
119 % they are already in the document from l3pdffield.
120 %
121 %
122 % Disabled keys are
123 % \begin{itemize}
124 % \item |V|, |DV|, |AS|: use |checked| instead.
125 % \item |FT| is overwritten.
126 % \item For checkboxes only the field flags |ReadOnly|, |Required| and |NoExport|
127 % make sense.
128 % |Radio|, |Pushbutton| are set automatically by the code
129 % as this is required for a checkbox.
130 % \end{itemize}
131 %
132 % \begin{function}{preset-checkbox}
133 % \begin{syntax}
134 % |preset-checkbox| = \Arg{key val list}
135 % \end{syntax}
136 % This key can be used with \cs{pdffield_setup:n} and allows to set default keys for a checkbox.
137 % \end{function}
138 %
139 % \begin{function}{name,T}
140 % \begin{syntax}
141 % |name| = \meta{partial name}\\
142 % |T| = \meta{partial name}
143 % \end{syntax}
144 % This sets the partial name of the field. The value
145 % shouldn't contain a period, be not empty and sensibly consist of simple chars.
146 % Additionally the value is used to create the field ID.
147 % This means that checkboxes with the same partial name are annotations
148 % with the same field as parent and are checked and unchecked together---this
149 % what is typically expected.
150 % The field ID is then internal and can not be used to
151 % attach another annotation.
152 % For explicit control of the field ID use the |fieldID| key.
153 % \end{function}
154 %
155 % \begin{function}{fieldID}
156 % \begin{syntax}
157 % |fieldID| = \meta{field ID}\\
158 % \end{syntax}
159 % \emph{For experts only!}
160 % This allows to give the checkbox field a specific ID. This is only useful
161 % in the context of a larger fieldset, if for example you want to use
162 % the same partial name for more than one field, or if you want to attach another annotation
163 % to the field with \cs{pdffield_annot:n}. If used wrongly you can
164 % easily create invalid fieldset. It allows you to create to fields with the
165 % same partial name, but if you want to see both
166 % you need to ensure that their full names are
167 % different---for example by adding some parent fields.
168 % \end{function}
169 %
170 % \begin{function}{parent}
171 % \begin{syntax}
172 % |parent| = \meta{field ID}\\
173 % \end{syntax}
174 % This is only needed if the field should be part
175 % of a larger fieldset. The value should be a field ID of a field created previously
176 % with \cs{pdffield_field:nn}.
177 % \end{function}
178 %
179 % \begin{function}{altname,TU}
180 % \begin{syntax}
181 % |altname| = \meta{string}\\
182 % |TU| = \meta{string}\\
183 % \end{syntax}
184 % This is sets an alternative name for user interaction.
185 % This name can only be set at the first checkbox instance, when the field is initialized.
186 % \end{function}
187 %
188 % \begin{function}{mappingname,TM}
189 % \begin{syntax}
190 % |mappingname| = \meta{string}\\
191 % |TM| = \meta{string}\\
192 % \end{syntax}
193 % This is sets an alternative name for export.
194 % This name can only be set at the first checkbox instance, when the field is initialized.
195 % \end{function}
196 %
197 % \begin{function}{width,height,depth}
198 % \begin{syntax}
199 % |width| = \meta{dim expression}\\
200 % |height| = \meta{dim expression}\\
201 % |depth| = \meta{dim expression}
202 % \end{syntax}
203 % These keys allow to set the dimensions of checkbox instance.
204 % The value should be a dimension expression. By default
205 % |width| and |height| use \cs{normalbaselineskip}, the |depth| is zero.
206 % \end{function}
207 %
208 % \begin{function}{AP/N,appearance,AP/R,rollover-appearance,AP/D,down-appearance}
209 % \begin{syntax}
210 % |AP/N| = \meta{partial appearance name}\\
211 % |appearance| = \meta{partial appearance name}\\
212 % |AP/R| = \meta{partial appearance name}\\
213 % |rollover-appearance| = \meta{partial appearance name}\\
214 % |AP/D| = \meta{partial appearance name}\\
215 % |down-appearance| = \meta{partial appearance name}
216 % \end{syntax}
217 % This keys sets the normal appearance, the rollover appearance (when the
218 % mouse hovers over the checkbox) and the down appearance (when the
219 % mouse clicks). They take as value a
220 % \meta{partial appearance name} and expects that \emph{two} form Xobjects
221 % \meta{partial appearance name}|/Yes| and \meta{partial appearance name}|/Off|
222 % has been created. The initial value is |pdffield/checkbox/default|
223 % for the normal appearance and shows a \cs{texttimes}.
224 % The other appearances are not set by default.
225 % \end{function}
226 %
227 % \begin{function}{checked}
228 % \begin{syntax}
229 % |checked| = |true|\verb"|"|false|
230 % \end{syntax}
231 % This is a boolean key which allows to set if the
232 % checkbox should be initially checked or not. It sets the |/V| and |/DV| key of the field
233 % and the |/AS| key of the annotation instance. It is possible to use different
234 % values for different instances, if one wants to confuse the user.
235 % \end{function}
236 %
237 % \begin{function}{value,default}
238 % \begin{syntax}
239 % |value| = |Yes|\verb"|"|Off|\\
240 % |default| = |Yes|\verb"|"|Off|\\
241 % \end{syntax}
242 % With checkboxes this two key are simply an alternative input for |checked|.
243 % \end{function}
244 %
245 % \subsection{Using with hyperref}
246 % The \cs{CheckBox} command from hyperref also prints a label, something that the
247 % command here doesn't do. A redefinition like the following should allow \cs{CheckBox}
248 % to use the commands of this module. Be aware that the behaviour will not be identical!
249 % Not every setting and key from \pkg{hyperref} has been copied.
250 %
251 % \begin{verbatim}
252 % \ExplSyntaxOn\makeatletter
253 % \def\@CheckBox[#1]#2{\LayoutCheckField{#2}{\pdffield_checkbox:n {name=#2,#1}}}
254 % \ExplSyntaxOff\makeatother
255 % \end{verbatim}
256 %
257 % \subsection{Some background}
258 % For some general background about fieldsets, fields and field annotations, please
259 % check \pkg{l3pdffield}. Here are only some remarks about the special case of
260 % checkboxes.
261 %
262 % A checkbox consist of a field along with one or more field annotations.
263 % The annotations can appear on more than one page or locations and if one instance
264 % is checked all other instances follows and are checked too.
265 %
266 % A checkbox has two different looks: checked and unchecked. The hyperref
267 % implementation uses symbolic names for the two states and adds some
268 % values with the /MK key and lets the PDF viewer
269 % create a look from them. But this doesn't work reliably and is one of the reasons
270 % why a reimplementation is needed. Also newer PDF versions
271 % deprecate the /NeedAppearances setting and require that such a look,
272 % an \enquote{appearance}, is given as form XObjects.
273 % So the code forces the use of two appearances.
274 %
275 % \end{documentation}
276 %
277 % \begin{implementation}
278 % \DoNotIndex
279 % {\\
280 % ,\bitset_clear:N
281 % ,\bitset_new:Nn
282 % ,\bitset_set_false:Nn
283 % ,\bitset_set_true:Nn
284 % ,\bitset_to_arabic:N
285 % ,\bool_new:N
286 % ,\box_dp:N
287 % ,\box_ht:N
288 % ,\clist_map_inline:nn
289 % ,\color_export:nnN
290 % ,\color_set:nn
291 % ,\color_set:nnn
292 % ,\cs_new_protected:Npn
293 % ,\cs_new_protected:cpn
294 % ,\cs_set_eq:NN
295 % ,\cs_gset_eq:cN
296 % ,\cs_set_protected:Npn
297 % ,\cs_generate_variant:Nn
298 % ,\cs_gset_eq:NN
299 % ,\csname
300 % ,\dim_eval:n
301 % ,\dim_new:N
302 % ,\endcsname
303 % ,\exp_args:Ne
304 % ,\fboxsep
305 % ,\group_begin:
306 % ,\group_end:
307 % ,\hbox_to_wd:nn
308 % ,\hfill
309 % ,\hook_gput_code:nnn
310 % ,\int_eval:n
311 % ,\l_keys_choice_int
312 % ,\keys_define:nn
313 % ,\keys_set:nn
314 % ,\mode_leave_vertical:
315 % ,\msg_error:nnn
316 % ,\msg_error:nnnn
317 % ,\msg_new:nnn
318 % ,\msg_warning:nn
319 % ,\msg_warning:nnn
320 % ,\msg_warning:nnnnn
321 % ,\NeedsTeXFormat
322 % ,\normalsize
323 % ,\pdf_name_from_unicode_e:n
324 % ,\pdf_object_if_exist:nTF
325 % ,\pdf_object_if_exist:nF
326 % ,\pdf_object_new:n
327 % ,\pdf_object_ref:n
328 % ,\pdf_object_ref_last:
329 % ,\pdf_object_unnamed_write:nn
330 % ,\pdf_object_write:nnn
331 % ,\pdf_string_from_unicode:nnN
332 % ,\pdfannot_box_ref_last:
333 % ,\pdfannot_dict_put:nnn
334 % ,\pdfannot_dict_remove:nn
335 % ,\pdfannot_widget_box:nnn
336 % ,\pdfdict_if_empty:nTF
337 % ,\pdfdict_get:nnN
338 % ,\pdfdict_new:n
339 % ,\pdfdict_put:nnn
340 % ,\pdfdict_remove:nn
341 % ,\pdfdict_use:n
342 % ,\pdfmanagement_add:nnn
343 % ,\pdfmeta_standard_verify:nTF
344 % ,\pdfxform_if_exist:nTF
345 % ,\pdfxform_new:nnn
346 % ,\pdfxform_ref:n
347 % ,\phantom
348 % ,\prg_do_nothing:
349 % ,\ProvidesExplPackage
350 % ,\rule
351 % ,\seq_gput_right:Nn
352 % ,\seq_if_exist:NTF
353 % ,\seq_new:N
354 % ,\seq_use:Nn
355 % ,\str_if_empty:NTF
356 % ,\str_if_in:NnTF
357 % ,\str_new:N
358 % ,\strut
359 % ,\strutbox
360 % ,\tl_if_empty:NTF
361 % ,\tl_if_empty:NT
362 % ,\tl_if_empty:NF
363 % ,\tl_put_left:Nn
364 % ,\tl_if_empty:nTF
365 % ,\tl_if_head_eq_charcode:nNTF
366 % ,\tl_new:N
367 % ,\tl_set:Nn
368 % ,\tl_to_str:n
369 % ,\use:c
370 % }
371 % \section{\pkg{l3pdffield-checkbox} Implementation}
372 % \begin{macrocode}
373 %<*package>
374 %<@@=pdffield>
375 % \end{macrocode}
376 % \subsection{Variables}
377 % There are no specific variables.
378 % \subsection{Messages}
379 % There are no specific messages.
380 % \subsection{Appearances}
381 % The default appearances are a quadratic frame with cross (\cs{texttimes}) if
382 % checked.
383 % User appearances should have two versions and follow the naming
384 % module/\meta{name}/Yes and module/\meta{name}/Off.
385 % \begin{macro}[int]
386 % {
387 % \@@_checkbox_default_appearances:,
388 % pdffield/checkbox/default/Yes,
389 % pdffield/checkbox/default/Off
390 % }
391 % This defines the standard appearance. It is setup at the first
392 % use of a checkbox, and will adapt to the font family in use then.
393 % \begin{macrocode}
394 \cs_new_protected:Npn \@@_checkbox_default_appearances:
395 {
396 \pdffield_appearance:nn {pdffield/checkbox/default/Yes}
397 {
398 \normalsize
399 \fboxsep 0pt
400 \framebox
401 [ \dim_eval:n { \box_ht:N\strutbox+\box_dp:N\strutbox } ]
402 { \texttimes \strut }
403 }
404 \pdffield_appearance:nn {pdffield/checkbox/default/Off}
405 {
406 \normalsize
407 \fboxsep 0pt
408 \framebox
409 [ \dim_eval:n { \box_ht:N\strutbox+\box_dp:N\strutbox } ]
410 { \phantom{\texttimes} \strut }
411 }
412 \cs_gset_eq:NN \@@_checkbox_default_appearances: \prg_do_nothing:
413 }
414 % \end{macrocode}
415 % \end{macro}
416 %
417 %\subsection{Creating the field}
418 % A field should be created if the name doesn't exist
419 % \begin{macro}{ \@@_checkbox_field:n }
420 % \begin{macrocode}
421 \cs_new_protected:Npn \@@_checkbox_field:n #1 %name
422 {
423 \pdf_object_if_exist:nF {@@/field/@@/checkbox/#1}
424 {
425 \@@_field:n { @@/checkbox/#1 }
426 }
427 \keys_set:nn {pdffield}{parent={@@/checkbox/#1}}
428 }
429 \cs_generate_variant:Nn \@@_checkbox_field:n {V}
430 % \end{macrocode}
431 % \end{macro}
432 % \subsection{Assembling the checkbox}
433 %
434 % \begin{macro}{\@@_checkbox:n}
435 % \begin{macrocode}
436 \cs_new_protected:Npn \@@_checkbox:n #1
437 {
438 \group_begin:
439 \@@_checkbox_default_appearances:
440 \cs_set_eq:NN\@@_appearance_handler:nnn \@@_checkbox_appearance_handler:nnn
441 \cs_set_eq:NN\@@_value_handler:n \@@_checkbox_value_handler:n
442 \cs_set_eq:NN\@@_default_handler:n \@@_checkbox_default_handler:n
443 % \end{macrocode}
444 % Setting up the defaults.
445 % \begin{macrocode}
446 \keys_set:nn {pdffield}
447 {
448 fieldID=,
449 name=checkbox,
450 appearance = pdffield/checkbox/default,
451 checked=false,
452 width = \normalbaselineskip,
453 height = \normalbaselineskip,
454 }
455 % \end{macrocode}
456 % Value keys should be undefined.
457 % \begin{macrocode}
458 \@@_key_disable:nnn{checkbox}{V}{checked}
459 \@@_key_disable:nnn{checkbox}{DV}{checked}
460 \@@_key_disable:nnn{checkbox}{AS}{checked}
461 \keys_set:nn { pdffield }{@@/preset/checkbox,#1}
462 \keys_set:nn { pdffield }
463 {
464 ,unsetFf={Radio,Pushbutton}
465 ,FT= Btn
466 }
467 \tl_if_empty:NT\l_@@_fieldID_tl
468 {
469 \pdfdict_get:nnN {l_@@/field}{T}\l_@@_fieldID_tl
470 \tl_put_left:Nn \l_@@_fieldID_tl {@@/checkbox/}
471 }
472 \@@_checkbox_field:V\l_@@_fieldID_tl
473 \@@_annot:
474 \group_end:
475 }
476 % \end{macrocode}
477 % \end{macro}
478 %
479 % \subsection{Keys}
480 % Most keys are inherited simply the ones from the generic field and annot keys.
481 % A key to decide if the box is initially checked or not.
482 % We stay in the same family so that we can build a style.
483 % \begin{macro}{checked}
484 % This is a key specific for checkbox, it sets both field and annotation keys.
485 % \begin{macrocode}
486 \keys_define:nn { pdffield }
487 {
488 ,checked .choice:
489 ,checked / false .code:n =
490 {
491 \pdfdict_put:nne { l_@@/field }{V} { /Off }
492 \pdfdict_put:nne { l_@@/field }{DV}{ /Off }
493 \pdfannot_dict_put:nnn {widget}{AS}{ /Off }
494 }
495 ,checked / true .code:n =
496 {
497 \pdfdict_put:nne { l_@@/field }{V} { /Yes }
498 \pdfdict_put:nne { l_@@/field }{DV}{ /Yes }
499 \pdfannot_dict_put:nnn {widget}{AS}{ /Yes }
500 }
501 ,checked .default:n = {true}
502 ,checked .groups:n = {checkbox}
503 }
504 \keys_define:nn { pdffield }
505 {
506 ,__value .choice:
507 ,__value / Off .code:n =
508 {
509 \pdfdict_put:nne { l_@@/field }{V} { /Off }
510 \pdfdict_put:nne { l_@@/field }{DV}{ /Off }
511 \pdfannot_dict_put:nnn {widget}{AS}{ /Off }
512 }
513 ,__value / Yes .code:n =
514 {
515 \pdfdict_put:nne { l_@@/field }{V} { /Yes }
516 \pdfdict_put:nne { l_@@/field }{DV}{ /Yes }
517 \pdfannot_dict_put:nnn {widget}{AS}{ /Yes }
518 }
519 }
520 % \end{macrocode}
521 % \end{macro}
522 % \begin{macro}{\@@_checkbox_value_handler:n,\@@_checkbox_default_handler:n}
523 % |value| and |default| do the same as checked.
524 % \begin{macrocode}
525 \cs_new_protected:Npn \@@_checkbox_value_handler:n #1
526 {
527 \keys_set:nn{pdffield}{__value={#1}}
528 }
529 \cs_new_protected:Npn \@@_checkbox_default_handler:n #1
530 {
531 \keys_set:nn{pdffield}{__value={#1}}
532 }
533 % \end{macrocode}
534 % \end{macro}
535 % \begin{macro}{\@@_checkbox_appearance_handler:nnn}
536 % Appearances must create a dictionary, so we define a special handler.
537 % \Arg{name} is the xform name without the /Yes, /Off, \Arg{type} is N, R, or D,
538 % \Arg{text} is a word for the error message.
539 % \begin{macrocode}
540 \cs_new_protected:Npn \@@_checkbox_appearance_handler:nnn #1 #2 #3 %name, type, text
541 {
542 \pdfxform_if_exist:nTF { #1/Yes }
543 {
544 \pdf_object_if_exist:nF {@@/checkbox/AP/#1}
545 {
546 \pdf_object_new:n {@@/checkbox/AP/#1}
547 \pdf_object_write:nne
548 {@@/checkbox/AP/#1} { dict }
549 {
550 /Yes ~ \pdfxform_ref:n { #1/Yes}
551 /Off ~ \pdfxform_ref:n { #1/Off}
552 }
553 }
554 \pdfannot_dict_put:nne {widget/AP}{#2}{\pdf_object_ref:n{@@/checkbox/AP/#1}}
555 }
556 {
557 \msg_error:nnnn{pdffield}{appearance-missing}{#1}{#3}
558 }
559 }
560
561 % \end{macrocode}
562 %
563 % \end{macro}
564 %
565 % \subsection{user commands}
566 % \begin{macro}{\pdffield_checkbox:n}
567 % \begin{macrocode}
568 \cs_set_eq:NN \pdffield_checkbox:n \@@_checkbox:n
569 %</package>
570 % \end{macrocode}
571 % \end{macro}
572 %\end{implementation}
573
574 % \PrintIndex