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
76 local get_dir_count, get_adjust_dir_count
78 local function get_dir_count_inner(h)
80 if h.id==id_whatsit and h.subtype==sid_user and h.user_id==DIR then
81 local ic = node.has_attribute(h, attr_icflag)
82 return (ic<PROCESSED_BEGIN_FLAG)
83 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
103 function get_adjust_dir_count()
104 for i=tex_nest.ptr, 1, -1 do
105 local v = tex_nest[i]
106 local h, m = v.head.next, v.mode
107 if abs(m)== ltjs.vmode and h then
108 local t = get_dir_count_inner(h)
109 if t~=0 then return t end
112 return page_direction
114 luatexja.direction.get_dir_count = get_dir_count
115 luatexja.direction.get_adjust_dir_count = get_adjust_dir_count
121 local node_next = node.next
122 local node_set_attr = node.set_attribute
123 local function set_list_direction(v, name)
124 local lv, w = tex_nest.ptr, tex.lists.page_head
126 v,name = get_dir_count(), nil
128 v,name = get_adjust_dir_count(), nil
130 if tex.currentgrouptype==6 then
133 "You can't use `\\" .. name .. "' in an align",
134 "To change direction in an align, \n"
135 .. "you shold use \\hbox or \\vbox.")
137 local w = (lv==0) and tex.lists.page_head or tex_nest[lv].head.next
140 w.id==id_whatsit and w.subtype==sid_user and w.user_id==DIR then
141 node_set_attr(w, attr_dir, v)
142 if lv==0 then page_direction = v end
143 elseif lv==0 and not page_direction then
144 page_direction = v -- for first call of \yoko (in luatexja-core.sty)
148 "Use `\\" .. name .. "' at top of list",
149 'Direction change command by LuaTeX-ja is available\n'
150 .. 'only when the current list is null.')
153 local w = node_new(id_whatsit, sid_user)
154 setfield(w, 'next', nil)
155 setfield(w, 'user_id', DIR)
156 setfield(w, 'type', 110)
157 set_attr(w, attr_dir, v)
159 if lv==0 then page_direction = v end
161 tex_set_attr('global', attr_icflag, 0)
163 tex_set_attr('global', attr_dir, 0)
165 luatexja.direction.set_list_direction = set_list_direction
168 -- ボックスに dir whatsit を追加
169 local function create_dir_whatsit(hd, gc, new_dir)
170 if getid(hd)==id_whatsit and
171 getsubtype(hd)==sid_user and getfield(hd, 'user_id')==DIR then
172 set_attr(hd, attr_icflag,
173 (has_attr(hd, attr_icflag) or 0)%PROCESSED_BEGIN_FLAG
174 + PROCESSED_BEGIN_FLAG)
175 tex_set_attr('global', attr_icflag, 0)
178 local w = node_new(id_whatsit, sid_user)
179 setfield(w, 'next', hd)
180 setfield(w, 'user_id', DIR)
181 setfield(w, 'type', 110)
182 set_attr(w, attr_dir, new_dir)
183 set_attr(w, attr_icflag, PROCESSED_BEGIN_FLAG)
184 set_attr(hd, attr_icflag,
185 (has_attr(hd, attr_icflag) or 0)%PROCESSED_BEGIN_FLAG
186 + PROCESSED_BEGIN_FLAG)
187 tex_set_attr('global', attr_dir, 0)
188 tex_set_attr('global', attr_icflag, 0)
193 -- hpack_filter, vpack_filter, post_line_break_filter
194 -- の結果を組方向を明示するため,先頭に dir_node を設置
196 local function create_dir_whatsit_hpack(h, gc)
197 local hd = to_direct(h)
198 if gc=='fin_row' or gc == 'preamble' then
200 set_attr(hd, attr_icflag, PROCESSED_BEGIN_FLAG)
201 tex_set_attr('global', attr_icflag, 0)
206 return to_node(create_dir_whatsit(hd, gc, ltjs.list_dir))
210 luatexbase.add_to_callback('hpack_filter',
211 create_dir_whatsit_hpack, 'ltj.create_dir_whatsit', 10000)
215 local function create_dir_whatsit_parbox(h, gc)
216 stop_time_measure('tex_linebreak')
217 -- start 側は ltj-debug.lua に
218 local new_dir, hd = ltjs.list_dir, to_direct(h)
219 for line in traverse_id(id_hlist, hd) do
220 local nh = getlist(line)
221 setfield(line, 'head', create_dir_whatsit(nh, gc, new_dir) )
222 set_attr(line, attr_dir, new_dir)
224 tex_set_attr('global', attr_dir, 0)
227 luatexbase.add_to_callback('post_linebreak_filter',
228 create_dir_whatsit_parbox, 'ltj.create_dir_whatsit', 10000)
231 local create_dir_whatsit_vbox
234 local id_glue, sid_parskip = node.id('glue'), 3
235 create_dir_whatsit_vbox = function (hd, gc)
236 ltjs.list_dir = get_dir_count()
237 -- remove dir whatsit
238 for x in traverse_id(id_whatsit, hd) do
239 if getsubtype(x)==sid_user and getfield(x, 'user_id')==DIR then
244 ltjs.list_dir =has_attr(hd,attr_dir)
245 local x = node_next(hd)
246 if getid(x)==id_glue and getsubtype(x)==sid_parskip then
247 node_remove(hd,x); node_free(x)
251 hd = node_remove(hd, wh[i]); node_free(wh[i]); wh[i] = nil
253 if gc=='fin_row' or gc == 'preamble' then
255 set_attr(hd, attr_icflag, PROCESSED_BEGIN_FLAG)
256 tex_set_attr('global', attr_icflag, 0)
260 local n =node_next(hd)
262 local w = create_dir_whatsit(hd, gc, ltjs.list_dir)
263 -- move dir whatsit after hd
264 setfield(hd, 'next', w); setfield(w, 'next', n)
267 hd = create_dir_whatsit(hd, gc, ltjs.list_dir)
274 -- dir_node に包む方法を書いたテーブル
277 local get_h =function (w,h,d) return h end
278 local get_d =function (w,h,d) return d end
279 local get_h_d =function (w,h,d) return h+d end
280 local get_h_d_neg =function (w,h,d) return -h-d end
281 local get_h_neg =function (w,h,d) return -h end
282 local get_d_neg =function (w,h,d) return -d end
283 local get_w_half =function (w,h,d) return 0.5*w end
284 local get_w_neg_half =function (w,h,d) return -0.5*w end
285 local get_w_neg =function (w,h,d) return -w end
286 local get_w =function (w,h,d) return w end
287 local zero = function() return 0 end
289 [dir_yoko] = { -- yoko を
290 [dir_tate] = { -- tate 中で組む
295 { 'whatsit', sid_save },
296 { 'rotate', '0 1 -1 0' },
297 { 'kern', get_w_neg_half },
299 { 'kern', get_w_neg_half },
300 { 'whatsit', sid_restore },
303 { 'whatsit', sid_save },
304 { 'rotate', '0 1 -1 0' },
306 { 'box' , get_w_neg },
307 { 'kern', get_h_d_neg},
308 { 'whatsit', sid_restore },
311 [dir_dtou] = { -- dtou 中で組む
316 { 'whatsit', sid_save },
317 { 'rotate', '0 -1 1 0' },
318 { 'kern', get_w_neg },
319 { 'box', get_d_neg },
320 { 'whatsit', sid_restore },
323 { 'whatsit', sid_save },
324 { 'rotate', '0 -1 1 0' },
325 { 'kern', get_h_d_neg },
327 { 'whatsit', sid_restore },
331 [dir_tate] = { -- tate を
332 [dir_yoko] = { -- yoko 中で組む
337 { 'whatsit', sid_save },
338 { 'rotate', '0 -1 1 0' },
339 { 'kern', get_w_neg },
340 { 'box' , get_d_neg },
341 { 'whatsit', sid_restore },
344 { 'whatsit', sid_save },
345 { 'rotate', '0 -1 1 0' },
346 { 'kern', get_h_d_neg },
348 { 'whatsit', sid_restore },
351 [dir_dtou] = { -- dtou 中で組む
356 { 'whatsit', sid_save },
357 { 'rotate', '-1 0 0 -1' },
358 { 'kern', get_w_neg },
360 { 'whatsit', sid_restore },
363 { 'whatsit', sid_save },
364 { 'rotate', '-1 0 0 -1' },
365 { 'kern', get_h_d_neg },
366 { 'box', get_w_neg },
367 { 'whatsit', sid_restore },
371 [dir_dtou] = { -- dtou を
372 [dir_yoko] = { -- yoko 中で組む
377 { 'whatsit', sid_save },
378 { 'rotate', '0 1 -1 0' },
381 { 'kern', get_w_neg },
382 { 'whatsit', sid_restore },
386 { 'whatsit', sid_save },
387 { 'rotate', '0 1 -1 0' },
388 { 'box', get_w_neg },
389 { 'kern', get_h_d_neg },
390 { 'whatsit', sid_restore },
393 [dir_tate] = { -- tate 中で組む
398 { 'whatsit', sid_save },
399 { 'rotate', '-1 0 0 -1' },
400 { 'kern', get_w_neg },
402 { 'whatsit', sid_restore },
405 { 'whatsit', sid_save },
406 { 'rotate', ' -1 0 0 -1' },
407 { 'kern', get_h_d_neg },
408 { 'box', get_w_neg },
409 { 'whatsit', sid_restore },
416 -- 1st ret val: b の組方向
417 -- 2nd ret val はその DIR whatsit
418 local function get_box_dir(b, default)
419 start_time_measure('get_box_dir')
420 local dir = has_attr(b, attr_dir) or 0
421 local bh = getfield(b,'head')
422 -- b は insert node となりうるので getlist() は使えない
425 if bh and getid(bh)==id_whatsit
426 and getsubtype(bh)==sid_user and getfield(bh, 'user_id')==DIR then
428 dir = (dir==0) and has_attr(bh, attr_dir) or dir
432 stop_time_measure('get_box_dir')
433 return (dir==0 and default or dir), c
437 local getbox = tex.getbox
438 local function check_dir(reg_num)
439 start_time_measure('box_primitive_hook')
440 local list_dir = get_dir_count()
441 local b = tex.getbox(tex_getcount('ltj@tempcnta'))
443 local box_dir = get_box_dir(to_direct(b), dir_yoko)
444 if box_dir%dir_node_auto ~= list_dir%dir_node_auto then
445 print('NEST', tex_nest.ptr, tex_getcount('ltj@tempcnta'))
446 luatexja.ext_show_node_list(
447 (tex_nest.ptr==0) and tex.lists.page_head or tex_nest[tex_nest.ptr].head,
448 'LIST' .. tostring(list_dir) .. '> ', print)
449 luatexja.ext_show_node_list(b, 'BOX' .. tostring(box_dir) .. '> ', print)
452 "Incompatible direction list can't be unboxed",
453 'I refuse to unbox a box in differrent direction.')
456 stop_time_measure('box_primitive_hook')
458 luatexja.direction.check_dir = check_dir
461 -- dir_node に包まれている「本来の中身」を取り出し,
463 local function unwrap_dir_node(b, head, box_dir)
464 -- b: dir_node, head: the head of list, box_dir:
465 -- return values are (new head), (next of b), (contents), (dir of contents)
466 local bh = getlist(b)
467 local bc = node_next(node_next(node_next(bh)))
472 nh = insert_before(head, b, bc)
473 nh, nb = node_remove(nh, b)
474 setfield(b, 'next', nil)
475 setfield(b, 'head', nil)
478 local shift_old, b_dir, wh = nil, get_box_dir(bc, 0)
480 Dnode.flush_list(getfield(wh, 'value'))
481 setfield(wh, 'value', nil)
484 local info = dir_node_aux[b_dir][box_dir%dir_node_auto][getid(bc)]
485 for _,v in ipairs(info) do
488 getfield(bc,'width'), getfield(bc, 'height'), getfield(bc, 'depth'))
492 setfield(bc, 'shift', getfield(bc, 'shift') - shift_old)
493 return nh, nb, bc, b_dir
496 -- is_manual: 寸法変更に伴うものか?
497 local function create_dir_node(b, b_dir, new_dir, is_manual)
498 --print('create new node', b_dir, new_dir)
499 local info = dir_node_aux[b_dir][new_dir]
500 local w = getfield(b, 'width')
501 local h = getfield(b, 'height')
502 local d = getfield(b, 'depth')
503 local db = node_new(getid(b)) -- dir_node
504 set_attr(db, attr_dir,
505 new_dir + (is_manual and dir_node_manual or dir_node_auto))
506 set_attr(db, attr_icflag, PROCESSED)
507 set_attr(b, attr_icflag, PROCESSED)
508 tex_set_attr('global', attr_dir, 0)
509 tex_set_attr('global', attr_icflag, 0)
510 setfield(db, 'dir', getfield(b, 'dir'))
511 setfield(db, 'shift', 0)
512 setfield(db, 'width', info.width(w,h,d))
513 setfield(db, 'height', info.height(w,h,d))
514 setfield(db, 'depth', info.depth(w,h,d))
519 local make_dir_whatsit, process_dir_node
521 make_dir_whatsit = function (head, b, new_dir, origin)
522 -- head: list head, b: box
523 -- origin: コール元 (for debug)
524 -- return value: (new head), (next of b), (new b), (is_b_dir_node)
525 -- (new b): b か dir_node に被せられた b
526 local bh = getlist(b)
527 local box_dir, dn = get_box_dir(b, ltjs.list_dir)
528 -- 既に b の中身にあるwhatsit
530 if box_dir==new_dir then
531 -- 組方向が一緒のボックスなので,何もしなくて良い
532 return head, node_next(b), b, false
533 elseif box_dir%dir_node_auto == new_dir then
534 -- dir_node としてカプセル化されている
535 local bc = node_next(node_next(node_next(bh)))
536 local _, dnc = get_box_dir(b, 0)
537 if dnc then -- free all other dir_node
538 Dnode.flush_list(getfield(dnc, 'value'))
539 setfield(dnc, 'value', nil)
541 set_attr(b, attr_dir, box_dir%dir_node_auto + dir_node_auto)
542 return head, node_next(b), b, true
544 --luatexja.ext_show_node_list(to_node(b), 'mkd> ', print)
546 local nh, nb, ret, flag
547 if box_dir>= dir_node_auto then -- unwrap
549 head, nb, b, b_dir = unwrap_dir_node(b, head, box_dir)
551 if b_dir==new_dir then
552 -- dir_node の中身が周囲の組方向とあっている
553 return head, nb, b, false
554 else box_dir = b_dir end
557 local dnh = getfield(dn, 'value')
558 for x in traverse(dnh) do
559 if has_attr(x, attr_dir)%dir_node_auto == new_dir then
560 setfield(dn, 'value', to_node(node_remove(dnh, x)))
564 Dnode.flush_list(dnh)
565 db = db or create_dir_node(b, box_dir, new_dir, false)
566 local w = getfield(b, 'width')
567 local h = getfield(b, 'height')
568 local d = getfield(b, 'depth')
569 nh, nb = insert_before(head, b, db), nil
570 nh, nb = node_remove(nh, b)
571 local db_head, db_tail = nil
572 for _,v in ipairs(dir_node_aux[box_dir][new_dir][getid(b)]) do
573 local cmd, arg, nn = v[1], v[2]
575 nn = node_new(id_kern)
576 setfield(nn, 'kern', arg(w, h, d))
577 elseif cmd=='whatsit' then
578 nn = node_new(id_whatsit, arg)
579 elseif cmd=='rotate' then
580 nn = node_new(id_whatsit, sid_matrix)
581 setfield(nn, 'data', arg)
582 elseif cmd=='box' then
583 nn = b; setfield(b, 'next', nil)
584 setfield(nn, 'shift', getfield(nn, 'shift') + arg(w,h,d))
587 insert_after(db_head, db_tail, nn)
590 db_head, db_tail = nn, nn
592 setfield(db, 'head', db_head)
595 return nh, nb, ret, flag
598 process_dir_node = function (hd, gc)
599 local x, new_dir = hd, ltjs.list_dir or dir_yoko
602 if (xid==id_hlist and has_attr(x, attr_icflag)%PROCESSED_BEGIN_FLAG~=PACKED)
603 or xid==id_vlist then
604 hd, x = make_dir_whatsit(hd, x, new_dir, 'process_dir_node:' .. gc)
613 local node_prev = (Dnode~=node) and Dnode.getprev or node.prev
614 local function lastbox_hook()
615 start_time_measure('box_primitive_hook')
616 local bn = tex_nest[tex_nest.ptr].tail
618 local b, head = to_direct(bn), to_direct(tex_nest[tex_nest.ptr].head)
620 if bid==id_hlist or bid==id_vlist then
621 local box_dir = get_box_dir(b, 0)
622 if box_dir>= dir_node_auto then -- unwrap dir_node
623 local p = node_prev(b)
624 local dummy1, dummy2, nb = unwrap_dir_node(b, nil, box_dir)
625 setfield(p, 'next', nb); tex_nest[tex_nest.ptr].tail = to_node(nb)
626 setfield(b, 'next', nil); setfield(b, 'head', nil)
629 local _, wh = get_box_dir(b, 0) -- clean dir_node attached to the box
631 Dnode.flush_list(getfield('value', wh))
632 setfield(wh, 'value', nil)
636 stop_time_measure('box_primitive_hook')
639 luatexja.direction.make_dir_whatsit = make_dir_whatsit
640 luatexja.direction.lastbox_hook = lastbox_hook
643 -- \wd, \ht, \dp の代わり
645 local getbox, setdimen = tex.getbox, tex.setdimen
646 local function get_box_dim_common(key, s, l_dir)
647 local s_dir, wh = get_box_dir(s, dir_yoko)
648 if s_dir ~= l_dir then
649 local not_found = true
650 for x in traverse(getfield(wh, 'value')) do
651 if l_dir == has_attr(x, attr_dir)%dir_node_auto then
652 setdimen('ltj@tempdima', getfield(x, key))
653 not_found = false; break
657 local w = getfield(s, 'width')
658 local h = getfield(s, 'height')
659 local d = getfield(s, 'depth')
660 setdimen('ltj@tempdima',
661 dir_node_aux[s_dir][l_dir][key](w,h,d))
664 setdimen('ltj@tempdima', getfield(s, key))
667 local function get_box_dim(key, n)
668 local gt = tex.globaldefs; tex.globaldefs = 0
671 local l_dir = get_dir_count()
673 local b_dir = get_box_dir(s,dir_yoko)
674 if b_dir<dir_node_auto then
675 get_box_dim_common(key, s, l_dir)
676 elseif b_dir%dir_node_auto==l_dir then
677 setdimen('ltj@tempdima', getfield(s, key))
679 get_box_dim_common(key,
680 node_next(node_next(node_next(getlist(s)))), l_dir)
683 setdimen('ltj@tempdima', 0)
687 luatexja.direction.get_box_dim = get_box_dim
689 -- return value: (changed dimen of box itself?)
690 local function set_box_dim_common(key, s, l_dir)
691 local s_dir, wh = get_box_dir(s, dir_yoko)
692 if s_dir ~= l_dir then
694 wh = create_dir_whatsit(getlist(s), 'set_box_dim', s_dir)
695 setfield(s, 'head', wh)
698 local dnh = getfield(wh, 'value')
699 for x in traverse(dnh) do
700 if has_attr(x, attr_dir)%dir_node_auto==l_dir then
705 db = create_dir_node(s, s_dir, l_dir, true)
706 setfield(db, 'next', dnh)
707 setfield(wh, 'value',to_node(db))
709 setfield(db, key, tex.getdimen('ltj@tempdima'))
712 setfield(s, key, tex.getdimen('ltj@tempdima'))
714 -- change dimension of dir_nodes which are created "automatically"
716 = getfield(s,'width'), getfield(s, 'height'), getfield(s, 'depth')
717 for x in traverse(getfield(wh, 'value')) do
718 local x_dir = has_attr(x, attr_dir)
719 if x_dir<dir_node_manual then
720 local info = dir_node_aux[s_dir][x_dir%dir_node_auto]
721 setfield(x, 'width', info.width(bw,bh,bd))
722 setfield(x, 'height', info.height(bw,bh,bd))
723 setfield(x, 'depth', info.depth(bw,bh,bd))
730 local function set_box_dim(key)
731 local n = tex_getcount('ltj@tempcnta')
734 local l_dir = get_dir_count()
736 local b_dir = get_box_dir(s,dir_yoko)
737 if b_dir<dir_node_auto then
738 set_box_dim_common(key, s, l_dir)
739 elseif b_dir%dir_node_auto == l_dir then
741 setfield(s, key, tex.getdimen('ltj@tempdima'))
742 if b_dir<dir_node_manual then
743 set_attr(s, attr_dir, b_dir%dir_node_auto + dir_node_manual)
746 local sid, sl = getid(s), getlist(s)
747 local b = node_next(node_next(node_next(sl)))
748 local info = dir_node_aux[get_box_dir(b,dir_yoko)][b_dir%dir_node_auto]
750 for _,v in ipairs(info[sid]) do
753 getfield(b,'width'), getfield(b, 'height'), getfield(b, 'depth'))
757 if set_box_dim_common(key, b, l_dir) then
759 = getfield(b,'width'), getfield(b, 'height'), getfield(b, 'depth')
760 -- re-calculate shift
761 for i,v in ipairs(info[sid]) do
762 if getid(sl)==id_kern then
763 setfield(sl, 'kern', v[2](bw,bh,bd) )
764 elseif getid(sl)==sid then
765 local d = getfield(sl, 'shift')
766 setfield(sl, 'shift',
767 getfield(sl, 'shift') - shift_old + v[2](bw,bh,bd) )
771 -- re-calculate dimension of s, if s is created "automatically"
772 if b_dir<dir_node_manual then
773 setfield(s, 'width', info.width(bw,bh,bd))
774 setfield(s, 'height', info.height(bw,bh,bd))
775 setfield(s, 'depth', info.depth(bw,bh,bd))
781 luatexja.direction.set_box_dim = set_box_dim
785 local getbox = tex.getbox
786 local function get_register_dir(n)
790 local b_dir = get_box_dir(s, dir_yoko)
791 if b_dir<dir_node_auto then
794 local b_dir = get_box_dir(
795 node_next(node_next(node_next(getlist(s)))), dir_yoko)
802 luatexja.direction.get_register_dir = get_register_dir
806 local font_vert_table = {} -- key: fontnumber
808 local font_vert_basename = {} -- key: basename
809 local function add_feature_table(tname, src, dest)
810 for i,v in pairs(src) do
811 if type(v.slookups)=='table' then
812 local s = v.slookups[tname]
813 if s and not dest[i] then
820 local function prepare_vert_data(n, id)
821 -- test if already loaded
822 if type(id)=='number' then -- sometimes id is an integer
823 font_vert_table[n] = font_vert_table[id]; return
824 elseif (not id) or font_vert_table[n] then return
826 local fname = id.filename
827 local bname = file.basename(fname)
829 font_vert_table[n] = {}; return
830 elseif font_vert_basename[bname] then
831 font_vert_table[n] = font_vert_basename[bname]; return
834 local a = id.resources.sequences
836 local s = id.shared.rawdata.descriptions
837 for i,v in pairs(a) do
838 if v.features.vert or v.features.vrt2 then
839 add_feature_table(v.subtables[1], s, vtable)
843 font_vert_basename[bname] = vtable
844 font_vert_table[n] = vtable
847 function luatexja.direction.get_vert_glyph(n, chr)
848 local fn = font_vert_table[n]
849 return fn and fn[chr] or chr
851 luatexbase.add_to_callback('luatexja.define_font',
852 function (res, name, size, id)
853 prepare_vert_data(id, res)
855 'prepare_vert_data', 1)
857 local function a (n, dat) font_vert_table[n] = dat end
858 luatexja.rmlgbm.vert_addfunc = a
862 -- PACKED の hbox から文字を取り出す
863 -- luatexja.jfmglue.check_box などで使用
865 local function glyph_from_packed(h)
867 return (getid(b)==id_kern)
868 and node_next(node_next(node_next(node_next(b)))) or b
870 luatexja.direction.glyph_from_packed = glyph_from_packed
873 -- adjust and insertion
874 local id_adjust = node.id('adjust')
875 function luatexja.direction.check_adjust_direction()
876 start_time_measure('box_primitive_hook')
877 local list_dir = get_adjust_dir_count()
878 local a = tex_nest[tex_nest.ptr].tail
879 local ad = to_direct(a)
880 if a and getid(ad)==id_adjust then
881 local adj_dir = get_box_dir(ad)
882 if list_dir~=adj_dir then
885 'Direction Incompatible',
886 "\\vadjust's argument and outer vlist must have same direction.")
890 stop_time_measure('box_primitive_hook')
895 local split_dir_whatsit
896 local function dir_adjust_vpack(h, gc)
897 start_time_measure('direction_vpack')
898 local hd = to_direct(h)
899 if gc=='split_keep' then
900 -- supply dir_whatsit
901 hd = create_dir_whatsit_vbox(hd, gc)
902 split_dir_whatsit = hd
903 elseif gc=='split_off' then
906 if bh and getid(bh)==id_whatsit
907 and getsubtype(bh)==sid_user and getfield(bh, 'user_id')==DIR then
908 ltjs.list_dir = has_attr(bh, attr_dir); break
912 if split_dir_whatsit then
913 -- adjust direction of 'split_keep'
914 set_attr(split_dir_whatsit, attr_dir, ltjs.list_dir)
916 split_dir_whatsit=nil
917 elseif gc=='preamble' then
918 split_dir_whatsit=nil
921 hd = process_dir_node(create_dir_whatsit_vbox(hd, gc), gc)
922 split_dir_whatsit=nil
924 stop_time_measure('direction_vpack')
927 luatexbase.add_to_callback('vpack_filter',
929 'ltj.direction', 10000)
933 -- supply direction whatsit to the main vertical list "of the next page"
934 local function dir_adjust_pre_output(h, gc)
935 return to_node(create_dir_whatsit_vbox(to_direct(h), gc))
937 luatexbase.add_to_callback('pre_output_filter',
938 dir_adjust_pre_output,
939 'ltj.direction', 10000)
941 function luatexja.direction.remove_end_whatsit()
942 local h=tex.lists.page_head
944 h.id==id_whatsit and h.subtype==sid_user and
945 h.user_id == DIR then
946 tex.lists.page_head = nil
954 local function dir_adjust_buildpage(info)
956 local head = to_direct(tex.lists.contrib_head)
960 = make_dir_whatsit(head,
964 tex.lists.contrib_head = to_node(head)
968 luatexbase.add_to_callback('buildpage_filter',
969 dir_adjust_buildpage,
970 'ltj.direction', 10000)