OSDN Git Service

added several tex.setattribute('global', attr_dir, 0), because node.set_attribute...
[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       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)
184       end
185       tex_set_attr('global', attr_dir, 0)
186       return to_node(create_dir_whatsit(hd, gc, new_dir))
187    end
188    luatexbase.add_to_callback('post_linebreak_filter', 
189                               create_dir_whatsit_parbox, 'ltj.create_dir_whatsit', 10000)
190
191 end
192
193 -- dir_node に包む方法を書いたテーブル
194 local dir_node_aux
195 do
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
207    dir_node_aux = {
208       [dir_yoko] = { -- yoko を 
209          [dir_tate] = { -- tate 中で組む
210             width  = get_h_d,
211             height = get_w_half,
212             depth  = get_w_half,
213             [id_hlist] = {
214                { 'whatsit', sid_save },
215                { 'rotate', '0 1 -1 0' },
216                { 'kern', get_w_neg_half },
217                { 'box' , get_h },
218                { 'kern', get_w_neg_half },
219                { 'whatsit', sid_restore },
220             },
221             [id_vlist] = {
222                { 'whatsit', sid_save },
223                { 'rotate', '0 1 -1 0' },
224                { 'kern' , zero },
225                { 'box' , get_w_neg },
226                { 'kern', get_h_d_neg},
227                { 'whatsit', sid_restore },
228             },
229          },
230          [dir_dtou] = { -- dtou 中で組む
231             width  = get_h_d,
232             height = get_w,
233             depth  = zero,
234             [id_hlist] = {
235                { 'whatsit', sid_save },
236                { 'rotate', '0 -1 1 0' },
237                { 'kern', get_w_neg },
238                { 'box', get_d_neg },
239                { 'whatsit', sid_restore },
240             },
241             [id_vlist] = {
242                { 'whatsit', sid_save },
243                { 'rotate', '0 -1 1 0' },
244                { 'kern', get_h_d_neg },
245                { 'box', zero },
246                { 'whatsit', sid_restore },
247             },
248          },
249       },
250       [dir_tate] = { -- tate を
251          [dir_yoko] = { -- yoko 中で組む
252             width  = get_h_d,
253             height = get_w,
254             depth  = zero,
255             [id_hlist] = {
256                { 'whatsit', sid_save },
257                { 'rotate', '0 -1 1 0' },
258                { 'kern', get_w_neg },
259                { 'box' , get_d_neg },
260                { 'whatsit', sid_restore },
261             },
262             [id_vlist] = {
263                { 'whatsit', sid_save },
264                { 'rotate', '0 -1 1 0' },
265                { 'kern', get_h_d_neg },
266                { 'box', zero },
267                { 'whatsit', sid_restore },
268             },
269          },
270          [dir_dtou] = { -- dtou 中で組む
271             width  = get_w,
272             height = get_d,
273             depth  = get_h,
274             [id_hlist] = {
275                { 'whatsit', sid_save },
276                { 'rotate', '-1 0 0 -1' },
277                { 'kern', get_w_neg },
278                { 'box', zero },
279                { 'whatsit', sid_restore },
280             },
281             [id_vlist] = {
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 },
287             },
288          },
289       },
290       [dir_dtou] = { -- dtou を
291          [dir_yoko] = { -- yoko 中で組む
292             width  = get_h_d,
293             height = get_w, 
294             depth  = zero,
295             [id_hlist] = {
296                { 'whatsit', sid_save },
297                { 'rotate', '0 1 -1 0' },
298                { 'kern', zero },
299                { 'box', get_h },
300                { 'kern', get_w_neg },
301                { 'whatsit', sid_restore },
302             },
303             [id_vlist] = {
304                { 'kern', zero },
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 },
310             },
311          },
312          [dir_tate] = { -- tate 中で組む
313             width  = get_w,
314             height = get_d,
315             depth  = get_h,
316             [id_hlist] = {
317                { 'whatsit', sid_save },
318                { 'rotate', '-1 0 0 -1' },
319                { 'kern', get_w_neg },
320                { 'box', zero },
321                { 'whatsit', sid_restore },
322             },
323             [id_vlist] = {
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 },
329             },
330          },
331       },
332    }
333 end
334
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)
340    local c
341    for i=1,2 do
342       if bh and getid(bh)==id_whatsit
343       and getsubtype(bh)==sid_user and getfield(bh, 'user_id')==DIR then
344          c = bh
345          if dir==0 then
346             dir = has_attr(bh, attr_dir)
347             set_attr(b, attr_dir, dir)
348             tex_set_attr('global', attr_dir, 0)
349          end
350       end
351       bh = node_next(bh)
352    end
353    return (dir==0 and default or dir), c
354 end
355
356 -- dir_node に包まれている「本来の中身」を取り出し,
357 -- 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)))
363    local nh, nb
364    node_remove(bh, bc); 
365    Dnode.flush_list(bh)
366    if head then
367       nh = insert_before(head, b, bc)
368       nh, nb = node_remove(nh, b)
369       setfield(b, 'next', nil)
370       setfield(b, 'head', nil)
371       node_free(b)
372    end
373    local shift_old, b_dir, wh = nil, get_box_dir(bc, 0)
374    if wh then
375       Dnode.flush_list(getfield(wh, 'value'))
376       setfield(wh, 'value', nil)
377    end
378    -- recalc. info
379    local info = dir_node_aux[b_dir][box_dir%dir_node_auto][getid(bc)]
380    for _,v in ipairs(info) do 
381       if v[1]=='box' then
382          shift_old = v[2](
383             getfield(bc,'width'), getfield(bc, 'height'), getfield(bc, 'depth'))
384          break
385       end
386    end
387    setfield(bc, 'shift', getfield(bc, 'shift') - shift_old)
388    return nh, nb, bc, b_dir
389 end
390
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))
408    return db
409 end
410
411 -- 異方向のボックスの処理
412 local make_dir_whatsit
413 do
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)
430          end
431          set_attr(b, attr_dir, box_dir%dir_node_auto + dir_node_auto)
432          return head, node_next(b), b, true
433       else
434          local nh, nb, ret, flag
435          if box_dir>= dir_node_auto then -- unwrap
436             local b_dir
437             head, nb, b, b_dir = unwrap_dir_node(b, head, box_dir)
438             bh = getlist(b)
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
442          end
443             local db
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)))
448                   db=x; break
449                end
450             end
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]
461                if cmd=='kern' then
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))
472                end
473                if db_head then
474                   insert_after(db_head, db_tail, nn)
475                   db_tail = nn
476                else
477                   db_head, db_tail = nn, nn
478                end
479             --end
480             setfield(db, 'head', db_head)
481             ret, flag = db, true
482          end
483          return nh, nb, ret, flag
484       end
485    end
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
490       while x do
491          local xid = getid(x)
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)
495          else
496             x = node_next(x)
497          end
498       end
499       stop_time_measure('direction_vpack')
500       return to_node(h)
501    end
502    luatexja.direction.make_dir_whatsit = make_dir_whatsit
503    luatexbase.add_to_callback('vpack_filter',
504                               process_dir_node, 'ltj.dir_whatsit', 10001)
505 end
506
507 -- \wd, \ht, \dp の代わり
508 do
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
518             end
519          end
520          if not_found then
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))
526          end
527       else
528          setdimen('ltj@tempdima', getfield(s, key))
529       end
530    end
531    local function get_box_dim(key, n)
532       local gt = tex.globaldefs; tex.globaldefs = 0
533       local s = getbox(n)
534       if s then
535          local l_dir = get_dir_count()
536          s = to_direct(s)
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))
542          else
543             get_box_dim_common(key, 
544                                node_next(node_next(node_next(getlist(s)))), l_dir)
545          end
546       else
547          setdimen('ltj@tempdima', 0)
548       end
549       tex.globaldefs = gt
550    end
551    luatexja.direction.get_box_dim = get_box_dim
552
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
557          if not wh then
558             wh = create_dir_whatsit(getlist(s), 'set_box_dim', s_dir)
559             setfield(s, 'head', wh)
560          end
561          local db
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
565                db = x; break
566             end
567          end
568          if not db then
569             db = create_dir_node(s, s_dir, l_dir, true)
570             setfield(db, 'next', dnh)
571             setfield(wh, 'value',to_node(db))
572          end
573          setfield(db, key, tex.getdimen('ltj@tempdima'))
574          return false
575       else
576          setfield(s, key, tex.getdimen('ltj@tempdima'))
577          if wh then
578             -- change dimension of dir_nodes which are created "automatically"
579                local bw, bh, bd 
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))
588                end
589             end
590          end
591          return true
592       end
593    end
594    local function set_box_dim(key)
595       local n = tex_getcount('ltj@tempcnta')
596       local s = getbox(n)
597       if s then
598          local l_dir = get_dir_count()
599          s = to_direct(s)
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)
607             end
608          else
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]
612             local shift_old
613             for _,v in ipairs(info[sid]) do 
614                if v[1]=='box' then
615                   shift_old = v[2](
616                      getfield(b,'width'), getfield(b, 'height'), getfield(b, 'depth'))
617                   break
618                end
619             end
620            if set_box_dim_common(key, b, l_dir) then
621                local bw, bh, bd 
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) )
631                   end
632                   sl = node_next(sl)
633                end
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))
639                end
640             end
641          end
642       end
643    end
644    luatexja.direction.set_box_dim = set_box_dim
645 end
646
647 -- \ifydir, \iftdir, \ifddir
648 do
649    local cat_lp = luatexbase.catcodetables['latex-package']
650    local getbox = tex.getbox
651    local function dir_conditional(n, mode)
652       local s = getbox(n)
653       local res = false
654       if s then
655          s = to_direct(s)
656          local b_dir = get_box_dir(s, dir_yoko)
657          if b_dir<dir_node_auto then
658             res = (b_dir==mode)
659          else
660             local b_dir = get_box_dir(
661                node_next(node_next(node_next(getlist(s)))), dir_yoko)
662             res = (b_dir==mode)
663          end
664       end
665       tex.sprint(cat_lp, '\\if' .. tostring(res))
666    end
667    luatexja.direction.dir_conditional = dir_conditional
668 end
669
670 -- 縦書き用字形への変換テーブル
671 local font_vert_table = {} -- key: fontnumber
672 do
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
679                dest[i] = s
680             end
681          end
682       end
683    end
684
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
690       end
691       local fname = id.filename
692       local bname = file.basename(fname)
693       if not fname then
694          font_vert_table[n] = {}; return
695       elseif font_vert_basename[bname] then
696          font_vert_table[n] = font_vert_basename[bname]; return
697       end
698       local vtable = {}
699       local a = id.resources.sequences
700       if a then
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)
705             end
706          end
707       end
708       font_vert_basename[bname] = vtable
709       font_vert_table[n] = vtable
710    end
711    -- 縦書き用字形への変換
712    function luatexja.direction.get_vert_glyph(n, chr)
713       local fn = font_vert_table[n]
714       return fn and fn[chr] or chr
715    end
716    luatexbase.add_to_callback('luatexja.define_font',
717                               function (res, name, size, id)
718                                  prepare_vert_data(id, res)
719                               end,
720                               'prepare_vert_data', 1)
721
722    local function a (n, dat) font_vert_table[n] = dat end
723    luatexja.rmlgbm.vert_addfunc = a
724
725 end
726
727 -- PACKED の hbox から文字を取り出す
728 -- luatexja.jfmglue.check_box などで使用
729 do
730    local function glyph_from_packed(h)
731       local b = getlist(h)
732       return (getid(b)==id_kern) 
733          and node_next(node_next(node_next(node_next(b)))) or b
734    end
735    luatexja.direction.glyph_from_packed = glyph_from_packed
736 end