OSDN Git Service

test: flatten the stack table
[luatex-ja/luatexja.git] / src / ltj-stack.lua
1 --
2 -- luatexja/stack.lua
3 --
4 luatexbase.provides_module({
5   name = 'luatexja.stack',
6   date = '2011/04/01',
7   version = '0.1',
8   description = 'LuaTeX-ja stack system',
9 })
10 module('luatexja.stack', package.seeall)
11 local err, warn, info, log = luatexbase.errwarinf(_NAME)
12
13 luatexja.load_module('base');      local ltjb = luatexja.base
14
15 local node_new = node.new
16 local id_whatsit = node.id('whatsit')
17 local sid_user = node.subtype('user_defined')
18 local hmode = 118 -- in luatexref-t.pdf, this must be 127
19
20 charprop_stack_table={}; 
21 local charprop_stack_table = charprop_stack_table
22 charprop_stack_table[0]={}
23
24 -- modified from table.fastcopy
25 local next = next
26 local function stack_table_copy_aux(old) 
27    local new = {}
28    for i,v in next, old do
29       new[i] = v
30    end
31    return new
32 end
33 local function stack_table_copy(old) 
34    if old then 
35       local new = {}
36       for i,v in next, old do
37          new[i] = stack_table_copy_aux(v)
38       end
39       return new
40    else
41       return {}
42    end 
43 end
44
45 function get_stack_level()
46    local i = tex.getcount('ltj@@stack')
47    local j = tex.currentgrouplevel
48    if j > tex.getcount('ltj@@group@level') then
49       i = i+1 -- new stack level
50       local gd = tex.globaldefs
51       if gd>0 then tex.globaldefs = 0 end
52       --  'tex.globaldefs = 0' is local even if \globaldefs > 0.
53       tex.setcount('ltj@@group@level', j)
54       for k,v in pairs(charprop_stack_table) do -- clear the stack above i
55          if k>=i then charprop_stack_table[k]=nil end
56       end
57       charprop_stack_table[i] = stack_table_copy(charprop_stack_table[i-1])
58       tex.setcount('ltj@@stack', i)
59       if gd>0 then tex.globaldefs = gd end
60       if tex.nest[tex.nest.ptr].mode == hmode or
61          tex.nest[tex.nest.ptr].mode == -hmode then
62          local g = node_new(id_whatsit, sid_user)
63          g.user_id=30112; g.type=100; g.value=j; node.write(g)
64       end
65    end
66    return i
67 end
68
69 -- local function table_to_str(v) 
70 --    local s = ''
71 --    for i, a in pairs(v) do
72 --       s = s .. i .. "=" .. tostring(a) .. ', '
73 --    end
74 --    return s
75 -- end
76 -- function print_stack_table(i)
77 --    print('\n>>> get_stack_level:')
78 --    for k, v in pairs(charprop_stack_table[i]) do
79 --       print("  " , k, type(k), table_to_str(v));
80 --    end
81 -- end
82
83
84 -- EXT
85 function set_stack_table(g,m,c,p,lb,ub)
86    print(g,m,c)
87    local i = get_stack_level()
88    if type(p)~='number' or p<lb or p>ub then
89       ltjb.package_error('luatexja',
90                          "invalid code (".. tostring(p) .. ")",
91                          "The code should in the range "..tostring(lb) .. '..' ..
92                          tostring(ub) .. ".\n" ..
93                       "I'm going to use 0 instead of that illegal code value.")
94       p=0
95    elseif type(c)~='number' or c<-1 or c>0x10ffff then
96       ltjb.package_error('luatexja',
97                          'bad character code (' .. tostring(c) .. ')',
98                          'A character number must be between -1 and 0x10ffff.\n' ..
99                          "(-1 is used for denoting `math boundary')\n" ..
100                          'So I changed this one to zero.')
101       c=0
102    end
103    charprop_stack_table[i][c+m] = p
104   if g=='global' then
105      for j,v in pairs(charprop_stack_table) do 
106         charprop_stack_table[j][c+m] = p
107      end
108   end
109 end
110
111 -- EXT
112 function set_stack_font(g,m,c,p)
113    local i = get_stack_level()
114    if type(c)~='number' or c<0 or c>255 then 
115       ltjb.package_error('luatexja',
116                          "invalid family number (".. tostring(c) .. ")",
117                          "The family number should in the range 0 .. 255.\n" ..
118                           "I'm going to use 0 instead of that illegal family number.")
119       c=0
120    end
121    charprop_stack_table[i][c+m] = p
122    if g=='global' then
123      for j,v in pairs(charprop_stack_table) do 
124         charprop_stack_table[j][c+m] = p
125      end
126   end
127 end
128
129 -- EXT: store \ltj@tempskipa
130 function set_stack_skip(g,m,sp)
131   local i = get_stack_level()
132   if not sp then return end
133   if not charprop_stack_table[i][m] then 
134      charprop_stack_table[i][m] = {} 
135   end
136   charprop_stack_table[i][m].width   = sp.width
137   charprop_stack_table[i][m].stretch = sp.stretch
138   charprop_stack_table[i][m].shrink  = sp.shrink
139   charprop_stack_table[i][m].stretch_order = sp.stretch_order
140   charprop_stack_table[i][m].shrink_order  = sp.shrink_order
141   if g=='global' then
142      for j,v in pairs(charprop_stack_table) do 
143         if not charprop_stack_table[j][m] then charprop_stack_table[j][m] = {} end
144         charprop_stack_table[j][m].width   = sp.width
145         charprop_stack_table[j][m].stretch = sp.stretch
146         charprop_stack_table[j][m].shrink  = sp.shrink
147         charprop_stack_table[j][m].stretch_order = sp.stretch_order
148         charprop_stack_table[j][m].shrink_order  = sp.shrink_order
149      end
150   end
151 end
152
153 -- These three functions are used in ltj-jfmglue.lua.
154 function report_stack_level(bsl)
155    table_current_stack = charprop_stack_table[bsl]
156 end
157 function fast_get_skip_table(m)
158    return table_current_stack[m] 
159       or { width = 0, stretch = 0, shrink = 0, stretch_order = 0, shrink_order = 0 }
160 end
161
162 -- For other situations, use the following instead:
163 function get_skip_table(m, idx)
164    return charprop_stack_table[idx][m] 
165       or { width = 0, stretch = 0, shrink = 0, stretch_order = 0, shrink_order = 0 }
166 end
167 function get_penalty_table(mc, d, idx)
168    local i = charprop_stack_table[idx][mc]
169    return i or d
170 end
171
172
173 -- EOF