OSDN Git Service

Modified luatexja.load_module and renamed some .sty.
[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 p<lb or p>ub then 
86       ltjb.package_error('luatexja',
87                          "invalid code (".. p .. ")",
88                          {"The code should in the range "..tostring(lb) ..'..'.. tostring(ub) .. ".",
89                           "I'm going to use 0 instead of that illegal code value."})
90       p=0
91    elseif c<-1 or c>0x10ffff then 
92       ltjb.package_error('luatexja',
93                          'bad character code (' .. c .. ')',
94                          {'A character number must be between -1 and 0x10ffff.',
95                           "(-1 is used for denoting `math boundary')",
96                           'So I changed this one to zero.'})
97       c=0
98    elseif not charprop_stack_table[i][m] then 
99       charprop_stack_table[i][m] = {} 
100    end
101    charprop_stack_table[i][m][c] = p
102   if g=='global' then
103      for j,v in pairs(charprop_stack_table) do 
104         if not charprop_stack_table[j][m] then charprop_stack_table[j][m] = {} end
105         charprop_stack_table[j][m][c] = p
106      end
107   end
108 end
109
110 -- EXT
111 function set_stack_font(g,m,c,p)
112    local i = get_stack_level()
113    if c<0 or c>255 then 
114       ltjb.package_error('luatexja',
115                          "invalid family number (".. p .. ")",
116                          {"The family number should in the range 0 .. 255.",
117                           "I'm going to use 0 instead of that illegal family number."})
118       c=0
119    elseif not charprop_stack_table[i][m] then 
120       charprop_stack_table[i][m] = {} 
121    end
122    charprop_stack_table[i][m][c] = p
123   if g=='global' then
124      for j,v in pairs(charprop_stack_table) do 
125         if not charprop_stack_table[j][m] then charprop_stack_table[j][m] = {} end
126         charprop_stack_table[j][m][c] = p
127      end
128   end
129 end
130
131 -- EXT: store \ltj@tempskipa
132 function set_stack_skip(g,m,sp)
133   local i = get_stack_level()
134   if not sp then return end
135   if not charprop_stack_table[i][m] then 
136      charprop_stack_table[i][m] = {} 
137   end
138   charprop_stack_table[i][m].width   = sp.width
139   charprop_stack_table[i][m].stretch = sp.stretch
140   charprop_stack_table[i][m].shrink  = sp.shrink
141   charprop_stack_table[i][m].stretch_order = sp.stretch_order
142   charprop_stack_table[i][m].shrink_order  = sp.shrink_order
143   if g=='global' then
144      for j,v in pairs(charprop_stack_table) do 
145         if not charprop_stack_table[j][m] then charprop_stack_table[j][m] = {} end
146         charprop_stack_table[j][m].width   = sp.width
147         charprop_stack_table[j][m].stretch = sp.stretch
148         charprop_stack_table[j][m].shrink  = sp.shrink
149         charprop_stack_table[j][m].stretch_order = sp.stretch_order
150         charprop_stack_table[j][m].shrink_order  = sp.shrink_order
151      end
152   end
153 end
154
155 -- mode: nil iff it is called in callbacks
156 function get_skip_table(m, idx)
157    local i = charprop_stack_table[idx][m]
158    return i or { width = 0, stretch = 0, shrink = 0,
159                  stretch_order = 0, shrink_order = 0 }
160 end
161
162 function get_penalty_table(m,c,d, idx)
163    local i = charprop_stack_table[idx][m]
164    if i then i=i[c] end
165    return i or d
166 end
167
168 -- EOF