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 print('BEGIN POST_LINEBREAK', gc)
179 luatexja.ext_show_node_list(h, ' ', print)
180 -- start 側は ltj-debug.lua に
181 local new_dir, hd = ltjs.list_dir, to_direct(h)
182 for line in traverse_id(id_hlist, hd) do
183 set_attr(line, attr_dir, new_dir)
185 tex_set_attr('global', attr_dir, 0)
186 return to_node(create_dir_whatsit(hd, gc, new_dir))
188 luatexbase.add_to_callback('post_linebreak_filter',
189 create_dir_whatsit_parbox, 'ltj.create_dir_whatsit', 10000)
193 -- dir_node に包む方法を書いたテーブル
196 local get_h =function (w,h,d) return h end
197 local get_d =function (w,h,d) return d end
198 local get_h_d =function (w,h,d) return h+d end
199 local get_h_d_neg =function (w,h,d) return -h-d end
200 local get_h_neg =function (w,h,d) return -h end
201 local get_d_neg =function (w,h,d) return -d end
202 local get_w_half =function (w,h,d) return 0.5*w end
203 local get_w_neg_half =function (w,h,d) return -0.5*w end
204 local get_w_neg =function (w,h,d) return -w end
205 local get_w =function (w,h,d) return w end
206 local zero = function() return 0 end
208 [dir_yoko] = { -- yoko を
209 [dir_tate] = { -- tate 中で組む
214 { 'whatsit', sid_save },
215 { 'rotate', '0 1 -1 0' },
216 { 'kern', get_w_neg_half },
218 { 'kern', get_w_neg_half },
219 { 'whatsit', sid_restore },
222 { 'whatsit', sid_save },
223 { 'rotate', '0 1 -1 0' },
225 { 'box' , get_w_neg },
226 { 'kern', get_h_d_neg},
227 { 'whatsit', sid_restore },
230 [dir_dtou] = { -- dtou 中で組む
235 { 'whatsit', sid_save },
236 { 'rotate', '0 -1 1 0' },
237 { 'kern', get_w_neg },
238 { 'box', get_d_neg },
239 { 'whatsit', sid_restore },
242 { 'whatsit', sid_save },
243 { 'rotate', '0 -1 1 0' },
244 { 'kern', get_h_d_neg },
246 { 'whatsit', sid_restore },
250 [dir_tate] = { -- tate を
251 [dir_yoko] = { -- yoko 中で組む
256 { 'whatsit', sid_save },
257 { 'rotate', '0 -1 1 0' },
258 { 'kern', get_w_neg },
259 { 'box' , get_d_neg },
260 { 'whatsit', sid_restore },
263 { 'whatsit', sid_save },
264 { 'rotate', '0 -1 1 0' },
265 { 'kern', get_h_d_neg },
267 { 'whatsit', sid_restore },
270 [dir_dtou] = { -- dtou 中で組む
275 { 'whatsit', sid_save },
276 { 'rotate', '-1 0 0 -1' },
277 { 'kern', get_w_neg },
279 { 'whatsit', sid_restore },
282 { 'whatsit', sid_save },
283 { 'rotate', '-1 0 0 -1' },
284 { 'kern', get_h_d_neg },
285 { 'box', get_w_neg },
286 { 'whatsit', sid_restore },
290 [dir_dtou] = { -- dtou を
291 [dir_yoko] = { -- yoko 中で組む
296 { 'whatsit', sid_save },
297 { 'rotate', '0 1 -1 0' },
300 { 'kern', get_w_neg },
301 { 'whatsit', sid_restore },
305 { 'whatsit', sid_save },
306 { 'rotate', '0 1 -1 0' },
307 { 'box', get_w_neg },
308 { 'kern', get_h_d_neg },
309 { 'whatsit', sid_restore },
312 [dir_tate] = { -- tate 中で組む
317 { 'whatsit', sid_save },
318 { 'rotate', '-1 0 0 -1' },
319 { 'kern', get_w_neg },
321 { 'whatsit', sid_restore },
324 { 'whatsit', sid_save },
325 { 'rotate', ' -1 0 0 -1' },
326 { 'kern', get_h_d_neg },
327 { 'box', get_w_neg },
328 { 'whatsit', sid_restore },
335 -- b に DIR whatsit があればその内容を attr_dir にうつす (1st ret val)
336 -- 2nd ret val はその DIR whatsit
337 local function get_box_dir(b, default)
338 local dir = has_attr(b, attr_dir) or 0
339 local bh = getlist(b)
342 if bh and getid(bh)==id_whatsit
343 and getsubtype(bh)==sid_user and getfield(bh, 'user_id')==DIR then
346 dir = has_attr(bh, attr_dir)
347 set_attr(b, attr_dir, dir)
348 tex_set_attr('global', attr_dir, 0)
353 return (dir==0 and default or dir), c
356 -- dir_node に包まれている「本来の中身」を取り出し,
358 local function unwrap_dir_node(b, head, box_dir)
359 -- b: dir_node, head: the head of list, box_dir:
360 -- return values are (new head), (next of b), (contents), (dir of contents)
361 local bh = getlist(b)
362 local bc = node_next(node_next(node_next(bh)))
367 nh = insert_before(head, b, bc)
368 nh, nb = node_remove(nh, b)
369 setfield(b, 'next', nil)
370 setfield(b, 'head', nil)
373 local shift_old, b_dir, wh = nil, get_box_dir(bc, 0)
375 Dnode.flush_list(getfield(wh, 'value'))
376 setfield(wh, 'value', nil)
379 local info = dir_node_aux[b_dir][box_dir%dir_node_auto][getid(bc)]
380 for _,v in ipairs(info) do
383 getfield(bc,'width'), getfield(bc, 'height'), getfield(bc, 'depth'))
387 setfield(bc, 'shift', getfield(bc, 'shift') - shift_old)
388 return nh, nb, bc, b_dir
391 -- is_manual: 寸法変更に伴うものか?
392 local function create_dir_node(b, b_dir, new_dir, is_manual)
393 local info = dir_node_aux[b_dir][new_dir]
394 local w = getfield(b, 'width')
395 local h = getfield(b, 'height')
396 local d = getfield(b, 'depth')
397 local db = node_new(getid(b))
398 set_attr(db, attr_dir,
399 new_dir + (is_manual and dir_node_manual or dir_node_auto))
400 tex_set_attr('global', attr_dir, 0)
401 set_attr(db, attr_icflag, PROCESSED)
402 set_attr(b, attr_icflag, PROCESSED)
403 setfield(db, 'dir', getfield(b, 'dir'))
404 setfield(db, 'shift', 0)
405 setfield(db, 'width', info.width(w,h,d))
406 setfield(db, 'height', info.height(w,h,d))
407 setfield(db, 'depth', info.depth(w,h,d))
412 local make_dir_whatsit
414 make_dir_whatsit = function (head, b, new_dir, origin)
415 -- head: list head, b: box
416 -- origin: コール元 (for debug)
417 -- return value: (new head), (next of b), (new b), (is_b_dir_node)
418 -- (new b): b か dir_node に被せられた b
419 local bh = getlist(b)
420 local box_dir, dn = get_box_dir(b, ltjs.list_dir)
421 -- 既に b の中身にあるwhatsit
422 if box_dir==new_dir then
423 return head, node_next(b), b, false
424 elseif box_dir%dir_node_auto == new_dir then
425 local bc = node_next(node_next(node_next(bh)))
426 local _, dnc = get_box_dir(b, 0)
427 if dnc then -- free all other dir_node
428 Dnode.flush_list(getfield(dnc, 'value'))
429 setfield(dnc, 'value', nil)
431 set_attr(b, attr_dir, box_dir%dir_node_auto + dir_node_auto)
432 return head, node_next(b), b, true
434 local nh, nb, ret, flag
435 if box_dir>= dir_node_auto then -- unwrap
437 head, nb, b, b_dir = unwrap_dir_node(b, head, box_dir)
439 if b_dir==new_dir then -- no need to create new dir_node
440 return head, nb, b, false
441 else box_dir = b_dir end
444 local dnh = getfield(dn, 'value')
445 for x in traverse(dnh) do
446 if has_attr(x, attr_dir)%dir_node_auto == new_dir then
447 setfield(dn, 'value', to_node(node_remove(dnh, x)))
451 Dnode.flush_list(dnh)
452 db = db or create_dir_node(b, box_dir, new_dir, false)
453 local w = getfield(b, 'width')
454 local h = getfield(b, 'height')
455 local d = getfield(b, 'depth')
456 nh, nb = insert_before(head, b, db), nil
457 nh, nb = node_remove(nh, b)
458 local db_head, db_tail = nil
459 for _,v in ipairs(dir_node_aux[box_dir][new_dir][getid(b)]) do
460 local cmd, arg, nn = v[1], v[2]
462 nn = node_new(id_kern)
463 setfield(nn, 'kern', arg(w, h, d))
464 elseif cmd=='whatsit' then
465 nn = node_new(id_whatsit, arg)
466 elseif cmd=='rotate' then
467 nn = node_new(id_whatsit, sid_matrix)
468 setfield(nn, 'data', arg)
469 elseif cmd=='box' then
470 nn = b; setfield(b, 'next', nil)
471 setfield(nn, 'shift', getfield(nn, 'shift') + arg(w,h,d))
474 insert_after(db_head, db_tail, nn)
477 db_head, db_tail = nn, nn
480 setfield(db, 'head', db_head)
483 return nh, nb, ret, flag
486 local function process_dir_node(head, gc)
487 start_time_measure('direction_vpack')
488 local h = to_direct(head)
489 local x, new_dir = h, ltjs.list_dir or dir_yoko
492 if (xid==id_hlist and has_attr(x, attr_icflag)%PROCESSED_BEGIN_FLAG~=PACKED)
493 or xid==id_vlist then
494 h, x = make_dir_whatsit(h, x, new_dir, 'process_dir_node:' .. gc)
499 stop_time_measure('direction_vpack')
502 luatexja.direction.make_dir_whatsit = make_dir_whatsit
503 luatexbase.add_to_callback('vpack_filter',
504 process_dir_node, 'ltj.dir_whatsit', 10001)
507 -- \wd, \ht, \dp の代わり
509 local getbox, setdimen = tex.getbox, tex.setdimen
510 local function get_box_dim_common(key, s, l_dir)
511 local s_dir, wh = get_box_dir(s, dir_yoko)
512 if s_dir ~= l_dir then
513 local not_found = true
514 for x in traverse(getfield(wh, 'value')) do
515 if l_dir == has_attr(x, attr_dir)%dir_node_auto then
516 setdimen('ltj@tempdima', getfield(x, key))
517 not_found = false; break
521 local w = getfield(s, 'width')
522 local h = getfield(s, 'height')
523 local d = getfield(s, 'depth')
524 setdimen('ltj@tempdima',
525 dir_node_aux[s_dir][l_dir][key](w,h,d))
528 setdimen('ltj@tempdima', getfield(s, key))
531 local function get_box_dim(key, n)
532 local gt = tex.globaldefs; tex.globaldefs = 0
535 local l_dir = get_dir_count()
537 local b_dir = has_attr(s, attr_dir) or 0
538 if b_dir<dir_node_auto then
539 get_box_dim_common(key, s, l_dir)
540 elseif b_dir%dir_node_auto==l_dir then
541 setdimen('ltj@tempdima', getfield(s, key))
543 get_box_dim_common(key,
544 node_next(node_next(node_next(getlist(s)))), l_dir)
547 setdimen('ltj@tempdima', 0)
551 luatexja.direction.get_box_dim = get_box_dim
553 -- return value: (changed dimen of box itself?)
554 local function set_box_dim_common(key, s, l_dir)
555 local s_dir, wh = get_box_dir(s, dir_yoko)
556 if s_dir ~= l_dir then
558 wh = create_dir_whatsit(getlist(s), 'set_box_dim', s_dir)
559 setfield(s, 'head', wh)
562 local dnh = getfield(wh, 'value')
563 for x in traverse(dnh) do
564 if has_attr(x, attr_dir)%dir_node_auto==l_dir then
569 db = create_dir_node(s, s_dir, l_dir, true)
570 setfield(db, 'next', dnh)
571 setfield(wh, 'value',to_node(db))
573 setfield(db, key, tex.getdimen('ltj@tempdima'))
576 setfield(s, key, tex.getdimen('ltj@tempdima'))
578 -- change dimension of dir_nodes which are created "automatically"
580 = getfield(s,'width'), getfield(s, 'height'), getfield(s, 'depth')
581 for x in traverse(getfield(wh, 'value')) do
582 local x_dir = has_attr(x, attr_dir)
583 if x_dir<dir_node_manual then
584 local info = dir_node_aux[s_dir][x_dir%dir_node_auto]
585 setfield(x, 'width', info.width(bw,bh,bd))
586 setfield(x, 'height', info.height(bw,bh,bd))
587 setfield(x, 'depth', info.depth(bw,bh,bd))
594 local function set_box_dim(key)
595 local n = tex_getcount('ltj@tempcnta')
598 local l_dir = get_dir_count()
600 local b_dir = has_attr(s, attr_dir) or 0
601 if b_dir<dir_node_auto then
602 set_box_dim_common(key, s, l_dir)
603 elseif b_dir%dir_node_auto == l_dir then
604 setfield(s, key, tex.getdimen('ltj@tempdima'))
605 if b_dir<dir_node_manual then
606 set_attr(s, attr_dir, b_dir%dir_node_auto + dir_node_manual)
609 local sid, sl = getid(s), getlist(s)
610 local b = node_next(node_next(node_next(sl)))
611 local info = dir_node_aux[get_box_dir(b)][b_dir%dir_node_auto]
613 for _,v in ipairs(info[sid]) do
616 getfield(b,'width'), getfield(b, 'height'), getfield(b, 'depth'))
620 if set_box_dim_common(key, b, l_dir) then
622 = getfield(b,'width'), getfield(b, 'height'), getfield(b, 'depth')
623 -- re-calculate shift
624 for i,v in ipairs(info[sid]) do
625 if getid(sl)==id_kern then
626 setfield(sl, 'kern', v[2](bw,bh,bd) )
627 elseif getid(sl)==sid then
628 local d = getfield(sl, 'shift')
629 setfield(sl, 'shift',
630 getfield(sl, 'shift') - shift_old + v[2](bw,bh,bd) )
634 -- re-calculate dimension of s, if s is created "automatically"
635 if b_dir<dir_node_manual then
636 setfield(s, 'width', info.width(bw,bh,bd))
637 setfield(s, 'height', info.height(bw,bh,bd))
638 setfield(s, 'depth', info.depth(bw,bh,bd))
644 luatexja.direction.set_box_dim = set_box_dim
647 -- \ifydir, \iftdir, \ifddir
649 local cat_lp = luatexbase.catcodetables['latex-package']
650 local getbox = tex.getbox
651 local function dir_conditional(n, mode)
656 local b_dir = get_box_dir(s, dir_yoko)
657 if b_dir<dir_node_auto then
660 local b_dir = get_box_dir(
661 node_next(node_next(node_next(getlist(s)))), dir_yoko)
665 tex.sprint(cat_lp, '\\if' .. tostring(res))
667 luatexja.direction.dir_conditional = dir_conditional
671 local font_vert_table = {} -- key: fontnumber
673 local font_vert_basename = {} -- key: basename
674 local function add_feature_table(tname, src, dest)
675 for i,v in pairs(src) do
676 if type(v.slookups)=='table' then
677 local s = v.slookups[tname]
678 if s and not dest[i] then
685 local function prepare_vert_data(n, id)
686 -- test if already loaded
687 if type(id)=='number' then -- sometimes id is an integer
688 font_vert_table[n] = font_vert_table[id]; return
689 elseif (not id) or font_vert_table[n] then return
691 local fname = id.filename
692 local bname = file.basename(fname)
694 font_vert_table[n] = {}; return
695 elseif font_vert_basename[bname] then
696 font_vert_table[n] = font_vert_basename[bname]; return
699 local a = id.resources.sequences
701 local s = id.shared.rawdata.descriptions
702 for i,v in pairs(a) do
703 if v.features.vert or v.features.vrt2 then
704 add_feature_table(v.subtables[1], s, vtable)
708 font_vert_basename[bname] = vtable
709 font_vert_table[n] = vtable
712 function luatexja.direction.get_vert_glyph(n, chr)
713 local fn = font_vert_table[n]
714 return fn and fn[chr] or chr
716 luatexbase.add_to_callback('luatexja.define_font',
717 function (res, name, size, id)
718 prepare_vert_data(id, res)
720 'prepare_vert_data', 1)
722 local function a (n, dat) font_vert_table[n] = dat end
723 luatexja.rmlgbm.vert_addfunc = a
727 -- PACKED の hbox から文字を取り出す
728 -- luatexja.jfmglue.check_box などで使用
730 local function glyph_from_packed(h)
732 return (getid(b)==id_kern)
733 and node_next(node_next(node_next(node_next(b)))) or b
735 luatexja.direction.glyph_from_packed = glyph_from_packed