OSDN Git Service

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