OSDN Git Service

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