OSDN Git Service

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