OSDN Git Service

Redefine \vadjust
[luatex-ja/luatexja.git] / src / ltj-direction.lua
1 --
2 -- src/ltj-direction.lua
3 --
4
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 = {}
9
10 local attr_dir = luatexbase.attributes['ltj@dir']
11 local attr_icflag = luatexbase.attributes['ltj@icflag']
12
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
35
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')
44
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
57
58
59 local get_dir_count
60 -- \tate, \yoko
61 do
62    local abs = math.abs
63    function get_dir_count()
64       return tex_getcount('ltj@dir@count')
65    end
66    luatexja.direction.get_dir_count = get_dir_count 
67
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)
76          end
77       else
78          local w = node_next(to_direct(lv.head))
79          if to_node(w) then
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             else
84               ltjb.package_error(
85                  'luatexja',
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.')
89             end
90          else
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)
96             Dnode.write(w)
97          end
98          tex_set_attr('global', attr_icflag, 0)
99       end
100       tex_set_attr('global', attr_dir, 0)
101    end
102    luatexja.direction.set_list_direction = set_list_direction
103 end
104
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)
116       return w
117 end
118
119 -- hpack_filter, vpack_filter, post_line_break_filter
120 -- の結果を組方向を明示するため,先頭に dir_node を設置
121 do
122    local function create_dir_whatsit_hpack(h, gc)
123       local hd = to_direct(h)
124       if gc=='fin_row' or gc == 'preamble'  then
125          if hd  then
126             set_attr(hd, attr_icflag, PROCESSED_BEGIN_FLAG)
127             tex_set_attr('global', attr_icflag, 0)
128          end
129          return h
130       else
131          return to_node(create_dir_whatsit(hd, gc, ltjs.list_dir))
132       end
133    end
134
135    luatexbase.add_to_callback('hpack_filter', 
136                               create_dir_whatsit_hpack, 'ltj.create_dir_whatsit', 10000)
137
138    local wh = {}
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
146             wh[#wh+1]=x
147          end
148       end
149       if hd==wh[1] 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)
154          end
155       end
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
158          if hd  then
159             set_attr(hd, attr_icflag, PROCESSED_BEGIN_FLAG)
160             tex_set_attr('global', attr_icflag, 0)
161          end
162          return to_node(hd)
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)
168          return to_node(hd)
169       else
170          return to_node(create_dir_whatsit(hd, gc, ltjs.list_dir))
171       end
172    end
173    luatexbase.add_to_callback('vpack_filter', 
174                               create_dir_whatsit_vbox, 'ltj.create_dir_whatsit', 1)
175
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)
182       end
183       tex_set_attr('global', attr_dir, 0)
184       return to_node(create_dir_whatsit(hd, gc, new_dir))
185    end
186    luatexbase.add_to_callback('post_linebreak_filter', 
187                               create_dir_whatsit_parbox, 'ltj.create_dir_whatsit', 10000)
188
189 end
190
191 -- dir_node に包む方法を書いたテーブル
192 local dir_node_aux
193 do
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
205    dir_node_aux = {
206       [dir_yoko] = { -- yoko を 
207          [dir_tate] = { -- tate 中で組む
208             width  = get_h_d,
209             height = get_w_half,
210             depth  = get_w_half,
211             [id_hlist] = {
212                { 'whatsit', sid_save },
213                { 'rotate', '0 1 -1 0' },
214                { 'kern', get_w_neg_half },
215                { 'box' , get_h },
216                { 'kern', get_w_neg_half },
217                { 'whatsit', sid_restore },
218             },
219             [id_vlist] = {
220                { 'whatsit', sid_save },
221                { 'rotate', '0 1 -1 0' },
222                { 'kern' , zero },
223                { 'box' , get_w_neg },
224                { 'kern', get_h_d_neg},
225                { 'whatsit', sid_restore },
226             },
227          },
228          [dir_dtou] = { -- dtou 中で組む
229             width  = get_h_d,
230             height = get_w,
231             depth  = zero,
232             [id_hlist] = {
233                { 'whatsit', sid_save },
234                { 'rotate', '0 -1 1 0' },
235                { 'kern', get_w_neg },
236                { 'box', get_d_neg },
237                { 'whatsit', sid_restore },
238             },
239             [id_vlist] = {
240                { 'whatsit', sid_save },
241                { 'rotate', '0 -1 1 0' },
242                { 'kern', get_h_d_neg },
243                { 'box', zero },
244                { 'whatsit', sid_restore },
245             },
246          },
247       },
248       [dir_tate] = { -- tate を
249          [dir_yoko] = { -- yoko 中で組む
250             width  = get_h_d,
251             height = get_w,
252             depth  = zero,
253             [id_hlist] = {
254                { 'whatsit', sid_save },
255                { 'rotate', '0 -1 1 0' },
256                { 'kern', get_w_neg },
257                { 'box' , get_d_neg },
258                { 'whatsit', sid_restore },
259             },
260             [id_vlist] = {
261                { 'whatsit', sid_save },
262                { 'rotate', '0 -1 1 0' },
263                { 'kern', get_h_d_neg },
264                { 'box', zero },
265                { 'whatsit', sid_restore },
266             },
267          },
268          [dir_dtou] = { -- dtou 中で組む
269             width  = get_w,
270             height = get_d,
271             depth  = get_h,
272             [id_hlist] = {
273                { 'whatsit', sid_save },
274                { 'rotate', '-1 0 0 -1' },
275                { 'kern', get_w_neg },
276                { 'box', zero },
277                { 'whatsit', sid_restore },
278             },
279             [id_vlist] = {
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 },
285             },
286          },
287       },
288       [dir_dtou] = { -- dtou を
289          [dir_yoko] = { -- yoko 中で組む
290             width  = get_h_d,
291             height = get_w, 
292             depth  = zero,
293             [id_hlist] = {
294                { 'whatsit', sid_save },
295                { 'rotate', '0 1 -1 0' },
296                { 'kern', zero },
297                { 'box', get_h },
298                { 'kern', get_w_neg },
299                { 'whatsit', sid_restore },
300             },
301             [id_vlist] = {
302                { 'kern', zero },
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 },
308             },
309          },
310          [dir_tate] = { -- tate 中で組む
311             width  = get_w,
312             height = get_d,
313             depth  = get_h,
314             [id_hlist] = {
315                { 'whatsit', sid_save },
316                { 'rotate', '-1 0 0 -1' },
317                { 'kern', get_w_neg },
318                { 'box', zero },
319                { 'whatsit', sid_restore },
320             },
321             [id_vlist] = {
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 },
327             },
328          },
329       },
330    }
331 end
332
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)
338    local c
339    for i=1,2 do
340       if bh and getid(bh)==id_whatsit
341       and getsubtype(bh)==sid_user and getfield(bh, 'user_id')==DIR then
342          c = bh
343          if dir==0 then
344             dir = has_attr(bh, attr_dir)
345             set_attr(b, attr_dir, dir)
346             tex_set_attr('global', attr_dir, 0)
347          end
348       end
349       bh = node_next(bh)
350    end
351    return (dir==0 and default or dir), c
352 end
353
354 -- dir_node に包まれている「本来の中身」を取り出し,
355 -- 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)))
361    local nh, nb
362    node_remove(bh, bc); 
363    Dnode.flush_list(bh)
364    if head then
365       nh = insert_before(head, b, bc)
366       nh, nb = node_remove(nh, b)
367       setfield(b, 'next', nil)
368       setfield(b, 'head', nil)
369       node_free(b)
370    end
371    local shift_old, b_dir, wh = nil, get_box_dir(bc, 0)
372    if wh then
373       Dnode.flush_list(getfield(wh, 'value'))
374       setfield(wh, 'value', nil)
375    end
376    -- recalc. info
377    local info = dir_node_aux[b_dir][box_dir%dir_node_auto][getid(bc)]
378    for _,v in ipairs(info) do 
379       if v[1]=='box' then
380          shift_old = v[2](
381             getfield(bc,'width'), getfield(bc, 'height'), getfield(bc, 'depth'))
382          break
383       end
384    end
385    setfield(bc, 'shift', getfield(bc, 'shift') - shift_old)
386    return nh, nb, bc, b_dir
387 end
388
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))
406    return db
407 end
408
409 -- 異方向のボックスの処理
410 local make_dir_whatsit
411 do
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)
428          end
429          set_attr(b, attr_dir, box_dir%dir_node_auto + dir_node_auto)
430          return head, node_next(b), b, true
431       else
432          local nh, nb, ret, flag
433          if box_dir>= dir_node_auto then -- unwrap
434             local b_dir
435             head, nb, b, b_dir = unwrap_dir_node(b, head, box_dir)
436             bh = getlist(b)
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
440          end
441             local db
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)))
446                   db=x; break
447                end
448             end
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]
459                if cmd=='kern' then
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))
470                end
471                if db_head then
472                   insert_after(db_head, db_tail, nn)
473                   db_tail = nn
474                else
475                   db_head, db_tail = nn, nn
476                end
477             --end
478             setfield(db, 'head', db_head)
479             ret, flag = db, true
480          end
481          return nh, nb, ret, flag
482       end
483    end
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
488       while x do
489          local xid = getid(x)
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)
493          else
494             x = node_next(x)
495          end
496       end
497       stop_time_measure('direction_vpack')
498       return to_node(h)
499    end
500    luatexja.direction.make_dir_whatsit = make_dir_whatsit
501    luatexbase.add_to_callback('vpack_filter',
502                               process_dir_node, 'ltj.dir_whatsit', 10001)
503 end
504
505 -- \wd, \ht, \dp の代わり
506 do
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
516             end
517          end
518          if not_found then
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))
524          end
525       else
526          setdimen('ltj@tempdima', getfield(s, key))
527       end
528    end
529    local function get_box_dim(key, n)
530       local gt = tex.globaldefs; tex.globaldefs = 0
531       local s = getbox(n)
532       if s then
533          local l_dir = get_dir_count()
534          s = to_direct(s)
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))
540          else
541             get_box_dim_common(key, 
542                                node_next(node_next(node_next(getlist(s)))), l_dir)
543          end
544       else
545          setdimen('ltj@tempdima', 0)
546       end
547       tex.globaldefs = gt
548    end
549    luatexja.direction.get_box_dim = get_box_dim
550
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
555          if not wh then
556             wh = create_dir_whatsit(getlist(s), 'set_box_dim', s_dir)
557             setfield(s, 'head', wh)
558          end
559          local db
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
563                db = x; break
564             end
565          end
566          if not db then
567             db = create_dir_node(s, s_dir, l_dir, true)
568             setfield(db, 'next', dnh)
569             setfield(wh, 'value',to_node(db))
570          end
571          setfield(db, key, tex.getdimen('ltj@tempdima'))
572          return false
573       else
574          setfield(s, key, tex.getdimen('ltj@tempdima'))
575          if wh then
576             -- change dimension of dir_nodes which are created "automatically"
577                local bw, bh, bd 
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))
586                end
587             end
588          end
589          return true
590       end
591    end
592    local function set_box_dim(key)
593       local n = tex_getcount('ltj@tempcnta')
594       local s = getbox(n)
595       if s then
596          local l_dir = get_dir_count()
597          s = to_direct(s)
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)
605             end
606          else
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]
610             local shift_old
611             for _,v in ipairs(info[sid]) do 
612                if v[1]=='box' then
613                   shift_old = v[2](
614                      getfield(b,'width'), getfield(b, 'height'), getfield(b, 'depth'))
615                   break
616                end
617             end
618            if set_box_dim_common(key, b, l_dir) then
619                local bw, bh, bd 
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) )
629                   end
630                   sl = node_next(sl)
631                end
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))
637                end
638             end
639          end
640       end
641    end
642    luatexja.direction.set_box_dim = set_box_dim
643 end
644
645 -- \ifydir, \iftdir, \ifddir
646 do
647    local cat_lp = luatexbase.catcodetables['latex-package']
648    local getbox = tex.getbox
649    local function dir_conditional(n, mode)
650       local s = getbox(n)
651       local res = false
652       if s then
653          s = to_direct(s)
654          local b_dir = get_box_dir(s, dir_yoko)
655          if b_dir<dir_node_auto then
656             res = (b_dir==mode)
657          else
658             local b_dir = get_box_dir(
659                node_next(node_next(node_next(getlist(s)))), dir_yoko)
660             res = (b_dir==mode)
661          end
662       end
663       tex.sprint(cat_lp, '\\if' .. tostring(res))
664    end
665    luatexja.direction.dir_conditional = dir_conditional
666 end
667
668 -- 縦書き用字形への変換テーブル
669 local font_vert_table = {} -- key: fontnumber
670 do
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
677                dest[i] = s
678             end
679          end
680       end
681    end
682
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
688       end
689       local fname = id.filename
690       local bname = file.basename(fname)
691       if not fname then
692          font_vert_table[n] = {}; return
693       elseif font_vert_basename[bname] then
694          font_vert_table[n] = font_vert_basename[bname]; return
695       end
696       local vtable = {}
697       local a = id.resources.sequences
698       if a then
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)
703             end
704          end
705       end
706       font_vert_basename[bname] = vtable
707       font_vert_table[n] = vtable
708    end
709    -- 縦書き用字形への変換
710    function luatexja.direction.get_vert_glyph(n, chr)
711       local fn = font_vert_table[n]
712       return fn and fn[chr] or chr
713    end
714    luatexbase.add_to_callback('luatexja.define_font',
715                               function (res, name, size, id)
716                                  prepare_vert_data(id, res)
717                               end,
718                               'prepare_vert_data', 1)
719
720    local function a (n, dat) font_vert_table[n] = dat end
721    luatexja.rmlgbm.vert_addfunc = a
722
723 end
724
725 -- PACKED の hbox から文字を取り出す
726 -- luatexja.jfmglue.check_box などで使用
727 do
728    local function glyph_from_packed(h)
729       local b = getlist(h)
730       return (getid(b)==id_kern) 
731          and node_next(node_next(node_next(node_next(b)))) or b
732    end
733    luatexja.direction.glyph_from_packed = glyph_from_packed
734 end