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
63 local function adjust_badness(hd)
64 if not node_next(hd) and getid(hd)==id_whatsit and getsubtype(hd)==sid_user
65 and getfield(hd, 'user_id')==DIR then
66 -- avoid double whatsit
67 luatexja.global_temp=tex.globaldefs; tex.globaldefs=0
68 luatexja.hbadness_temp=tex.hbadness; tex.hbadness=10000
69 luatexja.vbadness_temp=tex.vbadness; tex.vbadness=10000
71 luatexja.global_temp = nil
72 luatexja.hbadness_temp=nil
73 luatexja.vbadness_temp=nil
77 local get_dir_count, get_adjust_dir_count
79 local function get_dir_count_inner(h)
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)
84 and (node.has_attribute(h,attr_dir)%dir_node_auto) or 0
92 function get_dir_count()
93 for i=tex_nest.ptr, 1, -1 do
94 local h = tex_nest[i].head.next
96 local t = get_dir_count_inner(h)
97 if t~=0 then return t end
100 return page_direction
102 function get_adjust_dir_count()
103 for i=tex_nest.ptr, 1, -1 do
104 local v = tex_nest[i]
105 local h, m = v.head.next, v.mode
106 if abs(m)== ltjs.vmode and h then
107 local t = get_dir_count_inner(h)
108 if t~=0 then return t end
111 return page_direction
113 luatexja.direction.get_dir_count = get_dir_count
114 luatexja.direction.get_adjust_dir_count = get_adjust_dir_count
118 -- \tate, \yoko,\dtou, \utod
120 local node_next = node.next
121 local node_set_attr = node.set_attribute
122 local node_traverse = node.traverse
123 local STCK = luatexja.userid_table.STCK
124 local IHB = luatexja.userid_table.IHB
126 local function test_list(h, lv)
128 return 2 -- need to create dir_whatsit
130 local flag = 2 -- need to create dir_whatsit
132 for p in node_traverse(h) do
133 if p.id==id_whatsit then
134 if p.subtype==sid_user then
137 flag = 1; w = w or p -- found
138 elseif not(uid==IHB or uid==STCK) then
139 flag = 0; break -- error
148 if flag==1 then -- move dir_whatsit w
155 local function set_list_direction(v, name)
156 local lv = tex_nest.ptr
158 v,name = get_dir_count(), nil
159 if lv>=1 and abs(tex_nest[lv-1].mode) == ltjs.mmode and v == dir_tate then
163 v,name = get_adjust_dir_count(), nil
165 if tex.currentgrouptype==6 then
168 "You can't use `\\" .. name .. "' in an align",
169 "To change direction in an align, \n"
170 .. "you shold use \\hbox or \\vbox.")
172 local h = (lv==0) and tex.lists.page_head or tex_nest[lv].head.next
173 local flag,w = test_list(h,lv)
175 if lv==0 and not page_direction then
176 page_direction = v -- for first call of \yoko (in luatexja-core.sty)
180 "Use `\\" .. tostring(name) .. "' at top of list",
181 'Direction change command by LuaTeX-ja is available\n'
182 .. 'only when the current list is null.')
185 node_set_attr(w, attr_dir, v)
186 if lv==0 then page_direction = v end
188 local w = node_new(id_whatsit, sid_user)
189 setfield(w, 'next', nil)
190 setfield(w, 'user_id', DIR)
191 setfield(w, 'type', 110)
192 set_attr(w, attr_dir, v)
194 if lv==0 then page_direction = v end
196 tex_set_attr('global', attr_icflag, 0)
198 tex_set_attr('global', attr_dir, 0)
200 luatexja.direction.set_list_direction = set_list_direction
203 -- ボックスに dir whatsit を追加
204 local function create_dir_whatsit(hd, gc, new_dir)
205 if getid(hd)==id_whatsit and
206 getsubtype(hd)==sid_user and getfield(hd, 'user_id')==DIR then
207 set_attr(hd, attr_icflag,
208 (has_attr(hd, attr_icflag) or 0)%PROCESSED_BEGIN_FLAG
209 + PROCESSED_BEGIN_FLAG)
210 tex_set_attr('global', attr_icflag, 0)
213 local w = node_new(id_whatsit, sid_user)
214 setfield(w, 'next', hd)
215 setfield(w, 'user_id', DIR)
216 setfield(w, 'type', 110)
217 set_attr(w, attr_dir, new_dir)
218 set_attr(w, attr_icflag, PROCESSED_BEGIN_FLAG)
219 set_attr(hd, attr_icflag,
220 (has_attr(hd, attr_icflag) or 0)%PROCESSED_BEGIN_FLAG
221 + PROCESSED_BEGIN_FLAG)
222 tex_set_attr('global', attr_dir, 0)
223 tex_set_attr('global', attr_icflag, 0)
228 -- hpack_filter, vpack_filter, post_line_break_filter
229 -- の結果を組方向を明示するため,先頭に dir_node を設置
231 local function create_dir_whatsit_hpack(h, gc)
232 local hd = to_direct(h)
233 if gc=='fin_row' or gc == 'preamble' then
235 set_attr(hd, attr_icflag, PROCESSED_BEGIN_FLAG)
236 tex_set_attr('global', attr_icflag, 0)
241 return to_node(create_dir_whatsit(hd, gc, ltjs.list_dir))
245 luatexbase.add_to_callback('hpack_filter',
246 create_dir_whatsit_hpack, 'ltj.create_dir_whatsit', 10000)
250 local function create_dir_whatsit_parbox(h, gc)
251 stop_time_measure('tex_linebreak')
252 -- start 側は ltj-debug.lua に
253 local new_dir, hd = ltjs.list_dir, to_direct(h)
254 for line in traverse_id(id_hlist, hd) do
255 local nh = getlist(line)
256 setfield(line, 'head', create_dir_whatsit(nh, gc, new_dir) )
257 set_attr(line, attr_dir, new_dir)
259 tex_set_attr('global', attr_dir, 0)
262 luatexbase.add_to_callback('post_linebreak_filter',
263 create_dir_whatsit_parbox, 'ltj.create_dir_whatsit', 10000)
266 local create_dir_whatsit_vbox
269 local id_glue, sid_parskip = node.id('glue'), 3
270 create_dir_whatsit_vbox = function (hd, gc)
271 ltjs.list_dir = get_dir_count()
272 -- remove dir whatsit
273 for x in traverse_id(id_whatsit, hd) do
274 if getsubtype(x)==sid_user and getfield(x, 'user_id')==DIR then
279 ltjs.list_dir =has_attr(hd,attr_dir)
280 local x = node_next(hd)
281 if getid(x)==id_glue and getsubtype(x)==sid_parskip then
282 node_remove(hd,x); node_free(x)
286 hd = node_remove(hd, wh[i]); node_free(wh[i]); wh[i] = nil
288 if gc=='fin_row' then -- gc == 'preamble' case is treated in dir_adjust_vpack
290 set_attr(hd, attr_icflag, PROCESSED_BEGIN_FLAG)
291 tex_set_attr('global', attr_icflag, 0)
295 local n =node_next(hd)
297 local w = create_dir_whatsit(hd, gc, ltjs.list_dir)
298 -- move dir whatsit after hd
299 setfield(hd, 'next', w); setfield(w, 'next', n)
302 hd = create_dir_whatsit(hd, gc, ltjs.list_dir)
309 -- dir_node に包む方法を書いたテーブル
312 local floor = math.floor
313 local get_h =function (w,h,d) return h end
314 local get_d =function (w,h,d) return d end
315 local get_h_d =function (w,h,d) return h+d end
316 local get_h_d_neg =function (w,h,d) return -h-d end
317 local get_d_neg =function (w,h,d) return -d end
318 local get_w_half =function (w,h,d) return floor(0.5*w) end
319 local get_w_half_rem =function (w,h,d) return w-floor(0.5*w) end
320 local get_w_neg =function (w,h,d) return -w end
321 local get_w =function (w,h,d) return w end
322 local zero = function() return 0 end
324 [dir_yoko] = { -- yoko を
325 [dir_tate] = { -- tate 中で組む
328 depth = get_w_half_rem,
330 { 'whatsit', sid_save },
331 { 'rotate', '0 1 -1 0' },
332 { 'kern', function(w,h,d,nw,nh,nd) return -nd end },
334 { 'kern', function(w,h,d,nw,nh,nd) return nd-w end },
335 { 'whatsit', sid_restore },
338 { 'whatsit', sid_save },
339 { 'rotate', '0 1 -1 0' },
341 { 'box' , function(w,h,d,nw,nh,nd) return -nh-nd end },
342 { 'kern', get_h_d_neg},
343 { 'whatsit', sid_restore },
346 [dir_dtou] = { -- dtou 中で組む
351 { 'whatsit', sid_save },
352 { 'rotate', '0 -1 1 0' },
353 { 'kern', function(w,h,d,nw,nh,nd) return -nh end },
354 { 'box', get_d_neg },
355 { 'kern', function(w,h,d,nw,nh,nd) return nh-w end },
356 { 'whatsit', sid_restore },
359 { 'whatsit', sid_save },
360 { 'rotate', '0 -1 1 0' },
361 { 'kern', get_h_d_neg },
363 { 'whatsit', sid_restore },
367 [dir_tate] = { -- tate を
368 [dir_yoko] = { -- yoko 中で組む
373 { 'whatsit', sid_save },
374 { 'rotate', '0 -1 1 0' },
375 { 'kern', function (w,h,d,nw,nh,nd) return -nh end },
376 { 'box' , get_d_neg },
377 { 'kern', function (w,h,d,nw,nh,nd) return nh-w end },
378 { 'whatsit', sid_restore },
381 { 'whatsit', sid_save },
382 { 'rotate', '0 -1 1 0' },
383 { 'kern', get_h_d_neg },
385 { 'whatsit', sid_restore },
388 [dir_dtou] = { -- dtou 中で組む
393 { 'whatsit', sid_save },
394 { 'rotate', '-1 0 0 -1' },
395 { 'kern', get_w_neg },
396 { 'box', function (w,h,d,nw,nh,nd) return h-nd end },
397 { 'whatsit', sid_restore },
400 { 'whatsit', sid_save },
401 { 'rotate', '-1 0 0 -1' },
402 { 'kern', get_h_d_neg },
403 { 'box', get_w_neg },
404 { 'whatsit', sid_restore },
408 [dir_dtou] = { -- dtou を
409 [dir_yoko] = { -- yoko 中で組む
414 { 'whatsit', sid_save },
415 { 'rotate', '0 1 -1 0' },
416 { 'kern', function (w,h,d,nw,nh,nd) return -nd end },
418 { 'kern', function (w,h,d,nw,nh,nd) return nd-w end },
419 { 'whatsit', sid_restore },
423 { 'whatsit', sid_save },
424 { 'rotate', '0 1 -1 0' },
425 { 'box', function (w,h,d,nw,nh,nd) return -nd-nh end },
426 { 'kern', get_h_d_neg },
427 { 'whatsit', sid_restore },
430 [dir_tate] = { -- tate 中で組む
435 { 'whatsit', sid_save },
436 { 'rotate', '-1 0 0 -1' },
437 { 'kern', get_w_neg },
438 { 'box', function (w,h,d,nw,nh,nd) return h-nd end },
439 { 'whatsit', sid_restore },
442 { 'whatsit', sid_save },
443 { 'rotate', ' -1 0 0 -1' },
444 { 'kern', function (w,h,d,nw,nh,nd) return -nh-nd end },
445 { 'box', get_w_neg },
446 { 'kern', function (w,h,d,nw,nh,nd) return nh+nd-h-d end },
447 { 'whatsit', sid_restore },
454 -- 1st ret val: b の組方向
455 -- 2nd ret val はその DIR whatsit
456 local function get_box_dir(b, default)
457 start_time_measure('get_box_dir')
458 local dir = has_attr(b, attr_dir) or 0
459 local bh = getfield(b,'head')
460 -- b は insert node となりうるので getlist() は使えない
463 if bh and getid(bh)==id_whatsit
464 and getsubtype(bh)==sid_user and getfield(bh, 'user_id')==DIR then
466 dir = (dir==0) and has_attr(bh, attr_dir) or dir
470 stop_time_measure('get_box_dir')
471 return (dir==0 and default or dir), c
475 local getbox = tex.getbox
476 local function check_dir(reg_num)
477 start_time_measure('box_primitive_hook')
478 local list_dir = get_dir_count()%dir_math_mod
479 local b = getbox(tex_getcount('ltj@tempcnta'))
481 local box_dir = get_box_dir(to_direct(b), dir_yoko)
482 if box_dir%dir_math_mod ~= list_dir then
483 -- print('NEST', tex_nest.ptr, tex_getcount('ltj@tempcnta'))
484 -- luatexja.ext_show_node_list(
485 -- (tex_nest.ptr==0) and tex.lists.page_head or tex_nest[tex_nest.ptr].head,
486 -- 'LIST' .. tostring(list_dir) .. '> ', print)
487 -- luatexja.ext_show_node_list(b, 'BOX' .. tostring(box_dir) .. '> ', print)
490 "Incompatible direction list can't be unboxed",
491 'I refuse to unbox a box in differrent direction.')
494 if luatexja.global_temp and tex.globaldefs~=luatexja.global_temp then
495 tex.globaldefs = luatexja.global_temp
497 stop_time_measure('box_primitive_hook')
499 luatexja.direction.check_dir = check_dir
502 -- dir_node に包まれている「本来の中身」を取り出し,
504 local function unwrap_dir_node(b, head, box_dir)
505 -- b: dir_node, head: the head of list, box_dir:
506 -- return values are (new head), (next of b), (contents), (dir of contents)
507 local bh = getlist(b)
508 local bc = node_next(node_next(node_next(bh)))
513 nh = insert_before(head, b, bc)
514 nh, nb = node_remove(nh, b)
515 setfield(b, 'next', nil)
516 setfield(b, 'head', nil)
519 local shift_old, b_dir, wh = nil, get_box_dir(bc, 0)
521 Dnode.flush_list(getfield(wh, 'value'))
522 setfield(wh, 'value', nil)
524 setfield(bc, 'shift', 0)
525 return nh, nb, bc, b_dir
528 -- is_manual: 寸法変更に伴うものか?
529 local function create_dir_node(b, b_dir, new_dir, is_manual)
530 local info = dir_node_aux[b_dir%dir_math_mod][new_dir%dir_math_mod]
531 local w = getfield(b, 'width')
532 local h = getfield(b, 'height')
533 local d = getfield(b, 'depth')
534 local db = node_new(getid(b)) -- dir_node
535 set_attr(db, attr_dir,
536 new_dir + (is_manual and dir_node_manual or dir_node_auto))
537 set_attr(db, attr_icflag, PROCESSED)
538 set_attr(b, attr_icflag, PROCESSED)
539 tex_set_attr('global', attr_dir, 0)
540 tex_set_attr('global', attr_icflag, 0)
541 setfield(db, 'dir', getfield(b, 'dir'))
542 setfield(db, 'shift', 0)
543 setfield(db, 'width', info.width(w,h,d))
544 setfield(db, 'height', info.height(w,h,d))
545 setfield(db, 'depth', info.depth(w,h,d))
550 local make_dir_whatsit, process_dir_node
552 make_dir_whatsit = function (head, b, new_dir, origin)
553 new_dir = new_dir%dir_math_mod
554 -- head: list head, b: box
555 -- origin: コール元 (for debug)
556 -- return value: (new head), (next of b), (new b), (is_b_dir_node)
557 -- (new b): b か dir_node に被せられた b
558 local bh = getlist(b)
559 local box_dir, dn = get_box_dir(b, ltjs.list_dir)
560 -- 既に b の中身にあるwhatsit
562 if box_dir%dir_math_mod==new_dir then
563 if box_dir>=dir_node_auto then
564 -- dir_node としてカプセル化されている
565 local bc = node_next(node_next(node_next(bh)))
566 local _, dnc = get_box_dir(b, 0)
567 if dnc then -- free all other dir_node
568 Dnode.flush_list(getfield(dnc, 'value'))
569 setfield(dnc, 'value', nil)
571 set_attr(b, attr_dir, box_dir%dir_math_mod + dir_node_auto)
572 return head, node_next(b), b, true
574 -- 組方向が一緒 (up to math dir) のボックスなので,何もしなくて良い
575 return head, node_next(b), b, false
579 local nh, nb, ret, flag
580 if box_dir>= dir_node_auto then -- unwrap
582 head, nb, b, b_dir = unwrap_dir_node(b, head, box_dir)
584 if b_dir%dir_math_mod==new_dir then
585 -- dir_node の中身が周囲の組方向とあっている
586 return head, nb, b, false
587 else box_dir = b_dir end
589 box_dir = box_dir%dir_math_mod
591 local dnh = getfield(dn, 'value')
592 for x in traverse(dnh) do
593 if has_attr(x, attr_dir)%dir_math_mod == new_dir then
594 setfield(dn, 'value', to_node(node_remove(dnh, x)))
598 Dnode.flush_list(getfield(dn, 'value'))
599 db = db or create_dir_node(b, box_dir, new_dir, false)
600 local w = getfield(b, 'width')
601 local h = getfield(b, 'height')
602 local d = getfield(b, 'depth')
603 local dn_w = getfield(db, 'width')
604 local dn_h = getfield(db, 'height')
605 local dn_d = getfield(db, 'depth')
606 nh, nb = insert_before(head, b, db), nil
607 nh, nb = node_remove(nh, b)
608 local db_head, db_tail = nil
609 for _,v in ipairs(dir_node_aux[box_dir][new_dir][getid(b)]) do
610 local cmd, arg, nn = v[1], v[2]
612 nn = node_new(id_kern)
613 setfield(nn, 'kern', arg(w, h, d, dn_w, dn_h, dn_d))
614 elseif cmd=='whatsit' then
615 nn = node_new(id_whatsit, arg)
616 elseif cmd=='rotate' then
617 nn = node_new(id_whatsit, sid_matrix)
618 setfield(nn, 'data', arg)
619 elseif cmd=='box' then
620 nn = b; setfield(b, 'next', nil)
621 setfield(nn, 'shift', arg(w, h, d, dn_w, dn_h, dn_d))
624 insert_after(db_head, db_tail, nn)
627 db_head, db_tail = nn, nn
629 setfield(db, 'head', db_head)
632 return nh, nb, ret, flag
635 process_dir_node = function (hd, gc)
636 local x, new_dir = hd, ltjs.list_dir or dir_yoko
639 if (xid==id_hlist and has_attr(x, attr_icflag)%PROCESSED_BEGIN_FLAG~=PACKED)
640 or xid==id_vlist then
641 hd, x = make_dir_whatsit(hd, x, new_dir, 'process_dir_node:' .. gc)
650 local node_prev = (Dnode~=node) and Dnode.getprev or node.prev
651 local function lastbox_hook()
652 start_time_measure('box_primitive_hook')
653 local bn = tex_nest[tex_nest.ptr].tail
655 local b, head = to_direct(bn), to_direct(tex_nest[tex_nest.ptr].head)
657 if bid==id_hlist or bid==id_vlist then
658 local box_dir = get_box_dir(b, 0)
659 if box_dir>= dir_node_auto then -- unwrap dir_node
660 local p = node_prev(b)
661 local dummy1, dummy2, nb = unwrap_dir_node(b, nil, box_dir)
662 setfield(p, 'next', nb); tex_nest[tex_nest.ptr].tail = to_node(nb)
663 setfield(b, 'next', nil); setfield(b, 'head', nil)
666 local _, wh = get_box_dir(b, 0) -- clean dir_node attached to the box
668 Dnode.flush_list(getfield('value', wh))
669 setfield(wh, 'value', nil)
673 stop_time_measure('box_primitive_hook')
676 luatexja.direction.make_dir_whatsit = make_dir_whatsit
677 luatexja.direction.lastbox_hook = lastbox_hook
680 -- \wd, \ht, \dp の代わり
682 local getbox, setdimen = tex.getbox, tex.setdimen
683 local function get_box_dim_common(key, s, l_dir)
685 local s_dir, wh = get_box_dir(s, dir_yoko)
686 s_dir = s_dir%dir_math_mod
687 if s_dir ~= l_dir then
688 local not_found = true
689 for x in traverse(getfield(wh, 'value')) do
690 if l_dir == has_attr(x, attr_dir)%dir_node_auto then
691 setdimen('ltj@tempdima', getfield(x, key))
692 not_found = false; break
696 local w = getfield(s, 'width')
697 local h = getfield(s, 'height')
698 local d = getfield(s, 'depth')
699 setdimen('ltj@tempdima',
700 dir_node_aux[s_dir][l_dir][key](w,h,d))
703 setdimen('ltj@tempdima', getfield(s, key))
706 local function get_box_dim(key, n)
707 local gt = tex.globaldefs; tex.globaldefs = 0
710 local l_dir = (get_dir_count())%dir_math_mod
712 local b_dir = get_box_dir(s,dir_yoko)
713 if b_dir<dir_node_auto then
714 get_box_dim_common(key, s, l_dir)
715 elseif b_dir%dir_math_mod==l_dir then
716 setdimen('ltj@tempdima', getfield(s, key))
718 get_box_dim_common(key,
719 node_next(node_next(node_next(getlist(s)))), l_dir)
722 setdimen('ltj@tempdima', 0)
726 luatexja.direction.get_box_dim = get_box_dim
728 -- return value: (changed dimen of box itself?)
729 local function set_box_dim_common(key, s, l_dir)
730 local s_dir, wh = get_box_dir(s, dir_yoko)
731 s_dir = s_dir%dir_math_mod
732 if s_dir ~= l_dir then
734 wh = create_dir_whatsit(getlist(s), 'set_box_dim', s_dir)
735 setfield(s, 'head', wh)
738 local dnh = getfield(wh, 'value')
739 for x in traverse(dnh) do
740 if has_attr(x, attr_dir)%dir_node_auto==l_dir then
745 db = create_dir_node(s, s_dir, l_dir, true)
746 setfield(db, 'next', dnh)
747 setfield(wh, 'value',to_node(db))
749 setfield(db, key, tex.getdimen('ltj@tempdima'))
752 setfield(s, key, tex.getdimen('ltj@tempdima'))
754 -- change dimension of dir_nodes which are created "automatically"
756 = getfield(s,'width'), getfield(s, 'height'), getfield(s, 'depth')
757 for x in traverse(getfield(wh, 'value')) do
758 local x_dir = has_attr(x, attr_dir)
759 if x_dir<dir_node_manual then
760 local info = dir_node_aux[s_dir][x_dir%dir_node_auto]
761 setfield(x, 'width', info.width(bw,bh,bd))
762 setfield(x, 'height', info.height(bw,bh,bd))
763 setfield(x, 'depth', info.depth(bw,bh,bd))
770 local function set_box_dim(key)
771 local n = tex_getcount('ltj@tempcnta')
774 local l_dir = (get_dir_count())%dir_math_mod
776 local b_dir = get_box_dir(s,dir_yoko)
777 if b_dir<dir_node_auto then
778 set_box_dim_common(key, s, l_dir)
779 elseif b_dir%dir_math_mod == l_dir then
781 setfield(s, key, tex.getdimen('ltj@tempdima'))
782 if b_dir<dir_node_manual then
783 set_attr(s, attr_dir, b_dir%dir_node_auto + dir_node_manual)
785 -- re-calculate shift and kern (TODO)
786 local sid, sl = getid(s), getlist(s)
787 local b = node_next(node_next(node_next(sl))) -- 本来の box
788 local info = dir_node_aux[get_box_dir(b,dir_yoko)%dir_math_mod][b_dir%dir_node_auto]
790 = getfield(b,'width'), getfield(b, 'height'), getfield(b, 'depth')
792 = getfield(s,'width'), getfield(s, 'height'), getfield(s, 'depth')
793 -- re-calculate shift and kern
794 for i,v in ipairs(info[sid]) do
795 if getid(sl)==id_kern then
796 setfield(sl, 'kern', v[2](bw,bh,bd,sw,sh,sd) )
797 elseif getid(sl)==sid then
798 setfield(sl, 'shift', v[2](bw,bh,bd,sw,sh,sd) )
803 local sid, sl = getid(s), getlist(s)
804 local b = node_next(node_next(node_next(sl))) -- 本来の box
805 local info = dir_node_aux[get_box_dir(b,dir_yoko)%dir_math_mod][b_dir%dir_node_auto]
807 = getfield(b,'width'), getfield(b, 'height'), getfield(b, 'depth')
809 = getfield(s,'width'), getfield(s, 'height'), getfield(s, 'depth')
810 if set_box_dim_common(key, b, l_dir) then
811 -- re-calculate shift and kern
812 for i,v in ipairs(info[sid]) do
813 if getid(sl)==id_kern then
814 setfield(sl, 'kern', v[2](bw,bh,bd,sw,sh,sd) )
815 elseif getid(sl)==sid then
816 setfield(sl, 'shift', v[2](bw,bh,bd,sw,sh,sd) )
820 -- re-calculate dimension of s, if s is created "automatically"
821 if b_dir<dir_node_manual then
822 setfield(s, 'width', info.width(bw,bh,bd))
823 setfield(s, 'height', info.height(bw,bh,bd))
824 setfield(s, 'depth', info.depth(bw,bh,bd))
830 luatexja.direction.set_box_dim = set_box_dim
834 local getbox = tex.getbox
835 local function get_register_dir(n)
839 local b_dir = get_box_dir(s, dir_yoko)
840 if b_dir<dir_node_auto then
843 local b_dir = get_box_dir(
844 node_next(node_next(node_next(getlist(s)))), dir_yoko)
851 luatexja.direction.get_register_dir = get_register_dir
855 local getbox, setbox, copy_list = tex.getbox, tex.setbox, Dnode.copy_list
857 function luatexja.direction.raise_box()
858 start_time_measure('box_primitive_hook')
859 local list_dir = get_dir_count()
860 local s = getbox('ltj@afbox')
862 local sd = to_direct(s)
863 local box_dir = get_box_dir(sd, dir_yoko)
864 if box_dir%dir_math_mod ~= list_dir then
868 copy_list(make_dir_whatsit(sd, sd, list_dir, 'box_move'))
869 -- without copy_list, we get a segfault
874 stop_time_measure('box_primitive_hook')
878 -- PACKED の hbox から文字を取り出す
879 -- luatexja.jfmglue.check_box などで使用
881 local function glyph_from_packed(h)
883 return (getid(b)==id_kern)
884 and node_next(node_next(node_next(node_next(b)))) or b
886 luatexja.direction.glyph_from_packed = glyph_from_packed
889 -- adjust and insertion
890 local id_adjust = node.id('adjust')
891 function luatexja.direction.check_adjust_direction()
892 start_time_measure('box_primitive_hook')
893 local list_dir = get_adjust_dir_count()
894 local a = tex_nest[tex_nest.ptr].tail
895 local ad = to_direct(a)
896 if a and getid(ad)==id_adjust then
897 local adj_dir = get_box_dir(ad)
898 if list_dir~=adj_dir then
901 'Direction Incompatible',
902 "\\vadjust's argument and outer vlist must have same direction.")
906 stop_time_measure('box_primitive_hook')
911 local split_dir_whatsit
912 local function dir_adjust_vpack(h, gc)
913 start_time_measure('direction_vpack')
914 local hd = to_direct(h)
915 if gc=='split_keep' then
916 -- supply dir_whatsit
917 hd = create_dir_whatsit_vbox(hd, gc)
918 split_dir_whatsit = hd
919 elseif gc=='split_off' then
922 if bh and getid(bh)==id_whatsit
923 and getsubtype(bh)==sid_user and getfield(bh, 'user_id')==DIR then
924 ltjs.list_dir = has_attr(bh, attr_dir); break
928 if split_dir_whatsit then
929 -- adjust direction of 'split_keep'
930 set_attr(split_dir_whatsit, attr_dir, ltjs.list_dir)
932 split_dir_whatsit=nil
933 elseif gc=='preamble' then
934 split_dir_whatsit=nil
937 hd = process_dir_node(create_dir_whatsit_vbox(hd, gc), gc)
938 split_dir_whatsit=nil
940 stop_time_measure('direction_vpack')
943 luatexbase.add_to_callback('vpack_filter',
945 'ltj.direction', 10000)
949 -- supply direction whatsit to the main vertical list "of the next page"
950 local function dir_adjust_pre_output(h, gc)
951 return to_node(create_dir_whatsit_vbox(to_direct(h), gc))
953 luatexbase.add_to_callback('pre_output_filter',
954 dir_adjust_pre_output,
955 'ltj.direction', 10000)
957 function luatexja.direction.remove_end_whatsit()
958 local h=tex.lists.page_head
960 h.id==id_whatsit and h.subtype==sid_user and
961 h.user_id == DIR then
962 tex.lists.page_head = nil
970 local function dir_adjust_buildpage(info)
972 local head = to_direct(tex.lists.contrib_head)
976 = make_dir_whatsit(head,
980 tex.lists.contrib_head = to_node(head)
984 luatexbase.add_to_callback('buildpage_filter',
985 dir_adjust_buildpage,
986 'ltj.direction', 10000)