OSDN Git Service

Rewrited process_input_buffer callback (ticket #25231).
[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 local node_new = node.new
14 local id_whatsit = node.id('whatsit')
15 local sid_user = node.subtype('user_defined')
16
17 local charprop_stack_table={}; charprop_stack_table[0]={}
18
19 function get_stack_level()
20    local i = tex.getcount('ltj@@stack')
21    local j = tex.currentgrouplevel
22    if j > tex.getcount('ltj@@group@level') then
23       i = i+1 -- new stack level
24       tex.setcount('ltj@@group@level', j)
25       for k,v in pairs(charprop_stack_table) do -- clear the stack above i
26          if k>=i then charprop_stack_table[k]=nil end
27       end
28       charprop_stack_table[i] = table.fastcopy(charprop_stack_table[i-1])
29       tex.setcount('ltj@@stack', i)
30       local g = node_new(id_whatsit, sid_user)
31       g.user_id=30112; g.type=100; g.value=j; node.write(g)
32    end
33    return i
34 end
35
36 -- EXT
37 function set_stack_table(g,m,c,p,lb,ub)
38    local i = get_stack_level()
39    if p<lb or p>ub then 
40       ltj.error('Invalid code (' .. p .. '), should in the range '
41                 .. tostring(lb) .. '..' .. tostring(ub) .. '.',
42              {"I'm going to use 0 instead of that illegal code value."})
43       p=0
44    elseif c<-1 or c>0x10FFFF then
45       ltj.error('Invalid character code (' .. c
46                 .. '), should in the range -1.."10FFFF.',{})
47       return 
48    elseif not charprop_stack_table[i][c] then 
49       charprop_stack_table[i][c] = {} 
50    end
51    charprop_stack_table[i][c][m] = p
52   if g=='global' then
53      for j,v in pairs(charprop_stack_table) do 
54         if not charprop_stack_table[j][c] then charprop_stack_table[j][c] = {} end
55         charprop_stack_table[j][c][m] = p
56      end
57   end
58 end
59
60 -- EXT: store \ltj@tempskipa
61 function set_stack_skip(g,c)
62   local i = get_stack_level()
63   local sp = tex.getskip('ltj@tempskipa')
64   if not charprop_stack_table[i][c] then 
65      charprop_stack_table[i][c] = {} 
66   end
67   charprop_stack_table[i][c].width   = sp.width
68   charprop_stack_table[i][c].stretch = sp.stretch
69   charprop_stack_table[i][c].shrink  = sp.shrink
70   charprop_stack_table[i][c].stretch_order = sp.stretch_order
71   charprop_stack_table[i][c].shrink_order  = sp.shrink_order
72   if g=='global' then
73      for j,v in pairs(charprop_stack_table) do 
74         if not charprop_stack_table[j][c] then charprop_stack_table[j][c] = {} end
75         charprop_stack_table[j][c].width   = sp.width
76         charprop_stack_table[j][c].stretch = sp.stretch
77         charprop_stack_table[j][c].shrink  = sp.shrink
78         charprop_stack_table[j][c].stretch_order = sp.stretch_order
79         charprop_stack_table[j][c].shrink_order  = sp.shrink_order
80      end
81   end
82 end
83
84 -- mode: nil iff it is called in callbacks
85 function get_skip_table(m, idx)
86    local i = charprop_stack_table[idx][m]
87    return i or { width = 0, stretch = 0, shrink = 0,
88                  stretch_order = 0, shrink_order = 0 }
89 end
90
91 function get_penalty_table(m,c,d, idx)
92    local i = charprop_stack_table[idx][c]
93    if i then i=i[m] end
94    return i or d
95 end
96
97 -- EOF