Debian Codegraph @

texmf-dist/tex/latex/pdfmanagement/lualinksplit.lua

raw
1 -- This is file lualinksplit.lua
2 -- Version: 0.97c, 2026-05-26
3 --
4 -- Copyright (C) 2025 Marcel Krüger, The LaTeX Project
5 --
6 -- It may be distributed and/or modified under the conditions of
7 -- the LaTeX Project Public License (LPPL), either version 1.3c of
8 -- this license or (at your option) any later version. The latest
9 -- version of this license is in the file:
10 --
11 -- https://www.latex-project.org/lppl.txt
12 --
13 -- This file is part of the "LaTeX PDF management bundle" (The Work in LPPL)
14 -- and all files in that bundle must be distributed together.
15
16 --[[
17 This reimplements split links/multiline links with lua.
18 To disable the code remove 'linksplit' from the 'pre_shipout_filter' callback.
19
20 It also changes the working of the linkstate extension which is used in luatex
21 by \pdfannot_link_on: and \pdfannot_link_off:
22
23 The original pdftex and luatex implementations only know on and off for multiline links.
24 In LuaTeX linkstate 1 is off and linkstate 0 is on.
25 This implementation does never actually turn multiline links off, but instead allows
26 for multiple separate "link contexts".
27 So a link started at linkstate 0 only continues while linkstate 0 is in effect but
28 gets suspended if linkstate is set to 1 (similar to the original).
29 But a link started in linkstate 1 (which does very weird things in the original
30 implementation) just works as a normal multiline link while linkstate 1 is in effect,
31 but gets suspended when the linkstate switches to 0. This usually leads to the same results,
32 unless you start multiline links in linkstate 1 in which case it is less broken.
33
34 It also allows to use even more linkstates, effectively allowing each positive number
35 to define a new independent link context. Links in footnotes can be supported by switching the
36 linkstate in a build/column/footnotes socket plug and then assigning this plug:
37
38 \NewSocketPlug{build/column/footnotes}{lualinksplit}{%
39 \setbox\footins=\vbox{\pdfextension linkstate-2\unvbox\footins}%
40 }
41
42 Packages like footmisc that switch this plug should add a link state too if needed.
43
44 linkstate 2 for footnotes just means that in footnotes links started in footnotes continue,
45 but links from the main document (0) and header and footer (1) are suspended.
46
47 Due to the way the implementation works in pdfTeX link settings are weirdly global:
48 They only get set when the box is shipped out, but then stay active until they get changed
49 again. That leads to odd situations if you change them in the middle of a hbox and
50 the box ends with different links then it started.
51 Normally when you set this you only want to set it for the current box though,
52 so most of that never actually happens.
53
54 Therefore the implementation here allows negative numbers which are the same as the
55 positive numbers but only affect the box they get shipped out in.
56 So -2 is the same as 2 except that it automatically resets at the end of the box.
57
58 The kernel currently uses the values 0 (main), 1 (header/footer) and 2 (footnotes).
59
60 --]]
61
62 local traverse = node.traverse
63 local traverse_list = node.traverse_list
64 local copy = node.copy
65 local node_new = node.new
66 local free = node.free
67 local rangedimensions = node.rangedimensions
68 local remove = node.remove
69 local insert_before = node.insert_before
70 local insert_after = node.insert_after
71
72 local hlist, vlist, whatsit = node.id'hlist', node.id'vlist', node.id'whatsit'
73
74 local pdf_start_link, pdf_end_link, pdf_link_state, user_defined = node.subtype'pdf_start_link', node.subtype'pdf_end_link', node.subtype'pdf_link_state', node.subtype'user_defined'
75 local pdf_link_adjust_level = luatexbase.new_whatsit'pdf_link_adjust_level'
76
77 local get_link_state_value, set_link_state_value do
78 local pdf_refobj = node.subtype'pdf_refobj'
79 function get_link_state_value(n)
80 n.subtype = pdf_refobj
81 local value = n.objnum
82 n.subtype = pdf_link_state
83 return value
84 end
85 function set_link_state_value(n, value)
86 n.subtype = pdf_refobj
87 n.objnum = value
88 n.subtype = pdf_link_state
89 end
90 end
91
92 local vmode do
93 local modevalues = tex.getmodevalues()
94 for k, v in pairs(modevalues) do
95 if v == 'vertical' then
96 vmode = k
97 break
98 end
99 end
100 assert(vmode)
101 end
102
103 local whatsits = node.whatsits()
104 local properties = node.get_properties_table()
105 local call_callback = luatexbase.call_callback
106
107 local function start_level(linkstacks, linkstate, level, head)
108 local stack = linkstacks[linkstate]
109 if not stack then return head end
110
111 local new_head = head
112 for i = 1, #stack do
113 local link = stack[i]
114 if link.level == level then
115 local start_link = copy(link.node_template)
116 new_head = insert_before(new_head, head, start_link)
117 properties[start_link] = {linksplit__artificial = true}
118 link.node, link.initial = start_link, false
119 start_link.objnum = pdf.reserveobj'annot'
120 end
121 end
122 return new_head
123 end
124
125 local function end_level(linkstacks, linkstate, level, head, outer)
126 local stack = linkstacks[linkstate]
127 if not stack then return end
128
129 for i = 1, #stack do
130 local link = stack[i]
131 if link.level == level then
132 local start_link = link.node
133 if start_link then
134 local end_link = node_new(whatsit, pdf_end_link)
135 end_link.attr = start_link.attr
136 -- We end the link directly after it's start.
137 -- The real dimensions are given in the start node.
138 insert_after(start_link, start_link, end_link)
139 properties[end_link] = {linksplit__artificial = true}
140 link.node = nil
141 -- Now we need to determine the link width.
142 -- We currently disregard bidi and instead just use the box width
143 -- minus the width of existing content.
144 local pre_link_width = rangedimensions(outer, head, start_link)
145 start_link.width = outer.width - pre_link_width
146
147 call_callback('linksplit', start_link, link.initial and 'initial' or 'middle')
148 end
149 end
150 end
151 end
152
153 local function push_link(linkstacks, linkstate, level, head, node, direction)
154 local stack = linkstacks[linkstate]
155 if not stack then
156 stack = {}
157 linkstacks[linkstate] = stack
158 end
159 stack[#stack + 1] = {
160 node = node,
161 node_template = copy(node),
162 level = level,
163 initial = true,
164 }
165 return head
166 end
167
168 local function pop_link(linkstacks, linkstate, level, head, node)
169 local stack = linkstacks[linkstate]
170 local link_count = stack and #stack
171 if not link_count or link_count == 0 then
172 tex.error("No link here to end")
173 head = remove(head, node)
174 free(node)
175 else
176 local top = stack[link_count]
177 if top.level ~= level then
178 head = remove(head, node)
179 if top.node then
180 insert_after(top.node, top.node, node)
181 else
182 free(node)
183 end
184 tex.error(string.format("Link started on level %i ended on level %i", top.level, level))
185 end
186 free(top.node_template)
187 stack[link_count] = nil
188
189 call_callback('linksplit', top.node, top.initial and 'isolated' or 'final')
190 end
191 return head
192 end
193
194 local process_vlist, process_hlist
195
196 function process_hlist(head, level, linkstacks, linkstate, outer)
197 level = level + 1
198 local real_head = head
199 local used_linkstate = linkstate or linkstacks.linkstate
200 real_head = start_level(linkstacks, used_linkstate, level, head)
201 -- Here we iterate first before we process the previous node.
202 -- This allows a node to remove itself during processing without
203 -- breaking the iteration.
204 local iter, state, n = traverse(head)
205 local next_n, next_id, next_sub = iter(state, n)
206 while next_n do
207 local n, id, sub = next_n, next_id, next_sub
208 next_n, next_id, next_sub = iter(state, n)
209 if id == vlist then
210 process_vlist(n.list, level, linkstacks, linkstate)
211 elseif id == hlist then
212 n.list = process_hlist(n.list, level, linkstacks, linkstate, n)
213 elseif id == whatsit then
214 if sub == pdf_start_link then
215 real_head = push_link(linkstacks, used_linkstate, level, real_head, n, 'TRT') -- FIXME: Direction
216 elseif sub == pdf_end_link then
217 real_head = pop_link(linkstacks, used_linkstate, level, real_head, n)
218 elseif sub == pdf_link_state then
219 texio.write_nl('WARNING: linkstate in hbox ignored')
220 elseif sub == user_defined then
221 if n.user_id == pdf_link_adjust_level then
222 texio.write_nl('WARNING: pdf_link_adjust_level in hbox ignored')
223 end
224 end
225 end
226 end
227 end_level(linkstacks, used_linkstate, level, real_head, outer)
228 return real_head
229 end
230
231 function process_vlist(head, level, linkstacks, linkstate)
232 level = level + 1
233 for n, id, sub in traverse(head) do
234 if id == vlist then
235 process_vlist(n.list, level, linkstacks, linkstate)
236 elseif id == hlist then
237 n.list = process_hlist(n.list, level, linkstacks, linkstate, n)
238 elseif id == whatsit then
239 if sub == pdf_link_state then
240 local value = get_link_state_value(n)
241 if value < 0 then
242 linkstate = -value
243 else
244 linkstacks.linkstate, linkstate = value, nil
245 end
246 elseif sub == pdf_start_link then
247 tex.error("'startlink' ended up in vlist")
248 elseif sub == pdf_end_link then
249 tex.error("'endlink' ended up in vlist")
250 elseif sub == user_defined then
251 if n.user_id == pdf_link_adjust_level then
252 level = level + n.value
253 end
254 end
255 end
256 end
257 return true
258 end
259
260 local linkstacks = {
261 linkstate = 0,
262 }
263
264 luatexbase.create_callback('linksplit', 'simple')
265 luatexbase.add_to_callback('pre_shipout_filter', function(head)
266 process_vlist(head, 0, linkstacks, nil)
267 return true
268 end, 'linksplit')
269
270 local pdflinkadjustlevel_func = luatexbase.new_luafunction'pdflinkadjustlevel'
271 token.set_lua('pdflinkadjustlevel', pdflinkadjustlevel_func, 'protected')
272 lua.get_functions_table()[pdflinkadjustlevel_func] = function()
273 local mode = tex.nest.top.mode
274 if mode ~= vmode and -mode ~= vmode then
275 tex.error("\\pdflinkadjustlevel is only allowed in vmode")
276 return
277 end
278 local value = token.scan_int()
279 local n = node_new(whatsit, user_defined)
280 n.user_id, n.type, n.value = pdf_link_adjust_level, 100, value
281 node.write(n)
282 end