OSDN Git Service

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