OSDN Git Service

regenerate pdf
[luatex-ja/luatexja.git] / src / ltj-stack.lua
1 --
2 -- ltj-stack.lua
3 --
4 luatexbase.provides_module({
5   name = 'luatexja.stack',
6   date = '2022-08-20',
7   description = 'LuaTeX-ja stack system',
8 })
9 luatexja.stack = {}
10 local ltjs=luatexja.stack
11 luatexja.load_module 'base';      local ltjb = luatexja.base
12
13 --------------------------------------------------------------------------------
14 -- stack table (obeys TeX's grouping)
15 --------------------------------------------------------------------------------
16 local node_new = node.new
17 local id_whatsit = node.id 'whatsit'
18 local sid_user = node.subtype 'user_defined'
19 local STCK = luatexja.userid_table.STCK
20 local fastcopy = table.fastcopy
21 local setcount, getcount = tex.setcount, tex.getcount
22 local scan_int, scan_keyword = token.scan_int, token.scan_keyword
23 local getnest = tex.getnest
24 local cnt_stack = luatexbase.registernumber 'ltj@@stack'
25 local cnt_grplvl = luatexbase.registernumber 'ltj@@group@level'
26 ltjs.hmode = 0 -- dummy
27
28 local charprop_stack_table={}
29 ltjs.charprop_stack_table = charprop_stack_table
30 charprop_stack_table[0]={}
31
32 local function get_stack_level()
33    local i = getcount(cnt_stack)
34    local j = tex.currentgrouplevel
35    if j > getcount(cnt_grplvl) then
36       i = i+1 -- new stack level
37       local gd = tex.globaldefs
38       if gd~=0 then tex.globaldefs = 0 end
39       --  'tex.globaldefs = 0' is local even if \globaldefs > 0.
40       setcount(cnt_grplvl, j)
41       for k,v in pairs(charprop_stack_table) do -- clear the stack above i
42          if k>=i then charprop_stack_table[k]=nil end
43       end
44       charprop_stack_table[i] = fastcopy(charprop_stack_table[i-1])
45       setcount(cnt_stack, i)
46       if gd~=0 then tex.globaldefs = gd end
47       if getnest().mode == -ltjs.hmode then -- rest. hmode のみ
48          local g = node_new(id_whatsit, sid_user)
49          g.user_id=STCK; g.type=100; g.value=j; node.write(g)
50       end
51    end
52    return i
53 end
54 ltjs.get_stack_level = get_stack_level
55
56 local function set_stack_table(m, p)
57    local i = get_stack_level()
58    charprop_stack_table[i][m] = p
59    if luatexja.isglobal=='global' then
60       for j,v in pairs(charprop_stack_table) do v[m] = p end
61    end
62 end
63 ltjs.set_stack_table = set_stack_table
64
65 -- EXT
66 function ltjs.set_stack_perchar(m,lb,ub, getter)
67    local c = scan_int()
68    scan_keyword(',')
69    local p = tonumber((getter or scan_int)())
70    if p<lb or p>ub then
71       ltjb.package_error('luatexja',
72                          "invalid code (".. tostring(p) .. ")",
73                          "The code should in the range "..tostring(lb) .. '..' ..
74                          tostring(ub) .. ".\n" ..
75                         "I'm going to use 0 instead of that illegal code value.")
76       p=0
77    end
78    set_stack_table(m+ltjb.in_unicode(c, true), p)
79 end
80
81 -- EXT
82 function ltjs.set_stack_font(m,c,p)
83    if type(c)~='number' or c<0 or c>255 then
84       ltjb.package_error('luatexja',
85                          "invalid family number (".. tostring(c) .. ")",
86                          "The family number should in the range 0 .. 255.\n" ..
87                           "I'm going to use 0 instead of that illegal family number.")
88       c=0
89    end
90    set_stack_table(m+c, p)
91 end
92
93 -- EXT: sp: glue_spec
94 local getglue = node.getglue
95 function ltjs.set_stack_skip(m,sp)
96   local i = get_stack_level()
97   if not sp then return end
98   local w,st,sh,sto,sho = getglue(sp)
99   if charprop_stack_table[i][m] then
100      local c = charprop_stack_table[i][m]
101      c[1], c[2], c[3], c[4], c[5] = w, st, sh, sto, sho
102   else
103      charprop_stack_table[i][m] = { w,st,sh,sto,sho }
104   end
105   if luatexja.isglobal=='global' then
106      for j,v in pairs(charprop_stack_table) do
107         if not v[m] then v[m] = { true,true,true,true,true } end
108         local c = v[m]
109         c[1], c[2], c[3], c[4], c[5] = w, st, sh, sto, sho
110      end
111   end
112 end
113
114 -- These three functions are used in ltj-jfmglue.lua.
115 -- list_dir and orig_char_table are used in other lua files.
116 local orig_char_table = {}
117 ltjs.orig_char_table = orig_char_table
118 ltjs.list_dir = nil -- dummy
119 ltjs.table_current_stack = nil -- dummy
120 local dummy_skip_table = { 0,0,0,0,0 }
121 function ltjs.report_stack_level(bsl)
122    ltjs.table_current_stack = charprop_stack_table[bsl]
123    return bsl
124 end
125 function ltjs.fast_get_stack_skip(m)
126    return ltjs.table_current_stack[m] or dummy_skip_table
127 end
128
129 -- For other situations, use the following instead:
130 function ltjs.get_stack_skip(m, idx)
131    return charprop_stack_table[idx][m] or dummy_skip_table
132 end
133 function ltjs.get_stack_table(mc, d, idx)
134    return charprop_stack_table[idx][mc] or d
135 end
136
137
138 -- EOF