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 luatexja.load_module('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
20 charprop_stack_table={};
21 local charprop_stack_table = charprop_stack_table
22 charprop_stack_table[0]={}
24 -- modified from table.fastcopy
26 local function stack_table_copy_aux(old)
28 for i,v in next, old do
33 local function stack_table_copy(old)
36 for i,v in next, old do
37 new[i] = stack_table_copy_aux(v)
45 function get_stack_level()
46 local i = tex.getcount('ltj@@stack')
47 local j = tex.currentgrouplevel
48 if j > tex.getcount('ltj@@group@level') then
49 i = i+1 -- new stack level
50 local gd = tex.globaldefs
51 if gd>0 then tex.globaldefs = 0 end
52 -- 'tex.globaldefs = 0' is local even if \globaldefs > 0.
53 tex.setcount('ltj@@group@level', j)
54 for k,v in pairs(charprop_stack_table) do -- clear the stack above i
55 if k>=i then charprop_stack_table[k]=nil end
57 charprop_stack_table[i] = stack_table_copy(charprop_stack_table[i-1])
58 tex.setcount('ltj@@stack', i)
59 if gd>0 then tex.globaldefs = gd end
60 if tex.nest[tex.nest.ptr].mode == hmode or
61 tex.nest[tex.nest.ptr].mode == -hmode then
62 local g = node_new(id_whatsit, sid_user)
63 g.user_id=30112; g.type=100; g.value=j; node.write(g)
69 -- local function table_to_str(v)
71 -- for i, a in pairs(v) do
72 -- s = s .. i .. "=" .. tostring(a) .. ', '
76 -- function print_stack_table(i)
77 -- print('\n>>> get_stack_level:')
78 -- for k, v in pairs(charprop_stack_table[i]) do
79 -- print(" " , k, type(k), table_to_str(v));
85 function set_stack_table(g,m,c,p,lb,ub)
86 local i = get_stack_level()
87 if type(p)~='number' or p<lb or p>ub then
88 ltjb.package_error('luatexja',
89 "invalid code (".. tostring(p) .. ")",
90 "The code should in the range "..tostring(lb) .. '..' ..
91 tostring(ub) .. ".\n" ..
92 "I'm going to use 0 instead of that illegal code value.")
94 elseif type(c)~='number' or c<-1 or c>0x10ffff then
95 ltjb.package_error('luatexja',
96 'bad character code (' .. tostring(c) .. ')',
97 'A character number must be between -1 and 0x10ffff.\n' ..
98 "(-1 is used for denoting `math boundary')\n" ..
99 'So I changed this one to zero.')
101 elseif not charprop_stack_table[i][c] then
102 charprop_stack_table[i][c] = {}
104 charprop_stack_table[i][c][m] = p
106 for j,v in pairs(charprop_stack_table) do
107 if not charprop_stack_table[j][c] then charprop_stack_table[j][c] = {} end
108 charprop_stack_table[j][c][m] = p
114 function set_stack_font(g,m,c,p)
115 local i = get_stack_level()
116 if type(c)~='number' or c<0 or c>255 then
117 ltjb.package_error('luatexja',
118 "invalid family number (".. tostring(c) .. ")",
119 "The family number should in the range 0 .. 255.\n" ..
120 "I'm going to use 0 instead of that illegal family number.")
122 elseif not charprop_stack_table[i][c] then
123 charprop_stack_table[i][c] = {}
125 charprop_stack_table[i][c][m] = p
127 for j,v in pairs(charprop_stack_table) do
128 if not charprop_stack_table[j][c] then charprop_stack_table[j][c] = {} end
129 charprop_stack_table[j][c][m] = p
134 -- EXT: store \ltj@tempskipa
135 function set_stack_skip(g,m,sp)
136 local i = get_stack_level()
137 if not sp then return end
138 if not charprop_stack_table[i][m] then
139 charprop_stack_table[i][m] = {}
141 charprop_stack_table[i][m].width = sp.width
142 charprop_stack_table[i][m].stretch = sp.stretch
143 charprop_stack_table[i][m].shrink = sp.shrink
144 charprop_stack_table[i][m].stretch_order = sp.stretch_order
145 charprop_stack_table[i][m].shrink_order = sp.shrink_order
147 for j,v in pairs(charprop_stack_table) do
148 if not charprop_stack_table[j][m] then charprop_stack_table[j][m] = {} end
149 charprop_stack_table[j][m].width = sp.width
150 charprop_stack_table[j][m].stretch = sp.stretch
151 charprop_stack_table[j][m].shrink = sp.shrink
152 charprop_stack_table[j][m].stretch_order = sp.stretch_order
153 charprop_stack_table[j][m].shrink_order = sp.shrink_order
158 -- These three functions are used in ltj-jfmglue.lua.
159 local table_current_stack
160 function report_stack_level(bsl)
161 table_current_stack = charprop_stack_table[bsl]
163 function fast_get_skip_table(m)
164 return table_current_stack[m]
165 or { width = 0, stretch = 0, shrink = 0, stretch_order = 0, shrink_order = 0 }
167 function fast_get_penalty_table(m,c)
168 local i = table_current_stack[c]
172 local empty_table = {}
173 function fast_get_penalty_table_parent(c)
174 return table_current_stack[c] or empty_table
177 -- For other situations, use the following instead:
178 function get_skip_table(m, idx)
179 return charprop_stack_table[idx][m]
180 or { width = 0, stretch = 0, shrink = 0, stretch_order = 0, shrink_order = 0 }
182 function get_penalty_table(m,c,d, idx)
183 local i = charprop_stack_table[idx][c]
184 return (i and i[m]) or d