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 Dnode = node.direct or node
14 local nullfunc = function (n) return n end
15 local to_node = (Dnode ~= node) and Dnode.tonode or nullfunc
16 local to_direct = (Dnode ~= node) and Dnode.todirect or nullfunc
17 local has_attr = Dnode.has_attribute
18 local set_attr = Dnode.set_attribute
19 local insert_before = Dnode.insert_before
20 local insert_after = Dnode.insert_after
21 local getid = (Dnode ~= node) and Dnode.getid or function(n) return n.id end
22 local getsubtype = (Dnode ~= node) and Dnode.getsubtype or function(n) return n.subtype end
23 local getlist = (Dnode ~= node) and Dnode.getlist or function(n) return n.head end
24 local setfield = (Dnode ~= node) and Dnode.setfield or function(n, i, c) n[i] = c end
25 local getfield = (Dnode ~= node) and Dnode.getfield or function(n, i) return n[i] end
26 local node_new = Dnode.new
27 local node_tail = Dnode.tail
28 local node_free = Dnode.free
29 local node_remove = Dnode.remove
30 local node_next = (Dnode ~= node) and Dnode.getnext or node.next
31 local traverse = Dnode.traverse
32 local traverse_id = Dnode.traverse_id
33 local start_time_measure, stop_time_measure
34 = ltjb.start_time_measure, ltjb.stop_time_measure
36 local id_kern = node.id('kern')
37 local id_hlist = node.id('hlist')
38 local id_vlist = node.id('vlist')
39 local id_whatsit = node.id('whatsit')
40 local sid_save = node.subtype('pdf_save')
41 local sid_restore = node.subtype('pdf_restore')
42 local sid_matrix = node.subtype('pdf_setmatrix')
43 local sid_user = node.subtype('user_defined')
45 local tex_getcount = tex.getcount
46 local tex_set_attr = tex.setattribute
47 local PROCESSED = luatexja.icflag_table.PROCESSED
48 local PROCESSED_BEGIN_FLAG = luatexja.icflag_table.PROCESSED_BEGIN_FLAG
49 local PACKED = luatexja.icflag_table.PACKED
50 local STCK = luatexja.userid_table.STCK
51 local DIR = luatexja.userid_table.DIR
52 local dir_tate = luatexja.dir_table.dir_tate
53 local dir_yoko = luatexja.dir_table.dir_yoko
54 local dir_dtou = luatexja.dir_table.dir_dtou
55 local dir_node_auto = luatexja.dir_table.dir_node_auto
56 local dir_node_manual = luatexja.dir_table.dir_node_manual
63 function get_dir_count()
64 return tex_getcount('ltj@dir@count')
66 luatexja.direction.get_dir_count = get_dir_count
68 local node_next = node.next
69 local node_set_attr = node.set_attribute
70 local function set_list_direction(v, name)
71 local lv, w = tex.nest[tex.nest.ptr], tex.lists.page_head
72 if lv.mode == 1 and w then
73 if w.id==id_whatsit and w.subtype==sid_user
74 and w.user_id==DIR then
75 node_set_attr(w, attr_dir, v)
78 local w = node_next(to_direct(lv.head))
80 if getid(w)==id_whatsit and getsubtype(w)==sid_user
81 and getfield(w, 'user_id')==DIR then
82 set_attr(w, attr_dir, v)
86 "Use `\\" .. name .. "' at top of list",
87 'Direction change command by LuaTeX-ja is available\n'
88 .. 'only when the current list is null.')
91 local w = node_new(id_whatsit, sid_user)
92 setfield(w, 'next', hd)
93 setfield(w, 'user_id', DIR)
94 setfield(w, 'type', 110)
95 set_attr(w, attr_dir, v)
98 tex_set_attr('global', attr_icflag, 0)
100 tex_set_attr('global', attr_dir, 0)
102 luatexja.direction.set_list_direction = set_list_direction
105 -- ボックスに dir whatsit を追加
106 local function create_dir_whatsit(hd, gc, new_dir)
107 local w = node_new(id_whatsit, sid_user)
108 setfield(w, 'next', hd)
109 setfield(w, 'user_id', DIR)
110 setfield(w, 'type', 110)
111 set_attr(w, attr_dir, new_dir)
112 tex_set_attr('global', attr_dir, 0)
113 set_attr(w, attr_icflag, PROCESSED_BEGIN_FLAG)
114 set_attr(hd, attr_icflag, (has_attr(hd, attr_icflag) or 0) + PROCESSED_BEGIN_FLAG)
115 tex_set_attr('global', attr_icflag, 0)
119 -- hpack_filter, vpack_filter, post_line_break_filter
120 -- の結果を組方向を明示するため,先頭に dir_node を設置
122 local function create_dir_whatsit_hpack(h, gc)
123 local hd = to_direct(h)
124 if gc=='fin_row' or gc == 'preamble' then
126 set_attr(hd, attr_icflag, PROCESSED_BEGIN_FLAG)
127 tex_set_attr('global', attr_icflag, 0)
131 return to_node(create_dir_whatsit(hd, gc, ltjs.list_dir))
135 luatexbase.add_to_callback('hpack_filter',
136 create_dir_whatsit_hpack, 'ltj.create_dir_whatsit', 10000)
139 local id_glue, sid_parskip = node.id('glue'), 3
140 local function create_dir_whatsit_vbox(h, gc)
141 local hd = to_direct(h)
142 ltjs.list_dir = get_dir_count()
143 -- remove dir whatsit
144 for x in traverse_id(id_whatsit, hd) do
145 if getsubtype(x)==sid_user and getfield(x, 'user_id')==DIR then
150 ltjs.list_dir = has_attr(hd,attr_dir)
151 local x = node_next(hd)
152 if getid(x)==id_glue and getsubtype(x)==sid_parskip then
153 node_remove(hd,x); node_free(x)
156 for i=1,#wh do hd = node_remove(hd, wh[i]); node_free(wh[i]); wh[i] = nil end
157 if gc=='fin_row' or gc == 'preamble' then
159 set_attr(hd, attr_icflag, PROCESSED_BEGIN_FLAG)
160 tex_set_attr('global', attr_icflag, 0)
163 elseif gc=='vtop' then
164 local w = create_dir_whatsit(hd, gc, ltjs.list_dir)
165 -- move dir whatsit after hd
166 local n = getfield(hd, 'next')
167 setfield(hd, 'next', w); setfield(w, 'next', n)
170 return to_node(create_dir_whatsit(hd, gc, ltjs.list_dir))
173 luatexbase.add_to_callback('vpack_filter',
174 create_dir_whatsit_vbox, 'ltj.create_dir_whatsit', 1)
176 local function create_dir_whatsit_parbox(h, gc)
177 stop_time_measure('tex_linebreak')
178 -- start 側は ltj-debug.lua に
179 local new_dir, hd = ltjs.list_dir, to_direct(h)
180 for line in traverse_id(id_hlist, hd) do
181 set_attr(line, attr_dir, new_dir)
183 tex_set_attr('global', attr_dir, 0)
184 return to_node(create_dir_whatsit(hd, gc, new_dir))
186 luatexbase.add_to_callback('post_linebreak_filter',
187 create_dir_whatsit_parbox, 'ltj.create_dir_whatsit', 10000)
191 -- dir_node に包む方法を書いたテーブル
194 local get_h =function (w,h,d) return h end
195 local get_d =function (w,h,d) return d end
196 local get_h_d =function (w,h,d) return h+d end
197 local get_h_d_neg =function (w,h,d) return -h-d end
198 local get_h_neg =function (w,h,d) return -h end
199 local get_d_neg =function (w,h,d) return -d end
200 local get_w_half =function (w,h,d) return 0.5*w end
201 local get_w_neg_half =function (w,h,d) return -0.5*w end
202 local get_w_neg =function (w,h,d) return -w end
203 local get_w =function (w,h,d) return w end
204 local zero = function() return 0 end
206 [dir_yoko] = { -- yoko を
207 [dir_tate] = { -- tate 中で組む
212 { 'whatsit', sid_save },
213 { 'rotate', '0 1 -1 0' },
214 { 'kern', get_w_neg_half },
216 { 'kern', get_w_neg_half },
217 { 'whatsit', sid_restore },
220 { 'whatsit', sid_save },
221 { 'rotate', '0 1 -1 0' },
223 { 'box' , get_w_neg },
224 { 'kern', get_h_d_neg},
225 { 'whatsit', sid_restore },
228 [dir_dtou] = { -- dtou 中で組む
233 { 'whatsit', sid_save },
234 { 'rotate', '0 -1 1 0' },
235 { 'kern', get_w_neg },
236 { 'box', get_d_neg },
237 { 'whatsit', sid_restore },
240 { 'whatsit', sid_save },
241 { 'rotate', '0 -1 1 0' },
242 { 'kern', get_h_d_neg },
244 { 'whatsit', sid_restore },
248 [dir_tate] = { -- tate を
249 [dir_yoko] = { -- yoko 中で組む
254 { 'whatsit', sid_save },
255 { 'rotate', '0 -1 1 0' },
256 { 'kern', get_w_neg },
257 { 'box' , get_d_neg },
258 { 'whatsit', sid_restore },
261 { 'whatsit', sid_save },
262 { 'rotate', '0 -1 1 0' },
263 { 'kern', get_h_d_neg },
265 { 'whatsit', sid_restore },
268 [dir_dtou] = { -- dtou 中で組む
273 { 'whatsit', sid_save },
274 { 'rotate', '-1 0 0 -1' },
275 { 'kern', get_w_neg },
277 { 'whatsit', sid_restore },
280 { 'whatsit', sid_save },
281 { 'rotate', '-1 0 0 -1' },
282 { 'kern', get_h_d_neg },
283 { 'box', get_w_neg },
284 { 'whatsit', sid_restore },
288 [dir_dtou] = { -- dtou を
289 [dir_yoko] = { -- yoko 中で組む
294 { 'whatsit', sid_save },
295 { 'rotate', '0 1 -1 0' },
298 { 'kern', get_w_neg },
299 { 'whatsit', sid_restore },
303 { 'whatsit', sid_save },
304 { 'rotate', '0 1 -1 0' },
305 { 'box', get_w_neg },
306 { 'kern', get_h_d_neg },
307 { 'whatsit', sid_restore },
310 [dir_tate] = { -- tate 中で組む
315 { 'whatsit', sid_save },
316 { 'rotate', '-1 0 0 -1' },
317 { 'kern', get_w_neg },
319 { 'whatsit', sid_restore },
322 { 'whatsit', sid_save },
323 { 'rotate', ' -1 0 0 -1' },
324 { 'kern', get_h_d_neg },
325 { 'box', get_w_neg },
326 { 'whatsit', sid_restore },
333 -- b に DIR whatsit があればその内容を attr_dir にうつす (1st ret val)
334 -- 2nd ret val はその DIR whatsit
335 local function get_box_dir(b, default)
336 local dir = has_attr(b, attr_dir) or 0
337 local bh = getlist(b)
340 if bh and getid(bh)==id_whatsit
341 and getsubtype(bh)==sid_user and getfield(bh, 'user_id')==DIR then
344 dir = has_attr(bh, attr_dir)
345 set_attr(b, attr_dir, dir)
346 tex_set_attr('global', attr_dir, 0)
351 return (dir==0 and default or dir), c
354 -- dir_node に包まれている「本来の中身」を取り出し,
356 local function unwrap_dir_node(b, head, box_dir)
357 -- b: dir_node, head: the head of list, box_dir:
358 -- return values are (new head), (next of b), (contents), (dir of contents)
359 local bh = getlist(b)
360 local bc = node_next(node_next(node_next(bh)))
365 nh = insert_before(head, b, bc)
366 nh, nb = node_remove(nh, b)
367 setfield(b, 'next', nil)
368 setfield(b, 'head', nil)
371 local shift_old, b_dir, wh = nil, get_box_dir(bc, 0)
373 Dnode.flush_list(getfield(wh, 'value'))
374 setfield(wh, 'value', nil)
377 local info = dir_node_aux[b_dir][box_dir%dir_node_auto][getid(bc)]
378 for _,v in ipairs(info) do
381 getfield(bc,'width'), getfield(bc, 'height'), getfield(bc, 'depth'))
385 setfield(bc, 'shift', getfield(bc, 'shift') - shift_old)
386 return nh, nb, bc, b_dir
389 -- is_manual: 寸法変更に伴うものか?
390 local function create_dir_node(b, b_dir, new_dir, is_manual)
391 local info = dir_node_aux[b_dir][new_dir]
392 local w = getfield(b, 'width')
393 local h = getfield(b, 'height')
394 local d = getfield(b, 'depth')
395 local db = node_new(getid(b))
396 set_attr(db, attr_dir,
397 new_dir + (is_manual and dir_node_manual or dir_node_auto))
398 tex_set_attr('global', attr_dir, 0)
399 set_attr(db, attr_icflag, PROCESSED)
400 set_attr(b, attr_icflag, PROCESSED)
401 setfield(db, 'dir', getfield(b, 'dir'))
402 setfield(db, 'shift', 0)
403 setfield(db, 'width', info.width(w,h,d))
404 setfield(db, 'height', info.height(w,h,d))
405 setfield(db, 'depth', info.depth(w,h,d))
410 local make_dir_whatsit
412 make_dir_whatsit = function (head, b, new_dir, origin)
413 -- head: list head, b: box
414 -- origin: コール元 (for debug)
415 -- return value: (new head), (next of b), (new b), (is_b_dir_node)
416 -- (new b): b か dir_node に被せられた b
417 local bh = getlist(b)
418 local box_dir, dn = get_box_dir(b, ltjs.list_dir)
419 -- 既に b の中身にあるwhatsit
420 if box_dir==new_dir then
421 return head, node_next(b), b, false
422 elseif box_dir%dir_node_auto == new_dir then
423 local bc = node_next(node_next(node_next(bh)))
424 local _, dnc = get_box_dir(b, 0)
425 if dnc then -- free all other dir_node
426 Dnode.flush_list(getfield(dnc, 'value'))
427 setfield(dnc, 'value', nil)
429 set_attr(b, attr_dir, box_dir%dir_node_auto + dir_node_auto)
430 return head, node_next(b), b, true
432 local nh, nb, ret, flag
433 if box_dir>= dir_node_auto then -- unwrap
435 head, nb, b, b_dir = unwrap_dir_node(b, head, box_dir)
437 if b_dir==new_dir then -- no need to create new dir_node
438 return head, nb, b, false
439 else box_dir = b_dir end
442 local dnh = getfield(dn, 'value')
443 for x in traverse(dnh) do
444 if has_attr(x, attr_dir)%dir_node_auto == new_dir then
445 setfield(dn, 'value', to_node(node_remove(dnh, x)))
449 Dnode.flush_list(dnh)
450 db = db or create_dir_node(b, box_dir, new_dir, false)
451 local w = getfield(b, 'width')
452 local h = getfield(b, 'height')
453 local d = getfield(b, 'depth')
454 nh, nb = insert_before(head, b, db), nil
455 nh, nb = node_remove(nh, b)
456 local db_head, db_tail = nil
457 for _,v in ipairs(dir_node_aux[box_dir][new_dir][getid(b)]) do
458 local cmd, arg, nn = v[1], v[2]
460 nn = node_new(id_kern)
461 setfield(nn, 'kern', arg(w, h, d))
462 elseif cmd=='whatsit' then
463 nn = node_new(id_whatsit, arg)
464 elseif cmd=='rotate' then
465 nn = node_new(id_whatsit, sid_matrix)
466 setfield(nn, 'data', arg)
467 elseif cmd=='box' then
468 nn = b; setfield(b, 'next', nil)
469 setfield(nn, 'shift', getfield(nn, 'shift') + arg(w,h,d))
472 insert_after(db_head, db_tail, nn)
475 db_head, db_tail = nn, nn
478 setfield(db, 'head', db_head)
481 return nh, nb, ret, flag
484 local function process_dir_node(head, gc)
485 start_time_measure('direction_vpack')
486 local h = to_direct(head)
487 local x, new_dir = h, ltjs.list_dir or dir_yoko
490 if (xid==id_hlist and has_attr(x, attr_icflag)%PROCESSED_BEGIN_FLAG~=PACKED)
491 or xid==id_vlist then
492 h, x = make_dir_whatsit(h, x, new_dir, 'process_dir_node:' .. gc)
497 stop_time_measure('direction_vpack')
500 luatexja.direction.make_dir_whatsit = make_dir_whatsit
501 luatexbase.add_to_callback('vpack_filter',
502 process_dir_node, 'ltj.dir_whatsit', 10001)
505 -- \wd, \ht, \dp の代わり
507 local getbox, setdimen = tex.getbox, tex.setdimen
508 local function get_box_dim_common(key, s, l_dir)
509 local s_dir, wh = get_box_dir(s, dir_yoko)
510 if s_dir ~= l_dir then
511 local not_found = true
512 for x in traverse(getfield(wh, 'value')) do
513 if l_dir == has_attr(x, attr_dir)%dir_node_auto then
514 setdimen('ltj@tempdima', getfield(x, key))
515 not_found = false; break
519 local w = getfield(s, 'width')
520 local h = getfield(s, 'height')
521 local d = getfield(s, 'depth')
522 setdimen('ltj@tempdima',
523 dir_node_aux[s_dir][l_dir][key](w,h,d))
526 setdimen('ltj@tempdima', getfield(s, key))
529 local function get_box_dim(key, n)
530 local gt = tex.globaldefs; tex.globaldefs = 0
533 local l_dir = get_dir_count()
535 local b_dir = has_attr(s, attr_dir) or 0
536 if b_dir<dir_node_auto then
537 get_box_dim_common(key, s, l_dir)
538 elseif b_dir%dir_node_auto==l_dir then
539 setdimen('ltj@tempdima', getfield(s, key))
541 get_box_dim_common(key,
542 node_next(node_next(node_next(getlist(s)))), l_dir)
545 setdimen('ltj@tempdima', 0)
549 luatexja.direction.get_box_dim = get_box_dim
551 -- return value: (changed dimen of box itself?)
552 local function set_box_dim_common(key, s, l_dir)
553 local s_dir, wh = get_box_dir(s, dir_yoko)
554 if s_dir ~= l_dir then
556 wh = create_dir_whatsit(getlist(s), 'set_box_dim', s_dir)
557 setfield(s, 'head', wh)
560 local dnh = getfield(wh, 'value')
561 for x in traverse(dnh) do
562 if has_attr(x, attr_dir)%dir_node_auto==l_dir then
567 db = create_dir_node(s, s_dir, l_dir, true)
568 setfield(db, 'next', dnh)
569 setfield(wh, 'value',to_node(db))
571 setfield(db, key, tex.getdimen('ltj@tempdima'))
574 setfield(s, key, tex.getdimen('ltj@tempdima'))
576 -- change dimension of dir_nodes which are created "automatically"
578 = getfield(s,'width'), getfield(s, 'height'), getfield(s, 'depth')
579 for x in traverse(getfield(wh, 'value')) do
580 local x_dir = has_attr(x, attr_dir)
581 if x_dir<dir_node_manual then
582 local info = dir_node_aux[s_dir][x_dir%dir_node_auto]
583 setfield(x, 'width', info.width(bw,bh,bd))
584 setfield(x, 'height', info.height(bw,bh,bd))
585 setfield(x, 'depth', info.depth(bw,bh,bd))
592 local function set_box_dim(key)
593 local n = tex_getcount('ltj@tempcnta')
596 local l_dir = get_dir_count()
598 local b_dir = has_attr(s, attr_dir) or 0
599 if b_dir<dir_node_auto then
600 set_box_dim_common(key, s, l_dir)
601 elseif b_dir%dir_node_auto == l_dir then
602 setfield(s, key, tex.getdimen('ltj@tempdima'))
603 if b_dir<dir_node_manual then
604 set_attr(s, attr_dir, b_dir%dir_node_auto + dir_node_manual)
607 local sid, sl = getid(s), getlist(s)
608 local b = node_next(node_next(node_next(sl)))
609 local info = dir_node_aux[get_box_dir(b)][b_dir%dir_node_auto]
611 for _,v in ipairs(info[sid]) do
614 getfield(b,'width'), getfield(b, 'height'), getfield(b, 'depth'))
618 if set_box_dim_common(key, b, l_dir) then
620 = getfield(b,'width'), getfield(b, 'height'), getfield(b, 'depth')
621 -- re-calculate shift
622 for i,v in ipairs(info[sid]) do
623 if getid(sl)==id_kern then
624 setfield(sl, 'kern', v[2](bw,bh,bd) )
625 elseif getid(sl)==sid then
626 local d = getfield(sl, 'shift')
627 setfield(sl, 'shift',
628 getfield(sl, 'shift') - shift_old + v[2](bw,bh,bd) )
632 -- re-calculate dimension of s, if s is created "automatically"
633 if b_dir<dir_node_manual then
634 setfield(s, 'width', info.width(bw,bh,bd))
635 setfield(s, 'height', info.height(bw,bh,bd))
636 setfield(s, 'depth', info.depth(bw,bh,bd))
642 luatexja.direction.set_box_dim = set_box_dim
645 -- \ifydir, \iftdir, \ifddir
647 local cat_lp = luatexbase.catcodetables['latex-package']
648 local getbox = tex.getbox
649 local function dir_conditional(n, mode)
654 local b_dir = get_box_dir(s, dir_yoko)
655 if b_dir<dir_node_auto then
658 local b_dir = get_box_dir(
659 node_next(node_next(node_next(getlist(s)))), dir_yoko)
663 tex.sprint(cat_lp, '\\if' .. tostring(res))
665 luatexja.direction.dir_conditional = dir_conditional
669 local font_vert_table = {} -- key: fontnumber
671 local font_vert_basename = {} -- key: basename
672 local function add_feature_table(tname, src, dest)
673 for i,v in pairs(src) do
674 if type(v.slookups)=='table' then
675 local s = v.slookups[tname]
676 if s and not dest[i] then
683 local function prepare_vert_data(n, id)
684 -- test if already loaded
685 if type(id)=='number' then -- sometimes id is an integer
686 font_vert_table[n] = font_vert_table[id]; return
687 elseif (not id) or font_vert_table[n] then return
689 local fname = id.filename
690 local bname = file.basename(fname)
692 font_vert_table[n] = {}; return
693 elseif font_vert_basename[bname] then
694 font_vert_table[n] = font_vert_basename[bname]; return
697 local a = id.resources.sequences
699 local s = id.shared.rawdata.descriptions
700 for i,v in pairs(a) do
701 if v.features.vert or v.features.vrt2 then
702 add_feature_table(v.subtables[1], s, vtable)
706 font_vert_basename[bname] = vtable
707 font_vert_table[n] = vtable
710 function luatexja.direction.get_vert_glyph(n, chr)
711 local fn = font_vert_table[n]
712 return fn and fn[chr] or chr
714 luatexbase.add_to_callback('luatexja.define_font',
715 function (res, name, size, id)
716 prepare_vert_data(id, res)
718 'prepare_vert_data', 1)
720 local function a (n, dat) font_vert_table[n] = dat end
721 luatexja.rmlgbm.vert_addfunc = a
725 -- PACKED の hbox から文字を取り出す
726 -- luatexja.jfmglue.check_box などで使用
728 local function glyph_from_packed(h)
730 return (getid(b)==id_kern)
731 and node_next(node_next(node_next(node_next(b)))) or b
733 luatexja.direction.glyph_from_packed = glyph_from_packed