+ return hd
+ end
+
+ -- lastbox
+ local node_prev = (node.direct~=node) and node.direct.getprev or node.prev
+ local function lastbox_hook()
+ start_time_measure('box_primitive_hook')
+ local bn = tex_nest[tex_nest.ptr].tail
+ if bn then
+ local b, head = to_direct(bn), to_direct(tex_nest[tex_nest.ptr].head)
+ local bid = getid(b)
+ if bid==id_hlist or bid==id_vlist then
+ local box_dir = get_box_dir(b, 0)
+ if box_dir>= dir_node_auto then -- unwrap dir_node
+ local p = node_prev(b)
+ local dummy1, dummy2, nb = unwrap_dir_node(b, nil, box_dir)
+ setfield(p, 'next', nb); tex_nest[tex_nest.ptr].tail = to_node(nb)
+ setfield(b, 'next', nil); setfield(b, 'head', nil)
+ node_free(b); b = nb
+ end
+ local _, wh = get_box_dir(b, 0) -- clean dir_node attached to the box
+ if wh then
+ node.direct.flush_list(getfield('value', wh))
+ setfield(wh, 'value', nil)
+ end
+ end
+ end
+ stop_time_measure('box_primitive_hook')
+ end
+
+ luatexja.direction.make_dir_whatsit = make_dir_whatsit
+ luatexja.direction.lastbox_hook = lastbox_hook
+end
+
+-- \wd, \ht, \dp の代わり
+do
+ local getbox, setdimen = tex.getbox, tex.setdimen
+ local function get_box_dim_common(key, s, l_dir)
+ -- s: not dir_node.
+ local s_dir, wh = get_box_dir(s, dir_yoko)
+ s_dir = s_dir%dir_math_mod
+ if s_dir ~= l_dir then
+ local not_found = true
+ for x in traverse(getfield(wh, 'value')) do
+ if l_dir == has_attr(x, attr_dir)%dir_node_auto then
+ setdimen('ltj@tempdima', getfield(x, key))
+ not_found = false; break
+ end
+ end
+ if not_found then
+ local w = getfield(s, 'width')
+ local h = getfield(s, 'height')
+ local d = getfield(s, 'depth')
+ setdimen('ltj@tempdima',
+ dir_node_aux[s_dir][l_dir][key](w,h,d))
+ end
+ else
+ setdimen('ltj@tempdima', getfield(s, key))
+ end
+ end
+ local function get_box_dim(key, n)
+ local gt = tex.globaldefs; tex.globaldefs = 0
+ local s = getbox(n)
+ if s then
+ local l_dir = (get_dir_count())%dir_math_mod
+ s = to_direct(s)
+ local b_dir = get_box_dir(s,dir_yoko)
+ if b_dir<dir_node_auto then
+ get_box_dim_common(key, s, l_dir)
+ elseif b_dir%dir_math_mod==l_dir then
+ setdimen('ltj@tempdima', getfield(s, key))
+ else
+ get_box_dim_common(key, getlist(s), l_dir)
+ end
+ else
+ setdimen('ltj@tempdima', 0)
+ end
+ tex.sprint(cat_lp, '\\ltj@tempdima')
+ tex.globaldefs = gt
+ end
+ luatexja.direction.get_box_dim = get_box_dim
+
+ -- return value: (changed dimen of box itself?)
+ local scan_dimen, scan_int = luatexja.token.scan_dimen, luatexja.token.scan_int
+ local scan_keyword = luatexja.token.scan_keyword
+ local function set_box_dim_common(key, s, l_dir)
+ local s_dir, wh = get_box_dir(s, dir_yoko)
+ s_dir = s_dir%dir_math_mod
+ if s_dir ~= l_dir then
+ if not wh then
+ wh = create_dir_whatsit(getlist(s), 'set_box_dim', s_dir)
+ setfield(s, 'head', wh)
+ end
+ local db
+ local dnh = getfield(wh, 'value')
+ for x in traverse(dnh) do
+ if has_attr(x, attr_dir)%dir_node_auto==l_dir then
+ db = x; break
+ end
+ end
+ if not db then
+ db = create_dir_node(s, s_dir, l_dir, true)
+ setfield(db, 'next', dnh)
+ setfield(wh, 'value',to_node(db))
+ end
+ setfield(db, key, scan_dimen())
+ return false
+ else
+ setfield(s, key, scan_dimen())
+ if wh then
+ -- change dimension of dir_nodes which are created "automatically"
+ local bw, bh, bd
+ = getfield(s,'width'), getfield(s, 'height'), getfield(s, 'depth')
+ for x in traverse(getfield(wh, 'value')) do
+ local x_dir = has_attr(x, attr_dir)
+ if x_dir<dir_node_manual then
+ local info = dir_node_aux[s_dir][x_dir%dir_node_auto]
+ setfield(x, 'width', info.width(bw,bh,bd))
+ setfield(x, 'height', info.height(bw,bh,bd))
+ setfield(x, 'depth', info.depth(bw,bh,bd))
+ end
+ end
+ end
+ return true
+ end
+ end
+ local function set_box_dim(key)
+ local s = getbox(scan_int()); scan_keyword('=')
+ if s then
+ local l_dir = (get_dir_count())%dir_math_mod
+ s = to_direct(s)
+ local b_dir = get_box_dir(s,dir_yoko)
+ if b_dir<dir_node_auto then
+ set_box_dim_common(key, s, l_dir)
+ elseif b_dir%dir_math_mod == l_dir then
+ -- s is dir_node
+ setfield(s, key, scan_dimen())
+ if b_dir<dir_node_manual then
+ set_attr(s, attr_dir, b_dir%dir_node_auto + dir_node_manual)
+ end
+ else
+ local sid, b = getid(s), getlist(s)
+ local info = dir_node_aux[get_box_dir(b,dir_yoko)%dir_math_mod][b_dir%dir_node_auto]
+ local bw, bh, bd
+ = getfield(b,'width'), getfield(b, 'height'), getfield(b, 'depth')
+ local sw, sh, sd
+ = getfield(s,'width'), getfield(s, 'height'), getfield(s, 'depth')
+ if set_box_dim_common(key, b, l_dir) and b_dir<dir_node_manual then
+ -- re-calculate dimension of s, if s is created "automatically"
+ if b_dir<dir_node_manual then
+ setfield(s, 'width', info.width(bw,bh,bd))
+ setfield(s, 'height', info.height(bw,bh,bd))
+ setfield(s, 'depth', info.depth(bw,bh,bd))
+ end
+ end
+ end
+ end
+ end
+ luatexja.direction.set_box_dim = set_box_dim
+end
+
+do
+ local getbox = tex.getbox
+ local function get_register_dir(n)
+ local s = getbox(n)
+ if s then
+ s = to_direct(s)
+ local b_dir = get_box_dir(s, dir_yoko)
+ if b_dir<dir_node_auto then
+ return b_dir
+ else
+ local b_dir = get_box_dir(
+ node_next(node_next(node_next(getlist(s)))), dir_yoko)
+ return b_dir
+ end
+ else
+ return 0
+ end
+ end
+ luatexja.direction.get_register_dir = get_register_dir
+end
+
+do
+ local getbox, setbox, copy_list = tex.getbox, tex.setbox, node.direct.copy_list
+ -- raise, lower
+ function luatexja.direction.raise_box()
+ start_time_measure('box_primitive_hook')
+ local list_dir = get_dir_count()
+ local s = getbox('ltj@afbox')
+ if s then
+ local sd = to_direct(s)
+ local box_dir = get_box_dir(sd, dir_yoko)
+ if box_dir%dir_math_mod ~= list_dir then
+ setbox(
+ 'ltj@afbox',
+ to_node(copy_list(make_dir_whatsit(sd, sd, list_dir, 'box_move')))
+ -- copy_list しないとリストの整合性が崩れる……?
+ )
+ end
+ end
+ stop_time_measure('box_primitive_hook')
+ end
+end
+
+-- PACKED の hbox から文字を取り出す
+-- luatexja.jfmglue.check_box などで使用
+do
+ local function glyph_from_packed(h)
+ local b = getlist(h)
+ return (getid(b)==id_kern or (getid(b)==id_whatsit and getsubtype(b)==sid_save) )
+ and node_next(node_next(node_next(b))) or b
+ end
+ luatexja.direction.glyph_from_packed = glyph_from_packed
+end
+
+-- adjust
+do
+ local id_adjust = node.id('adjust')
+ function luatexja.direction.check_adjust_direction()
+ start_time_measure('box_primitive_hook')
+ local list_dir = get_adjust_dir_count()
+ local a = tex_nest[tex_nest.ptr].tail
+ local ad = to_direct(a)
+ if a and getid(ad)==id_adjust then
+ local adj_dir = get_box_dir(ad)
+ if list_dir~=adj_dir then
+ ltjb.package_error(
+ 'luatexja',
+ 'Direction Incompatible',
+ "\\vadjust's argument and outer vlist must have same direction.")
+ node.direct.last_node()
+ end
+ end
+ stop_time_measure('box_primitive_hook')
+ end
+end
+
+-- insert
+do
+ local id_ins = node.id('ins')
+ local id_rule = node.id('rule')
+ function luatexja.direction.populate_insertion_dir_whatsit()
+ start_time_measure('box_primitive_hook')
+ local list_dir = get_dir_count()
+ local a = tex_nest[tex_nest.ptr].tail
+ local ad = to_direct(a)
+ if a and getid(ad)==id_ins then
+ local h = getfield(ad, 'head')
+ if getid(h)==id_whatsit and
+ getsubtype(h)==sid_user and getfield(h, 'user_id')==DIR then
+ local n = h; h = node_remove(h,h)
+ node_free(n)
+ end
+ for box_rule in traverse(h) do
+ if getid(box_rule)<id_rule then
+ h = insert_before(h, box_rule, dir_pool[list_dir]())
+ end
+ end
+ ensure_tex_attr(attr_dir, 0)
+ setfield(ad, 'head', h)
+ end
+ stop_time_measure('box_primitive_hook')
+ end
+end
+
+-- vsplit
+do
+ local split_dir_whatsit
+ local function dir_adjust_vpack(h, gc)
+ start_time_measure('direction_vpack')
+ local hd = to_direct(h)
+ if gc=='split_keep' then
+ -- supply dir_whatsit
+ hd = create_dir_whatsit_vbox(hd, gc)
+ split_dir_whatsit = hd
+ elseif gc=='split_off' then
+ for bh in traverse_id(id_whatsit, hd) do
+ if getsubtype(bh)==sid_user and getfield(bh, 'user_id')==DIR then
+ ltjs.list_dir = has_attr(bh, attr_dir); break
+ end
+ end
+ -- local bh=hd
+ -- for i=1,2 do
+ -- if bh and getid(bh)==id_whatsit
+ -- and getsubtype(bh)==sid_user and getfield(bh, 'user_id')==DIR then
+ -- ltjs.list_dir = has_attr(bh, attr_dir); break
+ -- end
+ -- bh = node_next(bh)
+ -- end
+ if split_dir_whatsit then
+ -- adjust direction of 'split_keep'
+ set_attr(split_dir_whatsit, attr_dir, ltjs.list_dir)
+ end
+ split_dir_whatsit=nil
+ elseif gc=='preamble' then
+ split_dir_whatsit=nil
+ else
+ adjust_badness(hd)
+ hd = process_dir_node(create_dir_whatsit_vbox(hd, gc), gc)
+ split_dir_whatsit=nil
+ end
+ stop_time_measure('direction_vpack')
+ return to_node(hd)
+ end
+ ltjb.add_to_callback('vpack_filter',
+ dir_adjust_vpack,
+ 'ltj.direction', 10000)
+end
+
+do
+ -- supply direction whatsit to the main vertical list "of the next page"
+ local function dir_adjust_pre_output(h, gc)
+ return to_node(create_dir_whatsit_vbox(to_direct(h), gc))
+ end
+ ltjb.add_to_callback('pre_output_filter',
+ dir_adjust_pre_output,
+ 'ltj.direction', 10000)
+
+ function luatexja.direction.remove_end_whatsit()
+ local h=tex.lists.page_head
+ if h and (not h.next) and
+ h.id==id_whatsit and h.subtype==sid_user and
+ h.user_id == DIR then
+ tex.lists.page_head = nil
+ node.free(h)
+ end
+ end
+end
+
+-- buildpage filter
+do
+ local function dir_adjust_buildpage(info)
+ if info=='box' then
+ local head = to_direct(tex.lists.contrib_head)
+ if head then
+ head = make_dir_whatsit(head,
+ node_tail(head),
+ get_dir_count(),
+ 'buildpage')
+ tex.lists.contrib_head = to_node(head)
+ end
+ end
+ end
+ ltjb.add_to_callback('buildpage_filter',
+ dir_adjust_buildpage,
+ 'ltj.direction', 10000)
+end
+
+-- finalize (executed just before \shipout)
+-- we supply correct pdfsavematrix nodes etc. inside dir_node
+do
+ local finalize_inner
+ local function finalize_dir_node(db,new_dir)
+ local b = getlist(db)
+ finalize_inner(b)
+ local w = getfield(b, 'width')
+ local h = getfield(b, 'height')
+ local d = getfield(b, 'depth')
+ local dn_w = getfield(db, 'width')
+ local dn_h = getfield(db, 'height')
+ local dn_d = getfield(db, 'depth')
+ local db_head, db_tail
+ for _,v in ipairs(dir_node_aux
+ [get_box_dir(b, dir_yoko)%dir_math_mod][new_dir][getid(b)]) do
+ local cmd, arg, nn = v[1], v[2]
+ if cmd=='kern' then
+ nn = node_new(id_kern)
+ setfield(nn, 'kern', arg(w, h, d, dn_w, dn_h, dn_d))
+ elseif cmd=='whatsit' then
+ nn = node_new(id_whatsit, arg)
+ elseif cmd=='rotate' then
+ nn = node_new(id_whatsit, sid_matrix)
+ setfield(nn, 'data', arg)
+ elseif cmd=='box' then
+ nn = b; setfield(b, 'next', nil)
+ setfield(nn, 'shift', arg(w, h, d, dn_w, dn_h, dn_d))
+ end
+ if db_head then
+ insert_after(db_head, db_tail, nn)
+ db_tail = nn
+ else
+ setfield(db, 'head', nn)
+ db_head, db_tail = nn, nn
+ end
+ end
+ end
+
+ tex.setattribute(attr_dir, dir_yoko)
+ local shipout_temp = node_new(id_hlist)
+ tex.setattribute(attr_dir, 0)
+
+ finalize_inner = function (box)
+ for n in traverse(getlist(box)) do
+ local nid = getid(n)
+ if (nid==id_hlist or nid==id_vlist) then
+ local ndir = get_box_dir(n, dir_yoko)
+ if ndir>=dir_node_auto then -- n is dir_node
+ finalize_dir_node(n, ndir%dir_math_mod)
+ else
+ finalize_inner(n)
+ end
+ end
+ end
+ end
+ local getbox = tex.getbox
+ local setbox, copy = node.direct.setbox, node.direct.copy
+ local lua_mem_kb = 0
+ function luatexja.direction.finalize()
+ local a = to_direct(tex.getbox("AtBeginShipoutBox"))
+ local a_dir = get_box_dir(a, dir_yoko)
+ if a_dir~=dir_yoko then
+ local b = create_dir_node(a, a_dir, dir_yoko, false)
+ setfield(b, 'head', a); a = b
+ end
+ setfield(shipout_temp, 'head', a)
+ finalize_inner(shipout_temp)
+ setbox('global', "AtBeginShipoutBox", copy(getlist(shipout_temp)))
+ setfield(shipout_temp, 'head',nil)
+
+ -- garbage collect
+ --local m = collectgarbage('count')
+ --if m>lua_mem_kb+20480 then
+ -- collectgarbage(); lua_mem_kb = collectgarbage('count')
+ --end
+ --print('Lua Memory Usage', lua_mem_kb)