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()
85 if type(p)~='number' or p<lb or p>ub then
86 ltjb.package_error('luatexja',
87 "invalid code (".. tostring(p) .. ")",
88 "The code should in the range "..tostring(lb) .. '..' ..
89 tostring(ub) .. ".\n" ..
90 "I'm going to use 0 instead of that illegal code value.")
92 elseif type(c)~='number' or c<-1 or c>0x10ffff then
93 ltjb.package_error('luatexja',
94 'bad character code (' .. tostring(c) .. ')',
95 'A character number must be between -1 and 0x10ffff.\n' ..
96 "(-1 is used for denoting `math boundary')\n" ..
97 'So I changed this one to zero.')
99 elseif not charprop_stack_table[i][m] then
100 charprop_stack_table[i][m] = {}
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
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.")
120 elseif not charprop_stack_table[i][m] then
121 charprop_stack_table[i][m] = {}
123 charprop_stack_table[i][m][c] = p
125 for j,v in pairs(charprop_stack_table) do
126 if not charprop_stack_table[j][m] then charprop_stack_table[j][m] = {} end
127 charprop_stack_table[j][m][c] = p
132 -- EXT: store \ltj@tempskipa
133 function set_stack_skip(g,m,sp)
134 local i = get_stack_level()
135 if not sp then return end
136 if not charprop_stack_table[i][m] then
137 charprop_stack_table[i][m] = {}
139 charprop_stack_table[i][m].width = sp.width
140 charprop_stack_table[i][m].stretch = sp.stretch
141 charprop_stack_table[i][m].shrink = sp.shrink
142 charprop_stack_table[i][m].stretch_order = sp.stretch_order
143 charprop_stack_table[i][m].shrink_order = sp.shrink_order
145 for j,v in pairs(charprop_stack_table) do
146 if not charprop_stack_table[j][m] then charprop_stack_table[j][m] = {} end
147 charprop_stack_table[j][m].width = sp.width
148 charprop_stack_table[j][m].stretch = sp.stretch
149 charprop_stack_table[j][m].shrink = sp.shrink
150 charprop_stack_table[j][m].stretch_order = sp.stretch_order
151 charprop_stack_table[j][m].shrink_order = sp.shrink_order
156 -- mode: nil iff it is called in callbacks
157 function get_skip_table(m, idx)
158 local i = charprop_stack_table[idx][m]
159 return i or { width = 0, stretch = 0, shrink = 0,
160 stretch_order = 0, shrink_order = 0 }
163 function get_penalty_table(m,c,d, idx)
164 local i = charprop_stack_table[idx][m]