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)
87 local i = get_stack_level()
88 if type(p)~='number' or p<lb or p>ub then
89 ltjb.package_error('luatexja',
90 "invalid code (".. tostring(p) .. ")",
91 "The code should in the range "..tostring(lb) .. '..' ..
92 tostring(ub) .. ".\n" ..
93 "I'm going to use 0 instead of that illegal code value.")
95 elseif type(c)~='number' or c<-1 or c>0x10ffff then
96 ltjb.package_error('luatexja',
97 'bad character code (' .. tostring(c) .. ')',
98 'A character number must be between -1 and 0x10ffff.\n' ..
99 "(-1 is used for denoting `math boundary')\n" ..
100 'So I changed this one to zero.')
103 charprop_stack_table[i][c+m] = p
105 for j,v in pairs(charprop_stack_table) do
106 charprop_stack_table[j][c+m] = p
112 function set_stack_font(g,m,c,p)
113 local i = get_stack_level()
114 if type(c)~='number' or c<0 or c>255 then
115 ltjb.package_error('luatexja',
116 "invalid family number (".. tostring(c) .. ")",
117 "The family number should in the range 0 .. 255.\n" ..
118 "I'm going to use 0 instead of that illegal family number.")
121 charprop_stack_table[i][c+m] = p
123 for j,v in pairs(charprop_stack_table) do
124 charprop_stack_table[j][c+m] = p
129 -- EXT: store \ltj@tempskipa
130 function set_stack_skip(g,m,sp)
131 local i = get_stack_level()
132 if not sp then return end
133 if not charprop_stack_table[i][m] then
134 charprop_stack_table[i][m] = {}
136 charprop_stack_table[i][m].width = sp.width
137 charprop_stack_table[i][m].stretch = sp.stretch
138 charprop_stack_table[i][m].shrink = sp.shrink
139 charprop_stack_table[i][m].stretch_order = sp.stretch_order
140 charprop_stack_table[i][m].shrink_order = sp.shrink_order
142 for j,v in pairs(charprop_stack_table) do
143 if not charprop_stack_table[j][m] then charprop_stack_table[j][m] = {} end
144 charprop_stack_table[j][m].width = sp.width
145 charprop_stack_table[j][m].stretch = sp.stretch
146 charprop_stack_table[j][m].shrink = sp.shrink
147 charprop_stack_table[j][m].stretch_order = sp.stretch_order
148 charprop_stack_table[j][m].shrink_order = sp.shrink_order
153 -- These three functions are used in ltj-jfmglue.lua.
154 function report_stack_level(bsl)
155 table_current_stack = charprop_stack_table[bsl]
157 function fast_get_skip_table(m)
158 return table_current_stack[m]
159 or { width = 0, stretch = 0, shrink = 0, stretch_order = 0, shrink_order = 0 }
162 -- For other situations, use the following instead:
163 function get_skip_table(m, idx)
164 return charprop_stack_table[idx][m]
165 or { width = 0, stretch = 0, shrink = 0, stretch_order = 0, shrink_order = 0 }
167 function get_penalty_table(mc, d, idx)
168 local i = charprop_stack_table[idx][mc]