Debian Codegraph @

texmf-dist/source/latex/pdfmanagement/latex-lab-bookmark.dtx

raw
1 % \iffalse meta-comment
2 %
3 %% File: latex-lab-bookmark.dtx (C) Copyright 2026 LaTeX Project
4 %
5 % It may be distributed and/or modified under the conditions of the
6 % LaTeX Project Public License (LPPL), either version 1.3c of this
7 % license or (at your option) any later version. The latest version
8 % of this license is in the file
9 %
10 % https://www.latex-project.org/lppl.txt
11 %
12 %
13 % The development version of the bundle can be found below
14 %
15 % https://github.com/latex3/latex2e/required/latex-lab
16 %
17 % for those people who are interested or want to report an issue.
18 %
19 \def\ltlabbookmarkdate{2026-02-18}
20 \def\ltlabbookmarkversion{0.95}
21 %<*driver>
22 \DocumentMetadata{tagging=on,pdfstandard=ua-2,testphase=bookmark}
23 \documentclass[kernel]{l3in2edoc}
24 \EnableCrossrefs
25 \CodelineIndex
26 \begin{document}
27 \DocInput{latex-lab-bookmark.dtx}
28 \end{document}
29 %</driver>
30 %
31 % \fi
32 %
33 %
34 % \title{The \textsf{latex-lab-bookmark} package\\
35 % Creating bookmarks }
36 % \author{\LaTeX{} Project\thanks{Initial implementation done by Ulrike Fischer}}
37 % \date{v\ltlabbookmarkversion\ \ltlabbookmarkdate}
38 %
39 % \maketitle
40 %
41 % \newcommand{\TODO}[1]{\textbf{[TODO:} #1\textbf{]}}
42 %
43 % \providecommand\hook[1]{\texttt{#1}}
44 % \begin{documentation}
45 % \begin{abstract}
46 % The following code implements a first draft for the creation of bookmarks
47 % with kernel methods.
48 % \end{abstract}
49 %
50 % \section{Introduction}
51 % This package implements support for the PDF outline aka bookmarks based on
52 % the l3pdfoutline module in the PDF management.
53 % It is based on the
54 % \pkg{bookmark} and the \pkg{hyperref} package and mostly implements the same user commands,
55 % most importantly the main commands |\bookmark| and |\bookmarksetup|.%
56 % \footnote{There is no clash here, as documents can use only one bookmark system: mixing
57 % commands from more than one package or using manually the primitives is not
58 % really possible without breaking the tree.} But it does not try to be a full replacement
59 % and to replicate every option in the same way as the other packages.
60 %
61 % \subsection{Usage}
62 % If \cs{DocumentMetadata} is used, the package should (currently) be loaded additionally
63 % with the |testphase| key:
64 % \begin{verbatim}
65 % \DocumentMetadata{testphase=bookmark}
66 % \documentclass{...}
67 % \end{verbatim}
68 % This will also load all the other tagging support code.
69 %
70 % If only the PDF management is loaded the code should be loaded as package:
71 % \begin{verbatim}
72 % \RequirePackage{pdfmanagement}
73 % \RequirePackage{latex-lab-testphase-bookmark}
74 % \end{verbatim}
75 %
76 % If loaded it will replace the code used for bookmarks in the
77 % \pkg{hyperref} and \pkg{bookmark} packages. It is possible to
78 % manually create bookmarks with this package even if \pkg{hyperref} or \pkg{bookmark}
79 % are not loaded, but then the targets must be created manually too, e.g. with
80 % |\pdf_destination:nn|.
81 %
82 % The package assumes that the input files are UTF-8 encoded!
83 %
84 % \subsection{User commands}
85 %
86 % \begin{function}{\bookmark}
87 % \begin{syntax}
88 % \cs{bookmark}\oarg{keyval options}\Arg{title text}
89 % \end{syntax}
90 %
91 % This is the main command. As keyval options it accepts the keys described below (which are
92 % more or less the same as the keys from the \pkg{bookmark} package).
93 % At least one action key is required!
94 % \end{function}
95 %
96 % \begin{function}{\bookmarksetup}
97 % \begin{syntax}
98 % \cs{bookmarksetup}\Arg{keyval options}
99 % \end{syntax}
100 %
101 % This can be used to change the behaviour of following
102 % bookmarks. It can also be used in the hook |bookmark|.
103 % \end{function}
104 %
105 %
106 % \begin{function}{\bookmarksetupnext}
107 % \begin{syntax}
108 % \cs{bookmarksetupnext}\Arg{keyval options}
109 % \end{syntax}
110 % This is a small wrapper around |\AddToHookNext{bookmark}{\bookmarksetup{#1}}|
111 % and provided for compatibility with the package \pkg{bookmark}. The hook it uses
112 % is executed after the optional argument of |\bookmark| has been processed and so allows
113 % to change the settings done there.
114 % \end{function}
115 %
116 % \begin{function}[EXP]{\bookmarkget}
117 % \begin{syntax}
118 % \cs{bookmarkget}\Arg{option name}
119 % \end{syntax}
120 % In the package \pkg{bookmark} this command allows to retrieve the current value of options.
121 % It is meant to be used inside the hook (as various options are set in a group it wouldn't give
122 % a really sensible output in other places). In \pkg{bookmark} the argument \meta{option name}
123 % can take lots of values but in practice only \texttt{level} has been used to, e.g., make
124 % chapter bookmarks bold. So currently only support for \texttt{level} has been implemented and
125 % all other values returns an empty result.
126 % \end{function}
127 %
128 % \begin{function}{\pdfbookmark,\subpdfbookmark,\belowpdfbookmark,\currentpdfbookmark}
129 % \begin{syntax}
130 % \cs{pdfbookmark}\oarg{level}\Arg{text}\Arg{name}
131 % \end{syntax}
132 %
133 % These commands are defined if hyperref is loaded and work as described in the
134 % hyperref documentation. They all not only create a bookmark with a GoTo link
135 % but also a destination \Arg{name} for this bookmark.
136 % \end{function}
137
138
139 % \subsection{Configuring bookmarks connected to table of contents entries}
140 % Bookmarks can in a document be added by two methods: manually with commands like
141 % \cs{pdfbookmark} or \cs{bookmark} and automatically through heading commands.
142 % For the second method \pkg{hyperref} hooks into the \cs{addcontentsline}
143 % command which means that only headings which can at least potentially appear
144 % in a table of contents are added to the bookmarks --- the standard starred
145 % headings currently not%
146 % \footnote{This will change with the new template implementation of headings.
147 % In this implementation also starred headings can create bookmarks.}
148 % The following keys allow to
149 % configure the bookmarks added with this second method\footnote{As mentioned above
150 % hyperref is currently required for these bookmarks}.
151 %
152 % \begin{description}
153 % \item[bookmarksnumbered, numbered] This allows to decide if the bookmarks show numbers.
154 % The code internally uses a socket |hyp/outline/numberformat|. The predeclared
155 % plugs of these socket redefine \cs{numberline}, \cs{booknumberline},
156 % \cs{partnumberline} and \cs{chapternumberline} to gobble their argument (plug |false|),
157 % or to print it with a following space (plug |true|). Other plugs can be defined
158 % by users or packages and be assigned with |numbered=|\meta{plugname}.
159 %
160 % \item[bookmarkstype,lists] By default \pkg{hyperref} and \pkg{bookmark} add only entries
161 % that go into the main table of contents (|toc|) to the outline. With these keys
162 % this can be changed.
163 % Differently to \pkg{hyperref}, the keys here accepts a list of extensions, so e.g. with
164 % |lists={toc,lof,lot}| it is possible to add entries from all three lists.
165 % \end{description}
166 %
167 %
168 % \subsection{Hooks}
169 % Code to create bookmarks is typically hidden inside other commands. This makes it difficult
170 % to override settings done in their optional argument. There is therefore a hook |bookmark|
171 % to add additional options. It can, for example, change keys if you add a |\bookmarksetup{...}|
172 % to it. This hook is executed after the keys in the optional argument has been processed
173 % and so allows to overwrite them.
174 %
175 % The package \pkg{bookmark} has actually two hooks: one which is filled when using |\bookmarksetupnext|
176 % and one which is filled by the |addtohook| key.
177 % This is different here: there is only one hook and there is no |addtohook| key. The hook
178 % should be filled with the standard commands, e.g., |\AddToHook|.
179 %
180 % As an example the following code will make all bookmarks of level 0 (in a book chapters) bold:
181 % \begin{verbatim}
182 % \AddToHook{bookmark}
183 % {\ifnum\bookmarkget{level}=0 \bookmarksetup{bold}\fi}
184 % \end{verbatim}
185 %
186 % \subsection{Level keys}
187 % While bookmarks use relative levels, a user normally wants to
188 % set levels relative to document sectioning but implementing a suitable
189 % interface is not trivial as bookmarks can't skip levels. So e.g. if
190 % you use in this order a section, a chapter, a part, a chapter, a section,
191 % then the first section, chapter and the part will all be in the bookmarks
192 % in level 1, while the second chapter will be a child of the part and so in level 2,
193 % and the next section in level 3. This is different to the printed
194 % table of contents where both chapters and both sections look alike
195 % and so are perceived to be on the same respective level.
196 %
197 % It is therefore not possible to map the level of a sectioning command (as seen by
198 % a user and by the \texttt{tocdepth} and \texttt{secnumdepth} counter) to a specific level
199 % in the bookmarks.
200 %
201 % The code used here (and also in the bookmark package) tries to address this problem,
202 % by storing for every bookmark not only the actual \emph{bookmark level} but also
203 % the (intended) \emph{document level}. The \emph{document level} of the
204 % last bookmark is the \emph{current document level}. If a new bookmark then requests to be set
205 % in a specific document level, the code looks up the levels of the previous bookmarks
206 % and chooses the best fit as parent. For example, if there is a chapter
207 % (document level 0, bookmark level 1)
208 % with a subsection as child (document level 2, bookmark level 2)
209 % and the document now wants to insert a section bookmark (document level 1), it will insert
210 % it as child of the chapter and sibling of the subsection.
211 %
212 %
213 %
214 % \begin{description}
215 %
216 % \item[level] The value is the requested \emph{document level}. It can be given
217 % as integer expression (positive or negative) or with keywords like \texttt{section}
218 % if a corresponding |\toclevel@section| command exists that expands to an integer.
219 %
220 % If the level value $n$ is larger then the current document level, the bookmark is
221 % inserted as child.
222 % If the two values are equal, the bookmark is inserted as sibling.
223 % If $n$ is smaller it looks up the parents until it finds a bookmark
224 % with a document level equal to $n$ or smaller to $n$. In the first
225 % case it is inserted as a sibling, in the second as a child of this bookmark.
226 %
227 % In all cases the current document level is updated to $n$
228
229 % \item[bookmarksdepth, depth] The value of these keys
230 % decides if a bookmark is shown at all. The value is a \emph{document level}
231 % and accepts the same values as the |level| key. A bookmark is suppressed if
232 % its document level value is larger than the depth value.
233 %
234 % \item[rellevel] This sets the level of a new bookmark in relation to the previous.
235 % It is in such a tree the natural way to set a level and quite easy to use and implement:
236 % a positive value (the actual value doesn't matter) produces a child, zero a sibling,
237 % a negative value goes up by the number of steps given but at most
238 % the number of steps needed to reach the root level. The value will also be used
239 % to update the current document level.
240 %
241 % \item[startatroot] This goes up to the root, the same can be achieved by using |rellevel|
242 % with a large negative number (and this is also how it is implemented).
243 % By default it will set the current document level to zero, the value can be changed
244 % by using |startatroot=$n$|, the setting is suppressed if |keeplevel| has been
245 % set first.
246 %
247 % \end{description}
248 %
249 % \subsection{Expanding commands in the bookmarks}
250 %
251 % Bookmarks can only show simple text. \LaTeX{} commands must there be converted into
252 % something simple. \pkg{hyperref} uses here |\pdfstringdef|. The code here uses
253 % kernel methods, this can lead to different output, e.g., if math is used.
254 % The conversion code can be switched back to |\pdfstringdef| (if hyperref is loaded)
255 % by reassigning a socket plug (the default plug is called |default|):
256 % \begin{verbatim}
257 % \AssignSocketPlug{hyp/outline/title}{pdfstringdef}
258 % \end{verbatim}
259 % Both methods understand the |\texorpdfstring| command.
260 %
261 % \subsection{Actions}
262 %
263 % The following actions are supported by this package (the list is quite
264 % similar to the \pkg{bookmark} package).
265 %
266 % \begin{description}
267 % \item[dest] The value is a destination name. That is the action normally used
268 % in bookmarks. When creating a tagged PDF it will also link to the relevant structure
269 % destination.
270 % \item[page] The value is a (absolute) page number. It creates a destination to
271 % the relevant page. It can be used together with the |gotor| key and will then create
272 % a link to the page in the external PDF. The |view| key can be used to control the view/zoom
273 % of the page. When creating a tagged PDF one should avoid this key for internal links as
274 % the page destination has no associated structure destination.
275 % \item[gotor] The value is the file name of an external PDF (with extension).
276 % \item[named] The value is one of |FirstPage|, |LastPage|, |NextPage|, |PrevPage|.
277 % \item[uri] The value is url. The value is automatically percent encoded. The hash and
278 % percent chars must be properly escaped:
279 % \begin{verbatim}
280 % \bookmark[uri=https://www.example.com\#abc]{uri with hash}
281 % \bookmark[uri=https://www.example.com\%abc]{uri with percent}
282 % \bookmark[uri=https://www.köln.de]{uri with non-ascii char}
283 % \end{verbatim}
284 %
285 % \item[view] The value is one of |Fit|, |FitB|, |FitH|, |FitV|,
286 % |FitBH|, |FitBV| which can be followed by a positive integer (separated by a space) or the
287 % keyword |null|. Or it can be |XYZ|. This can be followed (separated by spaces) by up to two
288 % positive integers and one decimarl or keywords |null| which are then taken as \textit{top left zoom}
289 % in this order. \textit{zoom} is a factor, so e.g. 0.5 will give a scaling of 50\%.
290 % \item[rawaction] This is simply passed into the |/A| of the action. Check the
291 % PDF reference to find out what is possible \ldots.
292 % \end{description}
293 % \end{documentation}
294 % \begin{implementation}
295 % \begin{macrocode}
296 %<@@=hyp>
297 %<*package>
298 % \end{macrocode}
299 %
300 % \begin{macrocode}
301 \ProvidesExplPackage{latex-lab-testphase-bookmark}{2026-05-26}{0.97c}
302 {PDF bookmarks with kernel methods}%
303 % \end{macrocode}
304 % \section{Implementation}
305 %
306 % \subsection{Variables}
307 %
308 % \begin{variable}{\g_@@_outline_currentlevel_int,\g_@@_outline_level_intarray}
309 % While bookmarks use relative levels, user set levels relative to document
310 % sectioning.
311 % \begin{macrocode}
312 \int_new:N \g_@@_outline_currentlevel_int
313 \int_new:N \l_@@_outline_level_int
314 \intarray_new:Nn \g_@@_outline_level_intarray {2000} %TODO size??
315 \intarray_gset:Nnn \g_@@_outline_level_intarray {1}{0}
316 \int_new:N \l_@@_outline_depth_int
317 \tl_new:N \l_@@_outline_title_tl
318 \tl_new:N \l_@@_outline_action_data_tl
319 \tl_new:N \l_@@_outline_action_dest_data_tl
320 \tl_new:N \l_@@_outline_action_view_tl
321 \bool_new:N \l_@@_outline_keeplevel_bool
322 \seq_new:N \l_@@_outline_type_seq
323 \bitset_new:Nn \l_@@_outline_action_bitset
324 {
325 goto = 1,
326 gotor = 2,
327 named = 3,
328 uri = 4,
329 raw = 5,
330 % \end{macrocode}
331 % this two are destination types that apply to both goto and gotor
332 % \begin{macrocode}
333 namedest = 6,
334 pagedest = 7
335 }
336 \bitset_set_true:Nn \l_@@_outline_action_bitset { goto }
337
338 \int_new:N \l_@@_outline_tmpa_int
339 \int_new:N \l_@@_outline_rellevel_tmpa_int
340 \tl_new:N \l_@@_outline_parent_tmpa_tl
341 \seq_new:N \l_@@_outline_tmpa_seq
342 \tl_new:N \l_@@_outline_tmpa_tl
343 \str_new:N \g_@@_outline_tmpa_str
344 % \end{macrocode}
345 % \end{variable}
346 %
347 % \subsection{Converting text}
348 % The title text of the bookmark and other strings must be converted to strings
349 % suitable for a PDF. With hyperref one can use |\pdfstringdef| but
350 % in l3pdftools we also provide a native version which is used by default.
351 % \begin{macrocode}
352 \socket_new:nn {hyp/outline/title}{2}
353 \socket_new_plug:nnn {hyp/outline/title}{pdfstringdef}
354 {
355 \pdfstringdef#2{#1}
356 }
357 % \end{macrocode}
358 %
359 % This uses the pdfmanagement version of \cs{pdfstringdef} defined
360 % in l3pdftools. We use that socket by default.
361 % Ensure that a text without parentheses is produced by using
362 % |string-raw|!
363 % \begin{macrocode}
364 \socket_new_plug:nnn {hyp/outline/title}{default}
365 {
366 \pdf_purify:nN {#1}#2
367 \pdf_string_from_unicode:nVN {utf16/string-raw} #2 #2
368 }
369 \socket_assign_plug:nn {hyp/outline/title}{default}
370 % \end{macrocode}
371 %
372 % \subsection{Regex to check the view value}
373 % A similar regex is used in the generic driver of hyperref and should
374 % perhaps be shared.
375 % \begin{macrocode}
376 \regex_const:Nn \c_@@_outline_dest_startview_regex
377 {
378 \A\ *
379 (?:
380 (?:XYZ (?:\ +(?:(?:\d+|\d*\.\d+)|null)){3}\ )
381 |
382 (?:Fit\b|FitB\b)
383 |
384 (?:(?:FitH|FitV|FitBH|FitBV)(?:\ +(?:\d+|\d*\.\d+)|\ +null){1})
385 |
386 (?:FitR (?:\ +\d+|\ +\d*\.\d+){4}\ )
387 )
388 }
389
390 \msg_new:nnn
391 { hyp /outline }
392 { invalid-view-value }
393 {
394 Invalid~value~'#1'~of~'#2' \\
395 is~replaced~by~'Fit'~\msg_line_context:.
396 }
397
398 % \end{macrocode}
399 %
400 % \subsection{Messages}
401 % \begin{macrocode}
402 \msg_new:nnn {hyp/outline}{no-action}
403 {
404 bookmark~action~missing!\\
405 Use~one~of~'dest',~'gotor',~'named',~'uri',~or~'rawaction'
406 }
407 % \end{macrocode}
408 %
409 % \subsection{Formatting the bookmark}
410 %
411 % \begin{macrocode}
412 \socket_new:nn {hyp/outline/numberformat}{0}
413 \socket_new_plug:nnn {hyp/outline/numberformat}{false}
414 {
415 \text_declare_purify_equivalent:Nn\numberline\use_none:n
416 \text_declare_purify_equivalent:Nn\booknumberline\use_none:n
417 \text_declare_purify_equivalent:Nn\partnumberline\use_none:n
418 \text_declare_purify_equivalent:Nn\chapternumberline\use_none:n
419 \cs_set_eq:NN\numberline\use_none:n
420 \cs_set_eq:NN\booknumberline\use_none:n
421 \cs_set_eq:NN\partnumberline\use_none:n
422 \cs_set_eq:NN\chapternumberline\use_none:n
423 }
424 \cs_new:Npn \@@_outline_use_numberline:n #1 {#1\c_space_tl}
425 \socket_new_plug:nnn {hyp/outline/numberformat}{true}
426 {
427 \text_declare_purify_equivalent:Nn\numberline \@@_outline_use_numberline:n
428 \text_declare_purify_equivalent:Nn\booknumberline \@@_outline_use_numberline:n
429 \text_declare_purify_equivalent:Nn\partnumberline \@@_outline_use_numberline:n
430 \text_declare_purify_equivalent:Nn\chapternumberline\@@_outline_use_numberline:n
431 \cs_set_eq:NN \numberline \@@_outline_use_numberline:n
432 \cs_set_eq:NN\booknumberline \@@_outline_use_numberline:n
433 \cs_set_eq:NN\partnumberline \@@_outline_use_numberline:n
434 \cs_set_eq:NN\chapternumberline\@@_outline_use_numberline:n
435 }
436 % \end{macrocode}
437 %
438 % \subsection{Key definitions}
439 %
440 % \begin{macrocode}
441 \keys_define:nn {hyp/outline}
442 {
443 ,bold .choice:
444 ,bold / true .code:n = \bitset_set_true:Nn \l_pdfoutline_F_bitset {Bold}
445 ,bold /false .code:n = \bitset_set_false:Nn\l_pdfoutline_F_bitset {Bold}
446 ,bold .default:n = true
447 ,italic .choice:
448 ,italic / true .code:n = \bitset_set_true:Nn\l_pdfoutline_F_bitset {Italic}
449 ,italic /false .code:n = \bitset_set_false:Nn\l_pdfoutline_F_bitset {Italic}
450 ,italic .default:n = true
451 ,numbered .code:n =
452 { \socket_assign_plug:nn { hyp/outline/numberformat } {#1}}
453 ,numbered .default:n = true
454 ,numbered .initial:n = false
455 ,bookmarksnumbered .meta:n = {numbered}
456 ,color .tl_set:N = \l_pdfoutline_color_tl
457
458 ,open .bool_set:N = \l_pdfoutline_open_bool
459 % \end{macrocode}
460 % TODO: make this perhaps a bit safer ...
461 % \begin{macrocode}
462 ,depth .code:n =
463 {
464 \cs_if_exist:cTF {toclevel@#1}
465 { \int_set:Nn \l_@@_outline_depth_int { \use:c {toclevel@#1} } }
466 { \int_set:Nn \l_@@_outline_depth_int { #1 } }
467 }
468 ,bookmarksdepth .meta:n = { depth = #1 }
469 ,depth .initial:n = {\int_use:N \c_max_int}
470 % \end{macrocode}
471 % openlevel is relative to the bookmark levels, not some document level!
472 % \begin{macrocode}
473 ,openlevel .int_set:N = \l_pdfoutline_open_int
474 ,bookmarksopenlevel .meta:n = { openlevel = #1 }
475 ,level .code:n =
476 {
477 \cs_if_exist:cTF {toclevel@#1}
478 { \int_set:Nn \l_@@_outline_level_int { \use:c {toclevel@#1} } }
479 { \int_set:Nn \l_@@_outline_level_int { #1 } }
480 }
481 ,rellevel .code:n =
482 {
483 \int_set:Nn
484 \l_@@_outline_level_int
485 { \g_@@_outline_currentlevel_int + #1 }
486 }
487 ,keeplevel .bool_set:N = \l_@@_outline_keeplevel_bool
488 ,startatroot .code:n =
489 {
490 \int_set:Nn \l_@@_outline_level_int { -1000 }
491 \bool_if:NF \l_@@_outline_keeplevel_bool
492 {
493 \int_gset:Nn \g_@@_outline_currentlevel_int {#1}
494 \bool_set_true:NF \l_@@_outline_keeplevel_bool
495 }
496 }
497 ,startatroot .default:n = 0
498 % \end{macrocode}
499 % dest applies to goto or gotor actions.
500 % \begin{macrocode}
501 ,dest .code:n =
502 {
503 \int_compare:nNnTF
504 { \bitset_item:Nn \l_@@_outline_action_bitset {gotor} } = {1}
505 {
506 \bitset_clear:N \l_@@_outline_action_bitset
507 \bitset_set_true:Nn \l_@@_outline_action_bitset { gotor }
508 }
509 {
510 \bitset_clear:N \l_@@_outline_action_bitset
511 \bitset_set_true:Nn \l_@@_outline_action_bitset { goto }
512 }
513 \bitset_set_true:Nn \l_@@_outline_action_bitset { namedest }
514 \tl_set:Nn\l_@@_outline_action_dest_data_tl {#1}
515 }
516 ,dest .default:n = Doc-Start
517 ,goto .code:n =
518 {
519 \bitset_clear:N \l_@@_outline_action_bitset
520 \bitset_set_true:Nn \l_@@_outline_action_bitset { goto }
521 \bitset_set_true:Nn \l_@@_outline_action_bitset { namedest }
522 \tl_set:Nn\l_@@_outline_action_dest_data_tl {#1}
523 }
524 ,gotor .code:n =
525 {
526 \int_compare:nNnTF
527 { \bitset_item:Nn \l_@@_outline_action_bitset {pagedest} } = {1}
528 {
529 \bitset_clear:N \l_@@_outline_action_bitset
530 \bitset_set_true:Nn \l_@@_outline_action_bitset { pagedest }
531 }
532 {
533 \bitset_clear:N \l_@@_outline_action_bitset
534 \bitset_set_true:Nn \l_@@_outline_action_bitset { namedest }
535 }
536 \bitset_set_true:Nn \l_@@_outline_action_bitset { gotor }
537 \tl_set:Nn\l_@@_outline_action_data_tl {#1}
538 }
539 ,named .choices:nn =
540 {FirstPage, NextPage, PrevPage, LastPage}
541 {
542 \bitset_clear:N \l_@@_outline_action_bitset
543 \bitset_set_true:Nn \l_@@_outline_action_bitset { named }
544 \tl_set:Nn \l_@@_outline_action_data_tl {/#1}
545 }
546 % \end{macrocode}
547 % page like dest applies to goto and gotor actions.
548 % \begin{macrocode}
549 ,page .code:n =
550 {
551 \int_compare:nNnTF
552 { \bitset_item:Nn \l_@@_outline_action_bitset {gotor} } = {1}
553 {
554 \bitset_clear:N \l_@@_outline_action_bitset
555 \bitset_set_true:Nn \l_@@_outline_action_bitset { gotor }
556 }
557 {
558 \bitset_clear:N \l_@@_outline_action_bitset
559 \bitset_set_true:Nn \l_@@_outline_action_bitset { goto }
560 }
561 \bitset_set_true:Nn \l_@@_outline_action_bitset { pagedest }
562 \tl_set:Nn \l_@@_outline_action_dest_data_tl {#1}
563 }
564 ,page .default:n = 1
565 ,rawaction .code:n =
566 {
567 \bitset_clear:N \l_@@_outline_action_bitset
568 \bitset_set_true:Nn \l_@@_outline_action_bitset { uri }
569 \tl_set:Nn\l_@@_outline_action_data_tl {#1}
570 }
571 ,view .code:n =
572 {
573 \tl_set:Ne \l_@@_outline_tmpa_tl {#1~null~null~null~}
574 \exp_args:NNV
575 \regex_extract_once:NnNTF
576 \c_@@_outline_dest_startview_regex
577 \l_@@_outline_tmpa_tl
578 \l_@@_outline_tmpa_seq
579 {
580 \tl_set:Ne \l_@@_outline_action_view_tl {/\seq_item:Nn \l_@@_outline_tmpa_seq {1}}
581 }
582 {
583 \msg_warning:nnnn {hyp/outline}{invalid-view-value}{#1}{view}
584 \tl_set:Nn \l_@@_outline_action_view_tl {Fit}
585 }
586 }
587 ,uri .code:n =
588 {
589 \bitset_clear:N \l_@@_outline_action_bitset
590 \bitset_set_true:Nn \l_@@_outline_action_bitset { uri }
591 \pdf_purify:nN{#1}\l_@@_outline_action_data_tl
592 \pdf_string_from_unicode:nVN
593 { utf8/URI } \l_@@_outline_action_data_tl \l_@@_outline_action_data_tl
594 }
595 ,lists .code:n =
596 { \seq_set_from_clist:Nn \l_@@_outline_type_seq { #1 } }
597 ,lists .initial:n = toc
598 ,bookmarkstype .meta:n = {lists={#1}}
599 }
600 % \end{macrocode}
601 % \begin{macro}{\bookmarksetup}
602 % \begin{macrocode}
603 \NewDocumentCommand\bookmarksetup{m}{\keys_set:nn{hyp/outline}{#1}}
604 % \end{macrocode}
605 % \end{macro}
606 %
607 % \begin{macrocode}
608 \NewHook{bookmark}
609 % \end{macrocode}
610 %
611 % \begin{macro}{\bookmarksetupnext}
612 % \begin{macrocode}
613 \NewDocumentCommand\bookmarksetupnext{m}{\AddToHookNext{bookmark}{\bookmarksetup{#1}}}
614 % \end{macrocode}
615 % \end{macro}
616 %
617 % \begin{macro}{\bookmarkget}
618 % \begin{macrocode}
619 \cs_new:Npn\bookmarkget #1 {\use:c{@@_bookmarkget_#1:}}
620 \cs_new:Npn\@@_bookmarkget_level:{\int_use:N\l_@@_outline_level_int}
621 % \end{macrocode}
622 % \end{macro}
623 % \begin{macro}{\bookmark}
624 % \begin{macrocode}
625 \NewDocumentCommand\bookmark{O{}m}
626 {
627 \bool_if:NT \l_pdfoutline_active_bool
628 {
629 \group_begin:
630 \keys_set:nn {hyp/outline} { #1 }
631 \UseHook{bookmark}
632 \socket_use:nnn{hyp/outline/title}{#2}\l_@@_outline_title_tl
633 \int_compare:nNnF {\l_@@_outline_level_int} > {\l_@@_outline_depth_int}
634 {
635 \@@_outline_bookmark_aux:
636 }
637 \group_end:
638 }
639 }
640 % \end{macrocode}
641 % \end{macro}
642 %
643 % \begin{macro}{\@@_outline_bookmark_aux:}
644 % \begin{macrocode}
645 \cs_new_protected:Npn \@@_outline_bookmark_aux:
646 % \end{macrocode}
647 % At first we calculate the rellevel from the requested level.
648 % \begin{macrocode}
649 {
650 \@@_outline_calc_rellevel:NN \l_@@_outline_level_int \l_@@_outline_rellevel_tmpa_int
651 % \end{macrocode}
652 % Update the global current level
653 % \begin{macrocode}
654 \bool_if:NF\l_@@_outline_keeplevel_bool
655 {
656 \int_gset:Nn \g_@@_outline_currentlevel_int { \l_@@_outline_level_int }
657 }
658 \int_compare:nNnTF
659 {
660 \bitset_item:Nn \l_@@_outline_action_bitset {goto} *
661 \bitset_item:Nn \l_@@_outline_action_bitset {namedest}
662 } = {1}
663 {
664 \pdfoutline_goto:nee
665 { \l_@@_outline_rellevel_tmpa_int } %level
666 { \l_@@_outline_action_dest_data_tl }
667 { \l_@@_outline_title_tl }
668 }
669 {
670 \int_case:nnF { \bitset_to_arabic:N \l_@@_outline_action_bitset }
671 {
672 { 65 } % goto (1) + pagedest (65)
673 {
674 \pdfoutline_action:nee
675 { \l_@@_outline_rellevel_tmpa_int }
676 { /S/GoTo~
677 /D
678 [
679 \pdf_pageobject_ref:n { \l_@@_outline_action_dest_data_tl }~
680 \l_@@_outline_action_view_tl
681 ]
682 }
683 { \l_@@_outline_title_tl }
684 }
685 { 34 } % gotor (2) + namedest (32)
686 {
687 \pdfoutline_action:nee
688 { \l_@@_outline_rellevel_tmpa_int }
689 { /S/GoToR~
690 /F ( \l_@@_outline_action_data_tl ) %Todo check format
691 /D ( \l_@@_outline_action_dest_data_tl ) %Todo check format
692 }
693 { \l_@@_outline_title_tl }
694 }
695 { 66 } % gotor (2) + pagedest (64)
696 {
697 \pdfoutline_action:nee
698 { \l_@@_outline_rellevel_tmpa_int }
699 { /S/GoToR~
700 /F ( \l_@@_outline_action_data_tl ) %Todo check format
701 /D
702 [
703 \pdf_pageobject_ref:n { \l_@@_outline_action_dest_data_tl }~
704 \l_@@_outline_action_view_tl
705 ]
706 }
707 { \l_@@_outline_title_tl }
708
709 }
710 { 4 } % named (4)
711 {
712 \pdfoutline_action:nee
713 { \l_@@_outline_rellevel_tmpa_int }
714 { /S/Named~
715 /N \l_@@_outline_action_data_tl %TODO check formt
716 }
717 { \l_@@_outline_title_tl }
718
719 }
720 { 8 } % uri (8)
721 {
722 \pdfoutline_action:nee
723 { \l_@@_outline_rellevel_tmpa_int }
724 { /S/URI~
725 /URI \l_@@_outline_action_data_tl %TODO check formt
726 }
727 { \l_@@_outline_title_tl }
728 }
729 { 16 } % raw (16)
730 {
731 \pdfoutline_action:nee
732 { \l_@@_outline_rellevel_tmpa_int }
733 {
734 \l_@@_outline_action_data_tl %TODO check formt
735 }
736 { \l_@@_outline_title_tl }
737 }
738 }
739 { \msg_error:nn {hyp/outline}{no-action} }
740 }
741 \intarray_gset:Nnn
742 \g_@@_outline_level_intarray
743 { \pdfoutline_id_ref_last: }
744 { \g_@@_outline_currentlevel_int }
745 } %
746 % \end{macrocode}
747 % \end{macro}
748 %
749 %
750 % \begin{macro}{\@@_outline_calc_rellevel:NN}
751 % This function computes the relative level.
752 % \begin{macrocode}
753 \cs_new_protected:Npn\@@_outline_calc_rellevel:NN #1 #2 % #1 the requested level, #2 the return value
754 {
755 \int_compare:nNnTF
756 { #1 }
757 <
758 { \g_@@_outline_currentlevel_int }
759 % \end{macrocode}
760 % if the requested level is lower, we must inspect the parents to find the best fit.
761 % starting with the previous bookmark
762 % \begin{macrocode}
763 {
764 \int_set:Nn #2 { 0 }
765 % \end{macrocode}
766 % get the first parent
767 % \begin{macrocode}
768 \tl_set:Ne \l_@@_outline_parent_tmpa_tl
769 { \pdfoutline_parent_ref:n { \pdfoutline_id_ref_last: } }
770 % \end{macrocode}
771 % get the document level of the first parent.
772 % \begin{macrocode}
773 \int_compare:nNnTF
774 { \l_@@_outline_parent_tmpa_tl } > { 0 }
775 {
776 \int_set:Nn \l_@@_outline_tmpa_int
777 {
778 \intarray_item:Nn
779 \g_@@_outline_level_intarray
780 { \l_@@_outline_parent_tmpa_tl }
781 }
782 }
783 { \int_set_eq:NN \l_@@_outline_tmpa_int #1 }
784 % if \l_@@_outline_tmpa_int = #1
785 % stop, decr rellevel (this sets it as sibling of the parent)
786 % if \l_@@_outline_tmpa_int < #1
787 % stop (this sets it as child of the parent as rellevel is unchanged)
788 % if \l_@@_outline_tmpa_int > #1
789 % get next parent and continue.
790 \int_do_while:nNnn
791 { \l_@@_outline_tmpa_int }
792 >
793 { #1 }
794 {
795 \int_case:nn
796 {
797 \int_sign:n
798 { \l_@@_outline_tmpa_int-#1 }
799 }
800 {
801 { 0 } { \int_decr:N #2 }
802 { 1 }
803 {
804 \int_decr:N #2
805 \tl_set:Ne \l_@@_outline_parent_tmpa_tl
806 { \pdfoutline_parent_ref:n { \l_@@_outline_parent_tmpa_tl } }
807 % \end{macrocode}
808 % We need to check if we reached the root level.
809 % \begin{macrocode}
810 \int_compare:nNnTF
811 { \l_@@_outline_parent_tmpa_tl } > { 1 }
812 {
813 \int_set:Nn \l_@@_outline_tmpa_int
814 {
815 \intarray_item:Nn
816 \g_@@_outline_level_intarray
817 { \l_@@_outline_parent_tmpa_tl }
818 }
819 }
820 {
821 % root
822 \int_set_eq:NN \l_@@_outline_tmpa_int #1
823 }
824 }
825 }
826 }
827 \int_if_zero:nT {\l_@@_outline_tmpa_int-#1}{\int_decr:N #2}
828 }
829 % \end{macrocode}
830 % requested level >= current level is the easy case: we create either a sibling or
831 % a child:
832 % \begin{macrocode}
833 {
834 \int_set:Nn #2
835 { #1 - \g_@@_outline_currentlevel_int }
836 }
837 }
838 % \end{macrocode}
839 % \end{macro}
840 %
841 % \subsection{Replacing hyperref commands}
842 %
843 % \begin{macrocode}
844 \disable@package@load{bookmark}{\RequirePackage{hyperref}}
845 \DeclareHookRule{package/hyperref/after}%
846 {latex-lab-testphase-bookmark}{voids}{latex-lab-testphase-sec-template}
847 \DeclareHookRule{package/hyperref/after}%
848 {latex-lab-testphase-bookmark}{voids}{latex-lab-testphase-sec}
849 \DeclareHookRule{package/hyperref/after}%
850 {latex-lab-testphase-bookmark}{voids}{latex-lab-testphase-toc}
851
852 \AddToHook{package/hyperref/after}
853 {
854 \RemoveFromHook{cmd/addcontentsline/before}[hyp]
855 \AddToHookWithArguments{cmd/addcontentsline/before}[hyp/outline]
856 { \@@_addcontentsline_bookmark:nnn {#1}{#2}{#3} }
857 \providecommand\addcontentslinebookmark{}
858 \providecommand\addcontentslinebookmarkOff{}
859 \RenewCommandCopy\addcontentslinebookmark\@@_addcontentsline_bookmark:nnn
860 \renewcommand\addcontentslinebookmarkOff
861 {
862 \bool_if:NTF \l_pdfoutline_active_bool
863 {
864 \def\addcontentslinebookmarkReset{\bool_set_true:N\l_pdfoutline_active_bool}
865 }
866 {
867 \let\addcontentslinebookmarkReset\relax
868 }
869 \bool_set_false:N\l_pdfoutline_active_bool
870 }
871 \legacy_if:nF{Hy@bookmarks}{\bool_set_false:N\l_pdfoutline_active_bool}
872 \renewcommand*{\pdfbookmark}[3][0]{%
873 \bookmark[level=#1,dest={#3.#1}]{#2}%
874 \hyper@anchorstart{#3.#1}\hyper@anchorend}
875 \def\currentpdfbookmark#1#2#3{%
876 \bookmark[rellevel=0,dest={#3.#1}]{#2}%
877 \hyper@anchorstart{#3.#1}\hyper@anchorend}
878 \def\subpdfbookmark#1#2#3{%
879 \bookmark[rellevel=1,dest={#3.#1}]{#2}%
880 \hyper@anchorstart{#3.#1}\hyper@anchorend}
881 \def\belopdfbookmark#1#2#3{%
882 \bookmark[keeplevel,rellevel=1,dest={#3.#1}]{#2}%
883 \hyper@anchorstart{#3.#1}\hyper@anchorend}
884 % \end{macrocode}
885 % This doesn't yet handle the package options. This must be done in hyperref.
886 % \begin{macrocode}
887 \keys_define:nn{hyp}
888 {bookmarksdepth .meta:nn = {hyp/outline}{depth=#1},
889 bookmarksopen .meta:nn = {hyp/outline}{open},
890 bookmarksopenlevel .meta:nn = {hyp/outline}{openlevel=#1},
891 bookmarksnumbered .meta:nn = {hyp/outline}{numbered}}
892 }
893 \DeclareHookRule{package/hyperref/after}{latex-lab-testphase-bookmark}{after}{latex-lab-testphase-toc}
894 % \end{macrocode}
895 %
896 % \begin{macro}{\@@_addcontentsline_bookmark:nnn}
897 % This is the command used at the begin of \cs{addcontentsline} which creates the bookmark.
898 % \begin{macrocode}
899 \cs_new_protected:Npn \@@_addcontentsline_bookmark:nnn #1 #2 #3 %%#1 toc type, #2 level, #3 content
900 {
901 \seq_if_in:NeT \l_@@_outline_type_seq { #1 }
902 {
903 \tl_if_empty:NT\@currentHref
904 { \MakeLinkTarget[page] {} }
905 \tl_if_exist:cF { toclevel@#2 }
906 {
907 \tl_new:c { toclevel@#2 }
908 \tl_gset:cn { toclevel@#2 } { 0 }
909 % message
910 }
911 \group_begin:
912 \socket_use:n{hyp/outline/numberformat}
913 \bookmark[level=#2,dest={\HyperDestNameFilter{\@currentHref}}]{#3}
914 \group_end:
915 }
916 }
917 % \end{macrocode}
918 % \end{macro}
919 %
920 % \begin{macrocode}
921 %</package>
922 % \end{macrocode}
923
924 % \begin{macrocode}
925 %<*latex-lab>
926 \ProvidesFile{bookmark-latex-lab-testphase.ltx}
927 [\ltlabbookmarkdate\space v\ltlabbookmarkversion\space latex-lab wrapper bookmark]
928 \RequirePackage{latex-lab-testphase-bookmark}
929 %</latex-lab>
930 % \end{macrocode}
931 % \end{implementation}