OSDN Git Service

ltj-stack.lua: use getglue()
[luatex-ja/luatexja.git] / src / ltj-stack.lua
1 --
2 -- ltj-stack.lua
3 --
4 luatexbase.provides_module({
5   name = 'luatexja.stack',
6   date = '2022-08-17',
7   description = 'LuaTeX-ja stack system',
8 })
9 luatexja.stack = {}
10 local ltjs=luatexja.stack
11 luatexja.load_module 'base';      local ltjb = luatexja.base
12
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
25
26 local charprop_stack_table={};
27
28 ltjs.charprop_stack_table = charprop_stack_table
29 charprop_stack_table[0]={}
30
31 local function get_stack_level()
32    local i = getcount 'ltj@@stack'
33    local j = tex.currentgrouplevel
34    if j > getcount 'ltj@@group@level' then
35       i = i+1 -- new stack level
36       local gd = tex.globaldefs
37       if gd~=0 then tex.globaldefs = 0 end
38       --  'tex.globaldefs = 0' is local even if \globaldefs > 0.
39       setcount('ltj@@group@level', j)
40       for k,v in pairs(charprop_stack_table) do -- clear the stack above i
41          if k>=i then charprop_stack_table[k]=nil end
42       end
43       charprop_stack_table[i] = fastcopy(charprop_stack_table[i-1])
44       setcount('ltj@@stack', i)
45       if gd~=0 then tex.globaldefs = gd end
46       if  tex_nest[tex_nest.ptr].mode == -ltjs.hmode then -- rest. hmode のみ
47          local g = node_new(id_whatsit, sid_user)
48          g.user_id=STCK; g.type=100; g.value=j; node.write(g)
49       end
50    end
51    return i
52 end
53 ltjs.get_stack_level = get_stack_level
54
55 local function set_stack_table(m, p)
56    local i = get_stack_level()
57    charprop_stack_table[i][m] = p
58    if luatexja.isglobal=='global' then
59       for j,v in pairs(charprop_stack_table) do v[m] = p end
60    end
61 end
62 ltjs.set_stack_table = set_stack_table
63
64 -- EXT
65 function ltjs.set_stack_perchar(m,lb,ub, getter)
66    local c = scan_int()
67    scan_keyword(',')
68    local p = tonumber((getter or scan_int)())
69    if p<lb or p>ub then
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.")
75       p=0
76    end
77    set_stack_table(m+ltjb.in_unicode(c, true), p)
78 end
79
80 -- EXT
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.")
87       c=0
88    end
89    set_stack_table(m+c, p)
90 end
91
92 -- EXT: sp: glue_spec
93 local getglue = node.getglue
94 function ltjs.set_stack_skip(m,sp)
95   local i = get_stack_level()
96   if not sp then return end
97   if not charprop_stack_table[i][m] then
98      charprop_stack_table[i][m] = {}
99   end
100   local w,st,sh,sto,sho = getglue(sp)
101   local c = charprop_stack_table[i][m]
102   c.width, c.stretch, c.shrink, c.stretch_order, c.shrink_order = 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] = {} end
106         local c = v[m]
107         c.width, c.stretch, c.shrink, c.stretch_order, c.shrink_order = w, st, sh, sto, sho
108      end
109   end
110 end
111
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 = { width = 0, stretch = 0, shrink = 0, stretch_order = 0, shrink_order = 0 }
119 function ltjs.report_stack_level(bsl)
120    ltjs.table_current_stack = charprop_stack_table[bsl]
121    return bsl
122 end
123 function ltjs.fast_get_stack_skip(m)
124    return ltjs.table_current_stack[m] or dummy_skip_table
125 end
126
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
130 end
131 function ltjs.get_stack_table(mc, d, idx)
132    return charprop_stack_table[idx][mc] or d
133 end
134
135
136 -- EOF