OSDN Git Service

Update the manual, and fix placement of \dtou box in \yoko dir.
[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 = get_w, 
274             depth  = zero,
275             [id_hlist] = {
276                { 'whatsit', sid_save },
277                { 'rotate', '0 1 -1 0' },
278                { 'kern', zero },
279                { 'box', get_h },
280                { 'kern', get_w_neg },
281                { 'whatsit', sid_restore },
282             },
283             [id_vlist] = {
284                { 'kern', zero },
285                { 'whatsit', sid_save },
286                { 'rotate', '0 1 -1 0' },
287                { 'box', get_w_neg },
288                { 'kern', get_h_d_neg },
289                { 'whatsit', sid_restore },
290             },
291          },
292          [dir_tate] = { -- tate 中で組む
293             width  = get_w,
294             height = get_d,
295             depth  = get_h,
296             [id_hlist] = {
297                { 'whatsit', sid_save },
298                { 'rotate', '-1 0 0 -1' },
299                { 'kern', get_w_neg },
300                { 'box', zero },
301                { 'whatsit', sid_restore },
302             },
303             [id_vlist] = {
304                { 'whatsit', sid_save },
305                { 'rotate', ' -1 0 0 -1' },
306                { 'kern', get_h_d_neg }, 
307                { 'box', get_w_neg },
308                { 'whatsit', sid_restore },
309             },
310          },
311       },
312    }
313 end
314
315 -- b に DIR whatsit があればその内容を attr_dir にうつす (1st ret val)
316 -- 2nd ret val はその DIR whatsit
317 local function get_box_dir(b, default)
318    local dir = has_attr(b, attr_dir) or 0
319    local bh = getlist(b)
320    local c
321    if bh and getid(bh)==id_whatsit
322    and getsubtype(bh)==sid_user and getfield(bh, 'user_id')==DIR then
323      c = bh
324       if dir==0 then
325          dir = has_attr(bh, attr_dir)
326          set_attr(b, attr_dir, dir)
327          tex_set_attr('global', attr_dir, 0)
328       end
329    end
330    return (dir==0 and default or dir), c
331 end
332
333 -- dir_node に包まれている「本来の中身」を取り出し,
334 -- dir_node を全部消去
335 local function unwrap_dir_node(b, head, box_dir)
336    -- b: dir_node, head: the head of list, box_dir: 
337    -- return values are (new head), (next of b), (contents), (dir of contents)
338    local bh = getlist(b)
339    local bc = node_next(node_next(node_next(bh)))
340    local nh, nb
341    node_remove(bh, bc); 
342    Dnode.flush_list(bh)
343    if head then
344       nh = insert_before(head, b, bc)
345       nh, nb = node_remove(nh, b)
346       setfield(b, 'next', nil)
347       setfield(b, 'head', nil)
348       node_free(b)
349    end
350    local shift_old, b_dir, wh = nil, get_box_dir(bc, 0)
351    if wh then
352       Dnode.flush_list(getfield(wh, 'value'))
353       setfield(wh, 'value', nil)
354    end
355    -- recalc. info
356    local info = dir_node_aux[b_dir][box_dir%dir_node_auto][getid(bc)]
357    for _,v in ipairs(info) do 
358       if v[1]=='box' then
359          shift_old = v[2](
360             getfield(bc,'width'), getfield(bc, 'height'), getfield(bc, 'depth'))
361          break
362       end
363    end
364    setfield(bc, 'shift', getfield(bc, 'shift') - shift_old)
365    return nh, nb, bc, b_dir
366 end
367
368 -- is_manual: 寸法変更に伴うものか?
369 local function create_dir_node(b, b_dir, new_dir, is_manual)
370    local info = dir_node_aux[b_dir][new_dir]
371    local w = getfield(b, 'width')
372    local h = getfield(b, 'height')
373    local d = getfield(b, 'depth')
374    local db = node_new(getid(b))
375    set_attr(db, attr_dir, 
376             new_dir + (is_manual and dir_node_manual or dir_node_auto))
377    set_attr(db, attr_icflag, PROCESSED)
378    set_attr(b, attr_icflag, PROCESSED)
379    setfield(db, 'dir', getfield(b, 'dir'))
380    setfield(db, 'shift', 0)
381    setfield(db, 'width',  info.width(w,h,d))
382    setfield(db, 'height', info.height(w,h,d))
383    setfield(db, 'depth',  info.depth(w,h,d))
384    return db
385 end
386
387 -- 異方向のボックスの処理
388 local make_dir_whatsit
389 do
390    make_dir_whatsit = function (head, b, new_dir, origin)
391       -- head: list head, b: box
392       -- origin: コール元 (for debug)
393       -- return value: (new head), (next of b), (new b), (is_b_dir_node)
394       -- (new b): b か dir_node に被せられた b
395       local bh = getlist(b)
396       local box_dir, dn =  get_box_dir(b, ltjs.list_dir)
397       -- 既に b の中身にあるwhatsit
398       if box_dir==new_dir then
399          return head, node_next(b), b, false
400       elseif  box_dir%dir_node_auto == new_dir  then
401          local bc = node_next(node_next(node_next(bh)))
402          local _, dnc = get_box_dir(b, 0)
403          if dnc then -- free all other dir_node
404             Dnode.flush_list(getfield(dnc, 'value'))
405             setfield(dnc, 'value', nil)
406          end
407          set_attr(b, attr_dir, box_dir%dir_node_auto + dir_node_auto)
408          return head, node_next(b), b, true
409       else
410          local nh, nb, ret, flag
411          if box_dir>= dir_node_auto then -- unwrap
412             local b_dir
413             head, nb, b, b_dir = unwrap_dir_node(b, head, box_dir)
414             bh = getlist(b)
415             if b_dir==new_dir then -- no need to create new dir_node
416                return head, nb, b, false 
417             else box_dir = b_dir end
418          end
419             local db
420             local dnh = getfield(dn, 'value')
421             for x in traverse(dnh) do
422                if has_attr(x, attr_dir)%dir_node_auto == new_dir then
423                   setfield(dn, 'value', to_node(node_remove(dnh, x)))
424                   db=x; break
425                end
426             end
427             Dnode.flush_list(dnh)
428             db = db or create_dir_node(b, box_dir, new_dir, false)
429             local w = getfield(b, 'width')
430             local h = getfield(b, 'height')
431             local d = getfield(b, 'depth')
432             nh, nb =  insert_before(head, b, db), nil
433             nh, nb = node_remove(nh, b)
434             local db_head, db_tail  = nil
435             for _,v in ipairs(dir_node_aux[box_dir][new_dir][getid(b)]) do
436                local cmd, arg, nn = v[1], v[2]
437                if cmd=='kern' then
438                   nn = node_new(id_kern)
439                   setfield(nn, 'kern', arg(w, h, d))
440                elseif cmd=='whatsit' then
441                   nn = node_new(id_whatsit, arg)
442                elseif cmd=='rotate' then
443                   nn = node_new(id_whatsit, sid_matrix)
444                   setfield(nn, 'data', arg)
445                elseif cmd=='box' then
446                   nn = b; setfield(b, 'next', nil)
447                   setfield(nn, 'shift', getfield(nn, 'shift') + arg(w,h,d))
448                end
449                if db_head then
450                   insert_after(db_head, db_tail, nn)
451                   db_tail = nn
452                else
453                   db_head, db_tail = nn, nn
454                end
455             --end
456             setfield(db, 'head', db_head)
457             ret, flag = db, true
458          end
459          return nh, nb, ret, flag
460       end
461    end
462    local function process_dir_node(head, gc)
463       start_time_measure('direction_vpack')
464       local h = to_direct(head)
465       local x, new_dir = h, ltjs.list_dir or dir_yoko
466       while x do
467          local xid = getid(x)
468          if (xid==id_hlist and has_attr(x, attr_icflag)%PROCESSED_BEGIN_FLAG~=PACKED) 
469          or xid==id_vlist then
470             h, x = make_dir_whatsit(h, x, new_dir, 'process_dir_node:' .. gc)
471          else
472             x = node_next(x)
473          end
474       end
475       stop_time_measure('direction_vpack')
476       return to_node(h)
477    end
478    luatexja.direction.make_dir_whatsit = make_dir_whatsit
479    luatexbase.add_to_callback('vpack_filter',
480                               process_dir_node, 'ltj.dir_whatsit', 10001)
481 end
482
483 -- \wd, \ht, \dp の代わり
484 do
485    local getbox, setdimen = tex.getbox, tex.setdimen
486    local function get_box_dim_common(key, s, l_dir)
487       local s_dir, wh = get_box_dir(s, dir_yoko)
488       if s_dir ~= l_dir then
489          local not_found = true
490          for x in traverse(getfield(wh, 'value')) do
491             if l_dir == has_attr(x, attr_dir)%dir_node_auto then
492                setdimen('ltj@tempdima', getfield(x, key))
493                not_found = false; break
494             end
495          end
496          if not_found then
497             local w = getfield(s, 'width')
498             local h = getfield(s, 'height')
499             local d = getfield(s, 'depth')
500             setdimen('ltj@tempdima', 
501                          dir_node_aux[s_dir][l_dir][key](w,h,d))
502          end
503       else
504          setdimen('ltj@tempdima', getfield(s, key))
505       end
506    end
507    local function get_box_dim(key, n)
508       local gt = tex.globaldefs; tex.globaldefs = 0
509       local s = getbox(n)
510       if s then
511          local l_dir = get_dir_count()
512          s = to_direct(s)
513          local b_dir = has_attr(s, attr_dir) or 0
514          if b_dir<dir_node_auto then
515             get_box_dim_common(key, s, l_dir)
516          elseif b_dir%dir_node_auto==l_dir then
517             setdimen('ltj@tempdima', getfield(s, key))
518          else
519             get_box_dim_common(key, 
520                                node_next(node_next(node_next(getlist(s)))), l_dir)
521          end
522       else
523          setdimen('ltj@tempdima', 0)
524       end
525       tex.globaldefs = gt
526    end
527    luatexja.direction.get_box_dim = get_box_dim
528
529    -- return value: (changed dimen of box itself?)
530    local function set_box_dim_common(key, s, l_dir)
531       local s_dir, wh = get_box_dir(s, dir_yoko)
532       if s_dir ~= l_dir then
533          if not wh then
534             wh = create_dir_whatsit(getlist(s), 'set_box_dim', s_dir)
535             setfield(s, 'head', wh)
536          end
537          local db
538          local dnh = getfield(wh, 'value')
539          for x in traverse(dnh) do
540             if has_attr(x, attr_dir)%dir_node_auto==l_dir then
541                db = x; break
542             end
543          end
544          if not db then
545             db = create_dir_node(s, s_dir, l_dir, true)
546             setfield(db, 'next', dnh)
547             setfield(wh, 'value',to_node(db))
548          end
549          setfield(db, key, tex.getdimen('ltj@tempdima'))
550          return false
551       else
552          setfield(s, key, tex.getdimen('ltj@tempdima'))
553          if wh then
554             -- change dimension of dir_nodes which are created "automatically"
555                local bw, bh, bd 
556                   = getfield(s,'width'), getfield(s, 'height'), getfield(s, 'depth')
557             for x in traverse(getfield(wh, 'value')) do
558                local x_dir = has_attr(x, attr_dir)
559                if x_dir<dir_node_manual then
560                   local info = dir_node_aux[s_dir][x_dir%dir_node_auto]
561                   setfield(x, 'width',  info.width(bw,bh,bd))
562                   setfield(x, 'height', info.height(bw,bh,bd))
563                   setfield(x, 'depth',  info.depth(bw,bh,bd))
564                end
565             end
566          end
567          return true
568       end
569    end
570    local function set_box_dim(key)
571       local n = tex_getcount('ltj@tempcnta')
572       local s = getbox(n)
573       if s then
574          local l_dir = get_dir_count()
575          s = to_direct(s)
576          local b_dir = has_attr(s, attr_dir) or 0
577          if b_dir<dir_node_auto then
578             set_box_dim_common(key, s, l_dir)
579          elseif b_dir%dir_node_auto == l_dir then
580             setfield(s, key, tex.getdimen('ltj@tempdima'))
581             if b_dir<dir_node_manual then
582                set_attr(s, attr_dir, b_dir%dir_node_auto + dir_node_manual)
583                tex_set_attr('global', attr_dir, 0)
584             end
585          else
586             local sid, sl = getid(s), getlist(s)
587             local b = node_next(node_next(node_next(sl)))
588             local info = dir_node_aux[get_box_dir(b)][b_dir%dir_node_auto]
589             local shift_old
590             for _,v in ipairs(info[sid]) do 
591                if v[1]=='box' then
592                   shift_old = v[2](
593                      getfield(b,'width'), getfield(b, 'height'), getfield(b, 'depth'))
594                   break
595                end
596             end
597            if set_box_dim_common(key, b, l_dir) then
598                local bw, bh, bd 
599                   = getfield(b,'width'), getfield(b, 'height'), getfield(b, 'depth')
600                -- re-calculate shift
601                for i,v in ipairs(info[sid]) do 
602                   if getid(sl)==id_kern then
603                      setfield(sl, 'kern', v[2](bw,bh,bd) )
604                   elseif getid(sl)==sid then
605                      local d = getfield(sl, 'shift')
606                      setfield(sl, 'shift', 
607                               getfield(sl, 'shift') - shift_old + v[2](bw,bh,bd) )
608                   end
609                   sl = node_next(sl)
610                end
611                -- re-calculate dimension of s, if s is created "automatically"
612                if b_dir<dir_node_manual then
613                   setfield(s, 'width',  info.width(bw,bh,bd))
614                   setfield(s, 'height', info.height(bw,bh,bd))
615                   setfield(s, 'depth',  info.depth(bw,bh,bd))
616                end
617             end
618          end
619       end
620    end
621    luatexja.direction.set_box_dim = set_box_dim
622 end
623
624 -- 縦書き用字形への変換テーブル
625 local font_vert_table = {} -- key: fontnumber
626 do
627    local font_vert_basename = {} -- key: basename
628    local function add_feature_table(tname, src, dest)
629       for i,v in pairs(src) do
630          if type(v.slookups)=='table' then
631             local s = v.slookups[tname]
632             if s and not dest[i] then
633                dest[i] = s
634             end
635          end
636       end
637    end
638
639    local function prepare_vert_data(n, id)
640       -- test if already loaded
641       if type(id)=='number' then -- sometimes id is an integer
642          font_vert_table[n] = font_vert_table[id]; return
643       elseif (not id) or font_vert_table[n]  then return
644       end
645       local fname = id.filename
646       local bname = file.basename(fname)
647       if not fname then
648          font_vert_table[n] = {}; return
649       elseif font_vert_basename[bname] then
650          font_vert_table[n] = font_vert_basename[bname]; return
651       end
652       local vtable = {}
653       local a = id.resources.sequences
654       if a then
655          local s = id.shared.rawdata.descriptions
656          for i,v in pairs(a) do
657             if v.features.vert or v.features.vrt2 then
658                add_feature_table(v.subtables[1], s, vtable)
659             end
660          end
661       end
662       font_vert_basename[bname] = vtable
663       font_vert_table[n] = vtable
664    end
665    -- 縦書き用字形への変換
666    function luatexja.direction.get_vert_glyph(n, chr)
667       local fn = font_vert_table[n]
668       return fn and fn[chr] or chr
669    end
670    luatexbase.add_to_callback('luatexja.define_font',
671                               function (res, name, size, id)
672                                  prepare_vert_data(id, res)
673                               end,
674                               'prepare_vert_data', 1)
675
676    local function a (n, dat) font_vert_table[n] = dat end
677    luatexja.rmlgbm.vert_addfunc = a
678
679 end
680
681 -- PACKED の hbox から文字を取り出す
682 -- luatexja.jfmglue.check_box などで使用
683 do
684    local function glyph_from_packed(h)
685       local b = getlist(h)
686       return (getid(b)==id_kern) 
687          and node_next(node_next(node_next(node_next(b)))) or b
688    end
689    luatexja.direction.glyph_from_packed = glyph_from_packed
690 end