4 luatexbase.provides_module({
5 name = 'luatexja.stack',
7 description = 'LuaTeX-ja stack system',
10 local ltjs=luatexja.stack
11 luatexja.load_module 'base'; local ltjb = luatexja.base
13 --------------------------------------------------------------------------------
14 -- stack table (obeys TeX's grouping)
15 --------------------------------------------------------------------------------
16 local node_new = node.new
17 local id_whatsit = node.id 'whatsit'
18 local sid_user = node.subtype 'user_defined'
19 local STCK = luatexja.userid_table.STCK
20 local fastcopy = table.fastcopy
21 local setcount, getcount = tex.setcount, tex.getcount
22 local scan_int, scan_keyword = token.scan_int, token.scan_keyword
23 local tex_nest = tex.nest
24 ltjs.hmode = 0 -- dummy
26 local charprop_stack_table={}
27 ltjs.charprop_stack_table = charprop_stack_table
28 charprop_stack_table[0]={}
30 local function get_stack_level()
31 local i = getcount 'ltj@@stack'
32 local j = tex.currentgrouplevel
33 if j > getcount 'ltj@@group@level' then
34 i = i+1 -- new stack level
35 local gd = tex.globaldefs
36 if gd~=0 then tex.globaldefs = 0 end
37 -- 'tex.globaldefs = 0' is local even if \globaldefs > 0.
38 setcount('ltj@@group@level', j)
39 for k,v in pairs(charprop_stack_table) do -- clear the stack above i
40 if k>=i then charprop_stack_table[k]=nil end
42 charprop_stack_table[i] = fastcopy(charprop_stack_table[i-1])
43 setcount('ltj@@stack', i)
44 if gd~=0 then tex.globaldefs = gd end
45 if tex_nest[tex_nest.ptr].mode == -ltjs.hmode then -- rest. hmode のみ
46 local g = node_new(id_whatsit, sid_user)
47 g.user_id=STCK; g.type=100; g.value=j; node.write(g)
52 ltjs.get_stack_level = get_stack_level
54 local function set_stack_table(m, p)
55 local i = get_stack_level()
56 charprop_stack_table[i][m] = p
57 if luatexja.isglobal=='global' then
58 for j,v in pairs(charprop_stack_table) do v[m] = p end
61 ltjs.set_stack_table = set_stack_table
64 function ltjs.set_stack_perchar(m,lb,ub, getter)
67 local p = tonumber((getter or scan_int)())
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.")
76 set_stack_table(m+ltjb.in_unicode(c, true), p)
80 function ltjs.set_stack_font(m,c,p)
81 if type(c)~='number' or c<0 or c>255 then
82 ltjb.package_error('luatexja',
83 "invalid family number (".. tostring(c) .. ")",
84 "The family number should in the range 0 .. 255.\n" ..
85 "I'm going to use 0 instead of that illegal family number.")
88 set_stack_table(m+c, p)
92 local getglue = node.getglue
93 function ltjs.set_stack_skip(m,sp)
94 local i = get_stack_level()
95 if not sp then return end
96 local w,st,sh,sto,sho = getglue(sp)
97 if charprop_stack_table[i][m] then
98 local c = charprop_stack_table[i][m]
99 c[1], c[2], c[3], c[4], c[5] = w, st, sh, sto, sho
101 charprop_stack_table[i][m] = { w,st,sh,sto,sho }
103 if luatexja.isglobal=='global' then
104 for j,v in pairs(charprop_stack_table) do
105 if not v[m] then v[m] = { true,true,true,true,true } end
107 c[1], c[2], c[3], c[4], c[5] = w, st, sh, sto, sho
112 -- These three functions are used in ltj-jfmglue.lua.
113 -- list_dir and orig_char_table are used in other lua files.
114 local orig_char_table = {}
115 ltjs.orig_char_table = orig_char_table
116 ltjs.list_dir = nil -- dummy
117 ltjs.table_current_stack = nil -- dummy
118 local dummy_skip_table = { 0,0,0,0,0 }
119 function ltjs.report_stack_level(bsl)
120 ltjs.table_current_stack = charprop_stack_table[bsl]
123 function ltjs.fast_get_stack_skip(m)
124 return ltjs.table_current_stack[m] or dummy_skip_table
127 -- For other situations, use the following instead:
128 function ltjs.get_stack_skip(m, idx)
129 return charprop_stack_table[idx][m] or dummy_skip_table
131 function ltjs.get_stack_table(mc, d, idx)
132 return charprop_stack_table[idx][mc] or d