OSDN Git Service

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