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 ensure_tex_attr = ltjb.ensure_tex_attr
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
68 local node_copy = Dnode.copy
70 for _,i in pairs({dir_tate, dir_yoko, dir_dtou, dir_utod}) do
71 local w = node_new(id_whatsit, sid_user)
72 set_attr(w, attr_dir, i)
73 setfield(w, 'user_id', DIR)
74 setfield(w, 'type', 110)
75 setfield(w, 'next', nil)
76 dir_pool[i] = function () return node_copy(w) end
81 local function adjust_badness(hd)
82 if not node_next(hd) and getid(hd)==id_whatsit and getsubtype(hd)==sid_user
83 and getfield(hd, 'user_id')==DIR then
84 -- avoid double whatsit
85 luatexja.global_temp=tex.globaldefs; tex.globaldefs=0
86 luatexja.hbadness_temp=tex.hbadness; tex.hbadness=10000
87 luatexja.vbadness_temp=tex.vbadness; tex.vbadness=10000
89 luatexja.global_temp = nil
90 luatexja.hbadness_temp=nil
91 luatexja.vbadness_temp=nil
95 local get_dir_count, get_adjust_dir_count
97 local function get_dir_count_inner(h)
99 if h.id==id_whatsit and h.subtype==sid_user and h.user_id==DIR then
100 local ic = node.has_attribute(h, attr_icflag) or 0
101 return (ic<PROCESSED_BEGIN_FLAG)
102 and (node.has_attribute(h,attr_dir)%dir_node_auto) or 0
110 function get_dir_count()
111 for i=tex_nest.ptr, 1, -1 do
112 local h = tex_nest[i].head.next
114 local t = get_dir_count_inner(h)
115 if t~=0 then return t end
118 return page_direction
120 function get_adjust_dir_count()
121 for i=tex_nest.ptr, 1, -1 do
122 local v = tex_nest[i]
123 local h, m = v.head.next, v.mode
124 if abs(m)== ltjs.vmode and h then
125 local t = get_dir_count_inner(h)
126 if t~=0 then return t end
129 return page_direction
131 luatexja.direction.get_dir_count = get_dir_count
132 luatexja.direction.get_adjust_dir_count = get_adjust_dir_count
136 -- \tate, \yoko,\dtou, \utod
138 local node_next = node.next
139 local node_set_attr = node.set_attribute
140 local node_traverse = node.traverse
141 local STCK = luatexja.userid_table.STCK
142 local IHB = luatexja.userid_table.IHB
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
157 flag = 0; break -- error
166 if flag==1 then -- dir_whatsit already exists
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 = to_direct(tex_nest[lv].head)
184 local w = dir_pool[v]()
185 insert_after(h, h, w)
186 tex_nest[lv].tail = to_node(node_tail(w))
187 ensure_tex_attr(attr_icflag, 0)
188 ensure_tex_attr(attr_dir, 0)
191 local function set_list_direction(v, name)
192 local lv = tex_nest.ptr
194 v,name = get_dir_count(), nil
195 if lv>=1 and abs(tex_nest[lv-1].mode) == ltjs.mmode and v == dir_tate then
199 v,name = get_adjust_dir_count(), nil
201 if tex.currentgrouptype==6 then
204 "You can't use `\\" .. name .. "' in an align",
205 "To change direction in an align, \n"
206 .. "you shold use \\hbox or \\vbox.")
208 local h = (lv==0) and tex.lists.page_head or tex_nest[lv].head.next
209 local flag,w = test_list(h,lv)
211 if lv==0 and not page_direction then
212 page_direction = v -- for first call of \yoko (in luatexja-core.sty)
216 "Use `\\" .. tostring(name) .. "' at top of list",
217 'Direction change command by LuaTeX-ja is available\n'
218 .. 'only when the current list is null.')
221 node_set_attr(w, attr_dir, v)
222 if lv==0 then page_direction = v end
224 if lv==0 then page_direction = v end
226 ensure_tex_attr(attr_icflag, 0)
228 ensure_tex_attr(attr_dir, 0)
230 luatexja.direction.set_list_direction = set_list_direction
233 -- ボックスに dir whatsit を追加
234 local function create_dir_whatsit(hd, gc, new_dir)
235 if getid(hd)==id_whatsit and
236 getsubtype(hd)==sid_user and getfield(hd, 'user_id')==DIR then
237 set_attr(hd, attr_icflag,
238 get_attr_icflag(hd) + PROCESSED_BEGIN_FLAG)
239 local n = node_next(hd)
241 set_attr(n, attr_icflag,
242 get_attr_icflag(n) + PROCESSED_BEGIN_FLAG)
244 ensure_tex_attr(attr_icflag, 0)
247 local w = dir_pool[new_dir]()
248 setfield(w, 'next', hd)
249 set_attr(w, attr_icflag, PROCESSED_BEGIN_FLAG)
250 set_attr(hd, attr_icflag,
251 get_attr_icflag(hd) + PROCESSED_BEGIN_FLAG)
252 ensure_tex_attr(attr_icflag, 0)
253 ensure_tex_attr(attr_dir, 0)
258 -- hpack_filter, vpack_filter, post_line_break_filter
259 -- の結果を組方向を明示するため,先頭に dir_node を設置
261 local function create_dir_whatsit_hpack(h, gc)
262 local hd = to_direct(h)
263 if gc=='fin_row' or gc == 'preamble' then
265 set_attr(hd, attr_icflag, PROCESSED_BEGIN_FLAG)
266 ensure_tex_attr(attr_icflag, 0)
271 return to_node(create_dir_whatsit(hd, gc, ltjs.list_dir))
275 luatexbase.add_to_callback('hpack_filter',
276 create_dir_whatsit_hpack, 'ltj.create_dir_whatsit', 10000)
280 local function create_dir_whatsit_parbox(h, gc)
281 stop_time_measure('tex_linebreak')
282 -- start 側は ltj-debug.lua に
283 local new_dir = ltjs.list_dir
284 for line in traverse_id(id_hlist, to_direct(h)) do
285 setfield(line, 'head', create_dir_whatsit(getlist(line), gc, new_dir) )
287 ensure_tex_attr(attr_dir, 0)
290 luatexbase.add_to_callback('post_linebreak_filter',
291 create_dir_whatsit_parbox, 'ltj.create_dir_whatsit', 10000)
294 local create_dir_whatsit_vbox
297 local id_glue, sid_parskip = node.id('glue'), 3
298 create_dir_whatsit_vbox = function (hd, gc)
299 ltjs.list_dir = get_dir_count()
300 -- remove dir whatsit
301 for x in traverse_id(id_whatsit, hd) do
302 if getsubtype(x)==sid_user and getfield(x, 'user_id')==DIR then
307 ltjs.list_dir =has_attr(hd,attr_dir)
308 local x = node_next(hd)
309 if getid(x)==id_glue and getsubtype(x)==sid_parskip then
310 node_remove(hd,x); node_free(x)
314 hd = node_remove(hd, wh[i]); node_free(wh[i]); wh[i] = nil
316 if gc=='fin_row' then -- gc == 'preamble' case is treated in dir_adjust_vpack
318 set_attr(hd, attr_icflag, PROCESSED_BEGIN_FLAG)
319 ensure_tex_attr(attr_icflag, 0)
323 local n =node_next(hd)
325 local w = create_dir_whatsit(hd, gc, ltjs.list_dir)
326 -- move dir whatsit after hd
327 setfield(hd, 'next', w); setfield(w, 'next', n)
330 hd = create_dir_whatsit(hd, gc, ltjs.list_dir)
337 -- dir_node に包む方法を書いたテーブル
340 local floor = math.floor
341 local get_h =function (w,h,d) return h end
342 local get_d =function (w,h,d) return d end
343 local get_h_d =function (w,h,d) return h+d end
344 local get_h_d_neg =function (w,h,d) return -h-d end
345 local get_d_neg =function (w,h,d) return -d end
346 local get_w_half =function (w,h,d) return floor(0.5*w) end
347 local get_w_half_rem =function (w,h,d) return w-floor(0.5*w) end
348 local get_w_neg =function (w,h,d) return -w end
349 local get_w =function (w,h,d) return w end
350 local zero = function() return 0 end
352 [dir_yoko] = { -- yoko を
353 [dir_tate] = { -- tate 中で組む
356 depth = get_w_half_rem,
358 { 'whatsit', sid_save },
359 { 'rotate', '0 1 -1 0' },
360 { 'kern', function(w,h,d,nw,nh,nd) return -nd end },
362 { 'kern', function(w,h,d,nw,nh,nd) return nd-w end },
363 { 'whatsit', sid_restore },
366 { 'whatsit', sid_save },
367 { 'rotate', '0 1 -1 0' },
369 { 'box' , function(w,h,d,nw,nh,nd) return -nh-nd end },
370 { 'kern', get_h_d_neg},
371 { 'whatsit', sid_restore },
374 [dir_dtou] = { -- dtou 中で組む
379 { 'whatsit', sid_save },
380 { 'rotate', '0 -1 1 0' },
381 { 'kern', function(w,h,d,nw,nh,nd) return -nh end },
382 { 'box', get_d_neg },
383 { 'kern', function(w,h,d,nw,nh,nd) return nh-w end },
384 { 'whatsit', sid_restore },
387 { 'whatsit', sid_save },
388 { 'rotate', '0 -1 1 0' },
389 { 'kern', get_h_d_neg },
391 { 'whatsit', sid_restore },
395 [dir_tate] = { -- tate を
396 [dir_yoko] = { -- yoko 中で組む
401 { 'whatsit', sid_save },
402 { 'rotate', '0 -1 1 0' },
403 { 'kern', function (w,h,d,nw,nh,nd) return -nh end },
404 { 'box' , get_d_neg },
405 { 'kern', function (w,h,d,nw,nh,nd) return nh-w end },
406 { 'whatsit', sid_restore },
409 { 'whatsit', sid_save },
410 { 'rotate', '0 -1 1 0' },
411 { 'kern', get_h_d_neg },
413 { 'whatsit', sid_restore },
416 [dir_dtou] = { -- dtou 中で組む
421 { 'whatsit', sid_save },
422 { 'rotate', '-1 0 0 -1' },
423 { 'kern', get_w_neg },
424 { 'box', function (w,h,d,nw,nh,nd) return h-nd end },
425 { 'whatsit', sid_restore },
428 { 'whatsit', sid_save },
429 { 'rotate', '-1 0 0 -1' },
430 { 'kern', get_h_d_neg },
431 { 'box', get_w_neg },
432 { 'whatsit', sid_restore },
436 [dir_dtou] = { -- dtou を
437 [dir_yoko] = { -- yoko 中で組む
442 { 'whatsit', sid_save },
443 { 'rotate', '0 1 -1 0' },
444 { 'kern', function (w,h,d,nw,nh,nd) return -nd end },
446 { 'kern', function (w,h,d,nw,nh,nd) return nd-w end },
447 { 'whatsit', sid_restore },
451 { 'whatsit', sid_save },
452 { 'rotate', '0 1 -1 0' },
453 { 'box', function (w,h,d,nw,nh,nd) return -nd-nh end },
454 { 'kern', get_h_d_neg },
455 { 'whatsit', sid_restore },
458 [dir_tate] = { -- tate 中で組む
463 { 'whatsit', sid_save },
464 { 'rotate', '-1 0 0 -1' },
465 { 'kern', get_w_neg },
466 { 'box', function (w,h,d,nw,nh,nd) return h-nd end },
467 { 'whatsit', sid_restore },
470 { 'whatsit', sid_save },
471 { 'rotate', ' -1 0 0 -1' },
472 { 'kern', function (w,h,d,nw,nh,nd) return -nh-nd end },
473 { 'box', get_w_neg },
474 { 'kern', function (w,h,d,nw,nh,nd) return nh+nd-h-d end },
475 { 'whatsit', sid_restore },
482 -- 1st ret val: b の組方向
483 -- 2nd ret val はその DIR whatsit
484 local function get_box_dir(b, default)
485 start_time_measure('get_box_dir')
486 local dir = has_attr(b, attr_dir) or 0
487 local bh = getfield(b,'head')
488 -- b は insert node となりうるので getlist() は使えない
490 for bh in traverse_id(id_whatsit, bh) do
491 if getsubtype(bh)==sid_user and getfield(bh, 'user_id')==DIR then
493 dir = (dir==0) and has_attr(bh, attr_dir) or dir
497 -- if bh and getid(bh)==id_whatsit
498 -- and getsubtype(bh)==sid_user and getfield(bh, 'user_id')==DIR then
500 -- dir = (dir==0) and has_attr(bh, attr_dir) or dir
502 -- bh = node_next(bh)
504 stop_time_measure('get_box_dir')
505 return (dir==0 and default or dir), c
509 local getbox = tex.getbox
511 function luatexja.direction.unbox_check_dir(is_copy)
512 start_time_measure('box_primitive_hook')
513 local list_dir = get_dir_count()%dir_math_mod
514 local b = getbox(tex_getcount('ltj@tempcnta'))
516 local box_dir = get_box_dir(to_direct(b), dir_yoko)
517 if box_dir%dir_math_mod ~= list_dir then
520 "Incompatible direction list can't be unboxed",
521 'I refuse to unbox a box in differrent direction.')
522 tex.sprint(cat_lp, '\\@gobbletwo')
525 local bd = to_direct(b)
526 local hd = getlist(bd)
529 if getid(hd)==id_whatsit and getsubtype(hd)==sid_user
530 and getfield(hd, 'user_id')==DIR then
532 nh, hd = node_remove(nh, hd)
533 if is_copy and (not dir_backup) then
535 setfield(dir_backup, 'next', nil)
543 setfield(bd, 'head', nh)
546 if luatexja.global_temp and tex.globaldefs~=luatexja.global_temp then
547 tex.globaldefs = luatexja.global_temp
549 stop_time_measure('box_primitive_hook')
551 function luatexja.direction.uncopy_restore_whatsit()
552 local b = getbox(tex_getcount('ltj@tempcnta'))
554 local bd = to_direct(b)
556 setfield(dir_backup, 'next', getlist(bd))
557 setfield(bd, 'head', dir_backup)
564 -- dir_node に包まれている「本来の中身」を取り出し,
566 local function unwrap_dir_node(b, head, box_dir)
567 -- b: dir_node, head: the head of list, box_dir:
568 -- return values are (new head), (next of b), (contents), (dir of contents)
569 local bh = getlist(b)
572 nh = insert_before(head, b, bh)
573 nh, nb = node_remove(nh, b)
574 setfield(b, 'next', nil)
577 local shift_old, b_dir, wh = nil, get_box_dir(bh, 0)
579 Dnode.flush_list(getfield(wh, 'value'))
580 setfield(wh, 'value', nil)
582 return nh, nb, bh, b_dir
585 -- is_manual: 寸法変更に伴うものか?
586 local function create_dir_node(b, b_dir, new_dir, is_manual)
587 local info = dir_node_aux[b_dir%dir_math_mod][new_dir%dir_math_mod]
588 local w = getfield(b, 'width')
589 local h = getfield(b, 'height')
590 local d = getfield(b, 'depth')
591 local db = node_new(getid(b)) -- dir_node
592 set_attr(db, attr_dir,
593 new_dir + (is_manual and dir_node_manual or dir_node_auto))
594 set_attr(db, attr_icflag, PROCESSED)
595 set_attr(b, attr_icflag, PROCESSED)
596 ensure_tex_attr(attr_dir, 0)
597 ensure_tex_attr(attr_icflag, 0)
598 setfield(db, 'dir', getfield(b, 'dir'))
599 setfield(db, 'shift', 0)
600 setfield(db, 'width', info.width(w,h,d))
601 setfield(db, 'height', info.height(w,h,d))
602 setfield(db, 'depth', info.depth(w,h,d))
607 local make_dir_whatsit, process_dir_node
609 make_dir_whatsit = function (head, b, new_dir, origin)
610 new_dir = new_dir%dir_math_mod
611 -- head: list head, b: box
612 -- origin: コール元 (for debug)
613 -- return value: (new head), (next of b), (new b), (is_b_dir_node)
614 -- (new b): b か dir_node に被せられた b
615 local bh = getlist(b)
616 local box_dir, dn = get_box_dir(b, ltjs.list_dir)
617 -- 既に b の中身にあるwhatsit
619 if box_dir%dir_math_mod==new_dir then
620 if box_dir>=dir_node_auto then
621 -- dir_node としてカプセル化されている
622 local _, dnc = get_box_dir(b, 0)
623 if dnc then -- free all other dir_node
624 Dnode.flush_list(getfield(dnc, 'value'))
625 setfield(dnc, 'value', nil)
627 set_attr(b, attr_dir, box_dir%dir_math_mod + dir_node_auto)
628 return head, node_next(b), b, true
630 -- 組方向が一緒 (up to math dir) のボックスなので,何もしなくて良い
631 return head, node_next(b), b, false
635 local nh, nb, ret, flag
636 if box_dir>= dir_node_auto then -- unwrap
638 head, nb, b, b_dir = unwrap_dir_node(b, head, box_dir)
640 if b_dir%dir_math_mod==new_dir then
641 -- dir_node の中身が周囲の組方向とあっている
642 return head, nb, b, false
643 else box_dir = b_dir end
645 box_dir = box_dir%dir_math_mod
647 local dnh = getfield(dn, 'value')
648 for x in traverse(dnh) do
649 if has_attr(x, attr_dir)%dir_math_mod == new_dir then
650 setfield(dn, 'value', to_node(node_remove(dnh, x)))
654 Dnode.flush_list(getfield(dn, 'value'))
655 setfield(dn, 'value', nil)
656 db = db or create_dir_node(b, box_dir, new_dir, false)
657 local w = getfield(b, 'width')
658 local h = getfield(b, 'height')
659 local d = getfield(b, 'depth')
660 local dn_w = getfield(db, 'width')
661 local dn_h = getfield(db, 'height')
662 local dn_d = getfield(db, 'depth')
663 nh, nb = insert_before(head, b, db), nil
664 nh, nb = node_remove(nh, b)
665 setfield(b, 'next', nil); setfield(db, 'head', b)
667 return nh, nb, ret, flag
670 process_dir_node = function (hd, gc)
671 local x, new_dir = hd, ltjs.list_dir or dir_yoko
674 if (xid==id_hlist and get_attr_icflag(x)~=PACKED)
675 or xid==id_vlist then
676 hd, x = make_dir_whatsit(hd, x, new_dir, 'process_dir_node:' .. gc)
685 local node_prev = (Dnode~=node) and Dnode.getprev or node.prev
686 local function lastbox_hook()
687 start_time_measure('box_primitive_hook')
688 local bn = tex_nest[tex_nest.ptr].tail
690 local b, head = to_direct(bn), to_direct(tex_nest[tex_nest.ptr].head)
692 if bid==id_hlist or bid==id_vlist then
693 local box_dir = get_box_dir(b, 0)
694 if box_dir>= dir_node_auto then -- unwrap dir_node
695 local p = node_prev(b)
696 local dummy1, dummy2, nb = unwrap_dir_node(b, nil, box_dir)
697 setfield(p, 'next', nb); tex_nest[tex_nest.ptr].tail = to_node(nb)
698 setfield(b, 'next', nil); setfield(b, 'head', nil)
701 local _, wh = get_box_dir(b, 0) -- clean dir_node attached to the box
703 Dnode.flush_list(getfield('value', wh))
704 setfield(wh, 'value', nil)
708 stop_time_measure('box_primitive_hook')
711 luatexja.direction.make_dir_whatsit = make_dir_whatsit
712 luatexja.direction.lastbox_hook = lastbox_hook
715 -- \wd, \ht, \dp の代わり
717 local getbox, setdimen = tex.getbox, tex.setdimen
718 local function get_box_dim_common(key, s, l_dir)
720 local s_dir, wh = get_box_dir(s, dir_yoko)
721 s_dir = s_dir%dir_math_mod
722 if s_dir ~= l_dir then
723 local not_found = true
724 for x in traverse(getfield(wh, 'value')) do
725 if l_dir == has_attr(x, attr_dir)%dir_node_auto then
726 setdimen('ltj@tempdima', getfield(x, key))
727 not_found = false; break
731 local w = getfield(s, 'width')
732 local h = getfield(s, 'height')
733 local d = getfield(s, 'depth')
734 setdimen('ltj@tempdima',
735 dir_node_aux[s_dir][l_dir][key](w,h,d))
738 setdimen('ltj@tempdima', getfield(s, key))
741 local function get_box_dim(key, n)
742 local gt = tex.globaldefs; tex.globaldefs = 0
745 local l_dir = (get_dir_count())%dir_math_mod
747 local b_dir = get_box_dir(s,dir_yoko)
748 if b_dir<dir_node_auto then
749 get_box_dim_common(key, s, l_dir)
750 elseif b_dir%dir_math_mod==l_dir then
751 setdimen('ltj@tempdima', getfield(s, key))
753 get_box_dim_common(key, getlist(s), l_dir)
756 setdimen('ltj@tempdima', 0)
758 tex.sprint(cat_lp, '\\ltj@tempdima')
761 luatexja.direction.get_box_dim = get_box_dim
763 -- return value: (changed dimen of box itself?)
764 local scan_dimen, scan_int = luatexja.token.scan_dimen, luatexja.token.scan_int
765 local scan_keyword = luatexja.token.scan_keyword
766 local function set_box_dim_common(key, s, l_dir)
767 local s_dir, wh = get_box_dir(s, dir_yoko)
768 s_dir = s_dir%dir_math_mod
769 if s_dir ~= l_dir then
771 wh = create_dir_whatsit(getlist(s), 'set_box_dim', s_dir)
772 setfield(s, 'head', wh)
775 local dnh = getfield(wh, 'value')
776 for x in traverse(dnh) do
777 if has_attr(x, attr_dir)%dir_node_auto==l_dir then
782 db = create_dir_node(s, s_dir, l_dir, true)
783 setfield(db, 'next', dnh)
784 setfield(wh, 'value',to_node(db))
786 setfield(db, key, scan_dimen())
789 setfield(s, key, scan_dimen())
791 -- change dimension of dir_nodes which are created "automatically"
793 = getfield(s,'width'), getfield(s, 'height'), getfield(s, 'depth')
794 for x in traverse(getfield(wh, 'value')) do
795 local x_dir = has_attr(x, attr_dir)
796 if x_dir<dir_node_manual then
797 local info = dir_node_aux[s_dir][x_dir%dir_node_auto]
798 setfield(x, 'width', info.width(bw,bh,bd))
799 setfield(x, 'height', info.height(bw,bh,bd))
800 setfield(x, 'depth', info.depth(bw,bh,bd))
807 local function set_box_dim(key)
808 local s = getbox(scan_int()); scan_keyword('=')
810 local l_dir = (get_dir_count())%dir_math_mod
812 local b_dir = get_box_dir(s,dir_yoko)
813 if b_dir<dir_node_auto then
814 set_box_dim_common(key, s, l_dir)
815 elseif b_dir%dir_math_mod == l_dir then
817 setfield(s, key, scan_dimen())
818 if b_dir<dir_node_manual then
819 set_attr(s, attr_dir, b_dir%dir_node_auto + dir_node_manual)
822 local sid, b = getid(s), getlist(s)
823 local info = dir_node_aux[get_box_dir(b,dir_yoko)%dir_math_mod][b_dir%dir_node_auto]
825 = getfield(b,'width'), getfield(b, 'height'), getfield(b, 'depth')
827 = getfield(s,'width'), getfield(s, 'height'), getfield(s, 'depth')
828 if set_box_dim_common(key, b, l_dir) and b_dir<dir_node_manual then
829 -- re-calculate dimension of s, if s is created "automatically"
830 if b_dir<dir_node_manual then
831 setfield(s, 'width', info.width(bw,bh,bd))
832 setfield(s, 'height', info.height(bw,bh,bd))
833 setfield(s, 'depth', info.depth(bw,bh,bd))
839 luatexja.direction.set_box_dim = set_box_dim
843 local getbox = tex.getbox
844 local function get_register_dir(n)
848 local b_dir = get_box_dir(s, dir_yoko)
849 if b_dir<dir_node_auto then
852 local b_dir = get_box_dir(
853 node_next(node_next(node_next(getlist(s)))), dir_yoko)
860 luatexja.direction.get_register_dir = get_register_dir
864 local getbox, setbox, copy_list = tex.getbox, tex.setbox, Dnode.copy_list
866 function luatexja.direction.raise_box()
867 start_time_measure('box_primitive_hook')
868 local list_dir = get_dir_count()
869 local s = getbox('ltj@afbox')
871 local sd = to_direct(s)
872 local box_dir = get_box_dir(sd, dir_yoko)
873 if box_dir%dir_math_mod ~= list_dir then
876 to_node(copy_list(make_dir_whatsit(sd, sd, list_dir, 'box_move')))
877 -- copy_list しないとリストの整合性が崩れる……?
881 stop_time_measure('box_primitive_hook')
885 -- PACKED の hbox から文字を取り出す
886 -- luatexja.jfmglue.check_box などで使用
888 local function glyph_from_packed(h)
890 return (getid(b)==id_kern or (getid(b)==id_whatsit and getsubtype(b)==sid_save) )
891 and node_next(node_next(node_next(b))) or b
893 luatexja.direction.glyph_from_packed = glyph_from_packed
898 local id_adjust = node.id('adjust')
899 function luatexja.direction.check_adjust_direction()
900 start_time_measure('box_primitive_hook')
901 local list_dir = get_adjust_dir_count()
902 local a = tex_nest[tex_nest.ptr].tail
903 local ad = to_direct(a)
904 if a and getid(ad)==id_adjust then
905 local adj_dir = get_box_dir(ad)
906 if list_dir~=adj_dir then
909 'Direction Incompatible',
910 "\\vadjust's argument and outer vlist must have same direction.")
914 stop_time_measure('box_primitive_hook')
920 local id_ins = node.id('ins')
921 local id_rule = node.id('rule')
922 function luatexja.direction.populate_insertion_dir_whatsit()
923 start_time_measure('box_primitive_hook')
924 local list_dir = get_dir_count()
925 local a = tex_nest[tex_nest.ptr].tail
926 local ad = to_direct(a)
927 if a and getid(ad)==id_ins then
928 local h = getfield(ad, 'head')
929 if getid(h)==id_whatsit and
930 getsubtype(h)==sid_user and getfield(h, 'user_id')==DIR then
931 local n = h; h = node_remove(h,h)
934 for box_rule in traverse(h) do
935 if getid(box_rule)<id_rule then
936 h = insert_before(h, box_rule, dir_pool[list_dir]())
939 ensure_tex_attr(attr_dir, 0)
940 setfield(ad, 'head', h)
942 stop_time_measure('box_primitive_hook')
948 local split_dir_whatsit
949 local function dir_adjust_vpack(h, gc)
950 start_time_measure('direction_vpack')
951 local hd = to_direct(h)
952 if gc=='split_keep' then
953 -- supply dir_whatsit
954 hd = create_dir_whatsit_vbox(hd, gc)
955 split_dir_whatsit = hd
956 elseif gc=='split_off' then
957 for bh in traverse_id(id_whatsit, hd) do
958 if getsubtype(bh)==sid_user and getfield(bh, 'user_id')==DIR then
959 ltjs.list_dir = has_attr(bh, attr_dir); break
964 -- if bh and getid(bh)==id_whatsit
965 -- and getsubtype(bh)==sid_user and getfield(bh, 'user_id')==DIR then
966 -- ltjs.list_dir = has_attr(bh, attr_dir); break
968 -- bh = node_next(bh)
970 if split_dir_whatsit then
971 -- adjust direction of 'split_keep'
972 set_attr(split_dir_whatsit, attr_dir, ltjs.list_dir)
974 split_dir_whatsit=nil
975 elseif gc=='preamble' then
976 split_dir_whatsit=nil
979 hd = process_dir_node(create_dir_whatsit_vbox(hd, gc), gc)
980 split_dir_whatsit=nil
982 stop_time_measure('direction_vpack')
985 luatexbase.add_to_callback('vpack_filter',
987 'ltj.direction', 10000)
991 -- supply direction whatsit to the main vertical list "of the next page"
992 local function dir_adjust_pre_output(h, gc)
993 return to_node(create_dir_whatsit_vbox(to_direct(h), gc))
995 luatexbase.add_to_callback('pre_output_filter',
996 dir_adjust_pre_output,
997 'ltj.direction', 10000)
999 function luatexja.direction.remove_end_whatsit()
1000 local h=tex.lists.page_head
1001 if h and (not h.next) and
1002 h.id==id_whatsit and h.subtype==sid_user and
1003 h.user_id == DIR then
1004 tex.lists.page_head = nil
1012 local function dir_adjust_buildpage(info)
1014 local head = to_direct(tex.lists.contrib_head)
1016 head = make_dir_whatsit(head,
1020 tex.lists.contrib_head = to_node(head)
1024 luatexbase.add_to_callback('buildpage_filter',
1025 dir_adjust_buildpage,
1026 'ltj.direction', 10000)
1029 -- finalize (executed just before \shipout)
1030 -- we supply correct pdfsavematrix nodes etc. inside dir_node
1032 local finalize_inner
1033 local function finalize_dir_node(db,new_dir)
1034 local b = getlist(db)
1036 local w = getfield(b, 'width')
1037 local h = getfield(b, 'height')
1038 local d = getfield(b, 'depth')
1039 local dn_w = getfield(db, 'width')
1040 local dn_h = getfield(db, 'height')
1041 local dn_d = getfield(db, 'depth')
1042 local db_head, db_tail
1043 for _,v in ipairs(dir_node_aux
1044 [get_box_dir(b, dir_yoko)%dir_math_mod][new_dir][getid(b)]) do
1045 local cmd, arg, nn = v[1], v[2]
1047 nn = node_new(id_kern)
1048 setfield(nn, 'kern', arg(w, h, d, dn_w, dn_h, dn_d))
1049 elseif cmd=='whatsit' then
1050 nn = node_new(id_whatsit, arg)
1051 elseif cmd=='rotate' then
1052 nn = node_new(id_whatsit, sid_matrix)
1053 setfield(nn, 'data', arg)
1054 elseif cmd=='box' then
1055 nn = b; setfield(b, 'next', nil)
1056 setfield(nn, 'shift', arg(w, h, d, dn_w, dn_h, dn_d))
1059 insert_after(db_head, db_tail, nn)
1062 setfield(db, 'head', nn)
1063 db_head, db_tail = nn, nn
1068 tex.setattribute(attr_dir, dir_yoko)
1069 local shipout_temp = node_new(id_hlist)
1070 tex.setattribute(attr_dir, 0)
1072 finalize_inner = function (box)
1073 for n in traverse(getlist(box)) do
1074 local nid = getid(n)
1075 if (nid==id_hlist or nid==id_vlist) then
1076 local ndir = get_box_dir(n, dir_yoko)
1077 if ndir>=dir_node_auto then -- n is dir_node
1078 finalize_dir_node(n, ndir%dir_math_mod)
1085 local getbox = tex.getbox
1086 local setbox, copy = Dnode.setbox, Dnode.copy
1087 local lua_mem_kb = 0
1088 function luatexja.direction.finalize()
1089 local a = to_direct(tex.getbox("AtBeginShipoutBox"))
1090 local a_dir = get_box_dir(a, dir_yoko)
1091 if a_dir~=dir_yoko then
1092 local b = create_dir_node(a, a_dir, dir_yoko, false)
1093 setfield(b, 'head', a); a = b
1095 setfield(shipout_temp, 'head', a)
1096 finalize_inner(shipout_temp)
1097 setbox('global', "AtBeginShipoutBox", copy(getlist(shipout_temp)))
1098 setfield(shipout_temp, 'head',nil)
1101 --local m = collectgarbage('count')
1102 --if m>lua_mem_kb+20480 then
1103 -- collectgarbage(); lua_mem_kb = collectgarbage('count')
1105 --print('Lua Memory Usage', lua_mem_kb)