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 local node_new = node.new
15 local id_whatsit = node.id('whatsit')
16 local sid_user = node.subtype('user_defined')
17 local STCK = luatexja.userid_table.STCK
20 charprop_stack_table={};
21 local charprop_stack_table = charprop_stack_table
22 charprop_stack_table[0]={}
25 function get_stack_level()
26 local i = tex.getcount('ltj@@stack')
27 local j = tex.currentgrouplevel
28 if j > tex.getcount('ltj@@group@level') then
29 i = i+1 -- new stack level
30 local gd = tex.globaldefs
31 if gd>0 then tex.globaldefs = 0 end
32 -- 'tex.globaldefs = 0' is local even if \globaldefs > 0.
33 tex.setcount('ltj@@group@level', j)
34 for k,v in pairs(charprop_stack_table) do -- clear the stack above i
35 if k>=i then charprop_stack_table[k]=nil end
37 charprop_stack_table[i] = table.fastcopy(charprop_stack_table[i-1])
38 tex.setcount('ltj@@stack', i)
39 if gd>0 then tex.globaldefs = gd end
40 if tex.nest[tex.nest.ptr].mode == hmode or
41 tex.nest[tex.nest.ptr].mode == -hmode then
42 local g = node_new(id_whatsit, sid_user)
43 g.user_id=STCK; g.type=100; g.value=j; node.write(g)
49 -- local function table_to_str(v)
51 -- for i, a in pairs(v) do
52 -- s = s .. i .. "=" .. tostring(a) .. ', '
56 -- function print_stack_table(i)
57 -- print('\n>>> get_stack_level:')
58 -- for k, v in pairs(charprop_stack_table[i]) do
59 -- print(" " , k, type(k), table_to_str(v));
65 function set_stack_table(g,m,c,p,lb,ub)
66 local i = get_stack_level()
67 if type(p)~='number' or p<lb or p>ub then
68 ltjb.package_error('luatexja',
69 "invalid code (".. tostring(p) .. ")",
70 "The code should in the range "..tostring(lb) .. '..' ..
71 tostring(ub) .. ".\n" ..
72 "I'm going to use 0 instead of that illegal code value.")
75 c = ltjb.in_unicode(c, true)
76 charprop_stack_table[i][c+m] = p
78 for j,v in pairs(charprop_stack_table) do
79 charprop_stack_table[j][c+m] = p
85 function set_stack_font(g,m,c,p)
86 local i = get_stack_level()
87 if type(c)~='number' or c<0 or c>255 then
88 ltjb.package_error('luatexja',
89 "invalid family number (".. tostring(c) .. ")",
90 "The family number should in the range 0 .. 255.\n" ..
91 "I'm going to use 0 instead of that illegal family number.")
94 charprop_stack_table[i][c+m] = p
96 for j,v in pairs(charprop_stack_table) do
97 charprop_stack_table[j][c+m] = p
102 -- EXT: store \ltj@tempskipa
103 function set_stack_skip(g,m,sp)
104 local i = get_stack_level()
105 if not sp then return end
106 if not charprop_stack_table[i][m] then
107 charprop_stack_table[i][m] = {}
109 charprop_stack_table[i][m].width = sp.width
110 charprop_stack_table[i][m].stretch = sp.stretch
111 charprop_stack_table[i][m].shrink = sp.shrink
112 charprop_stack_table[i][m].stretch_order = sp.stretch_order
113 charprop_stack_table[i][m].shrink_order = sp.shrink_order
115 for j,v in pairs(charprop_stack_table) do
116 if not charprop_stack_table[j][m] then charprop_stack_table[j][m] = {} end
117 charprop_stack_table[j][m].width = sp.width
118 charprop_stack_table[j][m].stretch = sp.stretch
119 charprop_stack_table[j][m].shrink = sp.shrink
120 charprop_stack_table[j][m].stretch_order = sp.stretch_order
121 charprop_stack_table[j][m].shrink_order = sp.shrink_order
126 -- These three functions are used in ltj-jfmglue.lua.
127 function report_stack_level(bsl)
128 table_current_stack = charprop_stack_table[bsl]
130 function fast_get_skip_table(m)
131 return table_current_stack[m]
132 or { width = 0, stretch = 0, shrink = 0, stretch_order = 0, shrink_order = 0 }
135 -- For other situations, use the following instead:
136 function get_skip_table(m, idx)
137 return charprop_stack_table[idx][m]
138 or { width = 0, stretch = 0, shrink = 0, stretch_order = 0, shrink_order = 0 }
140 function get_penalty_table(mc, d, idx)
141 local i = charprop_stack_table[idx][mc]