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 to_node = node.direct.tonode
14 local to_direct = node.direct.todirect
15 local has_attr = node.direct.has_attribute
16 local set_attr = node.direct.set_attribute
17 local insert_before = node.direct.insert_before
18 local insert_after = node.direct.insert_after
19 local getid = node.direct.getid
20 local getsubtype = node.direct.getsubtype
21 local getlist = node.direct.getlist
22 local setfield = node.direct.setfield
23 local getfield = node.direct.getfield
24 local node_new = node.direct.new
25 local node_tail = node.direct.tail
26 local node_free = node.direct.free
27 local node_remove = node.direct.remove
28 local node_next = node.direct.getnext
29 local traverse = node.direct.traverse
30 local traverse_id = node.direct.traverse_id
31 local start_time_measure, stop_time_measure
32 = ltjb.start_time_measure, ltjb.stop_time_measure
35 local id_kern = node.id 'kern'
36 local id_hlist = node.id 'hlist'
37 local id_vlist = node.id 'vlist'
38 local id_whatsit = node.id 'whatsit'
39 local sid_save = node.subtype 'pdf_save'
40 local sid_restore= node.subtype 'pdf_restore'
41 local sid_matrix = node.subtype 'pdf_setmatrix'
42 local sid_user = node.subtype 'user_defined'
44 local tex_nest = tex.nest
45 local tex_getcount = tex.getcount
46 local ensure_tex_attr = ltjb.ensure_tex_attr
47 local PROCESSED = luatexja.icflag_table.PROCESSED
48 local PROCESSED_BEGIN_FLAG = luatexja.icflag_table.PROCESSED_BEGIN_FLAG
49 local PACKED = luatexja.icflag_table.PACKED
50 local DIR = luatexja.userid_table.DIR
51 local dir_tate = luatexja.dir_table.dir_tate
52 local dir_yoko = luatexja.dir_table.dir_yoko
53 local dir_dtou = luatexja.dir_table.dir_dtou
54 local dir_utod = luatexja.dir_table.dir_utod
55 local dir_math_mod = luatexja.dir_table.dir_math_mod
56 local dir_node_auto = luatexja.dir_table.dir_node_auto
57 local dir_node_manual = luatexja.dir_table.dir_node_manual
58 local function get_attr_icflag(p)
59 return (has_attr(p, attr_icflag) or 0) % PROCESSED_BEGIN_FLAG
66 local node_copy = node.direct.copy
68 for _,i in pairs({dir_tate, dir_yoko, dir_dtou, dir_utod}) do
69 local w = node_new(id_whatsit, sid_user)
70 set_attr(w, attr_dir, i)
71 setfield(w, 'user_id', DIR)
72 setfield(w, 'type', 110)
73 setfield(w, 'next', nil)
74 dir_pool[i] = function () return node_copy(w) end
79 local function adjust_badness(hd)
80 if not node_next(hd) and getid(hd)==id_whatsit and getsubtype(hd)==sid_user
81 and getfield(hd, 'user_id')==DIR then
82 -- avoid double whatsit
83 luatexja.global_temp=tex.globaldefs; tex.globaldefs=0
84 luatexja.hbadness_temp=tex.hbadness; tex.hbadness=10000
85 luatexja.vbadness_temp=tex.vbadness; tex.vbadness=10000
87 luatexja.global_temp = nil
88 luatexja.hbadness_temp=nil
89 luatexja.vbadness_temp=nil
93 local get_dir_count, get_adjust_dir_count
95 local node_attr = node.has_attribute
96 local function get_dir_count_inner(h)
98 if h.id==id_whatsit and h.subtype==sid_user and h.user_id==DIR then
99 return ((node_attr(h, attr_icflag) or 0)<PROCESSED_BEGIN_FLAG)
100 and (node_attr(h,attr_dir)%dir_node_auto) or 0
108 function get_dir_count()
109 for i=tex_nest.ptr, 1, -1 do
110 local h = tex_nest[i].head.next
112 local t = get_dir_count_inner(h)
113 if t~=0 then return t end
116 return page_direction
118 function get_adjust_dir_count()
119 for i=tex_nest.ptr, 1, -1 do
120 local v = tex_nest[i]
121 local h, m = v.head.next, v.mode
122 if abs(m)== ltjs.vmode and h then
123 local t = get_dir_count_inner(h)
124 if t~=0 then return t end
127 return page_direction
129 luatexja.direction.get_dir_count = get_dir_count
130 luatexja.direction.get_adjust_dir_count = get_adjust_dir_count
134 -- \tate, \yoko,\dtou, \utod
136 local node_next = node.next
137 local node_set_attr = node.set_attribute
138 local node_traverse = node.traverse
139 local STCK = luatexja.userid_table.STCK
140 local IHB = luatexja.userid_table.IHB
141 local id_local = node.id 'local_par'
142 local id_dir = node.id 'dir'
144 local function test_list(h, lv)
146 return 2 -- need to create dir_whatsit
148 local flag = 2 -- need to create dir_whatsit
150 for p in node_traverse(h) do
151 if p.id==id_whatsit then
152 if p.subtype==sid_user then
155 flag = 1; w = w or p -- found
156 elseif not(uid==IHB or uid==STCK) then
160 elseif p.id~=id_local and p.id~=id_dir then
164 if flag==1 then -- dir_whatsit already exists
171 local node_next_node, node_tail_node = node.next, node.tail
172 local insert_after_node = node.insert_after
173 function luatexja.direction.set_list_direction_hook(v)
174 local lv = tex_nest.ptr -- must be >= 1
177 if abs(tex_nest[lv-1].mode) == ltjs.mmode and v == dir_tate then
181 v = get_adjust_dir_count()
183 local h = tex_nest[lv].head
184 local hn = node.next(h)
185 hn = (hn and hn.id==id_local) and hn or h
186 local w = to_node(dir_pool[v]())
187 insert_after_node(h, hn, w)
188 tex_nest[lv].tail = node_tail_node(w)
189 ensure_tex_attr(attr_icflag, 0)
190 ensure_tex_attr(attr_dir, 0)
193 local function set_list_direction(v, name)
194 local lv = tex_nest.ptr
196 v,name = get_dir_count(), nil
197 if lv>=1 and abs(tex_nest[lv-1].mode) == ltjs.mmode and v == dir_tate then
201 v,name = get_adjust_dir_count(), nil
203 local current_nest = tex_nest[lv]
204 if tex.currentgrouptype==6 then
207 "You can't use `\\" .. name .. "' in an align",
208 "To change the direction in an align, \n"
209 .. "you shold use \\hbox or \\vbox.")
210 elseif current_nest.mode == ltjs.hmode or abs(current_nest.mode) == ltjs.mmode then
213 "Improper `\\" .. name .. "'",
214 'You cannot change the direction in unrestricted horizontal mode \n'
215 .. 'nor math modes.')
217 local h = (lv==0) and tex.lists.page_head or current_nest.head.next
218 local flag,w = test_list(h,lv)
220 if lv==0 and not page_direction then
221 page_direction = v -- for first call of \yoko (in luatexja-core.sty)
223 if luatexja.debug then
224 luatexja.ext_show_node_list(node.direct.tonode(h),'>> ', texio.write_nl)
228 "Use `\\" .. tostring(name) .. "' at top of list",
229 'Direction change command by LuaTeX-ja is available\n'
230 .. 'only when the current list is null.')
233 node_set_attr(w, attr_dir, v)
234 if lv==0 then page_direction = v end
237 else -- flag == 2: need to create dir whatsit.
238 local h = current_nest.head
239 local hn = node.next(h)
240 hn = (hn and hn.id==id_local) and hn or h
241 local w = to_node(dir_pool[v]())
242 insert_after_node(h,hn,w)
243 current_nest.tail = node_tail_node(w)
245 ensure_tex_attr(attr_icflag, 0)
247 ensure_tex_attr(attr_dir, 0)
249 luatexja.direction.set_list_direction = set_list_direction
252 -- ボックスに dir whatsit を追加
253 local function create_dir_whatsit(hd, gc, new_dir)
254 if getid(hd)==id_whatsit and
255 getsubtype(hd)==sid_user and getfield(hd, 'user_id')==DIR then
256 set_attr(hd, attr_icflag,
257 get_attr_icflag(hd) + PROCESSED_BEGIN_FLAG)
258 local n =node_next(hd)
260 set_attr(n, attr_icflag,
261 get_attr_icflag(n) + PROCESSED_BEGIN_FLAG)
263 ensure_tex_attr(attr_icflag, 0)
266 local w = dir_pool[new_dir]()
267 set_attr(w, attr_icflag, PROCESSED_BEGIN_FLAG)
268 set_attr(hd, attr_icflag,
269 get_attr_icflag(hd) + PROCESSED_BEGIN_FLAG)
270 ensure_tex_attr(attr_icflag, 0)
271 ensure_tex_attr(attr_dir, 0)
272 return insert_before(hd, hd, w)
276 -- hpack_filter, vpack_filter, post_line_break_filter
277 -- の結果を組方向を明示するため,先頭に dir_node を設置
280 local function create_dir_whatsit_hpack(h, gc)
281 local hd = to_direct(h)
282 if gc=='fin_row' then
284 for p in traverse_id(15, hd) do -- unset
285 if get_box_dir(p, 0)==0 then
286 setfield(p, 'head', create_dir_whatsit(getlist(p), 'fin_row', ltjs.list_dir))
289 set_attr(hd, attr_icflag, PROCESSED_BEGIN_FLAG)
290 ensure_tex_attr(attr_icflag, 0)
293 elseif gc == 'preamble' then
296 return to_node(create_dir_whatsit(hd, gc, ltjs.list_dir))
300 ltjb.add_to_callback('hpack_filter',
301 create_dir_whatsit_hpack, 'ltj.create_dir_whatsit', 10000)
305 local function create_dir_whatsit_parbox(h, gc)
306 stop_time_measure 'tex_linebreak';
307 -- start 側は ltj-debug.lua に
308 local new_dir = ltjs.list_dir
309 for line in traverse_id(id_hlist, to_direct(h)) do
310 setfield(line, 'head', create_dir_whatsit(getlist(line), gc, new_dir) )
312 ensure_tex_attr(attr_dir, 0)
315 ltjb.add_to_callback('post_linebreak_filter',
316 create_dir_whatsit_parbox, 'ltj.create_dir_whatsit', 10000)
319 local create_dir_whatsit_vbox
322 local id_glue = node.id 'glue'
323 create_dir_whatsit_vbox = function (hd, gc)
324 ltjs.list_dir = get_dir_count()
325 -- remove dir whatsit
326 for x in traverse_id(id_whatsit, hd) do
327 if getsubtype(x)==sid_user and getfield(x, 'user_id')==DIR then
332 ltjs.list_dir = has_attr(hd, attr_dir)
333 local x = node_next(hd)
334 while x and getid(x)==id_glue and getsubtype(x)==3 do
335 node_remove(hd,x); node_free(x); x = node_next(hd)
338 -- if #wh==1 then wh[1]=nil else wh[#wh], wh[1]=nil, wh[#wh] end
342 hd = node_remove(hd, wh[i]); node_free(wh[i]); wh[i] = nil
344 if gc=='fin_row' then -- gc == 'preamble' case is treated in dir_adjust_vpack
346 set_attr(hd, attr_icflag, PROCESSED_BEGIN_FLAG)
347 ensure_tex_attr(attr_icflag, 0)
350 elseif gc=='vtop' then
351 local n = node_next(hd)
352 local w = create_dir_whatsit(hd, gc, ltjs.list_dir)
353 -- move dir whatsit after hd
354 setfield(hd, 'next', w); setfield(w, 'next', n);
356 else return create_dir_whatsit(hd, gc, ltjs.list_dir)
361 -- dir_node に包む方法を書いたテーブル
364 local floor = math.floor
365 local get_h =function (w,h,d) return h end
366 local get_d =function (w,h,d) return d end
367 local get_h_d =function (w,h,d) return h+d end
368 local get_h_d_neg =function (w,h,d) return -h-d end
369 local get_d_neg =function (w,h,d) return -d end
370 local get_w_half =function (w,h,d) return floor(0.5*w) end
371 local get_w_half_rem =function (w,h,d) return w-floor(0.5*w) end
372 local get_w_neg =function (w,h,d) return -w end
373 local get_w =function (w,h,d) return w end
374 local zero = function() return 0 end
376 [dir_yoko] = { -- yoko を
377 [dir_tate] = { -- tate 中で組む
380 depth = get_w_half_rem,
382 { 'whatsit', sid_save },
383 { 'rotate', '0 1 -1 0' },
384 { 'kern', function(w,h,d,nw,nh,nd) return -nd end },
386 { 'kern', function(w,h,d,nw,nh,nd) return nd-w end },
387 { 'whatsit', sid_restore },
390 { 'whatsit', sid_save },
391 { 'rotate', '0 1 -1 0' },
393 { 'box' , function(w,h,d,nw,nh,nd) return -nh-nd end },
394 { 'kern', get_h_d_neg},
395 { 'whatsit', sid_restore },
398 [dir_dtou] = { -- dtou 中で組む
403 { 'whatsit', sid_save },
404 { 'rotate', '0 -1 1 0' },
405 { 'kern', function(w,h,d,nw,nh,nd) return -nh end },
406 { 'box', get_d_neg },
407 { 'kern', function(w,h,d,nw,nh,nd) return nh-w end },
408 { 'whatsit', sid_restore },
411 { 'whatsit', sid_save },
412 { 'rotate', '0 -1 1 0' },
413 { 'kern', get_h_d_neg },
415 { 'whatsit', sid_restore },
419 [dir_tate] = { -- tate を
420 [dir_yoko] = { -- yoko 中で組む
425 { 'whatsit', sid_save },
426 { 'rotate', '0 -1 1 0' },
427 { 'kern', function (w,h,d,nw,nh,nd) return -nh end },
428 { 'box' , get_d_neg },
429 { 'kern', function (w,h,d,nw,nh,nd) return nh-w end },
430 { 'whatsit', sid_restore },
433 { 'whatsit', sid_save },
434 { 'rotate', '0 -1 1 0' },
435 { 'kern', get_h_d_neg },
437 { 'whatsit', sid_restore },
440 [dir_dtou] = { -- dtou 中で組む
445 { 'whatsit', sid_save },
446 { 'rotate', '-1 0 0 -1' },
447 { 'kern', get_w_neg },
448 { 'box', function (w,h,d,nw,nh,nd) return h-nd end },
449 { 'whatsit', sid_restore },
452 { 'whatsit', sid_save },
453 { 'rotate', '-1 0 0 -1' },
454 { 'kern', get_h_d_neg },
455 { 'box', get_w_neg },
456 { 'whatsit', sid_restore },
460 [dir_dtou] = { -- dtou を
461 [dir_yoko] = { -- yoko 中で組む
466 { 'whatsit', sid_save },
467 { 'rotate', '0 1 -1 0' },
468 { 'kern', function (w,h,d,nw,nh,nd) return -nd end },
470 { 'kern', function (w,h,d,nw,nh,nd) return nd-w end },
471 { 'whatsit', sid_restore },
475 { 'whatsit', sid_save },
476 { 'rotate', '0 1 -1 0' },
477 { 'box', function (w,h,d,nw,nh,nd) return -nd-nh end },
478 { 'kern', get_h_d_neg },
479 { 'whatsit', sid_restore },
482 [dir_tate] = { -- tate 中で組む
487 { 'whatsit', sid_save },
488 { 'rotate', '-1 0 0 -1' },
489 { 'kern', get_w_neg },
490 { 'box', function (w,h,d,nw,nh,nd) return h-nd end },
491 { 'whatsit', sid_restore },
494 { 'whatsit', sid_save },
495 { 'rotate', ' -1 0 0 -1' },
496 { 'kern', function (w,h,d,nw,nh,nd) return -nh-nd end },
497 { 'box', get_w_neg },
498 { 'kern', function (w,h,d,nw,nh,nd) return nh+nd-h-d end },
499 { 'whatsit', sid_restore },
506 -- 1st ret val: b の組方向
507 -- 2nd ret val はその DIR whatsit
508 function get_box_dir(b, default)
509 start_time_measure 'get_box_dir'
510 local dir = has_attr(b, attr_dir) or 0
511 local bh = getfield(b,'head')
512 -- b は insert node となりうるので getlist() は使えない
514 if bh~=0 then -- bh != nil
515 for bh in traverse_id(id_whatsit, bh) do
516 if getsubtype(bh)==sid_user and getfield(bh, 'user_id')==DIR then
517 c = bh; dir = (dir==0) and has_attr(bh, attr_dir) or dir
521 stop_time_measure 'get_box_dir'
522 return (dir==0 and default or dir), c
526 local getbox = tex.getbox
528 function luatexja.direction.unbox_check_dir(is_copy)
529 start_time_measure 'box_primitive_hook'
530 local list_dir = get_dir_count()%dir_math_mod
531 local b = getbox(tex_getcount 'ltj@tempcnta')
532 if b and getlist(to_direct(b)) then
533 local box_dir = get_box_dir(to_direct(b), dir_yoko)
534 if box_dir%dir_math_mod ~= list_dir then
537 "Incompatible direction list can't be unboxed",
538 'I refuse to unbox a box in differrent direction.')
539 tex.sprint(cat_lp, '\\@gobbletwo')
542 local bd = to_direct(b)
543 local hd = getlist(bd)
546 if getid(hd)==id_whatsit and getsubtype(hd)==sid_user
547 and getfield(hd, 'user_id')==DIR then
549 nh, hd = node_remove(nh, hd)
550 if is_copy==true and (not dir_backup) then
551 dir_backup = d; setfield(dir_backup, 'next', nil)
559 setfield(bd, 'head', nh)
562 if luatexja.global_temp and tex.globaldefs~=luatexja.global_temp then
563 tex.globaldefs = luatexja.global_temp
565 stop_time_measure 'box_primitive_hook'
567 function luatexja.direction.uncopy_restore_whatsit()
568 local b = getbox(tex_getcount 'ltj@tempcnta')
570 local bd = to_direct(b)
572 setfield(dir_backup, 'next', getlist(bd))
573 setfield(bd, 'head', dir_backup)
580 -- dir_node に包まれている「本来の中身」を取り出し,
582 local function unwrap_dir_node(b, head, box_dir)
583 -- b: dir_node, head: the head of list, box_dir:
584 -- return values are (new head), (next of b), (contents), (dir of contents)
585 local bh = getlist(b)
588 nh = insert_before(head, b, bh)
589 nh, nb = node_remove(nh, b)
590 setfield(b, 'next', nil)
593 local shift_old, b_dir, wh = nil, get_box_dir(bh, 0)
595 node.direct.flush_list(getfield(wh, 'value'))
596 setfield(wh, 'value', nil)
598 return nh, nb, bh, b_dir
601 -- is_manual: 寸法変更に伴うものか?
602 local function create_dir_node(b, b_dir, new_dir, is_manual)
603 local info = dir_node_aux[b_dir%dir_math_mod][new_dir%dir_math_mod]
604 local w = getfield(b, 'width')
605 local h = getfield(b, 'height')
606 local d = getfield(b, 'depth')
607 local db = node_new(getid(b)) -- dir_node
608 set_attr(db, attr_dir,
609 new_dir + (is_manual and dir_node_manual or dir_node_auto))
610 set_attr(db, attr_icflag, PROCESSED)
611 set_attr(b, attr_icflag, PROCESSED)
612 ensure_tex_attr(attr_dir, 0)
613 ensure_tex_attr(attr_icflag, 0)
614 setfield(db, 'dir', getfield(b, 'dir'))
615 setfield(db, 'shift', 0)
616 setfield(db, 'width', info.width(w,h,d))
617 setfield(db, 'height', info.height(w,h,d))
618 setfield(db, 'depth', info.depth(w,h,d))
623 local make_dir_whatsit, process_dir_node
625 make_dir_whatsit = function (head, b, new_dir, origin)
626 new_dir = new_dir%dir_math_mod
627 -- head: list head, b: box
628 -- origin: コール元 (for debug)
629 -- return value: (new head), (next of b), (new b), (is_b_dir_node)
630 -- (new b): b か dir_node に被せられた b
631 local bh = getlist(b)
632 local box_dir, dn = get_box_dir(b, ltjs.list_dir)
633 -- 既に b の中身にあるwhatsit
634 if (box_dir<dir_node_auto) and (not dn) then
635 bh = create_dir_whatsit(bh, 'make_dir_whatsit', dir_yoko)
636 dn = bh; setfield(b, 'head', bh)
638 if box_dir%dir_math_mod==new_dir then
639 if box_dir>=dir_node_auto then
640 -- dir_node としてカプセル化されている
641 local _, dnc = get_box_dir(b, 0)
642 if dnc then -- free all other dir_node
643 node.direct.flush_list(getfield(dnc, 'value'))
644 setfield(dnc, 'value', nil)
646 set_attr(b, attr_dir, box_dir%dir_math_mod + dir_node_auto)
647 return head, node_next(b), b, true
649 -- 組方向が一緒 (up to math dir) のボックスなので,何もしなくて良い
650 return head, node_next(b), b, false
654 local nh, nb, ret, flag
655 if box_dir>= dir_node_auto then -- unwrap
657 head, nb, b, b_dir = unwrap_dir_node(b, head, box_dir)
659 if b_dir%dir_math_mod==new_dir then
660 -- dir_node の中身が周囲の組方向とあっている
661 return head, nb, b, false
662 else box_dir = b_dir end
664 box_dir = box_dir%dir_math_mod
666 local dnh = getfield(dn, 'value')
667 for x in traverse(dnh) do
668 if has_attr(x, attr_dir)%dir_math_mod == new_dir then
669 setfield(dn, 'value', to_node(node_remove(dnh, x)))
673 node.direct.flush_list(getfield(dn, 'value'))
674 setfield(dn, 'value', nil)
675 db = db or create_dir_node(b, box_dir, new_dir, false)
676 local w = getfield(b, 'width')
677 local h = getfield(b, 'height')
678 local d = getfield(b, 'depth')
679 local dn_w = getfield(db, 'width')
680 local dn_h = getfield(db, 'height')
681 local dn_d = getfield(db, 'depth')
682 nh, nb = insert_before(head, b, db), nil
683 nh, nb = node_remove(nh, b)
684 setfield(b, 'next', nil); setfield(db, 'head', b)
686 return nh, nb, ret, flag
689 process_dir_node = function (hd, gc)
690 local x, new_dir = hd, ltjs.list_dir or dir_yoko
693 if (xid==id_hlist and get_attr_icflag(x)~=PACKED)
694 or xid==id_vlist then
695 hd, x = make_dir_whatsit(hd, x, new_dir, 'process_dir_node:' .. gc)
704 local node_prev = (node.direct~=node) and node.direct.getprev or node.prev
705 local id_glue = node.id 'glue'
706 local function lastbox_hook()
707 start_time_measure 'box_primitive_hook'
708 local bn = tex_nest[tex_nest.ptr].tail
710 local b, head = to_direct(bn), to_direct(tex_nest[tex_nest.ptr].head)
712 if bid==id_hlist or bid==id_vlist then
714 -- alignment の各行の中身が入ったボックス
715 if p and getid(p)==id_glue and getsubtype(p)==12 then -- tabskip
716 local np = node_next(p); local npid = getid(np)
717 if npid==id_hlist or npid==id_vlist then
718 setfield(b, 'head', create_dir_whatsit(p, 'align', get_box_dir(np, 0)))
721 local box_dir = get_box_dir(b, 0)
722 if box_dir>= dir_node_auto then -- unwrap dir_node
723 local p = node_prev(b)
724 local dummy1, dummy2, nb = unwrap_dir_node(b, nil, box_dir)
725 setfield(p, 'next', nb); tex_nest[tex_nest.ptr].tail = to_node(nb)
726 setfield(b, 'next', nil); setfield(b, 'head', nil)
729 local _, wh = get_box_dir(b, 0) -- clean dir_node attached to the box
731 node.direct.flush_list(getfield(wh, 'value'))
732 setfield(wh, 'value', nil)
736 stop_time_measure 'box_primitive_hook'
739 luatexja.direction.make_dir_whatsit = make_dir_whatsit
740 luatexja.direction.lastbox_hook = lastbox_hook
743 -- \wd, \ht, \dp の代わり
745 local getbox, setdimen = tex.getbox, tex.setdimen
746 local function get_box_dim_common(key, s, l_dir)
748 local s_dir, wh = get_box_dir(s, dir_yoko)
749 s_dir = s_dir%dir_math_mod
750 if s_dir ~= l_dir then
751 local not_found = true
752 for x in traverse(getfield(wh, 'value')) do
753 if l_dir == has_attr(x, attr_dir)%dir_node_auto then
754 setdimen('ltj@tempdima', getfield(x, key))
755 not_found = false; break
759 local w = getfield(s, 'width')
760 local h = getfield(s, 'height')
761 local d = getfield(s, 'depth')
762 setdimen('ltj@tempdima',
763 dir_node_aux[s_dir][l_dir][key](w,h,d))
766 setdimen('ltj@tempdima', getfield(s, key))
769 local function get_box_dim(key, n)
770 local gt = tex.globaldefs; tex.globaldefs = 0
773 local l_dir = (get_dir_count())%dir_math_mod
775 local b_dir = get_box_dir(s,dir_yoko)
776 if b_dir<dir_node_auto then
777 get_box_dim_common(key, s, l_dir)
778 elseif b_dir%dir_math_mod==l_dir then
779 setdimen('ltj@tempdima', getfield(s, key))
781 get_box_dim_common(key, getlist(s), l_dir)
784 setdimen('ltj@tempdima', 0)
786 tex.sprint(cat_lp, '\\ltj@tempdima')
789 luatexja.direction.get_box_dim = get_box_dim
791 -- return value: (changed dimen of box itself?)
792 local scan_dimen, scan_int = token.scan_dimen, token.scan_int
793 local scan_keyword = token.scan_keyword
794 local function set_box_dim_common(key, s, l_dir)
795 local s_dir, wh = get_box_dir(s, dir_yoko)
796 s_dir = s_dir%dir_math_mod
797 if s_dir ~= l_dir then
799 wh = create_dir_whatsit(getlist(s), 'set_box_dim', s_dir)
800 setfield(s, 'head', wh)
803 local dnh = getfield(wh, 'value')
804 for x in traverse(dnh) do
805 if has_attr(x, attr_dir)%dir_node_auto==l_dir then
810 db = create_dir_node(s, s_dir, l_dir, true)
811 setfield(db, 'next', dnh)
812 setfield(wh, 'value',to_node(db))
814 setfield(db, key, scan_dimen())
817 setfield(s, key, scan_dimen())
819 -- change dimension of dir_nodes which are created "automatically"
821 = getfield(s,'width'), getfield(s, 'height'), getfield(s, 'depth')
822 for x in traverse(getfield(wh, 'value')) do
823 local x_dir = has_attr(x, attr_dir)
824 if x_dir<dir_node_manual then
825 local info = dir_node_aux[s_dir][x_dir%dir_node_auto]
826 setfield(x, 'width', info.width(bw,bh,bd))
827 setfield(x, 'height', info.height(bw,bh,bd))
828 setfield(x, 'depth', info.depth(bw,bh,bd))
835 local function set_box_dim(key)
836 local s = getbox(scan_int()); scan_keyword('=')
838 local l_dir = (get_dir_count())%dir_math_mod
840 local b_dir = get_box_dir(s,dir_yoko)
841 if b_dir<dir_node_auto then
842 set_box_dim_common(key, s, l_dir)
843 elseif b_dir%dir_math_mod == l_dir then
845 setfield(s, key, scan_dimen())
846 if b_dir<dir_node_manual then
847 set_attr(s, attr_dir, b_dir%dir_node_auto + dir_node_manual)
850 local sid, b = getid(s), getlist(s)
851 local info = dir_node_aux[get_box_dir(b,dir_yoko)%dir_math_mod][b_dir%dir_node_auto]
853 = getfield(b,'width'), getfield(b, 'height'), getfield(b, 'depth')
855 = getfield(s,'width'), getfield(s, 'height'), getfield(s, 'depth')
856 if set_box_dim_common(key, b, l_dir) and b_dir<dir_node_manual then
857 -- re-calculate dimension of s, if s is created "automatically"
858 if b_dir<dir_node_manual then
859 setfield(s, 'width', info.width(bw,bh,bd))
860 setfield(s, 'height', info.height(bw,bh,bd))
861 setfield(s, 'depth', info.depth(bw,bh,bd))
867 luatexja.direction.set_box_dim = set_box_dim
871 local getbox = tex.getbox
872 local function get_register_dir(n)
876 local b_dir = get_box_dir(s, dir_yoko)
877 if b_dir<dir_node_auto then
880 local b_dir = get_box_dir(
881 node_next(node_next(node_next(getlist(s)))), dir_yoko)
888 luatexja.direction.get_register_dir = get_register_dir
892 local getbox, setbox, copy_list = tex.getbox, tex.setbox, node.direct.copy_list
894 function luatexja.direction.raise_box()
895 start_time_measure 'box_primitive_hook'
896 local list_dir = get_dir_count()
897 local s = getbox 'ltj@afbox'
899 local sd = to_direct(s)
900 local box_dir = get_box_dir(sd, dir_yoko)
901 if box_dir%dir_math_mod ~= list_dir then
903 to_node(copy_list(make_dir_whatsit(sd, sd, list_dir, 'box_move')))
904 -- copy_list しないとリストの整合性が崩れる……?
908 stop_time_measure 'box_primitive_hook'
912 -- PACKED の hbox から文字を取り出す
913 -- luatexja.jfmglue.check_box などで使用
915 local function glyph_from_packed(h)
917 return (getid(b)==id_kern or (getid(b)==id_whatsit and getsubtype(b)==sid_save) )
918 and node_next(node_next(node_next(b))) or b
920 luatexja.direction.glyph_from_packed = glyph_from_packed
925 local id_adjust = node.id 'adjust'
926 local scan_keyword = token.scan_keyword
927 function luatexja.direction.adjust_begin()
928 if scan_keyword 'pre' then tex.sprint(cat_lp, '\\ltj@@vadjust@pre')
929 else tex.sprint(cat_lp, '\\ltj@@vadjust@post') end
931 function luatexja.direction.check_adjust_direction()
932 start_time_measure 'box_primitive_hook'
933 local list_dir = get_adjust_dir_count()
934 local a = tex_nest[tex_nest.ptr].tail
935 local ad = to_direct(a)
936 if a and getid(ad)==id_adjust then
937 local adj_dir = get_box_dir(ad)
938 if list_dir~=adj_dir then
941 'Direction Incompatible',
942 "\\vadjust's argument and outer vlist must have same direction.")
943 node.direct.last_node()
946 stop_time_measure 'box_primitive_hook'
952 local id_ins = node.id 'ins'
953 local id_rule = node.id 'rule'
954 function luatexja.direction.populate_insertion_dir_whatsit()
955 start_time_measure 'box_primitive_hook'
956 local list_dir = get_dir_count()
957 local a = tex_nest[tex_nest.ptr].tail
958 local ad = to_direct(a)
959 if (not a) or getid(ad)~=id_ins then
960 a = node.tail(tex.lists.page_head); ad = to_direct(a)
962 if a and getid(ad)==id_ins then
963 local h = getfield(ad, 'head')
964 if getid(h)==id_whatsit and
965 getsubtype(h)==sid_user and getfield(h, 'user_id')==DIR then
966 local n = h; h = node_remove(h,h)
969 for box_rule in traverse(h) do
970 if getid(box_rule)<id_rule then
971 h = insert_before(h, box_rule, dir_pool[list_dir]())
974 ensure_tex_attr(attr_dir, 0)
975 setfield(ad, 'head', h)
977 stop_time_measure 'box_primitive_hook'
983 local split_dir_whatsit, split_dir_head
984 local cat_lp = luatexbase.catcodetables['latex-package']
985 local sprint, scan_int, tex_getbox = tex.sprint, token.scan_int, tex.getbox
986 function luatexja.direction.vsplit()
987 local n = scan_int();
988 local p = to_direct(tex_getbox(n))
989 if split_dir_head then node_free(split_dir_head); split_dir_head = nil end
990 if split_dir_whatsit then split_dir_watsit = nil end
991 split_dir_head = nil; split_dir_whatsit=nil
993 local bh = getlist(p)
994 if getid(bh)==id_whatsit and getsubtype(bh)==sid_user and getfield(bh, 'user_id')==DIR
995 and node_next(bh) then
996 ltjs.list_dir = has_attr(bh, attr_dir)
997 local q = node_next(p)
998 setfield(p, 'head', node_remove(bh,bh,bh))
1002 sprint(cat_lp, '\\ltj@@orig@vsplit' .. tostring(n))
1004 local function dir_adjust_vpack(h, gc)
1005 start_time_measure 'direction_vpack'
1006 local hd = to_direct(h)
1007 if gc=='split_keep' then
1008 -- supply dir_whatsit
1009 hd = create_dir_whatsit_vbox(hd, gc)
1010 split_dir_whatsit = hd
1011 elseif gc=='split_off' then
1012 if split_dir_head then
1013 ltjs.list_dir = has_attr(split_dir_head, attr_dir)
1014 hd = insert_before(hd, hd, split_dir_head)
1017 if split_dir_whatsit then
1018 -- adjust direction of 'split_keep'
1019 set_attr(split_dir_whatsit, attr_dir, ltjs.list_dir)
1020 split_dir_whatsit=nil
1022 elseif gc=='preamble' then
1023 split_dir_whatsit=nil
1026 -- hd = process_dir_node(create_dir_whatsit_vbox(hd, gc), gc)
1027 -- done in append_to_vpack callback
1028 hd = create_dir_whatsit_vbox(hd, gc)
1029 split_dir_whatsit=nil
1031 stop_time_measure 'direction_vpack'
1034 ltjb.add_to_callback('vpack_filter',
1036 'ltj.direction', 10000)
1040 local function dir_adjust_pre_output(h, gc)
1041 return to_node(create_dir_whatsit_vbox(to_direct(h), gc))
1043 ltjb.add_to_callback('pre_output_filter', dir_adjust_pre_output,
1044 'ltj.direction', 10000)
1047 -- append_to_vlist filter: done in ltj-lineskip.lua
1049 -- finalize (executed just before \shipout)
1050 -- we supply correct pdfsavematrix nodes etc. inside dir_node
1052 local finalize_inner
1053 local function finalize_dir_node(db,new_dir)
1054 local b = getlist(db)
1055 while b and ((getid(b)~=id_hlist) and (getid(b)~=id_vlist)) do
1056 local ob = b; b = node_remove(b,b); setfield(db, 'head', b);
1060 local w = getfield(b, 'width')
1061 local h = getfield(b, 'height')
1062 local d = getfield(b, 'depth')
1063 local dn_w = getfield(db, 'width')
1064 local dn_h = getfield(db, 'height')
1065 local dn_d = getfield(db, 'depth')
1066 local db_head, db_tail
1067 local t = dir_node_aux[get_box_dir(b, dir_yoko)%dir_math_mod][new_dir]
1068 t = t and t[getid(b)]; if not t then return end
1069 for _,v in ipairs(t) do
1070 local cmd, arg, nn = v[1], v[2]
1072 nn = node_new(id_kern, 1)
1073 setfield(nn, 'kern', arg(w, h, d, dn_w, dn_h, dn_d))
1074 elseif cmd=='whatsit' then
1075 nn = node_new(id_whatsit, arg)
1076 elseif cmd=='rotate' then
1077 nn = node_new(id_whatsit, sid_matrix)
1078 setfield(nn, 'data', arg)
1079 elseif cmd=='box' then
1080 nn = b; setfield(b, 'next', nil)
1081 setfield(nn, 'shift', arg(w, h, d, dn_w, dn_h, dn_d))
1084 insert_after(db_head, db_tail, nn)
1087 setfield(db, 'head', nn)
1088 db_head, db_tail = nn, nn
1093 tex.setattribute(attr_dir, dir_yoko)
1094 local shipout_temp = node_new(id_hlist)
1095 tex.setattribute(attr_dir, 0)
1097 finalize_inner = function (box)
1098 for n in traverse(getlist(box)) do
1099 local nid = getid(n)
1100 if (nid==id_hlist or nid==id_vlist) then
1101 local ndir = get_box_dir(n, dir_yoko)
1102 if ndir>=dir_node_auto then -- n is dir_node
1103 finalize_dir_node(n, ndir%dir_math_mod)
1110 local copy = node.direct.copy
1111 function luatexja.direction.shipout_lthook (head)
1112 start_time_measure 'box_primitive_hook'
1113 local a = to_direct(head)
1114 local a_dir = get_box_dir(a, dir_yoko)
1115 if a_dir~=dir_yoko then
1116 local b = create_dir_node(a, a_dir, dir_yoko, false)
1117 setfield(b, 'head', a); a = b
1119 setfield(shipout_temp, 'head', a); finalize_inner(shipout_temp)
1120 a = copy(getlist(shipout_temp)); setfield(shipout_temp, 'head',nil)
1121 stop_time_measure 'box_primitive_hook'