OSDN Git Service

Fix ticket #33832
[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 local get_dir_count
60 -- \tate, \yoko
61 do
62    local abs = math.abs
63    function get_dir_count()
64       return tex_getcount('ltj@dir@count')
65    end
66    luatexja.direction.get_dir_count = get_dir_count 
67
68    local node_next = node.next
69    local node_set_attr = node.set_attribute
70    local function set_list_direction(v, name)
71       local lv, w = tex.nest[tex.nest.ptr], tex.lists.page_head
72       if lv.mode == 1 and w then
73          if w.id==id_whatsit and w.subtype==sid_user
74          and w.user_id==DIR then
75             node_set_attr(w, attr_dir, v)
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 wh = {}
140    local id_glue, sid_parskip = node.id('glue'), 3
141    local function create_dir_whatsit_vbox(h, gc)
142       local hd = to_direct(h)
143       ltjs.list_dir = get_dir_count()
144       -- remove dir whatsit
145       for x in traverse_id(id_whatsit, hd) do
146          if getsubtype(x)==sid_user and getfield(x, 'user_id')==DIR then
147             wh[#wh+1]=x
148          end
149       end
150       if hd==wh[1] then
151          ltjs.list_dir = has_attr(hd,attr_dir)
152          local x = node_next(hd)
153          if getid(x)==id_glue and getsubtype(x)==sid_parskip then
154             node_remove(hd,x); node_free(x)
155          end
156       end
157       for i=1,#wh do  hd = node_remove(hd, wh[i]); node_free(wh[i]); wh[i] = nil end
158       if gc=='fin_row' or gc == 'preamble'  then
159          if hd  then
160             set_attr(hd, attr_icflag, PROCESSED_BEGIN_FLAG)
161             tex_set_attr('global', attr_icflag, 0)
162          end
163          return to_node(hd)
164       elseif gc=='vtop' then
165          local w = create_dir_whatsit(hd, gc, ltjs.list_dir)
166          -- move  dir whatsit after hd
167          local n = getfield(hd, 'next')
168          setfield(hd, 'next', w); setfield(w, 'next', n)
169          return to_node(hd)
170       else
171          return to_node(create_dir_whatsit(hd, gc, ltjs.list_dir))
172       end
173    end
174    luatexbase.add_to_callback('vpack_filter', 
175                               create_dir_whatsit_vbox, 'ltj.create_dir_whatsit', 1)
176
177    local function create_dir_whatsit_parbox(h, gc)
178       stop_time_measure('tex_linebreak')
179       -- start 側は ltj-debug.lua に
180       local new_dir, hd = ltjs.list_dir, to_direct(h)
181       for line in traverse_id(id_hlist, hd) do
182          set_attr(line, attr_dir, new_dir)
183       end
184       tex_set_attr('global', attr_dir, 0)
185       return to_node(create_dir_whatsit(hd, gc, new_dir))
186    end
187    luatexbase.add_to_callback('post_linebreak_filter', 
188                               create_dir_whatsit_parbox, 'ltj.create_dir_whatsit', 10000)
189
190 end
191
192 -- dir_node に包む方法を書いたテーブル
193 local dir_node_aux
194 do
195    local get_h =function (w,h,d) return h end
196    local get_d =function (w,h,d) return d end
197    local get_h_d =function (w,h,d) return h+d end
198    local get_h_d_neg =function (w,h,d) return -h-d end
199    local get_h_neg =function (w,h,d) return -h end
200    local get_d_neg =function (w,h,d) return -d end
201    local get_w_half =function (w,h,d) return 0.5*w end
202    local get_w_neg_half =function (w,h,d) return -0.5*w end
203    local get_w_neg =function (w,h,d) return -w end
204    local get_w =function (w,h,d) return w end
205    local zero = function() return 0 end
206    dir_node_aux = {
207       [dir_yoko] = { -- yoko を 
208          [dir_tate] = { -- tate 中で組む
209             width  = get_h_d,
210             height = get_w_half,
211             depth  = get_w_half,
212             [id_hlist] = {
213                { 'whatsit', sid_save },
214                { 'rotate', '0 1 -1 0' },
215                { 'kern', get_w_neg_half },
216                { 'box' , get_h },
217                { 'kern', get_w_neg_half },
218                { 'whatsit', sid_restore },
219             },
220             [id_vlist] = {
221                { 'whatsit', sid_save },
222                { 'rotate', '0 1 -1 0' },
223                { 'kern' , zero },
224                { 'box' , get_w_neg },
225                { 'kern', get_h_d_neg},
226                { 'whatsit', sid_restore },
227             },
228          },
229          [dir_dtou] = { -- dtou 中で組む
230             width  = get_h_d,
231             height = get_w,
232             depth  = zero,
233             [id_hlist] = {
234                { 'whatsit', sid_save },
235                { 'rotate', '0 -1 1 0' },
236                { 'kern', get_w_neg },
237                { 'box', get_d_neg },
238                { 'whatsit', sid_restore },
239             },
240             [id_vlist] = {
241                { 'whatsit', sid_save },
242                { 'rotate', '0 -1 1 0' },
243                { 'kern', get_h_d_neg },
244                { 'box', zero },
245                { 'whatsit', sid_restore },
246             },
247          },
248       },
249       [dir_tate] = { -- tate を
250          [dir_yoko] = { -- yoko 中で組む
251             width  = get_h_d,
252             height = get_w,
253             depth  = zero,
254             [id_hlist] = {
255                { 'whatsit', sid_save },
256                { 'rotate', '0 -1 1 0' },
257                { 'kern', get_w_neg },
258                { 'box' , get_d_neg },
259                { 'whatsit', sid_restore },
260             },
261             [id_vlist] = {
262                { 'whatsit', sid_save },
263                { 'rotate', '0 -1 1 0' },
264                { 'kern', get_h_d_neg },
265                { 'box', zero },
266                { 'whatsit', sid_restore },
267             },
268          },
269          [dir_dtou] = { -- dtou 中で組む
270             width  = get_w,
271             height = get_d,
272             depth  = get_h,
273             [id_hlist] = {
274                { 'whatsit', sid_save },
275                { 'rotate', '-1 0 0 -1' },
276                { 'kern', get_w_neg },
277                { 'box', zero },
278                { 'whatsit', sid_restore },
279             },
280             [id_vlist] = {
281                { 'whatsit', sid_save },
282                { 'rotate', '-1 0 0 -1' },
283                { 'kern', get_h_d_neg },
284                { 'box', get_w_neg },
285                { 'whatsit', sid_restore },
286             },
287          },
288       },
289       [dir_dtou] = { -- dtou を
290          [dir_yoko] = { -- yoko 中で組む
291             width  = get_h_d,
292             height = get_w, 
293             depth  = zero,
294             [id_hlist] = {
295                { 'whatsit', sid_save },
296                { 'rotate', '0 1 -1 0' },
297                { 'kern', zero },
298                { 'box', get_h },
299                { 'kern', get_w_neg },
300                { 'whatsit', sid_restore },
301             },
302             [id_vlist] = {
303                { 'kern', zero },
304                { 'whatsit', sid_save },
305                { 'rotate', '0 1 -1 0' },
306                { 'box', get_w_neg },
307                { 'kern', get_h_d_neg },
308                { 'whatsit', sid_restore },
309             },
310          },
311          [dir_tate] = { -- tate 中で組む
312             width  = get_w,
313             height = get_d,
314             depth  = get_h,
315             [id_hlist] = {
316                { 'whatsit', sid_save },
317                { 'rotate', '-1 0 0 -1' },
318                { 'kern', get_w_neg },
319                { 'box', zero },
320                { 'whatsit', sid_restore },
321             },
322             [id_vlist] = {
323                { 'whatsit', sid_save },
324                { 'rotate', ' -1 0 0 -1' },
325                { 'kern', get_h_d_neg }, 
326                { 'box', get_w_neg },
327                { 'whatsit', sid_restore },
328             },
329          },
330       },
331    }
332 end
333
334 -- b に DIR whatsit があればその内容を attr_dir にうつす (1st ret val)
335 -- 2nd ret val はその DIR whatsit
336 local function get_box_dir(b, default)
337    local dir = has_attr(b, attr_dir) or 0
338    local bh = getlist(b)
339    local c
340    for i=1,2 do
341       if bh and getid(bh)==id_whatsit
342       and getsubtype(bh)==sid_user and getfield(bh, 'user_id')==DIR then
343          c = bh
344          if dir==0 then
345             dir = has_attr(bh, attr_dir)
346             set_attr(b, attr_dir, dir)
347             tex_set_attr('global', attr_dir, 0)
348          end
349       end
350       bh = node_next(bh)
351    end
352    return (dir==0 and default or dir), c
353 end
354
355 -- dir_node に包まれている「本来の中身」を取り出し,
356 -- dir_node を全部消去
357 local function unwrap_dir_node(b, head, box_dir)
358    -- b: dir_node, head: the head of list, box_dir: 
359    -- return values are (new head), (next of b), (contents), (dir of contents)
360    local bh = getlist(b)
361    local bc = node_next(node_next(node_next(bh)))
362    local nh, nb
363    node_remove(bh, bc); 
364    Dnode.flush_list(bh)
365    if head then
366       nh = insert_before(head, b, bc)
367       nh, nb = node_remove(nh, b)
368       setfield(b, 'next', nil)
369       setfield(b, 'head', nil)
370       node_free(b)
371    end
372    local shift_old, b_dir, wh = nil, get_box_dir(bc, 0)
373    if wh then
374       Dnode.flush_list(getfield(wh, 'value'))
375       setfield(wh, 'value', nil)
376    end
377    -- recalc. info
378    local info = dir_node_aux[b_dir][box_dir%dir_node_auto][getid(bc)]
379    for _,v in ipairs(info) do 
380       if v[1]=='box' then
381          shift_old = v[2](
382             getfield(bc,'width'), getfield(bc, 'height'), getfield(bc, 'depth'))
383          break
384       end
385    end
386    setfield(bc, 'shift', getfield(bc, 'shift') - shift_old)
387    return nh, nb, bc, b_dir
388 end
389
390 -- is_manual: 寸法変更に伴うものか?
391 local function create_dir_node(b, b_dir, new_dir, is_manual)
392    local info = dir_node_aux[b_dir][new_dir]
393    local w = getfield(b, 'width')
394    local h = getfield(b, 'height')
395    local d = getfield(b, 'depth')
396    local db = node_new(getid(b))
397    set_attr(db, attr_dir, 
398             new_dir + (is_manual and dir_node_manual or dir_node_auto))
399    set_attr(db, attr_icflag, PROCESSED)
400    set_attr(b, attr_icflag, PROCESSED)
401    setfield(db, 'dir', getfield(b, 'dir'))
402    setfield(db, 'shift', 0)
403    setfield(db, 'width',  info.width(w,h,d))
404    setfield(db, 'height', info.height(w,h,d))
405    setfield(db, 'depth',  info.depth(w,h,d))
406    return db
407 end
408
409 -- 異方向のボックスの処理
410 local make_dir_whatsit
411 do
412    make_dir_whatsit = function (head, b, new_dir, origin)
413       -- head: list head, b: box
414       -- origin: コール元 (for debug)
415       -- return value: (new head), (next of b), (new b), (is_b_dir_node)
416       -- (new b): b か dir_node に被せられた b
417       local bh = getlist(b)
418       local box_dir, dn =  get_box_dir(b, ltjs.list_dir)
419       -- 既に b の中身にあるwhatsit
420       if box_dir==new_dir then
421          return head, node_next(b), b, false
422       elseif  box_dir%dir_node_auto == new_dir  then
423          local bc = node_next(node_next(node_next(bh)))
424          local _, dnc = get_box_dir(b, 0)
425          if dnc then -- free all other dir_node
426             Dnode.flush_list(getfield(dnc, 'value'))
427             setfield(dnc, 'value', nil)
428          end
429          set_attr(b, attr_dir, box_dir%dir_node_auto + dir_node_auto)
430          return head, node_next(b), b, true
431       else
432          local nh, nb, ret, flag
433          if box_dir>= dir_node_auto then -- unwrap
434             local b_dir
435             head, nb, b, b_dir = unwrap_dir_node(b, head, box_dir)
436             bh = getlist(b)
437             if b_dir==new_dir then -- no need to create new dir_node
438                return head, nb, b, false 
439             else box_dir = b_dir end
440          end
441             local db
442             local dnh = getfield(dn, 'value')
443             for x in traverse(dnh) do
444                if has_attr(x, attr_dir)%dir_node_auto == new_dir then
445                   setfield(dn, 'value', to_node(node_remove(dnh, x)))
446                   db=x; break
447                end
448             end
449             Dnode.flush_list(dnh)
450             db = db or create_dir_node(b, box_dir, new_dir, false)
451             local w = getfield(b, 'width')
452             local h = getfield(b, 'height')
453             local d = getfield(b, 'depth')
454             nh, nb =  insert_before(head, b, db), nil
455             nh, nb = node_remove(nh, b)
456             local db_head, db_tail  = nil
457             for _,v in ipairs(dir_node_aux[box_dir][new_dir][getid(b)]) do
458                local cmd, arg, nn = v[1], v[2]
459                if cmd=='kern' then
460                   nn = node_new(id_kern)
461                   setfield(nn, 'kern', arg(w, h, d))
462                elseif cmd=='whatsit' then
463                   nn = node_new(id_whatsit, arg)
464                elseif cmd=='rotate' then
465                   nn = node_new(id_whatsit, sid_matrix)
466                   setfield(nn, 'data', arg)
467                elseif cmd=='box' then
468                   nn = b; setfield(b, 'next', nil)
469                   setfield(nn, 'shift', getfield(nn, 'shift') + arg(w,h,d))
470                end
471                if db_head then
472                   insert_after(db_head, db_tail, nn)
473                   db_tail = nn
474                else
475                   db_head, db_tail = nn, nn
476                end
477             --end
478             setfield(db, 'head', db_head)
479             ret, flag = db, true
480          end
481          return nh, nb, ret, flag
482       end
483    end
484    local function process_dir_node(head, gc)
485       start_time_measure('direction_vpack')
486       local h = to_direct(head)
487       local x, new_dir = h, ltjs.list_dir or dir_yoko
488       while x do
489          local xid = getid(x)
490          if (xid==id_hlist and has_attr(x, attr_icflag)%PROCESSED_BEGIN_FLAG~=PACKED) 
491          or xid==id_vlist then
492             h, x = make_dir_whatsit(h, x, new_dir, 'process_dir_node:' .. gc)
493          else
494             x = node_next(x)
495          end
496       end
497       stop_time_measure('direction_vpack')
498       return to_node(h)
499    end
500    luatexja.direction.make_dir_whatsit = make_dir_whatsit
501    luatexbase.add_to_callback('vpack_filter',
502                               process_dir_node, 'ltj.dir_whatsit', 10001)
503 end
504
505 -- \wd, \ht, \dp の代わり
506 do
507    local getbox, setdimen = tex.getbox, tex.setdimen
508    local function get_box_dim_common(key, s, l_dir)
509       local s_dir, wh = get_box_dir(s, dir_yoko)
510       if s_dir ~= l_dir then
511          local not_found = true
512          for x in traverse(getfield(wh, 'value')) do
513             if l_dir == has_attr(x, attr_dir)%dir_node_auto then
514                setdimen('ltj@tempdima', getfield(x, key))
515                not_found = false; break
516             end
517          end
518          if not_found then
519             local w = getfield(s, 'width')
520             local h = getfield(s, 'height')
521             local d = getfield(s, 'depth')
522             setdimen('ltj@tempdima', 
523                          dir_node_aux[s_dir][l_dir][key](w,h,d))
524          end
525       else
526          setdimen('ltj@tempdima', getfield(s, key))
527       end
528    end
529    local function get_box_dim(key, n)
530       local gt = tex.globaldefs; tex.globaldefs = 0
531       local s = getbox(n)
532       if s then
533          local l_dir = get_dir_count()
534          s = to_direct(s)
535          local b_dir = has_attr(s, attr_dir) or 0
536          if b_dir<dir_node_auto then
537             get_box_dim_common(key, s, l_dir)
538          elseif b_dir%dir_node_auto==l_dir then
539             setdimen('ltj@tempdima', getfield(s, key))
540          else
541             get_box_dim_common(key, 
542                                node_next(node_next(node_next(getlist(s)))), l_dir)
543          end
544       else
545          setdimen('ltj@tempdima', 0)
546       end
547       tex.globaldefs = gt
548    end
549    luatexja.direction.get_box_dim = get_box_dim
550
551    -- return value: (changed dimen of box itself?)
552    local function set_box_dim_common(key, s, l_dir)
553       local s_dir, wh = get_box_dir(s, dir_yoko)
554       if s_dir ~= l_dir then
555          if not wh then
556             wh = create_dir_whatsit(getlist(s), 'set_box_dim', s_dir)
557             setfield(s, 'head', wh)
558          end
559          local db
560          local dnh = getfield(wh, 'value')
561          for x in traverse(dnh) do
562             if has_attr(x, attr_dir)%dir_node_auto==l_dir then
563                db = x; break
564             end
565          end
566          if not db then
567             db = create_dir_node(s, s_dir, l_dir, true)
568             setfield(db, 'next', dnh)
569             setfield(wh, 'value',to_node(db))
570          end
571          setfield(db, key, tex.getdimen('ltj@tempdima'))
572          return false
573       else
574          setfield(s, key, tex.getdimen('ltj@tempdima'))
575          if wh then
576             -- change dimension of dir_nodes which are created "automatically"
577                local bw, bh, bd 
578                   = getfield(s,'width'), getfield(s, 'height'), getfield(s, 'depth')
579             for x in traverse(getfield(wh, 'value')) do
580                local x_dir = has_attr(x, attr_dir)
581                if x_dir<dir_node_manual then
582                   local info = dir_node_aux[s_dir][x_dir%dir_node_auto]
583                   setfield(x, 'width',  info.width(bw,bh,bd))
584                   setfield(x, 'height', info.height(bw,bh,bd))
585                   setfield(x, 'depth',  info.depth(bw,bh,bd))
586                end
587             end
588          end
589          return true
590       end
591    end
592    local function set_box_dim(key)
593       local n = tex_getcount('ltj@tempcnta')
594       local s = getbox(n)
595       if s then
596          local l_dir = get_dir_count()
597          s = to_direct(s)
598          local b_dir = has_attr(s, attr_dir) or 0
599          if b_dir<dir_node_auto then
600             set_box_dim_common(key, s, l_dir)
601          elseif b_dir%dir_node_auto == l_dir then
602             setfield(s, key, tex.getdimen('ltj@tempdima'))
603             if b_dir<dir_node_manual then
604                set_attr(s, attr_dir, b_dir%dir_node_auto + dir_node_manual)
605             end
606          else
607             local sid, sl = getid(s), getlist(s)
608             local b = node_next(node_next(node_next(sl)))
609             local info = dir_node_aux[get_box_dir(b)][b_dir%dir_node_auto]
610             local shift_old
611             for _,v in ipairs(info[sid]) do 
612                if v[1]=='box' then
613                   shift_old = v[2](
614                      getfield(b,'width'), getfield(b, 'height'), getfield(b, 'depth'))
615                   break
616                end
617             end
618            if set_box_dim_common(key, b, l_dir) then
619                local bw, bh, bd 
620                   = getfield(b,'width'), getfield(b, 'height'), getfield(b, 'depth')
621                -- re-calculate shift
622                for i,v in ipairs(info[sid]) do 
623                   if getid(sl)==id_kern then
624                      setfield(sl, 'kern', v[2](bw,bh,bd) )
625                   elseif getid(sl)==sid then
626                      local d = getfield(sl, 'shift')
627                      setfield(sl, 'shift', 
628                               getfield(sl, 'shift') - shift_old + v[2](bw,bh,bd) )
629                   end
630                   sl = node_next(sl)
631                end
632                -- re-calculate dimension of s, if s is created "automatically"
633                if b_dir<dir_node_manual then
634                   setfield(s, 'width',  info.width(bw,bh,bd))
635                   setfield(s, 'height', info.height(bw,bh,bd))
636                   setfield(s, 'depth',  info.depth(bw,bh,bd))
637                end
638             end
639          end
640       end
641    end
642    luatexja.direction.set_box_dim = set_box_dim
643 end
644
645 -- \ifydir, \iftdir, \ifddir
646 do
647    local cat_lp = luatexbase.catcodetables['latex-package']
648    local getbox = tex.getbox
649    local function dir_conditional(n, mode)
650       local s = getbox(n)
651       local res = false
652       if s then
653          s = to_direct(s)
654          local b_dir = get_box_dir(s, dir_yoko)
655          if b_dir<dir_node_auto then
656             res = (b_dir==mode)
657          else
658             local b_dir = get_box_dir(
659                node_next(node_next(node_next(getlist(s)))), dir_yoko)
660             res = (b_dir==mode)
661          end
662       end
663       tex.sprint(cat_lp, '\\if' .. tostring(res))
664    end
665    luatexja.direction.dir_conditional = dir_conditional
666 end
667
668 -- 縦書き用字形への変換テーブル
669 local font_vert_table = {} -- key: fontnumber
670 do
671    local font_vert_basename = {} -- key: basename
672    local function add_feature_table(tname, src, dest)
673       for i,v in pairs(src) do
674          if type(v.slookups)=='table' then
675             local s = v.slookups[tname]
676             if s and not dest[i] then
677                dest[i] = s
678             end
679          end
680       end
681    end
682
683    local function prepare_vert_data(n, id)
684       -- test if already loaded
685       if type(id)=='number' then -- sometimes id is an integer
686          font_vert_table[n] = font_vert_table[id]; return
687       elseif (not id) or font_vert_table[n]  then return
688       end
689       local fname = id.filename
690       local bname = file.basename(fname)
691       if not fname then
692          font_vert_table[n] = {}; return
693       elseif font_vert_basename[bname] then
694          font_vert_table[n] = font_vert_basename[bname]; return
695       end
696       local vtable = {}
697       local a = id.resources.sequences
698       if a then
699          local s = id.shared.rawdata.descriptions
700          for i,v in pairs(a) do
701             if v.features.vert or v.features.vrt2 then
702                add_feature_table(v.subtables[1], s, vtable)
703             end
704          end
705       end
706       font_vert_basename[bname] = vtable
707       font_vert_table[n] = vtable
708    end
709    -- 縦書き用字形への変換
710    function luatexja.direction.get_vert_glyph(n, chr)
711       local fn = font_vert_table[n]
712       return fn and fn[chr] or chr
713    end
714    luatexbase.add_to_callback('luatexja.define_font',
715                               function (res, name, size, id)
716                                  prepare_vert_data(id, res)
717                               end,
718                               'prepare_vert_data', 1)
719
720    local function a (n, dat) font_vert_table[n] = dat end
721    luatexja.rmlgbm.vert_addfunc = a
722
723 end
724
725 -- PACKED の hbox から文字を取り出す
726 -- luatexja.jfmglue.check_box などで使用
727 do
728    local function glyph_from_packed(h)
729       local b = getlist(h)
730       return (getid(b)==id_kern) 
731          and node_next(node_next(node_next(node_next(b)))) or b
732    end
733    luatexja.direction.glyph_from_packed = glyph_from_packed
734 end