2 -- src/ltj-direction.lua
5 luatexja.load_module('base'); local ltjb = luatexja.base
6 luatexja.load_module('stack'); local ltjs = luatexja.stack
7 luatexja.load_module('rmlgbm'); local ltjr = luatexja.rmlgbm
8 luatexja.direction = {}
10 local attr_dir = luatexbase.attributes['ltj@dir']
11 local attr_icflag = luatexbase.attributes['ltj@icflag']
13 local cat_lp = luatexbase.catcodetables['latex-package']
14 local Dnode = node.direct or node
15 local nullfunc = function (n) return n end
16 local to_node = (Dnode ~= node) and Dnode.tonode or nullfunc
17 local to_direct = (Dnode ~= node) and Dnode.todirect or nullfunc
18 local has_attr = Dnode.has_attribute
19 local set_attr = Dnode.set_attribute
20 local insert_before = Dnode.insert_before
21 local insert_after = Dnode.insert_after
22 local getid = (Dnode ~= node) and Dnode.getid or function(n) return n.id end
23 local getsubtype = (Dnode ~= node) and Dnode.getsubtype or function(n) return n.subtype end
24 local getlist = (Dnode ~= node) and Dnode.getlist or function(n) return n.head end
25 local setfield = (Dnode ~= node) and Dnode.setfield or function(n, i, c) n[i] = c end
26 local getfield = (Dnode ~= node) and Dnode.getfield or function(n, i) return n[i] end
27 local node_new = Dnode.new
28 local node_tail = Dnode.tail
29 local node_free = Dnode.free
30 local node_remove = Dnode.remove
31 local node_next = (Dnode ~= node) and Dnode.getnext or node.next
32 local traverse = Dnode.traverse
33 local traverse_id = Dnode.traverse_id
34 local start_time_measure, stop_time_measure
35 = ltjb.start_time_measure, ltjb.stop_time_measure
38 local id_kern = node.id('kern')
39 local id_hlist = node.id('hlist')
40 local id_vlist = node.id('vlist')
41 local id_whatsit = node.id('whatsit')
42 local sid_save = node.subtype('pdf_save')
43 local sid_restore = node.subtype('pdf_restore')
44 local sid_matrix = node.subtype('pdf_setmatrix')
45 local sid_user = node.subtype('user_defined')
47 local tex_nest = tex.nest
48 local tex_getcount = tex.getcount
49 local tex_set_attr = tex.setattribute
50 local PROCESSED = luatexja.icflag_table.PROCESSED
51 local PROCESSED_BEGIN_FLAG = luatexja.icflag_table.PROCESSED_BEGIN_FLAG
52 local PACKED = luatexja.icflag_table.PACKED
53 local STCK = luatexja.userid_table.STCK
54 local DIR = luatexja.userid_table.DIR
55 local dir_tate = luatexja.dir_table.dir_tate
56 local dir_yoko = luatexja.dir_table.dir_yoko
57 local dir_dtou = luatexja.dir_table.dir_dtou
58 local dir_utod = luatexja.dir_table.dir_utod
59 local dir_math_mod = luatexja.dir_table.dir_math_mod
60 local dir_node_auto = luatexja.dir_table.dir_node_auto
61 local dir_node_manual = luatexja.dir_table.dir_node_manual
65 local function adjust_badness(hd)
66 if not node_next(hd) and getid(hd)==id_whatsit and getsubtype(hd)==sid_user
67 and getfield(hd, 'user_id')==DIR then
68 -- avoid double whatsit
69 luatexja.global_temp=tex.globaldefs; tex.globaldefs=0
70 luatexja.hbadness_temp=tex.hbadness; tex.hbadness=10000
71 luatexja.vbadness_temp=tex.vbadness; tex.vbadness=10000
73 luatexja.global_temp = nil
74 luatexja.hbadness_temp=nil
75 luatexja.vbadness_temp=nil
79 local get_dir_count, get_adjust_dir_count
81 local function get_dir_count_inner(h)
83 if h.id==id_whatsit and h.subtype==sid_user and h.user_id==DIR then
84 local ic = node.has_attribute(h, attr_icflag)
85 return (ic<PROCESSED_BEGIN_FLAG)
86 and (node.has_attribute(h,attr_dir)%dir_node_auto) or 0
94 function get_dir_count()
95 for i=tex_nest.ptr, 1, -1 do
96 local h = tex_nest[i].head.next
98 local t = get_dir_count_inner(h)
99 if t~=0 then return t end
102 return page_direction
104 function get_adjust_dir_count()
105 for i=tex_nest.ptr, 1, -1 do
106 local v = tex_nest[i]
107 local h, m = v.head.next, v.mode
108 if abs(m)== ltjs.vmode and h then
109 local t = get_dir_count_inner(h)
110 if t~=0 then return t end
113 return page_direction
115 luatexja.direction.get_dir_count = get_dir_count
116 luatexja.direction.get_adjust_dir_count = get_adjust_dir_count
120 -- \tate, \yoko,\dtou, \utod
122 local node_next = node.next
123 local node_set_attr = node.set_attribute
124 local function set_list_direction(v, name)
125 local lv, w = tex_nest.ptr, tex.lists.page_head
127 v,name = get_dir_count(), nil
128 if lv>=1 and abs(tex_nest[lv-1].mode) == ltjs.mmode and v == dir_tate then
132 v,name = get_adjust_dir_count(), nil
134 if tex.currentgrouptype==6 then
137 "You can't use `\\" .. name .. "' in an align",
138 "To change direction in an align, \n"
139 .. "you shold use \\hbox or \\vbox.")
141 local w = (lv==0) and tex.lists.page_head or tex_nest[lv].head.next
144 w.id==id_whatsit and w.subtype==sid_user and w.user_id==DIR then
145 node_set_attr(w, attr_dir, v)
146 if lv==0 then page_direction = v end
147 elseif lv==0 and not page_direction then
148 page_direction = v -- for first call of \yoko (in luatexja-core.sty)
152 "Use `\\" .. tostring(name) .. "' at top of list",
153 'Direction change command by LuaTeX-ja is available\n'
154 .. 'only when the current list is null.')
157 local w = node_new(id_whatsit, sid_user)
158 setfield(w, 'next', nil)
159 setfield(w, 'user_id', DIR)
160 setfield(w, 'type', 110)
161 set_attr(w, attr_dir, v)
163 if lv==0 then page_direction = v end
165 tex_set_attr('global', attr_icflag, 0)
167 tex_set_attr('global', attr_dir, 0)
169 luatexja.direction.set_list_direction = set_list_direction
172 -- ボックスに dir whatsit を追加
173 local function create_dir_whatsit(hd, gc, new_dir)
174 if getid(hd)==id_whatsit and
175 getsubtype(hd)==sid_user and getfield(hd, 'user_id')==DIR then
176 set_attr(hd, attr_icflag,
177 (has_attr(hd, attr_icflag) or 0)%PROCESSED_BEGIN_FLAG
178 + PROCESSED_BEGIN_FLAG)
179 tex_set_attr('global', attr_icflag, 0)
182 local w = node_new(id_whatsit, sid_user)
183 setfield(w, 'next', hd)
184 setfield(w, 'user_id', DIR)
185 setfield(w, 'type', 110)
186 set_attr(w, attr_dir, new_dir)
187 set_attr(w, attr_icflag, PROCESSED_BEGIN_FLAG)
188 set_attr(hd, attr_icflag,
189 (has_attr(hd, attr_icflag) or 0)%PROCESSED_BEGIN_FLAG
190 + PROCESSED_BEGIN_FLAG)
191 tex_set_attr('global', attr_dir, 0)
192 tex_set_attr('global', attr_icflag, 0)
197 -- hpack_filter, vpack_filter, post_line_break_filter
198 -- の結果を組方向を明示するため,先頭に dir_node を設置
200 local function create_dir_whatsit_hpack(h, gc)
201 local hd = to_direct(h)
202 if gc=='fin_row' or gc == 'preamble' then
204 set_attr(hd, attr_icflag, PROCESSED_BEGIN_FLAG)
205 tex_set_attr('global', attr_icflag, 0)
210 return to_node(create_dir_whatsit(hd, gc, ltjs.list_dir))
214 luatexbase.add_to_callback('hpack_filter',
215 create_dir_whatsit_hpack, 'ltj.create_dir_whatsit', 10000)
219 local function create_dir_whatsit_parbox(h, gc)
220 stop_time_measure('tex_linebreak')
221 -- start 側は ltj-debug.lua に
222 local new_dir, hd = ltjs.list_dir, to_direct(h)
223 for line in traverse_id(id_hlist, hd) do
224 local nh = getlist(line)
225 setfield(line, 'head', create_dir_whatsit(nh, gc, new_dir) )
226 set_attr(line, attr_dir, new_dir)
228 tex_set_attr('global', attr_dir, 0)
231 luatexbase.add_to_callback('post_linebreak_filter',
232 create_dir_whatsit_parbox, 'ltj.create_dir_whatsit', 10000)
235 local create_dir_whatsit_vbox
238 local id_glue, sid_parskip = node.id('glue'), 3
239 create_dir_whatsit_vbox = function (hd, gc)
240 ltjs.list_dir = get_dir_count()
241 -- remove dir whatsit
242 for x in traverse_id(id_whatsit, hd) do
243 if getsubtype(x)==sid_user and getfield(x, 'user_id')==DIR then
248 ltjs.list_dir =has_attr(hd,attr_dir)
249 local x = node_next(hd)
250 if getid(x)==id_glue and getsubtype(x)==sid_parskip then
251 node_remove(hd,x); node_free(x)
255 hd = node_remove(hd, wh[i]); node_free(wh[i]); wh[i] = nil
257 if gc=='fin_row' then -- gc == 'preamble' case is treated in dir_adjust_vpack
259 set_attr(hd, attr_icflag, PROCESSED_BEGIN_FLAG)
260 tex_set_attr('global', attr_icflag, 0)
264 local n =node_next(hd)
266 local w = create_dir_whatsit(hd, gc, ltjs.list_dir)
267 -- move dir whatsit after hd
268 setfield(hd, 'next', w); setfield(w, 'next', n)
271 hd = create_dir_whatsit(hd, gc, ltjs.list_dir)
278 -- dir_node に包む方法を書いたテーブル
281 local floor = math.floor
282 local get_h =function (w,h,d) return h end
283 local get_d =function (w,h,d) return d end
284 local get_h_d =function (w,h,d) return h+d end
285 local get_h_d_neg =function (w,h,d) return -h-d end
286 local get_h_neg =function (w,h,d) return -h end
287 local get_d_neg =function (w,h,d) return -d end
288 local get_w_half =function (w,h,d) return floor(0.5*w) end
289 local get_w_neg_half =function (w,h,d) return floor(-0.5*w) end
290 local get_w_half_rem =function (w,h,d) return w-floor(0.5*w) end
291 local get_w_neg_half_rem =function (w,h,d) return -w-floor(-0.5*w) end
292 local get_w_neg =function (w,h,d) return -w end
293 local get_w =function (w,h,d) return w end
294 local zero = function() return 0 end
296 [dir_yoko] = { -- yoko を
297 [dir_tate] = { -- tate 中で組む
300 depth = get_w_half_rem,
302 { 'whatsit', sid_save },
303 { 'rotate', '0 1 -1 0' },
304 { 'kern', get_w_neg_half },
306 { 'kern', get_w_neg_half_rem },
307 { 'whatsit', sid_restore },
310 { 'whatsit', sid_save },
311 { 'rotate', '0 1 -1 0' },
313 { 'box' , get_w_neg },
314 { 'kern', get_h_d_neg},
315 { 'whatsit', sid_restore },
318 [dir_dtou] = { -- dtou 中で組む
323 { 'whatsit', sid_save },
324 { 'rotate', '0 -1 1 0' },
325 { 'kern', get_w_neg },
326 { 'box', get_d_neg },
327 { 'whatsit', sid_restore },
330 { 'whatsit', sid_save },
331 { 'rotate', '0 -1 1 0' },
332 { 'kern', get_h_d_neg },
334 { 'whatsit', sid_restore },
338 [dir_tate] = { -- tate を
339 [dir_yoko] = { -- yoko 中で組む
344 { 'whatsit', sid_save },
345 { 'rotate', '0 -1 1 0' },
346 { 'kern', get_w_neg },
347 { 'box' , get_d_neg },
348 { 'whatsit', sid_restore },
351 { 'whatsit', sid_save },
352 { 'rotate', '0 -1 1 0' },
353 { 'kern', get_h_d_neg },
355 { 'whatsit', sid_restore },
358 [dir_dtou] = { -- dtou 中で組む
363 { 'whatsit', sid_save },
364 { 'rotate', '-1 0 0 -1' },
365 { 'kern', get_w_neg },
367 { 'whatsit', sid_restore },
370 { 'whatsit', sid_save },
371 { 'rotate', '-1 0 0 -1' },
372 { 'kern', get_h_d_neg },
373 { 'box', get_w_neg },
374 { 'whatsit', sid_restore },
378 [dir_dtou] = { -- dtou を
379 [dir_yoko] = { -- yoko 中で組む
384 { 'whatsit', sid_save },
385 { 'rotate', '0 1 -1 0' },
388 { 'kern', get_w_neg },
389 { 'whatsit', sid_restore },
393 { 'whatsit', sid_save },
394 { 'rotate', '0 1 -1 0' },
395 { 'box', get_w_neg },
396 { 'kern', get_h_d_neg },
397 { 'whatsit', sid_restore },
400 [dir_tate] = { -- tate 中で組む
405 { 'whatsit', sid_save },
406 { 'rotate', '-1 0 0 -1' },
407 { 'kern', get_w_neg },
409 { 'whatsit', sid_restore },
412 { 'whatsit', sid_save },
413 { 'rotate', ' -1 0 0 -1' },
414 { 'kern', get_h_d_neg },
415 { 'box', get_w_neg },
416 { 'whatsit', sid_restore },
423 -- 1st ret val: b の組方向
424 -- 2nd ret val はその DIR whatsit
425 local function get_box_dir(b, default)
426 start_time_measure('get_box_dir')
427 local dir = has_attr(b, attr_dir) or 0
428 local bh = getfield(b,'head')
429 -- b は insert node となりうるので getlist() は使えない
432 if bh and getid(bh)==id_whatsit
433 and getsubtype(bh)==sid_user and getfield(bh, 'user_id')==DIR then
435 dir = (dir==0) and has_attr(bh, attr_dir) or dir
439 stop_time_measure('get_box_dir')
440 return (dir==0 and default or dir), c
444 local getbox = tex.getbox
445 local function check_dir(reg_num)
446 start_time_measure('box_primitive_hook')
447 local list_dir = get_dir_count()%dir_math_mod
448 local b = getbox(tex_getcount('ltj@tempcnta'))
450 local box_dir = get_box_dir(to_direct(b), dir_yoko)
451 if box_dir%dir_math_mod ~= list_dir then
452 -- print('NEST', tex_nest.ptr, tex_getcount('ltj@tempcnta'))
453 -- luatexja.ext_show_node_list(
454 -- (tex_nest.ptr==0) and tex.lists.page_head or tex_nest[tex_nest.ptr].head,
455 -- 'LIST' .. tostring(list_dir) .. '> ', print)
456 -- luatexja.ext_show_node_list(b, 'BOX' .. tostring(box_dir) .. '> ', print)
459 "Incompatible direction list can't be unboxed",
460 'I refuse to unbox a box in differrent direction.')
463 if luatexja.global_temp and tex.globaldefs~=luatexja.global_temp then
464 tex.globaldefs = luatexja.global_temp
466 stop_time_measure('box_primitive_hook')
468 luatexja.direction.check_dir = check_dir
471 -- dir_node に包まれている「本来の中身」を取り出し,
473 local function unwrap_dir_node(b, head, box_dir)
474 -- b: dir_node, head: the head of list, box_dir:
475 -- return values are (new head), (next of b), (contents), (dir of contents)
476 local bh = getlist(b)
477 local bc = node_next(node_next(node_next(bh)))
482 nh = insert_before(head, b, bc)
483 nh, nb = node_remove(nh, b)
484 setfield(b, 'next', nil)
485 setfield(b, 'head', nil)
488 local shift_old, b_dir, wh = nil, get_box_dir(bc, 0)
490 Dnode.flush_list(getfield(wh, 'value'))
491 setfield(wh, 'value', nil)
494 local info = dir_node_aux[b_dir%dir_math_mod][box_dir%dir_math_mod][getid(bc)]
495 for _,v in ipairs(info) do
498 getfield(bc,'width'), getfield(bc, 'height'), getfield(bc, 'depth'))
502 setfield(bc, 'shift', getfield(bc, 'shift') - shift_old)
503 return nh, nb, bc, b_dir
506 -- is_manual: 寸法変更に伴うものか?
507 local function create_dir_node(b, b_dir, new_dir, is_manual)
508 local info = dir_node_aux[b_dir%dir_math_mod][new_dir%dir_math_mod]
509 local w = getfield(b, 'width')
510 local h = getfield(b, 'height')
511 local d = getfield(b, 'depth')
512 local db = node_new(getid(b)) -- dir_node
513 set_attr(db, attr_dir,
514 new_dir + (is_manual and dir_node_manual or dir_node_auto))
515 set_attr(db, attr_icflag, PROCESSED)
516 set_attr(b, attr_icflag, PROCESSED)
517 tex_set_attr('global', attr_dir, 0)
518 tex_set_attr('global', attr_icflag, 0)
519 setfield(db, 'dir', getfield(b, 'dir'))
520 setfield(db, 'shift', 0)
521 setfield(db, 'width', info.width(w,h,d))
522 setfield(db, 'height', info.height(w,h,d))
523 setfield(db, 'depth', info.depth(w,h,d))
528 local make_dir_whatsit, process_dir_node
530 make_dir_whatsit = function (head, b, new_dir, origin)
531 new_dir = new_dir%dir_math_mod
532 -- head: list head, b: box
533 -- origin: コール元 (for debug)
534 -- return value: (new head), (next of b), (new b), (is_b_dir_node)
535 -- (new b): b か dir_node に被せられた b
536 local bh = getlist(b)
537 local box_dir, dn = get_box_dir(b, ltjs.list_dir)
538 -- 既に b の中身にあるwhatsit
540 if box_dir%dir_math_mod==new_dir then
541 if box_dir>=dir_node_auto then
542 -- dir_node としてカプセル化されている
543 local bc = node_next(node_next(node_next(bh)))
544 local _, dnc = get_box_dir(b, 0)
545 if dnc then -- free all other dir_node
546 Dnode.flush_list(getfield(dnc, 'value'))
547 setfield(dnc, 'value', nil)
549 set_attr(b, attr_dir, box_dir%dir_math_mod + dir_node_auto)
550 return head, node_next(b), b, true
552 -- 組方向が一緒 (up to math dir) のボックスなので,何もしなくて良い
553 return head, node_next(b), b, false
557 local nh, nb, ret, flag
558 if box_dir>= dir_node_auto then -- unwrap
560 head, nb, b, b_dir = unwrap_dir_node(b, head, box_dir)
562 if b_dir%dir_math_mod==new_dir then
563 -- dir_node の中身が周囲の組方向とあっている
564 return head, nb, b, false
565 else box_dir = b_dir end
567 box_dir = box_dir%dir_math_mod
569 local dnh = getfield(dn, 'value')
570 for x in traverse(dnh) do
571 if has_attr(x, attr_dir)%dir_math_mod == new_dir then
572 setfield(dn, 'value', to_node(node_remove(dnh, x)))
576 Dnode.flush_list(getfield(dn, 'value'))
577 db = db or create_dir_node(b, box_dir, new_dir, false)
578 local w = getfield(b, 'width')
579 local h = getfield(b, 'height')
580 local d = getfield(b, 'depth')
581 nh, nb = insert_before(head, b, db), nil
582 nh, nb = node_remove(nh, b)
583 local db_head, db_tail = nil
584 for _,v in ipairs(dir_node_aux[box_dir][new_dir][getid(b)]) do
585 local cmd, arg, nn = v[1], v[2]
587 nn = node_new(id_kern)
588 setfield(nn, 'kern', arg(w, h, d))
589 elseif cmd=='whatsit' then
590 nn = node_new(id_whatsit, arg)
591 elseif cmd=='rotate' then
592 nn = node_new(id_whatsit, sid_matrix)
593 setfield(nn, 'data', arg)
594 elseif cmd=='box' then
595 nn = b; setfield(b, 'next', nil)
596 setfield(nn, 'shift', getfield(nn, 'shift') + arg(w,h,d))
599 insert_after(db_head, db_tail, nn)
602 db_head, db_tail = nn, nn
604 setfield(db, 'head', db_head)
607 return nh, nb, ret, flag
610 process_dir_node = function (hd, gc)
611 local x, new_dir = hd, ltjs.list_dir or dir_yoko
614 if (xid==id_hlist and has_attr(x, attr_icflag)%PROCESSED_BEGIN_FLAG~=PACKED)
615 or xid==id_vlist then
616 hd, x = make_dir_whatsit(hd, x, new_dir, 'process_dir_node:' .. gc)
625 local node_prev = (Dnode~=node) and Dnode.getprev or node.prev
626 local function lastbox_hook()
627 start_time_measure('box_primitive_hook')
628 local bn = tex_nest[tex_nest.ptr].tail
630 local b, head = to_direct(bn), to_direct(tex_nest[tex_nest.ptr].head)
632 if bid==id_hlist or bid==id_vlist then
633 local box_dir = get_box_dir(b, 0)
634 if box_dir>= dir_node_auto then -- unwrap dir_node
635 local p = node_prev(b)
636 local dummy1, dummy2, nb = unwrap_dir_node(b, nil, box_dir)
637 setfield(p, 'next', nb); tex_nest[tex_nest.ptr].tail = to_node(nb)
638 setfield(b, 'next', nil); setfield(b, 'head', nil)
641 local _, wh = get_box_dir(b, 0) -- clean dir_node attached to the box
643 Dnode.flush_list(getfield('value', wh))
644 setfield(wh, 'value', nil)
648 stop_time_measure('box_primitive_hook')
651 luatexja.direction.make_dir_whatsit = make_dir_whatsit
652 luatexja.direction.lastbox_hook = lastbox_hook
655 -- \wd, \ht, \dp の代わり
657 local getbox, setdimen = tex.getbox, tex.setdimen
658 local function get_box_dim_common(key, s, l_dir)
660 local s_dir, wh = get_box_dir(s, dir_yoko)
661 s_dir = s_dir%dir_math_mod
662 if s_dir ~= l_dir then
663 local not_found = true
664 for x in traverse(getfield(wh, 'value')) do
665 if l_dir == has_attr(x, attr_dir)%dir_node_auto then
666 setdimen('ltj@tempdima', getfield(x, key))
667 not_found = false; break
671 local w = getfield(s, 'width')
672 local h = getfield(s, 'height')
673 local d = getfield(s, 'depth')
674 setdimen('ltj@tempdima',
675 dir_node_aux[s_dir][l_dir][key](w,h,d))
678 setdimen('ltj@tempdima', getfield(s, key))
681 local function get_box_dim(key, n)
682 local gt = tex.globaldefs; tex.globaldefs = 0
685 local l_dir = (get_dir_count())%dir_math_mod
687 local b_dir = get_box_dir(s,dir_yoko)
688 if b_dir<dir_node_auto then
689 get_box_dim_common(key, s, l_dir)
690 elseif b_dir%dir_math_mod==l_dir then
691 setdimen('ltj@tempdima', getfield(s, key))
693 get_box_dim_common(key,
694 node_next(node_next(node_next(getlist(s)))), l_dir)
697 setdimen('ltj@tempdima', 0)
701 luatexja.direction.get_box_dim = get_box_dim
703 -- return value: (changed dimen of box itself?)
704 local function set_box_dim_common(key, s, l_dir)
705 local s_dir, wh = get_box_dir(s, dir_yoko)
706 s_dir = s_dir%dir_math_mod
707 if s_dir ~= l_dir then
709 wh = create_dir_whatsit(getlist(s), 'set_box_dim', s_dir)
710 setfield(s, 'head', wh)
713 local dnh = getfield(wh, 'value')
714 for x in traverse(dnh) do
715 if has_attr(x, attr_dir)%dir_node_auto==l_dir then
720 db = create_dir_node(s, s_dir, l_dir, true)
721 setfield(db, 'next', dnh)
722 setfield(wh, 'value',to_node(db))
724 setfield(db, key, tex.getdimen('ltj@tempdima'))
727 setfield(s, key, tex.getdimen('ltj@tempdima'))
729 -- change dimension of dir_nodes which are created "automatically"
731 = getfield(s,'width'), getfield(s, 'height'), getfield(s, 'depth')
732 for x in traverse(getfield(wh, 'value')) do
733 local x_dir = has_attr(x, attr_dir)
734 if x_dir<dir_node_manual then
735 local info = dir_node_aux[s_dir][x_dir%dir_node_auto]
736 setfield(x, 'width', info.width(bw,bh,bd))
737 setfield(x, 'height', info.height(bw,bh,bd))
738 setfield(x, 'depth', info.depth(bw,bh,bd))
745 local function set_box_dim(key)
746 local n = tex_getcount('ltj@tempcnta')
749 local l_dir = (get_dir_count())%dir_math_mod
751 local b_dir = get_box_dir(s,dir_yoko)
752 if b_dir<dir_node_auto then
753 set_box_dim_common(key, s, l_dir)
754 elseif b_dir%dir_math_mod == l_dir then
756 setfield(s, key, tex.getdimen('ltj@tempdima'))
757 if b_dir<dir_node_manual then
758 set_attr(s, attr_dir, b_dir%dir_node_auto + dir_node_manual)
761 local sid, sl = getid(s), getlist(s)
762 local b = node_next(node_next(node_next(sl))) -- 本来の box
763 local info = dir_node_aux[get_box_dir(b,dir_yoko)%dir_math_mod][b_dir%dir_node_auto]
765 for _,v in ipairs(info[sid]) do
768 getfield(b,'width'), getfield(b, 'height'), getfield(b, 'depth'))
772 if set_box_dim_common(key, b, l_dir) then
774 = getfield(b,'width'), getfield(b, 'height'), getfield(b, 'depth')
775 -- re-calculate shift
776 for i,v in ipairs(info[sid]) do
777 if getid(sl)==id_kern then
778 setfield(sl, 'kern', v[2](bw,bh,bd) )
779 elseif getid(sl)==sid then
780 local d = getfield(sl, 'shift')
781 setfield(sl, 'shift',
782 getfield(sl, 'shift') - shift_old + v[2](bw,bh,bd) )
786 -- re-calculate dimension of s, if s is created "automatically"
787 if b_dir<dir_node_manual then
788 setfield(s, 'width', info.width(bw,bh,bd))
789 setfield(s, 'height', info.height(bw,bh,bd))
790 setfield(s, 'depth', info.depth(bw,bh,bd))
796 luatexja.direction.set_box_dim = set_box_dim
800 local getbox = tex.getbox
801 local function get_register_dir(n)
805 local b_dir = get_box_dir(s, dir_yoko)
806 if b_dir<dir_node_auto then
809 local b_dir = get_box_dir(
810 node_next(node_next(node_next(getlist(s)))), dir_yoko)
817 luatexja.direction.get_register_dir = get_register_dir
821 local getbox, setbox, copy_list = tex.getbox, tex.setbox, Dnode.copy_list
823 function luatexja.direction.raise_box()
824 start_time_measure('box_primitive_hook')
825 local list_dir = get_dir_count()
826 local s = getbox('ltj@afbox')
828 local sd = to_direct(s)
829 local box_dir = get_box_dir(sd, dir_yoko)
830 if box_dir%dir_math_mod ~= list_dir then
834 copy_list(make_dir_whatsit(sd, sd, list_dir, 'box_move'))
835 -- without copy_list, we get a segfault
840 stop_time_measure('box_primitive_hook')
845 local font_vert_table = {} -- key: fontnumber
847 local font_vert_basename = {} -- key: basename
848 local function add_feature_table(tname, src, dest)
849 for i,v in pairs(src) do
850 if type(v.slookups)=='table' then
851 local s = v.slookups[tname]
852 if s and not dest[i] then
859 local function prepare_vert_data(n, id)
860 -- test if already loaded
861 if type(id)=='number' then -- sometimes id is an integer
862 font_vert_table[n] = font_vert_table[id]; return
863 elseif (not id) or font_vert_table[n] then return
865 local fname = id.filename
866 local bname = file.basename(fname)
868 font_vert_table[n] = {}; return
869 elseif font_vert_basename[bname] then
870 font_vert_table[n] = font_vert_basename[bname]; return
873 local a = id.resources.sequences
875 local s = id.shared.rawdata.descriptions
876 for i,v in pairs(a) do
877 if v.features.vert or v.features.vrt2 then
878 add_feature_table(v.subtables[1], s, vtable)
882 font_vert_basename[bname] = vtable
883 font_vert_table[n] = vtable
886 function luatexja.direction.get_vert_glyph(n, chr)
887 local fn = font_vert_table[n]
888 return fn and fn[chr] or chr
890 luatexbase.add_to_callback('luatexja.define_font',
891 function (res, name, size, id)
892 prepare_vert_data(id, res)
894 'prepare_vert_data', 1)
896 local function a (n, dat) font_vert_table[n] = dat end
897 luatexja.rmlgbm.vert_addfunc = a
901 -- PACKED の hbox から文字を取り出す
902 -- luatexja.jfmglue.check_box などで使用
904 local function glyph_from_packed(h)
906 return (getid(b)==id_kern)
907 and node_next(node_next(node_next(node_next(b)))) or b
909 luatexja.direction.glyph_from_packed = glyph_from_packed
912 -- adjust and insertion
913 local id_adjust = node.id('adjust')
914 function luatexja.direction.check_adjust_direction()
915 start_time_measure('box_primitive_hook')
916 local list_dir = get_adjust_dir_count()
917 local a = tex_nest[tex_nest.ptr].tail
918 local ad = to_direct(a)
919 if a and getid(ad)==id_adjust then
920 local adj_dir = get_box_dir(ad)
921 if list_dir~=adj_dir then
924 'Direction Incompatible',
925 "\\vadjust's argument and outer vlist must have same direction.")
929 stop_time_measure('box_primitive_hook')
934 local split_dir_whatsit
935 local function dir_adjust_vpack(h, gc)
936 start_time_measure('direction_vpack')
937 local hd = to_direct(h)
938 if gc=='split_keep' then
939 -- supply dir_whatsit
940 hd = create_dir_whatsit_vbox(hd, gc)
941 split_dir_whatsit = hd
942 elseif gc=='split_off' then
945 if bh and getid(bh)==id_whatsit
946 and getsubtype(bh)==sid_user and getfield(bh, 'user_id')==DIR then
947 ltjs.list_dir = has_attr(bh, attr_dir); break
951 if split_dir_whatsit then
952 -- adjust direction of 'split_keep'
953 set_attr(split_dir_whatsit, attr_dir, ltjs.list_dir)
955 split_dir_whatsit=nil
956 elseif gc=='preamble' then
957 split_dir_whatsit=nil
960 hd = process_dir_node(create_dir_whatsit_vbox(hd, gc), gc)
961 split_dir_whatsit=nil
963 stop_time_measure('direction_vpack')
966 luatexbase.add_to_callback('vpack_filter',
968 'ltj.direction', 10000)
972 -- supply direction whatsit to the main vertical list "of the next page"
973 local function dir_adjust_pre_output(h, gc)
974 return to_node(create_dir_whatsit_vbox(to_direct(h), gc))
976 luatexbase.add_to_callback('pre_output_filter',
977 dir_adjust_pre_output,
978 'ltj.direction', 10000)
980 function luatexja.direction.remove_end_whatsit()
981 local h=tex.lists.page_head
983 h.id==id_whatsit and h.subtype==sid_user and
984 h.user_id == DIR then
985 tex.lists.page_head = nil
993 local function dir_adjust_buildpage(info)
995 local head = to_direct(tex.lists.contrib_head)
999 = make_dir_whatsit(head,
1003 tex.lists.contrib_head = to_node(head)
1007 luatexbase.add_to_callback('buildpage_filter',
1008 dir_adjust_buildpage,
1009 'ltj.direction', 10000)