2 -- src/ltj-direction.lua
5 luatexja.load_module('base'); local ltjb = luatexja.base
6 luatexja.load_module('stack'); local ltjs = luatexja.stack
7 luatexja.direction = {}
9 local attr_dir = luatexbase.attributes['ltj@dir']
10 local attr_icflag = luatexbase.attributes['ltj@icflag']
12 local cat_lp = luatexbase.catcodetables['latex-package']
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
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')
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 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_utod = luatexja.dir_table.dir_utod
57 local dir_math_mod = luatexja.dir_table.dir_math_mod
58 local dir_node_auto = luatexja.dir_table.dir_node_auto
59 local dir_node_manual = luatexja.dir_table.dir_node_manual
60 local function get_attr_icflag(p)
61 return (has_attr(p, attr_icflag) or 0) % PROCESSED_BEGIN_FLAG
66 local function adjust_badness(hd)
67 if not node_next(hd) and getid(hd)==id_whatsit and getsubtype(hd)==sid_user
68 and getfield(hd, 'user_id')==DIR then
69 -- avoid double whatsit
70 luatexja.global_temp=tex.globaldefs; tex.globaldefs=0
71 luatexja.hbadness_temp=tex.hbadness; tex.hbadness=10000
72 luatexja.vbadness_temp=tex.vbadness; tex.vbadness=10000
74 luatexja.global_temp = nil
75 luatexja.hbadness_temp=nil
76 luatexja.vbadness_temp=nil
80 local get_dir_count, get_adjust_dir_count
82 local function get_dir_count_inner(h)
84 if h.id==id_whatsit and h.subtype==sid_user and h.user_id==DIR then
85 local ic = node.has_attribute(h, attr_icflag) or 0
86 return (ic<PROCESSED_BEGIN_FLAG)
87 and (node.has_attribute(h,attr_dir)%dir_node_auto) or 0
95 function get_dir_count()
96 for i=tex_nest.ptr, 1, -1 do
97 local h = tex_nest[i].head.next
99 local t = get_dir_count_inner(h)
100 if t~=0 then return t end
103 return page_direction
105 function get_adjust_dir_count()
106 for i=tex_nest.ptr, 1, -1 do
107 local v = tex_nest[i]
108 local h, m = v.head.next, v.mode
109 if abs(m)== ltjs.vmode and h then
110 local t = get_dir_count_inner(h)
111 if t~=0 then return t end
114 return page_direction
116 luatexja.direction.get_dir_count = get_dir_count
117 luatexja.direction.get_adjust_dir_count = get_adjust_dir_count
121 -- \tate, \yoko,\dtou, \utod
123 local node_next = node.next
124 local node_set_attr = node.set_attribute
125 local node_traverse = node.traverse
126 local STCK = luatexja.userid_table.STCK
127 local IHB = luatexja.userid_table.IHB
129 local function test_list(h, lv)
131 return 2 -- need to create dir_whatsit
133 local flag = 2 -- need to create dir_whatsit
135 for p in node_traverse(h) do
136 if p.id==id_whatsit then
137 if p.subtype==sid_user then
140 flag = 1; w = w or p -- found
141 elseif not(uid==IHB or uid==STCK) then
142 flag = 0; break -- error
151 if flag==1 then -- dir_whatsit already exists
158 function luatexja.direction.set_list_direction_hook(v)
159 local lv = tex_nest.ptr -- must be >= 1
162 if abs(tex_nest[lv-1].mode) == ltjs.mmode and v == dir_tate then
166 v = get_adjust_dir_count()
168 local h = to_direct(tex_nest[lv].head)
169 local w = node_new(id_whatsit, sid_user)
170 setfield(w, 'next', nil)
171 setfield(w, 'user_id', DIR)
172 setfield(w, 'type', 110)
173 set_attr(w, attr_dir, v)
174 insert_after(h, h, w)
175 tex_nest[lv].tail = to_node(node_tail(w))
176 tex_set_attr('global', attr_icflag, 0)
177 tex_set_attr('global', attr_dir, 0)
180 local function set_list_direction(v, name)
181 local lv = tex_nest.ptr
183 v,name = get_dir_count(), nil
184 if lv>=1 and abs(tex_nest[lv-1].mode) == ltjs.mmode and v == dir_tate then
188 v,name = get_adjust_dir_count(), nil
190 if tex.currentgrouptype==6 then
193 "You can't use `\\" .. name .. "' in an align",
194 "To change direction in an align, \n"
195 .. "you shold use \\hbox or \\vbox.")
197 local h = (lv==0) and tex.lists.page_head or tex_nest[lv].head.next
198 local flag,w = test_list(h,lv)
200 if lv==0 and not page_direction then
201 page_direction = v -- for first call of \yoko (in luatexja-core.sty)
205 "Use `\\" .. tostring(name) .. "' at top of list",
206 'Direction change command by LuaTeX-ja is available\n'
207 .. 'only when the current list is null.')
210 node_set_attr(w, attr_dir, v)
211 if lv==0 then page_direction = v end
213 local w = node_new(id_whatsit, sid_user)
214 setfield(w, 'next', nil)
215 setfield(w, 'user_id', DIR)
216 setfield(w, 'type', 110)
217 set_attr(w, attr_dir, v)
219 if lv==0 then page_direction = v end
221 tex_set_attr('global', attr_icflag, 0)
223 tex_set_attr('global', attr_dir, 0)
225 luatexja.direction.set_list_direction = set_list_direction
228 -- ボックスに dir whatsit を追加
229 local function create_dir_whatsit(hd, gc, new_dir)
230 if getid(hd)==id_whatsit and
231 getsubtype(hd)==sid_user and getfield(hd, 'user_id')==DIR then
232 set_attr(hd, attr_icflag,
233 get_attr_icflag(hd) + PROCESSED_BEGIN_FLAG)
234 tex_set_attr('global', attr_icflag, 0)
237 local w = node_new(id_whatsit, sid_user)
238 setfield(w, 'next', hd)
239 setfield(w, 'user_id', DIR)
240 setfield(w, 'type', 110)
241 set_attr(w, attr_dir, new_dir)
242 set_attr(w, attr_icflag, PROCESSED_BEGIN_FLAG)
243 set_attr(hd, attr_icflag,
244 (has_attr(hd, attr_icflag) or 0)%PROCESSED_BEGIN_FLAG
245 + PROCESSED_BEGIN_FLAG)
246 tex_set_attr('global', attr_dir, 0)
247 tex_set_attr('global', attr_icflag, 0)
252 -- hpack_filter, vpack_filter, post_line_break_filter
253 -- の結果を組方向を明示するため,先頭に dir_node を設置
255 local function create_dir_whatsit_hpack(h, gc)
256 local hd = to_direct(h)
257 if gc=='fin_row' or gc == 'preamble' then
259 set_attr(hd, attr_icflag, PROCESSED_BEGIN_FLAG)
260 tex_set_attr('global', attr_icflag, 0)
265 return to_node(create_dir_whatsit(hd, gc, ltjs.list_dir))
269 luatexbase.add_to_callback('hpack_filter',
270 create_dir_whatsit_hpack, 'ltj.create_dir_whatsit', 10000)
274 local function create_dir_whatsit_parbox(h, gc)
275 stop_time_measure('tex_linebreak')
276 -- start 側は ltj-debug.lua に
277 local new_dir, hd = ltjs.list_dir, to_direct(h)
278 for line in traverse_id(id_hlist, hd) do
279 local nh = getlist(line)
280 setfield(line, 'head', create_dir_whatsit(nh, gc, new_dir) )
281 --set_attr(line, attr_dir, new_dir)
283 tex_set_attr('global', attr_dir, 0)
286 luatexbase.add_to_callback('post_linebreak_filter',
287 create_dir_whatsit_parbox, 'ltj.create_dir_whatsit', 10000)
290 local create_dir_whatsit_vbox
293 local id_glue, sid_parskip = node.id('glue'), 3
294 create_dir_whatsit_vbox = function (hd, gc)
295 ltjs.list_dir = get_dir_count()
296 -- remove dir whatsit
297 for x in traverse_id(id_whatsit, hd) do
298 if getsubtype(x)==sid_user and getfield(x, 'user_id')==DIR then
303 ltjs.list_dir =has_attr(hd,attr_dir)
304 local x = node_next(hd)
305 if getid(x)==id_glue and getsubtype(x)==sid_parskip then
306 node_remove(hd,x); node_free(x)
310 hd = node_remove(hd, wh[i]); node_free(wh[i]); wh[i] = nil
312 if gc=='fin_row' then -- gc == 'preamble' case is treated in dir_adjust_vpack
314 set_attr(hd, attr_icflag, PROCESSED_BEGIN_FLAG)
315 tex_set_attr('global', attr_icflag, 0)
319 local n =node_next(hd)
321 local w = create_dir_whatsit(hd, gc, ltjs.list_dir)
322 -- move dir whatsit after hd
323 setfield(hd, 'next', w); setfield(w, 'next', n)
326 hd = create_dir_whatsit(hd, gc, ltjs.list_dir)
333 -- dir_node に包む方法を書いたテーブル
336 local floor = math.floor
337 local get_h =function (w,h,d) return h end
338 local get_d =function (w,h,d) return d end
339 local get_h_d =function (w,h,d) return h+d end
340 local get_h_d_neg =function (w,h,d) return -h-d end
341 local get_d_neg =function (w,h,d) return -d end
342 local get_w_half =function (w,h,d) return floor(0.5*w) end
343 local get_w_half_rem =function (w,h,d) return w-floor(0.5*w) end
344 local get_w_neg =function (w,h,d) return -w end
345 local get_w =function (w,h,d) return w end
346 local zero = function() return 0 end
348 [dir_yoko] = { -- yoko を
349 [dir_tate] = { -- tate 中で組む
352 depth = get_w_half_rem,
354 { 'whatsit', sid_save },
355 { 'rotate', '0 1 -1 0' },
356 { 'kern', function(w,h,d,nw,nh,nd) return -nd end },
358 { 'kern', function(w,h,d,nw,nh,nd) return nd-w end },
359 { 'whatsit', sid_restore },
362 { 'whatsit', sid_save },
363 { 'rotate', '0 1 -1 0' },
365 { 'box' , function(w,h,d,nw,nh,nd) return -nh-nd end },
366 { 'kern', get_h_d_neg},
367 { 'whatsit', sid_restore },
370 [dir_dtou] = { -- dtou 中で組む
375 { 'whatsit', sid_save },
376 { 'rotate', '0 -1 1 0' },
377 { 'kern', function(w,h,d,nw,nh,nd) return -nh end },
378 { 'box', get_d_neg },
379 { 'kern', function(w,h,d,nw,nh,nd) return nh-w end },
380 { 'whatsit', sid_restore },
383 { 'whatsit', sid_save },
384 { 'rotate', '0 -1 1 0' },
385 { 'kern', get_h_d_neg },
387 { 'whatsit', sid_restore },
391 [dir_tate] = { -- tate を
392 [dir_yoko] = { -- yoko 中で組む
397 { 'whatsit', sid_save },
398 { 'rotate', '0 -1 1 0' },
399 { 'kern', function (w,h,d,nw,nh,nd) return -nh end },
400 { 'box' , get_d_neg },
401 { 'kern', function (w,h,d,nw,nh,nd) return nh-w end },
402 { 'whatsit', sid_restore },
405 { 'whatsit', sid_save },
406 { 'rotate', '0 -1 1 0' },
407 { 'kern', get_h_d_neg },
409 { 'whatsit', sid_restore },
412 [dir_dtou] = { -- dtou 中で組む
417 { 'whatsit', sid_save },
418 { 'rotate', '-1 0 0 -1' },
419 { 'kern', get_w_neg },
420 { 'box', function (w,h,d,nw,nh,nd) return h-nd end },
421 { 'whatsit', sid_restore },
424 { 'whatsit', sid_save },
425 { 'rotate', '-1 0 0 -1' },
426 { 'kern', get_h_d_neg },
427 { 'box', get_w_neg },
428 { 'whatsit', sid_restore },
432 [dir_dtou] = { -- dtou を
433 [dir_yoko] = { -- yoko 中で組む
438 { 'whatsit', sid_save },
439 { 'rotate', '0 1 -1 0' },
440 { 'kern', function (w,h,d,nw,nh,nd) return -nd end },
442 { 'kern', function (w,h,d,nw,nh,nd) return nd-w end },
443 { 'whatsit', sid_restore },
447 { 'whatsit', sid_save },
448 { 'rotate', '0 1 -1 0' },
449 { 'box', function (w,h,d,nw,nh,nd) return -nd-nh end },
450 { 'kern', get_h_d_neg },
451 { 'whatsit', sid_restore },
454 [dir_tate] = { -- tate 中で組む
459 { 'whatsit', sid_save },
460 { 'rotate', '-1 0 0 -1' },
461 { 'kern', get_w_neg },
462 { 'box', function (w,h,d,nw,nh,nd) return h-nd end },
463 { 'whatsit', sid_restore },
466 { 'whatsit', sid_save },
467 { 'rotate', ' -1 0 0 -1' },
468 { 'kern', function (w,h,d,nw,nh,nd) return -nh-nd end },
469 { 'box', get_w_neg },
470 { 'kern', function (w,h,d,nw,nh,nd) return nh+nd-h-d end },
471 { 'whatsit', sid_restore },
478 -- 1st ret val: b の組方向
479 -- 2nd ret val はその DIR whatsit
480 local function get_box_dir(b, default)
481 start_time_measure('get_box_dir')
482 local dir = has_attr(b, attr_dir) or 0
483 local bh = getfield(b,'head')
484 -- b は insert node となりうるので getlist() は使えない
487 if bh and getid(bh)==id_whatsit
488 and getsubtype(bh)==sid_user and getfield(bh, 'user_id')==DIR then
490 dir = (dir==0) and has_attr(bh, attr_dir) or dir
494 stop_time_measure('get_box_dir')
495 return (dir==0 and default or dir), c
499 local getbox = tex.getbox
501 function luatexja.direction.unbox_check_dir(is_copy)
502 start_time_measure('box_primitive_hook')
503 local list_dir = get_dir_count()%dir_math_mod
504 local b = getbox(tex_getcount('ltj@tempcnta'))
506 local box_dir = get_box_dir(to_direct(b), dir_yoko)
507 if box_dir%dir_math_mod ~= list_dir then
510 "Incompatible direction list can't be unboxed",
511 'I refuse to unbox a box in differrent direction.')
512 tex.sprint(cat_lp, '\\@gobbletwo')
515 local bd = to_direct(b)
516 local hd = getlist(bd)
519 if getid(hd)==id_whatsit and getsubtype(hd)==sid_user
520 and getfield(hd, 'user_id')==DIR then
522 nh, hd = node_remove(nh, hd)
523 if is_copy and (not dir_backup) then
525 setfield(dir_backup, 'next', nil)
533 setfield(bd, 'head', nh)
536 if luatexja.global_temp and tex.globaldefs~=luatexja.global_temp then
537 tex.globaldefs = luatexja.global_temp
539 stop_time_measure('box_primitive_hook')
541 function luatexja.direction.uncopy_restore_whatsit()
542 local b = getbox(tex_getcount('ltj@tempcnta'))
544 local bd = to_direct(b)
546 setfield(dir_backup, 'next', getlist(bd))
547 setfield(bd, 'head', dir_backup)
554 -- dir_node に包まれている「本来の中身」を取り出し,
556 local function unwrap_dir_node(b, head, box_dir)
557 -- b: dir_node, head: the head of list, box_dir:
558 -- return values are (new head), (next of b), (contents), (dir of contents)
559 local bh = getlist(b)
562 nh = insert_before(head, b, bh)
563 nh, nb = node_remove(nh, b)
564 setfield(b, 'next', nil)
567 local shift_old, b_dir, wh = nil, get_box_dir(bh, 0)
569 Dnode.flush_list(getfield(wh, 'value'))
570 setfield(wh, 'value', nil)
572 return nh, nb, bh, b_dir
575 -- is_manual: 寸法変更に伴うものか?
576 local function create_dir_node(b, b_dir, new_dir, is_manual)
577 local info = dir_node_aux[b_dir%dir_math_mod][new_dir%dir_math_mod]
578 local w = getfield(b, 'width')
579 local h = getfield(b, 'height')
580 local d = getfield(b, 'depth')
581 local db = node_new(getid(b)) -- dir_node
582 set_attr(db, attr_dir,
583 new_dir + (is_manual and dir_node_manual or dir_node_auto))
584 set_attr(db, attr_icflag, PROCESSED)
585 set_attr(b, attr_icflag, PROCESSED)
586 tex_set_attr('global', attr_dir, 0)
587 tex_set_attr('global', attr_icflag, 0)
588 setfield(db, 'dir', getfield(b, 'dir'))
589 setfield(db, 'shift', 0)
590 setfield(db, 'width', info.width(w,h,d))
591 setfield(db, 'height', info.height(w,h,d))
592 setfield(db, 'depth', info.depth(w,h,d))
597 local make_dir_whatsit, process_dir_node
599 make_dir_whatsit = function (head, b, new_dir, origin)
600 new_dir = new_dir%dir_math_mod
601 -- head: list head, b: box
602 -- origin: コール元 (for debug)
603 -- return value: (new head), (next of b), (new b), (is_b_dir_node)
604 -- (new b): b か dir_node に被せられた b
605 local bh = getlist(b)
606 local box_dir, dn = get_box_dir(b, ltjs.list_dir)
607 -- 既に b の中身にあるwhatsit
609 if box_dir%dir_math_mod==new_dir then
610 if box_dir>=dir_node_auto then
611 -- dir_node としてカプセル化されている
612 local _, dnc = get_box_dir(b, 0)
613 if dnc then -- free all other dir_node
614 Dnode.flush_list(getfield(dnc, 'value'))
615 setfield(dnc, 'value', nil)
617 set_attr(b, attr_dir, box_dir%dir_math_mod + dir_node_auto)
618 return head, node_next(b), b, true
620 -- 組方向が一緒 (up to math dir) のボックスなので,何もしなくて良い
621 return head, node_next(b), b, false
625 local nh, nb, ret, flag
626 if box_dir>= dir_node_auto then -- unwrap
628 head, nb, b, b_dir = unwrap_dir_node(b, head, box_dir)
630 if b_dir%dir_math_mod==new_dir then
631 -- dir_node の中身が周囲の組方向とあっている
632 return head, nb, b, false
633 else box_dir = b_dir end
635 box_dir = box_dir%dir_math_mod
637 local dnh = getfield(dn, 'value')
638 for x in traverse(dnh) do
639 if has_attr(x, attr_dir)%dir_math_mod == new_dir then
640 setfield(dn, 'value', to_node(node_remove(dnh, x)))
644 Dnode.flush_list(getfield(dn, 'value'))
645 setfield(dn, 'value', nil)
646 db = db or create_dir_node(b, box_dir, new_dir, false)
647 local w = getfield(b, 'width')
648 local h = getfield(b, 'height')
649 local d = getfield(b, 'depth')
650 local dn_w = getfield(db, 'width')
651 local dn_h = getfield(db, 'height')
652 local dn_d = getfield(db, 'depth')
653 nh, nb = insert_before(head, b, db), nil
654 nh, nb = node_remove(nh, b)
655 setfield(b, 'next', nil); setfield(db, 'head', b)
657 return nh, nb, ret, flag
660 process_dir_node = function (hd, gc)
661 local x, new_dir = hd, ltjs.list_dir or dir_yoko
664 if (xid==id_hlist and get_attr_icflag(x)~=PACKED)
665 or xid==id_vlist then
666 hd, x = make_dir_whatsit(hd, x, new_dir, 'process_dir_node:' .. gc)
675 local node_prev = (Dnode~=node) and Dnode.getprev or node.prev
676 local function lastbox_hook()
677 start_time_measure('box_primitive_hook')
678 local bn = tex_nest[tex_nest.ptr].tail
680 local b, head = to_direct(bn), to_direct(tex_nest[tex_nest.ptr].head)
682 if bid==id_hlist or bid==id_vlist then
683 local box_dir = get_box_dir(b, 0)
684 if box_dir>= dir_node_auto then -- unwrap dir_node
685 local p = node_prev(b)
686 local dummy1, dummy2, nb = unwrap_dir_node(b, nil, box_dir)
687 setfield(p, 'next', nb); tex_nest[tex_nest.ptr].tail = to_node(nb)
688 setfield(b, 'next', nil); setfield(b, 'head', nil)
691 local _, wh = get_box_dir(b, 0) -- clean dir_node attached to the box
693 Dnode.flush_list(getfield('value', wh))
694 setfield(wh, 'value', nil)
698 stop_time_measure('box_primitive_hook')
701 luatexja.direction.make_dir_whatsit = make_dir_whatsit
702 luatexja.direction.lastbox_hook = lastbox_hook
705 -- \wd, \ht, \dp の代わり
707 local getbox, setdimen = tex.getbox, tex.setdimen
708 local function get_box_dim_common(key, s, l_dir)
710 local s_dir, wh = get_box_dir(s, dir_yoko)
711 s_dir = s_dir%dir_math_mod
712 if s_dir ~= l_dir then
713 local not_found = true
714 for x in traverse(getfield(wh, 'value')) do
715 if l_dir == has_attr(x, attr_dir)%dir_node_auto then
716 setdimen('ltj@tempdima', getfield(x, key))
717 not_found = false; break
721 local w = getfield(s, 'width')
722 local h = getfield(s, 'height')
723 local d = getfield(s, 'depth')
724 setdimen('ltj@tempdima',
725 dir_node_aux[s_dir][l_dir][key](w,h,d))
728 setdimen('ltj@tempdima', getfield(s, key))
731 local function get_box_dim(key, n)
732 local gt = tex.globaldefs; tex.globaldefs = 0
735 local l_dir = (get_dir_count())%dir_math_mod
737 local b_dir = get_box_dir(s,dir_yoko)
738 if b_dir<dir_node_auto then
739 get_box_dim_common(key, s, l_dir)
740 elseif b_dir%dir_math_mod==l_dir then
741 setdimen('ltj@tempdima', getfield(s, key))
743 get_box_dim_common(key, getlist(s), l_dir)
746 setdimen('ltj@tempdima', 0)
750 luatexja.direction.get_box_dim = get_box_dim
752 -- return value: (changed dimen of box itself?)
753 local function set_box_dim_common(key, s, l_dir)
754 local s_dir, wh = get_box_dir(s, dir_yoko)
755 s_dir = s_dir%dir_math_mod
756 if s_dir ~= l_dir then
758 wh = create_dir_whatsit(getlist(s), 'set_box_dim', s_dir)
759 setfield(s, 'head', wh)
762 local dnh = getfield(wh, 'value')
763 for x in traverse(dnh) do
764 if has_attr(x, attr_dir)%dir_node_auto==l_dir then
769 db = create_dir_node(s, s_dir, l_dir, true)
770 setfield(db, 'next', dnh)
771 setfield(wh, 'value',to_node(db))
773 setfield(db, key, tex.getdimen('ltj@tempdima'))
776 setfield(s, key, tex.getdimen('ltj@tempdima'))
778 -- change dimension of dir_nodes which are created "automatically"
780 = getfield(s,'width'), getfield(s, 'height'), getfield(s, 'depth')
781 for x in traverse(getfield(wh, 'value')) do
782 local x_dir = has_attr(x, attr_dir)
783 if x_dir<dir_node_manual then
784 local info = dir_node_aux[s_dir][x_dir%dir_node_auto]
785 setfield(x, 'width', info.width(bw,bh,bd))
786 setfield(x, 'height', info.height(bw,bh,bd))
787 setfield(x, 'depth', info.depth(bw,bh,bd))
794 local function set_box_dim(key)
795 local n = tex_getcount('ltj@tempcnta')
798 local l_dir = (get_dir_count())%dir_math_mod
800 local b_dir = get_box_dir(s,dir_yoko)
801 if b_dir<dir_node_auto then
802 set_box_dim_common(key, s, l_dir)
803 elseif b_dir%dir_math_mod == l_dir then
805 setfield(s, key, tex.getdimen('ltj@tempdima'))
806 if b_dir<dir_node_manual then
807 set_attr(s, attr_dir, b_dir%dir_node_auto + dir_node_manual)
810 local sid, b = getid(s), getlist(s)
811 local info = dir_node_aux[get_box_dir(b,dir_yoko)%dir_math_mod][b_dir%dir_node_auto]
813 = getfield(b,'width'), getfield(b, 'height'), getfield(b, 'depth')
815 = getfield(s,'width'), getfield(s, 'height'), getfield(s, 'depth')
816 if set_box_dim_common(key, b, l_dir) and b_dir<dir_node_manual then
817 -- re-calculate dimension of s, if s is created "automatically"
818 if b_dir<dir_node_manual then
819 setfield(s, 'width', info.width(bw,bh,bd))
820 setfield(s, 'height', info.height(bw,bh,bd))
821 setfield(s, 'depth', info.depth(bw,bh,bd))
827 luatexja.direction.set_box_dim = set_box_dim
831 local getbox = tex.getbox
832 local function get_register_dir(n)
836 local b_dir = get_box_dir(s, dir_yoko)
837 if b_dir<dir_node_auto then
840 local b_dir = get_box_dir(
841 node_next(node_next(node_next(getlist(s)))), dir_yoko)
848 luatexja.direction.get_register_dir = get_register_dir
852 local getbox, setbox, copy_list = tex.getbox, tex.setbox, Dnode.copy_list
854 function luatexja.direction.raise_box()
855 start_time_measure('box_primitive_hook')
856 local list_dir = get_dir_count()
857 local s = getbox('ltj@afbox')
859 local sd = to_direct(s)
860 local box_dir = get_box_dir(sd, dir_yoko)
861 if box_dir%dir_math_mod ~= list_dir then
864 to_node(copy_list(make_dir_whatsit(sd, sd, list_dir, 'box_move')))
865 -- copy_list しないとリストの整合性が崩れる……?
869 stop_time_measure('box_primitive_hook')
873 -- PACKED の hbox から文字を取り出す
874 -- luatexja.jfmglue.check_box などで使用
876 local function glyph_from_packed(h)
878 return (getid(b)==id_kern or (getid(b)==id_whatsit and getsubtype(b)==sid_save) )
879 and node_next(node_next(node_next(b))) or b
881 luatexja.direction.glyph_from_packed = glyph_from_packed
884 -- adjust and insertion
885 local id_adjust = node.id('adjust')
886 function luatexja.direction.check_adjust_direction()
887 start_time_measure('box_primitive_hook')
888 local list_dir = get_adjust_dir_count()
889 local a = tex_nest[tex_nest.ptr].tail
890 local ad = to_direct(a)
891 if a and getid(ad)==id_adjust then
892 local adj_dir = get_box_dir(ad)
893 if list_dir~=adj_dir then
896 'Direction Incompatible',
897 "\\vadjust's argument and outer vlist must have same direction.")
901 stop_time_measure('box_primitive_hook')
906 local split_dir_whatsit
907 local function dir_adjust_vpack(h, gc)
908 start_time_measure('direction_vpack')
909 local hd = to_direct(h)
910 if gc=='split_keep' then
911 -- supply dir_whatsit
912 hd = create_dir_whatsit_vbox(hd, gc)
913 split_dir_whatsit = hd
914 elseif gc=='split_off' then
917 if bh and getid(bh)==id_whatsit
918 and getsubtype(bh)==sid_user and getfield(bh, 'user_id')==DIR then
919 ltjs.list_dir = has_attr(bh, attr_dir); break
923 if split_dir_whatsit then
924 -- adjust direction of 'split_keep'
925 set_attr(split_dir_whatsit, attr_dir, ltjs.list_dir)
927 split_dir_whatsit=nil
928 elseif gc=='preamble' then
929 split_dir_whatsit=nil
932 hd = process_dir_node(create_dir_whatsit_vbox(hd, gc), gc)
933 split_dir_whatsit=nil
935 stop_time_measure('direction_vpack')
938 luatexbase.add_to_callback('vpack_filter',
940 'ltj.direction', 10000)
944 -- supply direction whatsit to the main vertical list "of the next page"
945 local function dir_adjust_pre_output(h, gc)
946 return to_node(create_dir_whatsit_vbox(to_direct(h), gc))
948 luatexbase.add_to_callback('pre_output_filter',
949 dir_adjust_pre_output,
950 'ltj.direction', 10000)
952 function luatexja.direction.remove_end_whatsit()
953 local h=tex.lists.page_head
954 if h and (not h.next) and
955 h.id==id_whatsit and h.subtype==sid_user and
956 h.user_id == DIR then
957 tex.lists.page_head = nil
965 local function dir_adjust_buildpage(info)
967 local head = to_direct(tex.lists.contrib_head)
971 = make_dir_whatsit(head,
975 tex.lists.contrib_head = to_node(head)
979 luatexbase.add_to_callback('buildpage_filter',
980 dir_adjust_buildpage,
981 'ltj.direction', 10000)
984 -- finalize (executed just before \shipout)
985 -- we supply correct pdfsavematrix nodes etc. inside dir_node
988 local function finalize_dir_node(db,new_dir)
989 local b = getlist(db)
991 local box_dir = get_box_dir(b, dir_yoko)%dir_math_mod
992 setfield(db, 'head', nil)
993 local w = getfield(b, 'width')
994 local h = getfield(b, 'height')
995 local d = getfield(b, 'depth')
996 local dn_w = getfield(db, 'width')
997 local dn_h = getfield(db, 'height')
998 local dn_d = getfield(db, 'depth')
999 local db_head, db_tail = nil
1000 for _,v in ipairs(dir_node_aux[box_dir][new_dir][getid(b)]) do
1001 local cmd, arg, nn = v[1], v[2]
1003 nn = node_new(id_kern)
1004 setfield(nn, 'kern', arg(w, h, d, dn_w, dn_h, dn_d))
1005 elseif cmd=='whatsit' then
1006 nn = node_new(id_whatsit, arg)
1007 elseif cmd=='rotate' then
1008 nn = node_new(id_whatsit, sid_matrix)
1009 setfield(nn, 'data', arg)
1010 elseif cmd=='box' then
1011 nn = b; setfield(b, 'next', nil)
1012 setfield(nn, 'shift', arg(w, h, d, dn_w, dn_h, dn_d))
1015 insert_after(db_head, db_tail, nn)
1018 db_head, db_tail = nn, nn
1020 setfield(db, 'head', db_head)
1023 finalize_inner = function (box)
1024 for n in traverse(getlist(box)) do
1025 local nid = getid(n)
1026 if (nid==id_hlist or nid==id_vlist) then
1027 local ndir = get_box_dir(n, dir_yoko)
1028 if ndir>=dir_node_auto then -- n is dir_node
1029 finalize_dir_node(n, ndir%dir_math_mod)
1036 local getbox = tex.getbox
1037 function luatexja.direction.finalize()
1038 finalize_inner(to_direct(tex.getbox("AtBeginShipoutBox")))