OSDN Git Service

bugfix
[luatex-ja/luatexja.git] / src / ltj-stack.lua
1 --
2 -- luatexja/stack.lua
3 --
4 luatexbase.provides_module({
5   name = 'luatexja.stack',
6   date = '2018/02/18',
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 = tex.setcount
22 local scan_int, scan_keyword = token.scan_int, token.scan_keyword
23 ltjs.hmode = 0 -- dummy
24
25 local charprop_stack_table={};
26 ltjs.charprop_stack_table = charprop_stack_table
27 charprop_stack_table[0]={}
28
29
30 local function get_stack_level()
31    local i = tex.getcount('ltj@@stack')
32    local j = tex.currentgrouplevel
33    if j > tex.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
41       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)
48       end
49    end
50    return i
51 end
52 ltjs.get_stack_level = get_stack_level
53
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
59          charprop_stack_table[j][m] = p
60       end
61    end
62 end
63 ltjs.set_stack_table = set_stack_table
64
65 -- EXT
66 function ltjs.set_stack_perchar(m,lb,ub, getter)
67    local c = scan_int()
68    scan_keyword(',')
69    local p = tonumber((getter or scan_int)())
70    if p<lb or p>ub then
71       ltjb.package_error('luatexja',
72                          "invalid code (".. tostring(p) .. ")",
73                          "The code should in the range "..tostring(lb) .. '..' ..
74                          tostring(ub) .. ".\n" ..
75                       "I'm going to use 0 instead of that illegal code value.")
76       p=0
77    end
78    set_stack_table(m+ltjb.in_unicode(c, true), p)
79 end
80
81 -- EXT
82 function ltjs.set_stack_font(m,c,p)
83    if type(c)~='number' or c<0 or c>255 then
84       ltjb.package_error('luatexja',
85                          "invalid family number (".. tostring(c) .. ")",
86                          "The family number should in the range 0 .. 255.\n" ..
87                           "I'm going to use 0 instead of that illegal family number.")
88       c=0
89    end
90    set_stack_table(m+c, p)
91 end
92
93 -- EXT: sp: glue_spec
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   charprop_stack_table[i][m].width   = sp.width
101   charprop_stack_table[i][m].stretch = sp.stretch
102   charprop_stack_table[i][m].shrink  = sp.shrink
103   charprop_stack_table[i][m].stretch_order = sp.stretch_order
104   charprop_stack_table[i][m].shrink_order  = sp.shrink_order
105   if luatexja.isglobal=='global' then
106      for j,v in pairs(charprop_stack_table) do
107         if not charprop_stack_table[j][m] then charprop_stack_table[j][m] = {} end
108         charprop_stack_table[j][m].width   = sp.width
109         charprop_stack_table[j][m].stretch = sp.stretch
110         charprop_stack_table[j][m].shrink  = sp.shrink
111         charprop_stack_table[j][m].stretch_order = sp.stretch_order
112         charprop_stack_table[j][m].shrink_order  = sp.shrink_order
113      end
114   end
115 end
116
117 -- These three functions are used in ltj-jfmglue.lua.
118 -- list_dir and orig_char_table are used in other lua files.
119 local orig_char_table = {}
120 ltjs.orig_char_table = orig_char_table
121 ltjs.list_dir = nil -- dummy
122 ltjs.table_current_stack = nil -- dummy
123 function ltjs.report_stack_level(bsl)
124    ltjs.table_current_stack = charprop_stack_table[bsl]
125    return bsl
126 end
127 function ltjs.fast_get_stack_skip(m)
128    return ltjs.table_current_stack[m]
129       or { width = 0, stretch = 0, shrink = 0, stretch_order = 0, shrink_order = 0 }
130 end
131
132 -- For other situations, use the following instead:
133 function ltjs.get_stack_skip(m, idx)
134    return charprop_stack_table[idx][m]
135       or { width = 0, stretch = 0, shrink = 0, stretch_order = 0, shrink_order = 0 }
136 end
137 function ltjs.get_stack_table(mc, d, idx)
138    local i = charprop_stack_table[idx][mc]
139    return i or d
140 end
141
142
143 -- EOF