4 luatexbase.provides_module({
5 name = 'luatexja.stack',
8 description = 'LuaTeX-ja stack system',
10 module('luatexja.stack', package.seeall)
11 local err, warn, info, log = luatexbase.errwarinf(_NAME)
13 require('luatexja.base'); local ltjb = luatexja.base
15 local node_new = node.new
16 local id_whatsit = node.id('whatsit')
17 local sid_user = node.subtype('user_defined')
18 local hmode = 118 -- in luatexref-t.pdf, this must be 127
19 local table_fastcopy = table.fastcopy
21 local charprop_stack_table={}; charprop_stack_table[0]={}
23 function get_stack_level()
24 local i = tex.getcount('ltj@@stack')
25 local j = tex.currentgrouplevel
26 if j > tex.getcount('ltj@@group@level') then
27 i = i+1 -- new stack level
28 local gd = tex.globaldefs
29 if gd>0 then tex.globaldefs = 0 end
30 -- 'tex.globaldefs = 0' is local even if \globaldefs > 0.
31 tex.setcount('ltj@@group@level', j)
32 for k,v in pairs(charprop_stack_table) do -- clear the stack above i
33 if k>=i then charprop_stack_table[k]=nil end
35 charprop_stack_table[i] = table_fastcopy(charprop_stack_table[i-1])
36 tex.setcount('ltj@@stack', i)
37 if gd>0 then tex.globaldefs = gd end
38 if tex.nest[tex.nest.ptr].mode == hmode or
39 tex.nest[tex.nest.ptr].mode == -hmode then
40 local g = node_new(id_whatsit, sid_user)
41 g.user_id=30112; g.type=100; g.value=j; node.write(g)
47 -- local function table_to_str(v)
49 -- for i, a in pairs(v) do
50 -- s = s .. i .. "=" .. tostring(a) .. ', '
54 -- function print_stack_table(i)
55 -- print('\n>>> get_stack_level:')
56 -- for k, v in pairs(charprop_stack_table[i]) do
57 -- print(" " , k, type(k), table_to_str(v));
63 function set_stack_table(g,m,c,p,lb,ub)
64 local i = get_stack_level()
66 ltjb.package_error('luatexja',
67 "invalid code (".. p .. ")",
68 {"The code should in the range "..tostring(lb) ..'..'.. tostring(ub) .. ".",
69 "I'm going to use 0 instead of that illegal code value."})
71 elseif c<-1 or c>0x10ffff then
72 ltjb.package_error('luatexja',
73 'bad character code (' .. c .. ')',
74 {'A character number must be between -1 and 0x10ffff.',
75 "(-1 is used for denoting `math boundary')",
76 'So I changed this one to zero.'})
78 elseif not charprop_stack_table[i][m] then
79 charprop_stack_table[i][m] = {}
81 charprop_stack_table[i][m][c] = p
83 for j,v in pairs(charprop_stack_table) do
84 if not charprop_stack_table[j][m] then charprop_stack_table[j][m] = {} end
85 charprop_stack_table[j][m][c] = p
91 function set_stack_font(g,m,c,p)
92 local i = get_stack_level()
94 ltjb.package_error('luatexja',
95 "invalid family number (".. p .. ")",
96 {"The family number should in the range 0 .. 255.",
97 "I'm going to use 0 instead of that illegal family number."})
99 elseif not charprop_stack_table[i][c] then
100 charprop_stack_table[i][c] = {}
102 charprop_stack_table[i][m][c] = p
104 for j,v in pairs(charprop_stack_table) do
105 if not charprop_stack_table[j][m] then charprop_stack_table[j][m] = {} end
106 charprop_stack_table[j][m][c] = p
111 -- EXT: store \ltj@tempskipa
112 function set_stack_skip(g,m,sp)
113 local i = get_stack_level()
114 if not sp then return end
115 if not charprop_stack_table[i][m] then
116 charprop_stack_table[i][m] = {}
118 charprop_stack_table[i][m].width = sp.width
119 charprop_stack_table[i][m].stretch = sp.stretch
120 charprop_stack_table[i][m].shrink = sp.shrink
121 charprop_stack_table[i][m].stretch_order = sp.stretch_order
122 charprop_stack_table[i][m].shrink_order = sp.shrink_order
124 for j,v in pairs(charprop_stack_table) do
125 if not charprop_stack_table[j][m] then charprop_stack_table[j][m] = {} end
126 charprop_stack_table[j][m].width = sp.width
127 charprop_stack_table[j][m].stretch = sp.stretch
128 charprop_stack_table[j][m].shrink = sp.shrink
129 charprop_stack_table[j][m].stretch_order = sp.stretch_order
130 charprop_stack_table[j][m].shrink_order = sp.shrink_order
135 -- mode: nil iff it is called in callbacks
136 function get_skip_table(m, idx)
137 local i = charprop_stack_table[idx][m]
138 return i or { width = 0, stretch = 0, shrink = 0,
139 stretch_order = 0, shrink_order = 0 }
142 function get_penalty_table(m,c,d, idx)
143 local i = charprop_stack_table[idx][m]