Debian Codegraph @

texmf-dist/source/latex/tagpdf/tagpdf-mc-luacode.dtx

raw
1 % \iffalse meta-comment
2 %
3 %% File: tagpdf-mc-luacode.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-mc module (tagpdf)}
30 \begin{document}
31 \DocInput{\jobname.dtx}
32 \end{document}
33 %</driver>
34 % \fi
35 % \title{^^A
36 % The \pkg{tagpdf-mc-luacode} module\\ Code related to Marked Content (mc-chunks), luamode-specific ^^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{implementation}
51 % The code is split into three parts: code shared by all engines,
52 % code specific to luamode and code not used by luamode.
53 %
54 % \section{Marked content code -- luamode code}
55 % luamode uses attributes to mark mc-chunks. The two attributes used are
56 % defined in the backend file. The backend also load the lua file, as it can contain
57 % functions needed elsewhere.
58 % The attributes for mc are global (between 0.6 and 0.81 they were local but this
59 % was reverted). The attributes are setup only in lua, and one should use
60 % the lua functions to set and get them.\\
61 % |g_@@_mc_type_attr|: the value represent the type\\
62 % |g_@@_mc_cnt_attr|: will hold the |\c@g_@@_MCID_abs_int value|
63 %
64 % Handling attribute needs a different system to number the page wise mcid's:
65 % a |\tagmcbegin ... \tagmcend| pair no longer surrounds exactly one mc chunk:
66 % it can be split at page breaks.
67 % We know the included mcid(s) only after the ship out.
68 % So for the |struct -> mcid| mapping we
69 % need to record |struct -> mc-cnt| (in |\g_@@_mc_parenttree_prop| and/or a lua table
70 % and at shipout |mc-cnt-> {mcid, mcid, ...}|
71 % and when building the trees connect both.
72 %
73 % Key definitions are overwritten for luatex to store that data in lua-tables.
74 % The data for the mc are in |ltx.@@.mc[absnum]|.
75 % The fields of the table are:\\
76 % tag : the type (a string)\\
77 % raw : more properties (string)\\
78 % label: a string. \\
79 % artifact: the presence indicates an artifact, the value (string) is the type.\\
80 % kids: a array of tables\\
81 % |{1={kid=num2,page=pagenum1}, 2={kid=num2,page=pagenum2},...}|,\\
82 % this describes the chunks the mc has been split to by the traversing code\\
83 % parent: the number of the structure it is in. Needed to build the parent tree.
84 %
85 % \begin{macrocode}
86 %<@@=tag>
87 %<*luamode>
88 \ProvidesExplPackage {tagpdf-mc-code-lua} {2026-05-17} {1.0c}
89 {tagpdf - mc code only for the luamode }
90 %</luamode>
91 %<*debug>
92 \ProvidesExplPackage {tagpdf-debug-lua} {2026-05-17} {1.0c}
93 {part of tagpdf - debugging code related to marking chunks - lua mode}
94 %</debug>
95 % \end{macrocode}
96 % The main function which wanders through the shipout box to inject the literals.
97 % if the new callback is there, it is used.
98 % \begin{macrocode}
99 %<*luamode>
100 \hook_gput_code:nnn{begindocument}{tagpdf/mc}
101 {
102 \bool_if:NT\g__tag_active_space_bool
103 {
104 \lua_now:e
105 {
106 if~luatexbase.callbacktypes.pre_shipout_filter~then~
107 luatexbase.add_to_callback("pre_shipout_filter", function(TAGBOX)~
108 ltx.@@.func.space_chars_shipout(TAGBOX)~return~true~
109 end, "tagpdf")~
110 if~luatexbase.declare_callback_rule~then~
111 luatexbase.declare_callback_rule("pre_shipout_filter", "luaotfload.dvi", "after", "tagpdf")
112 end~
113 end
114 }
115 \lua_now:e
116 {
117 if~luatexbase.callbacktypes.pre_shipout_filter~then~
118 token.get_next()~
119 end
120 }\@secondoftwo\@gobble
121 {
122 \hook_gput_code:nnn{shipout/before}{tagpdf/lua}
123 {
124 \lua_now:e
125 { ltx.@@.func.space_chars_shipout (tex.box["ShipoutBox"]) }
126 }
127 }
128 }
129 \bool_if:NT\g__tag_active_mc_bool
130 {
131 \lua_now:e
132 {
133 if~luatexbase.callbacktypes.pre_shipout_filter~then~
134 luatexbase.add_to_callback("pre_shipout_filter", function(TAGBOX)~
135 ltx.@@.func.mark_shipout(TAGBOX)~return~true~
136 end, "tagpdf")~
137 end
138 }
139 \lua_now:e
140 {
141 if~luatexbase.callbacktypes.pre_shipout_filter~then~
142 token.get_next()~
143 end
144 }\@secondoftwo\@gobble
145 {
146 \hook_gput_code:nnn{shipout/before}{tagpdf/lua}
147 {
148 \lua_now:e
149 { ltx.@@.func.mark_shipout (tex.box["ShipoutBox"]) }
150 }
151 }
152 }
153 }
154 % \end{macrocode}
155 % \subsection{Commands}
156 % \begin{macro}{\@@_add_missing_mcs_to_stream:Nn}
157 % This command is used in the output routine by the ptagging code.
158 % It should do nothing in luamode.
159 % \begin{macrocode}
160 \cs_new_protected:Npn \@@_add_missing_mcs_to_stream:Nn #1#2 {}
161 \cs_set_eq:NN \tag_mc_add_missing_to_stream:Nn \@@_add_missing_mcs_to_stream:Nn
162 % \end{macrocode}
163 % \end{macro}
164 % \begin{macro}{\tag_mc_new_stream:n}
165 % \begin{macrocode}
166 \cs_new_protected:Npn \tag_mc_new_stream:n #1 {}
167 % \end{macrocode}
168 % \end{macro}
169 % \begin{macro}[pTF]{\@@_mc_if_in:,\tag_mc_if_in:}
170 % This tests, if we are in an mc, for attributes
171 % this means to check against a number.
172 % \begin{macrocode}
173 \prg_new_conditional:Nnn \@@_mc_if_in: {p,T,F,TF}
174 {
175 \int_compare:nNnTF
176 { -2147483647 }
177 =
178 {\lua_now:e
179 {
180 tex.print(\int_use:N \c_document_cctab,tex.getattribute(luatexbase.attributes.g__tag_mc_type_attr))
181 }
182 }
183 { \prg_return_false: }
184 { \prg_return_true: }
185 }
186
187 \prg_new_eq_conditional:NNn \tag_mc_if_in: \@@_mc_if_in: {p,T,F,TF}
188 % \end{macrocode}
189 % \end{macro}
190 %
191 % \begin{macro}
192 % {
193 % \@@_mc_lua_set_mc_type_attr:n
194 % ,\@@_mc_lua_set_mc_type_attr:o
195 % ,\@@_mc_lua_unset_mc_type_attr:
196 % }
197 % This takes a tag name, and sets the attributes globally to the related number.
198 % \begin{macrocode}
199 \cs_new:Nn \@@_mc_lua_set_mc_type_attr:n % #1 is a tag name
200 {
201 %TODO ltx.@@.func.get_num_from("#1") seems not to return a suitable number??
202 \tl_set:Ne\l_@@_tmpa_tl{\lua_now:e{ltx.@@.func.output_num_from ("#1")} }
203 \lua_now:e
204 {
205 tex.setattribute
206 (
207 "global",
208 luatexbase.attributes.g_@@_mc_type_attr,
209 \l_@@_tmpa_tl
210 )
211 }
212 \lua_now:e
213 {
214 tex.setattribute
215 (
216 "global",
217 luatexbase.attributes.g_@@_mc_cnt_attr,
218 \@@_get_mc_abs_cnt:
219 )
220 }
221 }
222
223 \cs_generate_variant:Nn\@@_mc_lua_set_mc_type_attr:n { o }
224
225 \cs_new:Nn \@@_mc_lua_unset_mc_type_attr:
226 {
227 \lua_now:e
228 {
229 tex.setattribute
230 (
231 "global",
232 luatexbase.attributes.g_@@_mc_type_attr,
233 -2147483647
234 )
235 }
236 \lua_now:e
237 {
238 tex.setattribute
239 (
240 "global",
241 luatexbase.attributes.g_@@_mc_cnt_attr,
242 -2147483647
243 )
244 }
245 }
246
247 % \end{macrocode}
248 % \end{macro}
249 % \begin{macro}
250 % {
251 % \@@_mc_insert_mcid_kids:n
252 % ,\@@_mc_insert_mcid_single_kids:n
253 % }
254 % These commands will in the finish code replace the dummy for a mc by the real mcid kids
255 % we need a variant for the case that it is the only kid, to get the array right
256 % \begin{macrocode}
257 \cs_new:Nn \@@_mc_insert_mcid_kids:n
258 {
259 \lua_now:e { ltx.@@.func.mc_insert_kids (#1,0) }
260 }
261
262 \cs_new:Nn \@@_mc_insert_mcid_single_kids:n
263 {
264 \lua_now:e {ltx.@@.func.mc_insert_kids (#1,1) }
265 }
266 % \end{macrocode}
267 % \end{macro}
268 %
269 %\begin{macro}{\@@_mc_handle_stash:n,\@@_mc_handle_stash:e}
270 % This is the lua variant for the command to
271 % put an mcid absolute number in the current structure.
272 % \begin{macrocode}
273 %</luamode>
274 %<*luamode|debug>
275 %<luamode>\cs_new_protected:Npn \@@_mc_handle_stash:n #1 %1 mcidnum
276 %<debug>\cs_set_protected:Npn \@@_mc_handle_stash:n #1 %1 mcidnum
277 {
278 \@@_check_mc_used:n { #1 }
279 \seq_gput_right:cn % Don't fill a lua table due to the command in the item,
280 % so use the kernel command
281 { g_@@_struct_kids_\g_@@_struct_stack_current_tl _seq }
282 {
283 \@@_mc_insert_mcid_kids:n {#1}%
284 }
285 %<debug> \seq_gput_right:cn % Don't fill a lua table due to the command in the item,
286 %<debug> % so use the kernel command
287 %<debug> { g_@@_struct_debug_kids_\g_@@_struct_stack_current_tl _seq }
288 %<debug> {
289 %<debug> MC~#1%
290 %<debug> }
291 \lua_now:e
292 {
293 ltx.@@.func.store_struct_mcabs
294 (
295 \g_@@_struct_stack_current_tl,#1
296 )
297 }
298 }
299 %</luamode|debug>
300 %<*luamode>
301 \cs_generate_variant:Nn \@@_mc_handle_stash:n { e }
302 % \end{macrocode}
303 % \end{macro}
304 %
305 % \begin{macro}{\tag_mc_begin:n}
306 % This is the lua version of the user command.
307 % We currently don't check if there is nesting as it doesn't matter so
308 % much in lua.
309 % \begin{macrocode}
310 \cs_set_protected:Nn \tag_mc_begin:n
311 {
312 \@@_check_if_active_mc:T
313 {
314 \group_begin:
315 %\@@_check_mc_if_nested:
316 \bool_gset_true:N \g_@@_in_mc_bool
317 \bool_set_false:N\l_@@_mc_artifact_bool
318 \tl_clear:N \l_@@_mc_key_properties_tl
319 \int_gincr:N \c@g_@@_MCID_abs_int
320 % \end{macrocode}
321 % set the default tag to the structure:
322 % \begin{macrocode}
323 \tl_set_eq:NN \l_@@_mc_key_tag_tl \g_@@_struct_tag_tl
324 \tl_gset_eq:NN\g_@@_mc_key_tag_tl \g_@@_struct_tag_tl
325 \lua_now:e
326 {
327 ltx.@@.func.store_mc_data(\@@_get_mc_abs_cnt:,"tag","\g_@@_struct_tag_tl")
328 }
329 % \end{macrocode}
330 % 2025-05-23 allow lang on the MC (not really spec conform, but does work in acrobat).
331 % \begin{macrocode}
332 \tl_if_empty:NTF\l_@@_mc_lang_tl
333 {
334 \keys_set:nn { @@ / mc }{ label={}, #1 }
335 }
336 {
337 \keys_set:nn { @@ / mc }{ label={},lang=\l_@@_mc_lang_tl, #1 }
338 }
339 %check that a tag or artifact has been used
340 \@@_check_mc_tag:N \l_@@_mc_key_tag_tl
341 %set the attributes:
342 \@@_mc_lua_set_mc_type_attr:o { \l_@@_mc_key_tag_tl }
343 \bool_if:NF \l_@@_mc_artifact_bool
344 { % store the absolute num name in a label:
345 \tl_if_empty:NF {\l_@@_mc_key_label_tl}
346 {
347 \@@_mc_handle_mc_label:e { \l_@@_mc_key_label_tl }
348 }
349 % if not stashed record the absolute number
350 \bool_if:NF \l_@@_mc_key_stash_bool
351 {
352 \socket_use:nn{tag/check/parent-child}
353 {
354 \@@_mc_check_parent_child:o
355 { \g_@@_struct_stack_current_tl }
356 }
357 \@@_mc_handle_stash:e { \@@_get_mc_abs_cnt: }
358 }
359 }
360 \group_end:
361 }
362 }
363 % \end{macrocode}
364 % \end{macro}
365 % \begin{macro}{\tag_mc_end:}
366 %
367 % TODO: check how the use command must be guarded.
368 % \begin{macrocode}
369 \cs_set_protected:Nn \tag_mc_end:
370 {
371 \@@_check_if_active_mc:T
372 {
373 %\@@_check_mc_if_open:
374 \bool_gset_false:N \g_@@_in_mc_bool
375 \bool_set_false:N\l_@@_mc_artifact_bool
376 \@@_mc_lua_unset_mc_type_attr:
377 \tl_set:Nn \l_@@_mc_key_tag_tl { }
378 \tl_gset:Nn \g_@@_mc_key_tag_tl { }
379 }
380 }
381 % \end{macrocode}
382 % \end{macro}
383 %
384 % \begin{macro}{\tag_mc_reset_box:N}
385 % This allows to reset the mc-attributes in box. On base and generic mode it should do
386 % nothing.
387 % \begin{macrocode}
388 \cs_set_protected:Npn \tag_mc_reset_box:N #1
389 {
390 \lua_now:e
391 {
392 local~type=tex.getattribute(luatexbase.attributes.g_@@_mc_type_attr)
393 local~mc=tex.getattribute(luatexbase.attributes.g_@@_mc_cnt_attr)
394 ltx.__tag.func.update_mc_attributes(tex.getbox(\int_use:N #1),mc,type)
395 }
396 }
397 % \end{macrocode}
398 % \end{macro}
399 %
400 % \begin{macro}{ \@@_get_data_mc_tag: }
401 % The command to retrieve the current mc tag.
402 % TODO: Perhaps this should use the attribute instead.
403 % \begin{macrocode}
404 \cs_new:Npn \@@_get_data_mc_tag: { \g_@@_mc_key_tag_tl }
405 % \end{macrocode}
406 % \end{macro}
407
408 % \subsection{Key definitions}
409 % \begin{macro}
410 % {
411 % tag (mc-key),
412 % raw (mc-key),
413 % alt (mc-key),
414 % lang (mc-key=,
415 % actualtext (mc-key),
416 % label (mc-key),
417 % artifact (mc-key)
418 % }
419 % TODO: check conversion, check if local/global setting is right.
420 % \begin{macrocode}
421 \keys_define:nn { @@ / mc }
422 {
423 tag .code:n = %
424 {
425 \tl_set:Ne \l_@@_mc_key_tag_tl { #1 }
426 \tl_gset:Ne \g_@@_mc_key_tag_tl { #1 }
427 \lua_now:e
428 {
429 ltx.@@.func.store_mc_data(\@@_get_mc_abs_cnt:,"tag","#1")
430 }
431 },
432 raw .code:n =
433 {
434 \tl_put_right:Ne \l_@@_mc_key_properties_tl { #1 }
435 \lua_now:e
436 {
437 ltx.@@.func.store_mc_data(\@@_get_mc_abs_cnt:,"raw","#1")
438 }
439 },
440 alt .code:n = % Alt property
441 {
442 \tl_if_empty:oF{#1}
443 {
444 \str_set_convert:Noon
445 \l_@@_tmpa_str
446 { #1 }
447 { default }
448 { utf16/hex }
449 \tl_put_right:Nn \l_@@_mc_key_properties_tl { /Alt~< }
450 \tl_put_right:No \l_@@_mc_key_properties_tl { \l_@@_tmpa_str>~ }
451 \lua_now:e
452 {
453 ltx.@@.func.store_mc_data
454 (
455 \@@_get_mc_abs_cnt:,"alt","/Alt~<\str_use:N \l_@@_tmpa_str>"
456 )
457 }
458 }
459 },
460 lang .code:n = % Lang property
461 {
462 \tl_if_empty:oF{#1}
463 {
464 \tl_put_right:Ne \l__tag_mc_key_properties_tl { /Lang~(#1) }
465 \lua_now:e
466 {
467 ltx.__tag.func.store_mc_data
468 (
469 \__tag_get_mc_abs_cnt:,"lang","/Lang~(#1)"
470 )
471 }
472 }
473 },
474 alttext .meta:n = {alt=#1},
475 actualtext .code:n = % Alt property
476 {
477 \tl_if_empty:oF{#1}
478 {
479 \str_set_convert:Noon
480 \l_@@_tmpa_str
481 { #1 }
482 { default }
483 { utf16/hex }
484 \tl_put_right:Nn \l_@@_mc_key_properties_tl { /Alt~< }
485 \tl_put_right:No \l_@@_mc_key_properties_tl { \l_@@_tmpa_str>~ }
486 \lua_now:e
487 {
488 ltx.@@.func.store_mc_data
489 (
490 \@@_get_mc_abs_cnt:,
491 "actualtext",
492 "/ActualText~<\str_use:N \l_@@_tmpa_str>"
493 )
494 }
495 }
496 },
497 label .code:n =
498 {
499 \tl_set:Nn\l_@@_mc_key_label_tl { #1 }
500 \lua_now:e
501 {
502 ltx.@@.func.store_mc_data
503 (
504 \@@_get_mc_abs_cnt:,"label","#1"
505 )
506 }
507 },
508 __artifact-store .code:n =
509 {
510 \lua_now:e
511 {
512 ltx.@@.func.store_mc_data
513 (
514 \@@_get_mc_abs_cnt:,"artifact","#1"
515 )
516 }
517 },
518 artifact .code:n =
519 {
520 \exp_args:Nne
521 \keys_set:nn
522 { @@ / mc}
523 { __artifact-bool, __artifact-type=#1, tag=Artifact }
524 \exp_args:Nne
525 \keys_set:nn
526 { @@ / mc }
527 { __artifact-store=\l_@@_mc_artifact_type_tl }
528 },
529 artifact .default:n = { notype }
530 }
531
532 %</luamode>
533 % \end{macrocode}
534 % \end{macro}
535 %
536 % \end{implementation}
537 % \PrintIndex