OSDN Git Service

ltj-direction.lua: stop \count register \ltj@dir@count to store the direction (WIP).
[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/05/08',
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 --------------------------------------------------------------------------------
15 -- stack table (obeys TeX's grouping)
16 --------------------------------------------------------------------------------
17 local node_new = node.new
18 local id_whatsit = node.id('whatsit')
19 local sid_user = node.subtype('user_defined')
20 local STCK = luatexja.userid_table.STCK
21 hmode = 0 -- dummy
22
23 charprop_stack_table={};
24 local charprop_stack_table = charprop_stack_table
25 charprop_stack_table[0]={}
26
27
28 function get_stack_level(is_v)
29    local i = tex.getcount('ltj@@stack')
30    local j = tex.currentgrouplevel
31    if j > tex.getcount('ltj@@group@level') then
32       i = i+1 -- new stack level
33       local gd = tex.globaldefs
34       if gd~=0 then tex.globaldefs = 0 end
35       --  'tex.globaldefs = 0' is local even if \globaldefs > 0.
36       tex.setcount('ltj@@group@level', j)
37       for k,v in pairs(charprop_stack_table) do -- clear the stack above i
38          if k>=i then charprop_stack_table[k]=nil end
39       end
40       charprop_stack_table[i] = table.fastcopy(charprop_stack_table[i-1])
41       tex.setcount('ltj@@stack', i)
42       if gd~=0 then tex.globaldefs = gd end
43       if  is_v or tex.nest[tex.nest.ptr].mode == hmode or
44           tex.nest[tex.nest.ptr].mode == -hmode then
45          local g = node_new(id_whatsit, sid_user)
46          g.user_id=STCK; g.type=100; g.value=j; node.write(g)
47       end
48    end
49    return i
50 end
51
52 function set_stack_table(m,p, is_v)
53    local i = get_stack_level(is_v)
54    charprop_stack_table[i][m] = p
55    if luatexja.isglobal=='global' then
56       for j,v in pairs(charprop_stack_table) do
57          charprop_stack_table[j][m] = p
58       end
59    end
60 end
61 local set_stack_table = set_stack_table
62
63 -- EXT
64 function set_stack_perchar(m,c,p,lb,ub)
65    if type(p)~='number' or p<lb or p>ub then
66       ltjb.package_error('luatexja',
67                          "invalid code (".. tostring(p) .. ")",
68                          "The code should in the range "..tostring(lb) .. '..' ..
69                          tostring(ub) .. ".\n" ..
70                       "I'm going to use 0 instead of that illegal code value.")
71       p=0
72    end
73    set_stack_table(m+ltjb.in_unicode(c, true), p)
74 end
75
76 -- EXT
77 function set_stack_font(m,c,p)
78    if type(c)~='number' or c<0 or c>255 then
79       ltjb.package_error('luatexja',
80                          "invalid family number (".. tostring(c) .. ")",
81                          "The family number should in the range 0 .. 255.\n" ..
82                           "I'm going to use 0 instead of that illegal family number.")
83       c=0
84    end
85    set_stack_table(m+c, p)
86 end
87
88 -- EXT: store \ltj@tempskipa
89 function set_stack_skip(m,sp)
90   local i = get_stack_level()
91   if not sp then return end
92   if not charprop_stack_table[i][m] then
93      charprop_stack_table[i][m] = {}
94   end
95   charprop_stack_table[i][m].width   = sp.width
96   charprop_stack_table[i][m].stretch = sp.stretch
97   charprop_stack_table[i][m].shrink  = sp.shrink
98   charprop_stack_table[i][m].stretch_order = sp.stretch_order
99   charprop_stack_table[i][m].shrink_order  = sp.shrink_order
100   if luatexja.isglobal=='global' then
101      for j,v in pairs(charprop_stack_table) do
102         if not charprop_stack_table[j][m] then charprop_stack_table[j][m] = {} end
103         charprop_stack_table[j][m].width   = sp.width
104         charprop_stack_table[j][m].stretch = sp.stretch
105         charprop_stack_table[j][m].shrink  = sp.shrink
106         charprop_stack_table[j][m].stretch_order = sp.stretch_order
107         charprop_stack_table[j][m].shrink_order  = sp.shrink_order
108      end
109   end
110 end
111
112 -- These three functions are used in ltj-jfmglue.lua.
113 function report_stack_level(bsl)
114    table_current_stack = charprop_stack_table[bsl]
115    return bsl
116 end
117 function fast_get_stack_skip(m)
118    return table_current_stack[m]
119       or { width = 0, stretch = 0, shrink = 0, stretch_order = 0, shrink_order = 0 }
120 end
121
122 -- For other situations, use the following instead:
123 function get_stack_skip(m, idx)
124    return charprop_stack_table[idx][m]
125       or { width = 0, stretch = 0, shrink = 0, stretch_order = 0, shrink_order = 0 }
126 end
127 function get_stack_table(mc, d, idx)
128    local i = charprop_stack_table[idx][mc]
129    return i or d
130 end
131
132
133 --------------------------------------------------------------------------------
134 -- nest table (obeys TeX's semantic nest)
135 --------------------------------------------------------------------------------
136 nest_table = {}
137 local nest_table = nest_table
138
139
140 -- EOF