OSDN Git Service

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