OSDN Git Service

lltjdef.sty
[luatex-ja/luatexja.git] / src / ltj-stack.lua
1 --
2 -- ltj-stack.lua
3 --
4 luatexbase.provides_module({
5   name = 'luatexja.stack',
6   date = '2019/09/26',
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 ltjs.hmode = 0 -- dummy
24
25 local charprop_stack_table={};
26 ltjs.charprop_stack_table = charprop_stack_table
27 charprop_stack_table[0]={}
28
29 local function get_stack_level()
30    local i = getcount('ltj@@stack')
31    local j = tex.currentgrouplevel
32    if j > getcount('ltj@@group@level') then
33       i = i+1 -- new stack level
34       local gd = tex.globaldefs
35       if gd~=0 then tex.globaldefs = 0 end
36       --  'tex.globaldefs = 0' is local even if \globaldefs > 0.
37       setcount('ltj@@group@level', j)
38       for k,v in pairs(charprop_stack_table) do -- clear the stack above i
39          if k>=i then charprop_stack_table[k]=nil end
40       end
41       charprop_stack_table[i] = fastcopy(charprop_stack_table[i-1])
42       setcount('ltj@@stack', i)
43       if gd~=0 then tex.globaldefs = gd end
44       if  tex.nest[tex.nest.ptr].mode == -ltjs.hmode then -- rest. hmode のみ
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 ltjs.get_stack_level = get_stack_level
52
53 local function set_stack_table(m, p)
54    local i = get_stack_level()
55    charprop_stack_table[i][m] = p
56    if luatexja.isglobal=='global' then
57       for j,v in pairs(charprop_stack_table) do
58          charprop_stack_table[j][m] = p
59       end
60    end
61 end
62 ltjs.set_stack_table = set_stack_table
63
64 -- EXT
65 function ltjs.set_stack_perchar(m,lb,ub, getter)
66    local c = scan_int()
67    scan_keyword(',')
68    local p = tonumber((getter or scan_int)())
69    if p<lb or p>ub then
70       ltjb.package_error('luatexja',
71                          "invalid code (".. tostring(p) .. ")",
72                          "The code should in the range "..tostring(lb) .. '..' ..
73                          tostring(ub) .. ".\n" ..
74                       "I'm going to use 0 instead of that illegal code value.")
75       p=0
76    end
77    set_stack_table(m+ltjb.in_unicode(c, true), p)
78 end
79
80 -- EXT
81 function ltjs.set_stack_font(m,c,p)
82    if type(c)~='number' or c<0 or c>255 then
83       ltjb.package_error('luatexja',
84                          "invalid family number (".. tostring(c) .. ")",
85                          "The family number should in the range 0 .. 255.\n" ..
86                           "I'm going to use 0 instead of that illegal family number.")
87       c=0
88    end
89    set_stack_table(m+c, p)
90 end
91
92 -- EXT: sp: glue_spec
93 function ltjs.set_stack_skip(m,sp)
94   local i = get_stack_level()
95   if not sp then return end
96   if not charprop_stack_table[i][m] then
97      charprop_stack_table[i][m] = {}
98   end
99   charprop_stack_table[i][m].width   = sp.width
100   charprop_stack_table[i][m].stretch = sp.stretch
101   charprop_stack_table[i][m].shrink  = sp.shrink
102   charprop_stack_table[i][m].stretch_order = sp.stretch_order
103   charprop_stack_table[i][m].shrink_order  = sp.shrink_order
104   if luatexja.isglobal=='global' then
105      for j,v in pairs(charprop_stack_table) do
106         if not charprop_stack_table[j][m] then charprop_stack_table[j][m] = {} end
107         charprop_stack_table[j][m].width   = sp.width
108         charprop_stack_table[j][m].stretch = sp.stretch
109         charprop_stack_table[j][m].shrink  = sp.shrink
110         charprop_stack_table[j][m].stretch_order = sp.stretch_order
111         charprop_stack_table[j][m].shrink_order  = sp.shrink_order
112      end
113   end
114 end
115
116 -- These three functions are used in ltj-jfmglue.lua.
117 -- list_dir and orig_char_table are used in other lua files.
118 local orig_char_table = {}
119 ltjs.orig_char_table = orig_char_table
120 ltjs.list_dir = nil -- dummy
121 ltjs.table_current_stack = nil -- dummy
122 function ltjs.report_stack_level(bsl)
123    ltjs.table_current_stack = charprop_stack_table[bsl]
124    return bsl
125 end
126 function ltjs.fast_get_stack_skip(m)
127    return ltjs.table_current_stack[m]
128       or { width = 0, stretch = 0, shrink = 0, stretch_order = 0, shrink_order = 0 }
129 end
130
131 -- For other situations, use the following instead:
132 function ltjs.get_stack_skip(m, idx)
133    return charprop_stack_table[idx][m]
134       or { width = 0, stretch = 0, shrink = 0, stretch_order = 0, shrink_order = 0 }
135 end
136 function ltjs.get_stack_table(mc, d, idx)
137    local i = charprop_stack_table[idx][mc]
138    return i or d
139 end
140
141
142 -- EOF