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 function get_dir_count()
80 return tex_getcount('ltj@dir@count')
82 luatexja.direction.get_dir_count = get_dir_count
84 local node_next = node.next
85 local node_set_attr = node.set_attribute
86 local function set_list_direction(v, name)
87 local lv, w = tex_nest[tex_nest.ptr], tex.lists.page_head
88 if lv.mode == 1 and w then
89 if w.id==id_whatsit and w.subtype==sid_user
90 and w.user_id==DIR then
91 node_set_attr(w, attr_dir, v)
94 local w = node_next(lv.head)
97 if getid(w)==id_whatsit and getsubtype(w)==sid_user
98 and getfield(w, 'user_id')==DIR then
99 set_attr(w, attr_dir, v)
103 "Use `\\" .. name .. "' at top of list",
104 'Direction change command by LuaTeX-ja is available\n'
105 .. 'only when the current list is null.')
108 local w = node_new(id_whatsit, sid_user)
109 setfield(w, 'next', nil)
110 setfield(w, 'user_id', DIR)
111 setfield(w, 'type', 110)
112 set_attr(w, attr_dir, v)
115 tex_set_attr('global', attr_icflag, 0)
117 tex_set_attr('global', attr_dir, 0)
119 luatexja.direction.set_list_direction = set_list_direction
122 -- ボックスに dir whatsit を追加
123 local function create_dir_whatsit(hd, gc, new_dir)
124 local w = node_new(id_whatsit, sid_user)
125 setfield(w, 'next', hd)
126 setfield(w, 'user_id', DIR)
127 setfield(w, 'type', 110)
128 set_attr(w, attr_dir, new_dir)
129 tex_set_attr('global', attr_dir, 0)
130 set_attr(w, attr_icflag, PROCESSED_BEGIN_FLAG)
131 set_attr(hd, attr_icflag, (has_attr(hd, attr_icflag) or 0) + PROCESSED_BEGIN_FLAG)
132 tex_set_attr('global', attr_icflag, 0)
136 -- hpack_filter, vpack_filter, post_line_break_filter
137 -- の結果を組方向を明示するため,先頭に dir_node を設置
139 local function create_dir_whatsit_hpack(h, gc)
140 local hd = to_direct(h)
141 if gc=='fin_row' or gc == 'preamble' then
143 set_attr(hd, attr_icflag, PROCESSED_BEGIN_FLAG)
144 tex_set_attr('global', attr_icflag, 0)
149 if (c=='hbox' or gc=='adjusted_hbox') and
150 not node_next(hd) and getid(hd)==id_whatsit and
151 getsubtype(hd)==sid_user and getfield(hd, 'user_id')==DIR then
152 -- avoid double whatsit
153 set_attr(hd, attr_icflag, PROCESSED_BEGIN_FLAG)
154 tex_set_attr('global', attr_icflag, 0)
157 return to_node(create_dir_whatsit(hd, gc, ltjs.list_dir))
162 luatexbase.add_to_callback('hpack_filter',
163 create_dir_whatsit_hpack, 'ltj.create_dir_whatsit', 10000)
167 local function create_dir_whatsit_parbox(h, gc)
168 stop_time_measure('tex_linebreak')
169 -- start 側は ltj-debug.lua に
170 local new_dir, hd = ltjs.list_dir, to_direct(h)
171 for line in traverse_id(id_hlist, hd) do
172 set_attr(line, attr_dir, new_dir)
174 tex_set_attr('global', attr_dir, 0)
175 return h --to_node(create_dir_whatsit(hd, gc, new_dir))
177 luatexbase.add_to_callback('post_linebreak_filter',
178 create_dir_whatsit_parbox, 'ltj.create_dir_whatsit', 10000)
181 local create_dir_whatsit_vbox
184 local id_glue, sid_parskip = node.id('glue'), 3
185 create_dir_whatsit_vbox = function (hd, gc)
186 ltjs.list_dir = get_dir_count()
187 -- remove dir whatsit
188 for x in traverse_id(id_whatsit, hd) do
189 if getsubtype(x)==sid_user and getfield(x, 'user_id')==DIR then
194 ltjs.list_dir =has_attr(hd,attr_dir)
195 local x = node_next(hd)
196 if getid(x)==id_glue and getsubtype(x)==sid_parskip then
197 node_remove(hd,x); node_free(x)
201 hd = node_remove(hd, wh[i]); node_free(wh[i]); wh[i] = nil
203 if gc=='fin_row' or gc == 'preamble' then
205 set_attr(hd, attr_icflag, PROCESSED_BEGIN_FLAG)
206 tex_set_attr('global', attr_icflag, 0)
210 local n =node_next(hd)
211 if gc=='vtop' and n then
212 local w = create_dir_whatsit(hd, gc, ltjs.list_dir)
213 -- move dir whatsit after hd
214 setfield(hd, 'next', w); setfield(w, 'next', n)
217 return create_dir_whatsit(hd, gc, ltjs.list_dir)
223 -- dir_node に包む方法を書いたテーブル
226 local get_h =function (w,h,d) return h end
227 local get_d =function (w,h,d) return d end
228 local get_h_d =function (w,h,d) return h+d end
229 local get_h_d_neg =function (w,h,d) return -h-d end
230 local get_h_neg =function (w,h,d) return -h end
231 local get_d_neg =function (w,h,d) return -d end
232 local get_w_half =function (w,h,d) return 0.5*w end
233 local get_w_neg_half =function (w,h,d) return -0.5*w end
234 local get_w_neg =function (w,h,d) return -w end
235 local get_w =function (w,h,d) return w end
236 local zero = function() return 0 end
238 [dir_yoko] = { -- yoko を
239 [dir_tate] = { -- tate 中で組む
244 { 'whatsit', sid_save },
245 { 'rotate', '0 1 -1 0' },
246 { 'kern', get_w_neg_half },
248 { 'kern', get_w_neg_half },
249 { 'whatsit', sid_restore },
252 { 'whatsit', sid_save },
253 { 'rotate', '0 1 -1 0' },
255 { 'box' , get_w_neg },
256 { 'kern', get_h_d_neg},
257 { 'whatsit', sid_restore },
260 [dir_dtou] = { -- dtou 中で組む
265 { 'whatsit', sid_save },
266 { 'rotate', '0 -1 1 0' },
267 { 'kern', get_w_neg },
268 { 'box', get_d_neg },
269 { 'whatsit', sid_restore },
272 { 'whatsit', sid_save },
273 { 'rotate', '0 -1 1 0' },
274 { 'kern', get_h_d_neg },
276 { 'whatsit', sid_restore },
280 [dir_tate] = { -- tate を
281 [dir_yoko] = { -- yoko 中で組む
286 { 'whatsit', sid_save },
287 { 'rotate', '0 -1 1 0' },
288 { 'kern', get_w_neg },
289 { 'box' , get_d_neg },
290 { 'whatsit', sid_restore },
293 { 'whatsit', sid_save },
294 { 'rotate', '0 -1 1 0' },
295 { 'kern', get_h_d_neg },
297 { 'whatsit', sid_restore },
300 [dir_dtou] = { -- dtou 中で組む
305 { 'whatsit', sid_save },
306 { 'rotate', '-1 0 0 -1' },
307 { 'kern', get_w_neg },
309 { 'whatsit', sid_restore },
312 { 'whatsit', sid_save },
313 { 'rotate', '-1 0 0 -1' },
314 { 'kern', get_h_d_neg },
315 { 'box', get_w_neg },
316 { 'whatsit', sid_restore },
320 [dir_dtou] = { -- dtou を
321 [dir_yoko] = { -- yoko 中で組む
326 { 'whatsit', sid_save },
327 { 'rotate', '0 1 -1 0' },
330 { 'kern', get_w_neg },
331 { 'whatsit', sid_restore },
335 { 'whatsit', sid_save },
336 { 'rotate', '0 1 -1 0' },
337 { 'box', get_w_neg },
338 { 'kern', get_h_d_neg },
339 { 'whatsit', sid_restore },
342 [dir_tate] = { -- tate 中で組む
347 { 'whatsit', sid_save },
348 { 'rotate', '-1 0 0 -1' },
349 { 'kern', get_w_neg },
351 { 'whatsit', sid_restore },
354 { 'whatsit', sid_save },
355 { 'rotate', ' -1 0 0 -1' },
356 { 'kern', get_h_d_neg },
357 { 'box', get_w_neg },
358 { 'whatsit', sid_restore },
365 -- b に DIR whatsit があればその内容を attr_dir にうつす (1st ret val)
366 -- 2nd ret val はその DIR whatsit
367 local function get_box_dir(b, default)
368 start_time_measure('get_box_dir')
369 local dir = has_attr(b, attr_dir) or 0
370 local bh = getfield(b,'head')
371 -- b は insert node となりうるので getlist() は使えない
374 if bh and getid(bh)==id_whatsit
375 and getsubtype(bh)==sid_user and getfield(bh, 'user_id')==DIR then
378 dir = has_attr(bh, attr_dir)
379 set_attr(b, attr_dir, dir)
380 tex_set_attr('global', attr_dir, 0)
385 stop_time_measure('get_box_dir')
386 return (dir==0 and default or dir), c
390 local getbox = tex.getbox
391 local function check_dir(reg_num)
392 start_time_measure('box_primitive_hook')
393 local list_dir = get_dir_count()
394 local b = tex.getbox(tex_getcount('ltj@tempcnta'))
396 local box_dir = get_box_dir(to_direct(b), dir_yoko)
397 if box_dir%dir_node_auto ~= list_dir%dir_node_auto then
400 "Incompatible direction list can't be unboxed",
401 'I refuse to unbox a box in differrent direction.')
404 stop_time_measure('box_primitive_hook')
406 luatexja.direction.check_dir = check_dir
409 -- dir_node に包まれている「本来の中身」を取り出し,
411 local function unwrap_dir_node(b, head, box_dir)
412 -- b: dir_node, head: the head of list, box_dir:
413 -- return values are (new head), (next of b), (contents), (dir of contents)
414 local bh = getlist(b)
415 local bc = node_next(node_next(node_next(bh)))
420 nh = insert_before(head, b, bc)
421 nh, nb = node_remove(nh, b)
422 setfield(b, 'next', nil)
423 setfield(b, 'head', nil)
426 local shift_old, b_dir, wh = nil, get_box_dir(bc, 0)
428 Dnode.flush_list(getfield(wh, 'value'))
429 setfield(wh, 'value', nil)
432 local info = dir_node_aux[b_dir][box_dir%dir_node_auto][getid(bc)]
433 for _,v in ipairs(info) do
436 getfield(bc,'width'), getfield(bc, 'height'), getfield(bc, 'depth'))
440 setfield(bc, 'shift', getfield(bc, 'shift') - shift_old)
441 return nh, nb, bc, b_dir
444 -- is_manual: 寸法変更に伴うものか?
445 local function create_dir_node(b, b_dir, new_dir, is_manual)
446 local info = dir_node_aux[b_dir][new_dir]
447 local w = getfield(b, 'width')
448 local h = getfield(b, 'height')
449 local d = getfield(b, 'depth')
450 local db = node_new(getid(b))
451 set_attr(db, attr_dir,
452 new_dir + (is_manual and dir_node_manual or dir_node_auto))
453 tex_set_attr('global', attr_dir, 0)
454 set_attr(db, attr_icflag, PROCESSED)
455 set_attr(b, attr_icflag, PROCESSED)
456 setfield(db, 'dir', getfield(b, 'dir'))
457 setfield(db, 'shift', 0)
458 setfield(db, 'width', info.width(w,h,d))
459 setfield(db, 'height', info.height(w,h,d))
460 setfield(db, 'depth', info.depth(w,h,d))
465 local make_dir_whatsit, process_dir_node
467 make_dir_whatsit = function (head, b, new_dir, origin)
468 -- head: list head, b: box
469 -- origin: コール元 (for debug)
470 -- return value: (new head), (next of b), (new b), (is_b_dir_node)
471 -- (new b): b か dir_node に被せられた b
472 local bh = getlist(b)
473 local box_dir, dn = get_box_dir(b, ltjs.list_dir)
474 -- 既に b の中身にあるwhatsit
475 if box_dir==new_dir then
476 return head, node_next(b), b, false
477 elseif box_dir%dir_node_auto == new_dir then
478 local bc = node_next(node_next(node_next(bh)))
479 local _, dnc = get_box_dir(b, 0)
480 if dnc then -- free all other dir_node
481 Dnode.flush_list(getfield(dnc, 'value'))
482 setfield(dnc, 'value', nil)
484 set_attr(b, attr_dir, box_dir%dir_node_auto + dir_node_auto)
485 return head, node_next(b), b, true
487 local nh, nb, ret, flag
488 if box_dir>= dir_node_auto then -- unwrap
490 head, nb, b, b_dir = unwrap_dir_node(b, head, box_dir)
492 if b_dir==new_dir then -- no need to create new dir_node
493 return head, nb, b, false
494 else box_dir = b_dir end
497 local dnh = getfield(dn, 'value')
498 for x in traverse(dnh) do
499 if has_attr(x, attr_dir)%dir_node_auto == new_dir then
500 setfield(dn, 'value', to_node(node_remove(dnh, x)))
504 Dnode.flush_list(dnh)
505 db = db or create_dir_node(b, box_dir, new_dir, false)
506 local w = getfield(b, 'width')
507 local h = getfield(b, 'height')
508 local d = getfield(b, 'depth')
509 nh, nb = insert_before(head, b, db), nil
510 nh, nb = node_remove(nh, b)
511 local db_head, db_tail = nil
512 for _,v in ipairs(dir_node_aux[box_dir][new_dir][getid(b)]) do
513 local cmd, arg, nn = v[1], v[2]
515 nn = node_new(id_kern)
516 setfield(nn, 'kern', arg(w, h, d))
517 elseif cmd=='whatsit' then
518 nn = node_new(id_whatsit, arg)
519 elseif cmd=='rotate' then
520 nn = node_new(id_whatsit, sid_matrix)
521 setfield(nn, 'data', arg)
522 elseif cmd=='box' then
523 nn = b; setfield(b, 'next', nil)
524 setfield(nn, 'shift', getfield(nn, 'shift') + arg(w,h,d))
527 insert_after(db_head, db_tail, nn)
530 db_head, db_tail = nn, nn
532 setfield(db, 'head', db_head)
535 return nh, nb, ret, flag
538 process_dir_node = function (hd, gc)
539 local x, new_dir = hd, ltjs.list_dir or dir_yoko
542 if (xid==id_hlist and has_attr(x, attr_icflag)%PROCESSED_BEGIN_FLAG~=PACKED)
543 or xid==id_vlist then
544 hd, x = make_dir_whatsit(hd, x, new_dir, 'process_dir_node:' .. gc)
553 local node_prev = (Dnode~=node) and Dnode.getprev or node.prev
554 local function lastbox_hook()
555 start_time_measure('box_primitive_hook')
556 local bn = tex_nest[tex_nest.ptr].tail
558 local b, head = to_direct(bn), to_direct(tex_nest[tex_nest.ptr].head)
560 if bid==id_hlist or bid==id_vlist then
561 local box_dir = get_box_dir(b, 0)
562 if box_dir>= dir_node_auto then -- unwrap
563 local p = node_prev(b)
564 local dummy1, dummy2, nb = unwrap_dir_node(b, nil, box_dir)
565 setfield(p, 'next', nb); tex_nest[tex_nest.ptr].tail = to_node(nb)
566 setfield(b, 'next', nil); setfield(b, 'head', nil)
571 stop_time_measure('box_primitive_hook')
574 luatexja.direction.make_dir_whatsit = make_dir_whatsit
575 luatexja.direction.lastbox_hook = lastbox_hook
578 -- \wd, \ht, \dp の代わり
580 local getbox, setdimen = tex.getbox, tex.setdimen
581 local function get_box_dim_common(key, s, l_dir)
582 local s_dir, wh = get_box_dir(s, dir_yoko)
583 if s_dir ~= l_dir then
584 local not_found = true
585 for x in traverse(getfield(wh, 'value')) do
586 if l_dir == has_attr(x, attr_dir)%dir_node_auto then
587 setdimen('ltj@tempdima', getfield(x, key))
588 not_found = false; break
592 local w = getfield(s, 'width')
593 local h = getfield(s, 'height')
594 local d = getfield(s, 'depth')
595 setdimen('ltj@tempdima',
596 dir_node_aux[s_dir][l_dir][key](w,h,d))
599 setdimen('ltj@tempdima', getfield(s, key))
602 local function get_box_dim(key, n)
603 local gt = tex.globaldefs; tex.globaldefs = 0
606 local l_dir = get_dir_count()
608 local b_dir = has_attr(s, attr_dir) or 0
609 if b_dir<dir_node_auto then
610 get_box_dim_common(key, s, l_dir)
611 elseif b_dir%dir_node_auto==l_dir then
612 setdimen('ltj@tempdima', getfield(s, key))
614 get_box_dim_common(key,
615 node_next(node_next(node_next(getlist(s)))), l_dir)
618 setdimen('ltj@tempdima', 0)
622 luatexja.direction.get_box_dim = get_box_dim
624 -- return value: (changed dimen of box itself?)
625 local function set_box_dim_common(key, s, l_dir)
626 local s_dir, wh = get_box_dir(s, dir_yoko)
627 if s_dir ~= l_dir then
629 wh = create_dir_whatsit(getlist(s), 'set_box_dim', s_dir)
630 setfield(s, 'head', wh)
633 local dnh = getfield(wh, 'value')
634 for x in traverse(dnh) do
635 if has_attr(x, attr_dir)%dir_node_auto==l_dir then
640 db = create_dir_node(s, s_dir, l_dir, true)
641 setfield(db, 'next', dnh)
642 setfield(wh, 'value',to_node(db))
644 setfield(db, key, tex.getdimen('ltj@tempdima'))
647 setfield(s, key, tex.getdimen('ltj@tempdima'))
649 -- change dimension of dir_nodes which are created "automatically"
651 = getfield(s,'width'), getfield(s, 'height'), getfield(s, 'depth')
652 for x in traverse(getfield(wh, 'value')) do
653 local x_dir = has_attr(x, attr_dir)
654 if x_dir<dir_node_manual then
655 local info = dir_node_aux[s_dir][x_dir%dir_node_auto]
656 setfield(x, 'width', info.width(bw,bh,bd))
657 setfield(x, 'height', info.height(bw,bh,bd))
658 setfield(x, 'depth', info.depth(bw,bh,bd))
665 local function set_box_dim(key)
666 local n = tex_getcount('ltj@tempcnta')
669 local l_dir = get_dir_count()
671 local b_dir = has_attr(s, attr_dir) or 0
672 if b_dir<dir_node_auto then
673 set_box_dim_common(key, s, l_dir)
674 elseif b_dir%dir_node_auto == l_dir then
675 setfield(s, key, tex.getdimen('ltj@tempdima'))
676 if b_dir<dir_node_manual then
677 set_attr(s, attr_dir, b_dir%dir_node_auto + dir_node_manual)
680 local sid, sl = getid(s), getlist(s)
681 local b = node_next(node_next(node_next(sl)))
682 local info = dir_node_aux[get_box_dir(b)][b_dir%dir_node_auto]
684 for _,v in ipairs(info[sid]) do
687 getfield(b,'width'), getfield(b, 'height'), getfield(b, 'depth'))
691 if set_box_dim_common(key, b, l_dir) then
693 = getfield(b,'width'), getfield(b, 'height'), getfield(b, 'depth')
694 -- re-calculate shift
695 for i,v in ipairs(info[sid]) do
696 if getid(sl)==id_kern then
697 setfield(sl, 'kern', v[2](bw,bh,bd) )
698 elseif getid(sl)==sid then
699 local d = getfield(sl, 'shift')
700 setfield(sl, 'shift',
701 getfield(sl, 'shift') - shift_old + v[2](bw,bh,bd) )
705 -- re-calculate dimension of s, if s is created "automatically"
706 if b_dir<dir_node_manual then
707 setfield(s, 'width', info.width(bw,bh,bd))
708 setfield(s, 'height', info.height(bw,bh,bd))
709 setfield(s, 'depth', info.depth(bw,bh,bd))
715 luatexja.direction.set_box_dim = set_box_dim
718 -- \ifydir, \iftdir, \ifddir
720 local getbox = tex.getbox
721 local function dir_conditional(n, mode)
726 local b_dir = get_box_dir(s, dir_yoko)
727 if b_dir<dir_node_auto then
730 local b_dir = get_box_dir(
731 node_next(node_next(node_next(getlist(s)))), dir_yoko)
735 tex.sprint(cat_lp, '\\if' .. tostring(res))
737 luatexja.direction.dir_conditional = dir_conditional
741 local font_vert_table = {} -- key: fontnumber
743 local font_vert_basename = {} -- key: basename
744 local function add_feature_table(tname, src, dest)
745 for i,v in pairs(src) do
746 if type(v.slookups)=='table' then
747 local s = v.slookups[tname]
748 if s and not dest[i] then
755 local function prepare_vert_data(n, id)
756 -- test if already loaded
757 if type(id)=='number' then -- sometimes id is an integer
758 font_vert_table[n] = font_vert_table[id]; return
759 elseif (not id) or font_vert_table[n] then return
761 local fname = id.filename
762 local bname = file.basename(fname)
764 font_vert_table[n] = {}; return
765 elseif font_vert_basename[bname] then
766 font_vert_table[n] = font_vert_basename[bname]; return
769 local a = id.resources.sequences
771 local s = id.shared.rawdata.descriptions
772 for i,v in pairs(a) do
773 if v.features.vert or v.features.vrt2 then
774 add_feature_table(v.subtables[1], s, vtable)
778 font_vert_basename[bname] = vtable
779 font_vert_table[n] = vtable
782 function luatexja.direction.get_vert_glyph(n, chr)
783 local fn = font_vert_table[n]
784 return fn and fn[chr] or chr
786 luatexbase.add_to_callback('luatexja.define_font',
787 function (res, name, size, id)
788 prepare_vert_data(id, res)
790 'prepare_vert_data', 1)
792 local function a (n, dat) font_vert_table[n] = dat end
793 luatexja.rmlgbm.vert_addfunc = a
797 -- PACKED の hbox から文字を取り出す
798 -- luatexja.jfmglue.check_box などで使用
800 local function glyph_from_packed(h)
802 return (getid(b)==id_kern)
803 and node_next(node_next(node_next(node_next(b)))) or b
805 luatexja.direction.glyph_from_packed = glyph_from_packed
808 -- adjust and insertion
809 local id_adjust = node.id('adjust')
810 function luatexja.direction.check_adjust_direction()
811 start_time_measure('box_primitive_hook')
812 local list_dir = tex_getcount('ltj@adjdir@count')
813 local a = tex_nest[tex_nest.ptr].tail
814 local ad = to_direct(a)
815 if a and getid(ad)==id_adjust then
816 local adj_dir = get_box_dir(ad)
817 if list_dir~=adj_dir then
820 'Direction Incompatible',
821 "\\vadjust's argument and outer vlist must have same direction.")
825 stop_time_measure('box_primitive_hook')
830 local split_dir_whatsit
831 local function dir_adjust_vpack(h, gc)
832 start_time_measure('direction_vpack')
833 local hd = to_direct(h)
834 if gc=='split_keep' then
835 -- supply dir_whatsit
836 hd = create_dir_whatsit_vbox(hd, gc)
837 split_dir_whatsit = hd
838 elseif gc=='split_off' then
841 if bh and getid(bh)==id_whatsit
842 and getsubtype(bh)==sid_user and getfield(bh, 'user_id')==DIR then
843 ltjs.list_dir = has_attr(bh, attr_dir); break
847 if split_dir_whatsit then
848 -- adjust direction of 'split_keep'
849 set_attr(split_dir_whatsit, attr_dir, ltjs.list_dir)
850 tex_set_attr('global', attr_dir, 0)
852 split_dir_whatsit=nil
855 hd = process_dir_node(create_dir_whatsit_vbox(hd, gc), gc)
856 split_dir_whatsit=nil
858 stop_time_measure('direction_vpack')
861 luatexbase.add_to_callback('vpack_filter',
863 'ltj.direction', 10000)
867 -- supply direction whatsit to the main vertical list "of the next page"
868 local function dir_adjust_pre_output(h, gc)
869 return to_node(create_dir_whatsit_vbox(to_direct(h), gc))
871 luatexbase.add_to_callback('pre_output_filter',
872 dir_adjust_pre_output,
873 'ltj.direction', 10000)
875 function luatexja.direction.remove_end_whatsit()
876 local h=tex.lists.page_head
878 h.id==id_whatsit and h.subtype==sid_user and
879 h.user_id == DIR then
880 tex.lists.page_head = nil
888 local function dir_adjust_buildpage(info)
890 local head = to_direct(tex.lists.contrib_head)
893 = make_dir_whatsit(head,
897 tex.lists.contrib_head = to_node(head)
900 luatexbase.add_to_callback('buildpage_filter',
901 dir_adjust_buildpage,
902 'ltj.direction', 10000)