OSDN Git Service

better handling of dir_node
[luatex-ja/luatexja.git] / src / ltj-direction.lua
1 --
2 -- src/ltj-direction.lua
3 --
4
5 luatexja.load_module('base');      local ltjb = luatexja.base
6 luatexja.load_module('stack');     local ltjs = luatexja.stack
7 luatexja.load_module('rmlgbm');    local ltjr = luatexja.rmlgbm
8 luatexja.direction = {}
9
10 local attr_dir = luatexbase.attributes['ltj@dir']
11 local attr_icflag = luatexbase.attributes['ltj@icflag']
12
13 local Dnode = node.direct or node
14 local nullfunc = function (n) return n end
15 local to_node = (Dnode ~= node) and Dnode.tonode or nullfunc
16 local to_direct = (Dnode ~= node) and Dnode.todirect or nullfunc
17 local has_attr = Dnode.has_attribute
18 local set_attr = Dnode.set_attribute
19 local insert_before = Dnode.insert_before
20 local insert_after = Dnode.insert_after
21 local getid = (Dnode ~= node) and Dnode.getid or function(n) return n.id end
22 local getsubtype = (Dnode ~= node) and Dnode.getsubtype or function(n) return n.subtype end
23 local getlist = (Dnode ~= node) and Dnode.getlist or function(n) return n.head end
24 local setfield = (Dnode ~= node) and Dnode.setfield or function(n, i, c) n[i] = c end
25 local getfield = (Dnode ~= node) and Dnode.getfield or function(n, i) return n[i] end
26 local node_new = Dnode.new
27 local node_tail = Dnode.tail
28 local node_free = Dnode.free
29 local node_remove = Dnode.remove
30 local node_next = (Dnode ~= node) and Dnode.getnext or node.next
31 local traverse = Dnode.traverse
32 local traverse_id = Dnode.traverse_id
33 local start_time_measure, stop_time_measure 
34    = ltjb.start_time_measure, ltjb.stop_time_measure
35
36 local id_kern = node.id('kern')
37 local id_hlist = node.id('hlist')
38 local id_vlist = node.id('vlist')
39 local id_whatsit = node.id('whatsit')
40 local sid_save = node.subtype('pdf_save')
41 local sid_restore = node.subtype('pdf_restore')
42 local sid_matrix = node.subtype('pdf_setmatrix')
43 local sid_user = node.subtype('user_defined')
44
45 local tex_getcount = tex.getcount
46 local tex_set_attr = tex.setattribute
47 local PROCESSED    = luatexja.icflag_table.PROCESSED
48 local PROCESSED_BEGIN_FLAG = luatexja.icflag_table.PROCESSED_BEGIN_FLAG
49 local PACKED       = luatexja.icflag_table.PACKED
50 local STCK = luatexja.userid_table.STCK
51 local DIR  = luatexja.userid_table.DIR
52 local dir_tate = luatexja.dir_table.dir_tate
53 local dir_yoko = luatexja.dir_table.dir_yoko
54 local dir_dtou = luatexja.dir_table.dir_dtou
55 local dir_node_auto   = luatexja.dir_table.dir_node_auto
56 local dir_node_manual = luatexja.dir_table.dir_node_manual
57
58
59
60 local function get_dir_count()
61    return tex_getcount('ltj@dir@count')
62 end
63 luatexja.direction.get_dir_count = get_dir_count 
64
65 -- \tate, \yoko
66 do
67   local node_next = node.next
68   local node_set_attr = node.set_attribute
69   local function set_list_direction(v, name)
70      local lv, w = tex.nest[tex.nest.ptr], tex.lists.page_head
71      if lv.mode == 1 and w then
72         if w.id==id_whatsit and w.subtype==sid_user
73         and w.user_id==DIR then
74            node_set_attr(w, attr_dir, v)
75            
76         end
77      else
78         local w = node_next(to_direct(lv.head))
79         if to_node(w) then
80            if getid(w)==id_whatsit and getsubtype(w)==sid_user
81            and getfield(w, 'user_id')==DIR  then
82               set_attr(w, attr_dir, v)
83               tex_set_attr('global', attr_dir, 0)  
84            else
85               ltjb.package_error(
86                  'luatexja',
87                  "Use `\\" .. name .. "' at top of list",
88                  'Direction change command by LuaTeX-ja is available\n'
89                  .. 'only when the current list is null.')
90            end
91         else
92            local w = node_new(id_whatsit, sid_user)
93            setfield(w, 'next', hd)
94            setfield(w, 'user_id', DIR)
95            setfield(w, 'type', 110)
96            set_attr(w, attr_dir, v)
97            Dnode.write(w)
98            tex_set_attr('global', attr_dir, 0)
99         end
100         tex_set_attr('global', attr_icflag, 0)
101      end
102   end
103   luatexja.direction.set_list_direction = set_list_direction
104 end
105
106 -- ボックスに dir whatsit を追加
107 local function create_dir_whatsit(hd, gc, new_dir)
108       local w = node_new(id_whatsit, sid_user)
109       setfield(w, 'next', hd)
110       setfield(w, 'user_id', DIR)
111       setfield(w, 'type', 110)
112       set_attr(w, attr_dir, new_dir)
113       tex_set_attr('global', attr_dir, 0)  
114       set_attr(w, attr_icflag, PROCESSED_BEGIN_FLAG)
115       set_attr(hd, attr_icflag, (has_attr(hd, attr_icflag) or 0) + PROCESSED_BEGIN_FLAG)
116       tex_set_attr('global', attr_icflag, 0)
117       return w
118 end
119
120 -- hpack_filter, vpack_filter, post_line_break_filter
121 -- の結果を組方向を明示するため,先頭に dir_node を設置
122 do
123    local function create_dir_whatsit_hpack(h, gc)
124       local hd = to_direct(h)
125       if gc=='fin_row' or gc == 'preamble'  then
126          if hd  then
127             set_attr(hd, attr_icflag, PROCESSED_BEGIN_FLAG)
128             tex_set_attr('global', attr_icflag, 0)
129          end
130          return h
131       else
132          return to_node(create_dir_whatsit(hd, gc, ltjs.list_dir))
133       end
134    end
135
136    luatexbase.add_to_callback('hpack_filter', 
137                               create_dir_whatsit_hpack, 'ltj.create_dir_whatsit', 10000)
138
139    local function create_dir_whatsit_vbox(h, gc)
140       local hd= to_direct(h)
141       ltjs.list_dir = get_dir_count()
142       if getid(hd)==id_whatsit and getsubtype(hd)==sid_user
143       and getfield(hd, 'user_id')==DIR then
144          ltjs.list_dir = has_attr(hd, attr_dir)
145          return h
146       elseif gc=='fin_row' or gc == 'preamble'  then
147          if hd  then
148             set_attr(hd, attr_icflag, PROCESSED_BEGIN_FLAG)
149             tex_set_attr('global', attr_icflag, 0)
150          end
151          return h
152       else
153          return to_node(create_dir_whatsit(hd, gc, ltjs.list_dir))
154       end
155    end
156    luatexbase.add_to_callback('vpack_filter', 
157                               create_dir_whatsit_vbox, 'ltj.create_dir_whatsit', 1)
158
159    local function create_dir_whatsit_parbox(h, gc)
160       stop_time_measure('tex_linebreak')
161       -- start 側は ltj-debug.lua に
162       local new_dir, hd = ltjs.list_dir, to_direct(h)
163       for line in traverse_id(id_hlist, hd) do
164          set_attr(line, attr_dir, new_dir)
165       end
166       return to_node(create_dir_whatsit(hd, gc, new_dir))
167    end
168    luatexbase.add_to_callback('post_linebreak_filter',
169                               create_dir_whatsit_parbox, 'ltj.create_dir_whatsit', 10000)
170
171 end
172
173 -- dir_node に包む方法を書いたテーブル
174 local dir_node_aux
175 do
176    local get_h =function (w,h,d) return h end
177    local get_d =function (w,h,d) return d end
178    local get_h_d =function (w,h,d) return h+d end
179    local get_h_d_neg =function (w,h,d) return -h-d end
180    local get_h_neg =function (w,h,d) return -h end
181    local get_d_neg =function (w,h,d) return -d end
182    local get_w_half =function (w,h,d) return 0.5*w end
183    local get_w_neg_half =function (w,h,d) return -0.5*w end
184    local get_w_neg =function (w,h,d) return -w end
185    local get_w =function (w,h,d) return w end
186    local zero = function() return 0 end
187    dir_node_aux = {
188       [dir_yoko] = { -- yoko を 
189          [dir_tate] = { -- tate 中で組む
190             width  = get_h_d,
191             height = get_w_half,
192             depth  = get_w_half,
193             [id_hlist] = {
194                { 'whatsit', sid_save },
195                { 'rotate', '0 1 -1 0' },
196                { 'kern', get_w_neg_half },
197                { 'box' , get_h },
198                { 'kern', get_w_neg_half },
199                { 'whatsit', sid_restore },
200             },
201             [id_vlist] = {
202                { 'whatsit', sid_save },
203                { 'rotate', '0 1 -1 0' },
204                { 'kern' , zero },
205                { 'box' , get_w_neg },
206                { 'kern', get_h_d_neg},
207                { 'whatsit', sid_restore },
208             },
209          },
210          [dir_dtou] = { -- dtou 中で組む
211             width  = get_h_d,
212             height = get_w,
213             depth  = zero,
214             [id_hlist] = {
215                { 'whatsit', sid_save },
216                { 'rotate', '0 -1 1 0' },
217                { 'kern', get_w_neg },
218                { 'box', get_d_neg },
219                { 'whatsit', sid_restore },
220             },
221             [id_vlist] = {
222                { 'whatsit', sid_save },
223                { 'rotate', '0 -1 1 0' },
224                { 'kern', get_h_d_neg },
225                { 'box', zero },
226                { 'whatsit', sid_restore },
227             },
228          },
229       },
230       [dir_tate] = { -- tate を
231          [dir_yoko] = { -- yoko 中で組む
232             width  = get_h_d,
233             height = get_w,
234             depth  = zero,
235             [id_hlist] = {
236                { 'whatsit', sid_save },
237                { 'rotate', '0 -1 1 0' },
238                { 'kern', get_w_neg },
239                { 'box' , get_d_neg },
240                { 'whatsit', sid_restore },
241             },
242             [id_vlist] = {
243                { 'whatsit', sid_save },
244                { 'rotate', '0 -1 1 0' },
245                { 'kern', get_h_d_neg },
246                { 'box', zero },
247                { 'whatsit', sid_restore },
248             },
249          },
250          [dir_dtou] = { -- dtou 中で組む
251             width  = get_w,
252             height = get_d,
253             depth  = get_h,
254             [id_hlist] = {
255                { 'whatsit', sid_save },
256                { 'rotate', '-1 0 0 -1' },
257                { 'kern', get_w_neg },
258                { 'box', zero },
259                { 'whatsit', sid_restore },
260             },
261             [id_vlist] = {
262                { 'whatsit', sid_save },
263                { 'rotate', '-1 0 0 -1' },
264                { 'kern', get_h_d_neg },
265                { 'box', get_w_neg },
266                { 'whatsit', sid_restore },
267             },
268          },
269       },
270       [dir_dtou] = { -- dtou を
271          [dir_yoko] = { -- yoko 中で組む
272             width  = get_h_d,
273             height = zero,
274             depth  = get_w,
275             [id_hlist] = {
276                { 'whatsit', sid_save },
277                { 'rotate', '0 1 -1 0' },
278                { 'kern', get_w_neg },
279                { 'box', get_h },
280                { 'whatsit', sid_restore },
281             },
282             [id_vlist] = {
283                { 'kern', get_w },
284                { 'whatsit', sid_save },
285                { 'rotate', '0 1 -1 0' },
286                { 'box', zero },
287                { 'kern', get_h_d_neg },
288                { 'whatsit', sid_restore },
289             },
290          },
291          [dir_tate] = { -- tate 中で組む
292             width  = get_w,
293             height = get_d,
294             depth  = get_h,
295             [id_hlist] = {
296                { 'whatsit', sid_save },
297                { 'rotate', '-1 0 0 -1' },
298                { 'kern', get_w_neg },
299                { 'box', zero },
300                { 'whatsit', sid_restore },
301             },
302             [id_vlist] = {
303                { 'whatsit', sid_save },
304                { 'rotate', ' -1 0 0 -1' },
305                { 'kern', get_h_d_neg }, 
306                { 'box', get_w_neg },
307                { 'whatsit', sid_restore },
308             },
309          },
310       },
311    }
312 end
313
314 -- b に DIR whatsit があればその内容を attr_dir にうつす (1st ret val)
315 -- 2nd ret val はその DIR whatsit
316 local function get_box_dir(b, default)
317    local dir = has_attr(b, attr_dir) or 0
318    local bh = getlist(b)
319    local c
320    if bh and getid(bh)==id_whatsit
321    and getsubtype(bh)==sid_user and getfield(bh, 'user_id')==DIR then
322      c = bh
323       if dir==0 then
324          dir = has_attr(bh, attr_dir)
325          set_attr(b, attr_dir, dir)
326          tex_set_attr('global', attr_dir, 0)
327       end
328    end
329    return (dir==0 and default or dir), c
330 end
331
332 -- dir_node に包まれている「本来の中身」を取り出し,
333 -- dir_node を全部消去
334 local function unwrap_dir_node(b, head)
335    -- head: nil or nil-nil
336    -- if head is non-nil, return values are (new head), (next of b), (contents)
337    local bh = getlist(b)
338    local bc = node_next(node_next(node_next(bh)))
339    local nh, nb
340    node_remove(bh, bc); 
341    Dnode.flush_list(bh)
342    if head then
343       nh = insert_before(head, b, bc)
344       nh, nb = node_remove(nh, b)
345       setfield(b, 'next', nil)
346       setfield(b, 'head', nil)
347       luatexja.ext_show_node(to_node(b), ' ', print)
348       --node_free(b) -- TODO: これを入れると test51-vtest が途中で泊まる
349    end
350    local d, wh = get_box_dir(bc, 0)
351    if wh then
352       Dnode.flush_list(getfield(wh, 'value'))
353       setfield(wh, 'value', nil)
354    end
355    return nh, nb, bc
356 end
357
358 -- is_manual: 寸法変更に伴うものか?
359 local function create_dir_node(b, b_dir, new_dir, is_manual)
360    local info = dir_node_aux[b_dir][new_dir]
361    local w = getfield(b, 'width')
362    local h = getfield(b, 'height')
363    local d = getfield(b, 'depth')
364    local db = node_new(getid(b))
365    set_attr(db, attr_dir, 
366             new_dir + (is_manual and dir_node_manual or dir_node_auto))
367    set_attr(db, attr_icflag, PROCESSED)
368    set_attr(b, attr_icflag, PROCESSED)
369    setfield(db, 'dir', getfield(b, 'dir'))
370    setfield(db, 'shift', 0)
371    setfield(db, 'width',  info.width(w,h,d))
372    setfield(db, 'height', info.height(w,h,d))
373    setfield(db, 'depth',  info.depth(w,h,d))
374    return db
375 end
376
377 -- 異方向のボックスの処理
378 local make_dir_whatsit
379 do
380    make_dir_whatsit = function (head, b, new_dir, origin)
381       -- head: list head, b: box
382       -- origin: コール元 (for debug)
383       -- return value: (new head), (next of b), (new b), (is_b_dir_node)
384       -- (new b): b か dir_node に被せられた b
385       local bh = getlist(b)
386       local box_dir, dn =  get_box_dir(b, ltjs.list_dir)
387       -- 既に b の中身にあるwhatsit
388       if box_dir==new_dir then
389          return head, node_next(b), b, false
390       elseif  box_dir%dir_node_auto == new_dir  then
391 -- TODO. we have to free  all other ...
392          set_attr(b, attr_dir, box_dir%dir_node_auto + dir_node_auto)
393          return head, node_next(b), b, true
394       else
395          local nh, nb, ret, flag
396          if box_dir>= dir_node_auto then -- unwrap
397             nh, nb, ret = unwrap_dir_node(b, head)
398             head, b = nh, ret; 
399             bh = getlist(b); 
400             box_dir, dn =  get_box_dir(b, ltjs.list_dir)
401             if box_dir==new_dir then
402                return head, nb, b, fals
403             end
404          end
405             local db
406             local dnh = getfield(dn, 'value')
407             for x in traverse(dnh) do
408                if has_attr(x, attr_dir)%dir_node_auto == new_dir then
409                   setfield(dn, 'value', to_node(node_remove(dnh, x)))
410                   db=x; break
411                end
412             end
413             db = db or create_dir_node(b, box_dir, new_dir, false)
414             local w = getfield(b, 'width')
415             local h = getfield(b, 'height')
416             local d = getfield(b, 'depth')
417             nh, nb =  insert_before(head, b, db), nil
418             nh, nb = node_remove(nh, b)
419             local db_head, db_tail  = nil
420             for _,v in ipairs(dir_node_aux[box_dir][new_dir][getid(b)]) do
421                local cmd, arg, nn = v[1], v[2]
422                if cmd=='kern' then
423                   nn = node_new(id_kern)
424                   setfield(nn, 'kern', arg(w, h, d))
425                elseif cmd=='whatsit' then
426                   nn = node_new(id_whatsit, arg)
427                elseif cmd=='rotate' then
428                   nn = node_new(id_whatsit, sid_matrix)
429                   setfield(nn, 'data', arg)
430                elseif cmd=='box' then
431                   nn = b; setfield(b, 'next', nil)
432                   setfield(nn, 'shift', getfield(nn, 'shift') + arg(w,h,d))
433                end
434                if db_head then
435                   insert_after(db_head, db_tail, nn)
436                   db_tail = nn
437                else
438                   db_head, db_tail = nn, nn
439                end
440             --end
441             setfield(db, 'head', db_head)
442             ret, flag = db, true
443          end
444          return nh, nb, ret, flag
445       end
446    end
447    local function process_dir_node(head, gc)
448       start_time_measure('direction_vpack')
449       local h = to_direct(head)
450       local x, new_dir = h, ltjs.list_dir or dir_yoko
451       while x do
452          local xid = getid(x)
453          if (xid==id_hlist and has_attr(x, attr_icflag)%PROCESSED_BEGIN_FLAG~=PACKED) 
454          or xid==id_vlist then
455             h, x = make_dir_whatsit(h, x, new_dir, 'process_dir_node:' .. gc)
456          else
457             x = node_next(x)
458          end
459       end
460       stop_time_measure('direction_vpack')
461       return to_node(h)
462    end
463    luatexja.direction.make_dir_whatsit = make_dir_whatsit
464    luatexbase.add_to_callback('vpack_filter',
465                               process_dir_node, 'ltj.dir_whatsit', 10001)
466 end
467
468 -- \wd, \ht, \dp の代わり
469 do
470    local getbox, setdimen = tex.getbox, tex.setdimen
471    local function get_box_dim_common(key, s, l_dir)
472       local s_dir, wh = get_box_dir(s, dir_yoko)
473       if s_dir ~= l_dir then
474          local not_found = true
475          for x in traverse(getfield(wh, 'value')) do
476             if l_dir == has_attr(x, attr_dir)%dir_node_auto then
477                setdimen('ltj@tempdima', getfield(x, key))
478                not_found = false; break
479             end
480          end
481          if not_found then
482             local w = getfield(s, 'width')
483             local h = getfield(s, 'height')
484             local d = getfield(s, 'depth')
485             setdimen('ltj@tempdima', 
486                          dir_node_aux[s_dir][l_dir][key](w,h,d))
487          end
488       else
489          setdimen('ltj@tempdima', getfield(s, key))
490       end
491    end
492    local function get_box_dim(key, n)
493       local gt = tex.globaldefs; tex.globaldefs = 0
494       local s = getbox(n)
495       if s then
496          local l_dir = get_dir_count()
497          s = to_direct(s)
498          local b_dir = has_attr(s, attr_dir) or 0
499          if b_dir<dir_node_auto then
500             get_box_dim_common(key, s, l_dir)
501          elseif b_dir%dir_node_auto==l_dir then
502             setdimen('ltj@tempdima', getfield(s, key))
503          else
504             get_box_dim_common(
505                key, 
506                node_next(node_next(node_next(getlist(s)))),
507                l_dir)
508          end
509       else
510          setdimen('ltj@tempdima', 0)
511       end
512       tex.globaldefs = gt
513    end
514    luatexja.direction.get_box_dim = get_box_dim
515
516    -- return value: (changed dimen of box itself?)
517    local function set_box_dim_common(key, s, l_dir)
518       local s_dir, wh = get_box_dir(s, dir_yoko)
519       if s_dir ~= l_dir then
520          if not wh then
521             wh = create_dir_whatsit(getlist(s), 'set_box_dim', s_dir)
522             setfield(s, 'head', wh)
523          end
524          local db
525          local dnh = getfield(wh, 'value')
526          for x in traverse(dnh) do
527             if has_attr(x, attr_dir)%dir_node_auto==l_dir then
528                db = x; break
529             end
530          end
531          if not db then
532             db = create_dir_node(s, s_dir, l_dir, true)
533             setfield(db, 'next', dnh)
534             setfield(wh, 'value',to_node(db))
535          end
536          setfield(db, key, tex.getdimen('ltj@tempdima'))
537          return false
538       else
539          setfield(s, key, tex.getdimen('ltj@tempdima'))
540          if wh then
541             -- change dimension of dir_nodes which are created "automatically"
542                local bw, bh, bd 
543                   = getfield(s,'width'), getfield(s, 'height'), getfield(s, 'depth')
544             for x in traverse(getfield(wh, 'value')) do
545                local x_dir = has_attr(x, attr_dir)
546                if x_dir<dir_node_manual then
547                   local info = dir_node_aux[s_dir][x_dir%dir_node_auto]
548                   setfield(x, 'width',  info.width(bw,bh,bd))
549                   setfield(x, 'height', info.height(bw,bh,bd))
550                   setfield(x, 'depth',  info.depth(bw,bh,bd))
551                end
552             end
553          end
554          return true
555       end
556    end
557    local function set_box_dim(key)
558       local n = tex_getcount('ltj@tempcnta')
559       local s = getbox(n)
560       if s then
561          local l_dir = get_dir_count()
562          s = to_direct(s)
563          local b_dir = has_attr(s, attr_dir) or 0
564          if b_dir<dir_node_auto then
565             set_box_dim_common(key, s, l_dir)
566          elseif b_dir%dir_node_auto == l_dir then
567             setfield(s, key, tex.getdimen('ltj@tempdima'))
568             if b_dir<dir_node_manual then
569             end
570          else
571             local sid, sl = getid(s), getlist(s)
572             local b = node_next(node_next(node_next(sl)))
573             local info = dir_node_aux[get_box_dir(b)][b_dir%dir_node_auto]
574             local shift_old
575             for _,v in ipairs(info[sid]) do 
576                if v[1]=='box' then
577                   shift_old = v[2](
578                      getfield(b,'width'), getfield(b, 'height'), getfield(b, 'depth'))
579                   break
580                end
581             end
582            if set_box_dim_common(key, b, l_dir) then
583                local bw, bh, bd 
584                   = getfield(b,'width'), getfield(b, 'height'), getfield(b, 'depth')
585                -- re-calculate shifting info
586                for i,v in ipairs(info[sid]) do 
587                   if getid(sl)==id_kern then
588                      setfield(sl, 'kern', v[2](bw,bh,bd) )
589                   elseif getid(sl)==sid then
590                      local d = getfield(sl, 'shift')
591                      setfield(sl, 'shift', 
592                               getfield(sl, 'shift') - shift_old + v[2](bw,bh,bd) )
593                   end
594                   sl = node_next(sl)
595                end
596                -- re-calculate dimension of s, if s is created "automatically"
597                if b_dir<dir_node_manual then
598                   setfield(s, 'width',  info.width(bw,bh,bd))
599                   setfield(s, 'height', info.height(bw,bh,bd))
600                   setfield(s, 'depth',  info.depth(bw,bh,bd))
601                end
602             end
603          end
604       end
605    end
606    luatexja.direction.set_box_dim = set_box_dim
607 end
608
609 -- 縦書き用字形への変換テーブル
610 local font_vert_table = {} -- key: fontnumber
611 do
612    local font_vert_basename = {} -- key: basename
613    local function add_feature_table(tname, src, dest)
614       for i,v in pairs(src) do
615          if type(v.slookups)=='table' then
616             local s = v.slookups[tname]
617             if s and not dest[i] then
618                dest[i] = s
619             end
620          end
621       end
622    end
623
624    local function prepare_vert_data(n, id)
625       -- test if already loaded
626       if type(id)=='number' then -- sometimes id is an integer
627          font_vert_table[n] = font_vert_table[id]; return
628       elseif (not id) or font_vert_table[n]  then return
629       end
630       local fname = id.filename
631       local bname = file.basename(fname)
632       if not fname then
633          font_vert_table[n] = {}; return
634       elseif font_vert_basename[bname] then
635          font_vert_table[n] = font_vert_basename[bname]; return
636       end
637       local vtable = {}
638       local a = id.resources.sequences
639       if a then
640          local s = id.shared.rawdata.descriptions
641          for i,v in pairs(a) do
642             if v.features.vert or v.features.vrt2 then
643                add_feature_table(v.subtables[1], s, vtable)
644             end
645          end
646       end
647       font_vert_basename[bname] = vtable
648       font_vert_table[n] = vtable
649    end
650    -- 縦書き用字形への変換
651    function luatexja.direction.get_vert_glyph(n, chr)
652       local fn = font_vert_table[n]
653       return fn and fn[chr] or chr
654    end
655    luatexbase.add_to_callback('luatexja.define_font',
656                               function (res, name, size, id)
657                                  prepare_vert_data(id, res)
658                               end,
659                               'prepare_vert_data', 1)
660
661    local function a (n, dat) font_vert_table[n] = dat end
662    luatexja.rmlgbm.vert_addfunc = a
663
664 end
665
666 -- PACKED の hbox から文字を取り出す
667 -- luatexja.jfmglue.check_box などで使用
668 do
669    local function glyph_from_packed(h)
670       local b = getlist(h)
671       return (getid(b)==id_kern) 
672          and node_next(node_next(node_next(node_next(b)))) or b
673    end
674    luatexja.direction.glyph_from_packed = glyph_from_packed
675 end