OSDN Git Service

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