OSDN Git Service

Implement \ltjgetparameter{kansujichar}{<num>}, and updated tests
[luatex-ja/luatexja.git] / src / ltj-stack.lua
1 --
2 -- luatexja/stack.lua
3 --
4 luatexbase.provides_module({
5   name = 'luatexja.stack',
6   date = '2013/04/13',
7   description = 'LuaTeX-ja stack system',
8 })
9 module('luatexja.stack', package.seeall)
10 local err, warn, info, log = luatexbase.errwarinf(_NAME)
11
12 luatexja.load_module('base');      local ltjb = luatexja.base
13
14 local node_new = node.new
15 local id_whatsit = node.id('whatsit')
16 local sid_user = node.subtype('user_defined')
17 local STCK = luatexja.userid_table.STCK
18 hmode = 0 -- dummy 
19
20 charprop_stack_table={}; 
21 local charprop_stack_table = charprop_stack_table
22 charprop_stack_table[0]={}
23
24
25 function get_stack_level()
26    local i = tex.getcount('ltj@@stack')
27    local j = tex.currentgrouplevel
28    if j > tex.getcount('ltj@@group@level') then
29       i = i+1 -- new stack level
30       local gd = tex.globaldefs
31       if gd>0 then tex.globaldefs = 0 end
32       --  'tex.globaldefs = 0' is local even if \globaldefs > 0.
33       tex.setcount('ltj@@group@level', j)
34       for k,v in pairs(charprop_stack_table) do -- clear the stack above i
35          if k>=i then charprop_stack_table[k]=nil end
36       end
37       charprop_stack_table[i] = table.fastcopy(charprop_stack_table[i-1])
38       tex.setcount('ltj@@stack', i)
39       if gd>0 then tex.globaldefs = gd end
40       if tex.nest[tex.nest.ptr].mode == hmode or
41          tex.nest[tex.nest.ptr].mode == -hmode then
42          local g = node_new(id_whatsit, sid_user)
43          g.user_id=STCK; g.type=100; g.value=j; node.write(g)
44       end
45    end
46    return i
47 end
48
49 -- local function table_to_str(v) 
50 --    local s = ''
51 --    for i, a in pairs(v) do
52 --       s = s .. i .. "=" .. tostring(a) .. ', '
53 --    end
54 --    return s
55 -- end
56 -- function print_stack_table(i)
57 --    print('\n>>> get_stack_level:')
58 --    for k, v in pairs(charprop_stack_table[i]) do
59 --       print("  " , k, type(k), table_to_str(v));
60 --    end
61 -- end
62
63
64 -- EXT
65 function set_stack_table(g,m,c,p,lb,ub)
66    local i = get_stack_level()
67    if type(p)~='number' or p<lb or p>ub then
68       ltjb.package_error('luatexja',
69                          "invalid code (".. tostring(p) .. ")",
70                          "The code should in the range "..tostring(lb) .. '..' ..
71                          tostring(ub) .. ".\n" ..
72                       "I'm going to use 0 instead of that illegal code value.")
73       p=0
74    end
75    c = ltjb.in_unicode(c, true)
76    charprop_stack_table[i][c+m] = p
77   if g=='global' then
78      for j,v in pairs(charprop_stack_table) do 
79         charprop_stack_table[j][c+m] = p
80      end
81   end
82 end
83
84 -- EXT
85 function set_stack_font(g,m,c,p)
86    local i = get_stack_level()
87    if type(c)~='number' or c<0 or c>255 then 
88       ltjb.package_error('luatexja',
89                          "invalid family number (".. tostring(c) .. ")",
90                          "The family number should in the range 0 .. 255.\n" ..
91                           "I'm going to use 0 instead of that illegal family number.")
92       c=0
93    end
94    charprop_stack_table[i][c+m] = p
95    if g=='global' then
96      for j,v in pairs(charprop_stack_table) do 
97         charprop_stack_table[j][c+m] = p
98      end
99   end
100 end
101
102 -- EXT: store \ltj@tempskipa
103 function set_stack_skip(g,m,sp)
104   local i = get_stack_level()
105   if not sp then return end
106   if not charprop_stack_table[i][m] then 
107      charprop_stack_table[i][m] = {} 
108   end
109   charprop_stack_table[i][m].width   = sp.width
110   charprop_stack_table[i][m].stretch = sp.stretch
111   charprop_stack_table[i][m].shrink  = sp.shrink
112   charprop_stack_table[i][m].stretch_order = sp.stretch_order
113   charprop_stack_table[i][m].shrink_order  = sp.shrink_order
114   if g=='global' then
115      for j,v in pairs(charprop_stack_table) do 
116         if not charprop_stack_table[j][m] then charprop_stack_table[j][m] = {} end
117         charprop_stack_table[j][m].width   = sp.width
118         charprop_stack_table[j][m].stretch = sp.stretch
119         charprop_stack_table[j][m].shrink  = sp.shrink
120         charprop_stack_table[j][m].stretch_order = sp.stretch_order
121         charprop_stack_table[j][m].shrink_order  = sp.shrink_order
122      end
123   end
124 end
125
126 -- These three functions are used in ltj-jfmglue.lua.
127 function report_stack_level(bsl)
128    table_current_stack = charprop_stack_table[bsl]
129 end
130 function fast_get_skip_table(m)
131    return table_current_stack[m] 
132       or { width = 0, stretch = 0, shrink = 0, stretch_order = 0, shrink_order = 0 }
133 end
134
135 -- For other situations, use the following instead:
136 function get_skip_table(m, idx)
137    return charprop_stack_table[idx][m] 
138       or { width = 0, stretch = 0, shrink = 0, stretch_order = 0, shrink_order = 0 }
139 end
140 function get_penalty_table(mc, d, idx)
141    local i = charprop_stack_table[idx][mc]
142    return i or d
143 end
144
145
146 -- EOF