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.load_module('rmlgbm'); local ltjr = luatexja.rmlgbm
8 luatexja.direction = {}
10 local attr_dir = luatexbase.attributes['ltj@dir']
11 local attr_icflag = luatexbase.attributes['ltj@icflag']
13 local cat_lp = luatexbase.catcodetables['latex-package']
14 local Dnode = node.direct or node
15 local nullfunc = function (n) return n end
16 local to_node = (Dnode ~= node) and Dnode.tonode or nullfunc
17 local to_direct = (Dnode ~= node) and Dnode.todirect or nullfunc
18 local has_attr = Dnode.has_attribute
19 local set_attr = Dnode.set_attribute
20 local insert_before = Dnode.insert_before
21 local insert_after = Dnode.insert_after
22 local getid = (Dnode ~= node) and Dnode.getid or function(n) return n.id end
23 local getsubtype = (Dnode ~= node) and Dnode.getsubtype or function(n) return n.subtype end
24 local getlist = (Dnode ~= node) and Dnode.getlist or function(n) return n.head end
25 local setfield = (Dnode ~= node) and Dnode.setfield or function(n, i, c) n[i] = c end
26 local getfield = (Dnode ~= node) and Dnode.getfield or function(n, i) return n[i] end
27 local node_new = Dnode.new
28 local node_tail = Dnode.tail
29 local node_free = Dnode.free
30 local node_remove = Dnode.remove
31 local node_next = (Dnode ~= node) and Dnode.getnext or node.next
32 local traverse = Dnode.traverse
33 local traverse_id = Dnode.traverse_id
34 local start_time_measure, stop_time_measure
35 = ltjb.start_time_measure, ltjb.stop_time_measure
38 local id_kern = node.id('kern')
39 local id_hlist = node.id('hlist')
40 local id_vlist = node.id('vlist')
41 local id_whatsit = node.id('whatsit')
42 local sid_save = node.subtype('pdf_save')
43 local sid_restore = node.subtype('pdf_restore')
44 local sid_matrix = node.subtype('pdf_setmatrix')
45 local sid_user = node.subtype('user_defined')
47 local tex_nest = tex.nest
48 local tex_getcount = tex.getcount
49 local tex_set_attr = tex.setattribute
50 local PROCESSED = luatexja.icflag_table.PROCESSED
51 local PROCESSED_BEGIN_FLAG = luatexja.icflag_table.PROCESSED_BEGIN_FLAG
52 local PACKED = luatexja.icflag_table.PACKED
53 local STCK = luatexja.userid_table.STCK
54 local DIR = luatexja.userid_table.DIR
55 local dir_tate = luatexja.dir_table.dir_tate
56 local dir_yoko = luatexja.dir_table.dir_yoko
57 local dir_dtou = luatexja.dir_table.dir_dtou
58 local dir_utod = luatexja.dir_table.dir_utod
59 local dir_node_auto = luatexja.dir_table.dir_node_auto
60 local dir_node_manual = luatexja.dir_table.dir_node_manual
64 local function adjust_badness(hd)
65 if not node_next(hd) and getid(hd)==id_whatsit and getsubtype(hd)==sid_user
66 and getfield(hd, 'user_id')==DIR then
67 -- avoid double whatsit
68 luatexja.global_temp=tex.globaldefs; tex.globaldefs=0
69 luatexja.hbadness_temp=tex.hbadness; tex.hbadness=10000
70 luatexja.vbadness_temp=tex.vbadness; tex.vbadness=10000
72 luatexja.global_temp = nil
73 luatexja.hbadness_temp=nil
74 luatexja.vbadness_temp=nil
78 local get_dir_count, get_adjust_dir_count
80 local function get_dir_count_inner(h)
82 if h.id==id_whatsit and h.subtype==sid_user and h.user_id==DIR then
83 local ic = node.has_attribute(h, attr_icflag)
84 return (ic<PROCESSED_BEGIN_FLAG)
85 and (node.has_attribute(h,attr_dir)%dir_node_auto) or 0
93 function get_dir_count()
94 for i=tex_nest.ptr, 1, -1 do
95 local h = tex_nest[i].head.next
96 --luatexja.ext_show_node_list(h, 'GDR'.. i .. '> ', print)
98 local t = get_dir_count_inner(h)
99 if t~=0 then return t end
102 return page_direction
104 function get_adjust_dir_count()
105 for i=tex_nest.ptr, 1, -1 do
106 local v = tex_nest[i]
107 local h, m = v.head.next, v.mode
108 if abs(m)== ltjs.vmode and h then
109 local t = get_dir_count_inner(h)
110 if t~=0 then return t end
113 return page_direction
115 luatexja.direction.get_dir_count = get_dir_count
116 luatexja.direction.get_adjust_dir_count = get_adjust_dir_count
122 local node_next = node.next
123 local node_set_attr = node.set_attribute
124 local function set_list_direction(v, name)
125 local lv, w = tex_nest.ptr, tex.lists.page_head
127 v,name = get_dir_count(), nil
129 v,name = get_adjust_dir_count(), nil
130 elseif v=='math' then
131 v,name = get_dir_count(), nil
132 if abs(tex_nest[lv].mode) == ltjs.mmode and v == dir_tate then
136 if tex.currentgrouptype==6 then
139 "You can't use `\\" .. name .. "' in an align",
140 "To change direction in an align, \n"
141 .. "you shold use \\hbox or \\vbox.")
143 local w = (lv==0) and tex.lists.page_head or tex_nest[lv].head.next
146 w.id==id_whatsit and w.subtype==sid_user and w.user_id==DIR then
147 node_set_attr(w, attr_dir, v)
148 if lv==0 then page_direction = v end
149 elseif lv==0 and not page_direction then
150 page_direction = v -- for first call of \yoko (in luatexja-core.sty)
154 "Use `\\" .. name .. "' at top of list",
155 'Direction change command by LuaTeX-ja is available\n'
156 .. 'only when the current list is null.')
159 local w = node_new(id_whatsit, sid_user)
160 setfield(w, 'next', nil)
161 setfield(w, 'user_id', DIR)
162 setfield(w, 'type', 110)
163 set_attr(w, attr_dir, v)
165 if lv==0 then page_direction = v end
167 tex_set_attr('global', attr_icflag, 0)
169 tex_set_attr('global', attr_dir, 0)
171 luatexja.direction.set_list_direction = set_list_direction
174 -- ボックスに dir whatsit を追加
175 local function create_dir_whatsit(hd, gc, new_dir)
176 if getid(hd)==id_whatsit and
177 getsubtype(hd)==sid_user and getfield(hd, 'user_id')==DIR then
178 set_attr(hd, attr_icflag,
179 (has_attr(hd, attr_icflag) or 0)%PROCESSED_BEGIN_FLAG
180 + PROCESSED_BEGIN_FLAG)
181 tex_set_attr('global', attr_icflag, 0)
184 local w = node_new(id_whatsit, sid_user)
185 setfield(w, 'next', hd)
186 setfield(w, 'user_id', DIR)
187 setfield(w, 'type', 110)
188 set_attr(w, attr_dir, new_dir)
189 set_attr(w, attr_icflag, PROCESSED_BEGIN_FLAG)
190 set_attr(hd, attr_icflag,
191 (has_attr(hd, attr_icflag) or 0)%PROCESSED_BEGIN_FLAG
192 + PROCESSED_BEGIN_FLAG)
193 tex_set_attr('global', attr_dir, 0)
194 tex_set_attr('global', attr_icflag, 0)
199 -- hpack_filter, vpack_filter, post_line_break_filter
200 -- の結果を組方向を明示するため,先頭に dir_node を設置
202 local function create_dir_whatsit_hpack(h, gc)
203 local hd = to_direct(h)
204 if gc=='fin_row' or gc == 'preamble' then
206 set_attr(hd, attr_icflag, PROCESSED_BEGIN_FLAG)
207 tex_set_attr('global', attr_icflag, 0)
212 return to_node(create_dir_whatsit(hd, gc, ltjs.list_dir))
216 luatexbase.add_to_callback('hpack_filter',
217 create_dir_whatsit_hpack, 'ltj.create_dir_whatsit', 10000)
221 local function create_dir_whatsit_parbox(h, gc)
222 stop_time_measure('tex_linebreak')
223 -- start 側は ltj-debug.lua に
224 local new_dir, hd = ltjs.list_dir, to_direct(h)
225 for line in traverse_id(id_hlist, hd) do
226 local nh = getlist(line)
227 setfield(line, 'head', create_dir_whatsit(nh, gc, new_dir) )
228 set_attr(line, attr_dir, new_dir)
230 tex_set_attr('global', attr_dir, 0)
233 luatexbase.add_to_callback('post_linebreak_filter',
234 create_dir_whatsit_parbox, 'ltj.create_dir_whatsit', 10000)
237 local create_dir_whatsit_vbox
240 local id_glue, sid_parskip = node.id('glue'), 3
241 create_dir_whatsit_vbox = function (hd, gc)
242 ltjs.list_dir = get_dir_count()
243 -- remove dir whatsit
244 for x in traverse_id(id_whatsit, hd) do
245 if getsubtype(x)==sid_user and getfield(x, 'user_id')==DIR then
250 ltjs.list_dir =has_attr(hd,attr_dir)
251 local x = node_next(hd)
252 if getid(x)==id_glue and getsubtype(x)==sid_parskip then
253 node_remove(hd,x); node_free(x)
257 hd = node_remove(hd, wh[i]); node_free(wh[i]); wh[i] = nil
259 if gc=='fin_row' or gc == 'preamble' then
261 set_attr(hd, attr_icflag, PROCESSED_BEGIN_FLAG)
262 tex_set_attr('global', attr_icflag, 0)
266 local n =node_next(hd)
268 local w = create_dir_whatsit(hd, gc, ltjs.list_dir)
269 -- move dir whatsit after hd
270 setfield(hd, 'next', w); setfield(w, 'next', n)
273 hd = create_dir_whatsit(hd, gc, ltjs.list_dir)
280 -- dir_node に包む方法を書いたテーブル
283 local get_h =function (w,h,d) return h end
284 local get_d =function (w,h,d) return d end
285 local get_h_d =function (w,h,d) return h+d end
286 local get_h_d_neg =function (w,h,d) return -h-d end
287 local get_h_neg =function (w,h,d) return -h end
288 local get_d_neg =function (w,h,d) return -d end
289 local get_w_half =function (w,h,d) return 0.5*w end
290 local get_w_neg_half =function (w,h,d) return -0.5*w end
291 local get_w_neg =function (w,h,d) return -w end
292 local get_w =function (w,h,d) return w end
293 local zero = function() return 0 end
295 [dir_yoko] = { -- yoko を
296 [dir_tate] = { -- tate 中で組む
301 { 'whatsit', sid_save },
302 { 'rotate', '0 1 -1 0' },
303 { 'kern', get_w_neg_half },
305 { 'kern', get_w_neg_half },
306 { 'whatsit', sid_restore },
309 { 'whatsit', sid_save },
310 { 'rotate', '0 1 -1 0' },
312 { 'box' , get_w_neg },
313 { 'kern', get_h_d_neg},
314 { 'whatsit', sid_restore },
317 [dir_dtou] = { -- dtou 中で組む
322 { 'whatsit', sid_save },
323 { 'rotate', '0 -1 1 0' },
324 { 'kern', get_w_neg },
325 { 'box', get_d_neg },
326 { 'whatsit', sid_restore },
329 { 'whatsit', sid_save },
330 { 'rotate', '0 -1 1 0' },
331 { 'kern', get_h_d_neg },
333 { 'whatsit', sid_restore },
337 [dir_tate] = { -- tate を
338 [dir_yoko] = { -- yoko 中で組む
343 { 'whatsit', sid_save },
344 { 'rotate', '0 -1 1 0' },
345 { 'kern', get_w_neg },
346 { 'box' , get_d_neg },
347 { 'whatsit', sid_restore },
350 { 'whatsit', sid_save },
351 { 'rotate', '0 -1 1 0' },
352 { 'kern', get_h_d_neg },
354 { 'whatsit', sid_restore },
357 [dir_dtou] = { -- dtou 中で組む
362 { 'whatsit', sid_save },
363 { 'rotate', '-1 0 0 -1' },
364 { 'kern', get_w_neg },
366 { 'whatsit', sid_restore },
369 { 'whatsit', sid_save },
370 { 'rotate', '-1 0 0 -1' },
371 { 'kern', get_h_d_neg },
372 { 'box', get_w_neg },
373 { 'whatsit', sid_restore },
376 [dir_utod] = { -- utod 中で組む
396 [dir_dtou] = { -- dtou を
397 [dir_yoko] = { -- yoko 中で組む
402 { 'whatsit', sid_save },
403 { 'rotate', '0 1 -1 0' },
406 { 'kern', get_w_neg },
407 { 'whatsit', sid_restore },
411 { 'whatsit', sid_save },
412 { 'rotate', '0 1 -1 0' },
413 { 'box', get_w_neg },
414 { 'kern', get_h_d_neg },
415 { 'whatsit', sid_restore },
418 [dir_tate] = { -- tate 中で組む
423 { 'whatsit', sid_save },
424 { 'rotate', '-1 0 0 -1' },
425 { 'kern', get_w_neg },
427 { 'whatsit', sid_restore },
430 { 'whatsit', sid_save },
431 { 'rotate', ' -1 0 0 -1' },
432 { 'kern', get_h_d_neg },
433 { 'box', get_w_neg },
434 { 'whatsit', sid_restore },
439 dir_node_aux[dir_yoko][dir_utod] = dir_node_aux[dir_yoko][dir_tate]
440 dir_node_aux[dir_dtou][dir_utod] = dir_node_aux[dir_dtou][dir_tate]
441 dir_node_aux[dir_tate][dir_tate] = dir_node_aux[dir_tate][dir_utod]
442 dir_node_aux[dir_utod] = dir_node_aux[dir_tate]
445 -- 1st ret val: b の組方向
446 -- 2nd ret val はその DIR whatsit
447 local function get_box_dir(b, default)
448 start_time_measure('get_box_dir')
449 local dir = has_attr(b, attr_dir) or 0
450 local bh = getfield(b,'head')
451 -- b は insert node となりうるので getlist() は使えない
454 if bh and getid(bh)==id_whatsit
455 and getsubtype(bh)==sid_user and getfield(bh, 'user_id')==DIR then
457 dir = (dir==0) and has_attr(bh, attr_dir) or dir
461 stop_time_measure('get_box_dir')
462 return (dir==0 and default or dir), c
466 local getbox = tex.getbox
467 local function check_dir(reg_num)
468 start_time_measure('box_primitive_hook')
469 local list_dir = get_dir_count()
470 local b = getbox(tex_getcount('ltj@tempcnta'))
472 local box_dir = get_box_dir(to_direct(b), dir_yoko)
473 if box_dir%dir_node_auto ~= list_dir then
474 -- print('NEST', tex_nest.ptr, tex_getcount('ltj@tempcnta'))
475 -- luatexja.ext_show_node_list(
476 -- (tex_nest.ptr==0) and tex.lists.page_head or tex_nest[tex_nest.ptr].head,
477 -- 'LIST' .. tostring(list_dir) .. '> ', print)
478 -- luatexja.ext_show_node_list(b, 'BOX' .. tostring(box_dir) .. '> ', print)
481 "Incompatible direction list can't be unboxed",
482 'I refuse to unbox a box in differrent direction.')
485 if luatexja.global_temp and tex.globaldefs~=luatexja.global_temp then
486 tex.globaldefs = luatexja.global_temp
488 stop_time_measure('box_primitive_hook')
490 luatexja.direction.check_dir = check_dir
493 -- dir_node に包まれている「本来の中身」を取り出し,
495 local function unwrap_dir_node(b, head, box_dir)
496 -- b: dir_node, head: the head of list, box_dir:
497 -- return values are (new head), (next of b), (contents), (dir of contents)
498 local bh = getlist(b)
499 local bc = node_next(node_next(node_next(bh)))
504 nh = insert_before(head, b, bc)
505 nh, nb = node_remove(nh, b)
506 setfield(b, 'next', nil)
507 setfield(b, 'head', nil)
510 local shift_old, b_dir, wh = nil, get_box_dir(bc, 0)
512 Dnode.flush_list(getfield(wh, 'value'))
513 setfield(wh, 'value', nil)
516 local info = dir_node_aux[b_dir][box_dir%dir_node_auto][getid(bc)]
517 for _,v in ipairs(info) do
520 getfield(bc,'width'), getfield(bc, 'height'), getfield(bc, 'depth'))
524 setfield(bc, 'shift', getfield(bc, 'shift') - shift_old)
525 return nh, nb, bc, b_dir
528 -- is_manual: 寸法変更に伴うものか?
529 local function create_dir_node(b, b_dir, new_dir, is_manual)
530 --print('create new node', b_dir, new_dir)
531 local info = dir_node_aux[b_dir][new_dir]
532 local w = getfield(b, 'width')
533 local h = getfield(b, 'height')
534 local d = getfield(b, 'depth')
535 local db = node_new(getid(b)) -- dir_node
536 set_attr(db, attr_dir,
537 new_dir + (is_manual and dir_node_manual or dir_node_auto))
538 set_attr(db, attr_icflag, PROCESSED)
539 set_attr(b, attr_icflag, PROCESSED)
540 tex_set_attr('global', attr_dir, 0)
541 tex_set_attr('global', attr_icflag, 0)
542 setfield(db, 'dir', getfield(b, 'dir'))
543 setfield(db, 'shift', 0)
544 setfield(db, 'width', info.width(w,h,d))
545 setfield(db, 'height', info.height(w,h,d))
546 setfield(db, 'depth', info.depth(w,h,d))
551 local make_dir_whatsit, process_dir_node
553 make_dir_whatsit = function (head, b, new_dir, origin)
554 -- head: list head, b: box
555 -- origin: コール元 (for debug)
556 -- return value: (new head), (next of b), (new b), (is_b_dir_node)
557 -- (new b): b か dir_node に被せられた b
558 local bh = getlist(b)
559 local box_dir, dn = get_box_dir(b, ltjs.list_dir)
560 -- 既に b の中身にあるwhatsit
562 if box_dir==new_dir then
563 -- 組方向が一緒のボックスなので,何もしなくて良い
564 return head, node_next(b), b, false
565 elseif box_dir%dir_node_auto == new_dir then
566 -- dir_node としてカプセル化されている
567 local bc = node_next(node_next(node_next(bh)))
568 local _, dnc = get_box_dir(b, 0)
569 if dnc then -- free all other dir_node
570 Dnode.flush_list(getfield(dnc, 'value'))
571 setfield(dnc, 'value', nil)
573 set_attr(b, attr_dir, box_dir%dir_node_auto + dir_node_auto)
574 return head, node_next(b), b, true
576 --luatexja.ext_show_node_list(to_node(b), 'mkd> ', print)
578 local nh, nb, ret, flag
579 if box_dir>= dir_node_auto then -- unwrap
581 head, nb, b, b_dir = unwrap_dir_node(b, head, box_dir)
583 if b_dir==new_dir then
584 -- dir_node の中身が周囲の組方向とあっている
585 return head, nb, b, false
586 else box_dir = b_dir end
589 local dnh = getfield(dn, 'value')
590 for x in traverse(dnh) do
591 if has_attr(x, attr_dir)%dir_node_auto == new_dir then
592 setfield(dn, 'value', to_node(node_remove(dnh, x)))
596 Dnode.flush_list(dnh)
597 db = db or create_dir_node(b, box_dir, new_dir, false)
598 local w = getfield(b, 'width')
599 local h = getfield(b, 'height')
600 local d = getfield(b, 'depth')
601 nh, nb = insert_before(head, b, db), nil
602 nh, nb = node_remove(nh, b)
603 local db_head, db_tail = nil
604 for _,v in ipairs(dir_node_aux[box_dir][new_dir][getid(b)]) do
605 local cmd, arg, nn = v[1], v[2]
607 nn = node_new(id_kern)
608 setfield(nn, 'kern', arg(w, h, d))
609 elseif cmd=='whatsit' then
610 nn = node_new(id_whatsit, arg)
611 elseif cmd=='rotate' then
612 nn = node_new(id_whatsit, sid_matrix)
613 setfield(nn, 'data', arg)
614 elseif cmd=='box' then
615 nn = b; setfield(b, 'next', nil)
616 setfield(nn, 'shift', getfield(nn, 'shift') + arg(w,h,d))
617 elseif cmd=='nop' then
618 nn = node_new(id_kern)
619 setfield(nn, 'kern', 0)
622 insert_after(db_head, db_tail, nn)
625 db_head, db_tail = nn, nn
627 setfield(db, 'head', db_head)
630 return nh, nb, ret, flag
633 process_dir_node = function (hd, gc)
634 local x, new_dir = hd, ltjs.list_dir or dir_yoko
637 if (xid==id_hlist and has_attr(x, attr_icflag)%PROCESSED_BEGIN_FLAG~=PACKED)
638 or xid==id_vlist then
639 hd, x = make_dir_whatsit(hd, x, new_dir, 'process_dir_node:' .. gc)
648 local node_prev = (Dnode~=node) and Dnode.getprev or node.prev
649 local function lastbox_hook()
650 start_time_measure('box_primitive_hook')
651 local bn = tex_nest[tex_nest.ptr].tail
653 local b, head = to_direct(bn), to_direct(tex_nest[tex_nest.ptr].head)
655 if bid==id_hlist or bid==id_vlist then
656 local box_dir = get_box_dir(b, 0)
657 if box_dir>= dir_node_auto then -- unwrap dir_node
658 local p = node_prev(b)
659 local dummy1, dummy2, nb = unwrap_dir_node(b, nil, box_dir)
660 setfield(p, 'next', nb); tex_nest[tex_nest.ptr].tail = to_node(nb)
661 setfield(b, 'next', nil); setfield(b, 'head', nil)
664 local _, wh = get_box_dir(b, 0) -- clean dir_node attached to the box
666 Dnode.flush_list(getfield('value', wh))
667 setfield(wh, 'value', nil)
671 stop_time_measure('box_primitive_hook')
674 luatexja.direction.make_dir_whatsit = make_dir_whatsit
675 luatexja.direction.lastbox_hook = lastbox_hook
678 -- \wd, \ht, \dp の代わり
680 local getbox, setdimen = tex.getbox, tex.setdimen
681 local function get_box_dim_common(key, s, l_dir)
682 local s_dir, wh = get_box_dir(s, dir_yoko)
683 if s_dir ~= l_dir then
684 local not_found = true
685 for x in traverse(getfield(wh, 'value')) do
686 if l_dir == has_attr(x, attr_dir)%dir_node_auto then
687 setdimen('ltj@tempdima', getfield(x, key))
688 not_found = false; break
692 local w = getfield(s, 'width')
693 local h = getfield(s, 'height')
694 local d = getfield(s, 'depth')
695 setdimen('ltj@tempdima',
696 dir_node_aux[s_dir][l_dir][key](w,h,d))
699 setdimen('ltj@tempdima', getfield(s, key))
702 local function get_box_dim(key, n)
703 local gt = tex.globaldefs; tex.globaldefs = 0
706 local l_dir = get_dir_count()
708 local b_dir = get_box_dir(s,dir_yoko)
709 if b_dir<dir_node_auto then
710 get_box_dim_common(key, s, l_dir)
711 elseif b_dir%dir_node_auto==l_dir then
712 setdimen('ltj@tempdima', getfield(s, key))
714 get_box_dim_common(key,
715 node_next(node_next(node_next(getlist(s)))), l_dir)
718 setdimen('ltj@tempdima', 0)
722 luatexja.direction.get_box_dim = get_box_dim
724 -- return value: (changed dimen of box itself?)
725 local function set_box_dim_common(key, s, l_dir)
726 local s_dir, wh = get_box_dir(s, dir_yoko)
727 if s_dir ~= l_dir then
729 wh = create_dir_whatsit(getlist(s), 'set_box_dim', s_dir)
730 setfield(s, 'head', wh)
733 local dnh = getfield(wh, 'value')
734 for x in traverse(dnh) do
735 if has_attr(x, attr_dir)%dir_node_auto==l_dir then
740 db = create_dir_node(s, s_dir, l_dir, true)
741 setfield(db, 'next', dnh)
742 setfield(wh, 'value',to_node(db))
744 setfield(db, key, tex.getdimen('ltj@tempdima'))
747 setfield(s, key, tex.getdimen('ltj@tempdima'))
749 -- change dimension of dir_nodes which are created "automatically"
751 = getfield(s,'width'), getfield(s, 'height'), getfield(s, 'depth')
752 for x in traverse(getfield(wh, 'value')) do
753 local x_dir = has_attr(x, attr_dir)
754 if x_dir<dir_node_manual then
755 local info = dir_node_aux[s_dir][x_dir%dir_node_auto]
756 setfield(x, 'width', info.width(bw,bh,bd))
757 setfield(x, 'height', info.height(bw,bh,bd))
758 setfield(x, 'depth', info.depth(bw,bh,bd))
765 local function set_box_dim(key)
766 local n = tex_getcount('ltj@tempcnta')
769 local l_dir = get_dir_count()
771 local b_dir = get_box_dir(s,dir_yoko)
772 if b_dir<dir_node_auto then
773 set_box_dim_common(key, s, l_dir)
774 elseif b_dir%dir_node_auto == l_dir then
776 setfield(s, key, tex.getdimen('ltj@tempdima'))
777 if b_dir<dir_node_manual then
778 set_attr(s, attr_dir, b_dir%dir_node_auto + dir_node_manual)
781 local sid, sl = getid(s), getlist(s)
782 local b = node_next(node_next(node_next(sl)))
783 local info = dir_node_aux[get_box_dir(b,dir_yoko)][b_dir%dir_node_auto]
785 for _,v in ipairs(info[sid]) do
788 getfield(b,'width'), getfield(b, 'height'), getfield(b, 'depth'))
792 if set_box_dim_common(key, b, l_dir) then
794 = getfield(b,'width'), getfield(b, 'height'), getfield(b, 'depth')
795 -- re-calculate shift
796 for i,v in ipairs(info[sid]) do
797 if getid(sl)==id_kern then
798 setfield(sl, 'kern', v[2](bw,bh,bd) )
799 elseif getid(sl)==sid then
800 local d = getfield(sl, 'shift')
801 setfield(sl, 'shift',
802 getfield(sl, 'shift') - shift_old + v[2](bw,bh,bd) )
806 -- re-calculate dimension of s, if s is created "automatically"
807 if b_dir<dir_node_manual then
808 setfield(s, 'width', info.width(bw,bh,bd))
809 setfield(s, 'height', info.height(bw,bh,bd))
810 setfield(s, 'depth', info.depth(bw,bh,bd))
816 luatexja.direction.set_box_dim = set_box_dim
820 local getbox = tex.getbox
821 local function get_register_dir(n)
825 local b_dir = get_box_dir(s, dir_yoko)
826 if b_dir<dir_node_auto then
829 local b_dir = get_box_dir(
830 node_next(node_next(node_next(getlist(s)))), dir_yoko)
837 luatexja.direction.get_register_dir = get_register_dir
842 local getbox, setbox, copy_list = tex.getbox, tex.setbox, Dnode.copy_list
843 function luatexja.direction.raise_box()
844 start_time_measure('box_primitive_hook')
845 local list_dir = get_dir_count()
846 local s = getbox('ltj@afbox')
848 local sd = to_direct(s)
849 local box_dir = get_box_dir(sd, dir_yoko)
850 if box_dir%dir_node_auto ~= list_dir then
854 copy_list(make_dir_whatsit(sd, sd, list_dir, 'box_move'))
855 -- without copy_list, we get a segfault
860 stop_time_measure('box_primitive_hook')
865 local font_vert_table = {} -- key: fontnumber
867 local font_vert_basename = {} -- key: basename
868 local function add_feature_table(tname, src, dest)
869 for i,v in pairs(src) do
870 if type(v.slookups)=='table' then
871 local s = v.slookups[tname]
872 if s and not dest[i] then
879 local function prepare_vert_data(n, id)
880 -- test if already loaded
881 if type(id)=='number' then -- sometimes id is an integer
882 font_vert_table[n] = font_vert_table[id]; return
883 elseif (not id) or font_vert_table[n] then return
885 local fname = id.filename
886 local bname = file.basename(fname)
888 font_vert_table[n] = {}; return
889 elseif font_vert_basename[bname] then
890 font_vert_table[n] = font_vert_basename[bname]; return
893 local a = id.resources.sequences
895 local s = id.shared.rawdata.descriptions
896 for i,v in pairs(a) do
897 if v.features.vert or v.features.vrt2 then
898 add_feature_table(v.subtables[1], s, vtable)
902 font_vert_basename[bname] = vtable
903 font_vert_table[n] = vtable
906 function luatexja.direction.get_vert_glyph(n, chr)
907 local fn = font_vert_table[n]
908 return fn and fn[chr] or chr
910 luatexbase.add_to_callback('luatexja.define_font',
911 function (res, name, size, id)
912 prepare_vert_data(id, res)
914 'prepare_vert_data', 1)
916 local function a (n, dat) font_vert_table[n] = dat end
917 luatexja.rmlgbm.vert_addfunc = a
921 -- PACKED の hbox から文字を取り出す
922 -- luatexja.jfmglue.check_box などで使用
924 local function glyph_from_packed(h)
926 return (getid(b)==id_kern)
927 and node_next(node_next(node_next(node_next(b)))) or b
929 luatexja.direction.glyph_from_packed = glyph_from_packed
932 -- adjust and insertion
933 local id_adjust = node.id('adjust')
934 function luatexja.direction.check_adjust_direction()
935 start_time_measure('box_primitive_hook')
936 local list_dir = get_adjust_dir_count()
937 local a = tex_nest[tex_nest.ptr].tail
938 local ad = to_direct(a)
939 if a and getid(ad)==id_adjust then
940 local adj_dir = get_box_dir(ad)
941 if list_dir~=adj_dir then
944 'Direction Incompatible',
945 "\\vadjust's argument and outer vlist must have same direction.")
949 stop_time_measure('box_primitive_hook')
954 local split_dir_whatsit
955 local function dir_adjust_vpack(h, gc)
956 start_time_measure('direction_vpack')
957 local hd = to_direct(h)
958 if gc=='split_keep' then
959 -- supply dir_whatsit
960 hd = create_dir_whatsit_vbox(hd, gc)
961 split_dir_whatsit = hd
962 elseif gc=='split_off' then
965 if bh and getid(bh)==id_whatsit
966 and getsubtype(bh)==sid_user and getfield(bh, 'user_id')==DIR then
967 ltjs.list_dir = has_attr(bh, attr_dir); break
971 if split_dir_whatsit then
972 -- adjust direction of 'split_keep'
973 set_attr(split_dir_whatsit, attr_dir, ltjs.list_dir)
975 split_dir_whatsit=nil
976 elseif gc=='preamble' then
977 split_dir_whatsit=nil
980 hd = process_dir_node(create_dir_whatsit_vbox(hd, gc), gc)
981 split_dir_whatsit=nil
983 stop_time_measure('direction_vpack')
986 luatexbase.add_to_callback('vpack_filter',
988 'ltj.direction', 10000)
992 -- supply direction whatsit to the main vertical list "of the next page"
993 local function dir_adjust_pre_output(h, gc)
994 return to_node(create_dir_whatsit_vbox(to_direct(h), gc))
996 luatexbase.add_to_callback('pre_output_filter',
997 dir_adjust_pre_output,
998 'ltj.direction', 10000)
1000 function luatexja.direction.remove_end_whatsit()
1001 local h=tex.lists.page_head
1003 h.id==id_whatsit and h.subtype==sid_user and
1004 h.user_id == DIR then
1005 tex.lists.page_head = nil
1013 local function dir_adjust_buildpage(info)
1015 local head = to_direct(tex.lists.contrib_head)
1019 = make_dir_whatsit(head,
1023 tex.lists.contrib_head = to_node(head)
1027 luatexbase.add_to_callback('buildpage_filter',
1028 dir_adjust_buildpage,
1029 'ltj.direction', 10000)