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 STCK = luatexja.userid_table.STCK
21 charprop_stack_table={};
22 local charprop_stack_table = charprop_stack_table
23 charprop_stack_table[0]={}
26 function get_stack_level()
27 local i = tex.getcount('ltj@@stack')
28 local j = tex.currentgrouplevel
29 if j > tex.getcount('ltj@@group@level') then
30 i = i+1 -- new stack level
31 local gd = tex.globaldefs
32 if gd>0 then tex.globaldefs = 0 end
33 -- 'tex.globaldefs = 0' is local even if \globaldefs > 0.
34 tex.setcount('ltj@@group@level', j)
35 for k,v in pairs(charprop_stack_table) do -- clear the stack above i
36 if k>=i then charprop_stack_table[k]=nil end
38 charprop_stack_table[i] = table.fastcopy(charprop_stack_table[i-1])
39 tex.setcount('ltj@@stack', i)
40 if gd>0 then tex.globaldefs = gd end
41 if tex.nest[tex.nest.ptr].mode == hmode or
42 tex.nest[tex.nest.ptr].mode == -hmode then
43 local g = node_new(id_whatsit, sid_user)
44 g.user_id=STCK; g.type=100; g.value=j; node.write(g)
50 -- local function table_to_str(v)
52 -- for i, a in pairs(v) do
53 -- s = s .. i .. "=" .. tostring(a) .. ', '
57 -- function print_stack_table(i)
58 -- print('\n>>> get_stack_level:')
59 -- for k, v in pairs(charprop_stack_table[i]) do
60 -- print(" " , k, type(k), table_to_str(v));
66 function set_stack_table(g,m,c,p,lb,ub)
67 local i = get_stack_level()
68 if type(p)~='number' or p<lb or p>ub then
69 ltjb.package_error('luatexja',
70 "invalid code (".. tostring(p) .. ")",
71 "The code should in the range "..tostring(lb) .. '..' ..
72 tostring(ub) .. ".\n" ..
73 "I'm going to use 0 instead of that illegal code value.")
75 elseif type(c)~='number' or c<-1 or c>0x10ffff then
76 ltjb.package_error('luatexja',
77 'bad character code (' .. tostring(c) .. ')',
78 'A character number must be between -1 and 0x10ffff.\n' ..
79 "(-1 is used for denoting `math boundary')\n" ..
80 'So I changed this one to zero.')
83 charprop_stack_table[i][c+m] = p
85 for j,v in pairs(charprop_stack_table) do
86 charprop_stack_table[j][c+m] = p
92 function set_stack_font(g,m,c,p)
93 local i = get_stack_level()
94 if type(c)~='number' or c<0 or c>255 then
95 ltjb.package_error('luatexja',
96 "invalid family number (".. tostring(c) .. ")",
97 "The family number should in the range 0 .. 255.\n" ..
98 "I'm going to use 0 instead of that illegal family number.")
101 charprop_stack_table[i][c+m] = p
103 for j,v in pairs(charprop_stack_table) do
104 charprop_stack_table[j][c+m] = p
109 -- EXT: store \ltj@tempskipa
110 function set_stack_skip(g,m,sp)
111 local i = get_stack_level()
112 if not sp then return end
113 if not charprop_stack_table[i][m] then
114 charprop_stack_table[i][m] = {}
116 charprop_stack_table[i][m].width = sp.width
117 charprop_stack_table[i][m].stretch = sp.stretch
118 charprop_stack_table[i][m].shrink = sp.shrink
119 charprop_stack_table[i][m].stretch_order = sp.stretch_order
120 charprop_stack_table[i][m].shrink_order = sp.shrink_order
122 for j,v in pairs(charprop_stack_table) do
123 if not charprop_stack_table[j][m] then charprop_stack_table[j][m] = {} end
124 charprop_stack_table[j][m].width = sp.width
125 charprop_stack_table[j][m].stretch = sp.stretch
126 charprop_stack_table[j][m].shrink = sp.shrink
127 charprop_stack_table[j][m].stretch_order = sp.stretch_order
128 charprop_stack_table[j][m].shrink_order = sp.shrink_order
133 -- These three functions are used in ltj-jfmglue.lua.
134 function report_stack_level(bsl)
135 table_current_stack = charprop_stack_table[bsl]
137 function fast_get_skip_table(m)
138 return table_current_stack[m]
139 or { width = 0, stretch = 0, shrink = 0, stretch_order = 0, shrink_order = 0 }
142 -- For other situations, use the following instead:
143 function get_skip_table(m, idx)
144 return charprop_stack_table[idx][m]
145 or { width = 0, stretch = 0, shrink = 0, stretch_order = 0, shrink_order = 0 }
147 function get_penalty_table(mc, d, idx)
148 local i = charprop_stack_table[idx][mc]