OSDN Git Service

Fixed several bugs caused by the previous commit
[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 hmode = 0 -- dummy 
19
20 charprop_stack_table={}; 
21 local charprop_stack_table = charprop_stack_table
22 charprop_stack_table[0]={}
23
24
25 function get_stack_level()
26    local i = tex.getcount('ltj@@stack')
27    local j = tex.currentgrouplevel
28    if j > tex.getcount('ltj@@group@level') then
29       i = i+1 -- new stack level
30       local gd = tex.globaldefs
31       if gd>0 then tex.globaldefs = 0 end
32       --  'tex.globaldefs = 0' is local even if \globaldefs > 0.
33       tex.setcount('ltj@@group@level', j)
34       for k,v in pairs(charprop_stack_table) do -- clear the stack above i
35          if k>=i then charprop_stack_table[k]=nil end
36       end
37       charprop_stack_table[i] = table.fastcopy(charprop_stack_table[i-1])
38       tex.setcount('ltj@@stack', i)
39       if gd>0 then tex.globaldefs = gd end
40       if tex.nest[tex.nest.ptr].mode == hmode or
41          tex.nest[tex.nest.ptr].mode == -hmode then
42          local g = node_new(id_whatsit, sid_user)
43          g.user_id=30112; g.type=100; g.value=j; node.write(g)
44       end
45    end
46    return i
47 end
48
49 -- local function table_to_str(v) 
50 --    local s = ''
51 --    for i, a in pairs(v) do
52 --       s = s .. i .. "=" .. tostring(a) .. ', '
53 --    end
54 --    return s
55 -- end
56 -- function print_stack_table(i)
57 --    print('\n>>> get_stack_level:')
58 --    for k, v in pairs(charprop_stack_table[i]) do
59 --       print("  " , k, type(k), table_to_str(v));
60 --    end
61 -- end
62
63
64 -- EXT
65 function set_stack_table(g,m,c,p,lb,ub)
66    local i = get_stack_level()
67    if type(p)~='number' or p<lb or p>ub then
68       ltjb.package_error('luatexja',
69                          "invalid code (".. tostring(p) .. ")",
70                          "The code should in the range "..tostring(lb) .. '..' ..
71                          tostring(ub) .. ".\n" ..
72                       "I'm going to use 0 instead of that illegal code value.")
73       p=0
74    elseif type(c)~='number' or c<-1 or c>0x10ffff then
75       ltjb.package_error('luatexja',
76                          'bad character code (' .. tostring(c) .. ')',
77                          'A character number must be between -1 and 0x10ffff.\n' ..
78                          "(-1 is used for denoting `math boundary')\n" ..
79                          'So I changed this one to zero.')
80       c=0
81    end
82    charprop_stack_table[i][c+m] = p
83   if g=='global' then
84      for j,v in pairs(charprop_stack_table) do 
85         charprop_stack_table[j][c+m] = p
86      end
87   end
88 end
89
90 -- EXT
91 function set_stack_font(g,m,c,p)
92    local i = get_stack_level()
93    if type(c)~='number' or c<0 or c>255 then 
94       ltjb.package_error('luatexja',
95                          "invalid family number (".. tostring(c) .. ")",
96                          "The family number should in the range 0 .. 255.\n" ..
97                           "I'm going to use 0 instead of that illegal family number.")
98       c=0
99    end
100    charprop_stack_table[i][c+m] = p
101    if g=='global' then
102      for j,v in pairs(charprop_stack_table) do 
103         charprop_stack_table[j][c+m] = p
104      end
105   end
106 end
107
108 -- EXT: store \ltj@tempskipa
109 function set_stack_skip(g,m,sp)
110   local i = get_stack_level()
111   if not sp then return end
112   if not charprop_stack_table[i][m] then 
113      charprop_stack_table[i][m] = {} 
114   end
115   charprop_stack_table[i][m].width   = sp.width
116   charprop_stack_table[i][m].stretch = sp.stretch
117   charprop_stack_table[i][m].shrink  = sp.shrink
118   charprop_stack_table[i][m].stretch_order = sp.stretch_order
119   charprop_stack_table[i][m].shrink_order  = sp.shrink_order
120   if g=='global' then
121      for j,v in pairs(charprop_stack_table) do 
122         if not charprop_stack_table[j][m] then charprop_stack_table[j][m] = {} end
123         charprop_stack_table[j][m].width   = sp.width
124         charprop_stack_table[j][m].stretch = sp.stretch
125         charprop_stack_table[j][m].shrink  = sp.shrink
126         charprop_stack_table[j][m].stretch_order = sp.stretch_order
127         charprop_stack_table[j][m].shrink_order  = sp.shrink_order
128      end
129   end
130 end
131
132 -- These three functions are used in ltj-jfmglue.lua.
133 function report_stack_level(bsl)
134    table_current_stack = charprop_stack_table[bsl]
135 end
136 function fast_get_skip_table(m)
137    return table_current_stack[m] 
138       or { width = 0, stretch = 0, shrink = 0, stretch_order = 0, shrink_order = 0 }
139 end
140
141 -- For other situations, use the following instead:
142 function get_skip_table(m, idx)
143    return charprop_stack_table[idx][m] 
144       or { width = 0, stretch = 0, shrink = 0, stretch_order = 0, shrink_order = 0 }
145 end
146 function get_penalty_table(mc, d, idx)
147    local i = charprop_stack_table[idx][mc]
148    return i or d
149 end
150
151
152 -- EOF