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
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
102 luatexja.direction.get_dir_count = get_dir_count
107 local cs_true, cs_false = '\\iftrue', '\\iffalse'
108 luatexja.direction.dir_conditional = function(v)
109 local d = get_dir_count()
110 tex.sprint(cat_lp, (d==v) and cs_true or cs_false )
116 local node_next = node.next
117 local node_set_attr = node.set_attribute
118 local function set_list_direction(v, name)
119 local lv, w = tex_nest.ptr, tex.lists.page_head
120 if not v then v,name = get_dir_count(), nil end
121 if tex.currentgrouptype==6 then
124 "You can't use `\\" .. name .. "' in an align",
125 "To change direction in an align, \n"
126 .. "you shold use \\hbox or \\vbox.")
128 local w = (lv==0) and tex.lists.page_head or tex_nest[lv].head.next
130 luatexja.ext_show_node_list(w, 'set_dir', print)
132 w.id==id_whatsit and w.subtype==sid_user and w.user_id==DIR then
133 node_set_attr(w, attr_dir, v)
137 "Use `\\" .. name .. "' at top of list",
138 'Direction change command by LuaTeX-ja is available\n'
139 .. 'only when the current list is null.')
142 local w = node_new(id_whatsit, sid_user)
143 setfield(w, 'next', nil)
144 setfield(w, 'user_id', DIR)
145 setfield(w, 'type', 110)
146 set_attr(w, attr_dir, v)
148 if lv==0 then page_direction = v end
150 tex_set_attr('global', attr_icflag, 0)
152 tex_set_attr('global', attr_dir, 0)
154 luatexja.direction.set_list_direction = set_list_direction
157 -- ボックスに dir whatsit を追加
158 local function create_dir_whatsit(hd, gc, new_dir)
159 if getid(hd)==id_whatsit and
160 getsubtype(hd)==sid_user and getfield(hd, 'user_id')==DIR then
161 set_attr(hd, attr_icflag,
162 (has_attr(hd, attr_icflag) or 0)%PROCESSED_BEGIN_FLAG
163 + PROCESSED_BEGIN_FLAG)
164 tex_set_attr('global', attr_icflag, 0)
167 local w = node_new(id_whatsit, sid_user)
168 setfield(w, 'next', hd)
169 setfield(w, 'user_id', DIR)
170 setfield(w, 'type', 110)
171 set_attr(w, attr_dir, new_dir)
172 set_attr(w, attr_icflag, PROCESSED_BEGIN_FLAG)
173 set_attr(hd, attr_icflag,
174 (has_attr(hd, attr_icflag) or 0)%PROCESSED_BEGIN_FLAG
175 + PROCESSED_BEGIN_FLAG)
176 tex_set_attr('global', attr_dir, 0)
177 tex_set_attr('global', attr_icflag, 0)
182 -- hpack_filter, vpack_filter, post_line_break_filter
183 -- の結果を組方向を明示するため,先頭に dir_node を設置
185 local function create_dir_whatsit_hpack(h, gc)
186 local hd = to_direct(h)
187 if gc=='fin_row' or gc == 'preamble' then
189 set_attr(hd, attr_icflag, PROCESSED_BEGIN_FLAG)
190 tex_set_attr('global', attr_icflag, 0)
195 return to_node(create_dir_whatsit(hd, gc, ltjs.list_dir))
199 luatexbase.add_to_callback('hpack_filter',
200 create_dir_whatsit_hpack, 'ltj.create_dir_whatsit', 10000)
204 local function create_dir_whatsit_parbox(h, gc)
205 stop_time_measure('tex_linebreak')
206 -- start 側は ltj-debug.lua に
207 local new_dir, hd = ltjs.list_dir, to_direct(h)
208 for line in traverse_id(id_hlist, hd) do
209 local nh = getlist(line)
210 setfield(line, 'head', create_dir_whatsit(nh, gc, new_dir) )
211 set_attr(line, attr_dir, new_dir)
213 tex_set_attr('global', attr_dir, 0)
216 luatexbase.add_to_callback('post_linebreak_filter',
217 create_dir_whatsit_parbox, 'ltj.create_dir_whatsit', 10000)
220 local create_dir_whatsit_vbox
223 local id_glue, sid_parskip = node.id('glue'), 3
224 create_dir_whatsit_vbox = function (hd, gc)
225 ltjs.list_dir = get_dir_count()
226 -- remove dir whatsit
227 for x in traverse_id(id_whatsit, hd) do
228 if getsubtype(x)==sid_user and getfield(x, 'user_id')==DIR then
233 ltjs.list_dir =has_attr(hd,attr_dir)
234 local x = node_next(hd)
235 if getid(x)==id_glue and getsubtype(x)==sid_parskip then
236 node_remove(hd,x); node_free(x)
240 hd = node_remove(hd, wh[i]); node_free(wh[i]); wh[i] = nil
242 if gc=='fin_row' or gc == 'preamble' then
244 set_attr(hd, attr_icflag, PROCESSED_BEGIN_FLAG)
245 tex_set_attr('global', attr_icflag, 0)
249 local n =node_next(hd)
251 local w = create_dir_whatsit(hd, gc, ltjs.list_dir)
252 -- move dir whatsit after hd
253 setfield(hd, 'next', w); setfield(w, 'next', n)
256 hd = create_dir_whatsit(hd, gc, ltjs.list_dir)
263 -- dir_node に包む方法を書いたテーブル
266 local get_h =function (w,h,d) return h end
267 local get_d =function (w,h,d) return d end
268 local get_h_d =function (w,h,d) return h+d end
269 local get_h_d_neg =function (w,h,d) return -h-d end
270 local get_h_neg =function (w,h,d) return -h end
271 local get_d_neg =function (w,h,d) return -d end
272 local get_w_half =function (w,h,d) return 0.5*w end
273 local get_w_neg_half =function (w,h,d) return -0.5*w end
274 local get_w_neg =function (w,h,d) return -w end
275 local get_w =function (w,h,d) return w end
276 local zero = function() return 0 end
278 [dir_yoko] = { -- yoko を
279 [dir_tate] = { -- tate 中で組む
284 { 'whatsit', sid_save },
285 { 'rotate', '0 1 -1 0' },
286 { 'kern', get_w_neg_half },
288 { 'kern', get_w_neg_half },
289 { 'whatsit', sid_restore },
292 { 'whatsit', sid_save },
293 { 'rotate', '0 1 -1 0' },
295 { 'box' , get_w_neg },
296 { 'kern', get_h_d_neg},
297 { 'whatsit', sid_restore },
300 [dir_dtou] = { -- dtou 中で組む
305 { 'whatsit', sid_save },
306 { 'rotate', '0 -1 1 0' },
307 { 'kern', get_w_neg },
308 { 'box', get_d_neg },
309 { 'whatsit', sid_restore },
312 { 'whatsit', sid_save },
313 { 'rotate', '0 -1 1 0' },
314 { 'kern', get_h_d_neg },
316 { 'whatsit', sid_restore },
320 [dir_tate] = { -- tate を
321 [dir_yoko] = { -- yoko 中で組む
326 { 'whatsit', sid_save },
327 { 'rotate', '0 -1 1 0' },
328 { 'kern', get_w_neg },
329 { 'box' , get_d_neg },
330 { 'whatsit', sid_restore },
333 { 'whatsit', sid_save },
334 { 'rotate', '0 -1 1 0' },
335 { 'kern', get_h_d_neg },
337 { 'whatsit', sid_restore },
340 [dir_dtou] = { -- dtou 中で組む
345 { 'whatsit', sid_save },
346 { 'rotate', '-1 0 0 -1' },
347 { 'kern', get_w_neg },
349 { 'whatsit', sid_restore },
352 { 'whatsit', sid_save },
353 { 'rotate', '-1 0 0 -1' },
354 { 'kern', get_h_d_neg },
355 { 'box', get_w_neg },
356 { 'whatsit', sid_restore },
360 [dir_dtou] = { -- dtou を
361 [dir_yoko] = { -- yoko 中で組む
366 { 'whatsit', sid_save },
367 { 'rotate', '0 1 -1 0' },
370 { 'kern', get_w_neg },
371 { 'whatsit', sid_restore },
375 { 'whatsit', sid_save },
376 { 'rotate', '0 1 -1 0' },
377 { 'box', get_w_neg },
378 { 'kern', get_h_d_neg },
379 { 'whatsit', sid_restore },
382 [dir_tate] = { -- tate 中で組む
387 { 'whatsit', sid_save },
388 { 'rotate', '-1 0 0 -1' },
389 { 'kern', get_w_neg },
391 { 'whatsit', sid_restore },
394 { 'whatsit', sid_save },
395 { 'rotate', ' -1 0 0 -1' },
396 { 'kern', get_h_d_neg },
397 { 'box', get_w_neg },
398 { 'whatsit', sid_restore },
405 -- 1st ret val: b の組方向
406 -- 2nd ret val はその DIR whatsit
407 local function get_box_dir(b, default)
408 start_time_measure('get_box_dir')
409 local dir = has_attr(b, attr_dir) or 0
410 local bh = getfield(b,'head')
411 -- b は insert node となりうるので getlist() は使えない
414 if bh and getid(bh)==id_whatsit
415 and getsubtype(bh)==sid_user and getfield(bh, 'user_id')==DIR then
417 dir = (dir==0) and has_attr(bh, attr_dir) or dir
421 stop_time_measure('get_box_dir')
422 return (dir==0 and default or dir), c
426 local getbox = tex.getbox
427 local function check_dir(reg_num)
428 start_time_measure('box_primitive_hook')
429 local list_dir = get_dir_count()
430 local b = tex.getbox(tex_getcount('ltj@tempcnta'))
432 local box_dir = get_box_dir(to_direct(b), dir_yoko)
433 if box_dir%dir_node_auto ~= list_dir%dir_node_auto then
434 luatexja.ext_show_node_list(tex.nest[tex.nest.ptr].head, 'LIST> ', print)
435 luatexja.ext_show_node_list(b, 'BOX> ', print)
438 "Incompatible direction list can't be unboxed",
439 'I refuse to unbox a box in differrent direction.')
442 stop_time_measure('box_primitive_hook')
444 luatexja.direction.check_dir = check_dir
447 -- dir_node に包まれている「本来の中身」を取り出し,
449 local function unwrap_dir_node(b, head, box_dir)
450 -- b: dir_node, head: the head of list, box_dir:
451 -- return values are (new head), (next of b), (contents), (dir of contents)
452 local bh = getlist(b)
453 local bc = node_next(node_next(node_next(bh)))
458 nh = insert_before(head, b, bc)
459 nh, nb = node_remove(nh, b)
460 setfield(b, 'next', nil)
461 setfield(b, 'head', nil)
464 local shift_old, b_dir, wh = nil, get_box_dir(bc, 0)
466 Dnode.flush_list(getfield(wh, 'value'))
467 setfield(wh, 'value', nil)
470 local info = dir_node_aux[b_dir][box_dir%dir_node_auto][getid(bc)]
471 for _,v in ipairs(info) do
474 getfield(bc,'width'), getfield(bc, 'height'), getfield(bc, 'depth'))
478 setfield(bc, 'shift', getfield(bc, 'shift') - shift_old)
479 return nh, nb, bc, b_dir
482 -- is_manual: 寸法変更に伴うものか?
483 local function create_dir_node(b, b_dir, new_dir, is_manual)
484 --print('create new node', b_dir, new_dir)
485 local info = dir_node_aux[b_dir][new_dir]
486 local w = getfield(b, 'width')
487 local h = getfield(b, 'height')
488 local d = getfield(b, 'depth')
489 local db = node_new(getid(b)) -- dir_node
490 set_attr(db, attr_dir,
491 new_dir + (is_manual and dir_node_manual or dir_node_auto))
492 set_attr(db, attr_icflag, PROCESSED)
493 set_attr(b, attr_icflag, PROCESSED)
494 tex_set_attr('global', attr_dir, 0)
495 tex_set_attr('global', attr_icflag, 0)
496 setfield(db, 'dir', getfield(b, 'dir'))
497 setfield(db, 'shift', 0)
498 setfield(db, 'width', info.width(w,h,d))
499 setfield(db, 'height', info.height(w,h,d))
500 setfield(db, 'depth', info.depth(w,h,d))
505 local make_dir_whatsit, process_dir_node
507 make_dir_whatsit = function (head, b, new_dir, origin)
508 -- head: list head, b: box
509 -- origin: コール元 (for debug)
510 -- return value: (new head), (next of b), (new b), (is_b_dir_node)
511 -- (new b): b か dir_node に被せられた b
512 local bh = getlist(b)
513 local box_dir, dn = get_box_dir(b, ltjs.list_dir)
514 -- 既に b の中身にあるwhatsit
516 if box_dir==new_dir then
517 -- 組方向が一緒のボックスなので,何もしなくて良い
518 return head, node_next(b), b, false
519 elseif box_dir%dir_node_auto == new_dir then
520 -- dir_node としてカプセル化されている
521 local bc = node_next(node_next(node_next(bh)))
522 local _, dnc = get_box_dir(b, 0)
523 if dnc then -- free all other dir_node
524 Dnode.flush_list(getfield(dnc, 'value'))
525 setfield(dnc, 'value', nil)
527 set_attr(b, attr_dir, box_dir%dir_node_auto + dir_node_auto)
528 return head, node_next(b), b, true
530 --luatexja.ext_show_node_list(to_node(b), 'mkd> ', print)
532 local nh, nb, ret, flag
533 if box_dir>= dir_node_auto then -- unwrap
535 head, nb, b, b_dir = unwrap_dir_node(b, head, box_dir)
537 if b_dir==new_dir then
538 -- dir_node の中身が周囲の組方向とあっている
539 return head, nb, b, false
540 else box_dir = b_dir end
543 local dnh = getfield(dn, 'value')
544 for x in traverse(dnh) do
545 if has_attr(x, attr_dir)%dir_node_auto == new_dir then
546 setfield(dn, 'value', to_node(node_remove(dnh, x)))
550 Dnode.flush_list(dnh)
551 db = db or create_dir_node(b, box_dir, new_dir, false)
552 local w = getfield(b, 'width')
553 local h = getfield(b, 'height')
554 local d = getfield(b, 'depth')
555 nh, nb = insert_before(head, b, db), nil
556 nh, nb = node_remove(nh, b)
557 local db_head, db_tail = nil
558 for _,v in ipairs(dir_node_aux[box_dir][new_dir][getid(b)]) do
559 local cmd, arg, nn = v[1], v[2]
561 nn = node_new(id_kern)
562 setfield(nn, 'kern', arg(w, h, d))
563 elseif cmd=='whatsit' then
564 nn = node_new(id_whatsit, arg)
565 elseif cmd=='rotate' then
566 nn = node_new(id_whatsit, sid_matrix)
567 setfield(nn, 'data', arg)
568 elseif cmd=='box' then
569 nn = b; setfield(b, 'next', nil)
570 setfield(nn, 'shift', getfield(nn, 'shift') + arg(w,h,d))
573 insert_after(db_head, db_tail, nn)
576 db_head, db_tail = nn, nn
578 setfield(db, 'head', db_head)
581 return nh, nb, ret, flag
584 process_dir_node = function (hd, gc)
585 local x, new_dir = hd, ltjs.list_dir or dir_yoko
588 if (xid==id_hlist and has_attr(x, attr_icflag)%PROCESSED_BEGIN_FLAG~=PACKED)
589 or xid==id_vlist then
590 hd, x = make_dir_whatsit(hd, x, new_dir, 'process_dir_node:' .. gc)
599 local node_prev = (Dnode~=node) and Dnode.getprev or node.prev
600 local function lastbox_hook()
601 start_time_measure('box_primitive_hook')
602 local bn = tex_nest[tex_nest.ptr].tail
604 local b, head = to_direct(bn), to_direct(tex_nest[tex_nest.ptr].head)
606 if bid==id_hlist or bid==id_vlist then
607 local box_dir = get_box_dir(b, 0)
608 if box_dir>= dir_node_auto then -- unwrap dir_node
609 local p = node_prev(b)
610 local dummy1, dummy2, nb = unwrap_dir_node(b, nil, box_dir)
611 setfield(p, 'next', nb); tex_nest[tex_nest.ptr].tail = to_node(nb)
612 setfield(b, 'next', nil); setfield(b, 'head', nil)
615 local _, wh = get_box_dir(b, 0) -- clean dir_node attached to the box
617 Dnode.flush_list(getfield('value', wh))
618 setfield(wh, 'value', nil)
622 stop_time_measure('box_primitive_hook')
625 luatexja.direction.make_dir_whatsit = make_dir_whatsit
626 luatexja.direction.lastbox_hook = lastbox_hook
629 -- \wd, \ht, \dp の代わり
631 local getbox, setdimen = tex.getbox, tex.setdimen
632 local function get_box_dim_common(key, s, l_dir)
633 local s_dir, wh = get_box_dir(s, dir_yoko)
634 if s_dir ~= l_dir then
635 local not_found = true
636 for x in traverse(getfield(wh, 'value')) do
637 if l_dir == has_attr(x, attr_dir)%dir_node_auto then
638 setdimen('ltj@tempdima', getfield(x, key))
639 not_found = false; break
643 local w = getfield(s, 'width')
644 local h = getfield(s, 'height')
645 local d = getfield(s, 'depth')
646 setdimen('ltj@tempdima',
647 dir_node_aux[s_dir][l_dir][key](w,h,d))
650 setdimen('ltj@tempdima', getfield(s, key))
653 local function get_box_dim(key, n)
654 local gt = tex.globaldefs; tex.globaldefs = 0
657 local l_dir = get_dir_count()
659 local b_dir = get_box_dir(s,dir_yoko)
660 if b_dir<dir_node_auto then
661 get_box_dim_common(key, s, l_dir)
662 elseif b_dir%dir_node_auto==l_dir then
663 setdimen('ltj@tempdima', getfield(s, key))
665 get_box_dim_common(key,
666 node_next(node_next(node_next(getlist(s)))), l_dir)
669 setdimen('ltj@tempdima', 0)
673 luatexja.direction.get_box_dim = get_box_dim
675 -- return value: (changed dimen of box itself?)
676 local function set_box_dim_common(key, s, l_dir)
677 local s_dir, wh = get_box_dir(s, dir_yoko)
678 if s_dir ~= l_dir then
680 wh = create_dir_whatsit(getlist(s), 'set_box_dim', s_dir)
681 setfield(s, 'head', wh)
684 local dnh = getfield(wh, 'value')
685 for x in traverse(dnh) do
686 if has_attr(x, attr_dir)%dir_node_auto==l_dir then
691 db = create_dir_node(s, s_dir, l_dir, true)
692 setfield(db, 'next', dnh)
693 setfield(wh, 'value',to_node(db))
695 setfield(db, key, tex.getdimen('ltj@tempdima'))
698 setfield(s, key, tex.getdimen('ltj@tempdima'))
700 -- change dimension of dir_nodes which are created "automatically"
702 = getfield(s,'width'), getfield(s, 'height'), getfield(s, 'depth')
703 for x in traverse(getfield(wh, 'value')) do
704 local x_dir = has_attr(x, attr_dir)
705 if x_dir<dir_node_manual then
706 local info = dir_node_aux[s_dir][x_dir%dir_node_auto]
707 setfield(x, 'width', info.width(bw,bh,bd))
708 setfield(x, 'height', info.height(bw,bh,bd))
709 setfield(x, 'depth', info.depth(bw,bh,bd))
716 local function set_box_dim(key)
717 local n = tex_getcount('ltj@tempcnta')
720 local l_dir = get_dir_count()
722 local b_dir = get_box_dir(s,dir_yoko)
723 if b_dir<dir_node_auto then
724 set_box_dim_common(key, s, l_dir)
725 elseif b_dir%dir_node_auto == l_dir then
727 setfield(s, key, tex.getdimen('ltj@tempdima'))
728 if b_dir<dir_node_manual then
729 set_attr(s, attr_dir, b_dir%dir_node_auto + dir_node_manual)
732 local sid, sl = getid(s), getlist(s)
733 local b = node_next(node_next(node_next(sl)))
734 local info = dir_node_aux[get_box_dir(b,dir_yoko)][b_dir%dir_node_auto]
736 for _,v in ipairs(info[sid]) do
739 getfield(b,'width'), getfield(b, 'height'), getfield(b, 'depth'))
743 if set_box_dim_common(key, b, l_dir) then
745 = getfield(b,'width'), getfield(b, 'height'), getfield(b, 'depth')
746 -- re-calculate shift
747 for i,v in ipairs(info[sid]) do
748 if getid(sl)==id_kern then
749 setfield(sl, 'kern', v[2](bw,bh,bd) )
750 elseif getid(sl)==sid then
751 local d = getfield(sl, 'shift')
752 setfield(sl, 'shift',
753 getfield(sl, 'shift') - shift_old + v[2](bw,bh,bd) )
757 -- re-calculate dimension of s, if s is created "automatically"
758 if b_dir<dir_node_manual then
759 setfield(s, 'width', info.width(bw,bh,bd))
760 setfield(s, 'height', info.height(bw,bh,bd))
761 setfield(s, 'depth', info.depth(bw,bh,bd))
767 luatexja.direction.set_box_dim = set_box_dim
770 -- \ifydir, \iftdir, \ifddir
772 local getbox = tex.getbox
773 local function box_dir_conditional(n, mode)
778 local b_dir = get_box_dir(s, dir_yoko)
779 if b_dir<dir_node_auto then
782 local b_dir = get_box_dir(
783 node_next(node_next(node_next(getlist(s)))), dir_yoko)
787 tex.sprint(cat_lp, '\\if' .. tostring(res))
789 luatexja.direction.box_dir_conditional = box_dir_conditional
793 local font_vert_table = {} -- key: fontnumber
795 local font_vert_basename = {} -- key: basename
796 local function add_feature_table(tname, src, dest)
797 for i,v in pairs(src) do
798 if type(v.slookups)=='table' then
799 local s = v.slookups[tname]
800 if s and not dest[i] then
807 local function prepare_vert_data(n, id)
808 -- test if already loaded
809 if type(id)=='number' then -- sometimes id is an integer
810 font_vert_table[n] = font_vert_table[id]; return
811 elseif (not id) or font_vert_table[n] then return
813 local fname = id.filename
814 local bname = file.basename(fname)
816 font_vert_table[n] = {}; return
817 elseif font_vert_basename[bname] then
818 font_vert_table[n] = font_vert_basename[bname]; return
821 local a = id.resources.sequences
823 local s = id.shared.rawdata.descriptions
824 for i,v in pairs(a) do
825 if v.features.vert or v.features.vrt2 then
826 add_feature_table(v.subtables[1], s, vtable)
830 font_vert_basename[bname] = vtable
831 font_vert_table[n] = vtable
834 function luatexja.direction.get_vert_glyph(n, chr)
835 local fn = font_vert_table[n]
836 return fn and fn[chr] or chr
838 luatexbase.add_to_callback('luatexja.define_font',
839 function (res, name, size, id)
840 prepare_vert_data(id, res)
842 'prepare_vert_data', 1)
844 local function a (n, dat) font_vert_table[n] = dat end
845 luatexja.rmlgbm.vert_addfunc = a
849 -- PACKED の hbox から文字を取り出す
850 -- luatexja.jfmglue.check_box などで使用
852 local function glyph_from_packed(h)
854 return (getid(b)==id_kern)
855 and node_next(node_next(node_next(node_next(b)))) or b
857 luatexja.direction.glyph_from_packed = glyph_from_packed
860 -- adjust and insertion
861 local id_adjust = node.id('adjust')
862 function luatexja.direction.check_adjust_direction()
863 start_time_measure('box_primitive_hook')
864 local list_dir = tex_getcount('ltj@adjdir@count')
865 local a = tex_nest[tex_nest.ptr].tail
866 local ad = to_direct(a)
867 if a and getid(ad)==id_adjust then
868 local adj_dir = get_box_dir(ad)
869 if list_dir~=adj_dir then
872 'Direction Incompatible',
873 "\\vadjust's argument and outer vlist must have same direction.")
877 stop_time_measure('box_primitive_hook')
882 local split_dir_whatsit
883 local function dir_adjust_vpack(h, gc)
884 start_time_measure('direction_vpack')
885 local hd = to_direct(h)
886 if gc=='split_keep' then
887 -- supply dir_whatsit
888 hd = create_dir_whatsit_vbox(hd, gc)
889 split_dir_whatsit = hd
890 elseif gc=='split_off' then
893 if bh and getid(bh)==id_whatsit
894 and getsubtype(bh)==sid_user and getfield(bh, 'user_id')==DIR then
895 ltjs.list_dir = has_attr(bh, attr_dir); break
899 if split_dir_whatsit then
900 -- adjust direction of 'split_keep'
901 set_attr(split_dir_whatsit, attr_dir, ltjs.list_dir)
903 split_dir_whatsit=nil
904 elseif gc=='preamble' then
905 split_dir_whatsit=nil
908 hd = process_dir_node(create_dir_whatsit_vbox(hd, gc), gc)
909 split_dir_whatsit=nil
911 stop_time_measure('direction_vpack')
914 luatexbase.add_to_callback('vpack_filter',
916 'ltj.direction', 10000)
920 -- supply direction whatsit to the main vertical list "of the next page"
921 local function dir_adjust_pre_output(h, gc)
922 return to_node(create_dir_whatsit_vbox(to_direct(h), gc))
924 luatexbase.add_to_callback('pre_output_filter',
925 dir_adjust_pre_output,
926 'ltj.direction', 10000)
928 function luatexja.direction.remove_end_whatsit()
929 local h=tex.lists.page_head
931 h.id==id_whatsit and h.subtype==sid_user and
932 h.user_id == DIR then
933 tex.lists.page_head = nil
941 local function dir_adjust_buildpage(info)
943 local head = to_direct(tex.lists.contrib_head)
947 = make_dir_whatsit(head,
951 tex.lists.contrib_head = to_node(head)
955 luatexbase.add_to_callback('buildpage_filter',
956 dir_adjust_buildpage,
957 'ltj.direction', 10000)