4 luatexbase.provides_module({
5 name = 'luatexja.stack',
7 description = 'LuaTeX-ja stack system',
9 module('luatexja.stack', package.seeall)
10 local err, warn, info, log = luatexbase.errwarinf(_NAME)
12 luatexja.load_module('base'); local ltjb = luatexja.base
14 --------------------------------------------------------------------------------
15 -- stack table (obeys TeX's grouping)
16 --------------------------------------------------------------------------------
17 local node_new = node.new
18 local id_whatsit = node.id('whatsit')
19 local sid_user = node.subtype('user_defined')
20 local STCK = luatexja.userid_table.STCK
23 charprop_stack_table={};
24 local charprop_stack_table = charprop_stack_table
25 charprop_stack_table[0]={}
28 function get_stack_level(is_v)
29 local i = tex.getcount('ltj@@stack')
30 local j = tex.currentgrouplevel
31 if j > tex.getcount('ltj@@group@level') then
32 i = i+1 -- new stack level
33 local gd = tex.globaldefs
34 if gd~=0 then tex.globaldefs = 0 end
35 -- 'tex.globaldefs = 0' is local even if \globaldefs > 0.
36 tex.setcount('ltj@@group@level', j)
37 for k,v in pairs(charprop_stack_table) do -- clear the stack above i
38 if k>=i then charprop_stack_table[k]=nil end
40 charprop_stack_table[i] = table.fastcopy(charprop_stack_table[i-1])
41 tex.setcount('ltj@@stack', i)
42 if gd~=0 then tex.globaldefs = gd end
43 if is_v or tex.nest[tex.nest.ptr].mode == hmode or
44 tex.nest[tex.nest.ptr].mode == -hmode then
45 local g = node_new(id_whatsit, sid_user)
46 g.user_id=STCK; g.type=100; g.value=j; node.write(g)
52 function set_stack_table(m,p, is_v)
53 local i = get_stack_level(is_v)
54 charprop_stack_table[i][m] = p
55 if luatexja.isglobal=='global' then
56 for j,v in pairs(charprop_stack_table) do
57 charprop_stack_table[j][m] = p
61 local set_stack_table = set_stack_table
64 function set_stack_perchar(m,c,p,lb,ub)
65 if type(p)~='number' or p<lb or p>ub then
66 ltjb.package_error('luatexja',
67 "invalid code (".. tostring(p) .. ")",
68 "The code should in the range "..tostring(lb) .. '..' ..
69 tostring(ub) .. ".\n" ..
70 "I'm going to use 0 instead of that illegal code value.")
73 set_stack_table(m+ltjb.in_unicode(c, true), p)
77 function set_stack_font(m,c,p)
78 if type(c)~='number' or c<0 or c>255 then
79 ltjb.package_error('luatexja',
80 "invalid family number (".. tostring(c) .. ")",
81 "The family number should in the range 0 .. 255.\n" ..
82 "I'm going to use 0 instead of that illegal family number.")
85 set_stack_table(m+c, p)
89 function set_stack_skip(m,sp)
90 local i = get_stack_level()
91 if not sp then return end
92 if not charprop_stack_table[i][m] then
93 charprop_stack_table[i][m] = {}
95 charprop_stack_table[i][m].width = sp.width
96 charprop_stack_table[i][m].stretch = sp.stretch
97 charprop_stack_table[i][m].shrink = sp.shrink
98 charprop_stack_table[i][m].stretch_order = sp.stretch_order
99 charprop_stack_table[i][m].shrink_order = sp.shrink_order
100 if luatexja.isglobal=='global' then
101 for j,v in pairs(charprop_stack_table) do
102 if not charprop_stack_table[j][m] then charprop_stack_table[j][m] = {} end
103 charprop_stack_table[j][m].width = sp.width
104 charprop_stack_table[j][m].stretch = sp.stretch
105 charprop_stack_table[j][m].shrink = sp.shrink
106 charprop_stack_table[j][m].stretch_order = sp.stretch_order
107 charprop_stack_table[j][m].shrink_order = sp.shrink_order
112 -- These three functions are used in ltj-jfmglue.lua.
113 function report_stack_level(bsl)
114 table_current_stack = charprop_stack_table[bsl]
117 function fast_get_stack_skip(m)
118 return table_current_stack[m]
119 or { width = 0, stretch = 0, shrink = 0, stretch_order = 0, shrink_order = 0 }
122 -- For other situations, use the following instead:
123 function get_stack_skip(m, idx)
124 return charprop_stack_table[idx][m]
125 or { width = 0, stretch = 0, shrink = 0, stretch_order = 0, shrink_order = 0 }
127 function get_stack_table(mc, d, idx)
128 local i = charprop_stack_table[idx][mc]
133 --------------------------------------------------------------------------------
134 -- nest table (obeys TeX's semantic nest)
135 --------------------------------------------------------------------------------
137 local nest_table = nest_table