Debian Codegraph @

texmf-dist/source/latex/tagpdf/tagpdf-roles.dtx

raw
1 % \iffalse meta-comment
2 %
3 %% File: tagpdf-roles.dtx
4 %
5 % Copyright (C) 2019-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 "tagpdf 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/tagpdf
22 %
23 % for those people who are interested.
24 %<*driver>
25 \DocumentMetadata{tagging=on}
26 \documentclass{l3in2edoc}
27 \usepackage{array,booktabs,caption}
28 \hypersetup{pdfauthor=Ulrike Fischer,
29 pdftitle=tagpdf-checks module (tagpdf)}
30 \begin{document}
31 \DocInput{\jobname.dtx}
32 \end{document}
33 %</driver>
34 % \fi
35 % \title{^^A
36 % The \pkg{tagpdf-roles} module\\ Tags, roles and namespace code ^^A
37 % }
38 %
39 % \author{^^A
40 % Ulrike Fischer\thanks
41 % {^^A
42 % E-mail:
43 % \href{mailto:[email protected]}
44 % {[email protected]}^^A
45 % }^^A
46 % }
47 %
48 % \date{Version 1.0c, released 2026-05-17}
49 % \maketitle
50 % \begin{documentation}
51 % \begin{function}
52 % {
53 % add-new-tag (setup-key),
54 % tag (rolemap-key),
55 % namespace (rolemap-key),
56 % role (rolemap-key),
57 % role-namespace (rolemap-key),
58 % }
59 % The \texttt{add-new-tag} key can be used in \cs{tagpdfsetup} to declare and rolemap new tags.
60 % It takes as value a key-value list or a simple |new-tag/old-tag|.
61 %
62 % The key-value list knows the following keys:
63 % \begin{description}
64 % \item[\texttt{tag}] This is the name of the new tag as it should
65 % then be used in \cs{tagstructbegin}.
66 % \item[\texttt{namespace}] This is the namespace of the new tag.
67 % The value should be a shorthand of a namespace.
68 % The allowed values are currently |pdf|, |pdf2|, |mathml|,|latex|, |latex-book| and |user|.
69 % The default value (and recommended value for a new tag) is |user|.
70 % The public name of the user namespace is |tag/NS/user|. This can be used to reference
71 % the namespace e.g. in attributes.
72 % \item[\texttt{role}] This is the tag the tag should be mapped too.
73 % In a PDF 1.7 or earlier this is normally a tag from the |pdf| set,
74 % in PDF 2.0 from the |pdf|, |pdf2| and |mathml| set.
75 % It can also be a user tag. The tag must be declared before, as the code retrieves
76 % the class of the new tag from it.
77 % The PDF format allows mapping to be done transitively.
78 % But tagpdf can't/won't check such unusual role mapping.
79 % \item[\texttt{role-namespace}] If the role is a known tag
80 % the default value is the default namespace of this tag.
81 % With this key a specific namespace can be forced.
82 % \end{description}
83 %
84 % Namespaces are mostly a PDF 2.0 property, but it doesn't harm to
85 % set them also in a PDF 1.7 or earlier.
86 % \end{function}
87 %
88 % \begin{function}[TF]{\tag_check_child:nn}
89 % \begin{syntax}
90 % \cs{tag_check_child:nnTF} \Arg{tag} \Arg{namespace} \Arg{true code} \Arg{false code}
91 % \end{syntax}
92 % This checks if the tag \meta{tag} from the name space \meta{namespace}
93 % can be used at the current position. In tagpdf-base it is always true.
94 % \end{function}
95 % \end{documentation}
96 % \begin{implementation}
97 % \begin{macrocode}
98 %<@@=tag>
99 %<*header>
100 \ProvidesExplPackage {tagpdf-roles-code} {2026-05-17} {1.0c}
101 {part of tagpdf - code related to roles and structure names}
102 %</header>
103 % \end{macrocode}
104 % \section{Code related to roles and structure names}
105 % \begin{macrocode}
106 %<*package>
107 % \end{macrocode}
108 %
109 %
110 % \subsection{Variables}
111 % Tags are used in structures (\cs{tagstructbegin}) and mc-chunks (\cs{tagmcbegin}).
112 %
113 % They have a name (a string), in lua a number (for the lua attribute), and
114 % in PDF 2.0 belong to one or more name spaces, with one being the default
115 % name space.
116 %
117 % Tags of structures are classified, e.g. as grouping,
118 % inline or block level structure (and a few special classes like lists and tables),
119 % and must follow containments rules depending on their classification
120 % (for example a inline structure can not contain
121 % a block level structure). New tags inherit their
122 % classification from their rolemapping to the standard namespaces (\texttt{pdf}
123 % and/or \texttt{pdf2}).
124 % We store this classification as it will probably
125 % be needed for tests but currently the data is not much used.
126 % The classification for math (and the containment rules)
127 % is unclear currently and so not set.
128 %
129 % The attribute number is only relevant in lua and only for the MC chunks
130 % (so tags with the same name from different names spaces can have the same number),
131 % and so only stored if luatex is detected.
132 %
133 % Due to the namespaces the storing and processing of tags and there data
134 % are different in various places for PDF~2.0 and PDF~<2.0, which makes
135 % things a bit difficult and leads to some duplications. Perhaps at some time
136 % there should be a clear split.
137 %
138 % This are the main variables used by the code:
139 % \begin{description}
140 % \item[\cs{g_@@_role_tags_NS_prop}]
141 % This is the core list of tag names. It uses tags as keys
142 % and the shorthand (e.g. pdf2, or mathml) of the default name space as value.
143 %
144 % In pdf 2.0 the value is needed in the structure dictionaries.
145 %
146 % \item[\cs{g_@@_role_tags_class_prop}]
147 % This contains for each tag a classification type. It is used in pdf <2.0.
148 %
149 % \item[\cs{g_@@_role_NS_prop}] This contains the names spaces. The
150 % values are the object references. They are used in pdf 2.0.
151 %
152 % \item[\cs{g_@@_role_rolemap_prop}]
153 % This contains for each tag the role to a standard tag.
154 % It is used in pdf<2.0 for tag checking and to fill at the end the
155 % RoleMap dictionary.
156 %
157 % \item[\texttt{g\_@@\_role/RoleMap\_dict}] This dictionary contains
158 % the standard rolemaps. It is relevant only for pdf <2.0.
159 %
160 % \item[\cs{g_@@_role_NS_<ns>_prop}] This prop contains the tags of
161 % a name space and their role. The props are also use for
162 % remapping. As value they contain two brace groups: tag and namespace.
163 % In pdf <2.0 the namespace is empty.
164 %
165 % \item[\cs{g_@@_role_NS_<ns>_class_prop}]
166 % This prop contains the tags of
167 % a name space and their type. The value is only needed for pdf 2.0.
168 %
169 % \item[\cs{g_@@_role_index_prop}]
170 % This prop contains the standard tags (pdf in pdf<2.0,
171 % pdf,pdf2 + mathml in pdf 2.0) as keys, the values are a two-digit
172 % number. These numbers are used to get the containment rule of two tags
173 % from the intarray.
174 %
175 % \end{description}
176 %
177 %
178 % \begin{variable}{\g_@@_role_tags_NS_prop}
179 % This is the core list of tag names. It uses tags as keys
180 % and the shorthand (e.g. pdf2, or mathml) of the default name space as value.
181 % We store the default name space also in pdf <2.0, even if not needed:
182 % it doesn't harm and simplifies the code.
183 % There is no need to access this from lua, so we use the standard prop commands.
184 % \begin{macrocode}
185 \prop_new_linked:N \g_@@_role_tags_NS_prop
186 % \end{macrocode}
187 % \end{variable}
188 %
189 % \begin{variable}{\g_@@_role_tags_class_prop}
190 % With pdf 2.0 we store the class in the NS dependent props.
191 % With pdf <2.0 we store for now the type(s) of a tag in a common
192 % prop.
193 % Tags that are rolemapped should get the type from
194 % the target.
195 % \begin{macrocode}
196 \prop_new:N \g_@@_role_tags_class_prop
197 % \end{macrocode}
198 % \end{variable}
199 %
200 % \begin{variable}{\g_@@_role_NS_prop}
201 % This holds the list of supported name spaces.
202 % The keys are the name tagpdf will use, the values the object reference.
203 % The urls identifier are stored in related dict object.
204 % \begin{description}
205 % \item[mathml] http://www.w3.org/1998/Math/MathML
206 % \item[pdf2] http://iso.org/pdf2/ssn
207 % \item[pdf] http://iso.org/pdf/ssn (default)
208 % \item[user] \cs{c_@@_role_userNS_id_str} (random id, for user tags)
209 % \item[latex] https://www.latex-project.org/ns/dflt
210 % \item[latex-book] https://www.latex-project.org/ns/book
211 % \end{description}
212 % More namespaces are possible and
213 % their objects references and their rolemaps must be collected
214 % so that an array can be written to the StructTreeRoot at the end (see tagpdf-tree).
215 % We use a prop to store the object reference as it will be needed rather
216 % often.
217 % \begin{macrocode}
218 \prop_new:N \g_@@_role_NS_prop
219 % \end{macrocode}
220 % \end{variable}
221 %
222 % \begin{variable}{\g_@@_role_index_prop}
223 % This prop contains the standard tags (pdf in pdf<2.0,
224 % pdf,pdf2 + mathml in pdf 2.0) as keys, the values are a two-digit
225 % number. These numbers are used to get the containment rule of two tags
226 % from the intarray.
227 % \begin{macrocode}
228 \prop_new:N \g_@@_role_index_prop
229 % \end{macrocode}
230 % \end{variable}
231 % \begin{variable}{\l_@@_role_debug_prop}
232 % This variable is used to pass more infos to debug messages.
233 % \begin{macrocode}
234 \prop_new:N \l_@@_role_debug_prop
235 % \end{macrocode}
236 % \end{variable}
237 % We need also a bunch of temporary variables.
238 % \begin{variable}
239 % {
240 % ,\l_@@_role_tag_tmpa_tl
241 % ,\l_@@_role_tag_namespace_tmpa_tl
242 % ,\l_@@_role_tag_namespace_tmpb_tl %
243 % ,\l_@@_role_role_tmpa_tl
244 % ,\l_@@_role_role_namespace_tmpa_tl
245 % ,\l_@@_role_tmpa_seq
246 % }
247 % \begin{macrocode}
248 \tl_new:N \l_@@_role_tag_tmpa_tl
249 \tl_new:N \l_@@_role_tag_namespace_tmpa_tl
250 \tl_new:N \l_@@_role_tag_namespace_tmpb_tl
251 \tl_new:N \l_@@_role_role_tmpa_tl
252 \tl_new:N \l_@@_role_role_namespace_tmpa_tl
253 \seq_new:N\l_@@_role_tmpa_seq
254 % \end{macrocode}
255 % \end{variable}
256 %
257 % \subsection{Namespaces}
258 % The following commands setups a name space.
259 % With pdf version $<$2.0 this is only a prop with the
260 % rolemap. With pdf 2.0 a dictionary must be set up.
261 % Such a name space dictionaries can
262 % contain an optional |/Schema| and |/RoleMapNS| entry. We only reserve the
263 % objects but delay the writing to the finish code, where we can test if the
264 % keys and the name spaces are actually needed.
265 % This commands setups objects for the name space and its rolemap. It also
266 % initialize a dict to collect the rolemaps if needed, and a property
267 % with the tags of the name space and their rolemapping for loops.
268 % It is unclear if a reference to a schema file will be ever needed,
269 % but it doesn't harm \ldots.
270 %
271 % \begin{variable}{g_@@_role/RoleMap_dict,\g_@@_role_rolemap_prop}
272 % This is the object which contains the normal RoleMap. It is probably not
273 % needed in pdf 2.0 but currently kept.
274 % \begin{macrocode}
275 \pdfdict_new:n {g_@@_role/RoleMap_dict}
276 \@@_prop_new:N \g_@@_role_rolemap_prop
277 % \end{macrocode}
278 % \end{variable}
279 %
280 % \begin{function}{\@@_role_NS_new:nnn}
281 % \begin{syntax}
282 % \cs{@@_role_NS_new:nnn} \Arg{shorthand} \Arg{URI-ID} \Arg{Schema}
283 % \end{syntax}
284 % \end{function}
285 % \begin{macro}{\@@_role_NS_new:nnn}
286 % \begin{macrocode}
287 \pdf_version_compare:NnTF < {2.0}
288 {
289 \cs_new_protected:Npn \@@_role_NS_new:nnn #1 #2 #3
290 {
291 \@@_prop_new:c { g_@@_role_NS_#1_prop }
292 \prop_new:c { g_@@_role_NS_#1_class_prop }
293 \prop_gput:Nne \g_@@_role_NS_prop {#1}{}
294 }
295 }
296 {
297 \cs_new_protected:Npn \@@_role_NS_new:nnn #1 #2 #3
298 {
299 \@@_prop_new:c { g_@@_role_NS_#1_prop }
300 \prop_new:c { g_@@_role_NS_#1_class_prop }
301 \pdf_object_new:n {tag/NS/#1}
302 \pdfdict_new:n {g_@@_role/Namespace_#1_dict}
303 \pdf_object_new:n {@@/RoleMapNS/#1}
304 \pdfdict_new:n {g_@@_role/RoleMapNS_#1_dict}
305 \pdfdict_gput:nnn
306 {g_@@_role/Namespace_#1_dict}
307 {Type}
308 {/Namespace}
309 \pdf_string_from_unicode:nnN{utf8/string}{#2}\l_@@_tmpa_str
310 \tl_if_empty:NF \l_@@_tmpa_str
311 {
312 \pdfdict_gput:nne
313 {g_@@_role/Namespace_#1_dict}
314 {NS}
315 {\l_@@_tmpa_str}
316 }
317 %RoleMapNS is added in tree
318 \tl_if_empty:nF {#3}
319 {
320 \pdfdict_gput:nne{g_@@_role/Namespace_#1_dict}
321 {Schema}{#3}
322 }
323 \prop_gput:Nne \g_@@_role_NS_prop {#1}{\pdf_object_ref:n{tag/NS/#1}~}
324 }
325 }
326 % \end{macrocode}
327 % \end{macro}
328 % We need an id for the user space. For the tests it should be possible
329 % to set it to a fix value. So we use random numbers which can
330 % be fixed by setting a seed. We fake a sort of
331 % GUID but do not try to be really exact as it doesn't matter ...
332 %
333 % \begin{variable}{\c_@@_role_userNS_id_str}
334 % \begin{macrocode}
335 \str_const:Ne \c_@@_role_userNS_id_str
336 { data:,
337 \int_to_Hex:n{\int_rand:n {65535}}
338 \int_to_Hex:n{\int_rand:n {65535}}
339 -
340 \int_to_Hex:n{\int_rand:n {65535}}
341 -
342 \int_to_Hex:n{\int_rand:n {65535}}
343 -
344 \int_to_Hex:n{\int_rand:n {65535}}
345 -
346 \int_to_Hex:n{\int_rand:n {16777215}}
347 \int_to_Hex:n{\int_rand:n {16777215}}
348 }
349 % \end{macrocode}
350 % \end{variable}
351 % Now we setup the standard names spaces.
352 % The mathml space is loaded also for pdf < 2.0
353 % but not added to RoleMap unless a boolean is set to true with
354 % |tagpdf-setup{mathml-tags}|.
355 % \begin{macrocode}
356 \bool_new:N \g_@@_role_add_mathml_bool
357 \@@_role_NS_new:nnn {pdf} {http://iso.org/pdf/ssn}{}
358 \@@_role_NS_new:nnn {pdf2} {http://iso.org/pdf2/ssn}{}
359 \@@_role_NS_new:nnn {mathml}{http://www.w3.org/1998/Math/MathML}{}
360 \@@_role_NS_new:nnn {latex} {https://www.latex-project.org/ns/dflt}{}
361 \@@_role_NS_new:nnn {latex-book} {https://www.latex-project.org/ns/book}{}
362 \exp_args:Nne
363 \@@_role_NS_new:nnn {user}{\c_@@_role_userNS_id_str}{}
364 % \end{macrocode}
365 %
366 % \subsection{Adding a new tag}
367 % Both when reading the files and when setting up a tag manually
368 % we have to store data in various places.
369 %
370 % \begin{macro}{\@@_role_alloctag:nnn}
371 % This command allocates a new tag without role mapping. In the
372 % lua backend it will also record the attribute value.
373 % \begin{macrocode}
374 \pdf_version_compare:NnTF < {2.0}
375 {
376 \sys_if_engine_luatex:TF
377 {
378 \cs_new_protected:Npn \@@_role_alloctag:nnn #1 #2 #3 %#1 tagname, ns, type
379 {
380 \lua_now:e { ltx.@@.func.alloctag ('#1') }
381 \prop_gput:Nnn \g_@@_role_tags_NS_prop {#1}{#2}
382 \@@_prop_gput:cnn {g_@@_role_NS_#2_prop} {#1}{{}{}}
383 \prop_gput:Nnn \g_@@_role_tags_class_prop {#1}{#3}
384 \prop_gput:cnn {g_@@_role_NS_#2_class_prop} {#1}{--UNUSED--}
385 }
386 }
387 {
388 \cs_new_protected:Npn \@@_role_alloctag:nnn #1 #2 #3
389 {
390 \prop_gput:Nnn \g_@@_role_tags_NS_prop {#1}{#2}
391 \@@_prop_gput:cnn {g_@@_role_NS_#2_prop} {#1}{{}{}}
392 \prop_gput:Nnn \g_@@_role_tags_class_prop {#1}{#3}
393 \prop_gput:cnn {g_@@_role_NS_#2_class_prop} {#1}{--UNUSED--}
394 }
395 }
396 }
397 {
398 \sys_if_engine_luatex:TF
399 {
400 \cs_new_protected:Npn \@@_role_alloctag:nnn #1 #2 #3 %#1 tagname, ns, type
401 {
402 \lua_now:e { ltx.@@.func.alloctag ('#1') }
403 \prop_gput:Nnn \g_@@_role_tags_NS_prop {#1}{#2}
404 \@@_prop_gput:cnn {g_@@_role_NS_#2_prop} {#1}{{}{}}
405 \prop_gput:Nnn \g_@@_role_tags_class_prop {#1}{--UNUSED--}
406 \prop_gput:cnn {g_@@_role_NS_#2_class_prop} {#1}{#3}
407 }
408 }
409 {
410 \cs_new_protected:Npn \@@_role_alloctag:nnn #1 #2 #3
411 {
412 \prop_gput:Nnn \g_@@_role_tags_NS_prop {#1}{#2}
413 \@@_prop_gput:cnn {g_@@_role_NS_#2_prop} {#1}{{}{}}
414 \prop_gput:Nnn \g_@@_role_tags_class_prop {#1}{--UNUSED--}
415 \prop_gput:cnn {g_@@_role_NS_#2_class_prop} {#1}{#3}
416 }
417 }
418 }
419 \cs_generate_variant:Nn \@@_role_alloctag:nnn {nno}
420 % \end{macrocode}
421 % \end{macro}
422 %
423 % \subsubsection{pdf 1.7 and earlier}
424 %
425 % \begin{macro}{\@@_role_add_tag:nn}
426 % The pdf 1.7 version has only two arguments: new and rolemap name.
427 % The role must be an existing tag and should not be empty.
428 % We allow to change the role of an existing tag: as the rolemap is written
429 % at the end not confusion can happen.
430 % \begin{macrocode}
431 \cs_new_protected:Nn \@@_role_add_tag:nn % (new) name, reference to old
432 {
433 % \end{macrocode}
434 % checks and messages
435 % \begin{macrocode}
436 \@@_check_add_tag_role:nn {#1}{#2}
437 \prop_get:NnNF \g_@@_role_tags_NS_prop {#1}\l_@@_tmp_unused_tl
438 {
439 \int_compare:nNnT {\l_@@_loglevel_int} > { 0 }
440 {
441 \msg_info:nnn { tag }{new-tag}{#1}
442 }
443 }
444 % \end{macrocode}
445 % now the addition
446 % \begin{macrocode}
447 \prop_get:NnNF \g_@@_role_tags_class_prop {#2}\l_@@_tmpa_tl
448 {
449 \tl_set:Nn\l_@@_tmpa_tl{--UNKNOWN--}
450 }
451 \@@_role_alloctag:nno {#1}{user} { \l_@@_tmpa_tl }
452 % \end{macrocode}
453 % We resolve rolemapping recursively so that all targets are stored as standard
454 % tags.
455 % \begin{macrocode}
456 \tl_if_empty:nF { #2 }
457 {
458 \prop_get:NnNTF \g_@@_role_rolemap_prop {#2}\l_@@_tmpa_tl
459 {
460 \@@_prop_gput:Nno \g_@@_role_rolemap_prop {#1}{\l_@@_tmpa_tl}
461 }
462 {
463 \@@_prop_gput:Nne \g_@@_role_rolemap_prop {#1}{\tl_to_str:n{#2}}
464 }
465 }
466 }
467 \cs_generate_variant:Nn \@@_role_add_tag:nn {oo,ne}
468 % \end{macrocode}
469 % \end{macro}
470 %
471 % For the parent-child test we must be able to get the role.
472 % We use the same number of arguments as for the 2.0 command.
473 % If there is no role, we assume a standard tag.
474 % Note: this is quite fast and a move to lua doesn't improve speed.
475 % \begin{macro}{\@@_role_get:nnNN}
476 % \begin{macrocode}
477 \pdf_version_compare:NnT < {2.0}
478 {
479 \cs_new:Npn \@@_role_get:nnNN #1#2#3#4 %#1 tag, #2 NS, #3 tlvar which hold the role tag #4 empty
480 {
481 \prop_get:NnNF \g_@@_role_rolemap_prop {#1}#3
482 {
483 \tl_set:Nn #3 {#1}
484 }
485 \tl_set:Nn #4 {}
486 }
487 \cs_generate_variant:Nn \@@_role_get:nnNN {ooNN}
488 }
489
490 % \end{macrocode}
491 % \end{macro}
492 % \subsubsection{The pdf 2.0 version}
493 % \begin{macro}{\@@_role_add_tag:nnnn}
494 % The pdf 2.0 version takes four arguments:
495 % tag/namespace/role/namespace
496 % \begin{macrocode}
497 \cs_new_protected:Nn \@@_role_add_tag:nnnn %tag/namespace/role/namespace
498 {
499 \@@_check_add_tag_role:nnn {#1/#2}{#3}{#4}
500 \int_compare:nNnT {\l_@@_loglevel_int} > { 0 }
501 {
502 \msg_info:nnn { tag }{new-tag}{#1}
503 }
504 \prop_if_exist:cTF
505 { g_@@_role_NS_#4_class_prop }
506 {
507 \prop_get:cnN { g_@@_role_NS_#4_class_prop } {#3}\l_@@_tmpa_tl
508 \quark_if_no_value:NT \l_@@_tmpa_tl
509 {
510 \tl_set:Nn\l_@@_tmpa_tl{--UNKNOWN--}
511 }
512 }
513 { \tl_set:Nn\l_@@_tmpa_tl{--UNKNOWN--} }
514 \@@_role_alloctag:nno {#1}{#2}{ \l_@@_tmpa_tl }
515 % \end{macrocode}
516 % Do not remap standard tags. TODO add warning?
517 % \begin{macrocode}
518 \tl_if_in:nnF {-pdf-pdf2-mathml-}{-#2-}
519 {
520 \pdfdict_gput:nne {g_@@_role/RoleMapNS_#2_dict}{#1}
521 {
522 [
523 \pdf_name_from_unicode_e:n{#3}
524 \c_space_tl
525 \pdf_object_ref:n {tag/NS/#4}
526 ]
527 }
528 }
529 % \end{macrocode}
530 % We resolve rolemapping recursively so that all targets are stored as standard
531 % tags for the tests.
532 % \begin{macrocode}
533 \tl_if_empty:nF { #2 }
534 {
535 \prop_get:cnN { g_@@_role_NS_#4_prop } {#3}\l_@@_tmpa_tl
536 \quark_if_no_value:NTF \l_@@_tmpa_tl
537 {
538 \@@_prop_gput:cne { g_@@_role_NS_#2_prop } {#1}
539 {{\tl_to_str:n{#3}}{\tl_to_str:n{#4}}}
540 }
541 {
542 \@@_prop_gput:cno { g_@@_role_NS_#2_prop } {#1}{\l_@@_tmpa_tl}
543 }
544 }
545 % \end{macrocode}
546 % We also store into the pdf 1.7 rolemapping so that we can
547 % add that as fallback for pdf 1.7 processor
548 % \begin{macrocode}
549 \bool_if:NT \l__tag_role_update_bool
550 {
551 \tl_if_empty:nF { #3 }
552 {
553 \tl_if_eq:nnF{#1}{#3}
554 {
555 \prop_get:NnN \g_@@_role_rolemap_prop {#3}\l_@@_tmpa_tl
556 \quark_if_no_value:NTF \l_@@_tmpa_tl
557 {
558 \@@_prop_gput:Nne \g_@@_role_rolemap_prop {#1}{\tl_to_str:n{#3}}
559 }
560 {
561 \@@_prop_gput:Nno \g_@@_role_rolemap_prop {#1}{\l_@@_tmpa_tl}
562 }
563 }
564 }
565 }
566 }
567 \cs_generate_variant:Nn \@@_role_add_tag:nnnn {oooo}
568 % \end{macrocode}
569 % \end{macro}
570 %
571 % For the parent-child test we must be able to get the role.
572 % We use the same number of arguments as for the <2.0 command.
573 % Note: this is quite fast and a move to lua doesn't improve speed.
574 % \begin{macro}{\@@_role_get:nnNN}
575 % \begin{macrocode}
576 \pdf_version_compare:NnF < {2.0}
577 {
578 \cs_new:Npn \@@_role_get:nnNN #1#2#3#4
579 %#1 tag, #2 NS,
580 %#3 tlvar which hold the role tag
581 %#4 tlvar which hold the name of the target NS
582 {
583 \prop_if_exist:cTF {g_@@_role_NS_#2_prop}
584 {
585 \prop_get:cnNTF {g_@@_role_NS_#2_prop} {#1}\l_@@_get_tmpc_tl
586 {
587 \tl_set:Ne #3 {\exp_last_unbraced:No\use_i:nn {\l_@@_get_tmpc_tl}}
588 \tl_set:Ne #4 {\exp_last_unbraced:No\use_ii:nn {\l_@@_get_tmpc_tl}}
589 }
590 {
591 \msg_warning:nnn { tag } {role-unknown-tag} { #1 }
592 \tl_set:Nn #3 {#1}
593 \tl_set:Nn #4 {#2}
594 }
595 }
596 {
597 \msg_warning:nnn { tag } {role-unknown-NS} { #2 }
598 \tl_set:Nn #3 {#1}
599 \tl_set:Nn #4 {#2}
600 }
601 }
602 \cs_generate_variant:Nn \@@_role_get:nnNN {ooNN}
603 }
604 % \end{macrocode}
605 % \end{macro}
606 %
607 % \subsection{Helper command to read the data from files}
608 % In this section we setup the helper command to read namespace files.
609
610 % \begin{macro}{\@@_role_read_namespace_line:nw}
611 % This command will process a line in the name space file.
612 % The first argument is the name of the name space.
613 % The definition differ for pdf 2.0. as we have proper name spaces there.
614 % With pdf<2.0 special name spaces shouldn't update the default role or add to the rolemap
615 % again, they only store the values for later uses. We use a boolean here.
616 % \begin{macrocode}
617 \bool_new:N\l_@@_role_update_bool
618 \bool_set_true:N \l_@@_role_update_bool
619 % \end{macrocode}
620 %
621 % \begin{macrocode}
622 \pdf_version_compare:NnTF < {2.0}
623 {
624 \cs_new_protected:Npn \@@_role_read_namespace_line:nw #1#2,#3,#4,#5,#6\q_stop %
625 % #1 NS, #2 tag, #3 rolemap, #4 NS rolemap #5 type
626 {
627 \tl_if_empty:nF { #2 }
628 {
629 \bool_if:NTF \l_@@_role_update_bool
630 {
631 \tl_if_empty:nTF {#5}
632 {
633 \prop_get:NnN \g_@@_role_tags_class_prop {#3}\l_@@_tmpa_tl
634 \quark_if_no_value:NT \l_@@_tmpa_tl
635 {
636 \tl_set:Nn\l_@@_tmpa_tl{--UNKNOWN--}
637 }
638 }
639 {
640 \tl_set:Nn \l_@@_tmpa_tl {#5}
641 }
642 \@@_role_alloctag:nno {#2} {#1} { \l_@@_tmpa_tl }
643 \tl_if_eq:nnF {#2}{#3}
644 {
645 \@@_role_add_tag:nn {#2}{#3}
646 }
647 \@@_prop_gput:cnn {g_@@_role_NS_#1_prop} {#2}{{#3}{}}
648 }
649 {
650 \@@_prop_gput:cnn {g_@@_role_NS_#1_prop} {#2}{{#3}{}}
651 \prop_gput:cnn {g_@@_role_NS_#1_class_prop} {#2}{--UNUSED--}
652 }
653 }
654 }
655 }
656 {
657 \cs_new_protected:Npn \@@_role_read_namespace_line:nw #1#2,#3,#4,#5,#6\q_stop %
658 % #1 NS, #2 tag, #3 rolemap, #4 NS rolemap #5 type
659 {
660 \tl_if_empty:nF {#2}
661 {
662 \tl_if_empty:nTF {#5}
663 {
664 \prop_get:cnN { g_@@_role_NS_#4_class_prop } {#3}\l_@@_tmpa_tl
665 \quark_if_no_value:NT \l_@@_tmpa_tl
666 {
667 \tl_set:Nn\l_@@_tmpa_tl{--UNKNOWN--}
668 }
669 }
670 {
671 \tl_set:Nn \l_@@_tmpa_tl {#5}
672 }
673 \@@_role_alloctag:nno {#2} {#1} { \l_@@_tmpa_tl }
674 \bool_lazy_and:nnT
675 { ! \tl_if_empty_p:n {#3} }{! \str_if_eq_p:nn {#1}{pdf2}}
676 {
677 \@@_role_add_tag:nnnn {#2}{#1}{#3}{#4}
678 }
679 \@@_prop_gput:cnn {g_@@_role_NS_#1_prop} {#2}{{#3}{#4}}
680 }
681 }
682 }
683 % \end{macrocode}
684 % \end{macro}
685 %
686 % \begin{macro}{\@@_role_read_namespace:nn}
687 % This command reads a namespace file in the format
688 % tagpdf-ns-XX.def
689 % \begin{macrocode}
690 \cs_new_protected:Npn \@@_role_read_namespace:nn #1 #2 %name of namespace #2 name of file
691 {
692 \prop_if_exist:cF {g_@@_role_NS_#1_prop}
693 { \msg_warning:nnn {tag}{namespace-unknown}{#1} }
694 \file_if_exist:nTF { tagpdf-ns-#2.def }
695 {
696 \ior_open:Nn \g_tmpa_ior {tagpdf-ns-#2.def}
697 \msg_info:nnn {tag}{read-namespace}{#2}
698 \ior_map_inline:Nn \g_tmpa_ior
699 {
700 \@@_role_read_namespace_line:nw {#1} ##1,,,,\q_stop
701 }
702 \ior_close:N\g_tmpa_ior
703 }
704 {
705 \msg_info:nnn{tag}{namespace-missing}{#2}
706 }
707 }
708
709 % \end{macrocode}
710 % \end{macro}
711 %
712 % \begin{macro}{\@@_role_read_namespace:n}
713 % This command reads the default namespace file.
714 % \begin{macrocode}
715 \cs_new_protected:Npn \@@_role_read_namespace:n #1 %name of namespace
716 {
717 \@@_role_read_namespace:nn {#1}{#1}
718 }
719 % \end{macrocode}
720 % \end{macro}
721 %
722 % \subsection{Reading the default data}
723 % The order is important as we want pdf2 and latex as default: if two
724 % namespace define the same tag, the last one defines which one is used
725 % if the namespace is not explicitly given.
726 % \begin{macrocode}
727 \@@_role_read_namespace:n {pdf}
728 \@@_role_read_namespace:n {pdf2}
729 \@@_role_read_namespace:n {mathml}
730 % \end{macrocode}
731 % in pdf 1.7 the following namespaces should only store
732 % the settings for later use:
733 % \begin{macrocode}
734 \bool_set_false:N\l_@@_role_update_bool
735 \@@_role_read_namespace:n {latex-book}
736 \bool_set_true:N\l_@@_role_update_bool
737 \@@_role_read_namespace:n {latex}
738 \@@_role_read_namespace:nn {latex} {latex-lab}
739 \@@_role_read_namespace:n {pdf}
740 \@@_role_read_namespace:n {pdf2}
741 % \end{macrocode}
742 %
743 % But is the class provides a \cs{chapter} command then we switch
744 % \begin{macrocode}
745 \pdf_version_compare:NnTF < {2.0}
746 {
747 \hook_gput_code:nnn {begindocument}{tagpdf}
748 {
749 \bool_lazy_and:nnT
750 {
751 \cs_if_exist_p:N \chapter
752 }
753 {
754 \cs_if_exist_p:N \c@chapter
755 }
756 {
757 \prop_map_inline:cn{g_@@_role_NS_latex-book_prop}
758 {
759 \@@_role_add_tag:ne {#1}{\use_i:nn #2\c_empty_tl\c_empty_tl}
760 }
761 }
762 }
763 }
764 {
765 \hook_gput_code:nnn {begindocument}{tagpdf}
766 {
767 \bool_lazy_and:nnT
768 {
769 \cs_if_exist_p:N \chapter
770 }
771 {
772 \cs_if_exist_p:N \c@chapter
773 }
774 {
775 \prop_map_inline:cn{g_@@_role_NS_latex-book_prop}
776 {
777 \prop_gput:Nnn \g_@@_role_tags_NS_prop { #1 }{ latex-book }
778 \@@_prop_gput:Nne
779 \g_@@_role_rolemap_prop {#1}{\use_i:nn #2\c_empty_tl\c_empty_tl}
780 }
781 }
782 }
783 }
784 % \end{macrocode}
785 % \subsection{Parent-child rules}
786 % PDF define various rules about which tag can be a child of another tag.
787 % The following code implements the matrix to allow to use it in tests.
788 % \begin{variable}{\g_@@_role_parent_child_intarray}
789 % This intarray will store the rule as a number. For parent nm and child ij
790 % (n,m,i,j digits) the rule is at position nmij. As we have around 56 tags,
791 % we need roughly a size 6000.
792 % \begin{macrocode}
793 \intarray_new:Nn \g_@@_role_parent_child_intarray {6000}
794 % \end{macrocode}
795 % \end{variable}
796 % \begin{macro}{\c_@@_role_rules_prop,\c_@@_role_rules_num_prop}
797 % These two properties map the rule strings to numbers and back.
798 % There are in tagpdf-data.dtx near the csv files for easier maintenance.
799 % \end{macro}
800 %
801 % \begin{macro}{\@@_store_parent_child_rule:nnn}
802 % The helper command is used to store the rule.
803 % It assumes that parent and child are given as 2-digit number!
804 % \begin{macrocode}
805 \sys_if_engine_luatex:TF
806 {
807 \cs_new_protected:Npn \@@_store_parent_child_rule:nnn #1 #2 #3 % num parent, num child, #3 string
808 {
809 \prop_get:NeNTF \c_@@_role_rules_prop{#3} \l_@@_tmp_unused_tl
810 {
811 \intarray_gset:Nnn \g_@@_role_parent_child_intarray
812 { #1#2 }{0\l_@@_tmp_unused_tl}
813 \lua_now:e
814 {
815 ltx.@@.role.matrix[#1] = ltx.@@.role.matrix[#1] or {}
816 ltx.@@.role.matrix[#1][#2] = 0\l_@@_tmp_unused_tl
817 }
818 }
819 {
820 \intarray_gset:Nnn \g_@@_role_parent_child_intarray
821 { #1#2 }{0}
822 \lua_now:e
823 {
824 ltx.@@.role.matrix[#1] = ltx.@@.role.matrix[#1] or {}
825 ltx.@@.role.matrix[#1][#2] = 0
826 }
827 }
828 }
829 }
830 {
831 \cs_new_protected:Npn \@@_store_parent_child_rule:nnn #1 #2 #3 % num parent, num child, #3 string
832 {
833 \prop_get:NeNTF \c_@@_role_rules_prop{#3} \l_@@_tmp_unused_tl
834 {
835 \intarray_gset:Nnn \g_@@_role_parent_child_intarray
836 { #1#2 }{0\l_@@_tmp_unused_tl}
837 }
838 {
839 \intarray_gset:Nnn \g_@@_role_parent_child_intarray
840 { #1#2 }{0}
841 }
842 }
843 }
844 % \end{macrocode}
845 % \end{macro}
846 %
847 % \subsubsection{Reading in the csv-files}
848 % This counter will be used to identify the first (non-comment) line
849 % \begin{macrocode}
850 \int_zero:N \l_@@_tmpa_int
851 % \end{macrocode}
852 % Open the file depending on the PDF version
853 % \begin{macrocode}
854 \pdf_version_compare:NnTF < {2.0}
855 {
856 \ior_open:Nn \g_tmpa_ior {tagpdf-parent-child.csv}
857 }
858 {
859 \ior_open:Nn \g_tmpa_ior {tagpdf-parent-child-2.csv}
860 }
861 % \end{macrocode}
862
863 % Now the main loop over the file
864 % \begin{macrocode}
865 \ior_map_inline:Nn \g_tmpa_ior
866 {
867 % \end{macrocode}
868 % ignore lines containing only comments
869 % \begin{macrocode}
870 \tl_if_empty:nF{#1}
871 {
872 % \end{macrocode}
873 % count the lines ...
874 % \begin{macrocode}
875 \int_incr:N\l_@@_tmpa_int
876 % \end{macrocode}
877 % put the line into a seq. Attention! empty cells are dropped.
878 % \begin{macrocode}
879 \seq_set_from_clist:Nn\l_@@_tmpa_seq { #1 }
880 \int_compare:nNnTF {\l_@@_tmpa_int}=1
881 % \end{macrocode}
882 % This handles the header line. It gives the tags 2-digit numbers.
883 %
884 % \begin{macrocode}
885 {
886 \seq_map_indexed_inline:Nn \l_@@_tmpa_seq
887 {
888 \prop_gput:Nne\g_@@_role_index_prop
889 {##2}
890 {\int_compare:nNnT{##1}<{10}{0}##1}
891 }
892 }
893 % \end{macrocode}
894 % now the data lines.
895 % \begin{macrocode}
896 {
897 \seq_set_from_clist:Nn\l_@@_tmpa_seq { #1 }
898 % \end{macrocode}
899 % get the name of the child tag from the first column
900 % \begin{macrocode}
901 \seq_pop_left:NN\l_@@_tmpa_seq\l_@@_tmpa_tl
902 % \end{macrocode}
903 % get the number of the child, and store it in \cs{l_@@_tmpb_tl}
904 % \begin{macrocode}
905 \prop_get:NoN \g_@@_role_index_prop { \l_@@_tmpa_tl } \l_@@_tmpb_tl
906 % \end{macrocode}
907 % remove column 2+3
908 % \begin{macrocode}
909 \seq_pop_left:NN\l_@@_tmpa_seq\l_@@_tmpa_tl
910 \seq_pop_left:NN\l_@@_tmpa_seq\l_@@_tmpa_tl
911 % \end{macrocode}
912 % Now map over the rest. The index \verb+##1+ gives us the
913 % number of the parent, \verb+##2+ is the data.
914 % \begin{macrocode}
915 \seq_map_indexed_inline:Nn \l_@@_tmpa_seq
916 {
917 \exp_args:Nne
918 \@@_store_parent_child_rule:nnn {##1}{\l_@@_tmpb_tl}{ ##2 }
919 }
920 }
921 }
922 }
923 % \end{macrocode}
924 % close the read handle.
925 % \begin{macrocode}
926 \ior_close:N\g_tmpa_ior
927 % \end{macrocode}
928 % The Root,
929 % Hn and mathml tags are special and need to be added explicitly
930 % \begin{macrocode}
931 \prop_get:NnN\g_@@_role_index_prop{StructTreeRoot}\l_@@_tmpa_tl
932 \prop_gput:Nne\g_@@_role_index_prop{Root}{\l_@@_tmpa_tl}
933 \prop_get:NnN\g_@@_role_index_prop{Hn}\l_@@_tmpa_tl
934 \pdf_version_compare:NnTF < {2.0}
935 {
936 \int_step_inline:nn{6}
937 {
938 \prop_gput:Nne\g_@@_role_index_prop{H#1}{\l_@@_tmpa_tl}
939 }
940 }
941 {
942 \int_step_inline:nn{10}
943 {
944 \prop_gput:Nne\g_@@_role_index_prop{H#1}{\l_@@_tmpa_tl}
945 }
946 % \end{macrocode}
947 % all mathml tags are currently handled identically with the exception of
948 % math and mtext
949 % \begin{macrocode}
950 \prop_get:NnN\g_@@_role_index_prop {mathml}\l_@@_tmpa_tl
951 \prop_get:NnN\g_@@_role_index_prop {math}\l_@@_tmpb_tl
952 \prop_get:NnN\g_@@_role_index_prop {mtext}\l_@@_tmpc_tl
953 \prop_map_inline:Nn \g_@@_role_NS_mathml_prop
954 {
955 \prop_gput:Nno\g_@@_role_index_prop {#1} {\l_@@_tmpa_tl}
956 }
957 \prop_gput:Nno\g_@@_role_index_prop{math}{\l_@@_tmpb_tl}
958 \prop_gput:Nno\g_@@_role_index_prop{mtext}{\l_@@_tmpc_tl}
959 }
960 \sys_if_engine_luatex:T
961 {
962 \prop_map_inline:Nn\g_@@_role_index_prop
963 {
964 \lua_now:e { ltx.@@.role.index['#1']=#2 }
965 }
966 }
967 % \end{macrocode}
968 %
969 % \subsubsection{Retrieving the parent-child rule}
970 %
971 %
972 % \begin{macro}{\@@_role_get_parent_child_rule:nnN}
973 % This command retrieves the rule (as a number) and stores it in the tl-var.
974 % It assumes that the tags in \#1 and \#2 are standard tags after role mapping
975 % for which a rule exist. If the parent is one of Part, Div, NonStruct
976 % the result can be state 7, which means that a check must be
977 % repeated for the \enquote{real parent}.
978 %
979 % TODO check temporary variables. Check if the tl-var should be fix.
980 % \begin{macrocode}
981 \tl_new:N \l_@@_parent_child_check_tl
982 \sys_if_engine_luatex:TF
983 {
984 \cs_new_protected:Npn \@@_role_get_parent_child_rule:nnN #1 #2 #3
985 % #1 parent (string, standard tag after rolemapping!)
986 % #2 child (string, standard tag after rolemapping!)
987 % #3 tl for state
988 {
989 \tl_set:Ne#3
990 {
991 \lua_now:e{tex.print(\int_use:N\c_document_cctab,ltx.@@.func.role_get_parent_child_rule('#1','#2'))}
992 }
993 % \end{macrocode}
994 % Debugging messages, this can perhaps go into debug mode.
995 % \begin{macrocode}
996 \int_compare:nNnT {\l_@@_loglevel_int} > { 0 }
997 {
998 \prop_get:NoNF\c_@@_role_rules_num_prop {#3} \l_@@_tmpa_tl
999 {
1000 \tl_set:Nn \l_@@_tmpa_tl {unknown}
1001 }
1002 \tl_set:Nn \l_@@_tmpb_tl {#1}
1003 \msg_note:nneee
1004 { tag }
1005 { role-parent-child-result }
1006 { #1 }
1007 { #2 }
1008 {
1009 #3~(='\l_@@_tmpa_tl')
1010 }
1011 }
1012 \int_compare:nNnT {#3} = { 0 }
1013 {
1014 \msg_warning:nneee
1015 { tag }
1016 {role-parent-child-result}
1017 { #1 }
1018 { #2 }
1019 { unknown! }
1020 }
1021
1022 }
1023 }
1024 {
1025 \cs_new_protected:Npn \@@_role_get_parent_child_rule:nnN #1 #2 #3
1026 % #1 parent (string, standard tag after rolemapping)
1027 % #2 child (string, standard tag after rolemapping)
1028 % #3 tl for state
1029 {
1030 % \end{macrocode}
1031 %
1032 % \begin{macrocode}
1033 \prop_get:NnN \g_@@_role_index_prop{#1}\l_@@_tmpa_tl
1034 \prop_get:NnN \g_@@_role_index_prop{#2}\l_@@_tmpb_tl
1035 \bool_lazy_and:nnTF
1036 { ! \quark_if_no_value_p:N \l_@@_tmpa_tl }
1037 { ! \quark_if_no_value_p:N \l_@@_tmpb_tl }
1038 {
1039 % \end{macrocode}
1040 % Get the rule from the intarray
1041 % \begin{macrocode}
1042 \tl_set:Ne#3
1043 {
1044 \intarray_item:Nn
1045 \g_@@_role_parent_child_intarray
1046 {\l_@@_tmpa_tl\l_@@_tmpb_tl}
1047 }
1048 }
1049 {
1050 \tl_set:Nn#3 {0}
1051 }
1052 % \end{macrocode}
1053 % Debugging messages, this can perhaps go into debug mode.
1054 % \begin{macrocode}
1055 \int_compare:nNnT {\l_@@_loglevel_int} > { 0 }
1056 {
1057 \prop_get:NoNF\c_@@_role_rules_num_prop {#3} \l_@@_tmpa_tl
1058 {
1059 \tl_set:Nn \l_@@_tmpa_tl {unknown}
1060 }
1061 \tl_set:Nn \l_@@_tmpb_tl {#1}
1062 \msg_note:nneee
1063 { tag }
1064 { role-parent-child-result }
1065 { #1 }
1066 { #2 }
1067 {
1068 #3~(='\l_@@_tmpa_tl')
1069 }
1070 }
1071 \int_compare:nNnT {#3} = { 0 }
1072 {
1073 \msg_warning:nneee
1074 { tag }
1075 {role-parent-child-result}
1076 { #1 }
1077 { #2 }
1078 { unknown! }
1079 }
1080 }
1081 }
1082 \cs_generate_variant:Nn\@@_role_get_parent_child_rule:nnN {ooN}
1083 % \end{macrocode}
1084 % \end{macro}
1085 %
1086 % \begin{macro}{\@@_role_check_parent_child:nnnnN}
1087 % This command rolemaps its arguments and then
1088 % calls \cs{@@_role_get_parent_child_rule:nnN} to retrieve the
1089 % parent-child rule between both.
1090 % It does not try to resolve inheritation rules of \texttt{Part},
1091 % \texttt{Div} and \texttt{NonStruct} but
1092 % instead gives back the state 7. It is then the task
1093 % of the caller command to find the real parent and run the
1094 % check again.
1095 % In pdf 2.0 the name spaces of the tags are relevant, so we
1096 % have arguments for them, but in pdf <2.0 they are ignored and can
1097 % be left empty.
1098 % \begin{macrocode}
1099 \pdf_version_compare:NnTF < {2.0}
1100 {
1101 \cs_new_protected:Npn \@@_role_check_parent_child:nnnnN #1 #2 #3 #4 #5
1102 % #1 parent tag,% not necessarly rolemapped, but often the case
1103 % #2 NS (empty in pdf 1.x)
1104 % #3 child tag, % not necessarly rolemapped, but often the case
1105 % #4 NS (empty in pdf 1.x)
1106 % #5 tl var: to give the result back.
1107 {
1108 % \end{macrocode}
1109 % get the standard tags through rolemapping if needed
1110 % at first the parent
1111 % \begin{macrocode}
1112 \prop_get:NnNTF \g_@@_role_index_prop {#1}\l_@@_tmpa_tl
1113 {
1114 \tl_set:Nn \l_@@_tmpa_tl {#1}
1115 }
1116 {
1117 \prop_get:NnNF \g_@@_role_rolemap_prop {#1}\l_@@_tmpa_tl
1118 {
1119 \tl_set:Nn \l_@@_tmpa_tl {\q_no_value}
1120 }
1121 }
1122 % \end{macrocode}
1123 % now the child
1124 % \begin{macrocode}
1125 \prop_get:NnNTF \g_@@_role_index_prop {#3}\l_@@_tmpb_tl
1126 {
1127 \tl_set:Nn \l_@@_tmpb_tl {#3}
1128 }
1129 {
1130 \prop_get:NnNF \g_@@_role_rolemap_prop {#3}\l_@@_tmpb_tl
1131 {
1132 \tl_set:Nn \l_@@_tmpb_tl {\q_no_value}
1133 }
1134 }
1135 % \end{macrocode}
1136 % if we got tags for parent and child we call the checking command
1137 % \begin{macrocode}
1138 \bool_lazy_and:nnTF
1139 { ! \quark_if_no_value_p:N \l_@@_tmpa_tl }
1140 { ! \quark_if_no_value_p:N \l_@@_tmpb_tl }
1141 {
1142 \@@_role_get_parent_child_rule:ooN
1143 { \l_@@_tmpa_tl}
1144 { \l_@@_tmpb_tl}
1145 #5
1146 }
1147 {
1148 \tl_set:Nn #5 {0}
1149 \msg_warning:nneee
1150 { tag }
1151 {role-parent-child-result}
1152 { #1 }
1153 { #3 }
1154 { unknown! }
1155 }
1156 }
1157 }
1158 % \end{macrocode}
1159 % and now the pdf 2.0 version
1160 % \begin{macrocode}
1161 {
1162 \cs_new_protected:Npn \@@_role_check_parent_child:nnnnN #1 #2 #3 #4 #5 %tag,NS,tag,NS, tl var
1163 {
1164 % \end{macrocode}
1165 % If the namespace is empty, we assume a standard tag,
1166 % otherwise we retrieve the rolemapping from the namespace
1167 % \begin{macrocode}
1168 \tl_if_empty:nTF {#2}
1169 {
1170 \tl_set:Nn \l_@@_tmpa_tl {#1}
1171 }
1172 {
1173 \prop_if_exist:cTF { g_@@_role_NS_#2_prop }
1174 {
1175 \prop_get:cnNTF
1176 { g_@@_role_NS_#2_prop }
1177 {#1}
1178 \l_@@_tmpa_tl
1179 {
1180 \tl_set:Ne \l_@@_tmpa_tl {\tl_head:N\l_@@_tmpa_tl}
1181 \tl_if_empty:NT\l_@@_tmpa_tl
1182 {
1183 \tl_set:Nn \l_@@_tmpa_tl {#1}
1184 }
1185 }
1186 {
1187 \tl_set:Nn \l_@@_tmpa_tl {\q_no_value}
1188 }
1189 }
1190 {
1191 \msg_warning:nnn { tag } {role-unknown-NS} { #2}
1192 \tl_set:Nn \l_@@_tmpa_tl {\q_no_value}
1193 }
1194 }
1195 % \end{macrocode}
1196 % and the same for the child
1197 % If the namespace is empty, we assume a standard tag,
1198 % otherwise we retrieve the rolemapping from the namespace
1199 % \begin{macrocode}
1200 \tl_if_empty:nTF {#4}
1201 {
1202 \tl_set:Nn \l_@@_tmpb_tl {#3}
1203 }
1204 {
1205 \prop_if_exist:cTF { g_@@_role_NS_#4_prop }
1206 {
1207 \prop_get:cnNTF
1208 { g_@@_role_NS_#4_prop }
1209 {#3}
1210 \l_@@_tmpb_tl
1211 {
1212 \tl_set:Ne \l_@@_tmpb_tl { \tl_head:N\l_@@_tmpb_tl }
1213 \tl_if_empty:NT\l_@@_tmpb_tl
1214 {
1215 \tl_set:Nn \l_@@_tmpb_tl {#3}
1216 }
1217 }
1218 {
1219 \tl_set:Nn \l_@@_tmpb_tl {\q_no_value}
1220 }
1221 }
1222 {
1223 \msg_warning:nnn { tag } {role-unknown-NS} { #4}
1224 \tl_set:Nn \l_@@_tmpb_tl {\q_no_value}
1225 }
1226 }
1227 % \end{macrocode}
1228 % and now get the relation
1229 % \begin{macrocode}
1230 \bool_lazy_and:nnTF
1231 { ! \quark_if_no_value_p:N \l_@@_tmpa_tl }
1232 { ! \quark_if_no_value_p:N \l_@@_tmpb_tl }
1233 {
1234 \@@_role_get_parent_child_rule:ooN
1235 { \l_@@_tmpa_tl }
1236 { \l_@@_tmpb_tl }
1237 #5
1238 }
1239 {
1240 \tl_set:Nn #5 {0}
1241 \msg_warning:nneee
1242 { tag }
1243 {role-parent-child-result}
1244 { #2 : #1 }
1245 { #4 : #3 }
1246 { unknown! }
1247 }
1248 }
1249 }
1250 \cs_generate_variant:Nn\@@_role_check_parent_child:nnnnN {oonnN,ooooN}
1251 %</package>
1252 % \end{macrocode}
1253 % \end{macro}
1254 %
1255 % \begin{macro}[TF]{\tag_check_child:nn}
1256 % \begin{macrocode}
1257 %<base>\prg_new_protected_conditional:Npnn \tag_check_child:nn #1 #2 {T,F,TF}{\prg_return_true:}
1258 %<*package>
1259 \prg_set_protected_conditional:Npnn \tag_check_child:nn #1 #2 {T,F,TF} %#1 tag, #2 NS
1260 {
1261 \seq_get:NN\g_@@_struct_stack_seq\l_@@_tmpa_tl
1262 \@@_struct_get_role:enNN
1263 {\l_@@_tmpa_tl}
1264 {rolemap}
1265 \l_@@_get_parent_tmpa_tl
1266 \l_@@_get_parent_tmpb_tl
1267 \@@_role_check_parent_child:oonnN
1268 { \l_@@_get_parent_tmpa_tl }
1269 { \l_@@_get_parent_tmpb_tl }
1270 {#1}{#2}
1271 \l_@@_parent_child_check_tl
1272 \int_compare:nNnT {\l_@@_parent_child_check_tl} = { \c_@@_role_rule_checkparent_tl }
1273 {
1274 \seq_get:NN\g_@@_struct_stack_seq\l_@@_tmpa_tl
1275 \@@_struct_get_role:enNN
1276 {\l_@@_tmpa_tl}
1277 {parentrole}
1278 \l_@@_get_parent_tmpa_tl
1279 \l_@@_get_parent_tmpb_tl
1280 \@@_role_check_parent_child:oonnN
1281 { \l_@@_get_parent_tmpa_tl }
1282 { \l_@@_get_parent_tmpb_tl }
1283 {#1}{#2}
1284 \l_@@_parent_child_check_tl
1285 }
1286 \int_compare:nNnTF { \l_@@_parent_child_check_tl } < {0}
1287 {\prg_return_false:}
1288 {\prg_return_true:}
1289 }
1290 % \end{macrocode}
1291 % \end{macro}
1292 %
1293 %
1294 % \subsection{Key-val user interface}
1295 % The user interface uses the key |add-new-tag|, which takes either a
1296 % keyval list as argument, or a tag/role.
1297 % \begin{macro}
1298 % {
1299 % tag (rolemap-key),
1300 % tag-namespace (rolemap-key),
1301 % role (rolemap-key),
1302 % role-namespace (rolemap-key),
1303 % role/new-tag (setup-key),
1304 % add-new-tag (deprecated)}
1305 % \begin{macrocode}
1306 \keys_define:nn { @@ / tag-role }
1307 {
1308 ,tag .tl_set:N = \l_@@_role_tag_tmpa_tl
1309 ,tag-namespace .tl_set:N = \l_@@_role_tag_namespace_tmpa_tl
1310 ,role .tl_set:N = \l_@@_role_role_tmpa_tl
1311 ,role-namespace .tl_set:N = \l_@@_role_role_namespace_tmpa_tl
1312 }
1313
1314 \keys_define:nn { @@ / setup }
1315 {
1316 role/mathml-tags .bool_gset:N = \g_@@_role_add_mathml_bool
1317 ,role/new-tag .code:n =
1318 {
1319 \keys_set_known:nnnN
1320 {@@/tag-role}
1321 {
1322 tag-namespace=user,
1323 role-namespace=, %so that we can test for it.
1324 #1
1325 }{@@/tag-role}\l_@@_tmpa_tl
1326 \tl_if_empty:NF \l_@@_tmpa_tl
1327 {
1328 \exp_args:NNno \seq_set_split:Nnn \l_@@_tmpa_seq { / } {\l_@@_tmpa_tl/}
1329 \tl_set:Ne \l_@@_role_tag_tmpa_tl { \seq_item:Nn \l_@@_tmpa_seq {1} }
1330 \tl_set:Ne \l_@@_role_role_tmpa_tl { \seq_item:Nn \l_@@_tmpa_seq {2} }
1331 }
1332 \tl_if_empty:NT \l_@@_role_role_namespace_tmpa_tl
1333 {
1334 \prop_get:NoNTF
1335 \g_@@_role_tags_NS_prop
1336 { \l_@@_role_role_tmpa_tl }
1337 \l_@@_role_role_namespace_tmpa_tl
1338 {
1339 \prop_get:NoNF
1340 \g_@@_role_NS_prop
1341 { \l_@@_role_role_namespace_tmpa_tl }
1342 \l_@@_tmp_unused_tl
1343 {
1344 \tl_set:Nn \l_@@_role_role_namespace_tmpa_tl {user}
1345 }
1346 }
1347 {
1348 \tl_set:Nn \l_@@_role_role_namespace_tmpa_tl {user}
1349 }
1350 }
1351 \pdf_version_compare:NnTF < {2.0}
1352 {
1353 %TODO add check for emptyness?
1354 \@@_role_add_tag:oo
1355 { \l_@@_role_tag_tmpa_tl }
1356 { \l_@@_role_role_tmpa_tl }
1357 }
1358 {
1359 \@@_role_add_tag:oooo
1360 { \l_@@_role_tag_tmpa_tl }
1361 { \l_@@_role_tag_namespace_tmpa_tl }
1362 { \l_@@_role_role_tmpa_tl }
1363 { \l_@@_role_role_namespace_tmpa_tl }
1364 }
1365 }
1366 ,role/map-tags .choice:
1367 ,role/map-tags/false .code:n = { \socket_assign_plug:nn { tag/struct/tag } {latex-tags} }
1368 ,role/map-tags/pdf .code:n = { \socket_assign_plug:nn { tag/struct/tag } {pdf-tags} }
1369 % \end{macrocode}
1370 % \changes{0.99l}{2025-01-12}{allow setting of user-NS}
1371 % \begin{macrocode}
1372 ,role/user-NS .code:n =
1373 {
1374 \pdf_version_compare:NnF < {2.0}
1375 {
1376 \pdf_string_from_unicode:nnN{utf8/string}{https://www.latex-project.org/ns/local/#1}\l_@@_tmpa_str
1377 \tl_if_empty:NF \l_@@_tmpa_str
1378 {
1379 \pdfdict_gput:nne
1380 {g_@@_role/Namespace_user_dict}
1381 {NS}
1382 {\l_@@_tmpa_str}
1383 }
1384 }
1385 }
1386 % \end{macrocode}
1387 % deprecated names
1388 % \begin{macrocode}
1389 , mathml-tags .bool_gset:N = \g_@@_role_add_mathml_bool
1390 , add-new-tag .meta:n = {role/new-tag={#1}}
1391 }
1392 %</package>
1393 % \end{macrocode}
1394 % \end{macro}
1395 % \end{implementation}
1396 % \PrintIndex