OSDN Git Service

Add buildpage_callback to fix ticket #33939
[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             --print('unbox', reg_num, box_dir, list_dir)
371             ltjb.package_error(
372                'luatexja',
373                "Incompatible direction list can't be unboxed",
374                'I refuse to unbox a box in differrent direction.')
375          end
376       end
377       stop_time_measure('box_primitive_hook')
378    end
379    luatexja.direction.check_dir = check_dir
380 end
381
382 -- dir_node に包まれている「本来の中身」を取り出し,
383 -- dir_node を全部消去
384 local function unwrap_dir_node(b, head, box_dir)
385    -- b: dir_node, head: the head of list, box_dir: 
386    -- return values are (new head), (next of b), (contents), (dir of contents)
387    local bh = getlist(b)
388    local bc = node_next(node_next(node_next(bh)))
389    local nh, nb
390    node_remove(bh, bc); 
391    Dnode.flush_list(bh)
392    if head then
393       nh = insert_before(head, b, bc)
394       nh, nb = node_remove(nh, b)
395       setfield(b, 'next', nil)
396       setfield(b, 'head', nil)
397       node_free(b)
398    end
399    local shift_old, b_dir, wh = nil, get_box_dir(bc, 0)
400    if wh then
401       Dnode.flush_list(getfield(wh, 'value'))
402       setfield(wh, 'value', nil)
403    end
404    -- recalc. info
405    local info = dir_node_aux[b_dir][box_dir%dir_node_auto][getid(bc)]
406    for _,v in ipairs(info) do 
407       if v[1]=='box' then
408          shift_old = v[2](
409             getfield(bc,'width'), getfield(bc, 'height'), getfield(bc, 'depth'))
410          break
411       end
412    end
413    setfield(bc, 'shift', getfield(bc, 'shift') - shift_old)
414    return nh, nb, bc, b_dir
415 end
416
417 -- is_manual: 寸法変更に伴うものか?
418 local function create_dir_node(b, b_dir, new_dir, is_manual)
419    local info = dir_node_aux[b_dir][new_dir]
420    local w = getfield(b, 'width')
421    local h = getfield(b, 'height')
422    local d = getfield(b, 'depth')
423    local db = node_new(getid(b))
424    set_attr(db, attr_dir, 
425             new_dir + (is_manual and dir_node_manual or dir_node_auto))
426    tex_set_attr('global', attr_dir, 0)
427    set_attr(db, attr_icflag, PROCESSED)
428    set_attr(b, attr_icflag, PROCESSED)
429    setfield(db, 'dir', getfield(b, 'dir'))
430    setfield(db, 'shift', 0)
431    setfield(db, 'width',  info.width(w,h,d))
432    setfield(db, 'height', info.height(w,h,d))
433    setfield(db, 'depth',  info.depth(w,h,d))
434    return db
435 end
436
437 -- 異方向のボックスの処理
438 local make_dir_whatsit, process_dir_node
439 do
440    make_dir_whatsit = function (head, b, new_dir, origin)
441       -- head: list head, b: box
442       -- origin: コール元 (for debug)
443       -- return value: (new head), (next of b), (new b), (is_b_dir_node)
444       -- (new b): b か dir_node に被せられた b
445       local bh = getlist(b)
446       local box_dir, dn =  get_box_dir(b, ltjs.list_dir)
447       -- 既に b の中身にあるwhatsit
448       if box_dir==new_dir then
449          return head, node_next(b), b, false
450       elseif  box_dir%dir_node_auto == new_dir  then
451          local bc = node_next(node_next(node_next(bh)))
452          local _, dnc = get_box_dir(b, 0)
453          if dnc then -- free all other dir_node
454             Dnode.flush_list(getfield(dnc, 'value'))
455             setfield(dnc, 'value', nil)
456          end
457          set_attr(b, attr_dir, box_dir%dir_node_auto + dir_node_auto)
458          return head, node_next(b), b, true
459       else
460          local nh, nb, ret, flag
461          if box_dir>= dir_node_auto then -- unwrap
462             local b_dir
463             head, nb, b, b_dir = unwrap_dir_node(b, head, box_dir)
464             bh = getlist(b)
465             if b_dir==new_dir then -- no need to create new dir_node
466                return head, nb, b, false 
467             else box_dir = b_dir end
468          end
469             local db
470             local dnh = getfield(dn, 'value')
471             for x in traverse(dnh) do
472                if has_attr(x, attr_dir)%dir_node_auto == new_dir then
473                   setfield(dn, 'value', to_node(node_remove(dnh, x)))
474                   db=x; break
475                end
476             end
477             Dnode.flush_list(dnh)
478             db = db or create_dir_node(b, box_dir, new_dir, false)
479             local w = getfield(b, 'width')
480             local h = getfield(b, 'height')
481             local d = getfield(b, 'depth')
482             nh, nb =  insert_before(head, b, db), nil
483             nh, nb = node_remove(nh, b)
484             local db_head, db_tail  = nil
485             for _,v in ipairs(dir_node_aux[box_dir][new_dir][getid(b)]) do
486                local cmd, arg, nn = v[1], v[2]
487                if cmd=='kern' then
488                   nn = node_new(id_kern)
489                   setfield(nn, 'kern', arg(w, h, d))
490                elseif cmd=='whatsit' then
491                   nn = node_new(id_whatsit, arg)
492                elseif cmd=='rotate' then
493                   nn = node_new(id_whatsit, sid_matrix)
494                   setfield(nn, 'data', arg)
495                elseif cmd=='box' then
496                   nn = b; setfield(b, 'next', nil)
497                   setfield(nn, 'shift', getfield(nn, 'shift') + arg(w,h,d))
498                end
499                if db_head then
500                   insert_after(db_head, db_tail, nn)
501                   db_tail = nn
502                else
503                   db_head, db_tail = nn, nn
504                end
505                setfield(db, 'head', db_head)
506                ret, flag = db, true
507          end
508          return nh, nb, ret, flag
509       end
510    end
511    process_dir_node = function (hd, gc)
512       local x, new_dir = hd, ltjs.list_dir or dir_yoko
513       while x do
514          local xid = getid(x)
515          if (xid==id_hlist and has_attr(x, attr_icflag)%PROCESSED_BEGIN_FLAG~=PACKED) 
516          or xid==id_vlist then
517             hd, x = make_dir_whatsit(hd, x, new_dir, 'process_dir_node:' .. gc)
518          else
519             x = node_next(x)
520          end
521       end
522       return hd
523    end
524
525    -- lastbox
526    local node_prev = (Dnode~=node) and Dnode.getprev or node.prev
527    local function lastbox_hook()
528       start_time_measure('box_primitive_hook')
529       local bn = tex_nest[tex_nest.ptr].tail
530       if bn then
531          local b, head = to_direct(bn), to_direct(tex_nest[tex_nest.ptr].head)
532          local bid = getid(b)
533          if bid==id_hlist or bid==id_vlist then
534             local box_dir =  get_box_dir(b, 0)
535             if box_dir>= dir_node_auto then -- unwrap
536                local p = node_prev(b)
537                local dummy1, dummy2, nb = unwrap_dir_node(b, nil, box_dir)
538                setfield(p, 'next', nb);  tex_nest[tex_nest.ptr].tail = to_node(nb)
539                setfield(b, 'next', nil); setfield(b, 'head', nil)
540                node_free(b)
541             end
542          end
543       end
544       stop_time_measure('box_primitive_hook')
545    end
546
547    luatexja.direction.make_dir_whatsit = make_dir_whatsit
548    luatexja.direction.lastbox_hook = lastbox_hook
549 end
550
551 -- \wd, \ht, \dp の代わり
552 do
553    local getbox, setdimen = tex.getbox, tex.setdimen
554    local function get_box_dim_common(key, s, l_dir)
555       local s_dir, wh = get_box_dir(s, dir_yoko)
556       if s_dir ~= l_dir then
557          local not_found = true
558          for x in traverse(getfield(wh, 'value')) do
559             if l_dir == has_attr(x, attr_dir)%dir_node_auto then
560                setdimen('ltj@tempdima', getfield(x, key))
561                not_found = false; break
562             end
563          end
564          if not_found then
565             local w = getfield(s, 'width')
566             local h = getfield(s, 'height')
567             local d = getfield(s, 'depth')
568             setdimen('ltj@tempdima', 
569                          dir_node_aux[s_dir][l_dir][key](w,h,d))
570          end
571       else
572          setdimen('ltj@tempdima', getfield(s, key))
573       end
574    end
575    local function get_box_dim(key, n)
576       local gt = tex.globaldefs; tex.globaldefs = 0
577       local s = getbox(n)
578       if s then
579          local l_dir = get_dir_count()
580          s = to_direct(s)
581          local b_dir = has_attr(s, attr_dir) or 0
582          if b_dir<dir_node_auto then
583             get_box_dim_common(key, s, l_dir)
584          elseif b_dir%dir_node_auto==l_dir then
585             setdimen('ltj@tempdima', getfield(s, key))
586          else
587             get_box_dim_common(key, 
588                                node_next(node_next(node_next(getlist(s)))), l_dir)
589          end
590       else
591          setdimen('ltj@tempdima', 0)
592       end
593       tex.globaldefs = gt
594    end
595    luatexja.direction.get_box_dim = get_box_dim
596
597    -- return value: (changed dimen of box itself?)
598    local function set_box_dim_common(key, s, l_dir)
599       local s_dir, wh = get_box_dir(s, dir_yoko)
600       if s_dir ~= l_dir then
601          if not wh then
602             wh = create_dir_whatsit(getlist(s), 'set_box_dim', s_dir)
603             setfield(s, 'head', wh)
604          end
605          local db
606          local dnh = getfield(wh, 'value')
607          for x in traverse(dnh) do
608             if has_attr(x, attr_dir)%dir_node_auto==l_dir then
609                db = x; break
610             end
611          end
612          if not db then
613             db = create_dir_node(s, s_dir, l_dir, true)
614             setfield(db, 'next', dnh)
615             setfield(wh, 'value',to_node(db))
616          end
617          setfield(db, key, tex.getdimen('ltj@tempdima'))
618          return false
619       else
620          setfield(s, key, tex.getdimen('ltj@tempdima'))
621          if wh then
622             -- change dimension of dir_nodes which are created "automatically"
623                local bw, bh, bd 
624                   = getfield(s,'width'), getfield(s, 'height'), getfield(s, 'depth')
625             for x in traverse(getfield(wh, 'value')) do
626                local x_dir = has_attr(x, attr_dir)
627                if x_dir<dir_node_manual then
628                   local info = dir_node_aux[s_dir][x_dir%dir_node_auto]
629                   setfield(x, 'width',  info.width(bw,bh,bd))
630                   setfield(x, 'height', info.height(bw,bh,bd))
631                   setfield(x, 'depth',  info.depth(bw,bh,bd))
632                end
633             end
634          end
635          return true
636       end
637    end
638    local function set_box_dim(key)
639       local n = tex_getcount('ltj@tempcnta')
640       local s = getbox(n)
641       if s then
642          local l_dir = get_dir_count()
643          s = to_direct(s)
644          local b_dir = has_attr(s, attr_dir) or 0
645          if b_dir<dir_node_auto then
646             set_box_dim_common(key, s, l_dir)
647          elseif b_dir%dir_node_auto == l_dir then
648             setfield(s, key, tex.getdimen('ltj@tempdima'))
649             if b_dir<dir_node_manual then
650                set_attr(s, attr_dir, b_dir%dir_node_auto + dir_node_manual)
651             end
652          else
653             local sid, sl = getid(s), getlist(s)
654             local b = node_next(node_next(node_next(sl)))
655             local info = dir_node_aux[get_box_dir(b)][b_dir%dir_node_auto]
656             local shift_old
657             for _,v in ipairs(info[sid]) do 
658                if v[1]=='box' then
659                   shift_old = v[2](
660                      getfield(b,'width'), getfield(b, 'height'), getfield(b, 'depth'))
661                   break
662                end
663             end
664            if set_box_dim_common(key, b, l_dir) then
665                local bw, bh, bd 
666                   = getfield(b,'width'), getfield(b, 'height'), getfield(b, 'depth')
667                -- re-calculate shift
668                for i,v in ipairs(info[sid]) do 
669                   if getid(sl)==id_kern then
670                      setfield(sl, 'kern', v[2](bw,bh,bd) )
671                   elseif getid(sl)==sid then
672                      local d = getfield(sl, 'shift')
673                      setfield(sl, 'shift', 
674                               getfield(sl, 'shift') - shift_old + v[2](bw,bh,bd) )
675                   end
676                   sl = node_next(sl)
677                end
678                -- re-calculate dimension of s, if s is created "automatically"
679                if b_dir<dir_node_manual then
680                   setfield(s, 'width',  info.width(bw,bh,bd))
681                   setfield(s, 'height', info.height(bw,bh,bd))
682                   setfield(s, 'depth',  info.depth(bw,bh,bd))
683                end
684             end
685          end
686       end
687    end
688    luatexja.direction.set_box_dim = set_box_dim
689 end
690
691 -- \ifydir, \iftdir, \ifddir
692 do
693    local cat_lp = luatexbase.catcodetables['latex-package']
694    local getbox = tex.getbox
695    local function dir_conditional(n, mode)
696       local s = getbox(n)
697       local res = false
698       if s then
699          s = to_direct(s)
700          local b_dir = get_box_dir(s, dir_yoko)
701          if b_dir<dir_node_auto then
702             res = (b_dir==mode)
703          else
704             local b_dir = get_box_dir(
705                node_next(node_next(node_next(getlist(s)))), dir_yoko)
706             res = (b_dir==mode)
707          end
708       end
709       tex.sprint(cat_lp, '\\if' .. tostring(res))
710    end
711    luatexja.direction.dir_conditional = dir_conditional
712 end
713
714 -- 縦書き用字形への変換テーブル
715 local font_vert_table = {} -- key: fontnumber
716 do
717    local font_vert_basename = {} -- key: basename
718    local function add_feature_table(tname, src, dest)
719       for i,v in pairs(src) do
720          if type(v.slookups)=='table' then
721             local s = v.slookups[tname]
722             if s and not dest[i] then
723                dest[i] = s
724             end
725          end
726       end
727    end
728
729    local function prepare_vert_data(n, id)
730       -- test if already loaded
731       if type(id)=='number' then -- sometimes id is an integer
732          font_vert_table[n] = font_vert_table[id]; return
733       elseif (not id) or font_vert_table[n]  then return
734       end
735       local fname = id.filename
736       local bname = file.basename(fname)
737       if not fname then
738          font_vert_table[n] = {}; return
739       elseif font_vert_basename[bname] then
740          font_vert_table[n] = font_vert_basename[bname]; return
741       end
742       local vtable = {}
743       local a = id.resources.sequences
744       if a then
745          local s = id.shared.rawdata.descriptions
746          for i,v in pairs(a) do
747             if v.features.vert or v.features.vrt2 then
748                add_feature_table(v.subtables[1], s, vtable)
749             end
750          end
751       end
752       font_vert_basename[bname] = vtable
753       font_vert_table[n] = vtable
754    end
755    -- 縦書き用字形への変換
756    function luatexja.direction.get_vert_glyph(n, chr)
757       local fn = font_vert_table[n]
758       return fn and fn[chr] or chr
759    end
760    luatexbase.add_to_callback('luatexja.define_font',
761                               function (res, name, size, id)
762                                  prepare_vert_data(id, res)
763                               end,
764                               'prepare_vert_data', 1)
765
766    local function a (n, dat) font_vert_table[n] = dat end
767    luatexja.rmlgbm.vert_addfunc = a
768
769 end
770
771 -- PACKED の hbox から文字を取り出す
772 -- luatexja.jfmglue.check_box などで使用
773 do
774    local function glyph_from_packed(h)
775       local b = getlist(h)
776       return (getid(b)==id_kern) 
777          and node_next(node_next(node_next(node_next(b)))) or b
778    end
779    luatexja.direction.glyph_from_packed = glyph_from_packed
780 end
781
782 -- adjust and insertion
783 local id_adjust = node.id('adjust')
784 function luatexja.direction.check_adjust_direction()
785    start_time_measure('box_primitive_hook')
786    local list_dir = tex_getcount('ltj@adjdir@count')
787    local a = tex_nest[tex_nest.ptr].tail
788    local ad = to_direct(a)
789    if a and getid(ad)==id_adjust then
790       local adj_dir = get_box_dir(ad)
791       if list_dir~=adj_dir then
792          ltjb.package_error(
793                  'luatexja',
794                  'Direction Incompatible',
795                  "\\vadjust's argument and outer vlist must have same direction.")
796          Dnode.last_node()
797       end
798    end
799    stop_time_measure('box_primitive_hook')
800 end
801
802 -- setbox
803 local id_adjust = node.id('adjust')
804 function luatexja.direction.hook_empty_box()
805    start_time_measure('box_primitive_hook')
806    local reg_num =  tex_getcount('ltj@tempcnta')
807    local list_dir = get_dir_count()
808    local h = tex.getbox(reg_num)
809    if h then
810       if not h.head then
811          h.head = to_node(create_dir_whatsit(nil, nil, get_dir_count()))
812          luatexja.ext_show_node(h, '> ', print)
813       end
814    end
815    stop_time_measure('box_primitive_hook')
816 end
817
818 -- vsplit
819 do
820    local split_dir_whatsit
821    local function dir_adjust_vpack(h, gc)
822       start_time_measure('direction_vpack')
823       local hd = to_direct(h)
824       if gc=='split_keep' then
825          -- supply dir_whatsit
826          hd = create_dir_whatsit_vbox(hd, gc)
827          split_dir_whatsit = hd
828       elseif gc=='split_off'  then
829          local bh=hd
830          for i=1,2 do
831             if bh and getid(bh)==id_whatsit
832             and getsubtype(bh)==sid_user and getfield(bh, 'user_id')==DIR then
833                ltjs.list_dir  = has_attr(bh, attr_dir); break
834             end
835             bh = node_next(bh)
836          end
837          if split_dir_whatsit then
838             -- adjust direction of 'split_keep'
839             set_attr(split_dir_whatsit, attr_dir, ltjs.list_dir)
840             tex_set_attr('global', attr_dir, 0)
841          end
842          split_dir_whatsit=nil
843       else
844          hd = process_dir_node(create_dir_whatsit_vbox(hd, gc), gc)
845          split_dir_whatsit=nil
846       end
847       stop_time_measure('direction_vpack')
848       return to_node(hd)
849    end
850    luatexbase.add_to_callback('vpack_filter',
851                               dir_adjust_vpack,
852                               'ltj.direction', 10000)
853 end
854
855 -- supply direction whatsit to the main vertical list "of the next page"
856 do
857    local function dir_adjust_pre_output(h, gc)
858       return to_node(create_dir_whatsit_vbox(to_direct(h), gc))
859    end
860    luatexbase.add_to_callback('pre_output_filter',
861                               dir_adjust_pre_output,
862                               'ltj.direction', 10000)
863 end
864
865 -- 
866 do
867    local function dir_adjust_buildpage(info)
868       if info=='box' then
869          local head = to_direct(tex.lists.contrib_head)
870          local nb
871          head, _, nb
872             = make_dir_whatsit(head, 
873                                node_tail(head),
874                                get_dir_count(), 
875                                'buildpage')
876          tex.lists.contrib_head = to_node(head)
877       end
878    end
879    luatexbase.add_to_callback('buildpage_filter',
880                               dir_adjust_buildpage,
881                               'ltj.direction', 10000)
882 end