OSDN Git Service

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