OSDN Git Service

In non-restricted hmode, stack whatsit is not necessary.
[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/09/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 then -- rest. hmode のみ
41          local g = node_new(id_whatsit, sid_user)
42          g.user_id=STCK; g.type=100; g.value=j; node.write(g)
43       end
44    end
45    return i
46 end
47
48 function set_stack_table(m, p)
49    local i = get_stack_level()
50    charprop_stack_table[i][m] = p
51    if luatexja.isglobal=='global' then
52       for j,v in pairs(charprop_stack_table) do 
53          charprop_stack_table[j][m] = p
54       end
55    end
56 end
57 local set_stack_table = set_stack_table
58
59 -- EXT
60 function set_stack_perchar(m,c,p,lb,ub)
61    if type(p)~='number' or p<lb or p>ub then
62       ltjb.package_error('luatexja',
63                          "invalid code (".. tostring(p) .. ")",
64                          "The code should in the range "..tostring(lb) .. '..' ..
65                          tostring(ub) .. ".\n" ..
66                       "I'm going to use 0 instead of that illegal code value.")
67       p=0
68    end
69    set_stack_table(m+ltjb.in_unicode(c, true), p)
70 end
71
72 -- EXT
73 function set_stack_font(m,c,p)
74    if type(c)~='number' or c<0 or c>255 then 
75       ltjb.package_error('luatexja',
76                          "invalid family number (".. tostring(c) .. ")",
77                          "The family number should in the range 0 .. 255.\n" ..
78                           "I'm going to use 0 instead of that illegal family number.")
79       c=0
80    end
81    set_stack_table(m+c, p)
82 end
83
84 -- EXT: store \ltj@tempskipa
85 function set_stack_skip(m,sp)
86   local i = get_stack_level()
87   if not sp then return end
88   if not charprop_stack_table[i][m] then 
89      charprop_stack_table[i][m] = {} 
90   end
91   charprop_stack_table[i][m].width   = sp.width
92   charprop_stack_table[i][m].stretch = sp.stretch
93   charprop_stack_table[i][m].shrink  = sp.shrink
94   charprop_stack_table[i][m].stretch_order = sp.stretch_order
95   charprop_stack_table[i][m].shrink_order  = sp.shrink_order
96   if luatexja.isglobal=='global' then
97      for j,v in pairs(charprop_stack_table) do 
98         if not charprop_stack_table[j][m] then charprop_stack_table[j][m] = {} end
99         charprop_stack_table[j][m].width   = sp.width
100         charprop_stack_table[j][m].stretch = sp.stretch
101         charprop_stack_table[j][m].shrink  = sp.shrink
102         charprop_stack_table[j][m].stretch_order = sp.stretch_order
103         charprop_stack_table[j][m].shrink_order  = sp.shrink_order
104      end
105   end
106 end
107
108 -- These three functions are used in ltj-jfmglue.lua.
109 function report_stack_level(bsl)
110    table_current_stack = charprop_stack_table[bsl]
111 end
112 function fast_get_stack_skip(m)
113    return table_current_stack[m] 
114       or { width = 0, stretch = 0, shrink = 0, stretch_order = 0, shrink_order = 0 }
115 end
116
117 -- For other situations, use the following instead:
118 function get_stack_skip(m, idx)
119    return charprop_stack_table[idx][m] 
120       or { width = 0, stretch = 0, shrink = 0, stretch_order = 0, shrink_order = 0 }
121 end
122 function get_stack_table(mc, d, idx)
123    local i = charprop_stack_table[idx][mc]
124    return i or d
125 end
126
127
128 -- EOF