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 ltjs.hmode = 0 -- dummy
25 local charprop_stack_table={};
26 ltjs.charprop_stack_table = charprop_stack_table
27 charprop_stack_table[0]={}
29 local function get_stack_level()
30 local i = getcount 'ltj@@stack'
31 local j = tex.currentgrouplevel
32 if j > getcount 'ltj@@group@level' then
33 i = i+1 -- new stack level
34 local gd = tex.globaldefs
35 if gd~=0 then tex.globaldefs = 0 end
36 -- 'tex.globaldefs = 0' is local even if \globaldefs > 0.
37 setcount('ltj@@group@level', j)
38 for k,v in pairs(charprop_stack_table) do -- clear the stack above i
39 if k>=i then charprop_stack_table[k]=nil end
41 charprop_stack_table[i] = fastcopy(charprop_stack_table[i-1])
42 setcount('ltj@@stack', i)
43 if gd~=0 then tex.globaldefs = gd end
44 if tex.nest[tex.nest.ptr].mode == -ltjs.hmode then -- rest. hmode のみ
45 local g = node_new(id_whatsit, sid_user)
46 g.user_id=STCK; g.type=100; g.value=j; node.write(g)
51 ltjs.get_stack_level = get_stack_level
53 local function set_stack_table(m, p)
54 local i = get_stack_level()
55 charprop_stack_table[i][m] = p
56 if luatexja.isglobal=='global' then
57 for j,v in pairs(charprop_stack_table) do
58 charprop_stack_table[j][m] = p
62 ltjs.set_stack_table = set_stack_table
65 function ltjs.set_stack_perchar(m,lb,ub, getter)
68 local p = tonumber((getter or scan_int)())
70 ltjb.package_error('luatexja',
71 "invalid code (".. tostring(p) .. ")",
72 "The code should in the range "..tostring(lb) .. '..' ..
73 tostring(ub) .. ".\n" ..
74 "I'm going to use 0 instead of that illegal code value.")
77 set_stack_table(m+ltjb.in_unicode(c, true), p)
81 function ltjs.set_stack_font(m,c,p)
82 if type(c)~='number' or c<0 or c>255 then
83 ltjb.package_error('luatexja',
84 "invalid family number (".. tostring(c) .. ")",
85 "The family number should in the range 0 .. 255.\n" ..
86 "I'm going to use 0 instead of that illegal family number.")
89 set_stack_table(m+c, p)
93 function ltjs.set_stack_skip(m,sp)
94 local i = get_stack_level()
95 if not sp then return end
96 if not charprop_stack_table[i][m] then
97 charprop_stack_table[i][m] = {}
99 charprop_stack_table[i][m].width = sp.width
100 charprop_stack_table[i][m].stretch = sp.stretch
101 charprop_stack_table[i][m].shrink = sp.shrink
102 charprop_stack_table[i][m].stretch_order = sp.stretch_order
103 charprop_stack_table[i][m].shrink_order = sp.shrink_order
104 if luatexja.isglobal=='global' then
105 for j,v in pairs(charprop_stack_table) do
106 if not charprop_stack_table[j][m] then charprop_stack_table[j][m] = {} end
107 charprop_stack_table[j][m].width = sp.width
108 charprop_stack_table[j][m].stretch = sp.stretch
109 charprop_stack_table[j][m].shrink = sp.shrink
110 charprop_stack_table[j][m].stretch_order = sp.stretch_order
111 charprop_stack_table[j][m].shrink_order = sp.shrink_order
116 -- These three functions are used in ltj-jfmglue.lua.
117 -- list_dir and orig_char_table are used in other lua files.
118 local orig_char_table = {}
119 ltjs.orig_char_table = orig_char_table
120 ltjs.list_dir = nil -- dummy
121 ltjs.table_current_stack = nil -- dummy
122 function ltjs.report_stack_level(bsl)
123 ltjs.table_current_stack = charprop_stack_table[bsl]
126 function ltjs.fast_get_stack_skip(m)
127 return ltjs.table_current_stack[m]
128 or { width = 0, stretch = 0, shrink = 0, stretch_order = 0, shrink_order = 0 }
131 -- For other situations, use the following instead:
132 function ltjs.get_stack_skip(m, idx)
133 return charprop_stack_table[idx][m]
134 or { width = 0, stretch = 0, shrink = 0, stretch_order = 0, shrink_order = 0 }
136 function ltjs.get_stack_table(mc, d, idx)
137 local i = charprop_stack_table[idx][mc]