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
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 tex_set_attr = tex.setattribute
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 STCK = luatexja.userid_table.STCK
53 local DIR = luatexja.userid_table.DIR
54 local dir_tate = luatexja.dir_table.dir_tate
55 local dir_yoko = luatexja.dir_table.dir_yoko
56 local dir_dtou = luatexja.dir_table.dir_dtou
57 local dir_node_auto = luatexja.dir_table.dir_node_auto
58 local dir_node_manual = luatexja.dir_table.dir_node_manual
62 local function adjust_badness(hd)
63 if not node_next(hd) and getid(hd)==id_whatsit and getsubtype(hd)==sid_user
64 and getfield(hd, 'user_id')==DIR then
65 -- avoid double whatsit
66 luatexja.global_temp=tex.globaldefs; tex.globaldefs=0
67 luatexja.hbadness_temp=tex.hbadness; tex.hbadness=10000
68 luatexja.vbadness_temp=tex.vbadness; tex.vbadness=10000
70 luatexja.global_temp = nil
71 luatexja.hbadness_temp=nil
72 luatexja.vbadness_temp=nil
79 local function get_dir_count_inner(h)
81 if h.id==id_whatsit and h.subtype==sid_user and h.user_id==DIR then
82 local ic = node.has_attribute(h, attr_icflag)
83 return (ic<PROCESSED_BEGIN_FLAG) and (node.has_attribute(h,attr_dir)%dir_node_auto) or 0
91 function get_dir_count()
92 for i=tex_nest.ptr, 1, -1 do
93 local h = tex_nest[i].head.next
94 --luatexja.ext_show_node_list(h, 'GDR'.. i .. '> ', print)
96 local t = get_dir_count_inner(h)
97 if t~=0 then return t end
100 return page_direction
102 luatexja.direction.get_dir_count = get_dir_count
104 local node_next = node.next
105 local node_set_attr = node.set_attribute
106 local function set_list_direction(v, name)
107 local lv, w = tex_nest.ptr, tex.lists.page_head
109 if w.id==id_whatsit and w.subtype==sid_user
110 and w.user_id==DIR then
111 node_set_attr(w, attr_dir, v)
114 elseif tex.currentgrouptype==6 then
117 "You can't use `\\" .. name .. "' in an align",
118 "To change direction in an align, \n"
119 .. "you shold use \\hbox or \\vbox.")
121 local w = node_next(tex_nest[lv].head)
124 if getid(w)==id_whatsit and getsubtype(w)==sid_user
125 and getfield(w, 'user_id')==DIR then
126 set_attr(w, attr_dir, v)
130 "Use `\\" .. name .. "' at top of list",
131 'Direction change command by LuaTeX-ja is available\n'
132 .. 'only when the current list is null.')
135 local w = node_new(id_whatsit, sid_user)
136 setfield(w, 'next', nil)
137 setfield(w, 'user_id', DIR)
138 setfield(w, 'type', 110)
139 set_attr(w, attr_dir, v)
142 tex_set_attr('global', attr_icflag, 0)
144 tex_set_attr('global', attr_dir, 0)
146 luatexja.direction.set_list_direction = set_list_direction
149 -- ボックスに dir whatsit を追加
150 local function create_dir_whatsit(hd, gc, new_dir)
151 if getid(hd)==id_whatsit and
152 getsubtype(hd)==sid_user and getfield(hd, 'user_id')==DIR then
153 set_attr(hd, attr_icflag,
154 (has_attr(hd, attr_icflag) or 0)%PROCESSED_BEGIN_FLAG
155 + PROCESSED_BEGIN_FLAG)
156 tex_set_attr('global', attr_icflag, 0)
159 local w = node_new(id_whatsit, sid_user)
160 setfield(w, 'next', hd)
161 setfield(w, 'user_id', DIR)
162 setfield(w, 'type', 110)
163 set_attr(w, attr_dir, new_dir)
164 set_attr(w, attr_icflag, PROCESSED_BEGIN_FLAG)
165 set_attr(hd, attr_icflag,
166 (has_attr(hd, attr_icflag) or 0)%PROCESSED_BEGIN_FLAG
167 + PROCESSED_BEGIN_FLAG)
168 tex_set_attr('global', attr_dir, 0)
169 tex_set_attr('global', attr_icflag, 0)
174 -- hpack_filter, vpack_filter, post_line_break_filter
175 -- の結果を組方向を明示するため,先頭に dir_node を設置
177 local function create_dir_whatsit_hpack(h, gc)
178 local hd = to_direct(h)
179 if gc=='fin_row' or gc == 'preamble' then
181 set_attr(hd, attr_icflag, PROCESSED_BEGIN_FLAG)
182 tex_set_attr('global', attr_icflag, 0)
187 return to_node(create_dir_whatsit(hd, gc, ltjs.list_dir))
191 luatexbase.add_to_callback('hpack_filter',
192 create_dir_whatsit_hpack, 'ltj.create_dir_whatsit', 10000)
196 local function create_dir_whatsit_parbox(h, gc)
197 stop_time_measure('tex_linebreak')
198 -- start 側は ltj-debug.lua に
199 local new_dir, hd = ltjs.list_dir, to_direct(h)
200 for line in traverse_id(id_hlist, hd) do
201 local nh = getlist(line)
202 setfield(line, 'head', create_dir_whatsit(nh, gc, new_dir) )
203 set_attr(line, attr_dir, new_dir)
205 tex_set_attr('global', attr_dir, 0)
208 luatexbase.add_to_callback('post_linebreak_filter',
209 create_dir_whatsit_parbox, 'ltj.create_dir_whatsit', 10000)
212 local create_dir_whatsit_vbox
215 local id_glue, sid_parskip = node.id('glue'), 3
216 create_dir_whatsit_vbox = function (hd, gc)
217 ltjs.list_dir = get_dir_count()
218 -- remove dir whatsit
219 for x in traverse_id(id_whatsit, hd) do
220 if getsubtype(x)==sid_user and getfield(x, 'user_id')==DIR then
225 ltjs.list_dir =has_attr(hd,attr_dir)
226 local x = node_next(hd)
227 if getid(x)==id_glue and getsubtype(x)==sid_parskip then
228 node_remove(hd,x); node_free(x)
232 hd = node_remove(hd, wh[i]); node_free(wh[i]); wh[i] = nil
234 if gc=='fin_row' or gc == 'preamble' then
236 set_attr(hd, attr_icflag, PROCESSED_BEGIN_FLAG)
237 tex_set_attr('global', attr_icflag, 0)
241 local n =node_next(hd)
243 local w = create_dir_whatsit(hd, gc, ltjs.list_dir)
244 -- move dir whatsit after hd
245 setfield(hd, 'next', w); setfield(w, 'next', n)
248 hd = create_dir_whatsit(hd, gc, ltjs.list_dir)
255 -- dir_node に包む方法を書いたテーブル
258 local get_h =function (w,h,d) return h end
259 local get_d =function (w,h,d) return d end
260 local get_h_d =function (w,h,d) return h+d end
261 local get_h_d_neg =function (w,h,d) return -h-d end
262 local get_h_neg =function (w,h,d) return -h end
263 local get_d_neg =function (w,h,d) return -d end
264 local get_w_half =function (w,h,d) return 0.5*w end
265 local get_w_neg_half =function (w,h,d) return -0.5*w end
266 local get_w_neg =function (w,h,d) return -w end
267 local get_w =function (w,h,d) return w end
268 local zero = function() return 0 end
270 [dir_yoko] = { -- yoko を
271 [dir_tate] = { -- tate 中で組む
276 { 'whatsit', sid_save },
277 { 'rotate', '0 1 -1 0' },
278 { 'kern', get_w_neg_half },
280 { 'kern', get_w_neg_half },
281 { 'whatsit', sid_restore },
284 { 'whatsit', sid_save },
285 { 'rotate', '0 1 -1 0' },
287 { 'box' , get_w_neg },
288 { 'kern', get_h_d_neg},
289 { 'whatsit', sid_restore },
292 [dir_dtou] = { -- dtou 中で組む
297 { 'whatsit', sid_save },
298 { 'rotate', '0 -1 1 0' },
299 { 'kern', get_w_neg },
300 { 'box', get_d_neg },
301 { 'whatsit', sid_restore },
304 { 'whatsit', sid_save },
305 { 'rotate', '0 -1 1 0' },
306 { 'kern', get_h_d_neg },
308 { 'whatsit', sid_restore },
312 [dir_tate] = { -- tate を
313 [dir_yoko] = { -- yoko 中で組む
318 { 'whatsit', sid_save },
319 { 'rotate', '0 -1 1 0' },
320 { 'kern', get_w_neg },
321 { 'box' , get_d_neg },
322 { 'whatsit', sid_restore },
325 { 'whatsit', sid_save },
326 { 'rotate', '0 -1 1 0' },
327 { 'kern', get_h_d_neg },
329 { 'whatsit', sid_restore },
332 [dir_dtou] = { -- dtou 中で組む
337 { 'whatsit', sid_save },
338 { 'rotate', '-1 0 0 -1' },
339 { 'kern', get_w_neg },
341 { 'whatsit', sid_restore },
344 { 'whatsit', sid_save },
345 { 'rotate', '-1 0 0 -1' },
346 { 'kern', get_h_d_neg },
347 { 'box', get_w_neg },
348 { 'whatsit', sid_restore },
352 [dir_dtou] = { -- dtou を
353 [dir_yoko] = { -- yoko 中で組む
358 { 'whatsit', sid_save },
359 { 'rotate', '0 1 -1 0' },
362 { 'kern', get_w_neg },
363 { 'whatsit', sid_restore },
367 { 'whatsit', sid_save },
368 { 'rotate', '0 1 -1 0' },
369 { 'box', get_w_neg },
370 { 'kern', get_h_d_neg },
371 { 'whatsit', sid_restore },
374 [dir_tate] = { -- tate 中で組む
379 { 'whatsit', sid_save },
380 { 'rotate', '-1 0 0 -1' },
381 { 'kern', get_w_neg },
383 { 'whatsit', sid_restore },
386 { 'whatsit', sid_save },
387 { 'rotate', ' -1 0 0 -1' },
388 { 'kern', get_h_d_neg },
389 { 'box', get_w_neg },
390 { 'whatsit', sid_restore },
397 -- 1st ret val: b の組方向
398 -- 2nd ret val はその DIR whatsit
399 local function get_box_dir(b, default)
400 start_time_measure('get_box_dir')
401 local dir = has_attr(b, attr_dir) or 0
402 local bh = getfield(b,'head')
403 -- b は insert node となりうるので getlist() は使えない
406 if bh and getid(bh)==id_whatsit
407 and getsubtype(bh)==sid_user and getfield(bh, 'user_id')==DIR then
409 dir = (dir==0) and has_attr(bh, attr_dir) or dir
413 stop_time_measure('get_box_dir')
414 return (dir==0 and default or dir), c
418 local getbox = tex.getbox
419 local function check_dir(reg_num)
420 start_time_measure('box_primitive_hook')
421 local list_dir = get_dir_count()
422 local b = tex.getbox(tex_getcount('ltj@tempcnta'))
424 local box_dir = get_box_dir(to_direct(b), dir_yoko)
425 if box_dir%dir_node_auto ~= list_dir%dir_node_auto then
426 luatexja.ext_show_node_list(tex.nest[tex.nest.ptr].head, 'LIST> ', print)
427 luatexja.ext_show_node_list(b, 'BOX> ', print)
430 "Incompatible direction list can't be unboxed",
431 'I refuse to unbox a box in differrent direction.')
434 stop_time_measure('box_primitive_hook')
436 luatexja.direction.check_dir = check_dir
439 -- dir_node に包まれている「本来の中身」を取り出し,
441 local function unwrap_dir_node(b, head, box_dir)
442 -- b: dir_node, head: the head of list, box_dir:
443 -- return values are (new head), (next of b), (contents), (dir of contents)
444 local bh = getlist(b)
445 local bc = node_next(node_next(node_next(bh)))
450 nh = insert_before(head, b, bc)
451 nh, nb = node_remove(nh, b)
452 setfield(b, 'next', nil)
453 setfield(b, 'head', nil)
456 local shift_old, b_dir, wh = nil, get_box_dir(bc, 0)
458 Dnode.flush_list(getfield(wh, 'value'))
459 setfield(wh, 'value', nil)
462 local info = dir_node_aux[b_dir][box_dir%dir_node_auto][getid(bc)]
463 for _,v in ipairs(info) do
466 getfield(bc,'width'), getfield(bc, 'height'), getfield(bc, 'depth'))
470 setfield(bc, 'shift', getfield(bc, 'shift') - shift_old)
471 return nh, nb, bc, b_dir
474 -- is_manual: 寸法変更に伴うものか?
475 local function create_dir_node(b, b_dir, new_dir, is_manual)
476 print('create new node', b_dir, new_dir)
477 local info = dir_node_aux[b_dir][new_dir]
478 local w = getfield(b, 'width')
479 local h = getfield(b, 'height')
480 local d = getfield(b, 'depth')
481 local db = node_new(getid(b)) -- dir_node
482 set_attr(db, attr_dir,
483 new_dir + (is_manual and dir_node_manual or dir_node_auto))
484 set_attr(db, attr_icflag, PROCESSED)
485 set_attr(b, attr_icflag, PROCESSED)
486 tex_set_attr('global', attr_dir, 0)
487 tex_set_attr('global', attr_icflag, 0)
488 setfield(db, 'dir', getfield(b, 'dir'))
489 setfield(db, 'shift', 0)
490 setfield(db, 'width', info.width(w,h,d))
491 setfield(db, 'height', info.height(w,h,d))
492 setfield(db, 'depth', info.depth(w,h,d))
497 local make_dir_whatsit, process_dir_node
499 make_dir_whatsit = function (head, b, new_dir, origin)
500 -- head: list head, b: box
501 -- origin: コール元 (for debug)
502 -- return value: (new head), (next of b), (new b), (is_b_dir_node)
503 -- (new b): b か dir_node に被せられた b
504 local bh = getlist(b)
505 local box_dir, dn = get_box_dir(b, ltjs.list_dir)
506 -- 既に b の中身にあるwhatsit
508 if box_dir==new_dir then
509 -- 組方向が一緒のボックスなので,何もしなくて良い
510 return head, node_next(b), b, false
511 elseif box_dir%dir_node_auto == new_dir then
512 -- dir_node としてカプセル化されている
513 local bc = node_next(node_next(node_next(bh)))
514 local _, dnc = get_box_dir(b, 0)
515 if dnc then -- free all other dir_node
516 Dnode.flush_list(getfield(dnc, 'value'))
517 setfield(dnc, 'value', nil)
519 set_attr(b, attr_dir, box_dir%dir_node_auto + dir_node_auto)
520 return head, node_next(b), b, true
522 --luatexja.ext_show_node_list(to_node(b), 'mkd> ', print)
524 local nh, nb, ret, flag
525 if box_dir>= dir_node_auto then -- unwrap
527 head, nb, b, b_dir = unwrap_dir_node(b, head, box_dir)
529 if b_dir==new_dir then
530 -- dir_node の中身が周囲の組方向とあっている
531 return head, nb, b, false
532 else box_dir = b_dir end
535 local dnh = getfield(dn, 'value')
536 for x in traverse(dnh) do
537 if has_attr(x, attr_dir)%dir_node_auto == new_dir then
538 setfield(dn, 'value', to_node(node_remove(dnh, x)))
542 Dnode.flush_list(dnh)
543 db = db or create_dir_node(b, box_dir, new_dir, false)
544 local w = getfield(b, 'width')
545 local h = getfield(b, 'height')
546 local d = getfield(b, 'depth')
547 nh, nb = insert_before(head, b, db), nil
548 nh, nb = node_remove(nh, b)
549 local db_head, db_tail = nil
550 for _,v in ipairs(dir_node_aux[box_dir][new_dir][getid(b)]) do
551 local cmd, arg, nn = v[1], v[2]
553 nn = node_new(id_kern)
554 setfield(nn, 'kern', arg(w, h, d))
555 elseif cmd=='whatsit' then
556 nn = node_new(id_whatsit, arg)
557 elseif cmd=='rotate' then
558 nn = node_new(id_whatsit, sid_matrix)
559 setfield(nn, 'data', arg)
560 elseif cmd=='box' then
561 nn = b; setfield(b, 'next', nil)
562 setfield(nn, 'shift', getfield(nn, 'shift') + arg(w,h,d))
565 insert_after(db_head, db_tail, nn)
568 db_head, db_tail = nn, nn
570 setfield(db, 'head', db_head)
573 return nh, nb, ret, flag
576 process_dir_node = function (hd, gc)
577 local x, new_dir = hd, ltjs.list_dir or dir_yoko
580 if (xid==id_hlist and has_attr(x, attr_icflag)%PROCESSED_BEGIN_FLAG~=PACKED)
581 or xid==id_vlist then
582 hd, x = make_dir_whatsit(hd, x, new_dir, 'process_dir_node:' .. gc)
591 local node_prev = (Dnode~=node) and Dnode.getprev or node.prev
592 local function lastbox_hook()
593 start_time_measure('box_primitive_hook')
594 local bn = tex_nest[tex_nest.ptr].tail
596 local b, head = to_direct(bn), to_direct(tex_nest[tex_nest.ptr].head)
598 if bid==id_hlist or bid==id_vlist then
599 local box_dir = get_box_dir(b, 0)
600 if box_dir>= dir_node_auto then -- unwrap dir_node
601 local p = node_prev(b)
602 local dummy1, dummy2, nb = unwrap_dir_node(b, nil, box_dir)
603 setfield(p, 'next', nb); tex_nest[tex_nest.ptr].tail = to_node(nb)
604 setfield(b, 'next', nil); setfield(b, 'head', nil)
607 local _, wh = get_box_dir(b, 0) -- clean dir_node attached to the box
609 Dnode.flush_list(getfield('value', wh))
610 setfield(wh, 'value', nil)
614 stop_time_measure('box_primitive_hook')
617 luatexja.direction.make_dir_whatsit = make_dir_whatsit
618 luatexja.direction.lastbox_hook = lastbox_hook
621 -- \wd, \ht, \dp の代わり
623 local getbox, setdimen = tex.getbox, tex.setdimen
624 local function get_box_dim_common(key, s, l_dir)
625 local s_dir, wh = get_box_dir(s, dir_yoko)
626 if s_dir ~= l_dir then
627 local not_found = true
628 for x in traverse(getfield(wh, 'value')) do
629 if l_dir == has_attr(x, attr_dir)%dir_node_auto then
630 setdimen('ltj@tempdima', getfield(x, key))
631 not_found = false; break
635 local w = getfield(s, 'width')
636 local h = getfield(s, 'height')
637 local d = getfield(s, 'depth')
638 setdimen('ltj@tempdima',
639 dir_node_aux[s_dir][l_dir][key](w,h,d))
642 setdimen('ltj@tempdima', getfield(s, key))
645 local function get_box_dim(key, n)
646 local gt = tex.globaldefs; tex.globaldefs = 0
649 local l_dir = get_dir_count()
651 local b_dir = get_box_dir(s,dir_yoko)
652 if b_dir<dir_node_auto then
653 get_box_dim_common(key, s, l_dir)
654 elseif b_dir%dir_node_auto==l_dir then
655 setdimen('ltj@tempdima', getfield(s, key))
657 get_box_dim_common(key,
658 node_next(node_next(node_next(getlist(s)))), l_dir)
661 setdimen('ltj@tempdima', 0)
665 luatexja.direction.get_box_dim = get_box_dim
667 -- return value: (changed dimen of box itself?)
668 local function set_box_dim_common(key, s, l_dir)
669 local s_dir, wh = get_box_dir(s, dir_yoko)
670 if s_dir ~= l_dir then
672 wh = create_dir_whatsit(getlist(s), 'set_box_dim', s_dir)
673 setfield(s, 'head', wh)
676 local dnh = getfield(wh, 'value')
677 for x in traverse(dnh) do
678 if has_attr(x, attr_dir)%dir_node_auto==l_dir then
683 db = create_dir_node(s, s_dir, l_dir, true)
684 setfield(db, 'next', dnh)
685 setfield(wh, 'value',to_node(db))
687 setfield(db, key, tex.getdimen('ltj@tempdima'))
690 setfield(s, key, tex.getdimen('ltj@tempdima'))
692 -- change dimension of dir_nodes which are created "automatically"
694 = getfield(s,'width'), getfield(s, 'height'), getfield(s, 'depth')
695 for x in traverse(getfield(wh, 'value')) do
696 local x_dir = has_attr(x, attr_dir)
697 if x_dir<dir_node_manual then
698 local info = dir_node_aux[s_dir][x_dir%dir_node_auto]
699 setfield(x, 'width', info.width(bw,bh,bd))
700 setfield(x, 'height', info.height(bw,bh,bd))
701 setfield(x, 'depth', info.depth(bw,bh,bd))
708 local function set_box_dim(key)
709 local n = tex_getcount('ltj@tempcnta')
712 local l_dir = get_dir_count()
714 local b_dir = get_box_dir(s,dir_yoko)
715 if b_dir<dir_node_auto then
716 set_box_dim_common(key, s, l_dir)
717 elseif b_dir%dir_node_auto == l_dir then
719 setfield(s, key, tex.getdimen('ltj@tempdima'))
720 if b_dir<dir_node_manual then
721 set_attr(s, attr_dir, b_dir%dir_node_auto + dir_node_manual)
724 local sid, sl = getid(s), getlist(s)
725 local b = node_next(node_next(node_next(sl)))
726 local info = dir_node_aux[get_box_dir(b,dir_yoko)][b_dir%dir_node_auto]
728 for _,v in ipairs(info[sid]) do
731 getfield(b,'width'), getfield(b, 'height'), getfield(b, 'depth'))
735 if set_box_dim_common(key, b, l_dir) then
737 = getfield(b,'width'), getfield(b, 'height'), getfield(b, 'depth')
738 -- re-calculate shift
739 for i,v in ipairs(info[sid]) do
740 if getid(sl)==id_kern then
741 setfield(sl, 'kern', v[2](bw,bh,bd) )
742 elseif getid(sl)==sid then
743 local d = getfield(sl, 'shift')
744 setfield(sl, 'shift',
745 getfield(sl, 'shift') - shift_old + v[2](bw,bh,bd) )
749 -- re-calculate dimension of s, if s is created "automatically"
750 if b_dir<dir_node_manual then
751 setfield(s, 'width', info.width(bw,bh,bd))
752 setfield(s, 'height', info.height(bw,bh,bd))
753 setfield(s, 'depth', info.depth(bw,bh,bd))
759 luatexja.direction.set_box_dim = set_box_dim
762 -- \ifydir, \iftdir, \ifddir
764 local getbox = tex.getbox
765 local function dir_conditional(n, mode)
770 local b_dir = get_box_dir(s, dir_yoko)
771 if b_dir<dir_node_auto then
774 local b_dir = get_box_dir(
775 node_next(node_next(node_next(getlist(s)))), dir_yoko)
779 tex.sprint(cat_lp, '\\if' .. tostring(res))
781 luatexja.direction.dir_conditional = dir_conditional
785 local font_vert_table = {} -- key: fontnumber
787 local font_vert_basename = {} -- key: basename
788 local function add_feature_table(tname, src, dest)
789 for i,v in pairs(src) do
790 if type(v.slookups)=='table' then
791 local s = v.slookups[tname]
792 if s and not dest[i] then
799 local function prepare_vert_data(n, id)
800 -- test if already loaded
801 if type(id)=='number' then -- sometimes id is an integer
802 font_vert_table[n] = font_vert_table[id]; return
803 elseif (not id) or font_vert_table[n] then return
805 local fname = id.filename
806 local bname = file.basename(fname)
808 font_vert_table[n] = {}; return
809 elseif font_vert_basename[bname] then
810 font_vert_table[n] = font_vert_basename[bname]; return
813 local a = id.resources.sequences
815 local s = id.shared.rawdata.descriptions
816 for i,v in pairs(a) do
817 if v.features.vert or v.features.vrt2 then
818 add_feature_table(v.subtables[1], s, vtable)
822 font_vert_basename[bname] = vtable
823 font_vert_table[n] = vtable
826 function luatexja.direction.get_vert_glyph(n, chr)
827 local fn = font_vert_table[n]
828 return fn and fn[chr] or chr
830 luatexbase.add_to_callback('luatexja.define_font',
831 function (res, name, size, id)
832 prepare_vert_data(id, res)
834 'prepare_vert_data', 1)
836 local function a (n, dat) font_vert_table[n] = dat end
837 luatexja.rmlgbm.vert_addfunc = a
841 -- PACKED の hbox から文字を取り出す
842 -- luatexja.jfmglue.check_box などで使用
844 local function glyph_from_packed(h)
846 return (getid(b)==id_kern)
847 and node_next(node_next(node_next(node_next(b)))) or b
849 luatexja.direction.glyph_from_packed = glyph_from_packed
852 -- adjust and insertion
853 local id_adjust = node.id('adjust')
854 function luatexja.direction.check_adjust_direction()
855 start_time_measure('box_primitive_hook')
856 local list_dir = tex_getcount('ltj@adjdir@count')
857 local a = tex_nest[tex_nest.ptr].tail
858 local ad = to_direct(a)
859 if a and getid(ad)==id_adjust then
860 local adj_dir = get_box_dir(ad)
861 if list_dir~=adj_dir then
864 'Direction Incompatible',
865 "\\vadjust's argument and outer vlist must have same direction.")
869 stop_time_measure('box_primitive_hook')
874 local split_dir_whatsit
875 local function dir_adjust_vpack(h, gc)
876 start_time_measure('direction_vpack')
877 local hd = to_direct(h)
878 if gc=='split_keep' then
879 -- supply dir_whatsit
880 hd = create_dir_whatsit_vbox(hd, gc)
881 split_dir_whatsit = hd
882 elseif gc=='split_off' then
885 if bh and getid(bh)==id_whatsit
886 and getsubtype(bh)==sid_user and getfield(bh, 'user_id')==DIR then
887 ltjs.list_dir = has_attr(bh, attr_dir); break
891 if split_dir_whatsit then
892 -- adjust direction of 'split_keep'
893 set_attr(split_dir_whatsit, attr_dir, ltjs.list_dir)
895 split_dir_whatsit=nil
896 elseif gc=='preamble' then
897 split_dir_whatsit=nil
900 hd = process_dir_node(create_dir_whatsit_vbox(hd, gc), gc)
901 split_dir_whatsit=nil
903 stop_time_measure('direction_vpack')
906 luatexbase.add_to_callback('vpack_filter',
908 'ltj.direction', 10000)
912 -- supply direction whatsit to the main vertical list "of the next page"
913 local function dir_adjust_pre_output(h, gc)
914 return to_node(create_dir_whatsit_vbox(to_direct(h), gc))
916 luatexbase.add_to_callback('pre_output_filter',
917 dir_adjust_pre_output,
918 'ltj.direction', 10000)
920 function luatexja.direction.remove_end_whatsit()
921 local h=tex.lists.page_head
923 h.id==id_whatsit and h.subtype==sid_user and
924 h.user_id == DIR then
925 tex.lists.page_head = nil
933 local function dir_adjust_buildpage(info)
935 local head = to_direct(tex.lists.contrib_head)
939 = make_dir_whatsit(head,
943 tex.lists.contrib_head = to_node(head)
947 luatexbase.add_to_callback('buildpage_filter',
948 dir_adjust_buildpage,
949 'ltj.direction', 10000)