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 PROCESSED = luatexja.icflag_table.PROCESSED
49 local PROCESSED_BEGIN_FLAG = luatexja.icflag_table.PROCESSED_BEGIN_FLAG
50 local PACKED = luatexja.icflag_table.PACKED
51 local DIR = luatexja.userid_table.DIR
52 local dir_tate = luatexja.dir_table.dir_tate
53 local dir_yoko = luatexja.dir_table.dir_yoko
54 local dir_dtou = luatexja.dir_table.dir_dtou
55 local dir_utod = luatexja.dir_table.dir_utod
56 local dir_math_mod = luatexja.dir_table.dir_math_mod
57 local dir_node_auto = luatexja.dir_table.dir_node_auto
58 local dir_node_manual = luatexja.dir_table.dir_node_manual
59 local function get_attr_icflag(p)
60 return (has_attr(p, attr_icflag) or 0) % PROCESSED_BEGIN_FLAG
67 local tex_set_attr = tex.setattribute
68 local tex_get_attr = tex.getattribute
69 function ensure_tex_attr(a, v)
70 if tex_get_attr(a)~=v then
71 tex_set_attr('global', a, v)
76 local function adjust_badness(hd)
77 if not node_next(hd) and getid(hd)==id_whatsit and getsubtype(hd)==sid_user
78 and getfield(hd, 'user_id')==DIR then
79 -- avoid double whatsit
80 luatexja.global_temp=tex.globaldefs; tex.globaldefs=0
81 luatexja.hbadness_temp=tex.hbadness; tex.hbadness=10000
82 luatexja.vbadness_temp=tex.vbadness; tex.vbadness=10000
84 luatexja.global_temp = nil
85 luatexja.hbadness_temp=nil
86 luatexja.vbadness_temp=nil
90 local get_dir_count, get_adjust_dir_count
92 local function get_dir_count_inner(h)
94 if h.id==id_whatsit and h.subtype==sid_user and h.user_id==DIR then
95 local ic = node.has_attribute(h, attr_icflag) or 0
96 return (ic<PROCESSED_BEGIN_FLAG)
97 and (node.has_attribute(h,attr_dir)%dir_node_auto) or 0
105 function get_dir_count()
106 for i=tex_nest.ptr, 1, -1 do
107 local h = tex_nest[i].head.next
109 local t = get_dir_count_inner(h)
110 if t~=0 then return t end
113 return page_direction
115 function get_adjust_dir_count()
116 for i=tex_nest.ptr, 1, -1 do
117 local v = tex_nest[i]
118 local h, m = v.head.next, v.mode
119 if abs(m)== ltjs.vmode and h then
120 local t = get_dir_count_inner(h)
121 if t~=0 then return t end
124 return page_direction
126 luatexja.direction.get_dir_count = get_dir_count
127 luatexja.direction.get_adjust_dir_count = get_adjust_dir_count
131 -- \tate, \yoko,\dtou, \utod
133 local node_next = node.next
134 local node_set_attr = node.set_attribute
135 local node_traverse = node.traverse
136 local STCK = luatexja.userid_table.STCK
137 local IHB = luatexja.userid_table.IHB
139 local function test_list(h, lv)
141 return 2 -- need to create dir_whatsit
143 local flag = 2 -- need to create dir_whatsit
145 for p in node_traverse(h) do
146 if p.id==id_whatsit then
147 if p.subtype==sid_user then
150 flag = 1; w = w or p -- found
151 elseif not(uid==IHB or uid==STCK) then
152 flag = 0; break -- error
161 if flag==1 then -- dir_whatsit already exists
168 function luatexja.direction.set_list_direction_hook(v)
169 local lv = tex_nest.ptr -- must be >= 1
172 if abs(tex_nest[lv-1].mode) == ltjs.mmode and v == dir_tate then
176 v = get_adjust_dir_count()
178 local h = to_direct(tex_nest[lv].head)
179 local w = node_new(id_whatsit, sid_user)
180 setfield(w, 'next', nil)
181 setfield(w, 'user_id', DIR)
182 setfield(w, 'type', 110)
183 set_attr(w, attr_dir, v)
184 insert_after(h, h, w)
185 tex_nest[lv].tail = to_node(node_tail(w))
186 ensure_tex_attr(attr_icflag, 0)
187 ensure_tex_attr(attr_dir, 0)
190 local function set_list_direction(v, name)
191 local lv = tex_nest.ptr
193 v,name = get_dir_count(), nil
194 if lv>=1 and abs(tex_nest[lv-1].mode) == ltjs.mmode and v == dir_tate then
198 v,name = get_adjust_dir_count(), nil
200 if tex.currentgrouptype==6 then
203 "You can't use `\\" .. name .. "' in an align",
204 "To change direction in an align, \n"
205 .. "you shold use \\hbox or \\vbox.")
207 local h = (lv==0) and tex.lists.page_head or tex_nest[lv].head.next
208 local flag,w = test_list(h,lv)
210 if lv==0 and not page_direction then
211 page_direction = v -- for first call of \yoko (in luatexja-core.sty)
215 "Use `\\" .. tostring(name) .. "' at top of list",
216 'Direction change command by LuaTeX-ja is available\n'
217 .. 'only when the current list is null.')
220 node_set_attr(w, attr_dir, v)
221 if lv==0 then page_direction = v end
223 local w = node_new(id_whatsit, sid_user)
224 setfield(w, 'next', nil)
225 setfield(w, 'user_id', DIR)
226 setfield(w, 'type', 110)
227 set_attr(w, attr_dir, v)
229 if lv==0 then page_direction = v end
231 ensure_tex_attr(attr_icflag, 0)
233 ensure_tex_attr(attr_dir, 0)
235 luatexja.direction.set_list_direction = set_list_direction
238 -- ボックスに dir whatsit を追加
239 local function create_dir_whatsit(hd, gc, new_dir)
240 if getid(hd)==id_whatsit and
241 getsubtype(hd)==sid_user and getfield(hd, 'user_id')==DIR then
242 set_attr(hd, attr_icflag,
243 get_attr_icflag(hd) + PROCESSED_BEGIN_FLAG)
244 local n = node_next(hd)
246 set_attr(n, attr_icflag,
247 get_attr_icflag(n) + PROCESSED_BEGIN_FLAG)
249 ensure_tex_attr(attr_icflag, 0)
252 local w = node_new(id_whatsit, sid_user)
253 setfield(w, 'next', hd)
254 setfield(w, 'user_id', DIR)
255 setfield(w, 'type', 110)
256 set_attr(w, attr_dir, new_dir)
257 set_attr(w, attr_icflag, PROCESSED_BEGIN_FLAG)
258 set_attr(hd, attr_icflag,
259 (has_attr(hd, attr_icflag) or 0)%PROCESSED_BEGIN_FLAG
260 + PROCESSED_BEGIN_FLAG)
261 set_attr(hd, attr_icflag,
262 get_attr_icflag(hd) + PROCESSED_BEGIN_FLAG)
263 ensure_tex_attr(attr_icflag, 0)
264 ensure_tex_attr(attr_dir, 0)
269 -- hpack_filter, vpack_filter, post_line_break_filter
270 -- の結果を組方向を明示するため,先頭に dir_node を設置
272 local function create_dir_whatsit_hpack(h, gc)
273 local hd = to_direct(h)
274 if gc=='fin_row' or gc == 'preamble' then
276 set_attr(hd, attr_icflag, PROCESSED_BEGIN_FLAG)
277 ensure_tex_attr(attr_icflag, 0)
282 return to_node(create_dir_whatsit(hd, gc, ltjs.list_dir))
286 luatexbase.add_to_callback('hpack_filter',
287 create_dir_whatsit_hpack, 'ltj.create_dir_whatsit', 10000)
291 local function create_dir_whatsit_parbox(h, gc)
292 stop_time_measure('tex_linebreak')
293 -- start 側は ltj-debug.lua に
294 local new_dir, hd = ltjs.list_dir, to_direct(h)
295 for line in traverse_id(id_hlist, hd) do
296 local nh = getlist(line)
297 setfield(line, 'head', create_dir_whatsit(nh, gc, new_dir) )
298 --set_attr(line, attr_dir, new_dir)
300 ensure_tex_attr(attr_dir, 0)
303 luatexbase.add_to_callback('post_linebreak_filter',
304 create_dir_whatsit_parbox, 'ltj.create_dir_whatsit', 10000)
307 local create_dir_whatsit_vbox
310 local id_glue, sid_parskip = node.id('glue'), 3
311 create_dir_whatsit_vbox = function (hd, gc)
312 ltjs.list_dir = get_dir_count()
313 -- remove dir whatsit
314 for x in traverse_id(id_whatsit, hd) do
315 if getsubtype(x)==sid_user and getfield(x, 'user_id')==DIR then
320 ltjs.list_dir =has_attr(hd,attr_dir)
321 local x = node_next(hd)
322 if getid(x)==id_glue and getsubtype(x)==sid_parskip then
323 node_remove(hd,x); node_free(x)
327 hd = node_remove(hd, wh[i]); node_free(wh[i]); wh[i] = nil
329 if gc=='fin_row' then -- gc == 'preamble' case is treated in dir_adjust_vpack
331 set_attr(hd, attr_icflag, PROCESSED_BEGIN_FLAG)
332 ensure_tex_attr(attr_icflag, 0)
336 local n =node_next(hd)
338 local w = create_dir_whatsit(hd, gc, ltjs.list_dir)
339 -- move dir whatsit after hd
340 setfield(hd, 'next', w); setfield(w, 'next', n)
343 hd = create_dir_whatsit(hd, gc, ltjs.list_dir)
350 -- dir_node に包む方法を書いたテーブル
353 local floor = math.floor
354 local get_h =function (w,h,d) return h end
355 local get_d =function (w,h,d) return d end
356 local get_h_d =function (w,h,d) return h+d end
357 local get_h_d_neg =function (w,h,d) return -h-d end
358 local get_d_neg =function (w,h,d) return -d end
359 local get_w_half =function (w,h,d) return floor(0.5*w) end
360 local get_w_half_rem =function (w,h,d) return w-floor(0.5*w) end
361 local get_w_neg =function (w,h,d) return -w end
362 local get_w =function (w,h,d) return w end
363 local zero = function() return 0 end
365 [dir_yoko] = { -- yoko を
366 [dir_tate] = { -- tate 中で組む
369 depth = get_w_half_rem,
371 { 'whatsit', sid_save },
372 { 'rotate', '0 1 -1 0' },
373 { 'kern', function(w,h,d,nw,nh,nd) return -nd end },
375 { 'kern', function(w,h,d,nw,nh,nd) return nd-w end },
376 { 'whatsit', sid_restore },
379 { 'whatsit', sid_save },
380 { 'rotate', '0 1 -1 0' },
382 { 'box' , function(w,h,d,nw,nh,nd) return -nh-nd end },
383 { 'kern', get_h_d_neg},
384 { 'whatsit', sid_restore },
387 [dir_dtou] = { -- dtou 中で組む
392 { 'whatsit', sid_save },
393 { 'rotate', '0 -1 1 0' },
394 { 'kern', function(w,h,d,nw,nh,nd) return -nh end },
395 { 'box', get_d_neg },
396 { 'kern', function(w,h,d,nw,nh,nd) return nh-w end },
397 { 'whatsit', sid_restore },
400 { 'whatsit', sid_save },
401 { 'rotate', '0 -1 1 0' },
402 { 'kern', get_h_d_neg },
404 { 'whatsit', sid_restore },
408 [dir_tate] = { -- tate を
409 [dir_yoko] = { -- yoko 中で組む
414 { 'whatsit', sid_save },
415 { 'rotate', '0 -1 1 0' },
416 { 'kern', function (w,h,d,nw,nh,nd) return -nh end },
417 { 'box' , get_d_neg },
418 { 'kern', function (w,h,d,nw,nh,nd) return nh-w end },
419 { 'whatsit', sid_restore },
422 { 'whatsit', sid_save },
423 { 'rotate', '0 -1 1 0' },
424 { 'kern', get_h_d_neg },
426 { 'whatsit', sid_restore },
429 [dir_dtou] = { -- dtou 中で組む
434 { 'whatsit', sid_save },
435 { 'rotate', '-1 0 0 -1' },
436 { 'kern', get_w_neg },
437 { 'box', function (w,h,d,nw,nh,nd) return h-nd end },
438 { 'whatsit', sid_restore },
441 { 'whatsit', sid_save },
442 { 'rotate', '-1 0 0 -1' },
443 { 'kern', get_h_d_neg },
444 { 'box', get_w_neg },
445 { 'whatsit', sid_restore },
449 [dir_dtou] = { -- dtou を
450 [dir_yoko] = { -- yoko 中で組む
455 { 'whatsit', sid_save },
456 { 'rotate', '0 1 -1 0' },
457 { 'kern', function (w,h,d,nw,nh,nd) return -nd end },
459 { 'kern', function (w,h,d,nw,nh,nd) return nd-w end },
460 { 'whatsit', sid_restore },
464 { 'whatsit', sid_save },
465 { 'rotate', '0 1 -1 0' },
466 { 'box', function (w,h,d,nw,nh,nd) return -nd-nh end },
467 { 'kern', get_h_d_neg },
468 { 'whatsit', sid_restore },
471 [dir_tate] = { -- tate 中で組む
476 { 'whatsit', sid_save },
477 { 'rotate', '-1 0 0 -1' },
478 { 'kern', get_w_neg },
479 { 'box', function (w,h,d,nw,nh,nd) return h-nd end },
480 { 'whatsit', sid_restore },
483 { 'whatsit', sid_save },
484 { 'rotate', ' -1 0 0 -1' },
485 { 'kern', function (w,h,d,nw,nh,nd) return -nh-nd end },
486 { 'box', get_w_neg },
487 { 'kern', function (w,h,d,nw,nh,nd) return nh+nd-h-d end },
488 { 'whatsit', sid_restore },
495 -- 1st ret val: b の組方向
496 -- 2nd ret val はその DIR whatsit
497 local function get_box_dir(b, default)
498 start_time_measure('get_box_dir')
499 local dir = has_attr(b, attr_dir) or 0
500 local bh = getfield(b,'head')
501 -- b は insert node となりうるので getlist() は使えない
503 for bh in traverse_id(id_whatsit, bh) do
504 if getsubtype(bh)==sid_user and getfield(bh, 'user_id')==DIR then
506 dir = (dir==0) and has_attr(bh, attr_dir) or dir
510 -- if bh and getid(bh)==id_whatsit
511 -- and getsubtype(bh)==sid_user and getfield(bh, 'user_id')==DIR then
513 -- dir = (dir==0) and has_attr(bh, attr_dir) or dir
515 -- bh = node_next(bh)
517 stop_time_measure('get_box_dir')
518 return (dir==0 and default or dir), c
522 local getbox = tex.getbox
524 function luatexja.direction.unbox_check_dir(is_copy)
525 start_time_measure('box_primitive_hook')
526 local list_dir = get_dir_count()%dir_math_mod
527 local b = getbox(tex_getcount('ltj@tempcnta'))
529 local box_dir = get_box_dir(to_direct(b), dir_yoko)
530 if box_dir%dir_math_mod ~= list_dir then
533 "Incompatible direction list can't be unboxed",
534 'I refuse to unbox a box in differrent direction.')
535 tex.sprint(cat_lp, '\\@gobbletwo')
538 local bd = to_direct(b)
539 local hd = getlist(bd)
542 if getid(hd)==id_whatsit and getsubtype(hd)==sid_user
543 and getfield(hd, 'user_id')==DIR then
545 nh, hd = node_remove(nh, hd)
546 if is_copy and (not dir_backup) then
548 setfield(dir_backup, 'next', nil)
556 setfield(bd, 'head', nh)
559 if luatexja.global_temp and tex.globaldefs~=luatexja.global_temp then
560 tex.globaldefs = luatexja.global_temp
562 stop_time_measure('box_primitive_hook')
564 function luatexja.direction.uncopy_restore_whatsit()
565 local b = getbox(tex_getcount('ltj@tempcnta'))
567 local bd = to_direct(b)
569 setfield(dir_backup, 'next', getlist(bd))
570 setfield(bd, 'head', dir_backup)
577 -- dir_node に包まれている「本来の中身」を取り出し,
579 local function unwrap_dir_node(b, head, box_dir)
580 -- b: dir_node, head: the head of list, box_dir:
581 -- return values are (new head), (next of b), (contents), (dir of contents)
582 local bh = getlist(b)
585 nh = insert_before(head, b, bh)
586 nh, nb = node_remove(nh, b)
587 setfield(b, 'next', nil)
590 local shift_old, b_dir, wh = nil, get_box_dir(bh, 0)
592 Dnode.flush_list(getfield(wh, 'value'))
593 setfield(wh, 'value', nil)
595 return nh, nb, bh, b_dir
598 -- is_manual: 寸法変更に伴うものか?
599 local function create_dir_node(b, b_dir, new_dir, is_manual)
600 local info = dir_node_aux[b_dir%dir_math_mod][new_dir%dir_math_mod]
601 local w = getfield(b, 'width')
602 local h = getfield(b, 'height')
603 local d = getfield(b, 'depth')
604 local db = node_new(getid(b)) -- dir_node
605 set_attr(db, attr_dir,
606 new_dir + (is_manual and dir_node_manual or dir_node_auto))
607 set_attr(db, attr_icflag, PROCESSED)
608 set_attr(b, attr_icflag, PROCESSED)
609 ensure_tex_attr(attr_dir, 0)
610 ensure_tex_attr(attr_icflag, 0)
611 setfield(db, 'dir', getfield(b, 'dir'))
612 setfield(db, 'shift', 0)
613 setfield(db, 'width', info.width(w,h,d))
614 setfield(db, 'height', info.height(w,h,d))
615 setfield(db, 'depth', info.depth(w,h,d))
620 local make_dir_whatsit, process_dir_node
622 make_dir_whatsit = function (head, b, new_dir, origin)
623 new_dir = new_dir%dir_math_mod
624 -- head: list head, b: box
625 -- origin: コール元 (for debug)
626 -- return value: (new head), (next of b), (new b), (is_b_dir_node)
627 -- (new b): b か dir_node に被せられた b
628 local bh = getlist(b)
629 local box_dir, dn = get_box_dir(b, ltjs.list_dir)
630 -- 既に b の中身にあるwhatsit
632 if box_dir%dir_math_mod==new_dir then
633 if box_dir>=dir_node_auto then
634 -- dir_node としてカプセル化されている
635 local _, dnc = get_box_dir(b, 0)
636 if dnc then -- free all other dir_node
637 Dnode.flush_list(getfield(dnc, 'value'))
638 setfield(dnc, 'value', nil)
640 set_attr(b, attr_dir, box_dir%dir_math_mod + dir_node_auto)
641 return head, node_next(b), b, true
643 -- 組方向が一緒 (up to math dir) のボックスなので,何もしなくて良い
644 return head, node_next(b), b, false
648 local nh, nb, ret, flag
649 if box_dir>= dir_node_auto then -- unwrap
651 head, nb, b, b_dir = unwrap_dir_node(b, head, box_dir)
653 if b_dir%dir_math_mod==new_dir then
654 -- dir_node の中身が周囲の組方向とあっている
655 return head, nb, b, false
656 else box_dir = b_dir end
658 box_dir = box_dir%dir_math_mod
660 local dnh = getfield(dn, 'value')
661 for x in traverse(dnh) do
662 if has_attr(x, attr_dir)%dir_math_mod == new_dir then
663 setfield(dn, 'value', to_node(node_remove(dnh, x)))
667 Dnode.flush_list(getfield(dn, 'value'))
668 setfield(dn, 'value', nil)
669 db = db or create_dir_node(b, box_dir, new_dir, false)
670 local w = getfield(b, 'width')
671 local h = getfield(b, 'height')
672 local d = getfield(b, 'depth')
673 local dn_w = getfield(db, 'width')
674 local dn_h = getfield(db, 'height')
675 local dn_d = getfield(db, 'depth')
676 nh, nb = insert_before(head, b, db), nil
677 nh, nb = node_remove(nh, b)
678 setfield(b, 'next', nil); setfield(db, 'head', b)
680 return nh, nb, ret, flag
683 process_dir_node = function (hd, gc)
684 local x, new_dir = hd, ltjs.list_dir or dir_yoko
687 if (xid==id_hlist and get_attr_icflag(x)~=PACKED)
688 or xid==id_vlist then
689 hd, x = make_dir_whatsit(hd, x, new_dir, 'process_dir_node:' .. gc)
698 local node_prev = (Dnode~=node) and Dnode.getprev or node.prev
699 local function lastbox_hook()
700 start_time_measure('box_primitive_hook')
701 local bn = tex_nest[tex_nest.ptr].tail
703 local b, head = to_direct(bn), to_direct(tex_nest[tex_nest.ptr].head)
705 if bid==id_hlist or bid==id_vlist then
706 local box_dir = get_box_dir(b, 0)
707 if box_dir>= dir_node_auto then -- unwrap dir_node
708 local p = node_prev(b)
709 local dummy1, dummy2, nb = unwrap_dir_node(b, nil, box_dir)
710 setfield(p, 'next', nb); tex_nest[tex_nest.ptr].tail = to_node(nb)
711 setfield(b, 'next', nil); setfield(b, 'head', nil)
714 local _, wh = get_box_dir(b, 0) -- clean dir_node attached to the box
716 Dnode.flush_list(getfield('value', wh))
717 setfield(wh, 'value', nil)
721 stop_time_measure('box_primitive_hook')
724 luatexja.direction.make_dir_whatsit = make_dir_whatsit
725 luatexja.direction.lastbox_hook = lastbox_hook
728 -- \wd, \ht, \dp の代わり
730 local getbox, setdimen = tex.getbox, tex.setdimen
731 local function get_box_dim_common(key, s, l_dir)
733 local s_dir, wh = get_box_dir(s, dir_yoko)
734 s_dir = s_dir%dir_math_mod
735 if s_dir ~= l_dir then
736 local not_found = true
737 for x in traverse(getfield(wh, 'value')) do
738 if l_dir == has_attr(x, attr_dir)%dir_node_auto then
739 setdimen('ltj@tempdima', getfield(x, key))
740 not_found = false; break
744 local w = getfield(s, 'width')
745 local h = getfield(s, 'height')
746 local d = getfield(s, 'depth')
747 setdimen('ltj@tempdima',
748 dir_node_aux[s_dir][l_dir][key](w,h,d))
751 setdimen('ltj@tempdima', getfield(s, key))
754 local function get_box_dim(key, n)
755 local gt = tex.globaldefs; tex.globaldefs = 0
758 local l_dir = (get_dir_count())%dir_math_mod
760 local b_dir = get_box_dir(s,dir_yoko)
761 if b_dir<dir_node_auto then
762 get_box_dim_common(key, s, l_dir)
763 elseif b_dir%dir_math_mod==l_dir then
764 setdimen('ltj@tempdima', getfield(s, key))
766 get_box_dim_common(key, getlist(s), l_dir)
769 setdimen('ltj@tempdima', 0)
773 luatexja.direction.get_box_dim = get_box_dim
775 -- return value: (changed dimen of box itself?)
776 local function set_box_dim_common(key, s, l_dir)
777 local s_dir, wh = get_box_dir(s, dir_yoko)
778 s_dir = s_dir%dir_math_mod
779 if s_dir ~= l_dir then
781 wh = create_dir_whatsit(getlist(s), 'set_box_dim', s_dir)
782 setfield(s, 'head', wh)
785 local dnh = getfield(wh, 'value')
786 for x in traverse(dnh) do
787 if has_attr(x, attr_dir)%dir_node_auto==l_dir then
792 db = create_dir_node(s, s_dir, l_dir, true)
793 setfield(db, 'next', dnh)
794 setfield(wh, 'value',to_node(db))
796 setfield(db, key, tex.getdimen('ltj@tempdima'))
799 setfield(s, key, tex.getdimen('ltj@tempdima'))
801 -- change dimension of dir_nodes which are created "automatically"
803 = getfield(s,'width'), getfield(s, 'height'), getfield(s, 'depth')
804 for x in traverse(getfield(wh, 'value')) do
805 local x_dir = has_attr(x, attr_dir)
806 if x_dir<dir_node_manual then
807 local info = dir_node_aux[s_dir][x_dir%dir_node_auto]
808 setfield(x, 'width', info.width(bw,bh,bd))
809 setfield(x, 'height', info.height(bw,bh,bd))
810 setfield(x, 'depth', info.depth(bw,bh,bd))
817 local function set_box_dim(key)
818 local n = tex_getcount('ltj@tempcnta')
821 local l_dir = (get_dir_count())%dir_math_mod
823 local b_dir = get_box_dir(s,dir_yoko)
824 if b_dir<dir_node_auto then
825 set_box_dim_common(key, s, l_dir)
826 elseif b_dir%dir_math_mod == l_dir then
828 setfield(s, key, tex.getdimen('ltj@tempdima'))
829 if b_dir<dir_node_manual then
830 set_attr(s, attr_dir, b_dir%dir_node_auto + dir_node_manual)
833 local sid, b = getid(s), getlist(s)
834 local info = dir_node_aux[get_box_dir(b,dir_yoko)%dir_math_mod][b_dir%dir_node_auto]
836 = getfield(b,'width'), getfield(b, 'height'), getfield(b, 'depth')
838 = getfield(s,'width'), getfield(s, 'height'), getfield(s, 'depth')
839 if set_box_dim_common(key, b, l_dir) and b_dir<dir_node_manual then
840 -- re-calculate dimension of s, if s is created "automatically"
841 if b_dir<dir_node_manual then
842 setfield(s, 'width', info.width(bw,bh,bd))
843 setfield(s, 'height', info.height(bw,bh,bd))
844 setfield(s, 'depth', info.depth(bw,bh,bd))
850 luatexja.direction.set_box_dim = set_box_dim
854 local getbox = tex.getbox
855 local function get_register_dir(n)
859 local b_dir = get_box_dir(s, dir_yoko)
860 if b_dir<dir_node_auto then
863 local b_dir = get_box_dir(
864 node_next(node_next(node_next(getlist(s)))), dir_yoko)
871 luatexja.direction.get_register_dir = get_register_dir
875 local getbox, setbox, copy_list = tex.getbox, tex.setbox, Dnode.copy_list
877 function luatexja.direction.raise_box()
878 start_time_measure('box_primitive_hook')
879 local list_dir = get_dir_count()
880 local s = getbox('ltj@afbox')
882 local sd = to_direct(s)
883 local box_dir = get_box_dir(sd, dir_yoko)
884 if box_dir%dir_math_mod ~= list_dir then
887 to_node(copy_list(make_dir_whatsit(sd, sd, list_dir, 'box_move')))
888 -- copy_list しないとリストの整合性が崩れる……?
892 stop_time_measure('box_primitive_hook')
896 -- PACKED の hbox から文字を取り出す
897 -- luatexja.jfmglue.check_box などで使用
899 local function glyph_from_packed(h)
901 return (getid(b)==id_kern or (getid(b)==id_whatsit and getsubtype(b)==sid_save) )
902 and node_next(node_next(node_next(b))) or b
904 luatexja.direction.glyph_from_packed = glyph_from_packed
909 local id_adjust = node.id('adjust')
910 function luatexja.direction.check_adjust_direction()
911 start_time_measure('box_primitive_hook')
912 local list_dir = get_adjust_dir_count()
913 local a = tex_nest[tex_nest.ptr].tail
914 local ad = to_direct(a)
915 if a and getid(ad)==id_adjust then
916 local adj_dir = get_box_dir(ad)
917 if list_dir~=adj_dir then
920 'Direction Incompatible',
921 "\\vadjust's argument and outer vlist must have same direction.")
925 stop_time_measure('box_primitive_hook')
931 local id_ins = node.id('ins')
932 local id_rule = node.id('rule')
933 function luatexja.direction.populate_insertion_dir_whatsit()
934 start_time_measure('box_primitive_hook')
935 local list_dir = get_dir_count()
936 local a = tex_nest[tex_nest.ptr].tail
937 local ad = to_direct(a)
938 if a and getid(ad)==id_ins then
939 local h = getfield(ad, 'head')
940 if getid(h)==id_whatsit and
941 getsubtype(h)==sid_user and getfield(h, 'user_id')==DIR then
942 local n = h; h = node_remove(h,h)
945 for box_rule in traverse(h) do
946 if getid(box_rule)<id_rule then
947 local w = node_new(id_whatsit, sid_user)
948 setfield(w, 'next', nil)
949 setfield(w, 'user_id', DIR)
950 setfield(w, 'type', 110)
951 set_attr(w, attr_dir, list_dir)
952 h = insert_before(h, box_rule, w)
955 ensure_tex_attr(attr_dir, 0)
956 setfield(ad, 'head', h)
958 stop_time_measure('box_primitive_hook')
964 local split_dir_whatsit
965 local function dir_adjust_vpack(h, gc)
966 start_time_measure('direction_vpack')
967 local hd = to_direct(h)
968 if gc=='split_keep' then
969 -- supply dir_whatsit
970 hd = create_dir_whatsit_vbox(hd, gc)
971 split_dir_whatsit = hd
972 elseif gc=='split_off' then
973 for bh in traverse_id(id_whatsit, hd) do
974 if getsubtype(bh)==sid_user and getfield(bh, 'user_id')==DIR then
975 ltjs.list_dir = has_attr(bh, attr_dir); break
980 -- if bh and getid(bh)==id_whatsit
981 -- and getsubtype(bh)==sid_user and getfield(bh, 'user_id')==DIR then
982 -- ltjs.list_dir = has_attr(bh, attr_dir); break
984 -- bh = node_next(bh)
986 if split_dir_whatsit then
987 -- adjust direction of 'split_keep'
988 set_attr(split_dir_whatsit, attr_dir, ltjs.list_dir)
990 split_dir_whatsit=nil
991 elseif gc=='preamble' then
992 split_dir_whatsit=nil
995 hd = process_dir_node(create_dir_whatsit_vbox(hd, gc), gc)
996 split_dir_whatsit=nil
998 stop_time_measure('direction_vpack')
1001 luatexbase.add_to_callback('vpack_filter',
1003 'ltj.direction', 10000)
1007 -- supply direction whatsit to the main vertical list "of the next page"
1008 local function dir_adjust_pre_output(h, gc)
1009 return to_node(create_dir_whatsit_vbox(to_direct(h), gc))
1011 luatexbase.add_to_callback('pre_output_filter',
1012 dir_adjust_pre_output,
1013 'ltj.direction', 10000)
1015 function luatexja.direction.remove_end_whatsit()
1016 local h=tex.lists.page_head
1017 if h and (not h.next) and
1018 h.id==id_whatsit and h.subtype==sid_user and
1019 h.user_id == DIR then
1020 tex.lists.page_head = nil
1028 local function dir_adjust_buildpage(info)
1030 local head = to_direct(tex.lists.contrib_head)
1034 = make_dir_whatsit(head,
1038 tex.lists.contrib_head = to_node(head)
1042 luatexbase.add_to_callback('buildpage_filter',
1043 dir_adjust_buildpage,
1044 'ltj.direction', 10000)
1047 -- finalize (executed just before \shipout)
1048 -- we supply correct pdfsavematrix nodes etc. inside dir_node
1050 local finalize_inner
1051 local function finalize_dir_node(db,new_dir)
1052 local b = getlist(db)
1054 local box_dir = get_box_dir(b, dir_yoko)%dir_math_mod
1055 setfield(db, 'head', nil)
1056 local w = getfield(b, 'width')
1057 local h = getfield(b, 'height')
1058 local d = getfield(b, 'depth')
1059 local dn_w = getfield(db, 'width')
1060 local dn_h = getfield(db, 'height')
1061 local dn_d = getfield(db, 'depth')
1062 local db_head, db_tail = nil
1063 for _,v in ipairs(dir_node_aux[box_dir][new_dir][getid(b)]) do
1064 local cmd, arg, nn = v[1], v[2]
1066 nn = node_new(id_kern)
1067 setfield(nn, 'kern', arg(w, h, d, dn_w, dn_h, dn_d))
1068 elseif cmd=='whatsit' then
1069 nn = node_new(id_whatsit, arg)
1070 elseif cmd=='rotate' then
1071 nn = node_new(id_whatsit, sid_matrix)
1072 setfield(nn, 'data', arg)
1073 elseif cmd=='box' then
1074 nn = b; setfield(b, 'next', nil)
1075 setfield(nn, 'shift', arg(w, h, d, dn_w, dn_h, dn_d))
1078 insert_after(db_head, db_tail, nn)
1081 db_head, db_tail = nn, nn
1083 setfield(db, 'head', db_head)
1087 tex.setattribute(attr_dir, dir_yoko)
1088 local shipout_temp = node_new(id_hlist)
1089 tex.setattribute(attr_dir, 0)
1091 finalize_inner = function (box)
1092 for n in traverse(getlist(box)) do
1093 local nid = getid(n)
1094 if (nid==id_hlist or nid==id_vlist) then
1095 local ndir = get_box_dir(n, dir_yoko)
1096 if ndir>=dir_node_auto then -- n is dir_node
1097 finalize_dir_node(n, ndir%dir_math_mod)
1104 local getbox = tex.getbox
1105 local setbox, copy = Dnode.setbox, Dnode.copy
1106 function luatexja.direction.finalize()
1107 local a = to_direct(tex.getbox("AtBeginShipoutBox"))
1108 local a_dir = get_box_dir(a, dir_yoko)
1109 if a_dir~=dir_yoko then
1110 local b = create_dir_node(a, a_dir, dir_yoko, false)
1111 setfield(b, 'head', a); a = b
1113 setfield(shipout_temp, 'head', a)
1114 finalize_inner(shipout_temp)
1115 setbox('global', "AtBeginShipoutBox", copy(getlist(shipout_temp)))
1116 setfield(shipout_temp, 'head',nil)
1117 --print('MM', collectgarbage('count'))