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 local charprop_stack_table={}; charprop_stack_table[0]={}
22 -- modified from table.fastcopy
24 local function stack_table_copy_aux(old)
26 for i,v in next, old do
31 local function stack_table_copy(old)
34 for i,v in next, old do
35 new[i] = stack_table_copy_aux(v)
43 function get_stack_level()
44 local i = tex.getcount('ltj@@stack')
45 local j = tex.currentgrouplevel
46 if j > tex.getcount('ltj@@group@level') then
47 i = i+1 -- new stack level
48 local gd = tex.globaldefs
49 if gd>0 then tex.globaldefs = 0 end
50 -- 'tex.globaldefs = 0' is local even if \globaldefs > 0.
51 tex.setcount('ltj@@group@level', j)
52 for k,v in pairs(charprop_stack_table) do -- clear the stack above i
53 if k>=i then charprop_stack_table[k]=nil end
55 charprop_stack_table[i] = stack_table_copy(charprop_stack_table[i-1])
56 tex.setcount('ltj@@stack', i)
57 if gd>0 then tex.globaldefs = gd end
58 if tex.nest[tex.nest.ptr].mode == hmode or
59 tex.nest[tex.nest.ptr].mode == -hmode then
60 local g = node_new(id_whatsit, sid_user)
61 g.user_id=30112; g.type=100; g.value=j; node.write(g)
67 -- local function table_to_str(v)
69 -- for i, a in pairs(v) do
70 -- s = s .. i .. "=" .. tostring(a) .. ', '
74 -- function print_stack_table(i)
75 -- print('\n>>> get_stack_level:')
76 -- for k, v in pairs(charprop_stack_table[i]) do
77 -- print(" " , k, type(k), table_to_str(v));
83 function set_stack_table(g,m,c,p,lb,ub)
84 local i = get_stack_level()
86 ltjb.package_error('luatexja',
87 "invalid code (".. p .. ")",
88 {"The code should in the range "..tostring(lb) ..'..'.. tostring(ub) .. ".",
89 "I'm going to use 0 instead of that illegal code value."})
91 elseif c<-1 or c>0x10ffff then
92 ltjb.package_error('luatexja',
93 'bad character code (' .. c .. ')',
94 {'A character number must be between -1 and 0x10ffff.',
95 "(-1 is used for denoting `math boundary')",
96 'So I changed this one to zero.'})
98 elseif not charprop_stack_table[i][m] then
99 charprop_stack_table[i][m] = {}
101 charprop_stack_table[i][m][c] = p
103 for j,v in pairs(charprop_stack_table) do
104 if not charprop_stack_table[j][m] then charprop_stack_table[j][m] = {} end
105 charprop_stack_table[j][m][c] = p
111 function set_stack_font(g,m,c,p)
112 local i = get_stack_level()
114 ltjb.package_error('luatexja',
115 "invalid family number (".. p .. ")",
116 {"The family number should in the range 0 .. 255.",
117 "I'm going to use 0 instead of that illegal family number."})
119 elseif not charprop_stack_table[i][m] then
120 charprop_stack_table[i][m] = {}
122 charprop_stack_table[i][m][c] = p
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][c] = p
131 -- EXT: store \ltj@tempskipa
132 function set_stack_skip(g,m,sp)
133 local i = get_stack_level()
134 if not sp then return end
135 if not charprop_stack_table[i][m] then
136 charprop_stack_table[i][m] = {}
138 charprop_stack_table[i][m].width = sp.width
139 charprop_stack_table[i][m].stretch = sp.stretch
140 charprop_stack_table[i][m].shrink = sp.shrink
141 charprop_stack_table[i][m].stretch_order = sp.stretch_order
142 charprop_stack_table[i][m].shrink_order = sp.shrink_order
144 for j,v in pairs(charprop_stack_table) do
145 if not charprop_stack_table[j][m] then charprop_stack_table[j][m] = {} end
146 charprop_stack_table[j][m].width = sp.width
147 charprop_stack_table[j][m].stretch = sp.stretch
148 charprop_stack_table[j][m].shrink = sp.shrink
149 charprop_stack_table[j][m].stretch_order = sp.stretch_order
150 charprop_stack_table[j][m].shrink_order = sp.shrink_order
155 -- mode: nil iff it is called in callbacks
156 function get_skip_table(m, idx)
157 local i = charprop_stack_table[idx][m]
158 return i or { width = 0, stretch = 0, shrink = 0,
159 stretch_order = 0, shrink_order = 0 }
162 function get_penalty_table(m,c,d, idx)
163 local i = charprop_stack_table[idx][m]