OSDN Git Service

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