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.")
74 elseif type(c)~='number' or c<-1 or c>0x10ffff then
75 ltjb.package_error('luatexja',
76 'bad character code (' .. tostring(c) .. ')',
77 'A character number must be between -1 and 0x10ffff.\n' ..
78 "(-1 is used for denoting `math boundary')\n" ..
79 'So I changed this one to zero.')
82 charprop_stack_table[i][c+m] = p
84 for j,v in pairs(charprop_stack_table) do
85 charprop_stack_table[j][c+m] = p
91 function set_stack_font(g,m,c,p)
92 local i = get_stack_level()
93 if type(c)~='number' or c<0 or c>255 then
94 ltjb.package_error('luatexja',
95 "invalid family number (".. tostring(c) .. ")",
96 "The family number should in the range 0 .. 255.\n" ..
97 "I'm going to use 0 instead of that illegal family number.")
100 charprop_stack_table[i][c+m] = p
102 for j,v in pairs(charprop_stack_table) do
103 charprop_stack_table[j][c+m] = p
108 -- EXT: store \ltj@tempskipa
109 function set_stack_skip(g,m,sp)
110 local i = get_stack_level()
111 if not sp then return end
112 if not charprop_stack_table[i][m] then
113 charprop_stack_table[i][m] = {}
115 charprop_stack_table[i][m].width = sp.width
116 charprop_stack_table[i][m].stretch = sp.stretch
117 charprop_stack_table[i][m].shrink = sp.shrink
118 charprop_stack_table[i][m].stretch_order = sp.stretch_order
119 charprop_stack_table[i][m].shrink_order = sp.shrink_order
121 for j,v in pairs(charprop_stack_table) do
122 if not charprop_stack_table[j][m] then charprop_stack_table[j][m] = {} end
123 charprop_stack_table[j][m].width = sp.width
124 charprop_stack_table[j][m].stretch = sp.stretch
125 charprop_stack_table[j][m].shrink = sp.shrink
126 charprop_stack_table[j][m].stretch_order = sp.stretch_order
127 charprop_stack_table[j][m].shrink_order = sp.shrink_order
132 -- These three functions are used in ltj-jfmglue.lua.
133 function report_stack_level(bsl)
134 table_current_stack = charprop_stack_table[bsl]
136 function fast_get_skip_table(m)
137 return table_current_stack[m]
138 or { width = 0, stretch = 0, shrink = 0, stretch_order = 0, shrink_order = 0 }
141 -- For other situations, use the following instead:
142 function get_skip_table(m, idx)
143 return charprop_stack_table[idx][m]
144 or { width = 0, stretch = 0, shrink = 0, stretch_order = 0, shrink_order = 0 }
146 function get_penalty_table(mc, d, idx)
147 local i = charprop_stack_table[idx][mc]