OSDN Git Service

Suppress 'underfull {h,v}box' warning for 'empty' boxes, as original [Lua]TeX.
[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 cat_lp = luatexbase.catcodetables['latex-package']
14 local Dnode = node.direct or node
15 local nullfunc = function (n) return n end
16 local to_node = (Dnode ~= node) and Dnode.tonode or nullfunc
17 local to_direct = (Dnode ~= node) and Dnode.todirect or nullfunc
18 local has_attr = Dnode.has_attribute
19 local set_attr = Dnode.set_attribute
20 local insert_before = Dnode.insert_before
21 local insert_after = Dnode.insert_after
22 local getid = (Dnode ~= node) and Dnode.getid or function(n) return n.id end
23 local getsubtype = (Dnode ~= node) and Dnode.getsubtype or function(n) return n.subtype end
24 local getlist = (Dnode ~= node) and Dnode.getlist or function(n) return n.head end
25 local setfield = (Dnode ~= node) and Dnode.setfield or function(n, i, c) n[i] = c end
26 local getfield = (Dnode ~= node) and Dnode.getfield or function(n, i) return n[i] end
27 local node_new = Dnode.new
28 local node_tail = Dnode.tail
29 local node_free = Dnode.free
30 local node_remove = Dnode.remove
31 local node_next = (Dnode ~= node) and Dnode.getnext or node.next
32 local traverse = Dnode.traverse
33 local traverse_id = Dnode.traverse_id
34 local start_time_measure, stop_time_measure 
35    = ltjb.start_time_measure, ltjb.stop_time_measure
36
37 local id_kern = node.id('kern')
38 local id_hlist = node.id('hlist')
39 local id_vlist = node.id('vlist')
40 local id_whatsit = node.id('whatsit')
41 local sid_save = node.subtype('pdf_save')
42 local sid_restore = node.subtype('pdf_restore')
43 local sid_matrix = node.subtype('pdf_setmatrix')
44 local sid_user = node.subtype('user_defined')
45
46 local tex_nest = tex.nest
47 local tex_getcount = tex.getcount
48 local tex_set_attr = tex.setattribute
49 local PROCESSED    = luatexja.icflag_table.PROCESSED
50 local PROCESSED_BEGIN_FLAG = luatexja.icflag_table.PROCESSED_BEGIN_FLAG
51 local PACKED       = luatexja.icflag_table.PACKED
52 local STCK = luatexja.userid_table.STCK
53 local DIR  = luatexja.userid_table.DIR
54 local dir_tate = luatexja.dir_table.dir_tate
55 local dir_yoko = luatexja.dir_table.dir_yoko
56 local dir_dtou = luatexja.dir_table.dir_dtou
57 local dir_node_auto   = luatexja.dir_table.dir_node_auto
58 local dir_node_manual = luatexja.dir_table.dir_node_manual
59
60
61 --
62 local function adjust_badness(hd)
63    if not node_next(hd) and getid(hd)==id_whatsit and getsubtype(hd)==sid_user
64    and getfield(hd, 'user_id')==DIR then
65       -- avoid double whatsit
66       luatexja.global_temp=tex.globaldefs; tex.globaldefs=0
67       luatexja.hbadness_temp=tex.hbadness; tex.hbadness=10000
68       luatexja.vbadness_temp=tex.vbadness; tex.vbadness=10000
69    else
70       luatexja.global_temp = nil
71       luatexja.hbadness_temp=nil
72       luatexja.vbadness_temp=nil
73    end
74 end
75
76 local get_dir_count
77 -- \tate, \yoko
78 do
79    function get_dir_count()
80       return tex_getcount('ltj@dir@count')
81    end
82    luatexja.direction.get_dir_count = get_dir_count 
83
84    local node_next = node.next
85    local node_set_attr = node.set_attribute
86    local function set_list_direction(v, name)
87       local lv, w = tex_nest[tex_nest.ptr], tex.lists.page_head
88       if lv.mode == 1 and w then
89          if w.id==id_whatsit and w.subtype==sid_user
90          and w.user_id==DIR then
91             node_set_attr(w, attr_dir, v)
92          end
93       else
94          local w = node_next(lv.head)
95          if w then
96             w = to_direct(w)
97             if getid(w)==id_whatsit and getsubtype(w)==sid_user
98             and getfield(w, 'user_id')==DIR then
99                set_attr(w, attr_dir, v)
100             else
101               ltjb.package_error(
102                  'luatexja',
103                  "Use `\\" .. name .. "' at top of list",
104                  'Direction change command by LuaTeX-ja is available\n'
105                     .. 'only when the current list is null.')
106             end
107          else
108             local w = node_new(id_whatsit, sid_user)
109             setfield(w, 'next', nil)
110             setfield(w, 'user_id', DIR)
111             setfield(w, 'type', 110)
112             set_attr(w, attr_dir, v)
113             Dnode.write(w)
114          end
115          tex_set_attr('global', attr_icflag, 0)
116       end
117       tex_set_attr('global', attr_dir, 0)
118    end
119    luatexja.direction.set_list_direction = set_list_direction
120 end
121
122 -- ボックスに dir whatsit を追加
123 local function create_dir_whatsit(hd, gc, new_dir)
124       local w = node_new(id_whatsit, sid_user)
125       setfield(w, 'next', hd)
126       setfield(w, 'user_id', DIR)
127       setfield(w, 'type', 110)
128       set_attr(w, attr_dir, new_dir)
129       tex_set_attr('global', attr_dir, 0)  
130       set_attr(w, attr_icflag, PROCESSED_BEGIN_FLAG)
131       set_attr(hd, attr_icflag, (has_attr(hd, attr_icflag) or 0) + PROCESSED_BEGIN_FLAG)
132       tex_set_attr('global', attr_icflag, 0)
133       return w
134 end
135
136 -- hpack_filter, vpack_filter, post_line_break_filter
137 -- の結果を組方向を明示するため,先頭に dir_node を設置
138 do
139    local function create_dir_whatsit_hpack(h, gc)
140       local hd = to_direct(h)
141       if gc=='fin_row' or gc == 'preamble'  then
142          if hd  then
143             set_attr(hd, attr_icflag, PROCESSED_BEGIN_FLAG)
144             tex_set_attr('global', attr_icflag, 0)
145          end
146          return h
147       else
148          adjust_badness(hd)
149          if (c=='hbox' or gc=='adjusted_hbox') and 
150             not node_next(hd) and getid(hd)==id_whatsit and 
151             getsubtype(hd)==sid_user and getfield(hd, 'user_id')==DIR then
152                -- avoid double whatsit
153                set_attr(hd, attr_icflag, PROCESSED_BEGIN_FLAG)
154                tex_set_attr('global', attr_icflag, 0)
155                return h
156          else
157             return to_node(create_dir_whatsit(hd, gc, ltjs.list_dir))
158          end
159       end
160    end
161
162    luatexbase.add_to_callback('hpack_filter', 
163                               create_dir_whatsit_hpack, 'ltj.create_dir_whatsit', 10000)
164 end
165
166 do
167    local function create_dir_whatsit_parbox(h, gc)
168       stop_time_measure('tex_linebreak')
169       -- start 側は ltj-debug.lua に
170       local new_dir, hd = ltjs.list_dir, to_direct(h)
171       for line in traverse_id(id_hlist, hd) do
172          set_attr(line, attr_dir, new_dir)
173       end
174       tex_set_attr('global', attr_dir, 0)
175       return h --to_node(create_dir_whatsit(hd, gc, new_dir))
176    end
177    luatexbase.add_to_callback('post_linebreak_filter', 
178                               create_dir_whatsit_parbox, 'ltj.create_dir_whatsit', 10000)
179 end
180
181 local create_dir_whatsit_vbox
182 do
183    local wh = {}
184    local id_glue, sid_parskip = node.id('glue'), 3
185    create_dir_whatsit_vbox = function (hd, gc)
186       ltjs.list_dir = get_dir_count()
187       -- remove dir whatsit
188       for x in traverse_id(id_whatsit, hd) do
189          if getsubtype(x)==sid_user and getfield(x, 'user_id')==DIR then
190             wh[#wh+1]=x
191          end
192       end
193       if hd==wh[1] then
194          ltjs.list_dir =has_attr(hd,attr_dir)
195          local x = node_next(hd)
196          if getid(x)==id_glue and getsubtype(x)==sid_parskip then
197             node_remove(hd,x); node_free(x)
198          end
199       end
200       for i=1,#wh do  
201          hd = node_remove(hd, wh[i]); node_free(wh[i]); wh[i] = nil 
202       end
203       if gc=='fin_row' or gc == 'preamble'  then
204          if hd  then
205             set_attr(hd, attr_icflag, PROCESSED_BEGIN_FLAG)
206             tex_set_attr('global', attr_icflag, 0)
207          end
208          return hd
209       else
210          local n =node_next(hd)
211          if gc=='vtop' and n then
212             local w = create_dir_whatsit(hd, gc, ltjs.list_dir)
213             -- move  dir whatsit after hd
214             setfield(hd, 'next', w); setfield(w, 'next', n)
215             return hd
216          else
217             return create_dir_whatsit(hd, gc, ltjs.list_dir)
218          end
219       end
220    end
221 end
222
223 -- dir_node に包む方法を書いたテーブル
224 local dir_node_aux
225 do
226    local get_h =function (w,h,d) return h end
227    local get_d =function (w,h,d) return d end
228    local get_h_d =function (w,h,d) return h+d end
229    local get_h_d_neg =function (w,h,d) return -h-d end
230    local get_h_neg =function (w,h,d) return -h end
231    local get_d_neg =function (w,h,d) return -d end
232    local get_w_half =function (w,h,d) return 0.5*w end
233    local get_w_neg_half =function (w,h,d) return -0.5*w end
234    local get_w_neg =function (w,h,d) return -w end
235    local get_w =function (w,h,d) return w end
236    local zero = function() return 0 end
237    dir_node_aux = {
238       [dir_yoko] = { -- yoko を 
239          [dir_tate] = { -- tate 中で組む
240             width  = get_h_d,
241             height = get_w_half,
242             depth  = get_w_half,
243             [id_hlist] = {
244                { 'whatsit', sid_save },
245                { 'rotate', '0 1 -1 0' },
246                { 'kern', get_w_neg_half },
247                { 'box' , get_h },
248                { 'kern', get_w_neg_half },
249                { 'whatsit', sid_restore },
250             },
251             [id_vlist] = {
252                { 'whatsit', sid_save },
253                { 'rotate', '0 1 -1 0' },
254                { 'kern' , zero },
255                { 'box' , get_w_neg },
256                { 'kern', get_h_d_neg},
257                { 'whatsit', sid_restore },
258             },
259          },
260          [dir_dtou] = { -- dtou 中で組む
261             width  = get_h_d,
262             height = get_w,
263             depth  = zero,
264             [id_hlist] = {
265                { 'whatsit', sid_save },
266                { 'rotate', '0 -1 1 0' },
267                { 'kern', get_w_neg },
268                { 'box', get_d_neg },
269                { 'whatsit', sid_restore },
270             },
271             [id_vlist] = {
272                { 'whatsit', sid_save },
273                { 'rotate', '0 -1 1 0' },
274                { 'kern', get_h_d_neg },
275                { 'box', zero },
276                { 'whatsit', sid_restore },
277             },
278          },
279       },
280       [dir_tate] = { -- tate を
281          [dir_yoko] = { -- yoko 中で組む
282             width  = get_h_d,
283             height = get_w,
284             depth  = zero,
285             [id_hlist] = {
286                { 'whatsit', sid_save },
287                { 'rotate', '0 -1 1 0' },
288                { 'kern', get_w_neg },
289                { 'box' , get_d_neg },
290                { 'whatsit', sid_restore },
291             },
292             [id_vlist] = {
293                { 'whatsit', sid_save },
294                { 'rotate', '0 -1 1 0' },
295                { 'kern', get_h_d_neg },
296                { 'box', zero },
297                { 'whatsit', sid_restore },
298             },
299          },
300          [dir_dtou] = { -- dtou 中で組む
301             width  = get_w,
302             height = get_d,
303             depth  = get_h,
304             [id_hlist] = {
305                { 'whatsit', sid_save },
306                { 'rotate', '-1 0 0 -1' },
307                { 'kern', get_w_neg },
308                { 'box', zero },
309                { 'whatsit', sid_restore },
310             },
311             [id_vlist] = {
312                { 'whatsit', sid_save },
313                { 'rotate', '-1 0 0 -1' },
314                { 'kern', get_h_d_neg },
315                { 'box', get_w_neg },
316                { 'whatsit', sid_restore },
317             },
318          },
319       },
320       [dir_dtou] = { -- dtou を
321          [dir_yoko] = { -- yoko 中で組む
322             width  = get_h_d,
323             height = get_w, 
324             depth  = zero,
325             [id_hlist] = {
326                { 'whatsit', sid_save },
327                { 'rotate', '0 1 -1 0' },
328                { 'kern', zero },
329                { 'box', get_h },
330                { 'kern', get_w_neg },
331                { 'whatsit', sid_restore },
332             },
333             [id_vlist] = {
334                { 'kern', zero },
335                { 'whatsit', sid_save },
336                { 'rotate', '0 1 -1 0' },
337                { 'box', get_w_neg },
338                { 'kern', get_h_d_neg },
339                { 'whatsit', sid_restore },
340             },
341          },
342          [dir_tate] = { -- tate 中で組む
343             width  = get_w,
344             height = get_d,
345             depth  = get_h,
346             [id_hlist] = {
347                { 'whatsit', sid_save },
348                { 'rotate', '-1 0 0 -1' },
349                { 'kern', get_w_neg },
350                { 'box', zero },
351                { 'whatsit', sid_restore },
352             },
353             [id_vlist] = {
354                { 'whatsit', sid_save },
355                { 'rotate', ' -1 0 0 -1' },
356                { 'kern', get_h_d_neg }, 
357                { 'box', get_w_neg },
358                { 'whatsit', sid_restore },
359             },
360          },
361       },
362    }
363 end
364
365 -- b に DIR whatsit があればその内容を attr_dir にうつす (1st ret val)
366 -- 2nd ret val はその DIR whatsit
367 local function get_box_dir(b, default)
368    start_time_measure('get_box_dir')
369    local dir = has_attr(b, attr_dir) or 0
370    local bh = getfield(b,'head') 
371    -- b は insert node となりうるので getlist() は使えない
372    local c
373    for i=1,2 do
374       if bh and getid(bh)==id_whatsit
375       and getsubtype(bh)==sid_user and getfield(bh, 'user_id')==DIR then
376          c = bh
377          if dir==0 then
378             dir = has_attr(bh, attr_dir)
379             set_attr(b, attr_dir, dir)
380             tex_set_attr('global', attr_dir, 0)
381          end
382       end
383       bh = node_next(bh)
384    end
385    stop_time_measure('get_box_dir')
386    return (dir==0 and default or dir), c
387 end
388
389 do
390    local getbox = tex.getbox
391    local function check_dir(reg_num)
392       start_time_measure('box_primitive_hook')
393       local list_dir = get_dir_count()
394       local b = tex.getbox(tex_getcount('ltj@tempcnta'))
395       if b then
396          local box_dir = get_box_dir(to_direct(b), dir_yoko)
397          if box_dir%dir_node_auto ~= list_dir%dir_node_auto then
398             ltjb.package_error(
399                'luatexja',
400                "Incompatible direction list can't be unboxed",
401                'I refuse to unbox a box in differrent direction.')
402          end
403       end
404       stop_time_measure('box_primitive_hook')
405    end
406    luatexja.direction.check_dir = check_dir
407 end
408
409 -- dir_node に包まれている「本来の中身」を取り出し,
410 -- dir_node を全部消去
411 local function unwrap_dir_node(b, head, box_dir)
412    -- b: dir_node, head: the head of list, box_dir: 
413    -- return values are (new head), (next of b), (contents), (dir of contents)
414    local bh = getlist(b)
415    local bc = node_next(node_next(node_next(bh)))
416    local nh, nb
417    node_remove(bh, bc); 
418    Dnode.flush_list(bh)
419    if head then
420       nh = insert_before(head, b, bc)
421       nh, nb = node_remove(nh, b)
422       setfield(b, 'next', nil)
423       setfield(b, 'head', nil)
424       node_free(b)
425    end
426    local shift_old, b_dir, wh = nil, get_box_dir(bc, 0)
427    if wh then
428       Dnode.flush_list(getfield(wh, 'value'))
429       setfield(wh, 'value', nil)
430    end
431    -- recalc. info
432    local info = dir_node_aux[b_dir][box_dir%dir_node_auto][getid(bc)]
433    for _,v in ipairs(info) do 
434       if v[1]=='box' then
435          shift_old = v[2](
436             getfield(bc,'width'), getfield(bc, 'height'), getfield(bc, 'depth'))
437          break
438       end
439    end
440    setfield(bc, 'shift', getfield(bc, 'shift') - shift_old)
441    return nh, nb, bc, b_dir
442 end
443
444 -- is_manual: 寸法変更に伴うものか?
445 local function create_dir_node(b, b_dir, new_dir, is_manual)
446    local info = dir_node_aux[b_dir][new_dir]
447    local w = getfield(b, 'width')
448    local h = getfield(b, 'height')
449    local d = getfield(b, 'depth')
450    local db = node_new(getid(b))
451    set_attr(db, attr_dir, 
452             new_dir + (is_manual and dir_node_manual or dir_node_auto))
453    tex_set_attr('global', attr_dir, 0)
454    set_attr(db, attr_icflag, PROCESSED)
455    set_attr(b, attr_icflag, PROCESSED)
456    setfield(db, 'dir', getfield(b, 'dir'))
457    setfield(db, 'shift', 0)
458    setfield(db, 'width',  info.width(w,h,d))
459    setfield(db, 'height', info.height(w,h,d))
460    setfield(db, 'depth',  info.depth(w,h,d))
461    return db
462 end
463
464 -- 異方向のボックスの処理
465 local make_dir_whatsit, process_dir_node
466 do
467    make_dir_whatsit = function (head, b, new_dir, origin)
468       -- head: list head, b: box
469       -- origin: コール元 (for debug)
470       -- return value: (new head), (next of b), (new b), (is_b_dir_node)
471       -- (new b): b か dir_node に被せられた b
472       local bh = getlist(b)
473       local box_dir, dn =  get_box_dir(b, ltjs.list_dir)
474       -- 既に b の中身にあるwhatsit
475       if box_dir==new_dir then
476          return head, node_next(b), b, false
477       elseif  box_dir%dir_node_auto == new_dir  then
478          local bc = node_next(node_next(node_next(bh)))
479          local _, dnc = get_box_dir(b, 0)
480          if dnc then -- free all other dir_node
481             Dnode.flush_list(getfield(dnc, 'value'))
482             setfield(dnc, 'value', nil)
483          end
484          set_attr(b, attr_dir, box_dir%dir_node_auto + dir_node_auto)
485          return head, node_next(b), b, true
486       else
487          local nh, nb, ret, flag
488          if box_dir>= dir_node_auto then -- unwrap
489             local b_dir
490             head, nb, b, b_dir = unwrap_dir_node(b, head, box_dir)
491             bh = getlist(b)
492             if b_dir==new_dir then -- no need to create new dir_node
493                return head, nb, b, false 
494             else box_dir = b_dir end
495          end
496             local db
497             local dnh = getfield(dn, 'value')
498             for x in traverse(dnh) do
499                if has_attr(x, attr_dir)%dir_node_auto == new_dir then
500                   setfield(dn, 'value', to_node(node_remove(dnh, x)))
501                   db=x; break
502                end
503             end
504             Dnode.flush_list(dnh)
505             db = db or create_dir_node(b, box_dir, new_dir, false)
506             local w = getfield(b, 'width')
507             local h = getfield(b, 'height')
508             local d = getfield(b, 'depth')
509             nh, nb =  insert_before(head, b, db), nil
510             nh, nb = node_remove(nh, b)
511             local db_head, db_tail  = nil
512             for _,v in ipairs(dir_node_aux[box_dir][new_dir][getid(b)]) do
513                local cmd, arg, nn = v[1], v[2]
514                if cmd=='kern' then
515                   nn = node_new(id_kern)
516                   setfield(nn, 'kern', arg(w, h, d))
517                elseif cmd=='whatsit' then
518                   nn = node_new(id_whatsit, arg)
519                elseif cmd=='rotate' then
520                   nn = node_new(id_whatsit, sid_matrix)
521                   setfield(nn, 'data', arg)
522                elseif cmd=='box' then
523                   nn = b; setfield(b, 'next', nil)
524                   setfield(nn, 'shift', getfield(nn, 'shift') + arg(w,h,d))
525                end
526                if db_head then
527                   insert_after(db_head, db_tail, nn)
528                   db_tail = nn
529                else
530                   db_head, db_tail = nn, nn
531                end
532                setfield(db, 'head', db_head)
533                ret, flag = db, true
534          end
535          return nh, nb, ret, flag
536       end
537    end
538    process_dir_node = function (hd, gc)
539       local x, new_dir = hd, ltjs.list_dir or dir_yoko
540       while x do
541          local xid = getid(x)
542          if (xid==id_hlist and has_attr(x, attr_icflag)%PROCESSED_BEGIN_FLAG~=PACKED) 
543          or xid==id_vlist then
544             hd, x = make_dir_whatsit(hd, x, new_dir, 'process_dir_node:' .. gc)
545          else
546             x = node_next(x)
547          end
548       end
549       return hd
550    end
551
552    -- lastbox
553    local node_prev = (Dnode~=node) and Dnode.getprev or node.prev
554    local function lastbox_hook()
555       start_time_measure('box_primitive_hook')
556       local bn = tex_nest[tex_nest.ptr].tail
557       if bn then
558          local b, head = to_direct(bn), to_direct(tex_nest[tex_nest.ptr].head)
559          local bid = getid(b)
560          if bid==id_hlist or bid==id_vlist then
561             local box_dir =  get_box_dir(b, 0)
562             if box_dir>= dir_node_auto then -- unwrap
563                local p = node_prev(b)
564                local dummy1, dummy2, nb = unwrap_dir_node(b, nil, box_dir)
565                setfield(p, 'next', nb);  tex_nest[tex_nest.ptr].tail = to_node(nb)
566                setfield(b, 'next', nil); setfield(b, 'head', nil)
567                node_free(b)
568             end
569          end
570       end
571       stop_time_measure('box_primitive_hook')
572    end
573
574    luatexja.direction.make_dir_whatsit = make_dir_whatsit
575    luatexja.direction.lastbox_hook = lastbox_hook
576 end
577
578 -- \wd, \ht, \dp の代わり
579 do
580    local getbox, setdimen = tex.getbox, tex.setdimen
581    local function get_box_dim_common(key, s, l_dir)
582       local s_dir, wh = get_box_dir(s, dir_yoko)
583       if s_dir ~= l_dir then
584          local not_found = true
585          for x in traverse(getfield(wh, 'value')) do
586             if l_dir == has_attr(x, attr_dir)%dir_node_auto then
587                setdimen('ltj@tempdima', getfield(x, key))
588                not_found = false; break
589             end
590          end
591          if not_found then
592             local w = getfield(s, 'width')
593             local h = getfield(s, 'height')
594             local d = getfield(s, 'depth')
595             setdimen('ltj@tempdima', 
596                          dir_node_aux[s_dir][l_dir][key](w,h,d))
597          end
598       else
599          setdimen('ltj@tempdima', getfield(s, key))
600       end
601    end
602    local function get_box_dim(key, n)
603       local gt = tex.globaldefs; tex.globaldefs = 0
604       local s = getbox(n)
605       if s then
606          local l_dir = get_dir_count()
607          s = to_direct(s)
608          local b_dir = has_attr(s, attr_dir) or 0
609          if b_dir<dir_node_auto then
610             get_box_dim_common(key, s, l_dir)
611          elseif b_dir%dir_node_auto==l_dir then
612             setdimen('ltj@tempdima', getfield(s, key))
613          else
614             get_box_dim_common(key, 
615                                node_next(node_next(node_next(getlist(s)))), l_dir)
616          end
617       else
618          setdimen('ltj@tempdima', 0)
619       end
620       tex.globaldefs = gt
621    end
622    luatexja.direction.get_box_dim = get_box_dim
623
624    -- return value: (changed dimen of box itself?)
625    local function set_box_dim_common(key, s, l_dir)
626       local s_dir, wh = get_box_dir(s, dir_yoko)
627       if s_dir ~= l_dir then
628          if not wh then
629             wh = create_dir_whatsit(getlist(s), 'set_box_dim', s_dir)
630             setfield(s, 'head', wh)
631          end
632          local db
633          local dnh = getfield(wh, 'value')
634          for x in traverse(dnh) do
635             if has_attr(x, attr_dir)%dir_node_auto==l_dir then
636                db = x; break
637             end
638          end
639          if not db then
640             db = create_dir_node(s, s_dir, l_dir, true)
641             setfield(db, 'next', dnh)
642             setfield(wh, 'value',to_node(db))
643          end
644          setfield(db, key, tex.getdimen('ltj@tempdima'))
645          return false
646       else
647          setfield(s, key, tex.getdimen('ltj@tempdima'))
648          if wh then
649             -- change dimension of dir_nodes which are created "automatically"
650                local bw, bh, bd 
651                   = getfield(s,'width'), getfield(s, 'height'), getfield(s, 'depth')
652             for x in traverse(getfield(wh, 'value')) do
653                local x_dir = has_attr(x, attr_dir)
654                if x_dir<dir_node_manual then
655                   local info = dir_node_aux[s_dir][x_dir%dir_node_auto]
656                   setfield(x, 'width',  info.width(bw,bh,bd))
657                   setfield(x, 'height', info.height(bw,bh,bd))
658                   setfield(x, 'depth',  info.depth(bw,bh,bd))
659                end
660             end
661          end
662          return true
663       end
664    end
665    local function set_box_dim(key)
666       local n = tex_getcount('ltj@tempcnta')
667       local s = getbox(n)
668       if s then
669          local l_dir = get_dir_count()
670          s = to_direct(s)
671          local b_dir = has_attr(s, attr_dir) or 0
672          if b_dir<dir_node_auto then
673             set_box_dim_common(key, s, l_dir)
674          elseif b_dir%dir_node_auto == l_dir then
675             setfield(s, key, tex.getdimen('ltj@tempdima'))
676             if b_dir<dir_node_manual then
677                set_attr(s, attr_dir, b_dir%dir_node_auto + dir_node_manual)
678             end
679          else
680             local sid, sl = getid(s), getlist(s)
681             local b = node_next(node_next(node_next(sl)))
682             local info = dir_node_aux[get_box_dir(b)][b_dir%dir_node_auto]
683             local shift_old
684             for _,v in ipairs(info[sid]) do 
685                if v[1]=='box' then
686                   shift_old = v[2](
687                      getfield(b,'width'), getfield(b, 'height'), getfield(b, 'depth'))
688                   break
689                end
690             end
691            if set_box_dim_common(key, b, l_dir) then
692                local bw, bh, bd 
693                   = getfield(b,'width'), getfield(b, 'height'), getfield(b, 'depth')
694                -- re-calculate shift
695                for i,v in ipairs(info[sid]) do 
696                   if getid(sl)==id_kern then
697                      setfield(sl, 'kern', v[2](bw,bh,bd) )
698                   elseif getid(sl)==sid then
699                      local d = getfield(sl, 'shift')
700                      setfield(sl, 'shift', 
701                               getfield(sl, 'shift') - shift_old + v[2](bw,bh,bd) )
702                   end
703                   sl = node_next(sl)
704                end
705                -- re-calculate dimension of s, if s is created "automatically"
706                if b_dir<dir_node_manual then
707                   setfield(s, 'width',  info.width(bw,bh,bd))
708                   setfield(s, 'height', info.height(bw,bh,bd))
709                   setfield(s, 'depth',  info.depth(bw,bh,bd))
710                end
711             end
712          end
713       end
714    end
715    luatexja.direction.set_box_dim = set_box_dim
716 end
717
718 -- \ifydir, \iftdir, \ifddir
719 do
720    local getbox = tex.getbox
721    local function dir_conditional(n, mode)
722       local s = getbox(n)
723       local res = false
724       if s then
725          s = to_direct(s)
726          local b_dir = get_box_dir(s, dir_yoko)
727          if b_dir<dir_node_auto then
728             res = (b_dir==mode)
729          else
730             local b_dir = get_box_dir(
731                node_next(node_next(node_next(getlist(s)))), dir_yoko)
732             res = (b_dir==mode)
733          end
734       end
735       tex.sprint(cat_lp, '\\if' .. tostring(res))
736    end
737    luatexja.direction.dir_conditional = dir_conditional
738 end
739
740 -- 縦書き用字形への変換テーブル
741 local font_vert_table = {} -- key: fontnumber
742 do
743    local font_vert_basename = {} -- key: basename
744    local function add_feature_table(tname, src, dest)
745       for i,v in pairs(src) do
746          if type(v.slookups)=='table' then
747             local s = v.slookups[tname]
748             if s and not dest[i] then
749                dest[i] = s
750             end
751          end
752       end
753    end
754
755    local function prepare_vert_data(n, id)
756       -- test if already loaded
757       if type(id)=='number' then -- sometimes id is an integer
758          font_vert_table[n] = font_vert_table[id]; return
759       elseif (not id) or font_vert_table[n]  then return
760       end
761       local fname = id.filename
762       local bname = file.basename(fname)
763       if not fname then
764          font_vert_table[n] = {}; return
765       elseif font_vert_basename[bname] then
766          font_vert_table[n] = font_vert_basename[bname]; return
767       end
768       local vtable = {}
769       local a = id.resources.sequences
770       if a then
771          local s = id.shared.rawdata.descriptions
772          for i,v in pairs(a) do
773             if v.features.vert or v.features.vrt2 then
774                add_feature_table(v.subtables[1], s, vtable)
775             end
776          end
777       end
778       font_vert_basename[bname] = vtable
779       font_vert_table[n] = vtable
780    end
781    -- 縦書き用字形への変換
782    function luatexja.direction.get_vert_glyph(n, chr)
783       local fn = font_vert_table[n]
784       return fn and fn[chr] or chr
785    end
786    luatexbase.add_to_callback('luatexja.define_font',
787                               function (res, name, size, id)
788                                  prepare_vert_data(id, res)
789                               end,
790                               'prepare_vert_data', 1)
791
792    local function a (n, dat) font_vert_table[n] = dat end
793    luatexja.rmlgbm.vert_addfunc = a
794
795 end
796
797 -- PACKED の hbox から文字を取り出す
798 -- luatexja.jfmglue.check_box などで使用
799 do
800    local function glyph_from_packed(h)
801       local b = getlist(h)
802       return (getid(b)==id_kern) 
803          and node_next(node_next(node_next(node_next(b)))) or b
804    end
805    luatexja.direction.glyph_from_packed = glyph_from_packed
806 end
807
808 -- adjust and insertion
809 local id_adjust = node.id('adjust')
810 function luatexja.direction.check_adjust_direction()
811    start_time_measure('box_primitive_hook')
812    local list_dir = tex_getcount('ltj@adjdir@count')
813    local a = tex_nest[tex_nest.ptr].tail
814    local ad = to_direct(a)
815    if a and getid(ad)==id_adjust then
816       local adj_dir = get_box_dir(ad)
817       if list_dir~=adj_dir then
818          ltjb.package_error(
819                  'luatexja',
820                  'Direction Incompatible',
821                  "\\vadjust's argument and outer vlist must have same direction.")
822          Dnode.last_node()
823       end
824    end
825    stop_time_measure('box_primitive_hook')
826 end
827
828 -- vsplit
829 do
830    local split_dir_whatsit
831    local function dir_adjust_vpack(h, gc)
832       start_time_measure('direction_vpack')
833       local hd = to_direct(h)
834       if gc=='split_keep' then
835          -- supply dir_whatsit
836          hd = create_dir_whatsit_vbox(hd, gc)
837          split_dir_whatsit = hd
838       elseif gc=='split_off'  then
839          local bh=hd
840          for i=1,2 do
841             if bh and getid(bh)==id_whatsit
842             and getsubtype(bh)==sid_user and getfield(bh, 'user_id')==DIR then
843                ltjs.list_dir  = has_attr(bh, attr_dir); break
844             end
845             bh = node_next(bh)
846          end
847          if split_dir_whatsit then
848             -- adjust direction of 'split_keep'
849             set_attr(split_dir_whatsit, attr_dir, ltjs.list_dir)
850             tex_set_attr('global', attr_dir, 0)
851          end
852          split_dir_whatsit=nil
853       else
854          adjust_badness(hd)
855          hd = process_dir_node(create_dir_whatsit_vbox(hd, gc), gc)
856          split_dir_whatsit=nil
857       end
858       stop_time_measure('direction_vpack')
859       return to_node(hd)
860    end
861    luatexbase.add_to_callback('vpack_filter',
862                               dir_adjust_vpack,
863                               'ltj.direction', 10000)
864 end
865
866 do
867    -- supply direction whatsit to the main vertical list "of the next page"
868    local function dir_adjust_pre_output(h, gc)
869       return to_node(create_dir_whatsit_vbox(to_direct(h), gc))
870    end
871    luatexbase.add_to_callback('pre_output_filter',
872                               dir_adjust_pre_output,
873                               'ltj.direction', 10000)
874
875    function luatexja.direction.remove_end_whatsit()
876       local h=tex.lists.page_head
877       if (not h.next) and
878          h.id==id_whatsit and h.subtype==sid_user and
879          h.user_id == DIR then
880             tex.lists.page_head = nil
881             node.free(h)
882       end
883    end
884 end
885
886 -- 
887 do
888    local function dir_adjust_buildpage(info)
889       if info=='box' then
890          local head = to_direct(tex.lists.contrib_head)
891          local nb
892          head, _, nb
893             = make_dir_whatsit(head, 
894                                node_tail(head),
895                                get_dir_count(), 
896                                'buildpage')
897          tex.lists.contrib_head = to_node(head)
898       end
899    end
900    luatexbase.add_to_callback('buildpage_filter',
901                               dir_adjust_buildpage,
902                               'ltj.direction', 10000)
903 end