OSDN Git Service

de985d591751efee7944d9b2ed5ff9063e8d55f5
[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    local i = get_stack_level()
87    if type(p)~='number' or p<lb or p>ub then
88       ltjb.package_error('luatexja',
89                          "invalid code (".. tostring(p) .. ")",
90                          "The code should in the range "..tostring(lb) .. '..' ..
91                          tostring(ub) .. ".\n" ..
92                       "I'm going to use 0 instead of that illegal code value.")
93       p=0
94    elseif type(c)~='number' or c<-1 or c>0x10ffff then
95       ltjb.package_error('luatexja',
96                          'bad character code (' .. tostring(c) .. ')',
97                          'A character number must be between -1 and 0x10ffff.\n' ..
98                          "(-1 is used for denoting `math boundary')\n" ..
99                          'So I changed this one to zero.')
100       c=0
101    elseif not charprop_stack_table[i][c] then 
102       charprop_stack_table[i][c] = {} 
103    end
104    charprop_stack_table[i][c][m] = p
105   if g=='global' then
106      for j,v in pairs(charprop_stack_table) do 
107         if not charprop_stack_table[j][c] then charprop_stack_table[j][c] = {} end
108         charprop_stack_table[j][c][m] = p
109      end
110   end
111 end
112
113 -- EXT
114 function set_stack_font(g,m,c,p)
115    local i = get_stack_level()
116    if type(c)~='number' or c<0 or c>255 then 
117       ltjb.package_error('luatexja',
118                          "invalid family number (".. tostring(c) .. ")",
119                          "The family number should in the range 0 .. 255.\n" ..
120                           "I'm going to use 0 instead of that illegal family number.")
121       c=0
122    elseif not charprop_stack_table[i][c] then 
123       charprop_stack_table[i][c] = {} 
124    end
125    charprop_stack_table[i][c][m] = p
126    if g=='global' then
127      for j,v in pairs(charprop_stack_table) do 
128         if not charprop_stack_table[j][c] then charprop_stack_table[j][c] = {} end
129         charprop_stack_table[j][c][m] = p
130      end
131   end
132 end
133
134 -- EXT: store \ltj@tempskipa
135 function set_stack_skip(g,m,sp)
136   local i = get_stack_level()
137   if not sp then return end
138   if not charprop_stack_table[i][m] then 
139      charprop_stack_table[i][m] = {} 
140   end
141   charprop_stack_table[i][m].width   = sp.width
142   charprop_stack_table[i][m].stretch = sp.stretch
143   charprop_stack_table[i][m].shrink  = sp.shrink
144   charprop_stack_table[i][m].stretch_order = sp.stretch_order
145   charprop_stack_table[i][m].shrink_order  = sp.shrink_order
146   if g=='global' then
147      for j,v in pairs(charprop_stack_table) do 
148         if not charprop_stack_table[j][m] then charprop_stack_table[j][m] = {} end
149         charprop_stack_table[j][m].width   = sp.width
150         charprop_stack_table[j][m].stretch = sp.stretch
151         charprop_stack_table[j][m].shrink  = sp.shrink
152         charprop_stack_table[j][m].stretch_order = sp.stretch_order
153         charprop_stack_table[j][m].shrink_order  = sp.shrink_order
154      end
155   end
156 end
157
158 -- These three functions are used in ltj-jfmglue.lua.
159 local table_current_stack
160 function report_stack_level(bsl)
161    table_current_stack = charprop_stack_table[bsl]
162 end
163 function fast_get_skip_table(m)
164    return table_current_stack[m] 
165       or { width = 0, stretch = 0, shrink = 0, stretch_order = 0, shrink_order = 0 }
166 end
167 function fast_get_penalty_table(m,c)
168    local i = table_current_stack[c]
169    return (i and i[m])
170 end
171
172 local empty_table = {}
173 function fast_get_penalty_table_parent(c)
174    return table_current_stack[c] or empty_table
175 end
176
177 -- For other situations, use the following instead:
178 function get_skip_table(m, idx)
179    return charprop_stack_table[idx][m] 
180       or { width = 0, stretch = 0, shrink = 0, stretch_order = 0, shrink_order = 0 }
181 end
182 function get_penalty_table(m,c,d, idx)
183    local i = charprop_stack_table[idx][c]
184    return (i and i[m]) or d
185 end
186
187
188 -- EOF