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
60 local function get_dir_count()
61 return tex_getcount('ltj@dir@count')
63 luatexja.direction.get_dir_count = get_dir_count
67 local node_next = node.next
68 local node_set_attr = node.set_attribute
69 local function set_list_direction(v, name)
70 local lv, w = tex.nest[tex.nest.ptr], tex.lists.page_head
71 if lv.mode == 1 and w then
72 if w.id==id_whatsit and w.subtype==sid_user
73 and w.user_id==DIR then
74 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)
83 tex_set_attr('global', attr_dir, 0)
87 "Use `\\" .. name .. "' at top of list",
88 'Direction change command by LuaTeX-ja is available\n'
89 .. 'only when the current list is null.')
92 local w = node_new(id_whatsit, sid_user)
93 setfield(w, 'next', hd)
94 setfield(w, 'user_id', DIR)
95 setfield(w, 'type', 110)
96 set_attr(w, attr_dir, v)
98 tex_set_attr('global', attr_dir, 0)
100 tex_set_attr('global', attr_icflag, 0)
103 luatexja.direction.set_list_direction = set_list_direction
106 -- ボックスに dir whatsit を追加
107 local function create_dir_whatsit(hd, gc, new_dir)
108 local w = node_new(id_whatsit, sid_user)
109 setfield(w, 'next', hd)
110 setfield(w, 'user_id', DIR)
111 setfield(w, 'type', 110)
112 set_attr(w, attr_dir, new_dir)
113 tex_set_attr('global', attr_dir, 0)
114 set_attr(w, attr_icflag, PROCESSED_BEGIN_FLAG)
115 set_attr(hd, attr_icflag, (has_attr(hd, attr_icflag) or 0) + PROCESSED_BEGIN_FLAG)
116 tex_set_attr('global', attr_icflag, 0)
120 -- hpack_filter, vpack_filter, post_line_break_filter
121 -- の結果を組方向を明示するため,先頭に dir_node を設置
123 local function create_dir_whatsit_hpack(h, gc)
124 local hd = to_direct(h)
125 if gc=='fin_row' or gc == 'preamble' then
127 set_attr(hd, attr_icflag, PROCESSED_BEGIN_FLAG)
128 tex_set_attr('global', attr_icflag, 0)
132 return to_node(create_dir_whatsit(hd, gc, ltjs.list_dir))
136 luatexbase.add_to_callback('hpack_filter',
137 create_dir_whatsit_hpack, 'ltj.create_dir_whatsit', 10000)
139 local function create_dir_whatsit_vbox(h, gc)
140 local hd= to_direct(h)
141 ltjs.list_dir = get_dir_count()
142 if getid(hd)==id_whatsit and getsubtype(hd)==sid_user
143 and getfield(hd, 'user_id')==DIR then
144 ltjs.list_dir = has_attr(hd, attr_dir)
146 elseif gc=='fin_row' or gc == 'preamble' then
148 set_attr(hd, attr_icflag, PROCESSED_BEGIN_FLAG)
149 tex_set_attr('global', attr_icflag, 0)
153 return to_node(create_dir_whatsit(hd, gc, ltjs.list_dir))
156 luatexbase.add_to_callback('vpack_filter',
157 create_dir_whatsit_vbox, 'ltj.create_dir_whatsit', 1)
159 local function create_dir_whatsit_parbox(h, gc)
160 stop_time_measure('tex_linebreak')
161 -- start 側は ltj-debug.lua に
162 local new_dir, hd = ltjs.list_dir, to_direct(h)
163 for line in traverse_id(id_hlist, hd) do
164 set_attr(line, attr_dir, new_dir)
166 return to_node(create_dir_whatsit(hd, gc, new_dir))
168 luatexbase.add_to_callback('post_linebreak_filter',
169 create_dir_whatsit_parbox, 'ltj.create_dir_whatsit', 10000)
173 -- dir_node に包む方法を書いたテーブル
176 local get_h =function (w,h,d) return h end
177 local get_d =function (w,h,d) return d end
178 local get_h_d =function (w,h,d) return h+d end
179 local get_h_d_neg =function (w,h,d) return -h-d end
180 local get_h_neg =function (w,h,d) return -h end
181 local get_d_neg =function (w,h,d) return -d end
182 local get_w_half =function (w,h,d) return 0.5*w end
183 local get_w_neg_half =function (w,h,d) return -0.5*w end
184 local get_w_neg =function (w,h,d) return -w end
185 local get_w =function (w,h,d) return w end
186 local zero = function() return 0 end
188 [dir_yoko] = { -- yoko を
189 [dir_tate] = { -- tate 中で組む
194 { 'whatsit', sid_save },
195 { 'rotate', '0 1 -1 0' },
196 { 'kern', get_w_neg_half },
198 { 'kern', get_w_neg_half },
199 { 'whatsit', sid_restore },
202 { 'whatsit', sid_save },
203 { 'rotate', '0 1 -1 0' },
205 { 'box' , get_w_neg },
206 { 'kern', get_h_d_neg},
207 { 'whatsit', sid_restore },
210 [dir_dtou] = { -- dtou 中で組む
215 { 'whatsit', sid_save },
216 { 'rotate', '0 -1 1 0' },
217 { 'kern', get_w_neg },
218 { 'box', get_d_neg },
219 { 'whatsit', sid_restore },
222 { 'whatsit', sid_save },
223 { 'rotate', '0 -1 1 0' },
224 { 'kern', get_h_d_neg },
226 { 'whatsit', sid_restore },
230 [dir_tate] = { -- tate を
231 [dir_yoko] = { -- yoko 中で組む
236 { 'whatsit', sid_save },
237 { 'rotate', '0 -1 1 0' },
238 { 'kern', get_w_neg },
239 { 'box' , get_d_neg },
240 { 'whatsit', sid_restore },
243 { 'whatsit', sid_save },
244 { 'rotate', '0 -1 1 0' },
245 { 'kern', get_h_d_neg },
247 { 'whatsit', sid_restore },
250 [dir_dtou] = { -- dtou 中で組む
255 { 'whatsit', sid_save },
256 { 'rotate', '-1 0 0 -1' },
257 { 'kern', get_w_neg },
259 { 'whatsit', sid_restore },
262 { 'whatsit', sid_save },
263 { 'rotate', '-1 0 0 -1' },
264 { 'kern', get_h_d_neg },
265 { 'box', get_w_neg },
266 { 'whatsit', sid_restore },
270 [dir_dtou] = { -- dtou を
271 [dir_yoko] = { -- yoko 中で組む
276 { 'whatsit', sid_save },
277 { 'rotate', '0 1 -1 0' },
278 { 'kern', get_w_neg },
280 { 'whatsit', sid_restore },
284 { 'whatsit', sid_save },
285 { 'rotate', '0 1 -1 0' },
287 { 'kern', get_h_d_neg },
288 { 'whatsit', sid_restore },
291 [dir_tate] = { -- tate 中で組む
296 { 'whatsit', sid_save },
297 { 'rotate', '-1 0 0 -1' },
298 { 'kern', get_w_neg },
300 { 'whatsit', sid_restore },
303 { 'whatsit', sid_save },
304 { 'rotate', ' -1 0 0 -1' },
305 { 'kern', get_h_d_neg },
306 { 'box', get_w_neg },
307 { 'whatsit', sid_restore },
314 -- b に DIR whatsit があればその内容を attr_dir にうつす (1st ret val)
315 -- 2nd ret val はその DIR whatsit
316 local function get_box_dir(b, default)
317 local dir = has_attr(b, attr_dir) or 0
318 local bh = getlist(b)
320 if bh and getid(bh)==id_whatsit
321 and getsubtype(bh)==sid_user and getfield(bh, 'user_id')==DIR then
324 dir = has_attr(bh, attr_dir)
325 set_attr(b, attr_dir, dir)
326 tex_set_attr('global', attr_dir, 0)
329 return (dir==0 and default or dir), c
332 -- dir_node に包まれている「本来の中身」を取り出し,
334 local function unwrap_dir_node(b, head)
335 -- head: nil or nil-nil
336 -- if head is non-nil, return values are (new head), (next of b), (contents)
337 local bh = getlist(b)
338 local bc = node_next(node_next(node_next(bh)))
343 nh = insert_before(head, b, bc)
344 nh, nb = node_remove(nh, b)
345 setfield(b, 'next', nil)
346 setfield(b, 'head', nil)
347 luatexja.ext_show_node(to_node(b), ' ', print)
348 --node_free(b) -- TODO: これを入れると test51-vtest が途中で泊まる
350 local d, wh = get_box_dir(bc, 0)
352 Dnode.flush_list(getfield(wh, 'value'))
353 setfield(wh, 'value', nil)
358 -- is_manual: 寸法変更に伴うものか?
359 local function create_dir_node(b, b_dir, new_dir, is_manual)
360 local info = dir_node_aux[b_dir][new_dir]
361 local w = getfield(b, 'width')
362 local h = getfield(b, 'height')
363 local d = getfield(b, 'depth')
364 local db = node_new(getid(b))
365 set_attr(db, attr_dir,
366 new_dir + (is_manual and dir_node_manual or dir_node_auto))
367 set_attr(db, attr_icflag, PROCESSED)
368 set_attr(b, attr_icflag, PROCESSED)
369 setfield(db, 'dir', getfield(b, 'dir'))
370 setfield(db, 'shift', 0)
371 setfield(db, 'width', info.width(w,h,d))
372 setfield(db, 'height', info.height(w,h,d))
373 setfield(db, 'depth', info.depth(w,h,d))
378 local make_dir_whatsit
380 make_dir_whatsit = function (head, b, new_dir, origin)
381 -- head: list head, b: box
382 -- origin: コール元 (for debug)
383 -- return value: (new head), (next of b), (new b), (is_b_dir_node)
384 -- (new b): b か dir_node に被せられた b
385 local bh = getlist(b)
386 local box_dir, dn = get_box_dir(b, ltjs.list_dir)
387 -- 既に b の中身にあるwhatsit
388 if box_dir==new_dir then
389 return head, node_next(b), b, false
390 elseif box_dir%dir_node_auto == new_dir then
391 -- TODO. we have to free all other ...
392 set_attr(b, attr_dir, box_dir%dir_node_auto + dir_node_auto)
393 return head, node_next(b), b, true
395 local nh, nb, ret, flag
396 if box_dir>= dir_node_auto then -- unwrap
397 nh, nb, ret = unwrap_dir_node(b, head)
400 box_dir, dn = get_box_dir(b, ltjs.list_dir)
401 if box_dir==new_dir then
402 return head, nb, b, fals
406 local dnh = getfield(dn, 'value')
407 for x in traverse(dnh) do
408 if has_attr(x, attr_dir)%dir_node_auto == new_dir then
409 setfield(dn, 'value', to_node(node_remove(dnh, x)))
413 db = db or create_dir_node(b, box_dir, new_dir, false)
414 local w = getfield(b, 'width')
415 local h = getfield(b, 'height')
416 local d = getfield(b, 'depth')
417 nh, nb = insert_before(head, b, db), nil
418 nh, nb = node_remove(nh, b)
419 local db_head, db_tail = nil
420 for _,v in ipairs(dir_node_aux[box_dir][new_dir][getid(b)]) do
421 local cmd, arg, nn = v[1], v[2]
423 nn = node_new(id_kern)
424 setfield(nn, 'kern', arg(w, h, d))
425 elseif cmd=='whatsit' then
426 nn = node_new(id_whatsit, arg)
427 elseif cmd=='rotate' then
428 nn = node_new(id_whatsit, sid_matrix)
429 setfield(nn, 'data', arg)
430 elseif cmd=='box' then
431 nn = b; setfield(b, 'next', nil)
432 setfield(nn, 'shift', getfield(nn, 'shift') + arg(w,h,d))
435 insert_after(db_head, db_tail, nn)
438 db_head, db_tail = nn, nn
441 setfield(db, 'head', db_head)
444 return nh, nb, ret, flag
447 local function process_dir_node(head, gc)
448 start_time_measure('direction_vpack')
449 local h = to_direct(head)
450 local x, new_dir = h, ltjs.list_dir or dir_yoko
453 if (xid==id_hlist and has_attr(x, attr_icflag)%PROCESSED_BEGIN_FLAG~=PACKED)
454 or xid==id_vlist then
455 h, x = make_dir_whatsit(h, x, new_dir, 'process_dir_node:' .. gc)
460 stop_time_measure('direction_vpack')
463 luatexja.direction.make_dir_whatsit = make_dir_whatsit
464 luatexbase.add_to_callback('vpack_filter',
465 process_dir_node, 'ltj.dir_whatsit', 10001)
468 -- \wd, \ht, \dp の代わり
470 local getbox, setdimen = tex.getbox, tex.setdimen
471 local function get_box_dim_common(key, s, l_dir)
472 local s_dir, wh = get_box_dir(s, dir_yoko)
473 if s_dir ~= l_dir then
474 local not_found = true
475 for x in traverse(getfield(wh, 'value')) do
476 if l_dir == has_attr(x, attr_dir)%dir_node_auto then
477 setdimen('ltj@tempdima', getfield(x, key))
478 not_found = false; break
482 local w = getfield(s, 'width')
483 local h = getfield(s, 'height')
484 local d = getfield(s, 'depth')
485 setdimen('ltj@tempdima',
486 dir_node_aux[s_dir][l_dir][key](w,h,d))
489 setdimen('ltj@tempdima', getfield(s, key))
492 local function get_box_dim(key, n)
493 local gt = tex.globaldefs; tex.globaldefs = 0
496 local l_dir = get_dir_count()
498 local b_dir = has_attr(s, attr_dir) or 0
499 if b_dir<dir_node_auto then
500 get_box_dim_common(key, s, l_dir)
501 elseif b_dir%dir_node_auto==l_dir then
502 setdimen('ltj@tempdima', getfield(s, key))
506 node_next(node_next(node_next(getlist(s)))),
510 setdimen('ltj@tempdima', 0)
514 luatexja.direction.get_box_dim = get_box_dim
516 -- return value: (changed dimen of box itself?)
517 local function set_box_dim_common(key, s, l_dir)
518 local s_dir, wh = get_box_dir(s, dir_yoko)
519 if s_dir ~= l_dir then
521 wh = create_dir_whatsit(getlist(s), 'set_box_dim', s_dir)
522 setfield(s, 'head', wh)
525 local dnh = getfield(wh, 'value')
526 for x in traverse(dnh) do
527 if has_attr(x, attr_dir)%dir_node_auto==l_dir then
532 db = create_dir_node(s, s_dir, l_dir, true)
533 setfield(db, 'next', dnh)
534 setfield(wh, 'value',to_node(db))
536 setfield(db, key, tex.getdimen('ltj@tempdima'))
539 setfield(s, key, tex.getdimen('ltj@tempdima'))
541 -- change dimension of dir_nodes which are created "automatically"
543 = getfield(s,'width'), getfield(s, 'height'), getfield(s, 'depth')
544 for x in traverse(getfield(wh, 'value')) do
545 local x_dir = has_attr(x, attr_dir)
546 if x_dir<dir_node_manual then
547 local info = dir_node_aux[s_dir][x_dir%dir_node_auto]
548 setfield(x, 'width', info.width(bw,bh,bd))
549 setfield(x, 'height', info.height(bw,bh,bd))
550 setfield(x, 'depth', info.depth(bw,bh,bd))
557 local function set_box_dim(key)
558 local n = tex_getcount('ltj@tempcnta')
561 local l_dir = get_dir_count()
563 local b_dir = has_attr(s, attr_dir) or 0
564 if b_dir<dir_node_auto then
565 set_box_dim_common(key, s, l_dir)
566 elseif b_dir%dir_node_auto == l_dir then
567 setfield(s, key, tex.getdimen('ltj@tempdima'))
568 if b_dir<dir_node_manual then
571 local sid, sl = getid(s), getlist(s)
572 local b = node_next(node_next(node_next(sl)))
573 local info = dir_node_aux[get_box_dir(b)][b_dir%dir_node_auto]
575 for _,v in ipairs(info[sid]) do
578 getfield(b,'width'), getfield(b, 'height'), getfield(b, 'depth'))
582 if set_box_dim_common(key, b, l_dir) then
584 = getfield(b,'width'), getfield(b, 'height'), getfield(b, 'depth')
585 -- re-calculate shifting info
586 for i,v in ipairs(info[sid]) do
587 if getid(sl)==id_kern then
588 setfield(sl, 'kern', v[2](bw,bh,bd) )
589 elseif getid(sl)==sid then
590 local d = getfield(sl, 'shift')
591 setfield(sl, 'shift',
592 getfield(sl, 'shift') - shift_old + v[2](bw,bh,bd) )
596 -- re-calculate dimension of s, if s is created "automatically"
597 if b_dir<dir_node_manual then
598 setfield(s, 'width', info.width(bw,bh,bd))
599 setfield(s, 'height', info.height(bw,bh,bd))
600 setfield(s, 'depth', info.depth(bw,bh,bd))
606 luatexja.direction.set_box_dim = set_box_dim
610 local font_vert_table = {} -- key: fontnumber
612 local font_vert_basename = {} -- key: basename
613 local function add_feature_table(tname, src, dest)
614 for i,v in pairs(src) do
615 if type(v.slookups)=='table' then
616 local s = v.slookups[tname]
617 if s and not dest[i] then
624 local function prepare_vert_data(n, id)
625 -- test if already loaded
626 if type(id)=='number' then -- sometimes id is an integer
627 font_vert_table[n] = font_vert_table[id]; return
628 elseif (not id) or font_vert_table[n] then return
630 local fname = id.filename
631 local bname = file.basename(fname)
633 font_vert_table[n] = {}; return
634 elseif font_vert_basename[bname] then
635 font_vert_table[n] = font_vert_basename[bname]; return
638 local a = id.resources.sequences
640 local s = id.shared.rawdata.descriptions
641 for i,v in pairs(a) do
642 if v.features.vert or v.features.vrt2 then
643 add_feature_table(v.subtables[1], s, vtable)
647 font_vert_basename[bname] = vtable
648 font_vert_table[n] = vtable
651 function luatexja.direction.get_vert_glyph(n, chr)
652 local fn = font_vert_table[n]
653 return fn and fn[chr] or chr
655 luatexbase.add_to_callback('luatexja.define_font',
656 function (res, name, size, id)
657 prepare_vert_data(id, res)
659 'prepare_vert_data', 1)
661 local function a (n, dat) font_vert_table[n] = dat end
662 luatexja.rmlgbm.vert_addfunc = a
666 -- PACKED の hbox から文字を取り出す
667 -- luatexja.jfmglue.check_box などで使用
669 local function glyph_from_packed(h)
671 return (getid(b)==id_kern)
672 and node_next(node_next(node_next(node_next(b)))) or b
674 luatexja.direction.glyph_from_packed = glyph_from_packed