| 1 |
-- |
| 2 |
-- This is file `luamathalign.lua', |
| 3 |
-- generated with the docstrip utility. |
| 4 |
-- |
| 5 |
-- The original source files were: |
| 6 |
-- |
| 7 |
-- luamathalign.dtx (with options: `lua') |
| 8 |
-- |
| 9 |
-- Copyright (C) 2019--2022 by Marcel Krueger |
| 10 |
-- |
| 11 |
-- This file may be distributed and/or modified under the |
| 12 |
-- conditions of the LaTeX Project Public License, either |
| 13 |
-- version 1.3c of this license or (at your option) any later |
| 14 |
-- version. The latest version of this license is in: |
| 15 |
-- |
| 16 |
-- http://www.latex-project.org/lppl.txt |
| 17 |
-- |
| 18 |
-- and version 1.3 or later is part of all distributions of |
| 19 |
-- LaTeX version 2005/12/01 or later. |
| 20 |
local properties = node.get_properties_table() |
| 21 |
local luacmd = require'luamathalign-luacmd' |
| 22 |
local hlist = node.id'hlist' |
| 23 |
local vlist = node.id'vlist' |
| 24 |
local whatsit = node.id'whatsit' |
| 25 |
local glue = node.id'glue' |
| 26 |
local user_defined = node.subtype'user_defined' |
| 27 |
local whatsit_id = luatexbase.new_whatsit'mathalign' |
| 28 |
local node_cmd = token.command_id'node' |
| 29 |
local ampersand = token.new(38, 4) |
| 30 |
|
| 31 |
local mmode do |
| 32 |
for k,v in next, tex.getmodevalues() do |
| 33 |
if v == 'math' then mmode = k end |
| 34 |
end |
| 35 |
assert(mmode) |
| 36 |
end |
| 37 |
|
| 38 |
-- We might want to add y later |
| 39 |
local function is_marked(mark, list) |
| 40 |
for n in node.traverse(list) do |
| 41 |
local id = n.id |
| 42 |
if id == hlist or id == vlist then |
| 43 |
if is_marked(mark, n.head) then return true end |
| 44 |
elseif id == whatsit and n.subtype == user_defined |
| 45 |
and n.user_id == whatsit_id and n.value == mark then |
| 46 |
return true |
| 47 |
end |
| 48 |
end |
| 49 |
return false |
| 50 |
end |
| 51 |
local function assert_unmarked(mark, list, ...) |
| 52 |
local marked = is_marked(mark, list) |
| 53 |
if marked then |
| 54 |
tex.error("Multiple alignment marks", "I found multiple alignment marks \z |
| 55 |
of type " .. mark .. " in an alignment where I already had an \z |
| 56 |
alignment mark of that type. You should look at both of them and \z |
| 57 |
decide which one is right. I will continue with the first one for now.") |
| 58 |
end |
| 59 |
return ... |
| 60 |
end |
| 61 |
local measure do |
| 62 |
local vmeasure |
| 63 |
local function hmeasure(mark, list) |
| 64 |
local x, last = 0, list.head |
| 65 |
for n in node.traverse(last) do |
| 66 |
local id = n.id |
| 67 |
if id == hlist then |
| 68 |
local w, h, d = node.rangedimensions(list, last, n) |
| 69 |
x, last = x + w, n |
| 70 |
local dx = hmeasure(mark, n) |
| 71 |
if dx then return assert_unmarked(mark, n.next, dx + x) end |
| 72 |
elseif id == vlist then |
| 73 |
local w, h, d = node.rangedimensions(list, last, n) |
| 74 |
x, last = x + w, n |
| 75 |
local dx = vmeasure(mark, n) |
| 76 |
if dx then return assert_unmarked(mark, n.next, dx + x) end |
| 77 |
elseif id == whatsit and n.subtype == user_defined |
| 78 |
and n.user_id == whatsit_id and n.value == mark then |
| 79 |
local w, h, d = node.rangedimensions(list, last, n) |
| 80 |
local after |
| 81 |
list.head, after = node.remove(list.head, n) |
| 82 |
return assert_unmarked(mark, after, x + w) |
| 83 |
end |
| 84 |
end |
| 85 |
end |
| 86 |
function vmeasure(mark, list) |
| 87 |
for n in node.traverse(list.head) do |
| 88 |
local id = n.id |
| 89 |
if id == hlist then |
| 90 |
local dx = hmeasure(mark, n) |
| 91 |
if dx then return assert_unmarked(mark, n.next, dx + n.shift) end |
| 92 |
elseif id == vlist then |
| 93 |
local dx = vmeasure(mark, n) |
| 94 |
if dx then return assert_unmarked(mark, n.next, dx + n.shift) end |
| 95 |
elseif id == whatsit and n.subtype == user_defined |
| 96 |
and n.user_id == whatsit_id and n.value == mark then |
| 97 |
local after |
| 98 |
list.head, after = node.remove(list.head, n) |
| 99 |
return assert_unmarked(mark, after, 0) |
| 100 |
end |
| 101 |
end |
| 102 |
end |
| 103 |
function measure(mark, head) |
| 104 |
local x, last = 0, head |
| 105 |
for n in node.traverse(last) do |
| 106 |
local id = n.id |
| 107 |
if id == hlist then |
| 108 |
local w, h, d = node.dimensions(last, n) |
| 109 |
x, last = x + w, n |
| 110 |
local dx = hmeasure(mark, n) |
| 111 |
if dx then return assert_unmarked(mark, n.next, head, dx + x) end |
| 112 |
elseif id == vlist then |
| 113 |
local w, h, d = node.dimensions(last, n) |
| 114 |
x, last = x + w, n |
| 115 |
local dx = vmeasure(mark, n) |
| 116 |
if dx then return assert_unmarked(mark, n.next, head, dx + x) end |
| 117 |
elseif id == whatsit and n.subtype == user_defined |
| 118 |
and n.user_id == whatsit_id and n.value == mark then |
| 119 |
local w, h, d = node.dimensions(last, n) |
| 120 |
local after |
| 121 |
head, after = node.remove(head, n) |
| 122 |
return assert_unmarked(mark, after, head, x + w) |
| 123 |
end |
| 124 |
end |
| 125 |
return head |
| 126 |
end |
| 127 |
end |
| 128 |
|
| 129 |
local isolate do |
| 130 |
local visolate |
| 131 |
local function hisolate(list, offset) |
| 132 |
local x, last = 0, list.head |
| 133 |
local newhead, newtail = nil, nil |
| 134 |
local n = last |
| 135 |
while n do |
| 136 |
local id = n.id |
| 137 |
if id == hlist then |
| 138 |
local w, h, d = node.rangedimensions(list, last, n) |
| 139 |
x, last = x + w, n |
| 140 |
local inner_head, inner_tail, new_offset = hisolate(n, offset - x) |
| 141 |
if inner_head then |
| 142 |
if newhead then |
| 143 |
newtail.next, inner_head.prev = inner_head, newtail |
| 144 |
else |
| 145 |
newhead = inner_head |
| 146 |
end |
| 147 |
newtail = inner_tail |
| 148 |
offset = x + new_offset |
| 149 |
end |
| 150 |
n = n.next |
| 151 |
elseif id == vlist then |
| 152 |
local w, h, d = node.rangedimensions(list, last, n) |
| 153 |
x, last = x + w, n |
| 154 |
local inner_head, inner_tail, new_offset = visolate(n, offset - x) |
| 155 |
if inner_head then |
| 156 |
if newhead then |
| 157 |
newtail.next, inner_head.prev = inner_head, newtail |
| 158 |
else |
| 159 |
newhead = inner_head |
| 160 |
end |
| 161 |
newtail = inner_tail |
| 162 |
offset = x + new_offset |
| 163 |
end |
| 164 |
n = n.next |
| 165 |
elseif id == whatsit and n.subtype == user_defined |
| 166 |
and n.user_id == whatsit_id then |
| 167 |
local w, h, d = node.rangedimensions(list, last, n) |
| 168 |
x = x + w |
| 169 |
list.head, last = node.remove(list.head, n) |
| 170 |
if x ~= offset then |
| 171 |
local k = node.new(glue) |
| 172 |
k.width, offset = x - offset, x |
| 173 |
newhead, newtail = node.insert_after(newhead, newtail, k) |
| 174 |
end |
| 175 |
newhead, newtail = node.insert_after(newhead, newtail, n) |
| 176 |
n = last |
| 177 |
else |
| 178 |
n = n.next |
| 179 |
end |
| 180 |
end |
| 181 |
return newhead, newtail, offset |
| 182 |
end |
| 183 |
function visolate(list, offset) |
| 184 |
local newhead, newtail = nil, nil |
| 185 |
local n = list.head |
| 186 |
while n do |
| 187 |
local id = n.id |
| 188 |
if id == hlist then |
| 189 |
if dx then return assert_unmarked(mark, n.next, dx + n.shift) end |
| 190 |
local inner_head, inner_tail, new_offset = hisolate(n, offset) |
| 191 |
if inner_head then |
| 192 |
if newhead then |
| 193 |
newtail.next, inner_head.prev = inner_head, newtail |
| 194 |
else |
| 195 |
newhead = inner_head |
| 196 |
end |
| 197 |
newtail = inner_tail |
| 198 |
offset = new_offset |
| 199 |
end |
| 200 |
n = n.next |
| 201 |
elseif id == vlist then |
| 202 |
if dx then return assert_unmarked(mark, n.next, dx + n.shift) end |
| 203 |
local inner_head, inner_tail, new_offset = visolate(n, offset) |
| 204 |
if inner_head then |
| 205 |
if newhead then |
| 206 |
newtail.next, inner_head.prev = inner_head, newtail |
| 207 |
else |
| 208 |
newhead = inner_head |
| 209 |
end |
| 210 |
newtail = inner_tail |
| 211 |
offset = new_offset |
| 212 |
end |
| 213 |
n = n.next |
| 214 |
elseif id == whatsit and n.subtype == user_defined |
| 215 |
and n.user_id == whatsit_id then |
| 216 |
local after |
| 217 |
list.head, after = node.remove(list.head, n) |
| 218 |
if 0 ~= offset then |
| 219 |
local k = node.new(glue) |
| 220 |
k.width, offset = -offset, 0 |
| 221 |
newhead, newtail = node.insert_after(newhead, newtail, k) |
| 222 |
end |
| 223 |
newhead, newtail = node.insert_after(newhead, newtail, n) |
| 224 |
n = last |
| 225 |
else |
| 226 |
n = n.next |
| 227 |
end |
| 228 |
end |
| 229 |
return newhead, newtail, offset |
| 230 |
end |
| 231 |
function isolate(head) |
| 232 |
local x, last = 0, head |
| 233 |
local newhead, newtail, offset = nil, nil, 0 |
| 234 |
local n = last |
| 235 |
while n do |
| 236 |
local id = n.id |
| 237 |
if id == hlist then |
| 238 |
local w, h, d = node.dimensions(last, n) |
| 239 |
x, last = x + w, n |
| 240 |
local inner_head, inner_tail, new_offset = hisolate(n, offset - x) |
| 241 |
if inner_head then |
| 242 |
if newhead then |
| 243 |
newtail.next, inner_head.prev = inner_head, newtail |
| 244 |
else |
| 245 |
newhead = inner_head |
| 246 |
end |
| 247 |
newtail = inner_tail |
| 248 |
offset = x + new_offset |
| 249 |
end |
| 250 |
n = n.next |
| 251 |
elseif id == vlist then |
| 252 |
local w, h, d = node.dimensions(last, n) |
| 253 |
x, last = x + w, n |
| 254 |
local inner_head, inner_tail, new_offset = visolate(n, offset - x) |
| 255 |
if inner_head then |
| 256 |
if newhead then |
| 257 |
newtail.next, inner_head.prev = inner_head, newtail |
| 258 |
else |
| 259 |
newhead = inner_head |
| 260 |
end |
| 261 |
newtail = inner_tail |
| 262 |
offset = x + new_offset |
| 263 |
end |
| 264 |
n = n.next |
| 265 |
elseif id == whatsit and n.subtype == user_defined |
| 266 |
and n.user_id == whatsit_id then |
| 267 |
local w, h, d = node.dimensions(last, n) |
| 268 |
x = x + w |
| 269 |
head, last = node.remove(head, n) |
| 270 |
if x ~= offset then |
| 271 |
local k = node.new(glue) |
| 272 |
k.width, offset = x - offset, x |
| 273 |
newhead, newtail = node.insert_after(newhead, newtail, k) |
| 274 |
end |
| 275 |
newhead, newtail = node.insert_after(newhead, newtail, n) |
| 276 |
n = last |
| 277 |
else |
| 278 |
n = n.next |
| 279 |
end |
| 280 |
end |
| 281 |
return head, newhead |
| 282 |
end |
| 283 |
end |
| 284 |
|
| 285 |
local function find_mmode_boundary() |
| 286 |
for i=tex.nest.ptr,0,-1 do |
| 287 |
local nest = tex.nest[i] |
| 288 |
if nest.mode ~= mmode and nest.mode ~= -mmode then |
| 289 |
return nest, i |
| 290 |
end |
| 291 |
end |
| 292 |
end |
| 293 |
|
| 294 |
luatexbase.add_to_callback('post_mlist_to_hlist_filter', function(n) |
| 295 |
local nest = find_mmode_boundary() |
| 296 |
local props = properties[nest.head] |
| 297 |
local alignment = props and props.luamathalign_alignment |
| 298 |
if alignment then |
| 299 |
props.luamathalign_alignment = nil |
| 300 |
local x |
| 301 |
n, x = measure(alignment.mark, n) |
| 302 |
local k = node.new'glue' |
| 303 |
local off = x - n.width |
| 304 |
k.width, alignment.afterkern.width = off, -off |
| 305 |
node.insert_after(n.head, nil, k) |
| 306 |
n.width = x |
| 307 |
end |
| 308 |
return n |
| 309 |
end, 'luamathalign') |
| 310 |
|
| 311 |
local function get_kerntoken(newmark) |
| 312 |
local nest = find_mmode_boundary() |
| 313 |
local props = properties[nest.head] |
| 314 |
if not props then |
| 315 |
props = {} |
| 316 |
properties[nest.head] = props |
| 317 |
end |
| 318 |
if props.luamathalign_alignment then |
| 319 |
tex.error('Multiple alignment classes trying to control the same cell') |
| 320 |
return token.new(0, 0) |
| 321 |
else |
| 322 |
local afterkern = node.new'glue' |
| 323 |
props.luamathalign_alignment = {mark = newmark, afterkern = afterkern} |
| 324 |
return token.new(node.direct.todirect(afterkern), node_cmd) |
| 325 |
end |
| 326 |
end |
| 327 |
|
| 328 |
local function insert_whatsit(mark) |
| 329 |
local n = node.new(whatsit, user_defined) |
| 330 |
n.user_id, n.type, n.value = whatsit_id, string.byte'd', mark |
| 331 |
node.write(n) |
| 332 |
end |
| 333 |
luacmd("SetAlignmentPoint", function() |
| 334 |
local mark = token.scan_int() |
| 335 |
if mark < 0 then |
| 336 |
for i=tex.nest.ptr,0,-1 do |
| 337 |
local t = tex.nest[i].head |
| 338 |
local props = properties[t] |
| 339 |
if props and props.luamathalign_context ~= nil then |
| 340 |
mark = mark + 1 |
| 341 |
if mark == 0 then |
| 342 |
props.luamathalign_context = true |
| 343 |
return insert_whatsit(-i) |
| 344 |
end |
| 345 |
end |
| 346 |
end |
| 347 |
tex.error('No compatible alignment environment found', |
| 348 |
'This either means that \\SetAlignmentPoint was used outside\n\z |
| 349 |
of an alignment or the used alignment is not setup for use with\n\z |
| 350 |
luamathalign. In the latter case you might want to look at\n\z |
| 351 |
non-negative alignment marks.') |
| 352 |
else |
| 353 |
return insert_whatsit(mark) |
| 354 |
end |
| 355 |
end, "protected") |
| 356 |
|
| 357 |
function handle_whatsit(mark) |
| 358 |
token.put_next(ampersand, get_kerntoken(mark)) |
| 359 |
end |
| 360 |
luacmd("ExecuteAlignment", function() |
| 361 |
return handle_whatsit(token.scan_int()) |
| 362 |
end, "protected") |
| 363 |
|
| 364 |
luacmd("LuaMathAlign@begin", function() |
| 365 |
local t = tex.nest.top.head |
| 366 |
local props = properties[t] |
| 367 |
if not props then |
| 368 |
props = {} |
| 369 |
properties[t] = props |
| 370 |
end |
| 371 |
props.luamathalign_context = false |
| 372 |
end, "protected") |
| 373 |
luacmd("LuaMathAlign@end@early", function() |
| 374 |
local t = tex.nest.top.head |
| 375 |
local props = properties[t] |
| 376 |
if props then |
| 377 |
if props.luamathalign_context == true then |
| 378 |
handle_whatsit(-tex.nest.ptr) |
| 379 |
end |
| 380 |
props.luamathalign_context = nil |
| 381 |
end |
| 382 |
end, "protected") |
| 383 |
local delayed |
| 384 |
luacmd("LuaMathAlign@end", function() |
| 385 |
local t = tex.nest.top.head |
| 386 |
local props = properties[t] |
| 387 |
if props then |
| 388 |
if props.luamathalign_context == true then |
| 389 |
assert(not delayed) |
| 390 |
delayed = {get_kerntoken(-tex.nest.ptr), ampersand} |
| 391 |
end |
| 392 |
props.luamathalign_context = nil |
| 393 |
end |
| 394 |
end, "protected") |
| 395 |
luatexbase.add_to_callback("hpack_filter", function(head, groupcode) |
| 396 |
if delayed and groupcode == "align_set" then |
| 397 |
-- HACK: token.put_next puts the tokens into the input stream after the cell |
| 398 |
-- is fully read, before the next starts. This will act as if the content was |
| 399 |
-- written as the first element of the next field. |
| 400 |
token.put_next(delayed) |
| 401 |
delayed = nil |
| 402 |
end |
| 403 |
return true |
| 404 |
end, "luamathalign.delayed") |
| 405 |
|
| 406 |
luacmd("LuaMathAlign@IsolateAlignmentPoints", function() |
| 407 |
local main = token.scan_int() |
| 408 |
if not token.scan_keyword 'into' then |
| 409 |
tex.error'Expected "into"' |
| 410 |
end |
| 411 |
local marks = token.scan_int() |
| 412 |
local head, newhead = isolate(tex.box[main]) |
| 413 |
tex.box[marks] = node.direct.tonode(node.direct.hpack( |
| 414 |
newhead and node.direct.todirect(newhead) or 0)) |
| 415 |
end, "protected") |
| 416 |
-- |
| 417 |
-- |
| 418 |
-- End of file `luamathalign.lua'. |