OSDN Git Service

Import from old kitagawa_ruby branch (may not work now)
[luatex-ja/luatexja.git] / src / ltj-stack.lua
1 --
2 -- luatexja/stack.lua
3 --
4 luatexbase.provides_module({
5   name = 'luatexja.stack',
6   date = '2014/01/24',
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 function set_stack_table(m,p)
64    local i = get_stack_level()
65    charprop_stack_table[i][m] = p
66    if luatexja.isglobal=='global' then
67       for j,v in pairs(charprop_stack_table) do 
68          charprop_stack_table[j][m] = p
69       end
70    end
71 end
72 local set_stack_table = set_stack_table
73
74 -- EXT
75 function set_stack_perchar(m,c,p,lb,ub)
76    if type(p)~='number' or p<lb or p>ub then
77       ltjb.package_error('luatexja',
78                          "invalid code (".. tostring(p) .. ")",
79                          "The code should in the range "..tostring(lb) .. '..' ..
80                          tostring(ub) .. ".\n" ..
81                       "I'm going to use 0 instead of that illegal code value.")
82       p=0
83    end
84    set_stack_table(m+ltjb.in_unicode(c, true), p)
85 end
86
87 -- EXT
88 function set_stack_font(m,c,p)
89    if type(c)~='number' or c<0 or c>255 then 
90       ltjb.package_error('luatexja',
91                          "invalid family number (".. tostring(c) .. ")",
92                          "The family number should in the range 0 .. 255.\n" ..
93                           "I'm going to use 0 instead of that illegal family number.")
94       c=0
95    end
96    set_stack_table(m+c, p)
97 end
98
99 -- EXT: store \ltj@tempskipa
100 function set_stack_skip(m,sp)
101   local i = get_stack_level()
102   if not sp then return end
103   if not charprop_stack_table[i][m] then 
104      charprop_stack_table[i][m] = {} 
105   end
106   charprop_stack_table[i][m].width   = sp.width
107   charprop_stack_table[i][m].stretch = sp.stretch
108   charprop_stack_table[i][m].shrink  = sp.shrink
109   charprop_stack_table[i][m].stretch_order = sp.stretch_order
110   charprop_stack_table[i][m].shrink_order  = sp.shrink_order
111   if luatexja.isglobal=='global' then
112      for j,v in pairs(charprop_stack_table) do 
113         if not charprop_stack_table[j][m] then charprop_stack_table[j][m] = {} end
114         charprop_stack_table[j][m].width   = sp.width
115         charprop_stack_table[j][m].stretch = sp.stretch
116         charprop_stack_table[j][m].shrink  = sp.shrink
117         charprop_stack_table[j][m].stretch_order = sp.stretch_order
118         charprop_stack_table[j][m].shrink_order  = sp.shrink_order
119      end
120   end
121 end
122
123 -- These three functions are used in ltj-jfmglue.lua.
124 function report_stack_level(bsl)
125    table_current_stack = charprop_stack_table[bsl]
126 end
127 function fast_get_stack_skip(m)
128    return table_current_stack[m] 
129       or { width = 0, stretch = 0, shrink = 0, stretch_order = 0, shrink_order = 0 }
130 end
131
132 -- For other situations, use the following instead:
133 function get_stack_skip(m, idx)
134    return charprop_stack_table[idx][m] 
135       or { width = 0, stretch = 0, shrink = 0, stretch_order = 0, shrink_order = 0 }
136 end
137 function get_stack_table(mc, d, idx)
138    local i = charprop_stack_table[idx][mc]
139    return i or d
140 end
141
142
143 -- EOF