OSDN Git Service

Another optimize; \[x]kanjiskip in a paragraph shares one glue_spec node.
[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 local charprop_stack_table={}; charprop_stack_table[0]={}
21
22 -- modified from table.fastcopy
23 local next = next
24 local function stack_table_copy_aux(old) 
25    local new = {}
26    for i,v in next, old do
27       new[i] = v
28    end
29    return new
30 end
31 local function stack_table_copy(old) 
32    if old then 
33       local new = {}
34       for i,v in next, old do
35          new[i] = stack_table_copy_aux(v)
36       end
37       return new
38    else
39       return {}
40    end 
41 end
42
43 function get_stack_level()
44    local i = tex.getcount('ltj@@stack')
45    local j = tex.currentgrouplevel
46    if j > tex.getcount('ltj@@group@level') then
47       i = i+1 -- new stack level
48       local gd = tex.globaldefs
49       if gd>0 then tex.globaldefs = 0 end
50       --  'tex.globaldefs = 0' is local even if \globaldefs > 0.
51       tex.setcount('ltj@@group@level', j)
52       for k,v in pairs(charprop_stack_table) do -- clear the stack above i
53          if k>=i then charprop_stack_table[k]=nil end
54       end
55       charprop_stack_table[i] = stack_table_copy(charprop_stack_table[i-1])
56       tex.setcount('ltj@@stack', i)
57       if gd>0 then tex.globaldefs = gd end
58       if tex.nest[tex.nest.ptr].mode == hmode or
59          tex.nest[tex.nest.ptr].mode == -hmode then
60          local g = node_new(id_whatsit, sid_user)
61          g.user_id=30112; g.type=100; g.value=j; node.write(g)
62       end
63    end
64    return i
65 end
66
67 -- local function table_to_str(v) 
68 --    local s = ''
69 --    for i, a in pairs(v) do
70 --       s = s .. i .. "=" .. tostring(a) .. ', '
71 --    end
72 --    return s
73 -- end
74 -- function print_stack_table(i)
75 --    print('\n>>> get_stack_level:')
76 --    for k, v in pairs(charprop_stack_table[i]) do
77 --       print("  " , k, type(k), table_to_str(v));
78 --    end
79 -- end
80
81
82 -- EXT
83 function set_stack_table(g,m,c,p,lb,ub)
84    local i = get_stack_level()
85    if type(p)~='number' or p<lb or p>ub then
86       ltjb.package_error('luatexja',
87                          "invalid code (".. tostring(p) .. ")",
88                          "The code should in the range "..tostring(lb) .. '..' ..
89                          tostring(ub) .. ".\n" ..
90                       "I'm going to use 0 instead of that illegal code value.")
91       p=0
92    elseif type(c)~='number' or c<-1 or c>0x10ffff then
93       ltjb.package_error('luatexja',
94                          'bad character code (' .. tostring(c) .. ')',
95                          'A character number must be between -1 and 0x10ffff.\n' ..
96                          "(-1 is used for denoting `math boundary')\n" ..
97                          'So I changed this one to zero.')
98       c=0
99    elseif not charprop_stack_table[i][c] then 
100       charprop_stack_table[i][c] = {} 
101    end
102    charprop_stack_table[i][c][m] = p
103   if g=='global' then
104      for j,v in pairs(charprop_stack_table) do 
105         if not charprop_stack_table[j][c] then charprop_stack_table[j][c] = {} end
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    elseif not charprop_stack_table[i][c] then 
121       charprop_stack_table[i][c] = {} 
122    end
123    charprop_stack_table[i][c][m] = p
124    if g=='global' then
125      for j,v in pairs(charprop_stack_table) do 
126         if not charprop_stack_table[j][c] then charprop_stack_table[j][c] = {} end
127         charprop_stack_table[j][c][m] = p
128      end
129   end
130 end
131
132 -- EXT: store \ltj@tempskipa
133 function set_stack_skip(g,m,sp)
134   local i = get_stack_level()
135   if not sp then return end
136   if not charprop_stack_table[i][m] then 
137      charprop_stack_table[i][m] = {} 
138   end
139   charprop_stack_table[i][m].width   = sp.width
140   charprop_stack_table[i][m].stretch = sp.stretch
141   charprop_stack_table[i][m].shrink  = sp.shrink
142   charprop_stack_table[i][m].stretch_order = sp.stretch_order
143   charprop_stack_table[i][m].shrink_order  = sp.shrink_order
144   if g=='global' then
145      for j,v in pairs(charprop_stack_table) do 
146         if not charprop_stack_table[j][m] then charprop_stack_table[j][m] = {} end
147         charprop_stack_table[j][m].width   = sp.width
148         charprop_stack_table[j][m].stretch = sp.stretch
149         charprop_stack_table[j][m].shrink  = sp.shrink
150         charprop_stack_table[j][m].stretch_order = sp.stretch_order
151         charprop_stack_table[j][m].shrink_order  = sp.shrink_order
152      end
153   end
154 end
155
156 -- These three functions are used in ltj-jfmglue.lua.
157 local table_current_stack
158 function report_stack_level(bsl)
159    table_current_stack = charprop_stack_table[bsl]
160 end
161 function fast_get_skip_table(m)
162    return table_current_stack[m] 
163       or { width = 0, stretch = 0, shrink = 0, stretch_order = 0, shrink_order = 0 }
164 end
165 function fast_get_penalty_table(m,c)
166    local i = table_current_stack[c]
167    return (i and i[m])
168 end
169
170 local empty_table = {}
171 function fast_get_penalty_table_parent(c)
172    return table_current_stack[c] or empty_table
173 end
174
175 -- For other situations, use the following instead:
176 function get_skip_table(m, idx)
177    return charprop_stack_table[idx][m] 
178       or { width = 0, stretch = 0, shrink = 0, stretch_order = 0, shrink_order = 0 }
179 end
180 function get_penalty_table(m,c,d, idx)
181    local i = charprop_stack_table[idx][c]
182    return (i and i[m]) or d
183 end
184
185
186 -- EOF