diff options
Diffstat (limited to 'Master/texmf-dist/tex/lualatex/luamathalign/luamathalign.lua')
-rw-r--r-- | Master/texmf-dist/tex/lualatex/luamathalign/luamathalign.lua | 101 |
1 files changed, 72 insertions, 29 deletions
diff --git a/Master/texmf-dist/tex/lualatex/luamathalign/luamathalign.lua b/Master/texmf-dist/tex/lualatex/luamathalign/luamathalign.lua index aa8476b0553..03f75fa5f55 100644 --- a/Master/texmf-dist/tex/lualatex/luamathalign/luamathalign.lua +++ b/Master/texmf-dist/tex/lualatex/luamathalign/luamathalign.lua @@ -17,16 +17,24 @@ -- -- and version 1.3 or later is part of all distributions of -- LaTeX version 2005/12/01 or later. +local properties = node.get_properties_table() local luacmd = require'luamathalign-luacmd' -local luaprop = require'luamathalign-luaprop'('mathalign') local hlist = node.id'hlist' local vlist = node.id'vlist' local whatsit = node.id'whatsit' -local kern = node.id'kern' +local glue = node.id'glue' local user_defined = node.subtype'user_defined' local whatsit_id = luatexbase.new_whatsit'mathalign' local node_cmd = token.command_id'node' local ampersand = token.new(38, 4) + +local mmode do + for k,v in next, tex.getmodevalues() do + if v == 'math' then mmode = k end + end + assert(mmode) +end + -- We might want to add y later local function is_marked(mark, list) for n in node.traverse(list) do @@ -160,8 +168,8 @@ local isolate do x = x + w list.head, last = node.remove(list.head, n) if x ~= offset then - local k = node.new(kern) - k.kern, offset = x - offset, x + local k = node.new(glue) + k.width, offset = x - offset, x newhead, newtail = node.insert_after(newhead, newtail, k) end newhead, newtail = node.insert_after(newhead, newtail, n) @@ -208,8 +216,8 @@ local isolate do local after list.head, after = node.remove(list.head, n) if 0 ~= offset then - local k = node.new(kern) - k.kern, offset = -offset, 0 + local k = node.new(glue) + k.width, offset = -offset, 0 newhead, newtail = node.insert_after(newhead, newtail, k) end newhead, newtail = node.insert_after(newhead, newtail, n) @@ -260,8 +268,8 @@ local isolate do x = x + w head, last = node.remove(head, n) if x ~= offset then - local k = node.new(kern) - k.kern, offset = x - offset, x + local k = node.new(glue) + k.width, offset = x - offset, x newhead, newtail = node.insert_after(newhead, newtail, k) end newhead, newtail = node.insert_after(newhead, newtail, n) @@ -274,25 +282,47 @@ local isolate do end end -local mark, afterkern +local function find_mmode_boundary() + for i=tex.nest.ptr,0,-1 do + local nest = tex.nest[i] + if nest.mode ~= mmode and nest.mode ~= -mmode then + return nest, i + end + end +end + luatexbase.add_to_callback('post_mlist_to_hlist_filter', function(n) - if mark then + local nest = find_mmode_boundary() + local props = properties[nest.head] + local alignment = props and props.luamathalign_alignment + if alignment then + props.luamathalign_alignment = nil local x - n, x = measure(mark, n) - local k = node.new'kern' + n, x = measure(alignment.mark, n) + local k = node.new'glue' local off = x - n.width - k.kern, afterkern.kern = off, -off + k.width, alignment.afterkern.width = off, -off node.insert_after(n.head, nil, k) n.width = x - mark, afterkern = nil, nil end return n end, 'luamathalign') local function get_kerntoken(newmark) - assert(not mark) - mark, afterkern = newmark, node.new'kern' - return token.new(node.direct.todirect(afterkern), node_cmd) + local nest = find_mmode_boundary() + local props = properties[nest.head] + if not props then + props = {} + properties[nest.head] = props + end + if props.luamathalign_alignment then + tex.error('Multiple alignment classes trying to control the same cell') + return token.new(0, 0) + else + local afterkern = node.new'glue' + props.luamathalign_alignment = {mark = newmark, afterkern = afterkern} + return token.new(node.direct.todirect(afterkern), node_cmd) + end end local function insert_whatsit(mark) @@ -305,10 +335,11 @@ luacmd("SetAlignmentPoint", function() if mark < 0 then for i=tex.nest.ptr,0,-1 do local t = tex.nest[i].head - if luaprop.query(t) ~= nil then + local props = properties[t] + if props and props.luamathalign_context ~= nil then mark = mark + 1 if mark == 0 then - luaprop.set(t, true) + props.luamathalign_context = true return insert_whatsit(-i) end end @@ -331,22 +362,34 @@ luacmd("ExecuteAlignment", function() end, "protected") luacmd("LuaMathAlign@begin", function() - local nest = tex.nest.top - luaprop.set(nest.head, false) + local t = tex.nest.top.head + local props = properties[t] + if not props then + props = {} + properties[t] = props + end + props.luamathalign_context = false end, "protected") luacmd("LuaMathAlign@end@early", function() local t = tex.nest.top.head - if luaprop.set(t, nil) == true then - handle_whatsit(-tex.nest.ptr) + local props = properties[t] + if props then + if props.luamathalign_context == true then + handle_whatsit(-tex.nest.ptr) + end + props.luamathalign_context = nil end end, "protected") local delayed luacmd("LuaMathAlign@end", function() - local nest = tex.nest.top - local t = nest.head - if luaprop.set(t, nil) == true then - assert(not delayed) - delayed = {get_kerntoken(-tex.nest.ptr), ampersand} + local t = tex.nest.top.head + local props = properties[t] + if props then + if props.luamathalign_context == true then + assert(not delayed) + delayed = {get_kerntoken(-tex.nest.ptr), ampersand} + end + props.luamathalign_context = nil end end, "protected") luatexbase.add_to_callback("hpack_filter", function(head, groupcode) @@ -363,7 +406,7 @@ end, "luamathalign.delayed") luacmd("LuaMathAlign@IsolateAlignmentPoints", function() local main = token.scan_int() if not token.scan_keyword 'into' then - tex.error'Expected "into"' + tex.error'Expected "into"' end local marks = token.scan_int() local head, newhead = isolate(tex.box[main]) |