OSDN Git Service

Merge branch 'yato_20110605'
[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
20 local charprop_stack_table={}; charprop_stack_table[0]={}
21
22 function get_stack_level()
23    local i = tex.getcount('ltj@@stack')
24    local j = tex.currentgrouplevel
25    if j > tex.getcount('ltj@@group@level') then
26       i = i+1 -- new stack level
27       local gd = tex.globaldefs
28       if gd>0 then tex.globaldefs = 0 end
29       --  'tex.globaldefs = 0' is local even if \globaldefs > 0.
30       tex.setcount('ltj@@group@level', j)
31       for k,v in pairs(charprop_stack_table) do -- clear the stack above i
32          if k>=i then charprop_stack_table[k]=nil end
33       end
34       charprop_stack_table[i] = table.fastcopy(charprop_stack_table[i-1])
35       tex.setcount('ltj@@stack', i)
36       if gd>0 then tex.globaldefs = gd end
37       print('mode: ', tex.nest[tex.nest.ptr].mode)
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 -- EXT
48 function set_stack_table(g,m,c,p,lb,ub)
49    local i = get_stack_level()
50    if p<lb or p>ub then 
51       ltjb.package_error('luatexja',
52                          "invalid code (".. p .. ")",
53                          {"The code should in the range "..tostring(lb) ..'..'.. tostring(ub) .. ".",
54                           "I'm going to use 0 instead of that illegal code value."})
55       p=0
56    elseif c<-1 or c>0x10ffff then 
57       ltjb.package_error('luatexja',
58                          'bad character code (' .. c .. ')',
59                          {'A character number must be between -1 and 0x10ffff.',
60                           "(-1 is used for denoting `math boundary')",
61                           'So I changed this one to zero.'})
62       c=0
63    elseif not charprop_stack_table[i][c] then 
64       charprop_stack_table[i][c] = {} 
65    end
66    charprop_stack_table[i][c][m] = p
67   if g=='global' then
68      for j,v in pairs(charprop_stack_table) do 
69         if not charprop_stack_table[j][c] then charprop_stack_table[j][c] = {} end
70         charprop_stack_table[j][c][m] = p
71      end
72   end
73 end
74
75 -- EXT
76 function set_stack_font(g,m,c,p)
77    local i = get_stack_level()
78    if c<0 or c>255 then 
79       ltjb.package_error('luatexja',
80                          "invalid family number (".. p .. ")",
81                          {"The family number should in the range 0 .. 255.",
82                           "I'm going to use 0 instead of that illegal family number."})
83       c=0
84    elseif not charprop_stack_table[i][c] then 
85       charprop_stack_table[i][c] = {} 
86    end
87    charprop_stack_table[i][c][m] = p
88   if g=='global' then
89      for j,v in pairs(charprop_stack_table) do 
90         if not charprop_stack_table[j][c] then charprop_stack_table[j][c] = {} end
91         charprop_stack_table[j][c][m] = p
92      end
93   end
94 end
95
96 -- EXT: store \ltj@tempskipa
97 function set_stack_skip(g,c,sp)
98   local i = get_stack_level()
99   if not sp then return end
100   if not charprop_stack_table[i][c] then 
101      charprop_stack_table[i][c] = {} 
102   end
103   charprop_stack_table[i][c].width   = sp.width
104   charprop_stack_table[i][c].stretch = sp.stretch
105   charprop_stack_table[i][c].shrink  = sp.shrink
106   charprop_stack_table[i][c].stretch_order = sp.stretch_order
107   charprop_stack_table[i][c].shrink_order  = sp.shrink_order
108   if g=='global' then
109      for j,v in pairs(charprop_stack_table) do 
110         if not charprop_stack_table[j][c] then charprop_stack_table[j][c] = {} end
111         charprop_stack_table[j][c].width   = sp.width
112         charprop_stack_table[j][c].stretch = sp.stretch
113         charprop_stack_table[j][c].shrink  = sp.shrink
114         charprop_stack_table[j][c].stretch_order = sp.stretch_order
115         charprop_stack_table[j][c].shrink_order  = sp.shrink_order
116      end
117   end
118 end
119
120 -- mode: nil iff it is called in callbacks
121 function get_skip_table(m, idx)
122    local i = charprop_stack_table[idx][m]
123    return i or { width = 0, stretch = 0, shrink = 0,
124                  stretch_order = 0, shrink_order = 0 }
125 end
126
127 function get_penalty_table(m,c,d, idx)
128    local i = charprop_stack_table[idx][c]
129    if i then i=i[m] end
130    return i or d
131 end
132
133 -- EOF